PERFORCE change 167161 for review

Tatsiana Elavaya tsel at FreeBSD.org
Mon Aug 10 06:21:55 UTC 2009


http://perforce.freebsd.org/chv.cgi?CH=167161

Change 167161 by tsel at tsel_mz on 2009/08/10 06:21:30

	Reimplement rule dump
	Escape strings with spaces
	Check if same name is used to define cond or ruleset
	Use file command line argument
	More tests

Affected files ...

.. //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/Makefile#3 edit
.. //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/ipfw.hll.c#3 edit
.. //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/ipfw.hll.h#3 edit
.. //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/parse.y#3 edit
.. //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/subr.c#2 edit
.. //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/test/Makefile#2 edit
.. //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/test/t_dup_name1#1 add
.. //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/test/t_dup_name1.err#1 add
.. //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/test/t_dup_name2#1 add
.. //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/test/t_dup_name2.err#1 add
.. //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/test/test1.err#2 edit
.. //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/test/test2#2 edit
.. //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/test/test2.err#2 edit
.. //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/test/test3.output#2 edit
.. //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/test/test4#2 edit
.. //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/test/test4.output#1 add
.. //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/test/test5#1 add
.. //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/test/test5.output#1 add
.. //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/test/test6#1 add
.. //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/test/test6.output#1 add
.. //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/test/test7#1 add
.. //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/test/test7.output#1 add
.. //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/token.l#3 edit

Differences ...

==== //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/Makefile#3 (text+ko) ====

@@ -11,7 +11,7 @@
 DPADD=	${LIBL}
 LDADD=	-ll
 
-DEBUG_FLAGS+= -g
+DEBUG_FLAGS+= -g -O0
 
 .PHONY: test
 test:

==== //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/ipfw.hll.c#3 (text+ko) ====

@@ -24,6 +24,7 @@
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <err.h>
 #include <sysexits.h>
@@ -33,225 +34,166 @@
 struct ruleset *toplevel_ruleset;
 
 extern int yyparse();
+extern const char * yyfile;
 
-#define TRACE_ACTIONS		0x001
-#define TRACE_CONDS		0x002
-#define TRACE_ALL		(TRACE_ACTIONS | TRACE_CONDS)
-
-struct trace_stack;
-struct trace_entry;
-
-typedef int (*trace_func_t)(struct trace_stack *stack, struct trace_entry *t);
-
-struct trace_entry {
-	int cmds_pos;
-	void *arg;
-	void *data;
-	int flags;
-	trace_func_t func;
-};
-
-struct trace_stack {
-	char **cmds;
-	int last_push;
-	int cmds_used;
-	int cmds_capacity;
-};
+static void expand_rule(struct rule *rule, struct ruleset *ruleset);
+static void expand_cond(struct cond *cond, struct condset *condset);
 
 static void
-trace_push_cmd(struct trace_stack *stack, char *cmd)
+expand_ruleset(struct ruleset *ruleset)
 {
-	if (stack->cmds_used == stack->cmds_capacity) {
-		stack->cmds_capacity *= 2;
-		stack->cmds = safe_realloc(stack->cmds,
-				stack->cmds_capacity * sizeof(void*));
+	struct rule *r, *rtmp;
+
+	TAILQ_FOREACH_SAFE(r, &ruleset->rules, rule_entries, rtmp) {
+		expand_rule(r, ruleset);
 	}
-	stack->cmds[stack->cmds_used++] = cmd;
-}
-
-static inline void
-trace_entry_init(struct trace_entry *t, void *arg, trace_func_t func, int flags)
-{
-	t->arg = arg;
-	t->flags = flags;
-	t->data = NULL;
-	t->func = func;
 }
 
 static void
-trace_complete(struct trace_stack *stack)
+expand_rule(struct rule *rule, struct ruleset *ruleset)
 {
-	int i;
+	struct rule *ri;
 
-	if (!stack->cmds_used)
-		return;
-
-	printf("add ");
-	for (i = 0; i < stack->cmds_used; i++) {
-		printf("%s ", stack->cmds[i]);
+	if (rule->action_ruleset) {
+		expand_ruleset(rule->action_ruleset);
+		while (!TAILQ_EMPTY(&rule->action_ruleset->rules)) {
+			ri = TAILQ_FIRST(&rule->action_ruleset->rules);
+			cmds_copy(&ri->cond->cmds, 0, TAILQ_FIRST(&rule->cond->cmds), NULL);
+			TAILQ_REMOVE(&rule->action_ruleset->rules, ri, rule_entries);
+			TAILQ_INSERT_BEFORE(rule, ri, rule_entries);
+		}
+		TAILQ_REMOVE(&ruleset->rules, rule, rule_entries);
 	}
-	printf("\n");
 }
 
-static inline int
-trace(struct trace_stack *stack, struct trace_entry *t_parent, struct trace_entry *t)
+static void
+expand_condset(struct condset *condset)
 {
-	int r;
+	struct cond *c, *ctmp;
 
-	stack->last_push = 1;
-	t->cmds_pos = stack->cmds_used;
-	r = t->func(stack, t);
-
-	return (r);
-}
-
-static inline void
-trace_pop(struct trace_stack *stack, struct trace_entry *t)
-{
-	if (stack->last_push) {
-		trace_complete(stack);
-		stack->last_push = 0;
+	TAILQ_FOREACH_SAFE(c, &condset->conds, cond_entries, ctmp) {
+		expand_cond(c, condset);
 	}
-	stack->cmds_used = t->cmds_pos;
 }
 
-static int trace_rule(struct trace_stack *stack, struct trace_entry *t);
-static int trace_cond(struct trace_stack *stack, struct trace_entry *t);
-
-static int
-trace_ruleset(struct trace_stack *stack, struct trace_entry *t)
+static void
+expand_cond(struct cond *cond, struct condset *condset)
 {
-	struct trace_entry t_rule;
-	struct ruleset *ruleset = t->arg;
-	struct rule *rule = t->data;
-
-	if (rule == NULL) {
-		if (TAILQ_EMPTY(&ruleset->rules)) {
-			return (0);
-		}
-		rule = TAILQ_FIRST(&ruleset->rules);
-	}
-	trace_entry_init(&t_rule, rule, trace_rule, t->flags);
-	trace(stack, t, &t_rule);
-	rule = TAILQ_NEXT(rule, rule_entries);
-	t->data = rule;
-	if (rule == NULL) {
-		return (0);
-	}
-	return (1);
-}
-
-static int
-trace_rule(struct trace_stack *stack, struct trace_entry *t)
-{
-	struct trace_entry t_cond;
-	struct rule *rule = t->arg;
+	struct cond *ci, *c, *begin, *end;
 	struct cmd *cmd;
-	int flags = t->flags;
 
-	if (flags & TRACE_ACTIONS) {
-		if (0 || rule->action_ruleset) {
-#if 0
-			tnext = trace(stack, tnext, rule->action_ruleset, trace_ruleset, TRACE_ACTIONS);
-#endif
-		} else {
-			TAILQ_FOREACH(cmd, &rule->actions, cmd_entries) {
-				trace_push_cmd(stack, cmd->cmd);
+	begin = TAILQ_PREV(cond, cond_head, cond_entries);
+	end = TAILQ_NEXT(cond, cond_entries);
+loop:
+	TAILQ_FOREACH_REVERSE(cmd, &cond->cmds, cmd_head, cmd_entries) {	
+		if (cmd->cmd_condset) {
+			expand_condset(cmd->cmd_condset);
+			TAILQ_FOREACH(c, &cmd->cmd_condset->conds, cond_entries) {
+				ci = cond_alloc();
+				ci->lineno = cond->lineno;
+				cmds_copy(&ci->cmds, 1, TAILQ_FIRST(&cond->cmds), cmd);
+				cmds_copy(&ci->cmds, 1, TAILQ_FIRST(&c->cmds), NULL);
+				cmds_copy(&ci->cmds, 1, TAILQ_NEXT(cmd, cmd_entries), NULL);
+				TAILQ_INSERT_BEFORE(cond, ci, cond_entries);
 			}
+			TAILQ_REMOVE(&condset->conds, cond, cond_entries);
+			if (begin == NULL)
+				cond = TAILQ_FIRST(&condset->conds);
+			else
+				cond = TAILQ_NEXT(begin, cond_entries);
+			goto loop;
 		}
 	}
-
-	if (flags & TRACE_CONDS) {
-		trace_entry_init(&t_cond, rule->cond, trace_cond, TRACE_CONDS);
-		trace(stack, t, &t_cond);
-		trace_pop(stack, t);
-#if 0
-		if (rule->action_ruleset)
-			tnext = trace(stack, tnext, rule->action_ruleset, trace_ruleset, TRACE_CONDS);
-#endif
+	cond = TAILQ_NEXT(cond, cond_entries);
+	if (cond != end && cond != NULL) {
+		goto loop;
 	}
-	return (0);
 }
 
-static int
-trace_condset(struct trace_stack *stack, struct trace_entry *t)
+static void
+expand_rulecond(struct rule *rule, struct ruleset *ruleset)
 {
-	struct trace_entry t_cond;
-	struct condset *condset = t->arg;
-	struct cond *cond = t->data;
+	struct rule *ri;
+	struct condset *condset;
+	struct cond *c;
 
-	if (cond == NULL) {
-		if (TAILQ_EMPTY(&condset->conds)) {
-			return (0);
-		}
-		cond = TAILQ_FIRST(&condset->conds);
+	condset = condset_alloc();
+	TAILQ_INSERT_HEAD(&condset->conds, rule->cond, cond_entries);
+	rule->cond = NULL;
+	expand_condset(condset);
+	TAILQ_FOREACH(c, &condset->conds, cond_entries) {
+		ri = rule_alloc();
+		ri->actions = rule->actions;
+		ri->lineno = rule->lineno;
+		ri->cond = c;
+		TAILQ_INSERT_BEFORE(rule, ri, rule_entries);
 	}
-	trace_entry_init(&t_cond, cond, trace_cond, TRACE_CONDS);
-	trace(stack, t, &t_cond);
-	t->data = TAILQ_NEXT(cond, cond_entries);
-	if (t->data == NULL) {
-		return (0);
-	}
-	return (1);
+	TAILQ_REMOVE(&ruleset->rules, rule, rule_entries);
 }
 
-static int
-trace_cond(struct trace_stack *stack, struct trace_entry *t)
+static void
+show_ruleset(struct ruleset *ruleset)
 {
-	struct trace_entry t_condset;
-	struct trace_entry t_next;
-	struct cond *cond = t->arg;
-	struct cmd *cmd = t->data;
-	int r;
+	struct rule *r;
+	struct cmd *c;
+	char *cmdval;
 
-	if (cmd == NULL) {
-		if (TAILQ_EMPTY(&cond->cmds)) {
-			return (0);
+	TAILQ_FOREACH(r, &toplevel_ruleset->rules, rule_entries) {
+		printf("add ");
+		TAILQ_FOREACH(c, &r->actions, cmd_entries) {
+			if (c->cmd_condset) {
+				printf("@%s ", c->cmd_condset->name); 
+			} else {
+				printf("%s ", c->cmd); 
+			}
 		}
-		cmd = TAILQ_FIRST(&cond->cmds);
-	}
-	while (cmd) {
-		if (cmd->cmd_condset) {
-			trace_entry_init(&t_condset, cmd->cmd_condset, trace_condset, TRACE_CONDS);
-			cmd = TAILQ_NEXT(cmd, cmd_entries);
-			if (cmd != NULL) {
-				trace_entry_init(&t_next, cond, trace_cond, TRACE_CONDS);
-				t_next.data = cmd;
+		TAILQ_FOREACH(c, &r->cond->cmds, cmd_entries) {
+			if (c->cmd_condset) {
+				printf("@%s ", c->cmd_condset->name); 
+			} else {
+				cmdval = cmd_escape(c->cmd);
+				printf("%s ", cmdval); 
+				if (cmdval != c->cmd)
+					free(cmdval);
 			}
-			do {
-				r = trace(stack, t, &t_condset);
-				if (cmd != NULL) {
-					trace(stack, t, &t_next); /* trace_cond always completes in one pass */
-				}
-				trace_pop(stack, &t_condset);
-			} while (r);
-			break;
 		}
-		trace_push_cmd(stack, cmd->cmd);
-		cmd = TAILQ_NEXT(cmd, cmd_entries);
+		printf("\n", r->lineno);
 	}
-	return (0);
 }
 
 static void
-dump_rules(void)
+usage(void)
 {
-	struct trace_stack stack;
-	struct trace_entry t_ruleset;
+	fprintf(stderr, "usage: ipfw.hll file\n");
+        exit(EX_USAGE);
 
-	stack.cmds_used = 0;
-	stack.cmds_capacity = 100; 		/* Initial capacity. */
-	stack.cmds = safe_calloc(sizeof(void*) * stack.cmds_capacity);
-	trace_entry_init(&t_ruleset, toplevel_ruleset, trace_ruleset, TRACE_ALL);
-	while (trace(&stack, NULL, &t_ruleset))
-		;	/* nothong */
 }
 
 int
 main(int argc, char **argv)
 {
-	yyparse();
-	dump_rules();
+	struct rule *r, *rtmp;
+	int error;
+	
+	if (argc > 2) {
+		usage();
+	} else if (argc == 2) {
+		yyfile = argv[1];
+		if (freopen(yyfile, "r", stdin) == NULL)
+			err(EX_OSERR, "%s", yyfile);
+	} else {
+		yyfile = "<stdin>";
+	}
+
+	error = yyparse();
+	if (error != 0)
+		return (EX_DATAERR);
+
+	expand_ruleset(toplevel_ruleset);
+	TAILQ_FOREACH_SAFE(r, &toplevel_ruleset->rules, rule_entries, rtmp) {
+		expand_rulecond(r, toplevel_ruleset);
+	}
+	show_ruleset(toplevel_ruleset);
 
 	return (0);
 }

==== //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/ipfw.hll.h#3 (text+ko) ====

@@ -28,9 +28,21 @@
 
 #define DPRINTF(fmt, ...) fprintf(stderr, "%s: " fmt, __func__, ## __VA_ARGS__)
 
+TAILQ_HEAD(rule_head, rule);
+TAILQ_HEAD(cond_head, cond);
+TAILQ_HEAD(cmd_head, cmd);
+
 struct ruleset {
 	TAILQ_ENTRY(ruleset) ruleset_entries;
-	TAILQ_HEAD(, rule) rules;
+	struct rule_head rules;
+	char *name;
+	int count;
+	int lineno;
+};
+
+struct condset {
+	TAILQ_ENTRY(condset) condset_entries;
+	struct cond_head conds;
 	char *name;
 	int count;
 	int lineno;
@@ -38,23 +50,15 @@
 
 struct rule {
 	TAILQ_ENTRY(rule) rule_entries;
-	TAILQ_HEAD(, cmd) actions;
+	struct cmd_head actions;
 	struct ruleset *action_ruleset;
 	struct cond *cond;
 	int lineno;
 };
 
-struct condset {
-	TAILQ_ENTRY(condset) condset_entries;
-	TAILQ_HEAD(, cond) conds;
-	char *name;
-	int count;
-	int lineno;
-};
-
 struct cond {
 	TAILQ_ENTRY(cond) cond_entries;
-	TAILQ_HEAD(, cmd) cmds;
+	struct cmd_head cmds;
 	int lineno;
 };
 
@@ -65,8 +69,31 @@
 	int lineno;
 };
 
+struct var {
+	TAILQ_ENTRY(var) vars_entries;
+	char *name;
+	char *value;
+	int lineno;
+};
+
 extern struct ruleset *toplevel_ruleset;
 
 void *safe_calloc(int size);
 void *safe_realloc(void *mem, int size);
 
+int valid_name(char *name);
+struct ruleset * ruleset_alloc(void);
+struct ruleset * ruleset_lookup(char *name);
+void rulesets_insert(struct ruleset *ruleset);
+struct rule * rule_alloc(void);
+struct condset * condset_alloc(void);
+void condsets_insert(struct condset *condset);
+struct condset * condset_lookup(char *name);
+struct cond * cond_alloc(void);
+void cmds_copy(struct cmd_head *dst, int insert_tail, struct cmd *begin, struct cmd *end);
+struct cmd * cmd_alloc(void);
+char * cmd_escape(char *s);
+struct var * var_alloc(void);
+struct var * var_lookup(char *name);
+void var_insert(struct var *var);
+

==== //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/parse.y#3 (text+ko) ====

@@ -32,173 +32,11 @@
 
 #include "ipfw.hll.h"
 
-struct var {
-	TAILQ_ENTRY(var) vars_entries;
-	char *name;
-	char *value;
-	int lineno;
-};
-
 extern char *yytext;
+extern const char *yyfile;
 extern int yyline;
 extern int yylex();
 
-static TAILQ_HEAD(, ruleset) rulesets = TAILQ_HEAD_INITIALIZER(rulesets);
-static TAILQ_HEAD(, condset) condsets = TAILQ_HEAD_INITIALIZER(condsets);
-static TAILQ_HEAD(, var) vars = TAILQ_HEAD_INITIALIZER(vars);
-
-static int
-valid_name(char *name)
-{
-	char *s;
-
-	if (*name != '_' && !isalpha(*name))
-		return (0);
-	for (s = name + 1; *s; s++) {
-		if (*s != '_' && !isalnum(*s))
-			return (0);
-	}
-	return (1);
-}
-
-static struct ruleset *
-ruleset_alloc(void)
-{
-	struct ruleset *r;
-
-	r = safe_calloc(sizeof(struct ruleset));
-	r->lineno = yyline;
-	TAILQ_INIT(&r->rules);
-	return (r);
-}
-
-static struct ruleset *
-ruleset_lookup(char *name)
-{
-	struct ruleset *r;
-
-	TAILQ_FOREACH(r, &rulesets, ruleset_entries) {
-		if (strcmp(r->name, name) == 0)
-			return (r);
-	}
-	return (NULL);
-}
-
-static void
-rulesets_insert(struct ruleset *ruleset)
-{
-	struct ruleset *dup;
-
-	dup = ruleset_lookup(ruleset->name);
-	if (dup != NULL)
-		errx(EX_DATAERR, "line %d: ruleset '%s' is already defined at line %d",
-				ruleset->lineno, ruleset->name, dup->lineno);
-	TAILQ_INSERT_TAIL(&rulesets, ruleset, ruleset_entries);
-	ruleset->count++;
-}
-
-static struct rule *
-rule_alloc(void)
-{
-	struct rule *r;
-
-	r = safe_calloc(sizeof(struct rule));
-	TAILQ_INIT(&r->actions);
-	r->lineno = yyline;
-	return (r);
-}
-
-static struct condset *
-condset_alloc(void)
-{
-	struct condset *r;
-
-	r = safe_calloc(sizeof(struct condset));
-	r->lineno = yyline;
-	TAILQ_INIT(&r->conds);
-	return (r);
-}
-
-static struct condset *
-condset_lookup(char *name)
-{
-	struct condset *r;
-
-	TAILQ_FOREACH(r, &condsets, condset_entries) {
-		if (strcmp(r->name, name) == 0)
-			return (r);
-	}
-	return (NULL);
-}
-
-static void
-condsets_insert(struct condset *condset)
-{
-	struct condset *dup;
-
-	dup = condset_lookup(condset->name);
-	if (dup != NULL)
-		errx(EX_DATAERR, "line %d: condition '%s' is already defined at line %d",
-				condset->lineno, condset->name, dup->lineno);
-	TAILQ_INSERT_TAIL(&condsets, condset, condset_entries);
-	condset->count++;
-}
-
-static struct cond *
-cond_alloc(void)
-{
-	struct cond *r;
-
-	r = safe_calloc(sizeof(struct cond));
-	r->lineno = yyline;
-	TAILQ_INIT(&r->cmds);
-	return (r);
-}
-
-static struct cmd *
-cmd_alloc(void)
-{
-	struct cmd *r;
-
-	r = safe_calloc(sizeof(struct cmd));
-	r->lineno = yyline;
-	return (r);
-}
-
-static struct var *
-var_alloc(void)
-{
-	struct var *r;
-
-	r = safe_calloc(sizeof(struct var));
-	r->lineno = yyline;
-	return (r);
-}
-
-static struct var *
-var_lookup(char *name)
-{
-	struct var *r;
-
-	TAILQ_FOREACH(r, &vars, vars_entries) {
-		if (strcmp(r->name, name) == 0)
-			return (r);
-	}
-	return (NULL);
-}
-
-static void
-var_insert(struct var *var)
-{
-	struct var *dup;
-
-	dup = var_lookup(var->name);
-	if (dup != NULL)
-		errx(EX_DATAERR, "line %d: variable '%s' is already defined at line %d",
-				var->lineno, var->name, dup->lineno);
-	TAILQ_INSERT_TAIL(&vars, var, vars_entries);
-}
-
 %}
 
 %union {
@@ -242,7 +80,7 @@
 	: define_list rule_list
 		{ 
 			if ($2 == NULL)
-				errx(EX_DATAERR, "line %d: top level ruleset is empty", yyline);
+				errx(EX_DATAERR, "%s:%d: top level ruleset is empty", yyfile, yyline);
 			toplevel_ruleset = $2;
 		}
 	;
@@ -375,7 +213,7 @@
 			$$->lineno = $2.lineno;
 			$$->action_ruleset = ruleset_lookup($2.s);
 			if ($$->action_ruleset == NULL)
-				errx(EX_DATAERR, "line %d: ruleset not found: %s", $$->lineno, $2.s);
+				errx(EX_DATAERR, "%s:%d: ruleset not found: %s", yyfile, $$->lineno, $2.s);
 		}
 	| rule_action_list
 		{
@@ -437,7 +275,7 @@
 			$$->lineno = $2.lineno;
 			$$->cmd_condset = condset_lookup($2.s);
 			if ($$->cmd_condset == NULL)
-				errx(EX_DATAERR, "line %d: condition set not found: %s", $$->lineno, $2.s);
+				errx(EX_DATAERR, "%s:%d: condition set not found: %s", yyfile, $$->lineno, $2.s);
 		}
 	| str
 		{
@@ -451,7 +289,7 @@
 	: STR
 		{
 			if (!valid_name($1.s))
-				errx(EX_DATAERR, "line %d: invalid name '%s'", $1.lineno, $1.s);
+				errx(EX_DATAERR, "%s:%d: invalid name '%s'", yyfile, $1.lineno, $1.s);
 			$$ = $1;
 		}
 	;
@@ -468,13 +306,13 @@
 				v = p + 2;
 				if (p[1] != '{' || (p = strchr(v, '}')) == NULL) {
 					errx(EX_DATAERR,
-					    "line %d: syntax error: variable expansion failed", yyline);
+					    "%s:%d: syntax error: variable expansion failed", yyfile, yyline);
 				} else {
 					*p = '\0';
 					var = var_lookup(v);
 					*p = '}';
 					if (var == NULL)
-						errx(EX_DATAERR, "line %d: variable not found: %s", yyline, v);
+						errx(EX_DATAERR, "%s:%d: variable not found: %s", yyfile, yyline, v);
 					l = (v - s - 2) + strlen(var->value) + strlen(p + 1) + 1;
 					sn = safe_calloc(l);
 					memcpy(sn, s, v - s - 2);
@@ -483,20 +321,6 @@
 					s = sn;
 				}
 			}
-/*
-			if (strchr(s, ' ') != NULL || strchr(s, '"') != NULL) {
-				sn = safe_calloc(strlen(s) * 2 + 3);
-				p = sn;
-				*(p++) = '"';
-				for (; *s; s++, p++) {
-					if (*s == '"')
-						*(p++) = '\\';
-					*p = *s;
-				}
-				*(p++) = '"';
-				s = sn;
-			}
-*/
 			$$.s = s;
 			$$.lineno = $1.lineno;
 		}
@@ -507,9 +331,9 @@
 void yyerror(char *s)
 {
 	if (yytext)
-		errx(EX_DATAERR, "line %d: '%s': %s", yyline, yytext, s);
+		warnx("%s:%d: '%s': %s", yyfile, yyline, yytext, s);
 	else
-		errx(EX_DATAERR, "line %d: %s", yyline, s);
+		warnx("%s:%d: %s", yyfile, yyline, s);
 	
 }
 

==== //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/subr.c#2 (text+ko) ====

@@ -1,9 +1,18 @@
+#include <ctype.h>
+#include <err.h>
 #include <stdlib.h>
-#include <err.h>
+#include <string.h>
 #include <sysexits.h>
 
 #include "ipfw.hll.h"
 
+extern int yyline;
+extern const char * yyfile;
+
+static TAILQ_HEAD(, ruleset) rulesets = TAILQ_HEAD_INITIALIZER(rulesets);
+static TAILQ_HEAD(, condset) condsets = TAILQ_HEAD_INITIALIZER(condsets);
+static TAILQ_HEAD(, var) vars = TAILQ_HEAD_INITIALIZER(vars);
+
 void *safe_calloc(int size)
 {
 	void *r;
@@ -24,3 +33,204 @@
 	return r;
 }
 
+int
+valid_name(char *name)
+{
+	char *s;
+
+	if (*name != '_' && !isalpha(*name))
+		return (0);
+	for (s = name + 1; *s; s++) {
+		if (*s != '_' && !isalnum(*s))
+			return (0);
+	}
+	return (1);
+}
+
+struct ruleset *
+ruleset_alloc(void)
+{
+	struct ruleset *r;
+
+	r = safe_calloc(sizeof(struct ruleset));
+	r->lineno = yyline;
+	TAILQ_INIT(&r->rules);
+	return (r);
+}
+
+struct ruleset *
+ruleset_lookup(char *name)
+{
+	struct ruleset *r;
+
+	TAILQ_FOREACH(r, &rulesets, ruleset_entries) {
+		if (strcmp(r->name, name) == 0)
+			return (r);
+	}
+	return (NULL);
+}
+
+void
+rulesets_insert(struct ruleset *ruleset)
+{
+	struct condset *cdup;
+	struct ruleset *rdup;
+
+	rdup = ruleset_lookup(ruleset->name);
+	if (rdup != NULL)
+		errx(EX_DATAERR, "%s:%d: ruleset '%s' is already defined at line %d",
+				yyfile, ruleset->lineno, ruleset->name, rdup->lineno);
+	cdup = condset_lookup(ruleset->name);
+	if (cdup != NULL)
+		errx(EX_DATAERR, "%s:%d: condition '%s' is defined at line %d",
+				yyfile, ruleset->lineno, ruleset->name, cdup->lineno);
+	TAILQ_INSERT_TAIL(&rulesets, ruleset, ruleset_entries);
+	ruleset->count++;
+}
+
+struct rule *
+rule_alloc(void)
+{
+	struct rule *r;
+
+	r = safe_calloc(sizeof(struct rule));
+	TAILQ_INIT(&r->actions);
+	r->lineno = yyline;
+	r->cond = cond_alloc();
+	return (r);
+}
+
+struct condset *
+condset_alloc(void)
+{
+	struct condset *r;
+
+	r = safe_calloc(sizeof(struct condset));
+	r->lineno = yyline;
+	TAILQ_INIT(&r->conds);
+	return (r);
+}
+
+void
+condsets_insert(struct condset *condset)
+{
+	struct condset *cdup;
+	struct ruleset *rdup;
+
+	cdup = condset_lookup(condset->name);
+	if (cdup != NULL)
+		errx(EX_DATAERR, "%s:%d: condition '%s' is already defined at line %d",
+				yyfile, condset->lineno, condset->name, cdup->lineno);
+	rdup = ruleset_lookup(condset->name);
+	if (rdup != NULL)
+		errx(EX_DATAERR, "%s:%d: ruleset '%s' is defined at line %d",
+				yyfile, condset->lineno, condset->name, rdup->lineno);
+	TAILQ_INSERT_TAIL(&condsets, condset, condset_entries);
+	condset->count++;
+}
+
+struct condset *
+condset_lookup(char *name)
+{
+	struct condset *r;
+
+	TAILQ_FOREACH(r, &condsets, condset_entries) {
+		if (strcmp(r->name, name) == 0)
+			return (r);
+	}
+	return (NULL);
+}
+
+struct cond *
+cond_alloc(void)
+{
+	struct cond *r;
+
+	r = safe_calloc(sizeof(struct cond));
+	r->lineno = yyline;
+	TAILQ_INIT(&r->cmds);
+	return (r);
+}
+
+void
+cmds_copy(struct cmd_head *dst, int insert_tail, struct cmd *begin, struct cmd *end)
+{
+	struct cmd *i, *n;
+
+	for (i = begin; i != end && i != NULL; i = TAILQ_NEXT(i, cmd_entries)) {
+		n = safe_calloc(sizeof(struct cmd));
+		n->cmd = i->cmd;
+		n->cmd_condset = i->cmd_condset;
+		n->lineno = i->lineno;
+		if (insert_tail)
+			TAILQ_INSERT_TAIL(dst, n, cmd_entries);
+		else
+			TAILQ_INSERT_HEAD(dst, n, cmd_entries);
+	}
+}
+
+struct cmd *
+cmd_alloc(void)
+{
+	struct cmd *r;
+
+	r = safe_calloc(sizeof(struct cmd));
+	r->lineno = yyline;
+	return (r);
+}
+
+char *
+cmd_escape(char *s)
+{
+	char *sn, *p;
+
+	if (strchr(s, ' ') == NULL && strchr(s, '"') == NULL)
+		return s;
+	
+	sn = safe_calloc(strlen(s) * 2 + 3);
+	p = sn;
+	*(p++) = '"';
+	for (; *s; s++, p++) {
+		if (*s == '"')
+			*(p++) = '\\';
+		*p = *s;
+	}
+	*(p++) = '"';
+
+	return (sn);
+}
+
+struct var *
+var_alloc(void)
+{
+	struct var *r;
+
+	r = safe_calloc(sizeof(struct var));
+	r->lineno = yyline;
+	return (r);
+}
+
+struct var *
+var_lookup(char *name)
+{
+	struct var *r;
+
+	TAILQ_FOREACH(r, &vars, vars_entries) {
+		if (strcmp(r->name, name) == 0)
+			return (r);
+	}
+	return (NULL);
+}
+
+void
+var_insert(struct var *var)
+{
+	struct var *dup;
+
+	dup = var_lookup(var->name);
+	if (dup != NULL)
+		errx(EX_DATAERR, "%s:%d: variable '%s' is already defined at line %d",
+				yyfile, var->lineno, var->name, dup->lineno);
+	TAILQ_INSERT_TAIL(&vars, var, vars_entries);
+}
+

==== //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/test/Makefile#2 (text+ko) ====

@@ -1,7 +1,5 @@
-TESTS+= test0 test1
-
-# TODO
-TESTS+= test2 test3
+TESTS+= test0 test1 test2 test3 test4 test5 test6 test7
+TESTS+= t_dup_name1 t_dup_name2
 
 all: test
 

==== //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/test/test1.err#2 (text+ko) ====

@@ -1,1 +1,1 @@
-ipfw.hll: line 1: 'error': syntax error
+ipfw.hll: <stdin>:1: 'error': syntax error

==== //depot/projects/soc2009/tsel_ipfw/libexec/ipfw.hll/test/test2#2 (text+ko) ====

@@ -1,11 +1,9 @@
-define q { 
-	cond q11 q12
-	cond q21 q22
-}
+# sdfsdf
 

>>> TRUNCATED FOR MAIL (1000 lines) <<<


More information about the p4-projects mailing list