ppl.l 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605
  1. /* -*-C-*-
  2. * Wrc preprocessor lexical analysis
  3. *
  4. * Copyright 1999-2000 Bertho A. Stultiens (BS)
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. *
  20. * History:
  21. * 24-Apr-2000 BS - Started from scratch to restructure everything
  22. * and reintegrate the source into the wine-tree.
  23. * 04-Jan-2000 BS - Added comments about the lexicographical
  24. * grammar to give some insight in the complexity.
  25. * 28-Dec-1999 BS - Eliminated backing-up of the flexer by running
  26. * `flex -b' on the source. This results in some
  27. * weirdo extra rules, but a much faster scanner.
  28. * 23-Dec-1999 BS - Started this file
  29. *
  30. *-------------------------------------------------------------------------
  31. * The preprocessor's lexographical grammar (approximately):
  32. *
  33. * pp := {ws} # {ws} if {ws} {expr} {ws} \n
  34. * | {ws} # {ws} ifdef {ws} {id} {ws} \n
  35. * | {ws} # {ws} ifndef {ws} {id} {ws} \n
  36. * | {ws} # {ws} elif {ws} {expr} {ws} \n
  37. * | {ws} # {ws} else {ws} \n
  38. * | {ws} # {ws} endif {ws} \n
  39. * | {ws} # {ws} include {ws} < {anytext} > \n
  40. * | {ws} # {ws} include {ws} " {anytext} " \n
  41. * | {ws} # {ws} define {ws} {anytext} \n
  42. * | {ws} # {ws} define( {arglist} ) {ws} {expansion} \n
  43. * | {ws} # {ws} pragma {ws} {anytext} \n
  44. * | {ws} # {ws} ident {ws} {anytext} \n
  45. * | {ws} # {ws} error {ws} {anytext} \n
  46. * | {ws} # {ws} warning {ws} {anytext} \n
  47. * | {ws} # {ws} line {ws} " {anytext} " {number} \n
  48. * | {ws} # {ws} {number} " {anytext} " {number} [ {number} [{number}] ] \n
  49. * | {ws} # {ws} \n
  50. *
  51. * ws := [ \t\r\f\v]*
  52. *
  53. * expr := {expr} [+-*%^/|&] {expr}
  54. * | {expr} {logor|logand} {expr}
  55. * | [!~+-] {expr}
  56. * | {expr} ? {expr} : {expr}
  57. *
  58. * logor := ||
  59. *
  60. * logand := &&
  61. *
  62. * id := [a-zA-Z_][a-zA-Z0-9_]*
  63. *
  64. * anytext := [^\n]* (see note)
  65. *
  66. * arglist :=
  67. * | {id}
  68. * | {arglist} , {id}
  69. * | {arglist} , {id} ...
  70. *
  71. * expansion := {id}
  72. * | # {id}
  73. * | {anytext}
  74. * | {anytext} ## {anytext}
  75. *
  76. * number := [0-9]+
  77. *
  78. * Note: "anytext" is not always "[^\n]*". This is because the
  79. * trailing context must be considered as well.
  80. *
  81. * The only certain assumption for the preprocessor to make is that
  82. * directives start at the beginning of the line, followed by a '#'
  83. * and end with a newline.
  84. * Any directive may be suffixed with a line-continuation. Also
  85. * classical comment / *...* / (note: no comments within comments,
  86. * therefore spaces) is considered to be a line-continuation
  87. * (according to gcc and egcs AFAIK, ANSI is a bit vague).
  88. * Comments have not been added to the above grammar for simplicity
  89. * reasons. However, it is allowed to enter comment anywhere within
  90. * the directives as long as they do not interfere with the context.
  91. * All comments are considered to be deletable whitespace (both
  92. * classical form "/ *...* /" and C++ form "//...\n").
  93. *
  94. * All recursive scans, except for macro-expansion, are done by the
  95. * parser, whereas the simple state transitions of non-recursive
  96. * directives are done in the scanner. This results in the many
  97. * exclusive start-conditions of the scanner.
  98. *
  99. * Macro expansions are slightly more difficult because they have to
  100. * prescan the arguments. Parameter substitution is literal if the
  101. * substitution is # or ## (either side). This enables new identifiers
  102. * to be created (see 'info cpp' node Macro|Pitfalls|Prescan for more
  103. * information).
  104. *
  105. * FIXME: Variable macro parameters is recognized, but not yet
  106. * expanded. I have to reread the ANSI standard on the subject (yes,
  107. * ANSI defines it).
  108. *
  109. * The following special defines are supported:
  110. * __FILE__ -> "thissource.c"
  111. * __LINE__ -> 123
  112. * __DATE__ -> "May 1 2000"
  113. * __TIME__ -> "23:59:59"
  114. * These macros expand, as expected, into their ANSI defined values.
  115. *
  116. * The same include prevention is implemented as gcc and egcs does.
  117. * This results in faster processing because we do not read the text
  118. * at all. Some wine-sources attempt to include the same file 4 or 5
  119. * times. This strategy also saves a lot blank output-lines, which in
  120. * its turn improves the real resource scanner/parser.
  121. *
  122. */
  123. %top{
  124. #include "config.h"
  125. #include "wine/port.h"
  126. }
  127. /*
  128. * Special flex options and exclusive scanner start-conditions
  129. */
  130. %option stack
  131. %option 8bit never-interactive
  132. %option noinput nounput
  133. %option prefix="ppy_"
  134. %x pp_pp
  135. %x pp_eol
  136. %x pp_inc
  137. %x pp_dqs
  138. %x pp_sqs
  139. %x pp_iqs
  140. %x pp_comment
  141. %x pp_def
  142. %x pp_define
  143. %x pp_macro
  144. %x pp_mbody
  145. %x pp_macign
  146. %x pp_macscan
  147. %x pp_macexp
  148. %x pp_if
  149. %x pp_ifd
  150. %x pp_ifignored
  151. %x pp_endif
  152. %x pp_line
  153. %x pp_defined
  154. %x pp_ignore
  155. %x RCINCL
  156. ws [ \v\f\t\r]
  157. cident [a-zA-Z_][0-9a-zA-Z_]*
  158. ul [uUlL]|[uUlL][lL]|[lL][uU]|[lL][lL][uU]|[uU][lL][lL]|[lL][uU][lL]
  159. %{
  160. #include <stdio.h>
  161. #include <stdlib.h>
  162. #include <string.h>
  163. #include <ctype.h>
  164. #include <assert.h>
  165. #include <errno.h>
  166. #include <limits.h>
  167. #ifndef LLONG_MAX
  168. # define LLONG_MAX ((__int64)0x7fffffff << 32 | 0xffffffff)
  169. # define LLONG_MIN (-LLONG_MAX - 1)
  170. #endif
  171. #ifndef ULLONG_MAX
  172. # define ULLONG_MAX ((__int64)0xffffffff << 32 | 0xffffffff)
  173. #endif
  174. #ifndef HAVE_UNISTD_H
  175. #define YY_NO_UNISTD_H
  176. #endif
  177. #include "wine/wpp.h"
  178. #include "wpp_private.h"
  179. #include "ppy.tab.h"
  180. /*
  181. * Make sure that we are running an appropriate version of flex.
  182. */
  183. #if !defined(YY_FLEX_MAJOR_VERSION) || (1000 * YY_FLEX_MAJOR_VERSION + YY_FLEX_MINOR_VERSION < 2005)
  184. #error Must use flex version 2.5.1 or higher (yy_scan_* routines are required).
  185. #endif
  186. #define YY_READ_BUF_SIZE 65536 /* So we read most of a file at once */
  187. #define yy_current_state() YY_START
  188. #define yy_pp_state(x) yy_pop_state(); yy_push_state(x)
  189. /*
  190. * Always update the current character position within a line
  191. */
  192. #define YY_USER_ACTION pp_status.char_number+=ppy_leng;
  193. /*
  194. * Buffer management for includes and expansions
  195. */
  196. #define MAXBUFFERSTACK 128 /* Nesting more than 128 includes or macro expansion textss is insane */
  197. typedef struct bufferstackentry {
  198. YY_BUFFER_STATE bufferstate; /* Buffer to switch back to */
  199. FILE *file; /* File handle */
  200. pp_entry_t *define; /* Points to expanding define or NULL if handling includes */
  201. int line_number; /* Line that we were handling */
  202. int char_number; /* The current position on that line */
  203. char *filename; /* Filename that we were handling */
  204. int if_depth; /* How many #if:s deep to check matching #endif:s */
  205. int ncontinuations; /* Remember the continuation state */
  206. int should_pop; /* Set if we must pop the start-state on EOF */
  207. /* Include management */
  208. include_state_t incl;
  209. char *include_filename;
  210. } bufferstackentry_t;
  211. #define ALLOCBLOCKSIZE (1 << 10) /* Allocate these chunks at a time for string-buffers */
  212. /*
  213. * Macro expansion nesting
  214. * We need the stack to handle expansions while scanning
  215. * a macro's arguments. The TOS must always be the macro
  216. * that receives the current expansion from the scanner.
  217. */
  218. #define MAXMACEXPSTACK 128 /* Nesting more than 128 macro expansions is insane */
  219. typedef struct macexpstackentry {
  220. pp_entry_t *ppp; /* This macro we are scanning */
  221. char **args; /* With these arguments */
  222. char **ppargs; /* Resulting in these preprocessed arguments */
  223. int *nnls; /* Number of newlines per argument */
  224. int nargs; /* And this many arguments scanned */
  225. int parentheses; /* Nesting level of () */
  226. int curargsize; /* Current scanning argument's size */
  227. int curargalloc; /* Current scanning argument's block allocated */
  228. char *curarg; /* Current scanning argument's content */
  229. } macexpstackentry_t;
  230. #define MACROPARENTHESES() (top_macro()->parentheses)
  231. /*
  232. * Prototypes
  233. */
  234. static void newline(int);
  235. static int make_number(int radix, YYSTYPE *val, const char *str, int len);
  236. static void put_buffer(const char *s, int len);
  237. /* Buffer management */
  238. static void push_buffer(pp_entry_t *ppp, char *filename, char *incname, int pop);
  239. static bufferstackentry_t *pop_buffer(void);
  240. /* String functions */
  241. static void new_string(void);
  242. static void add_string(const char *str, int len);
  243. static char *get_string(void);
  244. static void put_string(void);
  245. static int string_start(void);
  246. /* Macro functions */
  247. static void push_macro(pp_entry_t *ppp);
  248. static macexpstackentry_t *top_macro(void);
  249. static macexpstackentry_t *pop_macro(void);
  250. static void free_macro(macexpstackentry_t *mep);
  251. static void add_text_to_macro(const char *text, int len);
  252. static void macro_add_arg(int last);
  253. static void macro_add_expansion(void);
  254. /* Expansion */
  255. static void expand_special(pp_entry_t *ppp);
  256. static void expand_define(pp_entry_t *ppp);
  257. static void expand_macro(macexpstackentry_t *mep);
  258. /*
  259. * Local variables
  260. */
  261. static int ncontinuations;
  262. static int strbuf_idx = 0;
  263. static int strbuf_alloc = 0;
  264. static char *strbuffer = NULL;
  265. static int str_startline;
  266. static macexpstackentry_t *macexpstack[MAXMACEXPSTACK];
  267. static int macexpstackidx = 0;
  268. static bufferstackentry_t bufferstack[MAXBUFFERSTACK];
  269. static int bufferstackidx = 0;
  270. /*
  271. * Global variables
  272. */
  273. include_state_t pp_incl_state =
  274. {
  275. -1, /* state */
  276. NULL, /* ppp */
  277. 0, /* ifdepth */
  278. 0 /* seen_junk */
  279. };
  280. struct list pp_includelogiclist = LIST_INIT( pp_includelogiclist );
  281. #define YY_INPUT(buf,result,max_size) \
  282. { \
  283. result = fread(buf, 1, max_size, pp_status.file); \
  284. }
  285. #define BUFFERINITIALCAPACITY 256
  286. void pp_writestring(const char *format, ...)
  287. {
  288. va_list valist;
  289. int len;
  290. static char *buffer;
  291. static int buffercapacity;
  292. char *new_buffer;
  293. if(buffercapacity == 0)
  294. {
  295. buffer = pp_xmalloc(BUFFERINITIALCAPACITY);
  296. buffercapacity = BUFFERINITIALCAPACITY;
  297. }
  298. va_start(valist, format);
  299. len = vsnprintf(buffer, buffercapacity,
  300. format, valist);
  301. va_end(valist);
  302. /* If the string is longer than buffersize, vsnprintf returns
  303. * the string length with glibc >= 2.1, -1 with glibc < 2.1 */
  304. while(len > buffercapacity || len < 0)
  305. {
  306. do
  307. {
  308. buffercapacity *= 2;
  309. } while(len > buffercapacity);
  310. new_buffer = pp_xrealloc(buffer, buffercapacity);
  311. if(new_buffer == NULL)
  312. {
  313. va_end(valist);
  314. return;
  315. }
  316. buffer = new_buffer;
  317. va_start(valist, format);
  318. len = vsnprintf(buffer, buffercapacity,
  319. format, valist);
  320. va_end(valist);
  321. }
  322. fwrite(buffer, 1, len, ppy_out);
  323. }
  324. %}
  325. /*
  326. **************************************************************************
  327. * The scanner starts here
  328. **************************************************************************
  329. */
  330. %%
  331. /*
  332. * Catch line-continuations.
  333. * Note: Gcc keeps the line-continuations in, for example, strings
  334. * intact. However, I prefer to remove them all so that the next
  335. * scanner will not need to reduce the continuation state.
  336. *
  337. * <*>\\\n newline(0);
  338. */
  339. /*
  340. * Detect the leading # of a preprocessor directive.
  341. */
  342. <INITIAL,pp_ignore>^{ws}*# pp_incl_state.seen_junk++; yy_push_state(pp_pp);
  343. /*
  344. * Scan for the preprocessor directives
  345. */
  346. <pp_pp>{ws}*include{ws}* if(yy_top_state() != pp_ignore) {yy_pp_state(pp_inc); return tINCLUDE;} else {yy_pp_state(pp_eol);}
  347. <pp_pp>{ws}*define{ws}* yy_pp_state(yy_current_state() != pp_ignore ? pp_def : pp_eol);
  348. <pp_pp>{ws}*error{ws}* yy_pp_state(pp_eol); if(yy_top_state() != pp_ignore) return tERROR;
  349. <pp_pp>{ws}*warning{ws}* yy_pp_state(pp_eol); if(yy_top_state() != pp_ignore) return tWARNING;
  350. <pp_pp>{ws}*pragma{ws}* yy_pp_state(pp_eol); if(yy_top_state() != pp_ignore) return tPRAGMA;
  351. <pp_pp>{ws}*ident{ws}* yy_pp_state(pp_eol); if(yy_top_state() != pp_ignore) return tPPIDENT;
  352. <pp_pp>{ws}*undef{ws}* if(yy_top_state() != pp_ignore) {yy_pp_state(pp_ifd); return tUNDEF;} else {yy_pp_state(pp_eol);}
  353. <pp_pp>{ws}*ifdef{ws}* yy_pp_state(pp_ifd); return tIFDEF;
  354. <pp_pp>{ws}*ifndef{ws}* pp_incl_state.seen_junk--; yy_pp_state(pp_ifd); return tIFNDEF;
  355. <pp_pp>{ws}*if{ws}* if(yy_top_state() != pp_ignore) {yy_pp_state(pp_if);} else {yy_pp_state(pp_ifignored);} return tIF;
  356. <pp_pp>{ws}*elif{ws}* yy_pp_state(pp_if); return tELIF;
  357. <pp_pp>{ws}*else{ws}* yy_pp_state(pp_endif); return tELSE;
  358. <pp_pp>{ws}*endif{ws}* yy_pp_state(pp_endif); return tENDIF;
  359. <pp_pp>{ws}*line{ws}* if(yy_top_state() != pp_ignore) {yy_pp_state(pp_line); return tLINE;} else {yy_pp_state(pp_eol);}
  360. <pp_pp>{ws}+ if(yy_top_state() != pp_ignore) {yy_pp_state(pp_line); return tGCCLINE;} else {yy_pp_state(pp_eol);}
  361. <pp_pp>{ws}*[a-z]+ ppy_error("Invalid preprocessor token '%s'", ppy_text);
  362. <pp_pp>\r?\n newline(1); yy_pop_state(); return tNL; /* This could be the null-token */
  363. <pp_pp>\\\r?\n newline(0);
  364. <pp_pp>\\\r? ppy_error("Preprocessor junk '%s'", ppy_text);
  365. <pp_pp>. return *ppy_text;
  366. /*
  367. * Handle #include and #line
  368. */
  369. <pp_line>[0-9]+ return make_number(10, &ppy_lval, ppy_text, ppy_leng);
  370. <pp_inc>\< new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_iqs);
  371. <pp_inc,pp_line>\" new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_dqs);
  372. <pp_inc,pp_line>{ws}+ ;
  373. <pp_inc,pp_line>\n newline(1); yy_pop_state(); return tNL;
  374. <pp_inc,pp_line>\\\r?\n newline(0);
  375. <pp_inc,pp_line>(\\\r?)|(.) ppy_error(yy_current_state() == pp_inc ? "Trailing junk in #include" : "Trailing junk in #line");
  376. /*
  377. * Ignore all input when a false clause is parsed
  378. */
  379. <pp_ignore>[^#/\\\n]+ ;
  380. <pp_ignore>\n newline(1);
  381. <pp_ignore>\\\r?\n newline(0);
  382. <pp_ignore>(\\\r?)|(.) ;
  383. /*
  384. * Handle #if and #elif.
  385. * These require conditionals to be evaluated, but we do not
  386. * want to jam the scanner normally when we see these tokens.
  387. * Note: tIDENT is handled below.
  388. */
  389. <pp_if>0[0-7]*{ul}? return make_number(8, &ppy_lval, ppy_text, ppy_leng);
  390. <pp_if>0[0-7]*[8-9]+{ul}? ppy_error("Invalid octal digit");
  391. <pp_if>[1-9][0-9]*{ul}? return make_number(10, &ppy_lval, ppy_text, ppy_leng);
  392. <pp_if>0[xX][0-9a-fA-F]+{ul}? return make_number(16, &ppy_lval, ppy_text, ppy_leng);
  393. <pp_if>0[xX] ppy_error("Invalid hex number");
  394. <pp_if>defined yy_push_state(pp_defined); return tDEFINED;
  395. <pp_if>"<<" return tLSHIFT;
  396. <pp_if>">>" return tRSHIFT;
  397. <pp_if>"&&" return tLOGAND;
  398. <pp_if>"||" return tLOGOR;
  399. <pp_if>"==" return tEQ;
  400. <pp_if>"!=" return tNE;
  401. <pp_if>"<=" return tLTE;
  402. <pp_if>">=" return tGTE;
  403. <pp_if>\n newline(1); yy_pop_state(); return tNL;
  404. <pp_if>\\\r?\n newline(0);
  405. <pp_if>\\\r? ppy_error("Junk in conditional expression");
  406. <pp_if>{ws}+ ;
  407. <pp_if>\' new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_sqs);
  408. <pp_if>\" ppy_error("String constants not allowed in conditionals");
  409. <pp_if>. return *ppy_text;
  410. <pp_ifignored>[^\n]+ ppy_lval.sint = 0; return tSINT;
  411. <pp_ifignored>\n newline(1); yy_pop_state(); return tNL;
  412. /*
  413. * Handle #ifdef, #ifndef and #undef
  414. * to get only an untranslated/unexpanded identifier
  415. */
  416. <pp_ifd>{cident} ppy_lval.cptr = pp_xstrdup(ppy_text); return tIDENT;
  417. <pp_ifd>{ws}+ ;
  418. <pp_ifd>\n newline(1); yy_pop_state(); return tNL;
  419. <pp_ifd>\\\r?\n newline(0);
  420. <pp_ifd>(\\\r?)|(.) ppy_error("Identifier expected");
  421. /*
  422. * Handle #else and #endif.
  423. */
  424. <pp_endif>{ws}+ ;
  425. <pp_endif>\n newline(1); yy_pop_state(); return tNL;
  426. <pp_endif>\\\r?\n newline(0);
  427. <pp_endif>. ppy_error("Garbage after #else or #endif.");
  428. /*
  429. * Handle the special 'defined' keyword.
  430. * This is necessary to get the identifier prior to any
  431. * substitutions.
  432. */
  433. <pp_defined>{cident} yy_pop_state(); ppy_lval.cptr = pp_xstrdup(ppy_text); return tIDENT;
  434. <pp_defined>{ws}+ ;
  435. <pp_defined>(\()|(\)) return *ppy_text;
  436. <pp_defined>\\\r?\n newline(0);
  437. <pp_defined>(\\.)|(\n)|(.) ppy_error("Identifier expected");
  438. /*
  439. * Handle #error, #warning, #pragma and #ident.
  440. * Pass everything literally to the parser, which
  441. * will act appropriately.
  442. * Comments are stripped from the literal text.
  443. */
  444. <pp_eol>[^/\\\n]+ if(yy_top_state() != pp_ignore) { ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL; }
  445. <pp_eol>\/[^/\\\n*]* if(yy_top_state() != pp_ignore) { ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL; }
  446. <pp_eol>(\\\r?)|(\/[^/*]) if(yy_top_state() != pp_ignore) { ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL; }
  447. <pp_eol>\n newline(1); yy_pop_state(); if(yy_current_state() != pp_ignore) { return tNL; }
  448. <pp_eol>\\\r?\n newline(0);
  449. /*
  450. * Handle left side of #define
  451. */
  452. <pp_def>{cident}\( ppy_lval.cptr = pp_xstrdup(ppy_text); ppy_lval.cptr[ppy_leng-1] = '\0'; yy_pp_state(pp_macro); return tMACRO;
  453. <pp_def>{cident} ppy_lval.cptr = pp_xstrdup(ppy_text); yy_pp_state(pp_define); return tDEFINE;
  454. <pp_def>{ws}+ ;
  455. <pp_def>\\\r?\n newline(0);
  456. <pp_def>(\\\r?)|(\n)|(.) perror("Identifier expected");
  457. /*
  458. * Scan the substitution of a define
  459. */
  460. <pp_define>[^'"/\\\n]+ ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL;
  461. <pp_define>(\\\r?)|(\/[^/*]) ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL;
  462. <pp_define>\\\r?\n{ws}+ newline(0); ppy_lval.cptr = pp_xstrdup(" "); return tLITERAL;
  463. <pp_define>\\\r?\n newline(0);
  464. <pp_define>\n newline(1); yy_pop_state(); return tNL;
  465. <pp_define>\' new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_sqs);
  466. <pp_define>\" new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_dqs);
  467. /*
  468. * Scan the definition macro arguments
  469. */
  470. <pp_macro>\){ws}* yy_pp_state(pp_mbody); return tMACROEND;
  471. <pp_macro>{ws}+ ;
  472. <pp_macro>{cident} ppy_lval.cptr = pp_xstrdup(ppy_text); return tIDENT;
  473. <pp_macro>, return ',';
  474. <pp_macro>"..." return tELLIPSIS;
  475. <pp_macro>(\\\r?)|(\n)|(.)|(\.\.?) ppy_error("Argument identifier expected");
  476. <pp_macro>\\\r?\n newline(0);
  477. /*
  478. * Scan the substitution of a macro
  479. */
  480. <pp_mbody>[^a-zA-Z0-9'"#/\\\n]+ ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL;
  481. <pp_mbody>{cident} ppy_lval.cptr = pp_xstrdup(ppy_text); return tIDENT;
  482. <pp_mbody>\#\# return tCONCAT;
  483. <pp_mbody>\# return tSTRINGIZE;
  484. <pp_mbody>[0-9][a-zA-Z0-9]*[^a-zA-Z0-9'"#/\\\n]* ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL;
  485. <pp_mbody>(\\\r?)|(\/[^/*'"#\\\n]*) ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL;
  486. <pp_mbody>\\\r?\n{ws}+ newline(0); ppy_lval.cptr = pp_xstrdup(" "); return tLITERAL;
  487. <pp_mbody>\\\r?\n newline(0);
  488. <pp_mbody>\n newline(1); yy_pop_state(); return tNL;
  489. <pp_mbody>\' new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_sqs);
  490. <pp_mbody>\" new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_dqs);
  491. /*
  492. * Macro expansion text scanning.
  493. * This state is active just after the identifier is scanned
  494. * that triggers an expansion. We *must* delete the leading
  495. * whitespace before we can start scanning for arguments.
  496. *
  497. * If we do not see a '(' as next trailing token, then we have
  498. * a false alarm. We just continue with a nose-bleed...
  499. */
  500. <pp_macign>{ws}*/\( yy_pp_state(pp_macscan);
  501. <pp_macign>{ws}*\n {
  502. if(yy_top_state() != pp_macscan)
  503. newline(0);
  504. }
  505. <pp_macign>{ws}*\\\r?\n newline(0);
  506. <pp_macign>{ws}+|{ws}*\\\r?|. {
  507. macexpstackentry_t *mac = pop_macro();
  508. yy_pop_state();
  509. put_buffer(mac->ppp->ident, strlen(mac->ppp->ident));
  510. put_buffer(ppy_text, ppy_leng);
  511. free_macro(mac);
  512. }
  513. /*
  514. * Macro expansion argument text scanning.
  515. * This state is active when a macro's arguments are being read for expansion.
  516. */
  517. <pp_macscan>\( {
  518. if(++MACROPARENTHESES() > 1)
  519. add_text_to_macro(ppy_text, ppy_leng);
  520. }
  521. <pp_macscan>\) {
  522. if(--MACROPARENTHESES() == 0)
  523. {
  524. yy_pop_state();
  525. macro_add_arg(1);
  526. }
  527. else
  528. add_text_to_macro(ppy_text, ppy_leng);
  529. }
  530. <pp_macscan>, {
  531. if(MACROPARENTHESES() > 1)
  532. add_text_to_macro(ppy_text, ppy_leng);
  533. else
  534. macro_add_arg(0);
  535. }
  536. <pp_macscan>\" new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_dqs);
  537. <pp_macscan>\' new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_sqs);
  538. <pp_macscan>"/*" yy_push_state(pp_comment); add_text_to_macro(" ", 1);
  539. <pp_macscan>\n pp_status.line_number++; pp_status.char_number = 1; add_text_to_macro(ppy_text, ppy_leng);
  540. <pp_macscan>([^/(),\\\n"']+)|(\/[^/*(),\\\n'"]*)|(\\\r?)|(.) add_text_to_macro(ppy_text, ppy_leng);
  541. <pp_macscan>\\\r?\n newline(0);
  542. /*
  543. * Comment handling (almost all start-conditions)
  544. */
  545. <INITIAL,pp_pp,pp_ignore,pp_eol,pp_inc,pp_if,pp_ifd,pp_endif,pp_defined,pp_def,pp_define,pp_macro,pp_mbody,RCINCL>"/*" yy_push_state(pp_comment);
  546. <pp_comment>[^*\n]*|"*"+[^*/\n]* ;
  547. <pp_comment>\n newline(0);
  548. <pp_comment>"*"+"/" yy_pop_state();
  549. /*
  550. * Remove C++ style comment (almost all start-conditions)
  551. */
  552. <INITIAL,pp_pp,pp_ignore,pp_eol,pp_inc,pp_if,pp_ifd,pp_endif,pp_defined,pp_def,pp_define,pp_macro,pp_mbody,pp_macscan,RCINCL>"//"[^\n]* {
  553. if(ppy_text[ppy_leng-1] == '\\')
  554. ppy_warning("C++ style comment ends with an escaped newline (escape ignored)");
  555. }
  556. /*
  557. * Single, double and <> quoted constants
  558. */
  559. <INITIAL,pp_macexp>\" pp_incl_state.seen_junk++; new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_dqs);
  560. <INITIAL,pp_macexp>\' pp_incl_state.seen_junk++; new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_sqs);
  561. <pp_dqs>[^"\\\n]+ add_string(ppy_text, ppy_leng);
  562. <pp_dqs>\" {
  563. add_string(ppy_text, ppy_leng);
  564. yy_pop_state();
  565. switch(yy_current_state())
  566. {
  567. case pp_pp:
  568. case pp_define:
  569. case pp_mbody:
  570. case pp_inc:
  571. case RCINCL:
  572. if (yy_current_state()==RCINCL) yy_pop_state();
  573. ppy_lval.cptr = get_string();
  574. return tDQSTRING;
  575. case pp_line:
  576. ppy_lval.cptr = get_string();
  577. return tDQSTRING;
  578. default:
  579. put_string();
  580. }
  581. }
  582. <pp_sqs>[^'\\\n]+ add_string(ppy_text, ppy_leng);
  583. <pp_sqs>\' {
  584. add_string(ppy_text, ppy_leng);
  585. yy_pop_state();
  586. switch(yy_current_state())
  587. {
  588. case pp_if:
  589. case pp_define:
  590. case pp_mbody:
  591. ppy_lval.cptr = get_string();
  592. return tSQSTRING;
  593. default:
  594. put_string();
  595. }
  596. }
  597. <pp_iqs>[^\>\\\n]+ add_string(ppy_text, ppy_leng);
  598. <pp_iqs>\> {
  599. add_string(ppy_text, ppy_leng);
  600. yy_pop_state();
  601. ppy_lval.cptr = get_string();
  602. return tIQSTRING;
  603. }
  604. <pp_dqs>\\\r?\n {
  605. /*
  606. * This is tricky; we need to remove the line-continuation
  607. * from preprocessor strings, but OTOH retain them in all
  608. * other strings. This is because the resource grammar is
  609. * even more braindead than initially analysed and line-
  610. * continuations in strings introduce, sigh, newlines in
  611. * the output. There goes the concept of non-breaking, non-
  612. * spacing whitespace.
  613. */
  614. switch(yy_top_state())
  615. {
  616. case pp_pp:
  617. case pp_define:
  618. case pp_mbody:
  619. case pp_inc:
  620. case pp_line:
  621. newline(0);
  622. break;
  623. default:
  624. add_string(ppy_text, ppy_leng);
  625. newline(-1);
  626. }
  627. }
  628. <pp_iqs,pp_dqs,pp_sqs>\\. add_string(ppy_text, ppy_leng);
  629. <pp_iqs,pp_dqs,pp_sqs>\n {
  630. newline(1);
  631. add_string(ppy_text, ppy_leng);
  632. ppy_warning("Newline in string constant encountered (started line %d)", string_start());
  633. }
  634. /*
  635. * Identifier scanning
  636. */
  637. <INITIAL,pp_if,pp_inc,pp_macexp>{cident} {
  638. pp_entry_t *ppp;
  639. pp_incl_state.seen_junk++;
  640. if(!(ppp = pplookup(ppy_text)))
  641. {
  642. if(yy_current_state() == pp_inc)
  643. ppy_error("Expected include filename");
  644. else if(yy_current_state() == pp_if)
  645. {
  646. ppy_lval.cptr = pp_xstrdup(ppy_text);
  647. return tIDENT;
  648. }
  649. else {
  650. if((yy_current_state()==INITIAL) && (strcasecmp(ppy_text,"RCINCLUDE")==0)){
  651. yy_push_state(RCINCL);
  652. return tRCINCLUDE;
  653. }
  654. else put_buffer(ppy_text, ppy_leng);
  655. }
  656. }
  657. else if(!ppp->expanding)
  658. {
  659. switch(ppp->type)
  660. {
  661. case def_special:
  662. expand_special(ppp);
  663. break;
  664. case def_define:
  665. expand_define(ppp);
  666. break;
  667. case def_macro:
  668. yy_push_state(pp_macign);
  669. push_macro(ppp);
  670. break;
  671. default:
  672. pp_internal_error(__FILE__, __LINE__, "Invalid define type %d\n", ppp->type);
  673. }
  674. }
  675. else put_buffer(ppy_text, ppy_leng);
  676. }
  677. /*
  678. * Everything else that needs to be passed and
  679. * newline and continuation handling
  680. */
  681. <INITIAL,pp_macexp>[^a-zA-Z_#'"/\\\n \r\t\f\v]+|(\/|\\)[^a-zA-Z_/*'"\\\n \r\t\v\f]* pp_incl_state.seen_junk++; put_buffer(ppy_text, ppy_leng);
  682. <INITIAL,pp_macexp>{ws}+ put_buffer(ppy_text, ppy_leng);
  683. <INITIAL>\n newline(1);
  684. <INITIAL>\\\r?\n newline(0);
  685. <INITIAL>\\\r? pp_incl_state.seen_junk++; put_buffer(ppy_text, ppy_leng);
  686. /*
  687. * Special catcher for macro argmument expansion to prevent
  688. * newlines to propagate to the output or admin.
  689. */
  690. <pp_macexp>(\n)|(.)|(\\\r?(\n|.)) put_buffer(ppy_text, ppy_leng);
  691. <RCINCL>[A-Za-z0-9_\.\\/]+ {
  692. ppy_lval.cptr=pp_xstrdup(ppy_text);
  693. yy_pop_state();
  694. return tRCINCLUDEPATH;
  695. }
  696. <RCINCL>{ws}+ ;
  697. <RCINCL>\" {
  698. new_string(); add_string(ppy_text,ppy_leng);yy_push_state(pp_dqs);
  699. }
  700. /*
  701. * This is a 'catch-all' rule to discover errors in the scanner
  702. * in an orderly manner.
  703. */
  704. <*>. pp_incl_state.seen_junk++; ppy_warning("Unmatched text '%c' (0x%02x); please report\n", isprint(*ppy_text & 0xff) ? *ppy_text : ' ', *ppy_text);
  705. <<EOF>> {
  706. YY_BUFFER_STATE b = YY_CURRENT_BUFFER;
  707. bufferstackentry_t *bep = pop_buffer();
  708. if((!bep && pp_get_if_depth()) || (bep && pp_get_if_depth() != bep->if_depth))
  709. ppy_warning("Unmatched #if/#endif at end of file");
  710. if(!bep)
  711. {
  712. if(YY_START != INITIAL)
  713. {
  714. ppy_error("Unexpected end of file during preprocessing");
  715. BEGIN(INITIAL);
  716. }
  717. yyterminate();
  718. }
  719. else if(bep->should_pop == 2)
  720. {
  721. macexpstackentry_t *mac;
  722. mac = pop_macro();
  723. expand_macro(mac);
  724. }
  725. ppy__delete_buffer(b);
  726. }
  727. %%
  728. /*
  729. **************************************************************************
  730. * Support functions
  731. **************************************************************************
  732. */
  733. #ifndef ppy_wrap
  734. int ppy_wrap(void)
  735. {
  736. return 1;
  737. }
  738. #endif
  739. /*
  740. *-------------------------------------------------------------------------
  741. * Output newlines or set them as continuations
  742. *
  743. * Input: -1 - Don't count this one, but update local position (see pp_dqs)
  744. * 0 - Line-continuation seen and cache output
  745. * 1 - Newline seen and flush output
  746. *-------------------------------------------------------------------------
  747. */
  748. static void newline(int dowrite)
  749. {
  750. pp_status.line_number++;
  751. pp_status.char_number = 1;
  752. if(dowrite == -1)
  753. return;
  754. ncontinuations++;
  755. if(dowrite)
  756. {
  757. for(;ncontinuations; ncontinuations--)
  758. put_buffer("\n", 1);
  759. }
  760. }
  761. /*
  762. *-------------------------------------------------------------------------
  763. * Make a number out of an any-base and suffixed string
  764. *
  765. * Possible number extensions:
  766. * - "" int
  767. * - "L" long int
  768. * - "LL" long long int
  769. * - "U" unsigned int
  770. * - "UL" unsigned long int
  771. * - "ULL" unsigned long long int
  772. * - "LU" unsigned long int
  773. * - "LLU" unsigned long long int
  774. * - "LUL" invalid
  775. *
  776. * FIXME:
  777. * The sizes of resulting 'int' and 'long' are compiler specific.
  778. *
  779. *-------------------------------------------------------------------------
  780. */
  781. static int make_number(int radix, YYSTYPE *val, const char *str, int len)
  782. {
  783. int is_l = 0;
  784. int is_ll = 0;
  785. int is_u = 0;
  786. char ext[4];
  787. long l;
  788. ext[3] = '\0';
  789. ext[2] = toupper(str[len-1]);
  790. ext[1] = len > 1 ? toupper(str[len-2]) : ' ';
  791. ext[0] = len > 2 ? toupper(str[len-3]) : ' ';
  792. if(!strcmp(ext, "LUL"))
  793. {
  794. ppy_error("Invalid constant suffix");
  795. return 0;
  796. }
  797. else if(!strcmp(ext, "LLU") || !strcmp(ext, "ULL"))
  798. {
  799. is_ll++;
  800. is_u++;
  801. }
  802. else if(!strcmp(ext+1, "LU") || !strcmp(ext+1, "UL"))
  803. {
  804. is_l++;
  805. is_u++;
  806. }
  807. else if(!strcmp(ext+1, "LL"))
  808. {
  809. is_ll++;
  810. }
  811. else if(!strcmp(ext+2, "L"))
  812. {
  813. is_l++;
  814. }
  815. else if(!strcmp(ext+2, "U"))
  816. {
  817. is_u++;
  818. }
  819. if(is_u && is_ll)
  820. {
  821. errno = 0;
  822. val->ull = strtoull(str, NULL, radix);
  823. if (val->ull == ULLONG_MAX && errno == ERANGE)
  824. ppy_error("integer constant %s is too large\n", str);
  825. return tULONGLONG;
  826. }
  827. else if(!is_u && is_ll)
  828. {
  829. errno = 0;
  830. val->sll = strtoll(str, NULL, radix);
  831. if ((val->sll == LLONG_MIN || val->sll == LLONG_MAX) && errno == ERANGE)
  832. ppy_error("integer constant %s is too large\n", str);
  833. return tSLONGLONG;
  834. }
  835. else if(is_u && is_l)
  836. {
  837. errno = 0;
  838. val->ulong = strtoul(str, NULL, radix);
  839. if (val->ulong == ULONG_MAX && errno == ERANGE)
  840. ppy_error("integer constant %s is too large\n", str);
  841. return tULONG;
  842. }
  843. else if(!is_u && is_l)
  844. {
  845. errno = 0;
  846. val->slong = strtol(str, NULL, radix);
  847. if ((val->slong == LONG_MIN || val->slong == LONG_MAX) && errno == ERANGE)
  848. ppy_error("integer constant %s is too large\n", str);
  849. return tSLONG;
  850. }
  851. else if(is_u && !is_l)
  852. {
  853. unsigned long ul;
  854. errno = 0;
  855. ul = strtoul(str, NULL, radix);
  856. if ((ul == ULONG_MAX && errno == ERANGE) || (ul > UINT_MAX))
  857. ppy_error("integer constant %s is too large\n", str);
  858. val->uint = (unsigned int)ul;
  859. return tUINT;
  860. }
  861. /* Else it must be an int... */
  862. errno = 0;
  863. l = strtol(str, NULL, radix);
  864. if (((l == LONG_MIN || l == LONG_MAX) && errno == ERANGE) ||
  865. (l > INT_MAX) || (l < INT_MIN))
  866. ppy_error("integer constant %s is too large\n", str);
  867. val->sint = (int)l;
  868. return tSINT;
  869. }
  870. /*
  871. *-------------------------------------------------------------------------
  872. * Macro and define expansion support
  873. *
  874. * FIXME: Variable macro arguments.
  875. *-------------------------------------------------------------------------
  876. */
  877. static void expand_special(pp_entry_t *ppp)
  878. {
  879. static char *buf = NULL;
  880. char *new_buf;
  881. assert(ppp->type == def_special);
  882. if(!strcmp(ppp->ident, "__LINE__"))
  883. {
  884. new_buf = pp_xrealloc(buf, 32);
  885. if(!new_buf)
  886. return;
  887. buf = new_buf;
  888. sprintf(buf, "%d", pp_status.line_number);
  889. }
  890. else if(!strcmp(ppp->ident, "__FILE__"))
  891. {
  892. new_buf = pp_xrealloc(buf, strlen(pp_status.input) + 3);
  893. if(!new_buf)
  894. return;
  895. buf = new_buf;
  896. sprintf(buf, "\"%s\"", pp_status.input);
  897. }
  898. else
  899. pp_internal_error(__FILE__, __LINE__, "Special macro '%s' not found...\n", ppp->ident);
  900. if(pp_flex_debug)
  901. fprintf(stderr, "expand_special(%d): %s:%d: '%s' -> '%s'\n",
  902. macexpstackidx,
  903. pp_status.input,
  904. pp_status.line_number,
  905. ppp->ident,
  906. buf ? buf : "");
  907. if(buf && buf[0])
  908. {
  909. push_buffer(ppp, NULL, NULL, 0);
  910. yy_scan_string(buf);
  911. }
  912. }
  913. static void expand_define(pp_entry_t *ppp)
  914. {
  915. assert(ppp->type == def_define);
  916. if(pp_flex_debug)
  917. fprintf(stderr, "expand_define(%d): %s:%d: '%s' -> '%s'\n",
  918. macexpstackidx,
  919. pp_status.input,
  920. pp_status.line_number,
  921. ppp->ident,
  922. ppp->subst.text);
  923. if(ppp->subst.text && ppp->subst.text[0])
  924. {
  925. push_buffer(ppp, NULL, NULL, 0);
  926. yy_scan_string(ppp->subst.text);
  927. }
  928. }
  929. static int curdef_idx = 0;
  930. static int curdef_alloc = 0;
  931. static char *curdef_text = NULL;
  932. static void add_text(const char *str, int len)
  933. {
  934. int new_alloc;
  935. char *new_text;
  936. if(len == 0)
  937. return;
  938. if(curdef_idx >= curdef_alloc || curdef_alloc - curdef_idx < len)
  939. {
  940. new_alloc = curdef_alloc + ((len + ALLOCBLOCKSIZE-1) & ~(ALLOCBLOCKSIZE-1));
  941. new_text = pp_xrealloc(curdef_text, new_alloc * sizeof(curdef_text[0]));
  942. if(!new_text)
  943. return;
  944. curdef_text = new_text;
  945. curdef_alloc = new_alloc;
  946. if(curdef_alloc > 65536)
  947. ppy_warning("Reallocating macro-expansion buffer larger than 64kB");
  948. }
  949. memcpy(&curdef_text[curdef_idx], str, len);
  950. curdef_idx += len;
  951. }
  952. static mtext_t *add_expand_text(mtext_t *mtp, macexpstackentry_t *mep, int *nnl)
  953. {
  954. char *cptr;
  955. char *exp;
  956. int tag;
  957. int n;
  958. if(mtp == NULL)
  959. return NULL;
  960. switch(mtp->type)
  961. {
  962. case exp_text:
  963. if(pp_flex_debug)
  964. fprintf(stderr, "add_expand_text: exp_text: '%s'\n", mtp->subst.text);
  965. add_text(mtp->subst.text, strlen(mtp->subst.text));
  966. break;
  967. case exp_stringize:
  968. if(pp_flex_debug)
  969. fprintf(stderr, "add_expand_text: exp_stringize(%d): '%s'\n",
  970. mtp->subst.argidx,
  971. mep->args[mtp->subst.argidx]);
  972. cptr = mep->args[mtp->subst.argidx];
  973. add_text("\"", 1);
  974. while(*cptr)
  975. {
  976. if(*cptr == '"' || *cptr == '\\')
  977. add_text("\\", 1);
  978. add_text(cptr, 1);
  979. cptr++;
  980. }
  981. add_text("\"", 1);
  982. break;
  983. case exp_concat:
  984. if(pp_flex_debug)
  985. fprintf(stderr, "add_expand_text: exp_concat\n");
  986. /* Remove trailing whitespace from current expansion text */
  987. while(curdef_idx)
  988. {
  989. if(isspace(curdef_text[curdef_idx-1] & 0xff))
  990. curdef_idx--;
  991. else
  992. break;
  993. }
  994. /* tag current position and recursively expand the next part */
  995. tag = curdef_idx;
  996. mtp = add_expand_text(mtp->next, mep, nnl);
  997. /* Now get rid of the leading space of the expansion */
  998. cptr = &curdef_text[tag];
  999. n = curdef_idx - tag;
  1000. while(n)
  1001. {
  1002. if(isspace(*cptr & 0xff))
  1003. {
  1004. cptr++;
  1005. n--;
  1006. }
  1007. else
  1008. break;
  1009. }
  1010. if(cptr != &curdef_text[tag])
  1011. {
  1012. memmove(&curdef_text[tag], cptr, n);
  1013. curdef_idx -= (curdef_idx - tag) - n;
  1014. }
  1015. break;
  1016. case exp_subst:
  1017. if((mtp->next && mtp->next->type == exp_concat) || (mtp->prev && mtp->prev->type == exp_concat))
  1018. exp = mep->args[mtp->subst.argidx];
  1019. else
  1020. exp = mep->ppargs[mtp->subst.argidx];
  1021. if(exp)
  1022. {
  1023. add_text(exp, strlen(exp));
  1024. *nnl -= mep->nnls[mtp->subst.argidx];
  1025. cptr = strchr(exp, '\n');
  1026. while(cptr)
  1027. {
  1028. *cptr = ' ';
  1029. cptr = strchr(cptr+1, '\n');
  1030. }
  1031. mep->nnls[mtp->subst.argidx] = 0;
  1032. }
  1033. if(pp_flex_debug)
  1034. fprintf(stderr, "add_expand_text: exp_subst(%d): '%s'\n", mtp->subst.argidx, exp);
  1035. break;
  1036. default:
  1037. pp_internal_error(__FILE__, __LINE__, "Invalid expansion type (%d) in macro expansion\n", mtp->type);
  1038. }
  1039. return mtp;
  1040. }
  1041. static void expand_macro(macexpstackentry_t *mep)
  1042. {
  1043. mtext_t *mtp;
  1044. int n, k;
  1045. char *cptr;
  1046. int nnl = 0;
  1047. pp_entry_t *ppp = mep->ppp;
  1048. int nargs = mep->nargs;
  1049. assert(ppp->type == def_macro);
  1050. assert(ppp->expanding == 0);
  1051. if((ppp->nargs >= 0 && nargs != ppp->nargs) || (ppp->nargs < 0 && nargs < -ppp->nargs))
  1052. {
  1053. ppy_error("Too %s macro arguments (%d)", nargs < abs(ppp->nargs) ? "few" : "many", nargs);
  1054. return;
  1055. }
  1056. for(n = 0; n < nargs; n++)
  1057. nnl += mep->nnls[n];
  1058. if(pp_flex_debug)
  1059. fprintf(stderr, "expand_macro(%d): %s:%d: '%s'(%d,%d) -> ...\n",
  1060. macexpstackidx,
  1061. pp_status.input,
  1062. pp_status.line_number,
  1063. ppp->ident,
  1064. mep->nargs,
  1065. nnl);
  1066. curdef_idx = 0;
  1067. for(mtp = ppp->subst.mtext; mtp; mtp = mtp->next)
  1068. {
  1069. if(!(mtp = add_expand_text(mtp, mep, &nnl)))
  1070. break;
  1071. }
  1072. for(n = 0; n < nnl; n++)
  1073. add_text("\n", 1);
  1074. /* To make sure there is room and termination (see below) */
  1075. add_text(" \0", 2);
  1076. /* Strip trailing whitespace from expansion */
  1077. for(k = curdef_idx, cptr = &curdef_text[curdef_idx-1]; k > 0; k--, cptr--)
  1078. {
  1079. if(!isspace(*cptr & 0xff))
  1080. break;
  1081. }
  1082. /*
  1083. * We must add *one* whitespace to make sure that there
  1084. * is a token-separation after the expansion.
  1085. */
  1086. *(++cptr) = ' ';
  1087. *(++cptr) = '\0';
  1088. k++;
  1089. /* Strip leading whitespace from expansion */
  1090. for(n = 0, cptr = curdef_text; n < k; n++, cptr++)
  1091. {
  1092. if(!isspace(*cptr & 0xff))
  1093. break;
  1094. }
  1095. if(k - n > 0)
  1096. {
  1097. if(pp_flex_debug)
  1098. fprintf(stderr, "expand_text: '%s'\n", curdef_text + n);
  1099. push_buffer(ppp, NULL, NULL, 0);
  1100. /*yy_scan_bytes(curdef_text + n, k - n);*/
  1101. yy_scan_string(curdef_text + n);
  1102. }
  1103. }
  1104. /*
  1105. *-------------------------------------------------------------------------
  1106. * String collection routines
  1107. *-------------------------------------------------------------------------
  1108. */
  1109. static void new_string(void)
  1110. {
  1111. strbuf_idx = 0;
  1112. str_startline = pp_status.line_number;
  1113. }
  1114. static void add_string(const char *str, int len)
  1115. {
  1116. int new_alloc;
  1117. char *new_buffer;
  1118. if(len == 0)
  1119. return;
  1120. if(strbuf_idx >= strbuf_alloc || strbuf_alloc - strbuf_idx < len)
  1121. {
  1122. new_alloc = strbuf_alloc + ((len + ALLOCBLOCKSIZE-1) & ~(ALLOCBLOCKSIZE-1));
  1123. new_buffer = pp_xrealloc(strbuffer, new_alloc * sizeof(strbuffer[0]));
  1124. if(!new_buffer)
  1125. return;
  1126. strbuffer = new_buffer;
  1127. strbuf_alloc = new_alloc;
  1128. if(strbuf_alloc > 65536)
  1129. ppy_warning("Reallocating string buffer larger than 64kB");
  1130. }
  1131. memcpy(&strbuffer[strbuf_idx], str, len);
  1132. strbuf_idx += len;
  1133. }
  1134. static char *get_string(void)
  1135. {
  1136. char *str = pp_xmalloc(strbuf_idx + 1);
  1137. memcpy(str, strbuffer, strbuf_idx);
  1138. str[strbuf_idx] = '\0';
  1139. return str;
  1140. }
  1141. static void put_string(void)
  1142. {
  1143. put_buffer(strbuffer, strbuf_idx);
  1144. }
  1145. static int string_start(void)
  1146. {
  1147. return str_startline;
  1148. }
  1149. /*
  1150. *-------------------------------------------------------------------------
  1151. * Buffer management
  1152. *-------------------------------------------------------------------------
  1153. */
  1154. static void push_buffer(pp_entry_t *ppp, char *filename, char *incname, int pop)
  1155. {
  1156. if(ppy_debug)
  1157. printf("push_buffer(%d): %p %p %p %d\n", bufferstackidx, ppp, filename, incname, pop);
  1158. if(bufferstackidx >= MAXBUFFERSTACK)
  1159. pp_internal_error(__FILE__, __LINE__, "Buffer stack overflow");
  1160. memset(&bufferstack[bufferstackidx], 0, sizeof(bufferstack[0]));
  1161. bufferstack[bufferstackidx].bufferstate = YY_CURRENT_BUFFER;
  1162. bufferstack[bufferstackidx].file = pp_status.file;
  1163. bufferstack[bufferstackidx].define = ppp;
  1164. bufferstack[bufferstackidx].line_number = pp_status.line_number;
  1165. bufferstack[bufferstackidx].char_number = pp_status.char_number;
  1166. bufferstack[bufferstackidx].if_depth = pp_get_if_depth();
  1167. bufferstack[bufferstackidx].should_pop = pop;
  1168. bufferstack[bufferstackidx].filename = pp_status.input;
  1169. bufferstack[bufferstackidx].ncontinuations = ncontinuations;
  1170. bufferstack[bufferstackidx].incl = pp_incl_state;
  1171. bufferstack[bufferstackidx].include_filename = incname;
  1172. if(ppp)
  1173. ppp->expanding = 1;
  1174. else if(filename)
  1175. {
  1176. /* These will track the ppy_error to the correct file and line */
  1177. pp_status.line_number = 1;
  1178. pp_status.char_number = 1;
  1179. pp_status.input = filename;
  1180. ncontinuations = 0;
  1181. }
  1182. else if(!pop)
  1183. pp_internal_error(__FILE__, __LINE__, "Pushing buffer without knowing where to go to");
  1184. bufferstackidx++;
  1185. }
  1186. static bufferstackentry_t *pop_buffer(void)
  1187. {
  1188. if(bufferstackidx < 0)
  1189. pp_internal_error(__FILE__, __LINE__, "Bufferstack underflow?");
  1190. if(bufferstackidx == 0)
  1191. return NULL;
  1192. bufferstackidx--;
  1193. if(bufferstack[bufferstackidx].define)
  1194. bufferstack[bufferstackidx].define->expanding = 0;
  1195. else
  1196. {
  1197. includelogicentry_t *iep = NULL;
  1198. if(!bufferstack[bufferstackidx].should_pop)
  1199. {
  1200. fclose(pp_status.file);
  1201. pp_writestring("# %d \"%s\" 2\n", bufferstack[bufferstackidx].line_number, bufferstack[bufferstackidx].filename);
  1202. /* We have EOF, check the include logic */
  1203. if(pp_incl_state.state == 2 && !pp_incl_state.seen_junk && pp_incl_state.ppp)
  1204. {
  1205. pp_entry_t *ppp = pplookup(pp_incl_state.ppp);
  1206. if(ppp)
  1207. {
  1208. iep = pp_xmalloc(sizeof(includelogicentry_t));
  1209. iep->ppp = ppp;
  1210. ppp->iep = iep;
  1211. iep->filename = bufferstack[bufferstackidx].include_filename;
  1212. list_add_head( &pp_includelogiclist, &iep->entry );
  1213. if(pp_status.debug)
  1214. fprintf(stderr, "pop_buffer: %s:%d: includelogic added, include_ppp='%s', file='%s'\n",
  1215. bufferstack[bufferstackidx].filename, bufferstack[bufferstackidx].line_number, pp_incl_state.ppp, iep->filename);
  1216. }
  1217. }
  1218. free(pp_incl_state.ppp);
  1219. pp_incl_state = bufferstack[bufferstackidx].incl;
  1220. }
  1221. if (bufferstack[bufferstackidx].include_filename)
  1222. {
  1223. free(pp_status.input);
  1224. pp_status.input = bufferstack[bufferstackidx].filename;
  1225. }
  1226. pp_status.line_number = bufferstack[bufferstackidx].line_number;
  1227. pp_status.char_number = bufferstack[bufferstackidx].char_number;
  1228. ncontinuations = bufferstack[bufferstackidx].ncontinuations;
  1229. if (!iep)
  1230. free(bufferstack[bufferstackidx].include_filename);
  1231. }
  1232. if(ppy_debug)
  1233. printf("pop_buffer(%d): %p %p (%d, %d, %d) %p %d\n",
  1234. bufferstackidx,
  1235. bufferstack[bufferstackidx].bufferstate,
  1236. bufferstack[bufferstackidx].define,
  1237. bufferstack[bufferstackidx].line_number,
  1238. bufferstack[bufferstackidx].char_number,
  1239. bufferstack[bufferstackidx].if_depth,
  1240. bufferstack[bufferstackidx].filename,
  1241. bufferstack[bufferstackidx].should_pop);
  1242. pp_status.file = bufferstack[bufferstackidx].file;
  1243. ppy__switch_to_buffer(bufferstack[bufferstackidx].bufferstate);
  1244. if(bufferstack[bufferstackidx].should_pop)
  1245. {
  1246. if(yy_current_state() == pp_macexp)
  1247. macro_add_expansion();
  1248. else
  1249. pp_internal_error(__FILE__, __LINE__, "Pop buffer and state without macro expansion state");
  1250. yy_pop_state();
  1251. }
  1252. return &bufferstack[bufferstackidx];
  1253. }
  1254. /*
  1255. *-------------------------------------------------------------------------
  1256. * Macro nestng support
  1257. *-------------------------------------------------------------------------
  1258. */
  1259. static void push_macro(pp_entry_t *ppp)
  1260. {
  1261. if(macexpstackidx >= MAXMACEXPSTACK)
  1262. {
  1263. ppy_error("Too many nested macros");
  1264. return;
  1265. }
  1266. macexpstack[macexpstackidx] = pp_xmalloc(sizeof(macexpstack[0][0]));
  1267. memset( macexpstack[macexpstackidx], 0, sizeof(macexpstack[0][0]));
  1268. macexpstack[macexpstackidx]->ppp = ppp;
  1269. macexpstackidx++;
  1270. }
  1271. static macexpstackentry_t *top_macro(void)
  1272. {
  1273. return macexpstackidx > 0 ? macexpstack[macexpstackidx-1] : NULL;
  1274. }
  1275. static macexpstackentry_t *pop_macro(void)
  1276. {
  1277. if(macexpstackidx <= 0)
  1278. pp_internal_error(__FILE__, __LINE__, "Macro expansion stack underflow\n");
  1279. return macexpstack[--macexpstackidx];
  1280. }
  1281. static void free_macro(macexpstackentry_t *mep)
  1282. {
  1283. int i;
  1284. for(i = 0; i < mep->nargs; i++)
  1285. free(mep->args[i]);
  1286. free(mep->args);
  1287. free(mep->nnls);
  1288. free(mep->curarg);
  1289. free(mep);
  1290. }
  1291. static void add_text_to_macro(const char *text, int len)
  1292. {
  1293. macexpstackentry_t *mep = top_macro();
  1294. assert(mep->ppp->expanding == 0);
  1295. if(mep->curargalloc - mep->curargsize <= len+1) /* +1 for '\0' */
  1296. {
  1297. char *new_curarg;
  1298. int new_alloc = mep->curargalloc + ((ALLOCBLOCKSIZE > len+1) ? ALLOCBLOCKSIZE : len+1);
  1299. new_curarg = pp_xrealloc(mep->curarg, new_alloc * sizeof(mep->curarg[0]));
  1300. if(!new_curarg)
  1301. return;
  1302. mep->curarg = new_curarg;
  1303. mep->curargalloc = new_alloc;
  1304. }
  1305. memcpy(mep->curarg + mep->curargsize, text, len);
  1306. mep->curargsize += len;
  1307. mep->curarg[mep->curargsize] = '\0';
  1308. }
  1309. static void macro_add_arg(int last)
  1310. {
  1311. int nnl = 0;
  1312. char *cptr;
  1313. macexpstackentry_t *mep = top_macro();
  1314. assert(mep->ppp->expanding == 0);
  1315. mep->args = pp_xrealloc(mep->args, (mep->nargs+1) * sizeof(mep->args[0]));
  1316. mep->ppargs = pp_xrealloc(mep->ppargs, (mep->nargs+1) * sizeof(mep->ppargs[0]));
  1317. mep->nnls = pp_xrealloc(mep->nnls, (mep->nargs+1) * sizeof(mep->nnls[0]));
  1318. mep->args[mep->nargs] = pp_xstrdup(mep->curarg ? mep->curarg : "");
  1319. cptr = mep->args[mep->nargs]-1;
  1320. while((cptr = strchr(cptr+1, '\n')))
  1321. {
  1322. nnl++;
  1323. }
  1324. mep->nnls[mep->nargs] = nnl;
  1325. mep->nargs++;
  1326. free(mep->curarg);
  1327. mep->curargalloc = mep->curargsize = 0;
  1328. mep->curarg = NULL;
  1329. if(pp_flex_debug)
  1330. fprintf(stderr, "macro_add_arg: %s:%d: %d -> '%s'\n",
  1331. pp_status.input,
  1332. pp_status.line_number,
  1333. mep->nargs-1,
  1334. mep->args[mep->nargs-1]);
  1335. /* Each macro argument must be expanded to cope with stingize */
  1336. if(last || mep->args[mep->nargs-1][0])
  1337. {
  1338. yy_push_state(pp_macexp);
  1339. push_buffer(NULL, NULL, NULL, last ? 2 : 1);
  1340. yy_scan_string(mep->args[mep->nargs-1]);
  1341. /*mep->bufferstackidx = bufferstackidx; But not nested! */
  1342. }
  1343. }
  1344. static void macro_add_expansion(void)
  1345. {
  1346. macexpstackentry_t *mep = top_macro();
  1347. assert(mep->ppp->expanding == 0);
  1348. mep->ppargs[mep->nargs-1] = pp_xstrdup(mep->curarg ? mep->curarg : "");
  1349. free(mep->curarg);
  1350. mep->curargalloc = mep->curargsize = 0;
  1351. mep->curarg = NULL;
  1352. if(pp_flex_debug)
  1353. fprintf(stderr, "macro_add_expansion: %s:%d: %d -> '%s'\n",
  1354. pp_status.input,
  1355. pp_status.line_number,
  1356. mep->nargs-1,
  1357. mep->ppargs[mep->nargs-1] ? mep->ppargs[mep->nargs-1] : "");
  1358. }
  1359. /*
  1360. *-------------------------------------------------------------------------
  1361. * Output management
  1362. *-------------------------------------------------------------------------
  1363. */
  1364. static void put_buffer(const char *s, int len)
  1365. {
  1366. if(top_macro())
  1367. add_text_to_macro(s, len);
  1368. else
  1369. fwrite(s, 1, len, ppy_out);
  1370. }
  1371. /*
  1372. *-------------------------------------------------------------------------
  1373. * Include management
  1374. *-------------------------------------------------------------------------
  1375. */
  1376. void pp_do_include(char *fname, int type)
  1377. {
  1378. char *newpath;
  1379. int n;
  1380. includelogicentry_t *iep;
  1381. void *fp;
  1382. if(!fname)
  1383. return;
  1384. LIST_FOR_EACH_ENTRY( iep, &pp_includelogiclist, includelogicentry_t, entry )
  1385. {
  1386. if(!strcmp(iep->filename, fname))
  1387. {
  1388. /*
  1389. * We are done. The file was included before.
  1390. * If the define was deleted, then this entry would have
  1391. * been deleted too.
  1392. */
  1393. free(fname);
  1394. return;
  1395. }
  1396. }
  1397. n = strlen(fname);
  1398. if(n <= 2)
  1399. {
  1400. ppy_error("Empty include filename");
  1401. free(fname);
  1402. return;
  1403. }
  1404. /* Undo the effect of the quotation */
  1405. fname[n-1] = '\0';
  1406. if((fp = pp_open_include(fname+1, type, pp_status.input, &newpath)) == NULL)
  1407. {
  1408. ppy_error("Unable to open include file %s", fname+1);
  1409. free(fname);
  1410. return;
  1411. }
  1412. fname[n-1] = *fname; /* Redo the quotes */
  1413. push_buffer(NULL, newpath, fname, 0);
  1414. pp_incl_state.seen_junk = 0;
  1415. pp_incl_state.state = 0;
  1416. pp_incl_state.ppp = NULL;
  1417. if(pp_status.debug)
  1418. fprintf(stderr, "pp_do_include: %s:%d: include_state=%d, include_ifdepth=%d\n",
  1419. pp_status.input, pp_status.line_number, pp_incl_state.state, pp_incl_state.ifdepth);
  1420. pp_status.file = fp;
  1421. ppy__switch_to_buffer(ppy__create_buffer(NULL, YY_BUF_SIZE));
  1422. pp_writestring("# 1 \"%s\" 1%s\n", newpath, type ? "" : " 3");
  1423. }
  1424. /*
  1425. *-------------------------------------------------------------------------
  1426. * Push/pop preprocessor ignore state when processing conditionals
  1427. * which are false.
  1428. *-------------------------------------------------------------------------
  1429. */
  1430. void pp_push_ignore_state(void)
  1431. {
  1432. yy_push_state(pp_ignore);
  1433. }
  1434. void pp_pop_ignore_state(void)
  1435. {
  1436. yy_pop_state();
  1437. }