home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / bin / sh / mknodes.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-15  |  10.3 KB  |  431 lines

  1. /*-
  2.  * Copyright (c) 1991 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Kenneth Almquist.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #ifndef lint
  38. char copyright[] =
  39. "@(#) Copyright (c) 1991 The Regents of the University of California.\n\
  40.  All rights reserved.\n";
  41. #endif /* not lint */
  42.  
  43. #ifndef lint
  44. static char sccsid[] = "@(#)mknodes.c    5.1 (Berkeley) 3/7/91";
  45. #endif /* not lint */
  46.  
  47. /*
  48.  * This program reads the nodetypes file and nodes.c.pat file.  It generates
  49.  * the files nodes.h and nodes.c.
  50.  */
  51.  
  52. #include <stdio.h>
  53.  
  54.  
  55. #define MAXTYPES 50        /* max number of node types */
  56. #define MAXFIELDS 20        /* max fields in a structure */
  57. #define BUFLEN 100        /* size of character buffers */
  58.  
  59. /* field types */
  60. #define T_NODE 1        /* union node *field */
  61. #define T_NODELIST 2        /* struct nodelist *field */
  62. #define T_STRING 3
  63. #define T_INT 4            /* int field */
  64. #define T_OTHER 5        /* other */
  65. #define T_TEMP 6        /* don't copy this field */
  66.  
  67.  
  68. struct field {            /* a structure field */
  69.     char *name;        /* name of field */
  70.     int type;            /* type of field */
  71.     char *decl;        /* declaration of field */
  72. };
  73.  
  74.  
  75. struct str {            /* struct representing a node structure */
  76.     char *tag;        /* structure tag */
  77.     int nfields;        /* number of fields in the structure */
  78.     struct field field[MAXFIELDS];    /* the fields of the structure */
  79.     int done;            /* set if fully parsed */
  80. };
  81.  
  82.  
  83. int ntypes;            /* number of node types */
  84. char *nodename[MAXTYPES];    /* names of the nodes */
  85. struct str *nodestr[MAXTYPES];    /* type of structure used by the node */
  86. int nstr;            /* number of structures */
  87. struct str str[MAXTYPES];    /* the structures */
  88. struct str *curstr;        /* current structure */
  89.  
  90.  
  91. FILE *infp = stdin;
  92. char line[1024];
  93. int linno;
  94. char *linep;
  95.  
  96.  
  97. char *savestr();
  98. #define equal(s1, s2)    (strcmp(s1, s2) == 0)
  99.  
  100.  
  101. main(argc, argv)
  102.     char **argv;
  103.     {
  104.     if (argc != 3)
  105.         error("usage: mknodes file\n");
  106.     if ((infp = fopen(argv[1], "r")) == NULL)
  107.         error("Can't open %s", argv[1]);
  108.     while (readline()) {
  109.         if (line[0] == ' ' || line[0] == '\t')
  110.             parsefield();
  111.         else if (line[0] != '\0')
  112.             parsenode();
  113.     }
  114.     output(argv[2]);
  115. }
  116.  
  117.  
  118.  
  119. parsenode() {
  120.     char name[BUFLEN];
  121.     char tag[BUFLEN];
  122.     struct str *sp;
  123.  
  124.     if (curstr && curstr->nfields > 0)
  125.         curstr->done = 1;
  126.     nextfield(name);
  127.     if (! nextfield(tag))
  128.         error("Tag expected");
  129.     if (*linep != '\0')
  130.         error("Garbage at end of line");
  131.     nodename[ntypes] = savestr(name);
  132.     for (sp = str ; sp < str + nstr ; sp++) {
  133.         if (equal(sp->tag, tag))
  134.             break;
  135.     }
  136.     if (sp >= str + nstr) {
  137.         sp->tag = savestr(tag);
  138.         sp->nfields = 0;
  139.         curstr = sp;
  140.         nstr++;
  141.     }
  142.     nodestr[ntypes] = sp;
  143.     ntypes++;
  144. }
  145.  
  146.  
  147. parsefield() {
  148.     char name[BUFLEN];
  149.     char type[BUFLEN];
  150.     char decl[2 * BUFLEN];
  151.     struct field *fp;
  152.  
  153.     if (curstr == NULL || curstr->done)
  154.         error("No current structure to add field to");
  155.     if (! nextfield(name))
  156.         error("No field name");
  157.     if (! nextfield(type))
  158.         error("No field type");
  159.     fp = &curstr->field[curstr->nfields];
  160.     fp->name = savestr(name);
  161.     if (equal(type, "nodeptr")) {
  162.         fp->type = T_NODE;
  163.         sprintf(decl, "union node *%s", name);
  164.     } else if (equal(type, "nodelist")) {
  165.         fp->type = T_NODELIST;
  166.         sprintf(decl, "struct nodelist *%s", name);
  167.     } else if (equal(type, "string")) {
  168.         fp->type = T_STRING;
  169.         sprintf(decl, "char *%s", name);
  170.     } else if (equal(type, "int")) {
  171.         fp->type = T_INT;
  172.         sprintf(decl, "int %s", name);
  173.     } else if (equal(type, "other")) {
  174.         fp->type = T_OTHER;
  175.     } else if (equal(type, "temp")) {
  176.         fp->type = T_TEMP;
  177.     } else {
  178.         error("Unknown type %s", type);
  179.     }
  180.     if (fp->type == T_OTHER || fp->type == T_TEMP) {
  181.         skipbl();
  182.         fp->decl = savestr(linep);
  183.     } else {
  184.         if (*linep)
  185.             error("Garbage at end of line");
  186.         fp->decl = savestr(decl);
  187.     }
  188.     curstr->nfields++;
  189. }
  190.  
  191.  
  192. char writer[] = "\
  193. /*\n\
  194.  * This file was generated by the mknodes program.\n\
  195.  */\n\
  196. \n";
  197.  
  198. output(file)
  199.     char *file;
  200.     {
  201.     FILE *hfile;
  202.     FILE *cfile;
  203.     FILE *patfile;
  204.     int i;
  205.     struct str *sp;
  206.     struct field *fp;
  207.     char *p;
  208.  
  209.     if ((patfile = fopen(file, "r")) == NULL)
  210.         error("Can't open %s", file);
  211.     if ((hfile = fopen("nodes.h", "w")) == NULL)
  212.         error("Can't create nodes.h");
  213.     if ((cfile = fopen("nodes.c", "w")) == NULL)
  214.         error("Can't create nodes.c");
  215.     fputs(writer, hfile);
  216.     for (i = 0 ; i < ntypes ; i++)
  217.         fprintf(hfile, "#define %s %d\n", nodename[i], i);
  218.     fputs("\n\n\n", hfile);
  219.     for (sp = str ; sp < &str[nstr] ; sp++) {
  220.         fprintf(hfile, "struct %s {\n", sp->tag);
  221.         for (i = sp->nfields, fp = sp->field ; --i >= 0 ; fp++) {
  222.             fprintf(hfile, "      %s;\n", fp->decl);
  223.         }
  224.         fputs("};\n\n\n", hfile);
  225.     }
  226.     fputs("union node {\n", hfile);
  227.     fprintf(hfile, "      int type;\n");
  228.     for (sp = str ; sp < &str[nstr] ; sp++) {
  229.         fprintf(hfile, "      struct %s %s;\n", sp->tag, sp->tag);
  230.     }
  231.     fputs("};\n\n\n", hfile);
  232.     fputs("struct nodelist {\n", hfile);
  233.     fputs("\tstruct nodelist *next;\n", hfile);
  234.     fputs("\tunion node *n;\n", hfile);
  235.     fputs("};\n\n\n", hfile);
  236.     fputs("#ifdef __STDC__\n", hfile);
  237.     fputs("union node *copyfunc(union node *);\n", hfile);
  238.     fputs("void freefunc(union node *);\n", hfile);
  239.     fputs("#else\n", hfile);
  240.     fputs("union node *copyfunc();\n", hfile);
  241.     fputs("void freefunc();\n", hfile);
  242.     fputs("#endif\n", hfile);
  243.  
  244.     fputs(writer, cfile);
  245.     while (fgets(line, sizeof line, patfile) != NULL) {
  246.         for (p = line ; *p == ' ' || *p == '\t' ; p++);
  247.         if (equal(p, "%SIZES\n"))
  248.             outsizes(cfile);
  249.         else if (equal(p, "%CALCSIZE\n"))
  250.             outfunc(cfile, 1);
  251.         else if (equal(p, "%COPY\n"))
  252.             outfunc(cfile, 0);
  253.         else
  254.             fputs(line, cfile);
  255.     }
  256. }
  257.  
  258.  
  259.  
  260. outsizes(cfile)
  261.     FILE *cfile;
  262.     {
  263.     int i;
  264.  
  265.     fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes);
  266.     for (i = 0 ; i < ntypes ; i++) {
  267.         fprintf(cfile, "      ALIGN(sizeof (struct %s)),\n", nodestr[i]->tag);
  268.     }
  269.     fprintf(cfile, "};\n");
  270. }
  271.  
  272.  
  273. outfunc(cfile, calcsize)
  274.     FILE *cfile;
  275.     {
  276.     struct str *sp;
  277.     struct field *fp;
  278.     int i;
  279.  
  280.     fputs("      if (n == NULL)\n", cfile);
  281.     if (calcsize)
  282.         fputs("        return;\n", cfile);
  283.     else
  284.         fputs("        return NULL;\n", cfile);
  285.     if (calcsize)
  286.         fputs("      funcblocksize += nodesize[n->type];\n", cfile);
  287.     else {
  288.         fputs("      new = funcblock;\n", cfile);
  289.         fputs("      funcblock += nodesize[n->type];\n", cfile);
  290.     }
  291.     fputs("      switch (n->type) {\n", cfile);
  292.     for (sp = str ; sp < &str[nstr] ; sp++) {
  293.         for (i = 0 ; i < ntypes ; i++) {
  294.             if (nodestr[i] == sp)
  295.                 fprintf(cfile, "      case %s:\n", nodename[i]);
  296.         }
  297.         for (i = sp->nfields ; --i >= 1 ; ) {
  298.             fp = &sp->field[i];
  299.             switch (fp->type) {
  300.             case T_NODE:
  301.                 if (calcsize) {
  302.                     indent(12, cfile);
  303.                     fprintf(cfile, "calcsize(n->%s.%s);\n",
  304.                         sp->tag, fp->name);
  305.                 } else {
  306.                     indent(12, cfile);
  307.                     fprintf(cfile, "new->%s.%s = copynode(n->%s.%s);\n",
  308.                         sp->tag, fp->name, sp->tag, fp->name);
  309.                 }
  310.                 break;
  311.             case T_NODELIST:
  312.                 if (calcsize) {
  313.                     indent(12, cfile);
  314.                     fprintf(cfile, "sizenodelist(n->%s.%s);\n",
  315.                         sp->tag, fp->name);
  316.                 } else {
  317.                     indent(12, cfile);
  318.                     fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s);\n",
  319.                         sp->tag, fp->name, sp->tag, fp->name);
  320.                 }
  321.                 break;
  322.             case T_STRING:
  323.                 if (calcsize) {
  324.                     indent(12, cfile);
  325.                     fprintf(cfile, "funcstringsize += strlen(n->%s.%s) + 1;\n",
  326.                         sp->tag, fp->name);
  327.                 } else {
  328.                     indent(12, cfile);
  329.                     fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s);\n",
  330.                         sp->tag, fp->name, sp->tag, fp->name);
  331.                 }
  332.                 break;
  333.             case T_INT:
  334.             case T_OTHER:
  335.                 if (! calcsize) {
  336.                     indent(12, cfile);
  337.                     fprintf(cfile, "new->%s.%s = n->%s.%s;\n",
  338.                         sp->tag, fp->name, sp->tag, fp->name);
  339.                 }
  340.                 break;
  341.             }
  342.         }
  343.         indent(12, cfile);
  344.         fputs("break;\n", cfile);
  345.     }
  346.     fputs("      };\n", cfile);
  347.     if (! calcsize)
  348.         fputs("      new->type = n->type;\n", cfile);
  349. }
  350.  
  351.  
  352. indent(amount, fp)
  353.     FILE *fp;
  354.     {
  355.     while (amount >= 8) {
  356.         putc('\t', fp);
  357.         amount -= 8;
  358.     }
  359.     while (--amount >= 0) {
  360.         putc(' ', fp);
  361.     }
  362. }
  363.  
  364.  
  365. int
  366. nextfield(buf)
  367.     char *buf;
  368.     {
  369.     register char *p, *q;
  370.  
  371.     p = linep;
  372.     while (*p == ' ' || *p == '\t')
  373.         p++;
  374.     q = buf;
  375.     while (*p != ' ' && *p != '\t' && *p != '\0')
  376.         *q++ = *p++;
  377.     *q = '\0';
  378.     linep = p;
  379.     return (q > buf);
  380. }
  381.  
  382.  
  383. skipbl() {
  384.     while (*linep == ' ' || *linep == '\t')
  385.         linep++;
  386. }
  387.  
  388.  
  389. int
  390. readline() {
  391.     register char *p;
  392.  
  393.     if (fgets(line, 1024, infp) == NULL)
  394.         return 0;
  395.     for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++);
  396.     while (p > line && (p[-1] == ' ' || p[-1] == '\t'))
  397.         p--;
  398.     *p = '\0';
  399.     linep = line;
  400.     linno++;
  401.     if (p - line > BUFLEN)
  402.         error("Line too long");
  403.     return 1;
  404. }
  405.  
  406.  
  407.  
  408. error(msg, a1, a2, a3, a4, a5, a6)
  409.     char *msg;
  410.     {
  411.     fprintf(stderr, "line %d: ", linno);
  412.     fprintf(stderr, msg, a1, a2, a3, a4, a5, a6);
  413.     putc('\n', stderr);
  414.     exit(2);
  415. }
  416.  
  417.  
  418.  
  419. char *
  420. savestr(s)
  421.     char *s;
  422.     {
  423.     register char *p;
  424.     char *malloc();
  425.  
  426.     if ((p = malloc(strlen(s) + 1)) == NULL)
  427.         error("Out of space");
  428.     strcpy(p, s);
  429.     return p;
  430. }
  431.