ktrace - Add kern.ktrace_suid, allowing suid/sgid execs to be ktraced.
[dragonfly.git] / contrib / less / command.c
blob3b15b1e444dc3c16a1ad4c5d609b16ce109cca10
1 /*
2 * Copyright (C) 1984-2008 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
9 */
13 * User-level command processor.
16 #include "less.h"
17 #if MSDOS_COMPILER==WIN32C
18 #include <windows.h>
19 #endif
20 #include "position.h"
21 #include "option.h"
22 #include "cmd.h"
24 extern int erase_char, erase2_char, kill_char;
25 extern int sigs;
26 extern int quit_if_one_screen;
27 extern int squished;
28 extern int sc_width;
29 extern int sc_height;
30 extern int swindow;
31 extern int jump_sline;
32 extern int quitting;
33 extern int wscroll;
34 extern int top_scroll;
35 extern int ignore_eoi;
36 extern int secure;
37 extern int hshift;
38 extern int show_attn;
39 extern char *every_first_cmd;
40 extern char *curr_altfilename;
41 extern char version[];
42 extern struct scrpos initial_scrpos;
43 extern IFILE curr_ifile;
44 extern void constant *ml_search;
45 extern void constant *ml_examine;
46 #if SHELL_ESCAPE || PIPEC
47 extern void constant *ml_shell;
48 #endif
49 #if EDITOR
50 extern char *editor;
51 extern char *editproto;
52 #endif
53 extern int screen_trashed; /* The screen has been overwritten */
54 extern int shift_count;
55 extern int oldbot;
56 extern int forw_prompt;
58 static char ungot[UNGOT_SIZE];
59 static char *ungotp = NULL;
60 #if SHELL_ESCAPE
61 static char *shellcmd = NULL; /* For holding last shell command for "!!" */
62 #endif
63 static int mca; /* The multicharacter command (action) */
64 static int search_type; /* The previous type of search */
65 static LINENUM number; /* The number typed by the user */
66 static long fraction; /* The fractional part of the number */
67 static char optchar;
68 static int optflag;
69 static int optgetname;
70 static POSITION bottompos;
71 static int save_hshift;
72 #if PIPEC
73 static char pipec;
74 #endif
76 static void multi_search();
79 * Move the cursor to start of prompt line before executing a command.
80 * This looks nicer if the command takes a long time before
81 * updating the screen.
83 static void
84 cmd_exec()
86 #if HILITE_SEARCH
87 clear_attn();
88 #endif
89 clear_bot();
90 flush();
94 * Set up the display to start a new multi-character command.
96 static void
97 start_mca(action, prompt, mlist, cmdflags)
98 int action;
99 char *prompt;
100 void *mlist;
101 int cmdflags;
103 mca = action;
104 clear_bot();
105 clear_cmd();
106 cmd_putstr(prompt);
107 set_mlist(mlist, cmdflags);
110 public int
111 in_mca()
113 return (mca != 0 && mca != A_PREFIX);
117 * Set up the display to start a new search command.
119 static void
120 mca_search()
122 #if HILITE_SEARCH
123 if (search_type & SRCH_FILTER)
124 mca = A_FILTER;
125 else
126 #endif
127 if (search_type & SRCH_FORW)
128 mca = A_F_SEARCH;
129 else
130 mca = A_B_SEARCH;
132 clear_bot();
133 clear_cmd();
135 if (search_type & SRCH_NO_MATCH)
136 cmd_putstr("Non-match ");
137 if (search_type & SRCH_FIRST_FILE)
138 cmd_putstr("First-file ");
139 if (search_type & SRCH_PAST_EOF)
140 cmd_putstr("EOF-ignore ");
141 if (search_type & SRCH_NO_MOVE)
142 cmd_putstr("Keep-pos ");
143 if (search_type & SRCH_NO_REGEX)
144 cmd_putstr("Regex-off ");
146 #if HILITE_SEARCH
147 if (search_type & SRCH_FILTER)
148 cmd_putstr("&/");
149 else
150 #endif
151 if (search_type & SRCH_FORW)
152 cmd_putstr("/");
153 else
154 cmd_putstr("?");
155 set_mlist(ml_search, 0);
159 * Set up the display to start a new toggle-option command.
161 static void
162 mca_opt_toggle()
164 int no_prompt;
165 int flag;
166 char *dash;
168 no_prompt = (optflag & OPT_NO_PROMPT);
169 flag = (optflag & ~OPT_NO_PROMPT);
170 dash = (flag == OPT_NO_TOGGLE) ? "_" : "-";
172 mca = A_OPT_TOGGLE;
173 clear_bot();
174 clear_cmd();
175 cmd_putstr(dash);
176 if (optgetname)
177 cmd_putstr(dash);
178 if (no_prompt)
179 cmd_putstr("(P)");
180 switch (flag)
182 case OPT_UNSET:
183 cmd_putstr("+");
184 break;
185 case OPT_SET:
186 cmd_putstr("!");
187 break;
189 set_mlist(NULL, 0);
193 * Execute a multicharacter command.
195 static void
196 exec_mca()
198 register char *cbuf;
200 cmd_exec();
201 cbuf = get_cmdbuf();
203 switch (mca)
205 case A_F_SEARCH:
206 case A_B_SEARCH:
207 multi_search(cbuf, (int) number);
208 break;
209 #if HILITE_SEARCH
210 case A_FILTER:
211 search_type ^= SRCH_NO_MATCH;
212 set_filter_pattern(cbuf, search_type);
213 break;
214 #endif
215 case A_FIRSTCMD:
217 * Skip leading spaces or + signs in the string.
219 while (*cbuf == '+' || *cbuf == ' ')
220 cbuf++;
221 if (every_first_cmd != NULL)
222 free(every_first_cmd);
223 if (*cbuf == '\0')
224 every_first_cmd = NULL;
225 else
226 every_first_cmd = save(cbuf);
227 break;
228 case A_OPT_TOGGLE:
229 toggle_option(optchar, cbuf, optflag);
230 optchar = '\0';
231 break;
232 case A_F_BRACKET:
233 match_brac(cbuf[0], cbuf[1], 1, (int) number);
234 break;
235 case A_B_BRACKET:
236 match_brac(cbuf[1], cbuf[0], 0, (int) number);
237 break;
238 #if EXAMINE
239 case A_EXAMINE:
240 if (secure)
241 break;
242 edit_list(cbuf);
243 #if TAGS
244 /* If tag structure is loaded then clean it up. */
245 cleantags();
246 #endif
247 break;
248 #endif
249 #if SHELL_ESCAPE
250 case A_SHELL:
252 * !! just uses whatever is in shellcmd.
253 * Otherwise, copy cmdbuf to shellcmd,
254 * expanding any special characters ("%" or "#").
256 if (*cbuf != '!')
258 if (shellcmd != NULL)
259 free(shellcmd);
260 shellcmd = fexpand(cbuf);
263 if (secure)
264 break;
265 if (shellcmd == NULL)
266 lsystem("", "!done");
267 else
268 lsystem(shellcmd, "!done");
269 break;
270 #endif
271 #if PIPEC
272 case A_PIPE:
273 if (secure)
274 break;
275 (void) pipe_mark(pipec, cbuf);
276 error("|done", NULL_PARG);
277 break;
278 #endif
283 * Add a character to a multi-character command.
285 static int
286 mca_char(c)
287 int c;
289 char *p;
290 int flag;
291 char buf[3];
292 PARG parg;
294 switch (mca)
296 case 0:
298 * Not in a multicharacter command.
300 return (NO_MCA);
302 case A_PREFIX:
304 * In the prefix of a command.
305 * This not considered a multichar command
306 * (even tho it uses cmdbuf, etc.).
307 * It is handled in the commands() switch.
309 return (NO_MCA);
311 case A_DIGIT:
313 * Entering digits of a number.
314 * Terminated by a non-digit.
316 if (!((c >= '0' && c <= '9') || c == '.') &&
317 editchar(c, EC_PEEK|EC_NOHISTORY|EC_NOCOMPLETE|EC_NORIGHTLEFT) == A_INVALID)
320 * Not part of the number.
321 * Treat as a normal command character.
323 number = cmd_int(&fraction);
324 mca = 0;
325 cmd_accept();
326 return (NO_MCA);
328 break;
330 case A_OPT_TOGGLE:
332 * Special case for the TOGGLE_OPTION command.
333 * If the option letter which was entered is a
334 * single-char option, execute the command immediately,
335 * so user doesn't have to hit RETURN.
336 * If the first char is + or -, this indicates
337 * OPT_UNSET or OPT_SET respectively, instead of OPT_TOGGLE.
338 * "--" begins inputting a long option name.
340 if (optchar == '\0' && len_cmdbuf() == 0)
342 flag = (optflag & ~OPT_NO_PROMPT);
343 if (flag == OPT_NO_TOGGLE)
345 switch (c)
347 case '_':
348 /* "__" = long option name. */
349 optgetname = TRUE;
350 mca_opt_toggle();
351 return (MCA_MORE);
353 } else
355 switch (c)
357 case '+':
358 /* "-+" = UNSET. */
359 optflag = (flag == OPT_UNSET) ?
360 OPT_TOGGLE : OPT_UNSET;
361 mca_opt_toggle();
362 return (MCA_MORE);
363 case '!':
364 /* "-!" = SET */
365 optflag = (flag == OPT_SET) ?
366 OPT_TOGGLE : OPT_SET;
367 mca_opt_toggle();
368 return (MCA_MORE);
369 case CONTROL('P'):
370 optflag ^= OPT_NO_PROMPT;
371 mca_opt_toggle();
372 return (MCA_MORE);
373 case '-':
374 /* "--" = long option name. */
375 optgetname = TRUE;
376 mca_opt_toggle();
377 return (MCA_MORE);
381 if (optgetname)
384 * We're getting a long option name.
385 * See if we've matched an option name yet.
386 * If so, display the complete name and stop
387 * accepting chars until user hits RETURN.
389 struct loption *o;
390 char *oname;
391 int lc;
393 if (c == '\n' || c == '\r')
396 * When the user hits RETURN, make sure
397 * we've matched an option name, then
398 * pretend he just entered the equivalent
399 * option letter.
401 if (optchar == '\0')
403 parg.p_string = get_cmdbuf();
404 error("There is no --%s option", &parg);
405 return (MCA_DONE);
407 optgetname = FALSE;
408 cmd_reset();
409 c = optchar;
410 } else
412 if (optchar != '\0')
415 * Already have a match for the name.
416 * Don't accept anything but erase/kill.
418 if (c == erase_char ||
419 c == erase2_char ||
420 c == kill_char)
421 return (MCA_DONE);
422 return (MCA_MORE);
425 * Add char to cmd buffer and try to match
426 * the option name.
428 if (cmd_char(c) == CC_QUIT)
429 return (MCA_DONE);
430 p = get_cmdbuf();
431 lc = ASCII_IS_LOWER(p[0]);
432 o = findopt_name(&p, &oname, NULL);
433 if (o != NULL)
436 * Got a match.
437 * Remember the option letter and
438 * display the full option name.
440 optchar = o->oletter;
441 if (!lc && ASCII_IS_LOWER(optchar))
442 optchar = ASCII_TO_UPPER(optchar);
443 cmd_reset();
444 mca_opt_toggle();
445 for (p = oname; *p != '\0'; p++)
447 c = *p;
448 if (!lc && ASCII_IS_LOWER(c))
449 c = ASCII_TO_UPPER(c);
450 if (cmd_char(c) != CC_OK)
451 return (MCA_DONE);
454 return (MCA_MORE);
456 } else
458 if (c == erase_char || c == erase2_char || c == kill_char)
459 break;
460 if (optchar != '\0')
461 /* We already have the option letter. */
462 break;
465 optchar = c;
466 if ((optflag & ~OPT_NO_PROMPT) != OPT_TOGGLE ||
467 single_char_option(c))
469 toggle_option(c, "", optflag);
470 return (MCA_DONE);
473 * Display a prompt appropriate for the option letter.
475 if ((p = opt_prompt(c)) == NULL)
477 buf[0] = '-';
478 buf[1] = c;
479 buf[2] = '\0';
480 p = buf;
482 start_mca(A_OPT_TOGGLE, p, (void*)NULL, 0);
483 return (MCA_MORE);
485 case A_F_SEARCH:
486 case A_B_SEARCH:
487 case A_FILTER:
489 * Special case for search commands.
490 * Certain characters as the first char of
491 * the pattern have special meaning:
492 * ! Toggle the NO_MATCH flag
493 * * Toggle the PAST_EOF flag
494 * @ Toggle the FIRST_FILE flag
496 if (len_cmdbuf() > 0)
498 * Only works for the first char of the pattern.
500 break;
502 flag = 0;
503 switch (c)
505 case CONTROL('E'): /* ignore END of file */
506 case '*':
507 if (mca != A_FILTER)
508 flag = SRCH_PAST_EOF;
509 break;
510 case CONTROL('F'): /* FIRST file */
511 case '@':
512 if (mca != A_FILTER)
513 flag = SRCH_FIRST_FILE;
514 break;
515 case CONTROL('K'): /* KEEP position */
516 if (mca != A_FILTER)
517 flag = SRCH_NO_MOVE;
518 break;
519 case CONTROL('R'): /* Don't use REGULAR EXPRESSIONS */
520 flag = SRCH_NO_REGEX;
521 break;
522 case CONTROL('N'): /* NOT match */
523 case '!':
524 flag = SRCH_NO_MATCH;
525 break;
527 if (flag != 0)
529 search_type ^= flag;
530 mca_search();
531 return (MCA_MORE);
533 break;
537 * Any other multicharacter command
538 * is terminated by a newline.
540 if (c == '\n' || c == '\r')
543 * Execute the command.
545 exec_mca();
546 return (MCA_DONE);
550 * Append the char to the command buffer.
552 if (cmd_char(c) == CC_QUIT)
554 * Abort the multi-char command.
556 return (MCA_DONE);
558 if ((mca == A_F_BRACKET || mca == A_B_BRACKET) && len_cmdbuf() >= 2)
561 * Special case for the bracket-matching commands.
562 * Execute the command after getting exactly two
563 * characters from the user.
565 exec_mca();
566 return (MCA_DONE);
570 * Need another character.
572 return (MCA_MORE);
576 * Discard any buffered file data.
578 static void
579 clear_buffers()
581 if (!(ch_getflags() & CH_CANSEEK))
582 return;
583 ch_flush();
584 clr_linenum();
585 #if HILITE_SEARCH
586 clr_hilite();
587 #endif
591 * Make sure the screen is displayed.
593 static void
594 make_display()
597 * If nothing is displayed yet, display starting from initial_scrpos.
599 if (empty_screen())
601 if (initial_scrpos.pos == NULL_POSITION)
603 * {{ Maybe this should be:
604 * jump_loc(ch_zero(), jump_sline);
605 * but this behavior seems rather unexpected
606 * on the first screen. }}
608 jump_loc(ch_zero(), 1);
609 else
610 jump_loc(initial_scrpos.pos, initial_scrpos.ln);
611 } else if (screen_trashed)
613 int save_top_scroll = top_scroll;
614 int save_ignore_eoi = ignore_eoi;
615 top_scroll = 1;
616 ignore_eoi = 0;
617 if (screen_trashed == 2)
619 /* Special case used by ignore_eoi: re-open the input file
620 * and jump to the end of the file. */
621 reopen_curr_ifile();
622 jump_forw();
624 repaint();
625 top_scroll = save_top_scroll;
626 ignore_eoi = save_ignore_eoi;
631 * Display the appropriate prompt.
633 static void
634 prompt()
636 register char *p;
638 if (ungotp != NULL && ungotp > ungot)
641 * No prompt necessary if commands are from
642 * ungotten chars rather than from the user.
644 return;
648 * Make sure the screen is displayed.
650 make_display();
651 bottompos = position(BOTTOM_PLUS_ONE);
654 * If we've hit EOF on the last file and the -E flag is set, quit.
656 if (get_quit_at_eof() == OPT_ONPLUS &&
657 eof_displayed() && !(ch_getflags() & CH_HELPFILE) &&
658 next_ifile(curr_ifile) == NULL_IFILE)
659 quit(QUIT_OK);
662 * If the entire file is displayed and the -F flag is set, quit.
664 if (quit_if_one_screen &&
665 entire_file_displayed() && !(ch_getflags() & CH_HELPFILE) &&
666 next_ifile(curr_ifile) == NULL_IFILE)
667 quit(QUIT_OK);
669 #if MSDOS_COMPILER==WIN32C
671 * In Win32, display the file name in the window title.
673 if (!(ch_getflags() & CH_HELPFILE))
674 SetConsoleTitle(pr_expand("Less?f - %f.", 0));
675 #endif
677 * Select the proper prompt and display it.
680 * If the previous action was a forward movement,
681 * don't clear the bottom line of the display;
682 * just print the prompt since the forward movement guarantees
683 * that we're in the right position to display the prompt.
684 * Clearing the line could cause a problem: for example, if the last
685 * line displayed ended at the right screen edge without a newline,
686 * then clearing would clear the last displayed line rather than
687 * the prompt line.
689 if (!forw_prompt)
690 clear_bot();
691 clear_cmd();
692 forw_prompt = 0;
693 p = pr_string();
694 if (is_filtering())
695 putstr("& ");
696 if (p == NULL || *p == '\0')
697 putchr(':');
698 else
700 at_enter(AT_STANDOUT);
701 putstr(p);
702 at_exit();
704 clear_eol();
708 * Display the less version message.
710 public void
711 dispversion()
713 PARG parg;
715 parg.p_string = version;
716 error("less %s", &parg);
720 * Get command character.
721 * The character normally comes from the keyboard,
722 * but may come from ungotten characters
723 * (characters previously given to ungetcc or ungetsc).
725 public int
726 getcc()
728 if (ungotp == NULL)
730 * Normal case: no ungotten chars, so get one from the user.
732 return (getchr());
734 if (ungotp > ungot)
736 * Return the next ungotten char.
738 return (*--ungotp);
741 * We have just run out of ungotten chars.
743 ungotp = NULL;
744 if (len_cmdbuf() == 0 || !empty_screen())
745 return (getchr());
747 * Command is incomplete, so try to complete it.
749 switch (mca)
751 case A_DIGIT:
753 * We have a number but no command. Treat as #g.
755 return ('g');
757 case A_F_SEARCH:
758 case A_B_SEARCH:
760 * We have "/string" but no newline. Add the \n.
762 return ('\n');
764 default:
766 * Some other incomplete command. Let user complete it.
768 return (getchr());
773 * "Unget" a command character.
774 * The next getcc() will return this character.
776 public void
777 ungetcc(c)
778 int c;
780 if (ungotp == NULL)
781 ungotp = ungot;
782 if (ungotp >= ungot + sizeof(ungot))
784 error("ungetcc overflow", NULL_PARG);
785 quit(QUIT_ERROR);
787 *ungotp++ = c;
791 * Unget a whole string of command characters.
792 * The next sequence of getcc()'s will return this string.
794 public void
795 ungetsc(s)
796 char *s;
798 register char *p;
800 for (p = s + strlen(s) - 1; p >= s; p--)
801 ungetcc(*p);
805 * Search for a pattern, possibly in multiple files.
806 * If SRCH_FIRST_FILE is set, begin searching at the first file.
807 * If SRCH_PAST_EOF is set, continue the search thru multiple files.
809 static void
810 multi_search(pattern, n)
811 char *pattern;
812 int n;
814 register int nomore;
815 IFILE save_ifile;
816 int changed_file;
818 changed_file = 0;
819 save_ifile = save_curr_ifile();
821 if (search_type & SRCH_FIRST_FILE)
824 * Start at the first (or last) file
825 * in the command line list.
827 if (search_type & SRCH_FORW)
828 nomore = edit_first();
829 else
830 nomore = edit_last();
831 if (nomore)
833 unsave_ifile(save_ifile);
834 return;
836 changed_file = 1;
837 search_type &= ~SRCH_FIRST_FILE;
840 for (;;)
842 n = search(search_type, pattern, n);
844 * The SRCH_NO_MOVE flag doesn't "stick": it gets cleared
845 * after being used once. This allows "n" to work after
846 * using a /@@ search.
848 search_type &= ~SRCH_NO_MOVE;
849 if (n == 0)
852 * Found it.
854 unsave_ifile(save_ifile);
855 return;
858 if (n < 0)
860 * Some kind of error in the search.
861 * Error message has been printed by search().
863 break;
865 if ((search_type & SRCH_PAST_EOF) == 0)
867 * We didn't find a match, but we're
868 * supposed to search only one file.
870 break;
872 * Move on to the next file.
874 if (search_type & SRCH_FORW)
875 nomore = edit_next(1);
876 else
877 nomore = edit_prev(1);
878 if (nomore)
879 break;
880 changed_file = 1;
884 * Didn't find it.
885 * Print an error message if we haven't already.
887 if (n > 0)
888 error("Pattern not found", NULL_PARG);
890 if (changed_file)
893 * Restore the file we were originally viewing.
895 reedit_ifile(save_ifile);
896 } else
898 unsave_ifile(save_ifile);
903 * Main command processor.
904 * Accept and execute commands until a quit command.
906 public void
907 commands()
909 register int c;
910 register int action;
911 register char *cbuf;
912 int newaction;
913 int save_search_type;
914 char *extra;
915 char tbuf[2];
916 PARG parg;
917 IFILE old_ifile;
918 IFILE new_ifile;
919 char *tagfile;
921 search_type = SRCH_FORW;
922 wscroll = (sc_height + 1) / 2;
923 newaction = A_NOACTION;
925 for (;;)
927 mca = 0;
928 cmd_accept();
929 number = 0;
930 optchar = '\0';
933 * See if any signals need processing.
935 if (sigs)
937 psignals();
938 if (quitting)
939 quit(QUIT_SAVED_STATUS);
943 * See if window size changed, for systems that don't
944 * generate SIGWINCH.
946 check_winch();
949 * Display prompt and accept a character.
951 cmd_reset();
952 prompt();
953 if (sigs)
954 continue;
955 if (newaction == A_NOACTION)
956 c = getcc();
958 again:
959 if (sigs)
960 continue;
962 if (newaction != A_NOACTION)
964 action = newaction;
965 newaction = A_NOACTION;
966 } else
969 * If we are in a multicharacter command, call mca_char.
970 * Otherwise we call fcmd_decode to determine the
971 * action to be performed.
973 if (mca)
974 switch (mca_char(c))
976 case MCA_MORE:
978 * Need another character.
980 c = getcc();
981 goto again;
982 case MCA_DONE:
984 * Command has been handled by mca_char.
985 * Start clean with a prompt.
987 continue;
988 case NO_MCA:
990 * Not a multi-char command
991 * (at least, not anymore).
993 break;
997 * Decode the command character and decide what to do.
999 if (mca)
1002 * We're in a multichar command.
1003 * Add the character to the command buffer
1004 * and display it on the screen.
1005 * If the user backspaces past the start
1006 * of the line, abort the command.
1008 if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0)
1009 continue;
1010 cbuf = get_cmdbuf();
1011 } else
1014 * Don't use cmd_char if we're starting fresh
1015 * at the beginning of a command, because we
1016 * don't want to echo the command until we know
1017 * it is a multichar command. We also don't
1018 * want erase_char/kill_char to be treated
1019 * as line editing characters.
1021 tbuf[0] = c;
1022 tbuf[1] = '\0';
1023 cbuf = tbuf;
1025 extra = NULL;
1026 action = fcmd_decode(cbuf, &extra);
1028 * If an "extra" string was returned,
1029 * process it as a string of command characters.
1031 if (extra != NULL)
1032 ungetsc(extra);
1035 * Clear the cmdbuf string.
1036 * (But not if we're in the prefix of a command,
1037 * because the partial command string is kept there.)
1039 if (action != A_PREFIX)
1040 cmd_reset();
1042 switch (action)
1044 case A_DIGIT:
1046 * First digit of a number.
1048 start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE);
1049 goto again;
1051 case A_F_WINDOW:
1053 * Forward one window (and set the window size).
1055 if (number > 0)
1056 swindow = (int) number;
1057 /* FALLTHRU */
1058 case A_F_SCREEN:
1060 * Forward one screen.
1062 if (number <= 0)
1063 number = get_swindow();
1064 cmd_exec();
1065 if (show_attn)
1066 set_attnpos(bottompos);
1067 forward((int) number, 0, 1);
1068 break;
1070 case A_B_WINDOW:
1072 * Backward one window (and set the window size).
1074 if (number > 0)
1075 swindow = (int) number;
1076 /* FALLTHRU */
1077 case A_B_SCREEN:
1079 * Backward one screen.
1081 if (number <= 0)
1082 number = get_swindow();
1083 cmd_exec();
1084 backward((int) number, 0, 1);
1085 break;
1087 case A_F_LINE:
1089 * Forward N (default 1) line.
1091 if (number <= 0)
1092 number = 1;
1093 cmd_exec();
1094 if (show_attn == OPT_ONPLUS && number > 1)
1095 set_attnpos(bottompos);
1096 forward((int) number, 0, 0);
1097 break;
1099 case A_B_LINE:
1101 * Backward N (default 1) line.
1103 if (number <= 0)
1104 number = 1;
1105 cmd_exec();
1106 backward((int) number, 0, 0);
1107 break;
1109 case A_FF_LINE:
1111 * Force forward N (default 1) line.
1113 if (number <= 0)
1114 number = 1;
1115 cmd_exec();
1116 if (show_attn == OPT_ONPLUS && number > 1)
1117 set_attnpos(bottompos);
1118 forward((int) number, 1, 0);
1119 break;
1121 case A_BF_LINE:
1123 * Force backward N (default 1) line.
1125 if (number <= 0)
1126 number = 1;
1127 cmd_exec();
1128 backward((int) number, 1, 0);
1129 break;
1131 case A_FF_SCREEN:
1133 * Force forward one screen.
1135 if (number <= 0)
1136 number = get_swindow();
1137 cmd_exec();
1138 if (show_attn == OPT_ONPLUS)
1139 set_attnpos(bottompos);
1140 forward((int) number, 1, 0);
1141 break;
1143 case A_F_FOREVER:
1145 * Forward forever, ignoring EOF.
1147 if (ch_getflags() & CH_HELPFILE)
1148 break;
1149 cmd_exec();
1150 jump_forw();
1151 ignore_eoi = 1;
1152 while (!sigs)
1154 make_display();
1155 forward(1, 0, 0);
1157 ignore_eoi = 0;
1159 * This gets us back in "F mode" after processing
1160 * a non-abort signal (e.g. window-change).
1162 if (sigs && !ABORT_SIGS())
1163 newaction = A_F_FOREVER;
1164 break;
1166 case A_F_SCROLL:
1168 * Forward N lines
1169 * (default same as last 'd' or 'u' command).
1171 if (number > 0)
1172 wscroll = (int) number;
1173 cmd_exec();
1174 if (show_attn == OPT_ONPLUS)
1175 set_attnpos(bottompos);
1176 forward(wscroll, 0, 0);
1177 break;
1179 case A_B_SCROLL:
1181 * Forward N lines
1182 * (default same as last 'd' or 'u' command).
1184 if (number > 0)
1185 wscroll = (int) number;
1186 cmd_exec();
1187 backward(wscroll, 0, 0);
1188 break;
1190 case A_FREPAINT:
1192 * Flush buffers, then repaint screen.
1193 * Don't flush the buffers on a pipe!
1195 clear_buffers();
1196 /* FALLTHRU */
1197 case A_REPAINT:
1199 * Repaint screen.
1201 cmd_exec();
1202 repaint();
1203 break;
1205 case A_GOLINE:
1207 * Go to line N, default beginning of file.
1209 if (number <= 0)
1210 number = 1;
1211 cmd_exec();
1212 jump_back(number);
1213 break;
1215 case A_PERCENT:
1217 * Go to a specified percentage into the file.
1219 if (number < 0)
1221 number = 0;
1222 fraction = 0;
1224 if (number > 100)
1226 number = 100;
1227 fraction = 0;
1229 cmd_exec();
1230 jump_percent((int) number, fraction);
1231 break;
1233 case A_GOEND:
1235 * Go to line N, default end of file.
1237 cmd_exec();
1238 if (number <= 0)
1239 jump_forw();
1240 else
1241 jump_back(number);
1242 break;
1244 case A_GOPOS:
1246 * Go to a specified byte position in the file.
1248 cmd_exec();
1249 if (number < 0)
1250 number = 0;
1251 jump_line_loc((POSITION) number, jump_sline);
1252 break;
1254 case A_STAT:
1256 * Print file name, etc.
1258 if (ch_getflags() & CH_HELPFILE)
1259 break;
1260 cmd_exec();
1261 parg.p_string = eq_message();
1262 error("%s", &parg);
1263 break;
1265 case A_VERSION:
1267 * Print version number, without the "@(#)".
1269 cmd_exec();
1270 dispversion();
1271 break;
1273 case A_QUIT:
1275 * Exit.
1277 if (curr_ifile != NULL_IFILE &&
1278 ch_getflags() & CH_HELPFILE)
1281 * Quit while viewing the help file
1282 * just means return to viewing the
1283 * previous file.
1285 hshift = save_hshift;
1286 if (edit_prev(1) == 0)
1287 break;
1289 if (extra != NULL)
1290 quit(*extra);
1291 quit(QUIT_OK);
1292 break;
1295 * Define abbreviation for a commonly used sequence below.
1297 #define DO_SEARCH() \
1298 if (number <= 0) number = 1; \
1299 mca_search(); \
1300 cmd_exec(); \
1301 multi_search((char *)NULL, (int) number);
1304 case A_F_SEARCH:
1306 * Search forward for a pattern.
1307 * Get the first char of the pattern.
1309 search_type = SRCH_FORW;
1310 if (number <= 0)
1311 number = 1;
1312 mca_search();
1313 c = getcc();
1314 goto again;
1316 case A_B_SEARCH:
1318 * Search backward for a pattern.
1319 * Get the first char of the pattern.
1321 search_type = SRCH_BACK;
1322 if (number <= 0)
1323 number = 1;
1324 mca_search();
1325 c = getcc();
1326 goto again;
1328 case A_FILTER:
1329 #if HILITE_SEARCH
1330 search_type = SRCH_FORW | SRCH_FILTER;
1331 mca_search();
1332 c = getcc();
1333 goto again;
1334 #else
1335 error("Command not available", NULL_PARG);
1336 break;
1337 #endif
1339 case A_AGAIN_SEARCH:
1341 * Repeat previous search.
1343 DO_SEARCH();
1344 break;
1346 case A_T_AGAIN_SEARCH:
1348 * Repeat previous search, multiple files.
1350 search_type |= SRCH_PAST_EOF;
1351 DO_SEARCH();
1352 break;
1354 case A_REVERSE_SEARCH:
1356 * Repeat previous search, in reverse direction.
1358 save_search_type = search_type;
1359 search_type = SRCH_REVERSE(search_type);
1360 DO_SEARCH();
1361 search_type = save_search_type;
1362 break;
1364 case A_T_REVERSE_SEARCH:
1366 * Repeat previous search,
1367 * multiple files in reverse direction.
1369 save_search_type = search_type;
1370 search_type = SRCH_REVERSE(search_type);
1371 search_type |= SRCH_PAST_EOF;
1372 DO_SEARCH();
1373 search_type = save_search_type;
1374 break;
1376 case A_UNDO_SEARCH:
1377 undo_search();
1378 break;
1380 case A_HELP:
1382 * Help.
1384 if (ch_getflags() & CH_HELPFILE)
1385 break;
1386 cmd_exec();
1387 save_hshift = hshift;
1388 hshift = 0;
1389 (void) edit(FAKE_HELPFILE);
1390 break;
1392 case A_EXAMINE:
1393 #if EXAMINE
1395 * Edit a new file. Get the filename.
1397 if (secure)
1399 error("Command not available", NULL_PARG);
1400 break;
1402 start_mca(A_EXAMINE, "Examine: ", ml_examine, 0);
1403 c = getcc();
1404 goto again;
1405 #else
1406 error("Command not available", NULL_PARG);
1407 break;
1408 #endif
1410 case A_VISUAL:
1412 * Invoke an editor on the input file.
1414 #if EDITOR
1415 if (secure)
1417 error("Command not available", NULL_PARG);
1418 break;
1420 if (ch_getflags() & CH_HELPFILE)
1421 break;
1422 if (strcmp(get_filename(curr_ifile), "-") == 0)
1424 error("Cannot edit standard input", NULL_PARG);
1425 break;
1427 if (curr_altfilename != NULL)
1429 error("WARNING: This file was viewed via LESSOPEN",
1430 NULL_PARG);
1432 start_mca(A_SHELL, "!", ml_shell, 0);
1434 * Expand the editor prototype string
1435 * and pass it to the system to execute.
1436 * (Make sure the screen is displayed so the
1437 * expansion of "+%lm" works.)
1439 make_display();
1440 cmd_exec();
1441 lsystem(pr_expand(editproto, 0), (char*)NULL);
1442 break;
1443 #else
1444 error("Command not available", NULL_PARG);
1445 break;
1446 #endif
1448 case A_NEXT_FILE:
1450 * Examine next file.
1452 #if TAGS
1453 if (ntags())
1455 error("No next file", NULL_PARG);
1456 break;
1458 #endif
1459 if (number <= 0)
1460 number = 1;
1461 if (edit_next((int) number))
1463 if (get_quit_at_eof() && eof_displayed() &&
1464 !(ch_getflags() & CH_HELPFILE))
1465 quit(QUIT_OK);
1466 parg.p_string = (number > 1) ? "(N-th) " : "";
1467 error("No %snext file", &parg);
1469 break;
1471 case A_PREV_FILE:
1473 * Examine previous file.
1475 #if TAGS
1476 if (ntags())
1478 error("No previous file", NULL_PARG);
1479 break;
1481 #endif
1482 if (number <= 0)
1483 number = 1;
1484 if (edit_prev((int) number))
1486 parg.p_string = (number > 1) ? "(N-th) " : "";
1487 error("No %sprevious file", &parg);
1489 break;
1491 case A_NEXT_TAG:
1492 #if TAGS
1493 if (number <= 0)
1494 number = 1;
1495 tagfile = nexttag((int) number);
1496 if (tagfile == NULL)
1498 error("No next tag", NULL_PARG);
1499 break;
1501 if (edit(tagfile) == 0)
1503 POSITION pos = tagsearch();
1504 if (pos != NULL_POSITION)
1505 jump_loc(pos, jump_sline);
1507 #else
1508 error("Command not available", NULL_PARG);
1509 #endif
1510 break;
1512 case A_PREV_TAG:
1513 #if TAGS
1514 if (number <= 0)
1515 number = 1;
1516 tagfile = prevtag((int) number);
1517 if (tagfile == NULL)
1519 error("No previous tag", NULL_PARG);
1520 break;
1522 if (edit(tagfile) == 0)
1524 POSITION pos = tagsearch();
1525 if (pos != NULL_POSITION)
1526 jump_loc(pos, jump_sline);
1528 #else
1529 error("Command not available", NULL_PARG);
1530 #endif
1531 break;
1533 case A_INDEX_FILE:
1535 * Examine a particular file.
1537 if (number <= 0)
1538 number = 1;
1539 if (edit_index((int) number))
1540 error("No such file", NULL_PARG);
1541 break;
1543 case A_REMOVE_FILE:
1544 if (ch_getflags() & CH_HELPFILE)
1545 break;
1546 old_ifile = curr_ifile;
1547 new_ifile = getoff_ifile(curr_ifile);
1548 if (new_ifile == NULL_IFILE)
1550 bell();
1551 break;
1553 if (edit_ifile(new_ifile) != 0)
1555 reedit_ifile(old_ifile);
1556 break;
1558 del_ifile(old_ifile);
1559 break;
1561 case A_OPT_TOGGLE:
1562 optflag = OPT_TOGGLE;
1563 optgetname = FALSE;
1564 mca_opt_toggle();
1565 c = getcc();
1566 goto again;
1568 case A_DISP_OPTION:
1570 * Report a flag setting.
1572 optflag = OPT_NO_TOGGLE;
1573 optgetname = FALSE;
1574 mca_opt_toggle();
1575 c = getcc();
1576 goto again;
1578 case A_FIRSTCMD:
1580 * Set an initial command for new files.
1582 start_mca(A_FIRSTCMD, "+", (void*)NULL, 0);
1583 c = getcc();
1584 goto again;
1586 case A_SHELL:
1588 * Shell escape.
1590 #if SHELL_ESCAPE
1591 if (secure)
1593 error("Command not available", NULL_PARG);
1594 break;
1596 start_mca(A_SHELL, "!", ml_shell, 0);
1597 c = getcc();
1598 goto again;
1599 #else
1600 error("Command not available", NULL_PARG);
1601 break;
1602 #endif
1604 case A_SETMARK:
1606 * Set a mark.
1608 if (ch_getflags() & CH_HELPFILE)
1609 break;
1610 start_mca(A_SETMARK, "mark: ", (void*)NULL, 0);
1611 c = getcc();
1612 if (c == erase_char || c == erase2_char ||
1613 c == kill_char || c == '\n' || c == '\r')
1614 break;
1615 setmark(c);
1616 break;
1618 case A_GOMARK:
1620 * Go to a mark.
1622 start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0);
1623 c = getcc();
1624 if (c == erase_char || c == erase2_char ||
1625 c == kill_char || c == '\n' || c == '\r')
1626 break;
1627 cmd_exec();
1628 gomark(c);
1629 break;
1631 case A_PIPE:
1632 #if PIPEC
1633 if (secure)
1635 error("Command not available", NULL_PARG);
1636 break;
1638 start_mca(A_PIPE, "|mark: ", (void*)NULL, 0);
1639 c = getcc();
1640 if (c == erase_char || c == erase2_char || c == kill_char)
1641 break;
1642 if (c == '\n' || c == '\r')
1643 c = '.';
1644 if (badmark(c))
1645 break;
1646 pipec = c;
1647 start_mca(A_PIPE, "!", ml_shell, 0);
1648 c = getcc();
1649 goto again;
1650 #else
1651 error("Command not available", NULL_PARG);
1652 break;
1653 #endif
1655 case A_B_BRACKET:
1656 case A_F_BRACKET:
1657 start_mca(action, "Brackets: ", (void*)NULL, 0);
1658 c = getcc();
1659 goto again;
1661 case A_LSHIFT:
1662 if (number > 0)
1663 shift_count = number;
1664 else
1665 number = (shift_count > 0) ?
1666 shift_count : sc_width / 2;
1667 if (number > hshift)
1668 number = hshift;
1669 hshift -= number;
1670 screen_trashed = 1;
1671 break;
1673 case A_RSHIFT:
1674 if (number > 0)
1675 shift_count = number;
1676 else
1677 number = (shift_count > 0) ?
1678 shift_count : sc_width / 2;
1679 hshift += number;
1680 screen_trashed = 1;
1681 break;
1683 case A_PREFIX:
1685 * The command is incomplete (more chars are needed).
1686 * Display the current char, so the user knows
1687 * what's going on, and get another character.
1689 if (mca != A_PREFIX)
1691 cmd_reset();
1692 start_mca(A_PREFIX, " ", (void*)NULL,
1693 CF_QUIT_ON_ERASE);
1694 (void) cmd_char(c);
1696 c = getcc();
1697 goto again;
1699 case A_NOACTION:
1700 break;
1702 default:
1703 bell();
1704 break;