Home | History | Annotate | Line # | Download | only in aic7xxx
aicasm_scan.l revision 1.4
      1  1.1   fvdl %{
      2  1.4  perry /*	$NetBSD: aicasm_scan.l,v 1.4 2005/02/27 00:27:23 perry Exp $	*/
      3  1.1   fvdl /*
      4  1.1   fvdl  * Lexical Analyzer for the Aic7xxx SCSI Host adapter sequencer assembler.
      5  1.1   fvdl  *
      6  1.3   fvdl  * Copyright (c) 1997, 1998, 2000 Justin T. Gibbs.
      7  1.3   fvdl  * Copyright (c) 2001, 2002 Adaptec Inc.
      8  1.1   fvdl  * All rights reserved.
      9  1.1   fvdl  *
     10  1.1   fvdl  * Redistribution and use in source and binary forms, with or without
     11  1.1   fvdl  * modification, are permitted provided that the following conditions
     12  1.1   fvdl  * are met:
     13  1.1   fvdl  * 1. Redistributions of source code must retain the above copyright
     14  1.1   fvdl  *    notice, this list of conditions, and the following disclaimer,
     15  1.1   fvdl  *    without modification.
     16  1.3   fvdl  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     17  1.3   fvdl  *    substantially similar to the "NO WARRANTY" disclaimer below
     18  1.3   fvdl  *    ("Disclaimer") and any redistribution must be conditioned upon
     19  1.3   fvdl  *    including a substantially similar Disclaimer requirement for further
     20  1.3   fvdl  *    binary redistribution.
     21  1.3   fvdl  * 3. Neither the names of the above-listed copyright holders nor the names
     22  1.3   fvdl  *    of any contributors may be used to endorse or promote products derived
     23  1.3   fvdl  *    from this software without specific prior written permission.
     24  1.3   fvdl  *
     25  1.3   fvdl  * Alternatively, this software may be distributed under the terms of the
     26  1.3   fvdl  * GNU General Public License ("GPL") version 2 as published by the Free
     27  1.3   fvdl  * Software Foundation.
     28  1.1   fvdl  *
     29  1.3   fvdl  * NO WARRANTY
     30  1.3   fvdl  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     31  1.3   fvdl  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     32  1.3   fvdl  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     33  1.3   fvdl  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     34  1.3   fvdl  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     35  1.1   fvdl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     36  1.1   fvdl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     37  1.3   fvdl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     38  1.3   fvdl  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     39  1.3   fvdl  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     40  1.3   fvdl  * POSSIBILITY OF SUCH DAMAGES.
     41  1.1   fvdl  *
     42  1.3   fvdl  * $FreeBSD: src/sys/dev/aic7xxx/aicasm/aicasm_scan.l,v 1.21 2002/09/27 03:23:02 gibbs Exp $
     43  1.1   fvdl  */
     44  1.1   fvdl 
     45  1.1   fvdl #include <sys/types.h>
     46  1.1   fvdl 
     47  1.3   fvdl #include <inttypes.h>
     48  1.1   fvdl #include <limits.h>
     49  1.3   fvdl #include <regex.h>
     50  1.1   fvdl #include <stdio.h>
     51  1.1   fvdl #include <string.h>
     52  1.1   fvdl #include <sysexits.h>
     53  1.3   fvdl #ifdef __linux__
     54  1.3   fvdl #include "../queue.h"
     55  1.3   fvdl #else
     56  1.1   fvdl #include <sys/queue.h>
     57  1.3   fvdl #endif
     58  1.1   fvdl 
     59  1.1   fvdl #include "aicasm.h"
     60  1.1   fvdl #include "aicasm_symbol.h"
     61  1.1   fvdl #include "aicasm_gram.h"
     62  1.1   fvdl 
     63  1.3   fvdl /* This is used for macro body capture too, so err on the large size. */
     64  1.3   fvdl #define MAX_STR_CONST 4096
     65  1.3   fvdl static char string_buf[MAX_STR_CONST];
     66  1.3   fvdl static char *string_buf_ptr;
     67  1.3   fvdl static int  parren_count;
     68  1.3   fvdl static int  quote_count;
     69  1.3   fvdl static char buf[255];
     70  1.1   fvdl %}
     71  1.1   fvdl 
     72  1.3   fvdl PATH		([/]*[-A-Za-z0-9_.])+
     73  1.1   fvdl WORD		[A-Za-z_][-A-Za-z_0-9]*
     74  1.1   fvdl SPACE		[ \t]+
     75  1.3   fvdl MCARG		[^(), \t]+
     76  1.3   fvdl MBODY		((\\[^\n])*[^\n\\]*)+
     77  1.1   fvdl 
     78  1.1   fvdl %x COMMENT
     79  1.1   fvdl %x CEXPR
     80  1.1   fvdl %x INCLUDE
     81  1.3   fvdl %x STRING
     82  1.3   fvdl %x MACRODEF
     83  1.3   fvdl %x MACROARGLIST
     84  1.3   fvdl %x MACROCALLARGS
     85  1.3   fvdl %x MACROBODY
     86  1.1   fvdl 
     87  1.1   fvdl %%
     88  1.1   fvdl \n			{ ++yylineno; }
     89  1.1   fvdl "/*"			{ BEGIN COMMENT;  /* Enter comment eating state */ }
     90  1.1   fvdl <COMMENT>"/*"		{ fprintf(stderr, "Warning! Comment within comment."); }
     91  1.1   fvdl <COMMENT>\n		{ ++yylineno; }
     92  1.1   fvdl <COMMENT>[^*/\n]*	;
     93  1.1   fvdl <COMMENT>"*"+[^*/\n]*	;
     94  1.1   fvdl <COMMENT>"/"+[^*/\n]*	;
     95  1.1   fvdl <COMMENT>"*"+"/"	{ BEGIN INITIAL; }
     96  1.1   fvdl if[ \t]*\(		{
     97  1.1   fvdl 				string_buf_ptr = string_buf;
     98  1.1   fvdl 				parren_count = 1;
     99  1.1   fvdl 				BEGIN CEXPR;
    100  1.1   fvdl 				return T_IF;
    101  1.1   fvdl 			}
    102  1.1   fvdl <CEXPR>\(		{	*string_buf_ptr++ = '('; parren_count++; }
    103  1.1   fvdl <CEXPR>\)		{
    104  1.1   fvdl 				parren_count--;
    105  1.1   fvdl 				if (parren_count == 0) {
    106  1.1   fvdl 					/* All done */
    107  1.1   fvdl 					BEGIN INITIAL;
    108  1.1   fvdl 					*string_buf_ptr = '\0';
    109  1.1   fvdl 					yylval.sym = symtable_get(string_buf);
    110  1.1   fvdl 					return T_CEXPR;
    111  1.1   fvdl 				} else {
    112  1.1   fvdl 					*string_buf_ptr++ = ')';
    113  1.1   fvdl 				}
    114  1.1   fvdl 			}
    115  1.1   fvdl <CEXPR>\n		{ ++yylineno; }
    116  1.3   fvdl <CEXPR>[^()\n]+	{
    117  1.3   fvdl 				char *yptr;
    118  1.3   fvdl 
    119  1.3   fvdl 				yptr = yytext;
    120  1.3   fvdl 				while (*yptr != '\0') {
    121  1.3   fvdl 					/* Remove duplicate spaces */
    122  1.3   fvdl 					if (*yptr == '\t')
    123  1.3   fvdl 						*yptr = ' ';
    124  1.3   fvdl 					if (*yptr == ' '
    125  1.3   fvdl 					 && string_buf_ptr != string_buf
    126  1.3   fvdl 					 && string_buf_ptr[-1] == ' ')
    127  1.3   fvdl 						yptr++;
    128  1.4  perry 					else
    129  1.3   fvdl 						*string_buf_ptr++ = *yptr++;
    130  1.3   fvdl 				}
    131  1.3   fvdl 			}
    132  1.1   fvdl 
    133  1.3   fvdl VERSION			{ return T_VERSION; }
    134  1.3   fvdl PREFIX			{ return T_PREFIX; }
    135  1.3   fvdl PATCH_ARG_LIST		{ return T_PATCH_ARG_LIST; }
    136  1.3   fvdl \"			{
    137  1.3   fvdl 				string_buf_ptr = string_buf;
    138  1.3   fvdl 				BEGIN STRING;
    139  1.3   fvdl 			}
    140  1.3   fvdl <STRING>[^"]+		{
    141  1.3   fvdl 				char *yptr;
    142  1.3   fvdl 
    143  1.3   fvdl 				yptr = yytext;
    144  1.3   fvdl 				while (*yptr)
    145  1.1   fvdl 					*string_buf_ptr++ = *yptr++;
    146  1.1   fvdl 			}
    147  1.3   fvdl <STRING>\"		{
    148  1.3   fvdl 				/* All done */
    149  1.3   fvdl 				BEGIN INITIAL;
    150  1.3   fvdl 				*string_buf_ptr = '\0';
    151  1.3   fvdl 				yylval.str = string_buf;
    152  1.3   fvdl 				return T_STRING;
    153  1.3   fvdl 			}
    154  1.3   fvdl {SPACE}			 ;
    155  1.1   fvdl 
    156  1.1   fvdl 	/* Register/SCB/SRAM definition keywords */
    157  1.3   fvdl export			{ return T_EXPORT; }
    158  1.1   fvdl register		{ return T_REGISTER; }
    159  1.1   fvdl const			{ yylval.value = FALSE; return T_CONST; }
    160  1.1   fvdl download		{ return T_DOWNLOAD; }
    161  1.1   fvdl address			{ return T_ADDRESS; }
    162  1.1   fvdl access_mode		{ return T_ACCESS_MODE; }
    163  1.3   fvdl modes			{ return T_MODES; }
    164  1.1   fvdl RW|RO|WO		{
    165  1.1   fvdl 				 if (strcmp(yytext, "RW") == 0)
    166  1.1   fvdl 					yylval.value = RW;
    167  1.1   fvdl 				 else if (strcmp(yytext, "RO") == 0)
    168  1.1   fvdl 					yylval.value = RO;
    169  1.1   fvdl 				 else
    170  1.1   fvdl 					yylval.value = WO;
    171  1.1   fvdl 				 return T_MODE;
    172  1.1   fvdl 			}
    173  1.3   fvdl BEGIN_CRITICAL		{ return T_BEGIN_CS; }
    174  1.3   fvdl END_CRITICAL		{ return T_END_CS; }
    175  1.3   fvdl SET_SRC_MODE		{ return T_SET_SRC_MODE; }
    176  1.3   fvdl SET_DST_MODE		{ return T_SET_DST_MODE; }
    177  1.3   fvdl field			{ return T_FIELD; }
    178  1.3   fvdl enum			{ return T_ENUM; }
    179  1.1   fvdl mask			{ return T_MASK; }
    180  1.1   fvdl alias			{ return T_ALIAS; }
    181  1.1   fvdl size			{ return T_SIZE; }
    182  1.1   fvdl scb			{ return T_SCB; }
    183  1.1   fvdl scratch_ram		{ return T_SRAM; }
    184  1.1   fvdl accumulator		{ return T_ACCUM; }
    185  1.3   fvdl mode_pointer		{ return T_MODE_PTR; }
    186  1.1   fvdl allones			{ return T_ALLONES; }
    187  1.1   fvdl allzeros		{ return T_ALLZEROS; }
    188  1.1   fvdl none			{ return T_NONE; }
    189  1.1   fvdl sindex			{ return T_SINDEX; }
    190  1.1   fvdl A			{ return T_A; }
    191  1.1   fvdl 
    192  1.1   fvdl 	/* Opcodes */
    193  1.1   fvdl shl			{ return T_SHL; }
    194  1.1   fvdl shr			{ return T_SHR; }
    195  1.1   fvdl ror			{ return T_ROR; }
    196  1.1   fvdl rol			{ return T_ROL; }
    197  1.1   fvdl mvi			{ return T_MVI; }
    198  1.1   fvdl mov			{ return T_MOV; }
    199  1.1   fvdl clr			{ return T_CLR; }
    200  1.1   fvdl jmp			{ return T_JMP; }
    201  1.1   fvdl jc			{ return T_JC;	}
    202  1.1   fvdl jnc			{ return T_JNC;	}
    203  1.1   fvdl je			{ return T_JE;	}
    204  1.1   fvdl jne			{ return T_JNE;	}
    205  1.1   fvdl jz			{ return T_JZ;	}
    206  1.1   fvdl jnz			{ return T_JNZ;	}
    207  1.1   fvdl call			{ return T_CALL; }
    208  1.1   fvdl add			{ return T_ADD; }
    209  1.1   fvdl adc			{ return T_ADC; }
    210  1.1   fvdl bmov			{ return T_BMOV; }
    211  1.1   fvdl inc			{ return T_INC; }
    212  1.1   fvdl dec			{ return T_DEC; }
    213  1.1   fvdl stc			{ return T_STC;	}
    214  1.1   fvdl clc			{ return T_CLC; }
    215  1.1   fvdl cmp			{ return T_CMP;	}
    216  1.3   fvdl not			{ return T_NOT;	}
    217  1.1   fvdl xor			{ return T_XOR;	}
    218  1.1   fvdl test			{ return T_TEST;}
    219  1.1   fvdl and			{ return T_AND;	}
    220  1.1   fvdl or			{ return T_OR;	}
    221  1.1   fvdl ret			{ return T_RET; }
    222  1.1   fvdl nop			{ return T_NOP; }
    223  1.1   fvdl else			{ return T_ELSE; }
    224  1.1   fvdl 
    225  1.1   fvdl 	/* Allowed Symbols */
    226  1.3   fvdl \<\<			{ return T_EXPR_LSHIFT; }
    227  1.3   fvdl \>\>			{ return T_EXPR_RSHIFT; }
    228  1.3   fvdl [-+,:()~|&."{};<>[\]/*!=] { return yytext[0]; }
    229  1.1   fvdl 
    230  1.1   fvdl 	/* Number processing */
    231  1.1   fvdl 0[0-7]*			{
    232  1.1   fvdl 				yylval.value = strtol(yytext, NULL, 8);
    233  1.1   fvdl 				return T_NUMBER;
    234  1.1   fvdl 			}
    235  1.1   fvdl 
    236  1.1   fvdl 0[xX][0-9a-fA-F]+	{
    237  1.1   fvdl 				yylval.value = strtoul(yytext + 2, NULL, 16);
    238  1.1   fvdl 				return T_NUMBER;
    239  1.1   fvdl 			}
    240  1.1   fvdl 
    241  1.1   fvdl [1-9][0-9]*		{
    242  1.1   fvdl 				yylval.value = strtol(yytext, NULL, 10);
    243  1.1   fvdl 				return T_NUMBER;
    244  1.1   fvdl 			}
    245  1.3   fvdl 	/* Include Files */
    246  1.3   fvdl #include{SPACE}		{
    247  1.3   fvdl 				BEGIN INCLUDE;
    248  1.3   fvdl 				quote_count = 0;
    249  1.3   fvdl 				return T_INCLUDE;
    250  1.3   fvdl 			}
    251  1.3   fvdl <INCLUDE>[<]		{ return yytext[0]; }
    252  1.3   fvdl <INCLUDE>[>]		{ BEGIN INITIAL; return yytext[0]; }
    253  1.3   fvdl <INCLUDE>[\"]		{
    254  1.3   fvdl 				if (quote_count != 0)
    255  1.3   fvdl 					BEGIN INITIAL;
    256  1.3   fvdl 				quote_count++;
    257  1.3   fvdl 				return yytext[0];
    258  1.3   fvdl 			}
    259  1.3   fvdl <INCLUDE>{PATH}		{
    260  1.3   fvdl 				char *yptr;
    261  1.1   fvdl 
    262  1.3   fvdl 				yptr = yytext;
    263  1.3   fvdl 				string_buf_ptr = string_buf;
    264  1.3   fvdl 				while (*yptr)
    265  1.3   fvdl 					*string_buf_ptr++ = *yptr++;
    266  1.3   fvdl 				yylval.str = string_buf;
    267  1.3   fvdl 				*string_buf_ptr = '\0';
    268  1.3   fvdl 				return T_PATH;
    269  1.3   fvdl 			}
    270  1.1   fvdl <INCLUDE>.		{ stop("Invalid include line", EX_DATAERR); }
    271  1.3   fvdl #define{SPACE}		{
    272  1.3   fvdl 				BEGIN MACRODEF;
    273  1.3   fvdl 				return T_DEFINE;
    274  1.3   fvdl 			}
    275  1.4  perry <MACRODEF>{WORD}{SPACE}	{
    276  1.3   fvdl 				char *yptr;
    277  1.1   fvdl 
    278  1.3   fvdl 				/* Strip space and return as a normal symbol */
    279  1.3   fvdl 				yptr = yytext;
    280  1.3   fvdl 				while (*yptr != ' ' && *yptr != '\t')
    281  1.3   fvdl 					yptr++;
    282  1.3   fvdl 				*yptr = '\0';
    283  1.3   fvdl 				yylval.sym = symtable_get(yytext);
    284  1.3   fvdl 				string_buf_ptr = string_buf;
    285  1.3   fvdl 				BEGIN MACROBODY;
    286  1.3   fvdl 				return T_SYMBOL;
    287  1.3   fvdl 			}
    288  1.3   fvdl <MACRODEF>{WORD}\(	{
    289  1.3   fvdl 				/*
    290  1.3   fvdl 				 * We store the symbol with its opening
    291  1.3   fvdl 				 * parren so we can differentiate macros
    292  1.3   fvdl 				 * that take args from macros with the
    293  1.3   fvdl 				 * same name that do not take args as
    294  1.3   fvdl 				 * is allowed in C.
    295  1.3   fvdl 				 */
    296  1.3   fvdl 				BEGIN MACROARGLIST;
    297  1.3   fvdl 				yylval.sym = symtable_get(yytext);
    298  1.3   fvdl 				unput('(');
    299  1.3   fvdl 				return T_SYMBOL;
    300  1.3   fvdl 			}
    301  1.3   fvdl <MACROARGLIST>{WORD}	{
    302  1.3   fvdl 				yylval.str = yytext;
    303  1.3   fvdl 				return T_ARG;
    304  1.3   fvdl 			}
    305  1.3   fvdl <MACROARGLIST>{SPACE}   ;
    306  1.3   fvdl <MACROARGLIST>[(,]	{
    307  1.3   fvdl 				return yytext[0];
    308  1.3   fvdl 			}
    309  1.3   fvdl <MACROARGLIST>[)]	{
    310  1.3   fvdl 				string_buf_ptr = string_buf;
    311  1.3   fvdl 				BEGIN MACROBODY;
    312  1.3   fvdl 				return ')';
    313  1.3   fvdl 			}
    314  1.3   fvdl <MACROARGLIST>.		{
    315  1.3   fvdl 				snprintf(buf, sizeof(buf), "Invalid character "
    316  1.3   fvdl 					 "'%c' in macro argument list",
    317  1.3   fvdl 					 yytext[0]);
    318  1.3   fvdl 				stop(buf, EX_DATAERR);
    319  1.3   fvdl 			}
    320  1.3   fvdl <MACROCALLARGS>{SPACE}  ;
    321  1.3   fvdl <MACROCALLARGS>\(	{
    322  1.3   fvdl 				parren_count++;
    323  1.3   fvdl 				if (parren_count == 1)
    324  1.3   fvdl 					return ('(');
    325  1.3   fvdl 				*string_buf_ptr++ = '(';
    326  1.3   fvdl 			}
    327  1.3   fvdl <MACROCALLARGS>\)	{
    328  1.3   fvdl 				parren_count--;
    329  1.3   fvdl 				if (parren_count == 0) {
    330  1.3   fvdl 					BEGIN INITIAL;
    331  1.3   fvdl 					return (')');
    332  1.3   fvdl 				}
    333  1.3   fvdl 				*string_buf_ptr++ = ')';
    334  1.3   fvdl 			}
    335  1.3   fvdl <MACROCALLARGS>{MCARG}	{
    336  1.3   fvdl 				char *yptr;
    337  1.1   fvdl 
    338  1.3   fvdl 				yptr = yytext;
    339  1.3   fvdl 				while (*yptr)
    340  1.3   fvdl 					*string_buf_ptr++ = *yptr++;
    341  1.3   fvdl 			}
    342  1.3   fvdl <MACROCALLARGS>\,	{
    343  1.3   fvdl 				if (string_buf_ptr != string_buf) {
    344  1.3   fvdl 					/*
    345  1.3   fvdl 					 * Return an argument and
    346  1.3   fvdl 					 * rescan this comma so we
    347  1.3   fvdl 					 * can return it as well.
    348  1.3   fvdl 					 */
    349  1.3   fvdl 					*string_buf_ptr = '\0';
    350  1.3   fvdl 					yylval.str = string_buf;
    351  1.3   fvdl 					string_buf_ptr = string_buf;
    352  1.3   fvdl 					unput(',');
    353  1.3   fvdl 					return T_ARG;
    354  1.3   fvdl 				}
    355  1.3   fvdl 				return ',';
    356  1.3   fvdl 			}
    357  1.3   fvdl <MACROBODY>\\\n		{
    358  1.3   fvdl 				/* Eat escaped newlines. */
    359  1.3   fvdl 				++yylineno;
    360  1.3   fvdl 			}
    361  1.3   fvdl <MACROBODY>\n		{
    362  1.3   fvdl 				/* Macros end on the first unescaped newline. */
    363  1.3   fvdl 				BEGIN INITIAL;
    364  1.3   fvdl 				*string_buf_ptr = '\0';
    365  1.3   fvdl 				yylval.str = string_buf;
    366  1.3   fvdl 				++yylineno;
    367  1.3   fvdl 				return T_MACROBODY;
    368  1.3   fvdl 			}
    369  1.3   fvdl <MACROBODY>{MBODY}	{
    370  1.3   fvdl 				char *yptr;
    371  1.1   fvdl 
    372  1.3   fvdl 				yptr = yytext;
    373  1.3   fvdl 				while (*yptr)
    374  1.3   fvdl 					*string_buf_ptr++ = *yptr++;
    375  1.3   fvdl 			}
    376  1.3   fvdl {WORD}\(		{
    377  1.3   fvdl 				char *yptr;
    378  1.3   fvdl 				char *ycopy;
    379  1.3   fvdl 
    380  1.3   fvdl 				/* May be a symbol or a macro invocation. */
    381  1.3   fvdl 				yylval.sym = symtable_get(yytext);
    382  1.3   fvdl 				if (yylval.sym->type == MACRO) {
    383  1.3   fvdl 					YY_BUFFER_STATE old_state;
    384  1.3   fvdl 					YY_BUFFER_STATE temp_state;
    385  1.3   fvdl 
    386  1.3   fvdl 					ycopy = strdup(yytext);
    387  1.3   fvdl 					yptr = ycopy + yyleng;
    388  1.3   fvdl 					while (yptr > ycopy)
    389  1.3   fvdl 						unput(*--yptr);
    390  1.3   fvdl 					old_state = YY_CURRENT_BUFFER;
    391  1.3   fvdl 					temp_state =
    392  1.3   fvdl 					    yy_create_buffer(stdin,
    393  1.3   fvdl 							     YY_BUF_SIZE);
    394  1.3   fvdl 					yy_switch_to_buffer(temp_state);
    395  1.3   fvdl 					mm_switch_to_buffer(old_state);
    396  1.3   fvdl 					mmparse();
    397  1.3   fvdl 					mm_switch_to_buffer(temp_state);
    398  1.3   fvdl 					yy_switch_to_buffer(old_state);
    399  1.3   fvdl 					mm_delete_buffer(temp_state);
    400  1.3   fvdl 					expand_macro(yylval.sym);
    401  1.3   fvdl 				} else {
    402  1.3   fvdl 					if (yylval.sym->type == UNINITIALIZED) {
    403  1.3   fvdl 						/* Try without the '(' */
    404  1.3   fvdl 						symbol_delete(yylval.sym);
    405  1.3   fvdl 						yytext[yyleng-1] = '\0';
    406  1.3   fvdl 						yylval.sym =
    407  1.3   fvdl 						    symtable_get(yytext);
    408  1.3   fvdl 					}
    409  1.3   fvdl 					unput('(');
    410  1.3   fvdl 					return T_SYMBOL;
    411  1.3   fvdl 				}
    412  1.3   fvdl 			}
    413  1.3   fvdl {WORD}			{
    414  1.3   fvdl 				yylval.sym = symtable_get(yytext);
    415  1.3   fvdl 				if (yylval.sym->type == MACRO) {
    416  1.3   fvdl 					expand_macro(yylval.sym);
    417  1.3   fvdl 				} else {
    418  1.3   fvdl 					return T_SYMBOL;
    419  1.3   fvdl 				}
    420  1.3   fvdl 			}
    421  1.4  perry .			{
    422  1.1   fvdl 				snprintf(buf, sizeof(buf), "Invalid character "
    423  1.1   fvdl 					 "'%c'", yytext[0]);
    424  1.1   fvdl 				stop(buf, EX_DATAERR);
    425  1.1   fvdl 			}
    426  1.1   fvdl %%
    427  1.1   fvdl 
    428  1.1   fvdl typedef struct include {
    429  1.1   fvdl         YY_BUFFER_STATE  buffer;
    430  1.1   fvdl         int              lineno;
    431  1.1   fvdl         char            *filename;
    432  1.1   fvdl 	SLIST_ENTRY(include) links;
    433  1.1   fvdl }include_t;
    434  1.1   fvdl 
    435  1.1   fvdl SLIST_HEAD(, include) include_stack;
    436  1.1   fvdl 
    437  1.1   fvdl void
    438  1.3   fvdl include_file(char *file_name, include_type type)
    439  1.1   fvdl {
    440  1.1   fvdl 	FILE *newfile;
    441  1.1   fvdl 	include_t *include;
    442  1.1   fvdl 
    443  1.1   fvdl 	newfile = NULL;
    444  1.1   fvdl 	/* Try the current directory first */
    445  1.1   fvdl 	if (includes_search_curdir != 0 || type == SOURCE_FILE)
    446  1.1   fvdl 		newfile = fopen(file_name, "r");
    447  1.1   fvdl 
    448  1.1   fvdl 	if (newfile == NULL && type != SOURCE_FILE) {
    449  1.1   fvdl                 path_entry_t include_dir;
    450  1.3   fvdl                 for (include_dir = search_path.slh_first;
    451  1.4  perry                      include_dir != NULL;
    452  1.3   fvdl                      include_dir = include_dir->links.sle_next) {
    453  1.1   fvdl 			char fullname[PATH_MAX];
    454  1.1   fvdl 
    455  1.1   fvdl 			if ((include_dir->quoted_includes_only == TRUE)
    456  1.1   fvdl 			 && (type != QUOTED_INCLUDE))
    457  1.1   fvdl 				continue;
    458  1.1   fvdl 
    459  1.1   fvdl 			snprintf(fullname, sizeof(fullname),
    460  1.1   fvdl 				 "%s/%s", include_dir->directory, file_name);
    461  1.1   fvdl 
    462  1.1   fvdl 			if ((newfile = fopen(fullname, "r")) != NULL)
    463  1.1   fvdl 				break;
    464  1.1   fvdl                 }
    465  1.1   fvdl         }
    466  1.1   fvdl 
    467  1.1   fvdl 	if (newfile == NULL) {
    468  1.1   fvdl 		perror(file_name);
    469  1.1   fvdl 		stop("Unable to open input file", EX_SOFTWARE);
    470  1.1   fvdl 		/* NOTREACHED */
    471  1.1   fvdl 	}
    472  1.1   fvdl 
    473  1.1   fvdl 	if (type != SOURCE_FILE) {
    474  1.1   fvdl 		include = (include_t *)malloc(sizeof(include_t));
    475  1.1   fvdl 		if (include == NULL) {
    476  1.1   fvdl 			stop("Unable to allocate include stack entry",
    477  1.1   fvdl 			     EX_SOFTWARE);
    478  1.1   fvdl 			/* NOTREACHED */
    479  1.1   fvdl 		}
    480  1.1   fvdl 		include->buffer = YY_CURRENT_BUFFER;
    481  1.1   fvdl 		include->lineno = yylineno;
    482  1.1   fvdl 		include->filename = yyfilename;
    483  1.1   fvdl 		SLIST_INSERT_HEAD(&include_stack, include, links);
    484  1.1   fvdl 	}
    485  1.1   fvdl 	yy_switch_to_buffer(yy_create_buffer(newfile, YY_BUF_SIZE));
    486  1.1   fvdl 	yylineno = 1;
    487  1.1   fvdl 	yyfilename = strdup(file_name);
    488  1.1   fvdl }
    489  1.1   fvdl 
    490  1.3   fvdl static void next_substitution(struct symbol *mac_symbol, const char *body_pos,
    491  1.3   fvdl 			      const char **next_match,
    492  1.3   fvdl 			      struct macro_arg **match_marg, regmatch_t *match);
    493  1.3   fvdl 
    494  1.3   fvdl void
    495  1.3   fvdl expand_macro(struct symbol *macro_symbol)
    496  1.3   fvdl {
    497  1.3   fvdl 	struct macro_arg *marg;
    498  1.3   fvdl 	struct macro_arg *match_marg;
    499  1.3   fvdl 	const char *body_head;
    500  1.3   fvdl 	const char *body_pos;
    501  1.3   fvdl 	const char *next_match;
    502  1.3   fvdl 
    503  1.3   fvdl 	/*
    504  1.3   fvdl 	 * Due to the nature of unput, we must work
    505  1.3   fvdl 	 * backwards through the macro body performing
    506  1.3   fvdl 	 * any expansions.
    507  1.3   fvdl 	 */
    508  1.3   fvdl 	body_head = macro_symbol->info.macroinfo->body;
    509  1.3   fvdl 	body_pos = body_head + strlen(body_head);
    510  1.3   fvdl 	while (body_pos > body_head) {
    511  1.3   fvdl 		regmatch_t match;
    512  1.3   fvdl 
    513  1.3   fvdl 		next_match = body_head;
    514  1.3   fvdl 		match_marg = NULL;
    515  1.3   fvdl 		next_substitution(macro_symbol, body_pos, &next_match,
    516  1.3   fvdl 				  &match_marg, &match);
    517  1.3   fvdl 
    518  1.3   fvdl 		/* Put back everything up until the replacement. */
    519  1.3   fvdl 		while (body_pos > next_match)
    520  1.3   fvdl 			unput(*--body_pos);
    521  1.3   fvdl 
    522  1.3   fvdl 		/* Perform the replacement. */
    523  1.3   fvdl 		if (match_marg != NULL) {
    524  1.3   fvdl 			const char *strp;
    525  1.3   fvdl 
    526  1.3   fvdl 			next_match = match_marg->replacement_text;
    527  1.3   fvdl 			strp = next_match + strlen(next_match);
    528  1.3   fvdl 			while (strp > next_match)
    529  1.3   fvdl 				unput(*--strp);
    530  1.3   fvdl 
    531  1.3   fvdl 			/* Skip past the unexpanded macro arg. */
    532  1.3   fvdl 			body_pos -= match.rm_eo - match.rm_so;
    533  1.3   fvdl 		}
    534  1.3   fvdl 	}
    535  1.3   fvdl 
    536  1.3   fvdl 	/* Cleanup replacement text. */
    537  1.3   fvdl 	STAILQ_FOREACH(marg, &macro_symbol->info.macroinfo->args, links) {
    538  1.3   fvdl 		free(marg->replacement_text);
    539  1.3   fvdl 	}
    540  1.3   fvdl }
    541  1.3   fvdl 
    542  1.3   fvdl /*
    543  1.3   fvdl  * Find the next substitution in the macro working backwards from
    544  1.3   fvdl  * body_pos until the beginning of the macro buffer.  next_match
    545  1.3   fvdl  * should be initialized to the beginning of the macro buffer prior
    546  1.3   fvdl  * to calling this routine.
    547  1.3   fvdl  */
    548  1.3   fvdl static void
    549  1.3   fvdl next_substitution(struct symbol *mac_symbol, const char *body_pos,
    550  1.3   fvdl 		  const char **next_match, struct macro_arg **match_marg,
    551  1.3   fvdl 		  regmatch_t *match)
    552  1.3   fvdl {
    553  1.3   fvdl 	regmatch_t	  matches[2];
    554  1.3   fvdl 	struct macro_arg *marg;
    555  1.3   fvdl 	const char	 *search_pos;
    556  1.3   fvdl 	int		  retval;
    557  1.3   fvdl 
    558  1.3   fvdl 	do {
    559  1.3   fvdl 		search_pos = *next_match;
    560  1.3   fvdl 
    561  1.3   fvdl 		STAILQ_FOREACH(marg, &mac_symbol->info.macroinfo->args, links) {
    562  1.3   fvdl 
    563  1.3   fvdl 			retval = regexec(&marg->arg_regex, search_pos, 2,
    564  1.3   fvdl 					 matches, 0);
    565  1.3   fvdl 			if (retval == 0
    566  1.3   fvdl 			 && (matches[1].rm_eo + search_pos) <= body_pos
    567  1.3   fvdl 			 && (matches[1].rm_eo + search_pos) > *next_match) {
    568  1.3   fvdl 				*match = matches[1];
    569  1.3   fvdl 				*next_match = match->rm_eo + search_pos;
    570  1.3   fvdl 				*match_marg = marg;
    571  1.3   fvdl 			}
    572  1.3   fvdl 		}
    573  1.3   fvdl 	} while (search_pos != *next_match);
    574  1.3   fvdl }
    575  1.3   fvdl 
    576  1.1   fvdl int
    577  1.1   fvdl yywrap()
    578  1.1   fvdl {
    579  1.1   fvdl 	include_t *include;
    580  1.1   fvdl 
    581  1.1   fvdl 	yy_delete_buffer(YY_CURRENT_BUFFER);
    582  1.1   fvdl 	(void)fclose(yyin);
    583  1.1   fvdl 	if (yyfilename != NULL)
    584  1.1   fvdl 		free(yyfilename);
    585  1.1   fvdl 	yyfilename = NULL;
    586  1.3   fvdl 	include = include_stack.slh_first;
    587  1.1   fvdl 	if (include != NULL) {
    588  1.1   fvdl 		yy_switch_to_buffer(include->buffer);
    589  1.1   fvdl 		yylineno = include->lineno;
    590  1.1   fvdl 		yyfilename = include->filename;
    591  1.1   fvdl 		SLIST_REMOVE_HEAD(&include_stack, links);
    592  1.1   fvdl 		free(include);
    593  1.1   fvdl 		return (0);
    594  1.1   fvdl 	}
    595  1.1   fvdl 	return (1);
    596  1.1   fvdl }
    597