home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / unix / volume06 / cpp.pch < prev    next >
Encoding:
Text File  |  1988-09-11  |  12.5 KB  |  439 lines

  1. Subject: v06i044:  Patches to 4.2BSD cpp for #elif, // comments (cpp.patch)
  2. Newsgroups: mod.sources
  3. Approved: rs@mirror.UUCP
  4.  
  5. Submitted by: seismo!gatech!emory!arnold (Arnold D. Robbins {EUCC})
  6. Mod.sources: Volume 6, Issue 44
  7. Archive-name: cpp.patch
  8.  
  9. [  This had been posted in net.bugs.4bsd, etc., but the Arnold and I
  10.    both thought it important enough to be published in mod.sources;
  11.    indeed, he had originally sent this to mod.sources, but it got
  12.    lost in the changeover from John to me.  -r$ ]
  13.  
  14. These patches include mcgill-vision!mouse's bug fix (due to a typo).
  15.  
  16. Basically, with these patches are installed, /lib/cpp gains two new
  17. capabilities: The #elif found in recent versions of System V and in the
  18. draft ANSI standard, and the ability to recognize C++ // comments,
  19. which start with the // delimiter, and go to the end of the line.
  20.  
  21. #elif is automatic: it is fast becoming a standard C feature, and I
  22. feel that everyone would want it. // comments require that the new -B
  23. option be given, for their recognition to be turned on. Sites with C++
  24. should modify their CC shell script to call /lib/cpp with this option.
  25. (I chose -B as sort of mnemonic for the BCPL programming language, from
  26. which // was re-instituted.)
  27.  
  28. Credits: Doug Gwyn of BRL implemented #elif for his System V emulation;
  29. however I typed it in to make the "style" consistent; all typos are
  30. mine. I wrote the // processing code. Enjoy.
  31.  
  32. Unpack this file and feed it to patch while in a cpp source directory.
  33.  
  34. --------------------CUT HERE--------------------
  35. #!/bin/sh
  36. # This is a shell archive.  Remove anything before this line,
  37. # then unpack it by saving it in a file and typing "sh file".
  38. # Contents:  patchkit
  39.  
  40. echo x - patchkit
  41. sed 's/^XX//' > "patchkit" <<'@//E*O*F patchkit//'
  42. XX*** ./README    Tue May 27 14:07:10 1986
  43. XX--- ../ncpp/README    Tue May 27 15:28:08 1986
  44. XX***************
  45. XX*** 1,6
  46. XX  #
  47. XX  # @(#)README 1.2 8/30/82
  48. XX  #
  49. XX  August 30, 1982
  50. XX  Fixed by Kurt Shoens, UCB
  51. XX  If the "#line n name" occurs, then all future references
  52.  
  53. XX--- 1,12 -----
  54. XX  #
  55. XX  # @(#)README 1.2 8/30/82
  56. XX  #
  57. XX+ May 27, 1985
  58. XX+ Modified by Arnold Robbins, Emory University Computing Center
  59. XX+ With the -B option, will recognize C++ style start with // and go to
  60. XX+ the end of the line comments. Also added in code from Doug Gwyn of
  61. XX+ the Ballistics Research Laboratory to implement #elif.
  62. XX+ 
  63. XX  August 30, 1982
  64. XX  Fixed by Kurt Shoens, UCB
  65. XX  If the "#line n name" occurs, then all future references
  66. XX*** ./cpp.c    Tue May 27 14:07:09 1986
  67. XX--- ../ncpp/cpp.c    Tue May 27 15:16:12 1986
  68. XX***************
  69. XX*** 153,158
  70. XX  STATIC    int    nd    = 1;
  71. XX  STATIC    int    pflag;    /* don't put out lines "# 12 foo.c" */
  72. XX  int    passcom;    /* don't delete comments */
  73. XX  STATIC    int rflag;    /* allow macro recursion */
  74. XX  STATIC    int    ifno;
  75. XX  # define NPREDEF 20
  76.  
  77. XX--- 153,159 -----
  78. XX  STATIC    int    nd    = 1;
  79. XX  STATIC    int    pflag;    /* don't put out lines "# 12 foo.c" */
  80. XX  int    passcom;    /* don't delete comments */
  81. XX+ int eolcom;        /* allow // ... \n comments */
  82. XX  STATIC    int rflag;    /* allow macro recursion */
  83. XX  STATIC    int    ifno;
  84. XX  # define NPREDEF 20
  85. XX***************
  86. XX*** 185,190
  87. XX  STATIC    struct symtab *udfloc;
  88. XX  STATIC    struct symtab *incloc;
  89. XX  STATIC    struct symtab *ifloc;
  90. XX  STATIC    struct symtab *elsloc;
  91. XX  STATIC    struct symtab *eifloc;
  92. XX  STATIC    struct symtab *ifdloc;
  93.  
  94. XX--- 186,192 -----
  95. XX  STATIC    struct symtab *udfloc;
  96. XX  STATIC    struct symtab *incloc;
  97. XX  STATIC    struct symtab *ifloc;
  98. XX+ STATIC    struct symtab *eliloc;        /* DAG -- added */
  99. XX  STATIC    struct symtab *elsloc;
  100. XX  STATIC    struct symtab *eifloc;
  101. XX  STATIC    struct symtab *ifdloc;
  102. XX***************
  103. XX*** 196,201
  104. XX  STATIC    struct symtab *uflloc;
  105. XX  STATIC    int    trulvl;
  106. XX  STATIC    int    flslvl;
  107. XX  
  108. XX  sayline() {
  109. XX      if (pflag==0) fprintf(fout,"# %d \"%s\"\n", lineno[ifno], fnames[ifno]);
  110.  
  111. XX--- 198,205 -----
  112. XX  STATIC    struct symtab *uflloc;
  113. XX  STATIC    int    trulvl;
  114. XX  STATIC    int    flslvl;
  115. XX+ #define MAX_IF_NESTING    64        /* DAD -- added (must be at least 6) */
  116. XX+ STATIC    int    ifdone[MAX_IF_NESTING];    /* DAG -- added */
  117. XX  
  118. XX  sayline() {
  119. XX      if (pflag==0) fprintf(fout,"# %d \"%s\"\n", lineno[ifno], fnames[ifno]);
  120. XX***************
  121. XX*** 379,385
  122. XX          else {++p; break;}
  123. XX      } break;
  124. XX      case '/': for (;;) {
  125. XX!         if (*p++=='*') {/* comment */
  126. XX              if (!passcom) {inp=p-2; dump(); ++flslvl;}
  127. XX              for (;;) {
  128. XX                  while (!iscom(*p++));
  129.  
  130. XX--- 383,390 -----
  131. XX          else {++p; break;}
  132. XX      } break;
  133. XX      case '/': for (;;) {
  134. XX!         if (*p=='/' && eolcom) {/* C++ style comment to end of line */
  135. XX!             p++;
  136. XX              if (!passcom) {inp=p-2; dump(); ++flslvl;}
  137. XX              for (;;) {
  138. XX                  while (*p && *p++ != '\n');
  139. XX***************
  140. XX*** 382,387
  141. XX          if (*p++=='*') {/* comment */
  142. XX              if (!passcom) {inp=p-2; dump(); ++flslvl;}
  143. XX              for (;;) {
  144. XX                  while (!iscom(*p++));
  145. XX                  if (p[-1]=='*') for (;;) {
  146. XX                      if (*p++=='/') goto endcom;
  147.  
  148. XX--- 387,411 -----
  149. XX              p++;
  150. XX              if (!passcom) {inp=p-2; dump(); ++flslvl;}
  151. XX              for (;;) {
  152. XX+                 while (*p && *p++ != '\n');
  153. XX+                 if (p[-1]=='\n') {
  154. XX+                     p--;
  155. XX+                     goto endcpluscom;
  156. XX+                 } else if (eob(--p)) {
  157. XX+                     if (!passcom) {inp=p; p=refill(p);}
  158. XX+                     else if ((p-inp)>=BUFSIZ) {/* split long comment */
  159. XX+                         inp=p; p=refill(p);
  160. XX+                     } else p=refill(p);
  161. XX+                 } else ++p; /* ignore null byte */
  162. XX+             }
  163. XX+         endcpluscom:
  164. XX+             if (!passcom) {outp=inp=p; --flslvl;}
  165. XX+             goto newline;
  166. XX+             break;
  167. XX+         }
  168. XX+         else if (*p++=='*') {/* comment */
  169. XX+             if (!passcom) {inp=p-2; dump(); ++flslvl;}
  170. XX+             for (;;) {
  171. XX                  while (!iscom(*p++));
  172. XX                  if (p[-1]=='*') for (;;) {
  173. XX                      if (*p++=='/') goto endcom;
  174. XX***************
  175. XX*** 430,435
  176. XX          }
  177. XX      } break;
  178. XX      case '\n': {
  179. XX          ++lineno[ifno]; if (isslo) {state=LF; return(p);}
  180. XX  prevlf:
  181. XX          state=BEG;
  182.  
  183. XX--- 454,460 -----
  184. XX          }
  185. XX      } break;
  186. XX      case '\n': {
  187. XX+ newline:
  188. XX          ++lineno[ifno]; if (isslo) {state=LF; return(p);}
  189. XX  prevlf:
  190. XX          state=BEG;
  191. XX***************
  192. XX*** 710,716
  193. XX  #define fasscan() ptrtab=fastab+COFF
  194. XX  #define sloscan() ptrtab=slotab+COFF
  195. XX  
  196. XX! char *
  197. XX  control(p) register char *p; {/* find and handle preprocessor control lines */
  198. XX      register struct symtab *np;
  199. XX  for (;;) {
  200.  
  201. XX--- 735,741 -----
  202. XX  #define fasscan() ptrtab=fastab+COFF
  203. XX  #define sloscan() ptrtab=slotab+COFF
  204. XX  
  205. XX! void        /* DAG -- bug fix (was (char *)) */
  206. XX  control(p) register char *p; {/* find and handle preprocessor control lines */
  207. XX      register struct symtab *np;
  208. XX  for (;;) {
  209. XX***************
  210. XX*** 723,729
  211. XX          if (flslvl==0) {p=doincl(p); continue;}
  212. XX      } else if (np==ifnloc) {/* ifndef */
  213. XX          ++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
  214. XX!         if (flslvl==0 && np->value==0) ++trulvl;
  215. XX          else ++flslvl;
  216. XX      } else if (np==ifdloc) {/* ifdef */
  217. XX          ++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
  218.  
  219. XX--- 748,758 -----
  220. XX          if (flslvl==0) {p=doincl(p); continue;}
  221. XX      } else if (np==ifnloc) {/* ifndef */
  222. XX          ++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
  223. XX!         if (flslvl==0)
  224. XX!             if (ifdone[trulvl] = np->value == 0)
  225. XX!                 ++trulvl;
  226. XX!             else
  227. XX!                 ++flslvl;
  228. XX          else ++flslvl;
  229. XX      } else if (np==ifdloc) {/* ifdef */
  230. XX          ++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
  231. XX***************
  232. XX*** 727,733
  233. XX          else ++flslvl;
  234. XX      } else if (np==ifdloc) {/* ifdef */
  235. XX          ++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
  236. XX!         if (flslvl==0 && np->value!=0) ++trulvl;
  237. XX          else ++flslvl;
  238. XX      } else if (np==eifloc) {/* endif */
  239. XX          if (flslvl) {if (--flslvl==0) sayline();}
  240.  
  241. XX--- 756,766 -----
  242. XX          else ++flslvl;
  243. XX      } else if (np==ifdloc) {/* ifdef */
  244. XX          ++flslvl; p=skipbl(p); np=slookup(inp,p,0); --flslvl;
  245. XX!         if (flslvl==0)
  246. XX!             if (ifdone[trulvl] = np->value!=0)
  247. XX!                 ++trulvl;
  248. XX!             else
  249. XX!                 ++flslvl;
  250. XX          else ++flslvl;
  251. XX      } else if (np==eifloc) {/* endif */
  252. XX          if (flslvl) {if (--flslvl==0) sayline();}
  253. XX***************
  254. XX*** 731,737
  255. XX          else ++flslvl;
  256. XX      } else if (np==eifloc) {/* endif */
  257. XX          if (flslvl) {if (--flslvl==0) sayline();}
  258. XX!         else if (trulvl) --trulvl;
  259. XX          else pperror("If-less endif",0);
  260. XX      } else if (np==elsloc) {/* else */
  261. XX          if (flslvl) {
  262.  
  263. XX--- 764,770 -----
  264. XX          else ++flslvl;
  265. XX      } else if (np==eifloc) {/* endif */
  266. XX          if (flslvl) {if (--flslvl==0) sayline();}
  267. XX!         else if (trulvl) ifdone[--trulvl] = 0;    /* DAG */
  268. XX          else pperror("If-less endif",0);
  269. XX      } else if (np==elsloc) {/* else */
  270. XX          if (flslvl) {
  271. XX***************
  272. XX*** 735,741
  273. XX          else pperror("If-less endif",0);
  274. XX      } else if (np==elsloc) {/* else */
  275. XX          if (flslvl) {
  276. XX!             if (--flslvl!=0) ++flslvl;
  277. XX              else {++trulvl; sayline();}
  278. XX          }
  279. XX          else if (trulvl) {++flslvl; --trulvl;}
  280.  
  281. XX--- 768,774 -----
  282. XX          else pperror("If-less endif",0);
  283. XX      } else if (np==elsloc) {/* else */
  284. XX          if (flslvl) {
  285. XX!             if (--flslvl!=0 || ifdone[trulvl]) ++flslvl;
  286. XX              else {++trulvl; sayline();}
  287. XX          }
  288. XX          else if (trulvl) {++flslvl; --trulvl;}
  289. XX***************
  290. XX*** 747,753
  291. XX      } else if (np==ifloc) {/* if */
  292. XX  #if tgp
  293. XX          pperror(" IF not implemented, true assumed", 0);
  294. XX!         if (flslvl==0) ++trulvl; else ++flslvl;
  295. XX  #else
  296. XX          newp=p;
  297. XX          if (flslvl==0 && yyparse()) ++trulvl; else ++flslvl;
  298.  
  299. XX--- 780,786 -----
  300. XX      } else if (np==ifloc) {/* if */
  301. XX  #if tgp
  302. XX          pperror(" IF not implemented, true assumed", 0);
  303. XX!         if (flslvl==0) ifdone[trulvl++] = 1; else ++flslvl;
  304. XX  #else
  305. XX          newp=p;
  306. XX          if (flslvl==0)
  307. XX***************
  308. XX*** 750,756
  309. XX          if (flslvl==0) ++trulvl; else ++flslvl;
  310. XX  #else
  311. XX          newp=p;
  312. XX!         if (flslvl==0 && yyparse()) ++trulvl; else ++flslvl;
  313. XX          p=newp;
  314. XX  #endif
  315. XX      } else if (np==lneloc) {/* line */
  316.  
  317. XX--- 783,796 -----
  318. XX          if (flslvl==0) ifdone[trulvl++] = 1; else ++flslvl;
  319. XX  #else
  320. XX          newp=p;
  321. XX!         if (flslvl==0)
  322. XX!         {
  323. XX!             if (ifdone[trulvl] = yyparse())    /* DAG */
  324. XX!                 ++trulvl;
  325. XX!             else
  326. XX!                 ++flslvl;
  327. XX!         }
  328. XX!         else ++flslvl;
  329. XX          p=newp;
  330. XX  #endif
  331. XX      } else if (np==eliloc) {/* elif */    /* DAG -- added */
  332. XX***************
  333. XX*** 753,758
  334. XX          if (flslvl==0 && yyparse()) ++trulvl; else ++flslvl;
  335. XX          p=newp;
  336. XX  #endif
  337. XX      } else if (np==lneloc) {/* line */
  338. XX          if (flslvl==0 && pflag==0) {
  339. XX              char *cp, *cp2, *savestring();
  340.  
  341. XX--- 793,839 -----
  342. XX          else ++flslvl;
  343. XX          p=newp;
  344. XX  #endif
  345. XX+     } else if (np==eliloc) {/* elif */    /* DAG -- added */
  346. XX+ #if tgp
  347. XX+         pperror ( " ELIF not implemented, true assumed", (char *) 0, (char *) 0);
  348. XX+         if (flslvl)
  349. XX+         {
  350. XX+             if (--flslvl == 0 && !ifdeone[trulvl])
  351. XX+             {
  352. XX+                 ifdone[trulvl++] = 1;
  353. XX+                 sayline():
  354. XX+             }
  355. XX+             else
  356. XX+                 ++flslvl;
  357. XX+         }
  358. XX+         else if (trulvl)
  359. XX+         {
  360. XX+             ++flslvl;
  361. XX+             --trulvl;
  362. XX+         }
  363. XX+         else
  364. XX+             pperror ( "If-less elif", (char *) 0, (char *) 0);
  365. XX+ #else
  366. XX+         newp = p;
  367. XX+         if (flslvl)
  368. XX+         {
  369. XX+             if (--flslvl == 0 && !ifdone[trulvl] && yyparse())
  370. XX+             {
  371. XX+                 ifdone[trulvl++] = 1;
  372. XX+                 sayline();
  373. XX+             }
  374. XX+             else
  375. XX+                 ++flslvl;
  376. XX+         }
  377. XX+         else if (trulvl)
  378. XX+         {
  379. XX+             ++flslvl;
  380. XX+             --trulvl;
  381. XX+         }
  382. XX+         else
  383. XX+             pperror ("If-less elif", (char *) 0, (char *) 0);
  384. XX+         p = newp;
  385. XX+ #endif
  386. XX      } else if (np==lneloc) {/* line */
  387. XX          if (flslvl==0 && pflag==0) {
  388. XX              char *cp, *cp2, *savestring();
  389. XX***************
  390. XX*** 1067,1072
  391. XX                  case 'E': continue;
  392. XX                  case 'R': ++rflag; continue;
  393. XX                  case 'C': passcom++; continue;
  394. XX                  case 'D':
  395. XX                      if (predef>prespc+NPREDEF) {
  396. XX                          pperror("too many -D options, ignoring %s",argv[i]);
  397.  
  398. XX--- 1148,1154 -----
  399. XX                  case 'E': continue;
  400. XX                  case 'R': ++rflag; continue;
  401. XX                  case 'C': passcom++; continue;
  402. XX+                 case 'B': eolcom++; continue;
  403. XX                  case 'D':
  404. XX                      if (predef>prespc+NPREDEF) {
  405. XX                          pperror("too many -D options, ignoring %s",argv[i]);
  406. XX***************
  407. XX*** 1141,1146
  408. XX      ifdloc=ppsym("ifdef");
  409. XX      ifnloc=ppsym("ifndef");
  410. XX      ifloc=ppsym("if");
  411. XX      lneloc=ppsym("line");
  412. XX      for (i=sizeof(macbit)/sizeof(macbit[0]); --i>=0; ) macbit[i]=0;
  413. XX  # if unix
  414.  
  415. XX--- 1223,1229 -----
  416. XX      ifdloc=ppsym("ifdef");
  417. XX      ifnloc=ppsym("ifndef");
  418. XX      ifloc=ppsym("if");
  419. XX+     eliloc=ppsym("elif");
  420. XX      lneloc=ppsym("line");
  421. XX      for (i=sizeof(macbit)/sizeof(macbit[0]); --i>=0; ) macbit[i]=0;
  422. XX  # if unix
  423.  
  424. @//E*O*F patchkit//
  425. chmod u=rw,g=rw,o=rw patchkit
  426.  
  427. echo Inspecting for damage in transit...
  428. temp=/tmp/sharin$$; dtemp=/tmp/sharout$$
  429. trap "rm -f $temp $dtemp; exit" 0 1 2 3 15
  430. cat > $temp <<\!!!
  431.      382    1399    9889 patchkit
  432. !!!
  433. wc  patchkit | sed 's=[^ ]*/==' | diff -b $temp - >$dtemp
  434. if test -s $dtemp
  435. then echo "Ouch [diff of wc output]:" ; cat $dtemp
  436. else echo "No problems found."
  437. fi
  438. exit 0
  439.