Mon Jun 16 12:49:36 1997 H.J. Lu <hjl@gnu.ai.mit.edu>
[external/binutils.git] / ld / ldlex.l
1 %{
2
3 /* Copyright (C) 1991, 92, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
4
5 This file is part of GLD, the Gnu Linker.
6
7 GLD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GLD 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.
16
17 You should have received a copy of the GNU General Public License
18 along with GLD; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 /*
22 This was written by steve chamberlain
23                     sac@cygnus.com
24 */
25
26
27 #include <ansidecl.h>
28 #include <stdio.h>
29 #include <ctype.h>
30
31 #ifdef MPW
32 /* Prevent enum redefinition problems. */
33 #define TRUE_FALSE_ALREADY_DEFINED
34 #endif /* MPW */
35
36 #include "bfd.h"
37 #include "sysdep.h"
38 #include "ld.h"
39 #include "ldgram.h"
40 #include "ldmisc.h"
41 #include "ldexp.h"
42 #include "ldlang.h"
43 #include "ldfile.h"
44 #include "ldlex.h"
45 #include "ldmain.h"
46
47 /* The type of top-level parser input.
48    yylex and yyparse (indirectly) both check this.  */
49 input_type parser_input;
50
51 /* Radix to use for bfd_scan_vma -- 0 (default to base 10) or 16.  */
52 int hex_mode;
53
54 /* Line number in the current input file.
55    (FIXME Actually, it doesn't appear to get reset for each file?)  */
56 unsigned int lineno = 1;
57
58 /* The string we are currently lexing, or NULL if we are reading a
59    file.  */
60 const char *lex_string = NULL;
61
62 /* Support for flex reading from more than one input file (stream).
63    `include_stack' is flex's input state for each open file;
64    `file_name_stack' is the file names.  `lineno_stack' is the current
65    line numbers.
66
67    If `include_stack_ptr' is 0, we haven't started reading anything yet.
68    Otherwise, stack elements 0 through `include_stack_ptr - 1' are valid.  */
69
70 #undef YY_INPUT
71 #define YY_INPUT(buf,result,max_size) yy_input(buf, &result, max_size)
72
73 #define MAX_INCLUDE_DEPTH 10
74 static YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
75 static const char *file_name_stack[MAX_INCLUDE_DEPTH];
76 static unsigned int lineno_stack[MAX_INCLUDE_DEPTH];
77 static unsigned int include_stack_ptr = 0;
78
79 static YY_BUFFER_STATE yy_create_string_buffer PARAMS ((const char *string,
80                                                         size_t size));
81 static void yy_input PARAMS ((char *, int *result, int max_size));
82
83 static void comment PARAMS ((void));
84 static void lex_warn_invalid PARAMS ((char *where, char *what));
85
86 /* STATES 
87         EXPRESSION      definitely in an expression
88         SCRIPT          definitely in a script
89         BOTH            either EXPRESSION or SCRIPT
90         DEFSYMEXP       in an argument to -defsym
91         MRI             in an MRI script
92         VERS_START      starting a Sun style mapfile
93         VERS_SCRIPT     a Sun style mapfile
94         VERS_NODE       a node within a Sun style mapfile
95 */
96 #define RTOKEN(x)  {  yylval.token = x; return x; }
97
98 /* Some versions of flex want this.  */
99 #ifndef yywrap
100 int yywrap () { return 1; }
101 #endif
102 %}
103
104 %a 4000
105 %o 5000
106
107 CMDFILENAMECHAR   [_a-zA-Z0-9\/\.\\_\+\$\:\[\]\\\,\=\&\!\<\>\-\~]
108 CMDFILENAMECHAR1  [_a-zA-Z0-9\/\.\\_\+\$\:\[\]\\\,\=\&\!\<\>\~]
109 FILENAMECHAR1   [_a-zA-Z\/\.\\\$\_\~]
110 SYMBOLCHARN     [_a-zA-Z\/\.\\\$\_\~0-9]
111 FILENAMECHAR    [_a-zA-Z0-9\/\.\-\_\+\=\$\:\[\]\\\,\~]
112 WILDCHAR        [_a-zA-Z0-9\/\.\-\_\+\=\$\:\[\]\\\,\~\?\*]
113 WHITE           [ \t\n\r]+ 
114
115 NOCFILENAMECHAR [_a-zA-Z0-9\/\.\-\_\+\$\:\[\]\\\~]
116
117 V_TAG [.$_a-zA-Z][._a-zA-Z0-9]*
118 V_IDENTIFIER [*?$_a-zA-Z][*?_a-zA-Z0-9]*
119
120 %s SCRIPT
121 %s EXPRESSION
122 %s BOTH
123 %s DEFSYMEXP
124 %s MRI
125 %s VERS_START
126 %s VERS_SCRIPT
127 %s VERS_NODE
128 %%
129
130   if (parser_input != input_selected)
131     {
132       /* The first token of the input determines the initial parser state.  */
133       input_type t = parser_input;
134       parser_input = input_selected;
135       switch (t)
136         {
137         case input_script: return INPUT_SCRIPT; break;
138         case input_mri_script: return INPUT_MRI_SCRIPT; break;
139         case input_version_script: return INPUT_VERSION_SCRIPT; break;
140         case input_defsym: return INPUT_DEFSYM; break;
141         default: abort ();
142         }
143     }
144
145 <BOTH,SCRIPT,EXPRESSION>"/*"    { comment(); }
146
147
148 <DEFSYMEXP>"-"                  { RTOKEN('-');}
149 <DEFSYMEXP>"+"                  { RTOKEN('+');}
150 <DEFSYMEXP>{FILENAMECHAR1}{SYMBOLCHARN}*   { yylval.name = buystring(yytext); return NAME; }
151 <DEFSYMEXP>"="                  { RTOKEN('='); }
152
153 <MRI,EXPRESSION>"$"([0-9A-Fa-f])+ {
154                                 yylval.integer = bfd_scan_vma (yytext+1, 0,16);
155                                 return INT;
156                         }
157
158 <MRI,EXPRESSION>([0-9A-Fa-f])+(H|h|X|x|B|b|O|o|D|d) {
159                                    int ibase ;
160                                    switch (yytext[yyleng-1]) {
161                                     case 'X': 
162                                     case 'x':
163                                     case 'H':
164                                     case 'h':
165                                      ibase = 16;
166                                      break;
167                                     case 'O':
168                                     case 'o':
169                                      ibase = 8;
170                                      break;
171                                     case 'B':
172                                     case 'b':
173                                      ibase = 2;
174                                      break;
175                                     default:
176                                      ibase = 10;
177                                    }
178                                    yylval.integer = bfd_scan_vma (yytext, 0,
179                                                                   ibase);
180                                    return INT;
181                                  }
182 <SCRIPT,DEFSYMEXP,MRI,BOTH,EXPRESSION>"$"?"0x"?([0-9A-Fa-f])+(M|K|m|k)? {
183                                   yylval.integer = bfd_scan_vma (yytext, 0,
184                                                                  hex_mode);
185                                   if (yytext[yyleng-1]=='M'
186                                         || yytext[yyleng-1] == 'm') {
187                                       yylval.integer *= 1024*1024;
188                                     }   
189                                   if (yytext[yyleng-1]=='K' 
190                                 || yytext[yyleng-1]=='k') {
191                                       yylval.integer *= 1024;
192                                     }           
193                                   return INT;
194                                 }
195 <BOTH,SCRIPT,EXPRESSION,MRI>"]"         { RTOKEN(']');}
196 <BOTH,SCRIPT,EXPRESSION,MRI>"["         { RTOKEN('[');}
197 <BOTH,SCRIPT,EXPRESSION,MRI>"<<="       { RTOKEN(LSHIFTEQ);}
198 <BOTH,SCRIPT,EXPRESSION,MRI>">>="       { RTOKEN(RSHIFTEQ);}
199 <BOTH,SCRIPT,EXPRESSION,MRI>"||"        { RTOKEN(OROR);}
200 <BOTH,SCRIPT,EXPRESSION,MRI>"=="        { RTOKEN(EQ);}
201 <BOTH,SCRIPT,EXPRESSION,MRI>"!="        { RTOKEN(NE);}
202 <BOTH,SCRIPT,EXPRESSION,MRI>">="        { RTOKEN(GE);}
203 <BOTH,SCRIPT,EXPRESSION,MRI>"<="        { RTOKEN(LE);}
204 <BOTH,SCRIPT,EXPRESSION,MRI>"<<"        { RTOKEN(LSHIFT);}
205 <BOTH,SCRIPT,EXPRESSION,MRI>">>"        { RTOKEN(RSHIFT);}
206 <BOTH,SCRIPT,EXPRESSION,MRI>"+="        { RTOKEN(PLUSEQ);}
207 <BOTH,SCRIPT,EXPRESSION,MRI>"-="        { RTOKEN(MINUSEQ);}
208 <BOTH,SCRIPT,EXPRESSION,MRI>"*="        { RTOKEN(MULTEQ);}
209 <BOTH,SCRIPT,EXPRESSION,MRI>"/="        { RTOKEN(DIVEQ);}
210 <BOTH,SCRIPT,EXPRESSION,MRI>"&="        { RTOKEN(ANDEQ);}
211 <BOTH,SCRIPT,EXPRESSION,MRI>"|="        { RTOKEN(OREQ);}
212 <BOTH,SCRIPT,EXPRESSION,MRI>"&&"        { RTOKEN(ANDAND);}
213 <BOTH,SCRIPT,EXPRESSION,MRI>">"         { RTOKEN('>');}
214 <BOTH,SCRIPT,EXPRESSION,MRI>","         { RTOKEN(',');}
215 <BOTH,SCRIPT,EXPRESSION,MRI>"&"         { RTOKEN('&');}
216 <BOTH,SCRIPT,EXPRESSION,MRI>"|"         { RTOKEN('|');}
217 <BOTH,SCRIPT,EXPRESSION,MRI>"~"         { RTOKEN('~');}
218 <BOTH,SCRIPT,EXPRESSION,MRI>"!"         { RTOKEN('!');}
219 <BOTH,SCRIPT,EXPRESSION,MRI>"?"         { RTOKEN('?');}
220 <BOTH,SCRIPT,EXPRESSION,MRI>"*"         { RTOKEN('*');}
221 <BOTH,SCRIPT,EXPRESSION,MRI>"+"         { RTOKEN('+');}
222 <BOTH,SCRIPT,EXPRESSION,MRI>"-"         { RTOKEN('-');}
223 <BOTH,SCRIPT,EXPRESSION,MRI>"/"         { RTOKEN('/');}
224 <BOTH,SCRIPT,EXPRESSION,MRI>"%"         { RTOKEN('%');}
225 <BOTH,SCRIPT,EXPRESSION,MRI>"<"         { RTOKEN('<');}
226 <BOTH,SCRIPT,EXPRESSION,MRI>"="          { RTOKEN('=');}
227 <BOTH,SCRIPT,EXPRESSION,MRI>"}"                 { RTOKEN('}') ; }
228 <BOTH,SCRIPT,EXPRESSION,MRI>"{"                 { RTOKEN('{'); }
229 <BOTH,SCRIPT,EXPRESSION,MRI>")"                 { RTOKEN(')');}
230 <BOTH,SCRIPT,EXPRESSION,MRI>"("                 { RTOKEN('(');}
231 <BOTH,SCRIPT,EXPRESSION,MRI>":"         { RTOKEN(':'); }
232 <BOTH,SCRIPT,EXPRESSION,MRI>";"         { RTOKEN(';');}
233 <BOTH,SCRIPT>"MEMORY"           { RTOKEN(MEMORY);}
234 <BOTH,SCRIPT>"ORIGIN"           { RTOKEN(ORIGIN);}
235 <BOTH,SCRIPT>"VERSION"          { RTOKEN(VERSION);}
236 <EXPRESSION,BOTH,SCRIPT>"BLOCK"         { RTOKEN(BLOCK);}
237 <EXPRESSION,BOTH,SCRIPT>"BIND"          { RTOKEN(BIND);}
238 <BOTH,SCRIPT>"LENGTH"           { RTOKEN(LENGTH);}
239 <EXPRESSION,BOTH,SCRIPT>"ALIGN"                 { RTOKEN(ALIGN_K);}
240 <EXPRESSION,BOTH,SCRIPT>"ADDR"                  { RTOKEN(ADDR);}
241 <EXPRESSION,BOTH,SCRIPT>"LOADADDR"              { RTOKEN(LOADADDR);}
242 <EXPRESSION,BOTH>"MAX"                  { RTOKEN(MAX); }
243 <EXPRESSION,BOTH>"MIN"                  { RTOKEN(MIN); }
244 <BOTH,SCRIPT>"ENTRY"                    { RTOKEN(ENTRY);}
245 <EXPRESSION,BOTH,SCRIPT>"NEXT"                  { RTOKEN(NEXT);}
246 <EXPRESSION,BOTH,SCRIPT>"sizeof_headers"        { RTOKEN(SIZEOF_HEADERS);}
247 <EXPRESSION,BOTH,SCRIPT>"SIZEOF_HEADERS"        { RTOKEN(SIZEOF_HEADERS);}
248 <BOTH,SCRIPT>"MAP"                      { RTOKEN(MAP);}
249 <EXPRESSION,BOTH,SCRIPT>"SIZEOF"                { RTOKEN(SIZEOF);}
250 <BOTH,SCRIPT>"TARGET"           { RTOKEN(TARGET_K);}
251 <BOTH,SCRIPT>"SEARCH_DIR"               { RTOKEN(SEARCH_DIR);}
252 <BOTH,SCRIPT>"OUTPUT"           { RTOKEN(OUTPUT);}
253 <BOTH,SCRIPT>"INPUT"                    { RTOKEN(INPUT);}
254 <EXPRESSION,BOTH,SCRIPT>"GROUP"         { RTOKEN(GROUP);}
255 <EXPRESSION,BOTH,SCRIPT>"DEFINED"               { RTOKEN(DEFINED);}
256 <BOTH,SCRIPT>"CREATE_OBJECT_SYMBOLS"    { RTOKEN(CREATE_OBJECT_SYMBOLS);}
257 <BOTH,SCRIPT>"CONSTRUCTORS"             { RTOKEN( CONSTRUCTORS);}
258 <BOTH,SCRIPT>"FORCE_COMMON_ALLOCATION" { RTOKEN(FORCE_COMMON_ALLOCATION);}
259 <BOTH,SCRIPT>"SECTIONS"         { RTOKEN(SECTIONS);}
260 <BOTH,SCRIPT>"FILL"                     { RTOKEN(FILL);}
261 <BOTH,SCRIPT>"STARTUP"          { RTOKEN(STARTUP);}
262 <BOTH,SCRIPT>"OUTPUT_FORMAT"            { RTOKEN(OUTPUT_FORMAT);}
263 <BOTH,SCRIPT>"OUTPUT_ARCH"              { RTOKEN( OUTPUT_ARCH);}
264 <BOTH,SCRIPT>"HLL"                      { RTOKEN(HLL);}
265 <BOTH,SCRIPT>"SYSLIB"           { RTOKEN(SYSLIB);}
266 <BOTH,SCRIPT>"FLOAT"                    { RTOKEN(FLOAT);}
267 <BOTH,SCRIPT>"QUAD"                     { RTOKEN( QUAD);}
268 <BOTH,SCRIPT>"SQUAD"                    { RTOKEN( SQUAD);}
269 <BOTH,SCRIPT>"LONG"                     { RTOKEN( LONG);}
270 <BOTH,SCRIPT>"SHORT"                    { RTOKEN( SHORT);}
271 <BOTH,SCRIPT>"BYTE"                     { RTOKEN( BYTE);}
272 <BOTH,SCRIPT>"NOFLOAT"          { RTOKEN(NOFLOAT);}
273 <EXPRESSION,BOTH,SCRIPT>"NOCROSSREFS"   { RTOKEN(NOCROSSREFS);}
274 <BOTH,SCRIPT>"OVERLAY"                  { RTOKEN(OVERLAY); }
275 <EXPRESSION,BOTH,SCRIPT>"NOLOAD"        { RTOKEN(NOLOAD);}
276 <EXPRESSION,BOTH,SCRIPT>"DSECT"         { RTOKEN(DSECT);}
277 <EXPRESSION,BOTH,SCRIPT>"COPY"          { RTOKEN(COPY);}
278 <EXPRESSION,BOTH,SCRIPT>"INFO"          { RTOKEN(INFO);}
279 <EXPRESSION,BOTH,SCRIPT>"OVERLAY"       { RTOKEN(OVERLAY);}
280 <BOTH,SCRIPT>"o"                        { RTOKEN(ORIGIN);}
281 <BOTH,SCRIPT>"org"                      { RTOKEN(ORIGIN);}
282 <BOTH,SCRIPT>"l"                        { RTOKEN( LENGTH);}
283 <BOTH,SCRIPT>"len"                      { RTOKEN( LENGTH);}
284 <BOTH,SCRIPT>"INCLUDE"                  { RTOKEN(INCLUDE);}
285 <BOTH,SCRIPT>"PHDRS"                    { RTOKEN (PHDRS); }
286 <EXPRESSION,BOTH,SCRIPT>"AT"                    { RTOKEN(AT);}
287 <EXPRESSION,BOTH,SCRIPT>"PROVIDE"               { RTOKEN(PROVIDE); }
288 <MRI>"#".*\n?\r?                { ++ lineno; }
289 <MRI>"\n"                       { ++ lineno;  RTOKEN(NEWLINE); }
290 <MRI>"\r"                       { ++ lineno;  RTOKEN(NEWLINE); }
291 <MRI>"*".*                      { /* Mri comment line */ }
292 <MRI>";".*                      { /* Mri comment line */ }
293 <MRI>"END"                      { RTOKEN(ENDWORD); }
294 <MRI>"ALIGNMOD"         { RTOKEN(ALIGNMOD);}
295 <MRI>"ALIGN"            { RTOKEN(ALIGN_K);}
296 <MRI>"CHIP"                     { RTOKEN(CHIP); }
297 <MRI>"BASE"                     { RTOKEN(BASE); }
298 <MRI>"ALIAS"                     { RTOKEN(ALIAS); }
299 <MRI>"TRUNCATE"                     { RTOKEN(TRUNCATE); }
300 <MRI>"LOAD"                     { RTOKEN(LOAD); }
301 <MRI>"PUBLIC"                   { RTOKEN(PUBLIC); }
302 <MRI>"ORDER"                    { RTOKEN(ORDER); }
303 <MRI>"NAME"                     { RTOKEN(NAMEWORD); }
304 <MRI>"FORMAT"                   { RTOKEN(FORMAT); }
305 <MRI>"CASE"                     { RTOKEN(CASE); }
306 <MRI>"EXTERN"                   { RTOKEN(EXTERN); }
307 <MRI>"START"                    { RTOKEN(START); }
308 <MRI>"LIST".*                   { RTOKEN(LIST); /* LIST and ignore to end of line */ }
309 <MRI>"SECT"                     { RTOKEN(SECT); }
310 <EXPRESSION,BOTH,SCRIPT,MRI>"ABSOLUTE"                  { RTOKEN(ABSOLUTE); }
311 <MRI>"end"                      { RTOKEN(ENDWORD); }
312 <MRI>"alignmod"         { RTOKEN(ALIGNMOD);}
313 <MRI>"align"            { RTOKEN(ALIGN_K);}
314 <MRI>"chip"                     { RTOKEN(CHIP); }
315 <MRI>"base"                     { RTOKEN(BASE); }
316 <MRI>"alias"                     { RTOKEN(ALIAS); }
317 <MRI>"truncate"                     { RTOKEN(TRUNCATE); }
318 <MRI>"load"                     { RTOKEN(LOAD); }
319 <MRI>"public"                   { RTOKEN(PUBLIC); }
320 <MRI>"order"                    { RTOKEN(ORDER); }
321 <MRI>"name"                     { RTOKEN(NAMEWORD); }
322 <MRI>"format"                   { RTOKEN(FORMAT); }
323 <MRI>"case"                     { RTOKEN(CASE); }
324 <MRI>"extern"                   { RTOKEN(EXTERN); }
325 <MRI>"start"                    { RTOKEN(START); }
326 <MRI>"list".*                   { RTOKEN(LIST); /* LIST and ignore to end of line */ }
327 <MRI>"sect"                     { RTOKEN(SECT); }
328 <EXPRESSION,BOTH,SCRIPT,MRI>"absolute"                  { RTOKEN(ABSOLUTE); }
329
330 <MRI>{FILENAMECHAR1}{NOCFILENAMECHAR}*  {
331 /* Filename without commas, needed to parse mri stuff */
332                                  yylval.name = buystring(yytext); 
333                                   return NAME;
334                                 }
335
336
337 <BOTH,EXPRESSION>{FILENAMECHAR1}{FILENAMECHAR}* {
338                                  yylval.name = buystring(yytext); 
339                                   return NAME;
340                                 }
341 <BOTH,EXPRESSION>"-l"{FILENAMECHAR}+ {
342                                   yylval.name = buystring (yytext + 2);
343                                   return LNAME;
344                                 }
345 <SCRIPT>{WILDCHAR}* { yylval.name = buystring(yytext); return NAME; }
346
347 <EXPRESSION,BOTH,SCRIPT>"\""[^\"]*"\"" {
348                                         /* No matter the state, quotes
349                                            give what's inside */
350                                         yylval.name = buystring(yytext+1);
351                                         yylval.name[yyleng-2] = 0;
352                                         return NAME;
353                                 }
354 <BOTH,SCRIPT,EXPRESSION>"\n"            { lineno++;}
355 <BOTH,SCRIPT,EXPRESSION>"\r"            { lineno++;}
356 <MRI,BOTH,SCRIPT,EXPRESSION>[ \t]
357
358 <VERS_NODE,VERS_SCRIPT>[:,;]    { return *yytext; }
359
360 <VERS_NODE>global               { RTOKEN(GLOBAL); }
361
362 <VERS_NODE>local                { RTOKEN(LOCAL); }
363
364 <VERS_NODE>{V_IDENTIFIER}       { yylval.name = buystring (yytext);
365                                   return VERS_IDENTIFIER; }
366
367 <VERS_SCRIPT>{V_TAG}            { yylval.name = buystring (yytext);
368                                   return VERS_TAG; }
369
370 <VERS_START>"{"                 { BEGIN(VERS_SCRIPT); return *yytext; }
371
372 <VERS_SCRIPT>"{"                { BEGIN(VERS_NODE); return *yytext; }
373 <VERS_SCRIPT,VERS_NODE>"}"      { BEGIN(VERS_SCRIPT); return *yytext; }
374
375 <VERS_START,VERS_NODE,VERS_SCRIPT>[\n\r]        { lineno++; }
376
377 <VERS_START,VERS_NODE,VERS_SCRIPT>#.*           { /* Eat up comments */ }
378
379 <VERS_START,VERS_NODE,VERS_SCRIPT>[ \t]+        { /* Eat up whitespace */ }
380
381 <<EOF>> {
382   include_stack_ptr--;
383     
384   if (include_stack_ptr == 0) 
385   {
386     yyterminate();
387   }
388   else 
389   {
390     yy_switch_to_buffer(include_stack[include_stack_ptr]);
391
392   }
393   BEGIN(SCRIPT);
394   ldfile_input_filename = file_name_stack[include_stack_ptr - 1];
395   lineno = lineno_stack[include_stack_ptr - 1];
396
397   return END;
398 }
399
400 <SCRIPT,MRI,VERS_START,VERS_SCRIPT,VERS_NODE>.  lex_warn_invalid(" in script", yytext);
401 <EXPRESSION,DEFSYMEXP,BOTH>.    lex_warn_invalid(" in expression", yytext);
402     
403 %%
404 \f
405
406 /* Switch flex to reading script file NAME, open on FILE,
407    saving the current input info on the include stack.  */
408
409 void
410 lex_push_file (file, name)
411      FILE *file;
412      const char *name;
413 {
414   if (include_stack_ptr >= MAX_INCLUDE_DEPTH) 
415     {
416       einfo("%F:includes nested too deeply\n");
417     }
418   file_name_stack[include_stack_ptr] = name;
419   lineno_stack[include_stack_ptr] = 1;
420   include_stack[include_stack_ptr] = YY_CURRENT_BUFFER;
421
422   include_stack_ptr++;
423   yyin = file;
424   yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
425   BEGIN (SCRIPT);
426 }
427
428 /* Return a newly created flex input buffer containing STRING,
429    which is SIZE bytes long.  */
430
431 static YY_BUFFER_STATE 
432 yy_create_string_buffer (string, size)
433      CONST char *string;
434      size_t size;
435 {
436   YY_BUFFER_STATE b;
437
438   /* Calls to m-alloc get turned by sed into xm-alloc.  */
439   b = (YY_BUFFER_STATE) malloc (sizeof (struct yy_buffer_state));
440   b->yy_input_file = 0;
441   b->yy_buf_size = size;
442
443   /* yy_ch_buf has to be 2 characters longer than the size given because
444      we need to put in 2 end-of-buffer characters.  */
445   b->yy_ch_buf = (char *) malloc ((unsigned) (b->yy_buf_size + 3));
446
447   b->yy_ch_buf[0] = '\n';
448   strcpy (b->yy_ch_buf+1, string);
449   b->yy_ch_buf[size+1] = YY_END_OF_BUFFER_CHAR;
450   b->yy_ch_buf[size+2] = YY_END_OF_BUFFER_CHAR;
451   b->yy_n_chars = size+1;
452   b->yy_buf_pos = &b->yy_ch_buf[1];
453
454   /* flex 2.4.7 changed the interface.  FIXME: We should not be using
455      a flex internal interface in the first place!  */
456 #ifdef YY_BUFFER_NEW
457   b->yy_buffer_status = YY_BUFFER_NEW;
458 #else
459   b->yy_eof_status = EOF_NOT_SEEN;
460 #endif
461
462   return b;
463 }
464
465 /* Switch flex to reading from STRING, saving the current input info
466    on the include stack.  */
467
468 void
469 lex_redirect (string)
470      CONST char *string;
471 {
472   YY_BUFFER_STATE tmp;
473
474   yy_init = 0;
475   if (include_stack_ptr >= MAX_INCLUDE_DEPTH) 
476     {
477       einfo("%F: macros nested too deeply\n");
478     }
479   file_name_stack[include_stack_ptr] = "redirect";
480   lineno_stack[include_stack_ptr] = 0;
481   include_stack[include_stack_ptr] = YY_CURRENT_BUFFER;
482   include_stack_ptr++;
483   tmp = yy_create_string_buffer (string, strlen (string));
484   yy_switch_to_buffer (tmp);
485   BEGIN (SCRIPT);
486 }
487 \f
488 /* Functions to switch to a different flex start condition,
489    saving the current start condition on `state_stack'.  */
490
491 static int state_stack[MAX_INCLUDE_DEPTH * 2];
492 static int *state_stack_p = state_stack;
493
494 void
495 ldlex_script ()
496 {
497   *(state_stack_p)++ = yy_start;
498   BEGIN (SCRIPT);
499 }
500
501 void
502 ldlex_mri_script ()
503 {
504   *(state_stack_p)++ = yy_start;
505   BEGIN (MRI);
506 }
507
508 void
509 ldlex_version_script ()
510 {
511   *(state_stack_p)++ = yy_start;
512   BEGIN (VERS_START);
513 }
514
515 void
516 ldlex_version_file ()
517 {
518   *(state_stack_p)++ = yy_start;
519   BEGIN (VERS_SCRIPT);
520 }
521
522 void
523 ldlex_defsym ()
524 {
525   *(state_stack_p)++ = yy_start;
526   BEGIN (DEFSYMEXP);
527 }
528            
529 void
530 ldlex_expression ()
531 {
532   *(state_stack_p)++ = yy_start;
533   BEGIN (EXPRESSION);
534 }
535
536 void
537 ldlex_both ()
538 {
539   *(state_stack_p)++ = yy_start;
540   BEGIN (BOTH);
541 }
542
543 void
544 ldlex_popstate ()
545 {
546   yy_start = *(--state_stack_p);
547 }
548 \f
549
550 /* Place up to MAX_SIZE characters in BUF and return in *RESULT
551    either the number of characters read, or 0 to indicate EOF.  */
552
553 static void
554 yy_input (buf, result, max_size)
555      char *buf;
556      int *result;
557      int max_size;
558 {
559   *result = 0; 
560   if (yy_current_buffer->yy_input_file)
561     {
562       if (yyin)
563         {
564           *result = read (fileno (yyin), (char *) buf, max_size);
565           if (*result < 0) 
566             einfo ("%F%P: read in flex scanner failed\n");
567         }
568     }
569 }
570
571 /* Eat the rest of a C-style comment.  */
572
573 static void
574 comment ()
575 {
576   int c;
577
578   while (1)
579   {
580     c = input();
581     while (c != '*' && c != EOF) 
582     {
583       if (c == '\n' || c == '\r')
584         lineno++;
585       c = input();
586     }
587
588     if (c == '*')
589     {
590       c = input();
591       while (c == '*')
592        c = input();
593       if (c == '/')
594        break;                   /* found the end */
595     }
596
597     if (c == '\n' || c == '\r')
598       lineno++;
599
600     if (c == EOF)
601     {
602       einfo( "%F%P: EOF in comment\n");
603       break;
604     }
605   }
606 }
607
608 /* Warn the user about a garbage character WHAT in the input
609    in context WHERE.  */
610
611 static void
612 lex_warn_invalid (where, what)
613      char *where, *what;
614 {
615   char buf[5];
616
617   /* If we have found an input file whose format we do not recognize,
618      and we are therefore treating it as a linker script, and we find
619      an invalid character, then most likely this is a real object file
620      of some different format.  Treat it as such.  */
621   if (ldfile_assumed_script)
622     {
623       bfd_set_error (bfd_error_file_not_recognized);
624       einfo ("%F%s: file not recognized: %E\n", ldfile_input_filename);
625     }
626
627   if (! isprint ((unsigned char) *what))
628     {
629       sprintf (buf, "\\%03o", (unsigned int) *what);
630       what = buf;
631     }
632
633   einfo ("%P:%S: ignoring invalid character `%s'%s\n", what, where);
634 }