home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / AP / ASH / ASH-LINU.2 / ASH-LINU / ash-linux-0.2 / mknodes.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-06  |  10.4 KB  |  437 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[] = "from: @(#)mknodes.c    5.1 (Berkeley) 3/7/91";*/
  45. static char rcsid[] = "mknodes.c,v 1.5 1993/09/05 17:32:07 mycroft Exp";
  46. #endif /* not lint */
  47.  
  48. /*
  49.  * This program reads the nodetypes file and nodes.c.pat file.  It generates
  50.  * the files nodes.h and nodes.c.
  51.  */
  52.  
  53. #include <stdio.h>
  54.  
  55.  
  56. #define MAXTYPES 50        /* max number of node types */
  57. #define MAXFIELDS 20        /* max fields in a structure */
  58. #define BUFLEN 100        /* size of character buffers */
  59.  
  60. /* field types */
  61. #define T_NODE 1        /* union node *field */
  62. #define T_NODELIST 2        /* struct nodelist *field */
  63. #define T_STRING 3
  64. #define T_INT 4            /* int field */
  65. #define T_OTHER 5        /* other */
  66. #define T_TEMP 6        /* don't copy this field */
  67.  
  68.  
  69. struct field {            /* a structure field */
  70.     char *name;        /* name of field */
  71.     int type;            /* type of field */
  72.     char *decl;        /* declaration of field */
  73. };
  74.  
  75.  
  76. struct str {            /* struct representing a node structure */
  77.     char *tag;        /* structure tag */
  78.     int nfields;        /* number of fields in the structure */
  79.     struct field field[MAXFIELDS];    /* the fields of the structure */
  80.     int done;            /* set if fully parsed */
  81. };
  82.  
  83.  
  84. int ntypes;            /* number of node types */
  85. char *nodename[MAXTYPES];    /* names of the nodes */
  86. struct str *nodestr[MAXTYPES];    /* type of structure used by the node */
  87. int nstr;            /* number of structures */
  88. struct str str[MAXTYPES];    /* the structures */
  89. struct str *curstr;        /* current structure */
  90.  
  91.  
  92. FILE *infp = stdin;
  93. char line[1024];
  94. int linno;
  95. char *linep;
  96.  
  97.  
  98. char *savestr();
  99. #define equal(s1, s2)    (strcmp(s1, s2) == 0)
  100.  
  101.  
  102. main(argc, argv)
  103.     char **argv;
  104. {
  105.     if (argc != 3) {
  106.         error("usage: mknodes file\n");
  107.         return(1);
  108.     }
  109.     if ((infp = fopen(argv[1], "r")) == NULL) {
  110.         error("Can't open %s", argv[1]);
  111.         return(1);
  112.     }
  113.     while (readline()) {
  114.         if (line[0] == ' ' || line[0] == '\t')
  115.             parsefield();
  116.         else if (line[0] != '\0')
  117.             parsenode();
  118.     }
  119.     output(argv[2]);
  120.     return(0);
  121. }
  122.  
  123.  
  124.  
  125. parsenode() {
  126.     char name[BUFLEN];
  127.     char tag[BUFLEN];
  128.     struct str *sp;
  129.  
  130.     if (curstr && curstr->nfields > 0)
  131.         curstr->done = 1;
  132.     nextfield(name);
  133.     if (! nextfield(tag))
  134.         error("Tag expected");
  135.     if (*linep != '\0')
  136.         error("Garbage at end of line");
  137.     nodename[ntypes] = savestr(name);
  138.     for (sp = str ; sp < str + nstr ; sp++) {
  139.         if (equal(sp->tag, tag))
  140.             break;
  141.     }
  142.     if (sp >= str + nstr) {
  143.         sp->tag = savestr(tag);
  144.         sp->nfields = 0;
  145.         curstr = sp;
  146.         nstr++;
  147.     }
  148.     nodestr[ntypes] = sp;
  149.     ntypes++;
  150. }
  151.  
  152.  
  153. parsefield() {
  154.     char name[BUFLEN];
  155.     char type[BUFLEN];
  156.     char decl[2 * BUFLEN];
  157.     struct field *fp;
  158.  
  159.     if (curstr == NULL || curstr->done)
  160.         error("No current structure to add field to");
  161.     if (! nextfield(name))
  162.         error("No field name");
  163.     if (! nextfield(type))
  164.         error("No field type");
  165.     fp = &curstr->field[curstr->nfields];
  166.     fp->name = savestr(name);
  167.     if (equal(type, "nodeptr")) {
  168.         fp->type = T_NODE;
  169.         sprintf(decl, "union node *%s", name);
  170.     } else if (equal(type, "nodelist")) {
  171.         fp->type = T_NODELIST;
  172.         sprintf(decl, "struct nodelist *%s", name);
  173.     } else if (equal(type, "string")) {
  174.         fp->type = T_STRING;
  175.         sprintf(decl, "char *%s", name);
  176.     } else if (equal(type, "int")) {
  177.         fp->type = T_INT;
  178.         sprintf(decl, "int %s", name);
  179.     } else if (equal(type, "other")) {
  180.         fp->type = T_OTHER;
  181.     } else if (equal(type, "temp")) {
  182.         fp->type = T_TEMP;
  183.     } else {
  184.         error("Unknown type %s", type);
  185.     }
  186.     if (fp->type == T_OTHER || fp->type == T_TEMP) {
  187.         skipbl();
  188.         fp->decl = savestr(linep);
  189.     } else {
  190.         if (*linep)
  191.             error("Garbage at end of line");
  192.         fp->decl = savestr(decl);
  193.     }
  194.     curstr->nfields++;
  195. }
  196.  
  197.  
  198. char writer[] = "\
  199. /*\n\
  200.  * This file was generated by the mknodes program.\n\
  201.  */\n\
  202. \n";
  203.  
  204. output(file)
  205.     char *file;
  206.     {
  207.     FILE *hfile;
  208.     FILE *cfile;
  209.     FILE *patfile;
  210.     int i;
  211.     struct str *sp;
  212.     struct field *fp;
  213.     char *p;
  214.  
  215.     if ((patfile = fopen(file, "r")) == NULL)
  216.         error("Can't open %s", file);
  217.     if ((hfile = fopen("nodes.h", "w")) == NULL)
  218.         error("Can't create nodes.h");
  219.     if ((cfile = fopen("nodes.c", "w")) == NULL)
  220.         error("Can't create nodes.c");
  221.     fputs(writer, hfile);
  222.     for (i = 0 ; i < ntypes ; i++)
  223.         fprintf(hfile, "#define %s %d\n", nodename[i], i);
  224.     fputs("\n\n\n", hfile);
  225.     for (sp = str ; sp < &str[nstr] ; sp++) {
  226.         fprintf(hfile, "struct %s {\n", sp->tag);
  227.         for (i = sp->nfields, fp = sp->field ; --i >= 0 ; fp++) {
  228.             fprintf(hfile, "      %s;\n", fp->decl);
  229.         }
  230.         fputs("};\n\n\n", hfile);
  231.     }
  232.     fputs("union node {\n", hfile);
  233.     fprintf(hfile, "      int type;\n");
  234.     for (sp = str ; sp < &str[nstr] ; sp++) {
  235.         fprintf(hfile, "      struct %s %s;\n", sp->tag, sp->tag);
  236.     }
  237.     fputs("};\n\n\n", hfile);
  238.     fputs("struct nodelist {\n", hfile);
  239.     fputs("\tstruct nodelist *next;\n", hfile);
  240.     fputs("\tunion node *n;\n", hfile);
  241.     fputs("};\n\n\n", hfile);
  242.     fputs("#ifdef __STDC__\n", hfile);
  243.     fputs("union node *copyfunc(union node *);\n", hfile);
  244.     fputs("void freefunc(union node *);\n", hfile);
  245.     fputs("#else\n", hfile);
  246.     fputs("union node *copyfunc();\n", hfile);
  247.     fputs("void freefunc();\n", hfile);
  248.     fputs("#endif\n", hfile);
  249.  
  250.     fputs(writer, cfile);
  251.     while (fgets(line, sizeof line, patfile) != NULL) {
  252.         for (p = line ; *p == ' ' || *p == '\t' ; p++);
  253.         if (equal(p, "%SIZES\n"))
  254.             outsizes(cfile);
  255.         else if (equal(p, "%CALCSIZE\n"))
  256.             outfunc(cfile, 1);
  257.         else if (equal(p, "%COPY\n"))
  258.             outfunc(cfile, 0);
  259.         else
  260.             fputs(line, cfile);
  261.     }
  262. }
  263.  
  264.  
  265.  
  266. outsizes(cfile)
  267.     FILE *cfile;
  268.     {
  269.     int i;
  270.  
  271.     fprintf(cfile, "static const short nodesize[%d] = {\n", ntypes);
  272.     for (i = 0 ; i < ntypes ; i++) {
  273.         fprintf(cfile, "      ALIGN(sizeof (struct %s)),\n", nodestr[i]->tag);
  274.     }
  275.     fprintf(cfile, "};\n");
  276. }
  277.  
  278.  
  279. outfunc(cfile, calcsize)
  280.     FILE *cfile;
  281.     {
  282.     struct str *sp;
  283.     struct field *fp;
  284.     int i;
  285.  
  286.     fputs("      if (n == NULL)\n", cfile);
  287.     if (calcsize)
  288.         fputs("        return;\n", cfile);
  289.     else
  290.         fputs("        return NULL;\n", cfile);
  291.     if (calcsize)
  292.         fputs("      funcblocksize += nodesize[n->type];\n", cfile);
  293.     else {
  294.         fputs("      new = funcblock;\n", cfile);
  295.         fputs("      funcblock += nodesize[n->type];\n", cfile);
  296.     }
  297.     fputs("      switch (n->type) {\n", cfile);
  298.     for (sp = str ; sp < &str[nstr] ; sp++) {
  299.         for (i = 0 ; i < ntypes ; i++) {
  300.             if (nodestr[i] == sp)
  301.                 fprintf(cfile, "      case %s:\n", nodename[i]);
  302.         }
  303.         for (i = sp->nfields ; --i >= 1 ; ) {
  304.             fp = &sp->field[i];
  305.             switch (fp->type) {
  306.             case T_NODE:
  307.                 if (calcsize) {
  308.                     indent(12, cfile);
  309.                     fprintf(cfile, "calcsize(n->%s.%s);\n",
  310.                         sp->tag, fp->name);
  311.                 } else {
  312.                     indent(12, cfile);
  313.                     fprintf(cfile, "new->%s.%s = copynode(n->%s.%s);\n",
  314.                         sp->tag, fp->name, sp->tag, fp->name);
  315.                 }
  316.                 break;
  317.             case T_NODELIST:
  318.                 if (calcsize) {
  319.                     indent(12, cfile);
  320.                     fprintf(cfile, "sizenodelist(n->%s.%s);\n",
  321.                         sp->tag, fp->name);
  322.                 } else {
  323.                     indent(12, cfile);
  324.                     fprintf(cfile, "new->%s.%s = copynodelist(n->%s.%s);\n",
  325.                         sp->tag, fp->name, sp->tag, fp->name);
  326.                 }
  327.                 break;
  328.             case T_STRING:
  329.                 if (calcsize) {
  330.                     indent(12, cfile);
  331.                     fprintf(cfile, "funcstringsize += strlen(n->%s.%s) + 1;\n",
  332.                         sp->tag, fp->name);
  333.                 } else {
  334.                     indent(12, cfile);
  335.                     fprintf(cfile, "new->%s.%s = nodesavestr(n->%s.%s);\n",
  336.                         sp->tag, fp->name, sp->tag, fp->name);
  337.                 }
  338.                 break;
  339.             case T_INT:
  340.             case T_OTHER:
  341.                 if (! calcsize) {
  342.                     indent(12, cfile);
  343.                     fprintf(cfile, "new->%s.%s = n->%s.%s;\n",
  344.                         sp->tag, fp->name, sp->tag, fp->name);
  345.                 }
  346.                 break;
  347.             }
  348.         }
  349.         indent(12, cfile);
  350.         fputs("break;\n", cfile);
  351.     }
  352.     fputs("      };\n", cfile);
  353.     if (! calcsize)
  354.         fputs("      new->type = n->type;\n", cfile);
  355. }
  356.  
  357.  
  358. indent(amount, fp)
  359.     FILE *fp;
  360.     {
  361.     while (amount >= 8) {
  362.         putc('\t', fp);
  363.         amount -= 8;
  364.     }
  365.     while (--amount >= 0) {
  366.         putc(' ', fp);
  367.     }
  368. }
  369.  
  370.  
  371. int
  372. nextfield(buf)
  373.     char *buf;
  374.     {
  375.     register char *p, *q;
  376.  
  377.     p = linep;
  378.     while (*p == ' ' || *p == '\t')
  379.         p++;
  380.     q = buf;
  381.     while (*p != ' ' && *p != '\t' && *p != '\0')
  382.         *q++ = *p++;
  383.     *q = '\0';
  384.     linep = p;
  385.     return (q > buf);
  386. }
  387.  
  388.  
  389. skipbl() {
  390.     while (*linep == ' ' || *linep == '\t')
  391.         linep++;
  392. }
  393.  
  394.  
  395. int
  396. readline() {
  397.     register char *p;
  398.  
  399.     if (fgets(line, 1024, infp) == NULL)
  400.         return 0;
  401.     for (p = line ; *p != '#' && *p != '\n' && *p != '\0' ; p++);
  402.     while (p > line && (p[-1] == ' ' || p[-1] == '\t'))
  403.         p--;
  404.     *p = '\0';
  405.     linep = line;
  406.     linno++;
  407.     if (p - line > BUFLEN)
  408.         error("Line too long");
  409.     return 1;
  410. }
  411.  
  412.  
  413.  
  414. error(msg, a1, a2, a3, a4, a5, a6)
  415.     char *msg;
  416.     {
  417.     fprintf(stderr, "line %d: ", linno);
  418.     fprintf(stderr, msg, a1, a2, a3, a4, a5, a6);
  419.     putc('\n', stderr);
  420.     exit(2);
  421. }
  422.  
  423.  
  424.  
  425. char *
  426. savestr(s)
  427.     char *s;
  428.     {
  429.     register char *p;
  430.     char *malloc();
  431.  
  432.     if ((p = malloc(strlen(s) + 1)) == NULL)
  433.         error("Out of space");
  434.     strcpy(p, s);
  435.     return p;
  436. }
  437.