2 * Copyright (C) 1984-2011 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;
31 extern int jump_sline;
34 extern int top_scroll;
35 extern int ignore_eoi;
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;
51 extern char *editproto;
53 extern int screen_trashed; /* The screen has been overwritten */
54 extern int shift_count;
56 extern int forw_prompt;
59 static char *shellcmd = NULL; /* For holding last shell command for "!!" */
61 static int mca; /* The multicharacter command (action) */
62 static int search_type; /* The previous type of search */
63 static LINENUM number; /* The number typed by the user */
64 static long fraction; /* The fractional part of the number */
65 static struct loption *curropt;
68 static int optgetname;
69 static POSITION bottompos;
70 static int save_hshift;
76 struct ungot *ug_next;
79 static struct ungot* ungot = NULL;
80 static int unget_end = 0;
82 static void multi_search();
85 * Move the cursor to start of prompt line before executing a command.
86 * This looks nicer if the command takes a long time before
87 * updating the screen.
100 * Set up the display to start a new multi-character command.
103 start_mca(action, prompt, mlist, cmdflags)
113 set_mlist(mlist, cmdflags);
119 return (mca != 0 && mca != A_PREFIX);
123 * Set up the display to start a new search command.
129 if (search_type & SRCH_FILTER)
133 if (search_type & SRCH_FORW)
141 if (search_type & SRCH_NO_MATCH)
142 cmd_putstr("Non-match ");
143 if (search_type & SRCH_FIRST_FILE)
144 cmd_putstr("First-file ");
145 if (search_type & SRCH_PAST_EOF)
146 cmd_putstr("EOF-ignore ");
147 if (search_type & SRCH_NO_MOVE)
148 cmd_putstr("Keep-pos ");
149 if (search_type & SRCH_NO_REGEX)
150 cmd_putstr("Regex-off ");
153 if (search_type & SRCH_FILTER)
157 if (search_type & SRCH_FORW)
161 set_mlist(ml_search, 0);
165 * Set up the display to start a new toggle-option command.
174 no_prompt = (optflag & OPT_NO_PROMPT);
175 flag = (optflag & ~OPT_NO_PROMPT);
176 dash = (flag == OPT_NO_TOGGLE) ? "_" : "-";
199 * Execute a multicharacter command.
213 multi_search(cbuf, (int) number);
217 search_type ^= SRCH_NO_MATCH;
218 set_filter_pattern(cbuf, search_type);
223 * Skip leading spaces or + signs in the string.
225 while (*cbuf == '+' || *cbuf == ' ')
227 if (every_first_cmd != NULL)
228 free(every_first_cmd);
230 every_first_cmd = NULL;
232 every_first_cmd = save(cbuf);
235 toggle_option(curropt, opt_lower, cbuf, optflag);
239 match_brac(cbuf[0], cbuf[1], 1, (int) number);
242 match_brac(cbuf[1], cbuf[0], 0, (int) number);
250 /* If tag structure is loaded then clean it up. */
258 * !! just uses whatever is in shellcmd.
259 * Otherwise, copy cmdbuf to shellcmd,
260 * expanding any special characters ("%" or "#").
264 if (shellcmd != NULL)
266 shellcmd = fexpand(cbuf);
271 if (shellcmd == NULL)
272 lsystem("", "!done");
274 lsystem(shellcmd, "!done");
281 (void) pipe_mark(pipec, cbuf);
282 error("|done", NULL_PARG);
289 * Is a character an erase or kill char?
295 return (c == erase_char || c == erase2_char || c == kill_char);
299 * Handle the first char of an option (after the initial dash).
302 mca_opt_first_char(c)
304 int flag = (optflag & ~OPT_NO_PROMPT);
305 if (flag == OPT_NO_TOGGLE)
310 /* "__" = long option name. */
321 optflag = (flag == OPT_UNSET) ?
322 OPT_TOGGLE : OPT_UNSET;
327 optflag = (flag == OPT_SET) ?
328 OPT_TOGGLE : OPT_SET;
332 optflag ^= OPT_NO_PROMPT;
336 /* "--" = long option name. */
342 /* Char was not handled here. */
347 * Add a char to a long option name.
348 * See if we've got a match for an option name yet.
349 * If so, display the complete name and stop
350 * accepting chars until user hits RETURN.
353 mca_opt_nonfirst_char(c)
362 * Already have a match for the name.
363 * Don't accept anything but erase/kill.
365 if (is_erase_char(c))
370 * Add char to cmd buffer and try to match
373 if (cmd_char(c) == CC_QUIT)
376 opt_lower = ASCII_IS_LOWER(p[0]);
377 curropt = findopt_name(&p, &oname, NULL);
382 * Remember the option and
383 * display the full option name.
387 for (p = oname; *p != '\0'; p++)
390 if (!opt_lower && ASCII_IS_LOWER(c))
391 c = ASCII_TO_UPPER(c);
392 if (cmd_char(c) != CC_OK)
400 * Handle a char of an option toggle command.
409 * This may be a short option (single char),
410 * or one char of a long option name,
411 * or one char of the option parameter.
413 if (curropt == NULL && len_cmdbuf() == 0)
415 int ret = mca_opt_first_char(c);
421 /* We're getting a long option name. */
422 if (c != '\n' && c != '\r')
423 return (mca_opt_nonfirst_char(c));
426 parg.p_string = get_cmdbuf();
427 error("There is no --%s option", &parg);
434 if (is_erase_char(c))
437 /* We're getting the option parameter. */
439 curropt = findopt(c);
442 parg.p_string = propt(c);
443 error("There is no %s option", &parg);
448 * If the option which was entered does not take a
449 * parameter, toggle the option immediately,
450 * so user doesn't have to hit RETURN.
452 if ((optflag & ~OPT_NO_PROMPT) != OPT_TOGGLE ||
453 !opt_has_param(curropt))
455 toggle_option(curropt, ASCII_IS_LOWER(c), "", optflag);
459 * Display a prompt appropriate for the option parameter.
461 start_mca(A_OPT_TOGGLE, opt_prompt(curropt), (void*)NULL, 0);
466 * Handle a char of a search command.
475 * Certain characters as the first char of
476 * the pattern have special meaning:
477 * ! Toggle the NO_MATCH flag
478 * * Toggle the PAST_EOF flag
479 * @ Toggle the FIRST_FILE flag
481 if (len_cmdbuf() > 0)
486 case CONTROL('E'): /* ignore END of file */
489 flag = SRCH_PAST_EOF;
491 case CONTROL('F'): /* FIRST file */
494 flag = SRCH_FIRST_FILE;
496 case CONTROL('K'): /* KEEP position */
500 case CONTROL('R'): /* Don't use REGULAR EXPRESSIONS */
501 flag = SRCH_NO_REGEX;
503 case CONTROL('N'): /* NOT match */
505 flag = SRCH_NO_MATCH;
519 * Handle a character of a multi-character command.
531 * We're not in a multicharacter command.
537 * In the prefix of a command.
538 * This not considered a multichar command
539 * (even tho it uses cmdbuf, etc.).
540 * It is handled in the commands() switch.
546 * Entering digits of a number.
547 * Terminated by a non-digit.
549 if (!((c >= '0' && c <= '9') || c == '.') &&
550 editchar(c, EC_PEEK|EC_NOHISTORY|EC_NOCOMPLETE|EC_NORIGHTLEFT) == A_INVALID)
553 * Not part of the number.
554 * End the number and treat this char
555 * as a normal command character.
557 number = cmd_int(&fraction);
565 ret = mca_opt_char(c);
573 ret = mca_search_char(c);
579 /* Other multicharacter command. */
584 * The multichar command is terminated by a newline.
586 if (c == '\n' || c == '\r')
589 * Execute the command.
596 * Append the char to the command buffer.
598 if (cmd_char(c) == CC_QUIT)
600 * Abort the multi-char command.
604 if ((mca == A_F_BRACKET || mca == A_B_BRACKET) && len_cmdbuf() >= 2)
607 * Special case for the bracket-matching commands.
608 * Execute the command after getting exactly two
609 * characters from the user.
616 * Need another character.
622 * Discard any buffered file data.
627 if (!(ch_getflags() & CH_CANSEEK))
637 * Make sure the screen is displayed.
643 * If nothing is displayed yet, display starting from initial_scrpos.
647 if (initial_scrpos.pos == NULL_POSITION)
649 * {{ Maybe this should be:
650 * jump_loc(ch_zero(), jump_sline);
651 * but this behavior seems rather unexpected
652 * on the first screen. }}
654 jump_loc(ch_zero(), 1);
656 jump_loc(initial_scrpos.pos, initial_scrpos.ln);
657 } else if (screen_trashed)
659 int save_top_scroll = top_scroll;
660 int save_ignore_eoi = ignore_eoi;
663 if (screen_trashed == 2)
665 /* Special case used by ignore_eoi: re-open the input file
666 * and jump to the end of the file. */
671 top_scroll = save_top_scroll;
672 ignore_eoi = save_ignore_eoi;
677 * Display the appropriate prompt.
687 * No prompt necessary if commands are from
688 * ungotten chars rather than from the user.
694 * Make sure the screen is displayed.
697 bottompos = position(BOTTOM_PLUS_ONE);
700 * If we've hit EOF on the last file and the -E flag is set, quit.
702 if (get_quit_at_eof() == OPT_ONPLUS &&
703 eof_displayed() && !(ch_getflags() & CH_HELPFILE) &&
704 next_ifile(curr_ifile) == NULL_IFILE)
708 * If the entire file is displayed and the -F flag is set, quit.
710 if (quit_if_one_screen &&
711 entire_file_displayed() && !(ch_getflags() & CH_HELPFILE) &&
712 next_ifile(curr_ifile) == NULL_IFILE)
715 #if MSDOS_COMPILER==WIN32C
717 * In Win32, display the file name in the window title.
719 if (!(ch_getflags() & CH_HELPFILE))
720 SetConsoleTitle(pr_expand("Less?f - %f.", 0));
723 * Select the proper prompt and display it.
726 * If the previous action was a forward movement,
727 * don't clear the bottom line of the display;
728 * just print the prompt since the forward movement guarantees
729 * that we're in the right position to display the prompt.
730 * Clearing the line could cause a problem: for example, if the last
731 * line displayed ended at the right screen edge without a newline,
732 * then clearing would clear the last displayed line rather than
742 if (p == NULL || *p == '\0')
746 at_enter(AT_STANDOUT);
754 * Display the less version message.
761 parg.p_string = version;
762 error("less %s", &parg);
766 * Get command character.
767 * The character normally comes from the keyboard,
768 * but may come from ungotten characters
769 * (characters previously given to ungetcc or ungetsc).
777 * We have just run out of ungotten chars.
780 if (len_cmdbuf() == 0 || !empty_screen())
783 * Command is incomplete, so try to complete it.
789 * We have a number but no command. Treat as #g.
796 * We have "/string" but no newline. Add the \n.
802 * Some other incomplete command. Let user complete it.
811 * Normal case: no ungotten chars, so get one from the user.
817 * Return the next ungotten char.
820 struct ungot *ug = ungot;
821 char c = ug->ug_char;
824 unget_end = (ungot == NULL);
830 * "Unget" a command character.
831 * The next getcc() will return this character.
837 struct ungot *ug = (struct ungot *) ecalloc(1, sizeof(struct ungot));
845 * Unget a whole string of command characters.
846 * The next sequence of getcc()'s will return this string.
854 for (p = s + strlen(s) - 1; p >= s; p--)
859 * Search for a pattern, possibly in multiple files.
860 * If SRCH_FIRST_FILE is set, begin searching at the first file.
861 * If SRCH_PAST_EOF is set, continue the search thru multiple files.
864 multi_search(pattern, n)
873 save_ifile = save_curr_ifile();
875 if (search_type & SRCH_FIRST_FILE)
878 * Start at the first (or last) file
879 * in the command line list.
881 if (search_type & SRCH_FORW)
882 nomore = edit_first();
884 nomore = edit_last();
887 unsave_ifile(save_ifile);
891 search_type &= ~SRCH_FIRST_FILE;
896 n = search(search_type, pattern, n);
898 * The SRCH_NO_MOVE flag doesn't "stick": it gets cleared
899 * after being used once. This allows "n" to work after
900 * using a /@@ search.
902 search_type &= ~SRCH_NO_MOVE;
908 unsave_ifile(save_ifile);
914 * Some kind of error in the search.
915 * Error message has been printed by search().
919 if ((search_type & SRCH_PAST_EOF) == 0)
921 * We didn't find a match, but we're
922 * supposed to search only one file.
926 * Move on to the next file.
928 if (search_type & SRCH_FORW)
929 nomore = edit_next(1);
931 nomore = edit_prev(1);
939 * Print an error message if we haven't already.
942 error("Pattern not found", NULL_PARG);
947 * Restore the file we were originally viewing.
949 reedit_ifile(save_ifile);
952 unsave_ifile(save_ifile);
957 * Main command processor.
958 * Accept and execute commands until a quit command.
967 int save_search_type;
975 search_type = SRCH_FORW;
976 wscroll = (sc_height + 1) / 2;
977 newaction = A_NOACTION;
987 * See if any signals need processing.
993 quit(QUIT_SAVED_STATUS);
997 * See if window size changed, for systems that don't
1003 * Display prompt and accept a character.
1009 if (newaction == A_NOACTION)
1016 if (newaction != A_NOACTION)
1019 newaction = A_NOACTION;
1023 * If we are in a multicharacter command, call mca_char.
1024 * Otherwise we call fcmd_decode to determine the
1025 * action to be performed.
1028 switch (mca_char(c))
1032 * Need another character.
1038 * Command has been handled by mca_char.
1039 * Start clean with a prompt.
1044 * Not a multi-char command
1045 * (at least, not anymore).
1051 * Decode the command character and decide what to do.
1056 * We're in a multichar command.
1057 * Add the character to the command buffer
1058 * and display it on the screen.
1059 * If the user backspaces past the start
1060 * of the line, abort the command.
1062 if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0)
1064 cbuf = get_cmdbuf();
1068 * Don't use cmd_char if we're starting fresh
1069 * at the beginning of a command, because we
1070 * don't want to echo the command until we know
1071 * it is a multichar command. We also don't
1072 * want erase_char/kill_char to be treated
1073 * as line editing characters.
1080 action = fcmd_decode(cbuf, &extra);
1082 * If an "extra" string was returned,
1083 * process it as a string of command characters.
1089 * Clear the cmdbuf string.
1090 * (But not if we're in the prefix of a command,
1091 * because the partial command string is kept there.)
1093 if (action != A_PREFIX)
1100 * First digit of a number.
1102 start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE);
1107 * Forward one window (and set the window size).
1110 swindow = (int) number;
1114 * Forward one screen.
1117 number = get_swindow();
1120 set_attnpos(bottompos);
1121 forward((int) number, 0, 1);
1126 * Backward one window (and set the window size).
1129 swindow = (int) number;
1133 * Backward one screen.
1136 number = get_swindow();
1138 backward((int) number, 0, 1);
1143 * Forward N (default 1) line.
1148 if (show_attn == OPT_ONPLUS && number > 1)
1149 set_attnpos(bottompos);
1150 forward((int) number, 0, 0);
1155 * Backward N (default 1) line.
1160 backward((int) number, 0, 0);
1165 * Force forward N (default 1) line.
1170 if (show_attn == OPT_ONPLUS && number > 1)
1171 set_attnpos(bottompos);
1172 forward((int) number, 1, 0);
1177 * Force backward N (default 1) line.
1182 backward((int) number, 1, 0);
1187 * Force forward one screen.
1190 number = get_swindow();
1192 if (show_attn == OPT_ONPLUS)
1193 set_attnpos(bottompos);
1194 forward((int) number, 1, 0);
1199 * Forward forever, ignoring EOF.
1201 if (ch_getflags() & CH_HELPFILE)
1213 * This gets us back in "F mode" after processing
1214 * a non-abort signal (e.g. window-change).
1216 if (sigs && !ABORT_SIGS())
1217 newaction = A_F_FOREVER;
1223 * (default same as last 'd' or 'u' command).
1226 wscroll = (int) number;
1228 if (show_attn == OPT_ONPLUS)
1229 set_attnpos(bottompos);
1230 forward(wscroll, 0, 0);
1236 * (default same as last 'd' or 'u' command).
1239 wscroll = (int) number;
1241 backward(wscroll, 0, 0);
1246 * Flush buffers, then repaint screen.
1247 * Don't flush the buffers on a pipe!
1261 * Go to line N, default beginning of file.
1271 * Go to a specified percentage into the file.
1284 jump_percent((int) number, fraction);
1289 * Go to line N, default end of file.
1300 * Go to a specified byte position in the file.
1305 jump_line_loc((POSITION) number, jump_sline);
1310 * Print file name, etc.
1312 if (ch_getflags() & CH_HELPFILE)
1315 parg.p_string = eq_message();
1321 * Print version number, without the "@(#)".
1331 if (curr_ifile != NULL_IFILE &&
1332 ch_getflags() & CH_HELPFILE)
1335 * Quit while viewing the help file
1336 * just means return to viewing the
1339 hshift = save_hshift;
1340 if (edit_prev(1) == 0)
1349 * Define abbreviation for a commonly used sequence below.
1351 #define DO_SEARCH() \
1352 if (number <= 0) number = 1; \
1355 multi_search((char *)NULL, (int) number);
1360 * Search forward for a pattern.
1361 * Get the first char of the pattern.
1363 search_type = SRCH_FORW;
1372 * Search backward for a pattern.
1373 * Get the first char of the pattern.
1375 search_type = SRCH_BACK;
1384 search_type = SRCH_FORW | SRCH_FILTER;
1389 error("Command not available", NULL_PARG);
1393 case A_AGAIN_SEARCH:
1395 * Repeat previous search.
1400 case A_T_AGAIN_SEARCH:
1402 * Repeat previous search, multiple files.
1404 search_type |= SRCH_PAST_EOF;
1408 case A_REVERSE_SEARCH:
1410 * Repeat previous search, in reverse direction.
1412 save_search_type = search_type;
1413 search_type = SRCH_REVERSE(search_type);
1415 search_type = save_search_type;
1418 case A_T_REVERSE_SEARCH:
1420 * Repeat previous search,
1421 * multiple files in reverse direction.
1423 save_search_type = search_type;
1424 search_type = SRCH_REVERSE(search_type);
1425 search_type |= SRCH_PAST_EOF;
1427 search_type = save_search_type;
1438 if (ch_getflags() & CH_HELPFILE)
1441 save_hshift = hshift;
1443 (void) edit(FAKE_HELPFILE);
1449 * Edit a new file. Get the filename.
1453 error("Command not available", NULL_PARG);
1456 start_mca(A_EXAMINE, "Examine: ", ml_examine, 0);
1460 error("Command not available", NULL_PARG);
1466 * Invoke an editor on the input file.
1471 error("Command not available", NULL_PARG);
1474 if (ch_getflags() & CH_HELPFILE)
1476 if (strcmp(get_filename(curr_ifile), "-") == 0)
1478 error("Cannot edit standard input", NULL_PARG);
1481 if (curr_altfilename != NULL)
1483 error("WARNING: This file was viewed via LESSOPEN",
1486 start_mca(A_SHELL, "!", ml_shell, 0);
1488 * Expand the editor prototype string
1489 * and pass it to the system to execute.
1490 * (Make sure the screen is displayed so the
1491 * expansion of "+%lm" works.)
1495 lsystem(pr_expand(editproto, 0), (char*)NULL);
1498 error("Command not available", NULL_PARG);
1504 * Examine next file.
1509 error("No next file", NULL_PARG);
1515 if (edit_next((int) number))
1517 if (get_quit_at_eof() && eof_displayed() &&
1518 !(ch_getflags() & CH_HELPFILE))
1520 parg.p_string = (number > 1) ? "(N-th) " : "";
1521 error("No %snext file", &parg);
1527 * Examine previous file.
1532 error("No previous file", NULL_PARG);
1538 if (edit_prev((int) number))
1540 parg.p_string = (number > 1) ? "(N-th) " : "";
1541 error("No %sprevious file", &parg);
1549 tagfile = nexttag((int) number);
1550 if (tagfile == NULL)
1552 error("No next tag", NULL_PARG);
1555 if (edit(tagfile) == 0)
1557 POSITION pos = tagsearch();
1558 if (pos != NULL_POSITION)
1559 jump_loc(pos, jump_sline);
1562 error("Command not available", NULL_PARG);
1570 tagfile = prevtag((int) number);
1571 if (tagfile == NULL)
1573 error("No previous tag", NULL_PARG);
1576 if (edit(tagfile) == 0)
1578 POSITION pos = tagsearch();
1579 if (pos != NULL_POSITION)
1580 jump_loc(pos, jump_sline);
1583 error("Command not available", NULL_PARG);
1589 * Examine a particular file.
1593 if (edit_index((int) number))
1594 error("No such file", NULL_PARG);
1598 if (ch_getflags() & CH_HELPFILE)
1600 old_ifile = curr_ifile;
1601 new_ifile = getoff_ifile(curr_ifile);
1602 if (new_ifile == NULL_IFILE)
1607 if (edit_ifile(new_ifile) != 0)
1609 reedit_ifile(old_ifile);
1612 del_ifile(old_ifile);
1616 optflag = OPT_TOGGLE;
1624 * Report a flag setting.
1626 optflag = OPT_NO_TOGGLE;
1634 * Set an initial command for new files.
1636 start_mca(A_FIRSTCMD, "+", (void*)NULL, 0);
1647 error("Command not available", NULL_PARG);
1650 start_mca(A_SHELL, "!", ml_shell, 0);
1654 error("Command not available", NULL_PARG);
1662 if (ch_getflags() & CH_HELPFILE)
1664 start_mca(A_SETMARK, "mark: ", (void*)NULL, 0);
1666 if (c == erase_char || c == erase2_char ||
1667 c == kill_char || c == '\n' || c == '\r')
1676 start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0);
1678 if (c == erase_char || c == erase2_char ||
1679 c == kill_char || c == '\n' || c == '\r')
1689 error("Command not available", NULL_PARG);
1692 start_mca(A_PIPE, "|mark: ", (void*)NULL, 0);
1694 if (c == erase_char || c == erase2_char || c == kill_char)
1696 if (c == '\n' || c == '\r')
1701 start_mca(A_PIPE, "!", ml_shell, 0);
1705 error("Command not available", NULL_PARG);
1711 start_mca(action, "Brackets: ", (void*)NULL, 0);
1717 shift_count = number;
1719 number = (shift_count > 0) ?
1720 shift_count : sc_width / 2;
1721 if (number > hshift)
1729 shift_count = number;
1731 number = (shift_count > 0) ?
1732 shift_count : sc_width / 2;
1739 * The command is incomplete (more chars are needed).
1740 * Display the current char, so the user knows
1741 * what's going on, and get another character.
1743 if (mca != A_PREFIX)
1746 start_mca(A_PREFIX, " ", (void*)NULL,