xref: /illumos-gate/usr/src/cmd/mdb/common/mdb/mdb_lex.l (revision f32691c9)
17c478bd9Sstevel@tonic-gate %pointer	/* Make yytext a pointer, not an array */
27c478bd9Sstevel@tonic-gate 
37c478bd9Sstevel@tonic-gate %{
47c478bd9Sstevel@tonic-gate /*
57c478bd9Sstevel@tonic-gate  * CDDL HEADER START
67c478bd9Sstevel@tonic-gate  *
77c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
8d67944fbSScott Rotondo  * Common Development and Distribution License (the "License").
9d67944fbSScott Rotondo  * You may not use this file except in compliance with the License.
107c478bd9Sstevel@tonic-gate  *
117c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
127c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
137c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
147c478bd9Sstevel@tonic-gate  * and limitations under the License.
157c478bd9Sstevel@tonic-gate  *
167c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
177c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
187c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
197c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
207c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
217c478bd9Sstevel@tonic-gate  *
227c478bd9Sstevel@tonic-gate  * CDDL HEADER END
237c478bd9Sstevel@tonic-gate  *
24d67944fbSScott Rotondo  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
28a11593b6SAlexander Eremin /*
29a11593b6SAlexander Eremin  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
303d580edaSSerapheim Dimitropoulos  * Copyright (c) 2017 by Delphix. All rights reserved.
317a2abfc9SRobert Mustacchi  * Copyright 2019, Joyent, Inc.
32*f32691c9SRobert Mustacchi  * Copyright 2021 Oxide Computer Company
33a11593b6SAlexander Eremin  */
34a11593b6SAlexander Eremin 
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <strings.h>
397c478bd9Sstevel@tonic-gate #include <stdarg.h>
407c478bd9Sstevel@tonic-gate #include <stdlib.h>
417c478bd9Sstevel@tonic-gate #include <stdio.h>
427c478bd9Sstevel@tonic-gate #include <errno.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <mdb/mdb_types.h>
457c478bd9Sstevel@tonic-gate #include <mdb/mdb_debug.h>
467c478bd9Sstevel@tonic-gate #include <mdb/mdb_nv.h>
477c478bd9Sstevel@tonic-gate #include <mdb/mdb_lex.h>
487c478bd9Sstevel@tonic-gate #include <mdb/mdb_frame.h>
497c478bd9Sstevel@tonic-gate #include <mdb/mdb_string.h>
507c478bd9Sstevel@tonic-gate #include <mdb/mdb_stdlib.h>
517c478bd9Sstevel@tonic-gate #include <mdb/mdb_err.h>
527c478bd9Sstevel@tonic-gate #include <mdb/mdb.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include "mdb_grammar.h"
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * lex hardcodes yyin and yyout to stdin and stdout, respectively, before we get
587c478bd9Sstevel@tonic-gate  * control.  We've redirected printf and fprintf (see mdb_lex.h) to yyprintf and
597c478bd9Sstevel@tonic-gate  * yyfprintf, which ignore the FILE * provided by yyout.  __iob-based stdin and
607c478bd9Sstevel@tonic-gate  * stdout are useless in kmdb, since we don't have stdio.  We define __iob here
617c478bd9Sstevel@tonic-gate  * to shut the linker up.
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate #ifdef _KMDB
64d67944fbSScott Rotondo FILE __iob[_NFILE];
657c478bd9Sstevel@tonic-gate #endif
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * We need to undefine lex's input, unput, and output macros so that references
697c478bd9Sstevel@tonic-gate  * to these call the functions we provide at the end of this source file,
707c478bd9Sstevel@tonic-gate  * instead of the default versions based on libc's stdio.
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate #ifdef input
737c478bd9Sstevel@tonic-gate #undef input
747c478bd9Sstevel@tonic-gate #endif
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #ifdef unput
777c478bd9Sstevel@tonic-gate #undef unput
787c478bd9Sstevel@tonic-gate #endif
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #ifdef output
817c478bd9Sstevel@tonic-gate #undef output
827c478bd9Sstevel@tonic-gate #endif
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static int input(void);
857c478bd9Sstevel@tonic-gate static void unput(int);
867c478bd9Sstevel@tonic-gate static void output(int);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate static void string_unquote(char *);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate extern int yydebug;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate  * This will prevent lex from trying to malloc() and resize our yytext variable,
947c478bd9Sstevel@tonic-gate  * instead it will just print out an error message and exit(), which seems
957c478bd9Sstevel@tonic-gate  * a lesser of two evils.
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate #define YYISARRAY
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate %}
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate %o	9000
1027c478bd9Sstevel@tonic-gate %a	5000
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate %s	S_SHELLCMD
1057c478bd9Sstevel@tonic-gate %s	S_INITIAL
1067c478bd9Sstevel@tonic-gate %s	S_FMTLIST
1077c478bd9Sstevel@tonic-gate %s	S_ARGLIST
1087c478bd9Sstevel@tonic-gate %s	S_EXPR
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate RGX_CMD_CHAR	[?%@A-Z\^_`a-z]
1117c478bd9Sstevel@tonic-gate RGX_SYMBOL	[a-zA-Z_.][0-9a-zA-Z_.`]*
1127c478bd9Sstevel@tonic-gate RGX_SIMPLE_CHAR	[^ \t\n;!|"'\$]
1137c478bd9Sstevel@tonic-gate RGX_CHR_SEQ	([^'\n]|\\[^'\n]|\\')*
1147c478bd9Sstevel@tonic-gate RGX_STR_SEQ	([^"\\\n]|\\[^"\n]|\\\")*
1157c478bd9Sstevel@tonic-gate RGX_COMMENT	"//".*\n
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate %%
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate <S_INITIAL>{RGX_COMMENT}	|
1207c478bd9Sstevel@tonic-gate <S_FMTLIST>{RGX_COMMENT}	|
1217c478bd9Sstevel@tonic-gate <S_ARGLIST>{RGX_COMMENT}	{
1227c478bd9Sstevel@tonic-gate 		/*
1237c478bd9Sstevel@tonic-gate 		 * Comments are legal in these three states -- if we see one
1247c478bd9Sstevel@tonic-gate 		 * eat the line and return the newline character.
1257c478bd9Sstevel@tonic-gate 		 */
126*f32691c9SRobert Mustacchi 		BEGIN(S_INITIAL);
1277c478bd9Sstevel@tonic-gate 		return ('\n');
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate <S_INITIAL>"=="	|
1317c478bd9Sstevel@tonic-gate <S_EXPR>"=="	return (MDB_TOK_EQUAL); /* Equality operator */
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate <S_INITIAL>"!="	|
1347c478bd9Sstevel@tonic-gate <S_EXPR>"!="	return (MDB_TOK_NOTEQUAL); /* Inequality operator */
1357c478bd9Sstevel@tonic-gate 
1367a2abfc9SRobert Mustacchi <S_INITIAL>"%%"	|
1377a2abfc9SRobert Mustacchi <S_EXPR>"%%"	return (MDB_TOK_MODULUS); /* Modulus operator */
1387a2abfc9SRobert Mustacchi 
1397c478bd9Sstevel@tonic-gate <S_INITIAL>"!"	|
1407c478bd9Sstevel@tonic-gate <S_FMTLIST>"!"	|
1417c478bd9Sstevel@tonic-gate <S_ARGLIST>"!"	{
1427c478bd9Sstevel@tonic-gate 		/*
1437c478bd9Sstevel@tonic-gate 		 * Shell escapes are legal in all of these states -- switch to
1447c478bd9Sstevel@tonic-gate 		 * the shell command state and return the ! character.
1457c478bd9Sstevel@tonic-gate 		 */
1467c478bd9Sstevel@tonic-gate 		BEGIN(S_SHELLCMD);
1477c478bd9Sstevel@tonic-gate 		return (yytext[0]);
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate <S_FMTLIST>"|"	|
1517c478bd9Sstevel@tonic-gate <S_ARGLIST>"|"	{
1527c478bd9Sstevel@tonic-gate 		/*
1537c478bd9Sstevel@tonic-gate 		 * Pipelines can appear in any of these states -- switch to
1547c478bd9Sstevel@tonic-gate 		 * the initial state and return the | character.
1557c478bd9Sstevel@tonic-gate 		 */
1567c478bd9Sstevel@tonic-gate 		BEGIN(S_INITIAL);
1577c478bd9Sstevel@tonic-gate 		return (yytext[0]);
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate <S_SHELLCMD>[^;\n]+	{
1617c478bd9Sstevel@tonic-gate 		/*
1627c478bd9Sstevel@tonic-gate 		 * Once in the shell-command state, we return all remaining
1637c478bd9Sstevel@tonic-gate 		 * characters up to a newline or ';' delimiter as a single
1647c478bd9Sstevel@tonic-gate 		 * string which will be passed to $SHELL -c.
1657c478bd9Sstevel@tonic-gate 		 */
1667c478bd9Sstevel@tonic-gate 		yylval.l_string = strdup(yytext);
1677c478bd9Sstevel@tonic-gate 		BEGIN(S_INITIAL);
1687c478bd9Sstevel@tonic-gate 		return (MDB_TOK_STRING);
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate <S_INITIAL>"::"{RGX_SYMBOL}	{
1727c478bd9Sstevel@tonic-gate 		/*
1737c478bd9Sstevel@tonic-gate 		 * Verb ::command-name -- lookup the correspond dcmd and
1747c478bd9Sstevel@tonic-gate 		 * switch to the argument list state.
1757c478bd9Sstevel@tonic-gate 		 */
1767c478bd9Sstevel@tonic-gate 		if ((yylval.l_dcmd = mdb_dcmd_lookup(yytext + 2)) == NULL)
1777c478bd9Sstevel@tonic-gate 			yyperror("invalid command '%s'", yytext);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 		BEGIN(S_ARGLIST);
1807c478bd9Sstevel@tonic-gate 		return (MDB_TOK_DCMD);
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate <S_INITIAL>"$<<"|"$<"|"$>"	|
1847c478bd9Sstevel@tonic-gate <S_INITIAL>[\$:]{RGX_CMD_CHAR}	{
1857c478bd9Sstevel@tonic-gate 		/*
1867c478bd9Sstevel@tonic-gate 		 * Old-style :c or $c command -- lookup the corresponding dcmd
1877c478bd9Sstevel@tonic-gate 		 * and switch to the argument list state.
1887c478bd9Sstevel@tonic-gate 		 */
1897c478bd9Sstevel@tonic-gate 		if ((yylval.l_dcmd = mdb_dcmd_lookup(yytext)) == NULL)
1907c478bd9Sstevel@tonic-gate 			yyperror("invalid command '%s'", yytext);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 		BEGIN(S_ARGLIST);
1937c478bd9Sstevel@tonic-gate 		return (MDB_TOK_DCMD);
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate <S_INITIAL>">/"[a-zA-Z0-9]"/"	{
1977c478bd9Sstevel@tonic-gate 		/*
1987c478bd9Sstevel@tonic-gate 		 * Variable assignment with size cast -- append the cast letter
1997c478bd9Sstevel@tonic-gate 		 * to the argument list, and switch to the argument list state.
2007c478bd9Sstevel@tonic-gate 		 */
2017c478bd9Sstevel@tonic-gate 		mdb_arg_t arg;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 		arg.a_un.a_char = yytext[2];
2047c478bd9Sstevel@tonic-gate 		arg.a_type = MDB_TYPE_CHAR;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 		mdb_argvec_append(&mdb.m_frame->f_argvec, &arg);
2077c478bd9Sstevel@tonic-gate 		yylval.l_dcmd = mdb_dcmd_lookup(">");
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 		BEGIN(S_ARGLIST);
2107c478bd9Sstevel@tonic-gate 		return (MDB_TOK_DCMD);
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate <S_INITIAL>">"	{
2147c478bd9Sstevel@tonic-gate 		/*
2157c478bd9Sstevel@tonic-gate 		 * Variable assignment -- switch to the argument list state.
2167c478bd9Sstevel@tonic-gate 		 */
2177c478bd9Sstevel@tonic-gate 		yylval.l_dcmd = mdb_dcmd_lookup(yytext);
2187c478bd9Sstevel@tonic-gate 		BEGIN(S_ARGLIST);
2197c478bd9Sstevel@tonic-gate 		return (MDB_TOK_DCMD);
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 
2223d580edaSSerapheim Dimitropoulos <S_INITIAL>[/\\?][ \t]*[vwzWZlLM]	{
2237c478bd9Sstevel@tonic-gate 		/*
2247c478bd9Sstevel@tonic-gate 		 * Format verb followed by write or match signifier -- switch
2257c478bd9Sstevel@tonic-gate 		 * to the value list state and return the verb character.  We
2267c478bd9Sstevel@tonic-gate 		 * also append the actual format character to the arg list.
2277c478bd9Sstevel@tonic-gate 		 */
2287c478bd9Sstevel@tonic-gate 		mdb_arg_t arg;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 		arg.a_un.a_char = yytext[yyleng - 1];
2317c478bd9Sstevel@tonic-gate 		arg.a_type = MDB_TYPE_CHAR;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 		mdb_argvec_append(&mdb.m_frame->f_argvec, &arg);
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 		BEGIN(S_ARGLIST);
2367c478bd9Sstevel@tonic-gate 		return yytext[0];
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate <S_INITIAL>[/\\@?=]	{
2407c478bd9Sstevel@tonic-gate 		/*
2417c478bd9Sstevel@tonic-gate 		 * Format verb -- switch to the format list state and return
2427c478bd9Sstevel@tonic-gate 		 * the actual verb character verbatim.
2437c478bd9Sstevel@tonic-gate 		 */
2447c478bd9Sstevel@tonic-gate 		BEGIN(S_FMTLIST);
2457c478bd9Sstevel@tonic-gate 		return (yytext[0]);
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate <S_INITIAL>'{RGX_CHR_SEQ}$	|
2497c478bd9Sstevel@tonic-gate <S_EXPR>'{RGX_CHR_SEQ}$		yyerror("syntax error: ' unmatched");
2507c478bd9Sstevel@tonic-gate 
251*f32691c9SRobert Mustacchi <S_INITIAL>'{RGX_CHR_SEQ}'	|
2527c478bd9Sstevel@tonic-gate <S_EXPR>'{RGX_CHR_SEQ}'		{
2537c478bd9Sstevel@tonic-gate 		char *s, *p, *q;
2547c478bd9Sstevel@tonic-gate 		size_t nbytes;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 		/*
2577c478bd9Sstevel@tonic-gate 		 * If the character sequence is zero-length, return 0.
2587c478bd9Sstevel@tonic-gate 		 */
2597c478bd9Sstevel@tonic-gate 		if (yyleng == 2) {
2607c478bd9Sstevel@tonic-gate 			yylval.l_immediate = 0;
2617c478bd9Sstevel@tonic-gate 			return (MDB_TOK_IMMEDIATE);
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 		s = yytext + 1;			/* Skip past initial quote */
2657c478bd9Sstevel@tonic-gate 		yytext[yyleng - 1] = '\0';	/* Overwrite final quote */
2667c478bd9Sstevel@tonic-gate 		nbytes = stresc2chr(s);		/* Convert escapes */
2677c478bd9Sstevel@tonic-gate 		yylval.l_immediate = 0;		/* Initialize token value */
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 		if (nbytes > sizeof (uintmax_t)) {
2707c478bd9Sstevel@tonic-gate 			yyerror("character constant may not exceed %lu bytes\n",
2717c478bd9Sstevel@tonic-gate 			    (ulong_t)sizeof (uintmax_t));
2727c478bd9Sstevel@tonic-gate 		}
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
2757c478bd9Sstevel@tonic-gate 		p = ((char*)&yylval.l_immediate) + nbytes - 1;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 		for (q = s; nbytes != 0; nbytes--)
2787c478bd9Sstevel@tonic-gate 			*p-- = *q++;
2797c478bd9Sstevel@tonic-gate #else
2807c478bd9Sstevel@tonic-gate 		bcopy(s, ((char *)&yylval.l_immediate) +
2817c478bd9Sstevel@tonic-gate 		    sizeof (uintmax_t) - nbytes, nbytes);
2827c478bd9Sstevel@tonic-gate #endif
2837c478bd9Sstevel@tonic-gate 		return (MDB_TOK_IMMEDIATE);
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate \"{RGX_STR_SEQ}$	yyerror("syntax error: \" unmatched");
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate \"{RGX_STR_SEQ}\"	{
2897c478bd9Sstevel@tonic-gate 		/*
2907c478bd9Sstevel@tonic-gate 		 * Quoted string -- convert C escape sequences and return the
2917c478bd9Sstevel@tonic-gate 		 * string as a token.
2927c478bd9Sstevel@tonic-gate 		 */
2937c478bd9Sstevel@tonic-gate 		yylval.l_string = strndup(yytext + 1, yyleng - 2);
2947c478bd9Sstevel@tonic-gate 		(void) stresc2chr(yylval.l_string);
2957c478bd9Sstevel@tonic-gate 		return (MDB_TOK_STRING);
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate <S_ARGLIST>"$["	|
2997c478bd9Sstevel@tonic-gate <S_FMTLIST>"$["	{
3007c478bd9Sstevel@tonic-gate 		/*
3017c478bd9Sstevel@tonic-gate 		 * Start of expression -- begin expression state and save the
3027c478bd9Sstevel@tonic-gate 		 * current state so we can return at the end of the expression.
3037c478bd9Sstevel@tonic-gate 		 */
3047c478bd9Sstevel@tonic-gate 		mdb.m_frame->f_oldstate = YYSTATE;
3057c478bd9Sstevel@tonic-gate 		BEGIN(S_EXPR);
3067c478bd9Sstevel@tonic-gate 		return (MDB_TOK_LEXPR);
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate <S_ARGLIST>{RGX_SIMPLE_CHAR}*("'"{RGX_CHR_SEQ}"'"|\"{RGX_STR_SEQ}\"|{RGX_SIMPLE_CHAR}+)* {
3107c478bd9Sstevel@tonic-gate 		/*
3117c478bd9Sstevel@tonic-gate 		 * String token -- create a copy of the string and return it.
3127c478bd9Sstevel@tonic-gate 		 * We need to handle embedded single and double-quote pairs,
3137c478bd9Sstevel@tonic-gate 		 * which overcomplicates this slightly.
3147c478bd9Sstevel@tonic-gate 		 */
3157c478bd9Sstevel@tonic-gate 		yylval.l_string = strdup(yytext);
3167c478bd9Sstevel@tonic-gate 		string_unquote(yylval.l_string);
3177c478bd9Sstevel@tonic-gate 		return (MDB_TOK_STRING);
3187c478bd9Sstevel@tonic-gate 	}
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate <S_FMTLIST>[0-9]+	{
3217c478bd9Sstevel@tonic-gate 		/*
3227c478bd9Sstevel@tonic-gate 		 * Immediate value -- in the format list, all immediates
3237c478bd9Sstevel@tonic-gate 		 * are assumed to be in decimal.
3247c478bd9Sstevel@tonic-gate 		 */
3254585130bSYuri Pankov 		yylval.l_immediate = mdb_strtonum(yytext, 10);
3267c478bd9Sstevel@tonic-gate 		return (MDB_TOK_IMMEDIATE);
3277c478bd9Sstevel@tonic-gate 	}
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate <S_FMTLIST>{RGX_SIMPLE_CHAR}	{
3307c478bd9Sstevel@tonic-gate 		/*
3317c478bd9Sstevel@tonic-gate 		 * Non-meta character -- in the format list, we return each
3327c478bd9Sstevel@tonic-gate 		 * character as a separate token to be added as an argument.
3337c478bd9Sstevel@tonic-gate 		 */
3347c478bd9Sstevel@tonic-gate 		yylval.l_char = yytext[0];
3357c478bd9Sstevel@tonic-gate 		return (MDB_TOK_CHAR);
3367c478bd9Sstevel@tonic-gate 	}
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate <S_EXPR>";"|"!"|\n	{
3397c478bd9Sstevel@tonic-gate 		/*
3407c478bd9Sstevel@tonic-gate 		 * In the expression state only, we cannot see a command
3417c478bd9Sstevel@tonic-gate 		 * delimiter or shell escape before we end the expression.
3427c478bd9Sstevel@tonic-gate 		 */
3437c478bd9Sstevel@tonic-gate 		yyerror("syntax error: $[ unmatched");
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate <S_EXPR>"]"	{
3477c478bd9Sstevel@tonic-gate 		/*
3487c478bd9Sstevel@tonic-gate 		 * End of expression state.  Restore the state we were in
3497c478bd9Sstevel@tonic-gate 		 * before the "$[" which started this expression.
3507c478bd9Sstevel@tonic-gate 		 */
3517c478bd9Sstevel@tonic-gate 		BEGIN(mdb.m_frame->f_oldstate);
3527c478bd9Sstevel@tonic-gate 		return (MDB_TOK_REXPR);
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate <S_INITIAL>"<"{RGX_SYMBOL}	|
3567c478bd9Sstevel@tonic-gate <S_INITIAL>"<"[0-9]		|
3577c478bd9Sstevel@tonic-gate <S_EXPR>"<"{RGX_SYMBOL}		|
3587c478bd9Sstevel@tonic-gate <S_EXPR>"<"[0-9]	{
3597c478bd9Sstevel@tonic-gate 		/*
3607c478bd9Sstevel@tonic-gate 		 * Variable reference -- lookup the variable and return a
3617c478bd9Sstevel@tonic-gate 		 * pointer to it.  Referencing undefined variables is an error.
3627c478bd9Sstevel@tonic-gate 		 */
3637c478bd9Sstevel@tonic-gate 		yylval.l_var = mdb_nv_lookup(&mdb.m_nv, &yytext[1]);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 		if (yylval.l_var == NULL)
3667c478bd9Sstevel@tonic-gate 			yyerror("variable '%s' is not defined", &yytext[1]);
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 		return (MDB_TOK_VAR_REF);
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate <S_INITIAL>"<<"	|
3727c478bd9Sstevel@tonic-gate <S_EXPR>"<<"	return (MDB_TOK_LSHIFT); /* Logical shift left operator */
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate <S_INITIAL>">>"	|
3757c478bd9Sstevel@tonic-gate <S_EXPR>">>"	return (MDB_TOK_RSHIFT); /* Logical shift right operator */
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate <S_INITIAL>"*/"[a-zA-Z0-9]"/"	|
3787c478bd9Sstevel@tonic-gate <S_EXPR>"*/"[a-zA-Z0-9]"/"	{
3797c478bd9Sstevel@tonic-gate 		switch (yytext[2]) {
3807c478bd9Sstevel@tonic-gate 			case 'c': case '1':
3817c478bd9Sstevel@tonic-gate 				return (MDB_TOK_COR1_DEREF);
3827c478bd9Sstevel@tonic-gate 			case 's': case '2':
3837c478bd9Sstevel@tonic-gate 				return (MDB_TOK_COR2_DEREF);
3847c478bd9Sstevel@tonic-gate 			case 'i': case '4':
3857c478bd9Sstevel@tonic-gate #ifdef _ILP32
3867c478bd9Sstevel@tonic-gate 			case 'l':
3877c478bd9Sstevel@tonic-gate #endif
3887c478bd9Sstevel@tonic-gate 				return (MDB_TOK_COR4_DEREF);
3897c478bd9Sstevel@tonic-gate #ifdef _LP64
3907c478bd9Sstevel@tonic-gate 			case 'l':
3917c478bd9Sstevel@tonic-gate #endif
3927c478bd9Sstevel@tonic-gate 			case '8':
3937c478bd9Sstevel@tonic-gate 				return (MDB_TOK_COR8_DEREF);
3947c478bd9Sstevel@tonic-gate 		}
3957c478bd9Sstevel@tonic-gate 		yyerror("invalid cast -- %s\n", yytext);
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate <S_INITIAL>"%/"[a-zA-Z0-9]"/"	|
3997c478bd9Sstevel@tonic-gate <S_EXPR>"%/"[a-zA-Z0-9]"/"	{
4007c478bd9Sstevel@tonic-gate 		switch (yytext[2]) {
4017c478bd9Sstevel@tonic-gate 			case 'c': case '1':
4027c478bd9Sstevel@tonic-gate 				return (MDB_TOK_OBJ1_DEREF);
4037c478bd9Sstevel@tonic-gate 			case 's': case '2':
4047c478bd9Sstevel@tonic-gate 				return (MDB_TOK_OBJ2_DEREF);
4057c478bd9Sstevel@tonic-gate 			case 'i': case '4':
4067c478bd9Sstevel@tonic-gate #ifdef _ILP32
4077c478bd9Sstevel@tonic-gate 			case 'l':
4087c478bd9Sstevel@tonic-gate #endif
4097c478bd9Sstevel@tonic-gate 				return (MDB_TOK_OBJ4_DEREF);
4107c478bd9Sstevel@tonic-gate #ifdef _LP64
4117c478bd9Sstevel@tonic-gate 			case 'l':
4127c478bd9Sstevel@tonic-gate #endif
4137c478bd9Sstevel@tonic-gate 			case '8':
4147c478bd9Sstevel@tonic-gate 				return (MDB_TOK_OBJ8_DEREF);
4157c478bd9Sstevel@tonic-gate 		}
4167c478bd9Sstevel@tonic-gate 		yyerror("invalid cast -- %s\n", yytext);
4177c478bd9Sstevel@tonic-gate 	}
4187c478bd9Sstevel@tonic-gate 
419*f32691c9SRobert Mustacchi <S_INITIAL>0[iI][0-1][0-1_]*	|
420*f32691c9SRobert Mustacchi <S_EXPR>0[iI][0-1][0-1_]*	{
4217c478bd9Sstevel@tonic-gate 		/*
4227c478bd9Sstevel@tonic-gate 		 * Binary immediate value.
4237c478bd9Sstevel@tonic-gate 		 */
4244585130bSYuri Pankov 		yylval.l_immediate = mdb_strtonum(yytext + 2, 2);
4257c478bd9Sstevel@tonic-gate 		return (MDB_TOK_IMMEDIATE);
4267c478bd9Sstevel@tonic-gate 	}
4277c478bd9Sstevel@tonic-gate 
428*f32691c9SRobert Mustacchi <S_INITIAL>0[oO][0-7][0-7_]*	|
429*f32691c9SRobert Mustacchi <S_EXPR>0[oO][0-7][0-7_]*	{
4307c478bd9Sstevel@tonic-gate 		/*
4317c478bd9Sstevel@tonic-gate 		 * Octal immediate value.
4327c478bd9Sstevel@tonic-gate 		 */
4334585130bSYuri Pankov 		yylval.l_immediate = mdb_strtonum(yytext + 2, 8);
4347c478bd9Sstevel@tonic-gate 		return (MDB_TOK_IMMEDIATE);
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 
437*f32691c9SRobert Mustacchi <S_INITIAL>0[tT][0-9][0-9_]*"."[0-9][0-9_]*	|
438*f32691c9SRobert Mustacchi <S_EXPR>0[tT][0-9][0-9_]*"."[0-9][0-9_]*	{
4397c478bd9Sstevel@tonic-gate #ifdef _KMDB
4407c478bd9Sstevel@tonic-gate 		yyerror("floating point not supported\n");
4417c478bd9Sstevel@tonic-gate #else
4427c478bd9Sstevel@tonic-gate 		/*
4437c478bd9Sstevel@tonic-gate 		 * Decimal floating point value.
4447c478bd9Sstevel@tonic-gate 		 */
4457c478bd9Sstevel@tonic-gate 		char *p, c;
4467c478bd9Sstevel@tonic-gate 		double d;
4477c478bd9Sstevel@tonic-gate 		int i;
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 		if ((p = strsplit(yytext, '.')) == NULL)
4507c478bd9Sstevel@tonic-gate 			yyerror("internal scanning error -- expected '.'\n");
4517c478bd9Sstevel@tonic-gate 
4524585130bSYuri Pankov 		d = (double)mdb_strtonum(yytext + 2, 10);
4537c478bd9Sstevel@tonic-gate 
454*f32691c9SRobert Mustacchi 		i = 0;
455*f32691c9SRobert Mustacchi 		while (*p != '\0') {
456*f32691c9SRobert Mustacchi 			c = *p++;
457*f32691c9SRobert Mustacchi 			if (c == '_')
458*f32691c9SRobert Mustacchi 				continue;
4597c478bd9Sstevel@tonic-gate 			d = d * 10 + c - '0';
460*f32691c9SRobert Mustacchi 			i++;
461*f32691c9SRobert Mustacchi 		}
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 		while (i-- != 0)
4647c478bd9Sstevel@tonic-gate 			d /= 10;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate                 yylval.l_immediate = *((uintmax_t *)&d);
4677c478bd9Sstevel@tonic-gate 		return (MDB_TOK_IMMEDIATE);
4687c478bd9Sstevel@tonic-gate #endif
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate 
471*f32691c9SRobert Mustacchi <S_INITIAL>0[tT][0-9][0-9_]*	|
472*f32691c9SRobert Mustacchi <S_EXPR>0[tT][0-9][0-9_]*	{
4737c478bd9Sstevel@tonic-gate 		/*
4747c478bd9Sstevel@tonic-gate 		 * Decimal immediate value.
4757c478bd9Sstevel@tonic-gate 		 */
4764585130bSYuri Pankov 		yylval.l_immediate = mdb_strtonum(yytext + 2, 10);
4777c478bd9Sstevel@tonic-gate 		return (MDB_TOK_IMMEDIATE);
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 
480*f32691c9SRobert Mustacchi <S_INITIAL>0[xX][0-9a-fA-F][0-9a-fA-F_]*	|
481*f32691c9SRobert Mustacchi <S_EXPR>0[xX][0-9a-fA-F][0-9a-fA-F_]*	{
4827c478bd9Sstevel@tonic-gate 		/*
4837c478bd9Sstevel@tonic-gate 		 * Hexadecimal value.
4847c478bd9Sstevel@tonic-gate 		 */
4854585130bSYuri Pankov 		yylval.l_immediate = mdb_strtonum(yytext + 2, 16);
4867c478bd9Sstevel@tonic-gate 		return (MDB_TOK_IMMEDIATE);
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate 
489*f32691c9SRobert Mustacchi <S_INITIAL>[0-9a-fA-F][0-9a-fA-F_]*	|
490*f32691c9SRobert Mustacchi <S_EXPR>[0-9a-fA-F][0-9a-fA-F_]*	{
4917c478bd9Sstevel@tonic-gate 		GElf_Sym sym;
4927c478bd9Sstevel@tonic-gate 		/*
4937c478bd9Sstevel@tonic-gate 		 * Immediate values without an explicit base are converted
4947c478bd9Sstevel@tonic-gate 		 * using the default radix (user configurable).  However, if
4957c478bd9Sstevel@tonic-gate 		 * the token does *not* begin with a digit, it is also a
4967c478bd9Sstevel@tonic-gate 		 * potential symbol (e.g. "f") so we have to check that first.
4977c478bd9Sstevel@tonic-gate 		 */
4987c478bd9Sstevel@tonic-gate 		if (strchr("0123456789", yytext[0]) == NULL &&
4997c478bd9Sstevel@tonic-gate 		    mdb_tgt_lookup_by_name(mdb.m_target,
5007c478bd9Sstevel@tonic-gate 		    MDB_TGT_OBJ_EVERY, yytext, &sym, NULL) == 0)
5017c478bd9Sstevel@tonic-gate 			yylval.l_immediate = (uintmax_t)sym.st_value;
5027c478bd9Sstevel@tonic-gate 		else
5034585130bSYuri Pankov 			yylval.l_immediate = mdb_strtonum(yytext, mdb.m_radix);
5047c478bd9Sstevel@tonic-gate 		return (MDB_TOK_IMMEDIATE);
5057c478bd9Sstevel@tonic-gate 	}
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate <S_INITIAL>{RGX_SYMBOL}	|
5087c478bd9Sstevel@tonic-gate <S_EXPR>{RGX_SYMBOL}	{
5097c478bd9Sstevel@tonic-gate 		/*
5107c478bd9Sstevel@tonic-gate 		 * Symbol -- parser will look up in symbol table.
5117c478bd9Sstevel@tonic-gate 		 */
5127c478bd9Sstevel@tonic-gate 		yylval.l_string = strdup(yytext);
5137c478bd9Sstevel@tonic-gate 		return (MDB_TOK_SYMBOL);
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate ";"|\n	{
5177c478bd9Sstevel@tonic-gate 		/*
5187c478bd9Sstevel@tonic-gate 		 * End of command -- return to start state and return literal.
5197c478bd9Sstevel@tonic-gate 		 */
5207c478bd9Sstevel@tonic-gate 		BEGIN(S_INITIAL);
5217c478bd9Sstevel@tonic-gate 		return (yytext[0]);
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate [ \t]	;			/* Ignore whitespace */
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate .	return (yytext[0]);	/* Return anything else */
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate %%
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate void
5317c478bd9Sstevel@tonic-gate mdb_lex_debug(int i)
5327c478bd9Sstevel@tonic-gate {
5337c478bd9Sstevel@tonic-gate 	yydebug = i;
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate void
5377c478bd9Sstevel@tonic-gate mdb_lex_reset(void)
5387c478bd9Sstevel@tonic-gate {
5397c478bd9Sstevel@tonic-gate 	BEGIN(S_INITIAL);
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate void
5437c478bd9Sstevel@tonic-gate yydiscard(void)
5447c478bd9Sstevel@tonic-gate {
5457c478bd9Sstevel@tonic-gate 	int c;
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	/*
548*f32691c9SRobert Mustacchi 	 * If stdin is a string, pipeline, or tty, throw away all our buffered
5497c478bd9Sstevel@tonic-gate 	 * data. Otherwise discard characters up to the next likely delimiter.
5507c478bd9Sstevel@tonic-gate 	 */
5517c478bd9Sstevel@tonic-gate 	if (mdb_iob_isastr(mdb.m_in) || mdb_iob_isatty(mdb.m_in) ||
5527c478bd9Sstevel@tonic-gate 	    mdb_iob_isapipe(mdb.m_in))
5537c478bd9Sstevel@tonic-gate 		mdb_iob_discard(mdb.m_in);
5547c478bd9Sstevel@tonic-gate 	else {
5557c478bd9Sstevel@tonic-gate 		while ((c = mdb_iob_getc(mdb.m_in)) != (int)EOF) {
5567c478bd9Sstevel@tonic-gate 			if (c == ';' || c == '\n')
5577c478bd9Sstevel@tonic-gate 				break;
5587c478bd9Sstevel@tonic-gate 		}
5597c478bd9Sstevel@tonic-gate 	}
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	BEGIN(S_INITIAL);
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate static void
5657c478bd9Sstevel@tonic-gate yyerror_reset(void)
5667c478bd9Sstevel@tonic-gate {
5677c478bd9Sstevel@tonic-gate 	yydiscard();
5687c478bd9Sstevel@tonic-gate 	mdb_argvec_reset(&mdb.m_frame->f_argvec);
5697c478bd9Sstevel@tonic-gate 	longjmp(mdb.m_frame->f_pcb, MDB_ERR_PARSE);
5707c478bd9Sstevel@tonic-gate }
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate void
5737c478bd9Sstevel@tonic-gate yyerror(const char *format, ...)
5747c478bd9Sstevel@tonic-gate {
5757c478bd9Sstevel@tonic-gate 	va_list alist;
5767c478bd9Sstevel@tonic-gate 	char *s;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	mdb_iob_printf(mdb.m_err, "%s: ", mdb.m_pname);
5797c478bd9Sstevel@tonic-gate 	va_start(alist, format);
5807c478bd9Sstevel@tonic-gate 	mdb_iob_vprintf(mdb.m_err, format, alist);
5817c478bd9Sstevel@tonic-gate 	va_end(alist);
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	if (strchr(format, '\n') == NULL) {
5847c478bd9Sstevel@tonic-gate 		if (!mdb_iob_isatty(mdb.m_in)) {
5857c478bd9Sstevel@tonic-gate 			mdb_iob_printf(mdb.m_err, " on line %d of %s",
5867c478bd9Sstevel@tonic-gate 			    yylineno, mdb_iob_name(mdb.m_in));
5877c478bd9Sstevel@tonic-gate 		}
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 		s = strchr2esc(yytext, strlen(yytext));
5907c478bd9Sstevel@tonic-gate 		mdb_iob_printf(mdb.m_err, " near \"%s\"\n", s);
5917c478bd9Sstevel@tonic-gate 		strfree(s);
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	yyerror_reset();
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate void
5987c478bd9Sstevel@tonic-gate yyperror(const char *format, ...)
5997c478bd9Sstevel@tonic-gate {
6007c478bd9Sstevel@tonic-gate 	va_list alist;
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	va_start(alist, format);
6037c478bd9Sstevel@tonic-gate 	vwarn(format, alist);
6047c478bd9Sstevel@tonic-gate 	va_end(alist);
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	yyerror_reset();
6077c478bd9Sstevel@tonic-gate }
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate int
6107c478bd9Sstevel@tonic-gate yywrap(void)
6117c478bd9Sstevel@tonic-gate {
6127c478bd9Sstevel@tonic-gate 	mdb_dprintf(MDB_DBG_PARSER, "yywrap at line %d\n", yylineno);
6137c478bd9Sstevel@tonic-gate 	return (1); /* indicate that lex should return a zero token for EOF */
6147c478bd9Sstevel@tonic-gate }
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
6177c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6187c478bd9Sstevel@tonic-gate int
6197c478bd9Sstevel@tonic-gate yyfprintf(FILE *stream, const char *format, ...)
6207c478bd9Sstevel@tonic-gate {
6217c478bd9Sstevel@tonic-gate 	va_list alist;
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	va_start(alist, format);
6247c478bd9Sstevel@tonic-gate 	mdb_iob_vprintf(mdb.m_err, format, alist);
6257c478bd9Sstevel@tonic-gate 	va_end(alist);
6267c478bd9Sstevel@tonic-gate 	return (0);
6277c478bd9Sstevel@tonic-gate }
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
6307c478bd9Sstevel@tonic-gate int
6317c478bd9Sstevel@tonic-gate yyprintf(const char *format, ...)
6327c478bd9Sstevel@tonic-gate {
6337c478bd9Sstevel@tonic-gate 	va_list alist;
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	va_start(alist, format);
6367c478bd9Sstevel@tonic-gate 	mdb_iob_vprintf(mdb.m_err, format, alist);
6377c478bd9Sstevel@tonic-gate 	va_end(alist);
6387c478bd9Sstevel@tonic-gate 	return (0);
6397c478bd9Sstevel@tonic-gate }
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate static int
6427c478bd9Sstevel@tonic-gate input(void)
6437c478bd9Sstevel@tonic-gate {
6447c478bd9Sstevel@tonic-gate 	int c = mdb_iob_getc(mdb.m_in);
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	if (c == '\n')
6477c478bd9Sstevel@tonic-gate 		yylineno++;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	return (c == EOF ? 0 : c);
6507c478bd9Sstevel@tonic-gate }
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate static void
6537c478bd9Sstevel@tonic-gate unput(int c)
6547c478bd9Sstevel@tonic-gate {
6557c478bd9Sstevel@tonic-gate 	if (c == '\n')
6567c478bd9Sstevel@tonic-gate 		yylineno--;
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	(void) mdb_iob_ungetc(mdb.m_in, c == 0 ? EOF : c);
6597c478bd9Sstevel@tonic-gate }
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate static void
6627c478bd9Sstevel@tonic-gate output(int c)
6637c478bd9Sstevel@tonic-gate {
6647c478bd9Sstevel@tonic-gate 	char ch = c;
6657c478bd9Sstevel@tonic-gate 	mdb_iob_nputs(mdb.m_out, &ch, sizeof (ch));
6667c478bd9Sstevel@tonic-gate }
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate static char *
6697c478bd9Sstevel@tonic-gate string_nextquote(char *s, char q1, char q2)
6707c478bd9Sstevel@tonic-gate {
6717c478bd9Sstevel@tonic-gate 	char c = 0;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	do {
6747c478bd9Sstevel@tonic-gate 		if (c != '\\' && (*s == q1 || *s == q2))
6757c478bd9Sstevel@tonic-gate 			return (s);
6767c478bd9Sstevel@tonic-gate 	} while ((c = *s++) != '\0');
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	return (NULL);
6797c478bd9Sstevel@tonic-gate }
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate static void
6827c478bd9Sstevel@tonic-gate string_unquote(char *s)
6837c478bd9Sstevel@tonic-gate {
6847c478bd9Sstevel@tonic-gate 	char *o, *p, *q, c;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	for (o = p = s; (p = string_nextquote(p, '\'', '"')) != NULL; o = p) {
6877c478bd9Sstevel@tonic-gate 		/*
6887c478bd9Sstevel@tonic-gate 		 * If the quote wasn't the first character, advance
6897c478bd9Sstevel@tonic-gate 		 * the destination buffer past what we skipped.
6907c478bd9Sstevel@tonic-gate 		 */
6917c478bd9Sstevel@tonic-gate 		if (p > o) {
692a11593b6SAlexander Eremin 			/* Using memmove to prevent possible overlap. */
693a11593b6SAlexander Eremin 			(void) memmove(s, o, p - o);
6947c478bd9Sstevel@tonic-gate 			s += p - o;
6957c478bd9Sstevel@tonic-gate 		}
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 		c = *p;	/* Save the current quote */
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 		/*
7007c478bd9Sstevel@tonic-gate 		 * Look ahead and find the matching quote.  If none is
7017c478bd9Sstevel@tonic-gate 		 * found, use yyerror to longjmp out of the lexer.
7027c478bd9Sstevel@tonic-gate 		 */
7037c478bd9Sstevel@tonic-gate 		if (c == '"')
7047c478bd9Sstevel@tonic-gate 			q = string_nextquote(p + 1, c, c);
7057c478bd9Sstevel@tonic-gate 		else
7067c478bd9Sstevel@tonic-gate 			q = strchr(p + 1, c);
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 		if (q == NULL)
7097c478bd9Sstevel@tonic-gate 			yyerror("syntax error: %c unmatched", c);
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 		/*
7127c478bd9Sstevel@tonic-gate 		 * If the string is non-empty, copy it to the destination
7137c478bd9Sstevel@tonic-gate 		 * and convert escape sequences if *p is double-quote.
7147c478bd9Sstevel@tonic-gate 		 */
7157c478bd9Sstevel@tonic-gate 		if (q > p + 1) {
716a11593b6SAlexander Eremin 			(void) memmove(s, p + 1, q - p - 1);
7177c478bd9Sstevel@tonic-gate 			if (c == '"') {
7187c478bd9Sstevel@tonic-gate                                 s[q - p - 1] = '\0';
7197c478bd9Sstevel@tonic-gate 				s += stresc2chr(s);
7207c478bd9Sstevel@tonic-gate 			} else
7217c478bd9Sstevel@tonic-gate 				s += q - p - 1;
7227c478bd9Sstevel@tonic-gate 		}
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 		p = q + 1; /* Advance p past matching quote */
7257c478bd9Sstevel@tonic-gate 	}
7267c478bd9Sstevel@tonic-gate 
727a11593b6SAlexander Eremin 	(void) memmove(s, o, strlen(o) + 1);
7287c478bd9Sstevel@tonic-gate }
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate /*
7317c478bd9Sstevel@tonic-gate  * Unfortunately, lex and yacc produces code that is inherently global.  They do
7327c478bd9Sstevel@tonic-gate  * not provide routines to save and restore state, instead relying on global
7337c478bd9Sstevel@tonic-gate  * variables.  There is one single lex state, so that if a frame switch then
7347c478bd9Sstevel@tonic-gate  * tries to perform any evaluation, the old values are corrupted.  This
7357c478bd9Sstevel@tonic-gate  * structure and corresponding function provide a means of preserving lex state
7367c478bd9Sstevel@tonic-gate  * across frame switches.  Note that this is tied to the lex implementation, so
7377c478bd9Sstevel@tonic-gate  * if the lex compiler is changed or upgraded to a different format, then this
7387c478bd9Sstevel@tonic-gate  * may need to be altered.  This is unavoidable due to the implementation of lex
7397c478bd9Sstevel@tonic-gate  * and yacc. This is essentially a collection of all the global variables
7407c478bd9Sstevel@tonic-gate  * defined by the lex code, excluding those that do not change through the
7417c478bd9Sstevel@tonic-gate  * course of yylex() and yyparse().
7427c478bd9Sstevel@tonic-gate  */
7437c478bd9Sstevel@tonic-gate extern struct yysvf *yylstate[], **yylsp, **yyolsp; extern int yyprevious;
7447c478bd9Sstevel@tonic-gate extern int *yyfnd;
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate extern YYSTYPE *yypv;
7477c478bd9Sstevel@tonic-gate extern int *yyps;
7487c478bd9Sstevel@tonic-gate extern int yytmp;
7497c478bd9Sstevel@tonic-gate extern int yystate;
7507c478bd9Sstevel@tonic-gate extern int yynerrs;
7517c478bd9Sstevel@tonic-gate extern int yyerrflag;
7527c478bd9Sstevel@tonic-gate extern int yychar;
7537c478bd9Sstevel@tonic-gate extern YYSTYPE yylval;
7547c478bd9Sstevel@tonic-gate extern YYSTYPE yyval;
7557c478bd9Sstevel@tonic-gate extern int *yys;
7567c478bd9Sstevel@tonic-gate extern YYSTYPE *yyv;
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate typedef struct mdb_lex_state {
7597c478bd9Sstevel@tonic-gate 	/* Variables needed by yylex */
7607c478bd9Sstevel@tonic-gate 	int	yyleng;
7617c478bd9Sstevel@tonic-gate 	char	yytext[YYLMAX];
7627c478bd9Sstevel@tonic-gate 	int	yymorfg;
7637c478bd9Sstevel@tonic-gate 	int	yylineno;
7647c478bd9Sstevel@tonic-gate 	void	*yyestate;
7657c478bd9Sstevel@tonic-gate 	void	*yylstate[BUFSIZ];
7667c478bd9Sstevel@tonic-gate 	void	*yylsp;
7677c478bd9Sstevel@tonic-gate 	void	*yyolsp;
7687c478bd9Sstevel@tonic-gate 	int	*yyfnd;
7697c478bd9Sstevel@tonic-gate 	int	yyprevious;
7707c478bd9Sstevel@tonic-gate 	void	*yybgin;
7717c478bd9Sstevel@tonic-gate 	/* Variables needed by yyparse */
772*f32691c9SRobert Mustacchi 	void	*yypv;
7737c478bd9Sstevel@tonic-gate 	int	*yyps;
7747c478bd9Sstevel@tonic-gate 	int	yytmp;
7757c478bd9Sstevel@tonic-gate 	int	yystate;
7767c478bd9Sstevel@tonic-gate 	int	yynerrs;
7777c478bd9Sstevel@tonic-gate 	int	yyerrflag;
7787c478bd9Sstevel@tonic-gate 	int	yychar;
7797c478bd9Sstevel@tonic-gate 	YYSTYPE	yylval;
7807c478bd9Sstevel@tonic-gate 	YYSTYPE	yyval;
7817c478bd9Sstevel@tonic-gate 	int	yys[YYMAXDEPTH];
7827c478bd9Sstevel@tonic-gate 	YYSTYPE	yyv[YYMAXDEPTH];
7837c478bd9Sstevel@tonic-gate } mdb_lex_state_t;
7847c478bd9Sstevel@tonic-gate 
785*f32691c9SRobert Mustacchi void
7867c478bd9Sstevel@tonic-gate mdb_lex_state_save(mdb_lex_state_t *s)
7877c478bd9Sstevel@tonic-gate {
7887c478bd9Sstevel@tonic-gate 	ASSERT(s != NULL);
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	s->yyleng = yyleng;
7917c478bd9Sstevel@tonic-gate 	s->yymorfg = yymorfg;
7927c478bd9Sstevel@tonic-gate 	s->yylineno = yylineno;
7937c478bd9Sstevel@tonic-gate 	s->yyestate = yyestate;
7947c478bd9Sstevel@tonic-gate 	bcopy(yylstate, s->yylstate, YYLMAX * sizeof (void *));
7957c478bd9Sstevel@tonic-gate 	s->yylsp = yylsp;
7967c478bd9Sstevel@tonic-gate 	s->yyolsp = yyolsp;
7977c478bd9Sstevel@tonic-gate 	s->yyfnd = yyfnd;
7987c478bd9Sstevel@tonic-gate 	s->yyprevious = yyprevious;
7997c478bd9Sstevel@tonic-gate 	s->yybgin = yybgin;
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	s->yypv = yypv;
8027c478bd9Sstevel@tonic-gate 	s->yyps = yyps;
8037c478bd9Sstevel@tonic-gate 	s->yystate = yystate;
8047c478bd9Sstevel@tonic-gate 	s->yytmp = yytmp;
8057c478bd9Sstevel@tonic-gate 	s->yynerrs = yynerrs;
8067c478bd9Sstevel@tonic-gate 	s->yyerrflag = yyerrflag;
8077c478bd9Sstevel@tonic-gate 	s->yychar = yychar;
8087c478bd9Sstevel@tonic-gate 	s->yylval = yylval;
8097c478bd9Sstevel@tonic-gate 	s->yyval = yyval;
8107c478bd9Sstevel@tonic-gate }
8117c478bd9Sstevel@tonic-gate 
812*f32691c9SRobert Mustacchi void
8137c478bd9Sstevel@tonic-gate mdb_lex_state_restore(mdb_lex_state_t *s)
8147c478bd9Sstevel@tonic-gate {
8157c478bd9Sstevel@tonic-gate 	ASSERT(s != NULL);
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	yyleng = s->yyleng;
8187c478bd9Sstevel@tonic-gate 	yytext = s->yytext;
8197c478bd9Sstevel@tonic-gate 	yymorfg = s->yymorfg;
8207c478bd9Sstevel@tonic-gate 	yylineno = s->yylineno;
8217c478bd9Sstevel@tonic-gate 	yyestate = s->yyestate;
8227c478bd9Sstevel@tonic-gate 	bcopy(s->yylstate, yylstate, YYLMAX * sizeof (void *));
8237c478bd9Sstevel@tonic-gate 	yylsp = s->yylsp;
8247c478bd9Sstevel@tonic-gate 	yyolsp = s->yyolsp;
8257c478bd9Sstevel@tonic-gate 	yyfnd = s->yyfnd;
8267c478bd9Sstevel@tonic-gate 	yyprevious = s->yyprevious;
8277c478bd9Sstevel@tonic-gate 	yybgin = s->yybgin;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	yypv = s->yypv;
8307c478bd9Sstevel@tonic-gate 	yyps = s->yyps;
8317c478bd9Sstevel@tonic-gate 	yystate = s->yystate;
8327c478bd9Sstevel@tonic-gate 	yytmp = s->yytmp;
8337c478bd9Sstevel@tonic-gate 	yynerrs = s->yynerrs;
8347c478bd9Sstevel@tonic-gate 	yyerrflag = s->yyerrflag;
8357c478bd9Sstevel@tonic-gate 	yychar = s->yychar;
8367c478bd9Sstevel@tonic-gate 	yylval = s->yylval;
8377c478bd9Sstevel@tonic-gate 	yyval = s->yyval;
8387c478bd9Sstevel@tonic-gate 	yys = s->yys;
8397c478bd9Sstevel@tonic-gate 	yyv = s->yyv;
8407c478bd9Sstevel@tonic-gate }
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate /*
8437c478bd9Sstevel@tonic-gate  * Create and initialize the lex/yacc-specific state associated with a frame
8447c478bd9Sstevel@tonic-gate  * structure.  We set all fields to known safe values so that
8457c478bd9Sstevel@tonic-gate  * mdb_lex_state_restore() can be used safely before mdb_lex_state_save().
8467c478bd9Sstevel@tonic-gate  */
8477c478bd9Sstevel@tonic-gate void
8487c478bd9Sstevel@tonic-gate mdb_lex_state_create(mdb_frame_t *f)
8497c478bd9Sstevel@tonic-gate {
8507c478bd9Sstevel@tonic-gate 	f->f_lstate = mdb_alloc(sizeof (mdb_lex_state_t), UM_SLEEP);
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	yyleng = 0;
8537c478bd9Sstevel@tonic-gate 	yymorfg = 0;
8547c478bd9Sstevel@tonic-gate 	/* yytext is fine with garbage in it */
8557c478bd9Sstevel@tonic-gate 	yytext = f->f_lstate->yytext;
8567c478bd9Sstevel@tonic-gate 	yylineno = 1;
8577c478bd9Sstevel@tonic-gate 	yyestate = NULL;
8587c478bd9Sstevel@tonic-gate 	bzero(yylstate, YYLMAX * sizeof (void *));
8597c478bd9Sstevel@tonic-gate 	yylsp = NULL;
8607c478bd9Sstevel@tonic-gate 	yyolsp = NULL;
8617c478bd9Sstevel@tonic-gate 	yyfnd = 0;
8627c478bd9Sstevel@tonic-gate 	yyprevious = YYNEWLINE;
8637c478bd9Sstevel@tonic-gate 	yys = f->f_lstate->yys;
8647c478bd9Sstevel@tonic-gate 	yyv = f->f_lstate->yyv;
8657c478bd9Sstevel@tonic-gate 	mdb_argvec_create(&f->f_argvec);
8667c478bd9Sstevel@tonic-gate 	f->f_oldstate = 0;
8677c478bd9Sstevel@tonic-gate 	mdb_lex_reset(); /* Responsible for setting yybgin */
8687c478bd9Sstevel@tonic-gate }
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate void
8717c478bd9Sstevel@tonic-gate mdb_lex_state_destroy(mdb_frame_t *f)
8727c478bd9Sstevel@tonic-gate {
8737c478bd9Sstevel@tonic-gate 	mdb_free(f->f_lstate, sizeof (mdb_lex_state_t));
8747c478bd9Sstevel@tonic-gate 	f->f_lstate = NULL;
8757c478bd9Sstevel@tonic-gate 	mdb_argvec_destroy(&f->f_argvec);
8767c478bd9Sstevel@tonic-gate }
877