2 * Copyright (C) 1984-2009 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;
58 static char ungot[UNGOT_SIZE];
59 static char *ungotp = NULL;
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 */
69 static int optgetname;
70 static POSITION bottompos;
71 static int save_hshift;
76 static void multi_search();
79 * Move the cursor to start of prompt line before executing a command.
80 * This looks nicer if the command takes a long time before
81 * updating the screen.
94 * Set up the display to start a new multi-character command.
97 start_mca(action, prompt, mlist, cmdflags)
107 set_mlist(mlist, cmdflags);
113 return (mca != 0 && mca != A_PREFIX);
117 * Set up the display to start a new search command.
123 if (search_type & SRCH_FILTER)
127 if (search_type & SRCH_FORW)
135 if (search_type & SRCH_NO_MATCH)
136 cmd_putstr("Non-match ");
137 if (search_type & SRCH_FIRST_FILE)
138 cmd_putstr("First-file ");
139 if (search_type & SRCH_PAST_EOF)
140 cmd_putstr("EOF-ignore ");
141 if (search_type & SRCH_NO_MOVE)
142 cmd_putstr("Keep-pos ");
143 if (search_type & SRCH_NO_REGEX)
144 cmd_putstr("Regex-off ");
147 if (search_type & SRCH_FILTER)
151 if (search_type & SRCH_FORW)
155 set_mlist(ml_search, 0);
159 * Set up the display to start a new toggle-option command.
168 no_prompt = (optflag & OPT_NO_PROMPT);
169 flag = (optflag & ~OPT_NO_PROMPT);
170 dash = (flag == OPT_NO_TOGGLE) ? "_" : "-";
193 * Execute a multicharacter command.
207 multi_search(cbuf, (int) number);
211 search_type ^= SRCH_NO_MATCH;
212 set_filter_pattern(cbuf, search_type);
217 * Skip leading spaces or + signs in the string.
219 while (*cbuf == '+' || *cbuf == ' ')
221 if (every_first_cmd != NULL)
222 free(every_first_cmd);
224 every_first_cmd = NULL;
226 every_first_cmd = save(cbuf);
229 toggle_option(optchar, cbuf, optflag);
233 match_brac(cbuf[0], cbuf[1], 1, (int) number);
236 match_brac(cbuf[1], cbuf[0], 0, (int) number);
244 /* If tag structure is loaded then clean it up. */
252 * !! just uses whatever is in shellcmd.
253 * Otherwise, copy cmdbuf to shellcmd,
254 * expanding any special characters ("%" or "#").
258 if (shellcmd != NULL)
260 shellcmd = fexpand(cbuf);
265 if (shellcmd == NULL)
266 lsystem("", "!done");
268 lsystem(shellcmd, "!done");
275 (void) pipe_mark(pipec, cbuf);
276 error("|done", NULL_PARG);
283 * Add a character to a multi-character command.
298 * Not in a multicharacter command.
304 * In the prefix of a command.
305 * This not considered a multichar command
306 * (even tho it uses cmdbuf, etc.).
307 * It is handled in the commands() switch.
313 * Entering digits of a number.
314 * Terminated by a non-digit.
316 if (!((c >= '0' && c <= '9') || c == '.') &&
317 editchar(c, EC_PEEK|EC_NOHISTORY|EC_NOCOMPLETE|EC_NORIGHTLEFT) == A_INVALID)
320 * Not part of the number.
321 * Treat as a normal command character.
323 number = cmd_int(&fraction);
332 * Special case for the TOGGLE_OPTION command.
333 * If the option letter which was entered is a
334 * single-char option, execute the command immediately,
335 * so user doesn't have to hit RETURN.
336 * If the first char is + or -, this indicates
337 * OPT_UNSET or OPT_SET respectively, instead of OPT_TOGGLE.
338 * "--" begins inputting a long option name.
340 if (optchar == '\0' && len_cmdbuf() == 0)
342 flag = (optflag & ~OPT_NO_PROMPT);
343 if (flag == OPT_NO_TOGGLE)
348 /* "__" = long option name. */
359 optflag = (flag == OPT_UNSET) ?
360 OPT_TOGGLE : OPT_UNSET;
365 optflag = (flag == OPT_SET) ?
366 OPT_TOGGLE : OPT_SET;
370 optflag ^= OPT_NO_PROMPT;
374 /* "--" = long option name. */
384 * We're getting a long option name.
385 * See if we've matched an option name yet.
386 * If so, display the complete name and stop
387 * accepting chars until user hits RETURN.
393 if (c == '\n' || c == '\r')
396 * When the user hits RETURN, make sure
397 * we've matched an option name, then
398 * pretend he just entered the equivalent
403 parg.p_string = get_cmdbuf();
404 error("There is no --%s option", &parg);
415 * Already have a match for the name.
416 * Don't accept anything but erase/kill.
418 if (c == erase_char ||
425 * Add char to cmd buffer and try to match
428 if (cmd_char(c) == CC_QUIT)
431 lc = ASCII_IS_LOWER(p[0]);
432 o = findopt_name(&p, &oname, NULL);
437 * Remember the option letter and
438 * display the full option name.
440 optchar = o->oletter;
441 if (!lc && ASCII_IS_LOWER(optchar))
442 optchar = ASCII_TO_UPPER(optchar);
445 for (p = oname; *p != '\0'; p++)
448 if (!lc && ASCII_IS_LOWER(c))
449 c = ASCII_TO_UPPER(c);
450 if (cmd_char(c) != CC_OK)
458 if (c == erase_char || c == erase2_char || c == kill_char)
461 /* We already have the option letter. */
466 if ((optflag & ~OPT_NO_PROMPT) != OPT_TOGGLE ||
467 single_char_option(c))
469 toggle_option(c, "", optflag);
473 * Display a prompt appropriate for the option letter.
475 if ((p = opt_prompt(c)) == NULL)
482 start_mca(A_OPT_TOGGLE, p, (void*)NULL, 0);
489 * Special case for search commands.
490 * Certain characters as the first char of
491 * the pattern have special meaning:
492 * ! Toggle the NO_MATCH flag
493 * * Toggle the PAST_EOF flag
494 * @ Toggle the FIRST_FILE flag
496 if (len_cmdbuf() > 0)
498 * Only works for the first char of the pattern.
505 case CONTROL('E'): /* ignore END of file */
508 flag = SRCH_PAST_EOF;
510 case CONTROL('F'): /* FIRST file */
513 flag = SRCH_FIRST_FILE;
515 case CONTROL('K'): /* KEEP position */
519 case CONTROL('R'): /* Don't use REGULAR EXPRESSIONS */
520 flag = SRCH_NO_REGEX;
522 case CONTROL('N'): /* NOT match */
524 flag = SRCH_NO_MATCH;
537 * Any other multicharacter command
538 * is terminated by a newline.
540 if (c == '\n' || c == '\r')
543 * Execute the command.
550 * Append the char to the command buffer.
552 if (cmd_char(c) == CC_QUIT)
554 * Abort the multi-char command.
558 if ((mca == A_F_BRACKET || mca == A_B_BRACKET) && len_cmdbuf() >= 2)
561 * Special case for the bracket-matching commands.
562 * Execute the command after getting exactly two
563 * characters from the user.
570 * Need another character.
576 * Discard any buffered file data.
581 if (!(ch_getflags() & CH_CANSEEK))
591 * Make sure the screen is displayed.
597 * If nothing is displayed yet, display starting from initial_scrpos.
601 if (initial_scrpos.pos == NULL_POSITION)
603 * {{ Maybe this should be:
604 * jump_loc(ch_zero(), jump_sline);
605 * but this behavior seems rather unexpected
606 * on the first screen. }}
608 jump_loc(ch_zero(), 1);
610 jump_loc(initial_scrpos.pos, initial_scrpos.ln);
611 } else if (screen_trashed)
613 int save_top_scroll = top_scroll;
614 int save_ignore_eoi = ignore_eoi;
617 if (screen_trashed == 2)
619 /* Special case used by ignore_eoi: re-open the input file
620 * and jump to the end of the file. */
625 top_scroll = save_top_scroll;
626 ignore_eoi = save_ignore_eoi;
631 * Display the appropriate prompt.
638 if (ungotp != NULL && ungotp > ungot)
641 * No prompt necessary if commands are from
642 * ungotten chars rather than from the user.
648 * Make sure the screen is displayed.
651 bottompos = position(BOTTOM_PLUS_ONE);
654 * If we've hit EOF on the last file and the -E flag is set, quit.
656 if (get_quit_at_eof() == OPT_ONPLUS &&
657 eof_displayed() && !(ch_getflags() & CH_HELPFILE) &&
658 next_ifile(curr_ifile) == NULL_IFILE)
662 * If the entire file is displayed and the -F flag is set, quit.
664 if (quit_if_one_screen &&
665 entire_file_displayed() && !(ch_getflags() & CH_HELPFILE) &&
666 next_ifile(curr_ifile) == NULL_IFILE)
669 #if MSDOS_COMPILER==WIN32C
671 * In Win32, display the file name in the window title.
673 if (!(ch_getflags() & CH_HELPFILE))
674 SetConsoleTitle(pr_expand("Less?f - %f.", 0));
677 * Select the proper prompt and display it.
680 * If the previous action was a forward movement,
681 * don't clear the bottom line of the display;
682 * just print the prompt since the forward movement guarantees
683 * that we're in the right position to display the prompt.
684 * Clearing the line could cause a problem: for example, if the last
685 * line displayed ended at the right screen edge without a newline,
686 * then clearing would clear the last displayed line rather than
696 if (p == NULL || *p == '\0')
700 at_enter(AT_STANDOUT);
708 * Display the less version message.
715 parg.p_string = version;
716 error("less %s", &parg);
720 * Get command character.
721 * The character normally comes from the keyboard,
722 * but may come from ungotten characters
723 * (characters previously given to ungetcc or ungetsc).
730 * Normal case: no ungotten chars, so get one from the user.
736 * Return the next ungotten char.
741 * We have just run out of ungotten chars.
744 if (len_cmdbuf() == 0 || !empty_screen())
747 * Command is incomplete, so try to complete it.
753 * We have a number but no command. Treat as #g.
760 * We have "/string" but no newline. Add the \n.
766 * Some other incomplete command. Let user complete it.
773 * "Unget" a command character.
774 * The next getcc() will return this character.
782 if (ungotp >= ungot + sizeof(ungot))
784 error("ungetcc overflow", NULL_PARG);
791 * Unget a whole string of command characters.
792 * The next sequence of getcc()'s will return this string.
800 for (p = s + strlen(s) - 1; p >= s; p--)
805 * Search for a pattern, possibly in multiple files.
806 * If SRCH_FIRST_FILE is set, begin searching at the first file.
807 * If SRCH_PAST_EOF is set, continue the search thru multiple files.
810 multi_search(pattern, n)
819 save_ifile = save_curr_ifile();
821 if (search_type & SRCH_FIRST_FILE)
824 * Start at the first (or last) file
825 * in the command line list.
827 if (search_type & SRCH_FORW)
828 nomore = edit_first();
830 nomore = edit_last();
833 unsave_ifile(save_ifile);
837 search_type &= ~SRCH_FIRST_FILE;
842 n = search(search_type, pattern, n);
844 * The SRCH_NO_MOVE flag doesn't "stick": it gets cleared
845 * after being used once. This allows "n" to work after
846 * using a /@@ search.
848 search_type &= ~SRCH_NO_MOVE;
854 unsave_ifile(save_ifile);
860 * Some kind of error in the search.
861 * Error message has been printed by search().
865 if ((search_type & SRCH_PAST_EOF) == 0)
867 * We didn't find a match, but we're
868 * supposed to search only one file.
872 * Move on to the next file.
874 if (search_type & SRCH_FORW)
875 nomore = edit_next(1);
877 nomore = edit_prev(1);
885 * Print an error message if we haven't already.
888 error("Pattern not found", NULL_PARG);
893 * Restore the file we were originally viewing.
895 reedit_ifile(save_ifile);
898 unsave_ifile(save_ifile);
903 * Main command processor.
904 * Accept and execute commands until a quit command.
913 int save_search_type;
921 search_type = SRCH_FORW;
922 wscroll = (sc_height + 1) / 2;
923 newaction = A_NOACTION;
933 * See if any signals need processing.
939 quit(QUIT_SAVED_STATUS);
943 * See if window size changed, for systems that don't
949 * Display prompt and accept a character.
955 if (newaction == A_NOACTION)
962 if (newaction != A_NOACTION)
965 newaction = A_NOACTION;
969 * If we are in a multicharacter command, call mca_char.
970 * Otherwise we call fcmd_decode to determine the
971 * action to be performed.
978 * Need another character.
984 * Command has been handled by mca_char.
985 * Start clean with a prompt.
990 * Not a multi-char command
991 * (at least, not anymore).
997 * Decode the command character and decide what to do.
1002 * We're in a multichar command.
1003 * Add the character to the command buffer
1004 * and display it on the screen.
1005 * If the user backspaces past the start
1006 * of the line, abort the command.
1008 if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0)
1010 cbuf = get_cmdbuf();
1014 * Don't use cmd_char if we're starting fresh
1015 * at the beginning of a command, because we
1016 * don't want to echo the command until we know
1017 * it is a multichar command. We also don't
1018 * want erase_char/kill_char to be treated
1019 * as line editing characters.
1026 action = fcmd_decode(cbuf, &extra);
1028 * If an "extra" string was returned,
1029 * process it as a string of command characters.
1035 * Clear the cmdbuf string.
1036 * (But not if we're in the prefix of a command,
1037 * because the partial command string is kept there.)
1039 if (action != A_PREFIX)
1046 * First digit of a number.
1048 start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE);
1053 * Forward one window (and set the window size).
1056 swindow = (int) number;
1060 * Forward one screen.
1063 number = get_swindow();
1066 set_attnpos(bottompos);
1067 forward((int) number, 0, 1);
1072 * Backward one window (and set the window size).
1075 swindow = (int) number;
1079 * Backward one screen.
1082 number = get_swindow();
1084 backward((int) number, 0, 1);
1089 * Forward N (default 1) line.
1094 if (show_attn == OPT_ONPLUS && number > 1)
1095 set_attnpos(bottompos);
1096 forward((int) number, 0, 0);
1101 * Backward N (default 1) line.
1106 backward((int) number, 0, 0);
1111 * Force forward N (default 1) line.
1116 if (show_attn == OPT_ONPLUS && number > 1)
1117 set_attnpos(bottompos);
1118 forward((int) number, 1, 0);
1123 * Force backward N (default 1) line.
1128 backward((int) number, 1, 0);
1133 * Force forward one screen.
1136 number = get_swindow();
1138 if (show_attn == OPT_ONPLUS)
1139 set_attnpos(bottompos);
1140 forward((int) number, 1, 0);
1145 * Forward forever, ignoring EOF.
1147 if (ch_getflags() & CH_HELPFILE)
1159 * This gets us back in "F mode" after processing
1160 * a non-abort signal (e.g. window-change).
1162 if (sigs && !ABORT_SIGS())
1163 newaction = A_F_FOREVER;
1169 * (default same as last 'd' or 'u' command).
1172 wscroll = (int) number;
1174 if (show_attn == OPT_ONPLUS)
1175 set_attnpos(bottompos);
1176 forward(wscroll, 0, 0);
1182 * (default same as last 'd' or 'u' command).
1185 wscroll = (int) number;
1187 backward(wscroll, 0, 0);
1192 * Flush buffers, then repaint screen.
1193 * Don't flush the buffers on a pipe!
1207 * Go to line N, default beginning of file.
1217 * Go to a specified percentage into the file.
1230 jump_percent((int) number, fraction);
1235 * Go to line N, default end of file.
1246 * Go to a specified byte position in the file.
1251 jump_line_loc((POSITION) number, jump_sline);
1256 * Print file name, etc.
1258 if (ch_getflags() & CH_HELPFILE)
1261 parg.p_string = eq_message();
1267 * Print version number, without the "@(#)".
1277 if (curr_ifile != NULL_IFILE &&
1278 ch_getflags() & CH_HELPFILE)
1281 * Quit while viewing the help file
1282 * just means return to viewing the
1285 hshift = save_hshift;
1286 if (edit_prev(1) == 0)
1295 * Define abbreviation for a commonly used sequence below.
1297 #define DO_SEARCH() \
1298 if (number <= 0) number = 1; \
1301 multi_search((char *)NULL, (int) number);
1306 * Search forward for a pattern.
1307 * Get the first char of the pattern.
1309 search_type = SRCH_FORW;
1318 * Search backward for a pattern.
1319 * Get the first char of the pattern.
1321 search_type = SRCH_BACK;
1330 search_type = SRCH_FORW | SRCH_FILTER;
1335 error("Command not available", NULL_PARG);
1339 case A_AGAIN_SEARCH:
1341 * Repeat previous search.
1346 case A_T_AGAIN_SEARCH:
1348 * Repeat previous search, multiple files.
1350 search_type |= SRCH_PAST_EOF;
1354 case A_REVERSE_SEARCH:
1356 * Repeat previous search, in reverse direction.
1358 save_search_type = search_type;
1359 search_type = SRCH_REVERSE(search_type);
1361 search_type = save_search_type;
1364 case A_T_REVERSE_SEARCH:
1366 * Repeat previous search,
1367 * multiple files in reverse direction.
1369 save_search_type = search_type;
1370 search_type = SRCH_REVERSE(search_type);
1371 search_type |= SRCH_PAST_EOF;
1373 search_type = save_search_type;
1384 if (ch_getflags() & CH_HELPFILE)
1387 save_hshift = hshift;
1389 (void) edit(FAKE_HELPFILE);
1395 * Edit a new file. Get the filename.
1399 error("Command not available", NULL_PARG);
1402 start_mca(A_EXAMINE, "Examine: ", ml_examine, 0);
1406 error("Command not available", NULL_PARG);
1412 * Invoke an editor on the input file.
1417 error("Command not available", NULL_PARG);
1420 if (ch_getflags() & CH_HELPFILE)
1422 if (strcmp(get_filename(curr_ifile), "-") == 0)
1424 error("Cannot edit standard input", NULL_PARG);
1427 if (curr_altfilename != NULL)
1429 error("WARNING: This file was viewed via LESSOPEN",
1432 start_mca(A_SHELL, "!", ml_shell, 0);
1434 * Expand the editor prototype string
1435 * and pass it to the system to execute.
1436 * (Make sure the screen is displayed so the
1437 * expansion of "+%lm" works.)
1441 lsystem(pr_expand(editproto, 0), (char*)NULL);
1444 error("Command not available", NULL_PARG);
1450 * Examine next file.
1455 error("No next file", NULL_PARG);
1461 if (edit_next((int) number))
1463 if (get_quit_at_eof() && eof_displayed() &&
1464 !(ch_getflags() & CH_HELPFILE))
1466 parg.p_string = (number > 1) ? "(N-th) " : "";
1467 error("No %snext file", &parg);
1473 * Examine previous file.
1478 error("No previous file", NULL_PARG);
1484 if (edit_prev((int) number))
1486 parg.p_string = (number > 1) ? "(N-th) " : "";
1487 error("No %sprevious file", &parg);
1495 tagfile = nexttag((int) number);
1496 if (tagfile == NULL)
1498 error("No next tag", NULL_PARG);
1501 if (edit(tagfile) == 0)
1503 POSITION pos = tagsearch();
1504 if (pos != NULL_POSITION)
1505 jump_loc(pos, jump_sline);
1508 error("Command not available", NULL_PARG);
1516 tagfile = prevtag((int) number);
1517 if (tagfile == NULL)
1519 error("No previous tag", NULL_PARG);
1522 if (edit(tagfile) == 0)
1524 POSITION pos = tagsearch();
1525 if (pos != NULL_POSITION)
1526 jump_loc(pos, jump_sline);
1529 error("Command not available", NULL_PARG);
1535 * Examine a particular file.
1539 if (edit_index((int) number))
1540 error("No such file", NULL_PARG);
1544 if (ch_getflags() & CH_HELPFILE)
1546 old_ifile = curr_ifile;
1547 new_ifile = getoff_ifile(curr_ifile);
1548 if (new_ifile == NULL_IFILE)
1553 if (edit_ifile(new_ifile) != 0)
1555 reedit_ifile(old_ifile);
1558 del_ifile(old_ifile);
1562 optflag = OPT_TOGGLE;
1570 * Report a flag setting.
1572 optflag = OPT_NO_TOGGLE;
1580 * Set an initial command for new files.
1582 start_mca(A_FIRSTCMD, "+", (void*)NULL, 0);
1593 error("Command not available", NULL_PARG);
1596 start_mca(A_SHELL, "!", ml_shell, 0);
1600 error("Command not available", NULL_PARG);
1608 if (ch_getflags() & CH_HELPFILE)
1610 start_mca(A_SETMARK, "mark: ", (void*)NULL, 0);
1612 if (c == erase_char || c == erase2_char ||
1613 c == kill_char || c == '\n' || c == '\r')
1622 start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0);
1624 if (c == erase_char || c == erase2_char ||
1625 c == kill_char || c == '\n' || c == '\r')
1635 error("Command not available", NULL_PARG);
1638 start_mca(A_PIPE, "|mark: ", (void*)NULL, 0);
1640 if (c == erase_char || c == erase2_char || c == kill_char)
1642 if (c == '\n' || c == '\r')
1647 start_mca(A_PIPE, "!", ml_shell, 0);
1651 error("Command not available", NULL_PARG);
1657 start_mca(action, "Brackets: ", (void*)NULL, 0);
1663 shift_count = number;
1665 number = (shift_count > 0) ?
1666 shift_count : sc_width / 2;
1667 if (number > hshift)
1675 shift_count = number;
1677 number = (shift_count > 0) ?
1678 shift_count : sc_width / 2;
1685 * The command is incomplete (more chars are needed).
1686 * Display the current char, so the user knows
1687 * what's going on, and get another character.
1689 if (mca != A_PREFIX)
1692 start_mca(A_PREFIX, " ", (void*)NULL,