home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.sbin / amd / fsinfo / fsi_lex.l < prev    next >
Encoding:
Lex Description  |  1991-06-26  |  9.2 KB  |  404 lines

  1. %{
  2. /*
  3.  * Copyright (c) 1989 Jan-Simon Pendry
  4.  * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
  5.  * Copyright (c) 1989 The Regents of the University of California.
  6.  * All rights reserved.
  7.  *
  8.  * This code is derived from software contributed to Berkeley by
  9.  * Jan-Simon Pendry at Imperial College, London.
  10.  *
  11.  * Redistribution and use in source and binary forms, with or without
  12.  * modification, are permitted provided that the following conditions
  13.  * are met:
  14.  * 1. Redistributions of source code must retain the above copyright
  15.  *    notice, this list of conditions and the following disclaimer.
  16.  * 2. Redistributions in binary form must reproduce the above copyright
  17.  *    notice, this list of conditions and the following disclaimer in the
  18.  *    documentation and/or other materials provided with the distribution.
  19.  * 3. All advertising materials mentioning features or use of this software
  20.  *    must display the following acknowledgement:
  21.  *    This product includes software developed by the University of
  22.  *    California, Berkeley and its contributors.
  23.  * 4. Neither the name of the University nor the names of its contributors
  24.  *    may be used to endorse or promote products derived from this software
  25.  *    without specific prior written permission.
  26.  *
  27.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  28.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  31.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  33.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  36.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  37.  * SUCH DAMAGE.
  38.  *
  39.  *    @(#)fsi_lex.l    5.4 (Berkeley) 6/26/91
  40.  *
  41.  * $Id: fsi_lex.l,v 5.2.1.3 91/05/07 22:19:03 jsp Alpha $
  42.  *
  43.  */
  44.  
  45. /*
  46.  * Lexical analyzer for fsinfo.
  47.  * TODO: Needs rewriting.
  48.  */
  49.  
  50. static int xinput();
  51. static void xunput();
  52.  
  53. #ifdef FLEX_SCANNER
  54. static int yylineno;
  55. /* Flex support with help from Vern Paxson <vern@helios.ee.lbl.gov> */
  56. #undef YY_INPUT
  57. #define YY_INPUT(buf,result,max_size)        \
  58. {                        \
  59.     int i;                    \
  60.     for (i = 0; i < max_size; i++) {    \
  61.         int ch = xinput(i == 0);    \
  62.         if (ch == 0)            \
  63.             break;            \
  64.         buf[i] = ch;            \
  65.     }                    \
  66.     result = i;                \
  67. }
  68.  
  69. #define    INIT_STATE {                \
  70.         switch ((yy_start - 1) / 2) {    \
  71.         case 0:                \
  72.             BEGIN F;        \
  73.             break;            \
  74.         }                \
  75. }
  76.  
  77.  
  78. #else
  79. /*
  80.  * Using old lex...
  81.  */
  82. #undef unput
  83. #define unput(ch) xunput(ch)
  84. #undef input
  85. #define input() xinput(1)
  86.  
  87. #define    INIT_STATE {                \
  88.         switch (yybgin - yysvec - 1) {    \
  89.         case 0:                \
  90.             BEGIN F;        \
  91.             break;            \
  92.         }                \
  93. }
  94.  
  95. #endif /* FLEX_SCANNER */
  96.  
  97. #include "../fsinfo/fsinfo.h"
  98. #include "fsi_gram.h"
  99. #include <ctype.h>
  100.  
  101. static char *filename;
  102. static char *optr;
  103. static char ostr[1024];
  104. static find_resword();
  105. static unsigned char ibuf[64];
  106. static unsigned char *iptr = ibuf;
  107. static int quoted;
  108. static int lastch, nextch = '\n';
  109. YYSTYPE yylval;
  110.  
  111. struct r {
  112.     char *rw;
  113.     int tok;
  114. } rr[] = {
  115.     { "->", tEQ },
  116.     { "arch", tARCH },
  117.     { "as", tAS },
  118.     { "automount", tAUTOMOUNT },
  119.     { "cluster", tCLUSTER },
  120.     { "config", tCONFIG },
  121.     { "dumpset", tDUMPSET },
  122.     { "exportfs", tEXPORTFS },
  123.     { "freq", tFREQ },
  124.     { "from", tFROM },
  125.     { "fs", tFS },
  126.     { "fstype", tFSTYPE },
  127.     { "host", tHOST },
  128.     { "hwaddr", tHWADDR },
  129.     { "inaddr", tINADDR },
  130.     { "localhost", tLOCALHOST },
  131.     { "log", tLOG },
  132.     { "mount", tMOUNT },
  133.     { "netif", tNETIF },
  134.     { "netmask", tNETMASK },
  135.     { "opts", tOPTS },
  136.     { "os", tOS },
  137.     { "passno", tPASSNO },
  138.     { "sel", tSEL },
  139.     { "volname", tVOLNAME },
  140.     { 0, 0 },
  141. };
  142. #define    NRES_WORDS (sizeof(rr)/sizeof(rr[0])-1)
  143.  
  144. %}
  145.  
  146. %start F Q
  147.  
  148. %%
  149.         INIT_STATE; /* witchcraft */
  150.  
  151. <F>[^ \t\n"={}]+    { return find_resword(yytext); }
  152. <F>[ \t]        ;
  153. <F>"\n"            { yylineno++; }
  154. <F>[={}]        { return *yytext; }
  155.  
  156. <F>\"            { BEGIN Q; optr = ostr; quoted = 1; }
  157. <Q>\n            { yylineno++; yyerror("\" expected"); BEGIN F; }
  158. <Q>\\b            { *optr++ = '\b'; /* escape */ }
  159. <Q>\\t            { *optr++ = '\t'; /* escape */ }
  160. <Q>\\\"            { *optr++ = '\"'; /* escape */ }
  161. <Q>\\\\            { *optr++ = '\\'; /* escape */ }
  162. <Q>\\\n            { yylineno++; /* continue */ }
  163. <Q>\\r            { *optr++ = '\r'; /* escape */ }
  164. <Q>\\n            { *optr++ = '\n'; /* escape */ }
  165. <Q>\\f            { *optr++ = '\f'; /* escape */ }
  166. <Q>\\.            { yyerror("Unknown \\ sequence"); }
  167. <Q>([ \t]|"\\\n"){2,}    { char *p = yytext-1; while (p = strchr(p+1, '\n')) yylineno++; }
  168. <Q>"\\ "        { *optr++ = ' '; /* force space */ }
  169. <Q>\"            { BEGIN F; quoted = 0;
  170.                 *optr = '\0';
  171.                 yylval.s = strdup(ostr);
  172.                 return tSTR;
  173.             }
  174. <Q>.            { *optr++ = *yytext; }
  175.  
  176. %%
  177.  
  178. static int find_resword(s)
  179. char *s;
  180. {
  181.     int tok = 0;
  182.  
  183.     int l = 0, m = NRES_WORDS/2, h = NRES_WORDS-1;
  184.     int rc = 0;
  185.     
  186.     m = NRES_WORDS/2;
  187.  
  188. #define FSTRCMP(p, q) ((*(p) == *(q)) ? strcmp((p)+1, (q)+1) : *(p) - *(q))
  189.  
  190.     while ((l <= h) && (rc = FSTRCMP(s, rr[m].rw))) {
  191.         /*fprintf(stderr, "failed to cmp(%s, %s), %d, %d, %d\n", s, rr[m].rw, l, m, h);*/
  192.         if (rc < 0)
  193.             h = m - 1;
  194.         else
  195.             l = m + 1;
  196.         m = (h + l) / 2;
  197.     }
  198.  
  199.     if (rc == 0)
  200.         tok = rr[m].tok;
  201.  
  202.     switch (tok) {
  203.     case tLOCALHOST:
  204.         s = "${host}";
  205.         /* fall through... */
  206.     case 0:
  207.         yylval.s = strdup(s);
  208.         tok = tSTR;
  209.         /* fall through... */
  210.     default:
  211.         return tok;
  212.     }
  213.  
  214. }
  215.  
  216. int yyerror(s, s1, s2, s3, s4)
  217. char *s;
  218. char *s1, *s2, *s3, *s4;
  219. {
  220.     col_cleanup(0);
  221.     fprintf(stderr, "%s:%d: ", filename ? filename : "/dev/stdin", yylineno);
  222.     fprintf(stderr, s, s1, s2, s3, s4);
  223.     fputc('\n', stderr);
  224.     parse_errors++;
  225. }
  226.  
  227. ioloc *current_location()
  228. {
  229.     ioloc *ip = ALLOC(ioloc);
  230.     ip->i_line = yylineno;
  231.     ip->i_file = filename;
  232.     return ip;
  233. }
  234.  
  235. #ifdef FLEX_SCANNER
  236. #undef yywrap
  237. #endif
  238.  
  239. int yywrap()
  240. {
  241. static int first = 1;
  242.     if (first) {
  243.         char prog[16*1024];
  244.         strcpy(prog, "cpp=cat; test -r /lib/cpp && cpp=/lib/cpp; test -r /usr/libexec/cpp && cpp=/usr/libexec/cpp; for file in ");
  245.         while (*++g_argv) {
  246.             if (access(*g_argv, 4) < 0) {
  247.                 error("\"%s\": Cannot open for reading", *g_argv);
  248.                 file_io_errors++;
  249.             } else {
  250.                 strcat(prog, *g_argv);
  251.                 strcat(prog, " ");
  252.             }
  253.         }
  254.         strcat(prog, "; do $cpp ");
  255.         strcat(prog, idvbuf);
  256.         strcat(prog, " -DHOSTNAME=\'");
  257.         strcat(prog, hostname);
  258.         strcat(prog, "\' \"$file\"; done");
  259.         yyin = popen(prog, "r");
  260.         if (yyin) {
  261.             /*if (filename) free(filename);*/
  262.             filename = strdup("unknown");
  263.             yylineno = 1;
  264.             first = 0;
  265.             return 0;
  266.         } else {
  267.             perror(prog);
  268.         }
  269.     }
  270.  
  271.     if (!first && yyin && pclose(yyin) != 0)
  272.         parse_errors++;
  273.  
  274.     return 1;
  275. }
  276.  
  277. #define xgetc(fp) ((iptr > ibuf) ? (*--iptr) : (lastch = nextch, nextch = getc(fp), (nextch == EOF ? nextch = lastch, EOF : nextch)))
  278.  
  279. static int xinput(need)
  280. int need;
  281. {
  282. static int c_comment = 0;
  283.     int ch, ch2;
  284.  
  285.     do {
  286.         ch = xgetc(yyin);
  287.         /* fprintf(stderr, "ch = %c, %#x, %d\n", ch, ibuf,iptr-ibuf); */
  288.         if (ch == EOF) return 0;
  289.         if (quoted)
  290.             return ch;
  291.         if (c_comment) {
  292.             ch2 = ch;
  293.             do {
  294.                 if (ch2 == '\n') {
  295.                     nextch = '\n';
  296.                     return ch2;
  297.                 }
  298.                 /* C style comment */
  299.                 do {
  300.                     ch2 = getc(yyin);
  301.                     if (ch2 == '\n') {
  302.                         nextch = '\n';
  303.                         return ch2;
  304.                     }
  305.                 } while (ch2 != '*' && ch2 != EOF);
  306.  
  307.                 while (ch2 == '*')
  308.                     ch2 = getc(yyin);
  309.             } while (ch2 != '/' && ch2 != EOF);
  310.             c_comment = 0;
  311.             if (ch2 == EOF)
  312.                 break;
  313.             continue;
  314.         }
  315.  
  316.         if (ch == '#') {
  317.             /*log("lastch = '%c' (%#x)", lastch, lastch);*/
  318.             if (lastch == '\n') {
  319.                 char fname[MAXPATHLEN];
  320.                 char *fptr;
  321.                 if (!need) {
  322.                     xunput('#');
  323.                     nextch = '\n';
  324.                     return 0;
  325.                 }
  326.                 fname[0] = '\0';
  327.                 /* Skip past space */
  328.                 do {
  329.                     ch2 = getc(yyin);
  330.                 } while (ch2 != EOF && ch2 != '\n' && !isdigit(ch2));
  331.                 if (isdigit(ch2)) {
  332.                     /* Read in line number */
  333.                     fptr = fname;
  334.                     do {
  335.                         *fptr++ = ch2;
  336.                         ch2 = getc(yyin);
  337.                     } while (isdigit(ch2));
  338.                     *fptr = '\0';
  339.                     if (fptr != fname)
  340.                         yylineno = atoi(fname) - 1;
  341.                 }
  342.                 /* Skip past space */
  343.                 while (ch2 != EOF && ch2 != '\"' && ch2 != '\n')
  344.                     ch2 = getc(yyin);
  345.                 if (ch2 == '\"') {
  346.                     /* Read file name */
  347.                     fptr = fname;
  348.                     ch2 = getc(yyin);
  349.                     while (ch2 != '\"' && ch2 != EOF && ch2 != EOF) {
  350.                         *fptr++ = ch2;
  351.                         ch2 = getc(yyin);
  352.                     }
  353.                     *fptr = '\0';
  354.                     if (fname[0]) {
  355.                         log("Setting filename to \"%s\"", fname);
  356.                         /*if (filename) free(filename);*/
  357.                         filename = strdup(fname);
  358.                     }
  359.                 }
  360.                 while (ch2 != '\n' && ch2 != EOF)
  361.                     ch2 = getc(yyin);
  362.             } else do {
  363.                 ch2 = getc(yyin);
  364.             } while (ch2 != '\n' && ch2 != EOF);
  365.             if (ch2 == '\n') {
  366.                 nextch = '\n';
  367.                 return ch2;
  368.             }
  369.         } else if (ch == '/') {
  370.             ch2 = getc(yyin);
  371.             if (ch2 == '/') {
  372.                 /* C++ style comment */
  373.                 do {
  374.                     ch2 = getc(yyin);
  375.                 } while (ch2 != '\n' && ch2 != EOF);
  376.                 if (ch2 == '\n') {
  377.                     nextch = '\n';
  378.                     return ch2;
  379.                 }
  380.             } else if (ch2 == '*') {
  381.                 c_comment = 1;
  382.                 continue;
  383.             } else {
  384.                 xunput(ch2);
  385.                 return ch;
  386.             }
  387.         } else {
  388.             return ch;
  389.         }
  390.     } while (ch2 != EOF);
  391.     error("End of file within comment");
  392.     return 0;
  393. }
  394.  
  395. static void xunput(c)
  396. int c;
  397. {
  398.     if (c && c != EOF) {
  399.         if (iptr == ibuf + sizeof(ibuf) - 1)
  400.              fatal("Out of space in lexical pushback");
  401.         *iptr++ = c;
  402.     }
  403. }
  404.