xref: /titanic_44/usr/src/cmd/svc/svccfg/svccfg.l (revision 753a6d457b330b1b29b2d3eefcd0831116ce950d)
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 2008 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>export		{ BEGIN WORD; return (SCC_EXPORT); }
81 <INITIAL>archive	{ BEGIN WORD; return (SCC_ARCHIVE); }
82 <INITIAL>restore	{ BEGIN WORD; return (SCC_RESTORE); }
83 <INITIAL>apply		{ BEGIN WORD; return (SCC_APPLY); }
84 <INITIAL>extract	{ BEGIN WORD; return (SCC_EXTRACT); }
85 <INITIAL>repository	{ BEGIN WORD; return (SCC_REPOSITORY); }
86 <INITIAL>inventory	{ BEGIN WORD; return (SCC_INVENTORY); }
87 <INITIAL>set		{ BEGIN WORD; return (SCC_SET); }
88 <INITIAL>end		{ BEGIN WORD; return (SCC_END); }
89 <INITIAL>exit		{ BEGIN WORD; return (SCC_END); }
90 <INITIAL>quit		{ BEGIN WORD; return (SCC_END); }
91 <INITIAL>help		{ return (SCC_HELP); }
92 
93 <INITIAL>list		{ BEGIN WORD; return (SCC_LIST); }
94 <INITIAL>add		{ BEGIN WORD; return (SCC_ADD); }
95 <INITIAL>delete		{ BEGIN WORD; return (SCC_DELETE); }
96 <INITIAL>select		{ BEGIN WORD; return (SCC_SELECT); }
97 <INITIAL>unselect	{ BEGIN WORD; return (SCC_UNSELECT); }
98 
99 <INITIAL>listpg		{ BEGIN WORD; return (SCC_LISTPG); }
100 <INITIAL>addpg		{ BEGIN WORD; return (SCC_ADDPG); }
101 <INITIAL>delpg		{ BEGIN WORD; return (SCC_DELPG); }
102 <INITIAL>delhash	{ BEGIN WORD; return (SCC_DELHASH); }
103 <INITIAL>listprop	{ BEGIN WORD; return (SCC_LISTPROP); }
104 <INITIAL>setprop	{ BEGIN WORD; return (SCC_SETPROP); }
105 <INITIAL>delprop	{ BEGIN WORD; return (SCC_DELPROP); }
106 <INITIAL>editprop	{ BEGIN WORD; return (SCC_EDITPROP); }
107 <INITIAL>describe	{ BEGIN WORD; return (SCC_DESCRIBE); }
108 <INITIAL>addpropvalue	{ BEGIN WORD; return (SCC_ADDPROPVALUE); }
109 <INITIAL>delpropvalue	{ BEGIN WORD; return (SCC_DELPROPVALUE); }
110 <INITIAL>setenv		{ BEGIN WORD; return (SCC_SETENV); }
111 <INITIAL>unsetenv	{ BEGIN WORD; return (SCC_UNSETENV); }
112 
113 <INITIAL>listsnap	{ BEGIN WORD; return (SCC_LISTSNAP); }
114 <INITIAL>selectsnap	{ BEGIN WORD; return (SCC_SELECTSNAP); }
115 <INITIAL>revert		{ BEGIN WORD; return (SCC_REVERT); }
116 <INITIAL>refresh	{ BEGIN WORD; return (SCC_REFRESH); }
117 
118 [^ \t\n">=()]+		{
119 				if ((yylval.str = strdup(yytext)) == NULL) {
120 					yyerror(gettext("Out of memory"));
121 					exit(UU_EXIT_FATAL);
122 				}
123 
124 				return SCV_WORD;
125 			}
126 
127 \"([^"\\]|\\.)*\"	{
128 				/*
129 				 * double-quoted strings start at a
130 				 * double-quote, include characters other than
131 				 * double-quote and backslash, and
132 				 * backslashed-characters, and end with a
133 				 * double-quote.
134 				 */
135 
136 				char *str, *cp;
137 				int shift;
138 
139 				if ((str = strdup(yytext)) == NULL) {
140 					yyerror(gettext("Out of memory"));
141 					exit(UU_EXIT_FATAL);
142 				}
143 
144 				/* Strip out the backslashes. */
145 				for (cp = str, shift = 0; *cp != '\0'; ++cp) {
146 					if (*cp == '\\') {
147 						++cp;
148 
149 						/*
150 						 * This can't be null because
151 						 * the string always ends with
152 						 * a double-quote.
153 						 */
154 
155 						++shift;
156 						*(cp - shift) = *cp;
157 					} else if (shift != 0)
158 						*(cp - shift) = *cp;
159 				}
160 
161 				/* Nullify everything after trailing quote */
162 				*(cp - shift) = '\0';
163 
164 				yylval.str = str;
165 				return SCV_STRING;
166 			}
167 
168 \n			{
169 				est->sc_cmd_lineno++;
170 				BEGIN INITIAL;
171 				return (SCS_NEWLINE);
172 			}
173 
174 [ \t]+			;
175 
176 ">"			{ return SCS_REDIRECT; }
177 "="			{ return SCS_EQUALS; }
178 "("			{ ++parens; return SCS_LPAREN; }
179 ")"			{ --parens; return SCS_RPAREN; }
180 
181 .			{
182 				uu_die(gettext("unrecognized character %s\n"),
183 				    yytext);
184 			}
185 
186 %%
187 
188 int
189 yyerror(const char *s)
190 {
191 	return (0);
192 }
193 
194 static int
195 input(void)
196 {
197 	static int saw_eof = 0;
198 
199 	int c = engine_cmd_getc(est);
200 
201 	/*
202 	 * To ensure input is terminated, slip in a newline on EOF.
203 	 */
204 	if (c == EOF) {
205 		if (saw_eof)
206 			return (0);
207 
208 		saw_eof = 1;
209 		return ('\n');
210 	} else
211 		saw_eof = 0;
212 
213 	if (c == '\n')
214 		yylineno++;
215 
216 	return (c);
217 }
218 
219 static void
220 unput(int c)
221 {
222 	if (c == '\n')
223 		yylineno--;
224 
225 	(void) engine_cmd_ungetc(est, c == 0 ? EOF : c);
226 }
227 
228 static void
229 output(int c)
230 {
231 	char ch = c;
232 	engine_cmd_nputs(est, &ch, sizeof (ch));
233 }
234