Annotation of researchv10no/cmd/odist/ape/Future/conf.c, revision 1.1.1.1

1.1       root        1: #include <ctype.h>
                      2: #include <regexp.h>    /* FIXME: use Posix interface. */
                      3: #include <stdio.h>
                      4: #include <stdlib.h>
                      5: #include <string.h>
                      6: #include "conf.h"
                      7: #include "dist.h"
                      8: 
                      9: static char *
                     10: gettok(char *tok, char *src)
                     11: {
                     12:        while (!isspace(*src)) {
                     13:                if (!*src)
                     14:                        break;
                     15:                *tok++ = *src++;
                     16:        }
                     17:        while (isspace(*src))
                     18:                ++src;
                     19:        return src;
                     20: }
                     21: 
                     22: /* return the next line of the config file matching the given args.
                     23:    and argument can be NULL and will be assumed to match.  the return
                     24:    value is in a static data area.  */
                     25: struct conf *
                     26: confread(FILE *fp, char *host, char *user, char *file)
                     27: {
                     28:        static char confline[MAXLINE];
                     29:        static char confhost[MAXLINE], confuser[MAXLINE], conffile[MAXLINE];
                     30:        char *next;
                     31:        static struct conf conf = { confhost, confuser, conffile };
                     32:        int len, flag;
                     33:        regexp *re;
                     34:        regsubexp match[1];
                     35: 
                     36:        for (;;) {
                     37:                if (!fgets(confline, MAXLINE, fp))
                     38:                        return NULL;
                     39:                if (confline[0] == '#')
                     40:                        continue;
                     41:                len = strlen(confline);
                     42:                if (confline[len - 1] != '\n') {
                     43:                        if (feof(fp))
                     44:                                eprintf("readconf: incomplete last line!");
                     45:                        else
                     46:                                eprintf("readconf: line too long or embedded NUL!");
                     47:                        exit(1);
                     48:                }
                     49:                next = gettok(confhost, confline);
                     50:                next = gettok(confuser, next);
                     51:                conf.cmds = gettok(conffile, next);
                     52:                if (host) {
                     53:                        re = regcomp(confhost);
                     54:                        flag = regexec(re, host, match, 1);
                     55:                        free(re);
                     56:                        if (!flag || match[0].ep - match[0].sp != strlen(host))
                     57:                                continue;
                     58:                }
                     59:                if (user) {
                     60:                        re = regcomp(confuser);
                     61:                        flag = regexec(re, user, match, 1);
                     62:                        free(re);
                     63:                        if (!flag || match[0].ep - match[0].sp != strlen(user))
                     64:                                continue;
                     65:                }
                     66:                if (file) {
                     67:                        re = regcomp(conffile);
                     68:                        flag = regexec(re, file, match, 1);
                     69:                        free(re);
                     70:                        if (!flag || match[0].ep - match[0].sp != strlen(file))
                     71:                                continue;
                     72:                }
                     73:                return &conf;
                     74:        }
                     75: }
                     76: 
                     77: /* call functions associated with the commands in the given string
                     78:    return the total of the return values */
                     79: int
                     80: confexec(char *cmdstr, struct confcmds cmdtab[])
                     81: {
                     82:        char cmd[MAXLINE], arg[MAXLINE];
                     83:        int i, np, ret;
                     84: 
                     85:        ret = 0;
                     86:        while (*cmdstr) {
                     87:                for (i = 0; isalnum(*cmdstr); ++cmdstr)
                     88:                        cmd[i++] = *cmdstr;
                     89:                cmd[i] = 0;
                     90:                if (*cmdstr == '(')
                     91:                        for (i = 0, np = 1, ++cmdstr; *cmdstr && np; ++cmdstr) {
                     92:                                if (*cmdstr == '(')
                     93:                                        ++np;
                     94:                                if (*cmdstr == ')' && !--np)
                     95:                                        continue;
                     96:                                arg[i++] = *cmdstr;
                     97:                        }
                     98:                for (i = 0; cmdtab[i].name; ++i)
                     99:                        if (!strcmp(cmd, cmdtab[i].name)) {
                    100:                                ret += (*cmdtab[i].func)(arg);
                    101:                                break;
                    102:                        }
                    103:                while (*cmdstr && !isalnum(*cmdstr))
                    104:                        ++cmdstr;
                    105:        }
                    106:        return ret;
                    107: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.