4 #include <minix/param.h>
7 /*=========================================================================*
9 *=========================================================================*/
10 int env_parse(const char *env
, const char *fmt
,
11 int field
, long *param
, long min
, long max
)
13 /* Parse an environment variable setting, something like "DPETH0=300:3".
14 * Panic if the parsing fails. Return EP_UNSET if the environment variable
15 * is not set, EP_OFF if it is set to "off", EP_ON if set to "on" or a
16 * field is left blank, or EP_SET if a field is given (return value through
17 * *param). Punctuation may be used in the environment and format string,
18 * fields in the environment string may be empty, and punctuation may be
19 * missing to skip fields. The format string contains characters 'd', 'o',
20 * 'x' and 'c' to indicate that 10, 8, 16, or 0 is used as the last argument
21 * to strtol(). A '*' means that a field should be skipped. If the format
22 * string contains something like "\4" then the string is repeated 4 characters
26 char value
[EP_BUF_SIZE
];
27 char PUNCT
[] = ":,;.";
31 if ((s
=env_get_param(env
, value
, sizeof(value
))) != 0) {
32 if (s
== ESRCH
) return(EP_UNSET
); /* only error allowed */
33 printf("WARNING: env_get_param() failed in env_parse(): %d\n",s
);
37 if (strcmp(val
, "off") == 0) return(EP_OFF
);
38 if (strcmp(val
, "on") == 0) return(EP_ON
);
43 while (*val
== ' ') val
++; /* skip spaces */
44 if (*val
== 0) return(r
); /* the proper exit point */
45 if (*fmt
== 0) break; /* too many values */
47 if (strchr(PUNCT
, *val
) != NULL
) {
48 /* Time to go to the next field. */
49 if (strchr(PUNCT
, *fmt
) != NULL
) i
++;
50 if (*fmt
++ == *val
) val
++;
51 if (*fmt
< 32) fmt
-= *fmt
; /* step back? */
53 /* Environment contains a value, get it. */
55 case '*': radix
= -1; break;
56 case 'd': radix
= 10; break;
57 case 'o': radix
= 010; break;
58 case 'x': radix
= 0x10; break;
59 case 'c': radix
= 0; break;
65 while (strchr(PUNCT
, *val
) == NULL
) val
++;
69 newpar
= strtol(val
, &end
, radix
);
71 if (end
== val
) break; /* not a number */
76 /* The field requested. */
77 if (newpar
< min
|| newpar
> max
) break;