* ldlex.l: Add rule to catch invalid input characters instead of
[external/binutils.git] / ld / ldlex.l
1 %{
2
3 /* Copyright (C) 1991, 1992, 1993, 1994 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, 675 Mass Ave, Cambridge, MA 02139, 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 /* start-sanitize-mpw */
30 #ifdef MPW
31 /* Prevent enum redefinition problems. */
32 #define TRUE_FALSE_ALREADY_DEFINED
33 #endif /* MPW */
34 /* end-sanitize-mpw */
35 #include "bfd.h"
36 #include "sysdep.h"
37 #include "ld.h"
38 #include "ldgram.h"
39 #include "ldmisc.h"
40 #include "ldexp.h"
41 #include "ldlang.h"
42 #include "ldfile.h"
43 #include "ldlex.h"
44 #include "ldmain.h"
45
46 int ldgram_in_defsym;
47
48 int hex_mode;
49
50 unsigned int lineno = 1;
51 int old;
52
53 #undef YY_INPUT
54 #define YY_INPUT(buf,result,max_size) yy_input(buf, &result, max_size)
55 #undef YY_FATAL_ERROR
56 #define YY_FATAL_ERROR(s)
57 #define MAX_INCLUDE_DEPTH 10
58 YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
59 char *file_name_stack[MAX_INCLUDE_DEPTH];
60 unsigned int include_stack_ptr = 0;
61
62 static YY_BUFFER_STATE yy_create_string_buffer PARAMS ((const char *string,
63                                                         int size));
64 static void yy_input PARAMS ((char *, int *result, int max_size));
65 static void comment PARAMS ((void));
66 static void lex_warn_invalid PARAMS ((char *where, char *what));
67
68 /* STATES 
69         COMMAND         on command line
70         EXPRESSION      definitely in an expression
71         SCRIPT          definitely in a script
72         BOTH            either EXPRESSION or SCRIPT
73         DEFSYMEXP       in an argument to -defsym
74         MRI             in an MRI script
75 */
76 #define RTOKEN(x)  {  yylval.token = x; return x; }
77 %}
78
79 %a 4000
80 %o 5000
81
82 CMDFILENAMECHAR   [_a-zA-Z0-9\/\.\\_\+\$\:\[\]\\\,\=\&\!\<\>\-\~]
83 CMDFILENAMECHAR1  [_a-zA-Z0-9\/\.\\_\+\$\:\[\]\\\,\=\&\!\<\>\~]
84 FILENAMECHAR1   [_a-zA-Z\/\.\\\$\_\~]
85 SYMBOLCHARN     [_a-zA-Z\/\.\\0-9]
86 FILENAMECHAR    [_a-zA-Z0-9\/\.\-\_\+\=\$\:\[\]\\\,\~]
87 FILENAME        {FILENAMECHAR}+
88 WHITE           [ \t\n]+ 
89
90 NOCFILENAMECHAR [_a-zA-Z0-9\/\.\-\_\+\$\:\[\]\\\~]
91
92
93 %s COMMAND
94 %s SCRIPT
95 %s EXPRESSION
96 %s BOTH
97 %s DEFSYMEXP
98 %s MRI
99 %%
100
101
102 <BOTH,SCRIPT,EXPRESSION,COMMAND>"/*"    { comment(); }
103
104
105 <DEFSYMEXP>"-"                  { RTOKEN('-');}
106 <DEFSYMEXP>"+"                  { RTOKEN('+');}
107 <DEFSYMEXP>{FILENAMECHAR1}{SYMBOLCHARN}*   { yylval.name = buystring(yytext); return NAME; }
108 <DEFSYMEXP>[ \t]                { RTOKEN(DEFSYMEND); }
109 <DEFSYMEXP>"="                  { RTOKEN('='); }
110 <COMMAND>"--help"               { return OPTION_help; }
111 <COMMAND>"--version"            { return OPTION_version; }
112 <COMMAND>"-defsym"{WHITE}*      { return OPTION_defsym; }
113 <COMMAND>"-format"              { return OPTION_format; }
114 <COMMAND>"-noinhibit-exec"      { return OPTION_noinhibit_exec; }
115 <COMMAND>"-noinhibit_exec"      { return OPTION_noinhibit_exec; }
116 <COMMAND>"-oformat"             { return OPTION_oformat; }
117 <COMMAND>"-sort-common"         { return OPTION_sort_common;}
118 <COMMAND>"-sort_common"         { return OPTION_sort_common;}
119 <COMMAND>"-warn-common"         { return OPTION_warn_common;}
120 <COMMAND>"-n"                   { return OPTION_n; }
121 <COMMAND>"-N"                   { return OPTION_N; }
122 <COMMAND>"-r"                   { return OPTION_r; }
123 <COMMAND>"-stats"               { return OPTION_stats; }
124 <COMMAND>"-no-keep-memory"      { return OPTION_no_keep_memory; }
125 <COMMAND>"-relax"               { return OPTION_relax; }
126 <COMMAND>"-i"                   { return OPTION_r; }
127 <COMMAND>"-Ur"                  { return OPTION_Ur; }
128 <COMMAND>"-o"                   { return OPTION_o; }
129 <COMMAND>"-g"                   { return OPTION_g; }
130 <COMMAND>"-e"                   { return OPTION_e; }
131 <COMMAND>"-e"{FILENAME}         {
132                                         yylval.name = buystring(yytext+2);
133                                         return OPTION_esymbol;
134                                 }
135 <COMMAND>"-b"                   { return OPTION_b; }
136 <COMMAND>"-dc"                  { return OPTION_dc; }
137 <COMMAND>"-dp"                  { return OPTION_dp; }
138 <COMMAND>"-d"                   { return OPTION_d; }
139 <COMMAND>"-v"                   { return OPTION_v; }
140 <COMMAND>"-V"                   { return OPTION_V; }
141 <COMMAND>"-m"                   { return OPTION_m; }
142 <COMMAND>"-m"{FILENAME}         { return OPTION_memul; }
143 <COMMAND>"-M"                   { return OPTION_M; }
144 <COMMAND>"-Map"                 { return OPTION_Map;}
145 <COMMAND>"-t"                   { return OPTION_t; }
146 <COMMAND>"-X"                   { return OPTION_X; }
147 <COMMAND>"-x"                   { return OPTION_x; }
148 <COMMAND>"-c"                   { return OPTION_c; }
149 <COMMAND>"-R"                   { return OPTION_R; }
150 <COMMAND>"-u"                   { return OPTION_u; }
151 <COMMAND>"-u"{FILENAME}         {
152                                         yylval.name = buystring(yytext+2);
153                                         return OPTION_usymbol;
154                                 }
155 <COMMAND>"-s"                   { return OPTION_s; }
156 <COMMAND>"-S"                   { return OPTION_S; }
157 <COMMAND>"-Bstat"               { return OPTION_Bstatic; }
158 <COMMAND>"-B"{FILENAME}         { /* Ignored */ }
159 <COMMAND>"-l"{FILENAME}         {
160                                         yylval.name = buystring(yytext+2);
161                                         return OPTION_l; 
162                                 }
163
164 <COMMAND>"-L"{FILENAME}         { 
165                                         yylval.name = buystring(yytext+2);
166                                         return OPTION_Lfile;
167                                 }
168 <COMMAND>"-L"                   { return OPTION_L; }
169 <COMMAND>"-Ttext"               {
170                                  yylval.name = ".text";
171                                  return OPTION_Texp;
172                                 }
173 <COMMAND>"-Tdata"               {
174                                  yylval.name = ".data";
175                                  return OPTION_Texp;
176                                }
177 <COMMAND>"-Tbss"                {
178                                  yylval.name = ".bss";
179                                  return OPTION_Texp;
180                                }
181 <COMMAND>"-O"([0-9])+           {
182                                         yylval.integer = atoi (yytext + 2);
183                                         return OPTION_Oval;
184                                 }
185 <COMMAND>"-O"{FILENAME}         {
186                                 yylval.name = buystring(yytext+2);
187                                 return OPTION_Texp;
188                                 }
189
190 <COMMAND>"-T"{FILENAME}         {
191                                  yylval.name = buystring(yytext+2);
192                                  return OPTION_Tfile;
193                                }
194 <COMMAND>"-T"                   {
195                                  return OPTION_T;
196                                }
197
198 <COMMAND>"-F"{FILENAME}         {
199                                  return OPTION_F;
200                                }
201 <COMMAND>"-F"                   {
202                                  return OPTION_F;
203                                }
204
205 <COMMAND>"-y"                  {
206                                         return OPTION_y;
207                                 }
208                                 
209 <COMMAND>"-y"{FILENAME}         {
210                                         yylval.name = buystring(yytext+2);
211                                         return OPTION_ysymbol;
212                                 }
213                                 
214 <COMMAND>"-A"{FILENAME}         {
215                                         yylval.name = buystring(yytext+2);
216                                         return OPTION_Aarch;
217                                 }
218
219 <COMMAND>"-retain-symbols-file" { return OPTION_RETAIN_SYMBOLS_FILE; }
220
221 <COMMAND>"-EB"                  {
222                                         return OPTION_EB;
223                                 }
224 <COMMAND>"-EL"                  {
225                                         return OPTION_EL;
226                                 }
227 <COMMAND>"-G"                   {
228                                         return OPTION_G;
229                                 }
230 <COMMAND>"-G"([0-9])+           {
231                                         yylval.integer = atoi (yytext + 2);
232                                         return OPTION_Gval;
233                                 }
234
235 <COMMAND>"-Qy"                  {       return OPTION_Qy; }
236 <COMMAND>"-dn"                  {       return OPTION_dn; }
237 <COMMAND>"-Y"                   {       return OPTION_Y;  }
238 <COMMAND>"-YP,"{FILENAME}       {
239                                         yylval.name = buystring (yytext+4);
240                                         return OPTION_YP;
241                                 }
242 <COMMAND>"-non_shared"          {       return OPTION_non_shared; }
243 <COMMAND>"-call_shared"         {       return OPTION_call_shared; }
244
245 <MRI,EXPRESSION>"$"([0-9A-Fa-f])+ {
246                                 yylval.integer = bfd_scan_vma (yytext+1, 0,16);
247                                 return INT;
248                         }
249
250 <MRI,EXPRESSION>([0-9A-Fa-f])+(H|X|B|O|D) {
251                                    int ibase ;
252                                    switch (yytext[yyleng-1]) {
253                                     case 'X': 
254                                     case 'H':
255                                      ibase = 16;
256                                      break;
257                                     case 'O':
258                                      ibase = 8;
259                                      break;
260                                     case 'B':
261                                      ibase = 2;
262                                      break;
263                                     default:
264                                      ibase = 10;
265                                    }
266                                    yylval.integer = bfd_scan_vma (yytext+1, 0,
267                                                                   ibase);
268                                    return INT;
269                                  }
270 <SCRIPT,DEFSYMEXP,MRI,BOTH,EXPRESSION>"$"?"0x"?([0-9A-Fa-f])+(M|K|m|k)? {
271                                   yylval.integer = bfd_scan_vma (yytext, 0,
272                                                                  hex_mode);
273                                   if (yytext[yyleng-1]=='M'
274                                         || yytext[yyleng-1] == 'm') {
275                                       yylval.integer *= 1024*1024;
276                                     }   
277                                   if (yytext[yyleng-1]=='K' 
278                                 || yytext[yyleng-1]=='k') {
279                                       yylval.integer *= 1024;
280                                     }           
281                                   return INT;
282                                 }
283 <BOTH,SCRIPT,EXPRESSION>"]"             { RTOKEN(']');}
284 <BOTH,SCRIPT,EXPRESSION>"["             { RTOKEN('[');}
285 <BOTH,SCRIPT,EXPRESSION>"<<="   { RTOKEN(LSHIFTEQ);}
286 <BOTH,SCRIPT,EXPRESSION>">>="   { RTOKEN(RSHIFTEQ);}
287 <BOTH,SCRIPT,EXPRESSION>"||"    { RTOKEN(OROR);}
288 <BOTH,SCRIPT,EXPRESSION>"=="    { RTOKEN(EQ);}
289 <BOTH,SCRIPT,EXPRESSION>"!="    { RTOKEN(NE);}
290 <BOTH,SCRIPT,EXPRESSION>">="    { RTOKEN(GE);}
291 <BOTH,SCRIPT,EXPRESSION>"<="    { RTOKEN(LE);}
292 <BOTH,SCRIPT,EXPRESSION>"<<"    { RTOKEN(LSHIFT);}
293 <BOTH,SCRIPT,EXPRESSION>">>"    { RTOKEN(RSHIFT);}
294 <BOTH,SCRIPT,EXPRESSION>"+="    { RTOKEN(PLUSEQ);}
295 <BOTH,SCRIPT,EXPRESSION>"-="    { RTOKEN(MINUSEQ);}
296 <BOTH,SCRIPT,EXPRESSION>"*="    { RTOKEN(MULTEQ);}
297 <BOTH,SCRIPT,EXPRESSION>"/="    { RTOKEN(DIVEQ);}
298 <BOTH,SCRIPT,EXPRESSION>"&="    { RTOKEN(ANDEQ);}
299 <BOTH,SCRIPT,EXPRESSION>"|="    { RTOKEN(OREQ);}
300 <BOTH,SCRIPT,EXPRESSION>"&&"    { RTOKEN(ANDAND);}
301 <BOTH,SCRIPT,EXPRESSION>">"             { RTOKEN('>');}
302 <MRI,BOTH,SCRIPT,EXPRESSION>","         { RTOKEN(',');}
303 <BOTH,SCRIPT,EXPRESSION>"&"             { RTOKEN('&');}
304 <BOTH,SCRIPT,EXPRESSION>"|"             { RTOKEN('|');}
305 <BOTH,SCRIPT,EXPRESSION>"~"             { RTOKEN('~');}
306 <BOTH,SCRIPT,EXPRESSION>"!"             { RTOKEN('!');}
307 <BOTH,SCRIPT,EXPRESSION>"?"             { RTOKEN('?');}
308 <BOTH,SCRIPT,EXPRESSION>"*"             { RTOKEN('*');}
309 <BOTH,SCRIPT,EXPRESSION>"+"             { RTOKEN('+');}
310 <BOTH,SCRIPT,EXPRESSION>"-"             { RTOKEN('-');}
311 <BOTH,SCRIPT,EXPRESSION>"/"             { RTOKEN('/');}
312 <BOTH,SCRIPT,EXPRESSION>"%"             { RTOKEN('%');}
313 <BOTH,SCRIPT,EXPRESSION>"<"             { RTOKEN('<');}
314 <BOTH,SCRIPT,EXPRESSION>">"             { RTOKEN('>');}
315 <MRI,BOTH,SCRIPT,EXPRESSION>"="          { RTOKEN('=');}
316 <BOTH,SCRIPT,EXPRESSION>"}"                     { RTOKEN('}') ; }
317 <BOTH,SCRIPT,EXPRESSION>"{"                     { RTOKEN('{'); }
318 <BOTH,SCRIPT,EXPRESSION>")"                     { RTOKEN(')');}
319 <BOTH,SCRIPT,EXPRESSION>"("                     { RTOKEN('(');}
320 <BOTH,SCRIPT,EXPRESSION>"]"             { RTOKEN(']');}
321 <BOTH,SCRIPT,EXPRESSION>"["             { RTOKEN('[');}
322 <BOTH,SCRIPT,EXPRESSION>":"             { RTOKEN(':'); }
323 <BOTH,SCRIPT,EXPRESSION>";"             { RTOKEN(';');}
324 <BOTH,SCRIPT,EXPRESSION>"-"             { RTOKEN('-');}
325 <BOTH,SCRIPT,EXPRESSION>"/"             { RTOKEN('/');}
326 <BOTH,SCRIPT>"MEMORY"           { RTOKEN(MEMORY);}
327 <BOTH,SCRIPT>"ORIGIN"           { RTOKEN(ORIGIN);}
328 <BOTH,SCRIPT>"BLOCK"                    { RTOKEN(BLOCK);}
329 <BOTH,SCRIPT>"LENGTH"           { RTOKEN(LENGTH);}
330 <EXPRESSION,BOTH,SCRIPT>"ALIGN"                 { RTOKEN(ALIGN_K);}
331 <EXPRESSION,BOTH,SCRIPT>"ADDR"                  { RTOKEN(ADDR);}
332 <BOTH,SCRIPT>"ENTRY"                    { RTOKEN(ENTRY);}
333 <EXPRESSION,BOTH,SCRIPT>"NEXT"                  { RTOKEN(NEXT);}
334 <EXPRESSION,BOTH,SCRIPT>"sizeof_headers"        { RTOKEN(SIZEOF_HEADERS);}
335 <EXPRESSION,BOTH,SCRIPT>"SIZEOF_HEADERS"        { RTOKEN(SIZEOF_HEADERS);}
336 <BOTH,SCRIPT>"MAP"                      { RTOKEN(MAP);}
337 <EXPRESSION,BOTH,SCRIPT>"SIZEOF"                { RTOKEN(SIZEOF);}
338 <BOTH,SCRIPT>"TARGET"           { RTOKEN(TARGET_K);}
339 <BOTH,SCRIPT>"SEARCH_DIR"               { RTOKEN(SEARCH_DIR);}
340 <BOTH,SCRIPT>"OUTPUT"           { RTOKEN(OUTPUT);}
341 <BOTH,SCRIPT>"INPUT"                    { RTOKEN(INPUT);}
342 <EXPRESSION,BOTH,SCRIPT>"DEFINED"               { RTOKEN(DEFINED);}
343 <BOTH,SCRIPT>"CREATE_OBJECT_SYMBOLS"    { RTOKEN(CREATE_OBJECT_SYMBOLS);}
344 <BOTH,SCRIPT>"CONSTRUCTORS"             { RTOKEN( CONSTRUCTORS);}
345 <BOTH,SCRIPT>"FORCE_COMMON_ALLOCATION" { RTOKEN(FORCE_COMMON_ALLOCATION);}
346 <BOTH,SCRIPT>"SECTIONS"         { RTOKEN(SECTIONS);}
347 <BOTH,SCRIPT>"FILL"                     { RTOKEN(FILL);}
348 <BOTH,SCRIPT>"STARTUP"          { RTOKEN(STARTUP);}
349 <BOTH,SCRIPT>"OUTPUT_FORMAT"            { RTOKEN(OUTPUT_FORMAT);}
350 <BOTH,SCRIPT>"OUTPUT_ARCH"              { RTOKEN( OUTPUT_ARCH);}
351 <BOTH,SCRIPT>"HLL"                      { RTOKEN(HLL);}
352 <BOTH,SCRIPT>"SYSLIB"           { RTOKEN(SYSLIB);}
353 <BOTH,SCRIPT>"FLOAT"                    { RTOKEN(FLOAT);}
354 <BOTH,SCRIPT>"QUAD"                     { RTOKEN( QUAD);}
355 <BOTH,SCRIPT>"LONG"                     { RTOKEN( LONG);}
356 <BOTH,SCRIPT>"SHORT"                    { RTOKEN( SHORT);}
357 <BOTH,SCRIPT>"BYTE"                     { RTOKEN( BYTE);}
358 <BOTH,SCRIPT>"NOFLOAT"          { RTOKEN(NOFLOAT);}
359 <EXPRESSION,BOTH,SCRIPT>"NOLOAD"                { RTOKEN(NOLOAD);}
360 <BOTH,SCRIPT>"DSECT"                    { RTOKEN(DSECT);}
361 <BOTH,SCRIPT>"COPY"                     { RTOKEN(COPY);}
362 <BOTH,SCRIPT>"INFO"                     { RTOKEN(INFO);}
363 <BOTH,SCRIPT>"OVERLAY"          { RTOKEN(OVERLAY);}
364 <BOTH,SCRIPT>"o"                        { RTOKEN(ORIGIN);}
365 <BOTH,SCRIPT>"org"                      { RTOKEN(ORIGIN);}
366 <BOTH,SCRIPT>"l"                        { RTOKEN( LENGTH);}
367 <BOTH,SCRIPT>"len"                      { RTOKEN( LENGTH);}
368 <BOTH,SCRIPT>"INCLUDE"                  { RTOKEN(INCLUDE);}
369 <EXPRESSION,BOTH,SCRIPT>"AT"                    { RTOKEN(AT);}
370 <MRI>"\n"                       { ++ lineno;  RTOKEN(NEWLINE); }
371 <MRI>"*".*                      { /* Mri comment line */ }
372 <MRI>"END"                      { RTOKEN(ENDWORD); }
373 <MRI>"ALIGNMOD"         { RTOKEN(ALIGNMOD);}
374 <MRI>"ALIGN"            { RTOKEN(ALIGN_K);}
375
376 <MRI>"CHIP"                     { RTOKEN(CHIP); }
377 <MRI>"BASE"                     { RTOKEN(BASE); }
378 <MRI>"ALIAS"                     { RTOKEN(ALIAS); }
379 <MRI>"TRUNCATE"                     { RTOKEN(TRUNCATE); }
380 <MRI>"LOAD"                     { RTOKEN(LOAD); }
381 <MRI>"PUBLIC"                   { RTOKEN(PUBLIC); }
382 <MRI>"ORDER"                    { RTOKEN(ORDER); }
383 <MRI>"NAME"                     { RTOKEN(NAMEWORD); }
384 <MRI>"FORMAT"                   { RTOKEN(FORMAT); }
385 <MRI>"LIST".*                   { RTOKEN(LIST); /* LIST and ignore to end of line */ }
386 <MRI>"SECT"                     { RTOKEN(SECT); }
387 <EXPRESSION,BOTH,SCRIPT,MRI>"ABSOLUTE"                  { RTOKEN(ABSOLUTE); }
388 <MRI>"end"                      { RTOKEN(ENDWORD); }
389 <MRI>"chip"                     { RTOKEN(CHIP); }
390 <MRI>"load"                     { RTOKEN(LOAD); }
391 <MRI>"order"                    { RTOKEN(ORDER); }
392 <MRI>"name"                     { RTOKEN(NAMEWORD); }
393 <MRI>"format"                   { RTOKEN(FORMAT); }
394 <MRI>"list".*                   { RTOKEN(LIST); /* LIST and ignore to end of line */ }
395 <MRI>"sect"                     { RTOKEN(SECT); }
396 <EXPRESSION,BOTH,SCRIPT,MRI>"absolute"                  { RTOKEN(ABSOLUTE); }
397
398 <COMMAND>{CMDFILENAMECHAR1}{CMDFILENAMECHAR}*   {
399                                  yylval.name = buystring(yytext); 
400                                   return NAME;
401                                 }
402
403
404 <MRI>{FILENAMECHAR1}{NOCFILENAMECHAR}*  {
405 /* Filename without commas, needed to parse mri stuff */
406                                  yylval.name = buystring(yytext); 
407                                   return NAME;
408                                 }
409
410
411 <BOTH,EXPRESSION>{FILENAMECHAR1}{FILENAMECHAR}* {
412                                  yylval.name = buystring(yytext); 
413                                   return NAME;
414                                 }
415 <SCRIPT,COMMAND>{FILENAMECHAR}* { yylval.name = buystring(yytext); 
416                                   return NAME;
417                                 }
418
419 <EXPRESSION,BOTH,COMMAND,SCRIPT>"\""[^\"]*"\"" {
420                                         /* No matter the state, quotes
421                                            give what's inside */
422                                         yylval.name = buystring(yytext+1);
423                                         yylval.name[yyleng-2] = 0;
424                                         return NAME;
425                                 }
426 <BOTH,SCRIPT,EXPRESSION>"\n"            { lineno++;}
427 <MRI,COMMAND,BOTH,SCRIPT,EXPRESSION>[ \t]
428
429 <<EOF>> {
430   include_stack_ptr--;
431     
432   if (include_stack_ptr == 0) 
433   {
434     yyterminate();
435   }
436   else 
437   {
438     yy_switch_to_buffer(include_stack[include_stack_ptr]);
439
440   }
441   if (include_stack_ptr==1) 
442   {
443     BEGIN(COMMAND);
444   }
445   else 
446   {
447     BEGIN(SCRIPT);
448     ldfile_input_filename = file_name_stack[include_stack_ptr-1];
449   }
450
451   return END;
452 }
453
454 <COMMAND>.              lex_warn_invalid(" on command line", yytext);
455 <SCRIPT,MRI>.           lex_warn_invalid(" in script", yytext);
456 <EXPRESSION,DEFSYMEXP,BOTH>.    lex_warn_invalid(" in expression", yytext);
457     
458 %%
459
460 void
461 lex_push_file (file, name)
462      FILE *file;
463      char *name;
464 {
465   if (include_stack_ptr >= MAX_INCLUDE_DEPTH) 
466   {
467     einfo("%F:includes nested too deeply\n");
468   }
469   file_name_stack[include_stack_ptr] = name;
470   include_stack[include_stack_ptr] = YY_CURRENT_BUFFER;
471
472   include_stack_ptr++;
473   yyin = file;
474   yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
475
476   BEGIN(SCRIPT);
477 }
478
479 static YY_BUFFER_STATE 
480 yy_create_string_buffer (string,  size)
481      CONST char *string;
482      int size;
483 {
484   YY_BUFFER_STATE b;
485
486   b = (YY_BUFFER_STATE) malloc( sizeof( struct yy_buffer_state ) );
487   b->yy_input_file = 0;
488   
489   if ( ! b )
490    YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
491
492   b->yy_buf_size = size;
493
494   /* yy_ch_buf has to be 2 characters longer than the size given because
495    * we need to put in 2 end-of-buffer characters.
496    */
497   b->yy_ch_buf = (YY_CHAR *) malloc( (unsigned) (b->yy_buf_size + 3) );
498
499   if ( ! b->yy_ch_buf )
500    YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
501
502
503   b->yy_ch_buf[0] = '\n';
504   strcpy(b->yy_ch_buf+1,  string);
505   b->yy_ch_buf[size+1] = YY_END_OF_BUFFER_CHAR;
506   b->yy_ch_buf[size+2] = YY_END_OF_BUFFER_CHAR;
507     b->yy_n_chars = size+1;
508   b->yy_buf_pos = &b->yy_ch_buf[1];
509
510   b->yy_eof_status = EOF_NOT_SEEN;
511
512   return ( b );
513 }
514
515
516
517 void
518 lex_redirect (string)
519      CONST  char *string;
520 {
521   YY_BUFFER_STATE tmp;
522
523   int len = strlen(string);
524   yy_init = 0  ;  
525   if (include_stack_ptr >= MAX_INCLUDE_DEPTH) 
526   {
527     einfo("%F: macros nested too deeply\n");
528   }
529   file_name_stack[include_stack_ptr] = "redirect";
530   include_stack[include_stack_ptr] = YY_CURRENT_BUFFER;
531   include_stack_ptr++;
532   tmp =   yy_create_string_buffer(string, len);
533   
534   yy_switch_to_buffer(tmp);
535   BEGIN(COMMAND);
536   yyout = stdout;
537 }
538
539 int state_stack[20];
540 int *state_stack_p = state_stack;
541
542 void
543 ldlex_script ()
544 {
545   *(state_stack_p)++ = yy_start;
546
547   BEGIN(SCRIPT);
548 }
549
550
551 void
552 ldlex_mri_script ()
553 {
554   *(state_stack_p)++ = yy_start;
555   BEGIN(MRI);
556 }
557
558 void
559 ldlex_defsym ()
560 {
561   *(state_stack_p)++ = yy_start;
562   BEGIN(DEFSYMEXP);
563 }
564            
565 void
566 ldlex_expression ()
567 {
568   *(state_stack_p)++ = yy_start;
569   BEGIN(EXPRESSION);
570
571 }
572 void
573 ldlex_both ()
574 {
575   *(state_stack_p)++ = yy_start;
576   BEGIN(BOTH);
577 }
578 void
579 ldlex_command ()
580 {
581   *(state_stack_p)++ = yy_start;
582   BEGIN(COMMAND);
583 }
584
585 void
586 ldlex_popstate ()
587 {
588   yy_start = *(--state_stack_p);
589 }
590
591 static void
592 yy_input(buf, result, max_size)
593 char *buf;
594 int *result;
595 int max_size;
596 {
597   *result = 0; 
598   if (yy_current_buffer->yy_input_file)
599   {
600     if (yyin)
601      if ( (*result = read( fileno(yyin), (char *) buf, max_size )) < 0 ) 
602       YY_FATAL_ERROR( "read() in flex scanner failed" );
603   }
604 }
605
606 static void
607 comment ()
608 {
609   int c;
610   while (1)
611   {
612     c = input();
613     while (c !='*' && c != EOF) 
614     {
615       if (c == '\n') lineno++;
616       c = input();
617     }
618
619
620     if (c == '*')
621     {
622       c = input();
623       while ( c == '*')
624        c = input();
625       if ( c == '/' )
626        break;                   /* found the end */
627     }
628
629     if ( c == EOF )
630     {
631       einfo( "%F%P :EOF in comment\n");
632       break;
633     }
634   }
635 }
636
637 static void
638 lex_warn_invalid (where, what)
639      char *where, *what;
640 {
641   fprintf(stderr,
642           "%s: ignoring invalid character `%s'%s\n",
643           program_name, what, where);
644 }