cpp1.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /******************************************************************************
  2. Copyright (c) 1993 - 2011 Daniel Stenberg
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.
  18. ******************************************************************************/
  19. #include <stdio.h>
  20. #include <ctype.h>
  21. #include "cppdef.h"
  22. #include "cpp.h"
  23. #if defined(AMIGA)
  24. #include <dos.h>
  25. #if defined(SHARED)
  26. int _OSERR=0;
  27. char *_ProgramName="junk";
  28. void __stdargs _XCEXIT(long a) { return; }
  29. #endif
  30. #endif
  31. FILE_LOCAL ReturnCode fpp_output(struct Global *, int); /* Output one character */
  32. FILE_LOCAL void fpp_sharp(struct Global *);
  33. INLINE FILE_LOCAL ReturnCode fpp_cppmain(struct Global *);
  34. int fppPreProcess(struct fppTag *tags)
  35. {
  36. size_t i=0;
  37. ReturnCode ret; /* cpp return code */
  38. int retVal; /* fppPreProcess return code */
  39. struct Global *global;
  40. global=(struct Global *)malloc(sizeof(struct Global));
  41. if(!global)
  42. return(FPP_OUT_OF_MEMORY);
  43. memset(global, 0, sizeof(struct Global));
  44. global->infile=NULL;
  45. global->line=0;
  46. global->wrongline=0;
  47. global->errors=0;
  48. global->recursion=0;
  49. global->rec_recover=FPP_TRUE;
  50. global->instring=FPP_FALSE;
  51. global->inmacro=FPP_FALSE;
  52. global->workp=NULL;
  53. global->keepcomments = FPP_FALSE; /* Write out comments flag */
  54. global->cflag = FPP_FALSE; /* -C option (keep comments) */
  55. global->eflag = FPP_FALSE; /* -E option (never fail) */
  56. global->nflag = 0; /* -N option (no predefines) */
  57. global->wflag = FPP_FALSE; /* -W option (write #defines) */
  58. global->ifstack[0]=FPP_TRUE; /* #if information */
  59. global->ifptr = global->ifstack;
  60. global->incend = global->incdir;
  61. /* names defined at cpp start */
  62. global->preset[0]="frexxcpp"; /* This is the Frexx cpp program */
  63. #if defined( amiga )
  64. global->preset[1]="amiga";
  65. global->preset[2]="m68000";
  66. global->preset[3]="amigados";
  67. global->preset[4]= NULL; /* Must be last */
  68. #elif defined( unix )
  69. global->preset[1]="unix";
  70. global->preset[2]= NULL;
  71. #endif
  72. /* Note: order is important */
  73. global->magic[0] = "__LINE__";
  74. global->magic[1] = "__FILE__";
  75. global->magic[2] = "__FUNCTION__";
  76. global->magic[3] = "__FUNC_LINE__";
  77. global->magic[4] = NULL; /* Must be last */
  78. global->funcline = 0;
  79. global->cplusplus=1;
  80. global->sharpfilename=NULL;
  81. global->parmp=NULL;
  82. global->nargs=0;
  83. global->macro=NULL;
  84. global->evalue=0;
  85. global->depends=NULL;
  86. global->input=NULL;
  87. global->output=NULL;
  88. global->error=NULL;
  89. global->first_file=NULL;
  90. global->userdata=NULL;
  91. global->linelines=FPP_TRUE;
  92. global->warnillegalcpp = FPP_FALSE;
  93. global->outputLINE = FPP_TRUE;
  94. global->warnnoinclude = FPP_TRUE;
  95. global->showversion = FPP_TRUE;
  96. global->showincluded = FPP_FALSE;
  97. global->showspace = FPP_FALSE;
  98. global->nestcomments = FPP_FALSE;
  99. global->warnnestcomments = FPP_FALSE;
  100. global->outputfile = FPP_TRUE;
  101. global->included = 0;
  102. global->comment = FPP_FALSE;
  103. global->rightconcat = FPP_FALSE;
  104. global->work[0] = '\0';
  105. global->initialfunc = NULL;
  106. global->allowincludelocal = FPP_TRUE;
  107. memset(global->symtab, 0, SBSIZE * sizeof(DEFBUF *));
  108. ret=fpp_initdefines(global); /* O.S. specific def's */
  109. if(ret)
  110. return(ret);
  111. fpp_dooptions(global, tags); /* Command line -flags */
  112. ret=fpp_addfile(global, stdin, global->work); /* "open" main input file */
  113. global->out = global->outputfile;
  114. if(!ret)
  115. ret=fpp_cppmain(global); /* Process main file */
  116. if ((i = (global->ifptr - global->ifstack)) != 0) {
  117. #if OLD_PREPROCESSOR
  118. fpp_cwarn(global, ERROR_IFDEF_DEPTH, i);
  119. #else
  120. fpp_cerror(global, ERROR_IFDEF_DEPTH, i);
  121. #endif
  122. }
  123. fflush(stdout);
  124. // BK - fclose(stdout);
  125. fpp_delalldefines(global);
  126. retVal = IO_NORMAL;
  127. if (global->errors > 0 && !global->eflag)
  128. retVal = IO_ERROR;
  129. free(global->tokenbuf);
  130. free(global->functionname);
  131. free(global->spacebuf);
  132. free(global);
  133. return retVal; /* No errors or -E option set */
  134. }
  135. INLINE FILE_LOCAL
  136. ReturnCode fpp_cppmain(struct Global *global)
  137. {
  138. /*
  139. * Main process for cpp -- copies tokens from the current input
  140. * stream (main file, include file, or a macro) to the output
  141. * file.
  142. */
  143. int c; /* Current character */
  144. int counter; /* newlines and spaces */
  145. ReturnCode ret; /* return code variable type */
  146. long bracelevel = 0;
  147. long parenlevel = 0;
  148. long bracketlevel = 0;
  149. int fake = 0;
  150. #define MAX_FUNC_LENGTH 50
  151. char tempfunc[MAX_FUNC_LENGTH + 1];
  152. char tempfunc2[MAX_FUNC_LENGTH + 1];
  153. char define = 0; /* probability of a function define phase in the program */
  154. char prev = 0; /* previous type */
  155. char go = 0;
  156. unsigned include = 0;
  157. char initfunc = 0;
  158. /* Initialize for reading tokens */
  159. global->tokenbsize = 50;
  160. global->tokenbuf = malloc(global->tokenbsize + 1);
  161. if(!global->tokenbuf)
  162. return(FPP_OUT_OF_MEMORY);
  163. global->functionname = malloc(global->tokenbsize + 1);
  164. if(!global->functionname)
  165. return(FPP_OUT_OF_MEMORY);
  166. global->functionname[0] = '\0';
  167. if(global->showspace) {
  168. global->spacebuf = (char *)malloc(MAX_SPACE_SIZE);
  169. if(!global->spacebuf)
  170. return(FPP_OUT_OF_MEMORY);
  171. }
  172. if(global->showversion)
  173. fpp_Error(global, VERSION_TEXT);
  174. /*
  175. * Explicitly output a #line at the start of cpp output so
  176. * that lint (etc.) knows the name of the original source
  177. * file. If we don't do this explicitly, we may get
  178. * the name of the first #include file instead.
  179. */
  180. if(global->linelines) /* if #line lines are wanted! */
  181. fpp_sharp(global);
  182. /*
  183. * This loop is started "from the top" at the beginning of each line
  184. * wrongline is set FPP_TRUE in many places if it is necessary to write
  185. * a #line record. (But we don't write them when expanding macros.)
  186. *
  187. * The counter variable has two different uses: at
  188. * the start of a line, it counts the number of blank lines that
  189. * have been skipped over. These are then either output via
  190. * #line records or by outputting explicit blank lines.
  191. * When expanding tokens within a line, the counter remembers
  192. * whether a blank/tab has been output. These are dropped
  193. * at the end of the line, and replaced by a single blank
  194. * within lines.
  195. */
  196. include = global->included;
  197. while(include--) {
  198. fpp_openinclude(global, global->include[(unsigned)include], FPP_TRUE);
  199. }
  200. for (;;) {
  201. counter = 0; /* Count empty lines */
  202. for (;;) { /* For each line, ... */
  203. global->comment = FPP_FALSE; /* No comment yet! */
  204. global->chpos = 0; /* Count whitespaces */
  205. while (type[(c = fpp_get(global))] == SPA) /* Skip leading blanks */
  206. if(global->showspace) {
  207. if(global->chpos<MAX_SPACE_SIZE-1)
  208. /* we still have buffer to store this! */
  209. global->spacebuf[global->chpos++]=(char)c;
  210. }
  211. if (c == '\n') { /* If line's all blank, */
  212. if(global->comment) {
  213. /* A comment was output! */
  214. fpp_Putchar(global, '\n');
  215. }
  216. else
  217. ++counter; /* Do nothing now */
  218. }
  219. else if (c == '#') { /* Is 1st non-space '#' */
  220. global->keepcomments = FPP_FALSE; /* Don't pass comments */
  221. ret = fpp_control(global, &counter); /* Yes, do a #command */
  222. if(ret)
  223. return(ret);
  224. global->keepcomments = (global->cflag && compiling);
  225. }
  226. else if (c == EOF_CHAR) /* At end of file? */
  227. break;
  228. else if (!compiling) { /* #ifdef false? */
  229. fpp_skipnl(global); /* Skip to newline */
  230. counter++; /* Count it, too. */
  231. } else {
  232. break; /* Actual token */
  233. }
  234. }
  235. if (c == EOF_CHAR) /* Exit process at */
  236. break; /* End of file */
  237. /*
  238. * If the loop didn't terminate because of end of file, we
  239. * know there is a token to compile. First, clean up after
  240. * absorbing newlines. counter has the number we skipped.
  241. */
  242. if(global->linelines) { /* if #line lines are wanted! */
  243. if ((global->wrongline && global->infile->fp != NULL) || counter > 4)
  244. fpp_sharp(global); /* Output # line number */
  245. else { /* If just a few, stuff */
  246. while (--counter >= 0) /* them out ourselves */
  247. fpp_Putchar(global, (int)'\n');
  248. }
  249. }
  250. if(global->showspace) {
  251. /* Show all whitespaces! */
  252. global->spacebuf[global->chpos] = '\0';
  253. fpp_Putstring(global, global->spacebuf);
  254. }
  255. /*
  256. * Process each token on this line.
  257. */
  258. fpp_unget(global); /* Reread the char. */
  259. for (;;) { /* For the whole line, */
  260. do { /* Token concat. loop */
  261. for (global->chpos = counter = 0; (type[(c = fpp_get(global))] == SPA);) {
  262. #if COMMENT_INVISIBLE
  263. if (c != COM_SEP)
  264. counter++;
  265. #else
  266. if(global->showspace && global->chpos < MAX_SPACE_SIZE-1) {
  267. global->spacebuf[global->chpos++]=(char)c;
  268. }
  269. counter++; /* Skip over blanks */
  270. #endif
  271. }
  272. if (c == EOF_CHAR || c == '\n')
  273. break; /* Exit line loop */
  274. else if (counter > 0) { /* If we got any spaces */
  275. if(!global->showspace) /* We don't output all spaces */
  276. fpp_Putchar(global, (int)' ');/* Output one space */
  277. else {
  278. global->spacebuf[global->chpos] = '\0';
  279. fpp_Putstring(global, global->spacebuf); /* Output all whitespaces */
  280. }
  281. }
  282. if((ret=fpp_macroid(global, &c))) /* Grab the token */
  283. return(ret);
  284. } while (type[c] == LET && fpp_catenate(global, 0, &ret) && !ret);
  285. if(ret)
  286. /* If the loop was broken because of a fatal error! */
  287. return(ret);
  288. if (c == EOF_CHAR || c == '\n') /* From macro exp error */
  289. break; /* Exit line loop */
  290. go++;
  291. switch (type[c]) {
  292. case LET:
  293. go =0;
  294. /* Quite ordinary token */
  295. fpp_Putstring(global, global->tokenbuf);
  296. if(!define) {
  297. /* Copy the name */
  298. strncpy(tempfunc, global->tokenbuf, MAX_FUNC_LENGTH);
  299. tempfunc[MAX_FUNC_LENGTH]=0;
  300. }
  301. /* fputs(global->tokenbuf, stdout); */
  302. break;
  303. case DIG: /* Output a number */
  304. case DOT: /* Dot may begin floats */
  305. go = 0;
  306. ret=fpp_scannumber(global, c, (ReturnCode(*)(struct Global *, int))fpp_output);
  307. if(ret)
  308. return(ret);
  309. fpp_catenate(global, 1, &ret); /* Check to see if the number is the lhs of a macro concat */
  310. if(ret)
  311. return(ret);
  312. break;
  313. case QUO: /* char or string const */
  314. go = 0;
  315. /* Copy it to output */
  316. if(!global->webmode) {
  317. ret=fpp_scanstring(global, c,
  318. (ReturnCode(*)(struct Global *, int))fpp_output);
  319. if(ret)
  320. return(ret);
  321. break;
  322. }
  323. /* FALLTHROUGH */
  324. default: /* Some other character */
  325. define++;
  326. switch(c) {
  327. case '{':
  328. if(! bracelevel++ && define > 2) {
  329. /*
  330. * This is a starting brace. If there is a probability of a
  331. * function defining, we copy the `tempfunc' function name to
  332. * `global->functionname'.
  333. */
  334. strcpy(global->functionname, tempfunc2);
  335. global->funcline = global->line;
  336. if(global->outputfunctions) {
  337. /*
  338. * Output the discovered function name to stderr!
  339. */
  340. fpp_Error(global, "#> Function defined at line %d: %s <#\n",
  341. global->line,
  342. global->functionname);
  343. }
  344. if(global->initialfunc) {
  345. int a;
  346. for(a=0; a<global->excluded; a++) {
  347. /* check for excluded functions */
  348. if(!strcmp(global->functionname,
  349. global->excludedinit[a]))
  350. break;
  351. }
  352. if(a==global->excluded) {
  353. fpp_expstuff(global, "__brace__", "{");
  354. fpp_expstuff(global, "__init_func__", global->initialfunc);
  355. initfunc = FPP_TRUE;
  356. }
  357. }
  358. }
  359. break;
  360. case '}':
  361. go = 0;
  362. if( (--bracelevel == initfunc) &&
  363. strcmp(global->infile->filename, "__init_func__") ) {
  364. /* we just stepped out of the function! */
  365. global->functionname[0] = '\0';
  366. global->funcline = 0;
  367. define = 1;
  368. if(initfunc) {
  369. fpp_Putchar(global, '}');
  370. bracelevel--;
  371. initfunc=0;
  372. }
  373. }
  374. fake = 0;
  375. break;
  376. case ';':
  377. case ',':
  378. if(go == 2) {
  379. define = 1;
  380. fake = 0;
  381. go--;
  382. break;
  383. }
  384. break;
  385. case '(':
  386. if(! parenlevel++ && !bracelevel) {
  387. if(go == 2) {
  388. /* foobar(text) -> "(" is found. This can't be a
  389. function */
  390. go--;
  391. define = 1;
  392. break;
  393. }
  394. if( define < 2 && prev == LET) {
  395. /* This is the first parenthesis on the ground brace
  396. level, and we did previously not have a probable
  397. function name */
  398. strncpy(tempfunc2, global->tokenbuf, MAX_FUNC_LENGTH);
  399. tempfunc2[MAX_FUNC_LENGTH]=0;
  400. define++;
  401. }
  402. else {
  403. /* we have a fake start */
  404. fake++;
  405. }
  406. }
  407. break;
  408. case ')':
  409. if(! --parenlevel && !bracelevel && define>1 && !fake) {
  410. /*
  411. * The starting parentheses level and
  412. * the starting brace level.
  413. * This might be the start of a function defining coming
  414. * up!
  415. */
  416. define++; /* increase probability */
  417. fake = 0;
  418. go = 1;
  419. }
  420. break;
  421. case '[':
  422. bracketlevel++;
  423. break;
  424. case ']':
  425. bracketlevel--;
  426. break;
  427. }
  428. define--; /* decrease function probability */
  429. fpp_Putchar(global, c); /* Just output it */
  430. break;
  431. } /* Switch ends */
  432. prev = type[c];
  433. } /* Line for loop */
  434. if (c == '\n') { /* Compiling at EOL? */
  435. fpp_Putchar(global, '\n'); /* Output newline, if */
  436. if (global->infile->fp == NULL) /* Expanding a macro, */
  437. global->wrongline = FPP_TRUE; /* Output # line later */
  438. }
  439. } /* Continue until EOF */
  440. if(global->showbalance) {
  441. if(bracketlevel) {
  442. fpp_cwarn(global, WARN_BRACKET_DEPTH, bracketlevel);
  443. }
  444. if(parenlevel) {
  445. fpp_cwarn(global, WARN_PAREN_DEPTH, parenlevel);
  446. }
  447. if(bracelevel) {
  448. fpp_cwarn(global, WARN_BRACE_DEPTH, bracelevel);
  449. }
  450. }
  451. if (global->wflag) {
  452. global->out = FPP_TRUE; /* enable fpp_output */
  453. fpp_outdefines(global); /* Write out #defines */
  454. }
  455. return(FPP_OK);
  456. }
  457. FILE_LOCAL
  458. ReturnCode fpp_output(struct Global *global, int c)
  459. {
  460. /*
  461. * Output one character to stdout -- fpp_output() is passed as an
  462. * argument to fpp_scanstring()
  463. */
  464. #if COMMENT_INVISIBLE
  465. if (c != TOK_SEP && c != COM_SEP)
  466. #else
  467. if (c != TOK_SEP)
  468. #endif
  469. fpp_Putchar(global, c);
  470. return(FPP_OK);
  471. }
  472. void fpp_Putchar(struct Global *global, int c)
  473. {
  474. /*
  475. * Output one character to stdout or to fpp_output function!
  476. */
  477. if(!global->out)
  478. return;
  479. #if defined(UNIX)
  480. if(global->output)
  481. global->output(c, global->userdata);
  482. else
  483. putchar(c);
  484. #else /* amiga */
  485. global->output(c, global->userdata);
  486. #endif
  487. }
  488. void fpp_Putstring(struct Global *global, char *string)
  489. {
  490. /*
  491. * Output a string! One letter at a time to the fpp_Putchar routine!
  492. */
  493. if(!string)
  494. return;
  495. while(*string)
  496. fpp_Putchar(global, *string++);
  497. }
  498. void fpp_Putint(struct Global *global, int number)
  499. {
  500. /*
  501. * Output the number as a string.
  502. */
  503. char buffer[16]; /* an integer can't be that big! */
  504. char *point=buffer;
  505. sprintf(buffer, "%d", number);
  506. while(*point)
  507. fpp_Putchar(global, *point++);
  508. }
  509. FILE_LOCAL
  510. void fpp_sharp(struct Global *global)
  511. {
  512. /*
  513. * Output a line number line.
  514. */
  515. char *name;
  516. if (global->keepcomments) /* Make sure # comes on */
  517. fpp_Putchar(global, '\n'); /* a fresh, new line. */
  518. /* printf("#%s %d", LINE_PREFIX, global->line); */
  519. fpp_Putchar(global, '#');
  520. if(global->outputLINE)
  521. fpp_Putstring(global, LINE_PREFIX);
  522. fpp_Putchar(global, ' ');
  523. fpp_Putint(global, global->line);
  524. if (global->infile->fp != NULL) {
  525. name = (global->infile->progname != NULL)
  526. ? global->infile->progname : global->infile->filename;
  527. if (global->sharpfilename == NULL
  528. || (global->sharpfilename != NULL && !streq(name, global->sharpfilename))) {
  529. if (global->sharpfilename != NULL)
  530. free(global->sharpfilename);
  531. global->sharpfilename = fpp_savestring(global, name);
  532. /* printf(" \"%s\"", name); */
  533. fpp_Putstring(global, " \"");
  534. fpp_Putstring(global, name);
  535. fpp_Putchar(global, '\"');
  536. }
  537. }
  538. fpp_Putchar(global, '\n');
  539. global->wrongline = FPP_FALSE;
  540. return;
  541. }