2 * Copyright (C) 1984-2015 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, see the README file.
12 * User-level command processor.
16 #if MSDOS_COMPILER==WIN32C
23 extern int erase_char, erase2_char, kill_char;
25 extern int quit_if_one_screen;
30 extern int jump_sline;
33 extern int top_scroll;
34 extern int ignore_eoi;
39 extern POSITION highest_hilite;
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;
58 extern int same_pos_bell;
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;
73 static int save_bs_mode;
79 struct ungot *ug_next;
83 static struct ungot* ungot = NULL;
85 static void multi_search();
88 * Move the cursor to start of prompt line before executing a command.
89 * This looks nicer if the command takes a long time before
90 * updating the screen.
103 * Set up the display to start a new multi-character command.
106 start_mca(action, prompt, mlist, cmdflags)
108 constant char *prompt;
109 constant void *mlist;
116 set_mlist(mlist, cmdflags);
122 return (mca != 0 && mca != A_PREFIX);
126 * Set up the display to start a new search command.
132 if (search_type & SRCH_FILTER)
136 if (search_type & SRCH_FORW)
144 if (search_type & SRCH_NO_MATCH)
145 cmd_putstr("Non-match ");
146 if (search_type & SRCH_FIRST_FILE)
147 cmd_putstr("First-file ");
148 if (search_type & SRCH_PAST_EOF)
149 cmd_putstr("EOF-ignore ");
150 if (search_type & SRCH_NO_MOVE)
151 cmd_putstr("Keep-pos ");
152 if (search_type & SRCH_NO_REGEX)
153 cmd_putstr("Regex-off ");
156 if (search_type & SRCH_FILTER)
160 if (search_type & SRCH_FORW)
165 set_mlist(ml_search, 0);
169 * Set up the display to start a new toggle-option command.
178 no_prompt = (optflag & OPT_NO_PROMPT);
179 flag = (optflag & ~OPT_NO_PROMPT);
180 dash = (flag == OPT_NO_TOGGLE) ? "_" : "-";
204 * Execute a multicharacter command.
218 multi_search(cbuf, (int) number, 0);
222 search_type ^= SRCH_NO_MATCH;
223 set_filter_pattern(cbuf, search_type);
228 * Skip leading spaces or + signs in the string.
230 while (*cbuf == '+' || *cbuf == ' ')
232 if (every_first_cmd != NULL)
233 free(every_first_cmd);
235 every_first_cmd = NULL;
237 every_first_cmd = save(cbuf);
240 toggle_option(curropt, opt_lower, cbuf, optflag);
244 match_brac(cbuf[0], cbuf[1], 1, (int) number);
247 match_brac(cbuf[1], cbuf[0], 0, (int) number);
255 /* If tag structure is loaded then clean it up. */
263 * !! just uses whatever is in shellcmd.
264 * Otherwise, copy cmdbuf to shellcmd,
265 * expanding any special characters ("%" or "#").
269 if (shellcmd != NULL)
271 shellcmd = fexpand(cbuf);
276 if (shellcmd == NULL)
277 lsystem("", "!done");
279 lsystem(shellcmd, "!done");
286 (void) pipe_mark(pipec, cbuf);
287 error("|done", NULL_PARG);
294 * Is a character an erase or kill char?
300 return (c == erase_char || c == erase2_char || c == kill_char);
304 * Handle the first char of an option (after the initial dash).
307 mca_opt_first_char(c)
310 int flag = (optflag & ~OPT_NO_PROMPT);
311 if (flag == OPT_NO_TOGGLE)
316 /* "__" = long option name. */
327 optflag = (flag == OPT_UNSET) ?
328 OPT_TOGGLE : OPT_UNSET;
333 optflag = (flag == OPT_SET) ?
334 OPT_TOGGLE : OPT_SET;
338 optflag ^= OPT_NO_PROMPT;
342 /* "--" = long option name. */
348 /* Char was not handled here. */
353 * Add a char to a long option name.
354 * See if we've got a match for an option name yet.
355 * If so, display the complete name and stop
356 * accepting chars until user hits RETURN.
359 mca_opt_nonfirst_char(c)
368 * Already have a match for the name.
369 * Don't accept anything but erase/kill.
371 if (is_erase_char(c))
376 * Add char to cmd buffer and try to match
379 if (cmd_char(c) == CC_QUIT)
382 opt_lower = ASCII_IS_LOWER(p[0]);
383 curropt = findopt_name(&p, &oname, NULL);
388 * Remember the option and
389 * display the full option name.
393 for (p = oname; *p != '\0'; p++)
396 if (!opt_lower && ASCII_IS_LOWER(c))
397 c = ASCII_TO_UPPER(c);
398 if (cmd_char(c) != CC_OK)
406 * Handle a char of an option toggle command.
415 * This may be a short option (single char),
416 * or one char of a long option name,
417 * or one char of the option parameter.
419 if (curropt == NULL && len_cmdbuf() == 0)
421 int ret = mca_opt_first_char(c);
427 /* We're getting a long option name. */
428 if (c != '\n' && c != '\r')
429 return (mca_opt_nonfirst_char(c));
432 parg.p_string = get_cmdbuf();
433 error("There is no --%s option", &parg);
440 if (is_erase_char(c))
443 /* We're getting the option parameter. */
445 curropt = findopt(c);
448 parg.p_string = propt(c);
449 error("There is no %s option", &parg);
454 * If the option which was entered does not take a
455 * parameter, toggle the option immediately,
456 * so user doesn't have to hit RETURN.
458 if ((optflag & ~OPT_NO_PROMPT) != OPT_TOGGLE ||
459 !opt_has_param(curropt))
461 toggle_option(curropt, ASCII_IS_LOWER(c), "", optflag);
465 * Display a prompt appropriate for the option parameter.
467 start_mca(A_OPT_TOGGLE, opt_prompt(curropt), (void*)NULL, 0);
472 * Handle a char of a search command.
481 * Certain characters as the first char of
482 * the pattern have special meaning:
483 * ! Toggle the NO_MATCH flag
484 * * Toggle the PAST_EOF flag
485 * @ Toggle the FIRST_FILE flag
487 if (len_cmdbuf() > 0)
492 case CONTROL('E'): /* ignore END of file */
495 flag = SRCH_PAST_EOF;
497 case CONTROL('F'): /* FIRST file */
500 flag = SRCH_FIRST_FILE;
502 case CONTROL('K'): /* KEEP position */
506 case CONTROL('R'): /* Don't use REGULAR EXPRESSIONS */
507 flag = SRCH_NO_REGEX;
509 case CONTROL('N'): /* NOT match */
511 flag = SRCH_NO_MATCH;
525 * Handle a character of a multi-character command.
537 * We're not in a multicharacter command.
543 * In the prefix of a command.
544 * This not considered a multichar command
545 * (even tho it uses cmdbuf, etc.).
546 * It is handled in the commands() switch.
552 * Entering digits of a number.
553 * Terminated by a non-digit.
555 if (!((c >= '0' && c <= '9') || c == '.') &&
556 editchar(c, EC_PEEK|EC_NOHISTORY|EC_NOCOMPLETE|EC_NORIGHTLEFT) == A_INVALID)
559 * Not part of the number.
560 * End the number and treat this char
561 * as a normal command character.
563 number = cmd_int(&fraction);
571 ret = mca_opt_char(c);
579 ret = mca_search_char(c);
585 /* Other multicharacter command. */
590 * The multichar command is terminated by a newline.
592 if (c == '\n' || c == '\r')
595 * Execute the command.
602 * Append the char to the command buffer.
604 if (cmd_char(c) == CC_QUIT)
606 * Abort the multi-char command.
610 if ((mca == A_F_BRACKET || mca == A_B_BRACKET) && len_cmdbuf() >= 2)
613 * Special case for the bracket-matching commands.
614 * Execute the command after getting exactly two
615 * characters from the user.
622 * Need another character.
628 * Discard any buffered file data.
633 if (!(ch_getflags() & CH_CANSEEK))
643 * Make sure the screen is displayed.
649 * If nothing is displayed yet, display starting from initial_scrpos.
653 if (initial_scrpos.pos == NULL_POSITION)
655 * {{ Maybe this should be:
656 * jump_loc(ch_zero(), jump_sline);
657 * but this behavior seems rather unexpected
658 * on the first screen. }}
660 jump_loc(ch_zero(), 1);
662 jump_loc(initial_scrpos.pos, initial_scrpos.ln);
663 } else if (screen_trashed)
665 int save_top_scroll = top_scroll;
666 int save_ignore_eoi = ignore_eoi;
669 if (screen_trashed == 2)
671 /* Special case used by ignore_eoi: re-open the input file
672 * and jump to the end of the file. */
677 top_scroll = save_top_scroll;
678 ignore_eoi = save_ignore_eoi;
683 * Display the appropriate prompt.
688 register constant char *p;
690 if (ungot != NULL && !ungot->ug_end_command)
693 * No prompt necessary if commands are from
694 * ungotten chars rather than from the user.
700 * Make sure the screen is displayed.
703 bottompos = position(BOTTOM_PLUS_ONE);
706 * If we've hit EOF on the last file and the -E flag is set, quit.
708 if (get_quit_at_eof() == OPT_ONPLUS &&
709 eof_displayed() && !(ch_getflags() & CH_HELPFILE) &&
710 next_ifile(curr_ifile) == NULL_IFILE)
714 * If the entire file is displayed and the -F flag is set, quit.
716 if (quit_if_one_screen &&
717 entire_file_displayed() && !(ch_getflags() & CH_HELPFILE) &&
718 next_ifile(curr_ifile) == NULL_IFILE)
721 #if MSDOS_COMPILER==WIN32C
723 * In Win32, display the file name in the window title.
725 if (!(ch_getflags() & CH_HELPFILE))
726 SetConsoleTitle(pr_expand("Less?f - %f.", 0));
729 * Select the proper prompt and display it.
732 * If the previous action was a forward movement,
733 * don't clear the bottom line of the display;
734 * just print the prompt since the forward movement guarantees
735 * that we're in the right position to display the prompt.
736 * Clearing the line could cause a problem: for example, if the last
737 * line displayed ended at the right screen edge without a newline,
738 * then clearing would clear the last displayed line rather than
748 if (p == NULL || *p == '\0')
752 at_enter(AT_STANDOUT);
760 * Display the less version message.
767 parg.p_string = version;
768 error("less %s", &parg);
772 * Get command character.
773 * The character normally comes from the keyboard,
774 * but may come from ungotten characters
775 * (characters previously given to ungetcc or ungetsc).
783 * Normal case: no ungotten chars, so get one from the user.
789 * Return the next ungotten char.
792 struct ungot *ug = ungot;
793 char c = ug->ug_char;
794 int end_command = ug->ug_end_command;
800 * Command is incomplete, so try to complete it.
806 * We have a number but no command. Treat as #g.
813 * We have "/string" but no newline. Add the \n.
819 * Some other incomplete command. Let user complete it.
829 * "Unget" a command character.
830 * The next getcc() will return this character.
836 struct ungot *ug = (struct ungot *) ecalloc(1, sizeof(struct ungot));
838 ug->ug_char = (char) c;
839 ug->ug_end_command = (c == CHAR_END_COMMAND);
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, silent)
874 save_ifile = save_curr_ifile();
876 if (search_type & SRCH_FIRST_FILE)
879 * Start at the first (or last) file
880 * in the command line list.
882 if (search_type & SRCH_FORW)
883 nomore = edit_first();
885 nomore = edit_last();
888 unsave_ifile(save_ifile);
892 search_type &= ~SRCH_FIRST_FILE;
897 n = search(search_type, pattern, n);
899 * The SRCH_NO_MOVE flag doesn't "stick": it gets cleared
900 * after being used once. This allows "n" to work after
901 * using a /@@ search.
903 search_type &= ~SRCH_NO_MOVE;
909 unsave_ifile(save_ifile);
915 * Some kind of error in the search.
916 * Error message has been printed by search().
920 if ((search_type & SRCH_PAST_EOF) == 0)
922 * We didn't find a match, but we're
923 * supposed to search only one file.
927 * Move on to the next file.
929 if (search_type & SRCH_FORW)
930 nomore = edit_next(1);
932 nomore = edit_prev(1);
940 * Print an error message if we haven't already.
942 if (n > 0 && !silent)
943 error("Pattern not found", NULL_PARG);
948 * Restore the file we were originally viewing.
950 reedit_ifile(save_ifile);
953 unsave_ifile(save_ifile);
958 * Forward forever, or until a highlighted line appears.
961 forw_loop(until_hilite)
966 if (ch_getflags() & CH_HELPFILE)
970 jump_forw_buffered();
971 curr_len = ch_length();
972 highest_hilite = until_hilite ? curr_len : NULL_POSITION;
976 if (until_hilite && highest_hilite > curr_len)
988 * This gets us back in "F mode" after processing
989 * a non-abort signal (e.g. window-change).
991 if (sigs && !ABORT_SIGS())
992 return (until_hilite ? A_F_UNTIL_HILITE : A_F_FOREVER);
998 * Main command processor.
999 * Accept and execute commands until a quit command.
1005 register int action;
1006 register char *cbuf;
1008 int save_search_type;
1016 search_type = SRCH_FORW;
1017 wscroll = (sc_height + 1) / 2;
1018 newaction = A_NOACTION;
1028 * See if any signals need processing.
1034 quit(QUIT_SAVED_STATUS);
1038 * See if window size changed, for systems that don't
1039 * generate SIGWINCH.
1044 * Display prompt and accept a character.
1050 if (newaction == A_NOACTION)
1057 if (newaction != A_NOACTION)
1060 newaction = A_NOACTION;
1064 * If we are in a multicharacter command, call mca_char.
1065 * Otherwise we call fcmd_decode to determine the
1066 * action to be performed.
1069 switch (mca_char(c))
1073 * Need another character.
1079 * Command has been handled by mca_char.
1080 * Start clean with a prompt.
1085 * Not a multi-char command
1086 * (at least, not anymore).
1092 * Decode the command character and decide what to do.
1097 * We're in a multichar command.
1098 * Add the character to the command buffer
1099 * and display it on the screen.
1100 * If the user backspaces past the start
1101 * of the line, abort the command.
1103 if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0)
1105 cbuf = get_cmdbuf();
1109 * Don't use cmd_char if we're starting fresh
1110 * at the beginning of a command, because we
1111 * don't want to echo the command until we know
1112 * it is a multichar command. We also don't
1113 * want erase_char/kill_char to be treated
1114 * as line editing characters.
1121 action = fcmd_decode(cbuf, &extra);
1123 * If an "extra" string was returned,
1124 * process it as a string of command characters.
1130 * Clear the cmdbuf string.
1131 * (But not if we're in the prefix of a command,
1132 * because the partial command string is kept there.)
1134 if (action != A_PREFIX)
1141 * First digit of a number.
1143 start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE);
1148 * Forward one window (and set the window size).
1151 swindow = (int) number;
1155 * Forward one screen.
1158 number = get_swindow();
1161 set_attnpos(bottompos);
1162 forward((int) number, 0, 1);
1167 * Backward one window (and set the window size).
1170 swindow = (int) number;
1174 * Backward one screen.
1177 number = get_swindow();
1179 backward((int) number, 0, 1);
1184 * Forward N (default 1) line.
1189 if (show_attn == OPT_ONPLUS && number > 1)
1190 set_attnpos(bottompos);
1191 forward((int) number, 0, 0);
1196 * Backward N (default 1) line.
1201 backward((int) number, 0, 0);
1206 * Force forward N (default 1) line.
1211 if (show_attn == OPT_ONPLUS && number > 1)
1212 set_attnpos(bottompos);
1213 forward((int) number, 1, 0);
1218 * Force backward N (default 1) line.
1223 backward((int) number, 1, 0);
1228 * Force forward one screen.
1231 number = get_swindow();
1233 if (show_attn == OPT_ONPLUS)
1234 set_attnpos(bottompos);
1235 forward((int) number, 1, 0);
1240 * Forward forever, ignoring EOF.
1243 set_attnpos(bottompos);
1244 newaction = forw_loop(0);
1247 case A_F_UNTIL_HILITE:
1248 newaction = forw_loop(1);
1254 * (default same as last 'd' or 'u' command).
1257 wscroll = (int) number;
1259 if (show_attn == OPT_ONPLUS)
1260 set_attnpos(bottompos);
1261 forward(wscroll, 0, 0);
1267 * (default same as last 'd' or 'u' command).
1270 wscroll = (int) number;
1272 backward(wscroll, 0, 0);
1277 * Flush buffers, then repaint screen.
1278 * Don't flush the buffers on a pipe!
1292 * Go to line N, default beginning of file.
1302 * Go to a specified percentage into the file.
1315 jump_percent((int) number, fraction);
1320 * Go to line N, default end of file.
1331 * Go to line N, default last buffered byte.
1335 jump_forw_buffered();
1342 * Go to a specified byte position in the file.
1347 jump_line_loc((POSITION) number, jump_sline);
1352 * Print file name, etc.
1354 if (ch_getflags() & CH_HELPFILE)
1357 parg.p_string = eq_message();
1363 * Print version number, without the "@(#)".
1373 if (curr_ifile != NULL_IFILE &&
1374 ch_getflags() & CH_HELPFILE)
1377 * Quit while viewing the help file
1378 * just means return to viewing the
1381 hshift = save_hshift;
1382 bs_mode = save_bs_mode;
1383 if (edit_prev(1) == 0)
1392 * Define abbreviation for a commonly used sequence below.
1394 #define DO_SEARCH() \
1395 if (number <= 0) number = 1; \
1398 multi_search((char *)NULL, (int) number, 0);
1403 * Search forward for a pattern.
1404 * Get the first char of the pattern.
1406 search_type = SRCH_FORW;
1415 * Search backward for a pattern.
1416 * Get the first char of the pattern.
1418 search_type = SRCH_BACK;
1427 search_type = SRCH_FORW | SRCH_FILTER;
1432 error("Command not available", NULL_PARG);
1436 case A_AGAIN_SEARCH:
1438 * Repeat previous search.
1443 case A_T_AGAIN_SEARCH:
1445 * Repeat previous search, multiple files.
1447 search_type |= SRCH_PAST_EOF;
1451 case A_REVERSE_SEARCH:
1453 * Repeat previous search, in reverse direction.
1455 save_search_type = search_type;
1456 search_type = SRCH_REVERSE(search_type);
1458 search_type = save_search_type;
1461 case A_T_REVERSE_SEARCH:
1463 * Repeat previous search,
1464 * multiple files in reverse direction.
1466 save_search_type = search_type;
1467 search_type = SRCH_REVERSE(search_type);
1468 search_type |= SRCH_PAST_EOF;
1470 search_type = save_search_type;
1481 if (ch_getflags() & CH_HELPFILE)
1484 save_hshift = hshift;
1486 save_bs_mode = bs_mode;
1487 bs_mode = BS_SPECIAL;
1488 (void) edit(FAKE_HELPFILE);
1494 * Edit a new file. Get the filename.
1498 error("Command not available", NULL_PARG);
1501 start_mca(A_EXAMINE, "Examine: ", ml_examine, 0);
1505 error("Command not available", NULL_PARG);
1511 * Invoke an editor on the input file.
1516 error("Command not available", NULL_PARG);
1519 if (ch_getflags() & CH_HELPFILE)
1521 if (strcmp(get_filename(curr_ifile), "-") == 0)
1523 error("Cannot edit standard input", NULL_PARG);
1526 if (curr_altfilename != NULL)
1528 error("WARNING: This file was viewed via LESSOPEN",
1531 start_mca(A_SHELL, "!", ml_shell, 0);
1533 * Expand the editor prototype string
1534 * and pass it to the system to execute.
1535 * (Make sure the screen is displayed so the
1536 * expansion of "+%lm" works.)
1540 lsystem(pr_expand(editproto, 0), (char*)NULL);
1543 error("Command not available", NULL_PARG);
1549 * Examine next file.
1554 error("No next file", NULL_PARG);
1560 if (edit_next((int) number))
1562 if (get_quit_at_eof() && eof_displayed() &&
1563 !(ch_getflags() & CH_HELPFILE))
1565 parg.p_string = (number > 1) ? "(N-th) " : "";
1566 error("No %snext file", &parg);
1572 * Examine previous file.
1577 error("No previous file", NULL_PARG);
1583 if (edit_prev((int) number))
1585 parg.p_string = (number > 1) ? "(N-th) " : "";
1586 error("No %sprevious file", &parg);
1594 tagfile = nexttag((int) number);
1595 if (tagfile == NULL)
1597 error("No next tag", NULL_PARG);
1600 if (edit(tagfile) == 0)
1602 POSITION pos = tagsearch();
1603 if (pos != NULL_POSITION)
1604 jump_loc(pos, jump_sline);
1607 error("Command not available", NULL_PARG);
1615 tagfile = prevtag((int) number);
1616 if (tagfile == NULL)
1618 error("No previous tag", NULL_PARG);
1621 if (edit(tagfile) == 0)
1623 POSITION pos = tagsearch();
1624 if (pos != NULL_POSITION)
1625 jump_loc(pos, jump_sline);
1628 error("Command not available", NULL_PARG);
1634 * Examine a particular file.
1638 if (edit_index((int) number))
1639 error("No such file", NULL_PARG);
1643 if (ch_getflags() & CH_HELPFILE)
1645 old_ifile = curr_ifile;
1646 new_ifile = getoff_ifile(curr_ifile);
1647 if (new_ifile == NULL_IFILE)
1652 if (edit_ifile(new_ifile) != 0)
1654 reedit_ifile(old_ifile);
1657 del_ifile(old_ifile);
1661 optflag = OPT_TOGGLE;
1669 * Report a flag setting.
1671 optflag = OPT_NO_TOGGLE;
1679 * Set an initial command for new files.
1681 start_mca(A_FIRSTCMD, "+", (void*)NULL, 0);
1692 error("Command not available", NULL_PARG);
1695 start_mca(A_SHELL, "!", ml_shell, 0);
1699 error("Command not available", NULL_PARG);
1707 if (ch_getflags() & CH_HELPFILE)
1709 start_mca(A_SETMARK, "mark: ", (void*)NULL, 0);
1711 if (c == erase_char || c == erase2_char ||
1712 c == kill_char || c == '\n' || c == '\r')
1721 start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0);
1723 if (c == erase_char || c == erase2_char ||
1724 c == kill_char || c == '\n' || c == '\r')
1734 error("Command not available", NULL_PARG);
1737 start_mca(A_PIPE, "|mark: ", (void*)NULL, 0);
1739 if (c == erase_char || c == erase2_char || c == kill_char)
1741 if (c == '\n' || c == '\r')
1746 start_mca(A_PIPE, "!", ml_shell, 0);
1750 error("Command not available", NULL_PARG);
1756 start_mca(action, "Brackets: ", (void*)NULL, 0);
1762 shift_count = number;
1764 number = (shift_count > 0) ?
1765 shift_count : sc_width / 2;
1766 if (number > hshift)
1774 shift_count = number;
1776 number = (shift_count > 0) ?
1777 shift_count : sc_width / 2;
1784 * The command is incomplete (more chars are needed).
1785 * Display the current char, so the user knows
1786 * what's going on, and get another character.
1788 if (mca != A_PREFIX)
1791 start_mca(A_PREFIX, " ", (void*)NULL,