home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 1 / GoldFishApril1994_CD2.img / d4xx / d473 / cnewssrc / cnews_src.lzh / libamiga / envparm.c < prev    next >
C/C++ Source or Header  |  1990-12-25  |  5KB  |  184 lines

  1. /*    :ts=4
  2.  *    envparm -- obtain the default value for the given parameter
  3.  *
  4.  *    All parameters are strings; all return values are character pointers
  5.  *    to internally malloc()'d character arrays.
  6.  *
  7.  *    The Manx compiler will generate code for GetEnv() which looks for
  8.  *    the ENV: variable; the Lattice compiler WILL NOT generate code for
  9.  *    the Manx getenv() call [the getenv() included here just returns NULL].
  10.  *
  11.  *    $Id: envparm.c,v 1.5 90/12/15 12:46:12 crash Exp Locker: crash $
  12.  *
  13.  *    $Log:    envparm.c,v $
  14.  * Revision 1.5  90/12/15  12:46:12  crash
  15.  * moved some environment checking around a little
  16.  * 
  17.  * Revision 1.4  90/10/13  17:51:03  crash
  18.  * boy, was GetEnv() screwed up!  Basically I changed the whole flow so there
  19.  * wouldn't be alot of code re-executing over and over again...
  20.  * 
  21.  * Revision 1.3  90/07/05  21:24:22  crash
  22.  * removed getenv() from AZTEC_C code
  23.  * 
  24.  * Revision 1.2  90/05/22  17:23:53  crash
  25.  * Oops, I forgot to declare getenv()
  26.  * 
  27.  * Revision 1.1  90/05/22  17:22:09  crash
  28.  * Initial revision
  29.  * 
  30.  */
  31.  
  32. /*
  33.  *    Check ENV: and if not found, look for a variable called "CNEWS", which
  34.  *    is the name of a file to check in addition to the environments.
  35.  *
  36.  *    Of course, from what Larry told me, this will a be moot as WB2.0
  37.  *    gains acceptance, since the get/put routines for environment variables
  38.  *    will be part of the OS (yea!), but there's always "backwards
  39.  *    compatibility" (sigh) :-(.
  40.  */
  41.  
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include <fcntl.h>
  45. #include "news.h"
  46. #include "fgetmfs.h"
  47.  
  48. extern char *GetEnv(char *var);
  49.  
  50. struct _env {
  51.     struct _env *next;
  52.     char    *word;
  53.     char    *value;
  54. } *head = 0;
  55.  
  56. static int parse(char *src, char **word, char **value);
  57. static struct _env *lookup(register char *word);
  58. static char *putparm(register char *word, register char *value);
  59. char *mkfilename(register char *dest, register char *dir, char *name);
  60.  
  61. static char *CNews_Config = 0;
  62. #define NEWSCTL    "NewsCtl:"
  63.  
  64. char *envparm(register char *var)
  65. {
  66.     register char *ptr;
  67.     register struct _env *env;
  68.  
  69.     /*
  70.      *    First, we cache the CNews.Config file...
  71.      */
  72.     if (!CNews_Config) {
  73.         /*
  74.          *    Process the contents of the config file...
  75.          */
  76.         FILE *fp;
  77.         int count = 0;
  78.         char *word, *value;
  79.  
  80.         if (CNews_Config = GetEnv("CNEWS"))
  81.             ;
  82.         else {
  83.             word = GetEnv("NEWSCTL");
  84.             CNews_Config = mkfilename(NULL, word ? word : NEWSCTL, "Cnews.Config");
  85.             if (word) free(word);
  86.         }
  87.         if ((fp = fopen(CNews_Config, "r")) == NULL)
  88.             return( NULL );
  89.         while (ptr = fgetms(fp)) {
  90.             count++;
  91.             if (word = strchr(ptr, '#'))    *word = '\0';
  92.             while (*ptr && isspace(*ptr)) ptr++;
  93.             if (!*ptr) continue;
  94.  
  95.             if (!parse(ptr, &word, &value) || !putparm(word, value))
  96.                 error("Couldn't store environment variable!\n", word);
  97.         }
  98.         fclose(fp);
  99.     }
  100.     /*
  101.      *    Is the CNews.Config cached?  If so, then the variable
  102.      *    should be found here...
  103.      */
  104.     if (env = lookup(var))
  105.         return( env->value );
  106.     if (ptr = GetEnv(var))        /* If NULL, it really isn't out there */
  107.         putparm(var, ptr);        /*   otherwise, store it for later    */
  108.     return( ptr );                /*   and return the value. */
  109. }
  110.  
  111. static int parse(char *src, char **word, char **value)
  112. {
  113.     while (*src && isspace(*src)) src++;
  114.     *word = src;
  115.     while (*src && *src != '=' && !isspace(*src)) src++;
  116.     if (!*src)
  117.         return( 0 );
  118.  
  119.     *src++ = '\0';
  120.     while (*src && *src != '"') src++;
  121.     if (!*src)
  122.         return( 0 );
  123.  
  124.     *value = ++src;
  125.     src = strchr(src, '"');
  126.     if (!src)
  127.         return( 0 );
  128.  
  129.     *src = '\0';
  130.     return( 1 );
  131. }
  132.  
  133. #ifdef AZTEC_C
  134. char *GetEnv(register char *word)
  135. {
  136.     register FILE *fp;
  137.     register char *fname, *value = NULL;
  138.  
  139.     fname = str3save("ENV:", "", word);
  140.     if (fp = fopen(fname, "r")) {
  141.         value = fgetms(fp);
  142.         fclose(fp);
  143.     }
  144.     free(fname);
  145.     return( value );
  146. }
  147. #endif /* AZTEC_C */
  148.  
  149. static char *putparm(register char *word, register char *value)
  150. {
  151.     register struct _env *next;
  152.     register int len = strlen(word) + 1;
  153.  
  154.     if (next = lookup(word)) {
  155.         if (next->value = (char *) realloc(next->value, strlen(value)+1))
  156.             strcpy(next->value, value);
  157.         return( next->value );
  158.     } else if (next = (struct _env *) malloc(sizeof(*next)+len)) {
  159.         next->next  = head;
  160.         next->word  = (char *) &next[1];    /* First byte after `next' */
  161.         if (next->value = (char *) malloc(strlen(value)+1)) {
  162.             strcpy(next->word, word);
  163.             strcpy(next->value, value);
  164.             head = next;
  165.             return( next->value );
  166.         }
  167.     }
  168.     return( NULL );
  169. }
  170.  
  171. static struct _env *lookup(register char *word)
  172. {
  173.     register struct _env *next;
  174.     register int len = strlen(word)+1;
  175.  
  176.     next = head;
  177.     while (next) {
  178.         if (!cistrncmp(next->word, word, len))
  179.             return( next );
  180.         next = next->next;
  181.     }
  182.     return( NULL );
  183. }
  184.