Blame command.y

Packit 575503
/*
Packit 575503
 * command.y - yacc/bison parser for debugger commands.
Packit 575503
 */
Packit 575503
Packit 575503
/*
Packit 575503
 * Copyright (C) 2004, 2010, 2011, 2014, 2016, 2017
Packit 575503
 * the Free Software Foundation, Inc.
Packit 575503
 *
Packit 575503
 * This file is part of GAWK, the GNU implementation of the
Packit 575503
 * AWK Programming Language.
Packit 575503
 *
Packit 575503
 * GAWK is free software; you can redistribute it and/or modify
Packit 575503
 * it under the terms of the GNU General Public License as published by
Packit 575503
 * the Free Software Foundation; either version 3 of the License, or
Packit 575503
 * (at your option) any later version.
Packit 575503
 *
Packit 575503
 * GAWK is distributed in the hope that it will be useful,
Packit 575503
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 575503
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 575503
 * GNU General Public License for more details.
Packit 575503
 *
Packit 575503
 * You should have received a copy of the GNU General Public License
Packit 575503
 * along with this program; if not, write to the Free Software
Packit 575503
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335,
Packit 575503
 * USA 
Packit 575503
 */
Packit 575503
Packit 575503
%{
Packit 575503
#include "awk.h"
Packit 575503
#include "cmd.h"
Packit 575503
Packit 575503
#if 0
Packit 575503
#define YYDEBUG 12
Packit 575503
int yydebug = 2;
Packit 575503
#endif
Packit 575503
Packit 575503
static int yylex(void);
Packit 575503
static void yyerror(const char *mesg, ...);
Packit 575503
Packit 575503
static int find_command(const char *token, size_t toklen);
Packit 575503
Packit 575503
static bool want_nodeval = false;
Packit 575503
Packit 575503
static int cmd_idx = -1;		/* index of current command in cmd table */
Packit 575503
static int repeat_idx = -1;		/* index of last repeatable command in command table */
Packit 575503
static CMDARG *arg_list = NULL;		/* list of arguments */
Packit 575503
static long errcount = 0;
Packit 575503
static char *lexptr_begin = NULL;
Packit 575503
static bool in_commands = false;
Packit 575503
static int num_dim;
Packit 575503
Packit 575503
static bool in_eval = false;
Packit 575503
static const char start_EVAL[] = "function @eval(){";
Packit 575503
static const char end_EVAL[] = "}";
Packit 575503
static CMDARG *append_statement(CMDARG *stmt_list, char *stmt);
Packit 575503
static NODE *concat_args(CMDARG *a, int count);
Packit 575503
Packit 575503
#ifdef HAVE_LIBREADLINE
Packit 575503
static char *next_word(char *p, int len, char **endp);
Packit 575503
static void history_expand_line(char **line);
Packit 575503
static char *command_generator(const char *text, int state);
Packit 575503
static char *srcfile_generator(const char *text, int state);
Packit 575503
static char *argument_generator(const char *text, int state);
Packit 575503
static char *variable_generator(const char *text, int state);
Packit 575503
extern char *option_generator(const char *text, int state);
Packit 575503
static int this_cmd = D_illegal;
Packit 575503
#else
Packit 575503
#define history_expand_line(p)	/* nothing */
Packit 575503
static int rl_inhibit_completion;	/* dummy variable */
Packit 575503
#endif
Packit 575503
Packit 575503
struct argtoken {
Packit 575503
	const char *name;
Packit 575503
	enum argtype cmd;
Packit 575503
	enum nametypeval value;
Packit 575503
};
Packit 575503
Packit 575503
/*
Packit 575503
 * These two should be static, but there are some compilers that
Packit 575503
 * don't like the static keyword with an empty size. Therefore give
Packit 575503
 * them names that are less likely to conflict with the rest of gawk.
Packit 575503
 */
Packit 575503
#define argtab zz_debug_argtab
Packit 575503
#define cmdtab zz_debug_cmdtab
Packit 575503
Packit 575503
extern struct argtoken argtab[];
Packit 575503
extern struct cmdtoken cmdtab[];
Packit 575503
Packit 575503
static CMDARG *mk_cmdarg(enum argtype type);
Packit 575503
static void append_cmdarg(CMDARG *arg);
Packit 575503
static int find_argument(CMDARG *arg);
Packit 575503
#define YYSTYPE CMDARG *
Packit 575503
%}
Packit 575503
Packit 575503
%token D_BACKTRACE D_BREAK D_CLEAR D_CONTINUE D_DELETE D_DISABLE D_DOWN
Packit 575503
%token D_ENABLE D_FINISH D_FRAME D_HELP D_IGNORE D_INFO D_LIST
Packit 575503
%token D_NEXT D_NEXTI D_PRINT D_PRINTF D_QUIT D_RETURN D_RUN D_SET
Packit 575503
%token D_STEP D_STEPI D_TBREAK D_UP D_UNTIL
Packit 575503
%token D_DISPLAY D_UNDISPLAY D_WATCH D_UNWATCH
Packit 575503
%token D_DUMP D_TRACE
Packit 575503
%token D_INT D_STRING D_NODE D_VARIABLE
Packit 575503
%token D_OPTION D_COMMANDS D_END D_SILENT D_SOURCE
Packit 575503
%token D_SAVE D_EVAL D_CONDITION
Packit 575503
%token D_STATEMENT
Packit 575503
Packit 575503
%%
Packit 575503
Packit 575503
input
Packit 575503
	: /* empty */
Packit 575503
	| input line
Packit 575503
	  {
Packit 575503
		cmd_idx = -1;
Packit 575503
		want_nodeval = false;
Packit 575503
		if (lexptr_begin != NULL) {
Packit 575503
			if (input_from_tty && lexptr_begin[0] != '\0')
Packit 575503
				add_history(lexptr_begin);
Packit 575503
			efree(lexptr_begin);
Packit 575503
			lexptr_begin = NULL;
Packit 575503
		}
Packit 575503
		if (arg_list != NULL) {
Packit 575503
			free_cmdarg(arg_list);
Packit 575503
			arg_list = NULL;
Packit 575503
		}
Packit 575503
	  }
Packit 575503
	;
Packit 575503
Packit 575503
line
Packit 575503
	: nls
Packit 575503
	| command nls
Packit 575503
	  {
Packit 575503
		if (errcount == 0 && cmd_idx >= 0) {
Packit 575503
			Func_cmd cmdfunc;
Packit 575503
			bool terminate = false;
Packit 575503
			CMDARG *args;
Packit 575503
			int ctype = 0;
Packit 575503
Packit 575503
			ctype = cmdtab[cmd_idx].type;
Packit 575503
Packit 575503
			/* a blank line repeats previous command
Packit 575503
			 * (list, next, nexti, step, stepi and continue without arguments).
Packit 575503
			 * save the index in the command table; used in yylex
Packit 575503
			 */
Packit 575503
			if ((ctype == D_list
Packit 575503
					|| ctype == D_next
Packit 575503
					|| ctype == D_step
Packit 575503
					|| ctype == D_nexti
Packit 575503
					|| ctype == D_stepi
Packit 575503
					|| ctype == D_continue)
Packit 575503
				&& arg_list == NULL
Packit 575503
				&& ! in_commands
Packit 575503
				&& input_from_tty
Packit 575503
			)
Packit 575503
				repeat_idx = cmd_idx;
Packit 575503
			else
Packit 575503
				repeat_idx = -1;
Packit 575503
Packit 575503
			/* call the command handler; reset the globals arg_list, cmd_idx,
Packit 575503
			 * since this handler could invoke yyparse again.
Packit 575503
			 * call do_commands for the list of commands in `commands';
Packit 575503
			 * arg_list isn't freed on return.
Packit 575503
			 */
Packit 575503
Packit 575503
			cmdfunc = cmdtab[cmd_idx].cf_ptr;
Packit 575503
			if (in_commands)
Packit 575503
				cmdfunc = do_commands;
Packit 575503
			cmd_idx = -1;
Packit 575503
			want_nodeval = false;
Packit 575503
Packit 575503
			args = arg_list;
Packit 575503
			arg_list = NULL;
Packit 575503
Packit 575503
			terminate = (*cmdfunc)(args, ctype);
Packit 575503
			if (! in_commands || ctype == D_commands)
Packit 575503
				free_cmdarg(args);
Packit 575503
			if (terminate)
Packit 575503
				YYACCEPT;
Packit 575503
		}
Packit 575503
	  }
Packit 575503
	| error nls
Packit 575503
	  {
Packit 575503
		yyerrok;
Packit 575503
	  }
Packit 575503
	;
Packit 575503
Packit 575503
control_cmd
Packit 575503
	: D_CONTINUE
Packit 575503
	| D_NEXT
Packit 575503
	| D_NEXTI
Packit 575503
	| D_STEP
Packit 575503
	| D_STEPI
Packit 575503
	;
Packit 575503
Packit 575503
d_cmd
Packit 575503
	: D_UNDISPLAY
Packit 575503
	| D_UNWATCH
Packit 575503
	| D_DISABLE
Packit 575503
	| D_DELETE
Packit 575503
	;
Packit 575503
Packit 575503
frame_cmd
Packit 575503
	: D_UP
Packit 575503
	| D_DOWN
Packit 575503
	| D_BACKTRACE
Packit 575503
	| D_FRAME
Packit 575503
	;
Packit 575503
Packit 575503
break_cmd
Packit 575503
	: D_BREAK
Packit 575503
	| D_TBREAK
Packit 575503
	;
Packit 575503
Packit 575503
/* mid-rule action buried in non-terminal to avoid conflict */
Packit 575503
set_want_nodeval
Packit 575503
	: { want_nodeval = true; }
Packit 575503
	;
Packit 575503
Packit 575503
eval_prologue
Packit 575503
	: D_EVAL set_want_nodeval opt_param_list nls
Packit 575503
	  {
Packit 575503
		if (errcount == 0) {
Packit 575503
			/* don't free arg_list;	passed on to statement_list
Packit 575503
			 * non-terminal (empty rule action). See below.
Packit 575503
			 */
Packit 575503
			if (input_from_tty) {
Packit 575503
				dbg_prompt = eval_prompt;
Packit 575503
				fprintf(out_fp,
Packit 575503
		_("Type (g)awk statement(s). End with the command \"end\"\n"));
Packit 575503
				rl_inhibit_completion = 1;
Packit 575503
			}
Packit 575503
			cmd_idx = -1;
Packit 575503
			in_eval = true;
Packit 575503
		}
Packit 575503
	  }
Packit 575503
	;
Packit 575503
Packit 575503
statement_list
Packit 575503
	: /* empty */
Packit 575503
	  {
Packit 575503
		$$ = append_statement(arg_list, (char *) start_EVAL);
Packit 575503
		if (read_a_line == read_commands_string)	/* unserializing 'eval' in 'commands' */
Packit 575503
			$$->a_string[0] = '\0';
Packit 575503
		free_cmdarg(arg_list);
Packit 575503
		arg_list = NULL;
Packit 575503
	  }
Packit 575503
	| statement_list D_STATEMENT { $$ = append_statement($1, lexptr_begin); } nls
Packit 575503
	  {
Packit 575503
		$$ = $3;
Packit 575503
	  }
Packit 575503
	;
Packit 575503
Packit 575503
eval_cmd
Packit 575503
	: eval_prologue statement_list D_END
Packit 575503
	  {
Packit 575503
		arg_list = append_statement($2, (char *) end_EVAL);
Packit 575503
		if (read_a_line == read_commands_string) {	/* unserializing 'eval' in 'commands' */
Packit 575503
			char *str = arg_list->a_string;
Packit 575503
			size_t len = strlen(str);
Packit 575503
			assert(len > 2 && str[len - 2] == '}');
Packit 575503
			str[len - 2] = '\0';
Packit 575503
		}
Packit 575503
		if (input_from_tty) {
Packit 575503
			dbg_prompt = in_commands ? commands_prompt : dgawk_prompt;
Packit 575503
			rl_inhibit_completion = 0;
Packit 575503
		}
Packit 575503
		cmd_idx = find_command("eval", 4);
Packit 575503
		in_eval = false;
Packit 575503
	  }
Packit 575503
	| D_EVAL set_want_nodeval string_node
Packit 575503
	  {
Packit 575503
		NODE *n;
Packit 575503
		CMDARG *arg;
Packit 575503
		n = $3->a_node;
Packit 575503
		arg = append_statement(NULL, (char *) start_EVAL);
Packit 575503
		(void) append_statement(arg, n->stptr);
Packit 575503
		(void) append_statement(arg, (char *) end_EVAL);
Packit 575503
		free_cmdarg(arg_list);
Packit 575503
		arg_list = arg;
Packit 575503
	  }
Packit 575503
	;
Packit 575503
Packit 575503
command
Packit 575503
	: D_HELP help_args
Packit 575503
	| D_QUIT
Packit 575503
	| D_RUN
Packit 575503
	| D_FINISH
Packit 575503
	| control_cmd opt_plus_integer
Packit 575503
	| frame_cmd opt_integer
Packit 575503
	  {
Packit 575503
		if (cmdtab[cmd_idx].class == D_FRAME
Packit 575503
				&& $2 != NULL && $2->a_int < 0)
Packit 575503
			yyerror(_("invalid frame number: %d"), $2->a_int);
Packit 575503
	  }
Packit 575503
	| D_INFO D_STRING
Packit 575503
	  {
Packit 575503
		int idx = find_argument($2);
Packit 575503
		if (idx < 0)
Packit 575503
			yyerror(_("info: invalid option - \"%s\""), $2->a_string);
Packit 575503
		else {
Packit 575503
			efree($2->a_string);
Packit 575503
			$2->a_string = NULL;
Packit 575503
			$2->type = D_argument;
Packit 575503
			$2->a_argument = argtab[idx].value;
Packit 575503
		}
Packit 575503
	  }
Packit 575503
	| D_IGNORE plus_integer D_INT
Packit 575503
	| D_ENABLE enable_args
Packit 575503
	| D_PRINT { want_nodeval = true; } print_args
Packit 575503
	| D_PRINTF { want_nodeval = true; } printf_args
Packit 575503
	| D_LIST list_args
Packit 575503
	| D_UNTIL location
Packit 575503
	| D_CLEAR location
Packit 575503
	| break_cmd break_args
Packit 575503
	| D_SET { want_nodeval = true; } variable '=' node
Packit 575503
	| D_OPTION option_args
Packit 575503
	| D_RETURN { want_nodeval = true; } opt_node
Packit 575503
	| D_DISPLAY { want_nodeval = true; } opt_variable
Packit 575503
	| D_WATCH { want_nodeval = true; } variable condition_exp
Packit 575503
	| d_cmd opt_integer_list
Packit 575503
	| D_DUMP opt_string
Packit 575503
	| D_SOURCE D_STRING
Packit 575503
	  {
Packit 575503
		if (in_cmd_src($2->a_string))
Packit 575503
			yyerror(_("source \"%s\": already sourced."), $2->a_string);
Packit 575503
	  }
Packit 575503
	| D_SAVE D_STRING
Packit 575503
	  {
Packit 575503
		if (! input_from_tty)
Packit 575503
			yyerror(_("save \"%s\": command not permitted."), $2->a_string);
Packit 575503
	  }
Packit 575503
	| D_COMMANDS commands_arg
Packit 575503
	  {
Packit 575503
		int type = 0;
Packit 575503
		int num;
Packit 575503
Packit 575503
		if ($2 != NULL)
Packit 575503
			num = $2->a_int;
Packit 575503
Packit 575503
		if (errcount != 0)
Packit 575503
			;
Packit 575503
		else if (in_commands)
Packit 575503
			yyerror(_("Can't use command `commands' for breakpoint/watchpoint commands"));
Packit 575503
		else if ($2 == NULL &&  ! (type = has_break_or_watch_point(&num, true)))
Packit 575503
			yyerror(_("no breakpoint/watchpoint has been set yet"));
Packit 575503
		else if ($2 != NULL && ! (type = has_break_or_watch_point(&num, false)))
Packit 575503
			yyerror(_("invalid breakpoint/watchpoint number"));
Packit 575503
		if (type) {
Packit 575503
			in_commands = true;
Packit 575503
			if (input_from_tty) {
Packit 575503
				dbg_prompt = commands_prompt;
Packit 575503
				fprintf(out_fp, _("Type commands for when %s %d is hit, one per line.\n"),
Packit 575503
								(type == D_break) ? "breakpoint" : "watchpoint", num);
Packit 575503
				fprintf(out_fp, _("End with the command \"end\"\n"));
Packit 575503
			}
Packit 575503
		}
Packit 575503
	  }
Packit 575503
	| D_END
Packit 575503
	  {
Packit 575503
		if (! in_commands)
Packit 575503
			yyerror(_("`end' valid only in command `commands' or `eval'"));
Packit 575503
		else {
Packit 575503
			if (input_from_tty)
Packit 575503
				dbg_prompt = dgawk_prompt;
Packit 575503
			in_commands = false;
Packit 575503
		}
Packit 575503
	  }
Packit 575503
	| D_SILENT
Packit 575503
	  {
Packit 575503
		if (! in_commands)
Packit 575503
			yyerror(_("`silent' valid only in command `commands'"));
Packit 575503
	  }
Packit 575503
	| D_TRACE D_STRING
Packit 575503
	  {
Packit 575503
		int idx = find_argument($2);
Packit 575503
		if (idx < 0)
Packit 575503
			yyerror(_("trace: invalid option - \"%s\""), $2->a_string);
Packit 575503
		else {
Packit 575503
			efree($2->a_string);
Packit 575503
			$2->a_string = NULL;
Packit 575503
			$2->type = D_argument;
Packit 575503
			$2->a_argument = argtab[idx].value;
Packit 575503
		}
Packit 575503
	  }
Packit 575503
	| D_CONDITION plus_integer { want_nodeval = true; } condition_exp
Packit 575503
	  {
Packit 575503
		int type;
Packit 575503
		int num = $2->a_int;
Packit 575503
		type = has_break_or_watch_point(&num, false);
Packit 575503
		if (! type)
Packit 575503
			yyerror(_("condition: invalid breakpoint/watchpoint number"));
Packit 575503
	  }
Packit 575503
	| eval_cmd
Packit 575503
	  {
Packit 575503
		if (in_commands) {
Packit 575503
			/* Prepend command 'eval' to argument list */
Packit 575503
			CMDARG *arg;
Packit 575503
			arg = mk_cmdarg(D_string);
Packit 575503
			arg->a_string = estrdup("eval", 4);
Packit 575503
			arg->next = arg_list;
Packit 575503
			arg_list = arg;
Packit 575503
		}
Packit 575503
	  }
Packit 575503
	;
Packit 575503
Packit 575503
condition_exp
Packit 575503
	: opt_string_node
Packit 575503
	  {
Packit 575503
		if ($1 != NULL) {
Packit 575503
			NODE *n = $1->a_node;
Packit 575503
			$1->type = D_string;
Packit 575503
			$1->a_string = n->stptr;
Packit 575503
			freenode(n);
Packit 575503
		}
Packit 575503
		$$ = $1;
Packit 575503
	  }
Packit 575503
	;
Packit 575503
Packit 575503
commands_arg
Packit 575503
	: opt_plus_integer
Packit 575503
	| error
Packit 575503
	  {	$$ = NULL; }
Packit 575503
	;
Packit 575503
Packit 575503
opt_param_list
Packit 575503
	: /* empty */
Packit 575503
	  { $$ = NULL; }
Packit 575503
	| param_list
Packit 575503
	;
Packit 575503
Packit 575503
param_list
Packit 575503
	: D_VARIABLE
Packit 575503
	| param_list D_VARIABLE
Packit 575503
	| param_list ',' D_VARIABLE
Packit 575503
	| error
Packit 575503
	  { $$ = NULL; }
Packit 575503
	;
Packit 575503
Packit 575503
opt_string_node
Packit 575503
	: /* empty */
Packit 575503
	  { $$ = NULL; }
Packit 575503
	| string_node
Packit 575503
	| error
Packit 575503
	  { $$ = NULL; }
Packit 575503
	;
Packit 575503
Packit 575503
string_node
Packit 575503
	: D_NODE
Packit 575503
	  {
Packit 575503
		NODE *n;
Packit 575503
		n = $1->a_node;
Packit 575503
		if ((n->flags & STRING) == 0)
Packit 575503
			yyerror(_("argument not a string"));
Packit 575503
	  }
Packit 575503
	;
Packit 575503
Packit 575503
option_args
Packit 575503
	: /* empty */
Packit 575503
	  { $$ = NULL; }
Packit 575503
	| D_STRING
Packit 575503
	  {
Packit 575503
		if (find_option($1->a_string) < 0)
Packit 575503
			yyerror(_("option: invalid parameter - \"%s\""), $1->a_string);
Packit 575503
 	  }
Packit 575503
	| D_STRING '=' D_STRING
Packit 575503
	  {
Packit 575503
		if (find_option($1->a_string) < 0)
Packit 575503
			yyerror(_("option: invalid parameter - \"%s\""), $1->a_string);
Packit 575503
 	  }
Packit 575503
	;
Packit 575503
Packit 575503
func_name
Packit 575503
	: D_STRING
Packit 575503
	  {
Packit 575503
		NODE *n;
Packit 575503
		n = lookup($1->a_string);
Packit 575503
		if (n == NULL || n->type != Node_func)
Packit 575503
			yyerror(_("no such function - \"%s\""), $1->a_string);
Packit 575503
		else {
Packit 575503
			$1->type = D_func;
Packit 575503
			efree($1->a_string);
Packit 575503
			$1->a_string = NULL;
Packit 575503
			$1->a_node = n;
Packit 575503
		}
Packit 575503
	  }
Packit 575503
	;
Packit 575503
Packit 575503
location
Packit 575503
	: /* empty */
Packit 575503
	  { $$ = NULL; }
Packit 575503
	| plus_integer
Packit 575503
	| func_name
Packit 575503
	| D_STRING ':' plus_integer
Packit 575503
	| D_STRING ':' func_name
Packit 575503
	;
Packit 575503
Packit 575503
break_args
Packit 575503
	: /* empty */
Packit 575503
	  { $$ = NULL; }
Packit 575503
	| plus_integer { want_nodeval = true; } condition_exp
Packit 575503
	| func_name
Packit 575503
	| D_STRING ':' plus_integer { want_nodeval = true; } condition_exp
Packit 575503
	| D_STRING ':' func_name
Packit 575503
	;
Packit 575503
Packit 575503
opt_variable
Packit 575503
	: /* empty */
Packit 575503
	  { $$ = NULL; }
Packit 575503
	| variable
Packit 575503
	;
Packit 575503
Packit 575503
opt_string
Packit 575503
	: /* empty */
Packit 575503
	  { $$ = NULL; }
Packit 575503
	| D_STRING
Packit 575503
	;
Packit 575503
Packit 575503
opt_node
Packit 575503
	: /* empty */
Packit 575503
	  { $$ = NULL; }
Packit 575503
	| node
Packit 575503
	;
Packit 575503
Packit 575503
help_args
Packit 575503
	: /* empty */
Packit 575503
	| D_STRING
Packit 575503
	;
Packit 575503
Packit 575503
enable_args
Packit 575503
	: opt_integer_list
Packit 575503
	| D_STRING opt_integer_list
Packit 575503
	  {
Packit 575503
		int idx = find_argument($1);
Packit 575503
		if (idx < 0)
Packit 575503
			yyerror(_("enable: invalid option - \"%s\""), $1->a_string);
Packit 575503
		else {
Packit 575503
			efree($1->a_string);
Packit 575503
			$1->a_string = NULL;
Packit 575503
			$1->type = D_argument;
Packit 575503
			$1->a_argument = argtab[idx].value;
Packit 575503
		}
Packit 575503
	  }
Packit 575503
	;
Packit 575503
Packit 575503
print_exp
Packit 575503
	: variable
Packit 575503
	| '@' D_VARIABLE
Packit 575503
	  {
Packit 575503
		$2->type = D_array;	/* dump all items */
Packit 575503
		$2->a_count = 0;
Packit 575503
	  }
Packit 575503
	| '@' D_VARIABLE subscript_list /* dump sub-array items*/
Packit 575503
	  {
Packit 575503
		$2->type = D_array;
Packit 575503
		$2->a_count = num_dim;
Packit 575503
	  }
Packit 575503
	;
Packit 575503
Packit 575503
print_args
Packit 575503
	: print_exp
Packit 575503
	| print_args print_exp
Packit 575503
	| print_args ',' print_exp
Packit 575503
	| error
Packit 575503
	;
Packit 575503
Packit 575503
printf_exp
Packit 575503
	: D_NODE
Packit 575503
	| variable
Packit 575503
	;
Packit 575503
Packit 575503
printf_args
Packit 575503
	: printf_exp
Packit 575503
	| printf_args ',' printf_exp
Packit 575503
	| error
Packit 575503
	;
Packit 575503
Packit 575503
list_args
Packit 575503
	: /* empty */
Packit 575503
	  { $$ = NULL; }
Packit 575503
	| '+'
Packit 575503
	  { $$ = NULL; }
Packit 575503
	| '-'
Packit 575503
	  {
Packit 575503
		CMDARG *a;
Packit 575503
		a = mk_cmdarg(D_int);
Packit 575503
		a->a_int = -1;
Packit 575503
		append_cmdarg(a);
Packit 575503
	  }
Packit 575503
	| plus_integer
Packit 575503
	| func_name
Packit 575503
	| integer_range
Packit 575503
	| D_STRING ':' plus_integer
Packit 575503
	| D_STRING ':' func_name
Packit 575503
	| D_STRING ':' integer_range
Packit 575503
	;
Packit 575503
Packit 575503
integer_range
Packit 575503
	: plus_integer '-' plus_integer
Packit 575503
	  {
Packit 575503
		if ($1->a_int > $3->a_int)
Packit 575503
			yyerror(_("invalid range specification: %d - %d"),
Packit 575503
				$1->a_int, $3->a_int);
Packit 575503
		else
Packit 575503
			$1->type = D_range;
Packit 575503
		$$ = $1;
Packit 575503
	  }
Packit 575503
	;
Packit 575503
Packit 575503
opt_integer_list
Packit 575503
	: /* empty */
Packit 575503
	  { $$ = NULL; }
Packit 575503
	| integer_list
Packit 575503
	| error
Packit 575503
	;
Packit 575503
Packit 575503
integer_list
Packit 575503
	: plus_integer
Packit 575503
	| integer_range
Packit 575503
	| integer_list plus_integer
Packit 575503
	| integer_list integer_range
Packit 575503
	;
Packit 575503
Packit 575503
exp_list
Packit 575503
	: node
Packit 575503
	  { $$ = $1; }
Packit 575503
	| exp_list ',' node
Packit 575503
	  { $$ = $1; }
Packit 575503
	| error
Packit 575503
	;
Packit 575503
Packit 575503
subscript
Packit 575503
	: '[' exp_list ']'
Packit 575503
	  {
Packit 575503
		CMDARG *a;
Packit 575503
		NODE *subs;
Packit 575503
		int count = 0;
Packit 575503
Packit 575503
		for (a = $2; a != NULL; a = a->next)
Packit 575503
			count++;
Packit 575503
		subs = concat_args($2, count);
Packit 575503
		free_cmdarg($2->next);
Packit 575503
		$2->next = NULL;
Packit 575503
		$2->type = D_node;
Packit 575503
		$2->a_node = subs;
Packit 575503
		$$ = $2;
Packit 575503
	  }
Packit 575503
	| '[' exp_list error
Packit 575503
	;
Packit 575503
Packit 575503
subscript_list
Packit 575503
	: subscript
Packit 575503
	  { $$ = $1; num_dim = 1; }
Packit 575503
	| subscript_list subscript
Packit 575503
	  {	$$ = $1; num_dim++; }
Packit 575503
	;
Packit 575503
Packit 575503
variable
Packit 575503
	: D_VARIABLE
Packit 575503
	| '$' D_NODE
Packit 575503
	  {
Packit 575503
		NODE *n = $2->a_node;
Packit 575503
		if ((n->flags & NUMBER) == 0)
Packit 575503
			yyerror(_("non-numeric value for field number"));
Packit 575503
		else
Packit 575503
			$2->type = D_field;
Packit 575503
		$$ = $2;
Packit 575503
	  }
Packit 575503
	| D_VARIABLE subscript_list
Packit 575503
	  {
Packit 575503
		/* a_string is array name, a_count is dimension count */
Packit 575503
		$1->type = D_subscript;
Packit 575503
		$1->a_count = num_dim;
Packit 575503
		$$ = $1;
Packit 575503
	  }
Packit 575503
	;
Packit 575503
Packit 575503
node
Packit 575503
	: D_NODE
Packit 575503
	  { $$ = $1; }
Packit 575503
	| '+' D_NODE
Packit 575503
	  {
Packit 575503
		NODE *n = $2->a_node;
Packit 575503
		if ((n->flags & NUMBER) == 0)
Packit 575503
			yyerror(_("non-numeric value found, numeric expected"));
Packit 575503
		$$ = $2;
Packit 575503
	  }
Packit 575503
	| '-' D_NODE
Packit 575503
	  {
Packit 575503
		NODE *n = $2->a_node;
Packit 575503
		if ((n->flags & NUMBER) == 0)
Packit 575503
			yyerror(_("non-numeric value found, numeric expected"));
Packit 575503
		else
Packit 575503
			negate_num(n);
Packit 575503
		$$ = $2;
Packit 575503
	  }
Packit 575503
	;
Packit 575503
Packit 575503
opt_plus_integer
Packit 575503
	: /* empty */
Packit 575503
	  { $$ = NULL; }
Packit 575503
	| plus_integer
Packit 575503
	  { $$ = $1; }
Packit 575503
	;
Packit 575503
Packit 575503
opt_integer
Packit 575503
	: /* empty */
Packit 575503
	  { $$ = NULL; }
Packit 575503
	| integer
Packit 575503
	  { $$ = $1; }
Packit 575503
	;
Packit 575503
Packit 575503
plus_integer
Packit 575503
	: D_INT
Packit 575503
	  {
Packit 575503
		if ($1->a_int == 0)
Packit 575503
			yyerror(_("non-zero integer value"));
Packit 575503
		$$ = $1;
Packit 575503
	  }
Packit 575503
	| '+' D_INT
Packit 575503
	  {
Packit 575503
		if ($2->a_int == 0)
Packit 575503
			yyerror(_("non-zero integer value"));
Packit 575503
		$$ = $2;
Packit 575503
	  }
Packit 575503
	;
Packit 575503
Packit 575503
integer
Packit 575503
	: D_INT
Packit 575503
	  { $$ = $1; }
Packit 575503
	| '+' D_INT
Packit 575503
	  { $$ = $2; }
Packit 575503
	| '-' D_INT
Packit 575503
	  {
Packit 575503
		$2->a_int = - $2->a_int;
Packit 575503
		$$ = $2;
Packit 575503
	  }
Packit 575503
	;
Packit 575503
Packit 575503
nls
Packit 575503
	: '\n'
Packit 575503
	  {
Packit 575503
		if (lexptr_begin != NULL) {
Packit 575503
			if (input_from_tty && lexptr_begin[0] != '\0')
Packit 575503
				add_history(lexptr_begin);
Packit 575503
			efree(lexptr_begin);
Packit 575503
			lexptr_begin = NULL;
Packit 575503
		}
Packit 575503
	  }
Packit 575503
	;
Packit 575503
Packit 575503
%%
Packit 575503
Packit 575503
Packit 575503
/* append_statement --- append 'stmt' to the list of eval awk statements */
Packit 575503
Packit 575503
static CMDARG *
Packit 575503
append_statement(CMDARG *stmt_list, char *stmt)
Packit 575503
{
Packit 575503
	CMDARG *a, *arg;
Packit 575503
	char *s;
Packit 575503
	int len, slen, ssize;
Packit 575503
Packit 575503
#define EVALSIZE	512
Packit 575503
Packit 575503
	if (stmt == start_EVAL) {
Packit 575503
		len = sizeof(start_EVAL);
Packit 575503
		for (a = stmt_list; a != NULL; a = a->next)
Packit 575503
			len += strlen(a->a_string) + 1;	/* 1 for ',' */
Packit 575503
		len += EVALSIZE;
Packit 575503
Packit 575503
		emalloc(s, char *, (len + 1) * sizeof(char), "append_statement");
Packit 575503
		arg = mk_cmdarg(D_string);
Packit 575503
		arg->a_string = s;
Packit 575503
		arg->a_count = len;	/* kludge */
Packit 575503
Packit 575503
		slen = sizeof("function @eval(") - 1;
Packit 575503
		memcpy(s, start_EVAL, slen);
Packit 575503
Packit 575503
		for (a = stmt_list; a != NULL; a = a->next) {
Packit 575503
			len = strlen(a->a_string);
Packit 575503
			memcpy(s + slen, a->a_string, len);
Packit 575503
			slen += len;
Packit 575503
			if (a->next != NULL)
Packit 575503
				s[slen++] = ',';
Packit 575503
		}
Packit 575503
		s[slen++] = ')';
Packit 575503
		s[slen++] = '{';
Packit 575503
		s[slen] = '\0';
Packit 575503
		return arg;
Packit 575503
	}
Packit 575503
Packit 575503
	len = strlen(stmt) + 1;	/* 1 for newline */
Packit 575503
	s = stmt_list->a_string;
Packit 575503
	slen = strlen(s);
Packit 575503
	ssize = stmt_list->a_count;
Packit 575503
	if (len > ssize - slen) {
Packit 575503
		ssize = slen + len + EVALSIZE;
Packit 575503
		erealloc(s, char *, (ssize + 1) * sizeof(char), "append_statement");
Packit 575503
		stmt_list->a_string = s;
Packit 575503
		stmt_list->a_count = ssize;
Packit 575503
	}
Packit 575503
	memcpy(s + slen, stmt, len);
Packit 575503
	slen += len;
Packit 575503
	if (slen >= 2 && s[slen - 2] != '\n') {
Packit 575503
		s[slen - 1] = '\n';
Packit 575503
		s[slen] = '\0';
Packit 575503
	}
Packit 575503
Packit 575503
	if (stmt == end_EVAL)
Packit 575503
		erealloc(stmt_list->a_string, char *, slen + 1, "append_statement");
Packit 575503
	return stmt_list;
Packit 575503
Packit 575503
#undef EVALSIZE
Packit 575503
}
Packit 575503
Packit 575503
Packit 575503
/* command names sorted in ascending order */
Packit 575503
Packit 575503
struct cmdtoken cmdtab[] = {
Packit 575503
{ "backtrace", "bt", D_backtrace, D_BACKTRACE, do_backtrace,
Packit 575503
	gettext_noop("backtrace [N] - print trace of all or N innermost (outermost if N < 0) frames.") },
Packit 575503
{ "break", "b", D_break, D_BREAK, do_breakpoint,
Packit 575503
	gettext_noop("break [[filename:]N|function] - set breakpoint at the specified location.") },
Packit 575503
{ "clear", "", D_clear, D_CLEAR, do_clear,
Packit 575503
	gettext_noop("clear [[filename:]N|function] - delete breakpoints previously set.") },
Packit 575503
{ "commands", "", D_commands, D_COMMANDS, do_commands,
Packit 575503
	gettext_noop("commands [num] - starts a list of commands to be executed at a breakpoint(watchpoint) hit.") },
Packit 575503
{ "condition", "", D_condition, D_CONDITION, do_condition,
Packit 575503
	gettext_noop("condition num [expr] - set or clear breakpoint or watchpoint condition.") },
Packit 575503
{ "continue", "c", D_continue, D_CONTINUE, do_continue,
Packit 575503
	gettext_noop("continue [COUNT] - continue program being debugged.") },
Packit 575503
{ "delete", "d", D_delete, D_DELETE, do_delete_breakpoint,
Packit 575503
	gettext_noop("delete [breakpoints] [range] - delete specified breakpoints.") },
Packit 575503
{ "disable", "", D_disable, D_DISABLE, do_disable_breakpoint,
Packit 575503
	gettext_noop("disable [breakpoints] [range] - disable specified breakpoints.") },
Packit 575503
{ "display", "", D_display, D_DISPLAY, do_display,
Packit 575503
	gettext_noop("display [var] - print value of variable each time the program stops.") },
Packit 575503
{ "down", "", D_down, D_DOWN, do_down,
Packit 575503
	gettext_noop("down [N] - move N frames down the stack.") },
Packit 575503
{ "dump", "", D_dump, D_DUMP, do_dump_instructions,
Packit 575503
	gettext_noop("dump [filename] - dump instructions to file or stdout.") },
Packit 575503
{ "enable", "e", D_enable, D_ENABLE, do_enable_breakpoint,
Packit 575503
	gettext_noop("enable [once|del] [breakpoints] [range] - enable specified breakpoints.") },
Packit 575503
{ "end", "", D_end, D_END, do_commands,
Packit 575503
	gettext_noop("end - end a list of commands or awk statements.") },
Packit 575503
{ "eval", "", D_eval, D_EVAL, do_eval,
Packit 575503
	gettext_noop("eval stmt|[p1, p2, ...] - evaluate awk statement(s).") },
Packit 575503
{ "exit", "", D_quit, D_QUIT, do_quit,
Packit 575503
	gettext_noop("exit - (same as quit) exit debugger.") },
Packit 575503
{ "finish", "", D_finish, D_FINISH, do_finish,
Packit 575503
	gettext_noop("finish - execute until selected stack frame returns.") },
Packit 575503
{ "frame", "f", D_frame, D_FRAME, do_frame,
Packit 575503
	gettext_noop("frame [N] - select and print stack frame number N.") },
Packit 575503
{ "help", "h", D_help, D_HELP, do_help,
Packit 575503
	gettext_noop("help [command] - print list of commands or explanation of command.") },
Packit 575503
{ "ignore", "", D_ignore, D_IGNORE, do_ignore_breakpoint,
Packit 575503
	gettext_noop("ignore N COUNT - set ignore-count of breakpoint number N to COUNT.") },
Packit 575503
{ "info", "i", D_info, D_INFO, do_info,
Packit 575503
	gettext_noop("info topic - source|sources|variables|functions|break|frame|args|locals|display|watch.") },
Packit 575503
{ "list", "l", D_list, D_LIST, do_list,
Packit 575503
	gettext_noop("list [-|+|[filename:]lineno|function|range] - list specified line(s).") },
Packit 575503
{ "next", "n", D_next, D_NEXT, do_next,
Packit 575503
	gettext_noop("next [COUNT] - step program, proceeding through subroutine calls.") },
Packit 575503
{ "nexti", "ni", D_nexti, D_NEXTI, do_nexti,
Packit 575503
	gettext_noop("nexti [COUNT] - step one instruction, but proceed through subroutine calls.") },
Packit 575503
{ "option", "o", D_option, D_OPTION, do_option,
Packit 575503
	gettext_noop("option [name[=value]] - set or display debugger option(s).") },
Packit 575503
{ "print", "p", D_print, D_PRINT, do_print_var,
Packit 575503
	gettext_noop("print var [var] - print value of a variable or array.") },
Packit 575503
{ "printf", "", D_printf, D_PRINTF, do_print_f,
Packit 575503
	gettext_noop("printf format, [arg], ... - formatted output.") },
Packit 575503
{ "quit", "q", D_quit, D_QUIT, do_quit,
Packit 575503
	gettext_noop("quit - exit debugger.") },
Packit 575503
{ "return", "", D_return, D_RETURN, do_return,
Packit 575503
	gettext_noop("return [value] - make selected stack frame return to its caller.") },
Packit 575503
{ "run", "r", D_run, D_RUN, do_run,
Packit 575503
	gettext_noop("run - start or restart executing program.") },
Packit 575503
#ifdef HAVE_LIBREADLINE
Packit 575503
{ "save", "", D_save, D_SAVE, do_save,
Packit 575503
	gettext_noop("save filename - save commands from the session to file.") },
Packit 575503
#endif
Packit 575503
{ "set", "", D_set, D_SET, do_set_var,
Packit 575503
	gettext_noop("set var = value - assign value to a scalar variable.") },
Packit 575503
{ "silent", "", D_silent, D_SILENT, do_commands,
Packit 575503
	gettext_noop("silent - suspends usual message when stopped at a breakpoint/watchpoint.") },
Packit 575503
{ "source", "", D_source, D_SOURCE, do_source,
Packit 575503
	gettext_noop("source file - execute commands from file.") },
Packit 575503
{ "step", "s", D_step, D_STEP, do_step,
Packit 575503
	gettext_noop("step [COUNT] - step program until it reaches a different source line.") },
Packit 575503
{ "stepi", "si", D_stepi, D_STEPI, do_stepi,
Packit 575503
	gettext_noop("stepi [COUNT] - step one instruction exactly.") },
Packit 575503
{ "tbreak", "t", D_tbreak, D_TBREAK, do_tmp_breakpoint,
Packit 575503
	gettext_noop("tbreak [[filename:]N|function] - set a temporary breakpoint.") },
Packit 575503
{ "trace", "", D_trace, D_TRACE, do_trace_instruction,
Packit 575503
	gettext_noop("trace on|off - print instruction before executing.") },
Packit 575503
{ "undisplay",	"", D_undisplay, D_UNDISPLAY, do_undisplay,
Packit 575503
	gettext_noop("undisplay [N] - remove variable(s) from automatic display list.") },
Packit 575503
{ "until", "u", D_until, D_UNTIL, do_until,
Packit 575503
	gettext_noop("until [[filename:]N|function] - execute until program reaches a different line or line N within current frame.") },
Packit 575503
{ "unwatch", "", D_unwatch, D_UNWATCH, do_unwatch,
Packit 575503
	gettext_noop("unwatch [N] - remove variable(s) from watch list.") },
Packit 575503
{ "up",	"", D_up, D_UP, do_up,
Packit 575503
	gettext_noop("up [N] - move N frames up the stack.") },
Packit 575503
{ "watch", "w", D_watch, D_WATCH, do_watch,
Packit 575503
	gettext_noop("watch var - set a watchpoint for a variable.") },
Packit 575503
{ "where", "", D_backtrace, D_BACKTRACE, do_backtrace,
Packit 575503
	gettext_noop("where [N] - (same as backtrace) print trace of all or N innermost (outermost if N < 0) frames.") },
Packit 575503
{ NULL, NULL, D_illegal, 0, (Func_cmd) 0,
Packit 575503
	 NULL },
Packit 575503
};
Packit 575503
Packit 575503
struct argtoken argtab[] = {
Packit 575503
	{ "args", D_info, A_ARGS },
Packit 575503
	{ "break", D_info, A_BREAK },
Packit 575503
	{ "del", D_enable, A_DEL },
Packit 575503
	{ "display", D_info, A_DISPLAY },
Packit 575503
	{ "frame", D_info, A_FRAME },
Packit 575503
	{ "functions", D_info, A_FUNCTIONS },
Packit 575503
	{ "locals", D_info, A_LOCALS },
Packit 575503
	{ "off", D_trace, A_TRACE_OFF },
Packit 575503
	{ "on", D_trace, A_TRACE_ON },
Packit 575503
	{ "once", D_enable, A_ONCE },
Packit 575503
	{ "source", D_info, A_SOURCE },
Packit 575503
	{ "sources", D_info, A_SOURCES },
Packit 575503
	{ "variables", D_info, A_VARIABLES },
Packit 575503
	{ "watch", D_info, A_WATCH },
Packit 575503
	{ NULL, D_illegal, A_NONE },
Packit 575503
};
Packit 575503
Packit 575503
Packit 575503
/* get_command --- return command handler function */
Packit 575503
Packit 575503
Func_cmd
Packit 575503
get_command(int ctype)
Packit 575503
{
Packit 575503
	int i;
Packit 575503
	for (i = 0; cmdtab[i].name != NULL; i++) {
Packit 575503
		if (cmdtab[i].type == ctype)
Packit 575503
			return cmdtab[i].cf_ptr;
Packit 575503
	}
Packit 575503
	return (Func_cmd) 0;
Packit 575503
}
Packit 575503
Packit 575503
/* get_command_name --- return command name given it's type */
Packit 575503
Packit 575503
const char *
Packit 575503
get_command_name(int ctype)
Packit 575503
{
Packit 575503
	int i;
Packit 575503
	for (i = 0; cmdtab[i].name != NULL; i++) {
Packit 575503
		if (cmdtab[i].type == ctype)
Packit 575503
			return cmdtab[i].name;
Packit 575503
	}
Packit 575503
	return NULL;
Packit 575503
}
Packit 575503
Packit 575503
/* mk_cmdarg --- make an argument for command */
Packit 575503
Packit 575503
static CMDARG *
Packit 575503
mk_cmdarg(enum argtype type)
Packit 575503
{
Packit 575503
	CMDARG *arg;
Packit 575503
	ezalloc(arg, CMDARG *, sizeof(CMDARG), "mk_cmdarg");
Packit 575503
	arg->type = type;
Packit 575503
	return arg;
Packit 575503
}
Packit 575503
Packit 575503
/* append_cmdarg --- append ARG to the list of arguments for the current command */
Packit 575503
Packit 575503
static void
Packit 575503
append_cmdarg(CMDARG *arg)
Packit 575503
{
Packit 575503
	static CMDARG *savetail;
Packit 575503
Packit 575503
	if (arg_list == NULL)
Packit 575503
		arg_list = arg;
Packit 575503
	else
Packit 575503
		savetail->next = arg;
Packit 575503
	savetail = arg;
Packit 575503
}
Packit 575503
Packit 575503
/* free_cmdarg --- free all arguments in LIST */
Packit 575503
Packit 575503
void
Packit 575503
free_cmdarg(CMDARG *list)
Packit 575503
{
Packit 575503
	CMDARG *arg, *nexta;
Packit 575503
Packit 575503
	for (arg = list; arg != NULL; arg = nexta) {
Packit 575503
		nexta = arg->next;
Packit 575503
Packit 575503
		switch (arg->type) {
Packit 575503
		case D_variable:
Packit 575503
		case D_subscript:
Packit 575503
		case D_array:
Packit 575503
		case D_string:
Packit 575503
			if (arg->a_string != NULL)
Packit 575503
				efree(arg->a_string);
Packit 575503
			break;
Packit 575503
		case D_node:
Packit 575503
		case D_field:
Packit 575503
			unref(arg->a_node);
Packit 575503
			break;
Packit 575503
		default:
Packit 575503
			break;
Packit 575503
		}
Packit 575503
		efree(arg);
Packit 575503
	}
Packit 575503
}
Packit 575503
Packit 575503
/* yyerror --- print a syntax error message */
Packit 575503
Packit 575503
static void
Packit 575503
yyerror(const char *mesg, ...)
Packit 575503
{
Packit 575503
	va_list args;
Packit 575503
	va_start(args, mesg);
Packit 575503
	fprintf(out_fp, _("error: "));
Packit 575503
	vfprintf(out_fp, mesg, args);
Packit 575503
	fprintf(out_fp, "\n");
Packit 575503
	va_end(args);
Packit 575503
	errcount++;
Packit 575503
	repeat_idx = -1;
Packit 575503
}
Packit 575503
Packit 575503
Packit 575503
/* yylex --- read a command and turn it into tokens */
Packit 575503
Packit 575503
static int
Packit 575503
#ifdef USE_EBCDIC
Packit 575503
yylex_ebcdic(void)
Packit 575503
#else
Packit 575503
yylex(void)
Packit 575503
#endif
Packit 575503
{
Packit 575503
	static char *lexptr = NULL;
Packit 575503
	static char *lexend;
Packit 575503
	int c;
Packit 575503
	char *tokstart;
Packit 575503
	size_t toklen;
Packit 575503
Packit 575503
	yylval = (CMDARG *) NULL;
Packit 575503
Packit 575503
	if (errcount > 0 && lexptr_begin == NULL) {
Packit 575503
		/* fake a new line */
Packit 575503
		errcount = 0;
Packit 575503
		return '\n';
Packit 575503
	}
Packit 575503
Packit 575503
	if (lexptr_begin == NULL) {
Packit 575503
again:
Packit 575503
		lexptr_begin = read_a_line(dbg_prompt);
Packit 575503
		if (lexptr_begin == NULL) {	/* EOF or error */
Packit 575503
			if (get_eof_status() == EXIT_FATAL)
Packit 575503
				exit(EXIT_FATAL);
Packit 575503
			if (get_eof_status() == EXIT_FAILURE) {
Packit 575503
				static int seen_eof = 0;
Packit 575503
Packit 575503
				/* force a quit, and let do_quit (in debug.c) exit */
Packit 575503
				if (! seen_eof) {
Packit 575503
					if (errno != 0)	{
Packit 575503
						fprintf(stderr, _("can't read command (%s)\n"), strerror(errno));
Packit 575503
						exit_val = EXIT_FAILURE;
Packit 575503
					} /* else
Packit 575503
						exit_val = EXIT_SUCCESS; */
Packit 575503
Packit 575503
					seen_eof = 1;
Packit 575503
					return '\n';	/* end current command if any */
Packit 575503
				} else if (seen_eof++ == 1) {
Packit 575503
					cmd_idx = find_command("quit", 4);
Packit 575503
					return D_QUIT;	/* 'quit' token */
Packit 575503
				} else
Packit 575503
					return '\n';	/* end command 'quit' */
Packit 575503
			}
Packit 575503
			if (errno != 0)
Packit 575503
				d_error(_("can't read command (%s)"), strerror(errno));
Packit 575503
			if (pop_cmd_src() == 0)
Packit 575503
				goto again;
Packit 575503
			exit(EXIT_FATAL);	/* shouldn't happen */
Packit 575503
		}
Packit 575503
Packit 575503
		if (! in_commands && ! in_eval	/* history expansion off in 'commands' and 'eval' */
Packit 575503
				&& input_from_tty
Packit 575503
		)
Packit 575503
			history_expand_line(&lexptr_begin);
Packit 575503
Packit 575503
		lexptr = lexptr_begin;
Packit 575503
		lexend = lexptr + strlen(lexptr);
Packit 575503
		if (*lexptr == '\0'		/* blank line */
Packit 575503
				&& repeat_idx >= 0
Packit 575503
				&& input_from_tty
Packit 575503
				&& ! in_eval
Packit 575503
		) {
Packit 575503
#ifdef HAVE_LIBREADLINE
Packit 575503
			HIST_ENTRY *h;
Packit 575503
			h = previous_history();
Packit 575503
			if (h != NULL)
Packit 575503
				add_history(h->line);
Packit 575503
#endif
Packit 575503
			cmd_idx = repeat_idx;
Packit 575503
			return cmdtab[cmd_idx].class;	/* repeat last command */
Packit 575503
		}
Packit 575503
		repeat_idx = -1;
Packit 575503
	}
Packit 575503
Packit 575503
	c = *lexptr;
Packit 575503
Packit 575503
	while (c == ' ' || c == '\t')
Packit 575503
		c = *++lexptr;
Packit 575503
Packit 575503
	if (! input_from_tty && c == '#')
Packit 575503
		return '\n';
Packit 575503
Packit 575503
	tokstart = lexptr;
Packit 575503
	if (lexptr >= lexend)
Packit 575503
		return '\n';
Packit 575503
Packit 575503
	if (cmd_idx < 0) {	/* need a command */
Packit 575503
		if (c == '?' && tokstart[1] == '\0'	&& ! in_eval) {
Packit 575503
			lexptr++;
Packit 575503
			cmd_idx = find_command("help", 4);
Packit 575503
			return D_HELP;
Packit 575503
		}
Packit 575503
Packit 575503
		while (c != '\0' && c != ' ' && c != '\t') {
Packit 575503
			if (! is_alpha(c) && ! in_eval) {
Packit 575503
				yyerror(_("invalid character in command"));
Packit 575503
				return '\n';
Packit 575503
			}
Packit 575503
			c = *++lexptr;
Packit 575503
		}
Packit 575503
Packit 575503
		toklen = lexptr - tokstart;
Packit 575503
Packit 575503
		if (in_eval) {
Packit 575503
			if (toklen == 3
Packit 575503
					&& tokstart[3] == '\0'
Packit 575503
					&& tokstart[0] == 'e'
Packit 575503
					&& tokstart[1] == 'n'
Packit 575503
					&& tokstart[2] == 'd'
Packit 575503
			) {
Packit 575503
				cmd_idx = find_command(tokstart, toklen);
Packit 575503
				return D_END;
Packit 575503
			}
Packit 575503
			lexptr = lexend;
Packit 575503
			return D_STATEMENT;
Packit 575503
		}
Packit 575503
Packit 575503
		cmd_idx = find_command(tokstart, toklen);
Packit 575503
		if (cmd_idx >= 0) {
Packit 575503
			if (in_commands && cmdtab[cmd_idx].type != D_eval) {
Packit 575503
				/* add the actual command string (lexptr_begin) to
Packit 575503
				 * arg_list; command string for 'eval' prepended to the arg_list
Packit 575503
				 * in the grammer above (see eval_cmd non-terminal).
Packit 575503
				 */
Packit 575503
				CMDARG *arg;
Packit 575503
				arg = mk_cmdarg(D_string);
Packit 575503
				arg->a_string = estrdup(lexptr_begin, lexend - lexptr_begin);
Packit 575503
				append_cmdarg(arg);
Packit 575503
			}
Packit 575503
			return cmdtab[cmd_idx].class;
Packit 575503
		} else {
Packit 575503
			yyerror(_("unknown command - \"%.*s\", try help"), toklen, tokstart);
Packit 575503
			return '\n';
Packit 575503
		}
Packit 575503
	}
Packit 575503
Packit 575503
	c = *lexptr;
Packit 575503
Packit 575503
	if (cmdtab[cmd_idx].type == D_option) {
Packit 575503
		if (c == '=')
Packit 575503
			return *lexptr++;
Packit 575503
	} else if (c == '-' || c == '+' || c == ':' || c == '|')
Packit 575503
		return *lexptr++;
Packit 575503
Packit 575503
	if (c == '"') {
Packit 575503
		char *str, *p;
Packit 575503
		int flags = ALREADY_MALLOCED;
Packit 575503
		bool esc_seen = false;
Packit 575503
Packit 575503
		toklen = lexend - lexptr;
Packit 575503
		emalloc(str, char *, toklen + 1, "yylex");
Packit 575503
		p = str;
Packit 575503
Packit 575503
		while ((c = *++lexptr) != '"') {
Packit 575503
			if (lexptr == lexend) {
Packit 575503
err:
Packit 575503
				efree(str);
Packit 575503
				yyerror(_("unterminated string"));
Packit 575503
				return '\n';
Packit 575503
			}
Packit 575503
			if (c == '\\') {
Packit 575503
				c = *++lexptr;
Packit 575503
				esc_seen = true;
Packit 575503
				if (want_nodeval || c != '"')
Packit 575503
					*p++ = '\\';
Packit 575503
			}
Packit 575503
			if (lexptr == lexend)
Packit 575503
				goto err;
Packit 575503
			*p++ = c;
Packit 575503
		}
Packit 575503
		lexptr++;
Packit 575503
		*p = '\0';
Packit 575503
Packit 575503
		if (! want_nodeval) {
Packit 575503
			yylval = mk_cmdarg(D_string);
Packit 575503
			yylval->a_string = str;
Packit 575503
			append_cmdarg(yylval);
Packit 575503
			return D_STRING;
Packit 575503
		} else {	/* awk string */
Packit 575503
			if (esc_seen)
Packit 575503
				flags |= SCAN;
Packit 575503
			yylval = mk_cmdarg(D_node);
Packit 575503
			yylval->a_node = make_str_node(str, p - str, flags);
Packit 575503
			append_cmdarg(yylval);
Packit 575503
			return D_NODE;
Packit 575503
		}
Packit 575503
	}
Packit 575503
Packit 575503
	if (! want_nodeval) {
Packit 575503
		while ((c = *++lexptr) != '\0' && c != ':' && c != '-'
Packit 575503
					&& c != ' ' && c != '\t' && c != '=')
Packit 575503
			;
Packit 575503
Packit 575503
		/* Is it an integer? */
Packit 575503
		if (isdigit((unsigned char) tokstart[0]) && cmdtab[cmd_idx].type != D_option) {
Packit 575503
			char *end;
Packit 575503
			long l;
Packit 575503
Packit 575503
			errno = 0;
Packit 575503
			l = strtol(tokstart, &end, 0);
Packit 575503
			if (errno != 0) {
Packit 575503
				yyerror(_("%s"), strerror(errno));
Packit 575503
				errno = 0;
Packit 575503
				return '\n';
Packit 575503
			}
Packit 575503
Packit 575503
			if (lexptr == end) {
Packit 575503
				yylval = mk_cmdarg(D_int);
Packit 575503
				yylval->a_int = l;
Packit 575503
				append_cmdarg(yylval);
Packit 575503
				return D_INT;
Packit 575503
			}
Packit 575503
		}
Packit 575503
Packit 575503
		/* Must be string */
Packit 575503
		yylval = mk_cmdarg(D_string);
Packit 575503
		yylval->a_string = estrdup(tokstart, lexptr - tokstart);
Packit 575503
		append_cmdarg(yylval);
Packit 575503
		return D_STRING;
Packit 575503
	}
Packit 575503
Packit 575503
	/* look for awk number */
Packit 575503
Packit 575503
	if (isdigit((unsigned char) tokstart[0])) {
Packit 575503
		NODE *r = NULL;
Packit 575503
Packit 575503
		errno = 0;
Packit 575503
#ifdef HAVE_MPFR
Packit 575503
		if (do_mpfr) {
Packit 575503
			int tval;
Packit 575503
			r = mpg_float();
Packit 575503
			tval = mpfr_strtofr(r->mpg_numbr, tokstart, & lexptr, 0, ROUND_MODE);
Packit 575503
			IEEE_FMT(r->mpg_numbr, tval);
Packit 575503
			if (mpfr_integer_p(r->mpg_numbr)) {
Packit 575503
				/* integral value, convert to a GMP type. */
Packit 575503
				NODE *tmp = r;
Packit 575503
				r = mpg_integer();
Packit 575503
				mpfr_get_z(r->mpg_i, tmp->mpg_numbr, MPFR_RNDZ);
Packit 575503
				unref(tmp);
Packit 575503
			}
Packit 575503
		} else
Packit 575503
#endif
Packit 575503
			r = make_number(strtod(tokstart, & lexptr));
Packit 575503
Packit 575503
		if (errno != 0) {
Packit 575503
			yyerror(strerror(errno));
Packit 575503
			unref(r);
Packit 575503
			errno = 0;
Packit 575503
			return '\n';
Packit 575503
		}
Packit 575503
		yylval = mk_cmdarg(D_node);
Packit 575503
		yylval->a_node = r;
Packit 575503
		append_cmdarg(yylval);
Packit 575503
		return D_NODE;
Packit 575503
	}
Packit 575503
Packit 575503
	c = *lexptr;
Packit 575503
	if (c == '$' || c == '@'
Packit 575503
			|| c == '[' || c == ']'
Packit 575503
			|| c == ',' || c == '=')
Packit 575503
		return *lexptr++;
Packit 575503
Packit 575503
	if (! is_letter(c)) {
Packit 575503
		yyerror(_("invalid character"));
Packit 575503
		return '\n';
Packit 575503
	}
Packit 575503
Packit 575503
	while (is_identchar(c))
Packit 575503
		c = *++lexptr;
Packit 575503
	toklen = lexptr - tokstart;
Packit 575503
Packit 575503
	/* awk variable */
Packit 575503
	yylval = mk_cmdarg(D_variable);
Packit 575503
	yylval->a_string = estrdup(tokstart, toklen);
Packit 575503
	append_cmdarg(yylval);
Packit 575503
	return D_VARIABLE;
Packit 575503
}
Packit 575503
Packit 575503
/* Convert single-character tokens coming out of yylex() from EBCDIC to
Packit 575503
   ASCII values on-the-fly so that the parse tables need not be regenerated
Packit 575503
   for EBCDIC systems.  */
Packit 575503
#ifdef USE_EBCDIC
Packit 575503
static int
Packit 575503
yylex(void)
Packit 575503
{
Packit 575503
	static char etoa_xlate[256];
Packit 575503
	static int do_etoa_init = 1;
Packit 575503
	int tok;
Packit 575503
Packit 575503
	if (do_etoa_init)
Packit 575503
	{
Packit 575503
		for (tok = 0; tok < 256; tok++)
Packit 575503
			etoa_xlate[tok] = (char) tok;
Packit 575503
#ifdef HAVE___ETOA_L
Packit 575503
		/* IBM helpfully provides this function.  */
Packit 575503
		__etoa_l(etoa_xlate, sizeof(etoa_xlate));
Packit 575503
#else
Packit 575503
# error "An EBCDIC-to-ASCII translation function is needed for this system"
Packit 575503
#endif
Packit 575503
		do_etoa_init = 0;
Packit 575503
	}
Packit 575503
Packit 575503
	tok = yylex_ebcdic();
Packit 575503
Packit 575503
	if (tok >= 0 && tok <= 0xFF)
Packit 575503
		tok = etoa_xlate[tok];
Packit 575503
Packit 575503
	return tok;
Packit 575503
}
Packit 575503
#endif /* USE_EBCDIC */
Packit 575503
Packit 575503
/* find_argument --- find index in 'argtab' for a command option */
Packit 575503
Packit 575503
static int
Packit 575503
find_argument(CMDARG *arg)
Packit 575503
{
Packit 575503
	/* non-number argument */
Packit 575503
	int idx;
Packit 575503
	char *name, *p;
Packit 575503
	size_t len;
Packit 575503
	assert(cmd_idx >= 0);
Packit 575503
	name = arg->a_string;
Packit 575503
	len = strlen(name);
Packit 575503
	for (idx = 0; (p = (char *) argtab[idx].name) != NULL; idx++) {
Packit 575503
		if (cmdtab[cmd_idx].type == argtab[idx].cmd
Packit 575503
				&& *p == *name
Packit 575503
				&& strlen(p) == len
Packit 575503
				&& strncmp(p, name, len) == 0
Packit 575503
		)
Packit 575503
			return idx;
Packit 575503
	}
Packit 575503
	return -1;	/* invalid option */
Packit 575503
}
Packit 575503
Packit 575503
/* concat_args --- concatenate argument strings into a single string NODE */
Packit 575503
Packit 575503
static NODE *
Packit 575503
concat_args(CMDARG *arg, int count)
Packit 575503
{
Packit 575503
	NODE *n;
Packit 575503
	NODE **tmp;
Packit 575503
	char *str, *subsep, *p;
Packit 575503
	long len, subseplen;
Packit 575503
	int i;
Packit 575503
Packit 575503
	if (count == 1) {
Packit 575503
		n = force_string(arg->a_node);
Packit 575503
		return dupnode(n);
Packit 575503
	}
Packit 575503
Packit 575503
	emalloc(tmp, NODE **, count * sizeof(NODE *), "concat_args");
Packit 575503
	subseplen = SUBSEP_node->var_value->stlen;
Packit 575503
	subsep = SUBSEP_node->var_value->stptr;
Packit 575503
	len = -subseplen;
Packit 575503
Packit 575503
	for (i = 0; i < count; i++) {
Packit 575503
		n = force_string(arg->a_node);
Packit 575503
		len += n->stlen + subseplen;
Packit 575503
		tmp[i] = n;
Packit 575503
		arg = arg->next;
Packit 575503
	}
Packit 575503
Packit 575503
	emalloc(str, char *, len + 1, "concat_args");
Packit 575503
	n = tmp[0];
Packit 575503
	memcpy(str, n->stptr, n->stlen);
Packit 575503
	p = str + n->stlen;
Packit 575503
	for (i = 1; i < count; i++) {
Packit 575503
		if (subseplen == 1)
Packit 575503
			*p++ = *subsep;
Packit 575503
		else if (subseplen > 0) {
Packit 575503
			memcpy(p, subsep, subseplen);
Packit 575503
			p += subseplen;
Packit 575503
		}
Packit 575503
Packit 575503
		n = tmp[i];
Packit 575503
		memcpy(p, n->stptr, n->stlen);
Packit 575503
		p += n->stlen;
Packit 575503
	}
Packit 575503
	str[len] = '\0';
Packit 575503
	efree(tmp);
Packit 575503
	return make_str_node(str, len, ALREADY_MALLOCED);
Packit 575503
}
Packit 575503
Packit 575503
/* find_command --- find the index in 'cmdtab' using exact,
Packit 575503
 *                  abbreviation or unique partial match
Packit 575503
 */
Packit 575503
Packit 575503
static int
Packit 575503
find_command(const char *token, size_t toklen)
Packit 575503
{
Packit 575503
	char *name, *abrv;
Packit 575503
	int i, k;
Packit 575503
	bool try_exact = true;
Packit 575503
	int abrv_match = -1;
Packit 575503
	int partial_match = -1;
Packit 575503
Packit 575503
#if 'a' == 0x81 /* it's EBCDIC */
Packit 575503
	/* make sure all lower case characters in token (sorting
Packit 575503
	 * isn't the solution in this case)
Packit 575503
	 */
Packit 575503
	for (i = 0; i < toklen; i++) {
Packit 575503
		if (token[i] != tolower(token[i]))
Packit 575503
			return -1;
Packit 575503
	}
Packit 575503
#endif
Packit 575503
Packit 575503
	k = sizeof(cmdtab)/sizeof(cmdtab[0]) - 1;
Packit 575503
	for (i = 0; i < k; i++) {
Packit 575503
		name = (char *) cmdtab[i].name;
Packit 575503
		if (try_exact && *token == *name
Packit 575503
				&& toklen == strlen(name)
Packit 575503
				&& strncmp(name, token, toklen) == 0
Packit 575503
		)
Packit 575503
			return i;
Packit 575503
Packit 575503
		if (*name > *token || i == (k - 1))
Packit 575503
			try_exact = false;
Packit 575503
Packit 575503
		if (abrv_match < 0) {
Packit 575503
			abrv = cmdtab[i].abbrvn;
Packit 575503
			if (abrv[0] == token[0]) {
Packit 575503
				if (toklen == 1 && ! abrv[1])
Packit 575503
					abrv_match = i;
Packit 575503
				else if (toklen == 2 && abrv[1] == token[1])
Packit 575503
					abrv_match = i;
Packit 575503
			}
Packit 575503
		}
Packit 575503
		if (! try_exact && abrv_match >= 0)
Packit 575503
			return abrv_match;
Packit 575503
		if (partial_match < 0) {
Packit 575503
			if (*token == *name
Packit 575503
					&& toklen < strlen(name)
Packit 575503
					&& strncmp(name, token, toklen) == 0
Packit 575503
			) {
Packit 575503
				if ((i == k - 1 || strncmp(cmdtab[i + 1].name, token, toklen) != 0)
Packit 575503
					&& (i == 0 || strncmp(cmdtab[i - 1].name, token, toklen) != 0)
Packit 575503
				)
Packit 575503
					partial_match = i;
Packit 575503
			}
Packit 575503
		}
Packit 575503
	}
Packit 575503
	return partial_match;
Packit 575503
}
Packit 575503
Packit 575503
/* do_help -- help command */
Packit 575503
Packit 575503
int
Packit 575503
do_help(CMDARG *arg, int cmd)
Packit 575503
{
Packit 575503
	int i;
Packit 575503
	if (arg == NULL) {
Packit 575503
		initialize_pager(out_fp);
Packit 575503
		if (setjmp(pager_quit_tag) == 0) {
Packit 575503
			for (i = 0; cmdtab[i].name != NULL; i++) {
Packit 575503
				gprintf(out_fp, "%s:\n", cmdtab[i].name);
Packit 575503
				gprintf(out_fp, "\t%s\n", _(cmdtab[i].help_txt));
Packit 575503
			}
Packit 575503
		}
Packit 575503
	} else if (arg->type == D_string) {
Packit 575503
		char *name;
Packit 575503
		name = arg->a_string;
Packit 575503
		i = find_command(name, strlen(name));
Packit 575503
		if (i >= 0) {
Packit 575503
			fprintf(out_fp, "%s\n", cmdtab[i].help_txt);
Packit 575503
			if (strcmp(cmdtab[i].name, "option") == 0)
Packit 575503
				option_help();
Packit 575503
		} else
Packit 575503
			fprintf(out_fp, _("undefined command: %s\n"), name);
Packit 575503
	}
Packit 575503
Packit 575503
	return false;
Packit 575503
}
Packit 575503
Packit 575503
Packit 575503
#ifdef HAVE_LIBREADLINE
Packit 575503
Packit 575503
/* next_word --- find the next word in a line to complete
Packit 575503
 *               (word seperation characters are space and tab).
Packit 575503
 */
Packit 575503
Packit 575503
static char *
Packit 575503
next_word(char *p, int len, char **endp)
Packit 575503
{
Packit 575503
	char *q;
Packit 575503
	int i;
Packit 575503
Packit 575503
	if (p == NULL || len <= 0)
Packit 575503
		return NULL;
Packit 575503
	for (i = 0; i < len; i++, p++)
Packit 575503
		if (*p != ' ' && *p != '\t')
Packit 575503
			break;
Packit 575503
	if (i == len)
Packit 575503
		return NULL;
Packit 575503
	if (endp != NULL) {
Packit 575503
		for (i++, q = p + 1; i < len; i++, q++)
Packit 575503
			if (*q == ' ' || *q == '\t')
Packit 575503
				break;
Packit 575503
		*endp = q;
Packit 575503
	}
Packit 575503
	return p;
Packit 575503
}
Packit 575503
Packit 575503
/* command_completion --- attempt to complete based on the word number in line;
Packit 575503
 *    try to complete on command names if this is the first word; for the next
Packit 575503
 *    word(s), the type of completion depends on the command name (first word).
Packit 575503
 */
Packit 575503
Packit 575503
#ifndef RL_READLINE_VERSION		/* < 4.2a */
Packit 575503
#define rl_completion_matches(x, y) completion_matches((char *) (x), (y))
Packit 575503
#endif
Packit 575503
Packit 575503
Packit 575503
char **
Packit 575503
command_completion(const char *text, int start, int end)
Packit 575503
{
Packit 575503
	char *cmdtok, *e;
Packit 575503
	int idx;
Packit 575503
	int len;
Packit 575503
Packit 575503
	rl_attempted_completion_over = true;	/* no default filename completion please */
Packit 575503
Packit 575503
	this_cmd = D_illegal;
Packit 575503
	len = start;
Packit 575503
	if ((cmdtok = next_word(rl_line_buffer, len, &e)) == NULL)	/* no first word yet */
Packit 575503
		return  rl_completion_matches(text, command_generator);
Packit 575503
	len -= (e - rl_line_buffer);
Packit 575503
Packit 575503
	idx = find_command(cmdtok, e - cmdtok);
Packit 575503
	if (idx < 0)
Packit 575503
		return NULL;
Packit 575503
	this_cmd = cmdtab[idx].type;
Packit 575503
Packit 575503
	if (! next_word(e, len, NULL)) {
Packit 575503
		switch (this_cmd) {
Packit 575503
		case D_break:
Packit 575503
		case D_list:
Packit 575503
		case D_until:
Packit 575503
		case D_tbreak:
Packit 575503
		case D_clear:
Packit 575503
			return rl_completion_matches(text, srcfile_generator);
Packit 575503
		case D_info:
Packit 575503
		case D_enable:
Packit 575503
		case D_trace:
Packit 575503
		case D_help:
Packit 575503
			return rl_completion_matches(text, argument_generator);
Packit 575503
		case D_option:
Packit 575503
			return rl_completion_matches(text, option_generator);
Packit 575503
		case D_print:
Packit 575503
		case D_printf:
Packit 575503
		case D_set:
Packit 575503
		case D_display:
Packit 575503
		case D_watch:
Packit 575503
			return rl_completion_matches(text, variable_generator);
Packit 575503
		default:
Packit 575503
			return NULL;
Packit 575503
		}
Packit 575503
	}
Packit 575503
Packit 575503
	if (this_cmd == D_print || this_cmd == D_printf)
Packit 575503
		return rl_completion_matches(text, variable_generator);
Packit 575503
	return NULL;
Packit 575503
}
Packit 575503
Packit 575503
/* command_generator --- generator function for command completion */
Packit 575503
Packit 575503
static char *
Packit 575503
command_generator(const char *text, int state)
Packit 575503
{
Packit 575503
	static size_t textlen;
Packit 575503
	static int idx = 0;
Packit 575503
	char *name;
Packit 575503
Packit 575503
	if (! state) {	/* first time */
Packit 575503
		textlen = strlen(text);
Packit 575503
		idx = 0;
Packit 575503
	}
Packit 575503
	while ((name = (char *) cmdtab[idx].name) != NULL) {
Packit 575503
		idx++;
Packit 575503
		if (strncmp(name, text, textlen) == 0)
Packit 575503
			return estrdup(name, strlen(name));
Packit 575503
	}
Packit 575503
	return NULL;
Packit 575503
}
Packit 575503
Packit 575503
/* srcfile_generator --- generator function for source file completion */
Packit 575503
Packit 575503
static char *
Packit 575503
srcfile_generator(const char *text, int state)
Packit 575503
{
Packit 575503
	static size_t textlen;
Packit 575503
	static SRCFILE *s;
Packit 575503
	char *name;
Packit 575503
	extern SRCFILE *srcfiles;
Packit 575503
Packit 575503
	if (! state) {	/* first time */
Packit 575503
		textlen = strlen(text);
Packit 575503
		s = srcfiles->next;
Packit 575503
	}
Packit 575503
	while (s != srcfiles) {
Packit 575503
		if (s->stype != SRC_FILE && s->stype != SRC_INC) {
Packit 575503
			s = s->next;
Packit 575503
			continue;
Packit 575503
		}
Packit 575503
		name = s->src;
Packit 575503
		s = s->next;
Packit 575503
		if (strncmp(name, text, textlen) == 0)
Packit 575503
			return estrdup(name, strlen(name));
Packit 575503
	}
Packit 575503
	return NULL;
Packit 575503
}
Packit 575503
Packit 575503
/* argument_generator --- generator function for non-number argument completion */
Packit 575503
Packit 575503
static char *
Packit 575503
argument_generator(const char *text, int state)
Packit 575503
{
Packit 575503
	static size_t textlen;
Packit 575503
	static int idx;
Packit 575503
	const char *name;
Packit 575503
Packit 575503
	if (! state) {	/* first time */
Packit 575503
		textlen = strlen(text);
Packit 575503
		idx = 0;
Packit 575503
	}
Packit 575503
Packit 575503
	if (this_cmd == D_help) {
Packit 575503
		while ((name = cmdtab[idx++].name) != NULL) {
Packit 575503
			if (strncmp(name, text, textlen) == 0)
Packit 575503
				return estrdup(name, strlen(name));
Packit 575503
		}
Packit 575503
	} else {
Packit 575503
		while ((name = argtab[idx].name) != NULL) {
Packit 575503
			if (this_cmd != argtab[idx++].cmd)
Packit 575503
				continue;
Packit 575503
			if (strncmp(name, text, textlen) == 0)
Packit 575503
				return estrdup(name, strlen(name));
Packit 575503
		}
Packit 575503
	}
Packit 575503
	return NULL;
Packit 575503
}
Packit 575503
Packit 575503
/* variable_generator --- generator function for variable name completion */
Packit 575503
Packit 575503
static char *
Packit 575503
variable_generator(const char *text, int state)
Packit 575503
{
Packit 575503
	static size_t textlen;
Packit 575503
	static int idx = 0;
Packit 575503
	static NODE *func = NULL;
Packit 575503
	static NODE **vars = NULL;
Packit 575503
	const char *name;
Packit 575503
	NODE *r;
Packit 575503
Packit 575503
	if (! state) {	/* first time */
Packit 575503
		textlen = strlen(text);
Packit 575503
		if (vars != NULL)
Packit 575503
			efree(vars);
Packit 575503
		vars = variable_list();
Packit 575503
		idx = 0;
Packit 575503
		func = get_function();  /* function in current context */
Packit 575503
	}
Packit 575503
Packit 575503
	/* function params */
Packit 575503
	while (func != NULL) {
Packit 575503
		if (idx >= func->param_cnt) {
Packit 575503
			func = NULL;	/* don't try to match params again */
Packit 575503
			idx = 0;
Packit 575503
			break;
Packit 575503
		}
Packit 575503
		name = func->fparms[idx++].param;
Packit 575503
		if (strncmp(name, text, textlen) == 0)
Packit 575503
			return estrdup(name, strlen(name));
Packit 575503
	}
Packit 575503
Packit 575503
	/* globals */
Packit 575503
	while ((r = vars[idx++]) != NULL) {
Packit 575503
		name = r->vname;
Packit 575503
		if (strncmp(name, text, textlen) == 0)
Packit 575503
			return estrdup(name, strlen(name));
Packit 575503
	}
Packit 575503
Packit 575503
	return NULL;
Packit 575503
}
Packit 575503
Packit 575503
/* history_expand_line ---  history expand the LINE */
Packit 575503
Packit 575503
static void
Packit 575503
history_expand_line(char **line)
Packit 575503
{
Packit 575503
	int ret;
Packit 575503
	char *expansion;
Packit 575503
Packit 575503
	if (! *line || input_fd != 0 || ! input_from_tty)
Packit 575503
		return;
Packit 575503
	using_history();
Packit 575503
	ret = history_expand(*line, &expansion);
Packit 575503
	if (ret < 0 || ret == 2)
Packit 575503
		efree(expansion);
Packit 575503
	else {
Packit 575503
		efree(*line);
Packit 575503
		*line = expansion;
Packit 575503
	}
Packit 575503
}
Packit 575503
Packit 575503
#endif