home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d109 / uupc.lha / UUpc / Source / LOCAL / genv.c < prev    next >
C/C++ Source or Header  |  1987-10-28  |  8KB  |  386 lines

  1. /*         genv.c
  2.  
  3.  
  4.  
  5.         copyright (C) 1987 Jeff Lydiatt
  6.  
  7.         Copying and use of this program are controlled by the terms of the
  8.         Free Software Foundations GNU Emacs General Public License.
  9.  
  10.  
  11.         version        0.1        09 July 87
  12.  
  13.    Maintenance Notes: 
  14.      01 Oct 87 - static line[] in getone() reported by Peter da Silva fixed. 
  15.  
  16. */
  17.  
  18. #include <stdio.h>
  19.  
  20. #include "genv.h"
  21.  
  22.      /* Environment variables are in file "PROFILE" */
  23. #define PROFILE "usr/lib/uucp/profile"
  24.  
  25. #define SAME    0
  26. #define MAXLINE 255
  27.  
  28. #define DMAILBOX    "mailbox"
  29. #define    DNAME        "noname"
  30. #define    DDOMAIN        "pc.uucp"
  31. #define    DMAILDIR    ":usr/spool/mail"
  32. #define DHOME        ":usr/noname"
  33. #define    DCONFDIR    ":usr/lib/uucp"
  34. #define    DSPOOLDIR    ":usr/spool/uucp"
  35. #define DLOGDIR        ":usr/spool/uucp"
  36. #define DPUBDIR        ":usr/spool/uucppublic"
  37. #define DNEWSDIR    ":usr/spool/rnews"
  38. #define DTEMPDIR    "RAM:T"
  39. #define    DMAILSERVICE    "host"
  40. #define    DNODENAME    "noname"
  41. #define    DDEVICE        "CON:"
  42. #define    DSPEED        "1200"
  43.  
  44. #define    TFILENAME    "tmpfile"
  45. #define    FILENAME     "%s/%s"
  46.  
  47. #include <stdio.h>
  48. #include <ctype.h>
  49.  
  50. struct environment
  51.   {
  52.      char name[16];
  53.      char value[64];
  54.  
  55.   };
  56. typedef struct environment ENV;
  57.  
  58. static ENV profile[]=
  59.   {
  60.     MAILBOX,     "",
  61.     NAME,        "",
  62.     DOMAIN,        "",
  63.     MAILDIR,    "",
  64.     HOME,        "",
  65.     CONFDIR,    "",
  66.     SPOOLDIR,    "",
  67.     LOGDIR,        "",
  68.     PUBDIR,        "",
  69.     NEWSDIR2,    "",
  70.     TEMPDIR,    "",
  71.     MAILSERVICE,    "",
  72.     NODENAME,    "",
  73.     DEVICE,        "",
  74.     SPEED,        "",
  75.     "",        "",
  76.   };    
  77. char    *name = NULL;
  78. char    *mailbox = NULL;
  79. char    *home = NULL;
  80. char    *domain = NULL;
  81. char    *maildir = NULL;
  82. char    *confdir = NULL;
  83. char     *spooldir = NULL;
  84. char    *logdir = NULL;
  85. char    *pubdir = NULL;
  86. char    *mailserv = NULL;
  87. char    *nodename = NULL;
  88. char    *device = NULL;
  89. char    *speed = NULL;
  90. char    *tempdir = NULL;
  91. char    *newsdir = NULL;
  92.  
  93. /*--------------------------------------------------------------*/
  94. /*    getone: get next character file f.  f already open    */
  95. /*--------------------------------------------------------------*/
  96.  
  97. static int getone( f )
  98. FILE *f;
  99. {
  100.    static char line[256];
  101.    static int pos = 0;
  102.    static int len = 0;
  103.    char c;
  104.  
  105.    if ( ++pos > len || line[pos] == '\0' ) 
  106.      {
  107.     if ( fgets( line, 255, f ) == NULL )
  108.        return EOF;
  109.     pos = 0;
  110.     len = strlen( line );
  111.      }
  112.  
  113.    c = line[pos];
  114.    return c;
  115.    
  116. }
  117.  
  118. /*--------------------------------------------------------------*/
  119. /*    getsym: get next symbol from file f.  f already open    */
  120. /*--------------------------------------------------------------*/
  121.  
  122. #define ID    1001
  123. #define DELIM    1002
  124. #define STR    1003
  125. #define EOL    1004
  126. #define OTHER    1005
  127. #define UNKNOWN -1000
  128.  
  129. static int getsym( f, sym )
  130. FILE *f;
  131. char *sym;
  132. {
  133.    /* Simple non reentrant, non reuseable get next symbol from file f */
  134.    /* Valid symbols are:
  135.         Type    Symbol Returned    Comment
  136.  
  137.     ID    <identifier>    any valid c identifier.
  138.         DELIM    '='        an equal sign.
  139.     STR    a string    anything between "" or ''.
  140.     EOL    '\n'        a newline.
  141.     EOF            the end of file character.
  142.     OTHER   a character    anything else.
  143.     
  144.     Comments begin with # and are delimited by an end of line
  145.    */
  146.  
  147.    static int lastchar = UNKNOWN; /* Unknown */
  148.    int c, delim;
  149.  
  150.    /* strip leading white space */
  151.  
  152.    if ( lastchar != UNKNOWN )
  153.       c = lastchar;
  154.    else
  155.       c = getone( f );
  156.  
  157.    while ( c == ' ' || c == '\t' )
  158.       c = getone( f );
  159.    lastchar = UNKNOWN;
  160.  
  161.   /* Comments are '#' delimited by EOL character */
  162.  
  163.    if ( c == '#' )
  164.       while ( c != '\n' && c != EOF )
  165.      c = getone( f );
  166.  
  167.    if ( c == EOF )        /* End of file? */
  168.       return EOF;
  169.  
  170.    if ( c == '\n' )        /* End of Line? */
  171.      {
  172.     strcpy( sym, "\n" );
  173.     return EOL;
  174.      }
  175.  
  176.    if ( c == '=' )        /* Delimiter '='? */
  177.      {
  178.     strcpy( sym, "=" );
  179.     return DELIM;
  180.      } 
  181.  
  182.    if ( c == '\"' || c == '\'' )/* String ? */
  183.      {
  184.     delim = c;
  185.     while ( (c = getone( f )) != delim && c != EOF && c != '\n' )
  186.        *sym++ = c;
  187.     *sym = '\0';
  188.     c = getone( f );
  189.     return STR;
  190.      }
  191.  
  192.    if ( isalpha( c ) )        /* Identifier ? */
  193.      {
  194.     *sym++ = c;
  195.     while ( ( c = getone( f )) == '_' || isalnum(c) )
  196.       *sym++ = c;
  197.     *sym = '\0';
  198.     lastchar = c;
  199.     return ID;
  200.      }
  201.  
  202.    *sym++ = c;
  203.    *sym = '\0';
  204.    return OTHER;
  205.  
  206. }
  207.     
  208.  
  209. /*--------------------------------------------------------------*/
  210. /*    setenv: insert an environment variable into my list    */
  211. /*--------------------------------------------------------------*/
  212.  
  213. static void setenv( var, value )
  214. char *var;
  215. char *value;
  216. {
  217.     register ENV *p;
  218.  
  219.     for ( p = &profile[0];
  220.           *(p->name) != '\0';
  221.           ++p ) 
  222.       {
  223.          if ( strcmp( p->name, var ) == SAME )
  224.         {
  225.  
  226.              strcpy( p->name, var );
  227.            strcpy( p->value, value);
  228.            break;
  229.         }
  230.        }
  231. }
  232.  
  233. /*--------------------------------------------------------------*/
  234. /*    getenv: get pointer to value of environment variable    */
  235. /*--------------------------------------------------------------*/
  236.  
  237. static char *getenv( var )
  238. char *var;
  239. {
  240.     register ENV *p;
  241.  
  242.     for ( p = &profile[0];
  243.           *(p->name) != '\0';
  244.           ++p )
  245.       {
  246.          if ( strcmp( p->name, var ) == SAME )
  247.             if ( *p->value != '\0' )
  248.            return p->value;
  249.         else
  250.            break;
  251.       }
  252.     return NULL;
  253. }
  254.  
  255. /*--------------------------------------------------------------*/
  256. /*    readenv: read environment from a file.             */
  257. /*--------------------------------------------------------------*/
  258.  
  259. static void readenv()
  260. {
  261.    FILE *f;
  262.    int symval;
  263.    char name[MAXLINE+1], value[MAXLINE+1];
  264.  
  265.    /* fprintf( stderr, "Opening profile\n"); /**/
  266.    if ( (f = fopen( PROFILE, "r" )) == NULL )
  267.      {
  268.     fprintf( stderr, "Can't open profile file \"%s\"\n",
  269.     PROFILE );
  270.     exit( 12 );
  271.      }
  272.  
  273.     /* File is layed out as follows:
  274.  
  275.     <environment variable> '=' <ID> | <STRING> # comment....
  276.  
  277.      */
  278.  
  279.     while ( (symval = getsym( f, name )) != EOF )
  280.       {
  281.     /* Skip over any comment lines */
  282.  
  283.     while ( symval == EOL )
  284.        symval = getsym( f, name );
  285.     if ( symval == EOF )
  286.        break;
  287.  
  288.     if ( symval != ID )
  289.       {
  290.          fprintf( stderr, "Bad environment variable name %s\n", name );
  291.          exit( 12 );
  292.       }
  293.  
  294.     if ( (symval = getsym( f, value )) != DELIM )
  295.       {
  296.          fprintf( stderr, "Missing '=' in environment file\n" );
  297.          exit( 12 );
  298.       }
  299.  
  300.     if ( (symval = getsym( f, value )) != ID && symval != STR )
  301.       {
  302.          fprintf( stderr, "missing value in environment file\n");
  303.          exit( 12 );
  304.       }
  305.  
  306.     setenv( name, value );
  307.       }
  308.     fclose( f );
  309.  
  310. }
  311.  
  312. /*--------------------------------------------------------------*/
  313. /*    exitenv: free that memory when done!            */
  314. /*--------------------------------------------------------------*/
  315.  
  316. void exitenv()
  317. {
  318. }
  319.  
  320. static void genv(thename, envname, dflt)
  321. char **thename;
  322. char *envname;
  323. char *dflt;
  324. {
  325.     if ((*thename = getenv( envname )) == NULL)
  326.       {
  327.          fprintf( stderr, "genv: %s not found, using %s\n", envname, dflt );
  328.          *thename = dflt;
  329.          setenv( envname, dflt );
  330.         
  331.       }
  332.     /* fprintf( stderr, "genv: %s \"%s\"\n", envname, *thename ); /**/
  333. }
  334.  
  335. void loadenv()
  336. {
  337.  
  338.     readenv();    /* read the profile from a file */ 
  339.  
  340.     /* get environment var's */
  341.     genv( &name, NAME, DNAME );
  342.     genv( &mailbox, MAILBOX, DMAILBOX );
  343.     genv( &home, HOME, DHOME );
  344.     genv( &domain, DOMAIN, DDOMAIN );
  345.     genv( &maildir, MAILDIR, DMAILDIR );
  346.     genv( &confdir, CONFDIR, DCONFDIR );
  347.     genv( &spooldir, SPOOLDIR, DSPOOLDIR );
  348.     genv( &logdir, LOGDIR, DLOGDIR );
  349.     genv( &pubdir, PUBDIR, DPUBDIR );
  350.     genv( &mailserv, MAILSERVICE, DMAILSERVICE );
  351.     genv( &nodename, NODENAME, DNODENAME );
  352.     genv( &device, DEVICE, DDEVICE );
  353.     genv( &speed, SPEED, DSPEED );
  354.     genv( &tempdir, TEMPDIR, DTEMPDIR );
  355.     genv( &newsdir, NEWSDIR2, DNEWSDIR );
  356. }
  357.  
  358. void mkfilename( filename, dirname, name )
  359. char * filename;
  360. char * dirname;
  361. char * name;
  362. {
  363.     sprintf( filename, FILENAME, dirname, name );    
  364.     /* fprintf( stderr, "New filename %s\n", filename );  /**/    
  365. }
  366.  
  367.  
  368. #ifdef TEST
  369. main()
  370. {
  371.     register ENV *p;
  372.  
  373.     loadenv();
  374.  
  375.     for ( p = &profile[0];
  376.           *(p->name) != '\0';
  377.           ++p)
  378.  
  379.         fprintf( stderr, "name=\"%s\", value=\"%s\"\n",
  380.              p->name, p->value);
  381.  
  382. #endif
  383.  
  384.  
  385.