/* * bauble: a tiny room-based messaging system. * * getcfg(): get configuration information from bauble.cfg */ #include #include #include #include #include #include "news.h" char *NETDIR = 0; /* network control files live here */ char *LOOPZAP= 0; /* loopzapping control files are here */ char *SPOOL = 0; /* spooling network requests/workfiles */ char *EDITOR = 0; /* your favorite text editor */ char *SITE = 0; /* sitename */ char *DOMAIN = 0; /* site domain */ char *MAIL = 0; /* mail goes here */ char *PUBLIC = 0; /* generally accessable public directory */ char *NEWS = 0; /* where news lives, if not NETDIR */ char *ORG = 0; /* your organization name */ char *TZ = 0; /* local timezone */ static struct options { char *name; char **ptr; } options[] = { { "organization", &ORG }, { "netdir", &NETDIR }, { "spool", &SPOOL }, { "mail", &MAIL }, { "public", &PUBLIC }, { "news", &NEWS }, { "loopzap",&LOOPZAP }, { "site", &SITE }, { "domain", &DOMAIN }, { "tz", &TZ } } ; /* * chk() checks directory names out, adding NETDIR prefix if needed, * printing an error message if not defined. */ static chk(p, def, prefix) char **p; char *def; char *prefix; { register char *x; if (*p == 0 && def) *p = strdup(def); if (*p) { if (**p != '\\' && **p != '/' && strchr(*p, ':') == 0) { x = malloc(strlen(*p) + strlen(prefix) + 2); makepath(x, prefix, *p); free(*p); *p = x; } return 1; } panic("out of memory"); return 0; } /* * getcfg() reads in the configuration */ getcfg() { register char *p, *q, *v; FILE *cfg; char text[200]; char arg[100]; register i; if ((p=getenv("BAUBLE")) == (char*)0) p = CFG_DIR; if ((cfg=fopen(makepath(text, p, "bauble.cfg"), "r")) == (FILE*)0) { fprintf(stderr, "can't open config file %s\n", p); return 0; } TZ = (p=getenv("TZ")) ? p : "GMT"; EDITOR = (p=getenv("EDITOR")) ? p : DEFAULT_EDITOR; while (fgets(text, 80, cfg)) { /* normalize the line (a'la Stadel) */ if (sscanf(text, "%[^#\n]", arg) < 1) continue; for (p=q=arg; *p; ) { if (isspace(*p)) { do { ++p; } while (isspace(*p)); if (*p && q != arg) *q++ = ' '; else break; } *q++ = *p++; } *q = 0; if ((p=strtok(arg, " ")) && (q=strtok((char*)0, "\n"))) { strlwr(p); if (sscanf(q, "\"%[^\"]", text) == 1) q = text; for (i=0; i < (sizeof options/sizeof options[0]); i++) if (strcmp(options[i].name, p) == 0) { if ((*(options[i].ptr) = strdup(q)) == (char*)0) { fprintf(stderr, "out of memory getting cfg\n"); return 0; } break; } } /* if (*p) aka we've got a word to parse */ } fclose(cfg); if (NETDIR && SITE && DOMAIN) return chk(&LOOPZAP, "loopzap", NETDIR) && chk(&SPOOL, "spool", NETDIR) && chk(&MAIL, "mail", SPOOL) && chk(&PUBLIC, "public", SPOOL) && chk(&NEWS, "news", SPOOL); fprintf(stderr, "cfgfile needs:"); if (!NETDIR) fprintf(stderr, " NETDIR"); if (!DOMAIN) fprintf(stderr, " DOMAIN"); if (!SITE) fprintf(stderr, " SITE"); putc('\n', stderr); return 0; }