Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | // ========== Copyright Header Begin ========================================== |
2 | // | |
3 | // OpenSPARC T2 Processor File: arg.cc | |
4 | // Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
5 | // DO NOT ALTER OR REMOVE COPYRIGHT NOTICES. | |
6 | // | |
7 | // The above named program is free software; you can redistribute it and/or | |
8 | // modify it under the terms of the GNU General Public | |
9 | // License version 2 as published by the Free Software Foundation. | |
10 | // | |
11 | // The above named program is distributed in the hope that it will be | |
12 | // useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | // General Public License for more details. | |
15 | // | |
16 | // You should have received a copy of the GNU General Public | |
17 | // License along with this work; if not, write to the Free Software | |
18 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. | |
19 | // | |
20 | // ========== Copyright Header End ============================================ | |
21 | // Utility for parsing sysconf args | |
22 | // Copyright 2004 Sun Microsystems Inc | |
23 | // All rights reserved | |
24 | // | |
25 | //@(#)arg.cc 1.3 06/04/18 | |
26 | // | |
27 | #include <sys/types.h> | |
28 | #include <stdio.h> | |
29 | #include <stdlib.h> | |
30 | #include <stdarg.h> | |
31 | #include <strings.h> | |
32 | #include "arg.h" | |
33 | ||
34 | // return str from name=value | |
35 | bool | |
36 | argval(const char *name, const char *arg, const char **value) | |
37 | { | |
38 | bool ans = false; | |
39 | char *tmp = strdup(arg); | |
40 | char *peq = strchr(tmp, '='); | |
41 | if (peq) { | |
42 | *peq = '\0'; | |
43 | ans = !strcmp(name, tmp); | |
44 | *peq = '='; | |
45 | if (ans) { | |
46 | if (*value) | |
47 | free((void *)(*value)); | |
48 | *value = strdup(peq + 1); | |
49 | } | |
50 | } else { | |
51 | ans = !strcmp(name, tmp); | |
52 | if (ans) { | |
53 | if (*value) | |
54 | free((void *)(*value)); | |
55 | *value = strdup(""); | |
56 | } | |
57 | } | |
58 | free(tmp); | |
59 | return ans; | |
60 | } | |
61 | ||
62 | // return bool from name=value | |
63 | bool | |
64 | argval(const char *name, const char *arg, bool *value) | |
65 | { | |
66 | const char *str = NULL; | |
67 | if (argval(name, arg, &str)) { | |
68 | if (!str[0]) | |
69 | *value = true; // missing '=value', treat as a switch | |
70 | else if (strchr("tTyY1", str[0])) // true or yes or 1 | |
71 | *value = true; | |
72 | else if (!strchr("oO", str[0])) // on vs off | |
73 | *value = false; | |
74 | else if (strchr("nN", str[1])) | |
75 | *value = true; | |
76 | else | |
77 | *value = false; | |
78 | free((void*)str); | |
79 | return true; | |
80 | } | |
81 | return false; | |
82 | } | |
83 | ||
84 | // return unsigned long long from name=value | |
85 | bool | |
86 | argval(const char *name, const char *arg, unsigned long long *value) | |
87 | { | |
88 | const char *str = NULL; | |
89 | if (argval(name, arg, &str)) { | |
90 | *value = getull(str); | |
91 | free((void*)str); | |
92 | return true; | |
93 | } | |
94 | return false; | |
95 | } | |
96 | ||
97 | // return int from name=value | |
98 | bool | |
99 | argval(const char *name, const char *arg, int *value) | |
100 | { | |
101 | unsigned long long ull; | |
102 | if (argval(name, arg, &ull)) { | |
103 | *value = (int) ull; | |
104 | return true; | |
105 | } | |
106 | return false; | |
107 | } | |
108 | ||
109 | // return ul from name=value | |
110 | bool | |
111 | argval(const char *name, const char *arg, unsigned long *value) | |
112 | { | |
113 | unsigned long long ull; | |
114 | if (argval(name, arg, &ull)) { | |
115 | *value = (unsigned long) ull; | |
116 | return true; | |
117 | } | |
118 | return false; | |
119 | } | |
120 | ||
121 | // convert decimal, octal or hex | |
122 | int | |
123 | getint(const char *s) | |
124 | { | |
125 | return (int) strtol(s, NULL, 0); | |
126 | } | |
127 | ||
128 | unsigned long long | |
129 | getull(const char *s) | |
130 | { | |
131 | return (unsigned long long) strtoll(s, NULL, 0); | |
132 | } | |
133 | ||
134 | // format a string and return a copy | |
135 | const char *makestr(const char *format, ...) | |
136 | { | |
137 | va_list ap; | |
138 | char buf[10000]; | |
139 | ||
140 | va_start(ap, format); | |
141 | vsnprintf(buf, sizeof buf, format, ap); | |
142 | va_end(ap); | |
143 | return strdup(buf); | |
144 | } | |
145 |