cppsetup.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* $Xorg: cppsetup.c,v 1.5 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/cppsetup.c,v 3.11 2001/12/17 20:52:22 dawes Exp $ */
  22. #include "def.h"
  23. #ifdef CPP
  24. /*
  25. * This file is strictly for the sake of cpy.y and yylex.c (if
  26. * you indeed have the source for cpp).
  27. */
  28. #define IB 1
  29. #define SB 2
  30. #define NB 4
  31. #define CB 8
  32. #define QB 16
  33. #define WB 32
  34. #define SALT '#'
  35. #if defined(pdp11) || defined(vax) || defined(ns16000) || defined(mc68000) || defined(ibm032)
  36. #define COFF 128
  37. #else
  38. #define COFF 0
  39. #endif
  40. /*
  41. * These variables used by cpy.y and yylex.c
  42. */
  43. extern char *outp, *inp, *newp, *pend;
  44. extern char *ptrtab;
  45. extern char fastab[];
  46. extern char slotab[];
  47. /*
  48. * cppsetup
  49. */
  50. struct filepointer *currentfile;
  51. struct inclist *currentinc;
  52. int
  53. cppsetup(char *line, struct filepointer *filep, struct inclist *inc)
  54. {
  55. char *p, savec;
  56. static boolean setupdone = FALSE;
  57. boolean value;
  58. if (!setupdone) {
  59. cpp_varsetup();
  60. setupdone = TRUE;
  61. }
  62. currentfile = filep;
  63. currentinc = inc;
  64. inp = newp = line;
  65. for (p=newp; *p; p++)
  66. ;
  67. /*
  68. * put a newline back on the end, and set up pend, etc.
  69. */
  70. *p++ = '\n';
  71. savec = *p;
  72. *p = '\0';
  73. pend = p;
  74. ptrtab = slotab+COFF;
  75. *--inp = SALT;
  76. outp=inp;
  77. value = yyparse();
  78. *p = savec;
  79. return(value);
  80. }
  81. struct symtab **lookup(symbol)
  82. char *symbol;
  83. {
  84. static struct symtab *undefined;
  85. struct symtab **sp;
  86. sp = isdefined(symbol, currentinc, NULL);
  87. if (sp == NULL) {
  88. sp = &undefined;
  89. (*sp)->s_value = NULL;
  90. }
  91. return (sp);
  92. }
  93. pperror(tag, x0,x1,x2,x3,x4)
  94. int tag,x0,x1,x2,x3,x4;
  95. {
  96. warning("\"%s\", line %d: ", currentinc->i_file, currentfile->f_line);
  97. warning(x0,x1,x2,x3,x4);
  98. }
  99. yyerror(s)
  100. register char *s;
  101. {
  102. fatalerr("Fatal error: %s\n", s);
  103. }
  104. #else /* not CPP */
  105. #include "ifparser.h"
  106. struct _parse_data {
  107. struct filepointer *filep;
  108. struct inclist *inc;
  109. char *filename;
  110. const char *line;
  111. };
  112. static const char *
  113. my_if_errors (IfParser *ip, const char *cp, const char *expecting)
  114. {
  115. struct _parse_data *pd = (struct _parse_data *) ip->data;
  116. int lineno = pd->filep->f_line;
  117. char *filename = pd->filename;
  118. char prefix[300];
  119. int prefixlen;
  120. int i;
  121. sprintf (prefix, "\"%s\":%d", filename, lineno);
  122. prefixlen = strlen(prefix);
  123. fprintf (stderr, "%s: %s", prefix, pd->line);
  124. i = cp - pd->line;
  125. if (i > 0 && pd->line[i-1] != '\n') {
  126. putc ('\n', stderr);
  127. }
  128. for (i += prefixlen + 3; i > 0; i--) {
  129. putc (' ', stderr);
  130. }
  131. fprintf (stderr, "^--- expecting %s\n", expecting);
  132. return NULL;
  133. }
  134. #define MAXNAMELEN 256
  135. static struct symtab **
  136. lookup_variable (IfParser *ip, const char *var, int len)
  137. {
  138. char tmpbuf[MAXNAMELEN + 1];
  139. struct _parse_data *pd = (struct _parse_data *) ip->data;
  140. if (len > MAXNAMELEN)
  141. return 0;
  142. strncpy (tmpbuf, var, len);
  143. tmpbuf[len] = '\0';
  144. return isdefined (tmpbuf, pd->inc, NULL);
  145. }
  146. static int
  147. my_eval_defined (IfParser *ip, const char *var, int len)
  148. {
  149. if (lookup_variable (ip, var, len))
  150. return 1;
  151. else
  152. return 0;
  153. }
  154. #define isvarfirstletter(ccc) (isalpha(ccc) || (ccc) == '_')
  155. static long
  156. my_eval_variable (IfParser *ip, const char *var, int len)
  157. {
  158. long val;
  159. struct symtab **s;
  160. s = lookup_variable (ip, var, len);
  161. if (!s)
  162. return 0;
  163. do {
  164. var = (*s)->s_value;
  165. if (!isvarfirstletter(*var) || !strcmp((*s)->s_name, var))
  166. break;
  167. s = lookup_variable (ip, var, strlen(var));
  168. } while (s);
  169. var = ParseIfExpression(ip, var, &val);
  170. if (var && *var) debug(4, ("extraneous: '%s'\n", var));
  171. return val;
  172. }
  173. int
  174. cppsetup(char *filename,
  175. char *line,
  176. struct filepointer *filep,
  177. struct inclist *inc)
  178. {
  179. IfParser ip;
  180. struct _parse_data pd;
  181. long val = 0;
  182. pd.filep = filep;
  183. pd.inc = inc;
  184. pd.line = line;
  185. pd.filename = filename;
  186. ip.funcs.handle_error = my_if_errors;
  187. ip.funcs.eval_defined = my_eval_defined;
  188. ip.funcs.eval_variable = my_eval_variable;
  189. ip.data = (char *) &pd;
  190. (void) ParseIfExpression (&ip, line, &val);
  191. if (val)
  192. return IF;
  193. else
  194. return IFFALSE;
  195. }
  196. #endif /* CPP */