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.

cppsetup.c 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* $XConsortium: cppsetup.c,v 1.13 94/04/17 20:10:32 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. #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 pdp11 | vax | ns16000 | mc68000 | 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. cppsetup(line, filep, inc)
  53. register char *line;
  54. register struct filepointer *filep;
  55. register struct inclist *inc;
  56. {
  57. register char *p, savec;
  58. static boolean setupdone = FALSE;
  59. boolean value;
  60. if (!setupdone) {
  61. cpp_varsetup();
  62. setupdone = TRUE;
  63. }
  64. currentfile = filep;
  65. currentinc = inc;
  66. inp = newp = line;
  67. for (p=newp; *p; p++)
  68. ;
  69. /*
  70. * put a newline back on the end, and set up pend, etc.
  71. */
  72. *p++ = '\n';
  73. savec = *p;
  74. *p = '\0';
  75. pend = p;
  76. ptrtab = slotab+COFF;
  77. *--inp = SALT;
  78. outp=inp;
  79. value = yyparse();
  80. *p = savec;
  81. return(value);
  82. }
  83. struct symtab *lookup(symbol)
  84. char *symbol;
  85. {
  86. static struct symtab undefined;
  87. struct symtab *sp;
  88. sp = isdefined(symbol, currentinc, NULL);
  89. if (sp == NULL) {
  90. sp = &undefined;
  91. sp->s_value = NULL;
  92. }
  93. return (sp);
  94. }
  95. pperror(tag, x0,x1,x2,x3,x4)
  96. int tag,x0,x1,x2,x3,x4;
  97. {
  98. warning("\"%s\", line %d: ", currentinc->i_file, currentfile->f_line);
  99. warning(x0,x1,x2,x3,x4);
  100. }
  101. yyerror(s)
  102. register char *s;
  103. {
  104. fatalerr("Fatal error: %s\n", s);
  105. }
  106. #else /* not CPP */
  107. #include "ifparser.h"
  108. struct _parse_data {
  109. struct filepointer *filep;
  110. struct inclist *inc;
  111. const char *line;
  112. };
  113. static const char *
  114. _my_if_errors (ip, cp, expecting)
  115. IfParser *ip;
  116. const char *cp;
  117. const char *expecting;
  118. {
  119. struct _parse_data *pd = (struct _parse_data *) ip->data;
  120. int lineno = pd->filep->f_line;
  121. char *filename = pd->inc->i_file;
  122. char prefix[300];
  123. int prefixlen;
  124. int i;
  125. sprintf (prefix, "\"%s\":%d", filename, lineno);
  126. prefixlen = strlen(prefix);
  127. fprintf (stderr, "%s: %s", prefix, pd->line);
  128. i = cp - pd->line;
  129. if (i > 0 && pd->line[i-1] != '\n') {
  130. putc ('\n', stderr);
  131. }
  132. for (i += prefixlen + 3; i > 0; i--) {
  133. putc (' ', stderr);
  134. }
  135. fprintf (stderr, "^--- expecting %s\n", expecting);
  136. return NULL;
  137. }
  138. #define MAXNAMELEN 256
  139. static struct symtab *
  140. _lookup_variable (ip, var, len)
  141. IfParser *ip;
  142. const char *var;
  143. int len;
  144. {
  145. char tmpbuf[MAXNAMELEN + 1];
  146. struct _parse_data *pd = (struct _parse_data *) ip->data;
  147. if (len > MAXNAMELEN)
  148. return 0;
  149. strncpy (tmpbuf, var, len);
  150. tmpbuf[len] = '\0';
  151. return isdefined (tmpbuf, pd->inc, NULL);
  152. }
  153. static int
  154. _my_eval_defined (ip, var, len)
  155. IfParser *ip;
  156. const char *var;
  157. int len;
  158. {
  159. if (_lookup_variable (ip, var, len))
  160. return 1;
  161. else
  162. return 0;
  163. }
  164. #define isvarfirstletter(ccc) (isalpha(ccc) || (ccc) == '_')
  165. static int
  166. _my_eval_variable (ip, var, len)
  167. IfParser *ip;
  168. const char *var;
  169. int len;
  170. {
  171. struct symtab *s;
  172. s = _lookup_variable (ip, var, len);
  173. if (!s)
  174. return 0;
  175. do {
  176. var = s->s_value;
  177. if (!isvarfirstletter(*var))
  178. break;
  179. s = _lookup_variable (ip, var, strlen(var));
  180. } while (s);
  181. return atoi(var);
  182. }
  183. cppsetup(line, filep, inc)
  184. register char *line;
  185. register struct filepointer *filep;
  186. register struct inclist *inc;
  187. {
  188. IfParser ip;
  189. struct _parse_data pd;
  190. int val = 0;
  191. pd.filep = filep;
  192. pd.inc = inc;
  193. pd.line = line;
  194. ip.funcs.handle_error = _my_if_errors;
  195. ip.funcs.eval_defined = _my_eval_defined;
  196. ip.funcs.eval_variable = _my_eval_variable;
  197. ip.data = (char *) &pd;
  198. (void) ParseIfExpression (&ip, line, &val);
  199. if (val)
  200. return IF;
  201. else
  202. return IFFALSE;
  203. }
  204. #endif /* CPP */