xref: /freebsd/contrib/dma/aliases_parse.y (revision c697fb7f)
1 %{
2 
3 #include <err.h>
4 #include <string.h>
5 #include <syslog.h>
6 #include "dma.h"
7 
8 extern int yylineno;
9 static void yyerror(const char *);
10 
11 static void
12 yyerror(const char *msg)
13 {
14 	/**
15 	 * Because we do error '\n' below, we need to report the error
16 	 * one line above of what yylineno points to.
17 	 */
18 	syslog(LOG_CRIT, "aliases line %d: %s", yylineno - 1, msg);
19 	fprintf(stderr, "aliases line %d: %s\n", yylineno - 1, msg);
20 }
21 
22 int
23 yywrap(void)
24 {
25 	return (1);
26 }
27 
28 %}
29 
30 %union {
31 	char *ident;
32 	struct stritem *strit;
33 	struct alias *alias;
34 }
35 
36 %token <ident> T_IDENT
37 %token T_ERROR
38 %token T_EOF 0
39 
40 %type <strit> dests
41 %type <alias> alias aliases
42 
43 %%
44 
45 start	: aliases T_EOF
46 		{
47 			LIST_FIRST(&aliases) = $1;
48 		}
49 
50 aliases	: /* EMPTY */
51 		{
52 			$$ = NULL;
53 		}
54 	| alias aliases
55 		{
56 			if ($2 != NULL && $1 != NULL)
57 				LIST_INSERT_AFTER($2, $1, next);
58 			else if ($2 == NULL)
59 				$2 = $1;
60 			$$ = $2;
61 		}
62        	;
63 
64 alias	: T_IDENT ':' dests '\n'
65 		{
66 			struct alias *al;
67 
68 			if ($1 == NULL)
69 				YYABORT;
70 			al = calloc(1, sizeof(*al));
71 			if (al == NULL)
72 				YYABORT;
73 			al->alias = $1;
74 			SLIST_FIRST(&al->dests) = $3;
75 			$$ = al;
76 		}
77 	| error '\n'
78 		{
79 			YYABORT;
80 		}
81      	;
82 
83 dests	: T_IDENT
84 		{
85 			struct stritem *it;
86 
87 			if ($1 == NULL)
88 				YYABORT;
89 			it = calloc(1, sizeof(*it));
90 			if (it == NULL)
91 				YYABORT;
92 			it->str = $1;
93 			$$ = it;
94 		}
95 	| T_IDENT ',' dests
96 		{
97 			struct stritem *it;
98 
99 			if ($1 == NULL)
100 				YYABORT;
101 			it = calloc(1, sizeof(*it));
102 			if (it == NULL)
103 				YYABORT;
104 			it->str = $1;
105 			SLIST_NEXT(it, next) = $3;
106 			$$ = it;
107 		}
108 	;
109 
110 %%
111