1 /* $Id: cpp.h,v 1.69 2014/08/18 18:46:05 ragge Exp $ */ 2 3 /* 4 * Copyright (c) 2004,2010 Anders Magnusson (ragge@ludd.luth.se). 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <stdio.h> /* for debug/printf */ 29 30 typedef unsigned char usch; 31 extern usch yytext[]; 32 extern usch *stringbuf; 33 34 extern int trulvl; 35 extern int flslvl; 36 extern int elflvl; 37 extern int elslvl; 38 extern int dflag; 39 extern int tflag, Aflag, Cflag, Pflag; 40 extern int Mflag, dMflag, MPflag, MMDflag; 41 extern usch *Mfile, *MPfile; 42 extern int ofd; 43 extern int defining; 44 45 /* args for lookup() */ 46 #define FIND 0 47 #define ENTER 1 48 49 /* buffer used internally */ 50 #ifndef CPPBUF 51 #if defined(mach_pdp11) 52 #define CPPBUF BUFSIZ 53 #define BUF_STACK 54 #elif defined(os_win32) 55 /* winxp seems to fail > 26608 bytes */ 56 #define CPPBUF 16384 57 #else 58 #define CPPBUF (65536*2) 59 #endif 60 #endif 61 62 #define MAXARGS 128 /* Max # of args to a macro. Should be enough */ 63 64 #define NAMEMAX CPPBUF /* currently pushbackbuffer */ 65 #define BBUFSZ (NAMEMAX+CPPBUF+1) 66 67 #define GCCARG 0xfd /* has gcc varargs that may be replaced with 0 */ 68 #define VARG 0xfe /* has varargs */ 69 #define OBJCT 0xff 70 #define WARN 1 /* SOH, not legal char */ 71 #define CONC 2 /* STX, not legal char */ 72 #define SNUFF 3 /* ETX, not legal char */ 73 #define EBLOCK 4 /* EOT, not legal char */ 74 #define PHOLD 5 /* ENQ, not legal char */ 75 76 /* Used in macro expansion */ 77 #define RECMAX 10000 /* max # of recursive macros */ 78 extern struct symtab *norep[RECMAX]; 79 extern int norepptr; 80 extern unsigned short bptr[RECMAX]; 81 extern int bidx; 82 #define MKB(l,h) (l+((h)<<8)) 83 84 /* quick checks for some characters */ 85 #define C_SPEC 0001 /* for fastscan() parsing */ 86 #define C_2 0002 /* for yylex() tokenizing */ 87 #define C_WSNL 0004 /* ' ','\t','\r','\n' */ 88 #define C_ID 0010 /* [_a-zA-Z0-9] */ 89 #define C_ID0 0020 /* [_a-zA-Z] */ 90 #define C_EP 0040 /* [epEP] */ 91 #define C_DIGIT 0100 /* [0-9] */ 92 #define C_HEX 0200 /* [0-9a-fA-F] */ 93 94 extern usch spechr[]; 95 96 #define iswsnl(x) (spechr[x] & (C_WSNL)) 97 98 /* definition for include file info */ 99 struct includ { 100 struct includ *next; 101 const usch *fname; /* current fn, changed if #line found */ 102 const usch *orgfn; /* current fn, not changed */ 103 int lineno; 104 int escln; /* escaped newlines, to be added */ 105 int infil; 106 usch *curptr; 107 usch *maxread; 108 usch *ostr; 109 usch *buffer; 110 int idx; 111 void *incs; 112 const usch *fn; 113 #ifdef BUF_STACK 114 usch bbuf[BBUFSZ]; 115 #else 116 usch *bbuf; 117 #endif 118 }; 119 #define INCINC 0 120 #define SYSINC 1 121 122 extern struct includ *ifiles; 123 124 /* Symbol table entry */ 125 struct symtab { 126 const usch *namep; 127 const usch *value; 128 const usch *file; 129 int line; 130 }; 131 132 struct initar { 133 struct initar *next; 134 int type; 135 char *str; 136 }; 137 138 /* 139 * Struct used in parse tree evaluation. 140 * op is one of: 141 * - number type (NUMBER, UNUMBER) 142 * - zero (0) if divided by zero. 143 */ 144 struct nd { 145 int op; 146 union { 147 long long val; 148 unsigned long long uval; 149 } n; 150 }; 151 152 #define nd_val n.val 153 #define nd_uval n.uval 154 155 struct symtab *lookup(const usch *namep, int enterf); 156 usch *gotident(struct symtab *nl); 157 int submac(struct symtab *nl, int); 158 int kfind(struct symtab *nl); 159 int doexp(void); 160 int donex(void); 161 void ppdir(void); 162 163 void define(void); 164 void include(void); 165 void include_next(void); 166 void line(void); 167 168 int pushfile(const usch *fname, const usch *fn, int idx, void *incs); 169 void popfile(void); 170 void prtline(void); 171 int yylex(void); 172 int sloscan(void); 173 void cunput(int); 174 int curline(void); 175 char *curfile(void); 176 void setline(int); 177 void setfile(char *); 178 int yyparse(void); 179 void unpstr(const usch *); 180 usch *savstr(const usch *str); 181 void savch(int c); 182 void mainscan(void); 183 void putch(int); 184 void putstr(const usch *s); 185 void line(void); 186 usch *sheap(const char *fmt, ...); 187 void warning(const char *fmt, ...); 188 void error(const char *fmt, ...); 189 int cinput(void); 190 void getcmnt(void); 191 void xwrite(int, const void *, unsigned int); 192