3 * Copyright (C) 1984-2015 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, 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;
40 extern int less_is_more;
41 extern POSITION highest_hilite;
42 extern char *every_first_cmd;
43 extern char *curr_altfilename;
44 extern char version[];
45 extern struct scrpos initial_scrpos;
46 extern IFILE curr_ifile;
47 extern void constant *ml_search;
48 extern void constant *ml_examine;
49 #if SHELL_ESCAPE || PIPEC
50 extern void constant *ml_shell;
54 extern char *editproto;
56 extern int screen_trashed; /* The screen has been overwritten */
57 extern int shift_count;
59 extern int forw_prompt;
60 extern int same_pos_bell;
63 static char *shellcmd = NULL; /* For holding last shell command for "!!" */
65 static int mca; /* The multicharacter command (action) */
66 static int search_type; /* The previous type of search */
67 static LINENUM number; /* The number typed by the user */
68 static long fraction; /* The fractional part of the number */
69 static struct loption *curropt;
72 static int optgetname;
73 static POSITION bottompos;
74 static int save_hshift;
75 static int save_bs_mode;
81 struct ungot *ug_next;
85 static struct ungot* ungot = NULL;
87 static void multi_search();
90 * Move the cursor to start of prompt line before executing a command.
91 * This looks nicer if the command takes a long time before
92 * updating the screen.
105 * Set up the display to start a new multi-character command.
108 start_mca(action, prompt, mlist, cmdflags)
110 constant char *prompt;
111 constant void *mlist;
118 set_mlist(mlist, cmdflags);
124 return (mca != 0 && mca != A_PREFIX);
128 * Set up the display to start a new search command.
134 if (search_type & SRCH_FILTER)
138 if (search_type & SRCH_FORW)
146 if (search_type & SRCH_NO_MATCH)
147 cmd_putstr("Non-match ");
148 if (search_type & SRCH_FIRST_FILE)
149 cmd_putstr("First-file ");
150 if (search_type & SRCH_PAST_EOF)
151 cmd_putstr("EOF-ignore ");
152 if (search_type & SRCH_NO_MOVE)
153 cmd_putstr("Keep-pos ");
154 if (search_type & SRCH_NO_REGEX)
155 cmd_putstr("Regex-off ");
158 if (search_type & SRCH_FILTER)
162 if (search_type & SRCH_FORW)
167 set_mlist(ml_search, 0);
171 * Set up the display to start a new toggle-option command.
180 no_prompt = (optflag & OPT_NO_PROMPT);
181 flag = (optflag & ~OPT_NO_PROMPT);
182 dash = (flag == OPT_NO_TOGGLE) ? "_" : "-";
206 * Execute a multicharacter command.
220 multi_search(cbuf, (int) number, 0);
224 search_type ^= SRCH_NO_MATCH;
225 set_filter_pattern(cbuf, search_type);
230 * Skip leading spaces or + signs in the string.
232 while (*cbuf == '+' || *cbuf == ' ')
234 if (every_first_cmd != NULL)
235 free(every_first_cmd);
237 every_first_cmd = NULL;
239 every_first_cmd = save(cbuf);
242 toggle_option(curropt, opt_lower, cbuf, optflag);
246 match_brac(cbuf[0], cbuf[1], 1, (int) number);
249 match_brac(cbuf[1], cbuf[0], 0, (int) number);
257 /* If tag structure is loaded then clean it up. */
265 * !! just uses whatever is in shellcmd.
266 * Otherwise, copy cmdbuf to shellcmd,
267 * expanding any special characters ("%" or "#").
271 if (shellcmd != NULL)
273 shellcmd = fexpand(cbuf);
278 if (shellcmd == NULL)
279 lsystem("", "!done");
281 lsystem(shellcmd, "!done");
288 (void) pipe_mark(pipec, cbuf);
289 error("|done", NULL_PARG);
296 * Is a character an erase or kill char?
302 return (c == erase_char || c == erase2_char || c == kill_char);
306 * Handle the first char of an option (after the initial dash).
309 mca_opt_first_char(c)
312 int flag = (optflag & ~OPT_NO_PROMPT);
313 if (flag == OPT_NO_TOGGLE)
318 /* "__" = long option name. */
329 optflag = (flag == OPT_UNSET) ?
330 OPT_TOGGLE : OPT_UNSET;
335 optflag = (flag == OPT_SET) ?
336 OPT_TOGGLE : OPT_SET;
340 optflag ^= OPT_NO_PROMPT;
344 /* "--" = long option name. */
350 /* Char was not handled here. */
355 * Add a char to a long option name.
356 * See if we've got a match for an option name yet.
357 * If so, display the complete name and stop
358 * accepting chars until user hits RETURN.
361 mca_opt_nonfirst_char(c)
370 * Already have a match for the name.
371 * Don't accept anything but erase/kill.
373 if (is_erase_char(c))
378 * Add char to cmd buffer and try to match
381 if (cmd_char(c) == CC_QUIT)
384 opt_lower = ASCII_IS_LOWER(p[0]);
385 curropt = findopt_name(&p, &oname, NULL);
390 * Remember the option and
391 * display the full option name.
395 for (p = oname; *p != '\0'; p++)
398 if (!opt_lower && ASCII_IS_LOWER(c))
399 c = ASCII_TO_UPPER(c);
400 if (cmd_char(c) != CC_OK)
408 * Handle a char of an option toggle command.
417 * This may be a short option (single char),
418 * or one char of a long option name,
419 * or one char of the option parameter.
421 if (curropt == NULL && len_cmdbuf() == 0)
423 int ret = mca_opt_first_char(c);
429 /* We're getting a long option name. */
430 if (c != '\n' && c != '\r')
431 return (mca_opt_nonfirst_char(c));
434 parg.p_string = get_cmdbuf();
435 error("There is no --%s option", &parg);
442 if (is_erase_char(c))
445 /* We're getting the option parameter. */
447 curropt = findopt(c);
450 parg.p_string = propt(c);
451 error("There is no %s option", &parg);
456 * If the option which was entered does not take a
457 * parameter, toggle the option immediately,
458 * so user doesn't have to hit RETURN.
460 if ((optflag & ~OPT_NO_PROMPT) != OPT_TOGGLE ||
461 !opt_has_param(curropt))
463 toggle_option(curropt, ASCII_IS_LOWER(c), "", optflag);
467 * Display a prompt appropriate for the option parameter.
469 start_mca(A_OPT_TOGGLE, opt_prompt(curropt), (void*)NULL, 0);
474 * Handle a char of a search command.
483 * Certain characters as the first char of
484 * the pattern have special meaning:
485 * ! Toggle the NO_MATCH flag
486 * * Toggle the PAST_EOF flag
487 * @ Toggle the FIRST_FILE flag
489 if (len_cmdbuf() > 0)
497 case CONTROL('E'): /* ignore END of file */
499 flag = SRCH_PAST_EOF;
504 case CONTROL('F'): /* FIRST file */
506 flag = SRCH_FIRST_FILE;
508 case CONTROL('K'): /* KEEP position */
512 case CONTROL('R'): /* Don't use REGULAR EXPRESSIONS */
513 flag = SRCH_NO_REGEX;
515 case CONTROL('N'): /* NOT match */
517 flag = SRCH_NO_MATCH;
531 * Handle a character of a multi-character command.
543 * We're not in a multicharacter command.
549 * In the prefix of a command.
550 * This not considered a multichar command
551 * (even tho it uses cmdbuf, etc.).
552 * It is handled in the commands() switch.
558 * Entering digits of a number.
559 * Terminated by a non-digit.
561 if (!((c >= '0' && c <= '9') || c == '.') &&
562 editchar(c, EC_PEEK|EC_NOHISTORY|EC_NOCOMPLETE|EC_NORIGHTLEFT) == A_INVALID)
565 * Not part of the number.
566 * End the number and treat this char
567 * as a normal command character.
569 number = cmd_int(&fraction);
577 ret = mca_opt_char(c);
585 ret = mca_search_char(c);
591 /* Other multicharacter command. */
596 * The multichar command is terminated by a newline.
598 if (c == '\n' || c == '\r')
601 * Execute the command.
608 * Append the char to the command buffer.
610 if (cmd_char(c) == CC_QUIT)
612 * Abort the multi-char command.
616 if ((mca == A_F_BRACKET || mca == A_B_BRACKET) && len_cmdbuf() >= 2)
619 * Special case for the bracket-matching commands.
620 * Execute the command after getting exactly two
621 * characters from the user.
628 * Need another character.
634 * Discard any buffered file data.
639 if (!(ch_getflags() & CH_CANSEEK))
649 * Make sure the screen is displayed.
655 * If nothing is displayed yet, display starting from initial_scrpos.
659 if (initial_scrpos.pos == NULL_POSITION)
661 * {{ Maybe this should be:
662 * jump_loc(ch_zero(), jump_sline);
663 * but this behavior seems rather unexpected
664 * on the first screen. }}
666 jump_loc(ch_zero(), 1);
668 jump_loc(initial_scrpos.pos, initial_scrpos.ln);
669 } else if (screen_trashed)
671 int save_top_scroll = top_scroll;
672 int save_ignore_eoi = ignore_eoi;
675 if (screen_trashed == 2)
677 /* Special case used by ignore_eoi: re-open the input file
678 * and jump to the end of the file. */
683 top_scroll = save_top_scroll;
684 ignore_eoi = save_ignore_eoi;
689 * Display the appropriate prompt.
694 register constant char *p;
696 if (ungot != NULL && !ungot->ug_end_command)
699 * No prompt necessary if commands are from
700 * ungotten chars rather than from the user.
706 * Make sure the screen is displayed.
709 bottompos = position(BOTTOM_PLUS_ONE);
712 * If we've hit EOF on the last file and the -E flag is set, quit.
714 if (get_quit_at_eof() == OPT_ONPLUS &&
715 eof_displayed() && !(ch_getflags() & CH_HELPFILE) &&
716 next_ifile(curr_ifile) == NULL_IFILE)
720 * If the entire file is displayed and the -F flag is set, quit.
722 if (quit_if_one_screen &&
723 entire_file_displayed() && !(ch_getflags() & CH_HELPFILE) &&
724 next_ifile(curr_ifile) == NULL_IFILE)
727 #if MSDOS_COMPILER==WIN32C
729 * In Win32, display the file name in the window title.
731 if (!(ch_getflags() & CH_HELPFILE))
732 SetConsoleTitle(pr_expand("Less?f - %f.", 0));
735 * Select the proper prompt and display it.
738 * If the previous action was a forward movement,
739 * don't clear the bottom line of the display;
740 * just print the prompt since the forward movement guarantees
741 * that we're in the right position to display the prompt.
742 * Clearing the line could cause a problem: for example, if the last
743 * line displayed ended at the right screen edge without a newline,
744 * then clearing would clear the last displayed line rather than
754 if (p == NULL || *p == '\0')
758 at_enter(AT_STANDOUT);
766 * Display the less version message.
773 parg.p_string = version;
774 error("less %s", &parg);
778 * Get command character.
779 * The character normally comes from the keyboard,
780 * but may come from ungotten characters
781 * (characters previously given to ungetcc or ungetsc).
789 * Normal case: no ungotten chars, so get one from the user.
795 * Return the next ungotten char.
798 struct ungot *ug = ungot;
799 char c = ug->ug_char;
800 int end_command = ug->ug_end_command;
806 * Command is incomplete, so try to complete it.
812 * We have a number but no command. Treat as #g.
819 * We have "/string" but no newline. Add the \n.
825 * Some other incomplete command. Let user complete it.
835 * "Unget" a command character.
836 * The next getcc() will return this character.
842 struct ungot *ug = (struct ungot *) ecalloc(1, sizeof(struct ungot));
844 ug->ug_char = (char) c;
845 ug->ug_end_command = (c == CHAR_END_COMMAND);
851 * Unget a whole string of command characters.
852 * The next sequence of getcc()'s will return this string.
860 for (p = s + strlen(s) - 1; p >= s; p--)
865 * Search for a pattern, possibly in multiple files.
866 * If SRCH_FIRST_FILE is set, begin searching at the first file.
867 * If SRCH_PAST_EOF is set, continue the search thru multiple files.
870 multi_search(pattern, n, silent)
880 save_ifile = save_curr_ifile();
882 if (search_type & SRCH_FIRST_FILE)
885 * Start at the first (or last) file
886 * in the command line list.
888 if (search_type & SRCH_FORW)
889 nomore = edit_first();
891 nomore = edit_last();
894 unsave_ifile(save_ifile);
898 search_type &= ~SRCH_FIRST_FILE;
903 n = search(search_type, pattern, n);
905 * The SRCH_NO_MOVE flag doesn't "stick": it gets cleared
906 * after being used once. This allows "n" to work after
907 * using a /@@ search.
909 search_type &= ~SRCH_NO_MOVE;
915 unsave_ifile(save_ifile);
921 * Some kind of error in the search.
922 * Error message has been printed by search().
926 if ((search_type & SRCH_PAST_EOF) == 0)
928 * We didn't find a match, but we're
929 * supposed to search only one file.
933 * Move on to the next file.
935 if (search_type & SRCH_FORW)
936 nomore = edit_next(1);
938 nomore = edit_prev(1);
946 * Print an error message if we haven't already.
948 if (n > 0 && !silent)
949 error("Pattern not found", NULL_PARG);
954 * Restore the file we were originally viewing.
956 reedit_ifile(save_ifile);
959 unsave_ifile(save_ifile);
964 * Forward forever, or until a highlighted line appears.
967 forw_loop(until_hilite)
972 if (ch_getflags() & CH_HELPFILE)
976 jump_forw_buffered();
977 curr_len = ch_length();
978 highest_hilite = until_hilite ? curr_len : NULL_POSITION;
982 if (until_hilite && highest_hilite > curr_len)
994 * This gets us back in "F mode" after processing
995 * a non-abort signal (e.g. window-change).
997 if (sigs && !ABORT_SIGS())
998 return (until_hilite ? A_F_UNTIL_HILITE : A_F_FOREVER);
1000 return (A_NOACTION);
1004 * Main command processor.
1005 * Accept and execute commands until a quit command.
1011 register int action;
1012 register char *cbuf;
1014 int save_search_type;
1022 search_type = SRCH_FORW;
1023 wscroll = (sc_height + 1) / 2;
1024 newaction = A_NOACTION;
1034 * See if any signals need processing.
1040 quit(QUIT_SAVED_STATUS);
1044 * See if window size changed, for systems that don't
1045 * generate SIGWINCH.
1050 * Display prompt and accept a character.
1056 if (newaction == A_NOACTION)
1063 if (newaction != A_NOACTION)
1066 newaction = A_NOACTION;
1070 * If we are in a multicharacter command, call mca_char.
1071 * Otherwise we call fcmd_decode to determine the
1072 * action to be performed.
1075 switch (mca_char(c))
1079 * Need another character.
1085 * Command has been handled by mca_char.
1086 * Start clean with a prompt.
1091 * Not a multi-char command
1092 * (at least, not anymore).
1098 * Decode the command character and decide what to do.
1103 * We're in a multichar command.
1104 * Add the character to the command buffer
1105 * and display it on the screen.
1106 * If the user backspaces past the start
1107 * of the line, abort the command.
1109 if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0)
1111 cbuf = get_cmdbuf();
1115 * Don't use cmd_char if we're starting fresh
1116 * at the beginning of a command, because we
1117 * don't want to echo the command until we know
1118 * it is a multichar command. We also don't
1119 * want erase_char/kill_char to be treated
1120 * as line editing characters.
1127 action = fcmd_decode(cbuf, &extra);
1129 * If an "extra" string was returned,
1130 * process it as a string of command characters.
1136 * Clear the cmdbuf string.
1137 * (But not if we're in the prefix of a command,
1138 * because the partial command string is kept there.)
1140 if (action != A_PREFIX)
1147 * First digit of a number.
1149 start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE);
1154 * Forward one window (and set the window size).
1157 swindow = (int) number;
1161 * Forward one screen.
1164 number = get_swindow();
1167 set_attnpos(bottompos);
1168 forward((int) number, 0, 1);
1173 * Backward one window (and set the window size).
1176 swindow = (int) number;
1180 * Backward one screen.
1183 number = get_swindow();
1185 backward((int) number, 0, 1);
1190 * Forward N (default 1) line.
1195 if (show_attn == OPT_ONPLUS && number > 1)
1196 set_attnpos(bottompos);
1197 forward((int) number, 0, 0);
1202 * Backward N (default 1) line.
1207 backward((int) number, 0, 0);
1212 * Force forward N (default 1) line.
1217 if (show_attn == OPT_ONPLUS && number > 1)
1218 set_attnpos(bottompos);
1219 forward((int) number, 1, 0);
1224 * Force backward N (default 1) line.
1229 backward((int) number, 1, 0);
1234 * Force forward one screen.
1237 number = get_swindow();
1239 if (show_attn == OPT_ONPLUS)
1240 set_attnpos(bottompos);
1241 forward((int) number, 1, 0);
1246 * Forward forever, ignoring EOF.
1249 set_attnpos(bottompos);
1250 newaction = forw_loop(0);
1253 case A_F_UNTIL_HILITE:
1254 newaction = forw_loop(1);
1260 * (default same as last 'd' or 'u' command).
1263 wscroll = (int) number;
1265 if (show_attn == OPT_ONPLUS)
1266 set_attnpos(bottompos);
1267 forward(wscroll, 0, 0);
1273 * (default same as last 'd' or 'u' command).
1276 wscroll = (int) number;
1278 backward(wscroll, 0, 0);
1283 * Flush buffers, then repaint screen.
1284 * Don't flush the buffers on a pipe!
1298 * Go to line N, default beginning of file.
1308 * Go to a specified percentage into the file.
1321 jump_percent((int) number, fraction);
1326 * Go to line N, default end of file.
1337 * Go to line N, default last buffered byte.
1341 jump_forw_buffered();
1348 * Go to a specified byte position in the file.
1353 jump_line_loc((POSITION) number, jump_sline);
1358 * Print file name, etc.
1360 if (ch_getflags() & CH_HELPFILE)
1363 parg.p_string = eq_message();
1369 * Print version number, without the "@(#)".
1379 if (curr_ifile != NULL_IFILE &&
1380 ch_getflags() & CH_HELPFILE)
1383 * Quit while viewing the help file
1384 * just means return to viewing the
1387 hshift = save_hshift;
1388 bs_mode = save_bs_mode;
1389 if (edit_prev(1) == 0)
1398 * Define abbreviation for a commonly used sequence below.
1400 #define DO_SEARCH() \
1401 if (number <= 0) number = 1; \
1404 multi_search((char *)NULL, (int) number, 0);
1409 * Search forward for a pattern.
1410 * Get the first char of the pattern.
1412 search_type = SRCH_FORW;
1421 * Search backward for a pattern.
1422 * Get the first char of the pattern.
1424 search_type = SRCH_BACK;
1433 search_type = SRCH_FORW | SRCH_FILTER;
1438 error("Command not available", NULL_PARG);
1442 case A_AGAIN_SEARCH:
1444 * Repeat previous search.
1449 case A_T_AGAIN_SEARCH:
1451 * Repeat previous search, multiple files.
1453 search_type |= SRCH_PAST_EOF;
1457 case A_REVERSE_SEARCH:
1459 * Repeat previous search, in reverse direction.
1461 save_search_type = search_type;
1462 search_type = SRCH_REVERSE(search_type);
1464 search_type = save_search_type;
1467 case A_T_REVERSE_SEARCH:
1469 * Repeat previous search,
1470 * multiple files in reverse direction.
1472 save_search_type = search_type;
1473 search_type = SRCH_REVERSE(search_type);
1474 search_type |= SRCH_PAST_EOF;
1476 search_type = save_search_type;
1487 if (ch_getflags() & CH_HELPFILE)
1490 save_hshift = hshift;
1492 save_bs_mode = bs_mode;
1493 bs_mode = BS_SPECIAL;
1494 (void) edit(FAKE_HELPFILE);
1500 * Edit a new file. Get the filename.
1504 error("Command not available", NULL_PARG);
1507 start_mca(A_EXAMINE, "Examine: ", ml_examine, 0);
1511 error("Command not available", NULL_PARG);
1517 * Invoke an editor on the input file.
1522 error("Command not available", NULL_PARG);
1525 if (ch_getflags() & CH_HELPFILE)
1527 if (strcmp(get_filename(curr_ifile), "-") == 0)
1529 error("Cannot edit standard input", NULL_PARG);
1532 if (curr_altfilename != NULL)
1534 error("WARNING: This file was viewed via LESSOPEN",
1537 start_mca(A_SHELL, "!", ml_shell, 0);
1539 * Expand the editor prototype string
1540 * and pass it to the system to execute.
1541 * (Make sure the screen is displayed so the
1542 * expansion of "+%lm" works.)
1546 lsystem(pr_expand(editproto, 0), (char*)NULL);
1549 error("Command not available", NULL_PARG);
1555 * Examine next file.
1560 error("No next file", NULL_PARG);
1566 if (edit_next((int) number))
1568 if (get_quit_at_eof() && eof_displayed() &&
1569 !(ch_getflags() & CH_HELPFILE))
1571 parg.p_string = (number > 1) ? "(N-th) " : "";
1572 error("No %snext file", &parg);
1578 * Examine previous file.
1583 error("No previous file", NULL_PARG);
1589 if (edit_prev((int) number))
1591 parg.p_string = (number > 1) ? "(N-th) " : "";
1592 error("No %sprevious file", &parg);
1600 tagfile = nexttag((int) number);
1601 if (tagfile == NULL)
1603 error("No next tag", NULL_PARG);
1606 if (edit(tagfile) == 0)
1608 POSITION pos = tagsearch();
1609 if (pos != NULL_POSITION)
1610 jump_loc(pos, jump_sline);
1613 error("Command not available", NULL_PARG);
1621 tagfile = prevtag((int) number);
1622 if (tagfile == NULL)
1624 error("No previous tag", NULL_PARG);
1627 if (edit(tagfile) == 0)
1629 POSITION pos = tagsearch();
1630 if (pos != NULL_POSITION)
1631 jump_loc(pos, jump_sline);
1634 error("Command not available", NULL_PARG);
1640 * Examine a particular file.
1644 if (edit_index((int) number))
1645 error("No such file", NULL_PARG);
1649 if (ch_getflags() & CH_HELPFILE)
1651 old_ifile = curr_ifile;
1652 new_ifile = getoff_ifile(curr_ifile);
1653 if (new_ifile == NULL_IFILE)
1658 if (edit_ifile(new_ifile) != 0)
1660 reedit_ifile(old_ifile);
1663 del_ifile(old_ifile);
1667 optflag = OPT_TOGGLE;
1675 * Report a flag setting.
1677 optflag = OPT_NO_TOGGLE;
1685 * Set an initial command for new files.
1687 start_mca(A_FIRSTCMD, "+", (void*)NULL, 0);
1698 error("Command not available", NULL_PARG);
1701 start_mca(A_SHELL, "!", ml_shell, 0);
1705 error("Command not available", NULL_PARG);
1713 if (ch_getflags() & CH_HELPFILE)
1715 start_mca(A_SETMARK, "mark: ", (void*)NULL, 0);
1717 if (c == erase_char || c == erase2_char ||
1718 c == kill_char || c == '\n' || c == '\r')
1727 start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0);
1729 if (c == erase_char || c == erase2_char ||
1730 c == kill_char || c == '\n' || c == '\r')
1740 error("Command not available", NULL_PARG);
1743 start_mca(A_PIPE, "|mark: ", (void*)NULL, 0);
1745 if (c == erase_char || c == erase2_char || c == kill_char)
1747 if (c == '\n' || c == '\r')
1752 start_mca(A_PIPE, "!", ml_shell, 0);
1756 error("Command not available", NULL_PARG);
1762 start_mca(action, "Brackets: ", (void*)NULL, 0);
1768 shift_count = number;
1770 number = (shift_count > 0) ?
1771 shift_count : sc_width / 2;
1772 if (number > hshift)
1780 shift_count = number;
1782 number = (shift_count > 0) ?
1783 shift_count : sc_width / 2;
1790 * The command is incomplete (more chars are needed).
1791 * Display the current char, so the user knows
1792 * what's going on, and get another character.
1794 if (mca != A_PREFIX)
1797 start_mca(A_PREFIX, " ", (void*)NULL,