[apparmor] [PATCH 10/13] Make value_list generic so it can be reused.
John Johansen
john.johansen at canonical.com
Tue Feb 14 17:32:32 UTC 2012
value_list can be reused by conditionals and list values, so pull it out
and abstract it some more.
Signed-off-by: John Johansen <john.johansen at canonical.com>
---
parser/parser.h | 9 +++++++++
parser/parser_misc.c | 35 +++++++++++++++++++++++++++++++++++
parser/parser_yacc.y | 36 +++++++-----------------------------
3 files changed, 51 insertions(+), 29 deletions(-)
diff --git a/parser/parser.h b/parser/parser.h
index ebe0e29..6c1cc4f 100644
--- a/parser/parser.h
+++ b/parser/parser.h
@@ -49,6 +49,12 @@ struct cod_pattern {
char *regex; // posix regex
};
+struct value_list {
+ char *value;
+
+ struct value_list *next;
+};
+
struct cod_entry {
char *namespace;
char *name;
@@ -275,6 +281,9 @@ extern struct var_string *split_out_var(char *string);
extern void free_var_string(struct var_string *var);
/* parser_misc.c */
+extern struct value_list *new_value_list(char *value);
+extern void free_value_list(struct value_list *list);
+extern void print_value_list(struct value_list *list);
extern char *processid(char *string, int len);
extern char *processquoted(char *string, int len);
extern char *processunquoted(char *string, int len);
diff --git a/parser/parser_misc.c b/parser/parser_misc.c
index 8aca061..f1779c7 100644
--- a/parser/parser_misc.c
+++ b/parser/parser_misc.c
@@ -939,6 +939,41 @@ void debug_cod_list(struct codomain *cod)
dump_policy_hats(cod);
}
+struct value_list *new_value_list(char *value)
+{
+ struct value_list *val = calloc(1, sizeof(struct value_list));
+ if (val)
+ val->value = value;
+ return val;
+}
+
+void free_value_list(struct value_list *list)
+{
+ struct value_list *next;
+
+ while (list) {
+ next = list->next;
+ if (list->value)
+ free(list->value);
+ free(list);
+ list = next;
+ }
+}
+
+void print_value_list(struct value_list *list)
+{
+ struct value_list *entry;
+
+ if (!list)
+ return;
+
+ fprintf(stderr, "%s", list->value);
+ list = list->next;
+ list_for_each(list, entry) {
+ fprintf(stderr, ", %s", entry->value);
+ }
+}
+
#ifdef UNIT_TEST
int test_str_to_boolean(void)
{
diff --git a/parser/parser_yacc.y b/parser/parser_yacc.y
index 6dfbee8..27eacf1 100644
--- a/parser/parser_yacc.y
+++ b/parser/parser_yacc.y
@@ -64,14 +64,8 @@
#define CAP_TO_MASK(x) (1ull << (x))
-struct value_list {
- char *value;
- struct value_list *next;
-};
-
int parser_token = 0;
-void free_value_list(struct value_list *list);
struct cod_entry *do_file_rule(char *namespace, char *id, int mode,
char *link_id, char *nt);
@@ -378,26 +372,23 @@ varassign: TOK_BOOL_VAR TOK_EQUALS TOK_VALUE
valuelist: TOK_VALUE
{
- struct value_list *new = calloc(1, sizeof(struct value_list));
- if (!new)
+ struct value_list *val = new_value_list($1);
+ if (!val)
yyerror(_("Memory allocation error."));
PDEBUG("Matched: value (%s)\n", $1);
- new->value = $1;
- new->next = NULL;
- $$ = new;
+ $$ = val;
}
valuelist: valuelist TOK_VALUE
{
- struct value_list *new = calloc(1, sizeof(struct value_list));
- if (!new)
+ struct value_list *val = new_value_list($2);
+ if (!val)
yyerror(_("Memory allocation error."));
PDEBUG("Matched: value list\n");
- new->value = $2;
- new->next = $1;
- $$ = new;
+ val->next = $1;
+ $$ = val;
}
flags: { /* nothing */
@@ -1115,19 +1106,6 @@ void yyerror(char *msg, ...)
exit(1);
}
-void free_value_list(struct value_list *list)
-{
- struct value_list *next;
-
- while (list) {
- next = list->next;
- if (list->value)
- free(list->value);
- free(list);
- list = next;
- }
-}
-
struct cod_entry *do_file_rule(char *namespace, char *id, int mode,
char *link_id, char *nt)
{
--
1.7.9
More information about the AppArmor
mailing list