3 * Copyright (C) 1984-2012 Mark Nudelman
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Less License, as specified in the README file.
8 * For more information, 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 int less_is_more;
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;
53 extern char *editproto;
55 extern int screen_trashed; /* The screen has been overwritten */
56 extern int shift_count;
58 extern int forw_prompt;
61 static char *shellcmd = NULL; /* For holding last shell command for "!!" */
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 struct loption *curropt;
70 static int optgetname;
71 static POSITION bottompos;
72 static int save_hshift;
78 struct ungot *ug_next;
81 static struct ungot* ungot = NULL;
82 static int unget_end = 0;
84 static void multi_search();
87 * Move the cursor to start of prompt line before executing a command.
88 * This looks nicer if the command takes a long time before
89 * updating the screen.
102 * Set up the display to start a new multi-character command.
105 start_mca(action, prompt, mlist, cmdflags)
107 constant char *prompt;
108 constant void *mlist;
115 set_mlist(mlist, cmdflags);
121 return (mca != 0 && mca != A_PREFIX);
125 * Set up the display to start a new search command.
131 if (search_type & SRCH_FILTER)
135 if (search_type & SRCH_FORW)
143 if (search_type & SRCH_NO_MATCH)
144 cmd_putstr("Non-match ");
145 if (search_type & SRCH_FIRST_FILE)
146 cmd_putstr("First-file ");
147 if (search_type & SRCH_PAST_EOF)
148 cmd_putstr("EOF-ignore ");
149 if (search_type & SRCH_NO_MOVE)
150 cmd_putstr("Keep-pos ");
151 if (search_type & SRCH_NO_REGEX)
152 cmd_putstr("Regex-off ");
155 if (search_type & SRCH_FILTER)
159 if (search_type & SRCH_FORW)
163 set_mlist(ml_search, 0);
167 * Set up the display to start a new toggle-option command.
176 no_prompt = (optflag & OPT_NO_PROMPT);
177 flag = (optflag & ~OPT_NO_PROMPT);
178 dash = (flag == OPT_NO_TOGGLE) ? "_" : "-";
201 * Execute a multicharacter command.
215 multi_search(cbuf, (int) number);
219 search_type ^= SRCH_NO_MATCH;
220 set_filter_pattern(cbuf, search_type);
225 * Skip leading spaces or + signs in the string.
227 while (*cbuf == '+' || *cbuf == ' ')
229 if (every_first_cmd != NULL)
230 free(every_first_cmd);
232 every_first_cmd = NULL;
234 every_first_cmd = save(cbuf);
237 toggle_option(curropt, opt_lower, cbuf, optflag);
241 match_brac(cbuf[0], cbuf[1], 1, (int) number);
244 match_brac(cbuf[1], cbuf[0], 0, (int) number);
252 /* If tag structure is loaded then clean it up. */
260 * !! just uses whatever is in shellcmd.
261 * Otherwise, copy cmdbuf to shellcmd,
262 * expanding any special characters ("%" or "#").
266 if (shellcmd != NULL)
268 shellcmd = fexpand(cbuf);
273 if (shellcmd == NULL)
274 lsystem("", "!done");
276 lsystem(shellcmd, "!done");
283 (void) pipe_mark(pipec, cbuf);
284 error("|done", NULL_PARG);
291 * Is a character an erase or kill char?
297 return (c == erase_char || c == erase2_char || c == kill_char);
301 * Handle the first char of an option (after the initial dash).
304 mca_opt_first_char(c)
307 int flag = (optflag & ~OPT_NO_PROMPT);
308 if (flag == OPT_NO_TOGGLE)
313 /* "__" = long option name. */
324 optflag = (flag == OPT_UNSET) ?
325 OPT_TOGGLE : OPT_UNSET;
330 optflag = (flag == OPT_SET) ?
331 OPT_TOGGLE : OPT_SET;
335 optflag ^= OPT_NO_PROMPT;
339 /* "--" = long option name. */
345 /* Char was not handled here. */
350 * Add a char to a long option name.
351 * See if we've got a match for an option name yet.
352 * If so, display the complete name and stop
353 * accepting chars until user hits RETURN.
356 mca_opt_nonfirst_char(c)
365 * Already have a match for the name.
366 * Don't accept anything but erase/kill.
368 if (is_erase_char(c))
373 * Add char to cmd buffer and try to match
376 if (cmd_char(c) == CC_QUIT)
379 opt_lower = ASCII_IS_LOWER(p[0]);
380 curropt = findopt_name(&p, &oname, NULL);
385 * Remember the option and
386 * display the full option name.
390 for (p = oname; *p != '\0'; p++)
393 if (!opt_lower && ASCII_IS_LOWER(c))
394 c = ASCII_TO_UPPER(c);
395 if (cmd_char(c) != CC_OK)
403 * Handle a char of an option toggle command.
412 * This may be a short option (single char),
413 * or one char of a long option name,
414 * or one char of the option parameter.
416 if (curropt == NULL && len_cmdbuf() == 0)
418 int ret = mca_opt_first_char(c);
424 /* We're getting a long option name. */
425 if (c != '\n' && c != '\r')
426 return (mca_opt_nonfirst_char(c));
429 parg.p_string = get_cmdbuf();
430 error("There is no --%s option", &parg);
437 if (is_erase_char(c))
440 /* We're getting the option parameter. */
442 curropt = findopt(c);
445 parg.p_string = propt(c);
446 error("There is no %s option", &parg);
451 * If the option which was entered does not take a
452 * parameter, toggle the option immediately,
453 * so user doesn't have to hit RETURN.
455 if ((optflag & ~OPT_NO_PROMPT) != OPT_TOGGLE ||
456 !opt_has_param(curropt))
458 toggle_option(curropt, ASCII_IS_LOWER(c), "", optflag);
462 * Display a prompt appropriate for the option parameter.
464 start_mca(A_OPT_TOGGLE, opt_prompt(curropt), (void*)NULL, 0);
469 * Handle a char of a search command.
478 * Certain characters as the first char of
479 * the pattern have special meaning:
480 * ! Toggle the NO_MATCH flag
481 * * Toggle the PAST_EOF flag
482 * @ Toggle the FIRST_FILE flag
484 if (len_cmdbuf() > 0)
492 case CONTROL('E'): /* ignore END of file */
494 flag = SRCH_PAST_EOF;
499 case CONTROL('F'): /* FIRST file */
501 flag = SRCH_FIRST_FILE;
503 case CONTROL('K'): /* KEEP position */
507 case CONTROL('R'): /* Don't use REGULAR EXPRESSIONS */
508 flag = SRCH_NO_REGEX;
510 case CONTROL('N'): /* NOT match */
512 flag = SRCH_NO_MATCH;
526 * Handle a character of a multi-character command.
538 * We're not in a multicharacter command.
544 * In the prefix of a command.
545 * This not considered a multichar command
546 * (even tho it uses cmdbuf, etc.).
547 * It is handled in the commands() switch.
553 * Entering digits of a number.
554 * Terminated by a non-digit.
556 if (!((c >= '0' && c <= '9') || c == '.') &&
557 editchar(c, EC_PEEK|EC_NOHISTORY|EC_NOCOMPLETE|EC_NORIGHTLEFT) == A_INVALID)
560 * Not part of the number.
561 * End the number and treat this char
562 * as a normal command character.
564 number = cmd_int(&fraction);
572 ret = mca_opt_char(c);
580 ret = mca_search_char(c);
586 /* Other multicharacter command. */
591 * The multichar command is terminated by a newline.
593 if (c == '\n' || c == '\r')
596 * Execute the command.
603 * Append the char to the command buffer.
605 if (cmd_char(c) == CC_QUIT)
607 * Abort the multi-char command.
611 if ((mca == A_F_BRACKET || mca == A_B_BRACKET) && len_cmdbuf() >= 2)
614 * Special case for the bracket-matching commands.
615 * Execute the command after getting exactly two
616 * characters from the user.
623 * Need another character.
629 * Discard any buffered file data.
634 if (!(ch_getflags() & CH_CANSEEK))
644 * Make sure the screen is displayed.
650 * If nothing is displayed yet, display starting from initial_scrpos.
654 if (initial_scrpos.pos == NULL_POSITION)
656 * {{ Maybe this should be:
657 * jump_loc(ch_zero(), jump_sline);
658 * but this behavior seems rather unexpected
659 * on the first screen. }}
661 jump_loc(ch_zero(), 1);
663 jump_loc(initial_scrpos.pos, initial_scrpos.ln);
664 } else if (screen_trashed)
666 int save_top_scroll = top_scroll;
667 int save_ignore_eoi = ignore_eoi;
670 if (screen_trashed == 2)
672 /* Special case used by ignore_eoi: re-open the input file
673 * and jump to the end of the file. */
678 top_scroll = save_top_scroll;
679 ignore_eoi = save_ignore_eoi;
684 * Display the appropriate prompt.
689 register constant char *p;
694 * No prompt necessary if commands are from
695 * ungotten chars rather than from the user.
701 * Make sure the screen is displayed.
704 bottompos = position(BOTTOM_PLUS_ONE);
707 * If we've hit EOF on the last file and the -E flag is set, quit.
709 if (get_quit_at_eof() == OPT_ONPLUS &&
710 eof_displayed() && !(ch_getflags() & CH_HELPFILE) &&
711 next_ifile(curr_ifile) == NULL_IFILE)
715 * If the entire file is displayed and the -F flag is set, quit.
717 if (quit_if_one_screen &&
718 entire_file_displayed() && !(ch_getflags() & CH_HELPFILE) &&
719 next_ifile(curr_ifile) == NULL_IFILE)
722 #if MSDOS_COMPILER==WIN32C
724 * In Win32, display the file name in the window title.
726 if (!(ch_getflags() & CH_HELPFILE))
727 SetConsoleTitle(pr_expand("Less?f - %f.", 0));
730 * Select the proper prompt and display it.
733 * If the previous action was a forward movement,
734 * don't clear the bottom line of the display;
735 * just print the prompt since the forward movement guarantees
736 * that we're in the right position to display the prompt.
737 * Clearing the line could cause a problem: for example, if the last
738 * line displayed ended at the right screen edge without a newline,
739 * then clearing would clear the last displayed line rather than
749 if (p == NULL || *p == '\0')
753 at_enter(AT_STANDOUT);
761 * Display the less version message.
768 parg.p_string = version;
769 error("less %s", &parg);
773 * Get command character.
774 * The character normally comes from the keyboard,
775 * but may come from ungotten characters
776 * (characters previously given to ungetcc or ungetsc).
784 * We have just run out of ungotten chars.
787 if (len_cmdbuf() == 0 || !empty_screen())
790 * Command is incomplete, so try to complete it.
796 * We have a number but no command. Treat as #g.
803 * We have "/string" but no newline. Add the \n.
809 * Some other incomplete command. Let user complete it.
818 * Normal case: no ungotten chars, so get one from the user.
824 * Return the next ungotten char.
827 struct ungot *ug = ungot;
828 char c = ug->ug_char;
831 unget_end = (ungot == NULL);
837 * "Unget" a command character.
838 * The next getcc() will return this character.
844 struct ungot *ug = (struct ungot *) ecalloc(1, sizeof(struct ungot));
853 * Unget a whole string of command characters.
854 * The next sequence of getcc()'s will return this string.
862 for (p = s + strlen(s) - 1; p >= s; p--)
867 * Search for a pattern, possibly in multiple files.
868 * If SRCH_FIRST_FILE is set, begin searching at the first file.
869 * If SRCH_PAST_EOF is set, continue the search thru multiple files.
872 multi_search(pattern, n)
881 save_ifile = save_curr_ifile();
883 if (search_type & SRCH_FIRST_FILE)
886 * Start at the first (or last) file
887 * in the command line list.
889 if (search_type & SRCH_FORW)
890 nomore = edit_first();
892 nomore = edit_last();
895 unsave_ifile(save_ifile);
899 search_type &= ~SRCH_FIRST_FILE;
904 n = search(search_type, pattern, n);
906 * The SRCH_NO_MOVE flag doesn't "stick": it gets cleared
907 * after being used once. This allows "n" to work after
908 * using a /@@ search.
910 search_type &= ~SRCH_NO_MOVE;
916 unsave_ifile(save_ifile);
922 * Some kind of error in the search.
923 * Error message has been printed by search().
927 if ((search_type & SRCH_PAST_EOF) == 0)
929 * We didn't find a match, but we're
930 * supposed to search only one file.
934 * Move on to the next file.
936 if (search_type & SRCH_FORW)
937 nomore = edit_next(1);
939 nomore = edit_prev(1);
947 * Print an error message if we haven't already.
950 error("Pattern not found", NULL_PARG);
955 * Restore the file we were originally viewing.
957 reedit_ifile(save_ifile);
960 unsave_ifile(save_ifile);
965 * Forward forever, or until a highlighted line appears.
968 forw_loop(until_hilite)
973 if (ch_getflags() & CH_HELPFILE)
978 curr_len = ch_length();
979 highest_hilite = until_hilite ? curr_len : NULL_POSITION;
983 if (until_hilite && highest_hilite > curr_len)
995 * This gets us back in "F mode" after processing
996 * a non-abort signal (e.g. window-change).
998 if (sigs && !ABORT_SIGS())
999 return (until_hilite ? A_F_UNTIL_HILITE : A_F_FOREVER);
1001 return (A_NOACTION);
1005 * Main command processor.
1006 * Accept and execute commands until a quit command.
1012 register int action;
1013 register char *cbuf;
1015 int save_search_type;
1022 int until_hilite = 0;
1024 search_type = SRCH_FORW;
1025 wscroll = (sc_height + 1) / 2;
1026 newaction = A_NOACTION;
1036 * See if any signals need processing.
1042 quit(QUIT_SAVED_STATUS);
1046 * See if window size changed, for systems that don't
1047 * generate SIGWINCH.
1052 * Display prompt and accept a character.
1058 if (newaction == A_NOACTION)
1065 if (newaction != A_NOACTION)
1068 newaction = A_NOACTION;
1072 * If we are in a multicharacter command, call mca_char.
1073 * Otherwise we call fcmd_decode to determine the
1074 * action to be performed.
1077 switch (mca_char(c))
1081 * Need another character.
1087 * Command has been handled by mca_char.
1088 * Start clean with a prompt.
1093 * Not a multi-char command
1094 * (at least, not anymore).
1100 * Decode the command character and decide what to do.
1105 * We're in a multichar command.
1106 * Add the character to the command buffer
1107 * and display it on the screen.
1108 * If the user backspaces past the start
1109 * of the line, abort the command.
1111 if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0)
1113 cbuf = get_cmdbuf();
1117 * Don't use cmd_char if we're starting fresh
1118 * at the beginning of a command, because we
1119 * don't want to echo the command until we know
1120 * it is a multichar command. We also don't
1121 * want erase_char/kill_char to be treated
1122 * as line editing characters.
1129 action = fcmd_decode(cbuf, &extra);
1131 * If an "extra" string was returned,
1132 * process it as a string of command characters.
1138 * Clear the cmdbuf string.
1139 * (But not if we're in the prefix of a command,
1140 * because the partial command string is kept there.)
1142 if (action != A_PREFIX)
1149 * First digit of a number.
1151 start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE);
1156 * Forward one window (and set the window size).
1159 swindow = (int) number;
1163 * Forward one screen.
1166 number = get_swindow();
1169 set_attnpos(bottompos);
1170 forward((int) number, 0, 1);
1175 * Backward one window (and set the window size).
1178 swindow = (int) number;
1182 * Backward one screen.
1185 number = get_swindow();
1187 backward((int) number, 0, 1);
1192 * Forward N (default 1) line.
1197 if (show_attn == OPT_ONPLUS && number > 1)
1198 set_attnpos(bottompos);
1199 forward((int) number, 0, 0);
1204 * Backward N (default 1) line.
1209 backward((int) number, 0, 0);
1214 * Force forward N (default 1) line.
1219 if (show_attn == OPT_ONPLUS && number > 1)
1220 set_attnpos(bottompos);
1221 forward((int) number, 1, 0);
1226 * Force backward N (default 1) line.
1231 backward((int) number, 1, 0);
1236 * Force forward one screen.
1239 number = get_swindow();
1241 if (show_attn == OPT_ONPLUS)
1242 set_attnpos(bottompos);
1243 forward((int) number, 1, 0);
1248 * Forward forever, ignoring EOF.
1250 newaction = forw_loop(0);
1253 case A_F_UNTIL_HILITE:
1254 newaction = forw_loop(1);
1260 * (default same as last 'd' or 'u' command).
1263 wscroll = (int) number;
1265 if (show_attn == OPT_ONPLUS)
1266 set_attnpos(bottompos);
1267 forward(wscroll, 0, 0);
1273 * (default same as last 'd' or 'u' command).
1276 wscroll = (int) number;
1278 backward(wscroll, 0, 0);
1283 * Flush buffers, then repaint screen.
1284 * Don't flush the buffers on a pipe!
1298 * Go to line N, default beginning of file.
1308 * Go to a specified percentage into the file.
1321 jump_percent((int) number, fraction);
1326 * Go to line N, default end of file.
1337 * Go to a specified byte position in the file.
1342 jump_line_loc((POSITION) number, jump_sline);
1347 * Print file name, etc.
1349 if (ch_getflags() & CH_HELPFILE)
1352 parg.p_string = eq_message();
1358 * Print version number, without the "@(#)".
1368 if (curr_ifile != NULL_IFILE &&
1369 ch_getflags() & CH_HELPFILE)
1372 * Quit while viewing the help file
1373 * just means return to viewing the
1376 hshift = save_hshift;
1377 if (edit_prev(1) == 0)
1386 * Define abbreviation for a commonly used sequence below.
1388 #define DO_SEARCH() \
1389 if (number <= 0) number = 1; \
1392 multi_search((char *)NULL, (int) number);
1397 * Search forward for a pattern.
1398 * Get the first char of the pattern.
1400 search_type = SRCH_FORW;
1409 * Search backward for a pattern.
1410 * Get the first char of the pattern.
1412 search_type = SRCH_BACK;
1421 search_type = SRCH_FORW | SRCH_FILTER;
1426 error("Command not available", NULL_PARG);
1430 case A_AGAIN_SEARCH:
1432 * Repeat previous search.
1437 case A_T_AGAIN_SEARCH:
1439 * Repeat previous search, multiple files.
1441 search_type |= SRCH_PAST_EOF;
1445 case A_REVERSE_SEARCH:
1447 * Repeat previous search, in reverse direction.
1449 save_search_type = search_type;
1450 search_type = SRCH_REVERSE(search_type);
1452 search_type = save_search_type;
1455 case A_T_REVERSE_SEARCH:
1457 * Repeat previous search,
1458 * multiple files in reverse direction.
1460 save_search_type = search_type;
1461 search_type = SRCH_REVERSE(search_type);
1462 search_type |= SRCH_PAST_EOF;
1464 search_type = save_search_type;
1475 if (ch_getflags() & CH_HELPFILE)
1478 save_hshift = hshift;
1480 (void) edit(FAKE_HELPFILE);
1486 * Edit a new file. Get the filename.
1490 error("Command not available", NULL_PARG);
1493 start_mca(A_EXAMINE, "Examine: ", ml_examine, 0);
1497 error("Command not available", NULL_PARG);
1503 * Invoke an editor on the input file.
1508 error("Command not available", NULL_PARG);
1511 if (ch_getflags() & CH_HELPFILE)
1513 if (strcmp(get_filename(curr_ifile), "-") == 0)
1515 error("Cannot edit standard input", NULL_PARG);
1518 if (curr_altfilename != NULL)
1520 error("WARNING: This file was viewed via LESSOPEN",
1523 start_mca(A_SHELL, "!", ml_shell, 0);
1525 * Expand the editor prototype string
1526 * and pass it to the system to execute.
1527 * (Make sure the screen is displayed so the
1528 * expansion of "+%lm" works.)
1532 lsystem(pr_expand(editproto, 0), (char*)NULL);
1535 error("Command not available", NULL_PARG);
1541 * Examine next file.
1546 error("No next file", NULL_PARG);
1552 if (edit_next((int) number))
1554 if (get_quit_at_eof() && eof_displayed() &&
1555 !(ch_getflags() & CH_HELPFILE))
1557 parg.p_string = (number > 1) ? "(N-th) " : "";
1558 error("No %snext file", &parg);
1564 * Examine previous file.
1569 error("No previous file", NULL_PARG);
1575 if (edit_prev((int) number))
1577 parg.p_string = (number > 1) ? "(N-th) " : "";
1578 error("No %sprevious file", &parg);
1586 tagfile = nexttag((int) number);
1587 if (tagfile == NULL)
1589 error("No next tag", NULL_PARG);
1592 if (edit(tagfile) == 0)
1594 POSITION pos = tagsearch();
1595 if (pos != NULL_POSITION)
1596 jump_loc(pos, jump_sline);
1599 error("Command not available", NULL_PARG);
1607 tagfile = prevtag((int) number);
1608 if (tagfile == NULL)
1610 error("No previous tag", NULL_PARG);
1613 if (edit(tagfile) == 0)
1615 POSITION pos = tagsearch();
1616 if (pos != NULL_POSITION)
1617 jump_loc(pos, jump_sline);
1620 error("Command not available", NULL_PARG);
1626 * Examine a particular file.
1630 if (edit_index((int) number))
1631 error("No such file", NULL_PARG);
1635 if (ch_getflags() & CH_HELPFILE)
1637 old_ifile = curr_ifile;
1638 new_ifile = getoff_ifile(curr_ifile);
1639 if (new_ifile == NULL_IFILE)
1644 if (edit_ifile(new_ifile) != 0)
1646 reedit_ifile(old_ifile);
1649 del_ifile(old_ifile);
1653 optflag = OPT_TOGGLE;
1661 * Report a flag setting.
1663 optflag = OPT_NO_TOGGLE;
1671 * Set an initial command for new files.
1673 start_mca(A_FIRSTCMD, "+", (void*)NULL, 0);
1684 error("Command not available", NULL_PARG);
1687 start_mca(A_SHELL, "!", ml_shell, 0);
1691 error("Command not available", NULL_PARG);
1699 if (ch_getflags() & CH_HELPFILE)
1701 start_mca(A_SETMARK, "mark: ", (void*)NULL, 0);
1703 if (c == erase_char || c == erase2_char ||
1704 c == kill_char || c == '\n' || c == '\r')
1713 start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0);
1715 if (c == erase_char || c == erase2_char ||
1716 c == kill_char || c == '\n' || c == '\r')
1726 error("Command not available", NULL_PARG);
1729 start_mca(A_PIPE, "|mark: ", (void*)NULL, 0);
1731 if (c == erase_char || c == erase2_char || c == kill_char)
1733 if (c == '\n' || c == '\r')
1738 start_mca(A_PIPE, "!", ml_shell, 0);
1742 error("Command not available", NULL_PARG);
1748 start_mca(action, "Brackets: ", (void*)NULL, 0);
1754 shift_count = number;
1756 number = (shift_count > 0) ?
1757 shift_count : sc_width / 2;
1758 if (number > hshift)
1766 shift_count = number;
1768 number = (shift_count > 0) ?
1769 shift_count : sc_width / 2;
1776 * The command is incomplete (more chars are needed).
1777 * Display the current char, so the user knows
1778 * what's going on, and get another character.
1780 if (mca != A_PREFIX)
1783 start_mca(A_PREFIX, " ", (void*)NULL,