2 * Copyright (C) 1984-2007 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.
13 * User-level command processor.
17 #if MSDOS_COMPILER==WIN32C
24 extern int erase_char
, erase2_char
, kill_char
;
26 extern int quit_if_one_screen
;
32 extern int jump_sline
;
35 extern int top_scroll
;
36 extern int ignore_eoi
;
40 extern char *every_first_cmd
;
41 extern char *curr_altfilename
;
42 extern char version
[];
43 extern struct scrpos initial_scrpos
;
44 extern IFILE curr_ifile
;
45 extern void constant
*ml_search
;
46 extern void constant
*ml_examine
;
47 #if SHELL_ESCAPE || PIPEC
48 extern void constant
*ml_shell
;
52 extern char *editproto
;
54 extern int screen_trashed
; /* The screen has been overwritten */
55 extern int shift_count
;
57 extern int forw_prompt
;
59 static char ungot
[UNGOT_SIZE
];
60 static char *ungotp
= NULL
;
62 static char *shellcmd
= NULL
; /* For holding last shell command for "!!" */
64 static int mca
; /* The multicharacter command (action) */
65 static int search_type
; /* The previous type of search */
66 static LINENUM number
; /* The number typed by the user */
67 static long fraction
; /* The fractional part of the number */
70 static int optgetname
;
71 static POSITION bottompos
;
72 static int save_hshift
;
77 static void multi_search();
80 * Move the cursor to start of prompt line before executing a command.
81 * This looks nicer if the command takes a long time before
82 * updating the screen.
93 * Set up the display to start a new multi-character command.
96 start_mca(action
, prompt
, mlist
, cmdflags
)
106 set_mlist(mlist
, cmdflags
);
112 return (mca
!= 0 && mca
!= A_PREFIX
);
116 * Set up the display to start a new search command.
121 if (search_type
& SRCH_FORW
)
129 if (search_type
& SRCH_NO_MATCH
)
130 cmd_putstr("Non-match ");
131 if (search_type
& SRCH_FIRST_FILE
)
132 cmd_putstr("First-file ");
133 if (search_type
& SRCH_PAST_EOF
)
134 cmd_putstr("EOF-ignore ");
135 if (search_type
& SRCH_NO_MOVE
)
136 cmd_putstr("Keep-pos ");
137 if (search_type
& SRCH_NO_REGEX
)
138 cmd_putstr("Regex-off ");
140 if (search_type
& SRCH_FORW
)
144 set_mlist(ml_search
, 0);
148 * Set up the display to start a new toggle-option command.
157 no_prompt
= (optflag
& OPT_NO_PROMPT
);
158 flag
= (optflag
& ~OPT_NO_PROMPT
);
159 dash
= (flag
== OPT_NO_TOGGLE
) ? "_" : "-";
182 * Execute a multicharacter command.
196 multi_search(cbuf
, (int) number
);
200 * Skip leading spaces or + signs in the string.
202 while (*cbuf
== '+' || *cbuf
== ' ')
204 if (every_first_cmd
!= NULL
)
205 free(every_first_cmd
);
207 every_first_cmd
= NULL
;
209 every_first_cmd
= save(cbuf
);
212 toggle_option(optchar
, cbuf
, optflag
);
216 match_brac(cbuf
[0], cbuf
[1], 1, (int) number
);
219 match_brac(cbuf
[1], cbuf
[0], 0, (int) number
);
227 /* If tag structure is loaded then clean it up. */
235 * !! just uses whatever is in shellcmd.
236 * Otherwise, copy cmdbuf to shellcmd,
237 * expanding any special characters ("%" or "#").
241 if (shellcmd
!= NULL
)
243 shellcmd
= fexpand(cbuf
);
248 if (shellcmd
== NULL
)
249 lsystem("", "!done");
251 lsystem(shellcmd
, "!done");
258 (void) pipe_mark(pipec
, cbuf
);
259 error("|done", NULL_PARG
);
266 * Add a character to a multi-character command.
281 * Not in a multicharacter command.
287 * In the prefix of a command.
288 * This not considered a multichar command
289 * (even tho it uses cmdbuf, etc.).
290 * It is handled in the commands() switch.
296 * Entering digits of a number.
297 * Terminated by a non-digit.
299 if (!((c
>= '0' && c
<= '9') || c
== '.') &&
300 editchar(c
, EC_PEEK
|EC_NOHISTORY
|EC_NOCOMPLETE
|EC_NORIGHTLEFT
) == A_INVALID
)
303 * Not part of the number.
304 * Treat as a normal command character.
306 number
= cmd_int(&fraction
);
315 * Special case for the TOGGLE_OPTION command.
316 * If the option letter which was entered is a
317 * single-char option, execute the command immediately,
318 * so user doesn't have to hit RETURN.
319 * If the first char is + or -, this indicates
320 * OPT_UNSET or OPT_SET respectively, instead of OPT_TOGGLE.
321 * "--" begins inputting a long option name.
323 if (optchar
== '\0' && len_cmdbuf() == 0)
325 flag
= (optflag
& ~OPT_NO_PROMPT
);
326 if (flag
== OPT_NO_TOGGLE
)
331 /* "__" = long option name. */
342 optflag
= (flag
== OPT_UNSET
) ?
343 OPT_TOGGLE
: OPT_UNSET
;
348 optflag
= (flag
== OPT_SET
) ?
349 OPT_TOGGLE
: OPT_SET
;
353 optflag
^= OPT_NO_PROMPT
;
357 /* "--" = long option name. */
367 * We're getting a long option name.
368 * See if we've matched an option name yet.
369 * If so, display the complete name and stop
370 * accepting chars until user hits RETURN.
376 if (c
== '\n' || c
== '\r')
379 * When the user hits RETURN, make sure
380 * we've matched an option name, then
381 * pretend he just entered the equivalent
386 parg
.p_string
= get_cmdbuf();
387 error("There is no --%s option", &parg
);
398 * Already have a match for the name.
399 * Don't accept anything but erase/kill.
401 if (c
== erase_char
||
408 * Add char to cmd buffer and try to match
411 if (cmd_char(c
) == CC_QUIT
)
414 lc
= ASCII_IS_LOWER(p
[0]);
415 o
= findopt_name(&p
, &oname
, NULL
);
420 * Remember the option letter and
421 * display the full option name.
423 optchar
= o
->oletter
;
424 if (!lc
&& ASCII_IS_LOWER(optchar
))
425 optchar
= ASCII_TO_UPPER(optchar
);
428 for (p
= oname
; *p
!= '\0'; p
++)
431 if (!lc
&& ASCII_IS_LOWER(c
))
432 c
= ASCII_TO_UPPER(c
);
433 if (cmd_char(c
) != CC_OK
)
441 if (c
== erase_char
|| c
== erase2_char
|| c
== kill_char
)
444 /* We already have the option letter. */
449 if ((optflag
& ~OPT_NO_PROMPT
) != OPT_TOGGLE
||
450 single_char_option(c
))
452 toggle_option(c
, "", optflag
);
456 * Display a prompt appropriate for the option letter.
458 if ((p
= opt_prompt(c
)) == NULL
)
465 start_mca(A_OPT_TOGGLE
, p
, (void*)NULL
, 0);
471 * Special case for search commands.
472 * Certain characters as the first char of
473 * the pattern have special meaning:
474 * ! Toggle the NO_MATCH flag
475 * * Toggle the PAST_EOF flag
476 * @ Toggle the FIRST_FILE flag
478 if (len_cmdbuf() > 0)
480 * Only works for the first char of the pattern.
487 case CONTROL('E'): /* ignore END of file */
489 flag
= SRCH_PAST_EOF
;
491 case CONTROL('F'): /* FIRST file */
493 flag
= SRCH_FIRST_FILE
;
495 case CONTROL('K'): /* KEEP position */
498 case CONTROL('R'): /* Don't use REGULAR EXPRESSIONS */
499 flag
= SRCH_NO_REGEX
;
501 case CONTROL('N'): /* NOT match */
503 flag
= SRCH_NO_MATCH
;
516 * Any other multicharacter command
517 * is terminated by a newline.
519 if (c
== '\n' || c
== '\r')
522 * Execute the command.
529 * Append the char to the command buffer.
531 if (cmd_char(c
) == CC_QUIT
)
533 * Abort the multi-char command.
537 if ((mca
== A_F_BRACKET
|| mca
== A_B_BRACKET
) && len_cmdbuf() >= 2)
540 * Special case for the bracket-matching commands.
541 * Execute the command after getting exactly two
542 * characters from the user.
549 * Need another character.
555 * Make sure the screen is displayed.
561 * If nothing is displayed yet, display starting from initial_scrpos.
565 if (initial_scrpos
.pos
== NULL_POSITION
)
567 * {{ Maybe this should be:
568 * jump_loc(ch_zero(), jump_sline);
569 * but this behavior seems rather unexpected
570 * on the first screen. }}
572 jump_loc(ch_zero(), 1);
574 jump_loc(initial_scrpos
.pos
, initial_scrpos
.ln
);
575 } else if (screen_trashed
)
578 save_top_scroll
= top_scroll
;
581 top_scroll
= save_top_scroll
;
586 * Display the appropriate prompt.
593 if (ungotp
!= NULL
&& ungotp
> ungot
)
596 * No prompt necessary if commands are from
597 * ungotten chars rather than from the user.
603 * Make sure the screen is displayed.
606 bottompos
= position(BOTTOM_PLUS_ONE
);
609 * If we've hit EOF on the last file, and the -E flag is set
610 * (or -F is set and this is the first prompt), then quit.
611 * {{ Relying on "first prompt" to detect a single-screen file
612 * fails if +G is used, for example. }}
614 if ((get_quit_at_eof() == OPT_ONPLUS
|| quit_if_one_screen
) &&
615 hit_eof
&& !(ch_getflags() & CH_HELPFILE
) &&
616 next_ifile(curr_ifile
) == NULL_IFILE
)
618 quit_if_one_screen
= FALSE
;
619 #if 0 /* This doesn't work well because some "te"s clear the screen. */
621 * If the -e flag is set and we've hit EOF on the last file,
622 * and the file is squished (shorter than the screen), quit.
624 if (get_quit_at_eof() && squished
&&
625 next_ifile(curr_ifile
) == NULL_IFILE
)
629 #if MSDOS_COMPILER==WIN32C
631 * In Win32, display the file name in the window title.
633 if (!(ch_getflags() & CH_HELPFILE
))
634 SetConsoleTitle(pr_expand("Less?f - %f.", 0));
637 * Select the proper prompt and display it.
640 * If the previous action was a forward movement,
641 * don't clear the bottom line of the display;
642 * just print the prompt since the forward movement guarantees
643 * that we're in the right position to display the prompt.
644 * Clearing the line could cause a problem: for example, if the last
645 * line displayed ended at the right screen edge without a newline,
646 * then clearing would clear the last displayed line rather than
654 if (p
== NULL
|| *p
== '\0')
658 at_enter(AT_STANDOUT
);
666 * Display the less version message.
673 parg
.p_string
= version
;
674 error("less %s", &parg
);
678 * Get command character.
679 * The character normally comes from the keyboard,
680 * but may come from ungotten characters
681 * (characters previously given to ungetcc or ungetsc).
688 * Normal case: no ungotten chars, so get one from the user.
694 * Return the next ungotten char.
699 * We have just run out of ungotten chars.
702 if (len_cmdbuf() == 0 || !empty_screen())
705 * Command is incomplete, so try to complete it.
711 * We have a number but no command. Treat as #g.
718 * We have "/string" but no newline. Add the \n.
724 * Some other incomplete command. Let user complete it.
731 * "Unget" a command character.
732 * The next getcc() will return this character.
740 if (ungotp
>= ungot
+ sizeof(ungot
))
742 error("ungetcc overflow", NULL_PARG
);
749 * Unget a whole string of command characters.
750 * The next sequence of getcc()'s will return this string.
758 for (p
= s
+ strlen(s
) - 1; p
>= s
; p
--)
763 * Search for a pattern, possibly in multiple files.
764 * If SRCH_FIRST_FILE is set, begin searching at the first file.
765 * If SRCH_PAST_EOF is set, continue the search thru multiple files.
768 multi_search(pattern
, n
)
777 save_ifile
= save_curr_ifile();
779 if (search_type
& SRCH_FIRST_FILE
)
782 * Start at the first (or last) file
783 * in the command line list.
785 if (search_type
& SRCH_FORW
)
786 nomore
= edit_first();
788 nomore
= edit_last();
791 unsave_ifile(save_ifile
);
795 search_type
&= ~SRCH_FIRST_FILE
;
800 n
= search(search_type
, pattern
, n
);
802 * The SRCH_NO_MOVE flag doesn't "stick": it gets cleared
803 * after being used once. This allows "n" to work after
804 * using a /@@ search.
806 search_type
&= ~SRCH_NO_MOVE
;
812 unsave_ifile(save_ifile
);
818 * Some kind of error in the search.
819 * Error message has been printed by search().
823 if ((search_type
& SRCH_PAST_EOF
) == 0)
825 * We didn't find a match, but we're
826 * supposed to search only one file.
830 * Move on to the next file.
832 if (search_type
& SRCH_FORW
)
833 nomore
= edit_next(1);
835 nomore
= edit_prev(1);
843 * Print an error message if we haven't already.
846 error("Pattern not found", NULL_PARG
);
851 * Restore the file we were originally viewing.
853 reedit_ifile(save_ifile
);
856 unsave_ifile(save_ifile
);
861 * Main command processor.
862 * Accept and execute commands until a quit command.
871 int save_search_type
;
879 search_type
= SRCH_FORW
;
880 wscroll
= (sc_height
+ 1) / 2;
881 newaction
= A_NOACTION
;
891 * See if any signals need processing.
897 quit(QUIT_SAVED_STATUS
);
901 * See if window size changed, for systems that don't
907 * Display prompt and accept a character.
913 if (newaction
== A_NOACTION
)
920 if (newaction
!= A_NOACTION
)
923 newaction
= A_NOACTION
;
927 * If we are in a multicharacter command, call mca_char.
928 * Otherwise we call fcmd_decode to determine the
929 * action to be performed.
936 * Need another character.
942 * Command has been handled by mca_char.
943 * Start clean with a prompt.
948 * Not a multi-char command
949 * (at least, not anymore).
955 * Decode the command character and decide what to do.
960 * We're in a multichar command.
961 * Add the character to the command buffer
962 * and display it on the screen.
963 * If the user backspaces past the start
964 * of the line, abort the command.
966 if (cmd_char(c
) == CC_QUIT
|| len_cmdbuf() == 0)
972 * Don't use cmd_char if we're starting fresh
973 * at the beginning of a command, because we
974 * don't want to echo the command until we know
975 * it is a multichar command. We also don't
976 * want erase_char/kill_char to be treated
977 * as line editing characters.
984 action
= fcmd_decode(cbuf
, &extra
);
986 * If an "extra" string was returned,
987 * process it as a string of command characters.
993 * Clear the cmdbuf string.
994 * (But not if we're in the prefix of a command,
995 * because the partial command string is kept there.)
997 if (action
!= A_PREFIX
)
1004 * First digit of a number.
1006 start_mca(A_DIGIT
, ":", (void*)NULL
, CF_QUIT_ON_ERASE
);
1011 * Forward one window (and set the window size).
1014 swindow
= (int) number
;
1018 * Forward one screen.
1021 number
= get_swindow();
1024 set_attnpos(bottompos
);
1025 forward((int) number
, 0, 1);
1030 * Backward one window (and set the window size).
1033 swindow
= (int) number
;
1037 * Backward one screen.
1040 number
= get_swindow();
1042 backward((int) number
, 0, 1);
1047 * Forward N (default 1) line.
1052 if (show_attn
== OPT_ONPLUS
&& number
> 1)
1053 set_attnpos(bottompos
);
1054 forward((int) number
, 0, 0);
1059 * Backward N (default 1) line.
1064 backward((int) number
, 0, 0);
1069 * Force forward N (default 1) line.
1074 if (show_attn
== OPT_ONPLUS
&& number
> 1)
1075 set_attnpos(bottompos
);
1076 forward((int) number
, 1, 0);
1081 * Force backward N (default 1) line.
1086 backward((int) number
, 1, 0);
1091 * Force forward one screen.
1094 number
= get_swindow();
1096 if (show_attn
== OPT_ONPLUS
)
1097 set_attnpos(bottompos
);
1098 forward((int) number
, 1, 0);
1103 * Forward forever, ignoring EOF.
1105 if (ch_getflags() & CH_HELPFILE
)
1115 * This gets us back in "F mode" after processing
1116 * a non-abort signal (e.g. window-change).
1118 if (sigs
&& !ABORT_SIGS())
1119 newaction
= A_F_FOREVER
;
1125 * (default same as last 'd' or 'u' command).
1128 wscroll
= (int) number
;
1130 if (show_attn
== OPT_ONPLUS
)
1131 set_attnpos(bottompos
);
1132 forward(wscroll
, 0, 0);
1138 * (default same as last 'd' or 'u' command).
1141 wscroll
= (int) number
;
1143 backward(wscroll
, 0, 0);
1148 * Flush buffers, then repaint screen.
1149 * Don't flush the buffers on a pipe!
1151 if (ch_getflags() & CH_CANSEEK
)
1170 * Go to line N, default beginning of file.
1180 * Go to a specified percentage into the file.
1193 jump_percent((int) number
, fraction
);
1198 * Go to line N, default end of file.
1209 * Go to a specified byte position in the file.
1214 jump_line_loc((POSITION
) number
, jump_sline
);
1219 * Print file name, etc.
1221 if (ch_getflags() & CH_HELPFILE
)
1224 parg
.p_string
= eq_message();
1230 * Print version number, without the "@(#)".
1240 if (curr_ifile
!= NULL_IFILE
&&
1241 ch_getflags() & CH_HELPFILE
)
1244 * Quit while viewing the help file
1245 * just means return to viewing the
1248 hshift
= save_hshift
;
1249 if (edit_prev(1) == 0)
1258 * Define abbreviation for a commonly used sequence below.
1260 #define DO_SEARCH() if (number <= 0) number = 1; \
1263 multi_search((char *)NULL, (int) number);
1268 * Search forward for a pattern.
1269 * Get the first char of the pattern.
1271 search_type
= SRCH_FORW
;
1280 * Search backward for a pattern.
1281 * Get the first char of the pattern.
1283 search_type
= SRCH_BACK
;
1290 case A_AGAIN_SEARCH
:
1292 * Repeat previous search.
1297 case A_T_AGAIN_SEARCH
:
1299 * Repeat previous search, multiple files.
1301 search_type
|= SRCH_PAST_EOF
;
1305 case A_REVERSE_SEARCH
:
1307 * Repeat previous search, in reverse direction.
1309 save_search_type
= search_type
;
1310 search_type
= SRCH_REVERSE(search_type
);
1312 search_type
= save_search_type
;
1315 case A_T_REVERSE_SEARCH
:
1317 * Repeat previous search,
1318 * multiple files in reverse direction.
1320 save_search_type
= search_type
;
1321 search_type
= SRCH_REVERSE(search_type
);
1322 search_type
|= SRCH_PAST_EOF
;
1324 search_type
= save_search_type
;
1335 if (ch_getflags() & CH_HELPFILE
)
1338 save_hshift
= hshift
;
1340 (void) edit(FAKE_HELPFILE
);
1346 * Edit a new file. Get the filename.
1350 error("Command not available", NULL_PARG
);
1353 start_mca(A_EXAMINE
, "Examine: ", ml_examine
, 0);
1357 error("Command not available", NULL_PARG
);
1363 * Invoke an editor on the input file.
1368 error("Command not available", NULL_PARG
);
1371 if (ch_getflags() & CH_HELPFILE
)
1373 if (strcmp(get_filename(curr_ifile
), "-") == 0)
1375 error("Cannot edit standard input", NULL_PARG
);
1378 if (curr_altfilename
!= NULL
)
1380 error("WARNING: This file was viewed via LESSOPEN",
1383 start_mca(A_SHELL
, "!", ml_shell
, 0);
1385 * Expand the editor prototype string
1386 * and pass it to the system to execute.
1387 * (Make sure the screen is displayed so the
1388 * expansion of "+%lm" works.)
1392 lsystem(pr_expand(editproto
, 0), (char*)NULL
);
1395 error("Command not available", NULL_PARG
);
1401 * Examine next file.
1406 error("No next file", NULL_PARG
);
1412 if (edit_next((int) number
))
1414 if (get_quit_at_eof() && hit_eof
&&
1415 !(ch_getflags() & CH_HELPFILE
))
1417 parg
.p_string
= (number
> 1) ? "(N-th) " : "";
1418 error("No %snext file", &parg
);
1424 * Examine previous file.
1429 error("No previous file", NULL_PARG
);
1435 if (edit_prev((int) number
))
1437 parg
.p_string
= (number
> 1) ? "(N-th) " : "";
1438 error("No %sprevious file", &parg
);
1446 tagfile
= nexttag((int) number
);
1447 if (tagfile
== NULL
)
1449 error("No next tag", NULL_PARG
);
1452 if (edit(tagfile
) == 0)
1454 POSITION pos
= tagsearch();
1455 if (pos
!= NULL_POSITION
)
1456 jump_loc(pos
, jump_sline
);
1459 error("Command not available", NULL_PARG
);
1467 tagfile
= prevtag((int) number
);
1468 if (tagfile
== NULL
)
1470 error("No previous tag", NULL_PARG
);
1473 if (edit(tagfile
) == 0)
1475 POSITION pos
= tagsearch();
1476 if (pos
!= NULL_POSITION
)
1477 jump_loc(pos
, jump_sline
);
1480 error("Command not available", NULL_PARG
);
1486 * Examine a particular file.
1490 if (edit_index((int) number
))
1491 error("No such file", NULL_PARG
);
1495 if (ch_getflags() & CH_HELPFILE
)
1497 old_ifile
= curr_ifile
;
1498 new_ifile
= getoff_ifile(curr_ifile
);
1499 if (new_ifile
== NULL_IFILE
)
1504 if (edit_ifile(new_ifile
) != 0)
1506 reedit_ifile(old_ifile
);
1509 del_ifile(old_ifile
);
1513 optflag
= OPT_TOGGLE
;
1521 * Report a flag setting.
1523 optflag
= OPT_NO_TOGGLE
;
1531 * Set an initial command for new files.
1533 start_mca(A_FIRSTCMD
, "+", (void*)NULL
, 0);
1544 error("Command not available", NULL_PARG
);
1547 start_mca(A_SHELL
, "!", ml_shell
, 0);
1551 error("Command not available", NULL_PARG
);
1559 if (ch_getflags() & CH_HELPFILE
)
1561 start_mca(A_SETMARK
, "mark: ", (void*)NULL
, 0);
1563 if (c
== erase_char
|| c
== erase2_char
||
1564 c
== kill_char
|| c
== '\n' || c
== '\r')
1573 start_mca(A_GOMARK
, "goto mark: ", (void*)NULL
, 0);
1575 if (c
== erase_char
|| c
== erase2_char
||
1576 c
== kill_char
|| c
== '\n' || c
== '\r')
1585 error("Command not available", NULL_PARG
);
1588 start_mca(A_PIPE
, "|mark: ", (void*)NULL
, 0);
1590 if (c
== erase_char
|| c
== erase2_char
|| c
== kill_char
)
1592 if (c
== '\n' || c
== '\r')
1597 start_mca(A_PIPE
, "!", ml_shell
, 0);
1601 error("Command not available", NULL_PARG
);
1607 start_mca(action
, "Brackets: ", (void*)NULL
, 0);
1613 shift_count
= number
;
1615 number
= (shift_count
> 0) ?
1616 shift_count
: sc_width
/ 2;
1617 if (number
> hshift
)
1625 shift_count
= number
;
1627 number
= (shift_count
> 0) ?
1628 shift_count
: sc_width
/ 2;
1635 * The command is incomplete (more chars are needed).
1636 * Display the current char, so the user knows
1637 * what's going on, and get another character.
1639 if (mca
!= A_PREFIX
)
1642 start_mca(A_PREFIX
, " ", (void*)NULL
,