xref: /minix3/external/bsd/less/dist/cmdbuf.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*	$NetBSD: cmdbuf.c,v 1.4 2013/09/04 19:44:21 tron Exp $	*/
2f7cf2976SLionel Sambuc 
3f7cf2976SLionel Sambuc /*
4*84d9c625SLionel Sambuc  * Copyright (C) 1984-2012  Mark Nudelman
5f7cf2976SLionel Sambuc  *
6f7cf2976SLionel Sambuc  * You may distribute under the terms of either the GNU General Public
7f7cf2976SLionel Sambuc  * License or the Less License, as specified in the README file.
8f7cf2976SLionel Sambuc  *
9*84d9c625SLionel Sambuc  * For more information, see the README file.
10f7cf2976SLionel Sambuc  */
11f7cf2976SLionel Sambuc 
12f7cf2976SLionel Sambuc 
13f7cf2976SLionel Sambuc /*
14f7cf2976SLionel Sambuc  * Functions which manipulate the command buffer.
15f7cf2976SLionel Sambuc  * Used only by command() and related functions.
16f7cf2976SLionel Sambuc  */
17f7cf2976SLionel Sambuc 
18f7cf2976SLionel Sambuc #include "less.h"
19f7cf2976SLionel Sambuc #include "cmd.h"
20f7cf2976SLionel Sambuc #include "charset.h"
21f7cf2976SLionel Sambuc #if HAVE_STAT
22f7cf2976SLionel Sambuc #include <sys/stat.h>
23f7cf2976SLionel Sambuc #endif
24f7cf2976SLionel Sambuc #if HAVE_ERRNO_H
25f7cf2976SLionel Sambuc #include <errno.h>
26f7cf2976SLionel Sambuc #endif
27f7cf2976SLionel Sambuc 
28f7cf2976SLionel Sambuc extern int sc_width;
29f7cf2976SLionel Sambuc extern int utf_mode;
30f7cf2976SLionel Sambuc 
31f7cf2976SLionel Sambuc static char cmdbuf[CMDBUF_SIZE]; /* Buffer for holding a multi-char command */
32f7cf2976SLionel Sambuc static int cmd_col;		/* Current column of the cursor */
33f7cf2976SLionel Sambuc static int prompt_col;		/* Column of cursor just after prompt */
34f7cf2976SLionel Sambuc static char *cp;		/* Pointer into cmdbuf */
35f7cf2976SLionel Sambuc static int cmd_offset;		/* Index into cmdbuf of first displayed char */
36f7cf2976SLionel Sambuc static int literal;		/* Next input char should not be interpreted */
37*84d9c625SLionel Sambuc static int updown_match = -1;	/* Prefix length in up/down movement */
38f7cf2976SLionel Sambuc 
39f7cf2976SLionel Sambuc #if TAB_COMPLETE_FILENAME
40f7cf2976SLionel Sambuc static int cmd_complete __P((int));
41f7cf2976SLionel Sambuc /*
42f7cf2976SLionel Sambuc  * These variables are statics used by cmd_complete.
43f7cf2976SLionel Sambuc  */
44f7cf2976SLionel Sambuc static int in_completion = 0;
45f7cf2976SLionel Sambuc static char *tk_text;
46f7cf2976SLionel Sambuc static char *tk_original;
47f7cf2976SLionel Sambuc static char *tk_ipoint;
48f7cf2976SLionel Sambuc static char *tk_trial;
49f7cf2976SLionel Sambuc static struct textlist tk_tlist;
50f7cf2976SLionel Sambuc #endif
51f7cf2976SLionel Sambuc 
52f7cf2976SLionel Sambuc static void cmd_repaint __P((char *));
53f7cf2976SLionel Sambuc static void cmd_home __P((void));
54f7cf2976SLionel Sambuc static void cmd_lshift __P((void));
55f7cf2976SLionel Sambuc static void cmd_rshift __P((void));
56f7cf2976SLionel Sambuc static int cmd_right __P((void));
57f7cf2976SLionel Sambuc static int cmd_left __P((void));
58f7cf2976SLionel Sambuc static int cmd_ichar __P((char *, int));
59f7cf2976SLionel Sambuc static int cmd_erase __P((void));
60f7cf2976SLionel Sambuc static int cmd_delete __P((void));
61f7cf2976SLionel Sambuc static int cmd_werase __P((void));
62f7cf2976SLionel Sambuc static int cmd_wdelete __P((void));
63f7cf2976SLionel Sambuc static int cmd_kill __P((void));
64f7cf2976SLionel Sambuc static int cmd_updown __P((int));
65f7cf2976SLionel Sambuc static int cmd_edit __P((int));
66f7cf2976SLionel Sambuc static int cmd_istr __P((char *));
67f7cf2976SLionel Sambuc static char *delimit_word __P((void));
68f7cf2976SLionel Sambuc static void init_compl __P((void));
69f7cf2976SLionel Sambuc static char *next_compl __P((int, char *));
70f7cf2976SLionel Sambuc 
71f7cf2976SLionel Sambuc #if SPACES_IN_FILENAMES
72f7cf2976SLionel Sambuc public char openquote = '"';
73f7cf2976SLionel Sambuc public char closequote = '"';
74f7cf2976SLionel Sambuc #endif
75f7cf2976SLionel Sambuc 
76f7cf2976SLionel Sambuc #if CMD_HISTORY
77f7cf2976SLionel Sambuc 
78f7cf2976SLionel Sambuc /* History file */
79f7cf2976SLionel Sambuc #define HISTFILE_FIRST_LINE      ".less-history-file:"
80f7cf2976SLionel Sambuc #define HISTFILE_SEARCH_SECTION  ".search"
81f7cf2976SLionel Sambuc #define HISTFILE_SHELL_SECTION   ".shell"
82f7cf2976SLionel Sambuc 
83f7cf2976SLionel Sambuc /*
84f7cf2976SLionel Sambuc  * A mlist structure represents a command history.
85f7cf2976SLionel Sambuc  */
86f7cf2976SLionel Sambuc struct mlist
87f7cf2976SLionel Sambuc {
88f7cf2976SLionel Sambuc 	struct mlist *next;
89f7cf2976SLionel Sambuc 	struct mlist *prev;
90f7cf2976SLionel Sambuc 	struct mlist *curr_mp;
91f7cf2976SLionel Sambuc 	char *string;
92f7cf2976SLionel Sambuc 	int modified;
93f7cf2976SLionel Sambuc };
94f7cf2976SLionel Sambuc 
95f7cf2976SLionel Sambuc /*
96f7cf2976SLionel Sambuc  * These are the various command histories that exist.
97f7cf2976SLionel Sambuc  */
98f7cf2976SLionel Sambuc struct mlist mlist_search =
99f7cf2976SLionel Sambuc 	{ &mlist_search,  &mlist_search,  &mlist_search,  NULL, 0 };
100f7cf2976SLionel Sambuc public void * constant ml_search = (void *) &mlist_search;
101f7cf2976SLionel Sambuc 
102f7cf2976SLionel Sambuc struct mlist mlist_examine =
103f7cf2976SLionel Sambuc 	{ &mlist_examine, &mlist_examine, &mlist_examine, NULL, 0 };
104f7cf2976SLionel Sambuc public void * constant ml_examine = (void *) &mlist_examine;
105f7cf2976SLionel Sambuc 
106f7cf2976SLionel Sambuc #if SHELL_ESCAPE || PIPEC
107f7cf2976SLionel Sambuc struct mlist mlist_shell =
108f7cf2976SLionel Sambuc 	{ &mlist_shell,   &mlist_shell,   &mlist_shell,   NULL, 0 };
109f7cf2976SLionel Sambuc public void * constant ml_shell = (void *) &mlist_shell;
110f7cf2976SLionel Sambuc #endif
111f7cf2976SLionel Sambuc 
112f7cf2976SLionel Sambuc #else /* CMD_HISTORY */
113f7cf2976SLionel Sambuc 
114f7cf2976SLionel Sambuc /* If CMD_HISTORY is off, these are just flags. */
115f7cf2976SLionel Sambuc public void * constant ml_search = (void *)1;
116f7cf2976SLionel Sambuc public void * constant ml_examine = (void *)2;
117f7cf2976SLionel Sambuc #if SHELL_ESCAPE || PIPEC
118f7cf2976SLionel Sambuc public void * constant ml_shell = (void *)3;
119f7cf2976SLionel Sambuc #endif
120f7cf2976SLionel Sambuc 
121f7cf2976SLionel Sambuc #endif /* CMD_HISTORY */
122f7cf2976SLionel Sambuc 
123f7cf2976SLionel Sambuc /*
124f7cf2976SLionel Sambuc  * History for the current command.
125f7cf2976SLionel Sambuc  */
126f7cf2976SLionel Sambuc static struct mlist *curr_mlist = NULL;
127f7cf2976SLionel Sambuc static int curr_cmdflags;
128f7cf2976SLionel Sambuc 
129f7cf2976SLionel Sambuc static char cmd_mbc_buf[MAX_UTF_CHAR_LEN];
130f7cf2976SLionel Sambuc static int cmd_mbc_buf_len;
131f7cf2976SLionel Sambuc static int cmd_mbc_buf_index;
132f7cf2976SLionel Sambuc 
133f7cf2976SLionel Sambuc 
134f7cf2976SLionel Sambuc /*
135f7cf2976SLionel Sambuc  * Reset command buffer (to empty).
136f7cf2976SLionel Sambuc  */
137f7cf2976SLionel Sambuc 	public void
cmd_reset()138f7cf2976SLionel Sambuc cmd_reset()
139f7cf2976SLionel Sambuc {
140f7cf2976SLionel Sambuc 	cp = cmdbuf;
141f7cf2976SLionel Sambuc 	*cp = '\0';
142f7cf2976SLionel Sambuc 	cmd_col = 0;
143f7cf2976SLionel Sambuc 	cmd_offset = 0;
144f7cf2976SLionel Sambuc 	literal = 0;
145f7cf2976SLionel Sambuc 	cmd_mbc_buf_len = 0;
146*84d9c625SLionel Sambuc 	updown_match = -1;
147f7cf2976SLionel Sambuc }
148f7cf2976SLionel Sambuc 
149f7cf2976SLionel Sambuc /*
150f7cf2976SLionel Sambuc  * Clear command line.
151f7cf2976SLionel Sambuc  */
152f7cf2976SLionel Sambuc 	public void
clear_cmd()153f7cf2976SLionel Sambuc clear_cmd()
154f7cf2976SLionel Sambuc {
155f7cf2976SLionel Sambuc 	cmd_col = prompt_col = 0;
156f7cf2976SLionel Sambuc 	cmd_mbc_buf_len = 0;
157*84d9c625SLionel Sambuc 	updown_match = -1;
158f7cf2976SLionel Sambuc }
159f7cf2976SLionel Sambuc 
160f7cf2976SLionel Sambuc /*
161f7cf2976SLionel Sambuc  * Display a string, usually as a prompt for input into the command buffer.
162f7cf2976SLionel Sambuc  */
163f7cf2976SLionel Sambuc 	public void
cmd_putstr(s)164f7cf2976SLionel Sambuc cmd_putstr(s)
165f7cf2976SLionel Sambuc 	char *s;
166f7cf2976SLionel Sambuc {
167f7cf2976SLionel Sambuc 	LWCHAR prev_ch = 0;
168f7cf2976SLionel Sambuc 	LWCHAR ch;
169f7cf2976SLionel Sambuc 	char *endline = s + strlen(s);
170f7cf2976SLionel Sambuc 	while (*s != '\0')
171f7cf2976SLionel Sambuc 	{
172f7cf2976SLionel Sambuc 		char *ns = s;
173f7cf2976SLionel Sambuc 		ch = step_char(&ns, +1, endline);
174f7cf2976SLionel Sambuc 		while (s < ns)
175f7cf2976SLionel Sambuc 			putchr(*s++);
176f7cf2976SLionel Sambuc 		if (!utf_mode)
177f7cf2976SLionel Sambuc 		{
178f7cf2976SLionel Sambuc 			cmd_col++;
179f7cf2976SLionel Sambuc 			prompt_col++;
180f7cf2976SLionel Sambuc 		} else if (!is_composing_char(ch) &&
181f7cf2976SLionel Sambuc 		           !is_combining_char(prev_ch, ch))
182f7cf2976SLionel Sambuc 		{
183f7cf2976SLionel Sambuc 			int width = is_wide_char(ch) ? 2 : 1;
184f7cf2976SLionel Sambuc 			cmd_col += width;
185f7cf2976SLionel Sambuc 			prompt_col += width;
186f7cf2976SLionel Sambuc 		}
187f7cf2976SLionel Sambuc 		prev_ch = ch;
188f7cf2976SLionel Sambuc 	}
189f7cf2976SLionel Sambuc }
190f7cf2976SLionel Sambuc 
191f7cf2976SLionel Sambuc /*
192f7cf2976SLionel Sambuc  * How many characters are in the command buffer?
193f7cf2976SLionel Sambuc  */
194f7cf2976SLionel Sambuc 	public int
len_cmdbuf()195f7cf2976SLionel Sambuc len_cmdbuf()
196f7cf2976SLionel Sambuc {
197f7cf2976SLionel Sambuc 	char *s = cmdbuf;
198f7cf2976SLionel Sambuc 	char *endline = s + strlen(s);
199f7cf2976SLionel Sambuc 	int len = 0;
200f7cf2976SLionel Sambuc 
201f7cf2976SLionel Sambuc 	while (*s != '\0')
202f7cf2976SLionel Sambuc 	{
203f7cf2976SLionel Sambuc 		step_char(&s, +1, endline);
204f7cf2976SLionel Sambuc 		len++;
205f7cf2976SLionel Sambuc 	}
206f7cf2976SLionel Sambuc 	return (len);
207f7cf2976SLionel Sambuc }
208f7cf2976SLionel Sambuc 
209f7cf2976SLionel Sambuc /*
210f7cf2976SLionel Sambuc  * Common part of cmd_step_right() and cmd_step_left().
211f7cf2976SLionel Sambuc  */
212f7cf2976SLionel Sambuc 	static char *
cmd_step_common(p,ch,len,pwidth,bswidth)213f7cf2976SLionel Sambuc cmd_step_common(p, ch, len, pwidth, bswidth)
214f7cf2976SLionel Sambuc 	char *p;
215f7cf2976SLionel Sambuc 	LWCHAR ch;
216f7cf2976SLionel Sambuc 	int len;
217f7cf2976SLionel Sambuc 	int *pwidth;
218f7cf2976SLionel Sambuc 	int *bswidth;
219f7cf2976SLionel Sambuc {
220f7cf2976SLionel Sambuc 	char *pr;
221f7cf2976SLionel Sambuc 
222f7cf2976SLionel Sambuc 	if (len == 1)
223f7cf2976SLionel Sambuc 	{
224f7cf2976SLionel Sambuc 		pr = prchar((int) ch);
225f7cf2976SLionel Sambuc 		if (pwidth != NULL || bswidth != NULL)
226f7cf2976SLionel Sambuc 		{
227f7cf2976SLionel Sambuc 			int len = strlen(pr);
228f7cf2976SLionel Sambuc 			if (pwidth != NULL)
229f7cf2976SLionel Sambuc 				*pwidth = len;
230f7cf2976SLionel Sambuc 			if (bswidth != NULL)
231f7cf2976SLionel Sambuc 				*bswidth = len;
232f7cf2976SLionel Sambuc 		}
233f7cf2976SLionel Sambuc 	} else
234f7cf2976SLionel Sambuc 	{
235f7cf2976SLionel Sambuc 		pr = prutfchar(ch);
236f7cf2976SLionel Sambuc 		if (pwidth != NULL || bswidth != NULL)
237f7cf2976SLionel Sambuc 		{
238f7cf2976SLionel Sambuc 			if (is_composing_char(ch))
239f7cf2976SLionel Sambuc 			{
240f7cf2976SLionel Sambuc 				if (pwidth != NULL)
241f7cf2976SLionel Sambuc 					*pwidth = 0;
242f7cf2976SLionel Sambuc 				if (bswidth != NULL)
243f7cf2976SLionel Sambuc 					*bswidth = 0;
244f7cf2976SLionel Sambuc 			} else if (is_ubin_char(ch))
245f7cf2976SLionel Sambuc 			{
246f7cf2976SLionel Sambuc 				int len = strlen(pr);
247f7cf2976SLionel Sambuc 				if (pwidth != NULL)
248f7cf2976SLionel Sambuc 					*pwidth = len;
249f7cf2976SLionel Sambuc 				if (bswidth != NULL)
250f7cf2976SLionel Sambuc 					*bswidth = len;
251f7cf2976SLionel Sambuc 			} else
252f7cf2976SLionel Sambuc 			{
253f7cf2976SLionel Sambuc 				LWCHAR prev_ch = step_char(&p, -1, cmdbuf);
254f7cf2976SLionel Sambuc 				if (is_combining_char(prev_ch, ch))
255f7cf2976SLionel Sambuc 				{
256f7cf2976SLionel Sambuc 					if (pwidth != NULL)
257f7cf2976SLionel Sambuc 						*pwidth = 0;
258f7cf2976SLionel Sambuc 					if (bswidth != NULL)
259f7cf2976SLionel Sambuc 						*bswidth = 0;
260f7cf2976SLionel Sambuc 				} else
261f7cf2976SLionel Sambuc 				{
262f7cf2976SLionel Sambuc 					if (pwidth != NULL)
263f7cf2976SLionel Sambuc 						*pwidth	= is_wide_char(ch)
264f7cf2976SLionel Sambuc 							?	2
265f7cf2976SLionel Sambuc 							:	1;
266f7cf2976SLionel Sambuc 					if (bswidth != NULL)
267f7cf2976SLionel Sambuc 						*bswidth = 1;
268f7cf2976SLionel Sambuc 				}
269f7cf2976SLionel Sambuc 			}
270f7cf2976SLionel Sambuc 		}
271f7cf2976SLionel Sambuc 	}
272f7cf2976SLionel Sambuc 
273f7cf2976SLionel Sambuc 	return (pr);
274f7cf2976SLionel Sambuc }
275f7cf2976SLionel Sambuc 
276f7cf2976SLionel Sambuc /*
277f7cf2976SLionel Sambuc  * Step a pointer one character right in the command buffer.
278f7cf2976SLionel Sambuc  */
279f7cf2976SLionel Sambuc 	static char *
cmd_step_right(pp,pwidth,bswidth)280f7cf2976SLionel Sambuc cmd_step_right(pp, pwidth, bswidth)
281f7cf2976SLionel Sambuc 	char **pp;
282f7cf2976SLionel Sambuc 	int *pwidth;
283f7cf2976SLionel Sambuc 	int *bswidth;
284f7cf2976SLionel Sambuc {
285f7cf2976SLionel Sambuc 	char *p = *pp;
286f7cf2976SLionel Sambuc 	LWCHAR ch = step_char(pp, +1, p + strlen(p));
287f7cf2976SLionel Sambuc 
288f7cf2976SLionel Sambuc 	return cmd_step_common(p, ch, *pp - p, pwidth, bswidth);
289f7cf2976SLionel Sambuc }
290f7cf2976SLionel Sambuc 
291f7cf2976SLionel Sambuc /*
292f7cf2976SLionel Sambuc  * Step a pointer one character left in the command buffer.
293f7cf2976SLionel Sambuc  */
294f7cf2976SLionel Sambuc 	static char *
cmd_step_left(pp,pwidth,bswidth)295f7cf2976SLionel Sambuc cmd_step_left(pp, pwidth, bswidth)
296f7cf2976SLionel Sambuc 	char **pp;
297f7cf2976SLionel Sambuc 	int *pwidth;
298f7cf2976SLionel Sambuc 	int *bswidth;
299f7cf2976SLionel Sambuc {
300f7cf2976SLionel Sambuc 	char *p = *pp;
301f7cf2976SLionel Sambuc 	LWCHAR ch = step_char(pp, -1, cmdbuf);
302f7cf2976SLionel Sambuc 
303f7cf2976SLionel Sambuc 	return cmd_step_common(*pp, ch, p - *pp, pwidth, bswidth);
304f7cf2976SLionel Sambuc }
305f7cf2976SLionel Sambuc 
306f7cf2976SLionel Sambuc /*
307f7cf2976SLionel Sambuc  * Repaint the line from cp onwards.
308f7cf2976SLionel Sambuc  * Then position the cursor just after the char old_cp (a pointer into cmdbuf).
309f7cf2976SLionel Sambuc  */
310f7cf2976SLionel Sambuc 	static void
cmd_repaint(old_cp)311f7cf2976SLionel Sambuc cmd_repaint(old_cp)
312f7cf2976SLionel Sambuc 	char *old_cp;
313f7cf2976SLionel Sambuc {
314f7cf2976SLionel Sambuc 	/*
315f7cf2976SLionel Sambuc 	 * Repaint the line from the current position.
316f7cf2976SLionel Sambuc 	 */
317f7cf2976SLionel Sambuc 	clear_eol();
318f7cf2976SLionel Sambuc 	while (*cp != '\0')
319f7cf2976SLionel Sambuc 	{
320f7cf2976SLionel Sambuc 		char *np = cp;
321f7cf2976SLionel Sambuc 		int width;
322f7cf2976SLionel Sambuc 		char *pr = cmd_step_right(&np, &width, NULL);
323f7cf2976SLionel Sambuc 		if (cmd_col + width >= sc_width)
324f7cf2976SLionel Sambuc 			break;
325f7cf2976SLionel Sambuc 		cp = np;
326f7cf2976SLionel Sambuc 		putstr(pr);
327f7cf2976SLionel Sambuc 		cmd_col += width;
328f7cf2976SLionel Sambuc 	}
329f7cf2976SLionel Sambuc 	while (*cp != '\0')
330f7cf2976SLionel Sambuc 	{
331f7cf2976SLionel Sambuc 		char *np = cp;
332f7cf2976SLionel Sambuc 		int width;
333f7cf2976SLionel Sambuc 		char *pr = cmd_step_right(&np, &width, NULL);
334f7cf2976SLionel Sambuc 		if (width > 0)
335f7cf2976SLionel Sambuc 			break;
336f7cf2976SLionel Sambuc 		cp = np;
337f7cf2976SLionel Sambuc 		putstr(pr);
338f7cf2976SLionel Sambuc 	}
339f7cf2976SLionel Sambuc 
340f7cf2976SLionel Sambuc 	/*
341f7cf2976SLionel Sambuc 	 * Back up the cursor to the correct position.
342f7cf2976SLionel Sambuc 	 */
343f7cf2976SLionel Sambuc 	while (cp > old_cp)
344f7cf2976SLionel Sambuc 		cmd_left();
345f7cf2976SLionel Sambuc }
346f7cf2976SLionel Sambuc 
347f7cf2976SLionel Sambuc /*
348f7cf2976SLionel Sambuc  * Put the cursor at "home" (just after the prompt),
349f7cf2976SLionel Sambuc  * and set cp to the corresponding char in cmdbuf.
350f7cf2976SLionel Sambuc  */
351f7cf2976SLionel Sambuc 	static void
cmd_home()352f7cf2976SLionel Sambuc cmd_home()
353f7cf2976SLionel Sambuc {
354f7cf2976SLionel Sambuc 	while (cmd_col > prompt_col)
355f7cf2976SLionel Sambuc 	{
356f7cf2976SLionel Sambuc 		int width, bswidth;
357f7cf2976SLionel Sambuc 
358f7cf2976SLionel Sambuc 		cmd_step_left(&cp, &width, &bswidth);
359f7cf2976SLionel Sambuc 		while (bswidth-- > 0)
360f7cf2976SLionel Sambuc 			putbs();
361f7cf2976SLionel Sambuc 		cmd_col -= width;
362f7cf2976SLionel Sambuc 	}
363f7cf2976SLionel Sambuc 
364f7cf2976SLionel Sambuc 	cp = &cmdbuf[cmd_offset];
365f7cf2976SLionel Sambuc }
366f7cf2976SLionel Sambuc 
367f7cf2976SLionel Sambuc /*
368f7cf2976SLionel Sambuc  * Shift the cmdbuf display left a half-screen.
369f7cf2976SLionel Sambuc  */
370f7cf2976SLionel Sambuc 	static void
cmd_lshift()371f7cf2976SLionel Sambuc cmd_lshift()
372f7cf2976SLionel Sambuc {
373f7cf2976SLionel Sambuc 	char *s;
374f7cf2976SLionel Sambuc 	char *save_cp;
375f7cf2976SLionel Sambuc 	int cols;
376f7cf2976SLionel Sambuc 
377f7cf2976SLionel Sambuc 	/*
378f7cf2976SLionel Sambuc 	 * Start at the first displayed char, count how far to the
379f7cf2976SLionel Sambuc 	 * right we'd have to move to reach the center of the screen.
380f7cf2976SLionel Sambuc 	 */
381f7cf2976SLionel Sambuc 	s = cmdbuf + cmd_offset;
382f7cf2976SLionel Sambuc 	cols = 0;
383f7cf2976SLionel Sambuc 	while (cols < (sc_width - prompt_col) / 2 && *s != '\0')
384f7cf2976SLionel Sambuc 	{
385f7cf2976SLionel Sambuc 		int width;
386f7cf2976SLionel Sambuc 		cmd_step_right(&s, &width, NULL);
387f7cf2976SLionel Sambuc 		cols += width;
388f7cf2976SLionel Sambuc 	}
389f7cf2976SLionel Sambuc 	while (*s != '\0')
390f7cf2976SLionel Sambuc 	{
391f7cf2976SLionel Sambuc 		int width;
392f7cf2976SLionel Sambuc 		char *ns = s;
393f7cf2976SLionel Sambuc 		cmd_step_right(&ns, &width, NULL);
394f7cf2976SLionel Sambuc 		if (width > 0)
395f7cf2976SLionel Sambuc 			break;
396f7cf2976SLionel Sambuc 		s = ns;
397f7cf2976SLionel Sambuc 	}
398f7cf2976SLionel Sambuc 
399f7cf2976SLionel Sambuc 	cmd_offset = s - cmdbuf;
400f7cf2976SLionel Sambuc 	save_cp = cp;
401f7cf2976SLionel Sambuc 	cmd_home();
402f7cf2976SLionel Sambuc 	cmd_repaint(save_cp);
403f7cf2976SLionel Sambuc }
404f7cf2976SLionel Sambuc 
405f7cf2976SLionel Sambuc /*
406f7cf2976SLionel Sambuc  * Shift the cmdbuf display right a half-screen.
407f7cf2976SLionel Sambuc  */
408f7cf2976SLionel Sambuc 	static void
cmd_rshift()409f7cf2976SLionel Sambuc cmd_rshift()
410f7cf2976SLionel Sambuc {
411f7cf2976SLionel Sambuc 	char *s;
412f7cf2976SLionel Sambuc 	char *save_cp;
413f7cf2976SLionel Sambuc 	int cols;
414f7cf2976SLionel Sambuc 
415f7cf2976SLionel Sambuc 	/*
416f7cf2976SLionel Sambuc 	 * Start at the first displayed char, count how far to the
417f7cf2976SLionel Sambuc 	 * left we'd have to move to traverse a half-screen width
418f7cf2976SLionel Sambuc 	 * of displayed characters.
419f7cf2976SLionel Sambuc 	 */
420f7cf2976SLionel Sambuc 	s = cmdbuf + cmd_offset;
421f7cf2976SLionel Sambuc 	cols = 0;
422f7cf2976SLionel Sambuc 	while (cols < (sc_width - prompt_col) / 2 && s > cmdbuf)
423f7cf2976SLionel Sambuc 	{
424f7cf2976SLionel Sambuc 		int width;
425f7cf2976SLionel Sambuc 		cmd_step_left(&s, &width, NULL);
426f7cf2976SLionel Sambuc 		cols += width;
427f7cf2976SLionel Sambuc 	}
428f7cf2976SLionel Sambuc 
429f7cf2976SLionel Sambuc 	cmd_offset = s - cmdbuf;
430f7cf2976SLionel Sambuc 	save_cp = cp;
431f7cf2976SLionel Sambuc 	cmd_home();
432f7cf2976SLionel Sambuc 	cmd_repaint(save_cp);
433f7cf2976SLionel Sambuc }
434f7cf2976SLionel Sambuc 
435f7cf2976SLionel Sambuc /*
436f7cf2976SLionel Sambuc  * Move cursor right one character.
437f7cf2976SLionel Sambuc  */
438f7cf2976SLionel Sambuc 	static int
cmd_right()439f7cf2976SLionel Sambuc cmd_right()
440f7cf2976SLionel Sambuc {
441f7cf2976SLionel Sambuc 	char *pr;
442f7cf2976SLionel Sambuc 	char *ncp;
443f7cf2976SLionel Sambuc 	int width;
444f7cf2976SLionel Sambuc 
445f7cf2976SLionel Sambuc 	if (*cp == '\0')
446f7cf2976SLionel Sambuc 	{
447f7cf2976SLionel Sambuc 		/* Already at the end of the line. */
448f7cf2976SLionel Sambuc 		return (CC_OK);
449f7cf2976SLionel Sambuc 	}
450f7cf2976SLionel Sambuc 	ncp = cp;
451f7cf2976SLionel Sambuc 	pr = cmd_step_right(&ncp, &width, NULL);
452f7cf2976SLionel Sambuc 	if (cmd_col + width >= sc_width)
453f7cf2976SLionel Sambuc 		cmd_lshift();
454f7cf2976SLionel Sambuc 	else if (cmd_col + width == sc_width - 1 && cp[1] != '\0')
455f7cf2976SLionel Sambuc 		cmd_lshift();
456f7cf2976SLionel Sambuc 	cp = ncp;
457f7cf2976SLionel Sambuc 	cmd_col += width;
458f7cf2976SLionel Sambuc 	putstr(pr);
459f7cf2976SLionel Sambuc 	while (*cp != '\0')
460f7cf2976SLionel Sambuc 	{
461f7cf2976SLionel Sambuc 		pr = cmd_step_right(&ncp, &width, NULL);
462f7cf2976SLionel Sambuc 		if (width > 0)
463f7cf2976SLionel Sambuc 			break;
464f7cf2976SLionel Sambuc 		putstr(pr);
465f7cf2976SLionel Sambuc 		cp = ncp;
466f7cf2976SLionel Sambuc 	}
467f7cf2976SLionel Sambuc 	return (CC_OK);
468f7cf2976SLionel Sambuc }
469f7cf2976SLionel Sambuc 
470f7cf2976SLionel Sambuc /*
471f7cf2976SLionel Sambuc  * Move cursor left one character.
472f7cf2976SLionel Sambuc  */
473f7cf2976SLionel Sambuc 	static int
cmd_left()474f7cf2976SLionel Sambuc cmd_left()
475f7cf2976SLionel Sambuc {
476f7cf2976SLionel Sambuc 	char *ncp;
477f7cf2976SLionel Sambuc 	int width, bswidth;
478f7cf2976SLionel Sambuc 
479f7cf2976SLionel Sambuc 	if (cp <= cmdbuf)
480f7cf2976SLionel Sambuc 	{
481f7cf2976SLionel Sambuc 		/* Already at the beginning of the line */
482f7cf2976SLionel Sambuc 		return (CC_OK);
483f7cf2976SLionel Sambuc 	}
484f7cf2976SLionel Sambuc 	ncp = cp;
485f7cf2976SLionel Sambuc 	while (ncp > cmdbuf)
486f7cf2976SLionel Sambuc 	{
487f7cf2976SLionel Sambuc 		cmd_step_left(&ncp, &width, &bswidth);
488f7cf2976SLionel Sambuc 		if (width > 0)
489f7cf2976SLionel Sambuc 			break;
490f7cf2976SLionel Sambuc 	}
491f7cf2976SLionel Sambuc 	if (cmd_col < prompt_col + width)
492f7cf2976SLionel Sambuc 		cmd_rshift();
493f7cf2976SLionel Sambuc 	cp = ncp;
494f7cf2976SLionel Sambuc 	cmd_col -= width;
495f7cf2976SLionel Sambuc 	while (bswidth-- > 0)
496f7cf2976SLionel Sambuc 		putbs();
497f7cf2976SLionel Sambuc 	return (CC_OK);
498f7cf2976SLionel Sambuc }
499f7cf2976SLionel Sambuc 
500f7cf2976SLionel Sambuc /*
501f7cf2976SLionel Sambuc  * Insert a char into the command buffer, at the current position.
502f7cf2976SLionel Sambuc  */
503f7cf2976SLionel Sambuc 	static int
cmd_ichar(cs,clen)504f7cf2976SLionel Sambuc cmd_ichar(cs, clen)
505f7cf2976SLionel Sambuc 	char *cs;
506f7cf2976SLionel Sambuc 	int clen;
507f7cf2976SLionel Sambuc {
508f7cf2976SLionel Sambuc 	char *s;
509f7cf2976SLionel Sambuc 
510f7cf2976SLionel Sambuc 	if (strlen(cmdbuf) + clen >= sizeof(cmdbuf)-1)
511f7cf2976SLionel Sambuc 	{
512f7cf2976SLionel Sambuc 		/* No room in the command buffer for another char. */
513f7cf2976SLionel Sambuc 		bell();
514f7cf2976SLionel Sambuc 		return (CC_ERROR);
515f7cf2976SLionel Sambuc 	}
516f7cf2976SLionel Sambuc 
517f7cf2976SLionel Sambuc 	/*
518f7cf2976SLionel Sambuc 	 * Make room for the new character (shift the tail of the buffer right).
519f7cf2976SLionel Sambuc 	 */
520f7cf2976SLionel Sambuc 	for (s = &cmdbuf[strlen(cmdbuf)];  s >= cp;  s--)
521f7cf2976SLionel Sambuc 		s[clen] = s[0];
522f7cf2976SLionel Sambuc 	/*
523f7cf2976SLionel Sambuc 	 * Insert the character into the buffer.
524f7cf2976SLionel Sambuc 	 */
525f7cf2976SLionel Sambuc 	for (s = cp;  s < cp + clen;  s++)
526f7cf2976SLionel Sambuc 		*s = *cs++;
527f7cf2976SLionel Sambuc 	/*
528f7cf2976SLionel Sambuc 	 * Reprint the tail of the line from the inserted char.
529f7cf2976SLionel Sambuc 	 */
530*84d9c625SLionel Sambuc 	updown_match = -1;
531f7cf2976SLionel Sambuc 	cmd_repaint(cp);
532f7cf2976SLionel Sambuc 	cmd_right();
533f7cf2976SLionel Sambuc 	return (CC_OK);
534f7cf2976SLionel Sambuc }
535f7cf2976SLionel Sambuc 
536f7cf2976SLionel Sambuc /*
537f7cf2976SLionel Sambuc  * Backspace in the command buffer.
538f7cf2976SLionel Sambuc  * Delete the char to the left of the cursor.
539f7cf2976SLionel Sambuc  */
540f7cf2976SLionel Sambuc 	static int
cmd_erase()541f7cf2976SLionel Sambuc cmd_erase()
542f7cf2976SLionel Sambuc {
543f7cf2976SLionel Sambuc 	register char *s;
544f7cf2976SLionel Sambuc 	int clen;
545f7cf2976SLionel Sambuc 
546f7cf2976SLionel Sambuc 	if (cp == cmdbuf)
547f7cf2976SLionel Sambuc 	{
548f7cf2976SLionel Sambuc 		/*
549f7cf2976SLionel Sambuc 		 * Backspace past beginning of the buffer:
550f7cf2976SLionel Sambuc 		 * this usually means abort the command.
551f7cf2976SLionel Sambuc 		 */
552f7cf2976SLionel Sambuc 		return (CC_QUIT);
553f7cf2976SLionel Sambuc 	}
554f7cf2976SLionel Sambuc 	/*
555f7cf2976SLionel Sambuc 	 * Move cursor left (to the char being erased).
556f7cf2976SLionel Sambuc 	 */
557f7cf2976SLionel Sambuc 	s = cp;
558f7cf2976SLionel Sambuc 	cmd_left();
559f7cf2976SLionel Sambuc 	clen = s - cp;
560f7cf2976SLionel Sambuc 
561f7cf2976SLionel Sambuc 	/*
562f7cf2976SLionel Sambuc 	 * Remove the char from the buffer (shift the buffer left).
563f7cf2976SLionel Sambuc 	 */
564f7cf2976SLionel Sambuc 	for (s = cp;  ;  s++)
565f7cf2976SLionel Sambuc 	{
566f7cf2976SLionel Sambuc 		s[0] = s[clen];
567f7cf2976SLionel Sambuc 		if (s[0] == '\0')
568f7cf2976SLionel Sambuc 			break;
569f7cf2976SLionel Sambuc 	}
570f7cf2976SLionel Sambuc 
571f7cf2976SLionel Sambuc 	/*
572f7cf2976SLionel Sambuc 	 * Repaint the buffer after the erased char.
573f7cf2976SLionel Sambuc 	 */
574*84d9c625SLionel Sambuc 	updown_match = -1;
575f7cf2976SLionel Sambuc 	cmd_repaint(cp);
576f7cf2976SLionel Sambuc 
577f7cf2976SLionel Sambuc 	/*
578f7cf2976SLionel Sambuc 	 * We say that erasing the entire command string causes us
579f7cf2976SLionel Sambuc 	 * to abort the current command, if CF_QUIT_ON_ERASE is set.
580f7cf2976SLionel Sambuc 	 */
581f7cf2976SLionel Sambuc 	if ((curr_cmdflags & CF_QUIT_ON_ERASE) && cp == cmdbuf && *cp == '\0')
582f7cf2976SLionel Sambuc 		return (CC_QUIT);
583f7cf2976SLionel Sambuc 	return (CC_OK);
584f7cf2976SLionel Sambuc }
585f7cf2976SLionel Sambuc 
586f7cf2976SLionel Sambuc /*
587f7cf2976SLionel Sambuc  * Delete the char under the cursor.
588f7cf2976SLionel Sambuc  */
589f7cf2976SLionel Sambuc 	static int
cmd_delete()590f7cf2976SLionel Sambuc cmd_delete()
591f7cf2976SLionel Sambuc {
592f7cf2976SLionel Sambuc 	if (*cp == '\0')
593f7cf2976SLionel Sambuc 	{
594f7cf2976SLionel Sambuc 		/* At end of string; there is no char under the cursor. */
595f7cf2976SLionel Sambuc 		return (CC_OK);
596f7cf2976SLionel Sambuc 	}
597f7cf2976SLionel Sambuc 	/*
598f7cf2976SLionel Sambuc 	 * Move right, then use cmd_erase.
599f7cf2976SLionel Sambuc 	 */
600f7cf2976SLionel Sambuc 	cmd_right();
601f7cf2976SLionel Sambuc 	cmd_erase();
602f7cf2976SLionel Sambuc 	return (CC_OK);
603f7cf2976SLionel Sambuc }
604f7cf2976SLionel Sambuc 
605f7cf2976SLionel Sambuc /*
606f7cf2976SLionel Sambuc  * Delete the "word" to the left of the cursor.
607f7cf2976SLionel Sambuc  */
608f7cf2976SLionel Sambuc 	static int
cmd_werase()609f7cf2976SLionel Sambuc cmd_werase()
610f7cf2976SLionel Sambuc {
611f7cf2976SLionel Sambuc 	if (cp > cmdbuf && cp[-1] == ' ')
612f7cf2976SLionel Sambuc 	{
613f7cf2976SLionel Sambuc 		/*
614f7cf2976SLionel Sambuc 		 * If the char left of cursor is a space,
615f7cf2976SLionel Sambuc 		 * erase all the spaces left of cursor (to the first non-space).
616f7cf2976SLionel Sambuc 		 */
617f7cf2976SLionel Sambuc 		while (cp > cmdbuf && cp[-1] == ' ')
618f7cf2976SLionel Sambuc 			(void) cmd_erase();
619f7cf2976SLionel Sambuc 	} else
620f7cf2976SLionel Sambuc 	{
621f7cf2976SLionel Sambuc 		/*
622f7cf2976SLionel Sambuc 		 * If the char left of cursor is not a space,
623f7cf2976SLionel Sambuc 		 * erase all the nonspaces left of cursor (the whole "word").
624f7cf2976SLionel Sambuc 		 */
625f7cf2976SLionel Sambuc 		while (cp > cmdbuf && cp[-1] != ' ')
626f7cf2976SLionel Sambuc 			(void) cmd_erase();
627f7cf2976SLionel Sambuc 	}
628f7cf2976SLionel Sambuc 	return (CC_OK);
629f7cf2976SLionel Sambuc }
630f7cf2976SLionel Sambuc 
631f7cf2976SLionel Sambuc /*
632f7cf2976SLionel Sambuc  * Delete the "word" under the cursor.
633f7cf2976SLionel Sambuc  */
634f7cf2976SLionel Sambuc 	static int
cmd_wdelete()635f7cf2976SLionel Sambuc cmd_wdelete()
636f7cf2976SLionel Sambuc {
637f7cf2976SLionel Sambuc 	if (*cp == ' ')
638f7cf2976SLionel Sambuc 	{
639f7cf2976SLionel Sambuc 		/*
640f7cf2976SLionel Sambuc 		 * If the char under the cursor is a space,
641f7cf2976SLionel Sambuc 		 * delete it and all the spaces right of cursor.
642f7cf2976SLionel Sambuc 		 */
643f7cf2976SLionel Sambuc 		while (*cp == ' ')
644f7cf2976SLionel Sambuc 			(void) cmd_delete();
645f7cf2976SLionel Sambuc 	} else
646f7cf2976SLionel Sambuc 	{
647f7cf2976SLionel Sambuc 		/*
648f7cf2976SLionel Sambuc 		 * If the char under the cursor is not a space,
649f7cf2976SLionel Sambuc 		 * delete it and all nonspaces right of cursor (the whole word).
650f7cf2976SLionel Sambuc 		 */
651f7cf2976SLionel Sambuc 		while (*cp != ' ' && *cp != '\0')
652f7cf2976SLionel Sambuc 			(void) cmd_delete();
653f7cf2976SLionel Sambuc 	}
654f7cf2976SLionel Sambuc 	return (CC_OK);
655f7cf2976SLionel Sambuc }
656f7cf2976SLionel Sambuc 
657f7cf2976SLionel Sambuc /*
658f7cf2976SLionel Sambuc  * Delete all chars in the command buffer.
659f7cf2976SLionel Sambuc  */
660f7cf2976SLionel Sambuc 	static int
cmd_kill()661f7cf2976SLionel Sambuc cmd_kill()
662f7cf2976SLionel Sambuc {
663f7cf2976SLionel Sambuc 	if (cmdbuf[0] == '\0')
664f7cf2976SLionel Sambuc 	{
665f7cf2976SLionel Sambuc 		/* Buffer is already empty; abort the current command. */
666f7cf2976SLionel Sambuc 		return (CC_QUIT);
667f7cf2976SLionel Sambuc 	}
668f7cf2976SLionel Sambuc 	cmd_offset = 0;
669f7cf2976SLionel Sambuc 	cmd_home();
670f7cf2976SLionel Sambuc 	*cp = '\0';
671*84d9c625SLionel Sambuc 	updown_match = -1;
672f7cf2976SLionel Sambuc 	cmd_repaint(cp);
673f7cf2976SLionel Sambuc 
674f7cf2976SLionel Sambuc 	/*
675f7cf2976SLionel Sambuc 	 * We say that erasing the entire command string causes us
676f7cf2976SLionel Sambuc 	 * to abort the current command, if CF_QUIT_ON_ERASE is set.
677f7cf2976SLionel Sambuc 	 */
678f7cf2976SLionel Sambuc 	if (curr_cmdflags & CF_QUIT_ON_ERASE)
679f7cf2976SLionel Sambuc 		return (CC_QUIT);
680f7cf2976SLionel Sambuc 	return (CC_OK);
681f7cf2976SLionel Sambuc }
682f7cf2976SLionel Sambuc 
683f7cf2976SLionel Sambuc /*
684f7cf2976SLionel Sambuc  * Select an mlist structure to be the current command history.
685f7cf2976SLionel Sambuc  */
686f7cf2976SLionel Sambuc 	public void
set_mlist(mlist,cmdflags)687f7cf2976SLionel Sambuc set_mlist(mlist, cmdflags)
688f7cf2976SLionel Sambuc 	void *mlist;
689f7cf2976SLionel Sambuc 	int cmdflags;
690f7cf2976SLionel Sambuc {
691f7cf2976SLionel Sambuc #if CMD_HISTORY
692f7cf2976SLionel Sambuc 	curr_mlist = (struct mlist *) mlist;
693f7cf2976SLionel Sambuc 	curr_cmdflags = cmdflags;
694f7cf2976SLionel Sambuc 
695f7cf2976SLionel Sambuc 	/* Make sure the next up-arrow moves to the last string in the mlist. */
696f7cf2976SLionel Sambuc 	if (curr_mlist != NULL)
697f7cf2976SLionel Sambuc 		curr_mlist->curr_mp = curr_mlist;
698f7cf2976SLionel Sambuc #endif
699f7cf2976SLionel Sambuc }
700f7cf2976SLionel Sambuc 
701f7cf2976SLionel Sambuc #if CMD_HISTORY
702f7cf2976SLionel Sambuc /*
703f7cf2976SLionel Sambuc  * Move up or down in the currently selected command history list.
704*84d9c625SLionel Sambuc  * Only consider entries whose first updown_match chars are equal to
705*84d9c625SLionel Sambuc  * cmdbuf's corresponding chars.
706f7cf2976SLionel Sambuc  */
707f7cf2976SLionel Sambuc 	static int
cmd_updown(action)708f7cf2976SLionel Sambuc cmd_updown(action)
709f7cf2976SLionel Sambuc 	int action;
710f7cf2976SLionel Sambuc {
711f7cf2976SLionel Sambuc 	char *s;
712*84d9c625SLionel Sambuc 	struct mlist *ml;
713f7cf2976SLionel Sambuc 
714f7cf2976SLionel Sambuc 	if (curr_mlist == NULL)
715f7cf2976SLionel Sambuc 	{
716f7cf2976SLionel Sambuc 		/*
717f7cf2976SLionel Sambuc 		 * The current command has no history list.
718f7cf2976SLionel Sambuc 		 */
719f7cf2976SLionel Sambuc 		bell();
720f7cf2976SLionel Sambuc 		return (CC_OK);
721f7cf2976SLionel Sambuc 	}
722*84d9c625SLionel Sambuc 
723*84d9c625SLionel Sambuc 	if (updown_match < 0)
724*84d9c625SLionel Sambuc 	{
725*84d9c625SLionel Sambuc 		updown_match = cp - cmdbuf;
726*84d9c625SLionel Sambuc 	}
727*84d9c625SLionel Sambuc 
728f7cf2976SLionel Sambuc 	/*
729*84d9c625SLionel Sambuc 	 * Find the next history entry which matches.
730f7cf2976SLionel Sambuc 	 */
731*84d9c625SLionel Sambuc 	for (ml = curr_mlist->curr_mp;;)
732*84d9c625SLionel Sambuc 	{
733*84d9c625SLionel Sambuc 		ml = (action == EC_UP) ? ml->prev : ml->next;
734*84d9c625SLionel Sambuc 		if (ml == curr_mlist)
735*84d9c625SLionel Sambuc 		{
736f7cf2976SLionel Sambuc 			/*
737*84d9c625SLionel Sambuc 			 * We reached the end (or beginning) of the list.
738*84d9c625SLionel Sambuc 			 */
739*84d9c625SLionel Sambuc 			break;
740*84d9c625SLionel Sambuc 		}
741*84d9c625SLionel Sambuc 		if (strncmp(cmdbuf, ml->string, updown_match) == 0)
742*84d9c625SLionel Sambuc 		{
743*84d9c625SLionel Sambuc 			/*
744*84d9c625SLionel Sambuc 			 * This entry matches; stop here.
745f7cf2976SLionel Sambuc 			 * Copy the entry into cmdbuf and echo it on the screen.
746f7cf2976SLionel Sambuc 			 */
747*84d9c625SLionel Sambuc 			curr_mlist->curr_mp = ml;
748*84d9c625SLionel Sambuc 			s = ml->string;
749f7cf2976SLionel Sambuc 			if (s == NULL)
750f7cf2976SLionel Sambuc 				s = "";
751*84d9c625SLionel Sambuc 			cmd_home();
752*84d9c625SLionel Sambuc 			clear_eol();
753f7cf2976SLionel Sambuc 			strcpy(cmdbuf, s);
754f7cf2976SLionel Sambuc 			for (cp = cmdbuf;  *cp != '\0';  )
755f7cf2976SLionel Sambuc 				cmd_right();
756f7cf2976SLionel Sambuc 			return (CC_OK);
757f7cf2976SLionel Sambuc 		}
758*84d9c625SLionel Sambuc 	}
759*84d9c625SLionel Sambuc 	/*
760*84d9c625SLionel Sambuc 	 * We didn't find a history entry that matches.
761*84d9c625SLionel Sambuc 	 */
762*84d9c625SLionel Sambuc 	bell();
763*84d9c625SLionel Sambuc 	return (CC_OK);
764*84d9c625SLionel Sambuc }
765f7cf2976SLionel Sambuc #endif
766f7cf2976SLionel Sambuc 
767f7cf2976SLionel Sambuc /*
768f7cf2976SLionel Sambuc  * Add a string to a history list.
769f7cf2976SLionel Sambuc  */
770f7cf2976SLionel Sambuc 	public void
cmd_addhist(mlist,cmd)771f7cf2976SLionel Sambuc cmd_addhist(mlist, cmd)
772f7cf2976SLionel Sambuc 	struct mlist *mlist;
773f7cf2976SLionel Sambuc 	char *cmd;
774f7cf2976SLionel Sambuc {
775f7cf2976SLionel Sambuc #if CMD_HISTORY
776f7cf2976SLionel Sambuc 	struct mlist *ml;
777f7cf2976SLionel Sambuc 
778f7cf2976SLionel Sambuc 	/*
779f7cf2976SLionel Sambuc 	 * Don't save a trivial command.
780f7cf2976SLionel Sambuc 	 */
781f7cf2976SLionel Sambuc 	if (strlen(cmd) == 0)
782f7cf2976SLionel Sambuc 		return;
783f7cf2976SLionel Sambuc 
784f7cf2976SLionel Sambuc 	/*
785f7cf2976SLionel Sambuc 	 * Save the command unless it's a duplicate of the
786f7cf2976SLionel Sambuc 	 * last command in the history.
787f7cf2976SLionel Sambuc 	 */
788f7cf2976SLionel Sambuc 	ml = mlist->prev;
789f7cf2976SLionel Sambuc 	if (ml == mlist || strcmp(ml->string, cmd) != 0)
790f7cf2976SLionel Sambuc 	{
791f7cf2976SLionel Sambuc 		/*
792f7cf2976SLionel Sambuc 		 * Did not find command in history.
793f7cf2976SLionel Sambuc 		 * Save the command and put it at the end of the history list.
794f7cf2976SLionel Sambuc 		 */
795f7cf2976SLionel Sambuc 		ml = (struct mlist *) ecalloc(1, sizeof(struct mlist));
796f7cf2976SLionel Sambuc 		ml->string = save(cmd);
797f7cf2976SLionel Sambuc 		ml->next = mlist;
798f7cf2976SLionel Sambuc 		ml->prev = mlist->prev;
799f7cf2976SLionel Sambuc 		mlist->prev->next = ml;
800f7cf2976SLionel Sambuc 		mlist->prev = ml;
801f7cf2976SLionel Sambuc 	}
802f7cf2976SLionel Sambuc 	/*
803f7cf2976SLionel Sambuc 	 * Point to the cmd just after the just-accepted command.
804f7cf2976SLionel Sambuc 	 * Thus, an UPARROW will always retrieve the previous command.
805f7cf2976SLionel Sambuc 	 */
806f7cf2976SLionel Sambuc 	mlist->curr_mp = ml->next;
807f7cf2976SLionel Sambuc #endif
808f7cf2976SLionel Sambuc }
809f7cf2976SLionel Sambuc 
810f7cf2976SLionel Sambuc /*
811f7cf2976SLionel Sambuc  * Accept the command in the command buffer.
812f7cf2976SLionel Sambuc  * Add it to the currently selected history list.
813f7cf2976SLionel Sambuc  */
814f7cf2976SLionel Sambuc 	public void
cmd_accept()815f7cf2976SLionel Sambuc cmd_accept()
816f7cf2976SLionel Sambuc {
817f7cf2976SLionel Sambuc #if CMD_HISTORY
818f7cf2976SLionel Sambuc 	/*
819f7cf2976SLionel Sambuc 	 * Nothing to do if there is no currently selected history list.
820f7cf2976SLionel Sambuc 	 */
821f7cf2976SLionel Sambuc 	if (curr_mlist == NULL)
822f7cf2976SLionel Sambuc 		return;
823f7cf2976SLionel Sambuc 	cmd_addhist(curr_mlist, cmdbuf);
824f7cf2976SLionel Sambuc 	curr_mlist->modified = 1;
825f7cf2976SLionel Sambuc #endif
826f7cf2976SLionel Sambuc }
827f7cf2976SLionel Sambuc 
828f7cf2976SLionel Sambuc /*
829f7cf2976SLionel Sambuc  * Try to perform a line-edit function on the command buffer,
830f7cf2976SLionel Sambuc  * using a specified char as a line-editing command.
831f7cf2976SLionel Sambuc  * Returns:
832f7cf2976SLionel Sambuc  *	CC_PASS	The char does not invoke a line edit function.
833f7cf2976SLionel Sambuc  *	CC_OK	Line edit function done.
834f7cf2976SLionel Sambuc  *	CC_QUIT	The char requests the current command to be aborted.
835f7cf2976SLionel Sambuc  */
836f7cf2976SLionel Sambuc 	static int
cmd_edit(c)837f7cf2976SLionel Sambuc cmd_edit(c)
838f7cf2976SLionel Sambuc 	int c;
839f7cf2976SLionel Sambuc {
840f7cf2976SLionel Sambuc 	int action;
841f7cf2976SLionel Sambuc 	int flags;
842f7cf2976SLionel Sambuc 
843f7cf2976SLionel Sambuc #if TAB_COMPLETE_FILENAME
844f7cf2976SLionel Sambuc #define	not_in_completion()	in_completion = 0
845f7cf2976SLionel Sambuc #else
846f7cf2976SLionel Sambuc #define	not_in_completion()
847f7cf2976SLionel Sambuc #endif
848f7cf2976SLionel Sambuc 
849f7cf2976SLionel Sambuc 	/*
850f7cf2976SLionel Sambuc 	 * See if the char is indeed a line-editing command.
851f7cf2976SLionel Sambuc 	 */
852f7cf2976SLionel Sambuc 	flags = 0;
853f7cf2976SLionel Sambuc #if CMD_HISTORY
854f7cf2976SLionel Sambuc 	if (curr_mlist == NULL)
855f7cf2976SLionel Sambuc 		/*
856f7cf2976SLionel Sambuc 		 * No current history; don't accept history manipulation cmds.
857f7cf2976SLionel Sambuc 		 */
858f7cf2976SLionel Sambuc 		flags |= EC_NOHISTORY;
859f7cf2976SLionel Sambuc #endif
860f7cf2976SLionel Sambuc #if TAB_COMPLETE_FILENAME
861f7cf2976SLionel Sambuc 	if (curr_mlist == ml_search)
862f7cf2976SLionel Sambuc 		/*
863f7cf2976SLionel Sambuc 		 * In a search command; don't accept file-completion cmds.
864f7cf2976SLionel Sambuc 		 */
865f7cf2976SLionel Sambuc 		flags |= EC_NOCOMPLETE;
866f7cf2976SLionel Sambuc #endif
867f7cf2976SLionel Sambuc 
868f7cf2976SLionel Sambuc 	action = editchar(c, flags);
869f7cf2976SLionel Sambuc 
870f7cf2976SLionel Sambuc 	switch (action)
871f7cf2976SLionel Sambuc 	{
872f7cf2976SLionel Sambuc 	case EC_RIGHT:
873f7cf2976SLionel Sambuc 		not_in_completion();
874f7cf2976SLionel Sambuc 		return (cmd_right());
875f7cf2976SLionel Sambuc 	case EC_LEFT:
876f7cf2976SLionel Sambuc 		not_in_completion();
877f7cf2976SLionel Sambuc 		return (cmd_left());
878f7cf2976SLionel Sambuc 	case EC_W_RIGHT:
879f7cf2976SLionel Sambuc 		not_in_completion();
880f7cf2976SLionel Sambuc 		while (*cp != '\0' && *cp != ' ')
881f7cf2976SLionel Sambuc 			cmd_right();
882f7cf2976SLionel Sambuc 		while (*cp == ' ')
883f7cf2976SLionel Sambuc 			cmd_right();
884f7cf2976SLionel Sambuc 		return (CC_OK);
885f7cf2976SLionel Sambuc 	case EC_W_LEFT:
886f7cf2976SLionel Sambuc 		not_in_completion();
887f7cf2976SLionel Sambuc 		while (cp > cmdbuf && cp[-1] == ' ')
888f7cf2976SLionel Sambuc 			cmd_left();
889f7cf2976SLionel Sambuc 		while (cp > cmdbuf && cp[-1] != ' ')
890f7cf2976SLionel Sambuc 			cmd_left();
891f7cf2976SLionel Sambuc 		return (CC_OK);
892f7cf2976SLionel Sambuc 	case EC_HOME:
893f7cf2976SLionel Sambuc 		not_in_completion();
894f7cf2976SLionel Sambuc 		cmd_offset = 0;
895f7cf2976SLionel Sambuc 		cmd_home();
896f7cf2976SLionel Sambuc 		cmd_repaint(cp);
897f7cf2976SLionel Sambuc 		return (CC_OK);
898f7cf2976SLionel Sambuc 	case EC_END:
899f7cf2976SLionel Sambuc 		not_in_completion();
900f7cf2976SLionel Sambuc 		while (*cp != '\0')
901f7cf2976SLionel Sambuc 			cmd_right();
902f7cf2976SLionel Sambuc 		return (CC_OK);
903f7cf2976SLionel Sambuc 	case EC_INSERT:
904f7cf2976SLionel Sambuc 		not_in_completion();
905f7cf2976SLionel Sambuc 		return (CC_OK);
906f7cf2976SLionel Sambuc 	case EC_BACKSPACE:
907f7cf2976SLionel Sambuc 		not_in_completion();
908f7cf2976SLionel Sambuc 		return (cmd_erase());
909f7cf2976SLionel Sambuc 	case EC_LINEKILL:
910f7cf2976SLionel Sambuc 		not_in_completion();
911f7cf2976SLionel Sambuc 		return (cmd_kill());
912f7cf2976SLionel Sambuc 	case EC_ABORT:
913f7cf2976SLionel Sambuc 		not_in_completion();
914f7cf2976SLionel Sambuc 		(void) cmd_kill();
915f7cf2976SLionel Sambuc 		return (CC_QUIT);
916f7cf2976SLionel Sambuc 	case EC_W_BACKSPACE:
917f7cf2976SLionel Sambuc 		not_in_completion();
918f7cf2976SLionel Sambuc 		return (cmd_werase());
919f7cf2976SLionel Sambuc 	case EC_DELETE:
920f7cf2976SLionel Sambuc 		not_in_completion();
921f7cf2976SLionel Sambuc 		return (cmd_delete());
922f7cf2976SLionel Sambuc 	case EC_W_DELETE:
923f7cf2976SLionel Sambuc 		not_in_completion();
924f7cf2976SLionel Sambuc 		return (cmd_wdelete());
925f7cf2976SLionel Sambuc 	case EC_LITERAL:
926f7cf2976SLionel Sambuc 		literal = 1;
927f7cf2976SLionel Sambuc 		return (CC_OK);
928f7cf2976SLionel Sambuc #if CMD_HISTORY
929f7cf2976SLionel Sambuc 	case EC_UP:
930f7cf2976SLionel Sambuc 	case EC_DOWN:
931f7cf2976SLionel Sambuc 		not_in_completion();
932f7cf2976SLionel Sambuc 		return (cmd_updown(action));
933f7cf2976SLionel Sambuc #endif
934f7cf2976SLionel Sambuc #if TAB_COMPLETE_FILENAME
935f7cf2976SLionel Sambuc 	case EC_F_COMPLETE:
936f7cf2976SLionel Sambuc 	case EC_B_COMPLETE:
937f7cf2976SLionel Sambuc 	case EC_EXPAND:
938f7cf2976SLionel Sambuc 		return (cmd_complete(action));
939f7cf2976SLionel Sambuc #endif
940f7cf2976SLionel Sambuc 	case EC_NOACTION:
941f7cf2976SLionel Sambuc 		return (CC_OK);
942f7cf2976SLionel Sambuc 	default:
943f7cf2976SLionel Sambuc 		not_in_completion();
944f7cf2976SLionel Sambuc 		return (CC_PASS);
945f7cf2976SLionel Sambuc 	}
946f7cf2976SLionel Sambuc }
947f7cf2976SLionel Sambuc 
948f7cf2976SLionel Sambuc #if TAB_COMPLETE_FILENAME
949f7cf2976SLionel Sambuc /*
950f7cf2976SLionel Sambuc  * Insert a string into the command buffer, at the current position.
951f7cf2976SLionel Sambuc  */
952f7cf2976SLionel Sambuc 	static int
cmd_istr(str)953f7cf2976SLionel Sambuc cmd_istr(str)
954f7cf2976SLionel Sambuc 	char *str;
955f7cf2976SLionel Sambuc {
956f7cf2976SLionel Sambuc 	char *s;
957f7cf2976SLionel Sambuc 	int action;
958f7cf2976SLionel Sambuc 	char *endline = str + strlen(str);
959f7cf2976SLionel Sambuc 
960f7cf2976SLionel Sambuc 	for (s = str;  *s != '\0';  )
961f7cf2976SLionel Sambuc 	{
962f7cf2976SLionel Sambuc 		char *os = s;
963f7cf2976SLionel Sambuc 		step_char(&s, +1, endline);
964f7cf2976SLionel Sambuc 		action = cmd_ichar(os, s - os);
965f7cf2976SLionel Sambuc 		if (action != CC_OK)
966f7cf2976SLionel Sambuc 		{
967f7cf2976SLionel Sambuc 			bell();
968f7cf2976SLionel Sambuc 			return (action);
969f7cf2976SLionel Sambuc 		}
970f7cf2976SLionel Sambuc 	}
971f7cf2976SLionel Sambuc 	return (CC_OK);
972f7cf2976SLionel Sambuc }
973f7cf2976SLionel Sambuc 
974f7cf2976SLionel Sambuc /*
975f7cf2976SLionel Sambuc  * Find the beginning and end of the "current" word.
976f7cf2976SLionel Sambuc  * This is the word which the cursor (cp) is inside or at the end of.
977f7cf2976SLionel Sambuc  * Return pointer to the beginning of the word and put the
978f7cf2976SLionel Sambuc  * cursor at the end of the word.
979f7cf2976SLionel Sambuc  */
980f7cf2976SLionel Sambuc 	static char *
delimit_word()981f7cf2976SLionel Sambuc delimit_word()
982f7cf2976SLionel Sambuc {
983f7cf2976SLionel Sambuc 	char *word = NULL;
984f7cf2976SLionel Sambuc #if SPACES_IN_FILENAMES
985f7cf2976SLionel Sambuc 	char *p;
986f7cf2976SLionel Sambuc 	int delim_quoted = 0;
987f7cf2976SLionel Sambuc 	int meta_quoted = 0;
988f7cf2976SLionel Sambuc 	char *esc = get_meta_escape();
989f7cf2976SLionel Sambuc 	int esclen = strlen(esc);
990f7cf2976SLionel Sambuc #endif
991f7cf2976SLionel Sambuc 
992f7cf2976SLionel Sambuc 	/*
993f7cf2976SLionel Sambuc 	 * Move cursor to end of word.
994f7cf2976SLionel Sambuc 	 */
995f7cf2976SLionel Sambuc 	if (*cp != ' ' && *cp != '\0')
996f7cf2976SLionel Sambuc 	{
997f7cf2976SLionel Sambuc 		/*
998f7cf2976SLionel Sambuc 		 * Cursor is on a nonspace.
999f7cf2976SLionel Sambuc 		 * Move cursor right to the next space.
1000f7cf2976SLionel Sambuc 		 */
1001f7cf2976SLionel Sambuc 		while (*cp != ' ' && *cp != '\0')
1002f7cf2976SLionel Sambuc 			cmd_right();
1003f7cf2976SLionel Sambuc 	} else if (cp > cmdbuf && cp[-1] != ' ')
1004f7cf2976SLionel Sambuc 	{
1005f7cf2976SLionel Sambuc 		/*
1006f7cf2976SLionel Sambuc 		 * Cursor is on a space, and char to the left is a nonspace.
1007f7cf2976SLionel Sambuc 		 * We're already at the end of the word.
1008f7cf2976SLionel Sambuc 		 */
1009f7cf2976SLionel Sambuc 		;
1010f7cf2976SLionel Sambuc #if 0
1011f7cf2976SLionel Sambuc 	} else
1012f7cf2976SLionel Sambuc 	{
1013f7cf2976SLionel Sambuc 		/*
1014f7cf2976SLionel Sambuc 		 * Cursor is on a space and char to the left is a space.
1015f7cf2976SLionel Sambuc 		 * Huh? There's no word here.
1016f7cf2976SLionel Sambuc 		 */
1017f7cf2976SLionel Sambuc 		return (NULL);
1018f7cf2976SLionel Sambuc #endif
1019f7cf2976SLionel Sambuc 	}
1020f7cf2976SLionel Sambuc 	/*
1021f7cf2976SLionel Sambuc 	 * Find the beginning of the word which the cursor is in.
1022f7cf2976SLionel Sambuc 	 */
1023f7cf2976SLionel Sambuc 	if (cp == cmdbuf)
1024f7cf2976SLionel Sambuc 		return (NULL);
1025f7cf2976SLionel Sambuc #if SPACES_IN_FILENAMES
1026f7cf2976SLionel Sambuc 	/*
1027f7cf2976SLionel Sambuc 	 * If we have an unbalanced quote (that is, an open quote
1028f7cf2976SLionel Sambuc 	 * without a corresponding close quote), we return everything
1029f7cf2976SLionel Sambuc 	 * from the open quote, including spaces.
1030f7cf2976SLionel Sambuc 	 */
1031f7cf2976SLionel Sambuc 	for (word = cmdbuf;  word < cp;  word++)
1032f7cf2976SLionel Sambuc 		if (*word != ' ')
1033f7cf2976SLionel Sambuc 			break;
1034f7cf2976SLionel Sambuc 	if (word >= cp)
1035f7cf2976SLionel Sambuc 		return (cp);
1036f7cf2976SLionel Sambuc 	for (p = cmdbuf;  p < cp;  p++)
1037f7cf2976SLionel Sambuc 	{
1038f7cf2976SLionel Sambuc 		if (meta_quoted)
1039f7cf2976SLionel Sambuc 		{
1040f7cf2976SLionel Sambuc 			meta_quoted = 0;
1041f7cf2976SLionel Sambuc 		} else if (esclen > 0 && p + esclen < cp &&
1042f7cf2976SLionel Sambuc 		           strncmp(p, esc, esclen) == 0)
1043f7cf2976SLionel Sambuc 		{
1044f7cf2976SLionel Sambuc 			meta_quoted = 1;
1045f7cf2976SLionel Sambuc 			p += esclen - 1;
1046f7cf2976SLionel Sambuc 		} else if (delim_quoted)
1047f7cf2976SLionel Sambuc 		{
1048f7cf2976SLionel Sambuc 			if (*p == closequote)
1049f7cf2976SLionel Sambuc 				delim_quoted = 0;
1050f7cf2976SLionel Sambuc 		} else /* (!delim_quoted) */
1051f7cf2976SLionel Sambuc 		{
1052f7cf2976SLionel Sambuc 			if (*p == openquote)
1053f7cf2976SLionel Sambuc 				delim_quoted = 1;
1054f7cf2976SLionel Sambuc 			else if (*p == ' ')
1055f7cf2976SLionel Sambuc 				word = p+1;
1056f7cf2976SLionel Sambuc 		}
1057f7cf2976SLionel Sambuc 	}
1058f7cf2976SLionel Sambuc #endif
1059f7cf2976SLionel Sambuc 	return (word);
1060f7cf2976SLionel Sambuc }
1061f7cf2976SLionel Sambuc 
1062f7cf2976SLionel Sambuc /*
1063f7cf2976SLionel Sambuc  * Set things up to enter completion mode.
1064f7cf2976SLionel Sambuc  * Expand the word under the cursor into a list of filenames
1065f7cf2976SLionel Sambuc  * which start with that word, and set tk_text to that list.
1066f7cf2976SLionel Sambuc  */
1067f7cf2976SLionel Sambuc 	static void
init_compl()1068f7cf2976SLionel Sambuc init_compl()
1069f7cf2976SLionel Sambuc {
1070f7cf2976SLionel Sambuc 	char *word;
1071f7cf2976SLionel Sambuc 	char c;
1072f7cf2976SLionel Sambuc 
1073f7cf2976SLionel Sambuc 	/*
1074f7cf2976SLionel Sambuc 	 * Get rid of any previous tk_text.
1075f7cf2976SLionel Sambuc 	 */
1076f7cf2976SLionel Sambuc 	if (tk_text != NULL)
1077f7cf2976SLionel Sambuc 	{
1078f7cf2976SLionel Sambuc 		free(tk_text);
1079f7cf2976SLionel Sambuc 		tk_text = NULL;
1080f7cf2976SLionel Sambuc 	}
1081f7cf2976SLionel Sambuc 	/*
1082f7cf2976SLionel Sambuc 	 * Find the original (uncompleted) word in the command buffer.
1083f7cf2976SLionel Sambuc 	 */
1084f7cf2976SLionel Sambuc 	word = delimit_word();
1085f7cf2976SLionel Sambuc 	if (word == NULL)
1086f7cf2976SLionel Sambuc 		return;
1087f7cf2976SLionel Sambuc 	/*
1088f7cf2976SLionel Sambuc 	 * Set the insertion point to the point in the command buffer
1089f7cf2976SLionel Sambuc 	 * where the original (uncompleted) word now sits.
1090f7cf2976SLionel Sambuc 	 */
1091f7cf2976SLionel Sambuc 	tk_ipoint = word;
1092f7cf2976SLionel Sambuc 	/*
1093f7cf2976SLionel Sambuc 	 * Save the original (uncompleted) word
1094f7cf2976SLionel Sambuc 	 */
1095f7cf2976SLionel Sambuc 	if (tk_original != NULL)
1096f7cf2976SLionel Sambuc 		free(tk_original);
1097f7cf2976SLionel Sambuc 	tk_original = (char *) ecalloc(cp-word+1, sizeof(char));
1098f7cf2976SLionel Sambuc 	strncpy(tk_original, word, cp-word);
1099f7cf2976SLionel Sambuc 	/*
1100f7cf2976SLionel Sambuc 	 * Get the expanded filename.
1101f7cf2976SLionel Sambuc 	 * This may result in a single filename, or
1102f7cf2976SLionel Sambuc 	 * a blank-separated list of filenames.
1103f7cf2976SLionel Sambuc 	 */
1104f7cf2976SLionel Sambuc 	c = *cp;
1105f7cf2976SLionel Sambuc 	*cp = '\0';
1106f7cf2976SLionel Sambuc 	if (*word != openquote)
1107f7cf2976SLionel Sambuc 	{
1108f7cf2976SLionel Sambuc 		tk_text = fcomplete(word);
1109f7cf2976SLionel Sambuc 	} else
1110f7cf2976SLionel Sambuc 	{
1111*84d9c625SLionel Sambuc #if MSDOS_COMPILER
1112*84d9c625SLionel Sambuc 		char *qword = NULL;
1113*84d9c625SLionel Sambuc #else
1114f7cf2976SLionel Sambuc 		char *qword = shell_quote(word+1);
1115*84d9c625SLionel Sambuc #endif
1116f7cf2976SLionel Sambuc 		if (qword == NULL)
1117f7cf2976SLionel Sambuc 			tk_text = fcomplete(word+1);
1118f7cf2976SLionel Sambuc 		else
1119f7cf2976SLionel Sambuc 		{
1120f7cf2976SLionel Sambuc 			tk_text = fcomplete(qword);
1121f7cf2976SLionel Sambuc 			free(qword);
1122f7cf2976SLionel Sambuc 		}
1123f7cf2976SLionel Sambuc 	}
1124f7cf2976SLionel Sambuc 	*cp = c;
1125f7cf2976SLionel Sambuc }
1126f7cf2976SLionel Sambuc 
1127f7cf2976SLionel Sambuc /*
1128f7cf2976SLionel Sambuc  * Return the next word in the current completion list.
1129f7cf2976SLionel Sambuc  */
1130f7cf2976SLionel Sambuc 	static char *
next_compl(action,prev)1131f7cf2976SLionel Sambuc next_compl(action, prev)
1132f7cf2976SLionel Sambuc 	int action;
1133f7cf2976SLionel Sambuc 	char *prev;
1134f7cf2976SLionel Sambuc {
1135f7cf2976SLionel Sambuc 	switch (action)
1136f7cf2976SLionel Sambuc 	{
1137f7cf2976SLionel Sambuc 	case EC_F_COMPLETE:
1138f7cf2976SLionel Sambuc 		return (forw_textlist(&tk_tlist, prev));
1139f7cf2976SLionel Sambuc 	case EC_B_COMPLETE:
1140f7cf2976SLionel Sambuc 		return (back_textlist(&tk_tlist, prev));
1141f7cf2976SLionel Sambuc 	}
1142f7cf2976SLionel Sambuc 	/* Cannot happen */
1143f7cf2976SLionel Sambuc 	return ("?");
1144f7cf2976SLionel Sambuc }
1145f7cf2976SLionel Sambuc 
1146f7cf2976SLionel Sambuc /*
1147f7cf2976SLionel Sambuc  * Complete the filename before (or under) the cursor.
1148f7cf2976SLionel Sambuc  * cmd_complete may be called multiple times.  The global in_completion
1149f7cf2976SLionel Sambuc  * remembers whether this call is the first time (create the list),
1150f7cf2976SLionel Sambuc  * or a subsequent time (step thru the list).
1151f7cf2976SLionel Sambuc  */
1152f7cf2976SLionel Sambuc 	static int
cmd_complete(action)1153f7cf2976SLionel Sambuc cmd_complete(action)
1154f7cf2976SLionel Sambuc 	int action;
1155f7cf2976SLionel Sambuc {
1156f7cf2976SLionel Sambuc 	char *s;
1157f7cf2976SLionel Sambuc 
1158f7cf2976SLionel Sambuc 	if (!in_completion || action == EC_EXPAND)
1159f7cf2976SLionel Sambuc 	{
1160f7cf2976SLionel Sambuc 		/*
1161f7cf2976SLionel Sambuc 		 * Expand the word under the cursor and
1162f7cf2976SLionel Sambuc 		 * use the first word in the expansion
1163f7cf2976SLionel Sambuc 		 * (or the entire expansion if we're doing EC_EXPAND).
1164f7cf2976SLionel Sambuc 		 */
1165f7cf2976SLionel Sambuc 		init_compl();
1166f7cf2976SLionel Sambuc 		if (tk_text == NULL)
1167f7cf2976SLionel Sambuc 		{
1168f7cf2976SLionel Sambuc 			bell();
1169f7cf2976SLionel Sambuc 			return (CC_OK);
1170f7cf2976SLionel Sambuc 		}
1171f7cf2976SLionel Sambuc 		if (action == EC_EXPAND)
1172f7cf2976SLionel Sambuc 		{
1173f7cf2976SLionel Sambuc 			/*
1174f7cf2976SLionel Sambuc 			 * Use the whole list.
1175f7cf2976SLionel Sambuc 			 */
1176f7cf2976SLionel Sambuc 			tk_trial = tk_text;
1177f7cf2976SLionel Sambuc 		} else
1178f7cf2976SLionel Sambuc 		{
1179f7cf2976SLionel Sambuc 			/*
1180f7cf2976SLionel Sambuc 			 * Use the first filename in the list.
1181f7cf2976SLionel Sambuc 			 */
1182f7cf2976SLionel Sambuc 			in_completion = 1;
1183f7cf2976SLionel Sambuc 			init_textlist(&tk_tlist, tk_text);
1184f7cf2976SLionel Sambuc 			tk_trial = next_compl(action, (char*)NULL);
1185f7cf2976SLionel Sambuc 		}
1186f7cf2976SLionel Sambuc 	} else
1187f7cf2976SLionel Sambuc 	{
1188f7cf2976SLionel Sambuc 		/*
1189f7cf2976SLionel Sambuc 		 * We already have a completion list.
1190f7cf2976SLionel Sambuc 		 * Use the next/previous filename from the list.
1191f7cf2976SLionel Sambuc 		 */
1192f7cf2976SLionel Sambuc 		tk_trial = next_compl(action, tk_trial);
1193f7cf2976SLionel Sambuc 	}
1194f7cf2976SLionel Sambuc 
1195f7cf2976SLionel Sambuc   	/*
1196f7cf2976SLionel Sambuc   	 * Remove the original word, or the previous trial completion.
1197f7cf2976SLionel Sambuc   	 */
1198f7cf2976SLionel Sambuc 	while (cp > tk_ipoint)
1199f7cf2976SLionel Sambuc 		(void) cmd_erase();
1200f7cf2976SLionel Sambuc 
1201f7cf2976SLionel Sambuc 	if (tk_trial == NULL)
1202f7cf2976SLionel Sambuc 	{
1203f7cf2976SLionel Sambuc 		/*
1204f7cf2976SLionel Sambuc 		 * There are no more trial completions.
1205f7cf2976SLionel Sambuc 		 * Insert the original (uncompleted) filename.
1206f7cf2976SLionel Sambuc 		 */
1207f7cf2976SLionel Sambuc 		in_completion = 0;
1208f7cf2976SLionel Sambuc 		if (cmd_istr(tk_original) != CC_OK)
1209f7cf2976SLionel Sambuc 			goto fail;
1210f7cf2976SLionel Sambuc 	} else
1211f7cf2976SLionel Sambuc 	{
1212f7cf2976SLionel Sambuc 		/*
1213f7cf2976SLionel Sambuc 		 * Insert trial completion.
1214f7cf2976SLionel Sambuc 		 */
1215f7cf2976SLionel Sambuc 		if (cmd_istr(tk_trial) != CC_OK)
1216f7cf2976SLionel Sambuc 			goto fail;
1217f7cf2976SLionel Sambuc 		/*
1218f7cf2976SLionel Sambuc 		 * If it is a directory, append a slash.
1219f7cf2976SLionel Sambuc 		 */
1220f7cf2976SLionel Sambuc 		if (is_dir(tk_trial))
1221f7cf2976SLionel Sambuc 		{
1222f7cf2976SLionel Sambuc 			if (cp > cmdbuf && cp[-1] == closequote)
1223f7cf2976SLionel Sambuc 				(void) cmd_erase();
1224f7cf2976SLionel Sambuc 			s = lgetenv("LESSSEPARATOR");
1225f7cf2976SLionel Sambuc 			if (s == NULL)
1226f7cf2976SLionel Sambuc 				s = PATHNAME_SEP;
1227f7cf2976SLionel Sambuc 			if (cmd_istr(s) != CC_OK)
1228f7cf2976SLionel Sambuc 				goto fail;
1229f7cf2976SLionel Sambuc 		}
1230f7cf2976SLionel Sambuc 	}
1231f7cf2976SLionel Sambuc 
1232f7cf2976SLionel Sambuc 	return (CC_OK);
1233f7cf2976SLionel Sambuc 
1234f7cf2976SLionel Sambuc fail:
1235f7cf2976SLionel Sambuc 	in_completion = 0;
1236f7cf2976SLionel Sambuc 	bell();
1237f7cf2976SLionel Sambuc 	return (CC_OK);
1238f7cf2976SLionel Sambuc }
1239f7cf2976SLionel Sambuc 
1240f7cf2976SLionel Sambuc #endif /* TAB_COMPLETE_FILENAME */
1241f7cf2976SLionel Sambuc 
1242f7cf2976SLionel Sambuc /*
1243f7cf2976SLionel Sambuc  * Process a single character of a multi-character command, such as
1244f7cf2976SLionel Sambuc  * a number, or the pattern of a search command.
1245f7cf2976SLionel Sambuc  * Returns:
1246f7cf2976SLionel Sambuc  *	CC_OK		The char was accepted.
1247f7cf2976SLionel Sambuc  *	CC_QUIT		The char requests the command to be aborted.
1248f7cf2976SLionel Sambuc  *	CC_ERROR	The char could not be accepted due to an error.
1249f7cf2976SLionel Sambuc  */
1250f7cf2976SLionel Sambuc 	public int
cmd_char(c)1251f7cf2976SLionel Sambuc cmd_char(c)
1252f7cf2976SLionel Sambuc 	int c;
1253f7cf2976SLionel Sambuc {
1254f7cf2976SLionel Sambuc 	int action;
1255f7cf2976SLionel Sambuc 	int len;
1256f7cf2976SLionel Sambuc 
1257f7cf2976SLionel Sambuc 	if (!utf_mode)
1258f7cf2976SLionel Sambuc 	{
1259f7cf2976SLionel Sambuc 		cmd_mbc_buf[0] = c;
1260f7cf2976SLionel Sambuc 		len = 1;
1261f7cf2976SLionel Sambuc 	} else
1262f7cf2976SLionel Sambuc 	{
1263f7cf2976SLionel Sambuc 		/* Perform strict validation in all possible cases.  */
1264f7cf2976SLionel Sambuc 		if (cmd_mbc_buf_len == 0)
1265f7cf2976SLionel Sambuc 		{
1266f7cf2976SLionel Sambuc 		 retry:
1267f7cf2976SLionel Sambuc 			cmd_mbc_buf_index = 1;
1268f7cf2976SLionel Sambuc 			*cmd_mbc_buf = c;
1269f7cf2976SLionel Sambuc 			if (IS_ASCII_OCTET(c))
1270f7cf2976SLionel Sambuc 				cmd_mbc_buf_len = 1;
1271f7cf2976SLionel Sambuc 			else if (IS_UTF8_LEAD(c))
1272f7cf2976SLionel Sambuc 			{
1273f7cf2976SLionel Sambuc 				cmd_mbc_buf_len = utf_len(c);
1274f7cf2976SLionel Sambuc 				return (CC_OK);
1275f7cf2976SLionel Sambuc 			} else
1276f7cf2976SLionel Sambuc 			{
1277f7cf2976SLionel Sambuc 				/* UTF8_INVALID or stray UTF8_TRAIL */
1278f7cf2976SLionel Sambuc 				bell();
1279f7cf2976SLionel Sambuc 				return (CC_ERROR);
1280f7cf2976SLionel Sambuc 			}
1281f7cf2976SLionel Sambuc 		} else if (IS_UTF8_TRAIL(c))
1282f7cf2976SLionel Sambuc 		{
1283f7cf2976SLionel Sambuc 			cmd_mbc_buf[cmd_mbc_buf_index++] = c;
1284f7cf2976SLionel Sambuc 			if (cmd_mbc_buf_index < cmd_mbc_buf_len)
1285f7cf2976SLionel Sambuc 				return (CC_OK);
1286f7cf2976SLionel Sambuc 			if (!is_utf8_well_formed(cmd_mbc_buf))
1287f7cf2976SLionel Sambuc 			{
1288f7cf2976SLionel Sambuc 				/* complete, but not well formed (non-shortest form), sequence */
1289f7cf2976SLionel Sambuc 				cmd_mbc_buf_len = 0;
1290f7cf2976SLionel Sambuc 				bell();
1291f7cf2976SLionel Sambuc 				return (CC_ERROR);
1292f7cf2976SLionel Sambuc 			}
1293f7cf2976SLionel Sambuc 		} else
1294f7cf2976SLionel Sambuc 		{
1295f7cf2976SLionel Sambuc 			/* Flush incomplete (truncated) sequence.  */
1296f7cf2976SLionel Sambuc 			cmd_mbc_buf_len = 0;
1297f7cf2976SLionel Sambuc 			bell();
1298f7cf2976SLionel Sambuc 			/* Handle new char.  */
1299f7cf2976SLionel Sambuc 			goto retry;
1300f7cf2976SLionel Sambuc 		}
1301f7cf2976SLionel Sambuc 
1302f7cf2976SLionel Sambuc 		len = cmd_mbc_buf_len;
1303f7cf2976SLionel Sambuc 		cmd_mbc_buf_len = 0;
1304f7cf2976SLionel Sambuc 	}
1305f7cf2976SLionel Sambuc 
1306f7cf2976SLionel Sambuc 	if (literal)
1307f7cf2976SLionel Sambuc 	{
1308f7cf2976SLionel Sambuc 		/*
1309f7cf2976SLionel Sambuc 		 * Insert the char, even if it is a line-editing char.
1310f7cf2976SLionel Sambuc 		 */
1311f7cf2976SLionel Sambuc 		literal = 0;
1312f7cf2976SLionel Sambuc 		return (cmd_ichar(cmd_mbc_buf, len));
1313f7cf2976SLionel Sambuc 	}
1314f7cf2976SLionel Sambuc 
1315f7cf2976SLionel Sambuc 	/*
1316f7cf2976SLionel Sambuc 	 * See if it is a line-editing character.
1317f7cf2976SLionel Sambuc 	 */
1318f7cf2976SLionel Sambuc 	if (in_mca() && len == 1)
1319f7cf2976SLionel Sambuc 	{
1320f7cf2976SLionel Sambuc 		action = cmd_edit(c);
1321f7cf2976SLionel Sambuc 		switch (action)
1322f7cf2976SLionel Sambuc 		{
1323f7cf2976SLionel Sambuc 		case CC_OK:
1324f7cf2976SLionel Sambuc 		case CC_QUIT:
1325f7cf2976SLionel Sambuc 			return (action);
1326f7cf2976SLionel Sambuc 		case CC_PASS:
1327f7cf2976SLionel Sambuc 			break;
1328f7cf2976SLionel Sambuc 		}
1329f7cf2976SLionel Sambuc 	}
1330f7cf2976SLionel Sambuc 
1331f7cf2976SLionel Sambuc 	/*
1332f7cf2976SLionel Sambuc 	 * Insert the char into the command buffer.
1333f7cf2976SLionel Sambuc 	 */
1334f7cf2976SLionel Sambuc 	return (cmd_ichar(cmd_mbc_buf, len));
1335f7cf2976SLionel Sambuc }
1336f7cf2976SLionel Sambuc 
1337f7cf2976SLionel Sambuc /*
1338f7cf2976SLionel Sambuc  * Return the number currently in the command buffer.
1339f7cf2976SLionel Sambuc  */
1340f7cf2976SLionel Sambuc 	public LINENUM
cmd_int(frac)1341f7cf2976SLionel Sambuc cmd_int(frac)
1342f7cf2976SLionel Sambuc 	long *frac;
1343f7cf2976SLionel Sambuc {
1344f7cf2976SLionel Sambuc 	char *p;
1345f7cf2976SLionel Sambuc 	LINENUM n = 0;
1346f7cf2976SLionel Sambuc 	int err;
1347f7cf2976SLionel Sambuc 
1348f7cf2976SLionel Sambuc 	for (p = cmdbuf;  *p >= '0' && *p <= '9';  p++)
1349f7cf2976SLionel Sambuc 		n = (n * 10) + (*p - '0');
1350f7cf2976SLionel Sambuc 	*frac = 0;
1351f7cf2976SLionel Sambuc 	if (*p++ == '.')
1352f7cf2976SLionel Sambuc 	{
1353f7cf2976SLionel Sambuc 		*frac = getfraction(&p, NULL, &err);
1354f7cf2976SLionel Sambuc 		/* {{ do something if err is set? }} */
1355f7cf2976SLionel Sambuc 	}
1356f7cf2976SLionel Sambuc 	return (n);
1357f7cf2976SLionel Sambuc }
1358f7cf2976SLionel Sambuc 
1359f7cf2976SLionel Sambuc /*
1360f7cf2976SLionel Sambuc  * Return a pointer to the command buffer.
1361f7cf2976SLionel Sambuc  */
1362f7cf2976SLionel Sambuc 	public char *
get_cmdbuf()1363f7cf2976SLionel Sambuc get_cmdbuf()
1364f7cf2976SLionel Sambuc {
1365f7cf2976SLionel Sambuc 	return (cmdbuf);
1366f7cf2976SLionel Sambuc }
1367f7cf2976SLionel Sambuc 
1368f7cf2976SLionel Sambuc #if CMD_HISTORY
1369f7cf2976SLionel Sambuc /*
1370f7cf2976SLionel Sambuc  * Return the last (most recent) string in the current command history.
1371f7cf2976SLionel Sambuc  */
1372f7cf2976SLionel Sambuc 	public char *
cmd_lastpattern()1373f7cf2976SLionel Sambuc cmd_lastpattern()
1374f7cf2976SLionel Sambuc {
1375f7cf2976SLionel Sambuc 	if (curr_mlist == NULL)
1376f7cf2976SLionel Sambuc 		return (NULL);
1377f7cf2976SLionel Sambuc 	return (curr_mlist->curr_mp->prev->string);
1378f7cf2976SLionel Sambuc }
1379f7cf2976SLionel Sambuc #endif
1380f7cf2976SLionel Sambuc 
1381f7cf2976SLionel Sambuc #if CMD_HISTORY
1382f7cf2976SLionel Sambuc /*
1383f7cf2976SLionel Sambuc  * Get the name of the history file.
1384f7cf2976SLionel Sambuc  */
1385f7cf2976SLionel Sambuc 	static char *
histfile_name()1386f7cf2976SLionel Sambuc histfile_name()
1387f7cf2976SLionel Sambuc {
1388f7cf2976SLionel Sambuc 	char *home;
1389f7cf2976SLionel Sambuc 	char *name;
1390f7cf2976SLionel Sambuc 	int len;
1391f7cf2976SLionel Sambuc 
1392f7cf2976SLionel Sambuc 	/* See if filename is explicitly specified by $LESSHISTFILE. */
1393f7cf2976SLionel Sambuc 	name = lgetenv("LESSHISTFILE");
1394f7cf2976SLionel Sambuc 	if (name != NULL && *name != '\0')
1395f7cf2976SLionel Sambuc 	{
1396f7cf2976SLionel Sambuc 		if (strcmp(name, "-") == 0 || strcmp(name, "/dev/null") == 0)
1397f7cf2976SLionel Sambuc 			/* $LESSHISTFILE == "-" means don't use a history file. */
1398f7cf2976SLionel Sambuc 			return (NULL);
1399f7cf2976SLionel Sambuc 		return (save(name));
1400f7cf2976SLionel Sambuc 	}
1401f7cf2976SLionel Sambuc 
1402f7cf2976SLionel Sambuc 	/* Otherwise, file is in $HOME. */
1403f7cf2976SLionel Sambuc 	home = lgetenv("HOME");
1404f7cf2976SLionel Sambuc 	if (home == NULL || *home == '\0')
1405f7cf2976SLionel Sambuc 	{
1406f7cf2976SLionel Sambuc #if OS2
1407f7cf2976SLionel Sambuc 		home = lgetenv("INIT");
1408f7cf2976SLionel Sambuc 		if (home == NULL || *home == '\0')
1409f7cf2976SLionel Sambuc #endif
1410f7cf2976SLionel Sambuc 			return (NULL);
1411f7cf2976SLionel Sambuc 	}
1412f7cf2976SLionel Sambuc 	len = strlen(home) + strlen(LESSHISTFILE) + 2;
1413f7cf2976SLionel Sambuc 	name = (char *) ecalloc(len, sizeof(char));
1414f7cf2976SLionel Sambuc 	SNPRINTF2(name, len, "%s/%s", home, LESSHISTFILE);
1415f7cf2976SLionel Sambuc 	return (name);
1416f7cf2976SLionel Sambuc }
1417f7cf2976SLionel Sambuc #endif /* CMD_HISTORY */
1418f7cf2976SLionel Sambuc 
1419f7cf2976SLionel Sambuc /*
1420f7cf2976SLionel Sambuc  * Initialize history from a .lesshist file.
1421f7cf2976SLionel Sambuc  */
1422f7cf2976SLionel Sambuc 	public void
init_cmdhist()1423f7cf2976SLionel Sambuc init_cmdhist()
1424f7cf2976SLionel Sambuc {
1425f7cf2976SLionel Sambuc #if CMD_HISTORY
1426f7cf2976SLionel Sambuc 	struct mlist *ml = NULL;
1427f7cf2976SLionel Sambuc 	char line[CMDBUF_SIZE];
1428f7cf2976SLionel Sambuc 	char *filename;
1429f7cf2976SLionel Sambuc 	FILE *f;
1430f7cf2976SLionel Sambuc 	char *p;
1431f7cf2976SLionel Sambuc #ifdef HAVE_STAT
1432f7cf2976SLionel Sambuc 	struct stat st;
1433f7cf2976SLionel Sambuc #endif
1434f7cf2976SLionel Sambuc 
1435f7cf2976SLionel Sambuc 	filename = histfile_name();
1436f7cf2976SLionel Sambuc 	if (filename == NULL)
1437f7cf2976SLionel Sambuc 		return;
1438f7cf2976SLionel Sambuc #ifdef HAVE_STAT
1439f7cf2976SLionel Sambuc 	/* ignore devices/fifos; allow symlinks */
1440f7cf2976SLionel Sambuc 	if (stat(filename, &st) < 0)
1441f7cf2976SLionel Sambuc 		return;
1442f7cf2976SLionel Sambuc 	if (!S_ISREG(st.st_mode))
1443f7cf2976SLionel Sambuc 		return;
1444f7cf2976SLionel Sambuc #endif
1445f7cf2976SLionel Sambuc 	f = fopen(filename, "r");
1446f7cf2976SLionel Sambuc 	free(filename);
1447f7cf2976SLionel Sambuc 	if (f == NULL)
1448f7cf2976SLionel Sambuc 		return;
1449f7cf2976SLionel Sambuc 	if (fgets(line, sizeof(line), f) == NULL ||
1450f7cf2976SLionel Sambuc 	    strncmp(line, HISTFILE_FIRST_LINE, strlen(HISTFILE_FIRST_LINE)) != 0)
1451f7cf2976SLionel Sambuc 	{
1452f7cf2976SLionel Sambuc 		fclose(f);
1453f7cf2976SLionel Sambuc 		return;
1454f7cf2976SLionel Sambuc 	}
1455f7cf2976SLionel Sambuc 	while (fgets(line, sizeof(line), f) != NULL)
1456f7cf2976SLionel Sambuc 	{
1457f7cf2976SLionel Sambuc 		for (p = line;  *p != '\0';  p++)
1458f7cf2976SLionel Sambuc 		{
1459f7cf2976SLionel Sambuc 			if (*p == '\n' || *p == '\r')
1460f7cf2976SLionel Sambuc 			{
1461f7cf2976SLionel Sambuc 				*p = '\0';
1462f7cf2976SLionel Sambuc 				break;
1463f7cf2976SLionel Sambuc 			}
1464f7cf2976SLionel Sambuc 		}
1465f7cf2976SLionel Sambuc 		if (strcmp(line, HISTFILE_SEARCH_SECTION) == 0)
1466f7cf2976SLionel Sambuc 			ml = &mlist_search;
1467f7cf2976SLionel Sambuc 		else if (strcmp(line, HISTFILE_SHELL_SECTION) == 0)
1468f7cf2976SLionel Sambuc 		{
1469f7cf2976SLionel Sambuc #if SHELL_ESCAPE || PIPEC
1470f7cf2976SLionel Sambuc 			ml = &mlist_shell;
1471f7cf2976SLionel Sambuc #else
1472f7cf2976SLionel Sambuc 			ml = NULL;
1473f7cf2976SLionel Sambuc #endif
1474f7cf2976SLionel Sambuc 		} else if (*line == '"')
1475f7cf2976SLionel Sambuc 		{
1476f7cf2976SLionel Sambuc 			if (ml != NULL)
1477f7cf2976SLionel Sambuc 				cmd_addhist(ml, line+1);
1478f7cf2976SLionel Sambuc 		}
1479f7cf2976SLionel Sambuc 	}
1480f7cf2976SLionel Sambuc 	fclose(f);
1481f7cf2976SLionel Sambuc #endif /* CMD_HISTORY */
1482f7cf2976SLionel Sambuc }
1483f7cf2976SLionel Sambuc 
1484f7cf2976SLionel Sambuc /*
1485f7cf2976SLionel Sambuc  *
1486f7cf2976SLionel Sambuc  */
1487f7cf2976SLionel Sambuc #if CMD_HISTORY
1488f7cf2976SLionel Sambuc 	static void
save_mlist(ml,f)1489f7cf2976SLionel Sambuc save_mlist(ml, f)
1490f7cf2976SLionel Sambuc 	struct mlist *ml;
1491f7cf2976SLionel Sambuc 	FILE *f;
1492f7cf2976SLionel Sambuc {
1493f7cf2976SLionel Sambuc 	int histsize = 0;
1494f7cf2976SLionel Sambuc 	int n;
1495f7cf2976SLionel Sambuc 	char *s;
1496f7cf2976SLionel Sambuc 
1497f7cf2976SLionel Sambuc 	s = lgetenv("LESSHISTSIZE");
1498f7cf2976SLionel Sambuc 	if (s != NULL)
1499f7cf2976SLionel Sambuc 		histsize = atoi(s);
1500f7cf2976SLionel Sambuc 	if (histsize == 0)
1501f7cf2976SLionel Sambuc 		histsize = 100;
1502f7cf2976SLionel Sambuc 
1503f7cf2976SLionel Sambuc 	ml = ml->prev;
1504f7cf2976SLionel Sambuc 	for (n = 0;  n < histsize;  n++)
1505f7cf2976SLionel Sambuc 	{
1506f7cf2976SLionel Sambuc 		if (ml->string == NULL)
1507f7cf2976SLionel Sambuc 			break;
1508f7cf2976SLionel Sambuc 		ml = ml->prev;
1509f7cf2976SLionel Sambuc 	}
1510f7cf2976SLionel Sambuc 	for (ml = ml->next;  ml->string != NULL;  ml = ml->next)
1511f7cf2976SLionel Sambuc 		fprintf(f, "\"%s\n", ml->string);
1512f7cf2976SLionel Sambuc }
1513f7cf2976SLionel Sambuc #endif /* CMD_HISTORY */
1514f7cf2976SLionel Sambuc 
1515f7cf2976SLionel Sambuc /*
1516f7cf2976SLionel Sambuc  *
1517f7cf2976SLionel Sambuc  */
1518f7cf2976SLionel Sambuc 	public void
save_cmdhist()1519f7cf2976SLionel Sambuc save_cmdhist()
1520f7cf2976SLionel Sambuc {
1521f7cf2976SLionel Sambuc #if CMD_HISTORY
1522f7cf2976SLionel Sambuc 	char *filename;
1523f7cf2976SLionel Sambuc 	FILE *f;
1524f7cf2976SLionel Sambuc 	int modified = 0;
1525f7cf2976SLionel Sambuc 
1526f7cf2976SLionel Sambuc 	if (mlist_search.modified)
1527f7cf2976SLionel Sambuc 		modified = 1;
1528f7cf2976SLionel Sambuc #if SHELL_ESCAPE || PIPEC
1529f7cf2976SLionel Sambuc 	if (mlist_shell.modified)
1530f7cf2976SLionel Sambuc 		modified = 1;
1531f7cf2976SLionel Sambuc #endif
1532f7cf2976SLionel Sambuc 	if (!modified)
1533f7cf2976SLionel Sambuc 		return;
1534*84d9c625SLionel Sambuc 	filename = histfile_name();
1535*84d9c625SLionel Sambuc 	if (filename == NULL)
1536*84d9c625SLionel Sambuc 		return;
1537f7cf2976SLionel Sambuc 	f = fopen(filename, "w");
1538f7cf2976SLionel Sambuc 	free(filename);
1539f7cf2976SLionel Sambuc 	if (f == NULL)
1540f7cf2976SLionel Sambuc 		return;
1541f7cf2976SLionel Sambuc #if HAVE_FCHMOD
1542f7cf2976SLionel Sambuc {
1543f7cf2976SLionel Sambuc 	/* Make history file readable only by owner. */
1544f7cf2976SLionel Sambuc 	int do_chmod = 1;
1545f7cf2976SLionel Sambuc #if HAVE_STAT
1546f7cf2976SLionel Sambuc 	struct stat statbuf;
1547f7cf2976SLionel Sambuc 	int r = fstat(fileno(f), &statbuf);
1548f7cf2976SLionel Sambuc 	if (r < 0 || !S_ISREG(statbuf.st_mode))
1549f7cf2976SLionel Sambuc 		/* Don't chmod if not a regular file. */
1550f7cf2976SLionel Sambuc 		do_chmod = 0;
1551f7cf2976SLionel Sambuc #endif
1552f7cf2976SLionel Sambuc 	if (do_chmod)
1553f7cf2976SLionel Sambuc 		fchmod(fileno(f), 0600);
1554f7cf2976SLionel Sambuc }
1555f7cf2976SLionel Sambuc #endif
1556f7cf2976SLionel Sambuc 
1557f7cf2976SLionel Sambuc 	fprintf(f, "%s\n", HISTFILE_FIRST_LINE);
1558f7cf2976SLionel Sambuc 
1559f7cf2976SLionel Sambuc 	fprintf(f, "%s\n", HISTFILE_SEARCH_SECTION);
1560f7cf2976SLionel Sambuc 	save_mlist(&mlist_search, f);
1561f7cf2976SLionel Sambuc 
1562f7cf2976SLionel Sambuc #if SHELL_ESCAPE || PIPEC
1563f7cf2976SLionel Sambuc 	fprintf(f, "%s\n", HISTFILE_SHELL_SECTION);
1564f7cf2976SLionel Sambuc 	save_mlist(&mlist_shell, f);
1565f7cf2976SLionel Sambuc #endif
1566f7cf2976SLionel Sambuc 
1567f7cf2976SLionel Sambuc 	fclose(f);
1568f7cf2976SLionel Sambuc #endif /* CMD_HISTORY */
1569f7cf2976SLionel Sambuc }
1570