source: trunk/minix/lib/sysutil/env_parse.c@ 20

Last change on this file since 20 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 2.8 KB
RevLine 
[9]1#include "sysutil.h"
2#include <stdlib.h>
3#include <string.h>
4
5
6/*=========================================================================*
7 * env_parse *
8 *=========================================================================*/
9PUBLIC int env_parse(env, fmt, field, param, min, max)
10char *env; /* environment variable to inspect */
11char *fmt; /* template to parse it with */
12int field; /* field number of value to return */
13long *param; /* address of parameter to get */
14long min, max; /* minimum and maximum values for the parameter */
15{
16/* Parse an environment variable setting, something like "DPETH0=300:3".
17 * Panic if the parsing fails. Return EP_UNSET if the environment variable
18 * is not set, EP_OFF if it is set to "off", EP_ON if set to "on" or a
19 * field is left blank, or EP_SET if a field is given (return value through
20 * *param). Punctuation may be used in the environment and format string,
21 * fields in the environment string may be empty, and punctuation may be
22 * missing to skip fields. The format string contains characters 'd', 'o',
23 * 'x' and 'c' to indicate that 10, 8, 16, or 0 is used as the last argument
24 * to strtol(). A '*' means that a field should be skipped. If the format
25 * string contains something like "\4" then the string is repeated 4 characters
26 * to the left.
27 */
28 char *val, *end;
29 char value[EP_BUF_SIZE];
30 char PUNCT[] = ":,;.";
31 long newpar;
32 int s, i, radix, r, keylen;
33
34 if ((s=env_get_param(env, value, sizeof(value))) != 0) {
35 if (s == ESRCH) return(EP_UNSET); /* only error allowed */
36 printf("WARNING: get_mon_param() failed in env_parse(): %d\n",s);
37 return(EP_EGETKENV);
38 }
39 val = value;
40 if (strcmp(val, "off") == 0) return(EP_OFF);
41 if (strcmp(val, "on") == 0) return(EP_ON);
42
43 i = 0;
44 r = EP_ON;
45 for (;;) {
46 while (*val == ' ') val++; /* skip spaces */
47 if (*val == 0) return(r); /* the proper exit point */
48 if (*fmt == 0) break; /* too many values */
49
50 if (strchr(PUNCT, *val) != NULL) {
51 /* Time to go to the next field. */
52 if (strchr(PUNCT, *fmt) != NULL) i++;
53 if (*fmt++ == *val) val++;
54 if (*fmt < 32) fmt -= *fmt; /* step back? */
55 } else {
56 /* Environment contains a value, get it. */
57 switch (*fmt) {
58 case '*': radix = -1; break;
59 case 'd': radix = 10; break;
60 case 'o': radix = 010; break;
61 case 'x': radix = 0x10; break;
62 case 'c': radix = 0; break;
63 default: goto badenv;
64 }
65
66 if (radix < 0) {
67 /* Skip. */
68 while (strchr(PUNCT, *val) == NULL) val++;
69 continue;
70 } else {
71 /* A number. */
72 newpar = strtol(val, &end, radix);
73
74 if (end == val) break; /* not a number */
75 val = end;
76 }
77
78 if (i == field) {
79 /* The field requested. */
80 if (newpar < min || newpar > max) break;
81 *param = newpar;
82 r = EP_SET;
83 }
84 }
85 }
86badenv:
87 env_panic(env);
88}
89
90
Note: See TracBrowser for help on using the repository browser.