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)
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.
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 * Main command processor.
960 * Accept and execute commands until a quit command.
969 int save_search_type;
977 search_type = SRCH_FORW;
978 wscroll = (sc_height + 1) / 2;
979 newaction = A_NOACTION;
989 * See if any signals need processing.
995 quit(QUIT_SAVED_STATUS);
999 * See if window size changed, for systems that don't
1000 * generate SIGWINCH.
1005 * Display prompt and accept a character.
1011 if (newaction == A_NOACTION)
1018 if (newaction != A_NOACTION)
1021 newaction = A_NOACTION;
1025 * If we are in a multicharacter command, call mca_char.
1026 * Otherwise we call fcmd_decode to determine the
1027 * action to be performed.
1030 switch (mca_char(c))
1034 * Need another character.
1040 * Command has been handled by mca_char.
1041 * Start clean with a prompt.
1046 * Not a multi-char command
1047 * (at least, not anymore).
1053 * Decode the command character and decide what to do.
1058 * We're in a multichar command.
1059 * Add the character to the command buffer
1060 * and display it on the screen.
1061 * If the user backspaces past the start
1062 * of the line, abort the command.
1064 if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0)
1066 cbuf = get_cmdbuf();
1070 * Don't use cmd_char if we're starting fresh
1071 * at the beginning of a command, because we
1072 * don't want to echo the command until we know
1073 * it is a multichar command. We also don't
1074 * want erase_char/kill_char to be treated
1075 * as line editing characters.
1082 action = fcmd_decode(cbuf, &extra);
1084 * If an "extra" string was returned,
1085 * process it as a string of command characters.
1091 * Clear the cmdbuf string.
1092 * (But not if we're in the prefix of a command,
1093 * because the partial command string is kept there.)
1095 if (action != A_PREFIX)
1102 * First digit of a number.
1104 start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE);
1109 * Forward one window (and set the window size).
1112 swindow = (int) number;
1116 * Forward one screen.
1119 number = get_swindow();
1122 set_attnpos(bottompos);
1123 forward((int) number, 0, 1);
1128 * Backward one window (and set the window size).
1131 swindow = (int) number;
1135 * Backward one screen.
1138 number = get_swindow();
1140 backward((int) number, 0, 1);
1145 * Forward N (default 1) line.
1150 if (show_attn == OPT_ONPLUS && number > 1)
1151 set_attnpos(bottompos);
1152 forward((int) number, 0, 0);
1157 * Backward N (default 1) line.
1162 backward((int) number, 0, 0);
1167 * Force forward N (default 1) line.
1172 if (show_attn == OPT_ONPLUS && number > 1)
1173 set_attnpos(bottompos);
1174 forward((int) number, 1, 0);
1179 * Force backward N (default 1) line.
1184 backward((int) number, 1, 0);
1189 * Force forward one screen.
1192 number = get_swindow();
1194 if (show_attn == OPT_ONPLUS)
1195 set_attnpos(bottompos);
1196 forward((int) number, 1, 0);
1201 * Forward forever, ignoring EOF.
1203 if (ch_getflags() & CH_HELPFILE)
1215 * This gets us back in "F mode" after processing
1216 * a non-abort signal (e.g. window-change).
1218 if (sigs && !ABORT_SIGS())
1219 newaction = A_F_FOREVER;
1225 * (default same as last 'd' or 'u' command).
1228 wscroll = (int) number;
1230 if (show_attn == OPT_ONPLUS)
1231 set_attnpos(bottompos);
1232 forward(wscroll, 0, 0);
1238 * (default same as last 'd' or 'u' command).
1241 wscroll = (int) number;
1243 backward(wscroll, 0, 0);
1248 * Flush buffers, then repaint screen.
1249 * Don't flush the buffers on a pipe!
1263 * Go to line N, default beginning of file.
1273 * Go to a specified percentage into the file.
1286 jump_percent((int) number, fraction);
1291 * Go to line N, default end of file.
1302 * Go to a specified byte position in the file.
1307 jump_line_loc((POSITION) number, jump_sline);
1312 * Print file name, etc.
1314 if (ch_getflags() & CH_HELPFILE)
1317 parg.p_string = eq_message();
1323 * Print version number, without the "@(#)".
1333 if (curr_ifile != NULL_IFILE &&
1334 ch_getflags() & CH_HELPFILE)
1337 * Quit while viewing the help file
1338 * just means return to viewing the
1341 hshift = save_hshift;
1342 if (edit_prev(1) == 0)
1351 * Define abbreviation for a commonly used sequence below.
1353 #define DO_SEARCH() \
1354 if (number <= 0) number = 1; \
1357 multi_search((char *)NULL, (int) number);
1362 * Search forward for a pattern.
1363 * Get the first char of the pattern.
1365 search_type = SRCH_FORW;
1374 * Search backward for a pattern.
1375 * Get the first char of the pattern.
1377 search_type = SRCH_BACK;
1386 search_type = SRCH_FORW | SRCH_FILTER;
1391 error("Command not available", NULL_PARG);
1395 case A_AGAIN_SEARCH:
1397 * Repeat previous search.
1402 case A_T_AGAIN_SEARCH:
1404 * Repeat previous search, multiple files.
1406 search_type |= SRCH_PAST_EOF;
1410 case A_REVERSE_SEARCH:
1412 * Repeat previous search, in reverse direction.
1414 save_search_type = search_type;
1415 search_type = SRCH_REVERSE(search_type);
1417 search_type = save_search_type;
1420 case A_T_REVERSE_SEARCH:
1422 * Repeat previous search,
1423 * multiple files in reverse direction.
1425 save_search_type = search_type;
1426 search_type = SRCH_REVERSE(search_type);
1427 search_type |= SRCH_PAST_EOF;
1429 search_type = save_search_type;
1440 if (ch_getflags() & CH_HELPFILE)
1443 save_hshift = hshift;
1445 (void) edit(FAKE_HELPFILE);
1451 * Edit a new file. Get the filename.
1455 error("Command not available", NULL_PARG);
1458 start_mca(A_EXAMINE, "Examine: ", ml_examine, 0);
1462 error("Command not available", NULL_PARG);
1468 * Invoke an editor on the input file.
1473 error("Command not available", NULL_PARG);
1476 if (ch_getflags() & CH_HELPFILE)
1478 if (strcmp(get_filename(curr_ifile), "-") == 0)
1480 error("Cannot edit standard input", NULL_PARG);
1483 if (curr_altfilename != NULL)
1485 error("WARNING: This file was viewed via LESSOPEN",
1488 start_mca(A_SHELL, "!", ml_shell, 0);
1490 * Expand the editor prototype string
1491 * and pass it to the system to execute.
1492 * (Make sure the screen is displayed so the
1493 * expansion of "+%lm" works.)
1497 lsystem(pr_expand(editproto, 0), (char*)NULL);
1500 error("Command not available", NULL_PARG);
1506 * Examine next file.
1511 error("No next file", NULL_PARG);
1517 if (edit_next((int) number))
1519 if (get_quit_at_eof() && eof_displayed() &&
1520 !(ch_getflags() & CH_HELPFILE))
1522 parg.p_string = (number > 1) ? "(N-th) " : "";
1523 error("No %snext file", &parg);
1529 * Examine previous file.
1534 error("No previous file", NULL_PARG);
1540 if (edit_prev((int) number))
1542 parg.p_string = (number > 1) ? "(N-th) " : "";
1543 error("No %sprevious file", &parg);
1551 tagfile = nexttag((int) number);
1552 if (tagfile == NULL)
1554 error("No next tag", NULL_PARG);
1557 if (edit(tagfile) == 0)
1559 POSITION pos = tagsearch();
1560 if (pos != NULL_POSITION)
1561 jump_loc(pos, jump_sline);
1564 error("Command not available", NULL_PARG);
1572 tagfile = prevtag((int) number);
1573 if (tagfile == NULL)
1575 error("No previous tag", NULL_PARG);
1578 if (edit(tagfile) == 0)
1580 POSITION pos = tagsearch();
1581 if (pos != NULL_POSITION)
1582 jump_loc(pos, jump_sline);
1585 error("Command not available", NULL_PARG);
1591 * Examine a particular file.
1595 if (edit_index((int) number))
1596 error("No such file", NULL_PARG);
1600 if (ch_getflags() & CH_HELPFILE)
1602 old_ifile = curr_ifile;
1603 new_ifile = getoff_ifile(curr_ifile);
1604 if (new_ifile == NULL_IFILE)
1609 if (edit_ifile(new_ifile) != 0)
1611 reedit_ifile(old_ifile);
1614 del_ifile(old_ifile);
1618 optflag = OPT_TOGGLE;
1626 * Report a flag setting.
1628 optflag = OPT_NO_TOGGLE;
1636 * Set an initial command for new files.
1638 start_mca(A_FIRSTCMD, "+", (void*)NULL, 0);
1649 error("Command not available", NULL_PARG);
1652 start_mca(A_SHELL, "!", ml_shell, 0);
1656 error("Command not available", NULL_PARG);
1664 if (ch_getflags() & CH_HELPFILE)
1666 start_mca(A_SETMARK, "mark: ", (void*)NULL, 0);
1668 if (c == erase_char || c == erase2_char ||
1669 c == kill_char || c == '\n' || c == '\r')
1678 start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0);
1680 if (c == erase_char || c == erase2_char ||
1681 c == kill_char || c == '\n' || c == '\r')
1691 error("Command not available", NULL_PARG);
1694 start_mca(A_PIPE, "|mark: ", (void*)NULL, 0);
1696 if (c == erase_char || c == erase2_char || c == kill_char)
1698 if (c == '\n' || c == '\r')
1703 start_mca(A_PIPE, "!", ml_shell, 0);
1707 error("Command not available", NULL_PARG);
1713 start_mca(action, "Brackets: ", (void*)NULL, 0);
1719 shift_count = number;
1721 number = (shift_count > 0) ?
1722 shift_count : sc_width / 2;
1723 if (number > hshift)
1731 shift_count = number;
1733 number = (shift_count > 0) ?
1734 shift_count : sc_width / 2;
1741 * The command is incomplete (more chars are needed).
1742 * Display the current char, so the user knows
1743 * what's going on, and get another character.
1745 if (mca != A_PREFIX)
1748 start_mca(A_PREFIX, " ", (void*)NULL,