cpp3.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /******************************************************************************
  2. Copyright (c) 1999 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 <time.h> /*OIS*0.92*/
  22. #include "cppdef.h"
  23. #include "cpp.h"
  24. ReturnCode openfile(struct Global *global, char *filename)
  25. {
  26. /*
  27. * Open a file, add it to the linked list of open files.
  28. * This is called only from openfile() in cpp2.c.
  29. */
  30. FILE *fp;
  31. ReturnCode ret;
  32. if ((fp = fopen(filename, "r")) == NULL)
  33. ret=FPP_OPEN_ERROR;
  34. else
  35. ret=addfile(global, fp, filename);
  36. if(!ret && global->showincluded) {
  37. /* no error occured! */
  38. Error(global, "cpp: included \"");
  39. Error(global, filename);
  40. Error(global, "\"\n");
  41. }
  42. return(ret);
  43. }
  44. ReturnCode addfile(struct Global *global,
  45. FILE *fp, /* Open file pointer */
  46. char *filename) /* Name of the file */
  47. {
  48. /*
  49. * Initialize tables for this open file. This is called from openfile()
  50. * above (for #include files), and from the entry to cpp to open the main
  51. * input file. It calls a common routine, getfile() to build the FILEINFO
  52. * structure which is used to read characters. (getfile() is also called
  53. * to setup a macro replacement.)
  54. */
  55. FILEINFO *file;
  56. ReturnCode ret;
  57. ret = getfile(global, NBUFF, filename, &file);
  58. if(ret)
  59. return(ret);
  60. file->fp = fp; /* Better remember FILE * */
  61. file->buffer[0] = EOS; /* Initialize for first read */
  62. global->line = 1; /* Working on line 1 now */
  63. global->wrongline = TRUE; /* Force out initial #line */
  64. return(FPP_OK);
  65. }
  66. int dooptions(struct Global *global, struct fppTag *tags)
  67. {
  68. /*
  69. * dooptions is called to process command line arguments (-Detc).
  70. * It is called only at cpp startup.
  71. */
  72. DEFBUF *dp;
  73. char end=FALSE; /* end of taglist */
  74. while(tags && !end) {
  75. switch(tags->tag) {
  76. case FPPTAG_END:
  77. end=TRUE;
  78. break;
  79. case FPPTAG_INITFUNC:
  80. global->initialfunc = (char *) tags->data;
  81. break;
  82. case FPPTAG_DISPLAYFUNCTIONS:
  83. global->outputfunctions = tags->data?1:0;
  84. break;
  85. case FPPTAG_RIGHTCONCAT:
  86. global->rightconcat = tags->data?1:0;
  87. break;
  88. case FPPTAG_OUTPUTMAIN:
  89. global->outputfile = tags->data?1:0;
  90. break;
  91. case FPPTAG_NESTED_COMMENTS:
  92. global->nestcomments = tags->data?1:0;
  93. break;
  94. case FPPTAG_WARNMISSINCLUDE:
  95. global->warnnoinclude = tags->data?1:0;
  96. break;
  97. case FPPTAG_WARN_NESTED_COMMENTS:
  98. global->warnnestcomments = tags->data?1:0;
  99. break;
  100. case FPPTAG_OUTPUTSPACE:
  101. global->showspace = tags->data?1:0;
  102. break;
  103. case FPPTAG_OUTPUTBALANCE:
  104. global->showbalance = tags->data?1:0;
  105. break;
  106. case FPPTAG_OUTPUTINCLUDES:
  107. global->showincluded = tags->data?1:0;
  108. break;
  109. case FPPTAG_IGNOREVERSION:
  110. global->showversion = tags->data?1:0;
  111. break;
  112. case FPPTAG_WARNILLEGALCPP:
  113. global->warnillegalcpp = tags->data?1:0;
  114. break;
  115. case FPPTAG_OUTPUTLINE:
  116. global->outputLINE = tags->data?1:0;
  117. break;
  118. case FPPTAG_KEEPCOMMENTS:
  119. if(tags->data) {
  120. global->cflag = TRUE;
  121. global->keepcomments = TRUE;
  122. }
  123. break;
  124. case FPPTAG_DEFINE:
  125. /*
  126. * If the option is just "-Dfoo", make it -Dfoo=1
  127. */
  128. {
  129. char *symbol=(char *)tags->data;
  130. char *text=symbol;
  131. while (*text != EOS && *text != '=')
  132. text++;
  133. if (*text == EOS)
  134. text = "1";
  135. else
  136. *text++ = EOS;
  137. /*
  138. * Now, save the word and its definition.
  139. */
  140. dp = defendel(global, symbol, FALSE);
  141. if(!dp)
  142. return(FPP_OUT_OF_MEMORY);
  143. dp->repl = savestring(global, text);
  144. dp->nargs = DEF_NOARGS;
  145. }
  146. break;
  147. case FPPTAG_IGNORE_NONFATAL:
  148. global->eflag = TRUE;
  149. break;
  150. case FPPTAG_INCLUDE_DIR:
  151. if (global->incend >= &global->incdir[NINCLUDE]) {
  152. cfatal(global, FATAL_TOO_MANY_INCLUDE_DIRS);
  153. return(FPP_TOO_MANY_INCLUDE_DIRS);
  154. }
  155. *global->incend++ = (char *)tags->data;
  156. break;
  157. case FPPTAG_INCLUDE_FILE:
  158. case FPPTAG_INCLUDE_MACRO_FILE:
  159. if (global->included >= NINCLUDE) {
  160. cfatal(global, FATAL_TOO_MANY_INCLUDE_FILES);
  161. return(FPP_TOO_MANY_INCLUDE_FILES);
  162. }
  163. global->include[global->included] = (char *)tags->data;
  164. global->includeshow[global->included] =
  165. (tags->tag == FPPTAG_INCLUDE_FILE);
  166. global->included++;
  167. break;
  168. case FPPTAG_BUILTINS:
  169. global->nflag|=(tags->data?NFLAG_BUILTIN:0);
  170. break;
  171. case FPPTAG_PREDEFINES:
  172. global->nflag|=(tags->data?NFLAG_PREDEFINE:0);
  173. break;
  174. case FPPTAG_IGNORE_CPLUSPLUS:
  175. global->cplusplus=!tags->data;
  176. break;
  177. case FPPTAG_SIZEOF_TABLE:
  178. {
  179. SIZES *sizp; /* For -S */
  180. int size; /* For -S */
  181. int isdatum; /* FALSE for -S* */
  182. int endtest; /* For -S */
  183. char *text=(char *)tags->data;
  184. sizp = size_table;
  185. if (isdatum = (*text != '*')) /* If it's just -S, */
  186. endtest = T_FPTR; /* Stop here */
  187. else { /* But if it's -S* */
  188. text++; /* Step over '*' */
  189. endtest = 0; /* Stop at end marker */
  190. }
  191. while (sizp->bits != endtest && *text != EOS) {
  192. if (!isdigit(*text)) { /* Skip to next digit */
  193. text++;
  194. continue;
  195. }
  196. size = 0; /* Compile the value */
  197. while (isdigit(*text)) {
  198. size *= 10;
  199. size += (*text++ - '0');
  200. }
  201. if (isdatum)
  202. sizp->size = size; /* Datum size */
  203. else
  204. sizp->psize = size; /* Pointer size */
  205. sizp++;
  206. }
  207. if (sizp->bits != endtest)
  208. cwarn(global, WARN_TOO_FEW_VALUES_TO_SIZEOF, NULL);
  209. else if (*text != EOS)
  210. cwarn(global, WARN_TOO_MANY_VALUES_TO_SIZEOF, NULL);
  211. }
  212. break;
  213. case FPPTAG_UNDEFINE:
  214. if (defendel(global, (char *)tags->data, TRUE) == NULL)
  215. cwarn(global, WARN_NOT_DEFINED, tags->data);
  216. break;
  217. case FPPTAG_OUTPUT_DEFINES:
  218. global->wflag++;
  219. break;
  220. case FPPTAG_INPUT_NAME:
  221. strcpy(global->work, tags->data); /* Remember input filename */
  222. global->first_file=tags->data;
  223. break;
  224. case FPPTAG_INPUT:
  225. global->input=(char *(*)(char *, int, void *))tags->data;
  226. break;
  227. case FPPTAG_OUTPUT:
  228. global->output=(void (*)(int, void *))tags->data;
  229. break;
  230. case FPPTAG_ERROR:
  231. global->error=(void (*)(void *, char *, va_list))tags->data;
  232. break;
  233. case FPPTAG_USERDATA:
  234. global->userdata=tags->data;
  235. break;
  236. case FPPTAG_LINE:
  237. global->linelines= tags->data?1:0;
  238. break;
  239. case FPPTAG_EXCLFUNC:
  240. global->excludedinit[ global->excluded++ ] = (char *)tags->data;
  241. break;
  242. case FPPTAG_WEBMODE:
  243. global->webmode=(tags->data?1:0);
  244. break;
  245. default:
  246. cwarn(global, WARN_INTERNAL_ERROR, NULL);
  247. break;
  248. }
  249. tags++;
  250. }
  251. return(0);
  252. }
  253. ReturnCode initdefines(struct Global *global)
  254. {
  255. /*
  256. * Initialize the built-in #define's. There are two flavors:
  257. * #define decus 1 (static definitions)
  258. * #define __FILE__ ?? (dynamic, evaluated by magic)
  259. * Called only on cpp startup.
  260. *
  261. * Note: the built-in static definitions are supressed by the -N option.
  262. * __LINE__, __FILE__, __TIME__ and __DATE__ are always present.
  263. */
  264. char **pp;
  265. char *tp;
  266. DEFBUF *dp;
  267. struct tm *tm;
  268. int i;
  269. time_t tvec;
  270. static char months[12][4] = {
  271. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  272. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  273. };
  274. /*
  275. * Predefine the built-in symbols. Allow the
  276. * implementor to pre-define a symbol as "" to
  277. * eliminate it.
  278. */
  279. if (!(global->nflag & NFLAG_BUILTIN)) {
  280. for (pp = global->preset; *pp != NULL; pp++) {
  281. if (*pp[0] != EOS) {
  282. dp = defendel(global, *pp, FALSE);
  283. if(!dp)
  284. return(FPP_OUT_OF_MEMORY);
  285. dp->repl = savestring(global, "1");
  286. dp->nargs = DEF_NOARGS;
  287. }
  288. }
  289. }
  290. /*
  291. * The magic pre-defines (__FILE__ and __LINE__ are
  292. * initialized with negative argument counts. expand()
  293. * notices this and calls the appropriate routine.
  294. * DEF_NOARGS is one greater than the first "magic" definition.
  295. */
  296. if (!(global->nflag & NFLAG_PREDEFINE)) {
  297. for (pp = global->magic, i = DEF_NOARGS; *pp != NULL; pp++) {
  298. dp = defendel(global, *pp, FALSE);
  299. if(!dp)
  300. return(FPP_OUT_OF_MEMORY);
  301. dp->nargs = --i;
  302. }
  303. #if OK_DATE
  304. /*
  305. * Define __DATE__ as today's date.
  306. */
  307. dp = defendel(global, "__DATE__", FALSE);
  308. tp = malloc(14);
  309. if(!tp || !dp)
  310. return(FPP_OUT_OF_MEMORY);
  311. dp->repl = tp;
  312. dp->nargs = DEF_NOARGS;
  313. time(&tvec);
  314. tm = localtime(&tvec);
  315. sprintf(tp, "\"%3s %2d %4d\"", /* "Aug 20 1988" */
  316. months[tm->tm_mon],
  317. tm->tm_mday,
  318. tm->tm_year + 1900);
  319. /*
  320. * Define __TIME__ as this moment's time.
  321. */
  322. dp = defendel(global, "__TIME__", FALSE);
  323. tp = malloc(11);
  324. if(!tp || !dp)
  325. return(FPP_OUT_OF_MEMORY);
  326. dp->repl = tp;
  327. dp->nargs = DEF_NOARGS;
  328. sprintf(tp, "\"%2d:%02d:%02d\"", /* "20:42:31" */
  329. tm->tm_hour,
  330. tm->tm_min,
  331. tm->tm_sec);
  332. #endif
  333. }
  334. return(FPP_OK);
  335. }
  336. void deldefines(struct Global *global)
  337. {
  338. /*
  339. * Delete the built-in #define's.
  340. */
  341. char **pp;
  342. int i;
  343. /*
  344. * Delete the built-in symbols, unless -WW.
  345. */
  346. if (global->wflag < 2) {
  347. for (pp = global->preset; *pp != NULL; pp++) {
  348. defendel(global, *pp, TRUE);
  349. }
  350. }
  351. /*
  352. * The magic pre-defines __FILE__ and __LINE__
  353. */
  354. for (pp = global->magic, i = DEF_NOARGS; *pp != NULL; pp++) {
  355. defendel(global, *pp, TRUE);
  356. }
  357. #if OK_DATE
  358. /*
  359. * Undefine __DATE__.
  360. */
  361. defendel(global, "__DATE__", TRUE);
  362. /*
  363. * Undefine __TIME__.
  364. */
  365. defendel(global, "__TIME__", TRUE);
  366. #endif
  367. return;
  368. }