home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / net / cpp.elif / text0000.txt < prev   
Encoding:
Text File  |  1986-06-05  |  11.4 KB  |  426 lines

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