xref: /original-bsd/usr.bin/more/command.c (revision 76f06662)
1273e1e22Sbostic /*
2273e1e22Sbostic  * Copyright (c) 1988 Mark Nudleman
3*76f06662Sbostic  * Copyright (c) 1988, 1993
4*76f06662Sbostic  *	The Regents of the University of California.  All rights reserved.
5273e1e22Sbostic  *
69918a12eSbostic  * %sccs.include.redist.c%
7273e1e22Sbostic  */
8273e1e22Sbostic 
9273e1e22Sbostic #ifndef lint
10*76f06662Sbostic static char sccsid[] = "@(#)command.c	8.1 (Berkeley) 06/06/93";
11273e1e22Sbostic #endif /* not lint */
12273e1e22Sbostic 
13c07fef21Sbostic #include <sys/param.h>
14c07fef21Sbostic #include <stdio.h>
15c07fef21Sbostic #include <ctype.h>
16c07fef21Sbostic #include <less.h>
17b6ddd35dSbostic #include "pathnames.h"
18273e1e22Sbostic 
19273e1e22Sbostic #define	NO_MCA		0
20273e1e22Sbostic #define	MCA_DONE	1
21273e1e22Sbostic #define	MCA_MORE	2
22273e1e22Sbostic 
23c07fef21Sbostic extern int erase_char, kill_char, werase_char;
24273e1e22Sbostic extern int ispipe;
25273e1e22Sbostic extern int sigs;
26273e1e22Sbostic extern int quit_at_eof;
27273e1e22Sbostic extern int hit_eof;
28273e1e22Sbostic extern int sc_width;
29273e1e22Sbostic extern int sc_height;
30273e1e22Sbostic extern int sc_window;
31273e1e22Sbostic extern int curr_ac;
32273e1e22Sbostic extern int ac;
33273e1e22Sbostic extern int quitting;
34273e1e22Sbostic extern int scroll;
35273e1e22Sbostic extern int screen_trashed;	/* The screen has been overwritten */
36273e1e22Sbostic 
37273e1e22Sbostic static char cmdbuf[120];	/* Buffer for holding a multi-char command */
38273e1e22Sbostic static char *cp;		/* Pointer into cmdbuf */
39273e1e22Sbostic static int cmd_col;		/* Current column of the multi-char command */
40c07fef21Sbostic static int longprompt;		/* if stat command instead of prompt */
41273e1e22Sbostic static int mca;			/* The multicharacter command (action) */
42273e1e22Sbostic static int last_mca;		/* The previous mca */
43273e1e22Sbostic static int number;		/* The number typed by the user */
44273e1e22Sbostic static int wsearch;		/* Search for matches (1) or non-matches (0) */
45273e1e22Sbostic 
46bb1d8624Sbostic #define	CMD_RESET	cp = cmdbuf	/* reset command buffer to empty */
47bb1d8624Sbostic #define	CMD_EXEC	lower_left(); flush()
48273e1e22Sbostic 
49bb1d8624Sbostic /* backspace in command buffer. */
50c07fef21Sbostic static
cmd_erase()51273e1e22Sbostic cmd_erase()
52273e1e22Sbostic {
53273e1e22Sbostic 	/*
54bb1d8624Sbostic 	 * backspace past beginning of the string: this usually means
55bb1d8624Sbostic 	 * abort the command.
56273e1e22Sbostic 	 */
57bb1d8624Sbostic 	if (cp == cmdbuf)
58273e1e22Sbostic 		return(1);
59273e1e22Sbostic 
60bb1d8624Sbostic 	/* erase an extra character, for the carat. */
61c07fef21Sbostic 	if (CONTROL_CHAR(*--cp)) {
62273e1e22Sbostic 		backspace();
63c07fef21Sbostic 		--cmd_col;
64273e1e22Sbostic 	}
65bb1d8624Sbostic 
66273e1e22Sbostic 	backspace();
67c07fef21Sbostic 	--cmd_col;
68273e1e22Sbostic 	return(0);
69273e1e22Sbostic }
70273e1e22Sbostic 
71bb1d8624Sbostic /* set up the display to start a new multi-character command. */
start_mca(action,prompt)72273e1e22Sbostic start_mca(action, prompt)
73273e1e22Sbostic 	int action;
74273e1e22Sbostic 	char *prompt;
75273e1e22Sbostic {
76273e1e22Sbostic 	lower_left();
77273e1e22Sbostic 	clear_eol();
78273e1e22Sbostic 	putstr(prompt);
79273e1e22Sbostic 	cmd_col = strlen(prompt);
80273e1e22Sbostic 	mca = action;
81273e1e22Sbostic }
82273e1e22Sbostic 
83273e1e22Sbostic /*
84bb1d8624Sbostic  * process a single character of a multi-character command, such as
85273e1e22Sbostic  * a number, or the pattern of a search command.
86273e1e22Sbostic  */
87c07fef21Sbostic static
cmd_char(c)88273e1e22Sbostic cmd_char(c)
89273e1e22Sbostic 	int c;
90273e1e22Sbostic {
91273e1e22Sbostic 	if (c == erase_char)
92c07fef21Sbostic 		return(cmd_erase());
93c07fef21Sbostic 	/* in this order, in case werase == erase_char */
94c07fef21Sbostic 	if (c == werase_char) {
95c07fef21Sbostic 		if (cp > cmdbuf) {
96c07fef21Sbostic 			while (isspace(cp[-1]) && !cmd_erase());
97c07fef21Sbostic 			while (!isspace(cp[-1]) && !cmd_erase());
98c07fef21Sbostic 			while (isspace(cp[-1]) && !cmd_erase());
99c07fef21Sbostic 		}
100c07fef21Sbostic 		return(cp == cmdbuf);
101c07fef21Sbostic 	}
102c07fef21Sbostic 	if (c == kill_char) {
103c07fef21Sbostic 		while (!cmd_erase());
104273e1e22Sbostic 		return(1);
105c07fef21Sbostic 	}
106273e1e22Sbostic 	/*
107c07fef21Sbostic 	 * No room in the command buffer, or no room on the screen;
108c07fef21Sbostic 	 * {{ Could get fancy here; maybe shift the displayed line
109c07fef21Sbostic 	 * and make room for more chars, like ksh. }}
110273e1e22Sbostic 	 */
111c07fef21Sbostic 	if (cp >= &cmdbuf[sizeof(cmdbuf)-1] || cmd_col >= sc_width-3)
112273e1e22Sbostic 		bell();
113c07fef21Sbostic 	else {
114273e1e22Sbostic 		*cp++ = c;
115c07fef21Sbostic 		if (CONTROL_CHAR(c)) {
116273e1e22Sbostic 			putchr('^');
117273e1e22Sbostic 			cmd_col++;
118c07fef21Sbostic 			c = CARAT_CHAR(c);
119273e1e22Sbostic 		}
120273e1e22Sbostic 		putchr(c);
121273e1e22Sbostic 		cmd_col++;
122273e1e22Sbostic 	}
123273e1e22Sbostic 	return(0);
124273e1e22Sbostic }
125273e1e22Sbostic 
prompt()126273e1e22Sbostic prompt()
127273e1e22Sbostic {
128f5460932Sbostic 	extern int linenums, short_file;
1296a16b438Sbostic 	extern char *current_name, *firstsearch, *next_name;
130c07fef21Sbostic 	off_t len, pos, ch_length(), position(), forw_line();
131c07fef21Sbostic 	char pbuf[40];
132273e1e22Sbostic 
133273e1e22Sbostic 	/*
134c07fef21Sbostic 	 * if nothing is displayed yet, display starting from line 1;
135c07fef21Sbostic 	 * if search string provided, go there instead.
136273e1e22Sbostic 	 */
137c07fef21Sbostic 	if (position(TOP) == NULL_POSITION) {
138c07fef21Sbostic 		if (forw_line((off_t)0) == NULL_POSITION)
139c07fef21Sbostic 			return(0);
140c07fef21Sbostic 		if (!firstsearch || !search(1, firstsearch, 1, 1))
141273e1e22Sbostic 			jump_back(1);
142c07fef21Sbostic 	}
143273e1e22Sbostic 	else if (screen_trashed)
144273e1e22Sbostic 		repaint();
145273e1e22Sbostic 
146c07fef21Sbostic 	/* if no -e flag and we've hit EOF on the last file, quit. */
147f5460932Sbostic 	if ((!quit_at_eof || short_file) && hit_eof && curr_ac + 1 >= ac)
148273e1e22Sbostic 		quit();
149273e1e22Sbostic 
150c07fef21Sbostic 	/* select the proper prompt and display it. */
151273e1e22Sbostic 	lower_left();
152273e1e22Sbostic 	clear_eol();
153c07fef21Sbostic 	if (longprompt) {
154273e1e22Sbostic 		so_enter();
155c07fef21Sbostic 		putstr(current_name);
156c07fef21Sbostic 		putstr(":");
157c07fef21Sbostic 		if (!ispipe) {
158c07fef21Sbostic 			(void)sprintf(pbuf, " file %d/%d", curr_ac + 1, ac);
159c07fef21Sbostic 			putstr(pbuf);
160c07fef21Sbostic 		}
161c07fef21Sbostic 		if (linenums) {
162e8917bdeSbostic 			(void)sprintf(pbuf, " line %d", currline(BOTTOM));
163c07fef21Sbostic 			putstr(pbuf);
164c07fef21Sbostic 		}
165e8917bdeSbostic 		if ((pos = position(BOTTOM)) != NULL_POSITION) {
166089e3d08Sbostic 			(void)sprintf(pbuf, " byte %qd", pos);
167c07fef21Sbostic 			putstr(pbuf);
168c07fef21Sbostic 			if (!ispipe && (len = ch_length())) {
169089e3d08Sbostic 				(void)sprintf(pbuf, "/%qd pct %qd%%",
170e8917bdeSbostic 				    len, ((100 * pos) / len));
171c07fef21Sbostic 				putstr(pbuf);
172c07fef21Sbostic 			}
173c07fef21Sbostic 		}
174c07fef21Sbostic 		so_exit();
175c07fef21Sbostic 		longprompt = 0;
176c07fef21Sbostic 	}
177c07fef21Sbostic 	else {
178c07fef21Sbostic 		so_enter();
179c07fef21Sbostic 		putstr(current_name);
180c07fef21Sbostic 		if (hit_eof)
1816a16b438Sbostic 			if (next_name) {
18230b1ef4eSleres 				putstr(": END (next file: ");
18330b1ef4eSleres 				putstr(next_name);
18430b1ef4eSleres 				putstr(")");
1856a16b438Sbostic 			}
1866a16b438Sbostic 			else
187c07fef21Sbostic 				putstr(": END");
188daa38086Sbostic 		else if (!ispipe &&
189daa38086Sbostic 		    (pos = position(BOTTOM)) != NULL_POSITION &&
190daa38086Sbostic 		    (len = ch_length())) {
191089e3d08Sbostic 			(void)sprintf(pbuf, " (%qd%%)", ((100 * pos) / len));
192c07fef21Sbostic 			putstr(pbuf);
193c07fef21Sbostic 		}
194273e1e22Sbostic 		so_exit();
195273e1e22Sbostic 	}
196c07fef21Sbostic 	return(1);
197273e1e22Sbostic }
198273e1e22Sbostic 
199bb1d8624Sbostic /* get command character. */
200c07fef21Sbostic static
getcc()201273e1e22Sbostic getcc()
202273e1e22Sbostic {
2031837513dSbostic 	extern int cmdstack;
2041837513dSbostic 	int ch;
205caf3a2e4Sralph 	off_t position();
2061837513dSbostic 
2071837513dSbostic 	/* left over from error() routine. */
2081837513dSbostic 	if (cmdstack) {
2091837513dSbostic 		ch = cmdstack;
2101837513dSbostic 		cmdstack = NULL;
2111837513dSbostic 		return(ch);
2121837513dSbostic 	}
213c07fef21Sbostic 	if (cp > cmdbuf && position(TOP) == NULL_POSITION) {
214273e1e22Sbostic 		/*
215273e1e22Sbostic 		 * Command is incomplete, so try to complete it.
216273e1e22Sbostic 		 * There are only two cases:
217273e1e22Sbostic 		 * 1. We have "/string" but no newline.  Add the \n.
218273e1e22Sbostic 		 * 2. We have a number but no command.  Treat as #g.
219273e1e22Sbostic 		 * (This is all pretty hokey.)
220273e1e22Sbostic 		 */
221273e1e22Sbostic 		if (mca != A_DIGIT)
222273e1e22Sbostic 			/* Not a number; must be search string */
223273e1e22Sbostic 			return('\n');
224273e1e22Sbostic 		else
225273e1e22Sbostic 			/* A number; append a 'g' */
226273e1e22Sbostic 			return('g');
227273e1e22Sbostic 	}
228273e1e22Sbostic 	return(getchr());
229273e1e22Sbostic }
230273e1e22Sbostic 
231bb1d8624Sbostic /* execute a multicharacter command. */
232c07fef21Sbostic static
exec_mca()233273e1e22Sbostic exec_mca()
234273e1e22Sbostic {
235c07fef21Sbostic 	extern int file;
236c07fef21Sbostic 	extern char *tagfile;
237273e1e22Sbostic 	register char *p;
238c07fef21Sbostic 	char *glob();
239273e1e22Sbostic 
240273e1e22Sbostic 	*cp = '\0';
241bb1d8624Sbostic 	CMD_EXEC;
242c07fef21Sbostic 	switch (mca) {
243273e1e22Sbostic 	case A_F_SEARCH:
244c07fef21Sbostic 		(void)search(1, cmdbuf, number, wsearch);
245273e1e22Sbostic 		break;
246273e1e22Sbostic 	case A_B_SEARCH:
247c07fef21Sbostic 		(void)search(0, cmdbuf, number, wsearch);
248273e1e22Sbostic 		break;
249273e1e22Sbostic 	case A_EXAMINE:
250c07fef21Sbostic 		for (p = cmdbuf; isspace(*p); ++p);
251c07fef21Sbostic 		(void)edit(glob(p));
252c07fef21Sbostic 		break;
253c07fef21Sbostic 	case A_TAGFILE:
254c07fef21Sbostic 		for (p = cmdbuf; isspace(*p); ++p);
255c07fef21Sbostic 		findtag(p);
256c07fef21Sbostic 		if (tagfile == NULL)
257c07fef21Sbostic 			break;
258c07fef21Sbostic 		if (edit(tagfile))
259c07fef21Sbostic 			(void)tagsearch();
260273e1e22Sbostic 		break;
261273e1e22Sbostic 	}
262273e1e22Sbostic }
263273e1e22Sbostic 
264bb1d8624Sbostic /* add a character to a multi-character command. */
265c07fef21Sbostic static
mca_char(c)266273e1e22Sbostic mca_char(c)
267273e1e22Sbostic 	int c;
268273e1e22Sbostic {
269c07fef21Sbostic 	switch (mca) {
270c07fef21Sbostic 	case 0:			/* not in a multicharacter command. */
271c07fef21Sbostic 	case A_PREFIX:		/* in the prefix of a command. */
272273e1e22Sbostic 		return(NO_MCA);
273273e1e22Sbostic 	case A_DIGIT:
274273e1e22Sbostic 		/*
275273e1e22Sbostic 		 * Entering digits of a number.
276273e1e22Sbostic 		 * Terminated by a non-digit.
277273e1e22Sbostic 		 */
278c07fef21Sbostic 		if (!isascii(c) || !isdigit(c) &&
279c07fef21Sbostic 		    c != erase_char && c != kill_char && c != werase_char) {
280273e1e22Sbostic 			/*
281273e1e22Sbostic 			 * Not part of the number.
282273e1e22Sbostic 			 * Treat as a normal command character.
283273e1e22Sbostic 			 */
284bb1d8624Sbostic 			*cp = '\0';
285bb1d8624Sbostic 			number = atoi(cmdbuf);
286bb1d8624Sbostic 			CMD_RESET;
287273e1e22Sbostic 			mca = 0;
288273e1e22Sbostic 			return(NO_MCA);
289273e1e22Sbostic 		}
290273e1e22Sbostic 		break;
291273e1e22Sbostic 	}
292273e1e22Sbostic 
293273e1e22Sbostic 	/*
294273e1e22Sbostic 	 * Any other multicharacter command
295273e1e22Sbostic 	 * is terminated by a newline.
296273e1e22Sbostic 	 */
297c07fef21Sbostic 	if (c == '\n' || c == '\r') {
298273e1e22Sbostic 		exec_mca();
299273e1e22Sbostic 		return(MCA_DONE);
300273e1e22Sbostic 	}
301bb1d8624Sbostic 
302bb1d8624Sbostic 	/* append the char to the command buffer. */
303273e1e22Sbostic 	if (cmd_char(c))
304273e1e22Sbostic 		return(MCA_DONE);
305bb1d8624Sbostic 
306273e1e22Sbostic 	return(MCA_MORE);
307273e1e22Sbostic }
308273e1e22Sbostic 
309273e1e22Sbostic /*
310273e1e22Sbostic  * Main command processor.
311273e1e22Sbostic  * Accept and execute commands until a quit command, then return.
312273e1e22Sbostic  */
commands()313273e1e22Sbostic commands()
314273e1e22Sbostic {
315273e1e22Sbostic 	register int c;
316273e1e22Sbostic 	register int action;
317273e1e22Sbostic 
318273e1e22Sbostic 	last_mca = 0;
319273e1e22Sbostic 	scroll = (sc_height + 1) / 2;
320273e1e22Sbostic 
321c07fef21Sbostic 	for (;;) {
322273e1e22Sbostic 		mca = 0;
323273e1e22Sbostic 		number = 0;
324273e1e22Sbostic 
325273e1e22Sbostic 		/*
326273e1e22Sbostic 		 * See if any signals need processing.
327273e1e22Sbostic 		 */
328c07fef21Sbostic 		if (sigs) {
329273e1e22Sbostic 			psignals();
330273e1e22Sbostic 			if (quitting)
331273e1e22Sbostic 				quit();
332273e1e22Sbostic 		}
333273e1e22Sbostic 		/*
334273e1e22Sbostic 		 * Display prompt and accept a character.
335273e1e22Sbostic 		 */
336bb1d8624Sbostic 		CMD_RESET;
337c07fef21Sbostic 		if (!prompt()) {
338c07fef21Sbostic 			next_file(1);
339c07fef21Sbostic 			continue;
340c07fef21Sbostic 		}
341273e1e22Sbostic 		noprefix();
342273e1e22Sbostic 		c = getcc();
343273e1e22Sbostic 
344467fd63aSbostic again:		if (sigs)
345273e1e22Sbostic 			continue;
346273e1e22Sbostic 
347273e1e22Sbostic 		/*
348273e1e22Sbostic 		 * If we are in a multicharacter command, call mca_char.
349273e1e22Sbostic 		 * Otherwise we call cmd_decode to determine the
350273e1e22Sbostic 		 * action to be performed.
351273e1e22Sbostic 		 */
352273e1e22Sbostic 		if (mca)
353c07fef21Sbostic 			switch (mca_char(c)) {
354273e1e22Sbostic 			case MCA_MORE:
355273e1e22Sbostic 				/*
356273e1e22Sbostic 				 * Need another character.
357273e1e22Sbostic 				 */
358273e1e22Sbostic 				c = getcc();
359273e1e22Sbostic 				goto again;
360273e1e22Sbostic 			case MCA_DONE:
361273e1e22Sbostic 				/*
362273e1e22Sbostic 				 * Command has been handled by mca_char.
363273e1e22Sbostic 				 * Start clean with a prompt.
364273e1e22Sbostic 				 */
365273e1e22Sbostic 				continue;
366273e1e22Sbostic 			case NO_MCA:
367273e1e22Sbostic 				/*
368273e1e22Sbostic 				 * Not a multi-char command
369273e1e22Sbostic 				 * (at least, not anymore).
370273e1e22Sbostic 				 */
371273e1e22Sbostic 				break;
372273e1e22Sbostic 			}
373273e1e22Sbostic 
374bb1d8624Sbostic 		/* decode the command character and decide what to do. */
375bb1d8624Sbostic 		switch (action = cmd_decode(c)) {
376bb1d8624Sbostic 		case A_DIGIT:		/* first digit of a number */
377273e1e22Sbostic 			start_mca(A_DIGIT, ":");
378273e1e22Sbostic 			goto again;
379bb1d8624Sbostic 		case A_F_SCREEN:	/* forward one screen */
380bb1d8624Sbostic 			CMD_EXEC;
381bb1d8624Sbostic 			if (number <= 0 && (number = sc_window) <= 0)
382273e1e22Sbostic 				number = sc_height - 1;
383273e1e22Sbostic 			forward(number, 1);
384273e1e22Sbostic 			break;
385bb1d8624Sbostic 		case A_B_SCREEN:	/* backward one screen */
386bb1d8624Sbostic 			CMD_EXEC;
387bb1d8624Sbostic 			if (number <= 0 && (number = sc_window) <= 0)
388273e1e22Sbostic 				number = sc_height - 1;
389273e1e22Sbostic 			backward(number, 1);
390273e1e22Sbostic 			break;
391bb1d8624Sbostic 		case A_F_LINE:		/* forward N (default 1) line */
392bb1d8624Sbostic 			CMD_EXEC;
393bb1d8624Sbostic 			forward(number <= 0 ? 1 : number, 0);
394273e1e22Sbostic 			break;
395bb1d8624Sbostic 		case A_B_LINE:		/* backward N (default 1) line */
396bb1d8624Sbostic 			CMD_EXEC;
397bb1d8624Sbostic 			backward(number <= 0 ? 1 : number, 0);
398273e1e22Sbostic 			break;
399bb1d8624Sbostic 		case A_F_SCROLL:	/* forward N lines */
400bb1d8624Sbostic 			CMD_EXEC;
401273e1e22Sbostic 			if (number > 0)
402273e1e22Sbostic 				scroll = number;
403273e1e22Sbostic 			forward(scroll, 0);
404273e1e22Sbostic 			break;
405bb1d8624Sbostic 		case A_B_SCROLL:	/* backward N lines */
406bb1d8624Sbostic 			CMD_EXEC;
407273e1e22Sbostic 			if (number > 0)
408273e1e22Sbostic 				scroll = number;
409273e1e22Sbostic 			backward(scroll, 0);
410273e1e22Sbostic 			break;
411bb1d8624Sbostic 		case A_FREPAINT:	/* flush buffers and repaint */
412bb1d8624Sbostic 			if (!ispipe) {
413273e1e22Sbostic 				ch_init(0, 0);
414273e1e22Sbostic 				clr_linenum();
415273e1e22Sbostic 			}
416bb1d8624Sbostic 			/* FALLTHROUGH */
4173604dd4cSbostic 		case A_REPAINT:		/* repaint the screen */
418bb1d8624Sbostic 			CMD_EXEC;
419273e1e22Sbostic 			repaint();
420273e1e22Sbostic 			break;
421bb1d8624Sbostic 		case A_GOLINE:		/* go to line N, default 1 */
422bb1d8624Sbostic 			CMD_EXEC;
423273e1e22Sbostic 			if (number <= 0)
424273e1e22Sbostic 				number = 1;
425273e1e22Sbostic 			jump_back(number);
426273e1e22Sbostic 			break;
427bb1d8624Sbostic 		case A_PERCENT:		/* go to percent of file */
428bb1d8624Sbostic 			CMD_EXEC;
429273e1e22Sbostic 			if (number < 0)
430273e1e22Sbostic 				number = 0;
431bb1d8624Sbostic 			else if (number > 100)
432273e1e22Sbostic 				number = 100;
433273e1e22Sbostic 			jump_percent(number);
434273e1e22Sbostic 			break;
435bb1d8624Sbostic 		case A_GOEND:		/* go to line N, default end */
436bb1d8624Sbostic 			CMD_EXEC;
437273e1e22Sbostic 			if (number <= 0)
438273e1e22Sbostic 				jump_forw();
439273e1e22Sbostic 			else
440273e1e22Sbostic 				jump_back(number);
441273e1e22Sbostic 			break;
442c07fef21Sbostic 		case A_STAT:		/* print file name, etc. */
443c07fef21Sbostic 			longprompt = 1;
444c07fef21Sbostic 			continue;
445c07fef21Sbostic 		case A_QUIT:		/* exit */
446273e1e22Sbostic 			quit();
447bb1d8624Sbostic 		case A_F_SEARCH:	/* search for a pattern */
448273e1e22Sbostic 		case A_B_SEARCH:
449273e1e22Sbostic 			if (number <= 0)
450273e1e22Sbostic 				number = 1;
451273e1e22Sbostic 			start_mca(action, (action==A_F_SEARCH) ? "/" : "?");
452273e1e22Sbostic 			last_mca = mca;
453273e1e22Sbostic 			wsearch = 1;
454273e1e22Sbostic 			c = getcc();
455c07fef21Sbostic 			if (c == '!') {
456273e1e22Sbostic 				/*
457bb1d8624Sbostic 				 * Invert the sense of the search; set wsearch
458bb1d8624Sbostic 				 * to 0 and get a new character for the start
459bb1d8624Sbostic 				 * of the pattern.
460273e1e22Sbostic 				 */
461273e1e22Sbostic 				start_mca(action,
462273e1e22Sbostic 				    (action == A_F_SEARCH) ? "!/" : "!?");
463273e1e22Sbostic 				wsearch = 0;
464273e1e22Sbostic 				c = getcc();
465273e1e22Sbostic 			}
466273e1e22Sbostic 			goto again;
467bb1d8624Sbostic 		case A_AGAIN_SEARCH:		/* repeat previous search */
468273e1e22Sbostic 			if (number <= 0)
469273e1e22Sbostic 				number = 1;
470273e1e22Sbostic 			if (wsearch)
471273e1e22Sbostic 				start_mca(last_mca,
472273e1e22Sbostic 				    (last_mca == A_F_SEARCH) ? "/" : "?");
473273e1e22Sbostic 			else
474273e1e22Sbostic 				start_mca(last_mca,
475273e1e22Sbostic 				    (last_mca == A_F_SEARCH) ? "!/" : "!?");
476bb1d8624Sbostic 			CMD_EXEC;
477c07fef21Sbostic 			(void)search(mca == A_F_SEARCH, (char *)NULL,
478c07fef21Sbostic 			    number, wsearch);
479273e1e22Sbostic 			break;
480bb1d8624Sbostic 		case A_HELP:			/* help */
481273e1e22Sbostic 			lower_left();
482273e1e22Sbostic 			clear_eol();
483273e1e22Sbostic 			putstr("help");
484bb1d8624Sbostic 			CMD_EXEC;
485273e1e22Sbostic 			help();
486273e1e22Sbostic 			break;
487bb1d8624Sbostic 		case A_TAGFILE:			/* tag a new file */
488bb1d8624Sbostic 			CMD_RESET;
489c07fef21Sbostic 			start_mca(A_TAGFILE, "Tag: ");
490c07fef21Sbostic 			c = getcc();
491c07fef21Sbostic 			goto again;
4923604dd4cSbostic 		case A_FILE_LIST:		/* show list of file names */
493bb1d8624Sbostic 			CMD_EXEC;
4943604dd4cSbostic 			showlist();
4953604dd4cSbostic 			repaint();
4963604dd4cSbostic 			break;
497bb1d8624Sbostic 		case A_EXAMINE:			/* edit a new file */
498bb1d8624Sbostic 			CMD_RESET;
499273e1e22Sbostic 			start_mca(A_EXAMINE, "Examine: ");
500273e1e22Sbostic 			c = getcc();
501273e1e22Sbostic 			goto again;
502bb1d8624Sbostic 		case A_VISUAL:			/* invoke the editor */
503bb1d8624Sbostic 			if (ispipe) {
504273e1e22Sbostic 				error("Cannot edit standard input");
505273e1e22Sbostic 				break;
506273e1e22Sbostic 			}
507bb1d8624Sbostic 			CMD_EXEC;
50866ba487bSbostic 			editfile();
509273e1e22Sbostic 			ch_init(0, 0);
510273e1e22Sbostic 			clr_linenum();
511273e1e22Sbostic 			break;
512bb1d8624Sbostic 		case A_NEXT_FILE:		/* examine next file */
513273e1e22Sbostic 			if (number <= 0)
514273e1e22Sbostic 				number = 1;
515273e1e22Sbostic 			next_file(number);
516273e1e22Sbostic 			break;
517bb1d8624Sbostic 		case A_PREV_FILE:		/* examine previous file */
518273e1e22Sbostic 			if (number <= 0)
519273e1e22Sbostic 				number = 1;
520273e1e22Sbostic 			prev_file(number);
521273e1e22Sbostic 			break;
522bb1d8624Sbostic 		case A_SETMARK:			/* set a mark */
523273e1e22Sbostic 			lower_left();
524273e1e22Sbostic 			clear_eol();
525273e1e22Sbostic 			start_mca(A_SETMARK, "mark: ");
526273e1e22Sbostic 			c = getcc();
527273e1e22Sbostic 			if (c == erase_char || c == kill_char)
528273e1e22Sbostic 				break;
529273e1e22Sbostic 			setmark(c);
530273e1e22Sbostic 			break;
531bb1d8624Sbostic 		case A_GOMARK:			/* go to mark */
532273e1e22Sbostic 			lower_left();
533273e1e22Sbostic 			clear_eol();
534273e1e22Sbostic 			start_mca(A_GOMARK, "goto mark: ");
535273e1e22Sbostic 			c = getcc();
536273e1e22Sbostic 			if (c == erase_char || c == kill_char)
537273e1e22Sbostic 				break;
538273e1e22Sbostic 			gomark(c);
539273e1e22Sbostic 			break;
540273e1e22Sbostic 		case A_PREFIX:
541273e1e22Sbostic 			/*
542273e1e22Sbostic 			 * The command is incomplete (more chars are needed).
543bb1d8624Sbostic 			 * Display the current char so the user knows what's
544bb1d8624Sbostic 			 * going on and get another character.
545273e1e22Sbostic 			 */
546273e1e22Sbostic 			if (mca != A_PREFIX)
547bb1d8624Sbostic 				start_mca(A_PREFIX, "");
548bb1d8624Sbostic 			if (CONTROL_CHAR(c)) {
549273e1e22Sbostic 				putchr('^');
550c07fef21Sbostic 				c = CARAT_CHAR(c);
551273e1e22Sbostic 			}
552273e1e22Sbostic 			putchr(c);
553273e1e22Sbostic 			c = getcc();
554273e1e22Sbostic 			goto again;
555273e1e22Sbostic 		default:
556273e1e22Sbostic 			bell();
557273e1e22Sbostic 			break;
558273e1e22Sbostic 		}
559273e1e22Sbostic 	}
560273e1e22Sbostic }
56166ba487bSbostic 
editfile()56266ba487bSbostic editfile()
56366ba487bSbostic {
564c07fef21Sbostic 	extern char *current_file;
56566ba487bSbostic 	static int dolinenumber;
56666ba487bSbostic 	static char *editor;
56766ba487bSbostic 	int c;
568c07fef21Sbostic 	char buf[MAXPATHLEN * 2 + 20], *getenv();
56966ba487bSbostic 
57066ba487bSbostic 	if (editor == NULL) {
57166ba487bSbostic 		editor = getenv("EDITOR");
57266ba487bSbostic 		/* pass the line number to vi */
57366ba487bSbostic 		if (editor == NULL || *editor == '\0') {
574b6ddd35dSbostic 			editor = _PATH_VI;
57566ba487bSbostic 			dolinenumber = 1;
57666ba487bSbostic 		}
57766ba487bSbostic 		else
57866ba487bSbostic 			dolinenumber = 0;
57966ba487bSbostic 	}
58066ba487bSbostic 	if (dolinenumber && (c = currline(MIDDLE)))
58166ba487bSbostic 		(void)sprintf(buf, "%s +%d %s", editor, c, current_file);
58266ba487bSbostic 	else
58366ba487bSbostic 		(void)sprintf(buf, "%s %s", editor, current_file);
58466ba487bSbostic 	lsystem(buf);
58566ba487bSbostic }
5863604dd4cSbostic 
showlist()5873604dd4cSbostic showlist()
5883604dd4cSbostic {
5893604dd4cSbostic 	extern int sc_width;
5903604dd4cSbostic 	extern char **av;
5913604dd4cSbostic 	register int indx, width;
5923604dd4cSbostic 	int len;
5933604dd4cSbostic 	char *p;
5943604dd4cSbostic 
5953604dd4cSbostic 	if (ac <= 0) {
5963604dd4cSbostic 		error("No files provided as arguments.");
5973604dd4cSbostic 		return;
5983604dd4cSbostic 	}
5993604dd4cSbostic 	for (width = indx = 0; indx < ac;) {
6003604dd4cSbostic 		p = strcmp(av[indx], "-") ? av[indx] : "stdin";
6013604dd4cSbostic 		len = strlen(p) + 1;
6023604dd4cSbostic 		if (curr_ac == indx)
6033604dd4cSbostic 			len += 2;
6043604dd4cSbostic 		if (width + len + 1 >= sc_width) {
6053604dd4cSbostic 			if (!width) {
6063604dd4cSbostic 				if (curr_ac == indx)
6073604dd4cSbostic 					putchr('[');
6083604dd4cSbostic 				putstr(p);
6093604dd4cSbostic 				if (curr_ac == indx)
6103604dd4cSbostic 					putchr(']');
6113604dd4cSbostic 				++indx;
6123604dd4cSbostic 			}
6133604dd4cSbostic 			width = 0;
6143604dd4cSbostic 			putchr('\n');
6153604dd4cSbostic 			continue;
6163604dd4cSbostic 		}
6173604dd4cSbostic 		if (width)
6183604dd4cSbostic 			putchr(' ');
6193604dd4cSbostic 		if (curr_ac == indx)
6203604dd4cSbostic 			putchr('[');
6213604dd4cSbostic 		putstr(p);
6223604dd4cSbostic 		if (curr_ac == indx)
6233604dd4cSbostic 			putchr(']');
6243604dd4cSbostic 		width += len;
6253604dd4cSbostic 		++indx;
6263604dd4cSbostic 	}
6273604dd4cSbostic 	putchr('\n');
6283604dd4cSbostic 	error((char *)NULL);
6293604dd4cSbostic }
630