xref: /freebsd/crypto/heimdal/lib/roken/parse_units.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
1 /*
2  * Copyright (c) 1997, 1998, 1999 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.12 1999/12/02 16:58:51 joda 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 (val == 0 && p == next) {
88 	    if(!accept_no_val_p)
89 		return -1;
90 	    no_val_p = 1;
91 	}
92 	p = next;
93 	while (isspace((unsigned char)*p))
94 	    ++p;
95 	if (*p == '\0') {
96 	    res = (*func)(res, val, def_mult);
97 	    if (res < 0)
98 		return res;
99 	    break;
100 	} else if (*p == '+') {
101 	    ++p;
102 	    val = 1;
103 	} else if (*p == '-') {
104 	    ++p;
105 	    val = -1;
106 	}
107 	if (no_val_p && val == 0)
108 	    val = 1;
109 	u_len = strcspn (p, ", \t");
110 	partial = 0;
111 	partial_unit = NULL;
112 	if (u_len > 1 && p[u_len - 1] == 's')
113 	    --u_len;
114 	for (u = units; u->name; ++u) {
115 	    if (strncasecmp (p, u->name, u_len) == 0) {
116 		if (u_len == strlen (u->name)) {
117 		    p += u_len;
118 		    res = (*func)(res, val, u->mult);
119 		    if (res < 0)
120 			return res;
121 		    break;
122 		} else {
123 		    ++partial;
124 		    partial_unit = u;
125 		}
126 	    }
127 	}
128 	if (u->name == NULL) {
129 	    if (partial == 1) {
130 		p += u_len;
131 		res = (*func)(res, val, partial_unit->mult);
132 		if (res < 0)
133 		    return res;
134 	    } else {
135 		return -1;
136 	    }
137 	}
138 	if (*p == 's')
139 	    ++p;
140     }
141     return res;
142 }
143 
144 /*
145  * The string consists of a sequence of `n unit'
146  */
147 
148 static int
149 acc_units(int res, int val, unsigned mult)
150 {
151     return res + val * mult;
152 }
153 
154 int
155 parse_units (const char *s, const struct units *units,
156 	     const char *def_unit)
157 {
158     return parse_something (s, units, def_unit, acc_units, 0, 0);
159 }
160 
161 /*
162  * The string consists of a sequence of `[+-]flag'.  `orig' consists
163  * the original set of flags, those are then modified and returned as
164  * the function value.
165  */
166 
167 static int
168 acc_flags(int res, int val, unsigned mult)
169 {
170     if(val == 1)
171 	return res | mult;
172     else if(val == -1)
173 	return res & ~mult;
174     else if (val == 0)
175 	return mult;
176     else
177 	return -1;
178 }
179 
180 int
181 parse_flags (const char *s, const struct units *units,
182 	     int orig)
183 {
184     return parse_something (s, units, NULL, acc_flags, orig, 1);
185 }
186 
187 /*
188  * Return a string representation according to `units' of `num' in `s'
189  * with maximum length `len'.  The actual length is the function value.
190  */
191 
192 static size_t
193 unparse_something (int num, const struct units *units, char *s, size_t len,
194 		   int (*print) (char *s, size_t len, int div,
195 				const char *name, int rem),
196 		   int (*update) (int in, unsigned mult),
197 		   const char *zero_string)
198 {
199     const struct units *u;
200     size_t ret = 0, tmp;
201 
202     if (num == 0)
203 	return snprintf (s, len, "%s", zero_string);
204 
205     for (u = units; num > 0 && u->name; ++u) {
206 	int div;
207 
208 	div = num / u->mult;
209 	if (div) {
210 	    num = (*update) (num, u->mult);
211 	    tmp = (*print) (s, len, div, u->name, num);
212 
213 	    len -= tmp;
214 	    s += tmp;
215 	    ret += tmp;
216 	}
217     }
218     return ret;
219 }
220 
221 static int
222 print_unit (char *s, size_t len, int div, const char *name, int rem)
223 {
224     return snprintf (s, len, "%u %s%s%s",
225 		     div, name,
226 		     div == 1 ? "" : "s",
227 		     rem > 0 ? " " : "");
228 }
229 
230 static int
231 update_unit (int in, unsigned mult)
232 {
233     return in % mult;
234 }
235 
236 static int
237 update_unit_approx (int in, unsigned mult)
238 {
239     if (in / mult > 0)
240 	return 0;
241     else
242 	return update_unit (in, mult);
243 }
244 
245 size_t
246 unparse_units (int num, const struct units *units, char *s, size_t len)
247 {
248     return unparse_something (num, units, s, len,
249 			      print_unit,
250 			      update_unit,
251 			      "0");
252 }
253 
254 size_t
255 unparse_units_approx (int num, const struct units *units, char *s, size_t len)
256 {
257     return unparse_something (num, units, s, len,
258 			      print_unit,
259 			      update_unit_approx,
260 			      "0");
261 }
262 
263 void
264 print_units_table (const struct units *units, FILE *f)
265 {
266     const struct units *u, *u2;
267     unsigned max_sz = 0;
268 
269     for (u = units; u->name; ++u) {
270 	max_sz = max(max_sz, strlen(u->name));
271     }
272 
273     for (u = units; u->name;) {
274 	char buf[1024];
275 	const struct units *next;
276 
277 	for (next = u + 1; next->name && next->mult == u->mult; ++next)
278 	    ;
279 
280 	if (next->name) {
281 	    for (u2 = next;
282 		 u2->name && u->mult % u2->mult != 0;
283 		 ++u2)
284 		;
285 	    if (u2->name == NULL)
286 		--u2;
287 	    unparse_units (u->mult, u2, buf, sizeof(buf));
288 	    fprintf (f, "1 %*s = %s\n", max_sz, u->name, buf);
289 	} else {
290 	    fprintf (f, "1 %s\n", u->name);
291 	}
292 	u = next;
293     }
294 }
295 
296 static int
297 print_flag (char *s, size_t len, int div, const char *name, int rem)
298 {
299     return snprintf (s, len, "%s%s", name, rem > 0 ? ", " : "");
300 }
301 
302 static int
303 update_flag (int in, unsigned mult)
304 {
305     return in - mult;
306 }
307 
308 size_t
309 unparse_flags (int num, const struct units *units, char *s, size_t len)
310 {
311     return unparse_something (num, units, s, len,
312 			      print_flag,
313 			      update_flag,
314 			      "");
315 }
316 
317 void
318 print_flags_table (const struct units *units, FILE *f)
319 {
320     const struct units *u;
321 
322     for(u = units; u->name; ++u)
323 	fprintf(f, "%s%s", u->name, (u+1)->name ? ", " : "\n");
324 }
325