Remove building with NOCRYPTO option
[minix.git] / external / bsd / less / dist / command.c
blobddca14ee154d117737748c97c34b2303f8c8a8f1
1 /* $NetBSD: command.c,v 1.5 2013/09/04 19:44:21 tron Exp $ */
3 /*
4 * Copyright (C) 1984-2012 Mark Nudelman
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.
9 * For more information, see the README file.
14 * User-level command processor.
17 #include "less.h"
18 #if MSDOS_COMPILER==WIN32C
19 #include <windows.h>
20 #endif
21 #include "position.h"
22 #include "option.h"
23 #include "cmd.h"
25 extern int erase_char, erase2_char, kill_char;
26 extern int sigs;
27 extern int quit_if_one_screen;
28 extern int squished;
29 extern int sc_width;
30 extern int sc_height;
31 extern int swindow;
32 extern int jump_sline;
33 extern int quitting;
34 extern int wscroll;
35 extern int top_scroll;
36 extern int ignore_eoi;
37 extern int secure;
38 extern int hshift;
39 extern int show_attn;
40 extern POSITION highest_hilite;
41 extern char *every_first_cmd;
42 extern char *curr_altfilename;
43 extern char version[];
44 extern struct scrpos initial_scrpos;
45 extern IFILE curr_ifile;
46 extern void constant *ml_search;
47 extern void * constant ml_examine;
48 #if SHELL_ESCAPE || PIPEC
49 extern void * constant ml_shell;
50 #endif
51 #if EDITOR
52 extern char *editor;
53 extern char *editproto;
54 #endif
55 extern int screen_trashed; /* The screen has been overwritten */
56 extern int shift_count;
57 extern int oldbot;
58 extern int forw_prompt;
59 extern int be_helpful;
60 extern int more_mode;
62 static int helpprompt;
64 #if SHELL_ESCAPE
65 static char *shellcmd = NULL; /* For holding last shell command for "!!" */
66 #endif
67 static int mca; /* The multicharacter command (action) */
68 static int search_type; /* The previous type of search */
69 static LINENUM number; /* The number typed by the user */
70 static long fraction; /* The fractional part of the number */
71 static struct loption *curropt;
72 static int opt_lower;
73 static int optflag;
74 static int optgetname;
75 static POSITION bottompos;
76 static int save_hshift;
77 #if PIPEC
78 static char pipec;
79 #endif
81 struct ungot {
82 struct ungot *ug_next;
83 char ug_char;
85 static struct ungot* ungot = NULL;
86 static int unget_end = 0;
88 static void multi_search();
91 * Move the cursor to start of prompt line before executing a command.
92 * This looks nicer if the command takes a long time before
93 * updating the screen.
95 static void
96 cmd_exec()
98 #if HILITE_SEARCH
99 clear_attn();
100 #endif
101 clear_bot();
102 flush();
106 * Set up the display to start a new multi-character command.
108 static void
109 start_mca(action, prompt, mlist, cmdflags)
110 int action;
111 constant char *prompt;
112 constant void *mlist;
113 int cmdflags;
115 mca = action;
116 clear_bot();
117 clear_cmd();
118 cmd_putstr(prompt);
119 set_mlist(mlist, cmdflags);
122 public int
123 in_mca()
125 return (mca != 0 && mca != A_PREFIX);
129 * Set up the display to start a new search command.
131 static void
132 mca_search()
134 #if HILITE_SEARCH
135 if (search_type & SRCH_FILTER)
136 mca = A_FILTER;
137 else
138 #endif
139 if (search_type & SRCH_FORW)
140 mca = A_F_SEARCH;
141 else
142 mca = A_B_SEARCH;
144 clear_bot();
145 clear_cmd();
147 if (search_type & SRCH_NO_MATCH)
148 cmd_putstr("Non-match ");
149 if (search_type & SRCH_FIRST_FILE)
150 cmd_putstr("First-file ");
151 if (search_type & SRCH_PAST_EOF)
152 cmd_putstr("EOF-ignore ");
153 if (search_type & SRCH_NO_MOVE)
154 cmd_putstr("Keep-pos ");
155 if (search_type & SRCH_NO_REGEX)
156 cmd_putstr("Regex-off ");
158 #if HILITE_SEARCH
159 if (search_type & SRCH_FILTER)
160 cmd_putstr("&/");
161 else
162 #endif
163 if (search_type & SRCH_FORW)
164 cmd_putstr("/");
165 else
166 cmd_putstr("?");
167 set_mlist(ml_search, 0);
171 * Set up the display to start a new toggle-option command.
173 static void
174 mca_opt_toggle()
176 int no_prompt;
177 int flag;
178 char *dash;
180 no_prompt = (optflag & OPT_NO_PROMPT);
181 flag = (optflag & ~OPT_NO_PROMPT);
182 dash = (flag == OPT_NO_TOGGLE) ? "_" : "-";
184 mca = A_OPT_TOGGLE;
185 clear_bot();
186 clear_cmd();
187 cmd_putstr(dash);
188 if (optgetname)
189 cmd_putstr(dash);
190 if (no_prompt)
191 cmd_putstr("(P)");
192 switch (flag)
194 case OPT_UNSET:
195 cmd_putstr("+");
196 break;
197 case OPT_SET:
198 cmd_putstr("!");
199 break;
201 set_mlist(NULL, 0);
205 * Execute a multicharacter command.
207 static void
208 exec_mca()
210 register char *cbuf;
212 cmd_exec();
213 cbuf = get_cmdbuf();
215 switch (mca)
217 case A_F_SEARCH:
218 case A_B_SEARCH:
219 multi_search(cbuf, (int) number);
220 break;
221 #if HILITE_SEARCH
222 case A_FILTER:
223 search_type ^= SRCH_NO_MATCH;
224 set_filter_pattern(cbuf, search_type);
225 break;
226 #endif
227 case A_FIRSTCMD:
229 * Skip leading spaces or + signs in the string.
231 while (*cbuf == '+' || *cbuf == ' ')
232 cbuf++;
233 if (every_first_cmd != NULL)
234 free(every_first_cmd);
235 if (*cbuf == '\0')
236 every_first_cmd = NULL;
237 else
238 every_first_cmd = save(cbuf);
239 break;
240 case A_OPT_TOGGLE:
241 toggle_option(curropt, opt_lower, cbuf, optflag);
242 curropt = NULL;
243 break;
244 case A_F_BRACKET:
245 match_brac(cbuf[0], cbuf[1], 1, (int) number);
246 break;
247 case A_B_BRACKET:
248 match_brac(cbuf[1], cbuf[0], 0, (int) number);
249 break;
250 #if EXAMINE
251 case A_EXAMINE:
252 if (secure)
253 break;
254 edit_list(cbuf);
255 #if TAGS
256 /* If tag structure is loaded then clean it up. */
257 cleantags();
258 #endif
259 break;
260 #endif
261 #if SHELL_ESCAPE
262 case A_SHELL:
264 * !! just uses whatever is in shellcmd.
265 * Otherwise, copy cmdbuf to shellcmd,
266 * expanding any special characters ("%" or "#").
268 if (*cbuf != '!')
270 if (shellcmd != NULL)
271 free(shellcmd);
272 shellcmd = fexpand(cbuf);
275 if (secure)
276 break;
277 if (shellcmd == NULL)
278 lsystem("", "!done");
279 else
280 lsystem(shellcmd, "!done");
281 break;
282 #endif
283 #if PIPEC
284 case A_PIPE:
285 if (secure)
286 break;
287 (void) pipe_mark(pipec, cbuf);
288 error("|done", NULL_PARG);
289 break;
290 #endif
295 * Is a character an erase or kill char?
297 static int
298 is_erase_char(c)
299 int c;
301 return (c == erase_char || c == erase2_char || c == kill_char);
305 * Handle the first char of an option (after the initial dash).
307 static int
308 mca_opt_first_char(c)
309 int c;
311 int flag = (optflag & ~OPT_NO_PROMPT);
312 if (flag == OPT_NO_TOGGLE)
314 switch (c)
316 case '_':
317 /* "__" = long option name. */
318 optgetname = TRUE;
319 mca_opt_toggle();
320 return (MCA_MORE);
322 } else
324 switch (c)
326 case '+':
327 /* "-+" = UNSET. */
328 optflag = (flag == OPT_UNSET) ?
329 OPT_TOGGLE : OPT_UNSET;
330 mca_opt_toggle();
331 return (MCA_MORE);
332 case '!':
333 /* "-!" = SET */
334 optflag = (flag == OPT_SET) ?
335 OPT_TOGGLE : OPT_SET;
336 mca_opt_toggle();
337 return (MCA_MORE);
338 case CONTROL('P'):
339 optflag ^= OPT_NO_PROMPT;
340 mca_opt_toggle();
341 return (MCA_MORE);
342 case '-':
343 /* "--" = long option name. */
344 optgetname = TRUE;
345 mca_opt_toggle();
346 return (MCA_MORE);
349 /* Char was not handled here. */
350 return (NO_MCA);
354 * Add a char to a long option name.
355 * See if we've got a match for an option name yet.
356 * If so, display the complete name and stop
357 * accepting chars until user hits RETURN.
359 static int
360 mca_opt_nonfirst_char(c)
361 int c;
363 char *p;
364 char *oname;
366 if (curropt != NULL)
369 * Already have a match for the name.
370 * Don't accept anything but erase/kill.
372 if (is_erase_char(c))
373 return (MCA_DONE);
374 return (MCA_MORE);
377 * Add char to cmd buffer and try to match
378 * the option name.
380 if (cmd_char(c) == CC_QUIT)
381 return (MCA_DONE);
382 p = get_cmdbuf();
383 opt_lower = ASCII_IS_LOWER(p[0]);
384 curropt = findopt_name(&p, &oname, NULL);
385 if (curropt != NULL)
388 * Got a match.
389 * Remember the option and
390 * display the full option name.
392 cmd_reset();
393 mca_opt_toggle();
394 for (p = oname; *p != '\0'; p++)
396 c = *p;
397 if (!opt_lower && ASCII_IS_LOWER(c))
398 c = ASCII_TO_UPPER(c);
399 if (cmd_char(c) != CC_OK)
400 return (MCA_DONE);
403 return (MCA_MORE);
407 * Handle a char of an option toggle command.
409 static int
410 mca_opt_char(c)
411 int c;
413 PARG parg;
416 * This may be a short option (single char),
417 * or one char of a long option name,
418 * or one char of the option parameter.
420 if (curropt == NULL && len_cmdbuf() == 0)
422 int ret = mca_opt_first_char(c);
423 if (ret != NO_MCA)
424 return (ret);
426 if (optgetname)
428 /* We're getting a long option name. */
429 if (c != '\n' && c != '\r')
430 return (mca_opt_nonfirst_char(c));
431 if (curropt == NULL)
433 parg.p_string = get_cmdbuf();
434 error("There is no --%s option", &parg);
435 return (MCA_DONE);
437 optgetname = FALSE;
438 cmd_reset();
439 } else
441 if (is_erase_char(c))
442 return (NO_MCA);
443 if (curropt != NULL)
444 /* We're getting the option parameter. */
445 return (NO_MCA);
446 curropt = findopt(c);
447 if (curropt == NULL)
449 parg.p_string = propt(c);
450 error("There is no %s option", &parg);
451 return (MCA_DONE);
455 * If the option which was entered does not take a
456 * parameter, toggle the option immediately,
457 * so user doesn't have to hit RETURN.
459 if ((optflag & ~OPT_NO_PROMPT) != OPT_TOGGLE ||
460 !opt_has_param(curropt))
462 toggle_option(curropt, ASCII_IS_LOWER(c), "", optflag);
463 return (MCA_DONE);
466 * Display a prompt appropriate for the option parameter.
468 start_mca(A_OPT_TOGGLE, opt_prompt(curropt), (void*)NULL, 0);
469 return (MCA_MORE);
473 * Handle a char of a search command.
475 static int
476 mca_search_char(c)
477 int c;
479 int flag = 0;
482 * Certain characters as the first char of
483 * the pattern have special meaning:
484 * ! Toggle the NO_MATCH flag
485 * * Toggle the PAST_EOF flag (less extension)
486 * @ Toggle the FIRST_FILE flag (less extension)
488 if (len_cmdbuf() > 0)
489 return (NO_MCA);
491 switch (c)
493 case '*':
494 if (more_mode)
495 break;
496 case CONTROL('E'): /* ignore END of file */
497 if (mca != A_FILTER)
498 flag = SRCH_PAST_EOF;
499 break;
500 case '@':
501 if (more_mode)
502 break;
503 case CONTROL('F'): /* FIRST file */
504 if (mca != A_FILTER)
505 flag = SRCH_FIRST_FILE;
506 break;
507 case CONTROL('K'): /* KEEP position */
508 if (mca != A_FILTER)
509 flag = SRCH_NO_MOVE;
510 break;
511 case CONTROL('R'): /* Don't use REGULAR EXPRESSIONS */
512 flag = SRCH_NO_REGEX;
513 break;
514 case CONTROL('N'): /* NOT match */
515 case '!':
516 flag = SRCH_NO_MATCH;
517 break;
520 if (flag != 0)
522 search_type ^= flag;
523 mca_search();
524 return (MCA_MORE);
526 return (NO_MCA);
530 * Handle a character of a multi-character command.
532 static int
533 mca_char(c)
534 int c;
536 int ret;
538 switch (mca)
540 case 0:
542 * We're not in a multicharacter command.
544 return (NO_MCA);
546 case A_PREFIX:
548 * In the prefix of a command.
549 * This not considered a multichar command
550 * (even tho it uses cmdbuf, etc.).
551 * It is handled in the commands() switch.
553 return (NO_MCA);
555 case A_DIGIT:
557 * Entering digits of a number.
558 * Terminated by a non-digit.
560 if (!((c >= '0' && c <= '9') || c == '.') &&
561 editchar(c, EC_PEEK|EC_NOHISTORY|EC_NOCOMPLETE|EC_NORIGHTLEFT) == A_INVALID)
564 * Not part of the number.
565 * End the number and treat this char
566 * as a normal command character.
568 number = cmd_int(&fraction);
569 mca = 0;
570 cmd_accept();
571 return (NO_MCA);
573 break;
575 case A_OPT_TOGGLE:
576 ret = mca_opt_char(c);
577 if (ret != NO_MCA)
578 return (ret);
579 break;
581 case A_F_SEARCH:
582 case A_B_SEARCH:
583 case A_FILTER:
584 ret = mca_search_char(c);
585 if (ret != NO_MCA)
586 return (ret);
587 break;
589 default:
590 /* Other multicharacter command. */
591 break;
595 * The multichar command is terminated by a newline.
597 if (c == '\n' || c == '\r')
600 * Execute the command.
602 exec_mca();
603 return (MCA_DONE);
607 * Append the char to the command buffer.
609 if (cmd_char(c) == CC_QUIT)
611 * Abort the multi-char command.
613 return (MCA_DONE);
615 if ((mca == A_F_BRACKET || mca == A_B_BRACKET) && len_cmdbuf() >= 2)
618 * Special case for the bracket-matching commands.
619 * Execute the command after getting exactly two
620 * characters from the user.
622 exec_mca();
623 return (MCA_DONE);
627 * Need another character.
629 return (MCA_MORE);
633 * Discard any buffered file data.
635 static void
636 clear_buffers()
638 if (!(ch_getflags() & CH_CANSEEK))
639 return;
640 ch_flush();
641 clr_linenum();
642 #if HILITE_SEARCH
643 clr_hilite();
644 #endif
648 * Make sure the screen is displayed.
650 static void
651 make_display()
654 * If nothing is displayed yet, display starting from initial_scrpos.
656 if (empty_screen())
658 if (initial_scrpos.pos == NULL_POSITION)
660 * {{ Maybe this should be:
661 * jump_loc(ch_zero(), jump_sline);
662 * but this behavior seems rather unexpected
663 * on the first screen. }}
665 jump_loc(ch_zero(), 1);
666 else
667 jump_loc(initial_scrpos.pos, initial_scrpos.ln);
668 } else if (screen_trashed)
670 int save_top_scroll = top_scroll;
671 int save_ignore_eoi = ignore_eoi;
672 top_scroll = 1;
673 ignore_eoi = 0;
674 if (screen_trashed == 2)
676 /* Special case used by ignore_eoi: re-open the input file
677 * and jump to the end of the file. */
678 reopen_curr_ifile();
679 jump_forw();
681 repaint();
682 top_scroll = save_top_scroll;
683 ignore_eoi = save_ignore_eoi;
688 * Display the appropriate prompt.
690 static void
691 prompt()
693 register constant char *p;
695 if (ungot != NULL)
698 * No prompt necessary if commands are from
699 * ungotten chars rather than from the user.
701 return;
705 * Make sure the screen is displayed.
707 make_display();
708 bottompos = position(BOTTOM_PLUS_ONE);
711 * If we've hit EOF on the last file and the -E flag is set, quit.
713 if (get_quit_at_eof() == OPT_ONPLUS &&
714 eof_displayed() && !(ch_getflags() & CH_HELPFILE) &&
715 next_ifile(curr_ifile) == NULL_IFILE)
716 quit(QUIT_OK);
719 * If the entire file is displayed and the -F flag is set, quit.
721 if (quit_if_one_screen &&
722 entire_file_displayed() && !(ch_getflags() & CH_HELPFILE) &&
723 next_ifile(curr_ifile) == NULL_IFILE)
724 quit(QUIT_OK);
726 #if MSDOS_COMPILER==WIN32C
728 * In Win32, display the file name in the window title.
730 if (!(ch_getflags() & CH_HELPFILE))
731 SetConsoleTitle(pr_expand("Less?f - %f.", 0));
732 #endif
734 * Select the proper prompt and display it.
737 * If the previous action was a forward movement,
738 * don't clear the bottom line of the display;
739 * just print the prompt since the forward movement guarantees
740 * that we're in the right position to display the prompt.
741 * Clearing the line could cause a problem: for example, if the last
742 * line displayed ended at the right screen edge without a newline,
743 * then clearing would clear the last displayed line rather than
744 * the prompt line.
746 if (!forw_prompt)
747 clear_bot();
748 clear_cmd();
749 forw_prompt = 0;
750 if (helpprompt) {
751 at_enter(AT_STANDOUT);
752 putstr("[Press 'h' for instructions.]");
753 at_exit();
754 helpprompt = 0;
755 } else {
756 p = pr_string();
757 if (is_filtering())
758 putstr("& ");
759 if (p == NULL || *p == '\0')
760 putchr(':');
761 else
763 at_enter(AT_STANDOUT);
764 putstr(p);
765 at_exit();
768 clear_eol();
772 * Display the less version message.
774 public void
775 dispversion()
777 PARG parg;
779 parg.p_string = version;
780 error("less %s", &parg);
784 * Get command character.
785 * The character normally comes from the keyboard,
786 * but may come from ungotten characters
787 * (characters previously given to ungetcc or ungetsc).
789 public int
790 getcc()
792 if (unget_end)
795 * We have just run out of ungotten chars.
797 unget_end = 0;
798 if (len_cmdbuf() == 0 || !empty_screen())
799 return (getchr());
801 * Command is incomplete, so try to complete it.
803 switch (mca)
805 case A_DIGIT:
807 * We have a number but no command. Treat as #g.
809 return ('g');
811 case A_F_SEARCH:
812 case A_B_SEARCH:
814 * We have "/string" but no newline. Add the \n.
816 return ('\n');
818 default:
820 * Some other incomplete command. Let user complete it.
822 return (getchr());
826 if (ungot == NULL)
829 * Normal case: no ungotten chars, so get one from the user.
831 return (getchr());
835 * Return the next ungotten char.
838 struct ungot *ug = ungot;
839 char c = ug->ug_char;
840 ungot = ug->ug_next;
841 free(ug);
842 unget_end = (ungot == NULL);
843 return (c);
848 * "Unget" a command character.
849 * The next getcc() will return this character.
851 public void
852 ungetcc(c)
853 int c;
855 struct ungot *ug = (struct ungot *) ecalloc(1, sizeof(struct ungot));
857 ug->ug_char = c;
858 ug->ug_next = ungot;
859 ungot = ug;
860 unget_end = 0;
864 * Unget a whole string of command characters.
865 * The next sequence of getcc()'s will return this string.
867 public void
868 ungetsc(s)
869 char *s;
871 register char *p;
873 for (p = s + strlen(s) - 1; p >= s; p--)
874 ungetcc(*p);
878 * Search for a pattern, possibly in multiple files.
879 * If SRCH_FIRST_FILE is set, begin searching at the first file.
880 * If SRCH_PAST_EOF is set, continue the search thru multiple files.
882 static void
883 multi_search(pattern, n)
884 char *pattern;
885 int n;
887 register int nomore;
888 IFILE save_ifile;
889 int changed_file;
891 changed_file = 0;
892 save_ifile = save_curr_ifile();
894 if (search_type & SRCH_FIRST_FILE)
897 * Start at the first (or last) file
898 * in the command line list.
900 if (search_type & SRCH_FORW)
901 nomore = edit_first();
902 else
903 nomore = edit_last();
904 if (nomore)
906 unsave_ifile(save_ifile);
907 return;
909 changed_file = 1;
910 search_type &= ~SRCH_FIRST_FILE;
913 for (;;)
915 n = search(search_type, pattern, n);
917 * The SRCH_NO_MOVE flag doesn't "stick": it gets cleared
918 * after being used once. This allows "n" to work after
919 * using a /@@ search.
921 search_type &= ~SRCH_NO_MOVE;
922 if (n == 0)
925 * Found it.
927 unsave_ifile(save_ifile);
928 return;
931 if (n < 0)
933 * Some kind of error in the search.
934 * Error message has been printed by search().
936 break;
938 if ((search_type & SRCH_PAST_EOF) == 0)
940 * We didn't find a match, but we're
941 * supposed to search only one file.
943 break;
945 * Move on to the next file.
947 if (search_type & SRCH_FORW)
948 nomore = edit_next(1);
949 else
950 nomore = edit_prev(1);
951 if (nomore)
952 break;
953 changed_file = 1;
957 * Didn't find it.
958 * Print an error message if we haven't already.
960 if (n > 0)
961 error("Pattern not found", NULL_PARG);
963 if (changed_file)
966 * Restore the file we were originally viewing.
968 reedit_ifile(save_ifile);
969 } else
971 unsave_ifile(save_ifile);
976 * Forward forever, or until a highlighted line appears.
978 static int
979 forw_loop(until_hilite)
980 int until_hilite;
982 POSITION curr_len;
984 if (ch_getflags() & CH_HELPFILE)
985 return (A_NOACTION);
987 cmd_exec();
988 jump_forw();
989 curr_len = ch_length();
990 highest_hilite = until_hilite ? curr_len : NULL_POSITION;
991 ignore_eoi = 1;
992 while (!sigs)
994 if (until_hilite && highest_hilite > curr_len)
996 bell();
997 break;
999 make_display();
1000 forward(1, 0, 0);
1002 ignore_eoi = 0;
1003 ch_set_eof();
1006 * This gets us back in "F mode" after processing
1007 * a non-abort signal (e.g. window-change).
1009 if (sigs && !ABORT_SIGS())
1010 return (until_hilite ? A_F_UNTIL_HILITE : A_F_FOREVER);
1012 return (A_NOACTION);
1016 * Main command processor.
1017 * Accept and execute commands until a quit command.
1019 public void
1020 commands()
1022 register int c;
1023 register int action;
1024 register char *cbuf;
1025 int newaction;
1026 int save_search_type;
1027 char *extra;
1028 char tbuf[2];
1029 PARG parg;
1030 IFILE old_ifile;
1031 IFILE new_ifile;
1032 char *tagfile;
1033 int until_hilite = 0;
1035 search_type = SRCH_FORW;
1036 wscroll = (sc_height + 1) / 2;
1037 newaction = A_NOACTION;
1039 for (;;)
1041 mca = 0;
1042 cmd_accept();
1043 number = 0;
1044 curropt = NULL;
1047 * See if any signals need processing.
1049 if (sigs)
1051 psignals();
1052 if (quitting)
1053 quit(QUIT_SAVED_STATUS);
1057 * See if window size changed, for systems that don't
1058 * generate SIGWINCH.
1060 check_winch();
1063 * Display prompt and accept a character.
1065 cmd_reset();
1066 prompt();
1067 if (sigs)
1068 continue;
1069 if (newaction == A_NOACTION)
1070 c = getcc();
1072 again:
1073 if (sigs)
1074 continue;
1076 if (newaction != A_NOACTION)
1078 action = newaction;
1079 newaction = A_NOACTION;
1080 } else
1083 * If we are in a multicharacter command, call mca_char.
1084 * Otherwise we call fcmd_decode to determine the
1085 * action to be performed.
1087 if (mca)
1088 switch (mca_char(c))
1090 case MCA_MORE:
1092 * Need another character.
1094 c = getcc();
1095 goto again;
1096 case MCA_DONE:
1098 * Command has been handled by mca_char.
1099 * Start clean with a prompt.
1101 continue;
1102 case NO_MCA:
1104 * Not a multi-char command
1105 * (at least, not anymore).
1107 break;
1111 * Decode the command character and decide what to do.
1113 if (mca)
1116 * We're in a multichar command.
1117 * Add the character to the command buffer
1118 * and display it on the screen.
1119 * If the user backspaces past the start
1120 * of the line, abort the command.
1122 if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0)
1123 continue;
1124 cbuf = get_cmdbuf();
1125 } else
1128 * Don't use cmd_char if we're starting fresh
1129 * at the beginning of a command, because we
1130 * don't want to echo the command until we know
1131 * it is a multichar command. We also don't
1132 * want erase_char/kill_char to be treated
1133 * as line editing characters.
1135 tbuf[0] = c;
1136 tbuf[1] = '\0';
1137 cbuf = tbuf;
1139 extra = NULL;
1140 action = fcmd_decode(cbuf, &extra);
1142 * If an "extra" string was returned,
1143 * process it as a string of command characters.
1145 if (extra != NULL)
1146 ungetsc(extra);
1149 * Clear the cmdbuf string.
1150 * (But not if we're in the prefix of a command,
1151 * because the partial command string is kept there.)
1153 if (action != A_PREFIX)
1154 cmd_reset();
1156 switch (action)
1158 case A_DIGIT:
1160 * First digit of a number.
1162 start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE);
1163 goto again;
1165 case A_F_WINDOW:
1167 * Forward one window (and set the window size).
1169 if (number > 0)
1170 swindow = (int) number;
1171 /* FALLTHRU */
1172 case A_F_SCREEN:
1174 * Forward one screen.
1176 if (number <= 0)
1177 number = get_swindow();
1178 cmd_exec();
1179 if (show_attn)
1180 set_attnpos(bottompos);
1181 forward((int) number, 0, 1);
1182 break;
1184 case A_B_WINDOW:
1186 * Backward one window (and set the window size).
1188 if (number > 0)
1189 swindow = (int) number;
1190 /* FALLTHRU */
1191 case A_B_SCREEN:
1193 * Backward one screen.
1195 if (number <= 0)
1196 number = get_swindow();
1197 cmd_exec();
1198 backward((int) number, 0, 1);
1199 break;
1201 case A_F_LINE:
1203 * Forward N (default 1) line.
1205 if (number <= 0)
1206 number = 1;
1207 cmd_exec();
1208 if (show_attn == OPT_ONPLUS && number > 1)
1209 set_attnpos(bottompos);
1210 forward((int) number, 0, 0);
1211 break;
1213 case A_B_LINE:
1215 * Backward N (default 1) line.
1217 if (number <= 0)
1218 number = 1;
1219 cmd_exec();
1220 backward((int) number, 0, 0);
1221 break;
1223 case A_FF_LINE:
1225 * Force forward N (default 1) line.
1227 if (number <= 0)
1228 number = 1;
1229 cmd_exec();
1230 if (show_attn == OPT_ONPLUS && number > 1)
1231 set_attnpos(bottompos);
1232 forward((int) number, 1, 0);
1233 break;
1235 case A_BF_LINE:
1237 * Force backward N (default 1) line.
1239 if (number <= 0)
1240 number = 1;
1241 cmd_exec();
1242 backward((int) number, 1, 0);
1243 break;
1245 case A_FF_SCREEN:
1247 * Force forward one screen.
1249 if (number <= 0)
1250 number = get_swindow();
1251 cmd_exec();
1252 if (show_attn == OPT_ONPLUS)
1253 set_attnpos(bottompos);
1254 forward((int) number, 1, 0);
1255 break;
1257 case A_F_FOREVER:
1259 * Forward forever, ignoring EOF.
1261 newaction = forw_loop(0);
1262 break;
1264 case A_F_UNTIL_HILITE:
1265 newaction = forw_loop(1);
1266 break;
1268 case A_F_SCROLL:
1270 * Forward N lines
1271 * (default same as last 'd' or 'u' command).
1273 if (number > 0)
1274 wscroll = (int) number;
1275 cmd_exec();
1276 if (show_attn == OPT_ONPLUS)
1277 set_attnpos(bottompos);
1278 forward(wscroll, 0, 0);
1279 break;
1281 case A_B_SCROLL:
1283 * Forward N lines
1284 * (default same as last 'd' or 'u' command).
1286 if (number > 0)
1287 wscroll = (int) number;
1288 cmd_exec();
1289 backward(wscroll, 0, 0);
1290 break;
1292 case A_FREPAINT:
1294 * Flush buffers, then repaint screen.
1295 * Don't flush the buffers on a pipe!
1297 clear_buffers();
1298 /* FALLTHRU */
1299 case A_REPAINT:
1301 * Repaint screen.
1303 cmd_exec();
1304 repaint();
1305 break;
1307 case A_GOLINE:
1309 * Go to line N, default beginning of file.
1311 if (number <= 0)
1312 number = 1;
1313 cmd_exec();
1314 jump_back(number);
1315 break;
1317 case A_PERCENT:
1319 * Go to a specified percentage into the file.
1321 if (number < 0)
1323 number = 0;
1324 fraction = 0;
1326 if (number > 100)
1328 number = 100;
1329 fraction = 0;
1331 cmd_exec();
1332 jump_percent((int) number, fraction);
1333 break;
1335 case A_GOEND:
1337 * Go to line N, default end of file.
1339 cmd_exec();
1340 if (number <= 0)
1341 jump_forw();
1342 else
1343 jump_back(number);
1344 break;
1346 case A_GOPOS:
1348 * Go to a specified byte position in the file.
1350 cmd_exec();
1351 if (number < 0)
1352 number = 0;
1353 jump_line_loc((POSITION) number, jump_sline);
1354 break;
1356 case A_STAT:
1358 * Print file name, etc.
1360 if (ch_getflags() & CH_HELPFILE)
1361 break;
1362 cmd_exec();
1363 parg.p_string = eq_message();
1364 error("%s", &parg);
1365 break;
1367 case A_VERSION:
1369 * Print version number, without the "@(#)".
1371 cmd_exec();
1372 dispversion();
1373 break;
1375 case A_QUIT:
1377 * Exit.
1379 if (curr_ifile != NULL_IFILE &&
1380 ch_getflags() & CH_HELPFILE)
1383 * Quit while viewing the help file
1384 * just means return to viewing the
1385 * previous file.
1387 hshift = save_hshift;
1388 if (edit_prev(1) == 0)
1389 break;
1391 if (extra != NULL)
1392 quit(*extra);
1393 quit(QUIT_OK);
1394 break;
1397 * Define abbreviation for a commonly used sequence below.
1399 #define DO_SEARCH() \
1400 if (number <= 0) number = 1; \
1401 mca_search(); \
1402 cmd_exec(); \
1403 multi_search((char *)NULL, (int) number);
1406 case A_F_SEARCH:
1408 * Search forward for a pattern.
1409 * Get the first char of the pattern.
1411 search_type = SRCH_FORW;
1412 if (number <= 0)
1413 number = 1;
1414 mca_search();
1415 c = getcc();
1416 goto again;
1418 case A_B_SEARCH:
1420 * Search backward for a pattern.
1421 * Get the first char of the pattern.
1423 search_type = SRCH_BACK;
1424 if (number <= 0)
1425 number = 1;
1426 mca_search();
1427 c = getcc();
1428 goto again;
1430 case A_FILTER:
1431 #if HILITE_SEARCH
1432 search_type = SRCH_FORW | SRCH_FILTER;
1433 mca_search();
1434 c = getcc();
1435 goto again;
1436 #else
1437 error("Command not available", NULL_PARG);
1438 break;
1439 #endif
1441 case A_AGAIN_SEARCH:
1443 * Repeat previous search.
1445 DO_SEARCH();
1446 break;
1448 case A_T_AGAIN_SEARCH:
1450 * Repeat previous search, multiple files.
1452 search_type |= SRCH_PAST_EOF;
1453 DO_SEARCH();
1454 break;
1456 case A_REVERSE_SEARCH:
1458 * Repeat previous search, in reverse direction.
1460 save_search_type = search_type;
1461 search_type = SRCH_REVERSE(search_type);
1462 DO_SEARCH();
1463 search_type = save_search_type;
1464 break;
1466 case A_T_REVERSE_SEARCH:
1468 * Repeat previous search,
1469 * multiple files in reverse direction.
1471 save_search_type = search_type;
1472 search_type = SRCH_REVERSE(search_type);
1473 search_type |= SRCH_PAST_EOF;
1474 DO_SEARCH();
1475 search_type = save_search_type;
1476 break;
1478 case A_UNDO_SEARCH:
1479 undo_search();
1480 break;
1482 case A_HELP:
1484 * Help.
1486 if (ch_getflags() & CH_HELPFILE)
1487 break;
1488 cmd_exec();
1489 save_hshift = hshift;
1490 hshift = 0;
1491 (void) edit(FAKE_HELPFILE);
1492 break;
1494 case A_EXAMINE:
1495 #if EXAMINE
1497 * Edit a new file. Get the filename.
1499 if (secure)
1501 error("Command not available", NULL_PARG);
1502 break;
1504 start_mca(A_EXAMINE, "Examine: ", ml_examine, 0);
1505 c = getcc();
1506 goto again;
1507 #else
1508 error("Command not available", NULL_PARG);
1509 break;
1510 #endif
1512 case A_VISUAL:
1514 * Invoke an editor on the input file.
1516 #if EDITOR
1517 if (secure)
1519 error("Command not available", NULL_PARG);
1520 break;
1522 if (ch_getflags() & CH_HELPFILE)
1523 break;
1524 if (strcmp(get_filename(curr_ifile), "-") == 0)
1526 error("Cannot edit standard input", NULL_PARG);
1527 break;
1529 if (curr_altfilename != NULL)
1531 error("WARNING: This file was viewed via LESSOPEN",
1532 NULL_PARG);
1534 start_mca(A_SHELL, "!", ml_shell, 0);
1536 * Expand the editor prototype string
1537 * and pass it to the system to execute.
1538 * (Make sure the screen is displayed so the
1539 * expansion of "+%lm" works.)
1541 make_display();
1542 cmd_exec();
1543 lsystem(pr_expand(editproto, 0), (char*)NULL);
1544 break;
1545 #else
1546 error("Command not available", NULL_PARG);
1547 break;
1548 #endif
1550 case A_NEXT_FILE:
1552 * Examine next file.
1554 #if TAGS
1555 if (ntags())
1557 error("No next file", NULL_PARG);
1558 break;
1560 #endif
1561 if (number <= 0)
1562 number = 1;
1563 if (edit_next((int) number))
1565 if (get_quit_at_eof() && eof_displayed() &&
1566 !(ch_getflags() & CH_HELPFILE))
1567 quit(QUIT_OK);
1568 parg.p_string = (number > 1) ? "(N-th) " : "";
1569 error("No %snext file", &parg);
1571 break;
1573 case A_PREV_FILE:
1575 * Examine previous file.
1577 #if TAGS
1578 if (ntags())
1580 error("No previous file", NULL_PARG);
1581 break;
1583 #endif
1584 if (number <= 0)
1585 number = 1;
1586 if (edit_prev((int) number))
1588 parg.p_string = (number > 1) ? "(N-th) " : "";
1589 error("No %sprevious file", &parg);
1591 break;
1593 case A_NEXT_TAG:
1594 #if TAGS
1595 if (number <= 0)
1596 number = 1;
1597 tagfile = nexttag((int) number);
1598 if (tagfile == NULL)
1600 error("No next tag", NULL_PARG);
1601 break;
1603 if (edit(tagfile) == 0)
1605 POSITION pos = tagsearch();
1606 if (pos != NULL_POSITION)
1607 jump_loc(pos, jump_sline);
1609 #else
1610 error("Command not available", NULL_PARG);
1611 #endif
1612 break;
1614 case A_PREV_TAG:
1615 #if TAGS
1616 if (number <= 0)
1617 number = 1;
1618 tagfile = prevtag((int) number);
1619 if (tagfile == NULL)
1621 error("No previous tag", NULL_PARG);
1622 break;
1624 if (edit(tagfile) == 0)
1626 POSITION pos = tagsearch();
1627 if (pos != NULL_POSITION)
1628 jump_loc(pos, jump_sline);
1630 #else
1631 error("Command not available", NULL_PARG);
1632 #endif
1633 break;
1635 case A_INDEX_FILE:
1637 * Examine a particular file.
1639 if (number <= 0)
1640 number = 1;
1641 if (edit_index((int) number))
1642 error("No such file", NULL_PARG);
1643 break;
1645 case A_REMOVE_FILE:
1646 if (ch_getflags() & CH_HELPFILE)
1647 break;
1648 old_ifile = curr_ifile;
1649 new_ifile = getoff_ifile(curr_ifile);
1650 if (new_ifile == NULL_IFILE)
1652 bell();
1653 break;
1655 if (edit_ifile(new_ifile) != 0)
1657 reedit_ifile(old_ifile);
1658 break;
1660 del_ifile(old_ifile);
1661 break;
1663 case A_OPT_TOGGLE:
1664 optflag = OPT_TOGGLE;
1665 optgetname = FALSE;
1666 mca_opt_toggle();
1667 c = getcc();
1668 goto again;
1670 case A_DISP_OPTION:
1672 * Report a flag setting.
1674 optflag = OPT_NO_TOGGLE;
1675 optgetname = FALSE;
1676 mca_opt_toggle();
1677 c = getcc();
1678 goto again;
1680 case A_FIRSTCMD:
1682 * Set an initial command for new files.
1684 start_mca(A_FIRSTCMD, "+", (void*)NULL, 0);
1685 c = getcc();
1686 goto again;
1688 case A_SHELL:
1690 * Shell escape.
1692 #if SHELL_ESCAPE
1693 if (secure)
1695 error("Command not available", NULL_PARG);
1696 break;
1698 start_mca(A_SHELL, "!", ml_shell, 0);
1699 c = getcc();
1700 goto again;
1701 #else
1702 error("Command not available", NULL_PARG);
1703 break;
1704 #endif
1706 case A_SETMARK:
1708 * Set a mark.
1710 if (ch_getflags() & CH_HELPFILE)
1711 break;
1712 start_mca(A_SETMARK, "mark: ", (void*)NULL, 0);
1713 c = getcc();
1714 if (c == erase_char || c == erase2_char ||
1715 c == kill_char || c == '\n' || c == '\r')
1716 break;
1717 setmark(c);
1718 break;
1720 case A_GOMARK:
1722 * Go to a mark.
1724 start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0);
1725 c = getcc();
1726 if (c == erase_char || c == erase2_char ||
1727 c == kill_char || c == '\n' || c == '\r')
1728 break;
1729 cmd_exec();
1730 gomark(c);
1731 break;
1733 case A_PIPE:
1734 #if PIPEC
1735 if (secure)
1737 error("Command not available", NULL_PARG);
1738 break;
1740 start_mca(A_PIPE, "|mark: ", (void*)NULL, 0);
1741 c = getcc();
1742 if (c == erase_char || c == erase2_char || c == kill_char)
1743 break;
1744 if (c == '\n' || c == '\r')
1745 c = '.';
1746 if (badmark(c))
1747 break;
1748 pipec = c;
1749 start_mca(A_PIPE, "!", ml_shell, 0);
1750 c = getcc();
1751 goto again;
1752 #else
1753 error("Command not available", NULL_PARG);
1754 break;
1755 #endif
1757 case A_B_BRACKET:
1758 case A_F_BRACKET:
1759 start_mca(action, "Brackets: ", (void*)NULL, 0);
1760 c = getcc();
1761 goto again;
1763 case A_LSHIFT:
1764 if (number > 0)
1765 shift_count = number;
1766 else
1767 number = (shift_count > 0) ?
1768 shift_count : sc_width / 2;
1769 if (number > hshift)
1770 number = hshift;
1771 hshift -= number;
1772 screen_trashed = 1;
1773 break;
1775 case A_RSHIFT:
1776 if (number > 0)
1777 shift_count = number;
1778 else
1779 number = (shift_count > 0) ?
1780 shift_count : sc_width / 2;
1781 hshift += number;
1782 screen_trashed = 1;
1783 break;
1785 case A_PREFIX:
1787 * The command is incomplete (more chars are needed).
1788 * Display the current char, so the user knows
1789 * what's going on, and get another character.
1791 if (mca != A_PREFIX)
1793 cmd_reset();
1794 start_mca(A_PREFIX, " ", (void*)NULL,
1795 CF_QUIT_ON_ERASE);
1796 (void) cmd_char(c);
1798 c = getcc();
1799 goto again;
1801 case A_NOACTION:
1802 break;
1804 default:
1805 if (be_helpful)
1806 helpprompt = 1;
1807 else
1808 bell();
1809 break;