xref: /titanic_41/usr/src/cmd/svc/svccfg/svccfg.l (revision 3fb517f786391b507780c78aabb8d98bfea9efe9)
1 %{
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 
29 #pragma error_messages(off, E_BLOCK_DECL_UNUSED)
30 #pragma error_messages(off, E_EQUALITY_NOT_ASSIGNMENT)
31 #pragma error_messages(off, E_FUNC_RET_MAYBE_IGNORED2)
32 #pragma error_messages(off, E_STMT_NOT_REACHED)
33 
34 #include <libintl.h>
35 #include <string.h>
36 
37 #include "svccfg.h"
38 #include "svccfg_grammar.h"
39 
40 /*
41  * We need to undefine lex's input, unput, and output macros so that references
42  * to these call the functions we provide at the end of this source file,
43  * instead of the default versions based on libc's stdio.
44  */
45 #ifdef input
46 #undef input
47 #endif
48 
49 #ifdef unput
50 #undef unput
51 #endif
52 
53 #ifdef output
54 #undef output
55 #endif
56 
57 static int input(void);
58 static void unput(int);
59 static void output(int);
60 
61 int parens = 0;
62 
63 extern int yyerror(const char *);
64 
65 %}
66 
67 /*
68  * Since command tokens are only valid at the beginning of the command (or
69  * after help), we'll only return them in the INITIAL state, and report them
70  * as SCV_WORDs afterwards.
71  */
72 %Start	WORD
73 
74 %%
75 
76 #.*$			;	/* comments */
77 
78 <INITIAL>validate	{ BEGIN WORD; return (SCC_VALIDATE); }
79 <INITIAL>import		{ BEGIN WORD; return (SCC_IMPORT); }
80 <INITIAL>cleanup	{ BEGIN WORD; return (SCC_CLEANUP); }
81 <INITIAL>export		{ BEGIN WORD; return (SCC_EXPORT); }
82 <INITIAL>archive	{ BEGIN WORD; return (SCC_ARCHIVE); }
83 <INITIAL>restore	{ BEGIN WORD; return (SCC_RESTORE); }
84 <INITIAL>apply		{ BEGIN WORD; return (SCC_APPLY); }
85 <INITIAL>extract	{ BEGIN WORD; return (SCC_EXTRACT); }
86 <INITIAL>repository	{ BEGIN WORD; return (SCC_REPOSITORY); }
87 <INITIAL>inventory	{ BEGIN WORD; return (SCC_INVENTORY); }
88 <INITIAL>set		{ BEGIN WORD; return (SCC_SET); }
89 <INITIAL>end		{ BEGIN WORD; return (SCC_END); }
90 <INITIAL>exit		{ BEGIN WORD; return (SCC_END); }
91 <INITIAL>quit		{ BEGIN WORD; return (SCC_END); }
92 <INITIAL>help		{ return (SCC_HELP); }
93 
94 <INITIAL>list		{ BEGIN WORD; return (SCC_LIST); }
95 <INITIAL>add		{ BEGIN WORD; return (SCC_ADD); }
96 <INITIAL>delete		{ BEGIN WORD; return (SCC_DELETE); }
97 <INITIAL>select		{ BEGIN WORD; return (SCC_SELECT); }
98 <INITIAL>unselect	{ BEGIN WORD; return (SCC_UNSELECT); }
99 
100 <INITIAL>listpg		{ BEGIN WORD; return (SCC_LISTPG); }
101 <INITIAL>addpg		{ BEGIN WORD; return (SCC_ADDPG); }
102 <INITIAL>delpg		{ BEGIN WORD; return (SCC_DELPG); }
103 <INITIAL>delhash	{ BEGIN WORD; return (SCC_DELHASH); }
104 <INITIAL>listprop	{ BEGIN WORD; return (SCC_LISTPROP); }
105 <INITIAL>setprop	{ BEGIN WORD; return (SCC_SETPROP); }
106 <INITIAL>delprop	{ BEGIN WORD; return (SCC_DELPROP); }
107 <INITIAL>editprop	{ BEGIN WORD; return (SCC_EDITPROP); }
108 <INITIAL>describe	{ BEGIN WORD; return (SCC_DESCRIBE); }
109 <INITIAL>addpropvalue	{ BEGIN WORD; return (SCC_ADDPROPVALUE); }
110 <INITIAL>delpropvalue	{ BEGIN WORD; return (SCC_DELPROPVALUE); }
111 <INITIAL>setenv		{ BEGIN WORD; return (SCC_SETENV); }
112 <INITIAL>unsetenv	{ BEGIN WORD; return (SCC_UNSETENV); }
113 
114 <INITIAL>listsnap	{ BEGIN WORD; return (SCC_LISTSNAP); }
115 <INITIAL>selectsnap	{ BEGIN WORD; return (SCC_SELECTSNAP); }
116 <INITIAL>revert		{ BEGIN WORD; return (SCC_REVERT); }
117 <INITIAL>refresh	{ BEGIN WORD; return (SCC_REFRESH); }
118 
119 [^ \t\n">=()]+		{
120 				if ((yylval.str = strdup(yytext)) == NULL) {
121 					yyerror(gettext("Out of memory"));
122 					exit(UU_EXIT_FATAL);
123 				}
124 
125 				return SCV_WORD;
126 			}
127 
128 \"([^"\\]|\\.)*\"	{
129 				/*
130 				 * double-quoted strings start at a
131 				 * double-quote, include characters other than
132 				 * double-quote and backslash, and
133 				 * backslashed-characters, and end with a
134 				 * double-quote.
135 				 */
136 
137 				char *str, *cp;
138 				int shift;
139 
140 				if ((str = strdup(yytext)) == NULL) {
141 					yyerror(gettext("Out of memory"));
142 					exit(UU_EXIT_FATAL);
143 				}
144 
145 				/* Strip out the backslashes. */
146 				for (cp = str, shift = 0; *cp != '\0'; ++cp) {
147 					if (*cp == '\\') {
148 						++cp;
149 
150 						/*
151 						 * This can't be null because
152 						 * the string always ends with
153 						 * a double-quote.
154 						 */
155 
156 						++shift;
157 						*(cp - shift) = *cp;
158 					} else if (shift != 0)
159 						*(cp - shift) = *cp;
160 				}
161 
162 				/* Nullify everything after trailing quote */
163 				*(cp - shift) = '\0';
164 
165 				yylval.str = str;
166 				return SCV_STRING;
167 			}
168 
169 \n			{
170 				est->sc_cmd_lineno++;
171 				BEGIN INITIAL;
172 				return (SCS_NEWLINE);
173 			}
174 
175 [ \t]+			;
176 
177 ">"			{ return SCS_REDIRECT; }
178 "="			{ return SCS_EQUALS; }
179 "("			{ ++parens; return SCS_LPAREN; }
180 ")"			{ --parens; return SCS_RPAREN; }
181 
182 .			{
183 				uu_die(gettext("unrecognized character %s\n"),
184 				    yytext);
185 			}
186 
187 %%
188 
189 int
190 yyerror(const char *s)
191 {
192 	return (0);
193 }
194 
195 static int
196 input(void)
197 {
198 	static int saw_eof = 0;
199 
200 	int c = engine_cmd_getc(est);
201 
202 	/*
203 	 * To ensure input is terminated, slip in a newline on EOF.
204 	 */
205 	if (c == EOF) {
206 		if (saw_eof)
207 			return (0);
208 
209 		saw_eof = 1;
210 		return ('\n');
211 	} else
212 		saw_eof = 0;
213 
214 	if (c == '\n')
215 		yylineno++;
216 
217 	return (c);
218 }
219 
220 static void
221 unput(int c)
222 {
223 	if (c == '\n')
224 		yylineno--;
225 
226 	(void) engine_cmd_ungetc(est, c == 0 ? EOF : c);
227 }
228 
229 static void
230 output(int c)
231 {
232 	char ch = c;
233 	engine_cmd_nputs(est, &ch, sizeof (ch));
234 }
235