2 * Copyright (C) 1984-2012 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;
38 extern POSITION highest_hilite;
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)
105 constant char *prompt;
106 constant void *mlist;
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)
305 int flag = (optflag & ~OPT_NO_PROMPT);
306 if (flag == OPT_NO_TOGGLE)
311 /* "__" = long option name. */
322 optflag = (flag == OPT_UNSET) ?
323 OPT_TOGGLE : OPT_UNSET;
328 optflag = (flag == OPT_SET) ?
329 OPT_TOGGLE : OPT_SET;
333 optflag ^= OPT_NO_PROMPT;
337 /* "--" = long option name. */
343 /* Char was not handled here. */
348 * Add a char to a long option name.
349 * See if we've got a match for an option name yet.
350 * If so, display the complete name and stop
351 * accepting chars until user hits RETURN.
354 mca_opt_nonfirst_char(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.
410 * This may be a short option (single char),
411 * or one char of a long option name,
412 * or one char of the option parameter.
414 if (curropt == NULL && len_cmdbuf() == 0)
416 int ret = mca_opt_first_char(c);
422 /* We're getting a long option name. */
423 if (c != '\n' && c != '\r')
424 return (mca_opt_nonfirst_char(c));
427 parg.p_string = get_cmdbuf();
428 error("There is no --%s option", &parg);
435 if (is_erase_char(c))
438 /* We're getting the option parameter. */
440 curropt = findopt(c);
443 parg.p_string = propt(c);
444 error("There is no %s option", &parg);
449 * If the option which was entered does not take a
450 * parameter, toggle the option immediately,
451 * so user doesn't have to hit RETURN.
453 if ((optflag & ~OPT_NO_PROMPT) != OPT_TOGGLE ||
454 !opt_has_param(curropt))
456 toggle_option(curropt, ASCII_IS_LOWER(c), "", optflag);
460 * Display a prompt appropriate for the option parameter.
462 start_mca(A_OPT_TOGGLE, opt_prompt(curropt), (void*)NULL, 0);
467 * Handle a char of a search command.
476 * Certain characters as the first char of
477 * the pattern have special meaning:
478 * ! Toggle the NO_MATCH flag
479 * * Toggle the PAST_EOF flag
480 * @ Toggle the FIRST_FILE flag
482 if (len_cmdbuf() > 0)
487 case CONTROL('E'): /* ignore END of file */
490 flag = SRCH_PAST_EOF;
492 case CONTROL('F'): /* FIRST file */
495 flag = SRCH_FIRST_FILE;
497 case CONTROL('K'): /* KEEP position */
501 case CONTROL('R'): /* Don't use REGULAR EXPRESSIONS */
502 flag = SRCH_NO_REGEX;
504 case CONTROL('N'): /* NOT match */
506 flag = SRCH_NO_MATCH;
520 * Handle a character of a multi-character command.
532 * We're not in a multicharacter command.
538 * In the prefix of a command.
539 * This not considered a multichar command
540 * (even tho it uses cmdbuf, etc.).
541 * It is handled in the commands() switch.
547 * Entering digits of a number.
548 * Terminated by a non-digit.
550 if (!((c >= '0' && c <= '9') || c == '.') &&
551 editchar(c, EC_PEEK|EC_NOHISTORY|EC_NOCOMPLETE|EC_NORIGHTLEFT) == A_INVALID)
554 * Not part of the number.
555 * End the number and treat this char
556 * as a normal command character.
558 number = cmd_int(&fraction);
566 ret = mca_opt_char(c);
574 ret = mca_search_char(c);
580 /* Other multicharacter command. */
585 * The multichar command is terminated by a newline.
587 if (c == '\n' || c == '\r')
590 * Execute the command.
597 * Append the char to the command buffer.
599 if (cmd_char(c) == CC_QUIT)
601 * Abort the multi-char command.
605 if ((mca == A_F_BRACKET || mca == A_B_BRACKET) && len_cmdbuf() >= 2)
608 * Special case for the bracket-matching commands.
609 * Execute the command after getting exactly two
610 * characters from the user.
617 * Need another character.
623 * Discard any buffered file data.
628 if (!(ch_getflags() & CH_CANSEEK))
638 * Make sure the screen is displayed.
644 * If nothing is displayed yet, display starting from initial_scrpos.
648 if (initial_scrpos.pos == NULL_POSITION)
650 * {{ Maybe this should be:
651 * jump_loc(ch_zero(), jump_sline);
652 * but this behavior seems rather unexpected
653 * on the first screen. }}
655 jump_loc(ch_zero(), 1);
657 jump_loc(initial_scrpos.pos, initial_scrpos.ln);
658 } else if (screen_trashed)
660 int save_top_scroll = top_scroll;
661 int save_ignore_eoi = ignore_eoi;
664 if (screen_trashed == 2)
666 /* Special case used by ignore_eoi: re-open the input file
667 * and jump to the end of the file. */
672 top_scroll = save_top_scroll;
673 ignore_eoi = save_ignore_eoi;
678 * Display the appropriate prompt.
683 register constant char *p;
688 * No prompt necessary if commands are from
689 * ungotten chars rather than from the user.
695 * Make sure the screen is displayed.
698 bottompos = position(BOTTOM_PLUS_ONE);
701 * If we've hit EOF on the last file and the -E flag is set, quit.
703 if (get_quit_at_eof() == OPT_ONPLUS &&
704 eof_displayed() && !(ch_getflags() & CH_HELPFILE) &&
705 next_ifile(curr_ifile) == NULL_IFILE)
709 * If the entire file is displayed and the -F flag is set, quit.
711 if (quit_if_one_screen &&
712 entire_file_displayed() && !(ch_getflags() & CH_HELPFILE) &&
713 next_ifile(curr_ifile) == NULL_IFILE)
716 #if MSDOS_COMPILER==WIN32C
718 * In Win32, display the file name in the window title.
720 if (!(ch_getflags() & CH_HELPFILE))
721 SetConsoleTitle(pr_expand("Less?f - %f.", 0));
724 * Select the proper prompt and display it.
727 * If the previous action was a forward movement,
728 * don't clear the bottom line of the display;
729 * just print the prompt since the forward movement guarantees
730 * that we're in the right position to display the prompt.
731 * Clearing the line could cause a problem: for example, if the last
732 * line displayed ended at the right screen edge without a newline,
733 * then clearing would clear the last displayed line rather than
743 if (p == NULL || *p == '\0')
747 at_enter(AT_STANDOUT);
755 * Display the less version message.
762 parg.p_string = version;
763 error("less %s", &parg);
767 * Get command character.
768 * The character normally comes from the keyboard,
769 * but may come from ungotten characters
770 * (characters previously given to ungetcc or ungetsc).
778 * We have just run out of ungotten chars.
781 if (len_cmdbuf() == 0 || !empty_screen())
784 * Command is incomplete, so try to complete it.
790 * We have a number but no command. Treat as #g.
797 * We have "/string" but no newline. Add the \n.
803 * Some other incomplete command. Let user complete it.
812 * Normal case: no ungotten chars, so get one from the user.
818 * Return the next ungotten char.
821 struct ungot *ug = ungot;
822 char c = ug->ug_char;
825 unget_end = (ungot == NULL);
831 * "Unget" a command character.
832 * The next getcc() will return this character.
838 struct ungot *ug = (struct ungot *) ecalloc(1, sizeof(struct ungot));
847 * Unget a whole string of command characters.
848 * The next sequence of getcc()'s will return this string.
856 for (p = s + strlen(s) - 1; p >= s; p--)
861 * Search for a pattern, possibly in multiple files.
862 * If SRCH_FIRST_FILE is set, begin searching at the first file.
863 * If SRCH_PAST_EOF is set, continue the search thru multiple files.
866 multi_search(pattern, n)
875 save_ifile = save_curr_ifile();
877 if (search_type & SRCH_FIRST_FILE)
880 * Start at the first (or last) file
881 * in the command line list.
883 if (search_type & SRCH_FORW)
884 nomore = edit_first();
886 nomore = edit_last();
889 unsave_ifile(save_ifile);
893 search_type &= ~SRCH_FIRST_FILE;
898 n = search(search_type, pattern, n);
900 * The SRCH_NO_MOVE flag doesn't "stick": it gets cleared
901 * after being used once. This allows "n" to work after
902 * using a /@@ search.
904 search_type &= ~SRCH_NO_MOVE;
910 unsave_ifile(save_ifile);
916 * Some kind of error in the search.
917 * Error message has been printed by search().
921 if ((search_type & SRCH_PAST_EOF) == 0)
923 * We didn't find a match, but we're
924 * supposed to search only one file.
928 * Move on to the next file.
930 if (search_type & SRCH_FORW)
931 nomore = edit_next(1);
933 nomore = edit_prev(1);
941 * Print an error message if we haven't already.
944 error("Pattern not found", NULL_PARG);
949 * Restore the file we were originally viewing.
951 reedit_ifile(save_ifile);
954 unsave_ifile(save_ifile);
959 * Forward forever, or until a highlighted line appears.
962 forw_loop(until_hilite)
967 if (ch_getflags() & CH_HELPFILE)
972 curr_len = ch_length();
973 highest_hilite = until_hilite ? curr_len : NULL_POSITION;
977 if (until_hilite && highest_hilite > curr_len)
989 * This gets us back in "F mode" after processing
990 * a non-abort signal (e.g. window-change).
992 if (sigs && !ABORT_SIGS())
993 return (until_hilite ? A_F_UNTIL_HILITE : A_F_FOREVER);
999 * Main command processor.
1000 * Accept and execute commands until a quit command.
1006 register int action;
1007 register char *cbuf;
1009 int save_search_type;
1016 int until_hilite = 0;
1018 search_type = SRCH_FORW;
1019 wscroll = (sc_height + 1) / 2;
1020 newaction = A_NOACTION;
1030 * See if any signals need processing.
1036 quit(QUIT_SAVED_STATUS);
1040 * See if window size changed, for systems that don't
1041 * generate SIGWINCH.
1046 * Display prompt and accept a character.
1052 if (newaction == A_NOACTION)
1059 if (newaction != A_NOACTION)
1062 newaction = A_NOACTION;
1066 * If we are in a multicharacter command, call mca_char.
1067 * Otherwise we call fcmd_decode to determine the
1068 * action to be performed.
1071 switch (mca_char(c))
1075 * Need another character.
1081 * Command has been handled by mca_char.
1082 * Start clean with a prompt.
1087 * Not a multi-char command
1088 * (at least, not anymore).
1094 * Decode the command character and decide what to do.
1099 * We're in a multichar command.
1100 * Add the character to the command buffer
1101 * and display it on the screen.
1102 * If the user backspaces past the start
1103 * of the line, abort the command.
1105 if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0)
1107 cbuf = get_cmdbuf();
1111 * Don't use cmd_char if we're starting fresh
1112 * at the beginning of a command, because we
1113 * don't want to echo the command until we know
1114 * it is a multichar command. We also don't
1115 * want erase_char/kill_char to be treated
1116 * as line editing characters.
1123 action = fcmd_decode(cbuf, &extra);
1125 * If an "extra" string was returned,
1126 * process it as a string of command characters.
1132 * Clear the cmdbuf string.
1133 * (But not if we're in the prefix of a command,
1134 * because the partial command string is kept there.)
1136 if (action != A_PREFIX)
1143 * First digit of a number.
1145 start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE);
1150 * Forward one window (and set the window size).
1153 swindow = (int) number;
1157 * Forward one screen.
1160 number = get_swindow();
1163 set_attnpos(bottompos);
1164 forward((int) number, 0, 1);
1169 * Backward one window (and set the window size).
1172 swindow = (int) number;
1176 * Backward one screen.
1179 number = get_swindow();
1181 backward((int) number, 0, 1);
1186 * Forward N (default 1) line.
1191 if (show_attn == OPT_ONPLUS && number > 1)
1192 set_attnpos(bottompos);
1193 forward((int) number, 0, 0);
1198 * Backward N (default 1) line.
1203 backward((int) number, 0, 0);
1208 * Force forward N (default 1) line.
1213 if (show_attn == OPT_ONPLUS && number > 1)
1214 set_attnpos(bottompos);
1215 forward((int) number, 1, 0);
1220 * Force backward N (default 1) line.
1225 backward((int) number, 1, 0);
1230 * Force forward one screen.
1233 number = get_swindow();
1235 if (show_attn == OPT_ONPLUS)
1236 set_attnpos(bottompos);
1237 forward((int) number, 1, 0);
1242 * Forward forever, ignoring EOF.
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 a specified byte position in the file.
1336 jump_line_loc((POSITION) number, jump_sline);
1341 * Print file name, etc.
1343 if (ch_getflags() & CH_HELPFILE)
1346 parg.p_string = eq_message();
1352 * Print version number, without the "@(#)".
1362 if (curr_ifile != NULL_IFILE &&
1363 ch_getflags() & CH_HELPFILE)
1366 * Quit while viewing the help file
1367 * just means return to viewing the
1370 hshift = save_hshift;
1371 if (edit_prev(1) == 0)
1380 * Define abbreviation for a commonly used sequence below.
1382 #define DO_SEARCH() \
1383 if (number <= 0) number = 1; \
1386 multi_search((char *)NULL, (int) number);
1391 * Search forward for a pattern.
1392 * Get the first char of the pattern.
1394 search_type = SRCH_FORW;
1403 * Search backward for a pattern.
1404 * Get the first char of the pattern.
1406 search_type = SRCH_BACK;
1415 search_type = SRCH_FORW | SRCH_FILTER;
1420 error("Command not available", NULL_PARG);
1424 case A_AGAIN_SEARCH:
1426 * Repeat previous search.
1431 case A_T_AGAIN_SEARCH:
1433 * Repeat previous search, multiple files.
1435 search_type |= SRCH_PAST_EOF;
1439 case A_REVERSE_SEARCH:
1441 * Repeat previous search, in reverse direction.
1443 save_search_type = search_type;
1444 search_type = SRCH_REVERSE(search_type);
1446 search_type = save_search_type;
1449 case A_T_REVERSE_SEARCH:
1451 * Repeat previous search,
1452 * multiple files in reverse direction.
1454 save_search_type = search_type;
1455 search_type = SRCH_REVERSE(search_type);
1456 search_type |= SRCH_PAST_EOF;
1458 search_type = save_search_type;
1469 if (ch_getflags() & CH_HELPFILE)
1472 save_hshift = hshift;
1474 (void) edit(FAKE_HELPFILE);
1480 * Edit a new file. Get the filename.
1484 error("Command not available", NULL_PARG);
1487 start_mca(A_EXAMINE, "Examine: ", ml_examine, 0);
1491 error("Command not available", NULL_PARG);
1497 * Invoke an editor on the input file.
1502 error("Command not available", NULL_PARG);
1505 if (ch_getflags() & CH_HELPFILE)
1507 if (strcmp(get_filename(curr_ifile), "-") == 0)
1509 error("Cannot edit standard input", NULL_PARG);
1512 if (curr_altfilename != NULL)
1514 error("WARNING: This file was viewed via LESSOPEN",
1517 start_mca(A_SHELL, "!", ml_shell, 0);
1519 * Expand the editor prototype string
1520 * and pass it to the system to execute.
1521 * (Make sure the screen is displayed so the
1522 * expansion of "+%lm" works.)
1526 lsystem(pr_expand(editproto, 0), (char*)NULL);
1529 error("Command not available", NULL_PARG);
1535 * Examine next file.
1540 error("No next file", NULL_PARG);
1546 if (edit_next((int) number))
1548 if (get_quit_at_eof() && eof_displayed() &&
1549 !(ch_getflags() & CH_HELPFILE))
1551 parg.p_string = (number > 1) ? "(N-th) " : "";
1552 error("No %snext file", &parg);
1558 * Examine previous file.
1563 error("No previous file", NULL_PARG);
1569 if (edit_prev((int) number))
1571 parg.p_string = (number > 1) ? "(N-th) " : "";
1572 error("No %sprevious file", &parg);
1580 tagfile = nexttag((int) number);
1581 if (tagfile == NULL)
1583 error("No next tag", NULL_PARG);
1586 if (edit(tagfile) == 0)
1588 POSITION pos = tagsearch();
1589 if (pos != NULL_POSITION)
1590 jump_loc(pos, jump_sline);
1593 error("Command not available", NULL_PARG);
1601 tagfile = prevtag((int) number);
1602 if (tagfile == NULL)
1604 error("No previous tag", NULL_PARG);
1607 if (edit(tagfile) == 0)
1609 POSITION pos = tagsearch();
1610 if (pos != NULL_POSITION)
1611 jump_loc(pos, jump_sline);
1614 error("Command not available", NULL_PARG);
1620 * Examine a particular file.
1624 if (edit_index((int) number))
1625 error("No such file", NULL_PARG);
1629 if (ch_getflags() & CH_HELPFILE)
1631 old_ifile = curr_ifile;
1632 new_ifile = getoff_ifile(curr_ifile);
1633 if (new_ifile == NULL_IFILE)
1638 if (edit_ifile(new_ifile) != 0)
1640 reedit_ifile(old_ifile);
1643 del_ifile(old_ifile);
1647 optflag = OPT_TOGGLE;
1655 * Report a flag setting.
1657 optflag = OPT_NO_TOGGLE;
1665 * Set an initial command for new files.
1667 start_mca(A_FIRSTCMD, "+", (void*)NULL, 0);
1678 error("Command not available", NULL_PARG);
1681 start_mca(A_SHELL, "!", ml_shell, 0);
1685 error("Command not available", NULL_PARG);
1693 if (ch_getflags() & CH_HELPFILE)
1695 start_mca(A_SETMARK, "mark: ", (void*)NULL, 0);
1697 if (c == erase_char || c == erase2_char ||
1698 c == kill_char || c == '\n' || c == '\r')
1707 start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0);
1709 if (c == erase_char || c == erase2_char ||
1710 c == kill_char || c == '\n' || c == '\r')
1720 error("Command not available", NULL_PARG);
1723 start_mca(A_PIPE, "|mark: ", (void*)NULL, 0);
1725 if (c == erase_char || c == erase2_char || c == kill_char)
1727 if (c == '\n' || c == '\r')
1732 start_mca(A_PIPE, "!", ml_shell, 0);
1736 error("Command not available", NULL_PARG);
1742 start_mca(action, "Brackets: ", (void*)NULL, 0);
1748 shift_count = number;
1750 number = (shift_count > 0) ?
1751 shift_count : sc_width / 2;
1752 if (number > hshift)
1760 shift_count = number;
1762 number = (shift_count > 0) ?
1763 shift_count : sc_width / 2;
1770 * The command is incomplete (more chars are needed).
1771 * Display the current char, so the user knows
1772 * what's going on, and get another character.
1774 if (mca != A_PREFIX)
1777 start_mca(A_PREFIX, " ", (void*)NULL,