ppl.l 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  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. void *filehandle; /* Handle to be used with wpp_callbacks->read */
  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. includelogicentry_t *pp_includelogiclist = NULL;
  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. if(buffer == NULL)
  297. return;
  298. buffercapacity = BUFFERINITIALCAPACITY;
  299. }
  300. va_start(valist, format);
  301. len = vsnprintf(buffer, buffercapacity,
  302. format, valist);
  303. va_end(valist);
  304. /* If the string is longer than buffersize, vsnprintf returns
  305. * the string length with glibc >= 2.1, -1 with glibc < 2.1 */
  306. while(len > buffercapacity || len < 0)
  307. {
  308. do
  309. {
  310. buffercapacity *= 2;
  311. } while(len > buffercapacity);
  312. new_buffer = pp_xrealloc(buffer, buffercapacity);
  313. if(new_buffer == NULL)
  314. {
  315. va_end(valist);
  316. return;
  317. }
  318. buffer = new_buffer;
  319. va_start(valist, format);
  320. len = vsnprintf(buffer, buffercapacity,
  321. format, valist);
  322. va_end(valist);
  323. }
  324. fwrite(buffer, 1, len, ppy_out);
  325. }
  326. %}
  327. /*
  328. **************************************************************************
  329. * The scanner starts here
  330. **************************************************************************
  331. */
  332. %%
  333. /*
  334. * Catch line-continuations.
  335. * Note: Gcc keeps the line-continuations in, for example, strings
  336. * intact. However, I prefer to remove them all so that the next
  337. * scanner will not need to reduce the continuation state.
  338. *
  339. * <*>\\\n newline(0);
  340. */
  341. /*
  342. * Detect the leading # of a preprocessor directive.
  343. */
  344. <INITIAL,pp_ignore>^{ws}*# pp_incl_state.seen_junk++; yy_push_state(pp_pp);
  345. /*
  346. * Scan for the preprocessor directives
  347. */
  348. <pp_pp>{ws}*include{ws}* if(yy_top_state() != pp_ignore) {yy_pp_state(pp_inc); return tINCLUDE;} else {yy_pp_state(pp_eol);}
  349. <pp_pp>{ws}*define{ws}* yy_pp_state(yy_current_state() != pp_ignore ? pp_def : pp_eol);
  350. <pp_pp>{ws}*error{ws}* yy_pp_state(pp_eol); if(yy_top_state() != pp_ignore) return tERROR;
  351. <pp_pp>{ws}*warning{ws}* yy_pp_state(pp_eol); if(yy_top_state() != pp_ignore) return tWARNING;
  352. <pp_pp>{ws}*pragma{ws}* yy_pp_state(pp_eol); if(yy_top_state() != pp_ignore) return tPRAGMA;
  353. <pp_pp>{ws}*ident{ws}* yy_pp_state(pp_eol); if(yy_top_state() != pp_ignore) return tPPIDENT;
  354. <pp_pp>{ws}*undef{ws}* if(yy_top_state() != pp_ignore) {yy_pp_state(pp_ifd); return tUNDEF;} else {yy_pp_state(pp_eol);}
  355. <pp_pp>{ws}*ifdef{ws}* yy_pp_state(pp_ifd); return tIFDEF;
  356. <pp_pp>{ws}*ifndef{ws}* pp_incl_state.seen_junk--; yy_pp_state(pp_ifd); return tIFNDEF;
  357. <pp_pp>{ws}*if{ws}* if(yy_top_state() != pp_ignore) {yy_pp_state(pp_if);} else {yy_pp_state(pp_ifignored);} return tIF;
  358. <pp_pp>{ws}*elif{ws}* yy_pp_state(pp_if); return tELIF;
  359. <pp_pp>{ws}*else{ws}* yy_pp_state(pp_endif); return tELSE;
  360. <pp_pp>{ws}*endif{ws}* yy_pp_state(pp_endif); return tENDIF;
  361. <pp_pp>{ws}*line{ws}* if(yy_top_state() != pp_ignore) {yy_pp_state(pp_line); return tLINE;} else {yy_pp_state(pp_eol);}
  362. <pp_pp>{ws}+ if(yy_top_state() != pp_ignore) {yy_pp_state(pp_line); return tGCCLINE;} else {yy_pp_state(pp_eol);}
  363. <pp_pp>{ws}*[a-z]+ ppy_error("Invalid preprocessor token '%s'", ppy_text);
  364. <pp_pp>\r?\n newline(1); yy_pop_state(); return tNL; /* This could be the null-token */
  365. <pp_pp>\\\r?\n newline(0);
  366. <pp_pp>\\\r? ppy_error("Preprocessor junk '%s'", ppy_text);
  367. <pp_pp>. return *ppy_text;
  368. /*
  369. * Handle #include and #line
  370. */
  371. <pp_line>[0-9]+ return make_number(10, &ppy_lval, ppy_text, ppy_leng);
  372. <pp_inc>\< new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_iqs);
  373. <pp_inc,pp_line>\" new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_dqs);
  374. <pp_inc,pp_line>{ws}+ ;
  375. <pp_inc,pp_line>\n newline(1); yy_pop_state(); return tNL;
  376. <pp_inc,pp_line>\\\r?\n newline(0);
  377. <pp_inc,pp_line>(\\\r?)|(.) ppy_error(yy_current_state() == pp_inc ? "Trailing junk in #include" : "Trailing junk in #line");
  378. /*
  379. * Ignore all input when a false clause is parsed
  380. */
  381. <pp_ignore>[^#/\\\n]+ ;
  382. <pp_ignore>\n newline(1);
  383. <pp_ignore>\\\r?\n newline(0);
  384. <pp_ignore>(\\\r?)|(.) ;
  385. /*
  386. * Handle #if and #elif.
  387. * These require conditionals to be evaluated, but we do not
  388. * want to jam the scanner normally when we see these tokens.
  389. * Note: tIDENT is handled below.
  390. */
  391. <pp_if>0[0-7]*{ul}? return make_number(8, &ppy_lval, ppy_text, ppy_leng);
  392. <pp_if>0[0-7]*[8-9]+{ul}? ppy_error("Invalid octal digit");
  393. <pp_if>[1-9][0-9]*{ul}? return make_number(10, &ppy_lval, ppy_text, ppy_leng);
  394. <pp_if>0[xX][0-9a-fA-F]+{ul}? return make_number(16, &ppy_lval, ppy_text, ppy_leng);
  395. <pp_if>0[xX] ppy_error("Invalid hex number");
  396. <pp_if>defined yy_push_state(pp_defined); return tDEFINED;
  397. <pp_if>"<<" return tLSHIFT;
  398. <pp_if>">>" return tRSHIFT;
  399. <pp_if>"&&" return tLOGAND;
  400. <pp_if>"||" return tLOGOR;
  401. <pp_if>"==" return tEQ;
  402. <pp_if>"!=" return tNE;
  403. <pp_if>"<=" return tLTE;
  404. <pp_if>">=" return tGTE;
  405. <pp_if>\n newline(1); yy_pop_state(); return tNL;
  406. <pp_if>\\\r?\n newline(0);
  407. <pp_if>\\\r? ppy_error("Junk in conditional expression");
  408. <pp_if>{ws}+ ;
  409. <pp_if>\' new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_sqs);
  410. <pp_if>\" ppy_error("String constants not allowed in conditionals");
  411. <pp_if>. return *ppy_text;
  412. <pp_ifignored>[^\n]+ ppy_lval.sint = 0; return tSINT;
  413. <pp_ifignored>\n newline(1); yy_pop_state(); return tNL;
  414. /*
  415. * Handle #ifdef, #ifndef and #undef
  416. * to get only an untranslated/unexpanded identifier
  417. */
  418. <pp_ifd>{cident} ppy_lval.cptr = pp_xstrdup(ppy_text); return tIDENT;
  419. <pp_ifd>{ws}+ ;
  420. <pp_ifd>\n newline(1); yy_pop_state(); return tNL;
  421. <pp_ifd>\\\r?\n newline(0);
  422. <pp_ifd>(\\\r?)|(.) ppy_error("Identifier expected");
  423. /*
  424. * Handle #else and #endif.
  425. */
  426. <pp_endif>{ws}+ ;
  427. <pp_endif>\n newline(1); yy_pop_state(); return tNL;
  428. <pp_endif>\\\r?\n newline(0);
  429. <pp_endif>. ppy_error("Garbage after #else or #endif.");
  430. /*
  431. * Handle the special 'defined' keyword.
  432. * This is necessary to get the identifier prior to any
  433. * substitutions.
  434. */
  435. <pp_defined>{cident} yy_pop_state(); ppy_lval.cptr = pp_xstrdup(ppy_text); return tIDENT;
  436. <pp_defined>{ws}+ ;
  437. <pp_defined>(\()|(\)) return *ppy_text;
  438. <pp_defined>\\\r?\n newline(0);
  439. <pp_defined>(\\.)|(\n)|(.) ppy_error("Identifier expected");
  440. /*
  441. * Handle #error, #warning, #pragma and #ident.
  442. * Pass everything literally to the parser, which
  443. * will act appropriately.
  444. * Comments are stripped from the literal text.
  445. */
  446. <pp_eol>[^/\\\n]+ if(yy_top_state() != pp_ignore) { ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL; }
  447. <pp_eol>\/[^/\\\n*]* if(yy_top_state() != pp_ignore) { ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL; }
  448. <pp_eol>(\\\r?)|(\/[^/*]) if(yy_top_state() != pp_ignore) { ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL; }
  449. <pp_eol>\n newline(1); yy_pop_state(); if(yy_current_state() != pp_ignore) { return tNL; }
  450. <pp_eol>\\\r?\n newline(0);
  451. /*
  452. * Handle left side of #define
  453. */
  454. <pp_def>{cident}\( ppy_lval.cptr = pp_xstrdup(ppy_text); if(ppy_lval.cptr) ppy_lval.cptr[ppy_leng-1] = '\0'; yy_pp_state(pp_macro); return tMACRO;
  455. <pp_def>{cident} ppy_lval.cptr = pp_xstrdup(ppy_text); yy_pp_state(pp_define); return tDEFINE;
  456. <pp_def>{ws}+ ;
  457. <pp_def>\\\r?\n newline(0);
  458. <pp_def>(\\\r?)|(\n)|(.) perror("Identifier expected");
  459. /*
  460. * Scan the substitution of a define
  461. */
  462. <pp_define>[^'"/\\\n]+ ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL;
  463. <pp_define>(\\\r?)|(\/[^/*]) ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL;
  464. <pp_define>\\\r?\n{ws}+ newline(0); ppy_lval.cptr = pp_xstrdup(" "); return tLITERAL;
  465. <pp_define>\\\r?\n newline(0);
  466. <pp_define>\n newline(1); yy_pop_state(); return tNL;
  467. <pp_define>\' new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_sqs);
  468. <pp_define>\" new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_dqs);
  469. /*
  470. * Scan the definition macro arguments
  471. */
  472. <pp_macro>\){ws}* yy_pp_state(pp_mbody); return tMACROEND;
  473. <pp_macro>{ws}+ ;
  474. <pp_macro>{cident} ppy_lval.cptr = pp_xstrdup(ppy_text); return tIDENT;
  475. <pp_macro>, return ',';
  476. <pp_macro>"..." return tELIPSIS;
  477. <pp_macro>(\\\r?)|(\n)|(.)|(\.\.?) ppy_error("Argument identifier expected");
  478. <pp_macro>\\\r?\n newline(0);
  479. /*
  480. * Scan the substitution of a macro
  481. */
  482. <pp_mbody>[^a-zA-Z0-9'"#/\\\n]+ ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL;
  483. <pp_mbody>{cident} ppy_lval.cptr = pp_xstrdup(ppy_text); return tIDENT;
  484. <pp_mbody>\#\# return tCONCAT;
  485. <pp_mbody>\# return tSTRINGIZE;
  486. <pp_mbody>[0-9][a-zA-Z0-9]*[^a-zA-Z0-9'"#/\\\n]* ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL;
  487. <pp_mbody>(\\\r?)|(\/[^/*'"#\\\n]*) ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL;
  488. <pp_mbody>\\\r?\n{ws}+ newline(0); ppy_lval.cptr = pp_xstrdup(" "); return tLITERAL;
  489. <pp_mbody>\\\r?\n newline(0);
  490. <pp_mbody>\n newline(1); yy_pop_state(); return tNL;
  491. <pp_mbody>\' new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_sqs);
  492. <pp_mbody>\" new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_dqs);
  493. /*
  494. * Macro expansion text scanning.
  495. * This state is active just after the identifier is scanned
  496. * that triggers an expansion. We *must* delete the leading
  497. * whitespace before we can start scanning for arguments.
  498. *
  499. * If we do not see a '(' as next trailing token, then we have
  500. * a false alarm. We just continue with a nose-bleed...
  501. */
  502. <pp_macign>{ws}*/\( yy_pp_state(pp_macscan);
  503. <pp_macign>{ws}*\n {
  504. if(yy_top_state() != pp_macscan)
  505. newline(0);
  506. }
  507. <pp_macign>{ws}*\\\r?\n newline(0);
  508. <pp_macign>{ws}+|{ws}*\\\r?|. {
  509. macexpstackentry_t *mac = pop_macro();
  510. yy_pop_state();
  511. put_buffer(mac->ppp->ident, strlen(mac->ppp->ident));
  512. put_buffer(ppy_text, ppy_leng);
  513. free_macro(mac);
  514. }
  515. /*
  516. * Macro expansion argument text scanning.
  517. * This state is active when a macro's arguments are being read for expansion.
  518. */
  519. <pp_macscan>\( {
  520. if(++MACROPARENTHESES() > 1)
  521. add_text_to_macro(ppy_text, ppy_leng);
  522. }
  523. <pp_macscan>\) {
  524. if(--MACROPARENTHESES() == 0)
  525. {
  526. yy_pop_state();
  527. macro_add_arg(1);
  528. }
  529. else
  530. add_text_to_macro(ppy_text, ppy_leng);
  531. }
  532. <pp_macscan>, {
  533. if(MACROPARENTHESES() > 1)
  534. add_text_to_macro(ppy_text, ppy_leng);
  535. else
  536. macro_add_arg(0);
  537. }
  538. <pp_macscan>\" new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_dqs);
  539. <pp_macscan>\' new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_sqs);
  540. <pp_macscan>"/*" yy_push_state(pp_comment); add_text_to_macro(" ", 1);
  541. <pp_macscan>\n pp_status.line_number++; pp_status.char_number = 1; add_text_to_macro(ppy_text, ppy_leng);
  542. <pp_macscan>([^/(),\\\n"']+)|(\/[^/*(),\\\n'"]*)|(\\\r?)|(.) add_text_to_macro(ppy_text, ppy_leng);
  543. <pp_macscan>\\\r?\n newline(0);
  544. /*
  545. * Comment handling (almost all start-conditions)
  546. */
  547. <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);
  548. <pp_comment>[^*\n]*|"*"+[^*/\n]* ;
  549. <pp_comment>\n newline(0);
  550. <pp_comment>"*"+"/" yy_pop_state();
  551. /*
  552. * Remove C++ style comment (almost all start-conditions)
  553. */
  554. <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]* {
  555. if(ppy_text[ppy_leng-1] == '\\')
  556. ppy_warning("C++ style comment ends with an escaped newline (escape ignored)");
  557. }
  558. /*
  559. * Single, double and <> quoted constants
  560. */
  561. <INITIAL,pp_macexp>\" pp_incl_state.seen_junk++; new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_dqs);
  562. <INITIAL,pp_macexp>\' pp_incl_state.seen_junk++; new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_sqs);
  563. <pp_dqs>[^"\\\n]+ add_string(ppy_text, ppy_leng);
  564. <pp_dqs>\" {
  565. add_string(ppy_text, ppy_leng);
  566. yy_pop_state();
  567. switch(yy_current_state())
  568. {
  569. case pp_pp:
  570. case pp_define:
  571. case pp_mbody:
  572. case pp_inc:
  573. case RCINCL:
  574. if (yy_current_state()==RCINCL) yy_pop_state();
  575. ppy_lval.cptr = get_string();
  576. return tDQSTRING;
  577. case pp_line:
  578. ppy_lval.cptr = get_string();
  579. return tDQSTRING;
  580. default:
  581. put_string();
  582. }
  583. }
  584. <pp_sqs>[^'\\\n]+ add_string(ppy_text, ppy_leng);
  585. <pp_sqs>\' {
  586. add_string(ppy_text, ppy_leng);
  587. yy_pop_state();
  588. switch(yy_current_state())
  589. {
  590. case pp_if:
  591. case pp_define:
  592. case pp_mbody:
  593. ppy_lval.cptr = get_string();
  594. return tSQSTRING;
  595. default:
  596. put_string();
  597. }
  598. }
  599. <pp_iqs>[^\>\\\n]+ add_string(ppy_text, ppy_leng);
  600. <pp_iqs>\> {
  601. add_string(ppy_text, ppy_leng);
  602. yy_pop_state();
  603. ppy_lval.cptr = get_string();
  604. return tIQSTRING;
  605. }
  606. <pp_dqs>\\\r?\n {
  607. /*
  608. * This is tricky; we need to remove the line-continuation
  609. * from preprocessor strings, but OTOH retain them in all
  610. * other strings. This is because the resource grammar is
  611. * even more braindead than initially analysed and line-
  612. * continuations in strings introduce, sigh, newlines in
  613. * the output. There goes the concept of non-breaking, non-
  614. * spacing whitespace.
  615. */
  616. switch(yy_top_state())
  617. {
  618. case pp_pp:
  619. case pp_define:
  620. case pp_mbody:
  621. case pp_inc:
  622. case pp_line:
  623. newline(0);
  624. break;
  625. default:
  626. add_string(ppy_text, ppy_leng);
  627. newline(-1);
  628. }
  629. }
  630. <pp_iqs,pp_dqs,pp_sqs>\\. add_string(ppy_text, ppy_leng);
  631. <pp_iqs,pp_dqs,pp_sqs>\n {
  632. newline(1);
  633. add_string(ppy_text, ppy_leng);
  634. ppy_warning("Newline in string constant encountered (started line %d)", string_start());
  635. }
  636. /*
  637. * Identifier scanning
  638. */
  639. <INITIAL,pp_if,pp_inc,pp_macexp>{cident} {
  640. pp_entry_t *ppp;
  641. pp_incl_state.seen_junk++;
  642. if(!(ppp = pplookup(ppy_text)))
  643. {
  644. if(yy_current_state() == pp_inc)
  645. ppy_error("Expected include filename");
  646. else if(yy_current_state() == pp_if)
  647. {
  648. ppy_lval.cptr = pp_xstrdup(ppy_text);
  649. return tIDENT;
  650. }
  651. else {
  652. if((yy_current_state()==INITIAL) && (strcasecmp(ppy_text,"RCINCLUDE")==0)){
  653. yy_push_state(RCINCL);
  654. return tRCINCLUDE;
  655. }
  656. else put_buffer(ppy_text, ppy_leng);
  657. }
  658. }
  659. else if(!ppp->expanding)
  660. {
  661. switch(ppp->type)
  662. {
  663. case def_special:
  664. expand_special(ppp);
  665. break;
  666. case def_define:
  667. expand_define(ppp);
  668. break;
  669. case def_macro:
  670. yy_push_state(pp_macign);
  671. push_macro(ppp);
  672. break;
  673. default:
  674. pp_internal_error(__FILE__, __LINE__, "Invalid define type %d\n", ppp->type);
  675. }
  676. }
  677. else put_buffer(ppy_text, ppy_leng);
  678. }
  679. /*
  680. * Everything else that needs to be passed and
  681. * newline and continuation handling
  682. */
  683. <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);
  684. <INITIAL,pp_macexp>{ws}+ put_buffer(ppy_text, ppy_leng);
  685. <INITIAL>\n newline(1);
  686. <INITIAL>\\\r?\n newline(0);
  687. <INITIAL>\\\r? pp_incl_state.seen_junk++; put_buffer(ppy_text, ppy_leng);
  688. /*
  689. * Special catcher for macro argmument expansion to prevent
  690. * newlines to propagate to the output or admin.
  691. */
  692. <pp_macexp>(\n)|(.)|(\\\r?(\n|.)) put_buffer(ppy_text, ppy_leng);
  693. <RCINCL>[A-Za-z0-9_\.\\/]+ {
  694. ppy_lval.cptr=pp_xstrdup(ppy_text);
  695. yy_pop_state();
  696. return tRCINCLUDEPATH;
  697. }
  698. <RCINCL>{ws}+ ;
  699. <RCINCL>\" {
  700. new_string(); add_string(ppy_text,ppy_leng);yy_push_state(pp_dqs);
  701. }
  702. /*
  703. * This is a 'catch-all' rule to discover errors in the scanner
  704. * in an orderly manner.
  705. */
  706. <*>. pp_incl_state.seen_junk++; ppy_warning("Unmatched text '%c' (0x%02x); please report\n", isprint(*ppy_text & 0xff) ? *ppy_text : ' ', *ppy_text);
  707. <<EOF>> {
  708. YY_BUFFER_STATE b = YY_CURRENT_BUFFER;
  709. bufferstackentry_t *bep = pop_buffer();
  710. if((!bep && pp_get_if_depth()) || (bep && pp_get_if_depth() != bep->if_depth))
  711. ppy_warning("Unmatched #if/#endif at end of file");
  712. if(!bep)
  713. {
  714. if(YY_START != INITIAL)
  715. {
  716. ppy_error("Unexpected end of file during preprocessing");
  717. BEGIN(INITIAL);
  718. }
  719. yyterminate();
  720. }
  721. else if(bep->should_pop == 2)
  722. {
  723. macexpstackentry_t *mac;
  724. mac = pop_macro();
  725. expand_macro(mac);
  726. }
  727. ppy__delete_buffer(b);
  728. }
  729. %%
  730. /*
  731. **************************************************************************
  732. * Support functions
  733. **************************************************************************
  734. */
  735. #ifndef ppy_wrap
  736. int ppy_wrap(void)
  737. {
  738. return 1;
  739. }
  740. #endif
  741. /*
  742. *-------------------------------------------------------------------------
  743. * Output newlines or set them as continuations
  744. *
  745. * Input: -1 - Don't count this one, but update local position (see pp_dqs)
  746. * 0 - Line-continuation seen and cache output
  747. * 1 - Newline seen and flush output
  748. *-------------------------------------------------------------------------
  749. */
  750. static void newline(int dowrite)
  751. {
  752. pp_status.line_number++;
  753. pp_status.char_number = 1;
  754. if(dowrite == -1)
  755. return;
  756. ncontinuations++;
  757. if(dowrite)
  758. {
  759. for(;ncontinuations; ncontinuations--)
  760. put_buffer("\n", 1);
  761. }
  762. }
  763. /*
  764. *-------------------------------------------------------------------------
  765. * Make a number out of an any-base and suffixed string
  766. *
  767. * Possible number extensions:
  768. * - "" int
  769. * - "L" long int
  770. * - "LL" long long int
  771. * - "U" unsigned int
  772. * - "UL" unsigned long int
  773. * - "ULL" unsigned long long int
  774. * - "LU" unsigned long int
  775. * - "LLU" unsigned long long int
  776. * - "LUL" invalid
  777. *
  778. * FIXME:
  779. * The sizes of resulting 'int' and 'long' are compiler specific.
  780. * I depend on sizeof(int) > 2 here (although a relatively safe
  781. * assumption).
  782. * Long longs are not yet implemented because this is very compiler
  783. * specific and I don't want to think too much about the problems.
  784. *
  785. *-------------------------------------------------------------------------
  786. */
  787. static int make_number(int radix, YYSTYPE *val, const char *str, int len)
  788. {
  789. int is_l = 0;
  790. int is_ll = 0;
  791. int is_u = 0;
  792. char ext[4];
  793. long l;
  794. ext[3] = '\0';
  795. ext[2] = toupper(str[len-1]);
  796. ext[1] = len > 1 ? toupper(str[len-2]) : ' ';
  797. ext[0] = len > 2 ? toupper(str[len-3]) : ' ';
  798. if(!strcmp(ext, "LUL"))
  799. {
  800. ppy_error("Invalid constant suffix");
  801. return 0;
  802. }
  803. else if(!strcmp(ext, "LLU") || !strcmp(ext, "ULL"))
  804. {
  805. is_ll++;
  806. is_u++;
  807. }
  808. else if(!strcmp(ext+1, "LU") || !strcmp(ext+1, "UL"))
  809. {
  810. is_l++;
  811. is_u++;
  812. }
  813. else if(!strcmp(ext+1, "LL"))
  814. {
  815. is_ll++;
  816. }
  817. else if(!strcmp(ext+2, "L"))
  818. {
  819. is_l++;
  820. }
  821. else if(!strcmp(ext+2, "U"))
  822. {
  823. is_u++;
  824. }
  825. if(is_u && is_ll)
  826. {
  827. errno = 0;
  828. val->ull = strtoull(str, NULL, radix);
  829. if (val->ull == ULLONG_MAX && errno == ERANGE)
  830. ppy_error("integer constant %s is too large\n", str);
  831. return tULONGLONG;
  832. }
  833. else if(!is_u && is_ll)
  834. {
  835. errno = 0;
  836. val->sll = strtoll(str, NULL, radix);
  837. if ((val->sll == LLONG_MIN || val->sll == LLONG_MAX) && errno == ERANGE)
  838. ppy_error("integer constant %s is too large\n", str);
  839. return tSLONGLONG;
  840. }
  841. else if(is_u && is_l)
  842. {
  843. errno = 0;
  844. val->ulong = strtoul(str, NULL, radix);
  845. if (val->ulong == ULONG_MAX && errno == ERANGE)
  846. ppy_error("integer constant %s is too large\n", str);
  847. return tULONG;
  848. }
  849. else if(!is_u && is_l)
  850. {
  851. errno = 0;
  852. val->slong = strtol(str, NULL, radix);
  853. if ((val->slong == LONG_MIN || val->slong == LONG_MAX) && errno == ERANGE)
  854. ppy_error("integer constant %s is too large\n", str);
  855. return tSLONG;
  856. }
  857. else if(is_u && !is_l)
  858. {
  859. unsigned long ul;
  860. errno = 0;
  861. ul = strtoul(str, NULL, radix);
  862. if ((ul == ULONG_MAX && errno == ERANGE) || (ul > UINT_MAX))
  863. ppy_error("integer constant %s is too large\n", str);
  864. val->uint = (unsigned int)ul;
  865. return tUINT;
  866. }
  867. /* Else it must be an int... */
  868. errno = 0;
  869. l = strtol(str, NULL, radix);
  870. if (((l == LONG_MIN || l == LONG_MAX) && errno == ERANGE) ||
  871. (l > INT_MAX) || (l < INT_MIN))
  872. ppy_error("integer constant %s is too large\n", str);
  873. val->sint = (int)l;
  874. return tSINT;
  875. }
  876. /*
  877. *-------------------------------------------------------------------------
  878. * Macro and define expansion support
  879. *
  880. * FIXME: Variable macro arguments.
  881. *-------------------------------------------------------------------------
  882. */
  883. static void expand_special(pp_entry_t *ppp)
  884. {
  885. static char *buf = NULL;
  886. char *new_buf;
  887. assert(ppp->type == def_special);
  888. if(!strcmp(ppp->ident, "__LINE__"))
  889. {
  890. new_buf = pp_xrealloc(buf, 32);
  891. if(!new_buf)
  892. return;
  893. buf = new_buf;
  894. sprintf(buf, "%d", pp_status.line_number);
  895. }
  896. else if(!strcmp(ppp->ident, "__FILE__"))
  897. {
  898. new_buf = pp_xrealloc(buf, strlen(pp_status.input) + 3);
  899. if(!new_buf)
  900. return;
  901. buf = new_buf;
  902. sprintf(buf, "\"%s\"", pp_status.input);
  903. }
  904. else
  905. pp_internal_error(__FILE__, __LINE__, "Special macro '%s' not found...\n", ppp->ident);
  906. if(pp_flex_debug)
  907. fprintf(stderr, "expand_special(%d): %s:%d: '%s' -> '%s'\n",
  908. macexpstackidx,
  909. pp_status.input,
  910. pp_status.line_number,
  911. ppp->ident,
  912. buf ? buf : "");
  913. if(buf && buf[0])
  914. {
  915. push_buffer(ppp, NULL, NULL, 0);
  916. yy_scan_string(buf);
  917. }
  918. }
  919. static void expand_define(pp_entry_t *ppp)
  920. {
  921. assert(ppp->type == def_define);
  922. if(pp_flex_debug)
  923. fprintf(stderr, "expand_define(%d): %s:%d: '%s' -> '%s'\n",
  924. macexpstackidx,
  925. pp_status.input,
  926. pp_status.line_number,
  927. ppp->ident,
  928. ppp->subst.text);
  929. if(ppp->subst.text && ppp->subst.text[0])
  930. {
  931. push_buffer(ppp, NULL, NULL, 0);
  932. yy_scan_string(ppp->subst.text);
  933. }
  934. }
  935. static int curdef_idx = 0;
  936. static int curdef_alloc = 0;
  937. static char *curdef_text = NULL;
  938. static void add_text(const char *str, int len)
  939. {
  940. int new_alloc;
  941. char *new_text;
  942. if(len == 0)
  943. return;
  944. if(curdef_idx >= curdef_alloc || curdef_alloc - curdef_idx < len)
  945. {
  946. new_alloc = curdef_alloc + ((len + ALLOCBLOCKSIZE-1) & ~(ALLOCBLOCKSIZE-1));
  947. new_text = pp_xrealloc(curdef_text, new_alloc * sizeof(curdef_text[0]));
  948. if(!new_text)
  949. return;
  950. curdef_text = new_text;
  951. curdef_alloc = new_alloc;
  952. if(curdef_alloc > 65536)
  953. ppy_warning("Reallocating macro-expansion buffer larger than 64kB");
  954. }
  955. memcpy(&curdef_text[curdef_idx], str, len);
  956. curdef_idx += len;
  957. }
  958. static mtext_t *add_expand_text(mtext_t *mtp, macexpstackentry_t *mep, int *nnl)
  959. {
  960. char *cptr;
  961. char *exp;
  962. int tag;
  963. int n;
  964. if(mtp == NULL)
  965. return NULL;
  966. switch(mtp->type)
  967. {
  968. case exp_text:
  969. if(pp_flex_debug)
  970. fprintf(stderr, "add_expand_text: exp_text: '%s'\n", mtp->subst.text);
  971. add_text(mtp->subst.text, strlen(mtp->subst.text));
  972. break;
  973. case exp_stringize:
  974. if(pp_flex_debug)
  975. fprintf(stderr, "add_expand_text: exp_stringize(%d): '%s'\n",
  976. mtp->subst.argidx,
  977. mep->args[mtp->subst.argidx]);
  978. cptr = mep->args[mtp->subst.argidx];
  979. add_text("\"", 1);
  980. while(*cptr)
  981. {
  982. if(*cptr == '"' || *cptr == '\\')
  983. add_text("\\", 1);
  984. add_text(cptr, 1);
  985. cptr++;
  986. }
  987. add_text("\"", 1);
  988. break;
  989. case exp_concat:
  990. if(pp_flex_debug)
  991. fprintf(stderr, "add_expand_text: exp_concat\n");
  992. /* Remove trailing whitespace from current expansion text */
  993. while(curdef_idx)
  994. {
  995. if(isspace(curdef_text[curdef_idx-1] & 0xff))
  996. curdef_idx--;
  997. else
  998. break;
  999. }
  1000. /* tag current position and recursively expand the next part */
  1001. tag = curdef_idx;
  1002. mtp = add_expand_text(mtp->next, mep, nnl);
  1003. /* Now get rid of the leading space of the expansion */
  1004. cptr = &curdef_text[tag];
  1005. n = curdef_idx - tag;
  1006. while(n)
  1007. {
  1008. if(isspace(*cptr & 0xff))
  1009. {
  1010. cptr++;
  1011. n--;
  1012. }
  1013. else
  1014. break;
  1015. }
  1016. if(cptr != &curdef_text[tag])
  1017. {
  1018. memmove(&curdef_text[tag], cptr, n);
  1019. curdef_idx -= (curdef_idx - tag) - n;
  1020. }
  1021. break;
  1022. case exp_subst:
  1023. if((mtp->next && mtp->next->type == exp_concat) || (mtp->prev && mtp->prev->type == exp_concat))
  1024. exp = mep->args[mtp->subst.argidx];
  1025. else
  1026. exp = mep->ppargs[mtp->subst.argidx];
  1027. if(exp)
  1028. {
  1029. add_text(exp, strlen(exp));
  1030. *nnl -= mep->nnls[mtp->subst.argidx];
  1031. cptr = strchr(exp, '\n');
  1032. while(cptr)
  1033. {
  1034. *cptr = ' ';
  1035. cptr = strchr(cptr+1, '\n');
  1036. }
  1037. mep->nnls[mtp->subst.argidx] = 0;
  1038. }
  1039. if(pp_flex_debug)
  1040. fprintf(stderr, "add_expand_text: exp_subst(%d): '%s'\n", mtp->subst.argidx, exp);
  1041. break;
  1042. default:
  1043. pp_internal_error(__FILE__, __LINE__, "Invalid expansion type (%d) in macro expansion\n", mtp->type);
  1044. }
  1045. return mtp;
  1046. }
  1047. static void expand_macro(macexpstackentry_t *mep)
  1048. {
  1049. mtext_t *mtp;
  1050. int n, k;
  1051. char *cptr;
  1052. int nnl = 0;
  1053. pp_entry_t *ppp = mep->ppp;
  1054. int nargs = mep->nargs;
  1055. assert(ppp->type == def_macro);
  1056. assert(ppp->expanding == 0);
  1057. if((ppp->nargs >= 0 && nargs != ppp->nargs) || (ppp->nargs < 0 && nargs < -ppp->nargs))
  1058. {
  1059. ppy_error("Too %s macro arguments (%d)", nargs < abs(ppp->nargs) ? "few" : "many", nargs);
  1060. return;
  1061. }
  1062. for(n = 0; n < nargs; n++)
  1063. nnl += mep->nnls[n];
  1064. if(pp_flex_debug)
  1065. fprintf(stderr, "expand_macro(%d): %s:%d: '%s'(%d,%d) -> ...\n",
  1066. macexpstackidx,
  1067. pp_status.input,
  1068. pp_status.line_number,
  1069. ppp->ident,
  1070. mep->nargs,
  1071. nnl);
  1072. curdef_idx = 0;
  1073. for(mtp = ppp->subst.mtext; mtp; mtp = mtp->next)
  1074. {
  1075. if(!(mtp = add_expand_text(mtp, mep, &nnl)))
  1076. break;
  1077. }
  1078. for(n = 0; n < nnl; n++)
  1079. add_text("\n", 1);
  1080. /* To make sure there is room and termination (see below) */
  1081. add_text(" \0", 2);
  1082. /* Strip trailing whitespace from expansion */
  1083. for(k = curdef_idx, cptr = &curdef_text[curdef_idx-1]; k > 0; k--, cptr--)
  1084. {
  1085. if(!isspace(*cptr & 0xff))
  1086. break;
  1087. }
  1088. /*
  1089. * We must add *one* whitespace to make sure that there
  1090. * is a token-separation after the expansion.
  1091. */
  1092. *(++cptr) = ' ';
  1093. *(++cptr) = '\0';
  1094. k++;
  1095. /* Strip leading whitespace from expansion */
  1096. for(n = 0, cptr = curdef_text; n < k; n++, cptr++)
  1097. {
  1098. if(!isspace(*cptr & 0xff))
  1099. break;
  1100. }
  1101. if(k - n > 0)
  1102. {
  1103. if(pp_flex_debug)
  1104. fprintf(stderr, "expand_text: '%s'\n", curdef_text + n);
  1105. push_buffer(ppp, NULL, NULL, 0);
  1106. /*yy_scan_bytes(curdef_text + n, k - n);*/
  1107. yy_scan_string(curdef_text + n);
  1108. }
  1109. }
  1110. /*
  1111. *-------------------------------------------------------------------------
  1112. * String collection routines
  1113. *-------------------------------------------------------------------------
  1114. */
  1115. static void new_string(void)
  1116. {
  1117. #ifdef DEBUG
  1118. if(strbuf_idx)
  1119. ppy_warning("new_string: strbuf_idx != 0");
  1120. #endif
  1121. strbuf_idx = 0;
  1122. str_startline = pp_status.line_number;
  1123. }
  1124. static void add_string(const char *str, int len)
  1125. {
  1126. int new_alloc;
  1127. char *new_buffer;
  1128. if(len == 0)
  1129. return;
  1130. if(strbuf_idx >= strbuf_alloc || strbuf_alloc - strbuf_idx < len)
  1131. {
  1132. new_alloc = strbuf_alloc + ((len + ALLOCBLOCKSIZE-1) & ~(ALLOCBLOCKSIZE-1));
  1133. new_buffer = pp_xrealloc(strbuffer, new_alloc * sizeof(strbuffer[0]));
  1134. if(!new_buffer)
  1135. return;
  1136. strbuffer = new_buffer;
  1137. strbuf_alloc = new_alloc;
  1138. if(strbuf_alloc > 65536)
  1139. ppy_warning("Reallocating string buffer larger than 64kB");
  1140. }
  1141. memcpy(&strbuffer[strbuf_idx], str, len);
  1142. strbuf_idx += len;
  1143. }
  1144. static char *get_string(void)
  1145. {
  1146. char *str = pp_xmalloc(strbuf_idx + 1);
  1147. if(!str)
  1148. return NULL;
  1149. memcpy(str, strbuffer, strbuf_idx);
  1150. str[strbuf_idx] = '\0';
  1151. #ifdef DEBUG
  1152. strbuf_idx = 0;
  1153. #endif
  1154. return str;
  1155. }
  1156. static void put_string(void)
  1157. {
  1158. put_buffer(strbuffer, strbuf_idx);
  1159. #ifdef DEBUG
  1160. strbuf_idx = 0;
  1161. #endif
  1162. }
  1163. static int string_start(void)
  1164. {
  1165. return str_startline;
  1166. }
  1167. /*
  1168. *-------------------------------------------------------------------------
  1169. * Buffer management
  1170. *-------------------------------------------------------------------------
  1171. */
  1172. static void push_buffer(pp_entry_t *ppp, char *filename, char *incname, int pop)
  1173. {
  1174. if(ppy_debug)
  1175. printf("push_buffer(%d): %p %p %p %d\n", bufferstackidx, ppp, filename, incname, pop);
  1176. if(bufferstackidx >= MAXBUFFERSTACK)
  1177. pp_internal_error(__FILE__, __LINE__, "Buffer stack overflow");
  1178. memset(&bufferstack[bufferstackidx], 0, sizeof(bufferstack[0]));
  1179. bufferstack[bufferstackidx].bufferstate = YY_CURRENT_BUFFER;
  1180. bufferstack[bufferstackidx].filehandle = pp_status.file;
  1181. bufferstack[bufferstackidx].define = ppp;
  1182. bufferstack[bufferstackidx].line_number = pp_status.line_number;
  1183. bufferstack[bufferstackidx].char_number = pp_status.char_number;
  1184. bufferstack[bufferstackidx].if_depth = pp_get_if_depth();
  1185. bufferstack[bufferstackidx].should_pop = pop;
  1186. bufferstack[bufferstackidx].filename = pp_status.input;
  1187. bufferstack[bufferstackidx].ncontinuations = ncontinuations;
  1188. bufferstack[bufferstackidx].incl = pp_incl_state;
  1189. bufferstack[bufferstackidx].include_filename = incname;
  1190. if(ppp)
  1191. ppp->expanding = 1;
  1192. else if(filename)
  1193. {
  1194. /* These will track the ppy_error to the correct file and line */
  1195. pp_status.line_number = 1;
  1196. pp_status.char_number = 1;
  1197. pp_status.input = filename;
  1198. ncontinuations = 0;
  1199. }
  1200. else if(!pop)
  1201. pp_internal_error(__FILE__, __LINE__, "Pushing buffer without knowing where to go to");
  1202. bufferstackidx++;
  1203. }
  1204. static bufferstackentry_t *pop_buffer(void)
  1205. {
  1206. if(bufferstackidx < 0)
  1207. pp_internal_error(__FILE__, __LINE__, "Bufferstack underflow?");
  1208. if(bufferstackidx == 0)
  1209. return NULL;
  1210. bufferstackidx--;
  1211. if(bufferstack[bufferstackidx].define)
  1212. bufferstack[bufferstackidx].define->expanding = 0;
  1213. else
  1214. {
  1215. includelogicentry_t *iep = NULL;
  1216. if(!bufferstack[bufferstackidx].should_pop)
  1217. {
  1218. fclose(pp_status.file);
  1219. pp_writestring("# %d \"%s\" 2\n", bufferstack[bufferstackidx].line_number, bufferstack[bufferstackidx].filename);
  1220. /* We have EOF, check the include logic */
  1221. if(pp_incl_state.state == 2 && !pp_incl_state.seen_junk && pp_incl_state.ppp)
  1222. {
  1223. pp_entry_t *ppp = pplookup(pp_incl_state.ppp);
  1224. if(ppp)
  1225. {
  1226. iep = pp_xmalloc(sizeof(includelogicentry_t));
  1227. if (iep)
  1228. {
  1229. iep->ppp = ppp;
  1230. ppp->iep = iep;
  1231. iep->filename = bufferstack[bufferstackidx].include_filename;
  1232. iep->prev = NULL;
  1233. iep->next = pp_includelogiclist;
  1234. if(iep->next)
  1235. iep->next->prev = iep;
  1236. pp_includelogiclist = iep;
  1237. if(pp_status.debug)
  1238. fprintf(stderr, "pop_buffer: %s:%d: includelogic added, include_ppp='%s', file='%s'\n",
  1239. bufferstack[bufferstackidx].filename, bufferstack[bufferstackidx].line_number, pp_incl_state.ppp, iep->filename);
  1240. }
  1241. }
  1242. }
  1243. free(pp_incl_state.ppp);
  1244. pp_incl_state = bufferstack[bufferstackidx].incl;
  1245. }
  1246. if (bufferstack[bufferstackidx].include_filename)
  1247. {
  1248. free(pp_status.input);
  1249. pp_status.input = bufferstack[bufferstackidx].filename;
  1250. }
  1251. pp_status.line_number = bufferstack[bufferstackidx].line_number;
  1252. pp_status.char_number = bufferstack[bufferstackidx].char_number;
  1253. ncontinuations = bufferstack[bufferstackidx].ncontinuations;
  1254. if (!iep)
  1255. free(bufferstack[bufferstackidx].include_filename);
  1256. }
  1257. if(ppy_debug)
  1258. printf("pop_buffer(%d): %p %p (%d, %d, %d) %p %d\n",
  1259. bufferstackidx,
  1260. bufferstack[bufferstackidx].bufferstate,
  1261. bufferstack[bufferstackidx].define,
  1262. bufferstack[bufferstackidx].line_number,
  1263. bufferstack[bufferstackidx].char_number,
  1264. bufferstack[bufferstackidx].if_depth,
  1265. bufferstack[bufferstackidx].filename,
  1266. bufferstack[bufferstackidx].should_pop);
  1267. pp_status.file = bufferstack[bufferstackidx].filehandle;
  1268. ppy__switch_to_buffer(bufferstack[bufferstackidx].bufferstate);
  1269. if(bufferstack[bufferstackidx].should_pop)
  1270. {
  1271. if(yy_current_state() == pp_macexp)
  1272. macro_add_expansion();
  1273. else
  1274. pp_internal_error(__FILE__, __LINE__, "Pop buffer and state without macro expansion state");
  1275. yy_pop_state();
  1276. }
  1277. return &bufferstack[bufferstackidx];
  1278. }
  1279. /*
  1280. *-------------------------------------------------------------------------
  1281. * Macro nestng support
  1282. *-------------------------------------------------------------------------
  1283. */
  1284. static void push_macro(pp_entry_t *ppp)
  1285. {
  1286. if(macexpstackidx >= MAXMACEXPSTACK)
  1287. {
  1288. ppy_error("Too many nested macros");
  1289. return;
  1290. }
  1291. macexpstack[macexpstackidx] = pp_xmalloc(sizeof(macexpstack[0][0]));
  1292. if(!macexpstack[macexpstackidx])
  1293. return;
  1294. memset( macexpstack[macexpstackidx], 0, sizeof(macexpstack[0][0]));
  1295. macexpstack[macexpstackidx]->ppp = ppp;
  1296. macexpstackidx++;
  1297. }
  1298. static macexpstackentry_t *top_macro(void)
  1299. {
  1300. return macexpstackidx > 0 ? macexpstack[macexpstackidx-1] : NULL;
  1301. }
  1302. static macexpstackentry_t *pop_macro(void)
  1303. {
  1304. if(macexpstackidx <= 0)
  1305. pp_internal_error(__FILE__, __LINE__, "Macro expansion stack underflow\n");
  1306. return macexpstack[--macexpstackidx];
  1307. }
  1308. static void free_macro(macexpstackentry_t *mep)
  1309. {
  1310. int i;
  1311. for(i = 0; i < mep->nargs; i++)
  1312. free(mep->args[i]);
  1313. free(mep->args);
  1314. free(mep->nnls);
  1315. free(mep->curarg);
  1316. free(mep);
  1317. }
  1318. static void add_text_to_macro(const char *text, int len)
  1319. {
  1320. macexpstackentry_t *mep = top_macro();
  1321. assert(mep->ppp->expanding == 0);
  1322. if(mep->curargalloc - mep->curargsize <= len+1) /* +1 for '\0' */
  1323. {
  1324. char *new_curarg;
  1325. int new_alloc = mep->curargalloc + ((ALLOCBLOCKSIZE > len+1) ? ALLOCBLOCKSIZE : len+1);
  1326. new_curarg = pp_xrealloc(mep->curarg, new_alloc * sizeof(mep->curarg[0]));
  1327. if(!new_curarg)
  1328. return;
  1329. mep->curarg = new_curarg;
  1330. mep->curargalloc = new_alloc;
  1331. }
  1332. memcpy(mep->curarg + mep->curargsize, text, len);
  1333. mep->curargsize += len;
  1334. mep->curarg[mep->curargsize] = '\0';
  1335. }
  1336. static void macro_add_arg(int last)
  1337. {
  1338. int nnl = 0;
  1339. char *cptr;
  1340. char **new_args, **new_ppargs;
  1341. int *new_nnls;
  1342. macexpstackentry_t *mep = top_macro();
  1343. assert(mep->ppp->expanding == 0);
  1344. new_args = pp_xrealloc(mep->args, (mep->nargs+1) * sizeof(mep->args[0]));
  1345. if(!new_args)
  1346. return;
  1347. mep->args = new_args;
  1348. new_ppargs = pp_xrealloc(mep->ppargs, (mep->nargs+1) * sizeof(mep->ppargs[0]));
  1349. if(!new_ppargs)
  1350. return;
  1351. mep->ppargs = new_ppargs;
  1352. new_nnls = pp_xrealloc(mep->nnls, (mep->nargs+1) * sizeof(mep->nnls[0]));
  1353. if(!new_nnls)
  1354. return;
  1355. mep->nnls = new_nnls;
  1356. mep->args[mep->nargs] = pp_xstrdup(mep->curarg ? mep->curarg : "");
  1357. if(!mep->args[mep->nargs])
  1358. return;
  1359. cptr = mep->args[mep->nargs]-1;
  1360. while((cptr = strchr(cptr+1, '\n')))
  1361. {
  1362. nnl++;
  1363. }
  1364. mep->nnls[mep->nargs] = nnl;
  1365. mep->nargs++;
  1366. free(mep->curarg);
  1367. mep->curargalloc = mep->curargsize = 0;
  1368. mep->curarg = NULL;
  1369. if(pp_flex_debug)
  1370. fprintf(stderr, "macro_add_arg: %s:%d: %d -> '%s'\n",
  1371. pp_status.input,
  1372. pp_status.line_number,
  1373. mep->nargs-1,
  1374. mep->args[mep->nargs-1]);
  1375. /* Each macro argument must be expanded to cope with stingize */
  1376. if(last || mep->args[mep->nargs-1][0])
  1377. {
  1378. yy_push_state(pp_macexp);
  1379. push_buffer(NULL, NULL, NULL, last ? 2 : 1);
  1380. yy_scan_string(mep->args[mep->nargs-1]);
  1381. /*mep->bufferstackidx = bufferstackidx; But not nested! */
  1382. }
  1383. }
  1384. static void macro_add_expansion(void)
  1385. {
  1386. macexpstackentry_t *mep = top_macro();
  1387. assert(mep->ppp->expanding == 0);
  1388. mep->ppargs[mep->nargs-1] = pp_xstrdup(mep->curarg ? mep->curarg : "");
  1389. free(mep->curarg);
  1390. mep->curargalloc = mep->curargsize = 0;
  1391. mep->curarg = NULL;
  1392. if(pp_flex_debug)
  1393. fprintf(stderr, "macro_add_expansion: %s:%d: %d -> '%s'\n",
  1394. pp_status.input,
  1395. pp_status.line_number,
  1396. mep->nargs-1,
  1397. mep->ppargs[mep->nargs-1] ? mep->ppargs[mep->nargs-1] : "");
  1398. }
  1399. /*
  1400. *-------------------------------------------------------------------------
  1401. * Output management
  1402. *-------------------------------------------------------------------------
  1403. */
  1404. static void put_buffer(const char *s, int len)
  1405. {
  1406. if(top_macro())
  1407. add_text_to_macro(s, len);
  1408. else
  1409. fwrite(s, 1, len, ppy_out);
  1410. }
  1411. /*
  1412. *-------------------------------------------------------------------------
  1413. * Include management
  1414. *-------------------------------------------------------------------------
  1415. */
  1416. void pp_do_include(char *fname, int type)
  1417. {
  1418. char *newpath;
  1419. int n;
  1420. includelogicentry_t *iep;
  1421. void *fp;
  1422. if(!fname)
  1423. return;
  1424. for(iep = pp_includelogiclist; iep; iep = iep->next)
  1425. {
  1426. if(!strcmp(iep->filename, fname))
  1427. {
  1428. /*
  1429. * We are done. The file was included before.
  1430. * If the define was deleted, then this entry would have
  1431. * been deleted too.
  1432. */
  1433. free(fname);
  1434. return;
  1435. }
  1436. }
  1437. n = strlen(fname);
  1438. if(n <= 2)
  1439. {
  1440. ppy_error("Empty include filename");
  1441. free(fname);
  1442. return;
  1443. }
  1444. /* Undo the effect of the quotation */
  1445. fname[n-1] = '\0';
  1446. if((fp = pp_open_include(fname+1, type, pp_status.input, &newpath)) == NULL)
  1447. {
  1448. ppy_error("Unable to open include file %s", fname+1);
  1449. free(fname);
  1450. return;
  1451. }
  1452. fname[n-1] = *fname; /* Redo the quotes */
  1453. push_buffer(NULL, newpath, fname, 0);
  1454. pp_incl_state.seen_junk = 0;
  1455. pp_incl_state.state = 0;
  1456. pp_incl_state.ppp = NULL;
  1457. if(pp_status.debug)
  1458. fprintf(stderr, "pp_do_include: %s:%d: include_state=%d, include_ifdepth=%d\n",
  1459. pp_status.input, pp_status.line_number, pp_incl_state.state, pp_incl_state.ifdepth);
  1460. pp_status.file = fp;
  1461. ppy__switch_to_buffer(ppy__create_buffer(NULL, YY_BUF_SIZE));
  1462. pp_writestring("# 1 \"%s\" 1%s\n", newpath, type ? "" : " 3");
  1463. }
  1464. /*
  1465. *-------------------------------------------------------------------------
  1466. * Push/pop preprocessor ignore state when processing conditionals
  1467. * which are false.
  1468. *-------------------------------------------------------------------------
  1469. */
  1470. void pp_push_ignore_state(void)
  1471. {
  1472. yy_push_state(pp_ignore);
  1473. }
  1474. void pp_pop_ignore_state(void)
  1475. {
  1476. yy_pop_state();
  1477. }