2 * Copyright (C) 1984-2014 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)
164 set_mlist(ml_search, 0);
168 * Set up the display to start a new toggle-option command.
177 no_prompt = (optflag & OPT_NO_PROMPT);
178 flag = (optflag & ~OPT_NO_PROMPT);
179 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?
298 return (c == erase_char || c == erase2_char || c == kill_char);
302 * Handle the first char of an option (after the initial dash).
305 mca_opt_first_char(c)
308 int flag = (optflag & ~OPT_NO_PROMPT);
309 if (flag == OPT_NO_TOGGLE)
314 /* "__" = long option name. */
325 optflag = (flag == OPT_UNSET) ?
326 OPT_TOGGLE : OPT_UNSET;
331 optflag = (flag == OPT_SET) ?
332 OPT_TOGGLE : OPT_SET;
336 optflag ^= OPT_NO_PROMPT;
340 /* "--" = long option name. */
346 /* Char was not handled here. */
351 * Add a char to a long option name.
352 * See if we've got a match for an option name yet.
353 * If so, display the complete name and stop
354 * accepting chars until user hits RETURN.
357 mca_opt_nonfirst_char(c)
366 * Already have a match for the name.
367 * Don't accept anything but erase/kill.
369 if (is_erase_char(c))
374 * Add char to cmd buffer and try to match
377 if (cmd_char(c) == CC_QUIT)
380 opt_lower = ASCII_IS_LOWER(p[0]);
381 curropt = findopt_name(&p, &oname, NULL);
386 * Remember the option and
387 * display the full option name.
391 for (p = oname; *p != '\0'; p++)
394 if (!opt_lower && ASCII_IS_LOWER(c))
395 c = ASCII_TO_UPPER(c);
396 if (cmd_char(c) != CC_OK)
404 * Handle a char of an option toggle command.
413 * This may be a short option (single char),
414 * or one char of a long option name,
415 * or one char of the option parameter.
417 if (curropt == NULL && len_cmdbuf() == 0)
419 int ret = mca_opt_first_char(c);
425 /* We're getting a long option name. */
426 if (c != '\n' && c != '\r')
427 return (mca_opt_nonfirst_char(c));
430 parg.p_string = get_cmdbuf();
431 error("There is no --%s option", &parg);
438 if (is_erase_char(c))
441 /* We're getting the option parameter. */
443 curropt = findopt(c);
446 parg.p_string = propt(c);
447 error("There is no %s option", &parg);
452 * If the option which was entered does not take a
453 * parameter, toggle the option immediately,
454 * so user doesn't have to hit RETURN.
456 if ((optflag & ~OPT_NO_PROMPT) != OPT_TOGGLE ||
457 !opt_has_param(curropt))
459 toggle_option(curropt, ASCII_IS_LOWER(c), "", optflag);
463 * Display a prompt appropriate for the option parameter.
465 start_mca(A_OPT_TOGGLE, opt_prompt(curropt), (void*)NULL, 0);
470 * Handle a char of a search command.
479 * Certain characters as the first char of
480 * the pattern have special meaning:
481 * ! Toggle the NO_MATCH flag
482 * * Toggle the PAST_EOF flag
483 * @ Toggle the FIRST_FILE flag
485 if (len_cmdbuf() > 0)
490 case CONTROL('E'): /* ignore END of file */
493 flag = SRCH_PAST_EOF;
495 case CONTROL('F'): /* FIRST file */
498 flag = SRCH_FIRST_FILE;
500 case CONTROL('K'): /* KEEP position */
504 case CONTROL('R'): /* Don't use REGULAR EXPRESSIONS */
505 flag = SRCH_NO_REGEX;
507 case CONTROL('N'): /* NOT match */
509 flag = SRCH_NO_MATCH;
523 * Handle a character of a multi-character command.
535 * We're not in a multicharacter command.
541 * In the prefix of a command.
542 * This not considered a multichar command
543 * (even tho it uses cmdbuf, etc.).
544 * It is handled in the commands() switch.
550 * Entering digits of a number.
551 * Terminated by a non-digit.
553 if (!((c >= '0' && c <= '9') || c == '.') &&
554 editchar(c, EC_PEEK|EC_NOHISTORY|EC_NOCOMPLETE|EC_NORIGHTLEFT) == A_INVALID)
557 * Not part of the number.
558 * End the number and treat this char
559 * as a normal command character.
561 number = cmd_int(&fraction);
569 ret = mca_opt_char(c);
577 ret = mca_search_char(c);
583 /* Other multicharacter command. */
588 * The multichar command is terminated by a newline.
590 if (c == '\n' || c == '\r')
593 * Execute the command.
600 * Append the char to the command buffer.
602 if (cmd_char(c) == CC_QUIT)
604 * Abort the multi-char command.
608 if ((mca == A_F_BRACKET || mca == A_B_BRACKET) && len_cmdbuf() >= 2)
611 * Special case for the bracket-matching commands.
612 * Execute the command after getting exactly two
613 * characters from the user.
620 * Need another character.
626 * Discard any buffered file data.
631 if (!(ch_getflags() & CH_CANSEEK))
641 * Make sure the screen is displayed.
647 * If nothing is displayed yet, display starting from initial_scrpos.
651 if (initial_scrpos.pos == NULL_POSITION)
653 * {{ Maybe this should be:
654 * jump_loc(ch_zero(), jump_sline);
655 * but this behavior seems rather unexpected
656 * on the first screen. }}
658 jump_loc(ch_zero(), 1);
660 jump_loc(initial_scrpos.pos, initial_scrpos.ln);
661 } else if (screen_trashed)
663 int save_top_scroll = top_scroll;
664 int save_ignore_eoi = ignore_eoi;
667 if (screen_trashed == 2)
669 /* Special case used by ignore_eoi: re-open the input file
670 * and jump to the end of the file. */
675 top_scroll = save_top_scroll;
676 ignore_eoi = save_ignore_eoi;
681 * Display the appropriate prompt.
686 register constant char *p;
688 if (ungot != NULL && !ungot->ug_end_command)
691 * No prompt necessary if commands are from
692 * ungotten chars rather than from the user.
698 * Make sure the screen is displayed.
701 bottompos = position(BOTTOM_PLUS_ONE);
704 * If we've hit EOF on the last file and the -E flag is set, quit.
706 if (get_quit_at_eof() == OPT_ONPLUS &&
707 eof_displayed() && !(ch_getflags() & CH_HELPFILE) &&
708 next_ifile(curr_ifile) == NULL_IFILE)
712 * If the entire file is displayed and the -F flag is set, quit.
714 if (quit_if_one_screen &&
715 entire_file_displayed() && !(ch_getflags() & CH_HELPFILE) &&
716 next_ifile(curr_ifile) == NULL_IFILE)
719 #if MSDOS_COMPILER==WIN32C
721 * In Win32, display the file name in the window title.
723 if (!(ch_getflags() & CH_HELPFILE))
724 SetConsoleTitle(pr_expand("Less?f - %f.", 0));
727 * Select the proper prompt and display it.
730 * If the previous action was a forward movement,
731 * don't clear the bottom line of the display;
732 * just print the prompt since the forward movement guarantees
733 * that we're in the right position to display the prompt.
734 * Clearing the line could cause a problem: for example, if the last
735 * line displayed ended at the right screen edge without a newline,
736 * then clearing would clear the last displayed line rather than
746 if (p == NULL || *p == '\0')
750 at_enter(AT_STANDOUT);
758 * Display the less version message.
765 parg.p_string = version;
766 error("less %s", &parg);
770 * Get command character.
771 * The character normally comes from the keyboard,
772 * but may come from ungotten characters
773 * (characters previously given to ungetcc or ungetsc).
781 * Normal case: no ungotten chars, so get one from the user.
787 * Return the next ungotten char.
790 struct ungot *ug = ungot;
791 char c = ug->ug_char;
792 int end_command = ug->ug_end_command;
798 * Command is incomplete, so try to complete it.
804 * We have a number but no command. Treat as #g.
811 * We have "/string" but no newline. Add the \n.
817 * Some other incomplete command. Let user complete it.
827 * "Unget" a command character.
828 * The next getcc() will return this character.
834 struct ungot *ug = (struct ungot *) ecalloc(1, sizeof(struct ungot));
836 ug->ug_char = (char) c;
837 ug->ug_end_command = (c == CHAR_END_COMMAND);
843 * Unget a whole string of command characters.
844 * The next sequence of getcc()'s will return this string.
852 for (p = s + strlen(s) - 1; p >= s; p--)
857 * Search for a pattern, possibly in multiple files.
858 * If SRCH_FIRST_FILE is set, begin searching at the first file.
859 * If SRCH_PAST_EOF is set, continue the search thru multiple files.
862 multi_search(pattern, n, silent)
872 save_ifile = save_curr_ifile();
874 if (search_type & SRCH_FIRST_FILE)
877 * Start at the first (or last) file
878 * in the command line list.
880 if (search_type & SRCH_FORW)
881 nomore = edit_first();
883 nomore = edit_last();
886 unsave_ifile(save_ifile);
890 search_type &= ~SRCH_FIRST_FILE;
895 n = search(search_type, pattern, n);
897 * The SRCH_NO_MOVE flag doesn't "stick": it gets cleared
898 * after being used once. This allows "n" to work after
899 * using a /@@ search.
901 search_type &= ~SRCH_NO_MOVE;
907 unsave_ifile(save_ifile);
913 * Some kind of error in the search.
914 * Error message has been printed by search().
918 if ((search_type & SRCH_PAST_EOF) == 0)
920 * We didn't find a match, but we're
921 * supposed to search only one file.
925 * Move on to the next file.
927 if (search_type & SRCH_FORW)
928 nomore = edit_next(1);
930 nomore = edit_prev(1);
938 * Print an error message if we haven't already.
940 if (n > 0 && !silent)
941 error("Pattern not found", NULL_PARG);
946 * Restore the file we were originally viewing.
948 reedit_ifile(save_ifile);
951 unsave_ifile(save_ifile);
956 * Forward forever, or until a highlighted line appears.
959 forw_loop(until_hilite)
964 if (ch_getflags() & CH_HELPFILE)
968 jump_forw_buffered();
969 curr_len = ch_length();
970 highest_hilite = until_hilite ? curr_len : NULL_POSITION;
974 if (until_hilite && highest_hilite > curr_len)
986 * This gets us back in "F mode" after processing
987 * a non-abort signal (e.g. window-change).
989 if (sigs && !ABORT_SIGS())
990 return (until_hilite ? A_F_UNTIL_HILITE : A_F_FOREVER);
996 * Main command processor.
997 * Accept and execute commands until a quit command.
1003 register int action;
1004 register char *cbuf;
1006 int save_search_type;
1014 search_type = SRCH_FORW;
1015 wscroll = (sc_height + 1) / 2;
1016 newaction = A_NOACTION;
1026 * See if any signals need processing.
1032 quit(QUIT_SAVED_STATUS);
1036 * See if window size changed, for systems that don't
1037 * generate SIGWINCH.
1042 * Display prompt and accept a character.
1048 if (newaction == A_NOACTION)
1055 if (newaction != A_NOACTION)
1058 newaction = A_NOACTION;
1062 * If we are in a multicharacter command, call mca_char.
1063 * Otherwise we call fcmd_decode to determine the
1064 * action to be performed.
1067 switch (mca_char(c))
1071 * Need another character.
1077 * Command has been handled by mca_char.
1078 * Start clean with a prompt.
1083 * Not a multi-char command
1084 * (at least, not anymore).
1090 * Decode the command character and decide what to do.
1095 * We're in a multichar command.
1096 * Add the character to the command buffer
1097 * and display it on the screen.
1098 * If the user backspaces past the start
1099 * of the line, abort the command.
1101 if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0)
1103 cbuf = get_cmdbuf();
1107 * Don't use cmd_char if we're starting fresh
1108 * at the beginning of a command, because we
1109 * don't want to echo the command until we know
1110 * it is a multichar command. We also don't
1111 * want erase_char/kill_char to be treated
1112 * as line editing characters.
1119 action = fcmd_decode(cbuf, &extra);
1121 * If an "extra" string was returned,
1122 * process it as a string of command characters.
1128 * Clear the cmdbuf string.
1129 * (But not if we're in the prefix of a command,
1130 * because the partial command string is kept there.)
1132 if (action != A_PREFIX)
1139 * First digit of a number.
1141 start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE);
1146 * Forward one window (and set the window size).
1149 swindow = (int) number;
1153 * Forward one screen.
1156 number = get_swindow();
1159 set_attnpos(bottompos);
1160 forward((int) number, 0, 1);
1165 * Backward one window (and set the window size).
1168 swindow = (int) number;
1172 * Backward one screen.
1175 number = get_swindow();
1177 backward((int) number, 0, 1);
1182 * Forward N (default 1) line.
1187 if (show_attn == OPT_ONPLUS && number > 1)
1188 set_attnpos(bottompos);
1189 forward((int) number, 0, 0);
1194 * Backward N (default 1) line.
1199 backward((int) number, 0, 0);
1204 * Force forward N (default 1) line.
1209 if (show_attn == OPT_ONPLUS && number > 1)
1210 set_attnpos(bottompos);
1211 forward((int) number, 1, 0);
1216 * Force backward N (default 1) line.
1221 backward((int) number, 1, 0);
1226 * Force forward one screen.
1229 number = get_swindow();
1231 if (show_attn == OPT_ONPLUS)
1232 set_attnpos(bottompos);
1233 forward((int) number, 1, 0);
1238 * Forward forever, ignoring EOF.
1241 set_attnpos(bottompos);
1242 newaction = forw_loop(0);
1245 case A_F_UNTIL_HILITE:
1246 newaction = forw_loop(1);
1252 * (default same as last 'd' or 'u' command).
1255 wscroll = (int) number;
1257 if (show_attn == OPT_ONPLUS)
1258 set_attnpos(bottompos);
1259 forward(wscroll, 0, 0);
1265 * (default same as last 'd' or 'u' command).
1268 wscroll = (int) number;
1270 backward(wscroll, 0, 0);
1275 * Flush buffers, then repaint screen.
1276 * Don't flush the buffers on a pipe!
1290 * Go to line N, default beginning of file.
1300 * Go to a specified percentage into the file.
1313 jump_percent((int) number, fraction);
1318 * Go to line N, default end of file.
1329 * Go to line N, default last buffered byte.
1333 jump_forw_buffered();
1340 * Go to a specified byte position in the file.
1345 jump_line_loc((POSITION) number, jump_sline);
1350 * Print file name, etc.
1352 if (ch_getflags() & CH_HELPFILE)
1355 parg.p_string = eq_message();
1361 * Print version number, without the "@(#)".
1371 if (curr_ifile != NULL_IFILE &&
1372 ch_getflags() & CH_HELPFILE)
1375 * Quit while viewing the help file
1376 * just means return to viewing the
1379 hshift = save_hshift;
1380 bs_mode = save_bs_mode;
1381 if (edit_prev(1) == 0)
1390 * Define abbreviation for a commonly used sequence below.
1392 #define DO_SEARCH() \
1393 if (number <= 0) number = 1; \
1396 multi_search((char *)NULL, (int) number, 0);
1401 * Search forward for a pattern.
1402 * Get the first char of the pattern.
1404 search_type = SRCH_FORW;
1413 * Search backward for a pattern.
1414 * Get the first char of the pattern.
1416 search_type = SRCH_BACK;
1425 search_type = SRCH_FORW | SRCH_FILTER;
1430 error("Command not available", NULL_PARG);
1434 case A_AGAIN_SEARCH:
1436 * Repeat previous search.
1441 case A_T_AGAIN_SEARCH:
1443 * Repeat previous search, multiple files.
1445 search_type |= SRCH_PAST_EOF;
1449 case A_REVERSE_SEARCH:
1451 * Repeat previous search, in reverse direction.
1453 save_search_type = search_type;
1454 search_type = SRCH_REVERSE(search_type);
1456 search_type = save_search_type;
1459 case A_T_REVERSE_SEARCH:
1461 * Repeat previous search,
1462 * multiple files in reverse direction.
1464 save_search_type = search_type;
1465 search_type = SRCH_REVERSE(search_type);
1466 search_type |= SRCH_PAST_EOF;
1468 search_type = save_search_type;
1479 if (ch_getflags() & CH_HELPFILE)
1482 save_hshift = hshift;
1484 save_bs_mode = bs_mode;
1485 bs_mode = BS_SPECIAL;
1486 (void) edit(FAKE_HELPFILE);
1492 * Edit a new file. Get the filename.
1496 error("Command not available", NULL_PARG);
1499 start_mca(A_EXAMINE, "Examine: ", ml_examine, 0);
1503 error("Command not available", NULL_PARG);
1509 * Invoke an editor on the input file.
1514 error("Command not available", NULL_PARG);
1517 if (ch_getflags() & CH_HELPFILE)
1519 if (strcmp(get_filename(curr_ifile), "-") == 0)
1521 error("Cannot edit standard input", NULL_PARG);
1524 if (curr_altfilename != NULL)
1526 error("WARNING: This file was viewed via LESSOPEN",
1529 start_mca(A_SHELL, "!", ml_shell, 0);
1531 * Expand the editor prototype string
1532 * and pass it to the system to execute.
1533 * (Make sure the screen is displayed so the
1534 * expansion of "+%lm" works.)
1538 lsystem(pr_expand(editproto, 0), (char*)NULL);
1541 error("Command not available", NULL_PARG);
1547 * Examine next file.
1552 error("No next file", NULL_PARG);
1558 if (edit_next((int) number))
1560 if (get_quit_at_eof() && eof_displayed() &&
1561 !(ch_getflags() & CH_HELPFILE))
1563 parg.p_string = (number > 1) ? "(N-th) " : "";
1564 error("No %snext file", &parg);
1570 * Examine previous file.
1575 error("No previous file", NULL_PARG);
1581 if (edit_prev((int) number))
1583 parg.p_string = (number > 1) ? "(N-th) " : "";
1584 error("No %sprevious file", &parg);
1592 tagfile = nexttag((int) number);
1593 if (tagfile == NULL)
1595 error("No next tag", NULL_PARG);
1598 if (edit(tagfile) == 0)
1600 POSITION pos = tagsearch();
1601 if (pos != NULL_POSITION)
1602 jump_loc(pos, jump_sline);
1605 error("Command not available", NULL_PARG);
1613 tagfile = prevtag((int) number);
1614 if (tagfile == NULL)
1616 error("No previous tag", NULL_PARG);
1619 if (edit(tagfile) == 0)
1621 POSITION pos = tagsearch();
1622 if (pos != NULL_POSITION)
1623 jump_loc(pos, jump_sline);
1626 error("Command not available", NULL_PARG);
1632 * Examine a particular file.
1636 if (edit_index((int) number))
1637 error("No such file", NULL_PARG);
1641 if (ch_getflags() & CH_HELPFILE)
1643 old_ifile = curr_ifile;
1644 new_ifile = getoff_ifile(curr_ifile);
1645 if (new_ifile == NULL_IFILE)
1650 if (edit_ifile(new_ifile) != 0)
1652 reedit_ifile(old_ifile);
1655 del_ifile(old_ifile);
1659 optflag = OPT_TOGGLE;
1667 * Report a flag setting.
1669 optflag = OPT_NO_TOGGLE;
1677 * Set an initial command for new files.
1679 start_mca(A_FIRSTCMD, "+", (void*)NULL, 0);
1690 error("Command not available", NULL_PARG);
1693 start_mca(A_SHELL, "!", ml_shell, 0);
1697 error("Command not available", NULL_PARG);
1705 if (ch_getflags() & CH_HELPFILE)
1707 start_mca(A_SETMARK, "mark: ", (void*)NULL, 0);
1709 if (c == erase_char || c == erase2_char ||
1710 c == kill_char || c == '\n' || c == '\r')
1719 start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0);
1721 if (c == erase_char || c == erase2_char ||
1722 c == kill_char || c == '\n' || c == '\r')
1732 error("Command not available", NULL_PARG);
1735 start_mca(A_PIPE, "|mark: ", (void*)NULL, 0);
1737 if (c == erase_char || c == erase2_char || c == kill_char)
1739 if (c == '\n' || c == '\r')
1744 start_mca(A_PIPE, "!", ml_shell, 0);
1748 error("Command not available", NULL_PARG);
1754 start_mca(action, "Brackets: ", (void*)NULL, 0);
1760 shift_count = number;
1762 number = (shift_count > 0) ?
1763 shift_count : sc_width / 2;
1764 if (number > hshift)
1772 shift_count = number;
1774 number = (shift_count > 0) ?
1775 shift_count : sc_width / 2;
1782 * The command is incomplete (more chars are needed).
1783 * Display the current char, so the user knows
1784 * what's going on, and get another character.
1786 if (mca != A_PREFIX)
1789 start_mca(A_PREFIX, " ", (void*)NULL,