xref: /illumos-gate/usr/src/cmd/pools/poolcfg/poolcfg.l (revision 2a8bcb4e)
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
67c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
77c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
87c478bd9Sstevel@tonic-gate  * with the License.
97c478bd9Sstevel@tonic-gate  *
107c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
117c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
127c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
137c478bd9Sstevel@tonic-gate  * and limitations under the License.
147c478bd9Sstevel@tonic-gate  *
157c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
167c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
177c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
187c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
197c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
207c478bd9Sstevel@tonic-gate  *
217c478bd9Sstevel@tonic-gate  * CDDL HEADER END
227c478bd9Sstevel@tonic-gate  *
237c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma       error_messages(off, E_STATEMENT_NOT_REACHED)
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * poolcfg.l
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * Overview
337c478bd9Sstevel@tonic-gate  * poolcfg.l implements a lexer for the poolcfg(1) utility.The lexer uses
347c478bd9Sstevel@tonic-gate  * the token definitions generated by YACC in the file poolcfg_Grammar.h.
357c478bd9Sstevel@tonic-gate  * To make token recognition simpler, the lexer uses a separate state for
367c478bd9Sstevel@tonic-gate  * each of the different data types supported by poolcfg(1).
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  * States
397c478bd9Sstevel@tonic-gate  * Lex provides the ability to minimise conflict between qualifying regexps
407c478bd9Sstevel@tonic-gate  * by providing states. A regexp that is qualified by a state will only be
417c478bd9Sstevel@tonic-gate  * used when the state is entered (using the BEGIN <state> command). (The
427c478bd9Sstevel@tonic-gate  * exception to this rule, is that rules defined in the default state are
437c478bd9Sstevel@tonic-gate  * available in all states.)
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  * poolcfg.l makes use of type states, one for each of the poolcfg(1)
467c478bd9Sstevel@tonic-gate  * supported data types:
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * ISTATE => int64_t
497c478bd9Sstevel@tonic-gate  * USTATE => uint64_t
507c478bd9Sstevel@tonic-gate  * BSTATE => uchar_t
517c478bd9Sstevel@tonic-gate  * SSTATE => const char *
527c478bd9Sstevel@tonic-gate  * DSTATE => double
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  * and a further state, CPUSTATE, to indicate the difference between matching
557c478bd9Sstevel@tonic-gate  * a valid "name" (i.e. id) for a cpu and a valid name for other components of
567c478bd9Sstevel@tonic-gate  * libpool.
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  * When a token indicating a variable declaration is matched, the corresponding
597c478bd9Sstevel@tonic-gate  * state is saved in the state variable. Once the assignment ('=') token is
607c478bd9Sstevel@tonic-gate  * matched, the stored state is entered and the additional state specific
617c478bd9Sstevel@tonic-gate  * matching regular expressions become available. Once a state specific
627c478bd9Sstevel@tonic-gate  * token is matched, the default state is restored.
63*2a8bcb4eSToomas Soome  *
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate #include <stdlib.h>
667c478bd9Sstevel@tonic-gate #include <sys/types.h>
677c478bd9Sstevel@tonic-gate #include <assert.h>
687c478bd9Sstevel@tonic-gate #include <string.h>
697c478bd9Sstevel@tonic-gate #include <errno.h>
707c478bd9Sstevel@tonic-gate #include <libintl.h>
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #include <pool.h>
737c478bd9Sstevel@tonic-gate #include "utils.h"
747c478bd9Sstevel@tonic-gate #include "poolcfg.h"
757c478bd9Sstevel@tonic-gate #include "poolcfg_grammar.h"
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static int lex_lineno = 1;		/* line-number for error reporting */
787c478bd9Sstevel@tonic-gate static int state = INITIAL;		/* state to be used */
797c478bd9Sstevel@tonic-gate extern void yyerror(char *s);
807c478bd9Sstevel@tonic-gate extern int dofile;			/* are we processing a file? */
817c478bd9Sstevel@tonic-gate %}
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate %s ISTATE
847c478bd9Sstevel@tonic-gate %s USTATE
857c478bd9Sstevel@tonic-gate %s BSTATE
867c478bd9Sstevel@tonic-gate %s SSTATE
877c478bd9Sstevel@tonic-gate %s DSTATE
887c478bd9Sstevel@tonic-gate %s CPUSTATE
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate %%
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate \n			lex_lineno++;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate [ \t]+			;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #.*			;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate info			{ return PCC_INFO; }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate create			{ return PCC_CREATE; }
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate destroy			{ return PCC_DESTROY; }
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate modify			{ return PCC_MODIFY; }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate associate		{ return PCC_ASSOC; }
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate transfer		{
1097c478bd9Sstevel@tonic-gate 				BEGIN USTATE;
1107c478bd9Sstevel@tonic-gate 				return PCC_TRANSFER;
1117c478bd9Sstevel@tonic-gate 			}
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate discover		{ return PCC_DISC; }
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate rename			{ return PCC_RENAME; }
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate to			{ return PCK_TO; }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate from			{ return PCK_FROM; }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate int			{
1227c478bd9Sstevel@tonic-gate 				state=ISTATE;
1237c478bd9Sstevel@tonic-gate 				return PCT_INT;
1247c478bd9Sstevel@tonic-gate 			}
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate uint			{
1277c478bd9Sstevel@tonic-gate 				state=USTATE;
1287c478bd9Sstevel@tonic-gate 				return PCT_UINT;
1297c478bd9Sstevel@tonic-gate 			}
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate boolean			{
1327c478bd9Sstevel@tonic-gate 				state=BSTATE;
1337c478bd9Sstevel@tonic-gate 				return PCT_BOOLEAN;
1347c478bd9Sstevel@tonic-gate 			}
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate string			{
1377c478bd9Sstevel@tonic-gate 				state=SSTATE;
1387c478bd9Sstevel@tonic-gate 				return PCT_STRING;
1397c478bd9Sstevel@tonic-gate 			}
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate float			{
1427c478bd9Sstevel@tonic-gate 				state=DSTATE;
1437c478bd9Sstevel@tonic-gate 				return PCT_FLOAT;
1447c478bd9Sstevel@tonic-gate 			}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate cpu			{
1477c478bd9Sstevel@tonic-gate 				BEGIN CPUSTATE;
1487c478bd9Sstevel@tonic-gate 				return PCE_CPU;
1497c478bd9Sstevel@tonic-gate 			}
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate pset			{ return PCE_PSET; }
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate pool			{ return PCE_POOL; }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate system			{ return PCE_SYSTEM; }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate \(			{ return PCK_OPENLST; }
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate \)			{ return PCK_CLOSELST; }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate =			{
1627c478bd9Sstevel@tonic-gate    				 BEGIN state;
1637c478bd9Sstevel@tonic-gate 				 return PCK_ASSIGN;
1647c478bd9Sstevel@tonic-gate 			}
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate \;			{ return PCK_SEPLST; }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate ~			{ return PCK_UNDEF; }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate <ISTATE>-?[0-9]+	{
1717c478bd9Sstevel@tonic-gate 				yylval.ival = strtoll(yytext, NULL, 0);
1727c478bd9Sstevel@tonic-gate 				if (errno == EINVAL || errno == ERANGE) {
1737c478bd9Sstevel@tonic-gate 					yyerror(gettext("Invalid value"));
1747c478bd9Sstevel@tonic-gate 					exit(E_ERROR);
1757c478bd9Sstevel@tonic-gate 				}
1767c478bd9Sstevel@tonic-gate 				BEGIN INITIAL;
1777c478bd9Sstevel@tonic-gate 				return PCV_VAL_INT;
1787c478bd9Sstevel@tonic-gate 			}
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate <USTATE>[0-9]+		{
1817c478bd9Sstevel@tonic-gate 				yylval.uval = strtoull(yytext, NULL, 0);
1827c478bd9Sstevel@tonic-gate 				if (errno == EINVAL || errno == ERANGE) {
1837c478bd9Sstevel@tonic-gate 					yyerror(gettext("Invalid value"));
1847c478bd9Sstevel@tonic-gate 					exit(E_ERROR);
1857c478bd9Sstevel@tonic-gate 				}
1867c478bd9Sstevel@tonic-gate 				BEGIN INITIAL;
1877c478bd9Sstevel@tonic-gate 				return PCV_VAL_UINT;
1887c478bd9Sstevel@tonic-gate 			}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate <BSTATE>true|false	{
1927c478bd9Sstevel@tonic-gate 				if (strcmp(yytext, "true") == 0)
1937c478bd9Sstevel@tonic-gate 					yylval.bval = 1;
1947c478bd9Sstevel@tonic-gate 				else
1957c478bd9Sstevel@tonic-gate 					yylval.bval = 0;
1967c478bd9Sstevel@tonic-gate 				BEGIN INITIAL;
1977c478bd9Sstevel@tonic-gate 				return PCV_VAL_BOOLEAN;
1987c478bd9Sstevel@tonic-gate 			}
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate <SSTATE>\"[^\"\n]*[\"\n] {
2017c478bd9Sstevel@tonic-gate 				if((yylval.sval = strdup(yytext+1)) == NULL) {
2027c478bd9Sstevel@tonic-gate 					yyerror(gettext("Out of memory"));
2037c478bd9Sstevel@tonic-gate 					exit(E_ERROR);
2047c478bd9Sstevel@tonic-gate 				}
2057c478bd9Sstevel@tonic-gate 				if (yylval.sval[yyleng-2] =='"')
2067c478bd9Sstevel@tonic-gate 					yylval.sval[yyleng-2] = 0;
2077c478bd9Sstevel@tonic-gate 				BEGIN INITIAL;
2087c478bd9Sstevel@tonic-gate 				return PCV_VAL_STRING;
2097c478bd9Sstevel@tonic-gate 			}
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate <DSTATE>([0-9]+|([0-9]*\.[0-9]+)([eE][-+]?[0-9]+)?) {
2127c478bd9Sstevel@tonic-gate 				yylval.dval = strtod(yytext, (char **)NULL);
2137c478bd9Sstevel@tonic-gate 				if (errno == EINVAL || errno == ERANGE) {
2147c478bd9Sstevel@tonic-gate 					yyerror(gettext("Invalid value"));
2157c478bd9Sstevel@tonic-gate 					exit(E_ERROR);
2167c478bd9Sstevel@tonic-gate 				}
2177c478bd9Sstevel@tonic-gate 				BEGIN INITIAL;
2187c478bd9Sstevel@tonic-gate 				return PCV_VAL_FLOAT;
2197c478bd9Sstevel@tonic-gate 			}
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate [A-Za-z][A-Za-z0-9,._-]*	{
2227c478bd9Sstevel@tonic-gate 				if ((yylval.sval = strdup(yytext)) == NULL) {
2237c478bd9Sstevel@tonic-gate 					yyerror(gettext("Out of memory"));
2247c478bd9Sstevel@tonic-gate 					exit(E_ERROR);
2257c478bd9Sstevel@tonic-gate 				}
2267c478bd9Sstevel@tonic-gate  				return PCV_SYMBOL;
2277c478bd9Sstevel@tonic-gate 			}
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate <CPUSTATE>[0-9]+	{
2307c478bd9Sstevel@tonic-gate 				if ((yylval.sval = strdup(yytext)) == NULL) {
2317c478bd9Sstevel@tonic-gate 					yyerror(gettext("Out of memory"));
2327c478bd9Sstevel@tonic-gate 					exit(E_ERROR);
2337c478bd9Sstevel@tonic-gate 				}
2347c478bd9Sstevel@tonic-gate 				BEGIN INITIAL;
2357c478bd9Sstevel@tonic-gate  				return PCV_SYMBOL;
2367c478bd9Sstevel@tonic-gate 			}
2377c478bd9Sstevel@tonic-gate .			{
2387c478bd9Sstevel@tonic-gate 				yyerror(gettext("Illegal character"));
2397c478bd9Sstevel@tonic-gate 				exit(E_ERROR);
2407c478bd9Sstevel@tonic-gate 			}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate %%
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate void
2457c478bd9Sstevel@tonic-gate yyerror(char *s)
2467c478bd9Sstevel@tonic-gate {
2477c478bd9Sstevel@tonic-gate 	if (dofile == PO_TRUE) {
2487c478bd9Sstevel@tonic-gate 		if (yytext[0] == '\0') {
2497c478bd9Sstevel@tonic-gate 			(void) warn(gettext("line %d, %s, token expected\n"),
2507c478bd9Sstevel@tonic-gate 			    lex_lineno, s);
2517c478bd9Sstevel@tonic-gate 			return;
2527c478bd9Sstevel@tonic-gate 		}
2537c478bd9Sstevel@tonic-gate 		(void) warn(gettext("line %d, %s at '%s'\n"), lex_lineno, s,
2547c478bd9Sstevel@tonic-gate 		    yytext);
2557c478bd9Sstevel@tonic-gate 	} else {
2567c478bd9Sstevel@tonic-gate 		if (yytext[0] == '\0') {
2577c478bd9Sstevel@tonic-gate 			(void) warn(gettext("%s, token expected\n"), s);
2587c478bd9Sstevel@tonic-gate 			return;
2597c478bd9Sstevel@tonic-gate 		}
2607c478bd9Sstevel@tonic-gate 		(void) warn(gettext("%s at '%s'\n"), s, yytext);
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
264