[HEIMDAL-646] malloc(0) checks for AIX
[heimdal.git] / lib / roken / parse_units.c
bloba848298c57326969f3cfe5a164d3352720df40c4
1 /*
2 * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include <config.h>
36 #include <stdio.h>
37 #include <ctype.h>
38 #include <string.h>
39 #include "roken.h"
40 #include "parse_units.h"
43 * Parse string in `s' according to `units' and return value.
44 * def_unit defines the default unit.
47 static int
48 parse_something (const char *s, const struct units *units,
49 const char *def_unit,
50 int (*func)(int res, int val, unsigned mult),
51 int init,
52 int accept_no_val_p)
54 const char *p;
55 int res = init;
56 unsigned def_mult = 1;
58 if (def_unit != NULL) {
59 const struct units *u;
61 for (u = units; u->name; ++u) {
62 if (strcasecmp (u->name, def_unit) == 0) {
63 def_mult = u->mult;
64 break;
67 if (u->name == NULL)
68 return -1;
71 p = s;
72 while (*p) {
73 double val;
74 char *next;
75 const struct units *u, *partial_unit;
76 size_t u_len;
77 unsigned partial;
78 int no_val_p = 0;
80 while(isspace((unsigned char)*p) || *p == ',')
81 ++p;
83 val = strtod (p, &next); /* strtol(p, &next, 0); */
84 if (p == next) {
85 val = 0;
86 if(!accept_no_val_p)
87 return -1;
88 no_val_p = 1;
90 p = next;
91 while (isspace((unsigned char)*p))
92 ++p;
93 if (*p == '\0') {
94 res = (*func)(res, val, def_mult);
95 if (res < 0)
96 return res;
97 break;
98 } else if (*p == '+') {
99 ++p;
100 val = 1;
101 } else if (*p == '-') {
102 ++p;
103 val = -1;
105 if (no_val_p && val == 0)
106 val = 1;
107 u_len = strcspn (p, ", \t");
108 partial = 0;
109 partial_unit = NULL;
110 if (u_len > 1 && p[u_len - 1] == 's')
111 --u_len;
112 for (u = units; u->name; ++u) {
113 if (strncasecmp (p, u->name, u_len) == 0) {
114 if (u_len == strlen (u->name)) {
115 p += u_len;
116 res = (*func)(res, val, u->mult);
117 if (res < 0)
118 return res;
119 break;
120 } else {
121 ++partial;
122 partial_unit = u;
126 if (u->name == NULL) {
127 if (partial == 1) {
128 p += u_len;
129 res = (*func)(res, val, partial_unit->mult);
130 if (res < 0)
131 return res;
132 } else {
133 return -1;
136 if (*p == 's')
137 ++p;
139 return res;
143 * The string consists of a sequence of `n unit'
146 static int
147 acc_units(int res, int val, unsigned mult)
149 return res + val * mult;
152 int ROKEN_LIB_FUNCTION
153 parse_units (const char *s, const struct units *units,
154 const char *def_unit)
156 return parse_something (s, units, def_unit, acc_units, 0, 0);
160 * The string consists of a sequence of `[+-]flag'. `orig' consists
161 * the original set of flags, those are then modified and returned as
162 * the function value.
165 static int
166 acc_flags(int res, int val, unsigned mult)
168 if(val == 1)
169 return res | mult;
170 else if(val == -1)
171 return res & ~mult;
172 else if (val == 0)
173 return mult;
174 else
175 return -1;
178 int ROKEN_LIB_FUNCTION
179 parse_flags (const char *s, const struct units *units,
180 int orig)
182 return parse_something (s, units, NULL, acc_flags, orig, 1);
186 * Return a string representation according to `units' of `num' in `s'
187 * with maximum length `len'. The actual length is the function value.
190 static int
191 unparse_something (int num, const struct units *units, char *s, size_t len,
192 int (*print) (char *, size_t, int, const char *, int),
193 int (*update) (int, unsigned),
194 const char *zero_string)
196 const struct units *u;
197 int ret = 0, tmp;
199 if (num == 0)
200 return snprintf (s, len, "%s", zero_string);
202 for (u = units; num > 0 && u->name; ++u) {
203 int divisor;
205 divisor = num / u->mult;
206 if (divisor) {
207 num = (*update) (num, u->mult);
208 tmp = (*print) (s, len, divisor, u->name, num);
209 if (tmp < 0)
210 return tmp;
211 if (tmp > len) {
212 len = 0;
213 s = NULL;
214 } else {
215 len -= tmp;
216 s += tmp;
218 ret += tmp;
221 return ret;
224 static int
225 print_unit (char *s, size_t len, int divisor, const char *name, int rem)
227 return snprintf (s, len, "%u %s%s%s",
228 divisor, name,
229 divisor == 1 ? "" : "s",
230 rem > 0 ? " " : "");
233 static int
234 update_unit (int in, unsigned mult)
236 return in % mult;
239 static int
240 update_unit_approx (int in, unsigned mult)
242 if (in / mult > 0)
243 return 0;
244 else
245 return update_unit (in, mult);
248 int ROKEN_LIB_FUNCTION
249 unparse_units (int num, const struct units *units, char *s, size_t len)
251 return unparse_something (num, units, s, len,
252 print_unit,
253 update_unit,
254 "0");
257 int ROKEN_LIB_FUNCTION
258 unparse_units_approx (int num, const struct units *units, char *s, size_t len)
260 return unparse_something (num, units, s, len,
261 print_unit,
262 update_unit_approx,
263 "0");
266 void ROKEN_LIB_FUNCTION
267 print_units_table (const struct units *units, FILE *f)
269 const struct units *u, *u2;
270 int max_sz = 0;
272 for (u = units; u->name; ++u) {
273 max_sz = max(max_sz, strlen(u->name));
276 for (u = units; u->name;) {
277 char buf[1024];
278 const struct units *next;
280 for (next = u + 1; next->name && next->mult == u->mult; ++next)
283 if (next->name) {
284 for (u2 = next;
285 u2->name && u->mult % u2->mult != 0;
286 ++u2)
288 if (u2->name == NULL)
289 --u2;
290 unparse_units (u->mult, u2, buf, sizeof(buf));
291 fprintf (f, "1 %*s = %s\n", max_sz, u->name, buf);
292 } else {
293 fprintf (f, "1 %s\n", u->name);
295 u = next;
299 static int
300 print_flag (char *s, size_t len, int divisor, const char *name, int rem)
302 return snprintf (s, len, "%s%s", name, rem > 0 ? ", " : "");
305 static int
306 update_flag (int in, unsigned mult)
308 return in - mult;
311 int ROKEN_LIB_FUNCTION
312 unparse_flags (int num, const struct units *units, char *s, size_t len)
314 return unparse_something (num, units, s, len,
315 print_flag,
316 update_flag,
317 "");
320 void ROKEN_LIB_FUNCTION
321 print_flags_table (const struct units *units, FILE *f)
323 const struct units *u;
325 for(u = units; u->name; ++u)
326 fprintf(f, "%s%s", u->name, (u+1)->name ? ", " : "\n");