ppl.l 42 KB

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