home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / b / bmh02src.zip / RC.C < prev    next >
C/C++ Source or Header  |  1992-08-16  |  6KB  |  207 lines

  1. /*
  2.    rc.c : Copyright Paul Healy, EI9GL, 1992.
  3.  
  4.    Derived from bm.
  5.  
  6.    Copyright 1986 Bdale Garbee, All Rights Reserved.
  7.    Permission granted for non-commercial copying and use, provided
  8.    this notice is retained.
  9.    Copyright 1987 1988 Dave Trulli NN2Z, All Rights Reserved.
  10.    Permission granted for non-commercial copying and use, provided
  11.    this notice is retained.
  12.  
  13.    resource filehandling - initialises internal bm user environment.
  14.  
  15.    911217 : Added this header
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <io.h>
  21. #include "rc.h"
  22. #include "pc.h"
  23. #include "misc.h"
  24.  
  25. static int NotLoaded = 1;
  26.  
  27. #ifdef UNIX
  28. #define   RUNCOM   ".bmrc"      /* config file */
  29. #endif
  30. #ifdef MSDOS
  31. #define   RUNCOM   "/bm.rc"     
  32. #endif
  33. #ifndef RUNCOM
  34. #define   RUNCOM   "bm.rc"     
  35. #endif
  36.  
  37. static int parsefile(char *name);
  38.  
  39. /*
  40.  * commands, and their defaults, which are valid in bm.rc / .bmrc
  41.  */
  42. static struct tok {
  43.    char *str;
  44.    char *val;
  45. } rccmds[] = {  
  46.   "alias",    "/alias",        /* alias,    alias file */
  47.   "bufsize",  "40960",         /* bufsize,  internal mail file buffer (40k) */
  48.   "current",  "/current.bmh",  /* current,  file holding current folder/msg */
  49.   "edit",     NULL,            /* editor,   user's favorite text editor */
  50.   "editline", "y",             /* editline, call DOS fn 0A for console input */
  51.   "folder",   NULL,            /* folder,   directory for saving read mail */
  52.   "fullname", NULL,            /* fullname, fullname of user */
  53.   "host",     NULL,            /* hostname, name of this host */
  54.   "smtp",     "/spool/mail",   /* maildir,  mail directory */
  55.   "columns",  "80",            /* maxcol,   number of screen character columns */
  56.   "maxlet",   "300",           /* maxlet,   maximum number of msgs in a file */
  57.   "rows",     "24",            /* maxrow,   number of screen character rows */
  58.   "mqueue",   "/spool/mqueue", /* mqueue,   defined mqueue outbound directory */
  59.   "newsdir",  "/spool/news",   /* newsdir,  news directory */
  60.   "organ",    NULL,            /* organ,    nntp organization header */
  61.   "record",   NULL,            /* record,   record outbound mail in this file */
  62.   "reply",    NULL,            /* replyto,  address for reply-to header */
  63.   "rewrite",  NULL,            /* rewrite,  rewrite file */
  64.   "signature",NULL,            /* signature, location of sig file  */
  65.   "spooldir", "/spool",        /* spooldir, spool directory - areas file etc */
  66.   "mbox",     "mbox",          /* savebox,  name of the mbox text file */
  67.   "screen",   "bios",          /* screen,   screen access mode under Turbo C */
  68.   "source",   NULL,            /* source,   pull in rc info from another file */
  69.   "tsbids",   "y",             /* tsbids,   add time stamp to bids in history file */
  70.   "tmpdir",   NULL,            /* tmpdir,   directory for temporary files */
  71.   "user",     NULL,            /* username, name of this user from rc file */
  72.   "viewer",   NULL,            /* viewer,   program to view files with */
  73.   NULL,       
  74. };
  75.  
  76. /*
  77.  * return the rc type from a line of the configuration file
  78.  */
  79. static int
  80. rctype(char *s)
  81. {
  82.    int i;
  83.  
  84.    for (i=0; rccmds[i].str != NULL; i++) {
  85.       if (strcmp(rccmds[i].str, s) == 0)
  86.          return i;
  87.    }
  88.    return -1;
  89. }
  90.  
  91. /*
  92.  * check for important settings
  93.  */
  94. static int
  95. checkrc(UserVar u)
  96. {
  97.    switch (u) {
  98.       case maildir:
  99.       case mqueue:
  100.          if(access(rccmds[u].val, 0)) {
  101.             fprintf(stderr, "bm: unable to access %s\n", rccmds[u].val);
  102.             return -1;
  103.             }
  104.          break;
  105.       case hostname:
  106.          if(rccmds[u].val == NULL) {
  107.             fprintf(stderr, "bm: %s not set\n", rccmds[u].str);
  108.             return -1;
  109.             }
  110.          break;
  111.       case screen:
  112.          setvideo(rccmds[u].val);
  113.          break;
  114.       case source:
  115.          if (parsefile(rccmds[u].val) == -1)
  116.             return -1;
  117.          break;
  118.       default:
  119.          break;
  120.       }
  121.    return 0;
  122. }
  123.  
  124. static int
  125. parserc(char *s)
  126. {
  127.    int type;
  128.    char *argv[MAXARGS];
  129.  
  130.    if ( (parse(s, argv, MAXARGS) < 2 ) || (type = rctype(argv[0])) == -1) {
  131.       fprintf(stderr, "bm: invalid command '%s'\n", argv[0]);
  132.       return -1;
  133.       }
  134.    else {
  135.       rccmds[type].val = strdup(argv[1]);
  136.       if (checkrc(type) == -1)
  137.          return -1;
  138.       }
  139.    return 0;
  140. }
  141.  
  142. static int
  143. parsefile(char *name)
  144. {
  145.    FILE *rcfp;
  146.    char rcline[LINELEN];
  147.  
  148.    if ( (rcfp = fopen(name, "r")) == NULL) {
  149.       fprintf(stderr, "bm: cannot open '%s', check your installation\n", name);
  150.       return -1;
  151.    }
  152.  
  153.    while (fgets(rcline, sizeof(rcline), rcfp) != NULL) {
  154.       if (*rcline == '#' || *rcline == '\n' ||
  155.           *rcline == '\0' || *rcline == ';')
  156.          continue;
  157.       rip(rcline);
  158.       if (parserc(rcline) == -1) {
  159.          fclose(rcfp);
  160.          return -1;
  161.          }
  162.    }
  163.    return fclose(rcfp);
  164. }
  165.  
  166. char *
  167. getrc(UserVar u)
  168. {
  169.    if (NotLoaded)
  170.       loadconfig();
  171.  
  172.    return rccmds[u].val;
  173. }
  174.  
  175. /*
  176.  * returns -1 if config file couldn't be parsed correctly. 0 otherwise.
  177.  */
  178. int
  179. loadconfig(void)
  180. {
  181.    char runcom[LINELEN], *p;
  182.  
  183.    NotLoaded = 0;
  184.    /*
  185.     *  check for BMHRC/BMRC in the ENV
  186.     */
  187.    if ((p = getenv("BMHRC")) != NULL)
  188.       strcpy(runcom, p);
  189.    else if ( (p = getenv("BMRC")) != NULL ) 
  190.       strcpy(runcom, p);
  191.    else if ((p = getenv("HOME")) != NULL)    /* Try $HOME/RUNCOM */
  192.       sprintf(runcom, "%s/%s", p, RUNCOM);
  193.    else
  194.       strcpy(runcom, RUNCOM);
  195.  
  196.    if (parsefile(runcom) == -1)
  197.       return -1;
  198.    
  199.    if ( (checkrc(maildir) == -1)  ||
  200.         (checkrc(mqueue) == -1)   ||
  201.         (checkrc(hostname) == -1) )
  202.       return -1;
  203.  
  204.    return 0;
  205. }
  206.  
  207.