svccfg.l revision 5040:ff6ebd8761a6
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 2007 Sun Microsystems, Inc.  All rights reserved.
25 * Use is subject to license terms.
26 */
27
28#pragma ident	"%Z%%M%	%I%	%E% SMI"
29
30#pragma error_messages(off, E_BLOCK_DECL_UNUSED)
31#pragma error_messages(off, E_EQUALITY_NOT_ASSIGNMENT)
32#pragma error_messages(off, E_FUNC_RET_MAYBE_IGNORED2)
33#pragma error_messages(off, E_STMT_NOT_REACHED)
34
35#include <libintl.h>
36#include <string.h>
37
38#include "svccfg.h"
39#include "svccfg_grammar.h"
40
41/*
42 * We need to undefine lex's input, unput, and output macros so that references
43 * to these call the functions we provide at the end of this source file,
44 * instead of the default versions based on libc's stdio.
45 */
46#ifdef input
47#undef input
48#endif
49
50#ifdef unput
51#undef unput
52#endif
53
54#ifdef output
55#undef output
56#endif
57
58static int input(void);
59static void unput(int);
60static void output(int);
61
62int parens = 0;
63
64extern int yyerror(const char *);
65
66%}
67
68/*
69 * Since command tokens are only valid at the beginning of the command (or
70 * after help), we'll only return them in the INITIAL state, and report them
71 * as SCV_WORDs afterwards.
72 */
73%Start	WORD
74
75%%
76
77#.*$			;	/* comments */
78
79<INITIAL>validate	{ BEGIN WORD; return (SCC_VALIDATE); }
80<INITIAL>import		{ BEGIN WORD; return (SCC_IMPORT); }
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>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>addpropvalue	{ BEGIN WORD; return (SCC_ADDPROPVALUE); }
108<INITIAL>delpropvalue	{ BEGIN WORD; return (SCC_DELPROPVALUE); }
109<INITIAL>setenv		{ BEGIN WORD; return (SCC_SETENV); }
110<INITIAL>unsetenv	{ BEGIN WORD; return (SCC_UNSETENV); }
111
112<INITIAL>listsnap	{ BEGIN WORD; return (SCC_LISTSNAP); }
113<INITIAL>selectsnap	{ BEGIN WORD; return (SCC_SELECTSNAP); }
114<INITIAL>revert		{ BEGIN WORD; return (SCC_REVERT); }
115
116[^ \t\n">=()]+		{
117				if ((yylval.str = strdup(yytext)) == NULL) {
118					yyerror(gettext("Out of memory"));
119					exit(UU_EXIT_FATAL);
120				}
121
122				return SCV_WORD;
123			}
124
125\"([^"\\]|\\.)*\"	{
126				/*
127				 * double-quoted strings start at a
128				 * double-quote, include characters other than
129				 * double-quote and backslash, and
130				 * backslashed-characters, and end with a
131				 * double-quote.
132				 */
133
134				char *str, *cp;
135				int shift;
136
137				if ((str = strdup(yytext)) == NULL) {
138					yyerror(gettext("Out of memory"));
139					exit(UU_EXIT_FATAL);
140				}
141
142				/* Strip out the backslashes. */
143				for (cp = str, shift = 0; *cp != '\0'; ++cp) {
144					if (*cp == '\\') {
145						++cp;
146
147						/*
148						 * This can't be null because
149						 * the string always ends with
150						 * a double-quote.
151						 */
152
153						++shift;
154						*(cp - shift) = *cp;
155					} else if (shift != 0)
156						*(cp - shift) = *cp;
157				}
158
159				/* Nullify everything after trailing quote */
160				*(cp - shift) = '\0';
161
162				yylval.str = str;
163				return SCV_STRING;
164			}
165
166\n			{
167				est->sc_cmd_lineno++;
168				BEGIN INITIAL;
169				return (SCS_NEWLINE);
170			}
171
172[ \t]+			;
173
174">"			{ return SCS_REDIRECT; }
175"="			{ return SCS_EQUALS; }
176"("			{ ++parens; return SCS_LPAREN; }
177")"			{ --parens; return SCS_RPAREN; }
178
179.			{
180				uu_die(gettext("unrecognized character %s\n"),
181				    yytext);
182			}
183
184%%
185
186int
187yyerror(const char *s)
188{
189	return (0);
190}
191
192static int
193input(void)
194{
195	static int saw_eof = 0;
196
197	int c = engine_cmd_getc(est);
198
199	/*
200	 * To ensure input is terminated, slip in a newline on EOF.
201	 */
202	if (c == EOF) {
203		if (saw_eof)
204			return (0);
205
206		saw_eof = 1;
207		return ('\n');
208	} else
209		saw_eof = 0;
210
211	if (c == '\n')
212		yylineno++;
213
214	return (c);
215}
216
217static void
218unput(int c)
219{
220	if (c == '\n')
221		yylineno--;
222
223	(void) engine_cmd_ungetc(est, c == 0 ? EOF : c);
224}
225
226static void
227output(int c)
228{
229	char ch = c;
230	engine_cmd_nputs(est, &ch, sizeof (ch));
231}
232