xref: /illumos-gate/usr/src/cmd/svc/svccfg/svccfg.l (revision 1f6eb0216cb17ca5fdff9563329f1dda47c8b801)
17c478bd9Sstevel@tonic-gate %{
27c478bd9Sstevel@tonic-gate /*
37c478bd9Sstevel@tonic-gate  * CDDL HEADER START
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
63eae19d9Swesolows  * Common Development and Distribution License (the "License").
73eae19d9Swesolows  * You may not use this file except in compliance with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
213eae19d9Swesolows  */
223eae19d9Swesolows 
233eae19d9Swesolows /*
24347a77f2Samaguire  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #pragma error_messages(off, E_BLOCK_DECL_UNUSED)
307c478bd9Sstevel@tonic-gate #pragma error_messages(off, E_EQUALITY_NOT_ASSIGNMENT)
317c478bd9Sstevel@tonic-gate #pragma error_messages(off, E_FUNC_RET_MAYBE_IGNORED2)
327c478bd9Sstevel@tonic-gate #pragma error_messages(off, E_STMT_NOT_REACHED)
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <libintl.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include "svccfg.h"
387c478bd9Sstevel@tonic-gate #include "svccfg_grammar.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * We need to undefine lex's input, unput, and output macros so that references
427c478bd9Sstevel@tonic-gate  * to these call the functions we provide at the end of this source file,
437c478bd9Sstevel@tonic-gate  * instead of the default versions based on libc's stdio.
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate #ifdef input
467c478bd9Sstevel@tonic-gate #undef input
477c478bd9Sstevel@tonic-gate #endif
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #ifdef unput
507c478bd9Sstevel@tonic-gate #undef unput
517c478bd9Sstevel@tonic-gate #endif
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #ifdef output
547c478bd9Sstevel@tonic-gate #undef output
557c478bd9Sstevel@tonic-gate #endif
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static int input(void);
587c478bd9Sstevel@tonic-gate static void unput(int);
597c478bd9Sstevel@tonic-gate static void output(int);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate int parens = 0;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate extern int yyerror(const char *);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate %}
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * Since command tokens are only valid at the beginning of the command (or
697c478bd9Sstevel@tonic-gate  * after help), we'll only return them in the INITIAL state, and report them
707c478bd9Sstevel@tonic-gate  * as SCV_WORDs afterwards.
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate %Start	WORD
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate %%
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #.*$			;	/* comments */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate <INITIAL>validate	{ BEGIN WORD; return (SCC_VALIDATE); }
797c478bd9Sstevel@tonic-gate <INITIAL>import		{ BEGIN WORD; return (SCC_IMPORT); }
807c478bd9Sstevel@tonic-gate <INITIAL>export		{ BEGIN WORD; return (SCC_EXPORT); }
817c478bd9Sstevel@tonic-gate <INITIAL>archive	{ BEGIN WORD; return (SCC_ARCHIVE); }
823eae19d9Swesolows <INITIAL>restore	{ BEGIN WORD; return (SCC_RESTORE); }
837c478bd9Sstevel@tonic-gate <INITIAL>apply		{ BEGIN WORD; return (SCC_APPLY); }
847c478bd9Sstevel@tonic-gate <INITIAL>extract	{ BEGIN WORD; return (SCC_EXTRACT); }
857c478bd9Sstevel@tonic-gate <INITIAL>repository	{ BEGIN WORD; return (SCC_REPOSITORY); }
867c478bd9Sstevel@tonic-gate <INITIAL>inventory	{ BEGIN WORD; return (SCC_INVENTORY); }
877c478bd9Sstevel@tonic-gate <INITIAL>set		{ BEGIN WORD; return (SCC_SET); }
887c478bd9Sstevel@tonic-gate <INITIAL>end		{ BEGIN WORD; return (SCC_END); }
897c478bd9Sstevel@tonic-gate <INITIAL>exit		{ BEGIN WORD; return (SCC_END); }
907c478bd9Sstevel@tonic-gate <INITIAL>quit		{ BEGIN WORD; return (SCC_END); }
917c478bd9Sstevel@tonic-gate <INITIAL>help		{ return (SCC_HELP); }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate <INITIAL>list		{ BEGIN WORD; return (SCC_LIST); }
947c478bd9Sstevel@tonic-gate <INITIAL>add		{ BEGIN WORD; return (SCC_ADD); }
957c478bd9Sstevel@tonic-gate <INITIAL>delete		{ BEGIN WORD; return (SCC_DELETE); }
967c478bd9Sstevel@tonic-gate <INITIAL>select		{ BEGIN WORD; return (SCC_SELECT); }
977c478bd9Sstevel@tonic-gate <INITIAL>unselect	{ BEGIN WORD; return (SCC_UNSELECT); }
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate <INITIAL>listpg		{ BEGIN WORD; return (SCC_LISTPG); }
1007c478bd9Sstevel@tonic-gate <INITIAL>addpg		{ BEGIN WORD; return (SCC_ADDPG); }
1017c478bd9Sstevel@tonic-gate <INITIAL>delpg		{ BEGIN WORD; return (SCC_DELPG); }
10270cbfe41SPhilippe Jung <INITIAL>delhash	{ BEGIN WORD; return (SCC_DELHASH); }
1037c478bd9Sstevel@tonic-gate <INITIAL>listprop	{ BEGIN WORD; return (SCC_LISTPROP); }
1047c478bd9Sstevel@tonic-gate <INITIAL>setprop	{ BEGIN WORD; return (SCC_SETPROP); }
1057c478bd9Sstevel@tonic-gate <INITIAL>delprop	{ BEGIN WORD; return (SCC_DELPROP); }
1067c478bd9Sstevel@tonic-gate <INITIAL>editprop	{ BEGIN WORD; return (SCC_EDITPROP); }
107*1f6eb021SLiane Praza <INITIAL>describe	{ BEGIN WORD; return (SCC_DESCRIBE); }
1087c478bd9Sstevel@tonic-gate <INITIAL>addpropvalue	{ BEGIN WORD; return (SCC_ADDPROPVALUE); }
1097c478bd9Sstevel@tonic-gate <INITIAL>delpropvalue	{ BEGIN WORD; return (SCC_DELPROPVALUE); }
1107c478bd9Sstevel@tonic-gate <INITIAL>setenv		{ BEGIN WORD; return (SCC_SETENV); }
1117c478bd9Sstevel@tonic-gate <INITIAL>unsetenv	{ BEGIN WORD; return (SCC_UNSETENV); }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate <INITIAL>listsnap	{ BEGIN WORD; return (SCC_LISTSNAP); }
1147c478bd9Sstevel@tonic-gate <INITIAL>selectsnap	{ BEGIN WORD; return (SCC_SELECTSNAP); }
1157c478bd9Sstevel@tonic-gate <INITIAL>revert		{ BEGIN WORD; return (SCC_REVERT); }
116347a77f2Samaguire <INITIAL>refresh	{ BEGIN WORD; return (SCC_REFRESH); }
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate [^ \t\n">=()]+		{
1197c478bd9Sstevel@tonic-gate 				if ((yylval.str = strdup(yytext)) == NULL) {
1207c478bd9Sstevel@tonic-gate 					yyerror(gettext("Out of memory"));
1217c478bd9Sstevel@tonic-gate 					exit(UU_EXIT_FATAL);
1227c478bd9Sstevel@tonic-gate 				}
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 				return SCV_WORD;
1257c478bd9Sstevel@tonic-gate 			}
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate \"([^"\\]|\\.)*\"	{
1287c478bd9Sstevel@tonic-gate 				/*
1297c478bd9Sstevel@tonic-gate 				 * double-quoted strings start at a
1307c478bd9Sstevel@tonic-gate 				 * double-quote, include characters other than
1317c478bd9Sstevel@tonic-gate 				 * double-quote and backslash, and
1327c478bd9Sstevel@tonic-gate 				 * backslashed-characters, and end with a
1337c478bd9Sstevel@tonic-gate 				 * double-quote.
1347c478bd9Sstevel@tonic-gate 				 */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 				char *str, *cp;
1377c478bd9Sstevel@tonic-gate 				int shift;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 				if ((str = strdup(yytext)) == NULL) {
1407c478bd9Sstevel@tonic-gate 					yyerror(gettext("Out of memory"));
1417c478bd9Sstevel@tonic-gate 					exit(UU_EXIT_FATAL);
1427c478bd9Sstevel@tonic-gate 				}
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 				/* Strip out the backslashes. */
1457c478bd9Sstevel@tonic-gate 				for (cp = str, shift = 0; *cp != '\0'; ++cp) {
1467c478bd9Sstevel@tonic-gate 					if (*cp == '\\') {
1477c478bd9Sstevel@tonic-gate 						++cp;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 						/*
1507c478bd9Sstevel@tonic-gate 						 * This can't be null because
1517c478bd9Sstevel@tonic-gate 						 * the string always ends with
1527c478bd9Sstevel@tonic-gate 						 * a double-quote.
1537c478bd9Sstevel@tonic-gate 						 */
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 						++shift;
1567c478bd9Sstevel@tonic-gate 						*(cp - shift) = *cp;
1577c478bd9Sstevel@tonic-gate 					} else if (shift != 0)
1587c478bd9Sstevel@tonic-gate 						*(cp - shift) = *cp;
1597c478bd9Sstevel@tonic-gate 				}
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 				/* Nullify everything after trailing quote */
1627c478bd9Sstevel@tonic-gate 				*(cp - shift) = '\0';
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 				yylval.str = str;
1657c478bd9Sstevel@tonic-gate 				return SCV_STRING;
1667c478bd9Sstevel@tonic-gate 			}
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate \n			{
1697c478bd9Sstevel@tonic-gate 				est->sc_cmd_lineno++;
1707c478bd9Sstevel@tonic-gate 				BEGIN INITIAL;
1717c478bd9Sstevel@tonic-gate 				return (SCS_NEWLINE);
1727c478bd9Sstevel@tonic-gate 			}
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate [ \t]+			;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate ">"			{ return SCS_REDIRECT; }
1777c478bd9Sstevel@tonic-gate "="			{ return SCS_EQUALS; }
1787c478bd9Sstevel@tonic-gate "("			{ ++parens; return SCS_LPAREN; }
1797c478bd9Sstevel@tonic-gate ")"			{ --parens; return SCS_RPAREN; }
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate .			{
1827c478bd9Sstevel@tonic-gate 				uu_die(gettext("unrecognized character %s\n"),
1837c478bd9Sstevel@tonic-gate 				    yytext);
1847c478bd9Sstevel@tonic-gate 			}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate %%
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate int
1897c478bd9Sstevel@tonic-gate yyerror(const char *s)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate 	return (0);
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate static int
1957c478bd9Sstevel@tonic-gate input(void)
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate 	static int saw_eof = 0;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	int c = engine_cmd_getc(est);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	/*
2027c478bd9Sstevel@tonic-gate 	 * To ensure input is terminated, slip in a newline on EOF.
2037c478bd9Sstevel@tonic-gate 	 */
2047c478bd9Sstevel@tonic-gate 	if (c == EOF) {
2057c478bd9Sstevel@tonic-gate 		if (saw_eof)
2067c478bd9Sstevel@tonic-gate 			return (0);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 		saw_eof = 1;
2097c478bd9Sstevel@tonic-gate 		return ('\n');
2107c478bd9Sstevel@tonic-gate 	} else
2117c478bd9Sstevel@tonic-gate 		saw_eof = 0;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	if (c == '\n')
2147c478bd9Sstevel@tonic-gate 		yylineno++;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	return (c);
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate static void
2207c478bd9Sstevel@tonic-gate unput(int c)
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate 	if (c == '\n')
2237c478bd9Sstevel@tonic-gate 		yylineno--;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	(void) engine_cmd_ungetc(est, c == 0 ? EOF : c);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate static void
2297c478bd9Sstevel@tonic-gate output(int c)
2307c478bd9Sstevel@tonic-gate {
2317c478bd9Sstevel@tonic-gate 	char ch = c;
2327c478bd9Sstevel@tonic-gate 	engine_cmd_nputs(est, &ch, sizeof (ch));
2337c478bd9Sstevel@tonic-gate }
234