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