home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / net / cpp.elif < prev    next >
Internet Message Format  |  1986-06-05  |  12KB

  1. From arnold@emory.UUCP (Arnold D. Robbins {EUCC}) Mon Jun  2 11:21:17 1986
  2. Path: seismo!columbia!caip!princeton!allegra!ulysses!mhuxr!mhuxn!mhuxm!mhuxf!mhuxi!mhuhk!mhuxt!houxm!mtuxo!mtune!akguc!akgua!emory!arnold
  3. From: arnold@emory.UUCP (Arnold D. Robbins {EUCC})
  4. Newsgroups: net.lang.c,net.lang.c++,net.unix-wizards,net.sources
  5. Subject: Patch to 4.2 /lib/cpp for #elif, and C++ style // comments
  6. Message-ID: <1645@emory.UUCP>
  7. Date: 2 Jun 86 15:21:17 GMT
  8. Distribution: net
  9. Organization: Math & Computer Science, Emory University, Atlanta
  10. Lines: 423
  11.  
  12. This bounced when I tried to send it to mod.sources, so I am just posting.
  13. It was announced in various groups last week.
  14. =============================================================================
  15. Below is a context diff, suitable for use with patch, to the 4.2 BSD /lib/cpp.
  16. It provides two enhancements:
  17.  
  18.     1) Code provided by Doug Gwyn of the Ballistics Research Lab to
  19.        implement the recent-SV/ANSI C #elif preprocessor directive. This
  20.        code is not dependant on any command line option, since I figure
  21.        it is something that everyone will want, and it is something
  22.        that will be a standard in the C community. (BTW, the crummy
  23.        indentation is my fault, not his -- it follows the current "style".)
  24.  
  25.     2) Code written by me, based loosely on the existing code, to recognize
  26.        C++ comments that start with // and continue to the end of line.
  27.        This feature must be enabled with the -B command line argument.
  28.        ("B" is reminiscent of the old B language which had the //
  29.        comment, which is where C++ took it back from. Not terribly
  30.        mnemonic, I know.)
  31.  
  32. Those of you with C++ should change your CC shell script to invoke /lib/cpp
  33. with the new -B option.
  34.  
  35. Enjoy,
  36.  
  37. Arnold Robbins
  38. CSNET:    arnold@emory    BITNET:    arnold@emoryu1
  39. ARPA:    arnold%emory.csnet@csnet-relay.arpa
  40. UUCP:    { akgua, decvax, gatech, sb1, sb6, sunatl }!emory!arnold
  41.  
  42. "All this digital stuff is just a fad. Analog is the way to go."
  43.     -- William M. Robbins, 1984
  44. -------- cut here, feed to patch in /usr/src/lib/cpp ------
  45. *** ./README    Tue May 27 14:07:10 1986
  46. --- ../ncpp/README    Tue May 27 15:28:08 1986
  47. ***************
  48. *** 1,6
  49.   #
  50.   # @(#)README 1.2 8/30/82
  51.   #
  52.   August 30, 1982
  53.   Fixed by Kurt Shoens, UCB
  54.   If the "#line n name" occurs, then all future references
  55.  
  56. --- 1,12 -----
  57.   #
  58.   # @(#)README 1.2 8/30/82
  59.   #
  60. + May 27, 1985
  61. + Modified by Arnold Robbins, Emory University Computing Center.
  62. + With the -B option, will recognize C++ style start with // and go to
  63. + the end of the line comments. Also added in code from Doug Gwyn of
  64. + the Ballistics Research Laboratory to implement #elif.
  65.   August 30, 1982
  66.   Fixed by Kurt Shoens, UCB
  67.   If the "#line n name" occurs, then all future references
  68. *** ./cpp.c    Tue May 27 14:07:09 1986
  69. --- ../ncpp/cpp.c    Tue May 27 15:16:12 1986
  70. ***************
  71. *** 153,158
  72.   STATIC    int    nd    = 1;
  73.   STATIC    int    pflag;    /* don't put out lines "# 12 foo.c" */
  74.   int    passcom;    /* don't delete comments */
  75.   STATIC    int rflag;    /* allow macro recursion */
  76.   STATIC    int    ifno;
  77.   # define NPREDEF 20
  78.  
  79. --- 153,159 -----
  80.   STATIC    int    nd    = 1;
  81.   STATIC    int    pflag;    /* don't put out lines "# 12 foo.c" */
  82.   int    passcom;    /* don't delete comments */
  83. + int eolcom;        /* allow // ... \n comments */
  84.   STATIC    int rflag;    /* allow macro recursion */
  85.   STATIC    int    ifno;
  86.   # define NPREDEF 20
  87. ***************
  88. *** 185,190
  89.   STATIC    struct symtab *udfloc;
  90.   STATIC    struct symtab *incloc;
  91.   STATIC    struct symtab *ifloc;
  92.   STATIC    struct symtab *elsloc;
  93.   STATIC    struct symtab *eifloc;
  94.   STATIC    struct symtab *ifdloc;
  95.  
  96. --- 186,192 -----
  97.   STATIC    struct symtab *udfloc;
  98.   STATIC    struct symtab *incloc;
  99.   STATIC    struct symtab *ifloc;
  100. + STATIC    struct symtab *eliloc;        /* DAG -- added */
  101.   STATIC    struct symtab *elsloc;
  102.   STATIC    struct symtab *eifloc;
  103.   STATIC    struct symtab *ifdloc;
  104. ***************
  105. *** 196,201
  106.   STATIC    struct symtab *uflloc;
  107.   STATIC    int    trulvl;
  108.   STATIC    int    flslvl;
  109.   
  110.   sayline() {
  111.       if (pflag==0) fprintf(fout,"# %d \"%s\"\n", lineno[ifno], fnames[ifno]);
  112.  
  113. --- 198,205 -----
  114.   STATIC    struct symtab *uflloc;
  115.   STATIC    int    trulvl;
  116.   STATIC    int    flslvl;
  117. + #define MAX_IF_NESTING    64        /* DAG -- added (must be at least 6) */
  118. + STATIC    int    ifdone[MAX_IF_NESTING];    /* DAG -- added */
  119.   
  120.   sayline() {
  121.       if (pflag==0) fprintf(fout,"# %d \"%s\"\n", lineno[ifno], fnames[ifno]);
  122. ***************
  123. *** 379,385
  124.           else {++p; break;}
  125.       } break;
  126.       case '/': for (;;) {
  127. !         if (*p++=='*') {/* comment */
  128.               if (!passcom) {inp=p-2; dump(); ++flslvl;}
  129.               for (;;) {
  130.                   while (!iscom(*p++));
  131.  
  132. --- 383,390 -----
  133.           else {++p; break;}
  134.       } break;
  135.       case '/': for (;;) {
  136. !         if (*p=='/' && eolcom) {/* C++ style comment to end of line */
  137. !             p++;
  138.               if (!passcom) {inp=p-2; dump(); ++flslvl;}
  139.               for (;;) {
  140.                   while (*p && *p++ != '\n');
  141. ***************
  142. *** 382,387
  143.           if (*p++=='*') {/* comment */
  144.               if (!passcom) {inp=p-2; dump(); ++flslvl;}
  145.               for (;;) {
  146.                   while (!iscom(*p++));
  147.                   if (p[-1]=='*') for (;;) {
  148.                       if (*p++=='/') goto endcom;
  149.  
  150. --- 387,411 -----
  151.               p++;
  152.               if (!passcom) {inp=p-2; dump(); ++flslvl;}
  153.               for (;;) {
  154. +                 while (*p && *p++ != '\n');
  155. +                 if (p[-1]=='\n') {
  156. +                     p--;
  157. +                     goto endcpluscom;
  158. +                 } else if (eob(--p)) {
  159. +                     if (!passcom) {inp=p; p=refill(p);}
  160. +                     else if ((p-inp)>=BUFSIZ) {/* split long comment */
  161. +                         inp=p; p=refill(p);
  162. +                     } else p=refill(p);
  163. +                 } else ++p; /* ignore null byte */
  164. +             }
  165. +         endcpluscom:
  166. +             if (!passcom) {outp=inp=p; --flslvl;}
  167. +             goto newline;
  168. +             break;
  169. +         }
  170. +         else if (*p++=='*') {/* comment */
  171. +             if (!passcom) {inp=p-2; dump(); ++flslvl;}
  172. +             for (;;) {
  173.                   while (!iscom(*p++));
  174.                   if (p[-1]=='*') for (;;) {
  175.                       if (*p++=='/') goto endcom;
  176. ***************
  177. *** 430,435
  178.           }
  179.       } break;
  180.       case '\n': {
  181.           ++lineno[ifno]; if (isslo) {state=LF; return(p);}
  182.   prevlf:
  183.           state=BEG;
  184.  
  185. --- 454,460 -----
  186.           }
  187.       } break;
  188.       case '\n': {
  189. + newline:
  190.           ++lineno[ifno]; if (isslo) {state=LF; return(p);}
  191.   prevlf:
  192.           state=BEG;
  193. ***************
  194. *** 710,716
  195.   #define fasscan() ptrtab=fastab+COFF
  196.   #define sloscan() ptrtab=slotab+COFF
  197.   
  198. ! char *
  199.   control(p) register char *p; {/* find and handle preprocessor control lines */
  200.       register struct symtab *np;
  201.   for (;;) {
  202.  
  203. --- 735,741 -----
  204.   #define fasscan() ptrtab=fastab+COFF
  205.   #define sloscan() ptrtab=slotab+COFF
  206.   
  207. ! void        /* DAG -- bug fix (was (char *)) */
  208.   control(p) register char *p; {/* find and handle preprocessor control lines */
  209.       register struct symtab *np;
  210.   for (;;) {
  211. ***************
  212. *** 723,729
  213.           if (flslvl==0) {p=doincl(p); continue;}
  214.       } else if (np==ifnloc) {/* ifndef */
  215.           ++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
  216. !         if (flslvl==0 && np->value==0) ++trulvl;
  217.           else ++flslvl;
  218.       } else if (np==ifdloc) {/* ifdef */
  219.           ++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
  220.  
  221. --- 748,758 -----
  222.           if (flslvl==0) {p=doincl(p); continue;}
  223.       } else if (np==ifnloc) {/* ifndef */
  224.           ++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
  225. !         if (flslvl==0)
  226. !             if (ifdone[trulvl] = np->value == 0)
  227. !                 ++trulvl;
  228. !             else
  229. !                 ++flslvl;
  230.           else ++flslvl;
  231.       } else if (np==ifdloc) {/* ifdef */
  232.           ++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
  233. ***************
  234. *** 727,733
  235.           else ++flslvl;
  236.       } else if (np==ifdloc) {/* ifdef */
  237.           ++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
  238. !         if (flslvl==0 && np->value!=0) ++trulvl;
  239.           else ++flslvl;
  240.       } else if (np==eifloc) {/* endif */
  241.           if (flslvl) {if (--flslvl==0) sayline();}
  242.  
  243. --- 756,766 -----
  244.           else ++flslvl;
  245.       } else if (np==ifdloc) {/* ifdef */
  246.           ++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
  247. !         if (flslvl==0)
  248. !             if (ifdone[trulvl] = np->value!=0)
  249. !                 ++trulvl;
  250. !             else
  251. !                 ++flslvl;
  252.           else ++flslvl;
  253.       } else if (np==eifloc) {/* endif */
  254.           if (flslvl) {if (--flslvl==0) sayline();}
  255. ***************
  256. *** 731,737
  257.           else ++flslvl;
  258.       } else if (np==eifloc) {/* endif */
  259.           if (flslvl) {if (--flslvl==0) sayline();}
  260. !         else if (trulvl) --trulvl;
  261.           else pperror("If-less endif",0);
  262.       } else if (np==elsloc) {/* else */
  263.           if (flslvl) {
  264.  
  265. --- 764,770 -----
  266.           else ++flslvl;
  267.       } else if (np==eifloc) {/* endif */
  268.           if (flslvl) {if (--flslvl==0) sayline();}
  269. !         else if (trulvl) ifdone[--trulvl] = 0;    /* DAG */
  270.           else pperror("If-less endif",0);
  271.       } else if (np==elsloc) {/* else */
  272.           if (flslvl) {
  273. ***************
  274. *** 735,741
  275.           else pperror("If-less endif",0);
  276.       } else if (np==elsloc) {/* else */
  277.           if (flslvl) {
  278. !             if (--flslvl!=0) ++flslvl;
  279.               else {++trulvl; sayline();}
  280.           }
  281.           else if (trulvl) {++flslvl; --trulvl;}
  282.  
  283. --- 768,774 -----
  284.           else pperror("If-less endif",0);
  285.       } else if (np==elsloc) {/* else */
  286.           if (flslvl) {
  287. !             if (--flslvl!=0 || ifdone[trulvl]) ++flslvl;
  288.               else {++trulvl; sayline();}
  289.           }
  290.           else if (trulvl) {++flslvl; --trulvl;}
  291. ***************
  292. *** 747,753
  293.       } else if (np==ifloc) {/* if */
  294.   #if tgp
  295.           pperror(" IF not implemented, true assumed", 0);
  296. !         if (flslvl==0) ++trulvl; else ++flslvl;
  297.   #else
  298.           newp=p;
  299.           if (flslvl==0 && yyparse()) ++trulvl; else ++flslvl;
  300.  
  301. --- 780,786 -----
  302.       } else if (np==ifloc) {/* if */
  303.   #if tgp
  304.           pperror(" IF not implemented, true assumed", 0);
  305. !         if (flslvl==0) ifdone[trulvl++] = 1; else ++flslvl;
  306.   #else
  307.           newp=p;
  308.           if (flslvl==0)
  309. ***************
  310. *** 750,756
  311.           if (flslvl==0) ++trulvl; else ++flslvl;
  312.   #else
  313.           newp=p;
  314. !         if (flslvl==0 && yyparse()) ++trulvl; else ++flslvl;
  315.           p=newp;
  316.   #endif
  317.       } else if (np==lneloc) {/* line */
  318.  
  319. --- 783,796 -----
  320.           if (flslvl==0) ifdone[trulvl++] = 1; else ++flslvl;
  321.   #else
  322.           newp=p;
  323. !         if (flslvl==0)
  324. !         {
  325. !             if (ifdone[trulvl] = yyparse())    /* DAG */
  326. !                 ++trulvl;
  327. !             else
  328. !                 ++flslvl;
  329. !         }
  330. !         else ++flslvl;
  331.           p=newp;
  332.   #endif
  333.       } else if (np==eliloc) {/* elif */    /* DAG -- added */
  334. ***************
  335. *** 753,758
  336.           if (flslvl==0 && yyparse()) ++trulvl; else ++flslvl;
  337.           p=newp;
  338.   #endif
  339.       } else if (np==lneloc) {/* line */
  340.           if (flslvl==0 && pflag==0) {
  341.               char *cp, *cp2, *savestring();
  342.  
  343. --- 793,839 -----
  344.           else ++flslvl;
  345.           p=newp;
  346.   #endif
  347. +     } else if (np==eliloc) {/* elif */    /* DAG -- added */
  348. + #if tgp
  349. +         pperror ( " ELIF not implemented, true assumed", (char *) 0, (char *) 0);
  350. +         if (flslvl)
  351. +         {
  352. +             if (--flslvl == 0 && !ifdeone[trulvl])
  353. +             {
  354. +                 ifdone[trulvl++] = 1;
  355. +                 sayline():
  356. +             }
  357. +             else
  358. +                 ++flslvl;
  359. +         }
  360. +         else if (trulvl)
  361. +         {
  362. +             ++flslvl;
  363. +             --trulvl;
  364. +         }
  365. +         else
  366. +             pperror ( "If-less elif"< (char *) 0, (char *) 0);
  367. + #else
  368. +         newp = p;
  369. +         if (flslvl)
  370. +         {
  371. +             if (--flslvl == 0 && !ifdone[trulvl] && yyparse())
  372. +             {
  373. +                 ifdone[trulvl++] = 1;
  374. +                 sayline();
  375. +             }
  376. +             else
  377. +                 ++flslvl;
  378. +         }
  379. +         else if (trulvl)
  380. +         {
  381. +             ++flslvl;
  382. +             --trulvl;
  383. +         }
  384. +         else
  385. +             pperror ("If-less elif", (char *) 0, (char *) 0);
  386. +         p = newp;
  387. + #endif
  388.       } else if (np==lneloc) {/* line */
  389.           if (flslvl==0 && pflag==0) {
  390.               char *cp, *cp2, *savestring();
  391. ***************
  392. *** 1067,1072
  393.                   case 'E': continue;
  394.                   case 'R': ++rflag; continue;
  395.                   case 'C': passcom++; continue;
  396.                   case 'D':
  397.                       if (predef>prespc+NPREDEF) {
  398.                           pperror("too many -D options, ignoring %s",argv[i]);
  399.  
  400. --- 1148,1154 -----
  401.                   case 'E': continue;
  402.                   case 'R': ++rflag; continue;
  403.                   case 'C': passcom++; continue;
  404. +                 case 'B': eolcom++; continue;
  405.                   case 'D':
  406.                       if (predef>prespc+NPREDEF) {
  407.                           pperror("too many -D options, ignoring %s",argv[i]);
  408. ***************
  409. *** 1141,1146
  410.       ifdloc=ppsym("ifdef");
  411.       ifnloc=ppsym("ifndef");
  412.       ifloc=ppsym("if");
  413.       lneloc=ppsym("line");
  414.       for (i=sizeof(macbit)/sizeof(macbit[0]); --i>=0; ) macbit[i]=0;
  415.   # if unix
  416.  
  417. --- 1223,1229 -----
  418.       ifdloc=ppsym("ifdef");
  419.       ifnloc=ppsym("ifndef");
  420.       ifloc=ppsym("if");
  421. +     eliloc=ppsym("elif");
  422.       lneloc=ppsym("line");
  423.       for (i=sizeof(macbit)/sizeof(macbit[0]); --i>=0; ) macbit[i]=0;
  424.   # if unix
  425.  
  426. -- 
  427. Arnold Robbins
  428. CSNET:    arnold@emory    BITNET:    arnold@emoryu1
  429. ARPA:    arnold%emory.csnet@csnet-relay.arpa
  430. UUCP:    { akgua, decvax, gatech, sb1, sb6, sunatl }!emory!arnold
  431.  
  432. "All this digital stuff is just a fad. Analog is the way to go."
  433.     -- William M. Robbins, 1984
  434.  
  435.  
  436.