xref: /titanic_44/usr/src/cmd/sgs/elfedit/common/elfedit.c (revision 69112edd987c28fa551d4f8d9362a84a45365f17)
1d29b2c44Sab196087 /*
2d29b2c44Sab196087  * CDDL HEADER START
3d29b2c44Sab196087  *
4d29b2c44Sab196087  * The contents of this file are subject to the terms of the
5d29b2c44Sab196087  * Common Development and Distribution License (the "License").
6d29b2c44Sab196087  * You may not use this file except in compliance with the License.
7d29b2c44Sab196087  *
8d29b2c44Sab196087  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9d29b2c44Sab196087  * or http://www.opensolaris.org/os/licensing.
10d29b2c44Sab196087  * See the License for the specific language governing permissions
11d29b2c44Sab196087  * and limitations under the License.
12d29b2c44Sab196087  *
13d29b2c44Sab196087  * When distributing Covered Code, include this CDDL HEADER in each
14d29b2c44Sab196087  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15d29b2c44Sab196087  * If applicable, add the following below this CDDL HEADER, with the
16d29b2c44Sab196087  * fields enclosed by brackets "[]" replaced with your own identifying
17d29b2c44Sab196087  * information: Portions Copyright [yyyy] [name of copyright owner]
18d29b2c44Sab196087  *
19d29b2c44Sab196087  * CDDL HEADER END
20d29b2c44Sab196087  */
21d29b2c44Sab196087 
22d29b2c44Sab196087 /*
23*69112eddSAli Bahrami  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24d29b2c44Sab196087  * Use is subject to license terms.
25d29b2c44Sab196087  */
26d29b2c44Sab196087 
27d29b2c44Sab196087 #include	<sys/types.h>
28d29b2c44Sab196087 #include	<sys/stat.h>
29d29b2c44Sab196087 #include	<sys/wait.h>
30d29b2c44Sab196087 #include	<stdarg.h>
31d29b2c44Sab196087 #include	<fcntl.h>
32d29b2c44Sab196087 #include	<stdlib.h>
33d29b2c44Sab196087 #include	<stdio.h>
34d29b2c44Sab196087 #include	<signal.h>
35d29b2c44Sab196087 #include	<dirent.h>
36d29b2c44Sab196087 #include	<libelf.h>
37d29b2c44Sab196087 #include	<gelf.h>
38d29b2c44Sab196087 #include	<conv.h>
39d29b2c44Sab196087 #include	<dlfcn.h>
40d29b2c44Sab196087 #include	<link.h>
41d29b2c44Sab196087 #include	<stdarg.h>
42d29b2c44Sab196087 #include	<libgen.h>
43d29b2c44Sab196087 #include	<libintl.h>
44d29b2c44Sab196087 #include	<locale.h>
45d29b2c44Sab196087 #include	<unistd.h>
46d29b2c44Sab196087 #include	<errno.h>
47d29b2c44Sab196087 #include	<ctype.h>
48d29b2c44Sab196087 #include	<limits.h>
49d29b2c44Sab196087 #include	<strings.h>
50d29b2c44Sab196087 #include	<sgs.h>
51d29b2c44Sab196087 #include	"msg.h"
52d29b2c44Sab196087 #include	"_elfedit.h"
53d29b2c44Sab196087 #include	<debug.h>	/* liblddb */
54d29b2c44Sab196087 
55d29b2c44Sab196087 
56d29b2c44Sab196087 
57d29b2c44Sab196087 /*
58d29b2c44Sab196087  * Column at which elfedit_format_command_usage() will wrap the
59d29b2c44Sab196087  * generated usage string if the wrap argument is True (1).
60d29b2c44Sab196087  */
61d29b2c44Sab196087 #define	USAGE_WRAP_COL 55
62d29b2c44Sab196087 
63d29b2c44Sab196087 
64d29b2c44Sab196087 
65d29b2c44Sab196087 
66d29b2c44Sab196087 /*
67d29b2c44Sab196087  * Type used to represent a string buffer that can grow as needed
68d29b2c44Sab196087  * to hold strings of arbitrary length. The user should declare
69d29b2c44Sab196087  * variables of this type sa static. The strbuf_ensure_size() function
70d29b2c44Sab196087  * is used to ensure that it has a minimum desired size.
71d29b2c44Sab196087  */
72d29b2c44Sab196087 typedef struct {
73d29b2c44Sab196087 	char *buf;		/* String buffer */
74d29b2c44Sab196087 	size_t n;		/* Size of buffer */
75d29b2c44Sab196087 } STRBUF;
76d29b2c44Sab196087 
77d29b2c44Sab196087 
78d29b2c44Sab196087 
79d29b2c44Sab196087 
80d29b2c44Sab196087 /*
81d29b2c44Sab196087  * Types used by tokenize_user_cmd() to represent the result of
82d29b2c44Sab196087  * spliting a user command into individual tokens.
83d29b2c44Sab196087  */
84d29b2c44Sab196087 typedef struct {
85d29b2c44Sab196087 	char	*tok_str;	/* Token string */
86d29b2c44Sab196087 	size_t	tok_len;	/* strlen(str) */
87d29b2c44Sab196087 	size_t	tok_line_off;	/* Token offset in original string */
88d29b2c44Sab196087 } TOK_ELT;
89d29b2c44Sab196087 typedef struct {
90d29b2c44Sab196087 	size_t	tokst_cmd_len;	/* Length of original user command, without */
91d29b2c44Sab196087 				/*	newline or NULL termination chars */
92d29b2c44Sab196087 	size_t	tokst_str_size;	/* Space needed to hold all the resulting */
93d29b2c44Sab196087 				/*	tokens, including terminating NULL */
94d29b2c44Sab196087 	TOK_ELT	*tokst_buf;	/* The array of tokens */
95d29b2c44Sab196087 	size_t	tokst_cnt;	/* # of tokens in array */
96d29b2c44Sab196087 	size_t	tokst_bufsize;	/* capacity of array */
97d29b2c44Sab196087 } TOK_STATE;
98d29b2c44Sab196087 
99d29b2c44Sab196087 
100d29b2c44Sab196087 
101d29b2c44Sab196087 
102d29b2c44Sab196087 /* State block used by gettok_init() and gettok() */
103d29b2c44Sab196087 typedef struct {
104d29b2c44Sab196087 	const char	*gtok_buf;	/* Addr of buffer containing string */
105d29b2c44Sab196087 	char		*gtok_cur_buf;	/* Addr withing buffer for next token */
106d29b2c44Sab196087 	int		gtok_inc_null_final; /* True if final NULL token used */
107d29b2c44Sab196087 	int		gtok_null_seen;	/* True when NULL byte seen */
108d29b2c44Sab196087 	TOK_ELT		gtok_last_token; /* Last token parsed */
109d29b2c44Sab196087 
110d29b2c44Sab196087 } GETTOK_STATE;
111d29b2c44Sab196087 
112d29b2c44Sab196087 
113d29b2c44Sab196087 
114d29b2c44Sab196087 
115d29b2c44Sab196087 /*
116d29b2c44Sab196087  * The elfedit_cpl_*() functions are used for command line completion.
117d29b2c44Sab196087  * Currently this uses the tecla library, but to allow for changing the
118d29b2c44Sab196087  * library used, we hide all tecla interfaces from our modules. Instead,
119d29b2c44Sab196087  * cmd_match_fcn() builds an ELFEDIT_CPL_STATE struct, and we pass the
120d29b2c44Sab196087  * address of that struct as an opaque handle to the modules. Since the
121d29b2c44Sab196087  * pointer is opaque, the contents of ELFEDIT_CPL_STATE are free to change
122d29b2c44Sab196087  * as necessary.
123d29b2c44Sab196087  */
124d29b2c44Sab196087 typedef struct {
125d29b2c44Sab196087 	WordCompletion	*ecpl_cpl;		/* tecla handle */
126d29b2c44Sab196087 	const char	*ecpl_line;		/* raw input line */
127d29b2c44Sab196087 	int		ecpl_word_start;	/* start offset within line */
128d29b2c44Sab196087 	int		ecpl_word_end;		/* offset just past token */
129d29b2c44Sab196087 	/*
130d29b2c44Sab196087 	 * ecpl_add_mod_colon is a secret handshake between
131d29b2c44Sab196087 	 * elfedit_cpl_command() and  elfedit_cpl_add_match(). It adds
132d29b2c44Sab196087 	 * ':' to end of matched modules.
133d29b2c44Sab196087 	 */
134d29b2c44Sab196087 	int		ecpl_add_mod_colon;
135d29b2c44Sab196087 	const char	*ecpl_token_str;	/* token being completed */
136d29b2c44Sab196087 	size_t		ecpl_token_len;		/* strlen(ecpl_token_str) */
137d29b2c44Sab196087 } ELFEDIT_CPL_STATE;
138d29b2c44Sab196087 
139d29b2c44Sab196087 
140d29b2c44Sab196087 
141d29b2c44Sab196087 
142d29b2c44Sab196087 /* This structure maintains elfedit global state */
143d29b2c44Sab196087 STATE_T state;
144d29b2c44Sab196087 
145d29b2c44Sab196087 
146d29b2c44Sab196087 
147d29b2c44Sab196087 /*
148d29b2c44Sab196087  * Define a pair of static global variables that contain the
149d29b2c44Sab196087  * ISA strings that correspond to %i and %I tokens in module search
150d29b2c44Sab196087  * paths.
151d29b2c44Sab196087  *
152d29b2c44Sab196087  *	isa_i_str - The ISA string for the currently running program
153d29b2c44Sab196087  *	isa_I_str - For 64-bit programs, the same as isa_i_str. For
154d29b2c44Sab196087  *		32-bit programs, an empty string.
155d29b2c44Sab196087  */
156d29b2c44Sab196087 #ifdef __sparc
157d29b2c44Sab196087 #ifdef __sparcv9
158d29b2c44Sab196087 static const char *isa_i_str = MSG_ORIG(MSG_ISA_SPARC_64);
159d29b2c44Sab196087 static const char *isa_I_str = MSG_ORIG(MSG_ISA_SPARC_64);
160d29b2c44Sab196087 #else
161d29b2c44Sab196087 static const char *isa_i_str = MSG_ORIG(MSG_ISA_SPARC_32);
162d29b2c44Sab196087 static const char *isa_I_str = MSG_ORIG(MSG_STR_EMPTY);
163d29b2c44Sab196087 #endif
164d29b2c44Sab196087 #endif
165d29b2c44Sab196087 
166d29b2c44Sab196087 #ifdef __i386
167d29b2c44Sab196087 static const char *isa_i_str = MSG_ORIG(MSG_ISA_X86_32);
168d29b2c44Sab196087 static const char *isa_I_str = MSG_ORIG(MSG_STR_EMPTY);
169d29b2c44Sab196087 #endif
170d29b2c44Sab196087 #ifdef __amd64
171d29b2c44Sab196087 static const char *isa_i_str = MSG_ORIG(MSG_ISA_X86_64);
172d29b2c44Sab196087 static const char *isa_I_str = MSG_ORIG(MSG_ISA_X86_64);
173d29b2c44Sab196087 #endif
174d29b2c44Sab196087 
175d29b2c44Sab196087 
176d29b2c44Sab196087 
177d29b2c44Sab196087 /* Forward declarations */
178d29b2c44Sab196087 static void free_user_cmds(void);
179d29b2c44Sab196087 static void elfedit_pager_cleanup(void);
180d29b2c44Sab196087 
181d29b2c44Sab196087 
182d29b2c44Sab196087 
183d29b2c44Sab196087 /*
184d29b2c44Sab196087  * We supply this function for the msg module
185d29b2c44Sab196087  */
186d29b2c44Sab196087 const char *
_elfedit_msg(Msg mid)187d29b2c44Sab196087 _elfedit_msg(Msg mid)
188d29b2c44Sab196087 {
189d29b2c44Sab196087 	return (gettext(MSG_ORIG(mid)));
190d29b2c44Sab196087 }
191d29b2c44Sab196087 
192d29b2c44Sab196087 
193d29b2c44Sab196087 /*
194d29b2c44Sab196087  * Copy at most min(cpsize, dstsize-1) bytes from src into dst,
195d29b2c44Sab196087  * truncating src if necessary.  The  result is always null-terminated.
196d29b2c44Sab196087  *
197d29b2c44Sab196087  * entry:
198d29b2c44Sab196087  *	dst - Destination buffer
199d29b2c44Sab196087  *	src - Source string
200d29b2c44Sab196087  *	dstsize - sizeof(dst)
201d29b2c44Sab196087  *
202d29b2c44Sab196087  * note:
203d29b2c44Sab196087  *	This is similar to strncpy(), but with two modifications:
204d29b2c44Sab196087  *	1) You specify the number of characters to copy, not just
205d29b2c44Sab196087  *		the size of the destination. Hence, you can copy non-NULL
206d29b2c44Sab196087  *		terminated strings.
207d29b2c44Sab196087  *	2) The destination is guaranteed to be NULL terminated. strncpy()
208d29b2c44Sab196087  *		does not terminate a completely full buffer.
209d29b2c44Sab196087  */
210d29b2c44Sab196087 static void
elfedit_strnbcpy(char * dst,const char * src,size_t cpsize,size_t dstsize)211d29b2c44Sab196087 elfedit_strnbcpy(char *dst, const char *src, size_t cpsize, size_t dstsize)
212d29b2c44Sab196087 {
213d29b2c44Sab196087 	if (cpsize >= dstsize)
214d29b2c44Sab196087 		cpsize = dstsize - 1;
215d29b2c44Sab196087 	if (cpsize > 0)
216d29b2c44Sab196087 		(void) strncpy(dst, src, cpsize + 1);
217d29b2c44Sab196087 	dst[cpsize] = '\0';
218d29b2c44Sab196087 }
219d29b2c44Sab196087 
220d29b2c44Sab196087 
221d29b2c44Sab196087 /*
222d29b2c44Sab196087  * Calls exit() on behalf of elfedit.
223d29b2c44Sab196087  */
224d29b2c44Sab196087 void
elfedit_exit(int status)225d29b2c44Sab196087 elfedit_exit(int status)
226d29b2c44Sab196087 {
227d29b2c44Sab196087 	if (state.file.present) {
228d29b2c44Sab196087 		/* Exiting with unflushed changes pending? Issue debug notice */
229d29b2c44Sab196087 		if (state.file.dirty)
230d29b2c44Sab196087 			elfedit_msg(ELFEDIT_MSG_DEBUG,
231d29b2c44Sab196087 			    MSG_INTL(MSG_DEBUG_DIRTYEXIT));
232d29b2c44Sab196087 
233d29b2c44Sab196087 		/*
234d29b2c44Sab196087 		 * If the edit file is marked for unlink on exit, then
235d29b2c44Sab196087 		 * take care of it here.
236d29b2c44Sab196087 		 */
237d29b2c44Sab196087 		if (state.file.unlink_on_exit) {
238d29b2c44Sab196087 			elfedit_msg(ELFEDIT_MSG_DEBUG,
239d29b2c44Sab196087 			    MSG_INTL(MSG_DEBUG_UNLINKFILE),
240d29b2c44Sab196087 			    state.file.outfile);
241d29b2c44Sab196087 			(void) unlink(state.file.outfile);
242d29b2c44Sab196087 		}
243d29b2c44Sab196087 	}
244d29b2c44Sab196087 
245d29b2c44Sab196087 	exit(status);
246d29b2c44Sab196087 }
247d29b2c44Sab196087 
248d29b2c44Sab196087 
249d29b2c44Sab196087 /*
250d29b2c44Sab196087  * Standard message function for elfedit. All user visible
251d29b2c44Sab196087  * output, for error or informational reasons, should go through
252d29b2c44Sab196087  * this function.
253d29b2c44Sab196087  *
254d29b2c44Sab196087  * entry:
255d29b2c44Sab196087  *	type - Type of message. One of the ELFEDIT_MSG_* values.
256d29b2c44Sab196087  *	format, ... - As per the printf() family
257d29b2c44Sab196087  *
258d29b2c44Sab196087  * exit:
259d29b2c44Sab196087  *	The desired message has been output. For informational
260d29b2c44Sab196087  *	messages, control returns to the caller. For errors,
261d29b2c44Sab196087  *	this routine will terminate execution or strip the execution
262d29b2c44Sab196087  *	stack and return control directly to the outer control loop.
263d29b2c44Sab196087  *	In either case, the caller will not receive control.
264d29b2c44Sab196087  */
265d29b2c44Sab196087 /*PRINTFLIKE2*/
266d29b2c44Sab196087 void
elfedit_msg(elfedit_msg_t type,const char * format,...)267d29b2c44Sab196087 elfedit_msg(elfedit_msg_t type, const char *format, ...)
268d29b2c44Sab196087 {
269d29b2c44Sab196087 	typedef enum {			/* What to do after finished */
270d29b2c44Sab196087 		DISP_RET = 0,		/* Return to caller */
271d29b2c44Sab196087 		DISP_JMP = 1, 		/* if (interactive) longjmp else exit */
272d29b2c44Sab196087 		DISP_EXIT = 2		/* exit under all circumstances */
273d29b2c44Sab196087 	} DISP;
274d29b2c44Sab196087 
275d29b2c44Sab196087 	va_list args;
276d29b2c44Sab196087 	FILE *stream = stderr;
277d29b2c44Sab196087 	DISP disp = DISP_RET;
278d29b2c44Sab196087 	int do_output = 1;
279d29b2c44Sab196087 	int need_prefix = 1;
280d29b2c44Sab196087 
281d29b2c44Sab196087 	va_start(args, format);
282d29b2c44Sab196087 
283d29b2c44Sab196087 	switch (type) {
284d29b2c44Sab196087 	case ELFEDIT_MSG_ERR:
285d29b2c44Sab196087 	case ELFEDIT_MSG_CMDUSAGE:
286d29b2c44Sab196087 		disp = DISP_JMP;
287d29b2c44Sab196087 		break;
288d29b2c44Sab196087 	case ELFEDIT_MSG_FATAL:
289d29b2c44Sab196087 		disp = DISP_EXIT;
290d29b2c44Sab196087 		break;
291d29b2c44Sab196087 	case ELFEDIT_MSG_USAGE:
292d29b2c44Sab196087 		need_prefix = 0;
293d29b2c44Sab196087 		break;
294d29b2c44Sab196087 	case ELFEDIT_MSG_DEBUG:
295d29b2c44Sab196087 		if (!(state.flags & ELFEDIT_F_DEBUG))
296d29b2c44Sab196087 			return;
297d29b2c44Sab196087 		stream = stdout;
298d29b2c44Sab196087 		break;
299d29b2c44Sab196087 	case ELFEDIT_MSG_QUIET:
300d29b2c44Sab196087 		do_output = 0;
301d29b2c44Sab196087 		disp = DISP_JMP;
302d29b2c44Sab196087 		break;
303d29b2c44Sab196087 	}
304d29b2c44Sab196087 
305d29b2c44Sab196087 
306d29b2c44Sab196087 	/*
307d29b2c44Sab196087 	 * If there is a pager process running, we are returning to the
308d29b2c44Sab196087 	 * caller, and the output is going to stdout, then let the
309d29b2c44Sab196087 	 * pager handle it instead of writing it directly from this process.
310d29b2c44Sab196087 	 * That way, the output gets paged along with everything else.
311d29b2c44Sab196087 	 *
312d29b2c44Sab196087 	 * If there is a pager process running, and we are not returning
313d29b2c44Sab196087 	 * to the caller, then end the pager process now, before we generate
314d29b2c44Sab196087 	 * any new output. This allows for any text buffered in the pager
315d29b2c44Sab196087 	 * pipe to be output before the new stuff.
316d29b2c44Sab196087 	 */
317d29b2c44Sab196087 	if (state.pager.fptr != NULL) {
318d29b2c44Sab196087 		if (disp == DISP_RET) {
319d29b2c44Sab196087 			if (stream == stdout)
320d29b2c44Sab196087 				stream = state.pager.fptr;
321d29b2c44Sab196087 		} else {
322d29b2c44Sab196087 			elfedit_pager_cleanup();
323d29b2c44Sab196087 		}
324d29b2c44Sab196087 	}
325d29b2c44Sab196087 
326d29b2c44Sab196087 	/*
327d29b2c44Sab196087 	 * If this message is coming from within the libtecla command
328d29b2c44Sab196087 	 * completion code, call gl_normal_io() to give the library notice.
329d29b2c44Sab196087 	 * That function sets the tty back to cooked mode and advances
330d29b2c44Sab196087 	 * the cursor to the beginning of the next line so that our output
331d29b2c44Sab196087 	 * will appear properly. When we return to the command completion code,
332d29b2c44Sab196087 	 * tecla will re-enter raw mode and redraw the current command line.
333d29b2c44Sab196087 	 */
334d29b2c44Sab196087 	if (state.input.in_tecla)
335d29b2c44Sab196087 		(void) gl_normal_io(state.input.gl);
336d29b2c44Sab196087 
337d29b2c44Sab196087 	if (do_output) {
338d29b2c44Sab196087 		if (need_prefix)
339d29b2c44Sab196087 			(void) fprintf(stream, MSG_ORIG(MSG_STR_ELFEDIT));
340d29b2c44Sab196087 		(void) vfprintf(stream, format, args);
341d29b2c44Sab196087 		(void) fflush(stream);
342d29b2c44Sab196087 	}
343d29b2c44Sab196087 	va_end(args);
344d29b2c44Sab196087 
345d29b2c44Sab196087 	/*
346d29b2c44Sab196087 	 * If this is an error, then we do not return to the caller.
347d29b2c44Sab196087 	 * The action taken depends on whether the outer loop has registered
348d29b2c44Sab196087 	 * a jump buffer for us or not.
349d29b2c44Sab196087 	 */
350d29b2c44Sab196087 	if (disp != DISP_RET) {
351d29b2c44Sab196087 		if (state.msg_jbuf.active && (disp == DISP_JMP)) {
352d29b2c44Sab196087 			/* Free the user command list */
353d29b2c44Sab196087 			free_user_cmds();
354d29b2c44Sab196087 
355d29b2c44Sab196087 			/* Clean up to reflect effect of non-local goto */
356d29b2c44Sab196087 			state.input.in_tecla = FALSE;
357d29b2c44Sab196087 
358d29b2c44Sab196087 			/* Jump to the outer loop to resume */
359d29b2c44Sab196087 			siglongjmp(state.msg_jbuf.env, 1);
360d29b2c44Sab196087 		} else {
361d29b2c44Sab196087 			elfedit_exit(1);
362d29b2c44Sab196087 		}
363d29b2c44Sab196087 	}
364d29b2c44Sab196087 }
365d29b2c44Sab196087 
366d29b2c44Sab196087 
367d29b2c44Sab196087 /*
368d29b2c44Sab196087  * Wrapper on elfedit_msg() that issues an error that results from
369d29b2c44Sab196087  * a call to libelf.
370d29b2c44Sab196087  *
371d29b2c44Sab196087  * entry:
372d29b2c44Sab196087  *	file - Name of ELF object
373d29b2c44Sab196087  *	libelf_rtn_name - Name of routine that was called
374d29b2c44Sab196087  *
375d29b2c44Sab196087  * exit:
376d29b2c44Sab196087  *	An error has been issued that shows the routine called
377d29b2c44Sab196087  *	and the libelf error string for it from elf_errmsg().
378d29b2c44Sab196087  *	This routine does not return to the caller.
379d29b2c44Sab196087  */
380d29b2c44Sab196087 void
elfedit_elferr(const char * file,const char * libelf_rtn_name)381d29b2c44Sab196087 elfedit_elferr(const char *file, const char *libelf_rtn_name)
382d29b2c44Sab196087 {
383d29b2c44Sab196087 	const char *errstr = elf_errmsg(elf_errno());
384d29b2c44Sab196087 
385d29b2c44Sab196087 	elfedit_msg(ELFEDIT_MSG_ERR, MSG_INTL(MSG_ERR_LIBELF), file,
386d29b2c44Sab196087 	    libelf_rtn_name, errstr ? errstr : MSG_INTL(MSG_FMT_UNKNOWN));
387d29b2c44Sab196087 }
388d29b2c44Sab196087 
389d29b2c44Sab196087 
390d29b2c44Sab196087 /*
391d29b2c44Sab196087  * Start an output pager process for elfedit_printf()/elfedit_write() to use.
392d29b2c44Sab196087  *
393d29b2c44Sab196087  * note:
394d29b2c44Sab196087  *	If this elfedit session is not interactive, then no pager is
395d29b2c44Sab196087  *	started. Paging is only intended for interactive use. The caller
396d29b2c44Sab196087  *	is not supposed to worry about this point, but simply to use
397d29b2c44Sab196087  *	this function to flag situations in which paging might be needed.
398d29b2c44Sab196087  */
399d29b2c44Sab196087 void
elfedit_pager_init(void)400d29b2c44Sab196087 elfedit_pager_init(void)
401d29b2c44Sab196087 {
402d29b2c44Sab196087 	const char	*errstr;
403d29b2c44Sab196087 	const char	*cmd;
404d29b2c44Sab196087 	int		err;
405d29b2c44Sab196087 
406d29b2c44Sab196087 	/*
407d29b2c44Sab196087 	 * If there is no pager process running, start one.
408d29b2c44Sab196087 	 * Only do this for interactive sessions --- elfedit_pager()
409d29b2c44Sab196087 	 * won't use a pager in batch mode.
410d29b2c44Sab196087 	 */
411d29b2c44Sab196087 	if (state.msg_jbuf.active && state.input.full_tty &&
412d29b2c44Sab196087 	    (state.pager.fptr == NULL)) {
413d29b2c44Sab196087 		/*
414d29b2c44Sab196087 		 * If the user has the PAGER environment variable set,
415d29b2c44Sab196087 		 * then we will use that program. Otherwise we default
416d29b2c44Sab196087 		 * to /bin/more.
417d29b2c44Sab196087 		 */
418d29b2c44Sab196087 		cmd = getenv(MSG_ORIG(MSG_STR_PAGER));
419d29b2c44Sab196087 		if ((cmd == NULL) || (*cmd == '\0'))
420d29b2c44Sab196087 			cmd = MSG_ORIG(MSG_STR_BINMORE);
421d29b2c44Sab196087 
422d29b2c44Sab196087 		/*
423d29b2c44Sab196087 		 * The popen() manpage says that on failure, it "may set errno",
424d29b2c44Sab196087 		 * which is somewhat ambiguous. We explicitly zero it here, and
425d29b2c44Sab196087 		 * assume that any change is due to popen() failing.
426d29b2c44Sab196087 		 */
427d29b2c44Sab196087 		errno = 0;
428d29b2c44Sab196087 		state.pager.fptr = popen(cmd, MSG_ORIG(MSG_STR_W));
429d29b2c44Sab196087 		if (state.pager.fptr == NULL) {
430d29b2c44Sab196087 			err = errno;
431d29b2c44Sab196087 			errstr = (err == 0) ? MSG_INTL(MSG_ERR_UNKNOWNSYSERR) :
432d29b2c44Sab196087 			    strerror(err);
433d29b2c44Sab196087 			elfedit_msg(ELFEDIT_MSG_ERR, MSG_INTL(MSG_ERR_CNTEXEC),
434d29b2c44Sab196087 			    MSG_ORIG(MSG_STR_ELFEDIT), cmd, errstr);
435d29b2c44Sab196087 		}
436d29b2c44Sab196087 	}
437d29b2c44Sab196087 }
438d29b2c44Sab196087 
439d29b2c44Sab196087 
440d29b2c44Sab196087 /*
441d29b2c44Sab196087  * If there is a pager process present, close it out.
442d29b2c44Sab196087  *
443d29b2c44Sab196087  * note:
444d29b2c44Sab196087  *	This function is called from within elfedit_msg(), and as
445d29b2c44Sab196087  *	such, must not use elfedit_msg() to report errors. Furthermore,
446d29b2c44Sab196087  *	any such errors are not a sufficient reason to terminate the process
447d29b2c44Sab196087  *	or to longjmp(). This is a rare case where errors are written
448d29b2c44Sab196087  *	directly to stderr.
449d29b2c44Sab196087  */
450d29b2c44Sab196087 static void
elfedit_pager_cleanup(void)451d29b2c44Sab196087 elfedit_pager_cleanup(void)
452d29b2c44Sab196087 {
453d29b2c44Sab196087 	if (state.pager.fptr != NULL) {
454d29b2c44Sab196087 		if (pclose(state.pager.fptr) == -1)
455d29b2c44Sab196087 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_PAGERFINI));
456d29b2c44Sab196087 
457d29b2c44Sab196087 		state.pager.fptr = NULL;
458d29b2c44Sab196087 	}
459d29b2c44Sab196087 }
460d29b2c44Sab196087 
461d29b2c44Sab196087 
462d29b2c44Sab196087 /*
463d29b2c44Sab196087  * Print general formtted text for the user, using printf()-style
464d29b2c44Sab196087  * formatting. Uses the pager process if one has been started, or
465d29b2c44Sab196087  * stdout otherwise.
466d29b2c44Sab196087  */
467d29b2c44Sab196087 void
elfedit_printf(const char * format,...)468d29b2c44Sab196087 elfedit_printf(const char *format, ...)
469d29b2c44Sab196087 {
470d29b2c44Sab196087 	va_list	args;
471d29b2c44Sab196087 	int	err;
472d29b2c44Sab196087 	FILE	*fptr;
473d29b2c44Sab196087 	int	pager;
474d29b2c44Sab196087 	int	broken_pipe = 0;
475d29b2c44Sab196087 
476d29b2c44Sab196087 	/*
477d29b2c44Sab196087 	 * If there is a pager process, then use it. Otherwise write
478d29b2c44Sab196087 	 * directly to stdout.
479d29b2c44Sab196087 	 */
480d29b2c44Sab196087 	pager = (state.pager.fptr != NULL);
481d29b2c44Sab196087 	fptr = pager ? state.pager.fptr : stdout;
482d29b2c44Sab196087 
483d29b2c44Sab196087 	va_start(args, format);
484d29b2c44Sab196087 	errno = 0;
485d29b2c44Sab196087 	err = vfprintf(fptr, format, args);
486d29b2c44Sab196087 
487d29b2c44Sab196087 	/* Did we fail because a child pager process has exited? */
488d29b2c44Sab196087 	broken_pipe = pager && (err < 0) && (errno == EPIPE);
489d29b2c44Sab196087 
490d29b2c44Sab196087 	va_end(args);
491d29b2c44Sab196087 
492d29b2c44Sab196087 	/*
493d29b2c44Sab196087 	 * On error, we simply issue the error without cleaning up
494d29b2c44Sab196087 	 * the pager process. The message code handles that as a standard
495d29b2c44Sab196087 	 * part of error processing.
496d29b2c44Sab196087 	 *
497d29b2c44Sab196087 	 * We handle failure due to an exited pager process differently
498d29b2c44Sab196087 	 * than a normal error, because it is usually due to the user
499d29b2c44Sab196087 	 * intentionally telling it to.
500d29b2c44Sab196087 	 */
501d29b2c44Sab196087 	if (err < 0) {
502d29b2c44Sab196087 		if (broken_pipe)
503d29b2c44Sab196087 			elfedit_msg(ELFEDIT_MSG_QUIET, MSG_ORIG(MSG_STR_NULL));
504d29b2c44Sab196087 		else
505d29b2c44Sab196087 			elfedit_msg(ELFEDIT_MSG_ERR, MSG_INTL(MSG_ERR_PRINTF));
506d29b2c44Sab196087 	}
507d29b2c44Sab196087 }
508d29b2c44Sab196087 
509d29b2c44Sab196087 
510d29b2c44Sab196087 /*
511d29b2c44Sab196087  * Some our modules use liblddb routines to format ELF output.
512d29b2c44Sab196087  * In order to ensure that such output is sent to the pager pipe
513d29b2c44Sab196087  * when there is one, and stdout otherwise, we redefine the dbg_print()
514d29b2c44Sab196087  * function here.
515d29b2c44Sab196087  *
516d29b2c44Sab196087  * This item should be defined NODIRECT.
517d29b2c44Sab196087  */
518d29b2c44Sab196087 /* PRINTFLIKE2 */
519d29b2c44Sab196087 void
dbg_print(Lm_list * lml,const char * format,...)520d29b2c44Sab196087 dbg_print(Lm_list *lml, const char *format, ...)
521d29b2c44Sab196087 {
522d29b2c44Sab196087 	va_list	ap;
523d29b2c44Sab196087 	int	err;
524d29b2c44Sab196087 	FILE	*fptr;
525d29b2c44Sab196087 	int	pager;
526d29b2c44Sab196087 	int	broken_pipe = 0;
527d29b2c44Sab196087 
528d29b2c44Sab196087 #if	defined(lint)
529d29b2c44Sab196087 	/*
530d29b2c44Sab196087 	 * The lml argument is only meaningful for diagnostics sent to ld.so.1.
531d29b2c44Sab196087 	 * Supress the lint error by making a dummy assignment.
532d29b2c44Sab196087 	 */
533d29b2c44Sab196087 	lml = 0;
534d29b2c44Sab196087 #endif
535d29b2c44Sab196087 
536d29b2c44Sab196087 	/*
537d29b2c44Sab196087 	 * If there is a pager process, then use it. Otherwise write
538d29b2c44Sab196087 	 * directly to stdout.
539d29b2c44Sab196087 	 */
540d29b2c44Sab196087 	pager = (state.pager.fptr != NULL);
541d29b2c44Sab196087 	fptr = pager ? state.pager.fptr : stdout;
542d29b2c44Sab196087 
543d29b2c44Sab196087 	va_start(ap, format);
544d29b2c44Sab196087 	errno = 0;
545d29b2c44Sab196087 	err = vfprintf(fptr, format, ap);
546d29b2c44Sab196087 	if (err >= 0)
547d29b2c44Sab196087 		err = fprintf(fptr, MSG_ORIG(MSG_STR_NL));
548d29b2c44Sab196087 
549d29b2c44Sab196087 	/* Did we fail because a child pager process has exited? */
550d29b2c44Sab196087 	broken_pipe = (err < 0) && pager && (errno == EPIPE);
551d29b2c44Sab196087 
552d29b2c44Sab196087 	va_end(ap);
553d29b2c44Sab196087 
554d29b2c44Sab196087 	/*
555d29b2c44Sab196087 	 * On error, we simply issue the error without cleaning up
556d29b2c44Sab196087 	 * the pager process. The message code handles that as a standard
557d29b2c44Sab196087 	 * part of error processing.
558d29b2c44Sab196087 	 *
559d29b2c44Sab196087 	 * We handle failure due to an exited pager process differently
560d29b2c44Sab196087 	 * than a normal error, because it is usually due to the user
561d29b2c44Sab196087 	 * intentionally telling it to.
562d29b2c44Sab196087 	 */
563d29b2c44Sab196087 	if (err < 0) {
564d29b2c44Sab196087 		if (broken_pipe)
565d29b2c44Sab196087 			elfedit_msg(ELFEDIT_MSG_QUIET, MSG_ORIG(MSG_STR_NULL));
566d29b2c44Sab196087 		else
567d29b2c44Sab196087 			elfedit_msg(ELFEDIT_MSG_ERR, MSG_INTL(MSG_ERR_PRINTF));
568d29b2c44Sab196087 	}
569d29b2c44Sab196087 }
570d29b2c44Sab196087 
571d29b2c44Sab196087 
572d29b2c44Sab196087 /*
573d29b2c44Sab196087  * Write raw bytes of text in a manner similar to fwrite().
574d29b2c44Sab196087  * Uses the pager process if one has been started, or
575d29b2c44Sab196087  * stdout otherwise.
576d29b2c44Sab196087  */
577d29b2c44Sab196087 void
elfedit_write(const void * ptr,size_t size)578d29b2c44Sab196087 elfedit_write(const void *ptr, size_t size)
579d29b2c44Sab196087 {
580d29b2c44Sab196087 	FILE	*fptr;
581d29b2c44Sab196087 	int	err;
582d29b2c44Sab196087 
583d29b2c44Sab196087 	/*
584d29b2c44Sab196087 	 * If there is a pager process, then use it. Otherwise write
585d29b2c44Sab196087 	 * directly to stdout.
586d29b2c44Sab196087 	 */
587d29b2c44Sab196087 	fptr = (state.pager.fptr == NULL) ? stdout : state.pager.fptr;
588d29b2c44Sab196087 
589d29b2c44Sab196087 	if (fwrite(ptr, 1, size, fptr) != size) {
590d29b2c44Sab196087 		err = errno;
591d29b2c44Sab196087 		elfedit_msg(ELFEDIT_MSG_ERR, MSG_INTL(MSG_ERR_FWRITE),
592d29b2c44Sab196087 		    strerror(err));
593d29b2c44Sab196087 	}
594d29b2c44Sab196087 }
595d29b2c44Sab196087 
596d29b2c44Sab196087 
597d29b2c44Sab196087 /*
598cce0e03bSab196087  * Convert the NULL terminated string to the form used by the C
599c6c9aed4Sab196087  * language to represent literal strings. See conv_str_to_c_literal()
600c6c9aed4Sab196087  * for details.
601c6c9aed4Sab196087  *
602c6c9aed4Sab196087  * This routine differs from conv_str_to_c_literal() in two ways:
603c6c9aed4Sab196087  *	1) String is NULL terminated instead of counted
604c6c9aed4Sab196087  *	2) Signature of outfunc
605cce0e03bSab196087  *
606cce0e03bSab196087  * entry:
607cce0e03bSab196087  *	str - String to be processed
608cce0e03bSab196087  *	outfunc - Function to be called to move output characters. Note
609cce0e03bSab196087  *		that this function has the same signature as elfedit_write(),
610cce0e03bSab196087  *		and that function can be used to write the characters to
611cce0e03bSab196087  *		the output.
612cce0e03bSab196087  *
613cce0e03bSab196087  * exit:
614cce0e03bSab196087  *	The string has been processed, with the resulting data passed
615cce0e03bSab196087  *	to outfunc for processing.
616cce0e03bSab196087  */
617c6c9aed4Sab196087 static void
elfedit_str_to_c_literal_cb(const void * ptr,size_t size,void * uvalue)618c6c9aed4Sab196087 elfedit_str_to_c_literal_cb(const void *ptr, size_t size, void *uvalue)
619c6c9aed4Sab196087 {
620c6c9aed4Sab196087 	elfedit_write_func_t *outfunc = (elfedit_write_func_t *)uvalue;
621c6c9aed4Sab196087 
622c6c9aed4Sab196087 	(* outfunc)(ptr, size);
623c6c9aed4Sab196087 
624c6c9aed4Sab196087 }
625cce0e03bSab196087 void
elfedit_str_to_c_literal(const char * str,elfedit_write_func_t * outfunc)626cce0e03bSab196087 elfedit_str_to_c_literal(const char *str, elfedit_write_func_t *outfunc)
627cce0e03bSab196087 {
628c6c9aed4Sab196087 	conv_str_to_c_literal(str, strlen(str),
629c6c9aed4Sab196087 	    elfedit_str_to_c_literal_cb, (void *) outfunc);
630cce0e03bSab196087 }
631cce0e03bSab196087 
632cce0e03bSab196087 
633cce0e03bSab196087 /*
634d29b2c44Sab196087  * Wrappers on malloc() and realloc() that check the result for success
635d29b2c44Sab196087  * and issue an error if not. The caller can use the result of these
636d29b2c44Sab196087  * functions without checking for a NULL pointer, as we do not return to
637d29b2c44Sab196087  * the caller in the failure case.
638d29b2c44Sab196087  */
639d29b2c44Sab196087 void *
elfedit_malloc(const char * item_name,size_t size)640d29b2c44Sab196087 elfedit_malloc(const char *item_name, size_t size)
641d29b2c44Sab196087 {
642d29b2c44Sab196087 	void *m;
643d29b2c44Sab196087 
644d29b2c44Sab196087 	m = malloc(size);
645d29b2c44Sab196087 	if (m == NULL) {
646d29b2c44Sab196087 		int err = errno;
647d29b2c44Sab196087 		elfedit_msg(ELFEDIT_MSG_ERR, MSG_INTL(MSG_ERR_MALLOC),
648d29b2c44Sab196087 		    item_name, strerror(err));
649d29b2c44Sab196087 	}
650d29b2c44Sab196087 
651d29b2c44Sab196087 	return (m);
652d29b2c44Sab196087 }
653d29b2c44Sab196087 
654d29b2c44Sab196087 void *
elfedit_realloc(const char * item_name,void * ptr,size_t size)655d29b2c44Sab196087 elfedit_realloc(const char *item_name, void *ptr, size_t size)
656d29b2c44Sab196087 {
657d29b2c44Sab196087 	void *m;
658d29b2c44Sab196087 
659d29b2c44Sab196087 	m = realloc(ptr, size);
660d29b2c44Sab196087 	if (m == NULL) {
661d29b2c44Sab196087 		int err = errno;
662d29b2c44Sab196087 		elfedit_msg(ELFEDIT_MSG_ERR, MSG_INTL(MSG_ERR_MALLOC),
663d29b2c44Sab196087 		    item_name, strerror(err));
664d29b2c44Sab196087 	}
665d29b2c44Sab196087 
666d29b2c44Sab196087 	return (m);
667d29b2c44Sab196087 }
668d29b2c44Sab196087 
669d29b2c44Sab196087 
670d29b2c44Sab196087 /*
671d29b2c44Sab196087  * Ensure that the given buffer has room for n bytes of data.
672d29b2c44Sab196087  */
673d29b2c44Sab196087 static void
strbuf_ensure_size(STRBUF * str,size_t size)674d29b2c44Sab196087 strbuf_ensure_size(STRBUF *str, size_t size)
675d29b2c44Sab196087 {
676d29b2c44Sab196087 #define	INITIAL_STR_ALLOC 128
677d29b2c44Sab196087 
678d29b2c44Sab196087 	size_t n;
679d29b2c44Sab196087 
680d29b2c44Sab196087 	n = (str->n == 0) ? INITIAL_STR_ALLOC : str->n;
681d29b2c44Sab196087 	while (size > n)	/* Double buffer until string fits */
682d29b2c44Sab196087 		n *= 2;
683d29b2c44Sab196087 	if (n != str->n) {		/* Alloc new string buffer if needed */
684d29b2c44Sab196087 		str->buf = elfedit_realloc(MSG_INTL(MSG_ALLOC_UCMDSTR),
685d29b2c44Sab196087 		    str->buf, n);
686d29b2c44Sab196087 		str->n = n;
687d29b2c44Sab196087 	}
688d29b2c44Sab196087 
689d29b2c44Sab196087 #undef	INITIAL_STR_ALLOC
690d29b2c44Sab196087 }
691d29b2c44Sab196087 
692d29b2c44Sab196087 
693d29b2c44Sab196087 /*
694d29b2c44Sab196087  * Extract the argument/option information for the next item referenced
695d29b2c44Sab196087  * by optarg, and advance the pointer to the next item.
696d29b2c44Sab196087  *
697d29b2c44Sab196087  * entry:
698d29b2c44Sab196087  *	optarg - Address of pointer to argument or option array
699d29b2c44Sab196087  *	item - Struct to be filled in.
700d29b2c44Sab196087  *
701d29b2c44Sab196087  * exit:
702d29b2c44Sab196087  *	The item block has been filled in with the information for
703d29b2c44Sab196087  *	the next item in the optarg array. *optarg has been advanced
704d29b2c44Sab196087  *	to the next item.
705d29b2c44Sab196087  */
706d29b2c44Sab196087 void
elfedit_next_optarg(elfedit_cmd_optarg_t ** optarg,elfedit_optarg_item_t * item)707d29b2c44Sab196087 elfedit_next_optarg(elfedit_cmd_optarg_t **optarg, elfedit_optarg_item_t *item)
708d29b2c44Sab196087 {
709d29b2c44Sab196087 	/*
710d29b2c44Sab196087 	 * Array of inheritable options/arguments. Indexed by one less
711d29b2c44Sab196087 	 * than the corresponding ELFEDIT_STDOA_ value.
712d29b2c44Sab196087 	 */
713d29b2c44Sab196087 	static const elfedit_optarg_item_t stdoa[] = {
714d29b2c44Sab196087 		/* ELFEDIT_STDOA_O */
715d29b2c44Sab196087 		{ MSG_ORIG(MSG_STR_MINUS_O), MSG_ORIG(MSG_STR_OUTSTYLE),
716d29b2c44Sab196087 		    /* MSG_INTL(MSG_STDOA_OPTDESC_O) */
717d29b2c44Sab196087 		    (elfedit_i18nhdl_t)MSG_STDOA_OPTDESC_O,
718d29b2c44Sab196087 		    ELFEDIT_CMDOA_F_VALUE },
719d29b2c44Sab196087 
720d29b2c44Sab196087 		/* ELFEDIT_STDOA_AND */
721d29b2c44Sab196087 		{ MSG_ORIG(MSG_STR_MINUS_AND), NULL,
722d29b2c44Sab196087 		    /* MSG_INTL(MSG_STDOA_OPTDESC_AND) */
723d29b2c44Sab196087 		    (elfedit_i18nhdl_t)MSG_STDOA_OPTDESC_AND, 0 },
724d29b2c44Sab196087 
725d29b2c44Sab196087 		/* ELFEDIT_STDOA_CMP */
726d29b2c44Sab196087 		{ MSG_ORIG(MSG_STR_MINUS_CMP), NULL,
727d29b2c44Sab196087 		    /* MSG_INTL(MSG_STDOA_OPTDESC_CMP) */
728d29b2c44Sab196087 		    (elfedit_i18nhdl_t)MSG_STDOA_OPTDESC_CMP, 0 },
729d29b2c44Sab196087 
730d29b2c44Sab196087 		/* ELFEDIT_STDOA_OR */
731d29b2c44Sab196087 		{ MSG_ORIG(MSG_STR_MINUS_OR), NULL,
732d29b2c44Sab196087 		    /* MSG_INTL(MSG_STDOA_OPTDESC_OR) */
733d29b2c44Sab196087 		    (elfedit_i18nhdl_t)MSG_STDOA_OPTDESC_OR, 0 },
734d29b2c44Sab196087 	};
735d29b2c44Sab196087 
736d29b2c44Sab196087 	elfedit_cmd_optarg_t *oa;
737d29b2c44Sab196087 
738d29b2c44Sab196087 
739d29b2c44Sab196087 	/* Grab first item, advance the callers pointer over it */
740d29b2c44Sab196087 	oa = (*optarg)++;
741d29b2c44Sab196087 
742d29b2c44Sab196087 	if (oa->oa_flags & ELFEDIT_CMDOA_F_INHERIT) {
743d29b2c44Sab196087 		/* Values are pre-chewed in the stdoa array above */
744d29b2c44Sab196087 		*item = stdoa[((uintptr_t)oa->oa_name) - 1];
745d29b2c44Sab196087 
746d29b2c44Sab196087 		/*
747d29b2c44Sab196087 		 * Set the inherited flag so that elfedit_optarg_helpstr()
748d29b2c44Sab196087 		 * can tell who is responsible for translating the help string.
749d29b2c44Sab196087 		 */
750d29b2c44Sab196087 		item->oai_flags |= ELFEDIT_CMDOA_F_INHERIT;
751d29b2c44Sab196087 	} else {	/* Non-inherited item */
752d29b2c44Sab196087 		item->oai_name = oa->oa_name;
753d29b2c44Sab196087 		if ((oa->oa_flags & ELFEDIT_CMDOA_F_VALUE) != 0) {
754d29b2c44Sab196087 			item->oai_vname = oa[1].oa_name;
755d29b2c44Sab196087 
756d29b2c44Sab196087 			/* Advance users pointer past value element */
757d29b2c44Sab196087 			(*optarg)++;
758d29b2c44Sab196087 		} else {
759d29b2c44Sab196087 			item->oai_vname = NULL;
760d29b2c44Sab196087 		}
761d29b2c44Sab196087 		item->oai_help = oa->oa_help;
762d29b2c44Sab196087 		item->oai_flags = oa->oa_flags;
763d29b2c44Sab196087 	}
764d29b2c44Sab196087 
765d29b2c44Sab196087 	/*
766d29b2c44Sab196087 	 * The module determines the idmask and excmask fields whether
767d29b2c44Sab196087 	 * or not inheritance is in play.
768d29b2c44Sab196087 	 */
769d29b2c44Sab196087 	item->oai_idmask = oa->oa_idmask;
770d29b2c44Sab196087 	item->oai_excmask = oa->oa_excmask;
771d29b2c44Sab196087 }
772d29b2c44Sab196087 
773d29b2c44Sab196087 
774d29b2c44Sab196087 
775d29b2c44Sab196087 /*
776d29b2c44Sab196087  * Return the help string for an option/argument item, as returned
777d29b2c44Sab196087  * by elfedit_next_optarg(). This routine handles the details of
778d29b2c44Sab196087  * knowing whether the string is provided by elfedit itself (inherited),
779d29b2c44Sab196087  * or needs to be translated by the module.
780d29b2c44Sab196087  */
781d29b2c44Sab196087 const char *
elfedit_optarg_helpstr(elfeditGC_module_t * mod,elfedit_optarg_item_t * item)782d29b2c44Sab196087 elfedit_optarg_helpstr(elfeditGC_module_t *mod, elfedit_optarg_item_t *item)
783d29b2c44Sab196087 {
784d29b2c44Sab196087 	/*
785d29b2c44Sab196087 	 * The help string from an inherited item comes right out
786d29b2c44Sab196087 	 * of the main elfedit string table.
787d29b2c44Sab196087 	 */
788d29b2c44Sab196087 	if (item->oai_flags & ELFEDIT_CMDOA_F_INHERIT)
789d29b2c44Sab196087 		return (MSG_INTL((Msg) item->oai_help));
790d29b2c44Sab196087 
791d29b2c44Sab196087 	/*
792d29b2c44Sab196087 	 * If the string is defined by the module, then we need to
793d29b2c44Sab196087 	 * have the module translate it for us.
794d29b2c44Sab196087 	 */
795d29b2c44Sab196087 	return ((* mod->mod_i18nhdl_to_str)(item->oai_help));
796d29b2c44Sab196087 }
797d29b2c44Sab196087 
798d29b2c44Sab196087 
799d29b2c44Sab196087 
800d29b2c44Sab196087 /*
801d29b2c44Sab196087  * Used by usage_optarg() to insert a character into the output buffer,
802d29b2c44Sab196087  * advancing the buffer pointer and current column, and reducing the
803d29b2c44Sab196087  * amount of remaining space.
804d29b2c44Sab196087  */
805d29b2c44Sab196087 static void
usage_optarg_insert_ch(int ch,char ** cur,size_t * n,size_t * cur_col)806d29b2c44Sab196087 usage_optarg_insert_ch(int ch, char **cur, size_t *n, size_t *cur_col)
807d29b2c44Sab196087 {
808d29b2c44Sab196087 
809d29b2c44Sab196087 	*(*cur)++ = ch;
810d29b2c44Sab196087 	**cur = '\0';
811d29b2c44Sab196087 	(*n)--;
812d29b2c44Sab196087 	(*cur_col)++;
813d29b2c44Sab196087 }
814d29b2c44Sab196087 
815d29b2c44Sab196087 /*
816d29b2c44Sab196087  * Used by usage_optarg() to insert a string into the output
817d29b2c44Sab196087  * buffer, advancing the buffer pointer and current column, and reducing
818d29b2c44Sab196087  * the amount of remaining space.
819d29b2c44Sab196087  */
820d29b2c44Sab196087 static void
usage_optarg_insert_str(char ** cur,size_t * n,size_t * cur_col,const char * format,...)821d29b2c44Sab196087 usage_optarg_insert_str(char **cur, size_t *n, size_t *cur_col,
822d29b2c44Sab196087     const char *format, ...)
823d29b2c44Sab196087 {
824d29b2c44Sab196087 	size_t len;
825d29b2c44Sab196087 	va_list args;
826d29b2c44Sab196087 
827d29b2c44Sab196087 	va_start(args, format);
828d29b2c44Sab196087 	len = vsnprintf(*cur, *n, format, args);
829d29b2c44Sab196087 	va_end(args);
830d29b2c44Sab196087 
831d29b2c44Sab196087 	*cur += len;
832d29b2c44Sab196087 	*n -= len;
833d29b2c44Sab196087 	*cur_col += len;
834d29b2c44Sab196087 }
835d29b2c44Sab196087 /*
836d29b2c44Sab196087  * Used by usage_optarg() to insert an optarg item string into the output
837d29b2c44Sab196087  * buffer, advancing the buffer pointer and current column, and reducing
838d29b2c44Sab196087  * the amount of remaining space.
839d29b2c44Sab196087  */
840d29b2c44Sab196087 static void
usage_optarg_insert_item(elfedit_optarg_item_t * item,char ** cur,size_t * n,size_t * cur_col)841d29b2c44Sab196087 usage_optarg_insert_item(elfedit_optarg_item_t *item, char **cur,
842d29b2c44Sab196087     size_t *n, size_t *cur_col)
843d29b2c44Sab196087 {
844d29b2c44Sab196087 	size_t len;
845d29b2c44Sab196087 
846d29b2c44Sab196087 	if (item->oai_flags & ELFEDIT_CMDOA_F_VALUE) {
847d29b2c44Sab196087 		len = snprintf(*cur, *n, MSG_ORIG(MSG_STR_HLPOPTARG2),
848d29b2c44Sab196087 		    item->oai_name, item->oai_vname);
849d29b2c44Sab196087 	} else {
850d29b2c44Sab196087 		len = snprintf(*cur, *n, MSG_ORIG(MSG_STR_HLPOPTARG),
851d29b2c44Sab196087 		    item->oai_name);
852d29b2c44Sab196087 	}
853d29b2c44Sab196087 	*cur += len;
854d29b2c44Sab196087 	*n -= len;
855d29b2c44Sab196087 	*cur_col += len;
856d29b2c44Sab196087 }
857d29b2c44Sab196087 
858d29b2c44Sab196087 
859d29b2c44Sab196087 
860d29b2c44Sab196087 /*
861d29b2c44Sab196087  * Write the options/arguments to the usage string.
862d29b2c44Sab196087  *
863d29b2c44Sab196087  * entry:
864d29b2c44Sab196087  *	main_buf_n - Size of main buffer from which buf and buf_n are
865d29b2c44Sab196087  *		allocated.
866d29b2c44Sab196087  *	buf - Address of pointer to where next item is to be placed.
867d29b2c44Sab196087  *	buf_n - Address of count of remaining bytes in buffer
868d29b2c44Sab196087  *	buf_cur_col - Address of current output column for current line
869d29b2c44Sab196087  *		of generated string.
870d29b2c44Sab196087  *	optarg - Options list
871d29b2c44Sab196087  *	isopt - True if these are options, false for arguments.
872d29b2c44Sab196087  *	wrap_str - String to indent wrapped lines. If NULL, lines
873d29b2c44Sab196087  *		are not wrapped
874d29b2c44Sab196087  */
875d29b2c44Sab196087 static void
usage_optarg(size_t main_buf_n,char ** buf,size_t * buf_n,size_t * buf_cur_col,elfedit_cmd_optarg_t * optarg,int isopt,const char * wrap_str)876d29b2c44Sab196087 usage_optarg(size_t main_buf_n, char **buf, size_t *buf_n, size_t *buf_cur_col,
877d29b2c44Sab196087     elfedit_cmd_optarg_t *optarg, int isopt, const char *wrap_str)
878d29b2c44Sab196087 {
879d29b2c44Sab196087 	/*
880d29b2c44Sab196087 	 * An option can be combined into a simple format if it lacks
881d29b2c44Sab196087 	 * these flags and is only one character in length.
882d29b2c44Sab196087 	 */
883d29b2c44Sab196087 	static const elfedit_cmd_oa_flag_t exflags =
884d29b2c44Sab196087 	    (ELFEDIT_CMDOA_F_VALUE | ELFEDIT_CMDOA_F_MULT);
885d29b2c44Sab196087 
886d29b2c44Sab196087 	/*
887d29b2c44Sab196087 	 * A static buffer, which is grown as needed to accomodate
888d29b2c44Sab196087 	 * the maximum usage string seen.
889d29b2c44Sab196087 	 */
890d29b2c44Sab196087 	static STRBUF simple_str;
891d29b2c44Sab196087 
892d29b2c44Sab196087 	char			*cur = *buf;
893d29b2c44Sab196087 	size_t			n = *buf_n;
894d29b2c44Sab196087 	size_t			cur_col = *buf_cur_col;
895d29b2c44Sab196087 	int			len;
896d29b2c44Sab196087 	int			use_simple = 0;
897d29b2c44Sab196087 	elfedit_optarg_item_t	item;
898d29b2c44Sab196087 	elfedit_cmd_oa_mask_t	optmask = 0;
899d29b2c44Sab196087 	int			use_bkt;
900d29b2c44Sab196087 
901d29b2c44Sab196087 	/*
902d29b2c44Sab196087 	 * If processing options, pull the 1-character ones that don't have
903d29b2c44Sab196087 	 * an associated value and don't have any mutual exclusion issues into
904d29b2c44Sab196087 	 * a single combination string to go at the beginning of the usage.
905d29b2c44Sab196087 	 */
906d29b2c44Sab196087 	if (isopt) {
907d29b2c44Sab196087 		elfedit_cmd_optarg_t *tmp_optarg = optarg;
908d29b2c44Sab196087 		char *s;
909d29b2c44Sab196087 
910d29b2c44Sab196087 		/*
911d29b2c44Sab196087 		 * The simple string is guaranteed to fit in the same
912d29b2c44Sab196087 		 * amount of space reserved for the main buffer.
913d29b2c44Sab196087 		 */
914d29b2c44Sab196087 		strbuf_ensure_size(&simple_str, main_buf_n);
915d29b2c44Sab196087 		s = simple_str.buf;
916d29b2c44Sab196087 		*s++ = ' ';
917d29b2c44Sab196087 		*s++ = '[';
918d29b2c44Sab196087 		*s++ = '-';
919d29b2c44Sab196087 		while (tmp_optarg->oa_name != NULL) {
920d29b2c44Sab196087 			elfedit_next_optarg(&tmp_optarg, &item);
921d29b2c44Sab196087 			if (((item.oai_flags & exflags) == 0) &&
922d29b2c44Sab196087 			    (item.oai_name[2] == '\0') &&
923d29b2c44Sab196087 			    (item.oai_excmask == 0)) {
924d29b2c44Sab196087 				optmask |= item.oai_idmask;
925d29b2c44Sab196087 				*s++ = item.oai_name[1];
926d29b2c44Sab196087 			}
927d29b2c44Sab196087 		}
928d29b2c44Sab196087 
929d29b2c44Sab196087 		/*
930d29b2c44Sab196087 		 * If we found more than one, then finish the string and
931d29b2c44Sab196087 		 * add it. Don't do this for a single option, because
932d29b2c44Sab196087 		 * it looks better in that case if the option shows up
933d29b2c44Sab196087 		 * in alphabetical order rather than being hoisted.
934d29b2c44Sab196087 		 */
935d29b2c44Sab196087 		use_simple = (s > (simple_str.buf + 4));
936d29b2c44Sab196087 		if (use_simple) {
937d29b2c44Sab196087 			*s++ = ']';
938d29b2c44Sab196087 			*s++ = '\0';
939d29b2c44Sab196087 			usage_optarg_insert_str(&cur, &n, &cur_col,
940d29b2c44Sab196087 			    MSG_ORIG(MSG_STR_HLPOPTARG), simple_str.buf);
941d29b2c44Sab196087 		} else {
942d29b2c44Sab196087 			/* Not using it, so reset the cumulative options mask */
943d29b2c44Sab196087 			optmask = 0;
944d29b2c44Sab196087 		}
945d29b2c44Sab196087 	}
946d29b2c44Sab196087 
947d29b2c44Sab196087 	while (optarg->oa_name != NULL) {
948d29b2c44Sab196087 		elfedit_next_optarg(&optarg, &item);
949d29b2c44Sab196087 
950d29b2c44Sab196087 		if (isopt) {
951d29b2c44Sab196087 			/*
952d29b2c44Sab196087 			 * If this is an option that was pulled into the
953d29b2c44Sab196087 			 * combination string above, then skip over it.
954d29b2c44Sab196087 			 */
955d29b2c44Sab196087 			if (use_simple && ((item.oai_flags & exflags) == 0) &&
956d29b2c44Sab196087 			    (item.oai_name[2] == '\0') &&
957d29b2c44Sab196087 			    (item.oai_excmask == 0))
958d29b2c44Sab196087 				continue;
959d29b2c44Sab196087 
960d29b2c44Sab196087 			/*
961d29b2c44Sab196087 			 * If this is a mutual exclusion option that was
962d29b2c44Sab196087 			 * picked up out of order by a previous iteration
963d29b2c44Sab196087 			 * of this loop, then skip over it.
964d29b2c44Sab196087 			 */
965d29b2c44Sab196087 			if ((optmask & item.oai_idmask) != 0)
966d29b2c44Sab196087 				continue;
967d29b2c44Sab196087 
968d29b2c44Sab196087 			/* Add this item to the accumulating options mask */
969d29b2c44Sab196087 			optmask |= item.oai_idmask;
970d29b2c44Sab196087 		}
971d29b2c44Sab196087 
972d29b2c44Sab196087 		/* Wrap line, or insert blank separator */
973d29b2c44Sab196087 		if ((wrap_str != NULL) && (cur_col > USAGE_WRAP_COL)) {
974d29b2c44Sab196087 			len = snprintf(cur, n, MSG_ORIG(MSG_FMT_WRAPUSAGE),
975d29b2c44Sab196087 			    wrap_str);
976d29b2c44Sab196087 			cur += len;
977d29b2c44Sab196087 			n -= len;
978d29b2c44Sab196087 			cur_col = len - 1;   /* Don't count the newline */
979d29b2c44Sab196087 		} else {
980d29b2c44Sab196087 			usage_optarg_insert_ch(' ', &cur, &n, &cur_col);
981d29b2c44Sab196087 		}
982d29b2c44Sab196087 
983d29b2c44Sab196087 		use_bkt = (item.oai_flags & ELFEDIT_CMDOA_F_OPT) || isopt;
984d29b2c44Sab196087 		if (use_bkt)
985d29b2c44Sab196087 			usage_optarg_insert_ch('[', &cur, &n, &cur_col);
986d29b2c44Sab196087 
987d29b2c44Sab196087 		/* Add the item to the buffer */
988d29b2c44Sab196087 		usage_optarg_insert_item(&item, &cur, &n, &cur_col);
989d29b2c44Sab196087 
990d29b2c44Sab196087 		/*
991d29b2c44Sab196087 		 * If this item has a non-zero mutual exclusion mask,
992d29b2c44Sab196087 		 * then look for the other items and display them all
993d29b2c44Sab196087 		 * together with alternation (|). Note that plain arguments
994d29b2c44Sab196087 		 * cannot have a non-0 exclusion mask, so this is
995d29b2c44Sab196087 		 * effectively options-only (isopt != 0).
996d29b2c44Sab196087 		 */
997d29b2c44Sab196087 		if (item.oai_excmask != 0) {
998d29b2c44Sab196087 			elfedit_cmd_optarg_t *tmp_optarg = optarg;
999d29b2c44Sab196087 			elfedit_optarg_item_t tmp_item;
1000d29b2c44Sab196087 
1001d29b2c44Sab196087 			/*
1002d29b2c44Sab196087 			 * When showing alternation, elipses for multiple
1003d29b2c44Sab196087 			 * copies need to appear inside the [] brackets.
1004d29b2c44Sab196087 			 */
1005d29b2c44Sab196087 			if (item.oai_flags & ELFEDIT_CMDOA_F_MULT)
1006d29b2c44Sab196087 				usage_optarg_insert_str(&cur, &n, &cur_col,
1007d29b2c44Sab196087 				    MSG_ORIG(MSG_STR_ELIPSES));
1008d29b2c44Sab196087 
1009d29b2c44Sab196087 
1010d29b2c44Sab196087 			while (tmp_optarg->oa_name != NULL) {
1011d29b2c44Sab196087 				elfedit_next_optarg(&tmp_optarg, &tmp_item);
1012d29b2c44Sab196087 				if ((item.oai_excmask & tmp_item.oai_idmask) ==
1013d29b2c44Sab196087 				    0)
1014d29b2c44Sab196087 					continue;
1015d29b2c44Sab196087 				usage_optarg_insert_str(&cur, &n, &cur_col,
1016d29b2c44Sab196087 				    MSG_ORIG(MSG_STR_SP_BAR_SP));
1017d29b2c44Sab196087 				usage_optarg_insert_item(&tmp_item,
1018d29b2c44Sab196087 				    &cur, &n, &cur_col);
1019d29b2c44Sab196087 
1020d29b2c44Sab196087 				/*
1021d29b2c44Sab196087 				 * Add it to the mask of seen options.
1022d29b2c44Sab196087 				 * This will keep us from showing it twice.
1023d29b2c44Sab196087 				 */
1024d29b2c44Sab196087 				optmask |= tmp_item.oai_idmask;
1025d29b2c44Sab196087 			}
1026d29b2c44Sab196087 		}
1027d29b2c44Sab196087 		if (use_bkt)
1028d29b2c44Sab196087 			usage_optarg_insert_ch(']', &cur, &n, &cur_col);
1029d29b2c44Sab196087 
1030d29b2c44Sab196087 		/*
1031d29b2c44Sab196087 		 * If alternation was not shown above (non-zero exclusion mask)
1032d29b2c44Sab196087 		 * then the elipses for multiple copies are shown outside
1033d29b2c44Sab196087 		 * any [] brackets.
1034d29b2c44Sab196087 		 */
1035d29b2c44Sab196087 		if ((item.oai_excmask == 0) &&
1036d29b2c44Sab196087 		    (item.oai_flags & ELFEDIT_CMDOA_F_MULT))
1037d29b2c44Sab196087 			usage_optarg_insert_str(&cur, &n, &cur_col,
1038d29b2c44Sab196087 			    MSG_ORIG(MSG_STR_ELIPSES));
1039d29b2c44Sab196087 
1040d29b2c44Sab196087 	}
1041d29b2c44Sab196087 
1042d29b2c44Sab196087 	*buf = cur;
1043d29b2c44Sab196087 	*buf_n = n;
1044d29b2c44Sab196087 	*buf_cur_col = cur_col;
1045d29b2c44Sab196087 }
1046d29b2c44Sab196087 
1047d29b2c44Sab196087 
1048d29b2c44Sab196087 
1049d29b2c44Sab196087 /*
1050d29b2c44Sab196087  * Format the usage string for a command into a static buffer and
1051d29b2c44Sab196087  * return the pointer to the user. The resultant string is valid
1052d29b2c44Sab196087  * until the next call to this routine, and which point it
1053d29b2c44Sab196087  * will be overwritten or the memory is freed.
1054d29b2c44Sab196087  *
1055d29b2c44Sab196087  * entry:
1056d29b2c44Sab196087  *	mod, cmd - Module and command definitions for command to be described
1057d29b2c44Sab196087  *	wrap_str - NULL, or string to be used to indent when
1058d29b2c44Sab196087  *		lines are wrapped. If NULL, no wrapping is done, and
1059d29b2c44Sab196087  *		all output is on a single line.
1060d29b2c44Sab196087  *	cur_col - Starting column at which the string will be displayed.
1061d29b2c44Sab196087  *		Ignored if wrap_str is NULL.
1062d29b2c44Sab196087  */
1063d29b2c44Sab196087 const char *
elfedit_format_command_usage(elfeditGC_module_t * mod,elfeditGC_cmd_t * cmd,const char * wrap_str,size_t cur_col)1064d29b2c44Sab196087 elfedit_format_command_usage(elfeditGC_module_t *mod, elfeditGC_cmd_t *cmd,
1065d29b2c44Sab196087     const char *wrap_str, size_t cur_col)
1066d29b2c44Sab196087 {
1067d29b2c44Sab196087 
1068d29b2c44Sab196087 	/*
1069d29b2c44Sab196087 	 * A static buffer, which is grown as needed to accomodate
1070d29b2c44Sab196087 	 * the maximum usage string seen.
1071d29b2c44Sab196087 	 */
1072d29b2c44Sab196087 	static STRBUF str;
1073d29b2c44Sab196087 
1074d29b2c44Sab196087 	elfedit_cmd_optarg_t	*optarg;
1075d29b2c44Sab196087 	size_t			len, n, elipses_len;
1076d29b2c44Sab196087 	char			*cur;
1077d29b2c44Sab196087 	elfedit_optarg_item_t	item;
1078d29b2c44Sab196087 
1079d29b2c44Sab196087 	/*
1080d29b2c44Sab196087 	 * Estimate a worst case size for the usage string:
1081d29b2c44Sab196087 	 *	- module name
1082d29b2c44Sab196087 	 *	- lengths of the strings
1083d29b2c44Sab196087 	 *	- every option or argument is enclosed in brackets
1084d29b2c44Sab196087 	 *	- space in between each item, with an alternation (" | ")
1085d29b2c44Sab196087 	 *	- elipses will be displayed with each option and argument
1086d29b2c44Sab196087 	 */
1087d29b2c44Sab196087 	n = strlen(mod->mod_name) + strlen(cmd->cmd_name[0]) + 6;
1088d29b2c44Sab196087 	elipses_len = strlen(MSG_ORIG(MSG_STR_ELIPSES));
1089d29b2c44Sab196087 	if ((optarg = cmd->cmd_opt) != NULL)
1090d29b2c44Sab196087 		while (optarg->oa_name != NULL) {
1091d29b2c44Sab196087 			elfedit_next_optarg(&optarg, &item);
1092d29b2c44Sab196087 			n += strlen(item.oai_name) + 5 + elipses_len;
1093d29b2c44Sab196087 		}
1094d29b2c44Sab196087 	if ((optarg = cmd->cmd_args) != NULL)
1095d29b2c44Sab196087 		while (optarg->oa_name != NULL) {
1096d29b2c44Sab196087 			elfedit_next_optarg(&optarg, &item);
1097d29b2c44Sab196087 			n += strlen(item.oai_name) + 5 + elipses_len;
1098d29b2c44Sab196087 		}
1099d29b2c44Sab196087 	n++;			/* Null termination */
1100d29b2c44Sab196087 
1101d29b2c44Sab196087 	/*
1102d29b2c44Sab196087 	 * If wrapping lines, we insert a newline and then wrap_str
1103d29b2c44Sab196087 	 * every USAGE_WRAP_COL characters.
1104d29b2c44Sab196087 	 */
1105d29b2c44Sab196087 	if (wrap_str != NULL)
1106d29b2c44Sab196087 		n += ((n + USAGE_WRAP_COL) / USAGE_WRAP_COL) *
1107d29b2c44Sab196087 		    (strlen(wrap_str) + 1);
1108d29b2c44Sab196087 
1109d29b2c44Sab196087 	strbuf_ensure_size(&str, n);
1110d29b2c44Sab196087 
1111d29b2c44Sab196087 	/* Command name */
1112d29b2c44Sab196087 	cur = str.buf;
1113d29b2c44Sab196087 	n = str.n;
1114d29b2c44Sab196087 	if (strcmp(mod->mod_name, MSG_ORIG(MSG_MOD_SYS)) == 0)
1115d29b2c44Sab196087 		len = snprintf(cur, n, MSG_ORIG(MSG_FMT_SYSCMD),
1116d29b2c44Sab196087 		    cmd->cmd_name[0]);
1117d29b2c44Sab196087 	else
1118d29b2c44Sab196087 		len = snprintf(cur, n, MSG_ORIG(MSG_FMT_MODCMD),
1119d29b2c44Sab196087 		    mod->mod_name, cmd->cmd_name[0]);
1120d29b2c44Sab196087 	cur += len;
1121d29b2c44Sab196087 	n -= len;
1122d29b2c44Sab196087 	cur_col += len;
1123d29b2c44Sab196087 
1124d29b2c44Sab196087 	if (cmd->cmd_opt != NULL)
1125d29b2c44Sab196087 		usage_optarg(str.n, &cur, &n, &cur_col, cmd->cmd_opt,
1126d29b2c44Sab196087 		    1, wrap_str);
1127d29b2c44Sab196087 	if (cmd->cmd_args != NULL)
1128d29b2c44Sab196087 		usage_optarg(str.n, &cur, &n, &cur_col, cmd->cmd_args,
1129d29b2c44Sab196087 		    0, wrap_str);
1130d29b2c44Sab196087 
1131d29b2c44Sab196087 	return (str.buf);
1132d29b2c44Sab196087 }
1133d29b2c44Sab196087 
1134d29b2c44Sab196087 /*
1135d29b2c44Sab196087  * Wrapper on elfedit_msg() that issues an ELFEDIT_MSG_USAGE
1136d29b2c44Sab196087  * error giving usage information for the command currently
1137d29b2c44Sab196087  * referenced by state.cur_cmd.
1138d29b2c44Sab196087  */
1139d29b2c44Sab196087 void
elfedit_command_usage(void)1140d29b2c44Sab196087 elfedit_command_usage(void)
1141d29b2c44Sab196087 {
1142d29b2c44Sab196087 	elfedit_msg(ELFEDIT_MSG_CMDUSAGE, MSG_INTL(MSG_USAGE_CMD),
1143d29b2c44Sab196087 	    elfedit_format_command_usage(state.cur_cmd->ucmd_mod,
1144d29b2c44Sab196087 	    state.cur_cmd->ucmd_cmd, NULL, 0));
1145d29b2c44Sab196087 }
1146d29b2c44Sab196087 
1147d29b2c44Sab196087 
1148d29b2c44Sab196087 /*
1149d29b2c44Sab196087  * This function allows the loadable modules to get the command line
1150d29b2c44Sab196087  * flags.
1151d29b2c44Sab196087  */
1152d29b2c44Sab196087 elfedit_flag_t
elfedit_flags(void)1153d29b2c44Sab196087 elfedit_flags(void)
1154d29b2c44Sab196087 {
1155d29b2c44Sab196087 	return (state.flags);
1156d29b2c44Sab196087 }
1157d29b2c44Sab196087 
1158d29b2c44Sab196087 /*
1159d29b2c44Sab196087  * This function is used to register a per-command invocation output style
1160d29b2c44Sab196087  * that will momentarily override the global output style for the duration
1161d29b2c44Sab196087  * of the current command. This function must only be called by an
1162d29b2c44Sab196087  * active command.
1163d29b2c44Sab196087  *
1164d29b2c44Sab196087  * entry:
1165d29b2c44Sab196087  *	str - One of the valid strings for the output style
1166d29b2c44Sab196087  */
1167d29b2c44Sab196087 void
elfedit_set_cmd_outstyle(const char * str)1168d29b2c44Sab196087 elfedit_set_cmd_outstyle(const char *str)
1169d29b2c44Sab196087 {
1170d29b2c44Sab196087 	if ((state.cur_cmd != NULL) && (str != NULL)) {
1171d29b2c44Sab196087 		if (elfedit_atooutstyle(str, &state.cur_cmd->ucmd_ostyle) == 0)
1172d29b2c44Sab196087 			elfedit_msg(ELFEDIT_MSG_ERR,
1173d29b2c44Sab196087 			    MSG_INTL(MSG_ERR_BADOSTYLE), str);
1174d29b2c44Sab196087 		state.cur_cmd->ucmd_ostyle_set = 1;
1175d29b2c44Sab196087 	}
1176d29b2c44Sab196087 }
1177d29b2c44Sab196087 
1178d29b2c44Sab196087 /*
1179d29b2c44Sab196087  * This function allows the loadable modules to get the output style.
1180d29b2c44Sab196087  */
1181d29b2c44Sab196087 elfedit_outstyle_t
elfedit_outstyle(void)1182d29b2c44Sab196087 elfedit_outstyle(void)
1183d29b2c44Sab196087 {
1184d29b2c44Sab196087 	/*
1185d29b2c44Sab196087 	 * If there is an active  per-command output style,
1186d29b2c44Sab196087 	 * return it.
1187d29b2c44Sab196087 	 */
1188d29b2c44Sab196087 	if ((state.cur_cmd != NULL) && (state.cur_cmd->ucmd_ostyle_set))
1189d29b2c44Sab196087 		return (state.cur_cmd->ucmd_ostyle);
1190d29b2c44Sab196087 
1191d29b2c44Sab196087 
1192d29b2c44Sab196087 	return (state.outstyle);
1193d29b2c44Sab196087 }
1194d29b2c44Sab196087 
1195d29b2c44Sab196087 /*
1196d29b2c44Sab196087  * Return the command descriptor of the currently executing command.
1197d29b2c44Sab196087  * For use only by the modules or code called by the modules.
1198d29b2c44Sab196087  */
1199d29b2c44Sab196087 elfeditGC_cmd_t *
elfedit_curcmd(void)1200d29b2c44Sab196087 elfedit_curcmd(void)
1201d29b2c44Sab196087 {
1202d29b2c44Sab196087 	return (state.cur_cmd->ucmd_cmd);
1203d29b2c44Sab196087 }
1204d29b2c44Sab196087 
1205d29b2c44Sab196087 /*
1206d29b2c44Sab196087  * Build a dynamically allocated elfedit_obj_state_t struct that
1207d29b2c44Sab196087  * contains a cache of the ELF file contents. This pre-chewed form
1208d29b2c44Sab196087  * is fed to each command, reducing the amount of ELF boilerplate
1209d29b2c44Sab196087  * code each command needs to contain.
1210d29b2c44Sab196087  *
1211d29b2c44Sab196087  * entry:
1212d29b2c44Sab196087  *	file - Name of file to process
1213d29b2c44Sab196087  *
1214d29b2c44Sab196087  * exit:
1215d29b2c44Sab196087  *	Fills state.elf with the necessary information for the open file.
1216d29b2c44Sab196087  *
1217d29b2c44Sab196087  * note: The resulting elfedit_obj_state_t is allocated from a single
1218d29b2c44Sab196087  *	piece of memory, such that a single call to free() suffices
1219d29b2c44Sab196087  *	to release it as well as any memory it references.
1220d29b2c44Sab196087  */
1221d29b2c44Sab196087 static void
init_obj_state(const char * file)1222d29b2c44Sab196087 init_obj_state(const char *file)
1223d29b2c44Sab196087 {
1224d29b2c44Sab196087 	int	fd;
1225d29b2c44Sab196087 	Elf	*elf;
1226d29b2c44Sab196087 	int	open_flag;
1227d29b2c44Sab196087 
1228d29b2c44Sab196087 	/*
1229d29b2c44Sab196087 	 * In readonly mode, we open the file readonly so that it is
1230d29b2c44Sab196087 	 * impossible to modify the file by accident. This also allows
1231d29b2c44Sab196087 	 * us to access readonly files, perhaps in a case where we don't
1232d29b2c44Sab196087 	 * intend to change it.
1233d29b2c44Sab196087 	 *
1234d29b2c44Sab196087 	 * We always use ELF_C_RDWR with elf_begin(), even in a readonly
1235d29b2c44Sab196087 	 * session. This allows us to modify the in-memory image, which
1236d29b2c44Sab196087 	 * can be useful when examining a file, even though we don't intend
1237d29b2c44Sab196087 	 * to modify the on-disk data. The file is not writable in
1238d29b2c44Sab196087 	 * this case, and we don't call elf_update(), so it is safe to do so.
1239d29b2c44Sab196087 	 */
1240d29b2c44Sab196087 	open_flag = ((state.flags & ELFEDIT_F_READONLY) ? O_RDONLY : O_RDWR);
1241d29b2c44Sab196087 	if ((fd = open(file, open_flag)) == -1) {
1242d29b2c44Sab196087 		int err = errno;
1243d29b2c44Sab196087 		elfedit_msg(ELFEDIT_MSG_ERR, MSG_INTL(MSG_ERR_CNTOPNFILE),
1244d29b2c44Sab196087 		    file, strerror(err));
1245d29b2c44Sab196087 	}
1246d29b2c44Sab196087 	(void) elf_version(EV_CURRENT);
1247d29b2c44Sab196087 	elf = elf_begin(fd, ELF_C_RDWR, NULL);
1248d29b2c44Sab196087 	if (elf == NULL) {
1249d29b2c44Sab196087 		(void) close(fd);
1250d29b2c44Sab196087 		elfedit_elferr(file, MSG_ORIG(MSG_ELF_BEGIN));
1251d29b2c44Sab196087 		/*NOTREACHED*/
1252d29b2c44Sab196087 	}
1253d29b2c44Sab196087 
1254d29b2c44Sab196087 	/* We only handle standalone ELF files */
1255d29b2c44Sab196087 	switch (elf_kind(elf)) {
1256d29b2c44Sab196087 	case ELF_K_AR:
1257d29b2c44Sab196087 		(void) close(fd);
1258d29b2c44Sab196087 		elfedit_msg(ELFEDIT_MSG_ERR, MSG_INTL(MSG_ERR_NOAR), file);
1259d29b2c44Sab196087 		break;
1260d29b2c44Sab196087 	case ELF_K_ELF:
1261d29b2c44Sab196087 		break;
1262d29b2c44Sab196087 	default:
1263d29b2c44Sab196087 		(void) close(fd);
1264d29b2c44Sab196087 		elfedit_msg(ELFEDIT_MSG_ERR, MSG_INTL(MSG_ERR_UNRECELFFILE),
1265d29b2c44Sab196087 		    file);
1266d29b2c44Sab196087 		break;
1267d29b2c44Sab196087 	}
1268d29b2c44Sab196087 
1269d29b2c44Sab196087 	/*
1270d29b2c44Sab196087 	 * Tell libelf that we take responsibility for object layout.
1271d29b2c44Sab196087 	 * Otherwise, it will compute "proper" values for layout and
1272d29b2c44Sab196087 	 * alignment fields, and these values can overwrite the values
1273d29b2c44Sab196087 	 * set in the elfedit session. We are modifying existing
1274d29b2c44Sab196087 	 * objects --- the layout concerns have already been dealt
1275d29b2c44Sab196087 	 * with when the object was built.
1276d29b2c44Sab196087 	 */
1277d29b2c44Sab196087 	(void) elf_flagelf(elf, ELF_C_SET, ELF_F_LAYOUT);
1278d29b2c44Sab196087 
1279d29b2c44Sab196087 	/* Fill in state.elf.obj_state */
1280d29b2c44Sab196087 	state.elf.elfclass = gelf_getclass(elf);
1281d29b2c44Sab196087 	switch (state.elf.elfclass) {
1282d29b2c44Sab196087 	case ELFCLASS32:
1283d29b2c44Sab196087 		elfedit32_init_obj_state(file, fd, elf);
1284d29b2c44Sab196087 		break;
1285d29b2c44Sab196087 	case ELFCLASS64:
1286d29b2c44Sab196087 		elfedit64_init_obj_state(file, fd, elf);
1287d29b2c44Sab196087 		break;
1288d29b2c44Sab196087 	default:
1289d29b2c44Sab196087 		(void) close(fd);
1290d29b2c44Sab196087 		elfedit_msg(ELFEDIT_MSG_ERR, MSG_INTL(MSG_ERR_BADELFCLASS),
1291d29b2c44Sab196087 		    file);
1292d29b2c44Sab196087 		break;
1293d29b2c44Sab196087 	}
1294d29b2c44Sab196087 }
1295d29b2c44Sab196087 
1296d29b2c44Sab196087 
1297cce0e03bSab196087 #ifdef DEBUG_MODULE_LIST
1298d29b2c44Sab196087 /*
1299d29b2c44Sab196087  * Debug routine. Dump the module list to stdout.
1300d29b2c44Sab196087  */
1301d29b2c44Sab196087 static void
dbg_module_list(char * title)1302d29b2c44Sab196087 dbg_module_list(char *title)
1303d29b2c44Sab196087 {
1304d29b2c44Sab196087 	MODLIST_T *m;
1305d29b2c44Sab196087 
1306d29b2c44Sab196087 	printf("<MODULE LIST: %s>\n", title);
1307d29b2c44Sab196087 	for (m = state.modlist; m != NULL; m = m->next) {
1308d29b2c44Sab196087 		printf("Module: >%s<\n", m->mod->mod_name);
1309d29b2c44Sab196087 		printf("    hdl:  %llx\n", m->dl_hdl);
1310d29b2c44Sab196087 		printf("    path: >%s<\n", m->path ? m->path : "<builtin>");
1311d29b2c44Sab196087 	}
1312d29b2c44Sab196087 	printf("<END OF MODULE LIST>\n");
1313d29b2c44Sab196087 }
1314d29b2c44Sab196087 #endif
1315d29b2c44Sab196087 
1316d29b2c44Sab196087 
1317d29b2c44Sab196087 /*
1318d29b2c44Sab196087  * Search the module list for the named module.
1319d29b2c44Sab196087  *
1320d29b2c44Sab196087  * entry:
1321d29b2c44Sab196087  *	name - Name of module to find
1322d29b2c44Sab196087  *	insdef - Address of variable to receive address of predecessor
1323d29b2c44Sab196087  *		node to the desired one.
1324d29b2c44Sab196087  *
1325d29b2c44Sab196087  * exit:
1326d29b2c44Sab196087  *	If the module is it is found, this routine returns the pointer to
1327d29b2c44Sab196087  *	its MODLIST_T structure. *insdef references the predecessor node, or
1328d29b2c44Sab196087  *	is NULL if the found item is at the head of the list.
1329d29b2c44Sab196087  *
1330d29b2c44Sab196087  *	If the module is not found, NULL is returned. *insdef references
1331d29b2c44Sab196087  *	the predecessor node of the position where an entry for this module
1332d29b2c44Sab196087  *	would be placed, or NULL if it would go at the beginning.
1333d29b2c44Sab196087  */
1334d29b2c44Sab196087 static MODLIST_T *
module_loaded(const char * name,MODLIST_T ** insdef)1335d29b2c44Sab196087 module_loaded(const char *name, MODLIST_T **insdef)
1336d29b2c44Sab196087 {
1337d29b2c44Sab196087 	MODLIST_T	*moddef;
1338d29b2c44Sab196087 	int		cmp;
1339d29b2c44Sab196087 
1340d29b2c44Sab196087 	*insdef = NULL;
1341d29b2c44Sab196087 	moddef = state.modlist;
1342d29b2c44Sab196087 	if (moddef != NULL) {
1343d29b2c44Sab196087 		cmp = strcasecmp(name, moddef->ml_mod->mod_name);
1344d29b2c44Sab196087 		if (cmp == 0) {		/* Desired module is first in list */
1345d29b2c44Sab196087 			return (moddef);
1346d29b2c44Sab196087 		} else if (cmp > 0) {	/* cmp > 0: Insert in middle/end */
1347d29b2c44Sab196087 			*insdef = moddef;
1348d29b2c44Sab196087 			moddef = moddef->ml_next;
1349d29b2c44Sab196087 			cmp = -1;
1350d29b2c44Sab196087 			while (moddef && (cmp < 0)) {
1351d29b2c44Sab196087 				cmp = strcasecmp(moddef->ml_mod->mod_name,
1352d29b2c44Sab196087 				    name);
1353d29b2c44Sab196087 				if (cmp == 0)
1354d29b2c44Sab196087 					return (moddef);
1355d29b2c44Sab196087 				if (cmp < 0) {
1356d29b2c44Sab196087 					*insdef = moddef;
1357d29b2c44Sab196087 					moddef = (*insdef)->ml_next;
1358d29b2c44Sab196087 				}
1359d29b2c44Sab196087 			}
1360d29b2c44Sab196087 		}
1361d29b2c44Sab196087 	}
1362d29b2c44Sab196087 
1363d29b2c44Sab196087 	return (NULL);
1364d29b2c44Sab196087 }
1365d29b2c44Sab196087 
1366d29b2c44Sab196087 
1367d29b2c44Sab196087 /*
1368d29b2c44Sab196087  * Determine if a file is a sharable object based on its file path.
1369d29b2c44Sab196087  * If path ends in a .so, followed optionally by a period and 1 or more
1370d29b2c44Sab196087  * digits, we say that it is and return a pointer to the first character
1371d29b2c44Sab196087  * of the suffix. Otherwise NULL is returned.
1372d29b2c44Sab196087  */
1373d29b2c44Sab196087 static const char *
path_is_so(const char * path)1374d29b2c44Sab196087 path_is_so(const char *path)
1375d29b2c44Sab196087 {
1376d29b2c44Sab196087 	int		dotso_len;
1377d29b2c44Sab196087 	const char	*tail;
1378d29b2c44Sab196087 	size_t		len;
1379d29b2c44Sab196087 
1380d29b2c44Sab196087 	len = strlen(path);
1381d29b2c44Sab196087 	if (len == 0)
1382d29b2c44Sab196087 		return (NULL);
1383d29b2c44Sab196087 	tail = path + len;
1384d29b2c44Sab196087 	if (isdigit(*(tail - 1))) {
1385d29b2c44Sab196087 		while ((tail > path) && isdigit(*(tail - 1)))
1386d29b2c44Sab196087 			tail--;
1387d29b2c44Sab196087 		if ((tail <= path) || (*tail != '.'))
1388d29b2c44Sab196087 			return (NULL);
1389d29b2c44Sab196087 	}
1390d29b2c44Sab196087 	dotso_len = strlen(MSG_ORIG(MSG_STR_DOTSO));
1391d29b2c44Sab196087 	if ((tail - path) < dotso_len)
1392d29b2c44Sab196087 		return (NULL);
1393d29b2c44Sab196087 	tail -= dotso_len;
1394d29b2c44Sab196087 	if (strncmp(tail, MSG_ORIG(MSG_STR_DOTSO), dotso_len) == 0)
1395d29b2c44Sab196087 		return (tail);
1396d29b2c44Sab196087 
1397d29b2c44Sab196087 	return (NULL);
1398d29b2c44Sab196087 }
1399d29b2c44Sab196087 
1400d29b2c44Sab196087 
1401d29b2c44Sab196087 /*
1402d29b2c44Sab196087  * Locate the start of the unsuffixed file name within path. Returns pointer
1403d29b2c44Sab196087  * to first character of that name in path.
1404d29b2c44Sab196087  *
1405d29b2c44Sab196087  * entry:
1406d29b2c44Sab196087  *	path - Path to be examined.
1407d29b2c44Sab196087  *	tail - NULL, or pointer to position at tail of path from which
1408d29b2c44Sab196087  *		the search for '/' characters should start. If NULL,
1409d29b2c44Sab196087  *		strlen() is used to locate the end of the string.
1410d29b2c44Sab196087  *	buf - NULL, or buffer to receive a copy of the characters that
1411d29b2c44Sab196087  *		lie between the start of the filename and tail.
1412d29b2c44Sab196087  *	bufsize - sizeof(buf)
1413d29b2c44Sab196087  *
1414d29b2c44Sab196087  * exit:
1415d29b2c44Sab196087  *	The pointer to the first character of the unsuffixed file name
1416d29b2c44Sab196087  *	within path is returned. If buf is non-NULL, the characters
1417d29b2c44Sab196087  *	lying between that point and tail (or the end of path if tail
1418d29b2c44Sab196087  *	is NULL) are copied into buf.
1419d29b2c44Sab196087  */
1420d29b2c44Sab196087 static const char *
elfedit_basename(const char * path,const char * tail,char * buf,size_t bufsiz)1421d29b2c44Sab196087 elfedit_basename(const char *path, const char *tail, char *buf, size_t bufsiz)
1422d29b2c44Sab196087 {
1423d29b2c44Sab196087 	const char 	*s;
1424d29b2c44Sab196087 
1425d29b2c44Sab196087 	if (tail == NULL)
1426d29b2c44Sab196087 		tail = path + strlen(path);
1427d29b2c44Sab196087 	s = tail;
1428d29b2c44Sab196087 	while ((s > path) && (*(s - 1) != '/'))
1429d29b2c44Sab196087 		s--;
1430d29b2c44Sab196087 	if (buf != NULL)
1431d29b2c44Sab196087 		elfedit_strnbcpy(buf, s, tail - s, bufsiz);
1432d29b2c44Sab196087 	return (s);
1433d29b2c44Sab196087 }
1434d29b2c44Sab196087 
1435d29b2c44Sab196087 
1436d29b2c44Sab196087 /*
1437d29b2c44Sab196087  * Issue an error on behalf of load_module(), taking care to release
1438d29b2c44Sab196087  * resources that routine may have aquired:
1439d29b2c44Sab196087  *
1440d29b2c44Sab196087  * entry:
1441d29b2c44Sab196087  *	moddef - NULL, or a module definition to be released via free()
1442d29b2c44Sab196087  *	dl_hdl - NULL, or a handle to a sharable object to release via
1443d29b2c44Sab196087  *		dlclose().
1444d29b2c44Sab196087  *	dl_path - If dl_hdl is non-NULL, the path to the sharable object
1445d29b2c44Sab196087  *		file that was loaded.
1446d29b2c44Sab196087  *	format - A format string to pass to elfedit_msg(), containing
1447d29b2c44Sab196087  *		no more than (3) %s format codes, and no other format codes.
1448d29b2c44Sab196087  *	[s1-s4] - Strings to pass to elfedit_msg() to satisfy the four
1449d29b2c44Sab196087  *		allowed %s codes in format. Should be set to NULL if the
1450d29b2c44Sab196087  *		format string does not need them.
1451d29b2c44Sab196087  *
1452d29b2c44Sab196087  * note:
1453d29b2c44Sab196087  *	This routine makes a copy of the s1-s4 strings before freeing any
1454d29b2c44Sab196087  *	memory or unmapping the sharable library. It is therefore safe to
1455d29b2c44Sab196087  *	use strings from moddef, or from the sharable library (which will
1456d29b2c44Sab196087  *	be unmapped) to satisfy the other arguments s1-s4.
1457d29b2c44Sab196087  */
1458d29b2c44Sab196087 static void
load_module_err(MODLIST_T * moddef,void * dl_hdl,const char * dl_path,const char * format,const char * s1,const char * s2,const char * s3,const char * s4)1459d29b2c44Sab196087 load_module_err(MODLIST_T *moddef, void *dl_hdl, const char *dl_path,
1460d29b2c44Sab196087     const char *format, const char *s1, const char *s2, const char *s3,
1461d29b2c44Sab196087     const char *s4)
1462d29b2c44Sab196087 {
1463d29b2c44Sab196087 #define	SCRBUFSIZE (PATH_MAX + 256)   /* A path, plus some extra */
1464d29b2c44Sab196087 
1465d29b2c44Sab196087 	char s1_buf[SCRBUFSIZE];
1466d29b2c44Sab196087 	char s2_buf[SCRBUFSIZE];
1467d29b2c44Sab196087 	char s3_buf[SCRBUFSIZE];
1468d29b2c44Sab196087 	char s4_buf[SCRBUFSIZE];
1469d29b2c44Sab196087 
1470d29b2c44Sab196087 	/*
1471d29b2c44Sab196087 	 * The caller may provide strings for s1-s3 that are from
1472d29b2c44Sab196087 	 * moddef. If we free moddef, the printf() will die on access
1473d29b2c44Sab196087 	 * to free memory. We could push back on the user and force
1474d29b2c44Sab196087 	 * each call to carefully make copies of such data. However, this
1475d29b2c44Sab196087 	 * is an easy case to miss. Furthermore, this is an error case,
1476d29b2c44Sab196087 	 * and machine efficiency is not the main issue. We therefore make
1477d29b2c44Sab196087 	 * copies of the s1-s3 strings here into auto variables, and then
1478d29b2c44Sab196087 	 * use those copies. The user is freed from worrying about it.
1479d29b2c44Sab196087 	 *
1480d29b2c44Sab196087 	 * We use oversized stack based buffers instead of malloc() to
1481d29b2c44Sab196087 	 * reduce the number of ways that things can go wrong while
1482d29b2c44Sab196087 	 * reporting the error.
1483d29b2c44Sab196087 	 */
1484d29b2c44Sab196087 	if (s1 != NULL)
1485d29b2c44Sab196087 		(void) strlcpy(s1_buf, s1, sizeof (s1_buf));
1486d29b2c44Sab196087 	if (s2 != NULL)
1487d29b2c44Sab196087 		(void) strlcpy(s2_buf, s2, sizeof (s2_buf));
1488d29b2c44Sab196087 	if (s3 != NULL)
1489d29b2c44Sab196087 		(void) strlcpy(s3_buf, s3, sizeof (s3_buf));
1490d29b2c44Sab196087 	if (s4 != NULL)
1491d29b2c44Sab196087 		(void) strlcpy(s4_buf, s4, sizeof (s4_buf));
1492d29b2c44Sab196087 
1493d29b2c44Sab196087 
1494d29b2c44Sab196087 	if (moddef != NULL)
1495d29b2c44Sab196087 		free(moddef);
1496d29b2c44Sab196087 
1497d29b2c44Sab196087 	if ((dl_hdl != NULL) && (dlclose(dl_hdl) != 0))
1498d29b2c44Sab196087 		elfedit_msg(ELFEDIT_MSG_ERR, MSG_INTL(MSG_ERR_CNTDLCLOSE),
1499d29b2c44Sab196087 		    dl_path, dlerror());
1500d29b2c44Sab196087 
1501d29b2c44Sab196087 	elfedit_msg(ELFEDIT_MSG_ERR, format, s1_buf, s2_buf, s3_buf, s4_buf);
1502d29b2c44Sab196087 #undef	SCRBUFSIZE
1503d29b2c44Sab196087 }
1504d29b2c44Sab196087 
1505d29b2c44Sab196087 
1506d29b2c44Sab196087 /*
1507d29b2c44Sab196087  * Load a module sharable object for load_module().
1508d29b2c44Sab196087  *
1509d29b2c44Sab196087  * entry:
1510d29b2c44Sab196087  *	path - Path of file to open
1511d29b2c44Sab196087  *	moddef - If this function issues a non-returning error, it will
1512d29b2c44Sab196087  *		first return the memory referenced by moddef. This argument
1513d29b2c44Sab196087  *		is not used otherwise.
1514d29b2c44Sab196087  *	must_exist - If True, we consider it to be an error if the file given
1515d29b2c44Sab196087  *		by path does not exist. If False, no error is issued
1516d29b2c44Sab196087  *		and a NULL value is quietly returned.
1517d29b2c44Sab196087  *
1518d29b2c44Sab196087  * exit:
1519d29b2c44Sab196087  *	Returns a handle to the loaded object on success, or NULL if no
1520d29b2c44Sab196087  *	file was loaded.
1521d29b2c44Sab196087  */
1522d29b2c44Sab196087 static void *
load_module_dlopen(const char * path,MODLIST_T * moddef,int must_exist)1523d29b2c44Sab196087 load_module_dlopen(const char *path, MODLIST_T *moddef, int must_exist)
1524d29b2c44Sab196087 {
1525d29b2c44Sab196087 	int	fd;
1526d29b2c44Sab196087 	void	*hdl;
1527d29b2c44Sab196087 
1528d29b2c44Sab196087 	/*
1529d29b2c44Sab196087 	 * If the file is not required to exist, and it doesn't, then
1530d29b2c44Sab196087 	 * we want to quietly return without an error.
1531d29b2c44Sab196087 	 */
1532d29b2c44Sab196087 	if (!must_exist) {
1533d29b2c44Sab196087 		fd = open(path, O_RDONLY);
1534d29b2c44Sab196087 		if (fd >= 0) {
1535d29b2c44Sab196087 			(void) close(fd);
1536d29b2c44Sab196087 		} else if (errno == ENOENT) {
1537d29b2c44Sab196087 			return (NULL);
1538d29b2c44Sab196087 		}
1539d29b2c44Sab196087 	}
1540d29b2c44Sab196087 
1541d29b2c44Sab196087 	if ((hdl = dlopen(path, RTLD_LAZY|RTLD_FIRST)) == NULL)
1542d29b2c44Sab196087 		load_module_err(moddef, NULL, NULL,
1543d29b2c44Sab196087 		    MSG_INTL(MSG_ERR_CNTDLOPEN), path, dlerror(), NULL, NULL);
1544d29b2c44Sab196087 
1545d29b2c44Sab196087 	return (hdl);
1546d29b2c44Sab196087 }
1547d29b2c44Sab196087 
1548d29b2c44Sab196087 
1549d29b2c44Sab196087 /*
1550d29b2c44Sab196087  * Sanity check option arguments to prevent common errors. The rest of
1551d29b2c44Sab196087  * elfedit assumes these tests have been done, and does not check
1552d29b2c44Sab196087  * again.
1553d29b2c44Sab196087  */
1554d29b2c44Sab196087 static void
validate_optarg(elfedit_cmd_optarg_t * optarg,int isopt,MODLIST_T * moddef,const char * mod_name,const char * cmd_name,void * dl_hdl,const char * dl_path)1555d29b2c44Sab196087 validate_optarg(elfedit_cmd_optarg_t *optarg, int isopt, MODLIST_T *moddef,
1556d29b2c44Sab196087     const char *mod_name, const char *cmd_name,
1557d29b2c44Sab196087     void *dl_hdl, const char *dl_path)
1558d29b2c44Sab196087 {
1559d29b2c44Sab196087 #define	FAIL(_msg) errmsg = _msg; goto fail
1560d29b2c44Sab196087 
1561d29b2c44Sab196087 	Msg errmsg;
1562d29b2c44Sab196087 	elfedit_cmd_oa_mask_t	optmask = 0;
1563d29b2c44Sab196087 
1564d29b2c44Sab196087 	for (; optarg->oa_name != NULL; optarg++) {
1565d29b2c44Sab196087 		/*
1566d29b2c44Sab196087 		 * If ELFEDIT_CMDOA_F_INHERIT is set:
1567d29b2c44Sab196087 		 *	- oa_name must be a value in the range of
1568d29b2c44Sab196087 		 *		known ELFEDIT_STDOA_ values.
1569d29b2c44Sab196087 		 *	- oa_help must be NULL
1570d29b2c44Sab196087 		 *	- ELFEDIT_CMDOA_F_INHERIT must be the only flag set
1571d29b2c44Sab196087 		 */
1572d29b2c44Sab196087 		if (optarg->oa_flags & ELFEDIT_CMDOA_F_INHERIT) {
1573d29b2c44Sab196087 			if ((((uintptr_t)optarg->oa_name) >
1574d29b2c44Sab196087 			    ELFEDIT_NUM_STDOA) ||
1575d29b2c44Sab196087 			    (optarg->oa_help != 0) ||
1576d29b2c44Sab196087 			    (optarg->oa_flags != ELFEDIT_CMDOA_F_INHERIT))
1577d29b2c44Sab196087 				/*
1578d29b2c44Sab196087 				 * Can't use FAIL --- oa_name is not a valid
1579d29b2c44Sab196087 				 * string, and load_module_err() looks at args.
1580d29b2c44Sab196087 				 */
1581d29b2c44Sab196087 				load_module_err(moddef, dl_hdl, dl_path,
1582d29b2c44Sab196087 				    MSG_INTL(MSG_ERR_BADSTDOA), dl_path,
1583d29b2c44Sab196087 				    mod_name, cmd_name, NULL);
1584d29b2c44Sab196087 			continue;
1585d29b2c44Sab196087 		}
1586d29b2c44Sab196087 
1587d29b2c44Sab196087 		if (isopt) {
1588d29b2c44Sab196087 			/*
1589d29b2c44Sab196087 			 * Option name must start with a '-', and must
1590d29b2c44Sab196087 			 * have at one following character.
1591d29b2c44Sab196087 			 */
1592d29b2c44Sab196087 			if (optarg->oa_name[0] != '-') {
1593d29b2c44Sab196087 				/* MSG_INTL(MSG_ERR_OPT_MODPRE) */
1594d29b2c44Sab196087 				FAIL(MSG_ERR_OPT_MODPRE);
1595d29b2c44Sab196087 			}
1596d29b2c44Sab196087 			if (optarg->oa_name[1] == '\0') {
1597d29b2c44Sab196087 				/* MSG_INTL(MSG_ERR_OPT_MODLEN) */
1598d29b2c44Sab196087 				FAIL(MSG_ERR_OPT_MODLEN);
1599d29b2c44Sab196087 			}
1600d29b2c44Sab196087 
1601d29b2c44Sab196087 			/*
1602d29b2c44Sab196087 			 * oa_idmask must be 0, or it must have a single
1603d29b2c44Sab196087 			 * bit set (a power of 2).oa_excmask must be 0
1604d29b2c44Sab196087 			 * if oa_idmask is 0
1605d29b2c44Sab196087 			 */
1606d29b2c44Sab196087 			if (optarg->oa_idmask == 0) {
1607d29b2c44Sab196087 				if (optarg->oa_excmask != 0) {
1608d29b2c44Sab196087 					/* MSG_INTL(MSG_ERR_OPT_EXCMASKN0) */
1609d29b2c44Sab196087 					FAIL(MSG_ERR_OPT_EXCMASKN0);
1610d29b2c44Sab196087 				}
1611d29b2c44Sab196087 			} else {
1612d29b2c44Sab196087 				if (elfedit_bits_set(optarg->oa_idmask,
1613d29b2c44Sab196087 				    sizeof (optarg->oa_idmask)) != 1) {
1614d29b2c44Sab196087 					/* MSG_INTL(MSG_ERR_OPT_IDMASKPOW2) */
1615d29b2c44Sab196087 					FAIL(MSG_ERR_OPT_IDMASKPOW2);
1616d29b2c44Sab196087 				}
1617d29b2c44Sab196087 
1618d29b2c44Sab196087 				/* Non-zero idmask must be unique */
1619d29b2c44Sab196087 				if ((optarg->oa_idmask & optmask) != 0) {
1620d29b2c44Sab196087 					/* MSG_INTL(MSG_ERR_OPT_IDMASKUNIQ) */
1621d29b2c44Sab196087 					FAIL(MSG_ERR_OPT_IDMASKUNIQ);
1622d29b2c44Sab196087 				}
1623d29b2c44Sab196087 
1624d29b2c44Sab196087 				/* Add this one to the overall mask */
1625d29b2c44Sab196087 				optmask |= optarg->oa_idmask;
1626d29b2c44Sab196087 			}
1627d29b2c44Sab196087 		} else {
1628d29b2c44Sab196087 			/*
1629d29b2c44Sab196087 			 * Argument name cannot start with a'-', and must
1630d29b2c44Sab196087 			 * not be a null string.
1631d29b2c44Sab196087 			 */
1632d29b2c44Sab196087 			if (optarg->oa_name[0] == '-') {
1633d29b2c44Sab196087 				/* MSG_INTL(MSG_ERR_ARG_MODPRE) */
1634d29b2c44Sab196087 				FAIL(MSG_ERR_ARG_MODPRE);
1635d29b2c44Sab196087 			}
1636d29b2c44Sab196087 			if (optarg->oa_name[1] == '\0') {
1637d29b2c44Sab196087 				/* MSG_INTL(MSG_ERR_ARG_MODLEN) */
1638d29b2c44Sab196087 				FAIL(MSG_ERR_ARG_MODLEN);
1639d29b2c44Sab196087 			}
1640d29b2c44Sab196087 
1641d29b2c44Sab196087 
1642d29b2c44Sab196087 			/* oa_idmask and oa_excmask must both be 0 */
1643d29b2c44Sab196087 			if ((optarg->oa_idmask != 0) ||
1644d29b2c44Sab196087 			    (optarg->oa_excmask != 0)) {
1645d29b2c44Sab196087 				/* MSG_INTL(MSG_ERR_ARG_MASKNOT0) */
1646d29b2c44Sab196087 				FAIL(MSG_ERR_ARG_MASKNOT0);
1647d29b2c44Sab196087 			}
1648d29b2c44Sab196087 
1649d29b2c44Sab196087 		}
1650d29b2c44Sab196087 
1651d29b2c44Sab196087 		/*
1652d29b2c44Sab196087 		 * If it takes a value, make sure that we are
1653d29b2c44Sab196087 		 * processing options, because CMDOA_F_VALUE is not
1654d29b2c44Sab196087 		 * allowed for plain arguments. Then check the following
1655d29b2c44Sab196087 		 * item in the list:
1656d29b2c44Sab196087 		 *	- There must be a following item.
1657d29b2c44Sab196087 		 *	- oa_name must be non-NULL. This is the only field
1658d29b2c44Sab196087 		 *		that is used by elfedit.
1659d29b2c44Sab196087 		 *	- oa_help, oa_flags, oa_idmask, and oa_excmask
1660d29b2c44Sab196087 		 *		must be 0.
1661d29b2c44Sab196087 		 */
1662d29b2c44Sab196087 		if (optarg->oa_flags & ELFEDIT_CMDOA_F_VALUE) {
1663d29b2c44Sab196087 			elfedit_cmd_optarg_t *oa1 = optarg + 1;
1664d29b2c44Sab196087 
1665d29b2c44Sab196087 			if (!isopt) {
1666d29b2c44Sab196087 				/* MSG_INTL(MSG_ERR_ARG_CMDOA_VAL) */
1667d29b2c44Sab196087 				FAIL(MSG_ERR_ARG_CMDOA_VAL);
1668d29b2c44Sab196087 			}
1669d29b2c44Sab196087 
1670d29b2c44Sab196087 			if ((optarg + 1)->oa_name == NULL) {
1671d29b2c44Sab196087 				/* MSG_INTL(MSG_ERR_BADMODOPTVAL) */
1672d29b2c44Sab196087 				FAIL(MSG_ERR_BADMODOPTVAL);
1673d29b2c44Sab196087 			}
1674d29b2c44Sab196087 
1675d29b2c44Sab196087 			if (oa1->oa_name == NULL) {
1676d29b2c44Sab196087 				/* MSG_INTL(MSG_ERR_CMDOA_VALNAM) */
1677d29b2c44Sab196087 				FAIL(MSG_ERR_CMDOA_VALNAM);
1678d29b2c44Sab196087 			}
1679d29b2c44Sab196087 			if ((oa1->oa_help != NULL) || (oa1->oa_flags != 0) ||
1680d29b2c44Sab196087 			    (oa1->oa_idmask != 0) || (oa1->oa_excmask != 0)) {
1681d29b2c44Sab196087 				/* MSG_INTL(MSG_ERR_CMDOA_VALNOT0) */
1682d29b2c44Sab196087 				FAIL(MSG_ERR_CMDOA_VALNOT0);
1683d29b2c44Sab196087 			}
1684d29b2c44Sab196087 			optarg++;
1685d29b2c44Sab196087 		}
1686d29b2c44Sab196087 	}
1687d29b2c44Sab196087 
1688d29b2c44Sab196087 
1689d29b2c44Sab196087 	return;
1690d29b2c44Sab196087 
1691d29b2c44Sab196087 fail:
1692d29b2c44Sab196087 	load_module_err(moddef, dl_hdl, dl_path, MSG_INTL(errmsg),
1693d29b2c44Sab196087 	    dl_path, mod_name, cmd_name, optarg->oa_name);
1694d29b2c44Sab196087 }
1695d29b2c44Sab196087 
1696d29b2c44Sab196087 /*
1697d29b2c44Sab196087  * Look up the specified module, loading the module if necessary,
1698d29b2c44Sab196087  * and return its definition, or NULL on failure.
1699d29b2c44Sab196087  *
1700d29b2c44Sab196087  * entry:
1701d29b2c44Sab196087  *	name - Name of module to load. If name contains a '/' character or has
1702d29b2c44Sab196087  *		a ".so" suffix, then it is taken to be an absolute file path,
1703d29b2c44Sab196087  *		and is used directly as is. If name does not contain a '/'
1704d29b2c44Sab196087  *		character, then we look for it against the locations in
1705d29b2c44Sab196087  *		the module path, addint the '.so' suffix, and taking the first
1706d29b2c44Sab196087  *		one we find.
1707d29b2c44Sab196087  *	must_exist - If True, we consider it to be an error if we are unable
1708d29b2c44Sab196087  *		to locate a file to load and the module does not already exist.
1709d29b2c44Sab196087  *		If False, NULL is returned quietly in this case.
1710d29b2c44Sab196087  *	allow_abs - True if absolute paths are allowed. False to disallow
1711d29b2c44Sab196087  *		them.
1712d29b2c44Sab196087  *
1713d29b2c44Sab196087  * note:
1714d29b2c44Sab196087  *	If the path is absolute, then we load the file and take the module
1715d29b2c44Sab196087  *	name from the data returned by its elfedit_init() function. If a
1716d29b2c44Sab196087  *	module of that name is already loaded, it is unloaded and replaced
1717d29b2c44Sab196087  *	with the new one.
1718d29b2c44Sab196087  *
1719d29b2c44Sab196087  *	If the path is non absolute, then we check to see if the module has
1720d29b2c44Sab196087  *	already been loaded, and if so, we return that module definition.
1721d29b2c44Sab196087  *	In this case, nothing new is loaded. If the module has not been loaded,
1722d29b2c44Sab196087  *	we search the path for it and load it. If the module name provided
1723d29b2c44Sab196087  *	by the elfedit_init() function does not match the name of the file,
1724d29b2c44Sab196087  *	an error results.
1725d29b2c44Sab196087  */
1726d29b2c44Sab196087 elfeditGC_module_t *
elfedit_load_module(const char * name,int must_exist,int allow_abs)1727d29b2c44Sab196087 elfedit_load_module(const char *name, int must_exist, int allow_abs)
1728d29b2c44Sab196087 {
1729d29b2c44Sab196087 	elfedit_init_func_t	*init_func;
1730d29b2c44Sab196087 	elfeditGC_module_t	*mod;
1731d29b2c44Sab196087 	MODLIST_T		*moddef, *insdef;
1732d29b2c44Sab196087 	const char		*path;
1733d29b2c44Sab196087 	char			path_buf[PATH_MAX + 1];
1734d29b2c44Sab196087 	void			*hdl;
1735d29b2c44Sab196087 	size_t			i;
1736d29b2c44Sab196087 	int			is_abs_path;
1737d29b2c44Sab196087 	elfeditGC_cmd_t		*cmd;
1738d29b2c44Sab196087 
1739d29b2c44Sab196087 	/*
1740d29b2c44Sab196087 	 * If the name includes a .so suffix, or has any '/' characters,
1741d29b2c44Sab196087 	 * then it is an absolute path that we use as is to load the named
1742d29b2c44Sab196087 	 * file. Otherwise, we iterate over the path, adding the .so suffix
1743d29b2c44Sab196087 	 * and load the first file that matches.
1744d29b2c44Sab196087 	 */
1745d29b2c44Sab196087 	is_abs_path = (path_is_so(name) != NULL) ||
1746d29b2c44Sab196087 	    (name != elfedit_basename(name, NULL, NULL, 0));
1747d29b2c44Sab196087 
1748d29b2c44Sab196087 	if (is_abs_path && !allow_abs)
1749d29b2c44Sab196087 		load_module_err(NULL, NULL, NULL,
1750d29b2c44Sab196087 		    MSG_INTL(MSG_ERR_UNRECMOD), name, NULL, NULL, NULL);
1751d29b2c44Sab196087 
1752d29b2c44Sab196087 	/*
1753d29b2c44Sab196087 	 * If this is a non-absolute path, search for the module already
1754d29b2c44Sab196087 	 * having been loaded, and return it if so.
1755d29b2c44Sab196087 	 */
1756d29b2c44Sab196087 	if (!is_abs_path) {
1757d29b2c44Sab196087 		moddef = module_loaded(name, &insdef);
1758d29b2c44Sab196087 		if (moddef != NULL)
1759d29b2c44Sab196087 			return (moddef->ml_mod);
1760d29b2c44Sab196087 		/*
1761d29b2c44Sab196087 		 * As a result of module_loaded(), insdef now contains the
1762d29b2c44Sab196087 		 * immediate predecessor node for the new one, or NULL if
1763d29b2c44Sab196087 		 * it goes at the front. In the absolute-path case, we take
1764d29b2c44Sab196087 		 * care of this below, after the sharable object is loaded.
1765d29b2c44Sab196087 		 */
1766d29b2c44Sab196087 	}
1767d29b2c44Sab196087 
1768d29b2c44Sab196087 	/*
1769d29b2c44Sab196087 	 * malloc() a module definition block before trying to dlopen().
1770d29b2c44Sab196087 	 * Doing things in the other order can cause the dlopen()'d object
1771d29b2c44Sab196087 	 * to leak: If elfedit_malloc() fails, it can cause a jump to the
1772d29b2c44Sab196087 	 * outer command loop without returning to the caller. Hence,
1773d29b2c44Sab196087 	 * there will be no opportunity to clean up. Allocaing the module
1774d29b2c44Sab196087 	 * first allows us to free it if necessary.
1775d29b2c44Sab196087 	 */
1776d29b2c44Sab196087 	moddef = elfedit_malloc(MSG_INTL(MSG_ALLOC_MODDEF),
1777d29b2c44Sab196087 	    sizeof (*moddef) + PATH_MAX + 1);
1778d29b2c44Sab196087 	moddef->ml_path = ((char *)moddef) + sizeof (*moddef);
1779d29b2c44Sab196087 
1780d29b2c44Sab196087 	if (is_abs_path) {
1781d29b2c44Sab196087 		path = name;
1782d29b2c44Sab196087 		hdl = load_module_dlopen(name, moddef, must_exist);
1783d29b2c44Sab196087 	} else {
1784d29b2c44Sab196087 		hdl = NULL;
1785d29b2c44Sab196087 		path = path_buf;
1786d29b2c44Sab196087 		for (i = 0; i < state.modpath.n; i++) {
1787d29b2c44Sab196087 			if (snprintf(path_buf, sizeof (path_buf),
1788d29b2c44Sab196087 			    MSG_ORIG(MSG_FMT_BLDSOPATH), state.modpath.seg[i],
1789d29b2c44Sab196087 			    name) > sizeof (path_buf))
1790d29b2c44Sab196087 				load_module_err(moddef, NULL, NULL,
1791d29b2c44Sab196087 				    MSG_INTL(MSG_ERR_PATHTOOLONG),
1792d29b2c44Sab196087 				    state.modpath.seg[i], name, NULL, NULL);
1793d29b2c44Sab196087 			hdl = load_module_dlopen(path, moddef, 0);
1794d29b2c44Sab196087 		}
1795d29b2c44Sab196087 		if (must_exist && (hdl == NULL))
1796d29b2c44Sab196087 			load_module_err(moddef, NULL, NULL,
1797d29b2c44Sab196087 			    MSG_INTL(MSG_ERR_UNRECMOD), name, NULL, NULL, NULL);
1798d29b2c44Sab196087 	}
1799d29b2c44Sab196087 
1800d29b2c44Sab196087 	if (hdl == NULL) {
1801d29b2c44Sab196087 		free(moddef);
1802d29b2c44Sab196087 		return (NULL);
1803d29b2c44Sab196087 	}
1804d29b2c44Sab196087 
1805d29b2c44Sab196087 	if (state.elf.elfclass == ELFCLASS32) {
1806d29b2c44Sab196087 		init_func = (elfedit_init_func_t *)
1807d29b2c44Sab196087 		    dlsym(hdl, MSG_ORIG(MSG_STR_ELFEDITINIT32));
1808d29b2c44Sab196087 	} else {
1809d29b2c44Sab196087 		init_func = (elfedit_init_func_t *)
1810d29b2c44Sab196087 		    dlsym(hdl, MSG_ORIG(MSG_STR_ELFEDITINIT64));
1811d29b2c44Sab196087 	}
1812d29b2c44Sab196087 	if (init_func == NULL)
1813d29b2c44Sab196087 		load_module_err(moddef, hdl, path,
1814d29b2c44Sab196087 		    MSG_INTL(MSG_ERR_SONOTMOD), path, NULL, NULL, NULL);
1815d29b2c44Sab196087 
1816d29b2c44Sab196087 	/*
1817d29b2c44Sab196087 	 * Note that the init function will be passing us an
1818d29b2c44Sab196087 	 * elfedit[32|64]_module_t pointer, which we cast to the
1819d29b2c44Sab196087 	 * generic module pointer type in order to be able to manage
1820d29b2c44Sab196087 	 * either type with one set of code.
1821d29b2c44Sab196087 	 */
1822d29b2c44Sab196087 	if (!(mod = (elfeditGC_module_t *)(* init_func)(ELFEDIT_VER_CURRENT)))
1823d29b2c44Sab196087 		load_module_err(moddef, hdl, path,
1824d29b2c44Sab196087 		    MSG_INTL(MSG_ERR_BADMODLOAD), path, NULL, NULL, NULL);
1825d29b2c44Sab196087 
1826d29b2c44Sab196087 	/*
1827d29b2c44Sab196087 	 * Enforce some rules, to help module developers:
1828d29b2c44Sab196087 	 *	- The primary name of a command must not be
1829d29b2c44Sab196087 	 *		the empty string ("").
1830d29b2c44Sab196087 	 *	- Options must start with a '-' followed by at least
1831d29b2c44Sab196087 	 *		one character.
1832d29b2c44Sab196087 	 *	- Arguments and options must be well formed.
1833d29b2c44Sab196087 	 */
1834d29b2c44Sab196087 	for (cmd = mod->mod_cmds; cmd->cmd_func != NULL; cmd++) {
1835d29b2c44Sab196087 		if (**cmd->cmd_name == '\0')
1836d29b2c44Sab196087 			load_module_err(moddef, hdl, path,
1837d29b2c44Sab196087 			    MSG_INTL(MSG_ERR_NULLPRICMDNAM), mod->mod_name,
1838d29b2c44Sab196087 			    NULL, NULL, NULL);
1839d29b2c44Sab196087 
1840d29b2c44Sab196087 		if (cmd->cmd_args != NULL)
1841d29b2c44Sab196087 			validate_optarg(cmd->cmd_args, 0, moddef, mod->mod_name,
1842d29b2c44Sab196087 			    cmd->cmd_name[0], hdl, path);
1843d29b2c44Sab196087 		if (cmd->cmd_opt != NULL)
1844d29b2c44Sab196087 			validate_optarg(cmd->cmd_opt, 1, moddef, mod->mod_name,
1845d29b2c44Sab196087 			    cmd->cmd_name[0], hdl, path);
1846d29b2c44Sab196087 	}
1847d29b2c44Sab196087 
1848d29b2c44Sab196087 	/*
1849d29b2c44Sab196087 	 * Check the name the module provides. How we handle this depends
1850d29b2c44Sab196087 	 * on whether the path is absolute or the result of a path search.
1851d29b2c44Sab196087 	 */
1852d29b2c44Sab196087 	if (is_abs_path) {
1853d29b2c44Sab196087 		MODLIST_T *old_moddef = module_loaded(mod->mod_name, &insdef);
1854d29b2c44Sab196087 
1855d29b2c44Sab196087 		if (old_moddef != NULL) {	/* Replace existing */
1856d29b2c44Sab196087 			free(moddef);		/* Rare case: Don't need it */
1857d29b2c44Sab196087 			/*
1858d29b2c44Sab196087 			 * Be sure we don't unload builtin modules!
1859d29b2c44Sab196087 			 * These have a NULL dl_hdl field.
1860d29b2c44Sab196087 			 */
1861d29b2c44Sab196087 			if (old_moddef->ml_dl_hdl == NULL)
1862d29b2c44Sab196087 				load_module_err(NULL, hdl, path,
1863d29b2c44Sab196087 				    MSG_INTL(MSG_ERR_CNTULSMOD),
1864d29b2c44Sab196087 				    old_moddef->ml_mod->mod_name, NULL,
1865d29b2c44Sab196087 				    NULL, NULL);
1866d29b2c44Sab196087 
1867d29b2c44Sab196087 			/* Unload existing */
1868d29b2c44Sab196087 			if (dlclose(old_moddef->ml_dl_hdl) != 0)
1869d29b2c44Sab196087 				elfedit_msg(ELFEDIT_MSG_ERR,
1870d29b2c44Sab196087 				    MSG_INTL(MSG_ERR_CNTDLCLOSE),
1871d29b2c44Sab196087 				    old_moddef->ml_path, dlerror());
1872d29b2c44Sab196087 			elfedit_msg(ELFEDIT_MSG_DEBUG,
1873d29b2c44Sab196087 			    MSG_INTL(MSG_DEBUG_MODUNLOAD),
1874d29b2c44Sab196087 			    old_moddef->ml_mod->mod_name, old_moddef->ml_path);
1875d29b2c44Sab196087 			old_moddef->ml_mod = mod;
1876d29b2c44Sab196087 			old_moddef->ml_dl_hdl = hdl;
1877d29b2c44Sab196087 			(void) strlcpy((char *)old_moddef->ml_path, path,
1878d29b2c44Sab196087 			    PATH_MAX + 1);
1879d29b2c44Sab196087 			elfedit_msg(ELFEDIT_MSG_DEBUG,
1880d29b2c44Sab196087 			    MSG_INTL(MSG_DEBUG_MODLOAD),
1881d29b2c44Sab196087 			    old_moddef->ml_mod->mod_name, path);
1882d29b2c44Sab196087 			return (old_moddef->ml_mod);
1883d29b2c44Sab196087 		}
1884d29b2c44Sab196087 		/*
1885d29b2c44Sab196087 		 * insdef now contains the insertion point for the absolute
1886d29b2c44Sab196087 		 * path case.
1887d29b2c44Sab196087 		 */
1888d29b2c44Sab196087 	} else {
1889d29b2c44Sab196087 		/* If the names don't match, then error */
1890d29b2c44Sab196087 		if (strcasecmp(name, mod->mod_name) != 0)
1891d29b2c44Sab196087 			load_module_err(moddef, hdl, path,
1892d29b2c44Sab196087 			    MSG_INTL(MSG_ERR_BADMODNAME),
1893d29b2c44Sab196087 			    mod->mod_name, name, path, NULL);
1894d29b2c44Sab196087 	}
1895d29b2c44Sab196087 
1896d29b2c44Sab196087 	/*
1897d29b2c44Sab196087 	 * Link module into the module list. If insdef is NULL,
1898d29b2c44Sab196087 	 * it goes at the head. If insdef is non-NULL, it goes immediately
1899d29b2c44Sab196087 	 * after
1900d29b2c44Sab196087 	 */
1901d29b2c44Sab196087 	if (insdef == NULL) {
1902d29b2c44Sab196087 		moddef->ml_next = state.modlist;
1903d29b2c44Sab196087 		state.modlist = moddef;
1904d29b2c44Sab196087 	} else {
1905d29b2c44Sab196087 		moddef->ml_next = insdef->ml_next;
1906d29b2c44Sab196087 		insdef->ml_next = moddef;
1907d29b2c44Sab196087 	}
1908d29b2c44Sab196087 	moddef->ml_mod = mod;
1909d29b2c44Sab196087 	moddef->ml_dl_hdl = hdl;
1910d29b2c44Sab196087 	(void) strlcpy((char *)moddef->ml_path, path, PATH_MAX + 1);
1911d29b2c44Sab196087 
1912d29b2c44Sab196087 	elfedit_msg(ELFEDIT_MSG_DEBUG, MSG_INTL(MSG_DEBUG_MODLOAD),
1913d29b2c44Sab196087 	    moddef->ml_mod->mod_name, path);
1914d29b2c44Sab196087 
1915d29b2c44Sab196087 	return (moddef->ml_mod);
1916d29b2c44Sab196087 }
1917d29b2c44Sab196087 
1918d29b2c44Sab196087 
1919d29b2c44Sab196087 /*
1920d29b2c44Sab196087  * Unload the specified module
1921d29b2c44Sab196087  */
1922d29b2c44Sab196087 void
elfedit_unload_module(const char * name)1923d29b2c44Sab196087 elfedit_unload_module(const char *name)
1924d29b2c44Sab196087 {
1925d29b2c44Sab196087 	MODLIST_T	*moddef, *insdef;
1926d29b2c44Sab196087 
1927d29b2c44Sab196087 	moddef = module_loaded(name, &insdef);
1928d29b2c44Sab196087 	if (moddef == NULL)
1929d29b2c44Sab196087 		return;
1930d29b2c44Sab196087 
1931d29b2c44Sab196087 	/* Built in modules cannot be unloaded. They have a NULL dl_hdl field */
1932d29b2c44Sab196087 	if (moddef->ml_dl_hdl == NULL)
1933d29b2c44Sab196087 		elfedit_msg(ELFEDIT_MSG_ERR, MSG_INTL(MSG_ERR_CNTULSMOD),
1934d29b2c44Sab196087 		    moddef->ml_mod->mod_name);
1935d29b2c44Sab196087 
1936d29b2c44Sab196087 	/*
1937d29b2c44Sab196087 	 * When we unload it, the name string goes with it. So
1938d29b2c44Sab196087 	 * announce it while we still can without having to make a copy.
1939d29b2c44Sab196087 	 */
1940d29b2c44Sab196087 	elfedit_msg(ELFEDIT_MSG_DEBUG, MSG_INTL(MSG_DEBUG_MODUNLOAD),
1941d29b2c44Sab196087 	    moddef->ml_mod->mod_name, moddef->ml_path);
1942d29b2c44Sab196087 
1943d29b2c44Sab196087 	/*
1944d29b2c44Sab196087 	 * Close it before going further. On failure, we'll jump, and the
1945d29b2c44Sab196087 	 * record will remain in the module list. On success,
1946d29b2c44Sab196087 	 * we'll retain control, and can safely remove it.
1947d29b2c44Sab196087 	 */
1948d29b2c44Sab196087 	if (dlclose(moddef->ml_dl_hdl) != 0)
1949d29b2c44Sab196087 		elfedit_msg(ELFEDIT_MSG_ERR, MSG_INTL(MSG_ERR_CNTDLCLOSE),
1950d29b2c44Sab196087 		    moddef->ml_path, dlerror());
1951d29b2c44Sab196087 
1952d29b2c44Sab196087 	/* Unlink the record from the module list */
1953d29b2c44Sab196087 	if (insdef == NULL)
1954d29b2c44Sab196087 		state.modlist = moddef->ml_next;
1955d29b2c44Sab196087 	else
1956d29b2c44Sab196087 		insdef->ml_next = moddef->ml_next;
1957d29b2c44Sab196087 
1958d29b2c44Sab196087 	/* Release the memory */
1959d29b2c44Sab196087 	free(moddef);
1960d29b2c44Sab196087 }
1961d29b2c44Sab196087 
1962d29b2c44Sab196087 
1963d29b2c44Sab196087 /*
1964d29b2c44Sab196087  * Load all sharable objects found in the specified directory.
1965d29b2c44Sab196087  *
1966d29b2c44Sab196087  * entry:
1967d29b2c44Sab196087  *	dirpath - Path of directory to process.
1968d29b2c44Sab196087  *	must_exist - If True, it is an error if diropen() fails to open
1969d29b2c44Sab196087  *		the given directory. Of False, we quietly ignore it and return.
1970d29b2c44Sab196087  *	abs_path - If True, files are loaded using their literal paths.
1971d29b2c44Sab196087  *		If False, their module name is extracted from the dirpath
1972d29b2c44Sab196087  *		and a path based search is used to locate it.
1973d29b2c44Sab196087  */
1974d29b2c44Sab196087 void
elfedit_load_moddir(const char * dirpath,int must_exist,int abs_path)1975d29b2c44Sab196087 elfedit_load_moddir(const char *dirpath, int must_exist, int abs_path)
1976d29b2c44Sab196087 {
1977d29b2c44Sab196087 	char		path[PATH_MAX + 1];
1978d29b2c44Sab196087 	DIR		*dir;
1979d29b2c44Sab196087 	struct dirent	*dp;
1980d29b2c44Sab196087 	const char 	*tail;
1981d29b2c44Sab196087 
1982d29b2c44Sab196087 	dir = opendir(dirpath);
1983d29b2c44Sab196087 	if (dir == NULL) {
1984d29b2c44Sab196087 		int err = errno;
1985d29b2c44Sab196087 
1986d29b2c44Sab196087 		if (!must_exist && (err == ENOENT))
1987d29b2c44Sab196087 			return;
1988d29b2c44Sab196087 		elfedit_msg(ELFEDIT_MSG_ERR, MSG_INTL(MSG_ERR_CNTOPNDIR),
1989d29b2c44Sab196087 		    dirpath, strerror(err));
1990d29b2c44Sab196087 		/*NOTREACHED*/
1991d29b2c44Sab196087 	}
1992d29b2c44Sab196087 
1993d29b2c44Sab196087 	while (dp = readdir(dir)) {
1994d29b2c44Sab196087 		if ((tail = path_is_so(dp->d_name)) != NULL) {
1995d29b2c44Sab196087 			if (abs_path) {
1996d29b2c44Sab196087 				(void) snprintf(path, sizeof (path),
1997d29b2c44Sab196087 				    MSG_ORIG(MSG_FMT_BLDPATH), dirpath,
1998d29b2c44Sab196087 				    dp->d_name);
1999d29b2c44Sab196087 			} else {
2000d29b2c44Sab196087 				(void) elfedit_basename(dp->d_name, tail,
2001d29b2c44Sab196087 				    path, sizeof (path));
2002d29b2c44Sab196087 			}
2003d29b2c44Sab196087 			(void) elfedit_load_module(path, must_exist, 1);
2004d29b2c44Sab196087 		}
2005d29b2c44Sab196087 	}
2006d29b2c44Sab196087 	(void) closedir(dir);
2007d29b2c44Sab196087 }
2008d29b2c44Sab196087 
2009d29b2c44Sab196087 
2010d29b2c44Sab196087 /*
2011d29b2c44Sab196087  * Follow the module load path, and load the first module found for each
2012d29b2c44Sab196087  * given name.
2013d29b2c44Sab196087  */
2014d29b2c44Sab196087 void
elfedit_load_modpath(void)2015d29b2c44Sab196087 elfedit_load_modpath(void)
2016d29b2c44Sab196087 {
2017d29b2c44Sab196087 	size_t		i;
2018d29b2c44Sab196087 
2019d29b2c44Sab196087 	for (i = 0; i < state.modpath.n; i++)
2020d29b2c44Sab196087 		elfedit_load_moddir(state.modpath.seg[i], 0, 0);
2021d29b2c44Sab196087 }
2022d29b2c44Sab196087 
2023d29b2c44Sab196087 /*
2024d29b2c44Sab196087  * Given a module definition, look for the specified command.
2025d29b2c44Sab196087  * Returns the command if found, and NULL otherwise.
2026d29b2c44Sab196087  */
2027d29b2c44Sab196087 static elfeditGC_cmd_t *
find_cmd(elfeditGC_module_t * mod,const char * name)2028d29b2c44Sab196087 find_cmd(elfeditGC_module_t *mod, const char *name)
2029d29b2c44Sab196087 {
2030d29b2c44Sab196087 	elfeditGC_cmd_t *cmd;
2031d29b2c44Sab196087 	const char **cmd_name;
2032d29b2c44Sab196087 
2033d29b2c44Sab196087 	for (cmd = mod->mod_cmds; cmd->cmd_func != NULL; cmd++)
2034d29b2c44Sab196087 		for (cmd_name = cmd->cmd_name; *cmd_name; cmd_name++)
2035d29b2c44Sab196087 			if (strcasecmp(name, *cmd_name) == 0) {
2036d29b2c44Sab196087 				if (cmd_name != cmd->cmd_name)
2037d29b2c44Sab196087 					elfedit_msg(ELFEDIT_MSG_DEBUG,
2038d29b2c44Sab196087 					    MSG_INTL(MSG_DEBUG_CMDALIAS),
2039d29b2c44Sab196087 					    mod->mod_name, *cmd_name,
2040d29b2c44Sab196087 					    mod->mod_name, *cmd->cmd_name);
2041d29b2c44Sab196087 				return (cmd);
2042d29b2c44Sab196087 			}
2043d29b2c44Sab196087 
2044d29b2c44Sab196087 	return (NULL);
2045d29b2c44Sab196087 }
2046d29b2c44Sab196087 
2047d29b2c44Sab196087 
2048d29b2c44Sab196087 /*
2049d29b2c44Sab196087  * Given a command name, return its command definition.
2050d29b2c44Sab196087  *
2051d29b2c44Sab196087  * entry:
2052d29b2c44Sab196087  *	name - Command to be looked up
2053d29b2c44Sab196087  *	must_exist - If True, we consider it to be an error if the command
2054d29b2c44Sab196087  *		does not exist. If False, NULL is returned quietly in
2055d29b2c44Sab196087  *		this case.
2056d29b2c44Sab196087  *	mod_ret - NULL, or address of a variable to receive the
2057d29b2c44Sab196087  *		module definition block of the module containing
2058d29b2c44Sab196087  *		the command.
2059d29b2c44Sab196087  *
2060d29b2c44Sab196087  * exit:
2061d29b2c44Sab196087  *	On success, returns a pointer to the command definition, and
2062d29b2c44Sab196087  *	if mod_ret is non-NULL, *mod_ret receives a pointer to the
2063d29b2c44Sab196087  *	module definition. On failure, must_exist determines the
2064d29b2c44Sab196087  *	action taken: If must_exist is True, an error is issued and
2065d29b2c44Sab196087  *	control does not return to the caller. If must_exist is False,
2066d29b2c44Sab196087  *	NULL is quietly returned.
2067d29b2c44Sab196087  *
2068d29b2c44Sab196087  * note:
2069d29b2c44Sab196087  *	A ':' in name is used to delimit the module and command names.
2070d29b2c44Sab196087  *	If it is omitted, or if it is the first non-whitespace character
2071d29b2c44Sab196087  *	in the name, then the built in sys: module is implied.
2072d29b2c44Sab196087  */
2073d29b2c44Sab196087 elfeditGC_cmd_t *
elfedit_find_command(const char * name,int must_exist,elfeditGC_module_t ** mod_ret)2074d29b2c44Sab196087 elfedit_find_command(const char *name, int must_exist,
2075d29b2c44Sab196087     elfeditGC_module_t **mod_ret)
2076d29b2c44Sab196087 {
2077d29b2c44Sab196087 	elfeditGC_module_t	*mod;
2078d29b2c44Sab196087 	const char		*mod_str;
2079d29b2c44Sab196087 	const char		*cmd_str;
2080d29b2c44Sab196087 	char			mod_buf[ELFEDIT_MAXMODNAM + 1];
2081d29b2c44Sab196087 	size_t			n;
2082d29b2c44Sab196087 	elfeditGC_cmd_t		*cmd;
2083d29b2c44Sab196087 
2084d29b2c44Sab196087 
2085d29b2c44Sab196087 	cmd_str = strstr(name, MSG_ORIG(MSG_STR_COLON));
2086d29b2c44Sab196087 	if (cmd_str == NULL) {		/* No module name -> sys: */
2087d29b2c44Sab196087 		mod_str = MSG_ORIG(MSG_MOD_SYS);
2088d29b2c44Sab196087 		cmd_str = name;
2089d29b2c44Sab196087 	} else if (cmd_str == name) {	/* Empty module name -> sys: */
2090d29b2c44Sab196087 		mod_str = MSG_ORIG(MSG_MOD_SYS);
2091d29b2c44Sab196087 		cmd_str++;		/* Skip the colon */
2092d29b2c44Sab196087 	} else {			/* Have both module and command */
2093d29b2c44Sab196087 		n = cmd_str - name;
2094d29b2c44Sab196087 		if (n >= sizeof (mod_buf)) {
2095d29b2c44Sab196087 			if (must_exist)
2096d29b2c44Sab196087 				elfedit_msg(ELFEDIT_MSG_ERR,
2097d29b2c44Sab196087 				    MSG_INTL(MSG_ERR_MODNAMTOOLONG), name);
2098d29b2c44Sab196087 			return (NULL);
2099d29b2c44Sab196087 		}
2100d29b2c44Sab196087 		(void) strlcpy(mod_buf, name, n + 1);
2101d29b2c44Sab196087 		mod_str = mod_buf;
2102d29b2c44Sab196087 		cmd_str++;
2103d29b2c44Sab196087 	}
2104d29b2c44Sab196087 
2105d29b2c44Sab196087 	/* Lookup/load module. Won't return on error */
2106d29b2c44Sab196087 	mod = elfedit_load_module(mod_str, must_exist, 0);
2107d29b2c44Sab196087 	if (mod == NULL)
2108d29b2c44Sab196087 		return (NULL);
2109d29b2c44Sab196087 
2110d29b2c44Sab196087 	/* Locate the command */
2111d29b2c44Sab196087 	cmd = find_cmd(mod, cmd_str);
2112d29b2c44Sab196087 	if (cmd == NULL) {
2113d29b2c44Sab196087 		if (must_exist) {
2114d29b2c44Sab196087 			/*
2115d29b2c44Sab196087 			 * Catch empty command in order to provide
2116d29b2c44Sab196087 			 * a better error message.
2117d29b2c44Sab196087 			 */
2118d29b2c44Sab196087 			if (*cmd_str == '\0') {
2119d29b2c44Sab196087 				elfedit_msg(ELFEDIT_MSG_ERR,
2120d29b2c44Sab196087 				    MSG_INTL(MSG_ERR_MODNOCMD), mod_str);
2121d29b2c44Sab196087 			} else {
2122d29b2c44Sab196087 				elfedit_msg(ELFEDIT_MSG_ERR,
2123d29b2c44Sab196087 				    MSG_INTL(MSG_ERR_UNRECCMD),
2124d29b2c44Sab196087 				    mod_str, cmd_str);
2125d29b2c44Sab196087 			}
2126d29b2c44Sab196087 		}
2127d29b2c44Sab196087 	} else {
2128d29b2c44Sab196087 		if (mod_ret != NULL)
2129d29b2c44Sab196087 			*mod_ret = mod;
2130d29b2c44Sab196087 	}
2131d29b2c44Sab196087 	return (cmd);
2132d29b2c44Sab196087 }
2133d29b2c44Sab196087 
2134d29b2c44Sab196087 
2135d29b2c44Sab196087 /*
2136d29b2c44Sab196087  * Release all user command blocks found on state.ucmd
2137d29b2c44Sab196087  */
2138d29b2c44Sab196087 static void
free_user_cmds(void)2139d29b2c44Sab196087 free_user_cmds(void)
2140d29b2c44Sab196087 {
2141d29b2c44Sab196087 	USER_CMD_T *next;
2142d29b2c44Sab196087 
2143d29b2c44Sab196087 	while (state.ucmd.list) {
2144d29b2c44Sab196087 		next = state.ucmd.list->ucmd_next;
2145d29b2c44Sab196087 		free(state.ucmd.list);
2146d29b2c44Sab196087 		state.ucmd.list = next;
2147d29b2c44Sab196087 	}
2148d29b2c44Sab196087 	state.ucmd.tail = NULL;
2149d29b2c44Sab196087 	state.ucmd.n = 0;
2150d29b2c44Sab196087 	state.cur_cmd = NULL;
2151d29b2c44Sab196087 }
2152d29b2c44Sab196087 
2153d29b2c44Sab196087 
2154d29b2c44Sab196087 /*
2155d29b2c44Sab196087  * Process all user command blocks found on state.ucmd, and then
2156d29b2c44Sab196087  * remove them from the list.
2157d29b2c44Sab196087  */
2158d29b2c44Sab196087 static void
dispatch_user_cmds()2159d29b2c44Sab196087 dispatch_user_cmds()
2160d29b2c44Sab196087 {
2161d29b2c44Sab196087 	USER_CMD_T		*ucmd;
2162d29b2c44Sab196087 	elfedit_cmdret_t	cmd_ret;
2163d29b2c44Sab196087 
2164d29b2c44Sab196087 	ucmd = state.ucmd.list;
2165d29b2c44Sab196087 	if (ucmd) {
2166d29b2c44Sab196087 		/* Do them, in order */
2167d29b2c44Sab196087 		for (; ucmd; ucmd = ucmd->ucmd_next) {
2168d29b2c44Sab196087 			state.cur_cmd = ucmd;
2169d29b2c44Sab196087 			if (!state.msg_jbuf.active)
2170d29b2c44Sab196087 				elfedit_msg(ELFEDIT_MSG_DEBUG,
2171d29b2c44Sab196087 				    MSG_INTL(MSG_DEBUG_EXECCMD),
2172d29b2c44Sab196087 				    ucmd->ucmd_orig_str);
2173d29b2c44Sab196087 			/*
2174d29b2c44Sab196087 			 * The cmd_func field is the generic definition.
2175d29b2c44Sab196087 			 * We need to cast it to the type that matches
2176d29b2c44Sab196087 			 * the proper ELFCLASS before calling it.
2177d29b2c44Sab196087 			 */
2178d29b2c44Sab196087 			if (state.elf.elfclass == ELFCLASS32) {
2179d29b2c44Sab196087 				elfedit32_cmd_func_t *cmd_func =
2180d29b2c44Sab196087 				    (elfedit32_cmd_func_t *)
2181d29b2c44Sab196087 				    ucmd->ucmd_cmd->cmd_func;
2182d29b2c44Sab196087 
2183d29b2c44Sab196087 				cmd_ret = (* cmd_func)(state.elf.obj_state.s32,
2184d29b2c44Sab196087 				    ucmd->ucmd_argc, ucmd->ucmd_argv);
2185d29b2c44Sab196087 			} else {
2186d29b2c44Sab196087 				elfedit64_cmd_func_t *cmd_func =
2187d29b2c44Sab196087 				    (elfedit64_cmd_func_t *)
2188d29b2c44Sab196087 				    ucmd->ucmd_cmd->cmd_func;
2189d29b2c44Sab196087 
2190d29b2c44Sab196087 				cmd_ret = (* cmd_func)(state.elf.obj_state.s64,
2191d29b2c44Sab196087 				    ucmd->ucmd_argc, ucmd->ucmd_argv);
2192d29b2c44Sab196087 			}
2193d29b2c44Sab196087 			state.cur_cmd = NULL;
2194d29b2c44Sab196087 			/* If a pager was started, wrap it up */
2195d29b2c44Sab196087 			elfedit_pager_cleanup();
2196d29b2c44Sab196087 
2197d29b2c44Sab196087 			switch (cmd_ret) {
21984f680cc6SAli Bahrami 			case ELFEDIT_CMDRET_MOD_OS_MACH:
21994f680cc6SAli Bahrami 				/*
22004f680cc6SAli Bahrami 				 * Inform the elfconst module that the machine
22014f680cc6SAli Bahrami 				 * or osabi has has changed. It may be necessary
22024f680cc6SAli Bahrami 				 * to fetch new strings from libconv.
22034f680cc6SAli Bahrami 				 */
22044f680cc6SAli Bahrami 				state.elf.elfconst_ehdr_change = 1;
22054f680cc6SAli Bahrami 				/*FALLTHROUGH*/
2206d29b2c44Sab196087 			case ELFEDIT_CMDRET_MOD:
2207d29b2c44Sab196087 				/*
2208d29b2c44Sab196087 				 * Command modified the output ELF image,
2209d29b2c44Sab196087 				 * mark the file as needing a flush to disk.
2210d29b2c44Sab196087 				 */
2211d29b2c44Sab196087 				state.file.dirty = 1;
2212d29b2c44Sab196087 				break;
2213d29b2c44Sab196087 			case ELFEDIT_CMDRET_FLUSH:
2214d29b2c44Sab196087 				/*
2215d29b2c44Sab196087 				 * Command flushed the output file,
2216d29b2c44Sab196087 				 * clear the dirty bit.
2217d29b2c44Sab196087 				 */
2218d29b2c44Sab196087 				state.file.dirty = 0;
2219d29b2c44Sab196087 			}
2220d29b2c44Sab196087 		}
2221d29b2c44Sab196087 		free_user_cmds();
2222d29b2c44Sab196087 	}
2223d29b2c44Sab196087 }
2224d29b2c44Sab196087 
2225d29b2c44Sab196087 
2226d29b2c44Sab196087 /*
2227d29b2c44Sab196087  * Prepare a GETTOK_STATE struct for gettok().
2228d29b2c44Sab196087  *
2229d29b2c44Sab196087  * entry:
2230d29b2c44Sab196087  *	gettok_state - gettok state block to use
2231d29b2c44Sab196087  *	str - Writable buffer to tokenize. Note that gettok()
2232d29b2c44Sab196087  *		is allowed to change the contents of this buffer.
2233d29b2c44Sab196087  *	inc_null_final - If the line ends in whitespace instead of
2234d29b2c44Sab196087  *		immediately hitting a NULL, and inc_null_final is TRUE,
2235d29b2c44Sab196087  *		then a null final token is generated. Otherwise trailing
2236d29b2c44Sab196087  *		whitespace is ignored.
2237d29b2c44Sab196087  */
2238d29b2c44Sab196087 static void
gettok_init(GETTOK_STATE * gettok_state,char * buf,int inc_null_final)2239d29b2c44Sab196087 gettok_init(GETTOK_STATE *gettok_state, char *buf, int inc_null_final)
2240d29b2c44Sab196087 {
2241d29b2c44Sab196087 	gettok_state->gtok_buf = gettok_state->gtok_cur_buf = buf;
2242d29b2c44Sab196087 	gettok_state->gtok_inc_null_final = inc_null_final;
2243d29b2c44Sab196087 	gettok_state->gtok_null_seen = 0;
2244d29b2c44Sab196087 }
2245d29b2c44Sab196087 
2246d29b2c44Sab196087 
2247d29b2c44Sab196087 /*
2248d29b2c44Sab196087  * Locate the next token from the buffer.
2249d29b2c44Sab196087  *
2250d29b2c44Sab196087  * entry:
2251d29b2c44Sab196087  *	gettok_state - State of gettok() operation. Initialized
2252d29b2c44Sab196087  *		by gettok_init(), and passed to gettok().
2253d29b2c44Sab196087  *
2254d29b2c44Sab196087  * exit:
2255d29b2c44Sab196087  *	If a token is found, gettok_state->gtok_last_token is filled in
2256d29b2c44Sab196087  *	with the details and True (1) is returned. If no token is found,
2257d29b2c44Sab196087  *	False (1) is returned, and the contents of
2258d29b2c44Sab196087  *	gettok_state->gtok_last_token are undefined.
2259d29b2c44Sab196087  *
2260d29b2c44Sab196087  * note:
2261d29b2c44Sab196087  *	- The token returned references the memory in gettok_state->gtok_buf.
2262d29b2c44Sab196087  *		The caller should not modify the buffer until all such
2263d29b2c44Sab196087  *		pointers have been discarded.
2264d29b2c44Sab196087  *	- This routine will modify the contents of gettok_state->gtok_buf
2265d29b2c44Sab196087  *		as necessary to remove quotes and eliminate escape
2266d29b2c44Sab196087  *		(\)characters.
2267d29b2c44Sab196087  */
2268d29b2c44Sab196087 static int
gettok(GETTOK_STATE * gettok_state)2269d29b2c44Sab196087 gettok(GETTOK_STATE *gettok_state)
2270d29b2c44Sab196087 {
2271d29b2c44Sab196087 	char	*str = gettok_state->gtok_cur_buf;
2272d29b2c44Sab196087 	char	*look;
2273d29b2c44Sab196087 	int	quote_ch = '\0';
2274d29b2c44Sab196087 
2275d29b2c44Sab196087 	/* Skip leading whitespace */
2276d29b2c44Sab196087 	while (isspace(*str))
2277d29b2c44Sab196087 		str++;
2278d29b2c44Sab196087 
2279d29b2c44Sab196087 	if (*str == '\0') {
2280d29b2c44Sab196087 		/*
2281d29b2c44Sab196087 		 * If user requested it, and there was whitespace at the
2282d29b2c44Sab196087 		 * end, then generate one last null token.
2283d29b2c44Sab196087 		 */
2284d29b2c44Sab196087 		if (gettok_state->gtok_inc_null_final &&
2285d29b2c44Sab196087 		    !gettok_state->gtok_null_seen) {
2286d29b2c44Sab196087 			gettok_state->gtok_inc_null_final = 0;
2287d29b2c44Sab196087 			gettok_state->gtok_null_seen = 1;
2288d29b2c44Sab196087 			gettok_state->gtok_last_token.tok_str = str;
2289d29b2c44Sab196087 			gettok_state->gtok_last_token.tok_len = 0;
2290d29b2c44Sab196087 			gettok_state->gtok_last_token.tok_line_off =
2291d29b2c44Sab196087 			    str - gettok_state->gtok_buf;
2292d29b2c44Sab196087 			return (1);
2293d29b2c44Sab196087 		}
2294d29b2c44Sab196087 		gettok_state->gtok_null_seen = 1;
2295d29b2c44Sab196087 		return (0);
2296d29b2c44Sab196087 	}
2297d29b2c44Sab196087 
2298d29b2c44Sab196087 	/*
2299d29b2c44Sab196087 	 * Read token: The standard delimiter is whitespace, but
2300d29b2c44Sab196087 	 * we honor either single or double quotes. Also, we honor
2301d29b2c44Sab196087 	 * backslash escapes.
2302d29b2c44Sab196087 	 */
2303d29b2c44Sab196087 	gettok_state->gtok_last_token.tok_str = look = str;
2304d29b2c44Sab196087 	gettok_state->gtok_last_token.tok_line_off =
2305d29b2c44Sab196087 	    look - gettok_state->gtok_buf;
2306d29b2c44Sab196087 	for (; *look; look++) {
2307d29b2c44Sab196087 		if (*look == quote_ch) {	/* Terminates active quote */
2308d29b2c44Sab196087 			quote_ch = '\0';
2309d29b2c44Sab196087 			continue;
2310d29b2c44Sab196087 		}
2311d29b2c44Sab196087 
2312d29b2c44Sab196087 		if (quote_ch == '\0') {		/* No quote currently active */
2313d29b2c44Sab196087 			if ((*look == '\'') || (*look == '"')) {
2314d29b2c44Sab196087 				quote_ch = *look;	/* New active quote */
2315d29b2c44Sab196087 				continue;
2316d29b2c44Sab196087 			}
2317d29b2c44Sab196087 			if (isspace(*look))
2318d29b2c44Sab196087 				break;
2319d29b2c44Sab196087 		}
2320d29b2c44Sab196087 
2321cce0e03bSab196087 		/*
2322cce0e03bSab196087 		 * The semantics of the backslash character depends on
2323cce0e03bSab196087 		 * the quote style in use:
2324cce0e03bSab196087 		 *	- Within single quotes, backslash is not
2325cce0e03bSab196087 		 *		an escape character, and is taken literally.
2326cce0e03bSab196087 		 *	- If outside of quotes, the backslash is an escape
2327cce0e03bSab196087 		 *		character. The backslash is ignored and the
2328cce0e03bSab196087 		 *		following character is taken literally, losing
2329cce0e03bSab196087 		 *		any special properties it normally has.
2330cce0e03bSab196087 		 *	- Within double quotes, backslash works like a
2331cce0e03bSab196087 		 *		backslash escape within a C literal. Certain
2332cce0e03bSab196087 		 *		escapes are recognized and replaced with their
2333cce0e03bSab196087 		 *		special character. Any others are an error.
2334cce0e03bSab196087 		 */
2335d29b2c44Sab196087 		if (*look == '\\') {
2336cce0e03bSab196087 			if (quote_ch == '\'') {
2337cce0e03bSab196087 				*str++ = *look;
2338cce0e03bSab196087 				continue;
2339cce0e03bSab196087 			}
2340cce0e03bSab196087 
2341d29b2c44Sab196087 			look++;
2342cce0e03bSab196087 			if (*look == '\0') {	/* Esc applied to NULL term? */
2343cce0e03bSab196087 				elfedit_msg(ELFEDIT_MSG_ERR,
2344cce0e03bSab196087 				    MSG_INTL(MSG_ERR_ESCEOL));
2345cce0e03bSab196087 				/*NOTREACHED*/
2346cce0e03bSab196087 			}
2347cce0e03bSab196087 
2348cce0e03bSab196087 			if (quote_ch == '"') {
2349*69112eddSAli Bahrami 				int ch = conv_translate_c_esc(&look);
2350*69112eddSAli Bahrami 
2351*69112eddSAli Bahrami 				if (ch == -1)
2352*69112eddSAli Bahrami 					elfedit_msg(ELFEDIT_MSG_ERR,
2353*69112eddSAli Bahrami 					    MSG_INTL(MSG_ERR_BADCESC), *look);
2354*69112eddSAli Bahrami 				*str++ = ch;
2355cce0e03bSab196087 				look--;		/* for() will advance by 1 */
2356cce0e03bSab196087 				continue;
2357cce0e03bSab196087 			}
2358d29b2c44Sab196087 		}
2359d29b2c44Sab196087 
2360d29b2c44Sab196087 		if (look != str)
2361d29b2c44Sab196087 			*str = *look;
2362d29b2c44Sab196087 		str++;
2363d29b2c44Sab196087 	}
2364cce0e03bSab196087 
2365cce0e03bSab196087 	/* Don't allow unterminated quoted tokens */
2366cce0e03bSab196087 	if (quote_ch != '\0')
2367cce0e03bSab196087 		elfedit_msg(ELFEDIT_MSG_ERR, MSG_INTL(MSG_ERR_UNTERMQUOTE),
2368cce0e03bSab196087 		    quote_ch);
2369cce0e03bSab196087 
2370d29b2c44Sab196087 	gettok_state->gtok_last_token.tok_len = str -
2371d29b2c44Sab196087 	    gettok_state->gtok_last_token.tok_str;
2372d29b2c44Sab196087 	gettok_state->gtok_null_seen = *look == '\0';
2373d29b2c44Sab196087 	if (!gettok_state->gtok_null_seen)
2374d29b2c44Sab196087 		look++;
2375d29b2c44Sab196087 	*str = '\0';
2376d29b2c44Sab196087 	gettok_state->gtok_cur_buf = look;
2377d29b2c44Sab196087 
2378cce0e03bSab196087 #ifdef DEBUG_GETTOK
2379cce0e03bSab196087 	printf("GETTOK >");
2380cce0e03bSab196087 	elfedit_str_to_c_literal(gettok_state->gtok_last_token.tok_str,
2381cce0e03bSab196087 	    elfedit_write);
2382cce0e03bSab196087 	printf("< \tlen(%d) offset(%d)\n",
2383d29b2c44Sab196087 	    gettok_state->gtok_last_token.tok_len,
2384d29b2c44Sab196087 	    gettok_state->gtok_last_token.tok_line_off);
2385d29b2c44Sab196087 #endif
2386d29b2c44Sab196087 
2387d29b2c44Sab196087 	return (1);
2388d29b2c44Sab196087 }
2389d29b2c44Sab196087 
2390d29b2c44Sab196087 
2391d29b2c44Sab196087 /*
2392d29b2c44Sab196087  * Tokenize the user command string, and return a pointer to the
2393d29b2c44Sab196087  * TOK_STATE buffer maintained by this function. That buffer contains
2394d29b2c44Sab196087  * the tokenized strings.
2395d29b2c44Sab196087  *
2396d29b2c44Sab196087  * entry:
2397d29b2c44Sab196087  *	user_cmd_str - String to tokenize
2398d29b2c44Sab196087  *	len - # of characters in user_cmd_str to examine. If
2399d29b2c44Sab196087  *		(len < 0), then the complete string is processed
2400d29b2c44Sab196087  *		stopping with the NULL termination. Otherwise,
2401d29b2c44Sab196087  *		processing stops after len characters, and any
2402d29b2c44Sab196087  *		remaining characters are ignored.
2403d29b2c44Sab196087  *	inc_null_final - If True, and if user_cmd_str has whitespace
2404d29b2c44Sab196087  *		at the end following the last non-null token, then
2405d29b2c44Sab196087  *		a final null token will be included. If False, null
2406d29b2c44Sab196087  *		tokens are ignored.
2407d29b2c44Sab196087  *
2408d29b2c44Sab196087  * note:
2409d29b2c44Sab196087  *	This routine returns pointers to internally allocated memory.
2410d29b2c44Sab196087  *	The caller must not alter anything contained in the TOK_STATE
2411d29b2c44Sab196087  *	buffer returned. Furthermore, the the contents of TOK_STATE
2412d29b2c44Sab196087  *	are only valid until the next call to tokenize_user_cmd().
2413d29b2c44Sab196087  */
2414d29b2c44Sab196087 static TOK_STATE *
tokenize_user_cmd(const char * user_cmd_str,size_t len,int inc_null_final)2415d29b2c44Sab196087 tokenize_user_cmd(const char *user_cmd_str, size_t len, int inc_null_final)
2416d29b2c44Sab196087 {
2417d29b2c44Sab196087 #define	INITIAL_TOK_ALLOC 5
2418d29b2c44Sab196087 
2419d29b2c44Sab196087 	/*
2420d29b2c44Sab196087 	 * As we parse the user command, we need temporary space to
2421d29b2c44Sab196087 	 * hold the tokens. We do this by dynamically allocating a string
2422d29b2c44Sab196087 	 * buffer and a token array, and doubling them as necessary. This
2423d29b2c44Sab196087 	 * is a single threaded application, so static variables suffice.
2424d29b2c44Sab196087 	 */
2425d29b2c44Sab196087 	static STRBUF str;
2426d29b2c44Sab196087 	static TOK_STATE tokst;
2427d29b2c44Sab196087 
2428d29b2c44Sab196087 	GETTOK_STATE	gettok_state;
2429d29b2c44Sab196087 	size_t		n;
2430d29b2c44Sab196087 
2431d29b2c44Sab196087 	/*
2432d29b2c44Sab196087 	 * Make a copy we can modify. If (len == 0), take the entire
2433d29b2c44Sab196087 	 * string. Otherwise limit it to the specified length.
2434d29b2c44Sab196087 	 */
2435d29b2c44Sab196087 	tokst.tokst_cmd_len = strlen(user_cmd_str);
2436d29b2c44Sab196087 	if ((len > 0) && (len < tokst.tokst_cmd_len))
2437d29b2c44Sab196087 		tokst.tokst_cmd_len = len;
2438d29b2c44Sab196087 	tokst.tokst_cmd_len++;	/* Room for NULL termination */
2439d29b2c44Sab196087 	strbuf_ensure_size(&str, tokst.tokst_cmd_len);
2440d29b2c44Sab196087 	(void) strlcpy(str.buf, user_cmd_str, tokst.tokst_cmd_len);
2441d29b2c44Sab196087 
2442d29b2c44Sab196087 	/* Trim off any newline character that might be present */
2443d29b2c44Sab196087 	if ((tokst.tokst_cmd_len > 1) &&
2444d29b2c44Sab196087 	    (str.buf[tokst.tokst_cmd_len - 2] == '\n')) {
2445d29b2c44Sab196087 		tokst.tokst_cmd_len--;
2446d29b2c44Sab196087 		str.buf[tokst.tokst_cmd_len - 1] = '\0';
2447d29b2c44Sab196087 	}
2448d29b2c44Sab196087 
2449d29b2c44Sab196087 	/* Tokenize the user command string into tok struct */
2450d29b2c44Sab196087 	gettok_init(&gettok_state, str.buf, inc_null_final);
2451d29b2c44Sab196087 	tokst.tokst_str_size = 0;	/* Space needed for token strings */
2452d29b2c44Sab196087 	for (tokst.tokst_cnt = 0; gettok(&gettok_state) != 0;
2453d29b2c44Sab196087 	    tokst.tokst_cnt++) {
2454d29b2c44Sab196087 		/* If we need more room, expand the token buffer */
2455d29b2c44Sab196087 		if (tokst.tokst_cnt >= tokst.tokst_bufsize) {
2456d29b2c44Sab196087 			n = (tokst.tokst_bufsize == 0) ?
2457d29b2c44Sab196087 			    INITIAL_TOK_ALLOC : (tokst.tokst_bufsize * 2);
2458d29b2c44Sab196087 			tokst.tokst_buf = elfedit_realloc(
2459d29b2c44Sab196087 			    MSG_INTL(MSG_ALLOC_TOKBUF), tokst.tokst_buf,
2460d29b2c44Sab196087 			    n * sizeof (*tokst.tokst_buf));
2461d29b2c44Sab196087 			tokst.tokst_bufsize = n;
2462d29b2c44Sab196087 		}
2463d29b2c44Sab196087 		tokst.tokst_str_size +=
2464d29b2c44Sab196087 		    gettok_state.gtok_last_token.tok_len + 1;
2465d29b2c44Sab196087 		tokst.tokst_buf[tokst.tokst_cnt] = gettok_state.gtok_last_token;
2466d29b2c44Sab196087 	}
2467d29b2c44Sab196087 	/* fold the command token to lowercase */
2468d29b2c44Sab196087 	if (tokst.tokst_cnt > 0) {
2469d29b2c44Sab196087 		char *s;
2470d29b2c44Sab196087 
2471d29b2c44Sab196087 		for (s = tokst.tokst_buf[0].tok_str; *s; s++)
2472d29b2c44Sab196087 			if (isupper(*s))
2473d29b2c44Sab196087 				*s = tolower(*s);
2474d29b2c44Sab196087 	}
2475d29b2c44Sab196087 
2476d29b2c44Sab196087 	return (&tokst);
2477d29b2c44Sab196087 
2478d29b2c44Sab196087 #undef	INITIAL_TOK_ALLOC
2479d29b2c44Sab196087 }
2480d29b2c44Sab196087 
2481d29b2c44Sab196087 
2482d29b2c44Sab196087 /*
2483d29b2c44Sab196087  * Parse the user command string, and put an entry for it at the end
2484d29b2c44Sab196087  * of state.ucmd.
2485d29b2c44Sab196087  */
2486d29b2c44Sab196087 static void
parse_user_cmd(const char * user_cmd_str)2487d29b2c44Sab196087 parse_user_cmd(const char *user_cmd_str)
2488d29b2c44Sab196087 {
2489d29b2c44Sab196087 	TOK_STATE	*tokst;
2490d29b2c44Sab196087 	char		*s;
2491d29b2c44Sab196087 	size_t		n;
2492d29b2c44Sab196087 	size_t		len;
2493d29b2c44Sab196087 	USER_CMD_T	*ucmd;
2494d29b2c44Sab196087 	elfeditGC_module_t *mod;
2495d29b2c44Sab196087 	elfeditGC_cmd_t	*cmd;
2496d29b2c44Sab196087 
2497d29b2c44Sab196087 	/*
2498d29b2c44Sab196087 	 * Break it into tokens. If there are none, then it is
2499d29b2c44Sab196087 	 * an empty command and is ignored.
2500d29b2c44Sab196087 	 */
2501d29b2c44Sab196087 	tokst = tokenize_user_cmd(user_cmd_str, -1, 0);
2502d29b2c44Sab196087 	if (tokst->tokst_cnt == 0)
2503d29b2c44Sab196087 		return;
2504d29b2c44Sab196087 
2505d29b2c44Sab196087 	/* Find the command. Won't return on error */
2506d29b2c44Sab196087 	cmd = elfedit_find_command(tokst->tokst_buf[0].tok_str, 1, &mod);
2507d29b2c44Sab196087 
2508d29b2c44Sab196087 	/*
2509d29b2c44Sab196087 	 * If there is no ELF file being edited, then only commands
2510d29b2c44Sab196087 	 * from the sys: module are allowed.
2511d29b2c44Sab196087 	 */
2512d29b2c44Sab196087 	if ((state.file.present == 0) &&
2513d29b2c44Sab196087 	    (strcmp(mod->mod_name, MSG_ORIG(MSG_MOD_SYS)) != 0))
2514d29b2c44Sab196087 		elfedit_msg(ELFEDIT_MSG_ERR, MSG_INTL(MSG_ERR_NOFILSYSONLY),
2515d29b2c44Sab196087 		    mod->mod_name, cmd->cmd_name[0]);
2516d29b2c44Sab196087 
2517d29b2c44Sab196087 
2518d29b2c44Sab196087 	/* Allocate, fill in, and insert a USER_CMD_T block */
2519d29b2c44Sab196087 	n = S_DROUND(sizeof (USER_CMD_T));
2520d29b2c44Sab196087 	ucmd = elfedit_malloc(MSG_INTL(MSG_ALLOC_UCMD),
2521d29b2c44Sab196087 	    n + (sizeof (char *) * (tokst->tokst_cnt - 1)) +
2522d29b2c44Sab196087 	    tokst->tokst_cmd_len + tokst->tokst_str_size);
2523d29b2c44Sab196087 	ucmd->ucmd_next = NULL;
2524d29b2c44Sab196087 	ucmd->ucmd_argc = tokst->tokst_cnt - 1;
2525d29b2c44Sab196087 	/*LINTED E_BAD_PTR_CAST_ALIGN*/
2526d29b2c44Sab196087 	ucmd->ucmd_argv = (const char **)(n + (char *)ucmd);
2527d29b2c44Sab196087 	ucmd->ucmd_orig_str = (char *)(ucmd->ucmd_argv + ucmd->ucmd_argc);
2528d29b2c44Sab196087 	(void) strncpy(ucmd->ucmd_orig_str, user_cmd_str, tokst->tokst_cmd_len);
2529d29b2c44Sab196087 	ucmd->ucmd_mod = mod;
2530d29b2c44Sab196087 	ucmd->ucmd_cmd = cmd;
2531d29b2c44Sab196087 	ucmd->ucmd_ostyle_set = 0;
2532d29b2c44Sab196087 	s = ucmd->ucmd_orig_str + tokst->tokst_cmd_len;
2533d29b2c44Sab196087 	for (n = 1; n < tokst->tokst_cnt; n++) {
2534d29b2c44Sab196087 		len = tokst->tokst_buf[n].tok_len + 1;
2535d29b2c44Sab196087 		ucmd->ucmd_argv[n - 1] = s;
2536d29b2c44Sab196087 		(void) strncpy(s, tokst->tokst_buf[n].tok_str, len);
2537d29b2c44Sab196087 		s += len;
2538d29b2c44Sab196087 	}
2539d29b2c44Sab196087 	if (state.ucmd.list == NULL) {
2540d29b2c44Sab196087 		state.ucmd.list = state.ucmd.tail = ucmd;
2541d29b2c44Sab196087 	} else {
2542d29b2c44Sab196087 		state.ucmd.tail->ucmd_next = ucmd;
2543d29b2c44Sab196087 		state.ucmd.tail = ucmd;
2544d29b2c44Sab196087 	}
2545d29b2c44Sab196087 	state.ucmd.n++;
2546d29b2c44Sab196087 }
2547d29b2c44Sab196087 
2548d29b2c44Sab196087 
2549d29b2c44Sab196087 /*
2550d29b2c44Sab196087  * Copy infile to a new file with the name given by outfile.
2551d29b2c44Sab196087  */
2552d29b2c44Sab196087 static void
create_outfile(const char * infile,const char * outfile)2553d29b2c44Sab196087 create_outfile(const char *infile, const char *outfile)
2554d29b2c44Sab196087 {
2555d29b2c44Sab196087 	pid_t pid;
2556d29b2c44Sab196087 	int statloc;
2557d29b2c44Sab196087 	struct stat statbuf;
2558d29b2c44Sab196087 
2559d29b2c44Sab196087 
2560d29b2c44Sab196087 	pid = fork();
2561d29b2c44Sab196087 	switch (pid) {
2562d29b2c44Sab196087 	case -1:			/* Unable to create process */
2563d29b2c44Sab196087 		{
2564d29b2c44Sab196087 			int err = errno;
2565d29b2c44Sab196087 			elfedit_msg(ELFEDIT_MSG_ERR, MSG_INTL(MSG_ERR_CNTFORK),
2566d29b2c44Sab196087 			    strerror(err));
2567d29b2c44Sab196087 		}
2568d29b2c44Sab196087 		/*NOTREACHED*/
2569d29b2c44Sab196087 		return;
2570d29b2c44Sab196087 
2571d29b2c44Sab196087 	case 0:
2572d29b2c44Sab196087 		(void) execl(MSG_ORIG(MSG_STR_BINCP),
2573d29b2c44Sab196087 		    MSG_ORIG(MSG_STR_BINCP), infile, outfile, NULL);
2574d29b2c44Sab196087 		/*
2575d29b2c44Sab196087 		 * exec() only returns on error. This is the child process,
2576d29b2c44Sab196087 		 * so we want to stay away from the usual error mechanism
2577d29b2c44Sab196087 		 * and handle things directly.
2578d29b2c44Sab196087 		 */
2579d29b2c44Sab196087 		{
2580d29b2c44Sab196087 			int err = errno;
2581d29b2c44Sab196087 			(void) fprintf(stderr, MSG_INTL(MSG_ERR_CNTEXEC),
2582d29b2c44Sab196087 			    MSG_ORIG(MSG_STR_ELFEDIT),
2583d29b2c44Sab196087 			    MSG_ORIG(MSG_STR_BINCP), strerror(err));
2584d29b2c44Sab196087 		}
2585d29b2c44Sab196087 		exit(1);
2586d29b2c44Sab196087 		/*NOTREACHED*/
2587d29b2c44Sab196087 	}
2588d29b2c44Sab196087 
2589d29b2c44Sab196087 	/* This is the parent: Wait for the child to terminate */
2590d29b2c44Sab196087 	if (waitpid(pid, &statloc,  0) != pid) {
2591d29b2c44Sab196087 		int err = errno;
2592d29b2c44Sab196087 		elfedit_msg(ELFEDIT_MSG_ERR, MSG_INTL(MSG_ERR_CNTWAIT),
2593d29b2c44Sab196087 		    strerror(err));
2594d29b2c44Sab196087 	}
2595d29b2c44Sab196087 	/*
2596d29b2c44Sab196087 	 * If the child failed, then terminate the process. There is no
2597d29b2c44Sab196087 	 * need for an error message, because the child will have taken
2598d29b2c44Sab196087 	 * care of that.
2599d29b2c44Sab196087 	 */
2600d29b2c44Sab196087 	if (!WIFEXITED(statloc) || (WEXITSTATUS(statloc) != 0))
2601d29b2c44Sab196087 		exit(1);
2602d29b2c44Sab196087 
2603d29b2c44Sab196087 	/* Make sure the copy allows user write access */
2604d29b2c44Sab196087 	if (stat(outfile, &statbuf) == -1) {
2605d29b2c44Sab196087 		int err = errno;
2606d29b2c44Sab196087 		(void) unlink(outfile);
2607d29b2c44Sab196087 		elfedit_msg(ELFEDIT_MSG_ERR, MSG_INTL(MSG_ERR_CNTSTAT),
2608d29b2c44Sab196087 		    outfile, strerror(err));
2609d29b2c44Sab196087 	}
2610d29b2c44Sab196087 	if ((statbuf.st_mode & S_IWUSR) == 0) {
2611d29b2c44Sab196087 		/* Only keep permission bits, and add user write */
2612d29b2c44Sab196087 		statbuf.st_mode |= S_IWUSR;
2613d29b2c44Sab196087 		statbuf.st_mode &= 07777;   /* Only keep the permission bits */
2614d29b2c44Sab196087 		if (chmod(outfile, statbuf.st_mode) == -1) {
2615d29b2c44Sab196087 			int err = errno;
2616d29b2c44Sab196087 			(void) unlink(outfile);
2617d29b2c44Sab196087 			elfedit_msg(ELFEDIT_MSG_ERR, MSG_INTL(MSG_ERR_CNTCHMOD),
2618d29b2c44Sab196087 			    outfile, strerror(err));
2619d29b2c44Sab196087 		}
2620d29b2c44Sab196087 	}
2621d29b2c44Sab196087 }
2622d29b2c44Sab196087 
2623d29b2c44Sab196087 /*
2624d29b2c44Sab196087  * Given a module path string, determine how long the resulting path will
2625d29b2c44Sab196087  * be when all % tokens have been expanded.
2626d29b2c44Sab196087  *
2627d29b2c44Sab196087  * entry:
2628d29b2c44Sab196087  *	path - Path for which expanded length is desired
2629d29b2c44Sab196087  *	origin_root - Root of $ORIGIN  tree containing running elfedit program
2630d29b2c44Sab196087  *
2631d29b2c44Sab196087  * exit:
2632d29b2c44Sab196087  *	Returns the value strlen() will give for the expanded path.
2633d29b2c44Sab196087  */
2634d29b2c44Sab196087 static size_t
modpath_strlen(const char * path,const char * origin_root)2635d29b2c44Sab196087 modpath_strlen(const char *path, const char *origin_root)
2636d29b2c44Sab196087 {
2637d29b2c44Sab196087 	size_t len = 0;
2638d29b2c44Sab196087 	const char *s;
2639d29b2c44Sab196087 
2640d29b2c44Sab196087 	s = path;
2641d29b2c44Sab196087 	len = 0;
2642d29b2c44Sab196087 	for (s = path; *s != '\0'; s++) {
2643d29b2c44Sab196087 		if (*s == '%') {
2644d29b2c44Sab196087 			s++;
2645d29b2c44Sab196087 			switch (*s) {
2646d29b2c44Sab196087 			case 'i':	/* ISA of running elfedit */
2647d29b2c44Sab196087 				len += strlen(isa_i_str);
2648d29b2c44Sab196087 				break;
2649d29b2c44Sab196087 			case 'I':	/* "" for 32-bit, same as %i for 64 */
2650d29b2c44Sab196087 				len += strlen(isa_I_str);
2651d29b2c44Sab196087 				break;
2652d29b2c44Sab196087 			case 'o':	/* Insert default path */
2653d29b2c44Sab196087 				len +=
2654d29b2c44Sab196087 				    modpath_strlen(MSG_ORIG(MSG_STR_MODPATH),
2655d29b2c44Sab196087 				    origin_root);
2656d29b2c44Sab196087 				break;
2657d29b2c44Sab196087 			case 'r':	/* root of tree with running elfedit */
2658d29b2c44Sab196087 				len += strlen(origin_root);
2659d29b2c44Sab196087 				break;
2660d29b2c44Sab196087 
2661d29b2c44Sab196087 			case '%':	/* %% is reduced to just '%' */
2662d29b2c44Sab196087 				len++;
2663d29b2c44Sab196087 				break;
2664d29b2c44Sab196087 			default:	/* All other % codes are reserved */
2665d29b2c44Sab196087 				elfedit_msg(ELFEDIT_MSG_ERR,
2666d29b2c44Sab196087 				    MSG_INTL(MSG_ERR_BADPATHCODE), *s);
2667d29b2c44Sab196087 				/*NOTREACHED*/
2668d29b2c44Sab196087 				break;
2669d29b2c44Sab196087 			}
2670d29b2c44Sab196087 		} else {	/* Non-% character passes straight through */
2671d29b2c44Sab196087 			len++;
2672d29b2c44Sab196087 		}
2673d29b2c44Sab196087 	}
2674d29b2c44Sab196087 
2675d29b2c44Sab196087 	return (len);
2676d29b2c44Sab196087 }
2677d29b2c44Sab196087 
2678d29b2c44Sab196087 
2679d29b2c44Sab196087 /*
2680d29b2c44Sab196087  * Given a module path string, and a buffer large enough to hold the results,
2681d29b2c44Sab196087  * fill the buffer with the expanded path.
2682d29b2c44Sab196087  *
2683d29b2c44Sab196087  * entry:
2684d29b2c44Sab196087  *	path - Path for which expanded length is desired
2685d29b2c44Sab196087  *	origin_root - Root of tree containing running elfedit program
2686d29b2c44Sab196087  *	buf - Buffer to receive the result. buf must as large or larger
2687d29b2c44Sab196087  *		than the value given by modpath_strlen().
2688d29b2c44Sab196087  *
2689d29b2c44Sab196087  * exit:
2690d29b2c44Sab196087  *	Returns pointer to location following the last character
2691d29b2c44Sab196087  *	written to buf. A NULL byte is written to that address.
2692d29b2c44Sab196087  */
2693d29b2c44Sab196087 static char *
modpath_expand(const char * path,const char * origin_root,char * buf)2694d29b2c44Sab196087 modpath_expand(const char *path, const char *origin_root, char *buf)
2695d29b2c44Sab196087 {
2696d29b2c44Sab196087 	size_t len;
2697d29b2c44Sab196087 	const char *cp_str;
2698d29b2c44Sab196087 
2699d29b2c44Sab196087 	for (; *path != '\0'; path++) {
2700d29b2c44Sab196087 		if (*path == '%') {
2701d29b2c44Sab196087 			path++;
2702d29b2c44Sab196087 			cp_str = NULL;
2703d29b2c44Sab196087 			switch (*path) {
2704d29b2c44Sab196087 			case 'i':	/* ISA of running elfedit */
2705d29b2c44Sab196087 				cp_str = isa_i_str;
2706d29b2c44Sab196087 				break;
2707d29b2c44Sab196087 			case 'I':	/* "" for 32-bit, same as %i for 64 */
2708d29b2c44Sab196087 				cp_str = isa_I_str;
2709d29b2c44Sab196087 				break;
2710d29b2c44Sab196087 			case 'o':	/* Insert default path */
2711d29b2c44Sab196087 				buf = modpath_expand(MSG_ORIG(MSG_STR_MODPATH),
2712d29b2c44Sab196087 				    origin_root, buf);
2713d29b2c44Sab196087 				break;
2714d29b2c44Sab196087 			case 'r':
2715d29b2c44Sab196087 				cp_str = origin_root;
2716d29b2c44Sab196087 				break;
2717d29b2c44Sab196087 			case '%':	/* %% is reduced to just '%' */
2718d29b2c44Sab196087 				*buf++ = *path;
2719d29b2c44Sab196087 				break;
2720d29b2c44Sab196087 			default:	/* All other % codes are reserved */
2721d29b2c44Sab196087 				elfedit_msg(ELFEDIT_MSG_ERR,
2722d29b2c44Sab196087 				    MSG_INTL(MSG_ERR_BADPATHCODE), *path);
2723d29b2c44Sab196087 				/*NOTREACHED*/
2724d29b2c44Sab196087 				break;
2725d29b2c44Sab196087 			}
2726d29b2c44Sab196087 			if ((cp_str != NULL) && ((len = strlen(cp_str)) > 0)) {
2727d29b2c44Sab196087 				bcopy(cp_str, buf, len);
2728d29b2c44Sab196087 				buf += len;
2729d29b2c44Sab196087 			}
2730d29b2c44Sab196087 		} else {	/* Non-% character passes straight through */
2731d29b2c44Sab196087 			*buf++ = *path;
2732d29b2c44Sab196087 		}
2733d29b2c44Sab196087 	}
2734d29b2c44Sab196087 
2735d29b2c44Sab196087 	*buf = '\0';
2736d29b2c44Sab196087 	return (buf);
2737d29b2c44Sab196087 }
2738d29b2c44Sab196087 
2739d29b2c44Sab196087 
2740d29b2c44Sab196087 /*
2741d29b2c44Sab196087  * Establish the module search path: state.modpath
2742d29b2c44Sab196087  *
2743d29b2c44Sab196087  * The path used comes from the following sources, taking the first
2744d29b2c44Sab196087  * one that has a value, and ignoring any others:
2745d29b2c44Sab196087  *
2746d29b2c44Sab196087  *	- ELFEDIT_PATH environment variable
2747d29b2c44Sab196087  *	- -L command line argument
2748d29b2c44Sab196087  *	- Default value
2749d29b2c44Sab196087  *
2750d29b2c44Sab196087  * entry:
2751d29b2c44Sab196087  *	path - NULL, or the value of the -L command line argument
2752d29b2c44Sab196087  *
2753d29b2c44Sab196087  * exit:
2754d29b2c44Sab196087  *	state.modpath has been filled in
2755d29b2c44Sab196087  */
2756d29b2c44Sab196087 static void
establish_modpath(const char * cmdline_path)2757d29b2c44Sab196087 establish_modpath(const char *cmdline_path)
2758d29b2c44Sab196087 {
2759d29b2c44Sab196087 	char origin_root[PATH_MAX + 1];	/* Where elfedit binary is */
2760d29b2c44Sab196087 	const char	*path;		/* Initial path */
2761d29b2c44Sab196087 	char		*expath;	/* Expanded path */
2762d29b2c44Sab196087 	size_t		len;
2763d29b2c44Sab196087 	char		*src, *dst;
2764d29b2c44Sab196087 
2765d29b2c44Sab196087 	path = getenv(MSG_ORIG(MSG_STR_ENVVAR));
2766d29b2c44Sab196087 	if (path == NULL)
2767d29b2c44Sab196087 		path = cmdline_path;
2768d29b2c44Sab196087 	if (path == NULL)
2769d29b2c44Sab196087 		path = MSG_ORIG(MSG_STR_MODPATH);
2770d29b2c44Sab196087 
2771d29b2c44Sab196087 
2772d29b2c44Sab196087 	/*
2773d29b2c44Sab196087 	 * Root of tree containing running for running program. 32-bit elfedit
2774d29b2c44Sab196087 	 * is installed in /usr/bin, and 64-bit elfedit is one level lower
2775d29b2c44Sab196087 	 * in an ISA-specific subdirectory. So, we find the root by
2776d29b2c44Sab196087 	 * getting the $ORGIN of the current running program, and trimming
2777d29b2c44Sab196087 	 * off the last 2 (32-bit) or 3 (64-bit) directories.
2778d29b2c44Sab196087 	 *
2779d29b2c44Sab196087 	 * On a standard system, this will simply yield '/'. However,
2780d29b2c44Sab196087 	 * doing it this way allows us to run elfedit from a proto area,
2781d29b2c44Sab196087 	 * and pick up modules from the same proto area instead of those
2782d29b2c44Sab196087 	 * installed on the system.
2783d29b2c44Sab196087 	 */
2784d29b2c44Sab196087 	if (dlinfo(RTLD_SELF, RTLD_DI_ORIGIN, &origin_root) == -1)
2785d29b2c44Sab196087 		elfedit_msg(ELFEDIT_MSG_ERR, MSG_INTL(MSG_ERR_CNTGETORIGIN));
2786d29b2c44Sab196087 	len = (sizeof (char *) == 8) ? 3 : 2;
2787d29b2c44Sab196087 	src = origin_root + strlen(origin_root);
2788d29b2c44Sab196087 	while ((src > origin_root) && (len > 0)) {
2789d29b2c44Sab196087 		if (*(src - 1) == '/')
2790d29b2c44Sab196087 			len--;
2791d29b2c44Sab196087 		src--;
2792d29b2c44Sab196087 	}
2793d29b2c44Sab196087 	*src = '\0';
2794d29b2c44Sab196087 
2795d29b2c44Sab196087 
2796d29b2c44Sab196087 	/*
2797d29b2c44Sab196087 	 * Calculate space needed to hold expanded path. Note that
2798d29b2c44Sab196087 	 * this assumes that MSG_STR_MODPATH will never contain a '%o'
2799d29b2c44Sab196087 	 * code, and so, the expansion is not recursive. The codes allowed
2800d29b2c44Sab196087 	 * are:
2801d29b2c44Sab196087 	 *	%i - ISA of running elfedit (sparc, sparcv9, etc)
2802d29b2c44Sab196087 	 *	%I - 64-bit ISA: Same as %i for 64-bit versions of elfedit,
2803d29b2c44Sab196087 	 *		but yields empty string for 32-bit ISAs.
2804d29b2c44Sab196087 	 *	%o - The original (default) path.
2805d29b2c44Sab196087 	 *	%r - Root of tree holding elfedit program.
2806d29b2c44Sab196087 	 *	%% - A single %
2807d29b2c44Sab196087 	 *
2808d29b2c44Sab196087 	 * A % followed by anything else is an error. This allows us to
2809d29b2c44Sab196087 	 * add new codes in the future without backward compatability issues.
2810d29b2c44Sab196087 	 */
2811d29b2c44Sab196087 	len = modpath_strlen(path, origin_root);
2812d29b2c44Sab196087 
2813d29b2c44Sab196087 	expath = elfedit_malloc(MSG_INTL(MSG_ALLOC_EXPATH), len + 1);
2814d29b2c44Sab196087 	(void) modpath_expand(path, origin_root, expath);
2815d29b2c44Sab196087 
2816d29b2c44Sab196087 	/*
2817d29b2c44Sab196087 	 * Count path segments, eliminate extra '/', and replace ':'
2818d29b2c44Sab196087 	 * with NULL.
2819d29b2c44Sab196087 	 */
2820d29b2c44Sab196087 	state.modpath.n = 1;
2821d29b2c44Sab196087 	for (src = dst = expath; *src; src++) {
2822d29b2c44Sab196087 		if (*src == '/') {
2823d29b2c44Sab196087 			switch (*(src + 1)) {
2824d29b2c44Sab196087 			case '/':
2825d29b2c44Sab196087 			case ':':
2826d29b2c44Sab196087 			case '\0':
2827d29b2c44Sab196087 				continue;
2828d29b2c44Sab196087 			}
2829d29b2c44Sab196087 		}
2830d29b2c44Sab196087 		if (*src == ':') {
2831d29b2c44Sab196087 			state.modpath.n++;
2832d29b2c44Sab196087 			*dst = '\0';
2833d29b2c44Sab196087 		} else if (src != dst) {
2834d29b2c44Sab196087 			*dst = *src;
2835d29b2c44Sab196087 		}
2836d29b2c44Sab196087 		dst++;
2837d29b2c44Sab196087 	}
2838d29b2c44Sab196087 	if (src != dst)
2839d29b2c44Sab196087 		*dst = '\0';
2840d29b2c44Sab196087 
2841d29b2c44Sab196087 	state.modpath.seg = elfedit_malloc(MSG_INTL(MSG_ALLOC_PATHARR),
2842d29b2c44Sab196087 	    sizeof (state.modpath.seg[0]) * state.modpath.n);
2843d29b2c44Sab196087 
2844d29b2c44Sab196087 	src = expath;
2845d29b2c44Sab196087 	for (len = 0; len < state.modpath.n; len++) {
2846d29b2c44Sab196087 		if (*src == '\0') {
2847d29b2c44Sab196087 			state.modpath.seg[len] = MSG_ORIG(MSG_STR_DOT);
2848d29b2c44Sab196087 			src++;
2849d29b2c44Sab196087 		} else {
2850d29b2c44Sab196087 			state.modpath.seg[len] = src;
2851d29b2c44Sab196087 			src += strlen(src) + 1;
2852d29b2c44Sab196087 		}
2853d29b2c44Sab196087 	}
2854d29b2c44Sab196087 }
2855d29b2c44Sab196087 
2856d29b2c44Sab196087 /*
2857d29b2c44Sab196087  * When interactive (reading commands from a tty), we catch
2858d29b2c44Sab196087  * SIGINT in order to restart the outer command loop.
2859d29b2c44Sab196087  */
2860d29b2c44Sab196087 /*ARGSUSED*/
2861d29b2c44Sab196087 static void
sigint_handler(int sig,siginfo_t * sip,void * ucp)2862d29b2c44Sab196087 sigint_handler(int sig, siginfo_t *sip, void *ucp)
2863d29b2c44Sab196087 {
2864d29b2c44Sab196087 	/* Jump to the outer loop to resume */
2865d29b2c44Sab196087 	if (state.msg_jbuf.active) {
2866d29b2c44Sab196087 		state.msg_jbuf.active = 0;
2867d29b2c44Sab196087 		siglongjmp(state.msg_jbuf.env, 1);
2868d29b2c44Sab196087 	}
2869d29b2c44Sab196087 }
2870d29b2c44Sab196087 
2871d29b2c44Sab196087 
2872d29b2c44Sab196087 static void
usage(int full)2873d29b2c44Sab196087 usage(int full)
2874d29b2c44Sab196087 {
2875d29b2c44Sab196087 	elfedit_msg(ELFEDIT_MSG_USAGE, MSG_INTL(MSG_USAGE_BRIEF));
2876d29b2c44Sab196087 	if (full) {
2877d29b2c44Sab196087 		elfedit_msg(ELFEDIT_MSG_USAGE, MSG_INTL(MSG_USAGE_DETAIL1));
2878d29b2c44Sab196087 		elfedit_msg(ELFEDIT_MSG_USAGE, MSG_INTL(MSG_USAGE_DETAIL2));
2879d29b2c44Sab196087 		elfedit_msg(ELFEDIT_MSG_USAGE, MSG_INTL(MSG_USAGE_DETAIL3));
2880d29b2c44Sab196087 		elfedit_msg(ELFEDIT_MSG_USAGE, MSG_INTL(MSG_USAGE_DETAIL4));
2881d29b2c44Sab196087 		elfedit_msg(ELFEDIT_MSG_USAGE, MSG_INTL(MSG_USAGE_DETAIL5));
2882d29b2c44Sab196087 		elfedit_msg(ELFEDIT_MSG_USAGE, MSG_INTL(MSG_USAGE_DETAIL6));
2883d29b2c44Sab196087 		elfedit_msg(ELFEDIT_MSG_USAGE, MSG_INTL(MSG_USAGE_DETAIL_LAST));
2884d29b2c44Sab196087 	}
2885d29b2c44Sab196087 	elfedit_exit(2);
2886d29b2c44Sab196087 }
2887d29b2c44Sab196087 
2888d29b2c44Sab196087 
2889d29b2c44Sab196087 /*
2890d29b2c44Sab196087  * In order to complete commands, we need to know about them,
2891d29b2c44Sab196087  * which means that we need to force all the modules to be
2892d29b2c44Sab196087  * loaded. This is a relatively expensive operation, so we use
2893d29b2c44Sab196087  * this function, which avoids doing it more than once in a session.
2894d29b2c44Sab196087  */
2895d29b2c44Sab196087 static void
elfedit_cpl_load_modules(void)2896d29b2c44Sab196087 elfedit_cpl_load_modules(void)
2897d29b2c44Sab196087 {
2898d29b2c44Sab196087 	static int loaded;
2899d29b2c44Sab196087 
2900d29b2c44Sab196087 	if (!loaded) {
2901d29b2c44Sab196087 		elfedit_load_modpath();
2902d29b2c44Sab196087 		loaded = 1;	/* Don't do it again */
2903d29b2c44Sab196087 	}
2904d29b2c44Sab196087 }
2905d29b2c44Sab196087 
2906d29b2c44Sab196087 /*
2907d29b2c44Sab196087  * Compare the token to the given string, and if they share a common
2908d29b2c44Sab196087  * initial sequence, add the tail of string to the tecla command completion
2909d29b2c44Sab196087  * buffer:
2910d29b2c44Sab196087  *
2911d29b2c44Sab196087  * entry:
2912d29b2c44Sab196087  *	cpldata - Current completion state
2913d29b2c44Sab196087  *	str - String to match against token
2914d29b2c44Sab196087  *	casefold - True to allow case insensitive completion, False
2915d29b2c44Sab196087  *		if case must match exactly.
2916d29b2c44Sab196087  */
2917d29b2c44Sab196087 void
elfedit_cpl_match(void * cpldata,const char * str,int casefold)2918d29b2c44Sab196087 elfedit_cpl_match(void *cpldata, const char *str, int casefold)
2919d29b2c44Sab196087 {
2920d29b2c44Sab196087 	ELFEDIT_CPL_STATE *cstate = (ELFEDIT_CPL_STATE *) cpldata;
2921d29b2c44Sab196087 	const char	*cont_suffix;
2922d29b2c44Sab196087 	const char	*type_suffix;
2923d29b2c44Sab196087 
2924d29b2c44Sab196087 	/*
2925d29b2c44Sab196087 	 * Reasons to return immediately:
2926d29b2c44Sab196087 	 *	- NULL strings have no completion value
2927d29b2c44Sab196087 	 *	- The string is shorter than the existing item being completed
2928d29b2c44Sab196087 	 */
2929d29b2c44Sab196087 	if ((str == NULL) || (*str == '\0') ||
2930d29b2c44Sab196087 	    ((cstate->ecpl_token_len != 0) &&
2931d29b2c44Sab196087 	    ((strlen(str) < cstate->ecpl_token_len))))
2932d29b2c44Sab196087 		return;
2933d29b2c44Sab196087 
2934d29b2c44Sab196087 	/* If the string does not share the existing prefix, don't use it */
2935d29b2c44Sab196087 	if (casefold) {
2936d29b2c44Sab196087 		if (strncasecmp(cstate->ecpl_token_str, str,
2937d29b2c44Sab196087 		    cstate->ecpl_token_len) != 0)
2938d29b2c44Sab196087 			return;
2939d29b2c44Sab196087 	} else {
2940d29b2c44Sab196087 		if (strncmp(cstate->ecpl_token_str, str,
2941d29b2c44Sab196087 		    cstate->ecpl_token_len) != 0)
2942d29b2c44Sab196087 			return;
2943d29b2c44Sab196087 	}
2944d29b2c44Sab196087 
2945d29b2c44Sab196087 	if (cstate->ecpl_add_mod_colon) {
2946d29b2c44Sab196087 		cont_suffix = type_suffix = MSG_ORIG(MSG_STR_COLON);
2947d29b2c44Sab196087 	} else {
2948d29b2c44Sab196087 		cont_suffix = MSG_ORIG(MSG_STR_SPACE);
2949d29b2c44Sab196087 		type_suffix = NULL;
2950d29b2c44Sab196087 	}
2951d29b2c44Sab196087 	(void) cpl_add_completion(cstate->ecpl_cpl, cstate->ecpl_line,
2952d29b2c44Sab196087 	    cstate->ecpl_word_start, cstate->ecpl_word_end,
2953d29b2c44Sab196087 	    str + cstate->ecpl_token_len, type_suffix, cont_suffix);
2954d29b2c44Sab196087 
2955d29b2c44Sab196087 }
2956d29b2c44Sab196087 
2957d29b2c44Sab196087 
2958d29b2c44Sab196087 /*
295955ef6355Sab196087  * Convenience wrapper on elfedit_cpl_match(): Format an unsigned
296055ef6355Sab196087  * 32-bit integer as a string and enter the result for command completion.
296155ef6355Sab196087  */
296255ef6355Sab196087 void
elfedit_cpl_ndx(void * cpldata,uint_t ndx)296355ef6355Sab196087 elfedit_cpl_ndx(void *cpldata, uint_t ndx)
296455ef6355Sab196087 {
296555ef6355Sab196087 	Conv_inv_buf_t	buf;
296655ef6355Sab196087 
296755ef6355Sab196087 	(void) snprintf(buf.buf, sizeof (buf.buf),
296855ef6355Sab196087 	    MSG_ORIG(MSG_FMT_WORDVAL), ndx);
296955ef6355Sab196087 	elfedit_cpl_match(cpldata, buf.buf, 0);
297055ef6355Sab196087 }
297155ef6355Sab196087 
297255ef6355Sab196087 
297355ef6355Sab196087 /*
2974d29b2c44Sab196087  * Compare the token to the names of the commands from the given module,
2975d29b2c44Sab196087  * and if they share a common initial sequence, add the tail of string
2976d29b2c44Sab196087  * to the tecla command completion buffer:
2977d29b2c44Sab196087  *
2978d29b2c44Sab196087  * entry:
2979d29b2c44Sab196087  *	tok_buf - Token user has entered
2980d29b2c44Sab196087  *	tok_len - strlen(tok_buf)
2981d29b2c44Sab196087  *	mod - Module definition from which commands should be matched
2982d29b2c44Sab196087  *	cpl, line, word_start, word_end, cont_suffix - As documented
2983d29b2c44Sab196087  *		for gl_get_line() and cpl_add_completion.
2984d29b2c44Sab196087  */
2985d29b2c44Sab196087 static void
match_module_cmds(ELFEDIT_CPL_STATE * cstate,elfeditGC_module_t * mod)2986d29b2c44Sab196087 match_module_cmds(ELFEDIT_CPL_STATE *cstate, elfeditGC_module_t *mod)
2987d29b2c44Sab196087 {
2988d29b2c44Sab196087 	elfeditGC_cmd_t *cmd;
2989d29b2c44Sab196087 	const char **cmd_name;
2990d29b2c44Sab196087 
2991d29b2c44Sab196087 	for (cmd = mod->mod_cmds; cmd->cmd_func != NULL; cmd++)
2992d29b2c44Sab196087 		for (cmd_name = cmd->cmd_name; *cmd_name; cmd_name++)
2993d29b2c44Sab196087 			elfedit_cpl_match(cstate, *cmd_name, 1);
2994d29b2c44Sab196087 }
2995d29b2c44Sab196087 
2996d29b2c44Sab196087 
2997d29b2c44Sab196087 /*
2998d29b2c44Sab196087  * Compare the token to the known module names, and add those that
2999d29b2c44Sab196087  * match to the list of alternatives via elfedit_cpl_match().
3000d29b2c44Sab196087  *
3001d29b2c44Sab196087  * entry:
3002d29b2c44Sab196087  *	load_all_modules - If True, causes all modules to be loaded
3003d29b2c44Sab196087  *		before processing is done. If False, only the modules
3004d29b2c44Sab196087  *		currently seen will be used.
3005d29b2c44Sab196087  */
3006d29b2c44Sab196087 void
elfedit_cpl_module(void * cpldata,int load_all_modules)3007d29b2c44Sab196087 elfedit_cpl_module(void *cpldata, int load_all_modules)
3008d29b2c44Sab196087 {
3009d29b2c44Sab196087 	ELFEDIT_CPL_STATE	*cstate = (ELFEDIT_CPL_STATE *) cpldata;
3010d29b2c44Sab196087 	MODLIST_T		*modlist;
3011d29b2c44Sab196087 
3012d29b2c44Sab196087 	if (load_all_modules)
3013d29b2c44Sab196087 		elfedit_cpl_load_modules();
3014d29b2c44Sab196087 
3015d29b2c44Sab196087 	for (modlist = state.modlist; modlist != NULL;
3016d29b2c44Sab196087 	    modlist = modlist->ml_next) {
3017d29b2c44Sab196087 		elfedit_cpl_match(cstate, modlist->ml_mod->mod_name, 1);
3018d29b2c44Sab196087 	}
3019d29b2c44Sab196087 }
3020d29b2c44Sab196087 
3021d29b2c44Sab196087 
3022d29b2c44Sab196087 /*
3023d29b2c44Sab196087  * Compare the token to all the known commands, and add those that
3024d29b2c44Sab196087  * match to the list of alternatives.
3025d29b2c44Sab196087  *
3026d29b2c44Sab196087  * note:
3027d29b2c44Sab196087  *	This routine will force modules to be loaded as necessary to
3028d29b2c44Sab196087  *	obtain the names it needs to match.
3029d29b2c44Sab196087  */
3030d29b2c44Sab196087 void
elfedit_cpl_command(void * cpldata)3031d29b2c44Sab196087 elfedit_cpl_command(void *cpldata)
3032d29b2c44Sab196087 {
3033d29b2c44Sab196087 	ELFEDIT_CPL_STATE	*cstate = (ELFEDIT_CPL_STATE *) cpldata;
3034d29b2c44Sab196087 	ELFEDIT_CPL_STATE	colon_state;
3035d29b2c44Sab196087 	const char		*colon_pos;
3036d29b2c44Sab196087 	MODLIST_T		*modlist;
3037d29b2c44Sab196087 	MODLIST_T		*insdef;
3038d29b2c44Sab196087 	char			buf[128];
3039d29b2c44Sab196087 
3040d29b2c44Sab196087 	/*
3041d29b2c44Sab196087 	 * Is there a colon in the command? If so, locate its offset within
3042d29b2c44Sab196087 	 * the raw input line.
3043d29b2c44Sab196087 	 */
3044d29b2c44Sab196087 	for (colon_pos = cstate->ecpl_token_str;
3045d29b2c44Sab196087 	    *colon_pos && (*colon_pos != ':'); colon_pos++)
3046d29b2c44Sab196087 		;
3047d29b2c44Sab196087 
3048d29b2c44Sab196087 	/*
3049d29b2c44Sab196087 	 * If no colon was seen, then we are completing a module name,
3050d29b2c44Sab196087 	 * or one of the commands from 'sys:'
3051d29b2c44Sab196087 	 */
3052d29b2c44Sab196087 	if (*colon_pos == '\0') {
3053d29b2c44Sab196087 		/*
3054d29b2c44Sab196087 		 * Setting cstate->add_mod_colon tells elfedit_cpl_match()
3055d29b2c44Sab196087 		 * to add an implicit ':' to the names it matches. We use it
3056d29b2c44Sab196087 		 * here so the user doesn't have to enter the ':' manually.
3057d29b2c44Sab196087 		 * Hiding this in the opaque state instead of making it
3058d29b2c44Sab196087 		 * an argument to that function gives us the ability to
3059d29b2c44Sab196087 		 * change it later without breaking the published interface.
3060d29b2c44Sab196087 		 */
3061d29b2c44Sab196087 		cstate->ecpl_add_mod_colon = 1;
3062d29b2c44Sab196087 		elfedit_cpl_module(cpldata, 1);
3063d29b2c44Sab196087 		cstate->ecpl_add_mod_colon = 0;
3064d29b2c44Sab196087 
3065d29b2c44Sab196087 		/* Add bare (no sys: prefix) commands from the sys: module */
3066d29b2c44Sab196087 		match_module_cmds(cstate,
3067d29b2c44Sab196087 		    elfedit_load_module(MSG_ORIG(MSG_MOD_SYS), 1, 0));
3068d29b2c44Sab196087 
3069d29b2c44Sab196087 		return;
3070d29b2c44Sab196087 	}
3071d29b2c44Sab196087 
3072d29b2c44Sab196087 	/*
3073d29b2c44Sab196087 	 * A colon was seen, so we have a module name. Extract the name,
3074d29b2c44Sab196087 	 * substituting 'sys' for the case where the given name is empty.
3075d29b2c44Sab196087 	 */
3076d29b2c44Sab196087 	if (colon_pos == 0)
3077d29b2c44Sab196087 		(void) strlcpy(buf, MSG_ORIG(MSG_MOD_SYS), sizeof (buf));
3078d29b2c44Sab196087 	else
3079d29b2c44Sab196087 		elfedit_strnbcpy(buf, cstate->ecpl_token_str,
3080d29b2c44Sab196087 		    colon_pos - cstate->ecpl_token_str, sizeof (buf));
3081d29b2c44Sab196087 
3082d29b2c44Sab196087 	/*
3083d29b2c44Sab196087 	 * Locate the module. If it isn't already loaded, make an explicit
3084d29b2c44Sab196087 	 * attempt to load it and try again. If a module definition is
3085d29b2c44Sab196087 	 * obtained, process the commands it supplies.
3086d29b2c44Sab196087 	 */
3087d29b2c44Sab196087 	modlist = module_loaded(buf, &insdef);
3088d29b2c44Sab196087 	if (modlist == NULL) {
3089d29b2c44Sab196087 		(void) elfedit_load_module(buf, 0, 0);
3090d29b2c44Sab196087 		modlist = module_loaded(buf, &insdef);
3091d29b2c44Sab196087 	}
3092d29b2c44Sab196087 	if (modlist != NULL) {
3093d29b2c44Sab196087 		/*
3094d29b2c44Sab196087 		 * Make a copy of the cstate, and adjust the line and
3095d29b2c44Sab196087 		 * token so that the new one starts just past the colon
3096d29b2c44Sab196087 		 * character. We know that the colon exists because
3097d29b2c44Sab196087 		 * of the preceeding test that found it. Therefore, we do
3098d29b2c44Sab196087 		 * not need to test against running off the end of the
3099d29b2c44Sab196087 		 * string here.
3100d29b2c44Sab196087 		 */
3101d29b2c44Sab196087 		colon_state = *cstate;
3102d29b2c44Sab196087 		while (colon_state.ecpl_line[colon_state.ecpl_word_start] !=
3103d29b2c44Sab196087 		    ':')
3104d29b2c44Sab196087 			colon_state.ecpl_word_start++;
3105d29b2c44Sab196087 		while (*colon_state.ecpl_token_str != ':') {
3106d29b2c44Sab196087 			colon_state.ecpl_token_str++;
3107d29b2c44Sab196087 			colon_state.ecpl_token_len--;
3108d29b2c44Sab196087 		}
3109d29b2c44Sab196087 		/* Skip past the ':' character */
3110d29b2c44Sab196087 		colon_state.ecpl_word_start++;
3111d29b2c44Sab196087 		colon_state.ecpl_token_str++;
3112d29b2c44Sab196087 		colon_state.ecpl_token_len--;
3113d29b2c44Sab196087 
3114d29b2c44Sab196087 		match_module_cmds(&colon_state, modlist->ml_mod);
3115d29b2c44Sab196087 	}
3116d29b2c44Sab196087 }
3117d29b2c44Sab196087 
3118d29b2c44Sab196087 
3119d29b2c44Sab196087 /*
3120d29b2c44Sab196087  * Command completion function for use with libtacla.
3121d29b2c44Sab196087  */
3122d29b2c44Sab196087 /*ARGSUSED1*/
3123d29b2c44Sab196087 static int
cmd_match_fcn(WordCompletion * cpl,void * data,const char * line,int word_end)3124d29b2c44Sab196087 cmd_match_fcn(WordCompletion *cpl, void *data, const char *line, int word_end)
3125d29b2c44Sab196087 {
3126d29b2c44Sab196087 	const char		*argv[ELFEDIT_MAXCPLARGS];
3127d29b2c44Sab196087 	ELFEDIT_CPL_STATE	cstate;
3128d29b2c44Sab196087 	TOK_STATE		*tokst;
3129d29b2c44Sab196087 	int			ndx;
3130d29b2c44Sab196087 	int			i;
3131d29b2c44Sab196087 	elfeditGC_module_t	*mod;
3132d29b2c44Sab196087 	elfeditGC_cmd_t		*cmd;
3133d29b2c44Sab196087 	int			num_opt;
3134d29b2c44Sab196087 	int			opt_term_seen;
3135d29b2c44Sab196087 	int			skip_one;
3136d29b2c44Sab196087 	elfedit_cmd_optarg_t	*optarg;
3137d29b2c44Sab196087 	elfedit_optarg_item_t	item;
3138d29b2c44Sab196087 	int			ostyle_ndx = -1;
3139d29b2c44Sab196087 
3140d29b2c44Sab196087 	/*
3141d29b2c44Sab196087 	 * For debugging, enable the following block. It tells the tecla
3142d29b2c44Sab196087 	 * library that the program using is going to write to stdout.
3143d29b2c44Sab196087 	 * It will put the tty back into normal mode, and it will cause
3144d29b2c44Sab196087 	 * tecla to redraw the current input line when it gets control back.
3145d29b2c44Sab196087 	 */
3146cce0e03bSab196087 #ifdef DEBUG_CMD_MATCH
3147d29b2c44Sab196087 	gl_normal_io(state.input.gl);
3148d29b2c44Sab196087 #endif
3149d29b2c44Sab196087 
3150d29b2c44Sab196087 	/*
3151d29b2c44Sab196087 	 * Tokenize the line up through word_end. The last token in
3152d29b2c44Sab196087 	 * the list is the one requiring completion.
3153d29b2c44Sab196087 	 */
3154d29b2c44Sab196087 	tokst = tokenize_user_cmd(line, word_end, 1);
3155d29b2c44Sab196087 	if (tokst->tokst_cnt == 0)
3156d29b2c44Sab196087 		return (0);
3157d29b2c44Sab196087 
3158d29b2c44Sab196087 	/* Set up the cstate block, containing the completion state */
3159d29b2c44Sab196087 	ndx = tokst->tokst_cnt - 1;	/* Index of token to complete */
3160d29b2c44Sab196087 	cstate.ecpl_cpl = cpl;
3161d29b2c44Sab196087 	cstate.ecpl_line = line;
3162d29b2c44Sab196087 	cstate.ecpl_word_start = tokst->tokst_buf[ndx].tok_line_off;
3163d29b2c44Sab196087 	cstate.ecpl_word_end = word_end;
3164d29b2c44Sab196087 	cstate.ecpl_add_mod_colon = 0;
3165d29b2c44Sab196087 	cstate.ecpl_token_str = tokst->tokst_buf[ndx].tok_str;
3166d29b2c44Sab196087 	cstate.ecpl_token_len = tokst->tokst_buf[ndx].tok_len;
3167d29b2c44Sab196087 
3168d29b2c44Sab196087 	/*
3169d29b2c44Sab196087 	 * If there is only one token, then we are completing the
3170d29b2c44Sab196087 	 * command itself.
3171d29b2c44Sab196087 	 */
3172d29b2c44Sab196087 	if (ndx == 0) {
3173d29b2c44Sab196087 		elfedit_cpl_command(&cstate);
3174d29b2c44Sab196087 		return (0);
3175d29b2c44Sab196087 	}
3176d29b2c44Sab196087 
3177d29b2c44Sab196087 	/*
3178d29b2c44Sab196087 	 * There is more than one token. Use the first one to
3179d29b2c44Sab196087 	 * locate the definition for the command. If we don't have
3180d29b2c44Sab196087 	 * a definition for the command, then there's nothing more
3181d29b2c44Sab196087 	 * we can do.
3182d29b2c44Sab196087 	 */
3183d29b2c44Sab196087 	cmd = elfedit_find_command(tokst->tokst_buf[0].tok_str, 0, &mod);
3184d29b2c44Sab196087 	if (cmd == NULL)
3185d29b2c44Sab196087 		return (0);
3186d29b2c44Sab196087 
3187d29b2c44Sab196087 	/*
3188d29b2c44Sab196087 	 * Since we know the command, give them a quick usage message.
3189d29b2c44Sab196087 	 * It may be that they just need a quick reminder about the form
3190d29b2c44Sab196087 	 * of the command and the options.
3191d29b2c44Sab196087 	 */
3192d29b2c44Sab196087 	(void) gl_normal_io(state.input.gl);
3193d29b2c44Sab196087 	elfedit_printf(MSG_INTL(MSG_USAGE_CMD),
3194d29b2c44Sab196087 	    elfedit_format_command_usage(mod, cmd, NULL, 0));
3195d29b2c44Sab196087 
3196d29b2c44Sab196087 
3197d29b2c44Sab196087 	/*
3198d29b2c44Sab196087 	 * We have a generous setting for ELFEDIT_MAXCPLARGS, so there
3199d29b2c44Sab196087 	 * should always be plenty of room. If there's not room, we
3200d29b2c44Sab196087 	 * can't proceed.
3201d29b2c44Sab196087 	 */
3202d29b2c44Sab196087 	if (ndx >= ELFEDIT_MAXCPLARGS)
3203d29b2c44Sab196087 		return (0);
3204d29b2c44Sab196087 
3205d29b2c44Sab196087 	/*
3206d29b2c44Sab196087 	 * Put pointers to the tokens into argv, and determine how
3207d29b2c44Sab196087 	 * many of the tokens are optional arguments.
3208d29b2c44Sab196087 	 *
3209d29b2c44Sab196087 	 * We consider the final optional argument to be the rightmost
3210d29b2c44Sab196087 	 * argument that starts with a '-'. If a '--' is seen, then
3211d29b2c44Sab196087 	 * we stop there, and any argument that follows is a plain argument
3212d29b2c44Sab196087 	 * (even if it starts with '-').
3213d29b2c44Sab196087 	 *
3214d29b2c44Sab196087 	 * We look for an inherited '-o' option, because we are willing
3215d29b2c44Sab196087 	 * to supply command completion for these values.
3216d29b2c44Sab196087 	 */
3217d29b2c44Sab196087 	num_opt = 0;
3218d29b2c44Sab196087 	opt_term_seen = 0;
3219d29b2c44Sab196087 	skip_one = 0;
3220d29b2c44Sab196087 	for (i = 0; i < ndx; i++) {
3221d29b2c44Sab196087 		argv[i] = tokst->tokst_buf[i + 1].tok_str;
3222d29b2c44Sab196087 		if (opt_term_seen || skip_one) {
3223d29b2c44Sab196087 			skip_one = 0;
3224d29b2c44Sab196087 			continue;
3225d29b2c44Sab196087 		}
3226d29b2c44Sab196087 		skip_one = 0;
3227d29b2c44Sab196087 		ostyle_ndx = -1;
3228d29b2c44Sab196087 		if ((strcmp(argv[i], MSG_ORIG(MSG_STR_MINUS_MINUS)) == NULL) ||
3229d29b2c44Sab196087 		    (*argv[i] != '-')) {
3230d29b2c44Sab196087 			opt_term_seen = 1;
3231d29b2c44Sab196087 			continue;
3232d29b2c44Sab196087 		}
3233d29b2c44Sab196087 		num_opt = i + 1;
3234d29b2c44Sab196087 		/*
3235d29b2c44Sab196087 		 * If it is a recognised ELFEDIT_CMDOA_F_VALUE option,
3236d29b2c44Sab196087 		 * then the item following it is the associated value.
3237d29b2c44Sab196087 		 * Check for this and skip the value.
3238d29b2c44Sab196087 		 *
3239d29b2c44Sab196087 		 * At the same time, look for STDOA_OPT_O inherited
3240d29b2c44Sab196087 		 * options. We want to identify the index of any such
3241d29b2c44Sab196087 		 * item. Although the option is simply "-o", we are willing
3242d29b2c44Sab196087 		 * to treat any option that starts with "-o" as a potential
3243d29b2c44Sab196087 		 * STDOA_OPT_O. This lets us to command completion for things
3244d29b2c44Sab196087 		 * like "-onum", and is otherwise harmless, the only cost
3245d29b2c44Sab196087 		 * being a few additional strcmps by the cpl code.
3246d29b2c44Sab196087 		 */
3247d29b2c44Sab196087 		if ((optarg = cmd->cmd_opt) == NULL)
3248d29b2c44Sab196087 			continue;
3249d29b2c44Sab196087 		while (optarg->oa_name != NULL) {
3250d29b2c44Sab196087 			int is_ostyle_optarg =
3251d29b2c44Sab196087 			    (optarg->oa_flags & ELFEDIT_CMDOA_F_INHERIT) &&
3252d29b2c44Sab196087 			    (optarg->oa_name == ELFEDIT_STDOA_OPT_O);
3253d29b2c44Sab196087 
3254d29b2c44Sab196087 			elfedit_next_optarg(&optarg, &item);
3255d29b2c44Sab196087 			if (item.oai_flags & ELFEDIT_CMDOA_F_VALUE) {
3256d29b2c44Sab196087 				if (is_ostyle_optarg && (strncmp(argv[i],
3257d29b2c44Sab196087 				    MSG_ORIG(MSG_STR_MINUS_O), 2) == 0))
3258d29b2c44Sab196087 					ostyle_ndx = i + 1;
3259d29b2c44Sab196087 
3260d29b2c44Sab196087 				if (strcmp(item.oai_name, argv[i]) == 0) {
3261d29b2c44Sab196087 					num_opt = i + 2;
3262d29b2c44Sab196087 					skip_one = 1;
3263d29b2c44Sab196087 					break;
3264d29b2c44Sab196087 				}
3265d29b2c44Sab196087 				/*
3266d29b2c44Sab196087 				 * If it didn't match "-o" exactly, but it is
3267d29b2c44Sab196087 				 * ostyle_ndx, then it is a potential combined
3268d29b2c44Sab196087 				 * STDOA_OPT_O, as discussed above. It counts
3269d29b2c44Sab196087 				 * as a single argument.
3270d29b2c44Sab196087 				 */
3271d29b2c44Sab196087 				if (ostyle_ndx == ndx)
3272d29b2c44Sab196087 					break;
3273d29b2c44Sab196087 			}
3274d29b2c44Sab196087 		}
3275d29b2c44Sab196087 	}
3276d29b2c44Sab196087 
3277cce0e03bSab196087 #ifdef DEBUG_CMD_MATCH
3278cce0e03bSab196087 	(void) printf("NDX(%d) NUM_OPT(%d) ostyle_ndx(%d)\n", ndx, num_opt,
3279d29b2c44Sab196087 	    ostyle_ndx);
3280d29b2c44Sab196087 #endif
3281d29b2c44Sab196087 
3282d29b2c44Sab196087 	if (ostyle_ndx != -1) {
3283d29b2c44Sab196087 		/*
3284d29b2c44Sab196087 		 * If ostyle_ndx is one less than ndx, and ndx is
3285d29b2c44Sab196087 		 * the same as num_opt, then we have a definitive
3286d29b2c44Sab196087 		 * STDOA_OPT_O inherited outstyle option. We supply
3287d29b2c44Sab196087 		 * the value strings, and are done.
3288d29b2c44Sab196087 		 */
3289d29b2c44Sab196087 		if ((ostyle_ndx == (ndx - 1)) && (ndx == num_opt)) {
3290d29b2c44Sab196087 			elfedit_cpl_atoconst(&cstate, ELFEDIT_CONST_OUTSTYLE);
3291d29b2c44Sab196087 			return (0);
3292d29b2c44Sab196087 		}
3293d29b2c44Sab196087 
3294d29b2c44Sab196087 		/*
3295d29b2c44Sab196087 		 * If ostyle is the same as ndx, then we have an option
3296d29b2c44Sab196087 		 * staring with "-o" that may end up being a STDOA_OPT_O,
3297d29b2c44Sab196087 		 * and we are still inside that token. In this case, we
3298d29b2c44Sab196087 		 * supply completion strings that include the leading
3299d29b2c44Sab196087 		 * "-o" followed by the values, without a space
3300d29b2c44Sab196087 		 * (i.e. "-onum"). We then fall through, allowing any
3301d29b2c44Sab196087 		 * other options starting with "-o" to be added
3302d29b2c44Sab196087 		 * below. elfedit_cpl_match() will throw out the incorrect
3303d29b2c44Sab196087 		 * options, so it is harmless to add these extra items in
3304d29b2c44Sab196087 		 * the worst case, and useful otherwise.
3305d29b2c44Sab196087 		 */
3306d29b2c44Sab196087 		if (ostyle_ndx == ndx)
3307d29b2c44Sab196087 			elfedit_cpl_atoconst(&cstate,
3308d29b2c44Sab196087 			    ELFEDIT_CONST_OUTSTYLE_MO);
3309d29b2c44Sab196087 	}
3310d29b2c44Sab196087 
3311d29b2c44Sab196087 	/*
3312d29b2c44Sab196087 	 * If (ndx <= num_opt), then the token needing completion
3313d29b2c44Sab196087 	 * is an option. If the leading '-' is there, then we should fill
3314d29b2c44Sab196087 	 * in all of the option alternatives. If anything follows the '-'
3315d29b2c44Sab196087 	 * though, we assume that the user has already figured out what
3316d29b2c44Sab196087 	 * option to use, and we leave well enough alone.
3317d29b2c44Sab196087 	 *
3318d29b2c44Sab196087 	 * Note that we are intentionally ignoring a related case
3319d29b2c44Sab196087 	 * where supplying option strings would be legal: In the case
3320d29b2c44Sab196087 	 * where we are one past the last option (ndx == (num_opt + 1)),
3321d29b2c44Sab196087 	 * and the current option is an empty string, the argument can
3322d29b2c44Sab196087 	 * be either a plain argument or an option --- the user needs to
3323d29b2c44Sab196087 	 * enter the next character before we can tell. It would be
3324d29b2c44Sab196087 	 * OK to enter the option strings in this case. However, consider
3325d29b2c44Sab196087 	 * what happens when the first plain argument to the command does
3326d29b2c44Sab196087 	 * not provide any command completion (e.g. it is a plain integer).
3327d29b2c44Sab196087 	 * In this case, tecla will see that all the alternatives start
3328d29b2c44Sab196087 	 * with '-', and will insert a '-' into the input. If the user
3329d29b2c44Sab196087 	 * intends the next argument to be plain, they will have to delete
3330d29b2c44Sab196087 	 * this '-', which is annoying. Worse than that, they may be confused
3331d29b2c44Sab196087 	 * by it, and think that the plain argument is not allowed there.
3332d29b2c44Sab196087 	 * The best solution is to not supply option strings unless the
3333d29b2c44Sab196087 	 * user first enters the '-'.
3334d29b2c44Sab196087 	 */
3335d29b2c44Sab196087 	if ((ndx <= num_opt) && (argv[ndx - 1][0] == '-')) {
3336d29b2c44Sab196087 		if ((optarg = cmd->cmd_opt) != NULL) {
3337d29b2c44Sab196087 			while (optarg->oa_name != NULL) {
3338d29b2c44Sab196087 				elfedit_next_optarg(&optarg, &item);
3339d29b2c44Sab196087 				elfedit_cpl_match(&cstate, item.oai_name, 1);
3340d29b2c44Sab196087 			}
3341d29b2c44Sab196087 		}
3342d29b2c44Sab196087 		return (0);
3343d29b2c44Sab196087 	}
3344d29b2c44Sab196087 
3345d29b2c44Sab196087 	/*
3346d29b2c44Sab196087 	 * At this point we know that ndx and num_opt are not equal.
3347d29b2c44Sab196087 	 * If num_opt is larger than ndx, then we have an ELFEDIT_CMDOA_F_VALUE
3348d29b2c44Sab196087 	 * argument at the end, and the following value has not been entered.
3349d29b2c44Sab196087 	 *
3350d29b2c44Sab196087 	 * If ndx is greater than num_opt, it means that we are looking
3351d29b2c44Sab196087 	 * at a plain argument (or in the case where (ndx == (num_opt + 1)),
3352d29b2c44Sab196087 	 * a *potential* plain argument.
3353d29b2c44Sab196087 	 *
3354d29b2c44Sab196087 	 * If the command has a completion function registered, then we
3355d29b2c44Sab196087 	 * hand off the remaining work to it. The cmd_cplfunc field is
3356d29b2c44Sab196087 	 * the generic definition. We need to cast it to the type that matches
3357d29b2c44Sab196087 	 * the proper ELFCLASS before calling it.
3358d29b2c44Sab196087 	 */
3359d29b2c44Sab196087 	if (state.elf.elfclass == ELFCLASS32) {
3360d29b2c44Sab196087 		elfedit32_cmdcpl_func_t *cmdcpl_func =
3361d29b2c44Sab196087 		    (elfedit32_cmdcpl_func_t *)cmd->cmd_cplfunc;
3362d29b2c44Sab196087 
3363d29b2c44Sab196087 		if (cmdcpl_func != NULL)
3364d29b2c44Sab196087 			(* cmdcpl_func)(state.elf.obj_state.s32,
3365d29b2c44Sab196087 			    &cstate, ndx, argv, num_opt);
3366d29b2c44Sab196087 	} else {
3367d29b2c44Sab196087 		elfedit64_cmdcpl_func_t *cmdcpl_func =
3368d29b2c44Sab196087 		    (elfedit64_cmdcpl_func_t *)cmd->cmd_cplfunc;
3369d29b2c44Sab196087 
3370d29b2c44Sab196087 		if (cmdcpl_func != NULL)
3371d29b2c44Sab196087 			(* cmdcpl_func)(state.elf.obj_state.s64,
3372d29b2c44Sab196087 			    &cstate, ndx, argv, num_opt);
3373d29b2c44Sab196087 	}
3374d29b2c44Sab196087 
3375d29b2c44Sab196087 	return (0);
3376d29b2c44Sab196087 }
3377d29b2c44Sab196087 
3378d29b2c44Sab196087 
3379d29b2c44Sab196087 /*
3380d29b2c44Sab196087  * Read a line of input from stdin, and return pointer to it.
3381d29b2c44Sab196087  *
3382d29b2c44Sab196087  * This routine uses a private buffer, so the contents of the returned
3383d29b2c44Sab196087  * string are only good until the next call.
3384d29b2c44Sab196087  */
3385d29b2c44Sab196087 static const char *
read_cmd(void)3386d29b2c44Sab196087 read_cmd(void)
3387d29b2c44Sab196087 {
3388d29b2c44Sab196087 	char *s;
3389d29b2c44Sab196087 
3390d29b2c44Sab196087 	if (state.input.full_tty) {
3391d29b2c44Sab196087 		state.input.in_tecla = TRUE;
3392d29b2c44Sab196087 		s = gl_get_line(state.input.gl,
3393d29b2c44Sab196087 		    MSG_ORIG(MSG_STR_PROMPT), NULL, -1);
3394d29b2c44Sab196087 		state.input.in_tecla = FALSE;
3395d29b2c44Sab196087 		/*
3396d29b2c44Sab196087 		 * gl_get_line() returns NULL for EOF or for error. EOF is fine,
3397d29b2c44Sab196087 		 * but we need to catch and report anything else. Since
3398d29b2c44Sab196087 		 * reading from stdin is critical to our operation, an
3399d29b2c44Sab196087 		 * error implies that we cannot recover and must exit.
3400d29b2c44Sab196087 		 */
3401d29b2c44Sab196087 		if ((s == NULL) &&
3402d29b2c44Sab196087 		    (gl_return_status(state.input.gl) == GLR_ERROR)) {
3403d29b2c44Sab196087 			elfedit_msg(ELFEDIT_MSG_FATAL, MSG_INTL(MSG_ERR_GLREAD),
3404d29b2c44Sab196087 			    gl_error_message(state.input.gl, NULL, 0));
3405d29b2c44Sab196087 		}
3406d29b2c44Sab196087 	} else {
3407d29b2c44Sab196087 		/*
3408d29b2c44Sab196087 		 * This should be a dynamically sized buffer, but for now,
3409d29b2c44Sab196087 		 * I'm going to take a simpler path.
3410d29b2c44Sab196087 		 */
3411d29b2c44Sab196087 		static char cmd_buf[ELFEDIT_MAXCMD + 1];
3412d29b2c44Sab196087 
3413d29b2c44Sab196087 		s = fgets(cmd_buf, sizeof (cmd_buf), stdin);
3414d29b2c44Sab196087 	}
3415d29b2c44Sab196087 
3416d29b2c44Sab196087 	/* Return user string, or 'quit' on EOF */
3417d29b2c44Sab196087 	return (s ? s : MSG_ORIG(MSG_SYS_CMD_QUIT));
3418d29b2c44Sab196087 }
3419d29b2c44Sab196087 
3420d29b2c44Sab196087 int
main(int argc,char ** argv,char ** envp)3421d29b2c44Sab196087 main(int argc, char **argv, char **envp)
3422d29b2c44Sab196087 {
3423d29b2c44Sab196087 	/*
3424d29b2c44Sab196087 	 * Note: This function can use setjmp()/longjmp() which does
3425d29b2c44Sab196087 	 * not preserve the values of auto/register variables. Hence,
3426d29b2c44Sab196087 	 * variables that need their values preserved across a jump must
3427d29b2c44Sab196087 	 * be marked volatile, or must not be auto/register.
3428d29b2c44Sab196087 	 *
3429d29b2c44Sab196087 	 * Volatile can be messy, because it requires explictly casting
3430d29b2c44Sab196087 	 * away the attribute when passing it to functions, or declaring
3431d29b2c44Sab196087 	 * those functions with the attribute as well. In a single threaded
3432d29b2c44Sab196087 	 * program like this one, an easier approach is to make things
3433d29b2c44Sab196087 	 * static. That can be done here, or by putting things in the
3434d29b2c44Sab196087 	 * 'state' structure.
3435d29b2c44Sab196087 	 */
3436d29b2c44Sab196087 
3437d29b2c44Sab196087 	int		c, i;
3438d29b2c44Sab196087 	int		num_batch = 0;
3439d29b2c44Sab196087 	char		**batch_list = NULL;
3440d29b2c44Sab196087 	const char	*modpath = NULL;
3441d29b2c44Sab196087 
3442d29b2c44Sab196087 	/*
3443d29b2c44Sab196087 	 * Always have liblddb display unclipped section names.
3444d29b2c44Sab196087 	 * This global is exported by liblddb, and declared in debug.h.
3445d29b2c44Sab196087 	 */
3446d29b2c44Sab196087 	dbg_desc->d_extra |= DBG_E_LONG;
3447d29b2c44Sab196087 
3448d29b2c44Sab196087 	opterr = 0;
3449d29b2c44Sab196087 	while ((c = getopt(argc, argv, MSG_ORIG(MSG_STR_OPTIONS))) != EOF) {
3450d29b2c44Sab196087 		switch (c) {
3451d29b2c44Sab196087 		case 'a':
3452d29b2c44Sab196087 			state.flags |= ELFEDIT_F_AUTOPRINT;
3453d29b2c44Sab196087 			break;
3454d29b2c44Sab196087 
3455d29b2c44Sab196087 		case 'd':
3456d29b2c44Sab196087 			state.flags |= ELFEDIT_F_DEBUG;
3457d29b2c44Sab196087 			break;
3458d29b2c44Sab196087 
3459d29b2c44Sab196087 		case 'e':
3460d29b2c44Sab196087 			/*
3461d29b2c44Sab196087 			 * Delay parsing the -e options until after the call to
3462d29b2c44Sab196087 			 * conv_check_native() so that we won't bother loading
3463d29b2c44Sab196087 			 * modules of the wrong class.
3464d29b2c44Sab196087 			 */
3465d29b2c44Sab196087 			if (batch_list == NULL)
3466d29b2c44Sab196087 				batch_list = elfedit_malloc(
3467d29b2c44Sab196087 				    MSG_INTL(MSG_ALLOC_BATCHLST),
3468d29b2c44Sab196087 				    sizeof (*batch_list) * (argc - 1));
3469d29b2c44Sab196087 			batch_list[num_batch++] = optarg;
3470d29b2c44Sab196087 			break;
3471d29b2c44Sab196087 
3472d29b2c44Sab196087 		case 'L':
3473d29b2c44Sab196087 			modpath = optarg;
3474d29b2c44Sab196087 			break;
3475d29b2c44Sab196087 
3476d29b2c44Sab196087 		case 'o':
3477d29b2c44Sab196087 			if (elfedit_atooutstyle(optarg, &state.outstyle) == 0)
3478d29b2c44Sab196087 				usage(1);
3479d29b2c44Sab196087 			break;
3480d29b2c44Sab196087 
3481d29b2c44Sab196087 		case 'r':
3482d29b2c44Sab196087 			state.flags |= ELFEDIT_F_READONLY;
3483d29b2c44Sab196087 			break;
3484d29b2c44Sab196087 
3485d29b2c44Sab196087 		case '?':
3486d29b2c44Sab196087 			usage(1);
3487d29b2c44Sab196087 		}
3488d29b2c44Sab196087 	}
3489d29b2c44Sab196087 
3490d29b2c44Sab196087 	/*
3491d29b2c44Sab196087 	 * We allow 0, 1, or 2 files:
3492d29b2c44Sab196087 	 *
3493d29b2c44Sab196087 	 * The no-file case is an extremely limited mode, in which the
3494d29b2c44Sab196087 	 * only commands allowed to execute come from the sys: module.
3495d29b2c44Sab196087 	 * This mode exists primarily to allow easy access to the help
3496d29b2c44Sab196087 	 * facility.
3497d29b2c44Sab196087 	 *
3498d29b2c44Sab196087 	 * To get full access to elfedit's capablities, there must
3499d29b2c44Sab196087 	 * be an input file. If this is not a readonly
3500d29b2c44Sab196087 	 * session, then an optional second output file is allowed.
3501d29b2c44Sab196087 	 *
3502d29b2c44Sab196087 	 * In the case where two files are given and the session is
3503d29b2c44Sab196087 	 * readonly, use a full usage message, because the simple
3504d29b2c44Sab196087 	 * one isn't enough for the user to understand their error.
3505d29b2c44Sab196087 	 * Otherwise, the simple usage message suffices.
3506d29b2c44Sab196087 	 */
3507d29b2c44Sab196087 	argc = argc - optind;
3508d29b2c44Sab196087 	if ((argc == 2) && (state.flags & ELFEDIT_F_READONLY))
3509d29b2c44Sab196087 		usage(1);
3510d29b2c44Sab196087 	if (argc > 2)
3511d29b2c44Sab196087 		usage(0);
3512d29b2c44Sab196087 
3513d29b2c44Sab196087 	state.file.present = (argc != 0);
3514d29b2c44Sab196087 
3515d29b2c44Sab196087 	/*
3516d29b2c44Sab196087 	 * If we have a file to edit, and unless told otherwise by the
3517d29b2c44Sab196087 	 * caller, we try to run the 64-bit version of this program
3518d29b2c44Sab196087 	 * when the system is capable of it. If that fails, then we
3519d29b2c44Sab196087 	 * continue on with the currently running version.
3520d29b2c44Sab196087 	 *
3521d29b2c44Sab196087 	 * To force 32-bit execution on a 64-bit host, set the
3522d29b2c44Sab196087 	 * LD_NOEXEC_64 environment variable to a non-empty value.
3523d29b2c44Sab196087 	 *
3524d29b2c44Sab196087 	 * There is no reason to bother with this if in "no file" mode.
3525d29b2c44Sab196087 	 */
3526d29b2c44Sab196087 	if (state.file.present != 0)
3527d29b2c44Sab196087 		(void) conv_check_native(argv, envp);
3528d29b2c44Sab196087 
3529d29b2c44Sab196087 	elfedit_msg(ELFEDIT_MSG_DEBUG, MSG_INTL(MSG_DEBUG_VERSION),
3530d29b2c44Sab196087 	    (sizeof (char *) == 8) ? 64 : 32);
3531d29b2c44Sab196087 
3532d29b2c44Sab196087 	/*
3533d29b2c44Sab196087 	 * Put a module definition for the builtin system module on the
3534d29b2c44Sab196087 	 * module list. We know it starts out empty, so we do not have
3535d29b2c44Sab196087 	 * to go through a more general insertion process than this.
3536d29b2c44Sab196087 	 */
3537d29b2c44Sab196087 	state.modlist = elfedit_sys_init(ELFEDIT_VER_CURRENT);
3538d29b2c44Sab196087 
3539d29b2c44Sab196087 	/* Establish the search path for loadable modules */
3540d29b2c44Sab196087 	establish_modpath(modpath);
3541d29b2c44Sab196087 
3542d29b2c44Sab196087 	/*
3543d29b2c44Sab196087 	 * Now that we are running the final version of this program,
3544d29b2c44Sab196087 	 * deal with the input/output file(s).
3545d29b2c44Sab196087 	 */
3546d29b2c44Sab196087 	if (state.file.present == 0) {
3547d29b2c44Sab196087 		/*
3548d29b2c44Sab196087 		 * This is arbitrary --- we simply need to be able to
3549d29b2c44Sab196087 		 * load modules so that we can access their help strings
3550d29b2c44Sab196087 		 * and command completion functions. Without a file, we
3551d29b2c44Sab196087 		 * will refuse to call commands from any module other
3552d29b2c44Sab196087 		 * than sys. Those commands have been written to be aware
3553d29b2c44Sab196087 		 * of the case where there is no input file, and are
3554d29b2c44Sab196087 		 * therefore safe to run.
3555d29b2c44Sab196087 		 */
3556d29b2c44Sab196087 		state.elf.elfclass = ELFCLASS32;
3557d29b2c44Sab196087 		elfedit_msg(ELFEDIT_MSG_DEBUG, MSG_INTL(MSG_DEBUG_NOFILE));
3558d29b2c44Sab196087 
3559d29b2c44Sab196087 	} else {
3560d29b2c44Sab196087 		state.file.infile = argv[optind];
3561d29b2c44Sab196087 		if (argc == 1) {
3562d29b2c44Sab196087 			state.file.outfile = state.file.infile;
3563d29b2c44Sab196087 			if (state.flags & ELFEDIT_F_READONLY)
3564d29b2c44Sab196087 				elfedit_msg(ELFEDIT_MSG_DEBUG,
3565d29b2c44Sab196087 				    MSG_INTL(MSG_DEBUG_READONLY));
3566d29b2c44Sab196087 			else
3567d29b2c44Sab196087 				elfedit_msg(ELFEDIT_MSG_DEBUG,
3568d29b2c44Sab196087 				    MSG_INTL(MSG_DEBUG_INPLACEWARN),
3569d29b2c44Sab196087 				    state.file.infile);
3570d29b2c44Sab196087 		} else {
3571d29b2c44Sab196087 			state.file.outfile = argv[optind + 1];
3572d29b2c44Sab196087 			create_outfile(state.file.infile, state.file.outfile);
3573d29b2c44Sab196087 			elfedit_msg(ELFEDIT_MSG_DEBUG,
3574d29b2c44Sab196087 			    MSG_INTL(MSG_DEBUG_CPFILE),
3575d29b2c44Sab196087 			    state.file.infile, state.file.outfile);
3576d29b2c44Sab196087 			/*
3577d29b2c44Sab196087 			 * We are editing a copy of the original file that we
3578d29b2c44Sab196087 			 * just created. If we should exit before the edits are
3579d29b2c44Sab196087 			 * updated, then we want to unlink this copy so that we
3580d29b2c44Sab196087 			 * don't leave junk lying around. Once an update
3581d29b2c44Sab196087 			 * succeeds however, we'll leave it in place even
3582d29b2c44Sab196087 			 * if an error occurs afterwards.
3583d29b2c44Sab196087 			 */
3584d29b2c44Sab196087 			state.file.unlink_on_exit = 1;
3585d29b2c44Sab196087 			optind++;	/* Edit copy instead of the original */
3586d29b2c44Sab196087 		}
3587d29b2c44Sab196087 
3588d29b2c44Sab196087 		init_obj_state(state.file.outfile);
3589d29b2c44Sab196087 	}
3590d29b2c44Sab196087 
3591d29b2c44Sab196087 
3592d29b2c44Sab196087 	/*
3593d29b2c44Sab196087 	 * Process commands.
3594d29b2c44Sab196087 	 *
3595d29b2c44Sab196087 	 * If any -e options were used, then do them and
3596d29b2c44Sab196087 	 * immediately exit. On error, exit immediately without
3597d29b2c44Sab196087 	 * updating the target ELF file. On success, the 'write'
3598d29b2c44Sab196087 	 * and 'quit' commands are implicit in this mode.
3599d29b2c44Sab196087 	 *
3600d29b2c44Sab196087 	 * If no -e options are used, read commands from stdin.
3601d29b2c44Sab196087 	 * quit must be explicitly used. Exit is implicit on EOF.
3602d29b2c44Sab196087 	 * If stdin is a tty, then errors do not cause the editor
3603d29b2c44Sab196087 	 * to terminate. Rather, the error message is printed, and the
3604d29b2c44Sab196087 	 * user prompted to continue.
3605d29b2c44Sab196087 	 */
3606d29b2c44Sab196087 	if (batch_list != NULL) {	/* -e was used */
3607d29b2c44Sab196087 		/* Compile the commands */
3608d29b2c44Sab196087 		for (i = 0; i < num_batch; i++)
3609d29b2c44Sab196087 			parse_user_cmd(batch_list[i]);
3610d29b2c44Sab196087 		free(batch_list);
3611d29b2c44Sab196087 
3612d29b2c44Sab196087 		/*
3613d29b2c44Sab196087 		 * 'write' and 'quit' are implicit in this mode.
3614d29b2c44Sab196087 		 * Add them as well.
3615d29b2c44Sab196087 		 */
3616d29b2c44Sab196087 		if ((state.flags & ELFEDIT_F_READONLY) == 0)
3617d29b2c44Sab196087 			parse_user_cmd(MSG_ORIG(MSG_SYS_CMD_WRITE));
3618d29b2c44Sab196087 		parse_user_cmd(MSG_ORIG(MSG_SYS_CMD_QUIT));
3619d29b2c44Sab196087 
3620d29b2c44Sab196087 		/* And run them. This won't return, thanks to the 'quit' */
3621d29b2c44Sab196087 		dispatch_user_cmds();
3622d29b2c44Sab196087 	} else {
3623d29b2c44Sab196087 		state.input.is_tty = isatty(fileno(stdin));
3624d29b2c44Sab196087 		state.input.full_tty = state.input.is_tty &&
3625d29b2c44Sab196087 		    isatty(fileno(stdout));
3626d29b2c44Sab196087 
3627d29b2c44Sab196087 		if (state.input.full_tty) {
3628d29b2c44Sab196087 			struct sigaction act;
3629d29b2c44Sab196087 
3630d29b2c44Sab196087 			act.sa_sigaction = sigint_handler;
3631d29b2c44Sab196087 			(void) sigemptyset(&act.sa_mask);
3632d29b2c44Sab196087 			act.sa_flags = 0;
3633d29b2c44Sab196087 			if (sigaction(SIGINT, &act, NULL) == -1) {
3634d29b2c44Sab196087 				int err = errno;
3635d29b2c44Sab196087 				elfedit_msg(ELFEDIT_MSG_ERR,
3636d29b2c44Sab196087 				    MSG_INTL(MSG_ERR_SIGACTION), strerror(err));
3637d29b2c44Sab196087 			}
3638d29b2c44Sab196087 			/*
3639d29b2c44Sab196087 			 * If pager process exits before we are done
3640d29b2c44Sab196087 			 * writing, we can see SIGPIPE. Prevent it
3641d29b2c44Sab196087 			 * from killing the process.
3642d29b2c44Sab196087 			 */
3643d29b2c44Sab196087 			(void) sigignore(SIGPIPE);
3644d29b2c44Sab196087 
3645d29b2c44Sab196087 			/* Open tecla handle for command line editing */
3646d29b2c44Sab196087 			state.input.gl = new_GetLine(ELFEDIT_MAXCMD,
3647d29b2c44Sab196087 			    ELFEDIT_MAXHIST);
3648d29b2c44Sab196087 			/* Register our command completion function */
3649d29b2c44Sab196087 			(void) gl_customize_completion(state.input.gl,
3650d29b2c44Sab196087 			    NULL, cmd_match_fcn);
3651d29b2c44Sab196087 
3652d29b2c44Sab196087 			/*
3653d29b2c44Sab196087 			 * Make autoprint the default for interactive
3654d29b2c44Sab196087 			 * sessions.
3655d29b2c44Sab196087 			 */
3656d29b2c44Sab196087 			state.flags |= ELFEDIT_F_AUTOPRINT;
3657d29b2c44Sab196087 		}
3658d29b2c44Sab196087 		for (;;) {
3659d29b2c44Sab196087 			/*
3660d29b2c44Sab196087 			 * If this is an interactive session, then use
3661d29b2c44Sab196087 			 * sigsetjmp()/siglongjmp() to recover from bad
3662d29b2c44Sab196087 			 * commands and keep going. A non-0 return from
3663d29b2c44Sab196087 			 * sigsetjmp() means that an error just occurred.
3664d29b2c44Sab196087 			 * In that case, we simply restart this loop.
3665d29b2c44Sab196087 			 */
3666d29b2c44Sab196087 			if (state.input.is_tty) {
3667d29b2c44Sab196087 				if (sigsetjmp(state.msg_jbuf.env, 1) != 0) {
3668d29b2c44Sab196087 					if (state.input.full_tty)
3669d29b2c44Sab196087 						gl_abandon_line(state.input.gl);
3670d29b2c44Sab196087 					continue;
3671d29b2c44Sab196087 				}
3672d29b2c44Sab196087 				state.msg_jbuf.active = TRUE;
3673d29b2c44Sab196087 			}
3674d29b2c44Sab196087 
3675d29b2c44Sab196087 			/*
3676d29b2c44Sab196087 			 * Force all output out before each command.
3677d29b2c44Sab196087 			 * This is a no-OP when a tty is in use, but
3678d29b2c44Sab196087 			 * in a pipeline, it ensures that the block
3679d29b2c44Sab196087 			 * mode buffering doesn't delay output past
3680d29b2c44Sab196087 			 * the completion of each command.
3681d29b2c44Sab196087 			 *
3682d29b2c44Sab196087 			 * If we didn't do this, the output would eventually
3683d29b2c44Sab196087 			 * arrive at its destination, but the lag can be
3684d29b2c44Sab196087 			 * annoying when you pipe the output into a tool
3685d29b2c44Sab196087 			 * that displays the results in real time.
3686d29b2c44Sab196087 			 */
3687d29b2c44Sab196087 			(void) fflush(stdout);
3688d29b2c44Sab196087 			(void) fflush(stderr);
3689d29b2c44Sab196087 
3690d29b2c44Sab196087 			parse_user_cmd(read_cmd());
3691d29b2c44Sab196087 			dispatch_user_cmds();
3692d29b2c44Sab196087 			state.msg_jbuf.active = FALSE;
3693d29b2c44Sab196087 		}
3694d29b2c44Sab196087 	}
3695d29b2c44Sab196087 
3696d29b2c44Sab196087 
3697d29b2c44Sab196087 	/*NOTREACHED*/
3698d29b2c44Sab196087 	return (0);
3699d29b2c44Sab196087 }
3700