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