parse.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /* $Xorg: parse.c,v 1.6 2001/02/09 02:03:16 xorgcvs Exp $ */
  2. /*
  3. Copyright (c) 1993, 1994, 1998 The Open Group
  4. Permission to use, copy, modify, distribute, and sell this software and its
  5. documentation for any purpose is hereby granted without fee, provided that
  6. the above copyright notice appear in all copies and that both that
  7. copyright notice and this permission notice appear in supporting
  8. documentation.
  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. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  15. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. Except as contained in this notice, the name of The Open Group shall not be
  18. used in advertising or otherwise to promote the sale, use or other dealings
  19. in this Software without prior written authorization from The Open Group.
  20. */
  21. /* $XFree86: xc/config/makedepend/parse.c,v 1.12 2002/02/26 05:09:10 tsi Exp $ */
  22. #include "def.h"
  23. extern char *directives[];
  24. extern struct inclist inclist[ MAXFILES ],
  25. *inclistnext,
  26. maininclist;
  27. extern char *includedirs[ ],
  28. **includedirsnext;
  29. static int deftype (char *line, struct filepointer *filep,
  30. struct inclist *file_red, struct inclist *file,
  31. int parse_it);
  32. static int zero_value(char *filename, char *exp, struct filepointer *filep,
  33. struct inclist *file_red);
  34. static int merge2defines(struct inclist *file1, struct inclist *file2);
  35. static int
  36. gobble(struct filepointer *filep, struct inclist *file,
  37. struct inclist *file_red)
  38. {
  39. char *line;
  40. int type;
  41. while ((line = getnextline(filep))) {
  42. switch(type = deftype(line, filep, file_red, file, FALSE)) {
  43. case IF:
  44. case IFFALSE:
  45. case IFGUESSFALSE:
  46. case IFDEF:
  47. case IFNDEF:
  48. type = gobble(filep, file, file_red);
  49. while ((type == ELIF) || (type == ELIFFALSE) ||
  50. (type == ELIFGUESSFALSE))
  51. type = gobble(filep, file, file_red);
  52. if (type == ELSE)
  53. (void)gobble(filep, file, file_red);
  54. break;
  55. case ELSE:
  56. case ENDIF:
  57. debug(0,("%s, line %d: #%s\n",
  58. file->i_file, filep->f_line,
  59. directives[type]));
  60. return(type);
  61. case DEFINE:
  62. case UNDEF:
  63. case INCLUDE:
  64. case INCLUDEDOT:
  65. case PRAGMA:
  66. case ERROR:
  67. case IDENT:
  68. case SCCS:
  69. case EJECT:
  70. case WARNING:
  71. case INCLUDENEXT:
  72. case INCLUDENEXTDOT:
  73. break;
  74. case ELIF:
  75. case ELIFFALSE:
  76. case ELIFGUESSFALSE:
  77. return(type);
  78. case -1:
  79. warning("%s", file_red->i_file);
  80. if (file_red != file)
  81. warning1(" (reading %s)", file->i_file);
  82. warning1(", line %d: unknown directive == \"%s\"\n",
  83. filep->f_line, line);
  84. break;
  85. }
  86. }
  87. return(-1);
  88. }
  89. /*
  90. * Decide what type of # directive this line is.
  91. */
  92. static int
  93. deftype (char *line, struct filepointer *filep,
  94. struct inclist *file_red, struct inclist *file, int parse_it)
  95. {
  96. register char *p;
  97. char *directive, savechar, *q;
  98. register int ret;
  99. /*
  100. * Parse the directive...
  101. */
  102. directive=line+1;
  103. while (*directive == ' ' || *directive == '\t')
  104. directive++;
  105. p = directive;
  106. while ((*p == '_') || (*p >= 'a' && *p <= 'z'))
  107. p++;
  108. savechar = *p;
  109. *p = '\0';
  110. ret = match(directive, directives);
  111. *p = savechar;
  112. /* If we don't recognize this compiler directive or we happen to just
  113. * be gobbling up text while waiting for an #endif or #elif or #else
  114. * in the case of an #elif we must check the zero_value and return an
  115. * ELIF or an ELIFFALSE.
  116. */
  117. if (ret == ELIF && !parse_it)
  118. {
  119. while (*p == ' ' || *p == '\t')
  120. p++;
  121. /*
  122. * parse an expression.
  123. */
  124. debug(0,("%s, line %d: #elif %s ",
  125. file->i_file, filep->f_line, p));
  126. ret = zero_value(file->i_file, p, filep, file_red);
  127. if (ret != IF)
  128. {
  129. debug(0,("false...\n"));
  130. if (ret == IFFALSE)
  131. return(ELIFFALSE);
  132. else
  133. return(ELIFGUESSFALSE);
  134. }
  135. else
  136. {
  137. debug(0,("true...\n"));
  138. return(ELIF);
  139. }
  140. }
  141. if (ret < 0 || ! parse_it)
  142. return(ret);
  143. /*
  144. * now decide how to parse the directive, and do it.
  145. */
  146. while (*p == ' ' || *p == '\t')
  147. p++;
  148. q = p + strlen(p);
  149. do {
  150. q--;
  151. } while (*q == ' ' || *q == '\t');
  152. q[1] = '\0';
  153. switch (ret) {
  154. case IF:
  155. /*
  156. * parse an expression.
  157. */
  158. ret = zero_value(file->i_file, p, filep, file_red);
  159. debug(0,("%s, line %d: %s #if %s\n",
  160. file->i_file, filep->f_line, ret?"false":"true", p));
  161. break;
  162. case IFDEF:
  163. case IFNDEF:
  164. debug(0,("%s, line %d: #%s %s\n",
  165. file->i_file, filep->f_line, directives[ret], p));
  166. case UNDEF:
  167. /*
  168. * separate the name of a single symbol.
  169. */
  170. while (isalnum(*p) || *p == '_')
  171. *line++ = *p++;
  172. *line = '\0';
  173. break;
  174. case INCLUDE:
  175. case INCLUDENEXT:
  176. debug(2,("%s, line %d: #include%s %s\n",
  177. file->i_file, filep->f_line,
  178. (ret == INCLUDE) ? "" : "_next", p));
  179. /* Support ANSI macro substitution */
  180. while (1) {
  181. struct symtab **sym;
  182. if (!*p || *p == '"' || *p == '<')
  183. break;
  184. sym = isdefined(p, file_red, NULL);
  185. if (!sym)
  186. break;
  187. p = (*sym)->s_value;
  188. debug(3,("%s : #includes SYMBOL %s = %s\n",
  189. file->i_incstring,
  190. (*sym) -> s_name,
  191. (*sym) -> s_value));
  192. /* mark file as having included a 'soft include' */
  193. file->i_flags |= INCLUDED_SYM;
  194. }
  195. /*
  196. * Separate the name of the include file.
  197. */
  198. while (*p && *p != '"' && *p != '<')
  199. p++;
  200. if (! *p)
  201. return(-2);
  202. if (*p++ == '"') {
  203. if (ret == INCLUDE)
  204. ret = INCLUDEDOT;
  205. else
  206. ret = INCLUDENEXTDOT;
  207. while (*p && *p != '"')
  208. *line++ = *p++;
  209. } else
  210. while (*p && *p != '>')
  211. *line++ = *p++;
  212. *line = '\0';
  213. break;
  214. case DEFINE:
  215. /*
  216. * copy the definition back to the beginning of the line.
  217. */
  218. strcpy (line, p);
  219. break;
  220. case ELSE:
  221. case ENDIF:
  222. case ELIF:
  223. case PRAGMA:
  224. case ERROR:
  225. case IDENT:
  226. case SCCS:
  227. case EJECT:
  228. case WARNING:
  229. debug(0,("%s, line %d: #%s\n",
  230. file->i_file, filep->f_line, directives[ret]));
  231. /*
  232. * nothing to do.
  233. */
  234. break;
  235. }
  236. return(ret);
  237. }
  238. struct symtab **
  239. fdefined(char *symbol, struct inclist *file, struct inclist **srcfile)
  240. {
  241. struct inclist **ip;
  242. struct symtab **val;
  243. int i;
  244. static int recurse_lvl = 0;
  245. if (file->i_flags & DEFCHECKED)
  246. return(NULL);
  247. debug(2,("Looking for %s in %s\n", symbol, file->i_file));
  248. file->i_flags |= DEFCHECKED;
  249. if ((val = slookup(symbol, file)))
  250. debug(1,("%s defined in %s as %s\n",
  251. symbol, file->i_file, (*val)->s_value));
  252. if (val == NULL && file->i_list)
  253. {
  254. for (ip = file->i_list, i=0; i < file->i_listlen; i++, ip++)
  255. if (file->i_merged[i]==FALSE) {
  256. val = fdefined(symbol, *ip, srcfile);
  257. file->i_merged[i]=merge2defines(file,*ip);
  258. if (val!=NULL) break;
  259. }
  260. }
  261. else if (val != NULL && srcfile != NULL) *srcfile = file;
  262. recurse_lvl--;
  263. file->i_flags &= ~DEFCHECKED;
  264. return(val);
  265. }
  266. struct symtab **
  267. isdefined(char *symbol, struct inclist *file, struct inclist **srcfile)
  268. {
  269. struct symtab **val;
  270. if ((val = slookup(symbol, &maininclist))) {
  271. debug(1,("%s defined on command line\n", symbol));
  272. if (srcfile != NULL) *srcfile = &maininclist;
  273. return(val);
  274. }
  275. if ((val = fdefined(symbol, file, srcfile)))
  276. return(val);
  277. debug(1,("%s not defined in %s\n", symbol, file->i_file));
  278. return(NULL);
  279. }
  280. /*
  281. * Return type based on if the #if expression evaluates to 0
  282. */
  283. static int
  284. zero_value(char *filename,
  285. char *exp,
  286. struct filepointer *filep,
  287. struct inclist *file_red)
  288. {
  289. if (cppsetup(filename, exp, filep, file_red))
  290. return(IFFALSE);
  291. else
  292. return(IF);
  293. }
  294. void
  295. define2(char *name, char *val, struct inclist *file)
  296. {
  297. int first, last, below;
  298. register struct symtab **sp = NULL, **dest;
  299. struct symtab *stab;
  300. /* Make space if it's needed */
  301. if (file->i_defs == NULL)
  302. {
  303. file->i_defs = (struct symtab **)
  304. malloc(sizeof (struct symtab*) * SYMTABINC);
  305. file->i_ndefs = 0;
  306. }
  307. else if (!(file->i_ndefs % SYMTABINC))
  308. file->i_defs = (struct symtab **)
  309. realloc(file->i_defs,
  310. sizeof(struct symtab*)*(file->i_ndefs+SYMTABINC));
  311. if (file->i_defs == NULL)
  312. fatalerr("malloc()/realloc() failure in insert_defn()\n");
  313. below = first = 0;
  314. last = file->i_ndefs - 1;
  315. while (last >= first)
  316. {
  317. /* Fast inline binary search */
  318. register char *s1;
  319. register char *s2;
  320. register int middle = first + (last - first) / 2;
  321. /* Fast inline strchr() */
  322. s1 = name;
  323. s2 = file->i_defs[middle]->s_name;
  324. while (*s1++ == *s2++)
  325. if (s2[-1] == '\0') break;
  326. /* If exact match, set sp and break */
  327. if (*--s1 == *--s2)
  328. {
  329. sp = file->i_defs + middle;
  330. break;
  331. }
  332. /* If name > i_defs[middle] ... */
  333. if (*s1 > *s2)
  334. {
  335. below = first;
  336. first = middle + 1;
  337. }
  338. /* else ... */
  339. else
  340. {
  341. below = last = middle - 1;
  342. }
  343. }
  344. /* Search is done. If we found an exact match to the symbol name,
  345. just replace its s_value */
  346. if (sp != NULL)
  347. {
  348. debug(1,("redefining %s from %s to %s in file %s\n",
  349. name, (*sp)->s_value, val, file->i_file));
  350. free((*sp)->s_value);
  351. (*sp)->s_value = copy(val);
  352. return;
  353. }
  354. sp = file->i_defs + file->i_ndefs++;
  355. dest = file->i_defs + below + 1;
  356. while (sp > dest)
  357. {
  358. *sp = sp[-1];
  359. sp--;
  360. }
  361. stab = (struct symtab *) malloc(sizeof (struct symtab));
  362. if (stab == NULL)
  363. fatalerr("malloc()/realloc() failure in insert_defn()\n");
  364. debug(1,("defining %s to %s in file %s\n", name, val, file->i_file));
  365. stab->s_name = copy(name);
  366. stab->s_value = copy(val);
  367. *sp = stab;
  368. }
  369. void
  370. define(char *def, struct inclist *file)
  371. {
  372. char *val;
  373. /* Separate symbol name and its value */
  374. val = def;
  375. while (isalnum(*val) || *val == '_')
  376. val++;
  377. if (*val)
  378. *val++ = '\0';
  379. while (*val == ' ' || *val == '\t')
  380. val++;
  381. if (!*val)
  382. val = "1";
  383. define2(def, val, file);
  384. }
  385. struct symtab **
  386. slookup(char *symbol, struct inclist *file)
  387. {
  388. register int first = 0;
  389. register int last;
  390. if (!file)
  391. {
  392. return NULL;
  393. }
  394. last = file->i_ndefs - 1;
  395. while (last >= first)
  396. {
  397. /* Fast inline binary search */
  398. register char *s1;
  399. register char *s2;
  400. register int middle = first + (last - first) / 2;
  401. /* Fast inline strchr() */
  402. s1 = symbol;
  403. s2 = file->i_defs[middle]->s_name;
  404. while (*s1++ == *s2++)
  405. if (s2[-1] == '\0') break;
  406. /* If exact match, we're done */
  407. if (*--s1 == *--s2)
  408. {
  409. return file->i_defs + middle;
  410. }
  411. /* If symbol > i_defs[middle] ... */
  412. if (*s1 > *s2)
  413. {
  414. first = middle + 1;
  415. }
  416. /* else ... */
  417. else
  418. {
  419. last = middle - 1;
  420. }
  421. }
  422. return NULL;
  423. }
  424. static int
  425. merge2defines(struct inclist *file1, struct inclist *file2)
  426. {
  427. int i;
  428. if ((file1==NULL) || (file2==NULL) ||
  429. !(file2->i_flags & FINISHED))
  430. return 0;
  431. for (i=0; i < file2->i_listlen; i++)
  432. if (file2->i_merged[i]==FALSE)
  433. return 0;
  434. {
  435. int first1 = 0;
  436. int last1 = file1->i_ndefs - 1;
  437. int first2 = 0;
  438. int last2 = file2->i_ndefs - 1;
  439. int first=0;
  440. struct symtab** i_defs = NULL;
  441. int deflen=file1->i_ndefs+file2->i_ndefs;
  442. debug(2,("merging %s into %s\n",
  443. file2->i_file, file1->i_file));
  444. if (deflen>0)
  445. {
  446. /* make sure deflen % SYMTABINC == 0 is still true */
  447. deflen += (SYMTABINC - deflen % SYMTABINC) % SYMTABINC;
  448. i_defs=(struct symtab**)
  449. malloc(deflen*sizeof(struct symtab*));
  450. if (i_defs==NULL) return 0;
  451. }
  452. while ((last1 >= first1) && (last2 >= first2))
  453. {
  454. char *s1=file1->i_defs[first1]->s_name;
  455. char *s2=file2->i_defs[first2]->s_name;
  456. if (strcmp(s1,s2) < 0)
  457. i_defs[first++]=file1->i_defs[first1++];
  458. else if (strcmp(s1,s2) > 0)
  459. i_defs[first++]=file2->i_defs[first2++];
  460. else /* equal */
  461. {
  462. i_defs[first++]=file2->i_defs[first2++];
  463. first1++;
  464. }
  465. }
  466. while (last1 >= first1)
  467. {
  468. i_defs[first++]=file1->i_defs[first1++];
  469. }
  470. while (last2 >= first2)
  471. {
  472. i_defs[first++]=file2->i_defs[first2++];
  473. }
  474. if (file1->i_defs) free(file1->i_defs);
  475. file1->i_defs=i_defs;
  476. file1->i_ndefs=first;
  477. return 1;
  478. }
  479. }
  480. void
  481. undefine(char *symbol, struct inclist *file)
  482. {
  483. register struct symtab **ptr;
  484. struct inclist *srcfile;
  485. while ((ptr = isdefined(symbol, file, &srcfile)) != NULL)
  486. {
  487. srcfile->i_ndefs--;
  488. for (; ptr < srcfile->i_defs + srcfile->i_ndefs; ptr++)
  489. *ptr = ptr[1];
  490. }
  491. }
  492. int
  493. find_includes(struct filepointer *filep, struct inclist *file,
  494. struct inclist *file_red, int recursion, boolean failOK)
  495. {
  496. struct inclist *inclistp;
  497. char **includedirsp;
  498. register char *line;
  499. register int type;
  500. boolean recfailOK;
  501. while ((line = getnextline(filep))) {
  502. switch(type = deftype(line, filep, file_red, file, TRUE)) {
  503. case IF:
  504. doif:
  505. type = find_includes(filep, file,
  506. file_red, recursion+1, failOK);
  507. while ((type == ELIF) || (type == ELIFFALSE) ||
  508. (type == ELIFGUESSFALSE))
  509. type = gobble(filep, file, file_red);
  510. if (type == ELSE)
  511. gobble(filep, file, file_red);
  512. break;
  513. case IFFALSE:
  514. case IFGUESSFALSE:
  515. doiffalse:
  516. if (type == IFGUESSFALSE || type == ELIFGUESSFALSE)
  517. recfailOK = TRUE;
  518. else
  519. recfailOK = failOK;
  520. type = gobble(filep, file, file_red);
  521. if (type == ELSE)
  522. find_includes(filep, file,
  523. file_red, recursion+1, recfailOK);
  524. else
  525. if (type == ELIF)
  526. goto doif;
  527. else
  528. if ((type == ELIFFALSE) || (type == ELIFGUESSFALSE))
  529. goto doiffalse;
  530. break;
  531. case IFDEF:
  532. case IFNDEF:
  533. if ((type == IFDEF && isdefined(line, file_red, NULL))
  534. || (type == IFNDEF && !isdefined(line, file_red, NULL))) {
  535. debug(1,(type == IFNDEF ?
  536. "line %d: %s !def'd in %s via %s%s\n" : "",
  537. filep->f_line, line,
  538. file->i_file, file_red->i_file, ": doit"));
  539. type = find_includes(filep, file,
  540. file_red, recursion+1, failOK);
  541. while (type == ELIF || type == ELIFFALSE || type == ELIFGUESSFALSE)
  542. type = gobble(filep, file, file_red);
  543. if (type == ELSE)
  544. gobble(filep, file, file_red);
  545. }
  546. else {
  547. debug(1,(type == IFDEF ?
  548. "line %d: %s !def'd in %s via %s%s\n" : "",
  549. filep->f_line, line,
  550. file->i_file, file_red->i_file, ": gobble"));
  551. type = gobble(filep, file, file_red);
  552. if (type == ELSE)
  553. find_includes(filep, file,
  554. file_red, recursion+1, failOK);
  555. else if (type == ELIF)
  556. goto doif;
  557. else if (type == ELIFFALSE || type == ELIFGUESSFALSE)
  558. goto doiffalse;
  559. }
  560. break;
  561. case ELSE:
  562. case ELIFFALSE:
  563. case ELIFGUESSFALSE:
  564. case ELIF:
  565. if (!recursion)
  566. gobble(filep, file, file_red);
  567. case ENDIF:
  568. if (recursion)
  569. return(type);
  570. case DEFINE:
  571. define(line, file);
  572. break;
  573. case UNDEF:
  574. if (!*line) {
  575. warning("%s", file_red->i_file);
  576. if (file_red != file)
  577. warning1(" (reading %s)", file->i_file);
  578. warning1(", line %d: incomplete undef == \"%s\"\n",
  579. filep->f_line, line);
  580. break;
  581. }
  582. undefine(line, file_red);
  583. break;
  584. case INCLUDE:
  585. case INCLUDEDOT:
  586. case INCLUDENEXT:
  587. case INCLUDENEXTDOT:
  588. inclistp = inclistnext;
  589. includedirsp = includedirsnext;
  590. debug(2,("%s, reading %s, includes %s\n",
  591. file_red->i_file, file->i_file, line));
  592. add_include(filep, file, file_red, line, type, failOK);
  593. inclistnext = inclistp;
  594. includedirsnext = includedirsp;
  595. break;
  596. case ERROR:
  597. case WARNING:
  598. warning("%s", file_red->i_file);
  599. if (file_red != file)
  600. warning1(" (reading %s)", file->i_file);
  601. warning1(", line %d: %s\n",
  602. filep->f_line, line);
  603. break;
  604. case PRAGMA:
  605. case IDENT:
  606. case SCCS:
  607. case EJECT:
  608. break;
  609. case -1:
  610. warning("%s", file_red->i_file);
  611. if (file_red != file)
  612. warning1(" (reading %s)", file->i_file);
  613. warning1(", line %d: unknown directive == \"%s\"\n",
  614. filep->f_line, line);
  615. break;
  616. case -2:
  617. warning("%s", file_red->i_file);
  618. if (file_red != file)
  619. warning1(" (reading %s)", file->i_file);
  620. warning1(", line %d: incomplete include == \"%s\"\n",
  621. filep->f_line, line);
  622. break;
  623. }
  624. }
  625. file->i_flags |= FINISHED;
  626. debug(2,("finished with %s\n", file->i_file));
  627. return(-1);
  628. }