env_parse.c

Go to the documentation of this file.
00001 #include "sysutil.h"
00002 #include <stdlib.h>
00003 #include <string.h>
00004 
00005 
00006 /*=========================================================================*
00007  *                              env_parse                                  *
00008  *=========================================================================*/
00009 PUBLIC int env_parse(env, fmt, field, param, min, max)
00010 char *env;              /* environment variable to inspect */
00011 char *fmt;              /* template to parse it with */
00012 int field;              /* field number of value to return */
00013 long *param;            /* address of parameter to get */
00014 long min, max;          /* minimum and maximum values for the parameter */
00015 {
00016 /* Parse an environment variable setting, something like "DPETH0=300:3".
00017  * Panic if the parsing fails.  Return EP_UNSET if the environment variable
00018  * is not set, EP_OFF if it is set to "off", EP_ON if set to "on" or a
00019  * field is left blank, or EP_SET if a field is given (return value through
00020  * *param).  Punctuation may be used in the environment and format string,
00021  * fields in the environment string may be empty, and punctuation may be
00022  * missing to skip fields.  The format string contains characters 'd', 'o',
00023  * 'x' and 'c' to indicate that 10, 8, 16, or 0 is used as the last argument
00024  * to strtol().  A '*' means that a field should be skipped.  If the format
00025  * string contains something like "\4" then the string is repeated 4 characters
00026  * to the left.
00027  */
00028   char *val, *end;
00029   char value[EP_BUF_SIZE];
00030   char PUNCT[] = ":,;.";
00031   long newpar;
00032   int s, i, radix, r, keylen;
00033 
00034   if ((s=env_get_param(env, value, sizeof(value))) != 0) { 
00035       if (s == ESRCH) return(EP_UNSET);         /* only error allowed */ 
00036       printf("WARNING: get_mon_param() failed in env_parse(): %d\n",s);
00037       return(EP_EGETKENV);
00038   }
00039   val = value;
00040   if (strcmp(val, "off") == 0) return(EP_OFF);
00041   if (strcmp(val, "on") == 0) return(EP_ON);
00042 
00043   i = 0;
00044   r = EP_ON;
00045   for (;;) {
00046         while (*val == ' ') val++;      /* skip spaces */
00047         if (*val == 0) return(r);       /* the proper exit point */
00048         if (*fmt == 0) break;           /* too many values */
00049 
00050         if (strchr(PUNCT, *val) != NULL) {
00051                 /* Time to go to the next field. */
00052                 if (strchr(PUNCT, *fmt) != NULL) i++;
00053                 if (*fmt++ == *val) val++;
00054                 if (*fmt < 32) fmt -= *fmt;     /* step back? */
00055         } else {
00056                 /* Environment contains a value, get it. */
00057                 switch (*fmt) {
00058                 case '*':       radix =   -1;   break;
00059                 case 'd':       radix =   10;   break;
00060                 case 'o':       radix =  010;   break;
00061                 case 'x':       radix = 0x10;   break;
00062                 case 'c':       radix =    0;   break;
00063                 default:        goto badenv;
00064                 }
00065                 
00066                 if (radix < 0) {
00067                         /* Skip. */
00068                         while (strchr(PUNCT, *val) == NULL) val++;
00069                         continue;
00070                 } else {
00071                         /* A number. */
00072                         newpar = strtol(val, &end, radix);
00073 
00074                         if (end == val) break;  /* not a number */
00075                         val = end;
00076                 }
00077 
00078                 if (i == field) {
00079                         /* The field requested. */
00080                         if (newpar < min || newpar > max) break;
00081                         *param = newpar;
00082                         r = EP_SET;
00083                 }
00084         }
00085   }
00086 badenv:
00087   env_panic(env);
00088 }
00089 
00090 

Generated on Fri Apr 14 22:57:28 2006 for minix by  doxygen 1.4.6