[apparmor] [PATCH 5/9] Add new processid fn that handles both quoted and unquoted ids

John Johansen john.johansen at canonical.com
Wed Dec 28 03:01:48 UTC 2011


There is a lot of duplication of code calling processqunquoted and
processquoted.  Move all this code to use the new processid fn.

Signed-off-by: John Johansen <john.johansen at canonical.com>
---
 parser/parser.h      |    1 +
 parser/parser_lex.l  |   76 ++++++-------------------------------------------
 parser/parser_misc.c |   11 +++++++
 3 files changed, 22 insertions(+), 66 deletions(-)

diff --git a/parser/parser.h b/parser/parser.h
index 3677506..346620a 100644
--- a/parser/parser.h
+++ b/parser/parser.h
@@ -271,6 +271,7 @@ extern struct var_string *split_out_var(char *string);
 extern void free_var_string(struct var_string *var);
 
 /* parser_misc.c */
+extern char *processid(char *string, int len);
 extern char *processquoted(char *string, int len);
 extern char *processunquoted(char *string, int len);
 extern int get_keyword_token(const char *keyword);
diff --git a/parser/parser_lex.l b/parser/parser_lex.l
index 2f2b7fc..5cb24fb 100644
--- a/parser/parser_lex.l
+++ b/parser/parser_lex.l
@@ -247,7 +247,7 @@ LT_EQUAL	<=
 }
 
 <SUB_NAME>{
-	{ID}+	{
+	({ID}+|{QUOTED_ID})	{
 			  /* Ugh, this is a gross hack. I used to use
 			   * {ID}+ to match all TOK_IDs, but that would
 			   * also match TOK_MODE + TOK_END_OF_RULE
@@ -256,25 +256,11 @@ LT_EQUAL	<=
 			   * match any random string, I go into a
 			   * separate state. */
 			DUMP_PREPROCESS;
-			yylval.id =  processunquoted(yytext, yyleng);
+			yylval.id =  processid(yytext, yyleng);
 			PDEBUG("Found sub name: \"%s\"\n",  yylval.id);
 			yy_pop_state();
 			return TOK_ID;
 		}
-	{QUOTED_ID}	{
-			  /* Ugh, this is a gross hack. I used to use
-			   * {ID}+ to match all TOK_IDs, but that would
-			   * also match TOK_MODE + TOK_END_OF_RULE
-			   * without any spaces in between (because it's
-			   * a longer match). So now, when I want to
-			   * match any random string, I go into a
-			   * separate state. */
-			DUMP_PREPROCESS;
-			yylval.id = processquoted(yytext, yyleng);
-			PDEBUG("Found sub name: \"%s\"\n", yylval.id);
-			yy_pop_state();
-			return TOK_ID;
-		}
 
 	[^\n]	{
 			DUMP_PREPROCESS;
@@ -284,21 +270,7 @@ LT_EQUAL	<=
 }
 
 <SUB_NAME2>{
-	{ID}+	{
-			  /* Ugh, this is a gross hack. I used to use
-			   * {ID}+ to match all TOK_IDs, but that would
-			   * also match TOK_MODE + TOK_END_OF_RULE
-			   * without any spaces in between (because it's
-			   * a longer match). So now, when I want to
-			   * match any random string, I go into a
-			   * separate state. */
-			DUMP_PREPROCESS;
-			yylval.id = processunquoted(yytext, yyleng);
-			PDEBUG("Found sub name: \"%s\"\n", yylval.id);
-			yy_pop_state();
-			return TOK_ID;
-		}
-	{QUOTED_ID}	{
+	({ID}+|{QUOTED_ID})	{
 			  /* Ugh, this is a gross hack. I used to use
 			   * {ID}+ to match all TOK_IDs, but that would
 			   * also match TOK_MODE + TOK_END_OF_RULE
@@ -307,7 +279,7 @@ LT_EQUAL	<=
 			   * match any random string, I go into a
 			   * separate state. */
 			DUMP_PREPROCESS;
-			yylval.id  = processquoted(yytext, yyleng);
+			yylval.id = processid(yytext, yyleng);
 			PDEBUG("Found sub name: \"%s\"\n", yylval.id);
 			yy_pop_state();
 			return TOK_ID;
@@ -364,16 +336,9 @@ LT_EQUAL	<=
 <ASSIGN_MODE>{
 	{WS}+		{ DUMP_PREPROCESS; /* Eat whitespace */ }
 
-	{ID}+		{
-			DUMP_PREPROCESS;
-			yylval.var_val = processunquoted(yytext, yyleng);
-			PDEBUG("Found assignment value: \"%s\"\n", yylval.var_val);
-			return TOK_VALUE;
-			}
-
-	{QUOTED_ID}	{
+	({ID}+|{QUOTED_ID})		{
 			DUMP_PREPROCESS;
-			yylval.var_val = processquoted(yytext, yyleng);
+			yylval.var_val = processid(yytext, yyleng);
 			PDEBUG("Found assignment value: \"%s\"\n", yylval.var_val);
 			return TOK_VALUE;
 			}
@@ -431,7 +396,7 @@ LT_EQUAL	<=
 			return TOK_ARROW;
 			}
 
-	{ID}+	{
+	({ID}+|{QUOTED_ID})	{
 			  /* Ugh, this is a gross hack. I used to use
 			   * {ID}+ to match all TOK_IDs, but that would
 			   * also match TOK_MODE + TOK_END_OF_RULE
@@ -440,25 +405,11 @@ LT_EQUAL	<=
 			   * match any random string, I go into a
 			   * separate state. */
 			DUMP_PREPROCESS;
-			yylval.id = processunquoted(yytext, yyleng);
+			yylval.id = processid(yytext, yyleng);
 			PDEBUG("Found change profile name: \"%s\"\n", yylval.id);
 			yy_pop_state();
 			return TOK_ID;
 		}
-	{QUOTED_ID}	{
-			  /* Ugh, this is a gross hack. I used to use
-			   * {ID}+ to match all TOK_IDs, but that would
-			   * also match TOK_MODE + TOK_END_OF_RULE
-			   * without any spaces in between (because it's
-			   * a longer match). So now, when I want to
-			   * match any random string, I go into a
-			   * separate state. */
-			DUMP_PREPROCESS;
-			yylval.id = processquoted(yytext, yyleng);
-			PDEBUG("Found change profile quoted name: \"%s\"\n", yylval.id);
-			yy_pop_state();
-			return TOK_ID;
-		}
 
 	{WS}+			{  DUMP_PREPROCESS; /* Ignoring whitespace */ }
 	[^\n]	{
@@ -568,16 +519,9 @@ LT_EQUAL	<=
 			return TOK_CLOSE;
 			}
 
-{PATHNAME}		{
-			DUMP_PREPROCESS;
-			yylval.id = processunquoted(yytext, yyleng);
-			PDEBUG("Found id: \"%s\"\n", yylval.id);
-			return TOK_ID;
-			}
-
-{QPATHNAME}		{
+({PATHNAME}|{QPATHNAME})		{
 			DUMP_PREPROCESS;
-			yylval.id = processquoted(yytext, yyleng);
+			yylval.id = processid(yytext, yyleng);
 			PDEBUG("Found id: \"%s\"\n", yylval.id);
 			return TOK_ID;
 			}
diff --git a/parser/parser_misc.c b/parser/parser_misc.c
index 5aafaec..e438d4e 100644
--- a/parser/parser_misc.c
+++ b/parser/parser_misc.c
@@ -17,6 +17,7 @@
 
 /* assistance routines */
 
+#include <assert.h>
 #include <ctype.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -397,6 +398,16 @@ char *processunquoted(char *string, int len)
 	return tmp;
 }
 
+char *processid(char *string, int len)
+{
+	/* lexer should never call this fn if len <= 0 */
+	assert(len > 0);
+
+	if (*string == '"')
+		return processquoted(string, len);
+	return processunquoted(string, len);
+}
+
 /* rewrite a quoted string substituting escaped characters for the
  * real thing.  Strip the quotes around the string */
 
-- 
1.7.7.3




More information about the AppArmor mailing list