deffilep.y 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  1. %{ /* deffilep.y - parser for .def files */
  2. /* Copyright (C) 1995-2022 Free Software Foundation, Inc.
  3. This file is part of GNU Binutils.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "libiberty.h"
  18. #include "safe-ctype.h"
  19. #include "bfd.h"
  20. #include "bfdlink.h"
  21. #include "ld.h"
  22. #include "ldmisc.h"
  23. #include "deffile.h"
  24. #define TRACE 0
  25. #define ROUND_UP(a, b) (((a)+((b)-1))&~((b)-1))
  26. /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
  27. as well as gratuitiously global symbol names, so we can have multiple
  28. yacc generated parsers in ld. Note that these are only the variables
  29. produced by yacc. If other parser generators (bison, byacc, etc) produce
  30. additional global names that conflict at link time, then those parser
  31. generators need to be fixed instead of adding those names to this list. */
  32. #define yymaxdepth def_maxdepth
  33. #define yyparse def_parse
  34. #define yylex def_lex
  35. #define yyerror def_error
  36. #define yylval def_lval
  37. #define yychar def_char
  38. #define yydebug def_debug
  39. #define yypact def_pact
  40. #define yyr1 def_r1
  41. #define yyr2 def_r2
  42. #define yydef def_def
  43. #define yychk def_chk
  44. #define yypgo def_pgo
  45. #define yyact def_act
  46. #define yyexca def_exca
  47. #define yyerrflag def_errflag
  48. #define yynerrs def_nerrs
  49. #define yyps def_ps
  50. #define yypv def_pv
  51. #define yys def_s
  52. #define yy_yys def_yys
  53. #define yystate def_state
  54. #define yytmp def_tmp
  55. #define yyv def_v
  56. #define yy_yyv def_yyv
  57. #define yyval def_val
  58. #define yylloc def_lloc
  59. #define yyreds def_reds /* With YYDEBUG defined. */
  60. #define yytoks def_toks /* With YYDEBUG defined. */
  61. #define yylhs def_yylhs
  62. #define yylen def_yylen
  63. #define yydefred def_yydefred
  64. #define yydgoto def_yydgoto
  65. #define yysindex def_yysindex
  66. #define yyrindex def_yyrindex
  67. #define yygindex def_yygindex
  68. #define yytable def_yytable
  69. #define yycheck def_yycheck
  70. typedef struct def_pool_str {
  71. struct def_pool_str *next;
  72. char data[1];
  73. } def_pool_str;
  74. static def_pool_str *pool_strs = NULL;
  75. static char *def_pool_alloc (size_t sz);
  76. static char *def_pool_strdup (const char *str);
  77. static void def_pool_free (void);
  78. static void def_description (const char *);
  79. static void def_exports (const char *, const char *, int, int, const char *);
  80. static void def_heapsize (int, int);
  81. static void def_import (const char *, const char *, const char *, const char *,
  82. int, const char *);
  83. static void def_image_name (const char *, bfd_vma, int);
  84. static void def_section (const char *, int);
  85. static void def_section_alt (const char *, const char *);
  86. static void def_stacksize (int, int);
  87. static void def_version (int, int);
  88. static void def_directive (char *);
  89. static void def_aligncomm (char *str, int align);
  90. static int def_parse (void);
  91. static void def_error (const char *);
  92. static int def_lex (void);
  93. static int lex_forced_token = 0;
  94. static const char *lex_parse_string = 0;
  95. static const char *lex_parse_string_end = 0;
  96. %}
  97. %union {
  98. char *id;
  99. const char *id_const;
  100. int number;
  101. bfd_vma vma;
  102. char *digits;
  103. };
  104. %token NAME LIBRARY DESCRIPTION STACKSIZE_K HEAPSIZE CODE DATAU DATAL
  105. %token SECTIONS EXPORTS IMPORTS VERSIONK BASE CONSTANTU CONSTANTL
  106. %token PRIVATEU PRIVATEL ALIGNCOMM
  107. %token READ WRITE EXECUTE SHARED_K NONAMEU NONAMEL DIRECTIVE EQUAL
  108. %token <id> ID
  109. %token <digits> DIGITS
  110. %type <number> NUMBER
  111. %type <vma> VMA opt_base
  112. %type <digits> opt_digits
  113. %type <number> opt_ordinal
  114. %type <number> attr attr_list opt_number exp_opt_list exp_opt
  115. %type <id> opt_name opt_name2 opt_equal_name anylang_id opt_id
  116. %type <id> opt_equalequal_name
  117. %type <id_const> keyword_as_name
  118. %%
  119. start: start command
  120. | command
  121. ;
  122. command:
  123. NAME opt_name opt_base { def_image_name ($2, $3, 0); }
  124. | LIBRARY opt_name opt_base { def_image_name ($2, $3, 1); }
  125. | DESCRIPTION ID { def_description ($2);}
  126. | STACKSIZE_K NUMBER opt_number { def_stacksize ($2, $3);}
  127. | HEAPSIZE NUMBER opt_number { def_heapsize ($2, $3);}
  128. | CODE attr_list { def_section ("CODE", $2);}
  129. | DATAU attr_list { def_section ("DATA", $2);}
  130. | SECTIONS seclist
  131. | EXPORTS explist
  132. | IMPORTS implist
  133. | VERSIONK NUMBER { def_version ($2, 0);}
  134. | VERSIONK NUMBER '.' NUMBER { def_version ($2, $4);}
  135. | DIRECTIVE ID { def_directive ($2);}
  136. | ALIGNCOMM anylang_id ',' NUMBER { def_aligncomm ($2, $4);}
  137. ;
  138. explist:
  139. /* EMPTY */
  140. | expline
  141. | explist expline
  142. ;
  143. expline:
  144. /* The opt_comma is necessary to support both the usual
  145. DEF file syntax as well as .drectve syntax which
  146. mandates <expsym>,<expoptlist>. */
  147. opt_name2 opt_equal_name opt_ordinal opt_comma exp_opt_list opt_comma opt_equalequal_name
  148. { def_exports ($1, $2, $3, $5, $7); }
  149. ;
  150. exp_opt_list:
  151. /* The opt_comma is necessary to support both the usual
  152. DEF file syntax as well as .drectve syntax which
  153. allows for comma separated opt list. */
  154. exp_opt opt_comma exp_opt_list { $$ = $1 | $3; }
  155. | { $$ = 0; }
  156. ;
  157. exp_opt:
  158. NONAMEU { $$ = 1; }
  159. | NONAMEL { $$ = 1; }
  160. | CONSTANTU { $$ = 2; }
  161. | CONSTANTL { $$ = 2; }
  162. | DATAU { $$ = 4; }
  163. | DATAL { $$ = 4; }
  164. | PRIVATEU { $$ = 8; }
  165. | PRIVATEL { $$ = 8; }
  166. ;
  167. implist:
  168. implist impline
  169. | impline
  170. ;
  171. impline:
  172. ID '=' ID '.' ID '.' ID opt_equalequal_name
  173. { def_import ($1, $3, $5, $7, -1, $8); }
  174. | ID '=' ID '.' ID '.' NUMBER opt_equalequal_name
  175. { def_import ($1, $3, $5, 0, $7, $8); }
  176. | ID '=' ID '.' ID opt_equalequal_name
  177. { def_import ($1, $3, 0, $5, -1, $6); }
  178. | ID '=' ID '.' NUMBER opt_equalequal_name
  179. { def_import ($1, $3, 0, 0, $5, $6); }
  180. | ID '.' ID '.' ID opt_equalequal_name
  181. { def_import( 0, $1, $3, $5, -1, $6); }
  182. | ID '.' ID opt_equalequal_name
  183. { def_import ( 0, $1, 0, $3, -1, $4); }
  184. ;
  185. seclist:
  186. seclist secline
  187. | secline
  188. ;
  189. secline:
  190. ID attr_list { def_section ($1, $2);}
  191. | ID ID { def_section_alt ($1, $2);}
  192. ;
  193. attr_list:
  194. attr_list opt_comma attr { $$ = $1 | $3; }
  195. | attr { $$ = $1; }
  196. ;
  197. opt_comma:
  198. ','
  199. |
  200. ;
  201. opt_number: ',' NUMBER { $$=$2;}
  202. | { $$=-1;}
  203. ;
  204. attr:
  205. READ { $$ = 1;}
  206. | WRITE { $$ = 2;}
  207. | EXECUTE { $$=4;}
  208. | SHARED_K { $$=8;}
  209. ;
  210. keyword_as_name: BASE { $$ = "BASE"; }
  211. | CODE { $$ = "CODE"; }
  212. | CONSTANTU { $$ = "CONSTANT"; }
  213. | CONSTANTL { $$ = "constant"; }
  214. | DATAU { $$ = "DATA"; }
  215. | DATAL { $$ = "data"; }
  216. | DESCRIPTION { $$ = "DESCRIPTION"; }
  217. | DIRECTIVE { $$ = "DIRECTIVE"; }
  218. | EXECUTE { $$ = "EXECUTE"; }
  219. | EXPORTS { $$ = "EXPORTS"; }
  220. | HEAPSIZE { $$ = "HEAPSIZE"; }
  221. | IMPORTS { $$ = "IMPORTS"; }
  222. /* Disable LIBRARY keyword as valid symbol-name. This is necessary
  223. for libtool, which places this command after EXPORTS command.
  224. This behavior is illegal by specification, but sadly required by
  225. by compatibility reasons.
  226. See PR binutils/13710
  227. | LIBRARY { $$ = "LIBRARY"; } */
  228. | NAME { $$ = "NAME"; }
  229. | NONAMEU { $$ = "NONAME"; }
  230. | NONAMEL { $$ = "noname"; }
  231. | PRIVATEU { $$ = "PRIVATE"; }
  232. | PRIVATEL { $$ = "private"; }
  233. | READ { $$ = "READ"; }
  234. | SHARED_K { $$ = "SHARED"; }
  235. | STACKSIZE_K { $$ = "STACKSIZE"; }
  236. | VERSIONK { $$ = "VERSION"; }
  237. | WRITE { $$ = "WRITE"; }
  238. ;
  239. opt_name2: ID { $$ = $1; }
  240. | '.' keyword_as_name
  241. {
  242. char *name = xmalloc (strlen ($2) + 2);
  243. sprintf (name, ".%s", $2);
  244. $$ = name;
  245. }
  246. | '.' opt_name2
  247. {
  248. char *name = def_pool_alloc (strlen ($2) + 2);
  249. sprintf (name, ".%s", $2);
  250. $$ = name;
  251. }
  252. | keyword_as_name '.' opt_name2
  253. {
  254. char *name = def_pool_alloc (strlen ($1) + 1 + strlen ($3) + 1);
  255. sprintf (name, "%s.%s", $1, $3);
  256. $$ = name;
  257. }
  258. | ID '.' opt_name2
  259. {
  260. char *name = def_pool_alloc (strlen ($1) + 1 + strlen ($3) + 1);
  261. sprintf (name, "%s.%s", $1, $3);
  262. $$ = name;
  263. }
  264. ;
  265. opt_name: opt_name2 { $$ = $1; }
  266. | { $$ = ""; }
  267. ;
  268. opt_equalequal_name: EQUAL ID { $$ = $2; }
  269. | { $$ = 0; }
  270. ;
  271. opt_ordinal:
  272. '@' NUMBER { $$ = $2;}
  273. | { $$ = -1;}
  274. ;
  275. opt_equal_name:
  276. '=' opt_name2 { $$ = $2; }
  277. | { $$ = 0; }
  278. ;
  279. opt_base: BASE '=' VMA { $$ = $3;}
  280. | { $$ = (bfd_vma) -1;}
  281. ;
  282. anylang_id: ID { $$ = $1; }
  283. | '.' ID
  284. {
  285. char *id = def_pool_alloc (strlen ($2) + 2);
  286. sprintf (id, ".%s", $2);
  287. $$ = id;
  288. }
  289. | anylang_id '.' opt_digits opt_id
  290. {
  291. char *id = def_pool_alloc (strlen ($1) + 1 + strlen ($3) + strlen ($4) + 1);
  292. sprintf (id, "%s.%s%s", $1, $3, $4);
  293. $$ = id;
  294. }
  295. ;
  296. opt_digits: DIGITS { $$ = $1; }
  297. | { $$ = ""; }
  298. ;
  299. opt_id: ID { $$ = $1; }
  300. | { $$ = ""; }
  301. ;
  302. NUMBER: DIGITS { $$ = strtoul ($1, 0, 0); }
  303. ;
  304. VMA: DIGITS { $$ = (bfd_vma) strtoull ($1, 0, 0); }
  305. %%
  306. /*****************************************************************************
  307. API
  308. *****************************************************************************/
  309. static FILE *the_file;
  310. static const char *def_filename;
  311. static int linenumber;
  312. static def_file *def;
  313. static int saw_newline;
  314. struct directive
  315. {
  316. struct directive *next;
  317. char *name;
  318. int len;
  319. };
  320. static struct directive *directives = 0;
  321. def_file *
  322. def_file_empty (void)
  323. {
  324. def_file *rv = xmalloc (sizeof (def_file));
  325. memset (rv, 0, sizeof (def_file));
  326. rv->is_dll = -1;
  327. rv->base_address = (bfd_vma) -1;
  328. rv->stack_reserve = rv->stack_commit = -1;
  329. rv->heap_reserve = rv->heap_commit = -1;
  330. rv->version_major = rv->version_minor = -1;
  331. return rv;
  332. }
  333. def_file *
  334. def_file_parse (const char *filename, def_file *add_to)
  335. {
  336. struct directive *d;
  337. the_file = fopen (filename, "r");
  338. def_filename = filename;
  339. linenumber = 1;
  340. if (!the_file)
  341. {
  342. perror (filename);
  343. return 0;
  344. }
  345. if (add_to)
  346. {
  347. def = add_to;
  348. }
  349. else
  350. {
  351. def = def_file_empty ();
  352. }
  353. saw_newline = 1;
  354. if (def_parse ())
  355. {
  356. def_file_free (def);
  357. fclose (the_file);
  358. def_pool_free ();
  359. return 0;
  360. }
  361. fclose (the_file);
  362. while ((d = directives) != NULL)
  363. {
  364. #if TRACE
  365. printf ("Adding directive %08x `%s'\n", d->name, d->name);
  366. #endif
  367. def_file_add_directive (def, d->name, d->len);
  368. directives = d->next;
  369. free (d->name);
  370. free (d);
  371. }
  372. def_pool_free ();
  373. return def;
  374. }
  375. void
  376. def_file_free (def_file *fdef)
  377. {
  378. int i;
  379. if (!fdef)
  380. return;
  381. free (fdef->name);
  382. free (fdef->description);
  383. if (fdef->section_defs)
  384. {
  385. for (i = 0; i < fdef->num_section_defs; i++)
  386. {
  387. free (fdef->section_defs[i].name);
  388. free (fdef->section_defs[i].class);
  389. }
  390. free (fdef->section_defs);
  391. }
  392. if (fdef->exports)
  393. {
  394. for (i = 0; i < fdef->num_exports; i++)
  395. {
  396. if (fdef->exports[i].internal_name != fdef->exports[i].name)
  397. free (fdef->exports[i].internal_name);
  398. free (fdef->exports[i].name);
  399. free (fdef->exports[i].its_name);
  400. }
  401. free (fdef->exports);
  402. }
  403. if (fdef->imports)
  404. {
  405. for (i = 0; i < fdef->num_imports; i++)
  406. {
  407. if (fdef->imports[i].internal_name != fdef->imports[i].name)
  408. free (fdef->imports[i].internal_name);
  409. free (fdef->imports[i].name);
  410. free (fdef->imports[i].its_name);
  411. }
  412. free (fdef->imports);
  413. }
  414. while (fdef->modules)
  415. {
  416. def_file_module *m = fdef->modules;
  417. fdef->modules = fdef->modules->next;
  418. free (m);
  419. }
  420. while (fdef->aligncomms)
  421. {
  422. def_file_aligncomm *c = fdef->aligncomms;
  423. fdef->aligncomms = fdef->aligncomms->next;
  424. free (c->symbol_name);
  425. free (c);
  426. }
  427. free (fdef);
  428. }
  429. #ifdef DEF_FILE_PRINT
  430. void
  431. def_file_print (FILE *file, def_file *fdef)
  432. {
  433. int i;
  434. fprintf (file, ">>>> def_file at 0x%08x\n", fdef);
  435. if (fdef->name)
  436. fprintf (file, " name: %s\n", fdef->name ? fdef->name : "(unspecified)");
  437. if (fdef->is_dll != -1)
  438. fprintf (file, " is dll: %s\n", fdef->is_dll ? "yes" : "no");
  439. if (fdef->base_address != (bfd_vma) -1)
  440. {
  441. fprintf (file, " base address: 0x");
  442. fprintf_vma (file, fdef->base_address);
  443. fprintf (file, "\n");
  444. }
  445. if (fdef->description)
  446. fprintf (file, " description: `%s'\n", fdef->description);
  447. if (fdef->stack_reserve != -1)
  448. fprintf (file, " stack reserve: 0x%08x\n", fdef->stack_reserve);
  449. if (fdef->stack_commit != -1)
  450. fprintf (file, " stack commit: 0x%08x\n", fdef->stack_commit);
  451. if (fdef->heap_reserve != -1)
  452. fprintf (file, " heap reserve: 0x%08x\n", fdef->heap_reserve);
  453. if (fdef->heap_commit != -1)
  454. fprintf (file, " heap commit: 0x%08x\n", fdef->heap_commit);
  455. if (fdef->num_section_defs > 0)
  456. {
  457. fprintf (file, " section defs:\n");
  458. for (i = 0; i < fdef->num_section_defs; i++)
  459. {
  460. fprintf (file, " name: `%s', class: `%s', flags:",
  461. fdef->section_defs[i].name, fdef->section_defs[i].class);
  462. if (fdef->section_defs[i].flag_read)
  463. fprintf (file, " R");
  464. if (fdef->section_defs[i].flag_write)
  465. fprintf (file, " W");
  466. if (fdef->section_defs[i].flag_execute)
  467. fprintf (file, " X");
  468. if (fdef->section_defs[i].flag_shared)
  469. fprintf (file, " S");
  470. fprintf (file, "\n");
  471. }
  472. }
  473. if (fdef->num_exports > 0)
  474. {
  475. fprintf (file, " exports:\n");
  476. for (i = 0; i < fdef->num_exports; i++)
  477. {
  478. fprintf (file, " name: `%s', int: `%s', ordinal: %d, flags:",
  479. fdef->exports[i].name, fdef->exports[i].internal_name,
  480. fdef->exports[i].ordinal);
  481. if (fdef->exports[i].flag_private)
  482. fprintf (file, " P");
  483. if (fdef->exports[i].flag_constant)
  484. fprintf (file, " C");
  485. if (fdef->exports[i].flag_noname)
  486. fprintf (file, " N");
  487. if (fdef->exports[i].flag_data)
  488. fprintf (file, " D");
  489. fprintf (file, "\n");
  490. }
  491. }
  492. if (fdef->num_imports > 0)
  493. {
  494. fprintf (file, " imports:\n");
  495. for (i = 0; i < fdef->num_imports; i++)
  496. {
  497. fprintf (file, " int: %s, from: `%s', name: `%s', ordinal: %d\n",
  498. fdef->imports[i].internal_name,
  499. fdef->imports[i].module,
  500. fdef->imports[i].name,
  501. fdef->imports[i].ordinal);
  502. }
  503. }
  504. if (fdef->version_major != -1)
  505. fprintf (file, " version: %d.%d\n", fdef->version_major, fdef->version_minor);
  506. fprintf (file, "<<<< def_file at 0x%08x\n", fdef);
  507. }
  508. #endif
  509. /* Helper routine to check for identity of string pointers,
  510. which might be NULL. */
  511. static int
  512. are_names_equal (const char *s1, const char *s2)
  513. {
  514. if (!s1 && !s2)
  515. return 0;
  516. if (!s1 || !s2)
  517. return (!s1 ? -1 : 1);
  518. return strcmp (s1, s2);
  519. }
  520. static int
  521. cmp_export_elem (const def_file_export *e, const char *ex_name,
  522. const char *in_name, const char *its_name,
  523. int ord)
  524. {
  525. int r;
  526. if ((r = are_names_equal (ex_name, e->name)) != 0)
  527. return r;
  528. if ((r = are_names_equal (in_name, e->internal_name)) != 0)
  529. return r;
  530. if ((r = are_names_equal (its_name, e->its_name)) != 0)
  531. return r;
  532. return (ord - e->ordinal);
  533. }
  534. /* Search the position of the identical element, or returns the position
  535. of the next higher element. If last valid element is smaller, then MAX
  536. is returned. */
  537. static int
  538. find_export_in_list (def_file_export *b, int max,
  539. const char *ex_name, const char *in_name,
  540. const char *its_name, int ord, int *is_ident)
  541. {
  542. int e, l, r, p;
  543. *is_ident = 0;
  544. if (!max)
  545. return 0;
  546. if ((e = cmp_export_elem (b, ex_name, in_name, its_name, ord)) <= 0)
  547. {
  548. if (!e)
  549. *is_ident = 1;
  550. return 0;
  551. }
  552. if (max == 1)
  553. return 1;
  554. if ((e = cmp_export_elem (b + (max - 1), ex_name, in_name, its_name, ord)) > 0)
  555. return max;
  556. else if (!e || max == 2)
  557. {
  558. if (!e)
  559. *is_ident = 1;
  560. return max - 1;
  561. }
  562. l = 0; r = max - 1;
  563. while (l < r)
  564. {
  565. p = (l + r) / 2;
  566. e = cmp_export_elem (b + p, ex_name, in_name, its_name, ord);
  567. if (!e)
  568. {
  569. *is_ident = 1;
  570. return p;
  571. }
  572. else if (e < 0)
  573. r = p - 1;
  574. else if (e > 0)
  575. l = p + 1;
  576. }
  577. if ((e = cmp_export_elem (b + l, ex_name, in_name, its_name, ord)) > 0)
  578. ++l;
  579. else if (!e)
  580. *is_ident = 1;
  581. return l;
  582. }
  583. def_file_export *
  584. def_file_add_export (def_file *fdef,
  585. const char *external_name,
  586. const char *internal_name,
  587. int ordinal,
  588. const char *its_name,
  589. int *is_dup)
  590. {
  591. def_file_export *e;
  592. int pos;
  593. int max_exports = ROUND_UP(fdef->num_exports, 32);
  594. if (internal_name && !external_name)
  595. external_name = internal_name;
  596. if (external_name && !internal_name)
  597. internal_name = external_name;
  598. /* We need to avoid duplicates. */
  599. *is_dup = 0;
  600. pos = find_export_in_list (fdef->exports, fdef->num_exports,
  601. external_name, internal_name,
  602. its_name, ordinal, is_dup);
  603. if (*is_dup != 0)
  604. return (fdef->exports + pos);
  605. if (fdef->num_exports >= max_exports)
  606. {
  607. max_exports = ROUND_UP(fdef->num_exports + 1, 32);
  608. if (fdef->exports)
  609. fdef->exports = xrealloc (fdef->exports,
  610. max_exports * sizeof (def_file_export));
  611. else
  612. fdef->exports = xmalloc (max_exports * sizeof (def_file_export));
  613. }
  614. e = fdef->exports + pos;
  615. if (pos != fdef->num_exports)
  616. memmove (&e[1], e, (sizeof (def_file_export) * (fdef->num_exports - pos)));
  617. memset (e, 0, sizeof (def_file_export));
  618. e->name = xstrdup (external_name);
  619. e->internal_name = xstrdup (internal_name);
  620. e->its_name = (its_name ? xstrdup (its_name) : NULL);
  621. e->ordinal = ordinal;
  622. fdef->num_exports++;
  623. return e;
  624. }
  625. def_file_module *
  626. def_get_module (def_file *fdef, const char *name)
  627. {
  628. def_file_module *s;
  629. for (s = fdef->modules; s; s = s->next)
  630. if (strcmp (s->name, name) == 0)
  631. return s;
  632. return NULL;
  633. }
  634. static def_file_module *
  635. def_stash_module (def_file *fdef, const char *name)
  636. {
  637. def_file_module *s;
  638. if ((s = def_get_module (fdef, name)) != NULL)
  639. return s;
  640. s = xmalloc (sizeof (def_file_module) + strlen (name));
  641. s->next = fdef->modules;
  642. fdef->modules = s;
  643. s->user_data = 0;
  644. strcpy (s->name, name);
  645. return s;
  646. }
  647. static int
  648. cmp_import_elem (const def_file_import *e, const char *ex_name,
  649. const char *in_name, const char *module,
  650. int ord)
  651. {
  652. int r;
  653. if ((r = are_names_equal (module, (e->module ? e->module->name : NULL))))
  654. return r;
  655. if ((r = are_names_equal (ex_name, e->name)) != 0)
  656. return r;
  657. if ((r = are_names_equal (in_name, e->internal_name)) != 0)
  658. return r;
  659. if (ord != e->ordinal)
  660. return (ord < e->ordinal ? -1 : 1);
  661. return 0;
  662. }
  663. /* Search the position of the identical element, or returns the position
  664. of the next higher element. If last valid element is smaller, then MAX
  665. is returned. */
  666. static int
  667. find_import_in_list (def_file_import *b, int max,
  668. const char *ex_name, const char *in_name,
  669. const char *module, int ord, int *is_ident)
  670. {
  671. int e, l, r, p;
  672. *is_ident = 0;
  673. if (!max)
  674. return 0;
  675. if ((e = cmp_import_elem (b, ex_name, in_name, module, ord)) <= 0)
  676. {
  677. if (!e)
  678. *is_ident = 1;
  679. return 0;
  680. }
  681. if (max == 1)
  682. return 1;
  683. if ((e = cmp_import_elem (b + (max - 1), ex_name, in_name, module, ord)) > 0)
  684. return max;
  685. else if (!e || max == 2)
  686. {
  687. if (!e)
  688. *is_ident = 1;
  689. return max - 1;
  690. }
  691. l = 0; r = max - 1;
  692. while (l < r)
  693. {
  694. p = (l + r) / 2;
  695. e = cmp_import_elem (b + p, ex_name, in_name, module, ord);
  696. if (!e)
  697. {
  698. *is_ident = 1;
  699. return p;
  700. }
  701. else if (e < 0)
  702. r = p - 1;
  703. else if (e > 0)
  704. l = p + 1;
  705. }
  706. if ((e = cmp_import_elem (b + l, ex_name, in_name, module, ord)) > 0)
  707. ++l;
  708. else if (!e)
  709. *is_ident = 1;
  710. return l;
  711. }
  712. static void
  713. fill_in_import (def_file_import *i,
  714. const char *name,
  715. def_file_module *module,
  716. int ordinal,
  717. const char *internal_name,
  718. const char *its_name)
  719. {
  720. memset (i, 0, sizeof (def_file_import));
  721. if (name)
  722. i->name = xstrdup (name);
  723. i->module = module;
  724. i->ordinal = ordinal;
  725. if (internal_name)
  726. i->internal_name = xstrdup (internal_name);
  727. else
  728. i->internal_name = i->name;
  729. i->its_name = (its_name ? xstrdup (its_name) : NULL);
  730. }
  731. def_file_import *
  732. def_file_add_import (def_file *fdef,
  733. const char *name,
  734. const char *module,
  735. int ordinal,
  736. const char *internal_name,
  737. const char *its_name,
  738. int *is_dup)
  739. {
  740. def_file_import *i;
  741. int pos;
  742. int max_imports = ROUND_UP (fdef->num_imports, 16);
  743. /* We need to avoid here duplicates. */
  744. *is_dup = 0;
  745. pos = find_import_in_list (fdef->imports, fdef->num_imports,
  746. name,
  747. (!internal_name ? name : internal_name),
  748. module, ordinal, is_dup);
  749. if (*is_dup != 0)
  750. return fdef->imports + pos;
  751. if (fdef->num_imports >= max_imports)
  752. {
  753. max_imports = ROUND_UP (fdef->num_imports+1, 16);
  754. if (fdef->imports)
  755. fdef->imports = xrealloc (fdef->imports,
  756. max_imports * sizeof (def_file_import));
  757. else
  758. fdef->imports = xmalloc (max_imports * sizeof (def_file_import));
  759. }
  760. i = fdef->imports + pos;
  761. if (pos != fdef->num_imports)
  762. memmove (i + 1, i, sizeof (def_file_import) * (fdef->num_imports - pos));
  763. fill_in_import (i, name, def_stash_module (fdef, module), ordinal,
  764. internal_name, its_name);
  765. fdef->num_imports++;
  766. return i;
  767. }
  768. int
  769. def_file_add_import_from (def_file *fdef,
  770. int num_imports,
  771. const char *name,
  772. const char *module,
  773. int ordinal,
  774. const char *internal_name,
  775. const char *its_name ATTRIBUTE_UNUSED)
  776. {
  777. def_file_import *i;
  778. int is_dup;
  779. int pos;
  780. int max_imports = ROUND_UP (fdef->num_imports, 16);
  781. /* We need to avoid here duplicates. */
  782. is_dup = 0;
  783. pos = find_import_in_list (fdef->imports, fdef->num_imports,
  784. name, internal_name ? internal_name : name,
  785. module, ordinal, &is_dup);
  786. if (is_dup != 0)
  787. return -1;
  788. if (fdef->imports && pos != fdef->num_imports)
  789. {
  790. i = fdef->imports + pos;
  791. if (i->module && strcmp (i->module->name, module) == 0)
  792. return -1;
  793. }
  794. if (fdef->num_imports + num_imports - 1 >= max_imports)
  795. {
  796. max_imports = ROUND_UP (fdef->num_imports + num_imports, 16);
  797. if (fdef->imports)
  798. fdef->imports = xrealloc (fdef->imports,
  799. max_imports * sizeof (def_file_import));
  800. else
  801. fdef->imports = xmalloc (max_imports * sizeof (def_file_import));
  802. }
  803. i = fdef->imports + pos;
  804. if (pos != fdef->num_imports)
  805. memmove (i + num_imports, i,
  806. sizeof (def_file_import) * (fdef->num_imports - pos));
  807. return pos;
  808. }
  809. def_file_import *
  810. def_file_add_import_at (def_file *fdef,
  811. int pos,
  812. const char *name,
  813. const char *module,
  814. int ordinal,
  815. const char *internal_name,
  816. const char *its_name)
  817. {
  818. def_file_import *i = fdef->imports + pos;
  819. fill_in_import (i, name, def_stash_module (fdef, module), ordinal,
  820. internal_name, its_name);
  821. fdef->num_imports++;
  822. return i;
  823. }
  824. struct
  825. {
  826. char *param;
  827. int token;
  828. }
  829. diropts[] =
  830. {
  831. { "-heap", HEAPSIZE },
  832. { "-stack", STACKSIZE_K },
  833. { "-attr", SECTIONS },
  834. { "-export", EXPORTS },
  835. { "-aligncomm", ALIGNCOMM },
  836. { 0, 0 }
  837. };
  838. void
  839. def_file_add_directive (def_file *my_def, const char *param, int len)
  840. {
  841. def_file *save_def = def;
  842. const char *pend = param + len;
  843. char * tend = (char *) param;
  844. int i;
  845. def = my_def;
  846. while (param < pend)
  847. {
  848. while (param < pend
  849. && (ISSPACE (*param) || *param == '\n' || *param == 0))
  850. param++;
  851. if (param == pend)
  852. break;
  853. /* Scan forward until we encounter any of:
  854. - the end of the buffer
  855. - the start of a new option
  856. - a newline separating options
  857. - a NUL separating options. */
  858. for (tend = (char *) (param + 1);
  859. (tend < pend
  860. && !(ISSPACE (tend[-1]) && *tend == '-')
  861. && *tend != '\n' && *tend != 0);
  862. tend++)
  863. ;
  864. for (i = 0; diropts[i].param; i++)
  865. {
  866. len = strlen (diropts[i].param);
  867. if (tend - param >= len
  868. && strncmp (param, diropts[i].param, len) == 0
  869. && (param[len] == ':' || param[len] == ' '))
  870. {
  871. lex_parse_string_end = tend;
  872. lex_parse_string = param + len + 1;
  873. lex_forced_token = diropts[i].token;
  874. saw_newline = 0;
  875. if (def_parse ())
  876. continue;
  877. break;
  878. }
  879. }
  880. if (!diropts[i].param)
  881. {
  882. if (tend < pend)
  883. {
  884. char saved;
  885. saved = * tend;
  886. * tend = 0;
  887. /* xgettext:c-format */
  888. einfo (_("Warning: .drectve `%s' unrecognized\n"), param);
  889. * tend = saved;
  890. }
  891. else
  892. {
  893. einfo (_("Warning: corrupt .drectve at end of def file\n"));
  894. }
  895. }
  896. lex_parse_string = 0;
  897. param = tend;
  898. }
  899. def = save_def;
  900. def_pool_free ();
  901. }
  902. /* Parser Callbacks. */
  903. static void
  904. def_image_name (const char *name, bfd_vma base, int is_dll)
  905. {
  906. /* If a LIBRARY or NAME statement is specified without a name, there is nothing
  907. to do here. We retain the output filename specified on command line. */
  908. if (*name)
  909. {
  910. const char* image_name = lbasename (name);
  911. if (image_name != name)
  912. einfo ("%s:%d: Warning: path components stripped from %s, '%s'\n",
  913. def_filename, linenumber, is_dll ? "LIBRARY" : "NAME",
  914. name);
  915. free (def->name);
  916. /* Append the default suffix, if none specified. */
  917. if (strchr (image_name, '.') == 0)
  918. {
  919. const char * suffix = is_dll ? ".dll" : ".exe";
  920. def->name = xmalloc (strlen (image_name) + strlen (suffix) + 1);
  921. sprintf (def->name, "%s%s", image_name, suffix);
  922. }
  923. else
  924. def->name = xstrdup (image_name);
  925. }
  926. /* Honor a BASE address statement, even if LIBRARY string is empty. */
  927. def->base_address = base;
  928. def->is_dll = is_dll;
  929. }
  930. static void
  931. def_description (const char *text)
  932. {
  933. int len = def->description ? strlen (def->description) : 0;
  934. len += strlen (text) + 1;
  935. if (def->description)
  936. {
  937. def->description = xrealloc (def->description, len);
  938. strcat (def->description, text);
  939. }
  940. else
  941. {
  942. def->description = xmalloc (len);
  943. strcpy (def->description, text);
  944. }
  945. }
  946. static void
  947. def_stacksize (int reserve, int commit)
  948. {
  949. def->stack_reserve = reserve;
  950. def->stack_commit = commit;
  951. }
  952. static void
  953. def_heapsize (int reserve, int commit)
  954. {
  955. def->heap_reserve = reserve;
  956. def->heap_commit = commit;
  957. }
  958. static void
  959. def_section (const char *name, int attr)
  960. {
  961. def_file_section *s;
  962. int max_sections = ROUND_UP (def->num_section_defs, 4);
  963. if (def->num_section_defs >= max_sections)
  964. {
  965. max_sections = ROUND_UP (def->num_section_defs+1, 4);
  966. if (def->section_defs)
  967. def->section_defs = xrealloc (def->section_defs,
  968. max_sections * sizeof (def_file_import));
  969. else
  970. def->section_defs = xmalloc (max_sections * sizeof (def_file_import));
  971. }
  972. s = def->section_defs + def->num_section_defs;
  973. memset (s, 0, sizeof (def_file_section));
  974. s->name = xstrdup (name);
  975. if (attr & 1)
  976. s->flag_read = 1;
  977. if (attr & 2)
  978. s->flag_write = 1;
  979. if (attr & 4)
  980. s->flag_execute = 1;
  981. if (attr & 8)
  982. s->flag_shared = 1;
  983. def->num_section_defs++;
  984. }
  985. static void
  986. def_section_alt (const char *name, const char *attr)
  987. {
  988. int aval = 0;
  989. for (; *attr; attr++)
  990. {
  991. switch (*attr)
  992. {
  993. case 'R':
  994. case 'r':
  995. aval |= 1;
  996. break;
  997. case 'W':
  998. case 'w':
  999. aval |= 2;
  1000. break;
  1001. case 'X':
  1002. case 'x':
  1003. aval |= 4;
  1004. break;
  1005. case 'S':
  1006. case 's':
  1007. aval |= 8;
  1008. break;
  1009. }
  1010. }
  1011. def_section (name, aval);
  1012. }
  1013. static void
  1014. def_exports (const char *external_name,
  1015. const char *internal_name,
  1016. int ordinal,
  1017. int flags,
  1018. const char *its_name)
  1019. {
  1020. def_file_export *dfe;
  1021. int is_dup = 0;
  1022. if (!internal_name && external_name)
  1023. internal_name = external_name;
  1024. #if TRACE
  1025. printf ("def_exports, ext=%s int=%s\n", external_name, internal_name);
  1026. #endif
  1027. dfe = def_file_add_export (def, external_name, internal_name, ordinal,
  1028. its_name, &is_dup);
  1029. /* We might check here for flag redefinition and warn. For now we
  1030. ignore duplicates silently. */
  1031. if (is_dup)
  1032. return;
  1033. if (flags & 1)
  1034. dfe->flag_noname = 1;
  1035. if (flags & 2)
  1036. dfe->flag_constant = 1;
  1037. if (flags & 4)
  1038. dfe->flag_data = 1;
  1039. if (flags & 8)
  1040. dfe->flag_private = 1;
  1041. }
  1042. static void
  1043. def_import (const char *internal_name,
  1044. const char *module,
  1045. const char *dllext,
  1046. const char *name,
  1047. int ordinal,
  1048. const char *its_name)
  1049. {
  1050. char *buf = 0;
  1051. const char *ext = dllext ? dllext : "dll";
  1052. int is_dup = 0;
  1053. buf = xmalloc (strlen (module) + strlen (ext) + 2);
  1054. sprintf (buf, "%s.%s", module, ext);
  1055. module = buf;
  1056. def_file_add_import (def, name, module, ordinal, internal_name, its_name,
  1057. &is_dup);
  1058. free (buf);
  1059. }
  1060. static void
  1061. def_version (int major, int minor)
  1062. {
  1063. def->version_major = major;
  1064. def->version_minor = minor;
  1065. }
  1066. static void
  1067. def_directive (char *str)
  1068. {
  1069. struct directive *d = xmalloc (sizeof (struct directive));
  1070. d->next = directives;
  1071. directives = d;
  1072. d->name = xstrdup (str);
  1073. d->len = strlen (str);
  1074. }
  1075. static void
  1076. def_aligncomm (char *str, int align)
  1077. {
  1078. def_file_aligncomm *c, *p;
  1079. p = NULL;
  1080. c = def->aligncomms;
  1081. while (c != NULL)
  1082. {
  1083. int e = strcmp (c->symbol_name, str);
  1084. if (!e)
  1085. {
  1086. /* Not sure if we want to allow here duplicates with
  1087. different alignments, but for now we keep them. */
  1088. e = (int) c->alignment - align;
  1089. if (!e)
  1090. return;
  1091. }
  1092. if (e > 0)
  1093. break;
  1094. c = (p = c)->next;
  1095. }
  1096. c = xmalloc (sizeof (def_file_aligncomm));
  1097. c->symbol_name = xstrdup (str);
  1098. c->alignment = (unsigned int) align;
  1099. if (!p)
  1100. {
  1101. c->next = def->aligncomms;
  1102. def->aligncomms = c;
  1103. }
  1104. else
  1105. {
  1106. c->next = p->next;
  1107. p->next = c;
  1108. }
  1109. }
  1110. static void
  1111. def_error (const char *err)
  1112. {
  1113. einfo ("%P: %s:%d: %s\n",
  1114. def_filename ? def_filename : "<unknown-file>", linenumber, err);
  1115. }
  1116. /* Lexical Scanner. */
  1117. #undef TRACE
  1118. #define TRACE 0
  1119. /* Never freed, but always reused as needed, so no real leak. */
  1120. static char *buffer = 0;
  1121. static int buflen = 0;
  1122. static int bufptr = 0;
  1123. static void
  1124. put_buf (char c)
  1125. {
  1126. if (bufptr == buflen)
  1127. {
  1128. buflen += 50; /* overly reasonable, eh? */
  1129. if (buffer)
  1130. buffer = xrealloc (buffer, buflen + 1);
  1131. else
  1132. buffer = xmalloc (buflen + 1);
  1133. }
  1134. buffer[bufptr++] = c;
  1135. buffer[bufptr] = 0; /* not optimal, but very convenient. */
  1136. }
  1137. static struct
  1138. {
  1139. char *name;
  1140. int token;
  1141. }
  1142. tokens[] =
  1143. {
  1144. { "BASE", BASE },
  1145. { "CODE", CODE },
  1146. { "CONSTANT", CONSTANTU },
  1147. { "constant", CONSTANTL },
  1148. { "DATA", DATAU },
  1149. { "data", DATAL },
  1150. { "DESCRIPTION", DESCRIPTION },
  1151. { "DIRECTIVE", DIRECTIVE },
  1152. { "EXECUTE", EXECUTE },
  1153. { "EXPORTS", EXPORTS },
  1154. { "HEAPSIZE", HEAPSIZE },
  1155. { "IMPORTS", IMPORTS },
  1156. { "LIBRARY", LIBRARY },
  1157. { "NAME", NAME },
  1158. { "NONAME", NONAMEU },
  1159. { "noname", NONAMEL },
  1160. { "PRIVATE", PRIVATEU },
  1161. { "private", PRIVATEL },
  1162. { "READ", READ },
  1163. { "SECTIONS", SECTIONS },
  1164. { "SEGMENTS", SECTIONS },
  1165. { "SHARED", SHARED_K },
  1166. { "STACKSIZE", STACKSIZE_K },
  1167. { "VERSION", VERSIONK },
  1168. { "WRITE", WRITE },
  1169. { 0, 0 }
  1170. };
  1171. static int
  1172. def_getc (void)
  1173. {
  1174. int rv;
  1175. if (lex_parse_string)
  1176. {
  1177. if (lex_parse_string >= lex_parse_string_end)
  1178. rv = EOF;
  1179. else
  1180. rv = *lex_parse_string++;
  1181. }
  1182. else
  1183. {
  1184. rv = fgetc (the_file);
  1185. }
  1186. if (rv == '\n')
  1187. saw_newline = 1;
  1188. return rv;
  1189. }
  1190. static int
  1191. def_ungetc (int c)
  1192. {
  1193. if (lex_parse_string)
  1194. {
  1195. lex_parse_string--;
  1196. return c;
  1197. }
  1198. else
  1199. return ungetc (c, the_file);
  1200. }
  1201. static int
  1202. def_lex (void)
  1203. {
  1204. int c, i, q;
  1205. if (lex_forced_token)
  1206. {
  1207. i = lex_forced_token;
  1208. lex_forced_token = 0;
  1209. #if TRACE
  1210. printf ("lex: forcing token %d\n", i);
  1211. #endif
  1212. return i;
  1213. }
  1214. c = def_getc ();
  1215. /* Trim leading whitespace. */
  1216. while (c != EOF && (c == ' ' || c == '\t') && saw_newline)
  1217. c = def_getc ();
  1218. if (c == EOF)
  1219. {
  1220. #if TRACE
  1221. printf ("lex: EOF\n");
  1222. #endif
  1223. return 0;
  1224. }
  1225. if (saw_newline && c == ';')
  1226. {
  1227. do
  1228. {
  1229. c = def_getc ();
  1230. }
  1231. while (c != EOF && c != '\n');
  1232. if (c == '\n')
  1233. return def_lex ();
  1234. return 0;
  1235. }
  1236. /* Must be something else. */
  1237. saw_newline = 0;
  1238. if (ISDIGIT (c))
  1239. {
  1240. bufptr = 0;
  1241. while (c != EOF && (ISXDIGIT (c) || (c == 'x')))
  1242. {
  1243. put_buf (c);
  1244. c = def_getc ();
  1245. }
  1246. if (c != EOF)
  1247. def_ungetc (c);
  1248. yylval.digits = def_pool_strdup (buffer);
  1249. #if TRACE
  1250. printf ("lex: `%s' returns DIGITS\n", buffer);
  1251. #endif
  1252. return DIGITS;
  1253. }
  1254. if (ISALPHA (c) || strchr ("$:-_?@", c))
  1255. {
  1256. bufptr = 0;
  1257. q = c;
  1258. put_buf (c);
  1259. c = def_getc ();
  1260. if (q == '@')
  1261. {
  1262. if (ISBLANK (c) ) /* '@' followed by whitespace. */
  1263. return (q);
  1264. else if (ISDIGIT (c)) /* '@' followed by digit. */
  1265. {
  1266. def_ungetc (c);
  1267. return (q);
  1268. }
  1269. #if TRACE
  1270. printf ("lex: @ returns itself\n");
  1271. #endif
  1272. }
  1273. while (c != EOF && (ISALNUM (c) || strchr ("$:-_?/@<>", c)))
  1274. {
  1275. put_buf (c);
  1276. c = def_getc ();
  1277. }
  1278. if (c != EOF)
  1279. def_ungetc (c);
  1280. if (ISALPHA (q)) /* Check for tokens. */
  1281. {
  1282. for (i = 0; tokens[i].name; i++)
  1283. if (strcmp (tokens[i].name, buffer) == 0)
  1284. {
  1285. #if TRACE
  1286. printf ("lex: `%s' is a string token\n", buffer);
  1287. #endif
  1288. return tokens[i].token;
  1289. }
  1290. }
  1291. #if TRACE
  1292. printf ("lex: `%s' returns ID\n", buffer);
  1293. #endif
  1294. yylval.id = def_pool_strdup (buffer);
  1295. return ID;
  1296. }
  1297. if (c == '\'' || c == '"')
  1298. {
  1299. q = c;
  1300. c = def_getc ();
  1301. bufptr = 0;
  1302. while (c != EOF && c != q)
  1303. {
  1304. put_buf (c);
  1305. c = def_getc ();
  1306. }
  1307. yylval.id = def_pool_strdup (buffer);
  1308. #if TRACE
  1309. printf ("lex: `%s' returns ID\n", buffer);
  1310. #endif
  1311. return ID;
  1312. }
  1313. if ( c == '=')
  1314. {
  1315. c = def_getc ();
  1316. if (c == '=')
  1317. {
  1318. #if TRACE
  1319. printf ("lex: `==' returns EQUAL\n");
  1320. #endif
  1321. return EQUAL;
  1322. }
  1323. def_ungetc (c);
  1324. #if TRACE
  1325. printf ("lex: `=' returns itself\n");
  1326. #endif
  1327. return '=';
  1328. }
  1329. if (c == '.' || c == ',')
  1330. {
  1331. #if TRACE
  1332. printf ("lex: `%c' returns itself\n", c);
  1333. #endif
  1334. return c;
  1335. }
  1336. if (c == '\n')
  1337. {
  1338. linenumber++;
  1339. saw_newline = 1;
  1340. }
  1341. /*printf ("lex: 0x%02x ignored\n", c); */
  1342. return def_lex ();
  1343. }
  1344. static char *
  1345. def_pool_alloc (size_t sz)
  1346. {
  1347. def_pool_str *e;
  1348. e = (def_pool_str *) xmalloc (sizeof (def_pool_str) + sz);
  1349. e->next = pool_strs;
  1350. pool_strs = e;
  1351. return e->data;
  1352. }
  1353. static char *
  1354. def_pool_strdup (const char *str)
  1355. {
  1356. char *s;
  1357. size_t len;
  1358. if (!str)
  1359. return NULL;
  1360. len = strlen (str) + 1;
  1361. s = def_pool_alloc (len);
  1362. memcpy (s, str, len);
  1363. return s;
  1364. }
  1365. static void
  1366. def_pool_free (void)
  1367. {
  1368. def_pool_str *p;
  1369. while ((p = pool_strs) != NULL)
  1370. {
  1371. pool_strs = p->next;
  1372. free (p);
  1373. }
  1374. }