cppsetup.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these librararies and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $TOG: cppsetup.c /main/18 1998/02/06 11:09:35 kaleb $ */
  24. /*
  25. Copyright (c) 1993, 1994, 1998 The Open Group
  26. All Rights Reserved.
  27. The above copyright notice and this permission notice shall be included in
  28. all copies or substantial portions of the Software.
  29. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  32. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  33. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  34. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  35. Except as contained in this notice, the name of The Open Group shall not be
  36. used in advertising or otherwise to promote the sale, use or other dealings
  37. in this Software without prior written authorization from The Open Group.
  38. */
  39. #include "def.h"
  40. #ifdef CPP
  41. /*
  42. * This file is strictly for the sake of cpy.y and yylex.c (if
  43. * you indeed have the source for cpp).
  44. */
  45. #define IB 1
  46. #define SB 2
  47. #define NB 4
  48. #define CB 8
  49. #define QB 16
  50. #define WB 32
  51. #define SALT '#'
  52. #if pdp11 | vax | ns16000 | mc68000 | ibm032
  53. #define COFF 128
  54. #else
  55. #define COFF 0
  56. #endif
  57. /*
  58. * These variables used by cpy.y and yylex.c
  59. */
  60. extern char *outp, *inp, *newp, *pend;
  61. extern char *ptrtab;
  62. extern char fastab[];
  63. extern char slotab[];
  64. /*
  65. * cppsetup
  66. */
  67. struct filepointer *currentfile;
  68. struct inclist *currentinc;
  69. cppsetup(line, filep, inc)
  70. register char *line;
  71. register struct filepointer *filep;
  72. register struct inclist *inc;
  73. {
  74. register char *p, savec;
  75. static boolean setupdone = FALSE;
  76. boolean value;
  77. if (!setupdone) {
  78. cpp_varsetup();
  79. setupdone = TRUE;
  80. }
  81. currentfile = filep;
  82. currentinc = inc;
  83. inp = newp = line;
  84. for (p=newp; *p; p++)
  85. ;
  86. /*
  87. * put a newline back on the end, and set up pend, etc.
  88. */
  89. *p++ = '\n';
  90. savec = *p;
  91. *p = '\0';
  92. pend = p;
  93. ptrtab = slotab+COFF;
  94. *--inp = SALT;
  95. outp=inp;
  96. value = yyparse();
  97. *p = savec;
  98. return(value);
  99. }
  100. struct symtab **lookup(symbol)
  101. char *symbol;
  102. {
  103. static struct symtab *undefined;
  104. struct symtab **sp;
  105. sp = isdefined(symbol, currentinc, NULL);
  106. if (sp == NULL) {
  107. sp = &undefined;
  108. (*sp)->s_value = NULL;
  109. }
  110. return (sp);
  111. }
  112. pperror(tag, x0,x1,x2,x3,x4)
  113. int tag,x0,x1,x2,x3,x4;
  114. {
  115. warning("\"%s\", line %d: ", currentinc->i_file, currentfile->f_line);
  116. warning(x0,x1,x2,x3,x4);
  117. }
  118. yyerror(s)
  119. register char *s;
  120. {
  121. fatalerr("Fatal error: %s\n", s);
  122. }
  123. #else /* not CPP */
  124. #include "ifparser.h"
  125. struct _parse_data {
  126. struct filepointer *filep;
  127. struct inclist *inc;
  128. const char *line;
  129. };
  130. static const char *
  131. my_if_errors (ip, cp, expecting)
  132. IfParser *ip;
  133. const char *cp;
  134. const char *expecting;
  135. {
  136. struct _parse_data *pd = (struct _parse_data *) ip->data;
  137. int lineno = pd->filep->f_line;
  138. char *filename = pd->inc->i_file;
  139. char prefix[300];
  140. int prefixlen;
  141. int i;
  142. snprintf (prefix, 300, "\"%s\":%d", filename, lineno);
  143. prefixlen = strlen(prefix);
  144. fprintf (stderr, "%s: %s", prefix, pd->line);
  145. i = cp - pd->line;
  146. if (i > 0 && pd->line[i-1] != '\n') {
  147. putc ('\n', stderr);
  148. }
  149. for (i += prefixlen + 3; i > 0; i--) {
  150. putc (' ', stderr);
  151. }
  152. fprintf (stderr, "^--- expecting %s\n", expecting);
  153. return NULL;
  154. }
  155. #define MAXNAMELEN 256
  156. static struct symtab **
  157. lookup_variable (ip, var, len)
  158. IfParser *ip;
  159. const char *var;
  160. int len;
  161. {
  162. char tmpbuf[MAXNAMELEN + 1];
  163. struct _parse_data *pd = (struct _parse_data *) ip->data;
  164. if (len > MAXNAMELEN)
  165. return 0;
  166. strncpy (tmpbuf, var, len);
  167. tmpbuf[len] = '\0';
  168. return isdefined (tmpbuf, pd->inc, NULL);
  169. }
  170. static int
  171. my_eval_defined (ip, var, len)
  172. IfParser *ip;
  173. const char *var;
  174. int len;
  175. {
  176. if (lookup_variable (ip, var, len))
  177. return 1;
  178. else
  179. return 0;
  180. }
  181. #define isvarfirstletter(ccc) (isalpha(ccc) || (ccc) == '_')
  182. static long
  183. my_eval_variable (ip, var, len)
  184. IfParser *ip;
  185. const char *var;
  186. int len;
  187. {
  188. long val;
  189. struct symtab **s;
  190. s = lookup_variable (ip, var, len);
  191. if (!s)
  192. return 0;
  193. do {
  194. var = (*s)->s_value;
  195. if (!isvarfirstletter((int)*var) || !strcmp((*s)->s_name, var))
  196. break;
  197. s = lookup_variable (ip, var, strlen(var));
  198. } while (s);
  199. var = ParseIfExpression(ip, var, &val);
  200. if (var && *var) debug(4, ("extraneous: '%s'\n", var));
  201. return val;
  202. }
  203. int cppsetup(line, filep, inc)
  204. register char *line;
  205. register struct filepointer *filep;
  206. register struct inclist *inc;
  207. {
  208. IfParser ip;
  209. struct _parse_data pd;
  210. long val = 0;
  211. pd.filep = filep;
  212. pd.inc = inc;
  213. pd.line = line;
  214. ip.funcs.handle_error = my_if_errors;
  215. ip.funcs.eval_defined = my_eval_defined;
  216. ip.funcs.eval_variable = my_eval_variable;
  217. ip.data = (char *) &pd;
  218. (void) ParseIfExpression (&ip, line, &val);
  219. if (val)
  220. return IF;
  221. else
  222. return IFFALSE;
  223. }
  224. #endif /* CPP */