3 * Copyright (C) 1984-2015 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;
40 extern int less_is_more;
41 extern POSITION highest_hilite;
42 extern char *every_first_cmd;
43 extern char *curr_altfilename;
44 extern char version[];
45 extern struct scrpos initial_scrpos;
46 extern IFILE curr_ifile;
47 extern void constant *ml_search;
48 extern void constant *ml_examine;
49 #if SHELL_ESCAPE || PIPEC
50 extern void constant *ml_shell;
54 extern char *editproto;
56 extern int screen_trashed; /* The screen has been overwritten */
57 extern int shift_count;
59 extern int forw_prompt;
60 extern int same_pos_bell;
63 static char *shellcmd = NULL; /* For holding last shell command for "!!" */
65 static int mca; /* The multicharacter command (action) */
66 static int search_type; /* The previous type of search */
67 static LINENUM number; /* The number typed by the user */
68 static long fraction; /* The fractional part of the number */
69 static struct loption *curropt;
72 static int optgetname;
73 static POSITION bottompos;
74 static int save_hshift;
75 static int save_bs_mode;
81 struct ungot *ug_next;
85 static struct ungot* ungot = NULL;
87 static void multi_search(char *pattern, int n, int silent);
90 * Move the cursor to start of prompt line before executing a command.
91 * This looks nicer if the command takes a long time before
92 * updating the screen.
105 * Set up the display to start a new multi-character command.
108 start_mca(int action, constant char *prompt, constant void *mlist, int cmdflags)
114 set_mlist(mlist, cmdflags);
120 return (mca != 0 && mca != A_PREFIX);
124 * Set up the display to start a new search command.
130 if (search_type & SRCH_FILTER)
134 if (search_type & SRCH_FORW)
142 if (search_type & SRCH_NO_MATCH)
143 cmd_putstr("Non-match ");
144 if (search_type & SRCH_FIRST_FILE)
145 cmd_putstr("First-file ");
146 if (search_type & SRCH_PAST_EOF)
147 cmd_putstr("EOF-ignore ");
148 if (search_type & SRCH_NO_MOVE)
149 cmd_putstr("Keep-pos ");
150 if (search_type & SRCH_NO_REGEX)
151 cmd_putstr("Regex-off ");
154 if (search_type & SRCH_FILTER)
158 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) ? "_" : "-";
202 * Execute a multicharacter command.
216 multi_search(cbuf, (int) number, 0);
220 search_type ^= SRCH_NO_MATCH;
221 set_filter_pattern(cbuf, search_type);
226 * Skip leading spaces or + signs in the string.
228 while (*cbuf == '+' || *cbuf == ' ')
230 if (every_first_cmd != NULL)
231 free(every_first_cmd);
233 every_first_cmd = NULL;
235 every_first_cmd = save(cbuf);
238 toggle_option(curropt, opt_lower, cbuf, optflag);
242 match_brac(cbuf[0], cbuf[1], 1, (int) number);
245 match_brac(cbuf[1], cbuf[0], 0, (int) number);
253 /* If tag structure is loaded then clean it up. */
261 * !! just uses whatever is in shellcmd.
262 * Otherwise, copy cmdbuf to shellcmd,
263 * expanding any special characters ("%" or "#").
267 if (shellcmd != NULL)
269 shellcmd = fexpand(cbuf);
274 if (shellcmd == NULL)
275 lsystem("", "!done");
277 lsystem(shellcmd, "!done");
284 (void) pipe_mark(pipec, cbuf);
285 error("|done", NULL_PARG);
292 * 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(int c)
306 int flag = (optflag & ~OPT_NO_PROMPT);
307 if (flag == OPT_NO_TOGGLE)
312 /* "__" = long option name. */
323 optflag = (flag == OPT_UNSET) ?
324 OPT_TOGGLE : OPT_UNSET;
329 optflag = (flag == OPT_SET) ?
330 OPT_TOGGLE : OPT_SET;
334 optflag ^= OPT_NO_PROMPT;
338 /* "--" = long option name. */
344 /* Char was not handled here. */
349 * Add a char to a long option name.
350 * See if we've got a match for an option name yet.
351 * If so, display the complete name and stop
352 * accepting chars until user hits RETURN.
355 mca_opt_nonfirst_char(int c)
363 * Already have a match for the name.
364 * Don't accept anything but erase/kill.
366 if (is_erase_char(c))
371 * Add char to cmd buffer and try to match
374 if (cmd_char(c) == CC_QUIT)
377 opt_lower = ASCII_IS_LOWER(p[0]);
378 curropt = findopt_name(&p, &oname, NULL);
383 * Remember the option and
384 * display the full option name.
388 for (p = oname; *p != '\0'; p++)
391 if (!opt_lower && ASCII_IS_LOWER(c))
392 c = ASCII_TO_UPPER(c);
393 if (cmd_char(c) != CC_OK)
401 * 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.
469 mca_search_char(int c)
474 * Certain characters as the first char of
475 * the pattern have special meaning:
476 * ! Toggle the NO_MATCH flag
477 * * Toggle the PAST_EOF flag
478 * @ Toggle the FIRST_FILE flag
480 if (len_cmdbuf() > 0)
488 case CONTROL('E'): /* ignore END of file */
490 flag = SRCH_PAST_EOF;
495 case CONTROL('F'): /* FIRST file */
497 flag = SRCH_FIRST_FILE;
499 case CONTROL('K'): /* KEEP position */
503 case CONTROL('R'): /* Don't use REGULAR EXPRESSIONS */
504 flag = SRCH_NO_REGEX;
506 case CONTROL('N'): /* NOT match */
508 flag = SRCH_NO_MATCH;
522 * Handle a character of a multi-character command.
533 * We're not in a multicharacter command.
539 * In the prefix of a command.
540 * This not considered a multichar command
541 * (even tho it uses cmdbuf, etc.).
542 * It is handled in the commands() switch.
548 * Entering digits of a number.
549 * Terminated by a non-digit.
551 if (!((c >= '0' && c <= '9') || c == '.') &&
552 editchar(c, EC_PEEK|EC_NOHISTORY|EC_NOCOMPLETE|EC_NORIGHTLEFT) == A_INVALID)
555 * Not part of the number.
556 * End the number and treat this char
557 * as a normal command character.
559 number = cmd_int(&fraction);
567 ret = mca_opt_char(c);
575 ret = mca_search_char(c);
581 /* Other multicharacter command. */
586 * The multichar command is terminated by a newline.
588 if (c == '\n' || c == '\r')
591 * Execute the command.
598 * Append the char to the command buffer.
600 if (cmd_char(c) == CC_QUIT)
602 * Abort the multi-char command.
606 if ((mca == A_F_BRACKET || mca == A_B_BRACKET) && len_cmdbuf() >= 2)
609 * Special case for the bracket-matching commands.
610 * Execute the command after getting exactly two
611 * characters from the user.
618 * Need another character.
624 * Discard any buffered file data.
629 if (!(ch_getflags() & CH_CANSEEK))
639 * Make sure the screen is displayed.
645 * If nothing is displayed yet, display starting from initial_scrpos.
649 if (initial_scrpos.pos == NULL_POSITION)
651 * {{ Maybe this should be:
652 * jump_loc(ch_zero(), jump_sline);
653 * but this behavior seems rather unexpected
654 * on the first screen. }}
656 jump_loc(ch_zero(), 1);
658 jump_loc(initial_scrpos.pos, initial_scrpos.ln);
659 } else if (screen_trashed)
661 int save_top_scroll = top_scroll;
662 int save_ignore_eoi = ignore_eoi;
665 if (screen_trashed == 2)
667 /* Special case used by ignore_eoi: re-open the input file
668 * and jump to the end of the file. */
673 top_scroll = save_top_scroll;
674 ignore_eoi = save_ignore_eoi;
679 * Display the appropriate prompt.
686 if (ungot != NULL && !ungot->ug_end_command)
689 * No prompt necessary if commands are from
690 * ungotten chars rather than from the user.
696 * Make sure the screen is displayed.
699 bottompos = position(BOTTOM_PLUS_ONE);
702 * If we've hit EOF on the last file and the -E flag is set, quit.
704 if (get_quit_at_eof() == OPT_ONPLUS &&
705 eof_displayed() && !(ch_getflags() & CH_HELPFILE) &&
706 next_ifile(curr_ifile) == NULL_IFILE)
710 * If the entire file is displayed and the -F flag is set, quit.
712 if (quit_if_one_screen &&
713 entire_file_displayed() && !(ch_getflags() & CH_HELPFILE) &&
714 next_ifile(curr_ifile) == NULL_IFILE)
717 #if MSDOS_COMPILER==WIN32C
719 * In Win32, display the file name in the window title.
721 if (!(ch_getflags() & CH_HELPFILE))
722 SetConsoleTitle(pr_expand("Less?f - %f.", 0));
725 * Select the proper prompt and display it.
728 * If the previous action was a forward movement,
729 * don't clear the bottom line of the display;
730 * just print the prompt since the forward movement guarantees
731 * that we're in the right position to display the prompt.
732 * Clearing the line could cause a problem: for example, if the last
733 * line displayed ended at the right screen edge without a newline,
734 * then clearing would clear the last displayed line rather than
744 if (p == NULL || *p == '\0')
748 at_enter(AT_STANDOUT);
756 * Display the less version message.
763 parg.p_string = version;
764 error("less %s", &parg);
768 * Get command character.
769 * The character normally comes from the keyboard,
770 * but may come from ungotten characters
771 * (characters previously given to ungetcc or ungetsc).
779 * Normal case: no ungotten chars, so get one from the user.
785 * Return the next ungotten char.
788 struct ungot *ug = ungot;
789 char c = ug->ug_char;
790 int end_command = ug->ug_end_command;
796 * Command is incomplete, so try to complete it.
802 * We have a number but no command. Treat as #g.
809 * We have "/string" but no newline. Add the \n.
815 * Some other incomplete command. Let user complete it.
825 * "Unget" a command character.
826 * The next getcc() will return this character.
831 struct ungot *ug = (struct ungot *) ecalloc(1, sizeof(struct ungot));
833 ug->ug_char = (char) c;
834 ug->ug_end_command = (c == CHAR_END_COMMAND);
840 * Unget a whole string of command characters.
841 * The next sequence of getcc()'s will return this string.
848 for (p = s + strlen(s) - 1; p >= s; p--)
853 * Search for a pattern, possibly in multiple files.
854 * If SRCH_FIRST_FILE is set, begin searching at the first file.
855 * If SRCH_PAST_EOF is set, continue the search thru multiple files.
858 multi_search(char *pattern, int n, int silent)
865 save_ifile = save_curr_ifile();
867 if (search_type & SRCH_FIRST_FILE)
870 * Start at the first (or last) file
871 * in the command line list.
873 if (search_type & SRCH_FORW)
874 nomore = edit_first();
876 nomore = edit_last();
879 unsave_ifile(save_ifile);
883 search_type &= ~SRCH_FIRST_FILE;
888 n = search(search_type, pattern, n);
890 * The SRCH_NO_MOVE flag doesn't "stick": it gets cleared
891 * after being used once. This allows "n" to work after
892 * using a /@@ search.
894 search_type &= ~SRCH_NO_MOVE;
900 unsave_ifile(save_ifile);
906 * Some kind of error in the search.
907 * Error message has been printed by search().
911 if ((search_type & SRCH_PAST_EOF) == 0)
913 * We didn't find a match, but we're
914 * supposed to search only one file.
918 * Move on to the next file.
920 if (search_type & SRCH_FORW)
921 nomore = edit_next(1);
923 nomore = edit_prev(1);
931 * Print an error message if we haven't already.
933 if (n > 0 && !silent)
934 error("Pattern not found", NULL_PARG);
939 * Restore the file we were originally viewing.
941 reedit_ifile(save_ifile);
944 unsave_ifile(save_ifile);
949 * Forward forever, or until a highlighted line appears.
952 forw_loop(int until_hilite)
956 if (ch_getflags() & CH_HELPFILE)
960 jump_forw_buffered();
961 curr_len = ch_length();
962 highest_hilite = until_hilite ? curr_len : NULL_POSITION;
966 if (until_hilite && highest_hilite > curr_len)
978 * This gets us back in "F mode" after processing
979 * a non-abort signal (e.g. window-change).
981 if (sigs && !ABORT_SIGS())
982 return (until_hilite ? A_F_UNTIL_HILITE : A_F_FOREVER);
988 * Main command processor.
989 * Accept and execute commands until a quit command.
998 int save_search_type;
1006 search_type = SRCH_FORW;
1007 wscroll = (sc_height + 1) / 2;
1008 newaction = A_NOACTION;
1018 * See if any signals need processing.
1024 quit(QUIT_SAVED_STATUS);
1028 * See if window size changed, for systems that don't
1029 * generate SIGWINCH.
1034 * Display prompt and accept a character.
1040 if (newaction == A_NOACTION)
1047 if (newaction != A_NOACTION)
1050 newaction = A_NOACTION;
1054 * If we are in a multicharacter command, call mca_char.
1055 * Otherwise we call fcmd_decode to determine the
1056 * action to be performed.
1059 switch (mca_char(c))
1063 * Need another character.
1069 * Command has been handled by mca_char.
1070 * Start clean with a prompt.
1075 * Not a multi-char command
1076 * (at least, not anymore).
1082 * Decode the command character and decide what to do.
1087 * We're in a multichar command.
1088 * Add the character to the command buffer
1089 * and display it on the screen.
1090 * If the user backspaces past the start
1091 * of the line, abort the command.
1093 if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0)
1095 cbuf = get_cmdbuf();
1099 * Don't use cmd_char if we're starting fresh
1100 * at the beginning of a command, because we
1101 * don't want to echo the command until we know
1102 * it is a multichar command. We also don't
1103 * want erase_char/kill_char to be treated
1104 * as line editing characters.
1111 action = fcmd_decode(cbuf, &extra);
1113 * If an "extra" string was returned,
1114 * process it as a string of command characters.
1120 * Clear the cmdbuf string.
1121 * (But not if we're in the prefix of a command,
1122 * because the partial command string is kept there.)
1124 if (action != A_PREFIX)
1131 * First digit of a number.
1133 start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE);
1138 * Forward one window (and set the window size).
1141 swindow = (int) number;
1145 * Forward one screen.
1148 number = get_swindow();
1151 set_attnpos(bottompos);
1152 forward((int) number, 0, 1);
1157 * Backward one window (and set the window size).
1160 swindow = (int) number;
1164 * Backward one screen.
1167 number = get_swindow();
1169 backward((int) number, 0, 1);
1174 * Forward N (default 1) line.
1179 if (show_attn == OPT_ONPLUS && number > 1)
1180 set_attnpos(bottompos);
1181 forward((int) number, 0, 0);
1186 * Backward N (default 1) line.
1191 backward((int) number, 0, 0);
1196 * Force forward N (default 1) line.
1201 if (show_attn == OPT_ONPLUS && number > 1)
1202 set_attnpos(bottompos);
1203 forward((int) number, 1, 0);
1208 * Force backward N (default 1) line.
1213 backward((int) number, 1, 0);
1218 * Force forward one screen.
1221 number = get_swindow();
1223 if (show_attn == OPT_ONPLUS)
1224 set_attnpos(bottompos);
1225 forward((int) number, 1, 0);
1230 * Forward forever, ignoring EOF.
1233 set_attnpos(bottompos);
1234 newaction = forw_loop(0);
1237 case A_F_UNTIL_HILITE:
1238 newaction = forw_loop(1);
1244 * (default same as last 'd' or 'u' command).
1247 wscroll = (int) number;
1249 if (show_attn == OPT_ONPLUS)
1250 set_attnpos(bottompos);
1251 forward(wscroll, 0, 0);
1257 * (default same as last 'd' or 'u' command).
1260 wscroll = (int) number;
1262 backward(wscroll, 0, 0);
1267 * Flush buffers, then repaint screen.
1268 * Don't flush the buffers on a pipe!
1282 * Go to line N, default beginning of file.
1292 * Go to a specified percentage into the file.
1305 jump_percent((int) number, fraction);
1310 * Go to line N, default end of file.
1321 * Go to line N, default last buffered byte.
1325 jump_forw_buffered();
1332 * Go to a specified byte position in the file.
1337 jump_line_loc((POSITION) number, jump_sline);
1342 * Print file name, etc.
1344 if (ch_getflags() & CH_HELPFILE)
1347 parg.p_string = eq_message();
1353 * Print version number, without the "@(#)".
1363 if (curr_ifile != NULL_IFILE &&
1364 ch_getflags() & CH_HELPFILE)
1367 * Quit while viewing the help file
1368 * just means return to viewing the
1371 hshift = save_hshift;
1372 bs_mode = save_bs_mode;
1373 if (edit_prev(1) == 0)
1382 * Define abbreviation for a commonly used sequence below.
1384 #define DO_SEARCH() \
1385 if (number <= 0) number = 1; \
1388 multi_search((char *)NULL, (int) number, 0);
1393 * Search forward for a pattern.
1394 * Get the first char of the pattern.
1396 search_type = SRCH_FORW;
1405 * Search backward for a pattern.
1406 * Get the first char of the pattern.
1408 search_type = SRCH_BACK;
1417 search_type = SRCH_FORW | SRCH_FILTER;
1422 error("Command not available", NULL_PARG);
1426 case A_AGAIN_SEARCH:
1428 * Repeat previous search.
1433 case A_T_AGAIN_SEARCH:
1435 * Repeat previous search, multiple files.
1437 search_type |= SRCH_PAST_EOF;
1441 case A_REVERSE_SEARCH:
1443 * Repeat previous search, in reverse direction.
1445 save_search_type = search_type;
1446 search_type = SRCH_REVERSE(search_type);
1448 search_type = save_search_type;
1451 case A_T_REVERSE_SEARCH:
1453 * Repeat previous search,
1454 * multiple files in reverse direction.
1456 save_search_type = search_type;
1457 search_type = SRCH_REVERSE(search_type);
1458 search_type |= SRCH_PAST_EOF;
1460 search_type = save_search_type;
1471 if (ch_getflags() & CH_HELPFILE)
1474 save_hshift = hshift;
1476 save_bs_mode = bs_mode;
1477 bs_mode = BS_SPECIAL;
1478 (void) edit(FAKE_HELPFILE);
1484 * Edit a new file. Get the filename.
1488 error("Command not available", NULL_PARG);
1491 start_mca(A_EXAMINE, "Examine: ", ml_examine, 0);
1495 error("Command not available", NULL_PARG);
1501 * Invoke an editor on the input file.
1506 error("Command not available", NULL_PARG);
1509 if (ch_getflags() & CH_HELPFILE)
1511 if (strcmp(get_filename(curr_ifile), "-") == 0)
1513 error("Cannot edit standard input", NULL_PARG);
1516 if (curr_altfilename != NULL)
1518 error("WARNING: This file was viewed via LESSOPEN",
1521 start_mca(A_SHELL, "!", ml_shell, 0);
1523 * Expand the editor prototype string
1524 * and pass it to the system to execute.
1525 * (Make sure the screen is displayed so the
1526 * expansion of "+%lm" works.)
1530 lsystem(pr_expand(editproto, 0), (char*)NULL);
1533 error("Command not available", NULL_PARG);
1539 * Examine next file.
1544 error("No next file", NULL_PARG);
1550 if (edit_next((int) number))
1552 if (get_quit_at_eof() && eof_displayed() &&
1553 !(ch_getflags() & CH_HELPFILE))
1555 parg.p_string = (number > 1) ? "(N-th) " : "";
1556 error("No %snext file", &parg);
1562 * Examine previous file.
1567 error("No previous file", NULL_PARG);
1573 if (edit_prev((int) number))
1575 parg.p_string = (number > 1) ? "(N-th) " : "";
1576 error("No %sprevious file", &parg);
1584 tagfile = nexttag((int) number);
1585 if (tagfile == NULL)
1587 error("No next tag", NULL_PARG);
1590 if (edit(tagfile) == 0)
1592 POSITION pos = tagsearch();
1593 if (pos != NULL_POSITION)
1594 jump_loc(pos, jump_sline);
1597 error("Command not available", NULL_PARG);
1605 tagfile = prevtag((int) number);
1606 if (tagfile == NULL)
1608 error("No previous tag", NULL_PARG);
1611 if (edit(tagfile) == 0)
1613 POSITION pos = tagsearch();
1614 if (pos != NULL_POSITION)
1615 jump_loc(pos, jump_sline);
1618 error("Command not available", NULL_PARG);
1624 * Examine a particular file.
1628 if (edit_index((int) number))
1629 error("No such file", NULL_PARG);
1633 if (ch_getflags() & CH_HELPFILE)
1635 old_ifile = curr_ifile;
1636 new_ifile = getoff_ifile(curr_ifile);
1637 if (new_ifile == NULL_IFILE)
1642 if (edit_ifile(new_ifile) != 0)
1644 reedit_ifile(old_ifile);
1647 del_ifile(old_ifile);
1651 optflag = OPT_TOGGLE;
1659 * Report a flag setting.
1661 optflag = OPT_NO_TOGGLE;
1669 * Set an initial command for new files.
1671 start_mca(A_FIRSTCMD, "+", (void*)NULL, 0);
1682 error("Command not available", NULL_PARG);
1685 start_mca(A_SHELL, "!", ml_shell, 0);
1689 error("Command not available", NULL_PARG);
1697 if (ch_getflags() & CH_HELPFILE)
1699 start_mca(A_SETMARK, "mark: ", (void*)NULL, 0);
1701 if (c == erase_char || c == erase2_char ||
1702 c == kill_char || c == '\n' || c == '\r')
1711 start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0);
1713 if (c == erase_char || c == erase2_char ||
1714 c == kill_char || c == '\n' || c == '\r')
1724 error("Command not available", NULL_PARG);
1727 start_mca(A_PIPE, "|mark: ", (void*)NULL, 0);
1729 if (c == erase_char || c == erase2_char || c == kill_char)
1731 if (c == '\n' || c == '\r')
1736 start_mca(A_PIPE, "!", ml_shell, 0);
1740 error("Command not available", NULL_PARG);
1746 start_mca(action, "Brackets: ", (void*)NULL, 0);
1752 shift_count = number;
1754 number = (shift_count > 0) ?
1755 shift_count : sc_width / 2;
1756 if (number > hshift)
1764 shift_count = number;
1766 number = (shift_count > 0) ?
1767 shift_count : sc_width / 2;
1774 * The command is incomplete (more chars are needed).
1775 * Display the current char, so the user knows
1776 * what's going on, and get another character.
1778 if (mca != A_PREFIX)
1781 start_mca(A_PREFIX, " ", (void*)NULL,