[sr-dev] git:master: core: added import_file

Daniel-Constantin Mierla miconda at gmail.com
Tue Jul 5 09:25:13 CEST 2011


Module: sip-router
Branch: master
Commit: 1cdad56cd88cad2b0732818880ae326e6da5735c
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=1cdad56cd88cad2b0732818880ae326e6da5735c

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date:   Tue Jul  5 09:15:34 2011 +0200

core: added import_file

- import_file is similar to include_file but does not throws error if
  the file is not found:
- example: import_file "myfile.cfg"

---

 cfg.lex |  115 ++++++++++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 78 insertions(+), 37 deletions(-)

diff --git a/cfg.lex b/cfg.lex
index d7a3190..0518fea 100644
--- a/cfg.lex
+++ b/cfg.lex
@@ -158,7 +158,7 @@
 	} include_stack[MAX_INCLUDE_DEPTH];
 	static int include_stack_ptr = 0;
 
-	static int sr_push_yy_state(char *fin);
+	static int sr_push_yy_state(char *fin, int mode);
 	static int sr_pop_yy_state();
 
 	static struct sr_yy_fname {
@@ -177,7 +177,7 @@
 
 /* start conditions */
 %x STRING1 STRING2 STR_BETWEEN COMMENT COMMENT_LN ATTR SELECT AVP_PVAR PVAR_P 
-%x PVARID INCLF
+%x PVARID INCLF IMPTF
 %x LINECOMMENT DEFINE_ID DEFINE_EOL DEFINE_DATA IFDEF_ID IFDEF_EOL IFDEF_SKIP
 
 /* config script types : #!SER  or #!KAMAILIO or #!MAX_COMPAT */
@@ -251,8 +251,6 @@ CASE			"case"
 DEFAULT			"default"
 WHILE			"while"
 
-INCLUDEFILE     "include_file"
-
 CFG_SELECT	"cfg_select"
 CFG_RESET	"cfg_reset"
 
@@ -563,6 +561,10 @@ EAT_ABLE	[\ \t\b\r]
 SUBST       subst
 SUBSTDEF    substdef
 
+/* include files */
+INCLUDEFILE     "include_file"
+IMPORTFILE      "import_file"
+
 %%
 
 
@@ -650,6 +652,7 @@ SUBSTDEF    substdef
 <INITIAL>{WHILE}	{ count(); yylval.strval=yytext; return WHILE; }
 
 <INITIAL>{INCLUDEFILE}  { count(); BEGIN(INCLF); }
+<INITIAL>{IMPORTFILE}  { count(); BEGIN(IMPTF); }
 
 <INITIAL>{CFG_SELECT}	{ count(); yylval.strval=yytext; return CFG_SELECT; }
 <INITIAL>{CFG_RESET}	{ count(); yylval.strval=yytext; return CFG_RESET; }
@@ -1277,7 +1280,17 @@ SUBSTDEF    substdef
 
 <INCLF>[ \t]*      /* eat the whitespace */
 <INCLF>[^ \t\n]+   { /* get the include file name */
-				if(sr_push_yy_state(yytext)<0)
+				if(sr_push_yy_state(yytext, 0)<0)
+				{
+					LOG(L_CRIT, "error at %s line %d\n", (finame)?finame:"cfg", line);
+					exit(-1);
+				}
+				BEGIN(INITIAL);
+}
+
+<IMPTF>[ \t]*      /* eat the whitespace */
+<IMPTF>[^ \t\n]+   { /* get the import file name */
+				if(sr_push_yy_state(yytext, 1)<0)
 				{
 					LOG(L_CRIT, "error at %s line %d\n", (finame)?finame:"cfg", line);
 					exit(-1);
@@ -1444,14 +1457,16 @@ int yywrap()
 	return 1;
 }
 
-static int sr_push_yy_state(char *fin)
+static int sr_push_yy_state(char *fin, int mode)
 {
 	struct sr_yy_fname *fn = NULL;
+	FILE *fp = NULL;
 	char *x = NULL;
 	char *newf = NULL;
 #define MAX_INCLUDE_FNAME	128
 	char fbuf[MAX_INCLUDE_FNAME];
 	int i, j, l;
+	char *tmpfiname = 0;
 
 	if ( include_stack_ptr >= MAX_INCLUDE_DEPTH )
 	{
@@ -1505,54 +1520,80 @@ static int sr_push_yy_state(char *fin)
 	}
 	fbuf[j] = '\0';
 
-	include_stack[include_stack_ptr].state = YY_CURRENT_BUFFER;
-	include_stack[include_stack_ptr].line = line;
-	include_stack[include_stack_ptr].column = column;
-	include_stack[include_stack_ptr].startline = startline;
-	include_stack[include_stack_ptr].startcolumn = startcolumn;
-	include_stack[include_stack_ptr].finame = finame;
-	include_stack_ptr++;
-
-	line=1;
-	column=1;
-	startline=1;
-	startcolumn=1;
-
-	yyin = fopen(fbuf, "r" );
+	fp = fopen(fbuf, "r" );
 
-	if ( ! yyin )
+	if ( ! fp )
 	{
-		finame = (finame==0)?cfg_file:finame;
-		if(finame==0 || fbuf[0]=='/')
+		tmpfiname = (finame==0)?cfg_file:finame;
+		if(tmpfiname==0 || fbuf[0]=='/')
 		{
-			LOG(L_CRIT, "cannot open included file: %s\n", fin);
-			return -1;
+			if(mode==0)
+			{
+				LOG(L_CRIT, "cannot open included file: %s\n", fin);
+				return -1;
+			} else {
+				LOG(L_DBG, "importing file ignored: %s\n", fin);
+				return 0;
+			}
 		}
-		x = strrchr(finame, '/');
-		if(x)
+		x = strrchr(tmpfiname, '/');
+		if(x==NULL)
 		{
-			newf = (char*)pkg_malloc(x-finame+strlen(fbuf)+2);
-			if(newf==0)
+			/* nothing else to try */
+			if(mode==0)
 			{
-				LOG(L_CRIT, "no more pkg\n");
+				LOG(L_CRIT, "cannot open included file: %s\n", fin);
 				return -1;
+			} else {
+				LOG(L_DBG, "importing file ignored: %s\n", fin);
+				return 0;
 			}
-			newf[0] = '\0';
-			strncat(newf, finame, x-finame);
-			strcat(newf, "/");
-			strcat(newf, fbuf);
 		}
-		yyin = fopen(newf, "r" );
-		if ( ! yyin )
+
+		newf = (char*)pkg_malloc(x-tmpfiname+strlen(fbuf)+2);
+		if(newf==0)
 		{
-			LOG(L_CRIT, "cannot open included file: %s (%s)\n", fbuf, newf);
+			LOG(L_CRIT, "no more pkg\n");
 			return -1;
 		}
+		newf[0] = '\0';
+		strncat(newf, tmpfiname, x-tmpfiname);
+		strcat(newf, "/");
+		strcat(newf, fbuf);
+
+		fp = fopen(newf, "r" );
+		if ( fp==NULL )
+		{
+			pkg_free(newf);
+			if(mode==0)
+			{
+				LOG(L_CRIT, "cannot open included file: %s (%s)\n", fbuf, newf);
+				return -1;
+			} else {
+				LOG(L_DBG, "importing file ignored: %s (%s)\n", fbuf, newf);
+				return 0;
+			}
+		}
 		LOG(L_DBG, "including file: %s (%s)\n", fbuf, newf);
 	} else {
 		newf = fbuf;
 	}
 
+	include_stack[include_stack_ptr].state = YY_CURRENT_BUFFER;
+	include_stack[include_stack_ptr].line = line;
+	include_stack[include_stack_ptr].column = column;
+	include_stack[include_stack_ptr].startline = startline;
+	include_stack[include_stack_ptr].startcolumn = startcolumn;
+	include_stack[include_stack_ptr].finame = finame;
+	include_stack_ptr++;
+
+	line=1;
+	column=1;
+	startline=1;
+	startcolumn=1;
+
+	yyin = fp;
+
 	/* make a copy in PKG if does not exist */
 	fn = sr_yy_fname_list;
 	while(fn!=0)




More information about the sr-dev mailing list