You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

parse.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /* $XConsortium: parse.c,v 1.30 94/04/17 20:10:38 gildea Exp $ */
  2. /*
  3. Copyright (c) 1993, 1994 X Consortium
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  16. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. Except as contained in this notice, the name of the X Consortium shall not be
  19. used in advertising or otherwise to promote the sale, use or other dealings
  20. in this Software without prior written authorization from the X Consortium.
  21. */
  22. #include "def.h"
  23. extern char *directives[];
  24. extern struct inclist maininclist;
  25. find_includes(filep, file, file_red, recursion, failOK)
  26. struct filepointer *filep;
  27. struct inclist *file, *file_red;
  28. int recursion;
  29. boolean failOK;
  30. {
  31. register char *line;
  32. register int type;
  33. boolean recfailOK;
  34. while (line = getline(filep)) {
  35. switch(type = deftype(line, filep, file_red, file, TRUE)) {
  36. case IF:
  37. doif:
  38. type = find_includes(filep, file,
  39. file_red, recursion+1, failOK);
  40. while ((type == ELIF) || (type == ELIFFALSE) ||
  41. (type == ELIFGUESSFALSE))
  42. type = gobble(filep, file, file_red);
  43. if (type == ELSE)
  44. gobble(filep, file, file_red);
  45. break;
  46. case IFFALSE:
  47. case IFGUESSFALSE:
  48. doiffalse:
  49. if (type == IFGUESSFALSE || type == ELIFGUESSFALSE)
  50. recfailOK = TRUE;
  51. else
  52. recfailOK = failOK;
  53. type = gobble(filep, file, file_red);
  54. if (type == ELSE)
  55. find_includes(filep, file,
  56. file_red, recursion+1, recfailOK);
  57. else
  58. if (type == ELIF)
  59. goto doif;
  60. else
  61. if ((type == ELIFFALSE) || (type == ELIFGUESSFALSE))
  62. goto doiffalse;
  63. break;
  64. case IFDEF:
  65. case IFNDEF:
  66. if ((type == IFDEF && isdefined(line, file_red, NULL))
  67. || (type == IFNDEF && !isdefined(line, file_red, NULL))) {
  68. debug(1,(type == IFNDEF ?
  69. "line %d: %s !def'd in %s via %s%s\n" : "",
  70. filep->f_line, line,
  71. file->i_file, file_red->i_file, ": doit"));
  72. type = find_includes(filep, file,
  73. file_red, recursion+1, failOK);
  74. while (type == ELIF || type == ELIFFALSE || type == ELIFGUESSFALSE)
  75. type = gobble(filep, file, file_red);
  76. if (type == ELSE)
  77. gobble(filep, file, file_red);
  78. }
  79. else {
  80. debug(1,(type == IFDEF ?
  81. "line %d: %s !def'd in %s via %s%s\n" : "",
  82. filep->f_line, line,
  83. file->i_file, file_red->i_file, ": gobble"));
  84. type = gobble(filep, file, file_red);
  85. if (type == ELSE)
  86. find_includes(filep, file,
  87. file_red, recursion+1, failOK);
  88. else if (type == ELIF)
  89. goto doif;
  90. else if (type == ELIFFALSE || type == ELIFGUESSFALSE)
  91. goto doiffalse;
  92. }
  93. break;
  94. case ELSE:
  95. case ELIFFALSE:
  96. case ELIFGUESSFALSE:
  97. case ELIF:
  98. if (!recursion)
  99. gobble(filep, file, file_red);
  100. case ENDIF:
  101. if (recursion)
  102. return(type);
  103. case DEFINE:
  104. define(line, file);
  105. break;
  106. case UNDEF:
  107. if (!*line) {
  108. warning("%s, line %d: incomplete undef == \"%s\"\n",
  109. file_red->i_file, filep->f_line, line);
  110. break;
  111. }
  112. undefine(line, file_red);
  113. break;
  114. case INCLUDE:
  115. add_include(filep, file, file_red, line, FALSE, failOK);
  116. break;
  117. case INCLUDEDOT:
  118. add_include(filep, file, file_red, line, TRUE, failOK);
  119. break;
  120. case ERROR:
  121. warning("(from %s) %s: %d: %s\n",
  122. file_red->i_file, file->i_file,
  123. filep->f_line, line);
  124. break;
  125. case PRAGMA:
  126. case IDENT:
  127. case SCCS:
  128. case EJECT:
  129. break;
  130. case -1:
  131. warning("%s", file_red->i_file);
  132. if (file_red != file)
  133. warning1(" (reading %s)", file->i_file);
  134. warning1(", line %d: unknown directive == \"%s\"\n",
  135. filep->f_line, line);
  136. break;
  137. case -2:
  138. warning("%s", file_red->i_file);
  139. if (file_red != file)
  140. warning1(" (reading %s)", file->i_file);
  141. warning1(", line %d: incomplete include == \"%s\"\n",
  142. filep->f_line, line);
  143. break;
  144. }
  145. }
  146. return(-1);
  147. }
  148. gobble(filep, file, file_red)
  149. register struct filepointer *filep;
  150. struct inclist *file, *file_red;
  151. {
  152. register char *line;
  153. register int type;
  154. while (line = getline(filep)) {
  155. switch(type = deftype(line, filep, file_red, file, FALSE)) {
  156. case IF:
  157. case IFFALSE:
  158. case IFGUESSFALSE:
  159. case IFDEF:
  160. case IFNDEF:
  161. type = gobble(filep, file, file_red);
  162. while ((type == ELIF) || (type == ELIFFALSE) ||
  163. (type == ELIFGUESSFALSE))
  164. type = gobble(filep, file, file_red);
  165. if (type == ELSE)
  166. (void)gobble(filep, file, file_red);
  167. break;
  168. case ELSE:
  169. case ENDIF:
  170. debug(0,("%s, line %d: #%s\n",
  171. file->i_file, filep->f_line,
  172. directives[type]));
  173. return(type);
  174. case DEFINE:
  175. case UNDEF:
  176. case INCLUDE:
  177. case INCLUDEDOT:
  178. case PRAGMA:
  179. case ERROR:
  180. case IDENT:
  181. case SCCS:
  182. case EJECT:
  183. break;
  184. case ELIF:
  185. case ELIFFALSE:
  186. case ELIFGUESSFALSE:
  187. return(type);
  188. case -1:
  189. warning("%s, line %d: unknown directive == \"%s\"\n",
  190. file_red->i_file, filep->f_line, line);
  191. break;
  192. }
  193. }
  194. return(-1);
  195. }
  196. /*
  197. * Decide what type of # directive this line is.
  198. */
  199. int deftype (line, filep, file_red, file, parse_it)
  200. register char *line;
  201. register struct filepointer *filep;
  202. register struct inclist *file_red, *file;
  203. int parse_it;
  204. {
  205. register char *p;
  206. char *directive, savechar;
  207. register int ret;
  208. /*
  209. * Parse the directive...
  210. */
  211. directive=line+1;
  212. while (*directive == ' ' || *directive == '\t')
  213. directive++;
  214. p = directive;
  215. while (*p >= 'a' && *p <= 'z')
  216. p++;
  217. savechar = *p;
  218. *p = '\0';
  219. ret = match(directive, directives);
  220. *p = savechar;
  221. /* If we don't recognize this compiler directive or we happen to just
  222. * be gobbling up text while waiting for an #endif or #elif or #else
  223. * in the case of an #elif we must check the zero_value and return an
  224. * ELIF or an ELIFFALSE.
  225. */
  226. if (ret == ELIF && !parse_it)
  227. {
  228. while (*p == ' ' || *p == '\t')
  229. p++;
  230. /*
  231. * parse an expression.
  232. */
  233. debug(0,("%s, line %d: #elif %s ",
  234. file->i_file, filep->f_line, p));
  235. ret = zero_value(p, filep, file_red);
  236. if (ret != IF)
  237. {
  238. debug(0,("false...\n"));
  239. if (ret == IFFALSE)
  240. return(ELIFFALSE);
  241. else
  242. return(ELIFGUESSFALSE);
  243. }
  244. else
  245. {
  246. debug(0,("true...\n"));
  247. return(ELIF);
  248. }
  249. }
  250. if (ret < 0 || ! parse_it)
  251. return(ret);
  252. /*
  253. * now decide how to parse the directive, and do it.
  254. */
  255. while (*p == ' ' || *p == '\t')
  256. p++;
  257. switch (ret) {
  258. case IF:
  259. /*
  260. * parse an expression.
  261. */
  262. ret = zero_value(p, filep, file_red);
  263. debug(0,("%s, line %d: %s #if %s\n",
  264. file->i_file, filep->f_line, ret?"false":"true", p));
  265. break;
  266. case IFDEF:
  267. case IFNDEF:
  268. debug(0,("%s, line %d: #%s %s\n",
  269. file->i_file, filep->f_line, directives[ret], p));
  270. case UNDEF:
  271. /*
  272. * separate the name of a single symbol.
  273. */
  274. while (isalnum(*p) || *p == '_')
  275. *line++ = *p++;
  276. *line = '\0';
  277. break;
  278. case INCLUDE:
  279. debug(2,("%s, line %d: #include %s\n",
  280. file->i_file, filep->f_line, p));
  281. /* Support ANSI macro substitution */
  282. {
  283. struct symtab *sym = isdefined(p, file_red, NULL);
  284. while (sym) {
  285. p = sym->s_value;
  286. debug(3,("%s : #includes SYMBOL %s = %s\n",
  287. file->i_incstring,
  288. sym -> s_name,
  289. sym -> s_value));
  290. /* mark file as having included a 'soft include' */
  291. file->i_included_sym = TRUE;
  292. sym = isdefined(p, file_red, NULL);
  293. }
  294. }
  295. /*
  296. * Separate the name of the include file.
  297. */
  298. while (*p && *p != '"' && *p != '<')
  299. p++;
  300. if (! *p)
  301. return(-2);
  302. if (*p++ == '"') {
  303. ret = INCLUDEDOT;
  304. while (*p && *p != '"')
  305. *line++ = *p++;
  306. } else
  307. while (*p && *p != '>')
  308. *line++ = *p++;
  309. *line = '\0';
  310. break;
  311. case DEFINE:
  312. /*
  313. * copy the definition back to the beginning of the line.
  314. */
  315. strcpy (line, p);
  316. break;
  317. case ELSE:
  318. case ENDIF:
  319. case ELIF:
  320. case PRAGMA:
  321. case ERROR:
  322. case IDENT:
  323. case SCCS:
  324. case EJECT:
  325. debug(0,("%s, line %d: #%s\n",
  326. file->i_file, filep->f_line, directives[ret]));
  327. /*
  328. * nothing to do.
  329. */
  330. break;
  331. }
  332. return(ret);
  333. }
  334. struct symtab *isdefined(symbol, file, srcfile)
  335. register char *symbol;
  336. struct inclist *file;
  337. struct inclist **srcfile;
  338. {
  339. register struct symtab *val;
  340. if (val = slookup(symbol, &maininclist)) {
  341. debug(1,("%s defined on command line\n", symbol));
  342. if (srcfile != NULL) *srcfile = &maininclist;
  343. return(val);
  344. }
  345. if (val = fdefined(symbol, file, srcfile))
  346. return(val);
  347. debug(1,("%s not defined in %s\n", symbol, file->i_file));
  348. return(NULL);
  349. }
  350. struct symtab *fdefined(symbol, file, srcfile)
  351. register char *symbol;
  352. struct inclist *file;
  353. struct inclist **srcfile;
  354. {
  355. register struct inclist **ip;
  356. register struct symtab *val;
  357. register int i;
  358. static int recurse_lvl = 0;
  359. if (file->i_defchecked)
  360. return(NULL);
  361. file->i_defchecked = TRUE;
  362. if (val = slookup(symbol, file))
  363. debug(1,("%s defined in %s as %s\n", symbol, file->i_file, val->s_value));
  364. if (val == NULL && file->i_list)
  365. {
  366. for (ip = file->i_list, i=0; i < file->i_listlen; i++, ip++)
  367. if (val = fdefined(symbol, *ip, srcfile)) {
  368. break;
  369. }
  370. }
  371. else if (val != NULL && srcfile != NULL) *srcfile = file;
  372. recurse_lvl--;
  373. file->i_defchecked = FALSE;
  374. return(val);
  375. }
  376. /*
  377. * Return type based on if the #if expression evaluates to 0
  378. */
  379. zero_value(exp, filep, file_red)
  380. register char *exp;
  381. register struct filepointer *filep;
  382. register struct inclist *file_red;
  383. {
  384. if (cppsetup(exp, filep, file_red))
  385. return(IFFALSE);
  386. else
  387. return(IF);
  388. }
  389. define(def, file)
  390. char *def;
  391. struct inclist *file;
  392. {
  393. char *val;
  394. /* Separate symbol name and its value */
  395. val = def;
  396. while (isalnum(*val) || *val == '_')
  397. val++;
  398. if (*val)
  399. *val++ = '\0';
  400. while (*val == ' ' || *val == '\t')
  401. val++;
  402. if (!*val)
  403. val = "1";
  404. define2(def, val, file);
  405. }
  406. define2(name, val, file)
  407. char *name, *val;
  408. struct inclist *file;
  409. {
  410. int first, last, below;
  411. register struct symtab *sp = NULL, *dest;
  412. /* Make space if it's needed */
  413. if (file->i_defs == NULL)
  414. {
  415. file->i_defs = (struct symtab *)
  416. malloc(sizeof (struct symtab) * SYMTABINC);
  417. file->i_deflen = SYMTABINC;
  418. file->i_ndefs = 0;
  419. }
  420. else if (file->i_ndefs == file->i_deflen)
  421. file->i_defs = (struct symtab *)
  422. realloc(file->i_defs,
  423. sizeof(struct symtab)*(file->i_deflen+=SYMTABINC));
  424. if (file->i_defs == NULL)
  425. fatalerr("malloc()/realloc() failure in insert_defn()\n");
  426. below = first = 0;
  427. last = file->i_ndefs - 1;
  428. while (last >= first)
  429. {
  430. /* Fast inline binary search */
  431. register char *s1;
  432. register char *s2;
  433. register int middle = (first + last) / 2;
  434. /* Fast inline strchr() */
  435. s1 = name;
  436. s2 = file->i_defs[middle].s_name;
  437. while (*s1++ == *s2++)
  438. if (s2[-1] == '\0') break;
  439. /* If exact match, set sp and break */
  440. if (*--s1 == *--s2)
  441. {
  442. sp = file->i_defs + middle;
  443. break;
  444. }
  445. /* If name > i_defs[middle] ... */
  446. if (*s1 > *s2)
  447. {
  448. below = first;
  449. first = middle + 1;
  450. }
  451. /* else ... */
  452. else
  453. {
  454. below = last = middle - 1;
  455. }
  456. }
  457. /* Search is done. If we found an exact match to the symbol name,
  458. just replace its s_value */
  459. if (sp != NULL)
  460. {
  461. free(sp->s_value);
  462. sp->s_value = copy(val);
  463. return;
  464. }
  465. sp = file->i_defs + file->i_ndefs++;
  466. dest = file->i_defs + below + 1;
  467. while (sp > dest)
  468. {
  469. *sp = sp[-1];
  470. sp--;
  471. }
  472. sp->s_name = copy(name);
  473. sp->s_value = copy(val);
  474. }
  475. struct symtab *slookup(symbol, file)
  476. register char *symbol;
  477. register struct inclist *file;
  478. {
  479. register int first = 0;
  480. register int last = file->i_ndefs - 1;
  481. if (file) while (last >= first)
  482. {
  483. /* Fast inline binary search */
  484. register char *s1;
  485. register char *s2;
  486. register int middle = (first + last) / 2;
  487. /* Fast inline strchr() */
  488. s1 = symbol;
  489. s2 = file->i_defs[middle].s_name;
  490. while (*s1++ == *s2++)
  491. if (s2[-1] == '\0') break;
  492. /* If exact match, we're done */
  493. if (*--s1 == *--s2)
  494. {
  495. return file->i_defs + middle;
  496. }
  497. /* If symbol > i_defs[middle] ... */
  498. if (*s1 > *s2)
  499. {
  500. first = middle + 1;
  501. }
  502. /* else ... */
  503. else
  504. {
  505. last = middle - 1;
  506. }
  507. }
  508. return(NULL);
  509. }
  510. undefine(symbol, file)
  511. char *symbol;
  512. register struct inclist *file;
  513. {
  514. register struct symtab *ptr;
  515. struct inclist *srcfile;
  516. while ((ptr = isdefined(symbol, file, &srcfile)) != NULL)
  517. {
  518. srcfile->i_ndefs--;
  519. for (; ptr < srcfile->i_defs + srcfile->i_ndefs; ptr++)
  520. *ptr = ptr[1];
  521. }
  522. }