home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume26 / op / part01 / lex.l < prev    next >
Text File  |  1992-12-26  |  6KB  |  288 lines

  1. %{
  2. /* +-------------------------------------------------------------------+ */
  3. /* | Copyright 1991, David Koblas.                                     | */
  4. /* |   Permission to use, copy, modify, and distribute this software   | */
  5. /* |   and its documentation for any purpose and without fee is hereby | */
  6. /* |   granted, provided that the above copyright notice appear in all | */
  7. /* |   copies and that both that copyright notice and this permission  | */
  8. /* |   notice appear in supporting documentation.  This software is    | */
  9. /* |   provided "as is" without express or implied warranty.           | */
  10. /* +-------------------------------------------------------------------+ */
  11.  
  12. #include <stdio.h>
  13. #include <varargs.h>
  14. #include <ctype.h>
  15. #include "defs.h"
  16.  
  17. static cmd_t    *newcmd();
  18. char    *savestr();
  19.  
  20. extern char    *index();
  21.  
  22. int    yyline = 1;
  23.  
  24. %}
  25.  
  26. WS        [ \t]*
  27. NWS        [^ \n\t;]+
  28.  
  29. %s ARGS
  30.  
  31. %%
  32.     int    state = 0;
  33.     cmd_t    *cmd;
  34.  
  35. #[^\n]*            ;
  36. \n            { yyline++; BEGIN 0; }
  37. ^[^ \n\t]+        { cmd = newcmd(yytext); 
  38.                 state = (strcmp(yytext,"DEFAULT")==0) ? 1 : 0;
  39.                 BEGIN ARGS; }
  40. ^{WS}            BEGIN ARGS;
  41. <ARGS>";"        state++;
  42. <ARGS>{NWS}        addarg(state, cmd, yytext);
  43. <ARGS>{WS}        ;
  44. %%
  45. #include <sys/types.h>
  46. #include <sys/stat.h>
  47. #include <syslog.h>
  48.  
  49. msg(va_alist)
  50.  va_dcl
  51. {
  52. #if 0
  53.     va_list    ap;
  54.     char    *s;
  55.  
  56.     va_start(ap);
  57.     s = va_arg(ap, char *);
  58.     fprintf(stderr,"line %d: ",yyline);
  59.     vfprintf(stderr, s, ap);
  60.     fputc('\n', stderr);
  61.     va_end(ap);
  62. #endif
  63. }
  64.  
  65. static addarg(state, cmd, str)
  66. int    state;
  67. cmd_t    *cmd;
  68. char    *str;
  69. {
  70.     if (state == 0) {
  71.         msg("cmd='%s' add arg '%s'",cmd->name,str);
  72.         if (cmd->margs == cmd->nargs)  {
  73.             cmd->margs += cmd->margs;
  74.             cmd->args = (char **)realloc(cmd->args, 
  75.                     sizeof(char *) * cmd->margs);
  76.             if (cmd->args == NULL)
  77.                 fatal("Unable to groupw args");
  78.         }
  79.         cmd->args[cmd->nargs++] = savestr(str);
  80.     } else if (state == 1) {
  81.         msg("cmd='%s' add opt '%s'",cmd->name,str);
  82.         if (cmd->mopts == cmd->nopts) {
  83.             cmd->mopts += cmd->mopts;
  84.             cmd->opts = (char **)realloc(cmd->opts, 
  85.                     sizeof(char *) * cmd->mopts);
  86.             if (cmd->opts == NULL)
  87.                 fatal("Unable to groupw opts");
  88.         }
  89.         cmd->opts[cmd->nopts++] = savestr(str);
  90.     } else {
  91.         fatal("bad state (%d) received\n",state);
  92.     }
  93. }
  94.  
  95. char    *savestr(str)
  96. char    *str;
  97. {
  98.     char    *s = (char *)malloc(strlen(str)+1);
  99.  
  100.     if (s == NULL) 
  101.         fatal("No string space");
  102.  
  103.     strcpy(s, str);
  104.     return s;
  105. }
  106.  
  107. static cmd_t    *newcmd(name)
  108. char    *name;
  109. {
  110.     cmd_t    *cmd = (cmd_t *)malloc(sizeof(cmd_t));
  111.  
  112.     cmd->next = First;
  113.     First = cmd;
  114.  
  115.     if (cmd == NULL)
  116.         fatal("Unable to alloc space for new command");
  117.  
  118.     cmd->name = savestr(name);
  119.     cmd->nargs = 0;        cmd->margs = 16;
  120.     cmd->nopts = 0;        cmd->mopts = 16;
  121.     cmd->args = (char **)malloc(sizeof(char *)*cmd->margs);
  122.     cmd->opts = (char **)malloc(sizeof(char *)*cmd->mopts);
  123.  
  124.     return cmd;
  125. }
  126.  
  127. ReadFile(file)
  128. char    *file;
  129. {
  130.     struct stat    statbuf;
  131.     FILE        *fd;
  132.     
  133.     if ((stat(file, &statbuf) < 0) || 
  134.         (statbuf.st_uid != 0) || /* Owned by root */
  135.         ((statbuf.st_mode & 0077) != 0)) {/* no perm */
  136.         syslog(LOG_ERR, "Permission problems on %s", file);
  137.         fatal("Permission problems on %s", file);
  138.     }
  139.     if ((fd = fopen(file,"r")) == NULL) {
  140.         syslog(LOG_ERR, "Couldn't open on %s", file);
  141.         fatal("Couldn't open %s", file);
  142.     }
  143.  
  144.     yyin = fd;
  145.     yylex();
  146.  
  147.     return 0;
  148. }
  149.  
  150. CountArgs(cmd)
  151. cmd_t    *cmd;
  152. {
  153.     int    i, val;
  154.     int    wild = 0, max = 0;
  155.     char    *cp, *np, str[MAXSTRLEN];
  156.  
  157.     for (i = 0; i < cmd->nargs; i++) {
  158.         np = cmd->args[i];
  159.  
  160.         while ((cp = index(np, '$')) != NULL) {
  161.             if ((cp != cmd->args[i]) && (*(cp-1) == '\\'))
  162.                 np = cp + 1;
  163.             else
  164.                 break;
  165.         }
  166.  
  167.         if (cp == NULL)
  168.             continue;
  169.         if (*(cp+1) == '*') {
  170.             wild = 1;
  171.             continue;
  172.         }
  173.         
  174.         cp++;
  175.         np = cp;
  176.         
  177.         while (isdigit(*cp))
  178.             cp++;
  179.         if ((cp - np) == 0)
  180.             continue;
  181.         strncpy(str, np, cp - np);
  182.         str[cp - np] = '\0';
  183.         val = atoi(str);
  184.         if (val > max)
  185.             max = val;
  186.     }
  187.  
  188.     if (wild)
  189.         return -max;
  190.     return max;
  191. }
  192.  
  193. static int    cmpopts(a, b)
  194. char    *a, *b;
  195. {
  196.     char    *cp_a, *cp_b;
  197.     int    val_a, val_b;
  198.     char    str_a[MAXSTRLEN], str_b[MAXSTRLEN];
  199.  
  200.     if (*a != '$' && *b != '$')
  201.         return 0;
  202.     if (*a == '$' && *b != '$')
  203.         return -1;
  204.     if (*a != '$' && *b == '$')
  205.         return  1;
  206.  
  207.     cp_a = ++a;
  208.     cp_b = ++b;
  209.     while ((*cp_a != '\0') && (*cp_a != '='))
  210.         if (! isdigit(*cp_a))
  211.             break;
  212.     while ((*cp_b != '\0') && (*cp_b != '='))
  213.         if (! isdigit(*cp_b))
  214.             break;
  215.     
  216.     if (*cp_a != '=' && *cp_b != '=')
  217.         return 0;
  218.     if (*cp_a == '=' && *cp_b != '=')
  219.         return -1;
  220.     if (*cp_a != '=' && *cp_b == '=')
  221.         return  1;
  222.  
  223.     strncpy(str_a, a, cp_a - a);
  224.     str_a[cp_a - a] = '\0';
  225.     val_a = atoi(str_a);
  226.     strncpy(str_b, b, cp_b - a);
  227.     str_a[cp_b - b] = '\0';
  228.     val_b = atoi(str_b);
  229.  
  230.     if (val_a < val_b)
  231.         return -1;
  232.     if (val_a > val_b)
  233.         return  1;
  234.     return 0;
  235. }
  236.  
  237. sortopts(cmd)
  238. cmd_t    *cmd;
  239. {
  240.     qsort(cmd->opts, cmd->nopts, sizeof(char *), cmpopts);
  241. }
  242.  
  243. cmd_t    *Build(def, cmd)
  244. cmd_t    *def, *cmd;
  245. {
  246.     cmd_t        *new = newcmd("");
  247.     char        defname[MAXSTRLEN], optname[MAXSTRLEN], *cp;
  248.     int        i, j;
  249.     extern char    *index();
  250.  
  251.     if (cmd == NULL)
  252.         return def;
  253.     if (def == NULL)
  254.         return cmd;
  255.  
  256.     for (i = 0; i < cmd->nargs; i++)
  257.         addarg(0, new, cmd->args[i]);
  258.  
  259.     for (i = 0; i < def->nopts; i++) {
  260.         if ((cp = index(def->opts[i], '=')) == NULL)
  261.             strcpy(defname, def->opts[i]);
  262.         else {
  263.             int    l = cp - def->opts[i];
  264.             strncpy(defname, def->opts[i], l);
  265.             defname[l] = '\0';
  266.         }
  267.         for (j = 0; j < cmd->nopts; j++) {
  268.             if ((cp = index(cmd->opts[j], '=')) == NULL)
  269.                 strcpy(optname, cmd->opts[j]);
  270.             else {
  271.                 int    l = cp - cmd->opts[j];
  272.                 strncpy(optname, cmd->opts[j], l);
  273.                 optname[l] = '\0';
  274.             }
  275.             if (strcmp(defname, optname) == 0)
  276.                 def->opts[i][0] = '\0';
  277.         }
  278.         if (def->opts[i][0] != '\0')
  279.             addarg(1, new, def->opts[i]);
  280.     }
  281.     for (j = 0; j < cmd->nopts; j++)
  282.         addarg(1, new, cmd->opts[j]);
  283.  
  284.     /* sortopts(new); */
  285.  
  286.     return new;
  287. }
  288.