xref: /openbsd/usr.bin/less/command.c (revision 9b7c3dbb)
1 /*
2  * Copyright (C) 1984-2012  Mark Nudelman
3  * Modified for use with illumos by Garrett D'Amore.
4  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
5  *
6  * You may distribute under the terms of either the GNU General Public
7  * License or the Less License, as specified in the README file.
8  *
9  * For more information, see the README file.
10  */
11 
12 /*
13  * User-level command processor.
14  */
15 
16 #include "cmd.h"
17 #include "less.h"
18 #include "option.h"
19 #include "position.h"
20 
21 extern int erase_char, erase2_char, kill_char;
22 extern volatile sig_atomic_t sigs;
23 extern int quit_if_one_screen;
24 extern int less_is_more;
25 extern int squished;
26 extern int sc_width;
27 extern int sc_height;
28 extern int swindow;
29 extern int jump_sline;
30 extern int quitting;
31 extern int wscroll;
32 extern int top_scroll;
33 extern int ignore_eoi;
34 extern int secure;
35 extern int hshift;
36 extern int show_attn;
37 extern off_t highest_hilite;
38 extern char *every_first_cmd;
39 extern char *curr_altfilename;
40 extern char version[];
41 extern struct scrpos initial_scrpos;
42 extern IFILE curr_ifile;
43 extern void *ml_search;
44 extern void *ml_examine;
45 extern void *ml_shell;
46 extern char *editor;
47 extern char *editproto;
48 extern int screen_trashed;	/* The screen has been overwritten */
49 extern int shift_count;
50 extern int oldbot;
51 extern int forw_prompt;
52 
53 static int mca;			/* The multicharacter command (action) */
54 static int search_type;		/* The previous type of search */
55 static off_t number;		/* The number typed by the user */
56 static long fraction;		/* The fractional part of the number */
57 static struct loption *curropt;
58 static int opt_lower;
59 static int optflag;
60 static int optgetname;
61 static off_t bottompos;
62 static int save_hshift;
63 static int pipec;
64 
65 struct ungot {
66 	struct ungot *ug_next;
67 	int ug_char;
68 };
69 static struct ungot *ungot = NULL;
70 static int unget_end = 0;
71 
72 static void multi_search(char *, int);
73 
74 /*
75  * Move the cursor to start of prompt line before executing a command.
76  * This looks nicer if the command takes a long time before
77  * updating the screen.
78  */
79 static void
80 cmd_exec(void)
81 {
82 	clear_attn();
83 	clear_bot();
84 	flush(0);
85 }
86 
87 /*
88  * Set up the display to start a new multi-character command.
89  */
90 static void
91 start_mca(int action, const char *prompt, void *mlist, int cmdflags)
92 {
93 	mca = action;
94 	clear_bot();
95 	clear_cmd();
96 	cmd_putstr((char *)prompt);
97 	set_mlist(mlist, cmdflags);
98 }
99 
100 int
101 in_mca(void)
102 {
103 	return (mca != 0 && mca != A_PREFIX);
104 }
105 
106 /*
107  * Set up the display to start a new search command.
108  */
109 static void
110 mca_search(void)
111 {
112 	if (search_type & SRCH_FILTER)
113 		mca = A_FILTER;
114 	else if (search_type & SRCH_FORW)
115 		mca = A_F_SEARCH;
116 	else
117 		mca = A_B_SEARCH;
118 
119 	clear_bot();
120 	clear_cmd();
121 
122 	if (search_type & SRCH_NO_MATCH)
123 		cmd_putstr("Non-match ");
124 	if (search_type & SRCH_FIRST_FILE)
125 		cmd_putstr("First-file ");
126 	if (search_type & SRCH_PAST_EOF)
127 		cmd_putstr("EOF-ignore ");
128 	if (search_type & SRCH_NO_MOVE)
129 		cmd_putstr("Keep-pos ");
130 	if (search_type & SRCH_NO_REGEX)
131 		cmd_putstr("Regex-off ");
132 
133 	if (search_type & SRCH_FILTER)
134 		cmd_putstr("&/");
135 	else if (search_type & SRCH_FORW)
136 		cmd_putstr("/");
137 	else
138 		cmd_putstr("?");
139 	set_mlist(ml_search, 0);
140 }
141 
142 /*
143  * Set up the display to start a new toggle-option command.
144  */
145 static void
146 mca_opt_toggle(void)
147 {
148 	int no_prompt;
149 	int flag;
150 	char *dash;
151 
152 	no_prompt = (optflag & OPT_NO_PROMPT);
153 	flag = (optflag & ~OPT_NO_PROMPT);
154 	dash = (flag == OPT_NO_TOGGLE) ? "_" : "-";
155 
156 	mca = A_OPT_TOGGLE;
157 	clear_bot();
158 	clear_cmd();
159 	cmd_putstr(dash);
160 	if (optgetname)
161 		cmd_putstr(dash);
162 	if (no_prompt)
163 		cmd_putstr("(P)");
164 	switch (flag) {
165 	case OPT_UNSET:
166 		cmd_putstr("+");
167 		break;
168 	case OPT_SET:
169 		cmd_putstr("!");
170 		break;
171 	}
172 	set_mlist(NULL, 0);
173 }
174 
175 /*
176  * Execute a multicharacter command.
177  */
178 static void
179 exec_mca(void)
180 {
181 	char *cbuf;
182 
183 	cmd_exec();
184 	cbuf = get_cmdbuf();
185 
186 	switch (mca) {
187 	case A_F_SEARCH:
188 	case A_B_SEARCH:
189 		multi_search(cbuf, (int)number);
190 		break;
191 	case A_FILTER:
192 		search_type ^= SRCH_NO_MATCH;
193 		set_filter_pattern(cbuf, search_type);
194 		break;
195 	case A_FIRSTCMD:
196 		/*
197 		 * Skip leading spaces or + signs in the string.
198 		 */
199 		while (*cbuf == '+' || *cbuf == ' ')
200 			cbuf++;
201 		free(every_first_cmd);
202 		if (*cbuf == '\0')
203 			every_first_cmd = NULL;
204 		else
205 			every_first_cmd = estrdup(cbuf);
206 		break;
207 	case A_OPT_TOGGLE:
208 		toggle_option(curropt, opt_lower, cbuf, optflag);
209 		curropt = NULL;
210 		break;
211 	case A_F_BRACKET:
212 		match_brac(cbuf[0], cbuf[1], 1, (int)number);
213 		break;
214 	case A_B_BRACKET:
215 		match_brac(cbuf[1], cbuf[0], 0, (int)number);
216 		break;
217 	case A_EXAMINE:
218 		if (secure)
219 			break;
220 
221 		/* POSIX behavior, but possibly generally useful */
222 		if (strlen(cbuf) == 0) {
223 			reopen_curr_ifile();
224 			jump_back(1);
225 			break;
226 		}
227 		/* POSIX behavior - probably not generally useful */
228 		if (less_is_more && (strcmp(cbuf, "#") == 0)) {
229 			if (ntags()) {
230 				error("No previous file", NULL);
231 				break;
232 			}
233 			if (edit_prev(1)) {
234 				error("No previous file", NULL);
235 			} else {
236 				jump_back(1);
237 			}
238 			break;
239 		}
240 		edit_list(cbuf);
241 		/* If tag structure is loaded then clean it up. */
242 		cleantags();
243 		break;
244 	case A_PIPE:
245 		if (secure)
246 			break;
247 		(void) pipe_mark(pipec, cbuf);
248 		error("|done", NULL);
249 		break;
250 	}
251 }
252 
253 /*
254  * Is a character an erase or kill char?
255  */
256 static int
257 is_erase_char(int c)
258 {
259 	return (c == erase_char || c == erase2_char || c == kill_char);
260 }
261 
262 /*
263  * Handle the first char of an option (after the initial dash).
264  */
265 static int
266 mca_opt_first_char(int c)
267 {
268 	int flag = (optflag & ~OPT_NO_PROMPT);
269 	if (flag == OPT_NO_TOGGLE) {
270 		switch (c) {
271 		case '_':
272 			/* "__" = long option name. */
273 			optgetname = TRUE;
274 			mca_opt_toggle();
275 			return (MCA_MORE);
276 		}
277 	} else {
278 		switch (c) {
279 		case '+':
280 			/* "-+" = UNSET. */
281 			optflag = (flag == OPT_UNSET) ? OPT_TOGGLE : OPT_UNSET;
282 			mca_opt_toggle();
283 			return (MCA_MORE);
284 		case '!':
285 			/* "-!" = SET */
286 			optflag = (flag == OPT_SET) ? OPT_TOGGLE : OPT_SET;
287 			mca_opt_toggle();
288 			return (MCA_MORE);
289 		case CONTROL('P'):
290 			optflag ^= OPT_NO_PROMPT;
291 			mca_opt_toggle();
292 			return (MCA_MORE);
293 		case '-':
294 			/* "--" = long option name. */
295 			optgetname = TRUE;
296 			mca_opt_toggle();
297 			return (MCA_MORE);
298 		}
299 	}
300 	/* Char was not handled here. */
301 	return (NO_MCA);
302 }
303 
304 /*
305  * Add a char to a long option name.
306  * See if we've got a match for an option name yet.
307  * If so, display the complete name and stop
308  * accepting chars until user hits RETURN.
309  */
310 static int
311 mca_opt_nonfirst_char(int c)
312 {
313 	char *p;
314 	char *oname;
315 
316 	if (curropt != NULL) {
317 		/*
318 		 * Already have a match for the name.
319 		 * Don't accept anything but erase/kill.
320 		 */
321 		if (is_erase_char(c))
322 			return (MCA_DONE);
323 		return (MCA_MORE);
324 	}
325 	/*
326 	 * Add char to cmd buffer and try to match
327 	 * the option name.
328 	 */
329 	if (cmd_char(c) == CC_QUIT)
330 		return (MCA_DONE);
331 	p = get_cmdbuf();
332 	opt_lower = islower(p[0]);
333 	curropt = findopt_name(&p, &oname, NULL);
334 	if (curropt != NULL) {
335 		/*
336 		 * Got a match.
337 		 * Remember the option and
338 		 * display the full option name.
339 		 */
340 		cmd_reset();
341 		mca_opt_toggle();
342 		for (p = oname;  *p != '\0';  p++) {
343 			c = *p;
344 			if (!opt_lower && islower(c))
345 				c = toupper(c);
346 			if (cmd_char(c) != CC_OK)
347 				return (MCA_DONE);
348 		}
349 	}
350 	return (MCA_MORE);
351 }
352 
353 /*
354  * Handle a char of an option toggle command.
355  */
356 static int
357 mca_opt_char(int c)
358 {
359 	PARG parg;
360 
361 	/*
362 	 * This may be a short option (single char),
363 	 * or one char of a long option name,
364 	 * or one char of the option parameter.
365 	 */
366 	if (curropt == NULL && len_cmdbuf() == 0) {
367 		int ret = mca_opt_first_char(c);
368 		if (ret != NO_MCA)
369 			return (ret);
370 	}
371 	if (optgetname) {
372 		/* We're getting a long option name.  */
373 		if (c != '\n' && c != '\r')
374 			return (mca_opt_nonfirst_char(c));
375 		if (curropt == NULL) {
376 			parg.p_string = get_cmdbuf();
377 			error("There is no --%s option", &parg);
378 			return (MCA_DONE);
379 		}
380 		optgetname = FALSE;
381 		cmd_reset();
382 	} else {
383 		if (is_erase_char(c))
384 			return (NO_MCA);
385 		if (curropt != NULL)
386 			/* We're getting the option parameter. */
387 			return (NO_MCA);
388 		curropt = findopt(c);
389 		if (curropt == NULL) {
390 			parg.p_string = propt(c);
391 			error("There is no %s option", &parg);
392 			return (MCA_DONE);
393 		}
394 	}
395 	/*
396 	 * If the option which was entered does not take a
397 	 * parameter, toggle the option immediately,
398 	 * so user doesn't have to hit RETURN.
399 	 */
400 	if ((optflag & ~OPT_NO_PROMPT) != OPT_TOGGLE ||
401 	    !opt_has_param(curropt)) {
402 		toggle_option(curropt, islower(c), "", optflag);
403 		return (MCA_DONE);
404 	}
405 	/*
406 	 * Display a prompt appropriate for the option parameter.
407 	 */
408 	start_mca(A_OPT_TOGGLE, opt_prompt(curropt), NULL, 0);
409 	return (MCA_MORE);
410 }
411 
412 /*
413  * Handle a char of a search command.
414  */
415 static int
416 mca_search_char(int c)
417 {
418 	int flag = 0;
419 
420 	/*
421 	 * Certain characters as the first char of
422 	 * the pattern have special meaning:
423 	 *	!  Toggle the NO_MATCH flag
424 	 *	*  Toggle the PAST_EOF flag
425 	 *	@  Toggle the FIRST_FILE flag
426 	 */
427 	if (len_cmdbuf() > 0)
428 		return (NO_MCA);
429 
430 	switch (c) {
431 	case CONTROL('E'): /* ignore END of file */
432 	case '*':
433 		if (mca != A_FILTER)
434 			flag = SRCH_PAST_EOF;
435 		break;
436 	case CONTROL('F'): /* FIRST file */
437 	case '@':
438 		if (mca != A_FILTER)
439 			flag = SRCH_FIRST_FILE;
440 		break;
441 	case CONTROL('K'): /* KEEP position */
442 		if (mca != A_FILTER)
443 			flag = SRCH_NO_MOVE;
444 		break;
445 	case CONTROL('R'): /* Don't use REGULAR EXPRESSIONS */
446 		flag = SRCH_NO_REGEX;
447 		break;
448 	case CONTROL('N'): /* NOT match */
449 	case '!':
450 		flag = SRCH_NO_MATCH;
451 		break;
452 	}
453 
454 	if (flag != 0) {
455 		search_type ^= flag;
456 		mca_search();
457 		return (MCA_MORE);
458 	}
459 	return (NO_MCA);
460 }
461 
462 /*
463  * Handle a character of a multi-character command.
464  */
465 static int
466 mca_char(int c)
467 {
468 	int ret;
469 
470 	switch (mca) {
471 	case 0:
472 		/*
473 		 * We're not in a multicharacter command.
474 		 */
475 		return (NO_MCA);
476 
477 	case A_PREFIX:
478 		/*
479 		 * In the prefix of a command.
480 		 * This not considered a multichar command
481 		 * (even tho it uses cmdbuf, etc.).
482 		 * It is handled in the commands() switch.
483 		 */
484 		return (NO_MCA);
485 
486 	case A_DIGIT:
487 		/*
488 		 * Entering digits of a number.
489 		 * Terminated by a non-digit.
490 		 */
491 		if (!((c >= '0' && c <= '9') || c == '.') && editchar(c,
492 		    EC_PEEK|EC_NOHISTORY|EC_NOCOMPLETE|EC_NORIGHTLEFT) ==
493 		    A_INVALID) {
494 			/*
495 			 * Not part of the number.
496 			 * End the number and treat this char
497 			 * as a normal command character.
498 			 */
499 			number = cmd_int(&fraction);
500 			mca = 0;
501 			cmd_accept();
502 			return (NO_MCA);
503 		}
504 		break;
505 
506 	case A_OPT_TOGGLE:
507 		ret = mca_opt_char(c);
508 		if (ret != NO_MCA)
509 			return (ret);
510 		break;
511 
512 	case A_F_SEARCH:
513 	case A_B_SEARCH:
514 	case A_FILTER:
515 		ret = mca_search_char(c);
516 		if (ret != NO_MCA)
517 			return (ret);
518 		break;
519 
520 	default:
521 		/* Other multicharacter command. */
522 		break;
523 	}
524 
525 	/*
526 	 * The multichar command is terminated by a newline.
527 	 */
528 	if (c == '\n' || c == '\r') {
529 		/*
530 		 * Execute the command.
531 		 */
532 		exec_mca();
533 		return (MCA_DONE);
534 	}
535 
536 	/*
537 	 * Append the char to the command buffer.
538 	 */
539 	if (cmd_char(c) == CC_QUIT)
540 		/*
541 		 * Abort the multi-char command.
542 		 */
543 		return (MCA_DONE);
544 
545 	if ((mca == A_F_BRACKET || mca == A_B_BRACKET) && len_cmdbuf() >= 2) {
546 		/*
547 		 * Special case for the bracket-matching commands.
548 		 * Execute the command after getting exactly two
549 		 * characters from the user.
550 		 */
551 		exec_mca();
552 		return (MCA_DONE);
553 	}
554 
555 	/*
556 	 * Need another character.
557 	 */
558 	return (MCA_MORE);
559 }
560 
561 /*
562  * Discard any buffered file data.
563  */
564 static void
565 clear_buffers(void)
566 {
567 	if (!(ch_getflags() & CH_CANSEEK))
568 		return;
569 	ch_flush();
570 	clr_linenum();
571 	clr_hilite();
572 }
573 
574 /*
575  * Make sure the screen is displayed.
576  */
577 static void
578 make_display(void)
579 {
580 	/*
581 	 * If nothing is displayed yet, display starting from initial_scrpos.
582 	 */
583 	if (empty_screen()) {
584 		if (initial_scrpos.pos == -1)
585 			/*
586 			 * {{ Maybe this should be:
587 			 *    jump_loc(ch_zero(), jump_sline);
588 			 *    but this behavior seems rather unexpected
589 			 *    on the first screen. }}
590 			 */
591 			jump_loc(ch_zero(), 1);
592 		else
593 			jump_loc(initial_scrpos.pos, initial_scrpos.ln);
594 	} else if (screen_trashed) {
595 		int save_top_scroll = top_scroll;
596 		int save_ignore_eoi = ignore_eoi;
597 		top_scroll = 1;
598 		ignore_eoi = 0;
599 		if (screen_trashed == 2) {
600 			/*
601 			 * Special case used by ignore_eoi: re-open the input
602 			 * file and jump to the end of the file.
603 			 */
604 			reopen_curr_ifile();
605 			jump_forw();
606 		}
607 		repaint();
608 		top_scroll = save_top_scroll;
609 		ignore_eoi = save_ignore_eoi;
610 	}
611 }
612 
613 /*
614  * Display the appropriate prompt.
615  */
616 static void
617 prompt(void)
618 {
619 	const char *p;
620 
621 	if (ungot != NULL) {
622 		/*
623 		 * No prompt necessary if commands are from
624 		 * ungotten chars rather than from the user.
625 		 */
626 		return;
627 	}
628 
629 	/*
630 	 * Make sure the screen is displayed.
631 	 */
632 	make_display();
633 	bottompos = position(BOTTOM_PLUS_ONE);
634 
635 	/*
636 	 * If we've hit EOF on the last file and the -E flag is set, quit.
637 	 */
638 	if (get_quit_at_eof() == OPT_ONPLUS &&
639 	    eof_displayed() && !(ch_getflags() & CH_HELPFILE) &&
640 	    next_ifile(curr_ifile) == NULL)
641 		quit(QUIT_OK);
642 
643 	/*
644 	 * If the entire file is displayed and the -F flag is set, quit.
645 	 */
646 	if (quit_if_one_screen &&
647 	    entire_file_displayed() && !(ch_getflags() & CH_HELPFILE) &&
648 	    next_ifile(curr_ifile) == NULL)
649 		quit(QUIT_OK);
650 
651 	/*
652 	 * Select the proper prompt and display it.
653 	 */
654 	/*
655 	 * If the previous action was a forward movement,
656 	 * don't clear the bottom line of the display;
657 	 * just print the prompt since the forward movement guarantees
658 	 * that we're in the right position to display the prompt.
659 	 * Clearing the line could cause a problem: for example, if the last
660 	 * line displayed ended at the right screen edge without a newline,
661 	 * then clearing would clear the last displayed line rather than
662 	 * the prompt line.
663 	 */
664 	if (!forw_prompt)
665 		clear_bot();
666 	clear_cmd();
667 	forw_prompt = 0;
668 	p = prompt_string();
669 	if (is_filtering())
670 		putstr("& ");
671 	if (p == NULL || *p == '\0') {
672 		putchr(':');
673 	} else {
674 		at_enter(AT_STANDOUT);
675 		putstr(p);
676 		at_exit();
677 	}
678 	clear_eol();
679 }
680 
681 /*
682  * Display the less version message.
683  */
684 void
685 dispversion(void)
686 {
687 	PARG parg;
688 
689 	parg.p_string = version;
690 	error("less %s", &parg);
691 }
692 
693 /*
694  * Get command character.
695  * The character normally comes from the keyboard,
696  * but may come from ungotten characters
697  * (characters previously given to ungetcc or ungetsc).
698  */
699 int
700 getcc(void)
701 {
702 	if (unget_end) {
703 		/*
704 		 * We have just run out of ungotten chars.
705 		 */
706 		unget_end = 0;
707 		if (len_cmdbuf() == 0 || !empty_screen())
708 			return (getchr());
709 		/*
710 		 * Command is incomplete, so try to complete it.
711 		 */
712 		switch (mca) {
713 		case A_DIGIT:
714 			/*
715 			 * We have a number but no command.  Treat as #g.
716 			 */
717 			return ('g');
718 
719 		case A_F_SEARCH:
720 		case A_B_SEARCH:
721 			/*
722 			 * We have "/string" but no newline.  Add the \n.
723 			 */
724 			return ('\n');
725 
726 		default:
727 			/*
728 			 * Some other incomplete command.  Let user complete it.
729 			 */
730 			return (getchr());
731 		}
732 	}
733 
734 	if (ungot == NULL) {
735 		/*
736 		 * Normal case: no ungotten chars, so get one from the user.
737 		 */
738 		return (getchr());
739 	}
740 
741 	/*
742 	 * Return the next ungotten char.
743 	 */
744 	{
745 		struct ungot *ug = ungot;
746 		int c = ug->ug_char;
747 		ungot = ug->ug_next;
748 		free(ug);
749 		unget_end = (ungot == NULL);
750 		return (c);
751 	}
752 }
753 
754 /*
755  * "Unget" a command character.
756  * The next getcc() will return this character.
757  */
758 void
759 ungetcc(int c)
760 {
761 	struct ungot *ug = ecalloc(1, sizeof (struct ungot));
762 
763 	ug->ug_char = c;
764 	ug->ug_next = ungot;
765 	ungot = ug;
766 	unget_end = 0;
767 }
768 
769 /*
770  * Unget a whole string of command characters.
771  * The next sequence of getcc()'s will return this string.
772  */
773 void
774 ungetsc(char *s)
775 {
776 	char *p;
777 
778 	for (p = s + strlen(s) - 1;  p >= s;  p--)
779 		ungetcc(*p);
780 }
781 
782 /*
783  * Search for a pattern, possibly in multiple files.
784  * If SRCH_FIRST_FILE is set, begin searching at the first file.
785  * If SRCH_PAST_EOF is set, continue the search thru multiple files.
786  */
787 static void
788 multi_search(char *pattern, int n)
789 {
790 	int nomore;
791 	IFILE save_ifile;
792 	int changed_file;
793 
794 	changed_file = 0;
795 	save_ifile = save_curr_ifile();
796 
797 	if (search_type & SRCH_FIRST_FILE) {
798 		/*
799 		 * Start at the first (or last) file
800 		 * in the command line list.
801 		 */
802 		if (search_type & SRCH_FORW)
803 			nomore = edit_first();
804 		else
805 			nomore = edit_last();
806 		if (nomore) {
807 			unsave_ifile(save_ifile);
808 			return;
809 		}
810 		changed_file = 1;
811 		search_type &= ~SRCH_FIRST_FILE;
812 	}
813 
814 	for (;;) {
815 		n = search(search_type, pattern, n);
816 		/*
817 		 * The SRCH_NO_MOVE flag doesn't "stick": it gets cleared
818 		 * after being used once.  This allows "n" to work after
819 		 * using a /@@ search.
820 		 */
821 		search_type &= ~SRCH_NO_MOVE;
822 		if (n == 0) {
823 			/*
824 			 * Found it.
825 			 */
826 			unsave_ifile(save_ifile);
827 			return;
828 		}
829 
830 		if (n < 0)
831 			/*
832 			 * Some kind of error in the search.
833 			 * Error message has been printed by search().
834 			 */
835 			break;
836 
837 		if ((search_type & SRCH_PAST_EOF) == 0)
838 			/*
839 			 * We didn't find a match, but we're
840 			 * supposed to search only one file.
841 			 */
842 			break;
843 		/*
844 		 * Move on to the next file.
845 		 */
846 		if (search_type & SRCH_FORW)
847 			nomore = edit_next(1);
848 		else
849 			nomore = edit_prev(1);
850 		if (nomore)
851 			break;
852 		changed_file = 1;
853 	}
854 
855 	/*
856 	 * Didn't find it.
857 	 * Print an error message if we haven't already.
858 	 */
859 	if (n > 0)
860 		error("Pattern not found", NULL);
861 
862 	if (changed_file) {
863 		/*
864 		 * Restore the file we were originally viewing.
865 		 */
866 		reedit_ifile(save_ifile);
867 	} else {
868 		unsave_ifile(save_ifile);
869 	}
870 }
871 
872 /*
873  * Forward forever, or until a highlighted line appears.
874  */
875 static int
876 forw_loop(int until_hilite)
877 {
878 	off_t curr_len;
879 
880 	if (ch_getflags() & CH_HELPFILE)
881 		return (A_NOACTION);
882 
883 	cmd_exec();
884 	jump_forw();
885 	curr_len = ch_length();
886 	highest_hilite = until_hilite ? curr_len : -1;
887 	ignore_eoi = 1;
888 	while (!sigs) {
889 		if (until_hilite && highest_hilite > curr_len) {
890 			ring_bell();
891 			break;
892 		}
893 		make_display();
894 		forward(1, 0, 0);
895 	}
896 	ignore_eoi = 0;
897 	ch_set_eof();
898 
899 	/*
900 	 * This gets us back in "F mode" after processing
901 	 * a non-abort signal (e.g. window-change).
902 	 */
903 	if (sigs && !ABORT_SIGS())
904 		return (until_hilite ? A_F_UNTIL_HILITE : A_F_FOREVER);
905 
906 	return (A_NOACTION);
907 }
908 
909 /*
910  * Main command processor.
911  * Accept and execute commands until a quit command.
912  */
913 void
914 commands(void)
915 {
916 	int c = 0;
917 	int action;
918 	char *cbuf;
919 	int newaction;
920 	int save_search_type;
921 	char *extra;
922 	char tbuf[2];
923 	PARG parg;
924 	IFILE old_ifile;
925 	IFILE new_ifile;
926 	char *tagfile;
927 
928 	search_type = SRCH_FORW;
929 	wscroll = (sc_height + 1) / 2;
930 	newaction = A_NOACTION;
931 
932 	for (;;) {
933 		mca = 0;
934 		cmd_accept();
935 		number = 0;
936 		curropt = NULL;
937 
938 		/*
939 		 * See if any signals need processing.
940 		 */
941 		if (sigs) {
942 			psignals();
943 			if (quitting)
944 				quit(QUIT_SAVED_STATUS);
945 		}
946 
947 		/*
948 		 * Display prompt and accept a character.
949 		 */
950 		cmd_reset();
951 		prompt();
952 		if (sigs)
953 			continue;
954 		if (newaction == A_NOACTION)
955 			c = getcc();
956 
957 again:
958 		if (sigs)
959 			continue;
960 
961 		if (newaction != A_NOACTION) {
962 			action = newaction;
963 			newaction = A_NOACTION;
964 		} else {
965 			/*
966 			 * If we are in a multicharacter command, call mca_char.
967 			 * Otherwise we call fcmd_decode to determine the
968 			 * action to be performed.
969 			 */
970 			if (mca)
971 				switch (mca_char(c)) {
972 				case MCA_MORE:
973 					/*
974 					 * Need another character.
975 					 */
976 					c = getcc();
977 					goto again;
978 				case MCA_DONE:
979 					/*
980 					 * Command has been handled by mca_char.
981 					 * Start clean with a prompt.
982 					 */
983 					continue;
984 				case NO_MCA:
985 					/*
986 					 * Not a multi-char command
987 					 * (at least, not anymore).
988 					 */
989 					break;
990 				}
991 
992 			/*
993 			 * Decode the command character and decide what to do.
994 			 */
995 			if (mca) {
996 				/*
997 				 * We're in a multichar command.
998 				 * Add the character to the command buffer
999 				 * and display it on the screen.
1000 				 * If the user backspaces past the start
1001 				 * of the line, abort the command.
1002 				 */
1003 				if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0)
1004 					continue;
1005 				cbuf = get_cmdbuf();
1006 			} else {
1007 				/*
1008 				 * Don't use cmd_char if we're starting fresh
1009 				 * at the beginning of a command, because we
1010 				 * don't want to echo the command until we know
1011 				 * it is a multichar command.  We also don't
1012 				 * want erase_char/kill_char to be treated
1013 				 * as line editing characters.
1014 				 */
1015 				tbuf[0] = (char)c;
1016 				tbuf[1] = '\0';
1017 				cbuf = tbuf;
1018 			}
1019 			extra = NULL;
1020 			action = fcmd_decode(cbuf, &extra);
1021 			/*
1022 			 * If an "extra" string was returned,
1023 			 * process it as a string of command characters.
1024 			 */
1025 			if (extra != NULL)
1026 				ungetsc(extra);
1027 		}
1028 		/*
1029 		 * Clear the cmdbuf string.
1030 		 * (But not if we're in the prefix of a command,
1031 		 * because the partial command string is kept there.)
1032 		 */
1033 		if (action != A_PREFIX)
1034 			cmd_reset();
1035 
1036 		switch (action) {
1037 		case A_DIGIT:
1038 			/*
1039 			 * First digit of a number.
1040 			 */
1041 			start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE);
1042 			goto again;
1043 
1044 		case A_F_WINDOW:
1045 			/*
1046 			 * Forward one window (and set the window size).
1047 			 */
1048 			if (number > 0)
1049 				swindow = (int)number;
1050 			/* FALLTHRU */
1051 		case A_F_SCREEN:
1052 			/*
1053 			 * Forward one screen.
1054 			 */
1055 			if (number <= 0)
1056 				number = get_swindow();
1057 			cmd_exec();
1058 			if (show_attn)
1059 				set_attnpos(bottompos);
1060 			forward((int)number, 0, 1);
1061 			break;
1062 
1063 		case A_B_WINDOW:
1064 			/*
1065 			 * Backward one window (and set the window size).
1066 			 */
1067 			if (number > 0)
1068 				swindow = (int)number;
1069 			/* FALLTHRU */
1070 		case A_B_SCREEN:
1071 			/*
1072 			 * Backward one screen.
1073 			 */
1074 			if (number <= 0)
1075 				number = get_swindow();
1076 			cmd_exec();
1077 			backward((int)number, 0, 1);
1078 			break;
1079 
1080 		case A_F_LINE:
1081 			/*
1082 			 * Forward N (default 1) line.
1083 			 */
1084 			if (number <= 0)
1085 				number = 1;
1086 			cmd_exec();
1087 			if (show_attn == OPT_ONPLUS && number > 1)
1088 				set_attnpos(bottompos);
1089 			forward((int)number, 0, 0);
1090 			break;
1091 
1092 		case A_B_LINE:
1093 			/*
1094 			 * Backward N (default 1) line.
1095 			 */
1096 			if (number <= 0)
1097 				number = 1;
1098 			cmd_exec();
1099 			backward((int)number, 0, 0);
1100 			break;
1101 
1102 		case A_F_SKIP:
1103 			/*
1104 			 * Skip ahead one screen, and then number lines.
1105 			 */
1106 			if (number <= 0) {
1107 				number = get_swindow();
1108 			} else {
1109 				number += get_swindow();
1110 			}
1111 			cmd_exec();
1112 			if (show_attn == OPT_ONPLUS)
1113 				set_attnpos(bottompos);
1114 			forward((int)number, 0, 1);
1115 			break;
1116 
1117 		case A_FF_LINE:
1118 			/*
1119 			 * Force forward N (default 1) line.
1120 			 */
1121 			if (number <= 0)
1122 				number = 1;
1123 			cmd_exec();
1124 			if (show_attn == OPT_ONPLUS && number > 1)
1125 				set_attnpos(bottompos);
1126 			forward((int)number, 1, 0);
1127 			break;
1128 
1129 		case A_BF_LINE:
1130 			/*
1131 			 * Force backward N (default 1) line.
1132 			 */
1133 			if (number <= 0)
1134 				number = 1;
1135 			cmd_exec();
1136 			backward((int)number, 1, 0);
1137 			break;
1138 
1139 		case A_FF_SCREEN:
1140 			/*
1141 			 * Force forward one screen.
1142 			 */
1143 			if (number <= 0)
1144 				number = get_swindow();
1145 			cmd_exec();
1146 			if (show_attn == OPT_ONPLUS)
1147 				set_attnpos(bottompos);
1148 			forward((int)number, 1, 0);
1149 			break;
1150 
1151 		case A_F_FOREVER:
1152 			/*
1153 			 * Forward forever, ignoring EOF.
1154 			 */
1155 			newaction = forw_loop(0);
1156 			break;
1157 
1158 		case A_F_UNTIL_HILITE:
1159 			newaction = forw_loop(1);
1160 			break;
1161 
1162 		case A_F_SCROLL:
1163 			/*
1164 			 * Forward N lines
1165 			 * (default same as last 'd' or 'u' command).
1166 			 */
1167 			if (number > 0)
1168 				wscroll = (int)number;
1169 			cmd_exec();
1170 			if (show_attn == OPT_ONPLUS)
1171 				set_attnpos(bottompos);
1172 			forward(wscroll, 0, 0);
1173 			break;
1174 
1175 		case A_B_SCROLL:
1176 			/*
1177 			 * Forward N lines
1178 			 * (default same as last 'd' or 'u' command).
1179 			 */
1180 			if (number > 0)
1181 				wscroll = (int)number;
1182 			cmd_exec();
1183 			backward(wscroll, 0, 0);
1184 			break;
1185 
1186 		case A_FREPAINT:
1187 			/*
1188 			 * Flush buffers, then repaint screen.
1189 			 * Don't flush the buffers on a pipe!
1190 			 */
1191 			clear_buffers();
1192 			/* FALLTHRU */
1193 		case A_REPAINT:
1194 			/*
1195 			 * Repaint screen.
1196 			 */
1197 			cmd_exec();
1198 			repaint();
1199 			break;
1200 
1201 		case A_GOLINE:
1202 			/*
1203 			 * Go to line N, default beginning of file.
1204 			 */
1205 			if (number <= 0)
1206 				number = 1;
1207 			cmd_exec();
1208 			jump_back(number);
1209 			break;
1210 
1211 		case A_PERCENT:
1212 			/*
1213 			 * Go to a specified percentage into the file.
1214 			 */
1215 			if (number < 0) {
1216 				number = 0;
1217 				fraction = 0;
1218 			}
1219 			if (number > 100) {
1220 				number = 100;
1221 				fraction = 0;
1222 			}
1223 			cmd_exec();
1224 			jump_percent((int)number, fraction);
1225 			break;
1226 
1227 		case A_GOEND:
1228 			/*
1229 			 * Go to line N, default end of file.
1230 			 */
1231 			cmd_exec();
1232 			if (number <= 0)
1233 				jump_forw();
1234 			else
1235 				jump_back(number);
1236 			break;
1237 
1238 		case A_GOPOS:
1239 			/*
1240 			 * Go to a specified byte position in the file.
1241 			 */
1242 			cmd_exec();
1243 			if (number < 0)
1244 				number = 0;
1245 			jump_line_loc((off_t) number, jump_sline);
1246 			break;
1247 
1248 		case A_STAT:
1249 			/*
1250 			 * Print file name, etc.
1251 			 */
1252 			if (ch_getflags() & CH_HELPFILE)
1253 				break;
1254 			cmd_exec();
1255 			parg.p_string = eq_message();
1256 			error("%s", &parg);
1257 			break;
1258 
1259 		case A_VERSION:
1260 			/*
1261 			 * Print version number, without the "@(#)".
1262 			 */
1263 			cmd_exec();
1264 			dispversion();
1265 			break;
1266 
1267 		case A_QUIT:
1268 			/*
1269 			 * Exit.
1270 			 */
1271 			if (curr_ifile != NULL &&
1272 			    ch_getflags() & CH_HELPFILE) {
1273 				/*
1274 				 * Quit while viewing the help file
1275 				 * just means return to viewing the
1276 				 * previous file.
1277 				 */
1278 				hshift = save_hshift;
1279 				if (edit_prev(1) == 0)
1280 					break;
1281 			}
1282 			if (extra != NULL)
1283 				quit(*extra);
1284 			quit(QUIT_OK);
1285 			break;
1286 
1287 /*
1288  * Define abbreviation for a commonly used sequence below.
1289  */
1290 #define	DO_SEARCH() \
1291 			if (number <= 0) number = 1;	\
1292 			mca_search();			\
1293 			cmd_exec();			\
1294 			multi_search(NULL, (int)number);
1295 
1296 
1297 		case A_F_SEARCH:
1298 			/*
1299 			 * Search forward for a pattern.
1300 			 * Get the first char of the pattern.
1301 			 */
1302 			search_type = SRCH_FORW;
1303 			if (number <= 0)
1304 				number = 1;
1305 			mca_search();
1306 			c = getcc();
1307 			goto again;
1308 
1309 		case A_B_SEARCH:
1310 			/*
1311 			 * Search backward for a pattern.
1312 			 * Get the first char of the pattern.
1313 			 */
1314 			search_type = SRCH_BACK;
1315 			if (number <= 0)
1316 				number = 1;
1317 			mca_search();
1318 			c = getcc();
1319 			goto again;
1320 
1321 		case A_FILTER:
1322 			search_type = SRCH_FORW | SRCH_FILTER;
1323 			mca_search();
1324 			c = getcc();
1325 			goto again;
1326 
1327 		case A_AGAIN_SEARCH:
1328 			/*
1329 			 * Repeat previous search.
1330 			 */
1331 			DO_SEARCH();
1332 			break;
1333 
1334 		case A_T_AGAIN_SEARCH:
1335 			/*
1336 			 * Repeat previous search, multiple files.
1337 			 */
1338 			search_type |= SRCH_PAST_EOF;
1339 			DO_SEARCH();
1340 			break;
1341 
1342 		case A_REVERSE_SEARCH:
1343 			/*
1344 			 * Repeat previous search, in reverse direction.
1345 			 */
1346 			save_search_type = search_type;
1347 			search_type = SRCH_REVERSE(search_type);
1348 			DO_SEARCH();
1349 			search_type = save_search_type;
1350 			break;
1351 
1352 		case A_T_REVERSE_SEARCH:
1353 			/*
1354 			 * Repeat previous search,
1355 			 * multiple files in reverse direction.
1356 			 */
1357 			save_search_type = search_type;
1358 			search_type = SRCH_REVERSE(search_type);
1359 			search_type |= SRCH_PAST_EOF;
1360 			DO_SEARCH();
1361 			search_type = save_search_type;
1362 			break;
1363 
1364 		case A_UNDO_SEARCH:
1365 			undo_search();
1366 			break;
1367 
1368 		case A_HELP:
1369 			/*
1370 			 * Help.
1371 			 */
1372 			if (ch_getflags() & CH_HELPFILE)
1373 				break;
1374 			if (ungot != NULL || unget_end) {
1375 				error(less_is_more
1376 				    ? "Invalid option -p h"
1377 				    : "Invalid option ++h",
1378 				    NULL);
1379 				break;
1380 			}
1381 			cmd_exec();
1382 			save_hshift = hshift;
1383 			hshift = 0;
1384 			(void) edit(helpfile());
1385 			break;
1386 
1387 		case A_EXAMINE:
1388 			/*
1389 			 * Edit a new file.  Get the filename.
1390 			 */
1391 			if (secure) {
1392 				error("Command not available", NULL);
1393 				break;
1394 			}
1395 			start_mca(A_EXAMINE, "Examine: ", ml_examine, 0);
1396 			c = getcc();
1397 			goto again;
1398 
1399 		case A_VISUAL:
1400 			/*
1401 			 * Invoke an editor on the input file.
1402 			 */
1403 			if (secure) {
1404 				error("Command not available", NULL);
1405 				break;
1406 			}
1407 			if (ch_getflags() & CH_HELPFILE)
1408 				break;
1409 			if (strcmp(get_filename(curr_ifile), "-") == 0) {
1410 				error("Cannot edit standard input", NULL);
1411 				break;
1412 			}
1413 			if (curr_altfilename != NULL) {
1414 				error("WARNING: This file was viewed via "
1415 				    "LESSOPEN", NULL);
1416 			}
1417 			/*
1418 			 * Expand the editor prototype string
1419 			 * and pass it to the system to execute.
1420 			 * (Make sure the screen is displayed so the
1421 			 * expansion of "+%lm" works.)
1422 			 */
1423 			make_display();
1424 			cmd_exec();
1425 			lsystem(pr_expand(editproto, 0), NULL);
1426 			break;
1427 
1428 		case A_NEXT_FILE:
1429 			/*
1430 			 * Examine next file.
1431 			 */
1432 			if (ntags()) {
1433 				error("No next file", NULL);
1434 				break;
1435 			}
1436 			if (number <= 0)
1437 				number = 1;
1438 			if (edit_next((int)number)) {
1439 				if (get_quit_at_eof() && eof_displayed() &&
1440 				    !(ch_getflags() & CH_HELPFILE))
1441 					quit(QUIT_OK);
1442 				parg.p_string = (number > 1) ? "(N-th) " : "";
1443 				error("No %snext file", &parg);
1444 			}
1445 			break;
1446 
1447 		case A_PREV_FILE:
1448 			/*
1449 			 * Examine previous file.
1450 			 */
1451 			if (ntags()) {
1452 				error("No previous file", NULL);
1453 				break;
1454 			}
1455 			if (number <= 0)
1456 				number = 1;
1457 			if (edit_prev((int)number)) {
1458 				parg.p_string = (number > 1) ? "(N-th) " : "";
1459 				error("No %sprevious file", &parg);
1460 			}
1461 			break;
1462 
1463 		case A_NEXT_TAG:
1464 			if (number <= 0)
1465 				number = 1;
1466 			tagfile = nexttag((int)number);
1467 			if (tagfile == NULL) {
1468 				error("No next tag", NULL);
1469 				break;
1470 			}
1471 			if (edit(tagfile) == 0) {
1472 				off_t pos = tagsearch();
1473 				if (pos != -1)
1474 					jump_loc(pos, jump_sline);
1475 			}
1476 			break;
1477 
1478 		case A_PREV_TAG:
1479 			if (number <= 0)
1480 				number = 1;
1481 			tagfile = prevtag((int)number);
1482 			if (tagfile == NULL) {
1483 				error("No previous tag", NULL);
1484 				break;
1485 			}
1486 			if (edit(tagfile) == 0) {
1487 				off_t pos = tagsearch();
1488 				if (pos != -1)
1489 					jump_loc(pos, jump_sline);
1490 			}
1491 			break;
1492 
1493 		case A_INDEX_FILE:
1494 			/*
1495 			 * Examine a particular file.
1496 			 */
1497 			if (number <= 0)
1498 				number = 1;
1499 			if (edit_index((int)number))
1500 				error("No such file", NULL);
1501 			break;
1502 
1503 		case A_REMOVE_FILE:
1504 			if (ch_getflags() & CH_HELPFILE)
1505 				break;
1506 			old_ifile = curr_ifile;
1507 			new_ifile = getoff_ifile(curr_ifile);
1508 			if (new_ifile == NULL) {
1509 				ring_bell();
1510 				break;
1511 			}
1512 			if (edit_ifile(new_ifile) != 0) {
1513 				reedit_ifile(old_ifile);
1514 				break;
1515 			}
1516 			del_ifile(old_ifile);
1517 			break;
1518 
1519 		case A_OPT_TOGGLE:
1520 			optflag = OPT_TOGGLE;
1521 			optgetname = FALSE;
1522 			mca_opt_toggle();
1523 			c = getcc();
1524 			goto again;
1525 
1526 		case A_DISP_OPTION:
1527 			/*
1528 			 * Report a flag setting.
1529 			 */
1530 			optflag = OPT_NO_TOGGLE;
1531 			optgetname = FALSE;
1532 			mca_opt_toggle();
1533 			c = getcc();
1534 			goto again;
1535 
1536 		case A_FIRSTCMD:
1537 			/*
1538 			 * Set an initial command for new files.
1539 			 */
1540 			start_mca(A_FIRSTCMD, "+", NULL, 0);
1541 			c = getcc();
1542 			goto again;
1543 
1544 		case A_SETMARK:
1545 			/*
1546 			 * Set a mark.
1547 			 */
1548 			if (ch_getflags() & CH_HELPFILE)
1549 				break;
1550 			start_mca(A_SETMARK, "mark: ", (void*)NULL, 0);
1551 			c = getcc();
1552 			if (c == erase_char || c == erase2_char ||
1553 			    c == kill_char || c == '\n' || c == '\r')
1554 				break;
1555 			setmark(c);
1556 			break;
1557 
1558 		case A_GOMARK:
1559 			/*
1560 			 * Go to a mark.
1561 			 */
1562 			start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0);
1563 			c = getcc();
1564 			if (c == erase_char || c == erase2_char ||
1565 			    c == kill_char || c == '\n' || c == '\r')
1566 				break;
1567 			cmd_exec();
1568 			gomark(c);
1569 			break;
1570 
1571 		case A_PIPE:
1572 			if (secure) {
1573 				error("Command not available", NULL);
1574 				break;
1575 			}
1576 			start_mca(A_PIPE, "|mark: ", (void*)NULL, 0);
1577 			c = getcc();
1578 			if (c == erase_char || c == erase2_char ||
1579 			    c == kill_char)
1580 				break;
1581 			if (c == '\n' || c == '\r')
1582 				c = '.';
1583 			if (badmark(c))
1584 				break;
1585 			pipec = c;
1586 			start_mca(A_PIPE, "!", ml_shell, 0);
1587 			c = getcc();
1588 			goto again;
1589 
1590 		case A_B_BRACKET:
1591 		case A_F_BRACKET:
1592 			start_mca(action, "Brackets: ", (void*)NULL, 0);
1593 			c = getcc();
1594 			goto again;
1595 
1596 		case A_LSHIFT:
1597 			if (number > 0)
1598 				shift_count = number;
1599 			else
1600 				number = (shift_count > 0) ?
1601 				    shift_count : sc_width / 2;
1602 			if (number > hshift)
1603 				number = hshift;
1604 			hshift -= number;
1605 			screen_trashed = 1;
1606 			break;
1607 
1608 		case A_RSHIFT:
1609 			if (number > 0)
1610 				shift_count = number;
1611 			else
1612 				number = (shift_count > 0) ?
1613 				    shift_count : sc_width / 2;
1614 			hshift += number;
1615 			screen_trashed = 1;
1616 			break;
1617 
1618 		case A_PREFIX:
1619 			/*
1620 			 * The command is incomplete (more chars are needed).
1621 			 * Display the current char, so the user knows
1622 			 * what's going on, and get another character.
1623 			 */
1624 			if (mca != A_PREFIX) {
1625 				cmd_reset();
1626 				start_mca(A_PREFIX, " ", (void*)NULL,
1627 				    CF_QUIT_ON_ERASE);
1628 				(void) cmd_char(c);
1629 			}
1630 			c = getcc();
1631 			goto again;
1632 
1633 		case A_NOACTION:
1634 			break;
1635 
1636 		default:
1637 			ring_bell();
1638 			break;
1639 		}
1640 	}
1641 }
1642