3 * Copyright (C) 1984-2011 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 about less, or for information on how to
9 * contact the author, see the README file.
14 * User-level command processor.
18 #if MSDOS_COMPILER==WIN32C
25 extern int erase_char, erase2_char, kill_char;
27 extern int quit_if_one_screen;
32 extern int jump_sline;
35 extern int top_scroll;
36 extern int ignore_eoi;
40 extern int less_is_more;
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)
108 void constant *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 char constant *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 * Main command processor.
966 * Accept and execute commands until a quit command.
975 int save_search_type;
983 search_type = SRCH_FORW;
984 wscroll = (sc_height + 1) / 2;
985 newaction = A_NOACTION;
995 * See if any signals need processing.
1001 quit(QUIT_SAVED_STATUS);
1005 * See if window size changed, for systems that don't
1006 * generate SIGWINCH.
1011 * Display prompt and accept a character.
1017 if (newaction == A_NOACTION)
1024 if (newaction != A_NOACTION)
1027 newaction = A_NOACTION;
1031 * If we are in a multicharacter command, call mca_char.
1032 * Otherwise we call fcmd_decode to determine the
1033 * action to be performed.
1036 switch (mca_char(c))
1040 * Need another character.
1046 * Command has been handled by mca_char.
1047 * Start clean with a prompt.
1052 * Not a multi-char command
1053 * (at least, not anymore).
1059 * Decode the command character and decide what to do.
1064 * We're in a multichar command.
1065 * Add the character to the command buffer
1066 * and display it on the screen.
1067 * If the user backspaces past the start
1068 * of the line, abort the command.
1070 if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0)
1072 cbuf = get_cmdbuf();
1076 * Don't use cmd_char if we're starting fresh
1077 * at the beginning of a command, because we
1078 * don't want to echo the command until we know
1079 * it is a multichar command. We also don't
1080 * want erase_char/kill_char to be treated
1081 * as line editing characters.
1088 action = fcmd_decode(cbuf, &extra);
1090 * If an "extra" string was returned,
1091 * process it as a string of command characters.
1097 * Clear the cmdbuf string.
1098 * (But not if we're in the prefix of a command,
1099 * because the partial command string is kept there.)
1101 if (action != A_PREFIX)
1108 * First digit of a number.
1110 start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE);
1115 * Forward one window (and set the window size).
1118 swindow = (int) number;
1122 * Forward one screen.
1125 number = get_swindow();
1128 set_attnpos(bottompos);
1129 forward((int) number, 0, 1);
1134 * Backward one window (and set the window size).
1137 swindow = (int) number;
1141 * Backward one screen.
1144 number = get_swindow();
1146 backward((int) number, 0, 1);
1151 * Forward N (default 1) line.
1156 if (show_attn == OPT_ONPLUS && number > 1)
1157 set_attnpos(bottompos);
1158 forward((int) number, 0, 0);
1163 * Backward N (default 1) line.
1168 backward((int) number, 0, 0);
1173 * Force forward N (default 1) line.
1178 if (show_attn == OPT_ONPLUS && number > 1)
1179 set_attnpos(bottompos);
1180 forward((int) number, 1, 0);
1185 * Force backward N (default 1) line.
1190 backward((int) number, 1, 0);
1195 * Force forward one screen.
1198 number = get_swindow();
1200 if (show_attn == OPT_ONPLUS)
1201 set_attnpos(bottompos);
1202 forward((int) number, 1, 0);
1207 * Forward forever, ignoring EOF.
1209 if (ch_getflags() & CH_HELPFILE)
1221 * This gets us back in "F mode" after processing
1222 * a non-abort signal (e.g. window-change).
1224 if (sigs && !ABORT_SIGS())
1225 newaction = A_F_FOREVER;
1231 * (default same as last 'd' or 'u' command).
1234 wscroll = (int) number;
1236 if (show_attn == OPT_ONPLUS)
1237 set_attnpos(bottompos);
1238 forward(wscroll, 0, 0);
1244 * (default same as last 'd' or 'u' command).
1247 wscroll = (int) number;
1249 backward(wscroll, 0, 0);
1254 * Flush buffers, then repaint screen.
1255 * Don't flush the buffers on a pipe!
1269 * Go to line N, default beginning of file.
1279 * Go to a specified percentage into the file.
1292 jump_percent((int) number, fraction);
1297 * Go to line N, default end of file.
1308 * Go to a specified byte position in the file.
1313 jump_line_loc((POSITION) number, jump_sline);
1318 * Print file name, etc.
1320 if (ch_getflags() & CH_HELPFILE)
1323 parg.p_string = eq_message();
1329 * Print version number, without the "@(#)".
1339 if (curr_ifile != NULL_IFILE &&
1340 ch_getflags() & CH_HELPFILE)
1343 * Quit while viewing the help file
1344 * just means return to viewing the
1347 hshift = save_hshift;
1348 if (edit_prev(1) == 0)
1357 * Define abbreviation for a commonly used sequence below.
1359 #define DO_SEARCH() \
1360 if (number <= 0) number = 1; \
1363 multi_search((char *)NULL, (int) number);
1368 * Search forward for a pattern.
1369 * Get the first char of the pattern.
1371 search_type = SRCH_FORW;
1380 * Search backward for a pattern.
1381 * Get the first char of the pattern.
1383 search_type = SRCH_BACK;
1392 search_type = SRCH_FORW | SRCH_FILTER;
1397 error("Command not available", NULL_PARG);
1401 case A_AGAIN_SEARCH:
1403 * Repeat previous search.
1408 case A_T_AGAIN_SEARCH:
1410 * Repeat previous search, multiple files.
1412 search_type |= SRCH_PAST_EOF;
1416 case A_REVERSE_SEARCH:
1418 * Repeat previous search, in reverse direction.
1420 save_search_type = search_type;
1421 search_type = SRCH_REVERSE(search_type);
1423 search_type = save_search_type;
1426 case A_T_REVERSE_SEARCH:
1428 * Repeat previous search,
1429 * multiple files in reverse direction.
1431 save_search_type = search_type;
1432 search_type = SRCH_REVERSE(search_type);
1433 search_type |= SRCH_PAST_EOF;
1435 search_type = save_search_type;
1446 if (ch_getflags() & CH_HELPFILE)
1449 save_hshift = hshift;
1451 (void) edit(FAKE_HELPFILE);
1457 * Edit a new file. Get the filename.
1461 error("Command not available", NULL_PARG);
1464 start_mca(A_EXAMINE, "Examine: ", ml_examine, 0);
1468 error("Command not available", NULL_PARG);
1474 * Invoke an editor on the input file.
1479 error("Command not available", NULL_PARG);
1482 if (ch_getflags() & CH_HELPFILE)
1484 if (strcmp(get_filename(curr_ifile), "-") == 0)
1486 error("Cannot edit standard input", NULL_PARG);
1489 if (curr_altfilename != NULL)
1491 error("WARNING: This file was viewed via LESSOPEN",
1494 start_mca(A_SHELL, "!", ml_shell, 0);
1496 * Expand the editor prototype string
1497 * and pass it to the system to execute.
1498 * (Make sure the screen is displayed so the
1499 * expansion of "+%lm" works.)
1503 lsystem(pr_expand(editproto, 0), (char*)NULL);
1506 error("Command not available", NULL_PARG);
1512 * Examine next file.
1517 error("No next file", NULL_PARG);
1523 if (edit_next((int) number))
1525 if (get_quit_at_eof() && eof_displayed() &&
1526 !(ch_getflags() & CH_HELPFILE))
1528 parg.p_string = (number > 1) ? "(N-th) " : "";
1529 error("No %snext file", &parg);
1535 * Examine previous file.
1540 error("No previous file", NULL_PARG);
1546 if (edit_prev((int) number))
1548 parg.p_string = (number > 1) ? "(N-th) " : "";
1549 error("No %sprevious file", &parg);
1557 tagfile = nexttag((int) number);
1558 if (tagfile == NULL)
1560 error("No next tag", NULL_PARG);
1563 if (edit(tagfile) == 0)
1565 POSITION pos = tagsearch();
1566 if (pos != NULL_POSITION)
1567 jump_loc(pos, jump_sline);
1570 error("Command not available", NULL_PARG);
1578 tagfile = prevtag((int) number);
1579 if (tagfile == NULL)
1581 error("No previous tag", NULL_PARG);
1584 if (edit(tagfile) == 0)
1586 POSITION pos = tagsearch();
1587 if (pos != NULL_POSITION)
1588 jump_loc(pos, jump_sline);
1591 error("Command not available", NULL_PARG);
1597 * Examine a particular file.
1601 if (edit_index((int) number))
1602 error("No such file", NULL_PARG);
1606 if (ch_getflags() & CH_HELPFILE)
1608 old_ifile = curr_ifile;
1609 new_ifile = getoff_ifile(curr_ifile);
1610 if (new_ifile == NULL_IFILE)
1615 if (edit_ifile(new_ifile) != 0)
1617 reedit_ifile(old_ifile);
1620 del_ifile(old_ifile);
1624 optflag = OPT_TOGGLE;
1632 * Report a flag setting.
1634 optflag = OPT_NO_TOGGLE;
1642 * Set an initial command for new files.
1644 start_mca(A_FIRSTCMD, "+", (void*)NULL, 0);
1655 error("Command not available", NULL_PARG);
1658 start_mca(A_SHELL, "!", ml_shell, 0);
1662 error("Command not available", NULL_PARG);
1670 if (ch_getflags() & CH_HELPFILE)
1672 start_mca(A_SETMARK, "mark: ", (void*)NULL, 0);
1674 if (c == erase_char || c == erase2_char ||
1675 c == kill_char || c == '\n' || c == '\r')
1684 start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0);
1686 if (c == erase_char || c == erase2_char ||
1687 c == kill_char || c == '\n' || c == '\r')
1697 error("Command not available", NULL_PARG);
1700 start_mca(A_PIPE, "|mark: ", (void*)NULL, 0);
1702 if (c == erase_char || c == erase2_char || c == kill_char)
1704 if (c == '\n' || c == '\r')
1709 start_mca(A_PIPE, "!", ml_shell, 0);
1713 error("Command not available", NULL_PARG);
1719 start_mca(action, "Brackets: ", (void*)NULL, 0);
1725 shift_count = number;
1727 number = (shift_count > 0) ?
1728 shift_count : sc_width / 2;
1729 if (number > hshift)
1737 shift_count = number;
1739 number = (shift_count > 0) ?
1740 shift_count : sc_width / 2;
1747 * The command is incomplete (more chars are needed).
1748 * Display the current char, so the user knows
1749 * what's going on, and get another character.
1751 if (mca != A_PREFIX)
1754 start_mca(A_PREFIX, " ", (void*)NULL,