home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mdepsrc.zip / parse.c < prev    next >
C/C++ Source or Header  |  1996-12-10  |  13KB  |  568 lines

  1. /* $XConsortium: parse.c,v 1.30 94/04/17 20:10:38 gildea Exp $ */
  2. /*
  3.  
  4. Copyright (c) 1993, 1994  X Consortium
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12.  
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  19. X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  20. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22.  
  23. Except as contained in this notice, the name of the X Consortium shall not be
  24. used in advertising or otherwise to promote the sale, use or other dealings
  25. in this Software without prior written authorization from the X Consortium.
  26.  
  27. */
  28.  
  29. #include "def.h"
  30.  
  31. extern char    *directives[];
  32. extern struct inclist    maininclist;
  33.  
  34. find_includes(filep, file, file_red, recursion, failOK)
  35.     struct filepointer    *filep;
  36.     struct inclist        *file, *file_red;
  37.     int            recursion;
  38.     boolean            failOK;
  39. {
  40.     register char    *line;
  41.     register int    type;
  42.     boolean recfailOK;
  43.  
  44.     while (line = getline(filep)) {
  45.         switch(type = deftype(line, filep, file_red, file, TRUE)) {
  46.         case IF:
  47.         doif:
  48.             type = find_includes(filep, file,
  49.                 file_red, recursion+1, failOK);
  50.             while ((type == ELIF) || (type == ELIFFALSE) ||
  51.                    (type == ELIFGUESSFALSE))
  52.                 type = gobble(filep, file, file_red);
  53.             if (type == ELSE)
  54.                 gobble(filep, file, file_red);
  55.             break;
  56.         case IFFALSE:
  57.         case IFGUESSFALSE:
  58.             doiffalse:
  59.             if (type == IFGUESSFALSE || type == ELIFGUESSFALSE)
  60.                 recfailOK = TRUE;
  61.             else
  62.                 recfailOK = failOK;
  63.             type = gobble(filep, file, file_red);
  64.             if (type == ELSE)
  65.                 find_includes(filep, file,
  66.                       file_red, recursion+1, recfailOK);
  67.             else
  68.             if (type == ELIF)
  69.                 goto doif;
  70.             else
  71.             if ((type == ELIFFALSE) || (type == ELIFGUESSFALSE))
  72.                 goto doiffalse;
  73.             break;
  74.         case IFDEF:
  75.         case IFNDEF:
  76.             if ((type == IFDEF && isdefined(line, file_red, NULL))
  77.              || (type == IFNDEF && !isdefined(line, file_red, NULL))) {
  78.                 debug(1,(type == IFNDEF ?
  79.                     "line %d: %s !def'd in %s via %s%s\n" : "",
  80.                     filep->f_line, line,
  81.                     file->i_file, file_red->i_file, ": doit"));
  82.                 type = find_includes(filep, file,
  83.                     file_red, recursion+1, failOK);
  84.                 while (type == ELIF || type == ELIFFALSE || type == ELIFGUESSFALSE)
  85.                     type = gobble(filep, file, file_red);
  86.                 if (type == ELSE)
  87.                     gobble(filep, file, file_red);
  88.             }
  89.             else {
  90.                 debug(1,(type == IFDEF ?
  91.                     "line %d: %s !def'd in %s via %s%s\n" : "",
  92.                     filep->f_line, line,
  93.                     file->i_file, file_red->i_file, ": gobble"));
  94.                 type = gobble(filep, file, file_red);
  95.                 if (type == ELSE)
  96.                     find_includes(filep, file,
  97.                         file_red, recursion+1, failOK);
  98.                 else if (type == ELIF)
  99.                         goto doif;
  100.                 else if (type == ELIFFALSE || type == ELIFGUESSFALSE)
  101.                         goto doiffalse;
  102.             }
  103.             break;
  104.         case ELSE:
  105.         case ELIFFALSE:
  106.         case ELIFGUESSFALSE:
  107.         case ELIF:
  108.             if (!recursion)
  109.                 gobble(filep, file, file_red);
  110.         case ENDIF:
  111.             if (recursion)
  112.                 return(type);
  113.         case DEFINE:
  114.             define(line, file);
  115.             break;
  116.         case UNDEF:
  117.             if (!*line) {
  118.                 warning("%s, line %d: incomplete undef == \"%s\"\n",
  119.                 file_red->i_file, filep->f_line, line);
  120.                 break;
  121.             }
  122.             undefine(line, file_red);
  123.             break;
  124.         case INCLUDE:
  125.             add_include(filep, file, file_red, line, FALSE, failOK);
  126.             break;
  127.         case INCLUDEDOT:
  128.             add_include(filep, file, file_red, line, TRUE, failOK);
  129.             break;
  130.         case ERROR:
  131.                 warning("%s: %d: %s\n", file_red->i_file,
  132.                  filep->f_line, line);
  133.                 break;
  134.             
  135.         case PRAGMA:
  136.         case IDENT:
  137.         case SCCS:
  138.         case EJECT:
  139.             break;
  140.         case -1:
  141.             warning("%s", file_red->i_file);
  142.             if (file_red != file)
  143.                 warning1(" (reading %s)", file->i_file);
  144.             warning1(", line %d: unknown directive == \"%s\"\n",
  145.                  filep->f_line, line);
  146.             break;
  147.         case -2:
  148.             warning("%s", file_red->i_file);
  149.             if (file_red != file)
  150.                 warning1(" (reading %s)", file->i_file);
  151.             warning1(", line %d: incomplete include == \"%s\"\n",
  152.                  filep->f_line, line);
  153.             break;
  154.         }
  155.     }
  156.     return(-1);
  157. }
  158.  
  159. gobble(filep, file, file_red)
  160.     register struct filepointer *filep;
  161.     struct inclist        *file, *file_red;
  162. {
  163.     register char    *line;
  164.     register int    type;
  165.  
  166.     while (line = getline(filep)) {
  167.         switch(type = deftype(line, filep, file_red, file, FALSE)) {
  168.         case IF:
  169.         case IFFALSE:
  170.         case IFGUESSFALSE:
  171.         case IFDEF:
  172.         case IFNDEF:
  173.             type = gobble(filep, file, file_red);
  174.             while ((type == ELIF) || (type == ELIFFALSE) ||
  175.                    (type == ELIFGUESSFALSE))
  176.                 type = gobble(filep, file, file_red);
  177.             if (type == ELSE)
  178.                     (void)gobble(filep, file, file_red);
  179.             break;
  180.         case ELSE:
  181.         case ENDIF:
  182.             debug(0,("%s, line %d: #%s\n",
  183.                 file->i_file, filep->f_line,
  184.                 directives[type]));
  185.             return(type);
  186.         case DEFINE:
  187.         case UNDEF:
  188.         case INCLUDE:
  189.         case INCLUDEDOT:
  190.         case PRAGMA:
  191.         case ERROR:
  192.         case IDENT:
  193.         case SCCS:
  194.         case EJECT:
  195.             break;
  196.         case ELIF:
  197.         case ELIFFALSE:
  198.         case ELIFGUESSFALSE:
  199.             return(type);
  200.         case -1:
  201.             warning("%s, line %d: unknown directive == \"%s\"\n",
  202.                 file_red->i_file, filep->f_line, line);
  203.             break;
  204.         }
  205.     }
  206.     return(-1);
  207. }
  208.  
  209. /*
  210.  * Decide what type of # directive this line is.
  211.  */
  212. int deftype (line, filep, file_red, file, parse_it)
  213.     register char    *line;
  214.     register struct filepointer *filep;
  215.     register struct inclist *file_red, *file;
  216.     int    parse_it;
  217. {
  218.     register char    *p;
  219.     char    *directive, savechar;
  220.     register int    ret;
  221.  
  222.     /*
  223.      * Parse the directive...
  224.      */
  225.     directive=line+1;
  226.     while (*directive == ' ' || *directive == '\t')
  227.         directive++;
  228.  
  229.     p = directive;
  230.     while (*p >= 'a' && *p <= 'z')
  231.         p++;
  232.     savechar = *p;
  233.     *p = '\0';
  234.     ret = match(directive, directives);
  235.     *p = savechar;
  236.  
  237.     /* If we don't recognize this compiler directive or we happen to just
  238.      * be gobbling up text while waiting for an #endif or #elif or #else
  239.      * in the case of an #elif we must check the zero_value and return an
  240.      * ELIF or an ELIFFALSE.
  241.      */
  242.  
  243.     if (ret == ELIF && !parse_it)
  244.     {
  245.         while (*p == ' ' || *p == '\t')
  246.         p++;
  247.         /*
  248.          * parse an expression.
  249.          */
  250.         debug(0,("%s, line %d: #elif %s ",
  251.            file->i_file, filep->f_line, p));
  252.         ret = zero_value(p, filep, file_red);
  253.         if (ret != IF)
  254.         {
  255.         debug(0,("false...\n"));
  256.         if (ret == IFFALSE)
  257.             return(ELIFFALSE);
  258.         else
  259.             return(ELIFGUESSFALSE);
  260.         }
  261.         else
  262.         {
  263.         debug(0,("true...\n"));
  264.         return(ELIF);
  265.         }
  266.     }
  267.  
  268.     if (ret < 0 || ! parse_it)
  269.         return(ret);
  270.  
  271.     /*
  272.      * now decide how to parse the directive, and do it.
  273.      */
  274.     while (*p == ' ' || *p == '\t')
  275.         p++;
  276.     switch (ret) {
  277.     case IF:
  278.         /*
  279.          * parse an expression.
  280.          */
  281.         ret = zero_value(p, filep, file_red);
  282.         debug(0,("%s, line %d: %s #if %s\n",
  283.              file->i_file, filep->f_line, ret?"false":"true", p));
  284.         break;
  285.     case IFDEF:
  286.     case IFNDEF:
  287.         debug(0,("%s, line %d: #%s %s\n",
  288.             file->i_file, filep->f_line, directives[ret], p));
  289.     case UNDEF:
  290.         /*
  291.          * separate the name of a single symbol.
  292.          */
  293.         while (isalnum(*p) || *p == '_')
  294.             *line++ = *p++;
  295.         *line = '\0';
  296.         break;
  297.     case INCLUDE:
  298.         debug(2,("%s, line %d: #include %s\n",
  299.             file->i_file, filep->f_line, p));
  300.  
  301.         /* Support ANSI macro substitution */
  302.         {
  303.             struct symtab *sym = isdefined(p, file_red, NULL);
  304.             while (sym) {
  305.             p = sym->s_value;
  306.             debug(3,("%s : #includes SYMBOL %s = %s\n",
  307.                    file->i_incstring,
  308.                    sym -> s_name,
  309.                    sym -> s_value));
  310.             /* mark file as having included a 'soft include' */
  311.             file->i_included_sym = TRUE; 
  312.             sym = isdefined(p, file_red, NULL);
  313.             }
  314.         }
  315.  
  316.         /*
  317.          * Separate the name of the include file.
  318.          */
  319.         while (*p && *p != '"' && *p != '<')
  320.             p++;
  321.         if (! *p)
  322.             return(-2);
  323.         if (*p++ == '"') {
  324.             ret = INCLUDEDOT;
  325.             while (*p && *p != '"')
  326.                 *line++ = *p++;
  327.         } else
  328.             while (*p && *p != '>')
  329.                 *line++ = *p++;
  330.         *line = '\0';
  331.         break;
  332.     case DEFINE:
  333.         /*
  334.          * copy the definition back to the beginning of the line.
  335.          */
  336.         strcpy (line, p);
  337.         break;
  338.     case ELSE:
  339.     case ENDIF:
  340.     case ELIF:
  341.     case PRAGMA:
  342.     case ERROR:
  343.     case IDENT:
  344.     case SCCS:
  345.     case EJECT:
  346.         debug(0,("%s, line %d: #%s\n",
  347.             file->i_file, filep->f_line, directives[ret]));
  348.         /*
  349.          * nothing to do.
  350.          */
  351.         break;
  352.     }
  353.     return(ret);
  354. }
  355.  
  356. struct symtab *isdefined(symbol, file, srcfile)
  357.     register char    *symbol;
  358.     struct inclist    *file;
  359.     struct inclist    **srcfile;
  360. {
  361.     register struct symtab    *val;
  362.  
  363.     if (val = slookup(symbol, &maininclist)) {
  364.         debug(1,("%s defined on command line\n", symbol));
  365.         if (srcfile != NULL) *srcfile = &maininclist;
  366.         return(val);
  367.     }
  368.     if (val = fdefined(symbol, file, srcfile))
  369.         return(val);
  370.     debug(1,("%s not defined in %s\n", symbol, file->i_file));
  371.     return(NULL);
  372. }
  373.  
  374. struct symtab *fdefined(symbol, file, srcfile)
  375.     register char    *symbol;
  376.     struct inclist    *file;
  377.     struct inclist    **srcfile;
  378. {
  379.     register struct inclist    **ip;
  380.     register struct symtab    *val;
  381.     register int    i;
  382.     static int    recurse_lvl = 0;
  383.  
  384.     if (file->i_defchecked)
  385.         return(NULL);
  386.     file->i_defchecked = TRUE;
  387.     if (val = slookup(symbol, file))
  388.         debug(1,("%s defined in %s as %s\n", symbol, file->i_file, val->s_value));
  389.     if (val == NULL && file->i_list)
  390.         {
  391.         for (ip = file->i_list, i=0; i < file->i_listlen; i++, ip++)
  392.             if (val = fdefined(symbol, *ip, srcfile)) {
  393.                 break;
  394.             }
  395.         }
  396.     else if (val != NULL && srcfile != NULL) *srcfile = file;
  397.     recurse_lvl--;
  398.     file->i_defchecked = FALSE;
  399.  
  400.     return(val);
  401. }
  402.  
  403. /*
  404.  * Return type based on if the #if expression evaluates to 0
  405.  */
  406. zero_value(exp, filep, file_red)
  407.     register char    *exp;
  408.     register struct filepointer *filep;
  409.     register struct inclist *file_red;
  410. {
  411.     if (cppsetup(exp, filep, file_red))
  412.         return(IFFALSE);
  413.     else
  414.         return(IF);
  415. }
  416.  
  417. define(def, file)
  418.     char    *def;
  419.     struct inclist    *file;
  420. {
  421.     char *val;
  422.  
  423.     /* Separate symbol name and its value */
  424.     val = def;
  425.     while (isalnum(*val) || *val == '_')
  426.     val++;
  427.     if (*val)
  428.     *val++ = '\0';
  429.     while (*val == ' ' || *val == '\t')
  430.     val++;
  431.  
  432.     if (!*val)
  433.     val = "1";
  434.     define2(def, val, file);
  435. }
  436.  
  437. define2(name, val, file)
  438.     char    *name, *val;
  439.     struct inclist    *file;
  440. {
  441.     int first, last, below;
  442.     register struct symtab *sp = NULL, *dest;
  443.  
  444.     /* Make space if it's needed */
  445.     if (file->i_defs == NULL)
  446.     {
  447.     file->i_defs = (struct symtab *)
  448.             malloc(sizeof (struct symtab) * SYMTABINC);
  449.     file->i_deflen = SYMTABINC;
  450.     file->i_ndefs = 0;
  451.     }
  452.     else if (file->i_ndefs == file->i_deflen)
  453.     file->i_defs = (struct symtab *)
  454.             realloc(file->i_defs,
  455.                 sizeof(struct symtab)*(file->i_deflen+=SYMTABINC));
  456.  
  457.     if (file->i_defs == NULL)
  458.     fatalerr("malloc()/realloc() failure in insert_defn()\n");
  459.  
  460.     below = first = 0;
  461.     last = file->i_ndefs - 1;
  462.     while (last >= first)
  463.     {
  464.     /* Fast inline binary search */
  465.     register char *s1;
  466.     register char *s2;
  467.     register int middle = (first + last) / 2;
  468.  
  469.     /* Fast inline strchr() */
  470.     s1 = name;
  471.     s2 = file->i_defs[middle].s_name;
  472.     while (*s1++ == *s2++)
  473.         if (s2[-1] == '\0') break;
  474.  
  475.     /* If exact match, set sp and break */
  476.     if (*--s1 == *--s2) 
  477.     {
  478.         sp = file->i_defs + middle;
  479.         break;
  480.     }
  481.  
  482.     /* If name > i_defs[middle] ... */
  483.     if (*s1 > *s2) 
  484.     {
  485.         below = first;
  486.         first = middle + 1;
  487.     }
  488.     /* else ... */
  489.     else
  490.     {
  491.         below = last = middle - 1;
  492.     }
  493.     }
  494.  
  495.     /* Search is done.  If we found an exact match to the symbol name,
  496.        just replace its s_value */
  497.     if (sp != NULL)
  498.     {
  499.     free(sp->s_value);
  500.     sp->s_value = copy(val);
  501.     return;
  502.     }
  503.  
  504.     sp = file->i_defs + file->i_ndefs++;
  505.     dest = file->i_defs + below + 1;
  506.     while (sp > dest)
  507.     {
  508.     *sp = sp[-1];
  509.     sp--;
  510.     }
  511.     sp->s_name = copy(name);
  512.     sp->s_value = copy(val);
  513. }
  514.  
  515. struct symtab *slookup(symbol, file)
  516.     register char    *symbol;
  517.     register struct inclist    *file;
  518. {
  519.     register int first = 0;
  520.     register int last = file->i_ndefs - 1;
  521.  
  522.     if (file) while (last >= first)
  523.     {
  524.         /* Fast inline binary search */
  525.         register char *s1;
  526.         register char *s2;
  527.         register int middle = (first + last) / 2;
  528.  
  529.         /* Fast inline strchr() */
  530.         s1 = symbol;
  531.         s2 = file->i_defs[middle].s_name;
  532.         while (*s1++ == *s2++)
  533.             if (s2[-1] == '\0') break;
  534.  
  535.         /* If exact match, we're done */
  536.         if (*--s1 == *--s2) 
  537.         {
  538.             return file->i_defs + middle;
  539.         }
  540.  
  541.         /* If symbol > i_defs[middle] ... */
  542.         if (*s1 > *s2) 
  543.         {
  544.             first = middle + 1;
  545.         }
  546.         /* else ... */
  547.         else
  548.         {
  549.             last = middle - 1;
  550.         }
  551.     }
  552.     return(NULL);
  553. }
  554.  
  555. undefine(symbol, file)
  556.     char    *symbol;
  557.     register struct inclist    *file;
  558. {
  559.     register struct symtab *ptr;
  560.     struct inclist *srcfile;
  561.     while ((ptr = isdefined(symbol, file, &srcfile)) != NULL)
  562.     {
  563.         srcfile->i_ndefs--;
  564.         for (; ptr < srcfile->i_defs + srcfile->i_ndefs; ptr++)
  565.         *ptr = ptr[1];
  566.     }
  567. }
  568.