Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / sam / devices / common / arg.cc
// ========== Copyright Header Begin ==========================================
//
// OpenSPARC T2 Processor File: arg.cc
// Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
// DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
//
// The above named program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License version 2 as published by the Free Software Foundation.
//
// The above named program is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this work; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
//
// ========== Copyright Header End ============================================
// Utility for parsing sysconf args
// Copyright 2004 Sun Microsystems Inc
// All rights reserved
//
//@(#)arg.cc 1.3 06/04/18
//
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <strings.h>
#include "arg.h"
// return str from name=value
bool
argval(const char *name, const char *arg, const char **value)
{
bool ans = false;
char *tmp = strdup(arg);
char *peq = strchr(tmp, '=');
if (peq) {
*peq = '\0';
ans = !strcmp(name, tmp);
*peq = '=';
if (ans) {
if (*value)
free((void *)(*value));
*value = strdup(peq + 1);
}
} else {
ans = !strcmp(name, tmp);
if (ans) {
if (*value)
free((void *)(*value));
*value = strdup("");
}
}
free(tmp);
return ans;
}
// return bool from name=value
bool
argval(const char *name, const char *arg, bool *value)
{
const char *str = NULL;
if (argval(name, arg, &str)) {
if (!str[0])
*value = true; // missing '=value', treat as a switch
else if (strchr("tTyY1", str[0])) // true or yes or 1
*value = true;
else if (!strchr("oO", str[0])) // on vs off
*value = false;
else if (strchr("nN", str[1]))
*value = true;
else
*value = false;
free((void*)str);
return true;
}
return false;
}
// return unsigned long long from name=value
bool
argval(const char *name, const char *arg, unsigned long long *value)
{
const char *str = NULL;
if (argval(name, arg, &str)) {
*value = getull(str);
free((void*)str);
return true;
}
return false;
}
// return int from name=value
bool
argval(const char *name, const char *arg, int *value)
{
unsigned long long ull;
if (argval(name, arg, &ull)) {
*value = (int) ull;
return true;
}
return false;
}
// return ul from name=value
bool
argval(const char *name, const char *arg, unsigned long *value)
{
unsigned long long ull;
if (argval(name, arg, &ull)) {
*value = (unsigned long) ull;
return true;
}
return false;
}
// convert decimal, octal or hex
int
getint(const char *s)
{
return (int) strtol(s, NULL, 0);
}
unsigned long long
getull(const char *s)
{
return (unsigned long long) strtoll(s, NULL, 0);
}
// format a string and return a copy
const char *makestr(const char *format, ...)
{
va_list ap;
char buf[10000];
va_start(ap, format);
vsnprintf(buf, sizeof buf, format, ap);
va_end(ap);
return strdup(buf);
}