VM: full munmap
[minix.git] / lib / libsys / env_parse.c
blobe466d25755f3a4f36bfaeda5113f1915e5e5d365
1 #include "sysutil.h"
2 #include <stdlib.h>
3 #include <env.h>
4 #include <string.h>
5 #include <minix/param.h>
8 /*=========================================================================*
9 * env_parse *
10 *=========================================================================*/
11 int env_parse(env, fmt, field, param, min, max)
12 char *env; /* environment variable to inspect */
13 char *fmt; /* template to parse it with */
14 int field; /* field number of value to return */
15 long *param; /* address of parameter to get */
16 long min, max; /* minimum and maximum values for the parameter */
18 /* Parse an environment variable setting, something like "DPETH0=300:3".
19 * Panic if the parsing fails. Return EP_UNSET if the environment variable
20 * is not set, EP_OFF if it is set to "off", EP_ON if set to "on" or a
21 * field is left blank, or EP_SET if a field is given (return value through
22 * *param). Punctuation may be used in the environment and format string,
23 * fields in the environment string may be empty, and punctuation may be
24 * missing to skip fields. The format string contains characters 'd', 'o',
25 * 'x' and 'c' to indicate that 10, 8, 16, or 0 is used as the last argument
26 * to strtol(). A '*' means that a field should be skipped. If the format
27 * string contains something like "\4" then the string is repeated 4 characters
28 * to the left.
30 char *val, *end;
31 char value[EP_BUF_SIZE];
32 char PUNCT[] = ":,;.";
33 long newpar;
34 int s, i, radix, r;
36 if ((s=env_get_param(env, value, sizeof(value))) != 0) {
37 if (s == ESRCH) return(EP_UNSET); /* only error allowed */
38 printf("WARNING: env_get_param() failed in env_parse(): %d\n",s);
39 return(EP_EGETKENV);
41 val = value;
42 if (strcmp(val, "off") == 0) return(EP_OFF);
43 if (strcmp(val, "on") == 0) return(EP_ON);
45 i = 0;
46 r = EP_ON;
47 for (;;) {
48 while (*val == ' ') val++; /* skip spaces */
49 if (*val == 0) return(r); /* the proper exit point */
50 if (*fmt == 0) break; /* too many values */
52 if (strchr(PUNCT, *val) != NULL) {
53 /* Time to go to the next field. */
54 if (strchr(PUNCT, *fmt) != NULL) i++;
55 if (*fmt++ == *val) val++;
56 if (*fmt < 32) fmt -= *fmt; /* step back? */
57 } else {
58 /* Environment contains a value, get it. */
59 switch (*fmt) {
60 case '*': radix = -1; break;
61 case 'd': radix = 10; break;
62 case 'o': radix = 010; break;
63 case 'x': radix = 0x10; break;
64 case 'c': radix = 0; break;
65 default: goto badenv;
68 if (radix < 0) {
69 /* Skip. */
70 while (strchr(PUNCT, *val) == NULL) val++;
71 continue;
72 } else {
73 /* A number. */
74 newpar = strtol(val, &end, radix);
76 if (end == val) break; /* not a number */
77 val = end;
80 if (i == field) {
81 /* The field requested. */
82 if (newpar < min || newpar > max) break;
83 *param = newpar;
84 r = EP_SET;
88 badenv:
89 env_panic(env);
90 return -1;
93 /*=========================================================================*
94 * env_memory_parse *
95 *=========================================================================*/
97 int env_memory_parse(mem_chunks, maxchunks)
98 struct memory *mem_chunks; /* where to store the memory bits */
99 int maxchunks; /* how many were found */
101 static kinfo_t kinfo;
102 int mm, r;
104 if((r=sys_getkinfo(&kinfo)) != OK) return r;
106 /* Initialize everything to zero. */
107 memset(mem_chunks, 0, maxchunks*sizeof(*mem_chunks));
109 for(mm = 0; mm < MAXMEMMAP; mm++) {
110 mem_chunks[mm].base = kinfo.memmap[mm].addr;
111 mem_chunks[mm].size = kinfo.memmap[mm].len;
114 return OK;