aboutsummaryrefslogtreecommitdiff
path: root/ld/deffilep.y
blob: c0f350b35746ca7a1af96993952b3e59f3bb87fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
%{ /* deffilep.y - parser for .def files */

/*   Copyright (C) 1995, 1997, 1998 Free Software Foundation, Inc.

This file is part of GNU Binutils.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

#include <stdio.h>
#include <ctype.h>
#include "libiberty.h"
#include "bfd.h"
#include "ld.h"
#include "deffile.h"

#define TRACE 0

/* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
   as well as gratuitiously global symbol names, so we can have multiple
   yacc generated parsers in ld.  Note that these are only the variables
   produced by yacc.  If other parser generators (bison, byacc, etc) produce
   additional global names that conflict at link time, then those parser
   generators need to be fixed instead of adding those names to this list. */

#define	yymaxdepth def_maxdepth
#define	yyparse	def_parse
#define	yylex	def_lex
#define	yyerror	def_error
#define	yylval	def_lval
#define	yychar	def_char
#define	yydebug	def_debug
#define	yypact	def_pact	
#define	yyr1	def_r1			
#define	yyr2	def_r2			
#define	yydef	def_def		
#define	yychk	def_chk		
#define	yypgo	def_pgo		
#define	yyact	def_act		
#define	yyexca	def_exca
#define yyerrflag def_errflag
#define yynerrs	def_nerrs
#define	yyps	def_ps
#define	yypv	def_pv
#define	yys	def_s
#define	yy_yys	def_yys
#define	yystate	def_state
#define	yytmp	def_tmp
#define	yyv	def_v
#define	yy_yyv	def_yyv
#define	yyval	def_val
#define	yylloc	def_lloc
#define yyreds	def_reds		/* With YYDEBUG defined */
#define yytoks	def_toks		/* With YYDEBUG defined */
#define yylhs	def_yylhs
#define yylen	def_yylen
#define yydefred def_yydefred
#define yydgoto	def_yydgoto
#define yysindex def_yysindex
#define yyrindex def_yyrindex
#define yygindex def_yygindex
#define yytable	 def_yytable
#define yycheck	 def_yycheck

static int def_lex ();

static void def_description PARAMS ((const char *));
static void def_exports PARAMS ((const char *, const char *, int, int));
static void def_heapsize PARAMS ((int, int));
static void def_import
  PARAMS ((const char *, const char *, const char *, const char *, int));
static void def_library PARAMS ((const char *, int));
static void def_name PARAMS ((const char *, int));
static void def_section PARAMS ((const char *, int));
static void def_section_alt PARAMS ((const char *, const char *));
static void def_stacksize PARAMS ((int, int));
static void def_version PARAMS ((int, int));
static void def_directive PARAMS ((char *));
static int def_parse PARAMS ((void));
static int def_error PARAMS ((const char *));
static int def_debug;
static int def_lex PARAMS ((void));

static int lex_forced_token = 0;
static const char *lex_parse_string = 0;
static const char *lex_parse_string_end = 0;

%}

%union {
  char *id;
  int number;
};

%token NAME, LIBRARY, DESCRIPTION, STACKSIZE, HEAPSIZE, CODE, DATA
%token SECTIONS, EXPORTS, IMPORTS, VERSIONK, BASE, CONSTANT, PRIVATE
%token READ WRITE EXECUTE SHARED NONAME DIRECTIVE
%token <id> ID
%token <number> NUMBER
%type  <number> opt_base opt_ordinal
%type  <number> attr attr_list opt_number exp_opt_list exp_opt
%type  <id> opt_name opt_equal_name 

%%

start: start command
	| command
	;

command: 
		NAME opt_name opt_base { def_name ($2, $3); }
	|	LIBRARY opt_name opt_base { def_library ($2, $3); }
	|	DESCRIPTION ID { def_description ($2);}
	|	STACKSIZE NUMBER opt_number { def_stacksize ($2, $3);}
	|	HEAPSIZE NUMBER opt_number { def_heapsize ($2, $3);}
	|	CODE attr_list { def_section ("CODE", $2);}
	|	DATA attr_list  { def_section ("DATA", $2);}
	|	SECTIONS seclist
	|	EXPORTS explist 
	|	IMPORTS implist
	|	VERSIONK NUMBER { def_version ($2, 0);}
	|	VERSIONK NUMBER '.' NUMBER { def_version ($2, $4);}
	|	DIRECTIVE ID { def_directive ($2);}
	;


explist:
		/* EMPTY */
	|	expline
	|	explist expline
	;

expline:
		ID opt_equal_name opt_ordinal exp_opt_list
			{ def_exports ($1, $2, $3, $4); }
	;
exp_opt_list:
		exp_opt exp_opt_list { $$ = $1 | $2; }
	|	{ $$ = 0; }
	;
exp_opt:
		NONAME		{ $$ = 1; }
	|	CONSTANT	{ $$ = 2; }
	|	DATA		{ $$ = 4; }
	|	PRIVATE		{ $$ = 8; }
	;
implist:	
		implist impline
	|	impline
	;

impline:
               ID '=' ID '.' ID '.' ID     { def_import ($1, $3, $5, $7, -1); }
       |       ID '=' ID '.' ID '.' NUMBER { def_import ($1, $3, $5,  0, $7); }
       |       ID '=' ID '.' ID            { def_import ($1, $3,  0, $5, -1); }
       |       ID '=' ID '.' NUMBER        { def_import ($1, $3,  0,  0, $5); }
       |       ID '.' ID '.' ID            { def_import ( 0, $1, $3, $5, -1); }
       |       ID '.' ID                   { def_import ( 0, $1,  0, $3, -1); }
;

seclist:
		seclist secline
	|	secline
	;

secline:
	ID attr_list { def_section ($1, $2);}
	| ID ID { def_section_alt ($1, $2);}
	;

attr_list:
	attr_list opt_comma attr { $$ = $1 | $3; }
	| attr { $$ = $1; }
	;

opt_comma:
	','
	| 
	;
opt_number: ',' NUMBER { $$=$2;}
	|	   { $$=-1;}
	;
	
attr:
		READ	{ $$ = 1;}
	|	WRITE	{ $$ = 2;}	
	|	EXECUTE	{ $$=4;}
	|	SHARED	{ $$=8;}
	;

opt_name: ID		{ $$ = $1; }
	|		{ $$ = 0; }
	;

opt_ordinal: 
	  '@' NUMBER     { $$ = $2;}
	|                { $$ = -1;}
	;

opt_equal_name:
          '=' ID	{ $$ = $2; }
        | 		{ $$ =  0; }			 
	;

opt_base: BASE	'=' NUMBER	{ $$ = $3;}
	|	{ $$ = 0;}
	;

	

%%

/*****************************************************************************
 API
 *****************************************************************************/

static FILE *the_file;
static const char *def_filename;
static int linenumber;
static def_file *def;
static int max_exports, max_imports, max_sections;
static int saw_newline;

struct directive
  {
    struct directive *next;
    char *name;
    int len;
  };

static struct directive *directives = 0;

def_file *
def_file_empty ()
{
  def_file *rv = (def_file *) xmalloc (sizeof (def_file));
  memset (rv, 0, sizeof (def_file));
  max_exports = max_imports = max_sections = 0;
  rv->is_dll = -1;
  rv->base_address = (bfd_vma) (-1);
  rv->stack_reserve = rv->stack_commit = -1;
  rv->heap_reserve = rv->heap_commit = -1;
  rv->version_major = rv->version_minor = -1;
  return rv;
}

def_file *
def_file_parse (filename, add_to)
     const char *filename;
     def_file *add_to;
{
  struct directive *d;

  the_file = fopen (filename, "r");
  def_filename = filename;
  linenumber = 1;
  if (!the_file)
    {
      perror (filename);
      return 0;
    }
  if (add_to)
    {
      def = add_to;
      max_exports = def->num_exports;
      max_imports = def->num_imports;
      max_sections = def->num_section_defs;
    }
  else
    {
      def = def_file_empty ();
    }

  saw_newline = 1;
  if (def_parse ())
    {
      def_file_free (def);
      fclose (the_file);
      return 0;
    }

  fclose (the_file);

  for (d = directives; d; d = d->next)
    {
#if TRACE
      printf ("Adding directive `%s'\n", d->name);
#endif
      def_file_add_directive (def, d->name, d->len);
    }

  return def;
}

void
def_file_free (def)
     def_file *def;
{
  int i;
  if (!def)
    return;
  if (def->name)
    free (def->name);
  if (def->description)
    free (def->description);

  if (def->section_defs)
    {
      for (i = 0; i < def->num_section_defs; i++)
	{
	  if (def->section_defs[i].name)
	    free (def->section_defs[i].name);
	  if (def->section_defs[i].class)
	    free (def->section_defs[i].class);
	}
      free (def->section_defs);
    }

  if (def->exports)
    {
      for (i = 0; i < def->num_exports; i++)
	{
	  if (def->exports[i].internal_name
	      && def->exports[i].internal_name != def->exports[i].name)
	    free (def->exports[i].internal_name);
	  if (def->exports[i].name)
	    free (def->exports[i].name);
	}
      free (def->exports);
    }

  if (def->imports)
    {
      for (i = 0; i < def->num_imports; i++)
	{
	  if (def->imports[i].internal_name
	      && def->imports[i].internal_name != def->imports[i].name)
	    free (def->imports[i].internal_name);
	  if (def->imports[i].name)
	    free (def->imports[i].name);
	  if (def->imports[i].module)
	    free (def->imports[i].module);
	}
      free (def->imports);
    }

  free (def);
}

#ifdef DEF_FILE_PRINT
void
def_file_print (file, def)
     FILE *file;
     def_file *def;
{
  int i;
  fprintf (file, ">>>> def_file at 0x%08x\n", def);
  if (def->name)
    fprintf (file, "  name: %s\n", def->name ? def->name : "(unspecified)");
  if (def->is_dll != -1)
    fprintf (file, "  is dll: %s\n", def->is_dll ? "yes" : "no");
  if (def->base_address != (bfd_vma) (-1))
    fprintf (file, "  base address: 0x%08x\n", def->base_address);
  if (def->description)
    fprintf (file, "  description: `%s'\n", def->description);
  if (def->stack_reserve != -1)
    fprintf (file, "  stack reserve: 0x%08x\n", def->stack_reserve);
  if (def->stack_commit != -1)
    fprintf (file, "  stack commit: 0x%08x\n", def->stack_commit);
  if (def->heap_reserve != -1)
    fprintf (file, "  heap reserve: 0x%08x\n", def->heap_reserve);
  if (def->heap_commit != -1)
    fprintf (file, "  heap commit: 0x%08x\n", def->heap_commit);

  if (def->num_section_defs > 0)
    {
      fprintf (file, "  section defs:\n");
      for (i = 0; i < def->num_section_defs; i++)
	{
	  fprintf (file, "    name: `%s', class: `%s', flags:",
		   def->section_defs[i].name, def->section_defs[i].class);
	  if (def->section_defs[i].flag_read)
	    fprintf (file, " R");
	  if (def->section_defs[i].flag_write)
	    fprintf (file, " W");
	  if (def->section_defs[i].flag_execute)
	    fprintf (file, " X");
	  if (def->section_defs[i].flag_shared)
	    fprintf (file, " S");
	  fprintf (file, "\n");
	}
    }

  if (def->num_exports > 0)
    {
      fprintf (file, "  exports:\n");
      for (i = 0; i < def->num_exports; i++)
	{
	  fprintf (file, "    name: `%s', int: `%s', ordinal: %d, flags:",
		   def->exports[i].name, def->exports[i].internal_name,
		   def->exports[i].ordinal);
	  if (def->exports[i].flag_private)
	    fprintf (file, " P");
	  if (def->exports[i].flag_constant)
	    fprintf (file, " C");
	  if (def->exports[i].flag_noname)
	    fprintf (file, " N");
	  if (def->exports[i].flag_data)
	    fprintf (file, " D");
	  fprintf (file, "\n");
	}
    }

  if (def->num_imports > 0)
    {
      fprintf (file, "  imports:\n");
      for (i = 0; i < def->num_imports; i++)
	{
	  fprintf (file, "    int: %s, from: `%s', name: `%s', ordinal: %d\n",
		   def->imports[i].internal_name,
		   def->imports[i].module,
		   def->imports[i].name,
		   def->imports[i].ordinal);
	}
    }
  if (def->version_major != -1)
    fprintf (file, "  version: %d.%d\n", def->version_major, def->version_minor);
  fprintf (file, "<<<< def_file at 0x%08x\n", def);
}
#endif

def_file_export *
def_file_add_export (def, external_name, internal_name, ordinal)
     def_file *def;
     const char *external_name;
     const char *internal_name;
     int ordinal;
{
  def_file_export *e;
  if (def->num_exports >= max_exports)
    {
      max_exports = def->num_exports + 50;
      if (def->exports)
	def->exports = (def_file_export *) xrealloc (def->exports, max_exports * sizeof (def_file_export));
      else
	def->exports = (def_file_export *) xmalloc (max_exports * sizeof (def_file_export));
    }
  e = def->exports + def->num_exports;
  memset (e, 0, sizeof (def_file_export));
  if (internal_name && !external_name)
    external_name = internal_name;
  if (external_name && !internal_name)
    internal_name = external_name;
  e->name = xstrdup (external_name);
  e->internal_name = xstrdup (internal_name);
  e->ordinal = ordinal;
  def->num_exports++;
  return e;
}

def_file_import *
def_file_add_import (def, name, module, ordinal, internal_name)
     def_file *def;
     const char *name;
     const char *module;
     int ordinal;
     const char *internal_name;
{
  def_file_import *i;
  if (def->num_imports >= max_imports)
    {
      max_imports = def->num_imports + 50;
      if (def->imports)
	def->imports = (def_file_import *) xrealloc (def->imports, max_imports * sizeof (def_file_import));
      else
	def->imports = (def_file_import *) xmalloc (max_imports * sizeof (def_file_import));
    }
  i = def->imports + def->num_imports;
  memset (i, 0, sizeof (def_file_import));
  if (name)
    i->name = xstrdup (name);
  if (module)
    i->module = xstrdup (module);
  i->ordinal = ordinal;
  if (internal_name)
    i->internal_name = xstrdup (internal_name);
  else
    i->internal_name = i->name;
  def->num_imports++;
  return i;
}

struct
{
  char *param;
  int token;
}
diropts[] =
{
  { "-heap", HEAPSIZE },
  { "-stack", STACKSIZE },
  { "-attr", SECTIONS },
  { "-export", EXPORTS },
  { 0, 0 }
};

int
def_file_add_directive (my_def, param, len)
     def_file *my_def;
     const char *param;
     int len;
{
  def_file *save_def = def;
  const char *pend = param + len;
  const char *tend = param;
  unsigned int sh_reserve, sh_commit;
  int i, j;

  def = my_def;

  while (param < pend)
    {
      while (param < pend && isspace (*param))
	param++;
      for (tend = param + 1;
	   tend < pend && !(isspace (tend[-1]) && *tend == '-');
	   tend++);

      for (i = 0; diropts[i].param; i++)
	{
	  int len = strlen (diropts[i].param);
	  if (tend - param >= len
	      && strncmp (param, diropts[i].param, len) == 0
	      && (param[len] == ':' || param[len] == ' '))
	    {
	      lex_parse_string_end = tend;
	      lex_parse_string = param + len + 1;
	      lex_forced_token = diropts[i].token;
	      saw_newline = 0;
	      def_parse ();
	      break;
	    }
	}

      if (!diropts[i].param)
	{
	  /* xgettext:c-format */
	  einfo (_("Warning: .drectve `%.*s' unrecognized\n"),
		 tend - param, param);
	}
      lex_parse_string = 0;
      param = tend;
    }

  def = save_def;
}

/*****************************************************************************
 Parser Callbacks
 *****************************************************************************/

static void
def_name (name, base)
     const char *name;
     int base;
{
  if (def->name)
    free (def->name);
  def->name = xstrdup (name);
  def->base_address = base;
  def->is_dll = 0;
}

static void
def_library (name, base)
     const char *name;
     int base;
{
  if (def->name)
    free (def->name);
  def->name = xstrdup (name);
  def->base_address = base;
  def->is_dll = 1;
}

static void
def_description (text)
     const char *text;
{
  int len = def->description ? strlen (def->description) : 0;
  len += strlen (text) + 1;
  if (def->description)
    {
      def->description = (char *) xrealloc (def->description, len);
      strcat (def->description, text);
    }
  else
    {
      def->description = (char *) xmalloc (len);
      strcpy (def->description, text);
    }
}

static void
def_stacksize (reserve, commit)
     int reserve;
     int commit;
{
  def->stack_reserve = reserve;
  def->stack_commit = commit;
}

static void
def_heapsize (reserve, commit)
     int reserve;
     int commit;
{
  def->heap_reserve = reserve;
  def->heap_commit = commit;
}

static void
def_section (name, attr)
     const char *name;
     int attr;
{
  def_file_section *s;
  if (def->num_section_defs >= max_sections)
    {
      max_sections = def->num_section_defs + 50;
      if (def->section_defs)
	def->section_defs = (def_file_section *) xrealloc (def->section_defs, max_sections * sizeof (def_file_import));
      else
	def->section_defs = (def_file_section *) xmalloc (max_sections * sizeof (def_file_import));
    }
  s = def->section_defs + def->num_section_defs;
  memset (s, 0, sizeof (def_file_section));
  s->name = xstrdup (name);
  if (attr & 1)
    s->flag_read = 1;
  if (attr & 2)
    s->flag_write = 1;
  if (attr & 4)
    s->flag_execute = 1;
  if (attr & 8)
    s->flag_shared = 1;

  def->num_section_defs++;
}

static void
def_section_alt (name, attr)
     const char *name;
     const char *attr;
{
  int aval = 0;
  for (; *attr; attr++)
    {
      switch (*attr)
	{
	case 'R':
	case 'r':
	  aval |= 1;
	  break;
	case 'W':
	case 'w':
	  aval |= 2;
	  break;
	case 'X':
	case 'x':
	  aval |= 4;
	  break;
	case 'S':
	case 's':
	  aval |= 8;
	  break;
	}
    }
  def_section (name, aval);
}

static void
def_exports (external_name, internal_name, ordinal, flags)
     const char *external_name;
     const char *internal_name;
     int ordinal;
     int flags;
{
  def_file_export *dfe;

  if (!internal_name && external_name)
    internal_name = external_name;
#if TRACE
  printf ("def_exports, ext=%s int=%s\n", external_name, internal_name);
#endif

  dfe = def_file_add_export (def, external_name, internal_name, ordinal);
  if (flags & 1)
    dfe->flag_noname = 1;
  if (flags & 2)
    dfe->flag_constant = 1;
  if (flags & 4)
    dfe->flag_data = 1;
  if (flags & 8)
    dfe->flag_private = 1;
}

static void
def_import (internal_name, module, dllext, name, ordinal)
     const char *internal_name;
     const char *module;
     const char *dllext;
     const char *name;
     int ordinal;
{
  char *buf = 0;

  if (dllext != NULL)
    {
      buf = (char *) xmalloc (strlen (module) + strlen (dllext) + 2);
      sprintf (buf, "%s.%s", module, dllext);
      module = buf;
    }

  def_file_add_import (def, name, module, ordinal, internal_name);
  if (buf)
    free (buf);
}

static void
def_version (major, minor)
     int major;
     int minor;
{
  def->version_major = major;
  def->version_minor = minor;
}

static void
def_directive (str)
     char *str;
{
  struct directive *d = (struct directive *) xmalloc (sizeof (struct directive));
  d->next = directives;
  directives = d;
  d->name = xstrdup (str);
  d->len = strlen (str);
}

static int
def_error (err)
     const char *err;
{
  einfo ("%P: %s:%d: %s\n", def_filename, linenumber, err);

  return 0;
}


/*****************************************************************************
 Lexical Scanner
 *****************************************************************************/

#undef TRACE
#define TRACE 0

/* Never freed, but always reused as needed, so no real leak */
static char *buffer = 0;
static int buflen = 0;
static int bufptr = 0;

static void
put_buf (c)
     char c;
{
  if (bufptr == buflen)
    {
      buflen += 50;		/* overly reasonable, eh? */
      if (buffer)
	buffer = (char *) xrealloc (buffer, buflen + 1);
      else
	buffer = (char *) xmalloc (buflen + 1);
    }
  buffer[bufptr++] = c;
  buffer[bufptr] = 0;		/* not optimal, but very convenient */
}

static struct
{
  char *name;
  int token;
}
tokens[] =
{
  { "BASE", BASE },
  { "CODE", CODE },
  { "CONSTANT", CONSTANT },
  { "DATA", DATA },
  { "DESCRIPTION", DESCRIPTION },
  { "DIRECTIVE", DIRECTIVE },
  { "EXECUTE", EXECUTE },
  { "EXPORTS", EXPORTS },
  { "HEAPSIZE", HEAPSIZE },
  { "IMPORTS", IMPORTS },
  { "LIBRARY", LIBRARY },
  { "NAME", NAME },
  { "NONAME", NONAME },
  { "PRIVATE", PRIVATE },
  { "READ", READ },
  { "SECTIONS", SECTIONS },
  { "SEGMENTS", SECTIONS },
  { "SHARED", SHARED },
  { "STACKSIZE", STACKSIZE },
  { "VERSION", VERSIONK },
  { "WRITE", WRITE },
  { 0, 0 }
};

static int
def_getc ()
{
  int rv;
  if (lex_parse_string)
    {
      if (lex_parse_string >= lex_parse_string_end)
	rv = EOF;
      else
	rv = *lex_parse_string++;
    }
  else
    {
      rv = fgetc (the_file);
    }
  if (rv == '\n')
    saw_newline = 1;
  return rv;
}

static int
def_ungetc (c)
     int c;
{
  if (lex_parse_string)
    lex_parse_string--;
  else
    return ungetc (c, the_file);
}

static int
def_lex ()
{
  int c, i, q;

  if (lex_forced_token)
    {
      i = lex_forced_token;
      lex_forced_token = 0;
#if TRACE
      printf ("lex: forcing token %d\n", i);
#endif
      return i;
    }

  c = def_getc ();

  /* trim leading whitespace */
  while (c != EOF && (c == ' ' || c == '\t') && saw_newline)
    c = def_getc ();

  if (c == EOF)
    {
#if TRACE
      printf ("lex: EOF\n");
#endif
      return 0;
    }

  if (saw_newline && c == ';')
    {
      do
	{
	  c = def_getc ();
	}
      while (c != EOF && c != '\n');
      if (c == '\n')
	return def_lex ();
      return 0;
    }
  /* must be something else */
  saw_newline = 0;

  if (isdigit (c))
    {
      bufptr = 0;
      while (c != EOF && isxdigit (c) || (c == 'x'))
	{
	  put_buf (c);
	  c = def_getc ();
	}
      if (c != EOF)
	def_ungetc (c);
      yylval.number = strtoul (buffer, 0, 0);
#if TRACE
      printf ("lex: `%s' returns NUMBER %d\n", buffer, yylval.number);
#endif
      return NUMBER;
    }

  if (isalpha (c) || strchr ("$:-_?", c))
    {
      bufptr = 0;
      while (c != EOF && isalnum (c) || strchr ("$:-_?/@", c))
	{
	  put_buf (c);
	  c = def_getc ();
	}
      if (c != EOF)
	def_ungetc (c);
      for (i = 0; tokens[i].name; i++)
	if (strcmp (tokens[i].name, buffer) == 0)
	  {
#if TRACE
	    printf ("lex: `%s' is a string token\n", buffer);
#endif
	    return tokens[i].token;
	  }
#if TRACE
      printf ("lex: `%s' returns ID\n", buffer);
#endif
      yylval.id = xstrdup (buffer);
      return ID;
    }

  if (c == '\'' || c == '"')
    {
      q = c;
      c = def_getc ();
      bufptr = 0;
      while (c != EOF && c != q)
	{
	  put_buf (c);
	  c = def_getc ();
	}
      yylval.id = xstrdup (buffer);
#if TRACE
      printf ("lex: `%s' returns ID\n", buffer);
#endif
      return ID;
    }

  if (c == '=' || c == '.' || c == '@' || c == ',')
    {
#if TRACE
      printf ("lex: `%c' returns itself\n", c);
#endif
      return c;
    }

  if (c == '\n')
    {
      linenumber++;
      saw_newline = 1;
    }

  /*printf ("lex: 0x%02x ignored\n", c); */
  return def_lex ();
}