home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 243_01 / cpp2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-12  |  20.8 KB  |  644 lines

  1.  
  2. /*
  3.  *                              C P P 2 . C
  4.  *
  5.  *                         Process #control lines
  6.  *
  7.  * Edit history
  8.  * 13-Nov-84    MM      Split from cpp1.c
  9.  * 06-Jun-85    KR      fixed #include bug in doinclude()
  10.  */
  11.  
  12. #include        "cppdef.h"
  13. #include        "cpp.h"
  14.  
  15. FILE_LOCAL int    openinclude();
  16. FILE_LOCAL void    doinclude();
  17. FILE_LOCAL void    doif();
  18.  
  19.  
  20. #if HOST == SYS_VMS
  21. /*
  22.  * Include the rms stuff.  (We can't just include rms.h as it uses the
  23.  * VaxC-specific library include syntax that Decus CPP doesn't support.
  24.  * By including things by hand, we can CPP ourself.)
  25.  */
  26. #include        <nam.h>
  27. #include        <fab.h>
  28. #include        <rab.h>
  29. #include        <rmsdef.h>
  30. #endif
  31.  
  32. /*
  33.  * Generate (by hand-inspection) a set of unique values for each control
  34.  * operator.  Note that this is not guaranteed to work for non-Ascii
  35.  * machines.  CPP won't compile if there are hash conflicts.
  36.  */
  37.  
  38. #define L_assert        ('a' + ('s' << 1))
  39. #define L_define        ('d' + ('f' << 1))
  40. #define L_elif          ('e' + ('i' << 1))
  41. #define L_else          ('e' + ('s' << 1))
  42. #define L_endif         ('e' + ('d' << 1))
  43. #define L_if            ('i' + (EOS << 1))
  44. #define L_ifdef         ('i' + ('d' << 1))
  45. #define L_ifndef        ('i' + ('n' << 1))
  46. #define L_include       ('i' + ('c' << 1))
  47. #define L_line          ('l' + ('n' << 1))
  48. #define L_nogood        (EOS + (EOS << 1))      /* To catch #i          */
  49. #define L_pragma        ('p' + ('a' << 1))
  50. #define L_undef         ('u' + ('d' << 1))
  51. #if DEBUG
  52. #define L_debug         ('d' + ('b' << 1))      /* #debug               */
  53. #endif
  54.  
  55. int
  56. control(counter)
  57. int             counter;        /* Pending newline counter              */
  58. /*
  59.  * Process #control lines.  Simple commands are processed inline,
  60.  * while complex commands have their own subroutines.
  61.  *
  62.  * The counter is used to force out a newline before #line, and
  63.  * #pragma commands.  This prevents these commands from ending up at
  64.  * the end of the previous line if cpp is invoked with the -C option.
  65.  */
  66. {
  67.         register int            c;
  68.         register char           *tp;
  69.         register int            hash;
  70.         char                    *ep;
  71.  
  72.         c = skipws();
  73.         if (c == '\n' || c == EOF_CHAR)
  74.             return (counter + 1);
  75.         if (!isdigit(c))
  76.             scanid(c);                  /* Get #word to token[]         */
  77.         else {
  78.             unget();                    /* Hack -- allow #123 as a      */
  79.             strcpy(token, "line");      /* synonym for #line 123        */
  80.         }
  81.         hash = (token[1] == EOS) ? L_nogood : (token[0] + (token[2] << 1));
  82.         switch (hash) {
  83.         case L_assert:  tp = "assert";          break;
  84.         case L_define:  tp = "define";          break;
  85.         case L_elif:    tp = "elif";            break;
  86.         case L_else:    tp = "else";            break;
  87.         case L_endif:   tp = "endif";           break;
  88.         case L_if:      tp = "if";              break;
  89.         case L_ifdef:   tp = "ifdef";           break;
  90.         case L_ifndef:  tp = "ifndef";          break;
  91.         case L_include: tp = "include";         break;
  92.         case L_line:    tp = "line";            break;
  93.         case L_pragma:  tp = "pragma";          break;
  94.         case L_undef:   tp = "undef";           break;
  95. #if DEBUG
  96.         case L_debug:   tp = "debug";           break;
  97. #endif
  98.         default:        hash = L_nogood;
  99.         case L_nogood:  tp = "";                break;
  100.         }
  101.         if (!streq(tp, token))
  102.             hash = L_nogood;
  103.         /*
  104.          * hash is set to a unique value corresponding to the
  105.          * control keyword (or L_nogood if we think it's nonsense).
  106.          */
  107.         if (infile->fp == NULL)
  108.             cwarn("Control line \"%s\" within macro expansion", token);
  109.         if (!compiling) {                       /* Not compiling now    */
  110.             switch (hash) {
  111.             case L_if:                          /* These can't turn     */
  112.             case L_ifdef:                       /*  compilation on, but */
  113.             case L_ifndef:                      /*   we must nest #if's */
  114.                 if (++ifptr >= &ifstack[BLK_NEST])
  115.                     goto if_nest_err;
  116.                 *ifptr = 0;                     /* !WAS_COMPILING       */
  117.             case L_line:                        /* Many                 */
  118.             /*
  119.              * Are pragma's always processed?
  120.              */
  121.             case L_pragma:                      /*  options             */
  122.             case L_include:                     /*   are uninteresting  */
  123.             case L_define:                      /*    if we             */
  124.             case L_undef:                       /*     aren't           */
  125.             case L_assert:                      /*      compiling.      */
  126. dump_line:      skipnl();                       /* Ignore rest of line  */
  127.                 return (counter + 1);
  128.             }
  129.         }
  130.         /*
  131.          * Make sure that #line and #pragma are output on a fresh line.
  132.          */
  133.         if (counter > 0 && (hash == L_line || hash == L_pragma)) {
  134.             putchar('\n');
  135.             counter--;
  136.         }
  137.         switch (hash) {
  138.         case L_line:
  139.             /*
  140.              * Parse the line to update the line number and "progname"
  141.              * field and line number for the next input line.
  142.              * Set wrongline to force it out later.
  143.              */
  144.             c = skipws();
  145.             workp = work;                       /* Save name in work    */
  146.             while (c != '\n' && c != EOF_CHAR) {
  147.                 save(c);
  148.                 c = get();
  149.             }
  150.             unget();
  151.             save(EOS);
  152.             /*
  153.              * Split #line argument into <line-number> and <name>
  154.              * We subtract 1 as we want the number of the next line.
  155.              */
  156.             line = atoi(work) - 1;              /* Reset line number    */
  157.             for (tp = work; isdigit(*tp) || type[*tp] == SPA; tp++)
  158.                 ;                               /* Skip over digits     */
  159.             if (*tp != EOS) {                   /* Got a filename, so:  */
  160.                 if (*tp == '"' && (ep = strrchr(tp + 1, '"')) != NULL) {
  161.                     tp++;                       /* Skip over left quote */
  162.                     *ep = EOS;                  /* And ignore right one */
  163.                 }
  164.                 if (infile->progname != NULL)   /* Give up the old name */
  165.                     free(infile->progname);     /* if it's allocated.   */
  166.                 infile->progname = savestring(tp);
  167.             }
  168.             wrongline = TRUE;                   /* Force output later   */
  169.             break;
  170.  
  171.         case L_include:
  172.             doinclude();
  173.             break;
  174.  
  175.         case L_define:
  176.             dodefine();
  177.             break;
  178.  
  179.         case L_undef:
  180.             doundef();
  181.             break;
  182.  
  183.         case L_else:
  184.             if (ifptr == &ifstack[0])
  185.                 goto nest_err;
  186.             else if ((*ifptr & ELSE_SEEN) != 0)
  187.                 goto else_seen_err;
  188.             *ifptr |= ELSE_SEEN;
  189.             if ((*ifptr & WAS_COMPILING) != 0) {
  190.                 if (compiling || (*ifptr & TRUE_SEEN) != 0)
  191.                     compiling = FALSE;
  192.                 else {
  193.                     compiling = TRUE;
  194.                 }
  195.             }
  196.             break;
  197.  
  198.         case L_elif:
  199.             if (ifptr == &ifstack[0])
  200.                 goto nest_err;
  201.             else if ((*ifptr & ELSE_SEEN) != 0) {
  202. else_seen_err:  cerror("#%s may not follow #else", token);
  203.                 goto dump_line;
  204.             }
  205.             if ((*ifptr & (WAS_COMPILING | TRUE_SEEN)) != WAS_COMPILING) {
  206.                 compiling = FALSE;              /* Done compiling stuff */
  207.                 goto dump_line;                 /* Skip this clause     */
  208.             }
  209.             doif(L_if);
  210.             break;
  211.  
  212.         case L_if:
  213.         case L_ifdef:
  214.         case L_ifndef:
  215.             if (++ifptr >= &ifstack[BLK_NEST])
  216. if_nest_err:    cfatal("Too many nested #%s statements", token);
  217.             *ifptr = WAS_COMPILING;
  218.             doif(hash);
  219.             break;
  220.  
  221.         case L_endif:
  222.             if (ifptr == &ifstack[0]) {
  223. nest_err:       cerror("#%s must be in an #if", token);
  224.                 goto dump_line;
  225.             }
  226.             if (!compiling && (*ifptr & WAS_COMPILING) != 0)
  227.                 wrongline = TRUE;
  228.             compiling = (char)((*ifptr & WAS_COMPILING) != 0);
  229.             --ifptr;
  230.             break;
  231.  
  232.         case L_assert:
  233.             if (eval() == 0)
  234.                 cerror("Preprocessor assertion failure", NULLST);
  235.             break;
  236.  
  237.         case L_pragma:
  238.             /*
  239.              * #pragma is provided to pass "options" to later
  240.              * passes of the compiler.  cpp doesn't have any yet.
  241.              */
  242.             printf("#pragma ");
  243.             while ((c = get()) != '\n' && c != EOF_CHAR)
  244.                 cput(c);
  245.             unget();
  246.             break;
  247.  
  248. #if DEBUG
  249.         case L_debug:
  250.             c = skipws();
  251.             unget();
  252.             c = (isdigit(c)) ? eval() : 1;
  253.             if (debug == 0)
  254.                 dumpdef("debug set on");
  255.             debug = c;
  256.             break;
  257. #endif
  258.  
  259.         default:
  260.             /*
  261.              * Undefined #control keyword.
  262.              * Note: the correct behavior may be to warn and
  263.              * pass the line to a subsequent compiler pass.
  264.              * This would allow #asm or similar extensions.
  265.              */
  266.             cerror("Illegal # command \"%s\"", token);
  267.             break;
  268.         }
  269.         if (hash != L_include) {
  270. #if OK_IF_JUNK
  271.             /*
  272.              * Ignore the rest of the #control line so you can write
  273.              *          #if     foo
  274.              *          #endif  foo
  275.              */
  276.             skipnl();
  277. #else
  278.             if (skipws() != '\n') {
  279.                 cwarn("Unexpected text in #control line ignored", NULLST);
  280.                 skipnl();
  281.             }
  282. #endif
  283.         }
  284.         return (counter + 1);
  285. }
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309. FILE_LOCAL
  310. void
  311. doif(hash)
  312. int             hash;
  313. /*
  314.  * Process an #if, #ifdef, or #ifndef.  The latter two are straightforward,
  315.  * while #if needs a subroutine of its own to evaluate the expression.
  316.  *
  317.  * doif() is called only if compiling is TRUE.  If false, compilation
  318.  * is always supressed, so we don't need to evaluate anything.  This
  319.  * supresses unnecessary warnings.
  320.  */
  321. {
  322.         register int            c;
  323.         register int            found;
  324.  
  325.         if ((c = skipws()) == '\n' || c == EOF_CHAR) {
  326.             unget();
  327.             goto badif;
  328.         }
  329.         if (hash == L_if) {
  330.             unget();
  331.             found = (eval() != 0);      /* Evaluate expr, != 0 is  TRUE */
  332.             hash = L_ifdef;             /* #if is now like #ifdef       */
  333.         }
  334.         else {
  335.             if (type[c] != LET)         /* Next non-blank isn't letter  */
  336.                 goto badif;             /* ... is an error              */
  337.             found = (lookid(c) != NULL); /* Look for it in symbol table */
  338.         }
  339.         if (found == (hash == L_ifdef)) {
  340.             compiling = TRUE;
  341.             *ifptr |= TRUE_SEEN;
  342.         }
  343.         else {
  344.             compiling = FALSE;
  345.         }
  346.         return;
  347.  
  348. badif:  cerror("#if, #ifdef, or #ifndef without an argument", NULLST);
  349. #if !OK_IF_JUNK
  350.         skipnl();                               /* Prevent an extra     */
  351.         unget();                                /* Error message        */
  352. #endif
  353.         return;
  354. }
  355.  
  356.  
  357.  
  358. FILE_LOCAL
  359. void
  360. doinclude()
  361. /*
  362.  * Process the #include control line.
  363.  * There are three variations:
  364.  *      #include "file"         search somewhere relative to the
  365.  *                              current source file, if not found,
  366.  *                              treat as #include <file>.
  367.  *      #include <file>         Search in an implementation-dependent
  368.  *                              list of places.
  369.  *      #include token          Expand the token, it must be one of
  370.  *                              "file" or <file>, process as such.
  371.  *
  372.  * Note: the November 12 draft forbids '>' in the #include <file> format.
  373.  * This restriction is unnecessary and not implemented.
  374.  */
  375. {
  376.         register int            c;
  377.         register int            delim;
  378. #if HOST == SYS_VMS
  379.         char                    def_filename[NAM$C_MAXRSS + 1];
  380. #endif
  381.  
  382.         delim = macroid(skipws());
  383.         if (delim != '<' && delim != '"')
  384.             goto incerr;
  385.         if (delim == '<')
  386.             delim = '>';
  387.         workp = work;
  388.         instring = TRUE;                /* Accept all characters        */
  389.         while ((c = get()) != '\n' && c != EOF_CHAR)
  390.             save(c);                    /* Put it away.                 */
  391.         unget();                        /* Force nl after includee      */
  392.         /*
  393.          * The draft is unclear if the following should be done.
  394.          */
  395. #if OK_IF_JUNK
  396.         while (--workp >= work && *workp == ' ')
  397.             ;                           /* Trim blanks from filename    */
  398. #else
  399.         --workp;
  400. #endif
  401.         if (*workp != (char)delim)
  402.             goto incerr;
  403.         *workp = EOS;                   /* Terminate filename           */
  404.         instring = FALSE;
  405. #if HOST == SYS_VMS
  406.         /*
  407.          * Assume the default .h filetype.
  408.          */
  409.         if (!vmsparse(work, ".H", def_filename)) {
  410.             perror(work);               /* Oops.                        */
  411.             goto incerr;
  412.         }
  413.         else if (openinclude(def_filename, (delim == '"')))
  414.             return;
  415. #else
  416.         if (openinclude(work, (delim == '"')))
  417.             return;
  418. #endif
  419.         /*
  420.          * No sense continuing if #include file isn't there.
  421.          */
  422.         cfatal("Cannot open include file \"%s\"", work);
  423.  
  424. incerr: cerror("#include syntax error", NULLST);
  425.         return;
  426. }
  427.  
  428.  
  429.  
  430.  
  431. FILE_LOCAL int
  432. openinclude(filename, searchlocal)
  433. char            *filename;              /* Input file name              */
  434. int             searchlocal;            /* TRUE if #include "file"      */
  435. /*
  436.  * Actually open an include file.  This routine is only called from
  437.  * doinclude() above, but was written as a separate subroutine for
  438.  * programmer convenience.  It searches the list of directories
  439.  * and actually opens the file, linking it into the list of
  440.  * active files.  Returns TRUE if the file was opened, FALSE
  441.  * if openinclude() fails.  No error message is printed.
  442.  */
  443. {
  444.         register char           **incptr;
  445. #if HOST == SYS_VMS
  446. #if NWORK < (NAM$C_MAXRSS + 1)
  447.     << error, NWORK isn't greater than NAM$C_MAXRSS >>
  448. #endif
  449. #endif
  450.         char                    tmpname[NWORK]; /* Filename work area   */
  451.  
  452.         if (searchlocal) {
  453.             /*
  454.              * Look in local directory first
  455.              */
  456. #if HOST == SYS_UNIX
  457.             /*
  458.              * Try to open filename relative to the directory of the current
  459.              * source file (as opposed to the current directory). (ARF, SCK).
  460.              */
  461.             if (filename[0] != '/'
  462.              && hasdirectory(infile->filename, tmpname))
  463.                 strcat(tmpname, filename);
  464.             else {
  465.                 strcpy(tmpname, filename);
  466.             }
  467. #else
  468.             if (!hasdirectory(filename, tmpname)
  469.              && hasdirectory(infile->filename, tmpname))
  470.                 strcat(tmpname, filename);
  471.             else {
  472.                 strcpy(tmpname, filename);
  473.             }
  474. #endif
  475.             if (openfile(tmpname))
  476.                 return (TRUE);
  477.         }
  478.         /*
  479.          * Look in any directories specified by -I command line
  480.          * arguments, then in the builtin search list.
  481.          */
  482.         for (incptr = incdir; incptr < incend; incptr++) {
  483.             if (strlen(*incptr) + strlen(filename) >= (NWORK - 1))
  484.                 cfatal("Filename work buffer overflow", NULLST);
  485.             else {
  486. #if HOST == SYS_UNIX
  487.                 if (filename[0] == '/')
  488.                     strcpy(tmpname, filename);
  489.                 else {
  490.                     sprintf(tmpname, "%s/%s", *incptr, filename);
  491.                 }
  492. #else
  493.                 if (!hasdirectory(filename, tmpname))
  494.                     sprintf(tmpname, "%s%s", *incptr, filename);
  495. #endif
  496.                 if (openfile(tmpname))
  497.                     return (TRUE);
  498.             }
  499.         }
  500.         return (FALSE);
  501. }
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527. FILE_LOCAL int
  528. hasdirectory(source, result)
  529. char            *source;        /* Directory to examine                 */
  530. char            *result;        /* Put directory stuff here             */
  531. /*
  532.  * If a device or directory is found in the source filename string, the
  533.  * node/device/directory part of the string is copied to result and
  534.  * hasdirectory returns TRUE.  Else, nothing is copied and it returns FALSE.
  535.  */
  536. {
  537. #if HOST == SYS_UNIX
  538.         register char           *tp;
  539.  
  540.         if ((tp = strrchr(source, '/')) == NULL)
  541.             return (FALSE);
  542.         else {
  543.             strncpy(result, source, tp - source + 1);
  544.             result[tp - source + 1] = EOS;
  545.             return (TRUE);
  546.         }
  547. #else
  548. #if HOST == SYS_VMS
  549.         if (vmsparse(source, NULLST, result)
  550.          && result[0] != EOS)
  551.             return (TRUE);
  552.         else {
  553.             return (FALSE);
  554.         }
  555. #else
  556.         /*
  557.          * Random DEC operating system (RSX, RT11, RSTS/E)
  558.          */
  559.         register char           *tp;
  560.  
  561.         if ((tp = strrchr(source, ']')) == NULL
  562.          && (tp = strrchr(source, ':')) == NULL)
  563.             return (FALSE);
  564.         else {
  565.             strncpy(result, source, tp - source + 1);
  566.             result[tp - source + 1] = EOS;
  567.             return (TRUE);
  568.         }
  569. #endif
  570. #endif
  571. }
  572.  
  573.  
  574.  
  575. #if HOST == SYS_VMS
  576.  
  577. FILE_LOCAL int
  578. vmsparse(source, defstring, result)
  579. char            *source;
  580. char            *defstring;     /* non-NULL -> default string.          */
  581. char            *result;        /* Size is at least NAM$C_MAXRSS + 1    */
  582. /*
  583.  * Parse the source string, applying the default (properly, using
  584.  * the system parse routine), storing it in result.
  585.  * TRUE if it parsed, FALSE on error.
  586.  *
  587.  * If defstring is NULL, there are no defaults and result gets
  588.  * (just) the node::[directory] part of the string (possibly "")
  589.  */
  590. {
  591.         struct FAB      fab = cc$rms_fab;       /* File access block    */
  592.         struct NAM      nam = cc$rms_nam;       /* File name block      */
  593.         char            fullname[NAM$C_MAXRSS + 1];
  594.         register char   *rp;                    /* Result pointer       */
  595.  
  596.         fab.fab$l_nam = &nam;                   /* fab -> nam           */
  597.         fab.fab$l_fna = source;                 /* Source filename      */
  598.         fab.fab$b_fns = strlen(source);         /* Size of source       */
  599.         fab.fab$l_dna = defstring;              /* Default string       */
  600.         if (defstring != NULLST)
  601.             fab.fab$b_dns = strlen(defstring);  /* Size of default      */
  602.         nam.nam$l_esa = fullname;               /* Expanded filename    */
  603.         nam.nam$b_ess = NAM$C_MAXRSS;           /* Expanded name size   */
  604.         if (sys$parse(&fab) == RMS$_NORMAL) {   /* Parse away           */
  605.             fullname[nam.nam$b_esl] = EOS;      /* Terminate string     */
  606.             result[0] = EOS;                    /* Just in case         */
  607.             rp = &result[0];
  608.             /*
  609.              * Remove stuff added implicitly, accepting node names and
  610.              * dev:[directory] strings (but not process-permanent files).
  611.              */
  612.             if ((nam.nam$l_fnb & NAM$M_PPF) == 0) {
  613.                 if ((nam.nam$l_fnb & NAM$M_NODE) != 0) {
  614.                     strncpy(result, nam.nam$l_node, nam.nam$b_node);
  615.                     rp += nam.nam$b_node;
  616.                     *rp = EOS;
  617.                 }
  618.                 if ((nam.nam$l_fnb & NAM$M_EXP_DEV) != 0) {
  619.                     strncpy(rp, nam.nam$l_dev, nam.nam$b_dev);
  620.                     rp += nam.nam$b_dev;
  621.                 }
  622.                 if ((nam.nam$l_fnb & NAM$M_EXP_DIR) != 0) {
  623.                     strncpy(rp, nam.nam$l_dir, nam.nam$b_dir);
  624.                     rp += nam.nam$b_dir;
  625.                     *rp = EOS;
  626.                 }
  627.             }
  628.             if (defstring != NULLST) {
  629.                 strncpy(rp, nam.nam$l_name, nam.nam$b_name + nam.nam$b_type);
  630.                 rp += nam.nam$b_name + nam.nam$b_type;
  631.                 *rp = EOS;
  632.                 if ((nam.nam$l_fnb & NAM$M_EXP_VER) != 0) {
  633.                     strncpy(rp, nam.nam$l_ver, nam.nam$b_ver);
  634.                     rp[nam.nam$b_ver] = EOS;
  635.                 }
  636.             }
  637.             return (TRUE);
  638.         }
  639.         return (FALSE);
  640. }
  641. #endif
  642.  
  643.  
  644.