xref: /freebsd/crypto/heimdal/lib/roken/parse_units.c (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
1 /*
2  * Copyright (c) 1997 - 2001 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
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.
16  *
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.
20  *
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.
32  */
33 
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 RCSID("$Id: parse_units.c,v 1.13 2001/03/26 00:47:06 assar Exp $");
37 #endif
38 
39 #include <stdio.h>
40 #include <ctype.h>
41 #include <string.h>
42 #include <roken.h>
43 #include "parse_units.h"
44 
45 /*
46  * Parse string in `s' according to `units' and return value.
47  * def_unit defines the default unit.
48  */
49 
50 static int
51 parse_something (const char *s, const struct units *units,
52 		 const char *def_unit,
53 		 int (*func)(int res, int val, unsigned mult),
54 		 int init,
55 		 int accept_no_val_p)
56 {
57     const char *p;
58     int res = init;
59     unsigned def_mult = 1;
60 
61     if (def_unit != NULL) {
62 	const struct units *u;
63 
64 	for (u = units; u->name; ++u) {
65 	    if (strcasecmp (u->name, def_unit) == 0) {
66 		def_mult = u->mult;
67 		break;
68 	    }
69 	}
70 	if (u->name == NULL)
71 	    return -1;
72     }
73 
74     p = s;
75     while (*p) {
76 	double val;
77 	char *next;
78 	const struct units *u, *partial_unit;
79 	size_t u_len;
80 	unsigned partial;
81 	int no_val_p = 0;
82 
83 	while(isspace((unsigned char)*p) || *p == ',')
84 	    ++p;
85 
86 	val = strtod (p, &next); /* strtol(p, &next, 0); */
87 	if (p == next) {
88 	    val = 0;
89 	    if(!accept_no_val_p)
90 		return -1;
91 	    no_val_p = 1;
92 	}
93 	p = next;
94 	while (isspace((unsigned char)*p))
95 	    ++p;
96 	if (*p == '\0') {
97 	    res = (*func)(res, val, def_mult);
98 	    if (res < 0)
99 		return res;
100 	    break;
101 	} else if (*p == '+') {
102 	    ++p;
103 	    val = 1;
104 	} else if (*p == '-') {
105 	    ++p;
106 	    val = -1;
107 	}
108 	if (no_val_p && val == 0)
109 	    val = 1;
110 	u_len = strcspn (p, ", \t");
111 	partial = 0;
112 	partial_unit = NULL;
113 	if (u_len > 1 && p[u_len - 1] == 's')
114 	    --u_len;
115 	for (u = units; u->name; ++u) {
116 	    if (strncasecmp (p, u->name, u_len) == 0) {
117 		if (u_len == strlen (u->name)) {
118 		    p += u_len;
119 		    res = (*func)(res, val, u->mult);
120 		    if (res < 0)
121 			return res;
122 		    break;
123 		} else {
124 		    ++partial;
125 		    partial_unit = u;
126 		}
127 	    }
128 	}
129 	if (u->name == NULL) {
130 	    if (partial == 1) {
131 		p += u_len;
132 		res = (*func)(res, val, partial_unit->mult);
133 		if (res < 0)
134 		    return res;
135 	    } else {
136 		return -1;
137 	    }
138 	}
139 	if (*p == 's')
140 	    ++p;
141     }
142     return res;
143 }
144 
145 /*
146  * The string consists of a sequence of `n unit'
147  */
148 
149 static int
150 acc_units(int res, int val, unsigned mult)
151 {
152     return res + val * mult;
153 }
154 
155 int
156 parse_units (const char *s, const struct units *units,
157 	     const char *def_unit)
158 {
159     return parse_something (s, units, def_unit, acc_units, 0, 0);
160 }
161 
162 /*
163  * The string consists of a sequence of `[+-]flag'.  `orig' consists
164  * the original set of flags, those are then modified and returned as
165  * the function value.
166  */
167 
168 static int
169 acc_flags(int res, int val, unsigned mult)
170 {
171     if(val == 1)
172 	return res | mult;
173     else if(val == -1)
174 	return res & ~mult;
175     else if (val == 0)
176 	return mult;
177     else
178 	return -1;
179 }
180 
181 int
182 parse_flags (const char *s, const struct units *units,
183 	     int orig)
184 {
185     return parse_something (s, units, NULL, acc_flags, orig, 1);
186 }
187 
188 /*
189  * Return a string representation according to `units' of `num' in `s'
190  * with maximum length `len'.  The actual length is the function value.
191  */
192 
193 static size_t
194 unparse_something (int num, const struct units *units, char *s, size_t len,
195 		   int (*print) (char *s, size_t len, int div,
196 				const char *name, int rem),
197 		   int (*update) (int in, unsigned mult),
198 		   const char *zero_string)
199 {
200     const struct units *u;
201     size_t ret = 0, tmp;
202 
203     if (num == 0)
204 	return snprintf (s, len, "%s", zero_string);
205 
206     for (u = units; num > 0 && u->name; ++u) {
207 	int div;
208 
209 	div = num / u->mult;
210 	if (div) {
211 	    num = (*update) (num, u->mult);
212 	    tmp = (*print) (s, len, div, u->name, num);
213 
214 	    len -= tmp;
215 	    s += tmp;
216 	    ret += tmp;
217 	}
218     }
219     return ret;
220 }
221 
222 static int
223 print_unit (char *s, size_t len, int div, const char *name, int rem)
224 {
225     return snprintf (s, len, "%u %s%s%s",
226 		     div, name,
227 		     div == 1 ? "" : "s",
228 		     rem > 0 ? " " : "");
229 }
230 
231 static int
232 update_unit (int in, unsigned mult)
233 {
234     return in % mult;
235 }
236 
237 static int
238 update_unit_approx (int in, unsigned mult)
239 {
240     if (in / mult > 0)
241 	return 0;
242     else
243 	return update_unit (in, mult);
244 }
245 
246 size_t
247 unparse_units (int num, const struct units *units, char *s, size_t len)
248 {
249     return unparse_something (num, units, s, len,
250 			      print_unit,
251 			      update_unit,
252 			      "0");
253 }
254 
255 size_t
256 unparse_units_approx (int num, const struct units *units, char *s, size_t len)
257 {
258     return unparse_something (num, units, s, len,
259 			      print_unit,
260 			      update_unit_approx,
261 			      "0");
262 }
263 
264 void
265 print_units_table (const struct units *units, FILE *f)
266 {
267     const struct units *u, *u2;
268     unsigned max_sz = 0;
269 
270     for (u = units; u->name; ++u) {
271 	max_sz = max(max_sz, strlen(u->name));
272     }
273 
274     for (u = units; u->name;) {
275 	char buf[1024];
276 	const struct units *next;
277 
278 	for (next = u + 1; next->name && next->mult == u->mult; ++next)
279 	    ;
280 
281 	if (next->name) {
282 	    for (u2 = next;
283 		 u2->name && u->mult % u2->mult != 0;
284 		 ++u2)
285 		;
286 	    if (u2->name == NULL)
287 		--u2;
288 	    unparse_units (u->mult, u2, buf, sizeof(buf));
289 	    fprintf (f, "1 %*s = %s\n", max_sz, u->name, buf);
290 	} else {
291 	    fprintf (f, "1 %s\n", u->name);
292 	}
293 	u = next;
294     }
295 }
296 
297 static int
298 print_flag (char *s, size_t len, int div, const char *name, int rem)
299 {
300     return snprintf (s, len, "%s%s", name, rem > 0 ? ", " : "");
301 }
302 
303 static int
304 update_flag (int in, unsigned mult)
305 {
306     return in - mult;
307 }
308 
309 size_t
310 unparse_flags (int num, const struct units *units, char *s, size_t len)
311 {
312     return unparse_something (num, units, s, len,
313 			      print_flag,
314 			      update_flag,
315 			      "");
316 }
317 
318 void
319 print_flags_table (const struct units *units, FILE *f)
320 {
321     const struct units *u;
322 
323     for(u = units; u->name; ++u)
324 	fprintf(f, "%s%s", u->name, (u+1)->name ? ", " : "\n");
325 }
326