From d7c69a5d5faaa4f78455e9c11b5f34317d9e1f37 Mon Sep 17 00:00:00 2001
From: Maciek Kaliszewski <kenon@go2.pl>
Date: Mon, 4 Jun 2001 03:09:11 +0000
Subject: [PATCH] Added support for RCINCLUDE directive. Now wrc ignores
 everything except preprocessor directives from included *.h *.c files.

---
 tools/wrc/ppl.l | 54 ++++++++++++++++++++++++++++++++++++++++++-------
 tools/wrc/ppy.y | 13 ++++++++++++
 2 files changed, 60 insertions(+), 7 deletions(-)

diff --git a/tools/wrc/ppl.l b/tools/wrc/ppl.l
index bb6f8f2a897..4b2500826b4 100644
--- a/tools/wrc/ppl.l
+++ b/tools/wrc/ppl.l
@@ -131,6 +131,7 @@
 %x pp_line
 %x pp_defined
 %x pp_ignore
+%x RCINCL
 
 ws	[ \v\f\t\r]
 cident	[a-zA-Z_][0-9a-zA-Z_]*
@@ -190,6 +191,7 @@ typedef struct bufferstackentry {
 	char		*include_filename;
 	int		include_ifdepth;
 	int		seen_junk;
+	int 		pass_data;
 } bufferstackentry_t;
 
 #define ALLOCBLOCKSIZE	(1 << 10)	/* Allocate these chunks at a time for string-buffers */
@@ -260,6 +262,8 @@ static int macexpstackidx = 0;
 static bufferstackentry_t bufferstack[MAXBUFFERSTACK];
 static int bufferstackidx = 0;
 
+static int pass_data=1;
+
 /*
  * Global variables
  */
@@ -509,7 +513,7 @@ includelogicentry_t *includelogiclist = NULL;
 	/*
 	 * Comment handling (almost all start-conditions)
 	 */
-<INITIAL,pp_pp,pp_ignore,pp_eol,pp_inc,pp_if,pp_ifd,pp_defined,pp_def,pp_define,pp_macro,pp_mbody>"/*"	yy_push_state(pp_comment);
+<INITIAL,pp_pp,pp_ignore,pp_eol,pp_inc,pp_if,pp_ifd,pp_defined,pp_def,pp_define,pp_macro,pp_mbody,RCINCL>"/*" yy_push_state(pp_comment);
 <pp_comment>[^*\n]*|"*"+[^*/\n]*	;
 <pp_comment>\n				newline(0);
 <pp_comment>"*"+"/"			yy_pop_state();
@@ -517,7 +521,7 @@ includelogicentry_t *includelogiclist = NULL;
 	/*
 	 * Remove C++ style comment (almost all start-conditions)
 	 */
-<INITIAL,pp_pp,pp_ignore,pp_eol,pp_inc,pp_if,pp_ifd,pp_defined,pp_def,pp_define,pp_macro,pp_mbody,pp_macscan>"//"[^\n]*	{
+<INITIAL,pp_pp,pp_ignore,pp_eol,pp_inc,pp_if,pp_ifd,pp_defined,pp_def,pp_define,pp_macro,pp_mbody,pp_macscan,RCINCL>"//"[^\n]*	{
 		if(pptext[ppleng-1] == '\\')
 			ppwarning("C++ style comment ends with an escaped newline (escape ignored)");
 	}
@@ -538,6 +542,8 @@ includelogicentry_t *includelogiclist = NULL;
 		case pp_mbody:
 		case pp_inc:
 		case pp_line:
+		case RCINCL:
+			if (yy_current_state()==RCINCL) yy_pop_state();
 			pplval.cptr = get_string();
 			return tDQSTRING;
 		default:
@@ -613,8 +619,13 @@ includelogicentry_t *includelogiclist = NULL;
 				pplval.cptr = xstrdup(pptext);
 				return tIDENT;
 			}
-			else
-				put_buffer(pptext, ppleng);
+			else {
+				if((yy_current_state()==INITIAL) && (strcasecmp(pptext,"RCINCLUDE")==0)){
+					yy_push_state(RCINCL);
+					return tRCINCLUDE;
+				}
+				else put_buffer(pptext, ppleng);
+			}
 		}
 		else if(!ppp->expanding)
 		{
@@ -652,6 +663,18 @@ includelogicentry_t *includelogiclist = NULL;
 	 */
 <pp_macexp>(\n)|(.)|(\\\r?(\n|.))	put_buffer(pptext, ppleng);
 
+<RCINCL>[A-Za-z0-9_\.\\/]+ {
+		pplval.cptr=xstrdup(pptext);
+        	yy_pop_state();
+		return tRCINCLUDEPATH;
+	}
+
+<RCINCL>{ws}+ ;
+
+<RCINCL>\"		{
+		new_string(); add_string(pptext,ppleng);yy_push_state(pp_dqs);
+	}
+
 	/*
 	 * This is a 'catch-all' rule to discover errors in the scanner
 	 * in an orderly manner.
@@ -1164,6 +1187,7 @@ static void push_buffer(pp_entry_t *ppp, char *filename, char *incname, int pop)
 	bufferstack[bufferstackidx].include_filename	= incname;
 	bufferstack[bufferstackidx].include_ifdepth	= include_ifdepth;
 	bufferstack[bufferstackidx].seen_junk		= seen_junk;
+	bufferstack[bufferstackidx].pass_data		= pass_data;
 
 	if(ppp)
 		ppp->expanding = 1;
@@ -1229,6 +1253,8 @@ static bufferstackentry_t *pop_buffer(void)
 			include_ppp	= bufferstack[bufferstackidx].include_ppp;
 			include_ifdepth	= bufferstack[bufferstackidx].include_ifdepth;
 			seen_junk	= bufferstack[bufferstackidx].seen_junk;
+			pass_data	= bufferstack[bufferstackidx].pass_data;
+
 		}
 	}
 
@@ -1386,8 +1412,10 @@ static void put_buffer(char *s, int len)
 {
 	if(top_macro())
 		add_text_to_macro(s, len);
-	else
-		fwrite(s, 1, len, ppout);
+	else {
+           if(pass_data)
+           fwrite(s, 1, len, ppout);           
+        }
 }
 
 
@@ -1396,6 +1424,15 @@ static void put_buffer(char *s, int len)
  * Include management
  *-------------------------------------------------------------------------
  */
+int is_c_h_include(char *fname)  
+{        
+	int sl=strlen(fname);
+	if (sl < 2) return 0;
+	if ((toupper(fname[sl-1])!='H') && (toupper(fname[sl-1])!='C')) return 0;
+        if (fname[sl-2]!='.') return 0;
+	return 1;
+}
+
 void do_include(char *fname, int type)
 {
 	char *newpath;
@@ -1431,8 +1468,11 @@ void do_include(char *fname, int type)
 	seen_junk = 0;
 	include_state = 0;
 	include_ppp = NULL;
+	if (is_c_h_include(newpath)) pass_data=0;
+	else pass_data=1;
+
 	if(debuglevel & DEBUGLEVEL_PPMSG)
-		fprintf(stderr, "do_include: %s:%d: include_state=%d, include_ppp='%s', include_ifdepth=%d\n", input_name, line_number, include_state, include_ppp, include_ifdepth);
+		fprintf(stderr, "do_include: %s:%d: include_state=%d, include_ppp='%s', include_ifdepth=%d ,pass_data=%d\n", input_name, line_number, include_state, include_ppp, include_ifdepth,pass_data);
 	pp_switch_to_buffer(pp_create_buffer(ppin, YY_BUF_SIZE));
 
 	fprintf(ppout, "# 1 \"%s\" 1%s\n", newpath, type ? "" : " 3"); 
diff --git a/tools/wrc/ppy.y b/tools/wrc/ppy.y
index 305b63d3ac5..f6c8c380a02 100644
--- a/tools/wrc/ppy.y
+++ b/tools/wrc/ppy.y
@@ -123,6 +123,7 @@ static int	nmacro_args;
 	mtext_t		*mtext;
 }
 
+%token tRCINCLUDE
 %token tIF tIFDEF tIFNDEF tELSE tELIF tENDIF tDEFINED tNL
 %token tINCLUDE tLINE tGCCLINE tERROR tWARNING tPRAGMA tPPIDENT
 %token tUNDEF tMACROEND tCONCAT tELIPSIS tSTRINGIZE
@@ -134,6 +135,8 @@ static int	nmacro_args;
 %token <slong> tSLONG
 %token <ull> tULONGLONG
 %token <sll> tSLONGLONG
+%token <cptr> tRCINCLUDEPATH
+
 %right '?' ':'
 %left tLOGOR
 %left tLOGAND
@@ -267,6 +270,16 @@ preprocessor
 	| tWARNING opt_text tNL	{ ppwarning("#warning directive: '%s'", $2); if($2) free($2); }
 	| tPRAGMA opt_text tNL	{ if(pedantic) ppwarning("#pragma ignored (arg: '%s')", $2); if($2) free($2); }
 	| tPPIDENT opt_text tNL	{ if(pedantic) ppwarning("#ident ignored (arg: '%s')", $2); if($2) free($2); }
+        | tRCINCLUDE tRCINCLUDEPATH {
+                int nl=strlen($2) +3;
+                char *fn=xmalloc(nl);
+                snprintf(fn,nl,"\"%s\"",$2);
+		free($2);
+		do_include(fn,1);
+	}
+	| tRCINCLUDE tDQSTRING {
+		do_include($2,1);
+	}
 	/*| tNL*/
 	;
 
-- 
GitLab