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