xref: /minix3/minix/lib/libsys/env_parse.c (revision 77e79d33746fa2b709086b9caa51b1415d66c801)
1 #include "sysutil.h"
2 #include <stdlib.h>
3 #include <string.h>
4 #include <minix/param.h>
5 
6 
7 /*=========================================================================*
8  *				env_parse				   *
9  *=========================================================================*/
10 int env_parse(const char *env, const char *fmt,
11 	int field, long *param, long min, long max)
12 {
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
23  * to the left.
24  */
25   char *val, *end;
26   char value[EP_BUF_SIZE];
27   char PUNCT[] = ":,;.";
28   long newpar;
29   int s, i, radix, r;
30 
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);
34       return(EP_EGETKENV);
35   }
36   val = value;
37   if (strcmp(val, "off") == 0) return(EP_OFF);
38   if (strcmp(val, "on") == 0) return(EP_ON);
39 
40   i = 0;
41   r = EP_ON;
42   for (;;) {
43 	while (*val == ' ') val++;	/* skip spaces */
44 	if (*val == 0) return(r);	/* the proper exit point */
45 	if (*fmt == 0) break;		/* too many values */
46 
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? */
52 	} else {
53 		/* Environment contains a value, get it. */
54 		switch (*fmt) {
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;
60 		default:	goto badenv;
61 		}
62 
63 		if (radix < 0) {
64 			/* Skip. */
65 			while (strchr(PUNCT, *val) == NULL) val++;
66 			continue;
67 		} else {
68 			/* A number. */
69 			newpar = strtol(val, &end, radix);
70 
71 			if (end == val) break;	/* not a number */
72 			val = end;
73 		}
74 
75 		if (i == field) {
76 			/* The field requested. */
77 			if (newpar < min || newpar > max) break;
78 			*param = newpar;
79 			r = EP_SET;
80 		}
81 	}
82   }
83 badenv:
84   env_panic(env);
85   return -1;
86 }
87