3 * Copyright (C) 1984-2009 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;
60 static char ungot[UNGOT_SIZE];
61 static char *ungotp = NULL;
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 */
71 static int optgetname;
72 static POSITION bottompos;
73 static int save_hshift;
78 static void multi_search();
81 * Move the cursor to start of prompt line before executing a command.
82 * This looks nicer if the command takes a long time before
83 * updating the screen.
96 * Set up the display to start a new multi-character command.
99 start_mca(action, prompt, mlist, cmdflags)
109 set_mlist(mlist, cmdflags);
115 return (mca != 0 && mca != A_PREFIX);
119 * Set up the display to start a new search command.
125 if (search_type & SRCH_FILTER)
129 if (search_type & SRCH_FORW)
137 if (search_type & SRCH_NO_MATCH)
138 cmd_putstr("Non-match ");
139 if (search_type & SRCH_FIRST_FILE)
140 cmd_putstr("First-file ");
141 if (search_type & SRCH_PAST_EOF)
142 cmd_putstr("EOF-ignore ");
143 if (search_type & SRCH_NO_MOVE)
144 cmd_putstr("Keep-pos ");
145 if (search_type & SRCH_NO_REGEX)
146 cmd_putstr("Regex-off ");
149 if (search_type & SRCH_FILTER)
153 if (search_type & SRCH_FORW)
157 set_mlist(ml_search, 0);
161 * Set up the display to start a new toggle-option command.
170 no_prompt = (optflag & OPT_NO_PROMPT);
171 flag = (optflag & ~OPT_NO_PROMPT);
172 dash = (flag == OPT_NO_TOGGLE) ? "_" : "-";
195 * Execute a multicharacter command.
209 multi_search(cbuf, (int) number);
213 search_type ^= SRCH_NO_MATCH;
214 set_filter_pattern(cbuf, search_type);
219 * Skip leading spaces or + signs in the string.
221 while (*cbuf == '+' || *cbuf == ' ')
223 if (every_first_cmd != NULL)
224 free(every_first_cmd);
226 every_first_cmd = NULL;
228 every_first_cmd = save(cbuf);
231 toggle_option(optchar, cbuf, optflag);
235 match_brac(cbuf[0], cbuf[1], 1, (int) number);
238 match_brac(cbuf[1], cbuf[0], 0, (int) number);
246 /* If tag structure is loaded then clean it up. */
254 * !! just uses whatever is in shellcmd.
255 * Otherwise, copy cmdbuf to shellcmd,
256 * expanding any special characters ("%" or "#").
260 if (shellcmd != NULL)
262 shellcmd = fexpand(cbuf);
267 if (shellcmd == NULL)
268 lsystem("", "!done");
270 lsystem(shellcmd, "!done");
277 (void) pipe_mark(pipec, cbuf);
278 error("|done", NULL_PARG);
285 * Add a character to a multi-character command.
300 * Not in a multicharacter command.
306 * In the prefix of a command.
307 * This not considered a multichar command
308 * (even tho it uses cmdbuf, etc.).
309 * It is handled in the commands() switch.
315 * Entering digits of a number.
316 * Terminated by a non-digit.
318 if (!((c >= '0' && c <= '9') || c == '.') &&
319 editchar(c, EC_PEEK|EC_NOHISTORY|EC_NOCOMPLETE|EC_NORIGHTLEFT) == A_INVALID)
322 * Not part of the number.
323 * Treat as a normal command character.
325 number = cmd_int(&fraction);
334 * Special case for the TOGGLE_OPTION command.
335 * If the option letter which was entered is a
336 * single-char option, execute the command immediately,
337 * so user doesn't have to hit RETURN.
338 * If the first char is + or -, this indicates
339 * OPT_UNSET or OPT_SET respectively, instead of OPT_TOGGLE.
340 * "--" begins inputting a long option name.
342 if (optchar == '\0' && len_cmdbuf() == 0)
344 flag = (optflag & ~OPT_NO_PROMPT);
345 if (flag == OPT_NO_TOGGLE)
350 /* "__" = long option name. */
361 optflag = (flag == OPT_UNSET) ?
362 OPT_TOGGLE : OPT_UNSET;
367 optflag = (flag == OPT_SET) ?
368 OPT_TOGGLE : OPT_SET;
372 optflag ^= OPT_NO_PROMPT;
376 /* "--" = long option name. */
386 * We're getting a long option name.
387 * See if we've matched an option name yet.
388 * If so, display the complete name and stop
389 * accepting chars until user hits RETURN.
395 if (c == '\n' || c == '\r')
398 * When the user hits RETURN, make sure
399 * we've matched an option name, then
400 * pretend he just entered the equivalent
405 parg.p_string = get_cmdbuf();
406 error("There is no --%s option", &parg);
417 * Already have a match for the name.
418 * Don't accept anything but erase/kill.
420 if (c == erase_char ||
427 * Add char to cmd buffer and try to match
430 if (cmd_char(c) == CC_QUIT)
433 lc = ASCII_IS_LOWER(p[0]);
434 o = findopt_name(&p, &oname, NULL);
439 * Remember the option letter and
440 * display the full option name.
442 optchar = o->oletter;
443 if (!lc && ASCII_IS_LOWER(optchar))
444 optchar = ASCII_TO_UPPER(optchar);
447 for (p = oname; *p != '\0'; p++)
450 if (!lc && ASCII_IS_LOWER(c))
451 c = ASCII_TO_UPPER(c);
452 if (cmd_char(c) != CC_OK)
460 if (c == erase_char || c == erase2_char || c == kill_char)
463 /* We already have the option letter. */
468 if ((optflag & ~OPT_NO_PROMPT) != OPT_TOGGLE ||
469 single_char_option(c))
471 toggle_option(c, "", optflag);
475 * Display a prompt appropriate for the option letter.
477 if ((p = opt_prompt(c)) == NULL)
484 start_mca(A_OPT_TOGGLE, p, (void*)NULL, 0);
491 * Special case for search commands.
492 * Certain characters as the first char of
493 * the pattern have special meaning:
494 * ! Toggle the NO_MATCH flag
495 * * Toggle the PAST_EOF flag
496 * @ Toggle the FIRST_FILE flag
498 if (len_cmdbuf() > 0)
500 * Only works for the first char of the pattern.
510 case CONTROL('E'): /* ignore END of file */
512 flag = SRCH_PAST_EOF;
517 case CONTROL('F'): /* FIRST file */
519 flag = SRCH_FIRST_FILE;
521 case CONTROL('K'): /* KEEP position */
525 case CONTROL('R'): /* Don't use REGULAR EXPRESSIONS */
526 flag = SRCH_NO_REGEX;
528 case CONTROL('N'): /* NOT match */
530 flag = SRCH_NO_MATCH;
543 * Any other multicharacter command
544 * is terminated by a newline.
546 if (c == '\n' || c == '\r')
549 * Execute the command.
556 * Append the char to the command buffer.
558 if (cmd_char(c) == CC_QUIT)
560 * Abort the multi-char command.
564 if ((mca == A_F_BRACKET || mca == A_B_BRACKET) && len_cmdbuf() >= 2)
567 * Special case for the bracket-matching commands.
568 * Execute the command after getting exactly two
569 * characters from the user.
576 * Need another character.
582 * Discard any buffered file data.
587 if (!(ch_getflags() & CH_CANSEEK))
597 * Make sure the screen is displayed.
603 * If nothing is displayed yet, display starting from initial_scrpos.
607 if (initial_scrpos.pos == NULL_POSITION)
609 * {{ Maybe this should be:
610 * jump_loc(ch_zero(), jump_sline);
611 * but this behavior seems rather unexpected
612 * on the first screen. }}
614 jump_loc(ch_zero(), 1);
616 jump_loc(initial_scrpos.pos, initial_scrpos.ln);
617 } else if (screen_trashed)
619 int save_top_scroll = top_scroll;
620 int save_ignore_eoi = ignore_eoi;
623 if (screen_trashed == 2)
625 /* Special case used by ignore_eoi: re-open the input file
626 * and jump to the end of the file. */
631 top_scroll = save_top_scroll;
632 ignore_eoi = save_ignore_eoi;
637 * Display the appropriate prompt.
644 if (ungotp != NULL && ungotp > ungot)
647 * No prompt necessary if commands are from
648 * ungotten chars rather than from the user.
654 * Make sure the screen is displayed.
657 bottompos = position(BOTTOM_PLUS_ONE);
660 * If we've hit EOF on the last file and the -E flag is set, quit.
662 if (get_quit_at_eof() == OPT_ONPLUS &&
663 eof_displayed() && !(ch_getflags() & CH_HELPFILE) &&
664 next_ifile(curr_ifile) == NULL_IFILE)
668 * If the entire file is displayed and the -F flag is set, quit.
670 if (quit_if_one_screen &&
671 entire_file_displayed() && !(ch_getflags() & CH_HELPFILE) &&
672 next_ifile(curr_ifile) == NULL_IFILE)
675 #if MSDOS_COMPILER==WIN32C
677 * In Win32, display the file name in the window title.
679 if (!(ch_getflags() & CH_HELPFILE))
680 SetConsoleTitle(pr_expand("Less?f - %f.", 0));
683 * Select the proper prompt and display it.
686 * If the previous action was a forward movement,
687 * don't clear the bottom line of the display;
688 * just print the prompt since the forward movement guarantees
689 * that we're in the right position to display the prompt.
690 * Clearing the line could cause a problem: for example, if the last
691 * line displayed ended at the right screen edge without a newline,
692 * then clearing would clear the last displayed line rather than
702 if (p == NULL || *p == '\0')
706 at_enter(AT_STANDOUT);
714 * Display the less version message.
721 parg.p_string = version;
722 error("less %s", &parg);
726 * Get command character.
727 * The character normally comes from the keyboard,
728 * but may come from ungotten characters
729 * (characters previously given to ungetcc or ungetsc).
736 * Normal case: no ungotten chars, so get one from the user.
742 * Return the next ungotten char.
747 * We have just run out of ungotten chars.
750 if (len_cmdbuf() == 0 || !empty_screen())
753 * Command is incomplete, so try to complete it.
759 * We have a number but no command. Treat as #g.
766 * We have "/string" but no newline. Add the \n.
772 * Some other incomplete command. Let user complete it.
779 * "Unget" a command character.
780 * The next getcc() will return this character.
788 if (ungotp >= ungot + sizeof(ungot))
790 error("ungetcc overflow", NULL_PARG);
797 * Unget a whole string of command characters.
798 * The next sequence of getcc()'s will return this string.
806 for (p = s + strlen(s) - 1; p >= s; p--)
811 * Search for a pattern, possibly in multiple files.
812 * If SRCH_FIRST_FILE is set, begin searching at the first file.
813 * If SRCH_PAST_EOF is set, continue the search thru multiple files.
816 multi_search(pattern, n)
825 save_ifile = save_curr_ifile();
827 if (search_type & SRCH_FIRST_FILE)
830 * Start at the first (or last) file
831 * in the command line list.
833 if (search_type & SRCH_FORW)
834 nomore = edit_first();
836 nomore = edit_last();
839 unsave_ifile(save_ifile);
843 search_type &= ~SRCH_FIRST_FILE;
848 n = search(search_type, pattern, n);
850 * The SRCH_NO_MOVE flag doesn't "stick": it gets cleared
851 * after being used once. This allows "n" to work after
852 * using a /@@ search.
854 search_type &= ~SRCH_NO_MOVE;
860 unsave_ifile(save_ifile);
866 * Some kind of error in the search.
867 * Error message has been printed by search().
871 if ((search_type & SRCH_PAST_EOF) == 0)
873 * We didn't find a match, but we're
874 * supposed to search only one file.
878 * Move on to the next file.
880 if (search_type & SRCH_FORW)
881 nomore = edit_next(1);
883 nomore = edit_prev(1);
891 * Print an error message if we haven't already.
894 error("Pattern not found", NULL_PARG);
899 * Restore the file we were originally viewing.
901 reedit_ifile(save_ifile);
904 unsave_ifile(save_ifile);
909 * Main command processor.
910 * Accept and execute commands until a quit command.
919 int save_search_type;
927 search_type = SRCH_FORW;
928 wscroll = (sc_height + 1) / 2;
929 newaction = A_NOACTION;
939 * See if any signals need processing.
945 quit(QUIT_SAVED_STATUS);
949 * See if window size changed, for systems that don't
955 * Display prompt and accept a character.
961 if (newaction == A_NOACTION)
968 if (newaction != A_NOACTION)
971 newaction = A_NOACTION;
975 * If we are in a multicharacter command, call mca_char.
976 * Otherwise we call fcmd_decode to determine the
977 * action to be performed.
984 * Need another character.
990 * Command has been handled by mca_char.
991 * Start clean with a prompt.
996 * Not a multi-char command
997 * (at least, not anymore).
1003 * Decode the command character and decide what to do.
1008 * We're in a multichar command.
1009 * Add the character to the command buffer
1010 * and display it on the screen.
1011 * If the user backspaces past the start
1012 * of the line, abort the command.
1014 if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0)
1016 cbuf = get_cmdbuf();
1020 * Don't use cmd_char if we're starting fresh
1021 * at the beginning of a command, because we
1022 * don't want to echo the command until we know
1023 * it is a multichar command. We also don't
1024 * want erase_char/kill_char to be treated
1025 * as line editing characters.
1032 action = fcmd_decode(cbuf, &extra);
1034 * If an "extra" string was returned,
1035 * process it as a string of command characters.
1041 * Clear the cmdbuf string.
1042 * (But not if we're in the prefix of a command,
1043 * because the partial command string is kept there.)
1045 if (action != A_PREFIX)
1052 * First digit of a number.
1054 start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE);
1059 * Forward one window (and set the window size).
1062 swindow = (int) number;
1066 * Forward one screen.
1069 number = get_swindow();
1072 set_attnpos(bottompos);
1073 forward((int) number, 0, 1);
1078 * Backward one window (and set the window size).
1081 swindow = (int) number;
1085 * Backward one screen.
1088 number = get_swindow();
1090 backward((int) number, 0, 1);
1095 * Forward N (default 1) line.
1100 if (show_attn == OPT_ONPLUS && number > 1)
1101 set_attnpos(bottompos);
1102 forward((int) number, 0, 0);
1107 * Backward N (default 1) line.
1112 backward((int) number, 0, 0);
1117 * Force forward N (default 1) line.
1122 if (show_attn == OPT_ONPLUS && number > 1)
1123 set_attnpos(bottompos);
1124 forward((int) number, 1, 0);
1129 * Force backward N (default 1) line.
1134 backward((int) number, 1, 0);
1139 * Force forward one screen.
1142 number = get_swindow();
1144 if (show_attn == OPT_ONPLUS)
1145 set_attnpos(bottompos);
1146 forward((int) number, 1, 0);
1151 * Forward forever, ignoring EOF.
1153 if (ch_getflags() & CH_HELPFILE)
1165 * This gets us back in "F mode" after processing
1166 * a non-abort signal (e.g. window-change).
1168 if (sigs && !ABORT_SIGS())
1169 newaction = A_F_FOREVER;
1175 * (default same as last 'd' or 'u' command).
1178 wscroll = (int) number;
1180 if (show_attn == OPT_ONPLUS)
1181 set_attnpos(bottompos);
1182 forward(wscroll, 0, 0);
1188 * (default same as last 'd' or 'u' command).
1191 wscroll = (int) number;
1193 backward(wscroll, 0, 0);
1198 * Flush buffers, then repaint screen.
1199 * Don't flush the buffers on a pipe!
1213 * Go to line N, default beginning of file.
1223 * Go to a specified percentage into the file.
1236 jump_percent((int) number, fraction);
1241 * Go to line N, default end of file.
1252 * Go to a specified byte position in the file.
1257 jump_line_loc((POSITION) number, jump_sline);
1262 * Print file name, etc.
1264 if (ch_getflags() & CH_HELPFILE)
1267 parg.p_string = eq_message();
1273 * Print version number, without the "@(#)".
1283 if (curr_ifile != NULL_IFILE &&
1284 ch_getflags() & CH_HELPFILE)
1287 * Quit while viewing the help file
1288 * just means return to viewing the
1291 hshift = save_hshift;
1292 if (edit_prev(1) == 0)
1301 * Define abbreviation for a commonly used sequence below.
1303 #define DO_SEARCH() \
1304 if (number <= 0) number = 1; \
1307 multi_search((char *)NULL, (int) number);
1312 * Search forward for a pattern.
1313 * Get the first char of the pattern.
1315 search_type = SRCH_FORW;
1324 * Search backward for a pattern.
1325 * Get the first char of the pattern.
1327 search_type = SRCH_BACK;
1336 search_type = SRCH_FORW | SRCH_FILTER;
1341 error("Command not available", NULL_PARG);
1345 case A_AGAIN_SEARCH:
1347 * Repeat previous search.
1352 case A_T_AGAIN_SEARCH:
1354 * Repeat previous search, multiple files.
1356 search_type |= SRCH_PAST_EOF;
1360 case A_REVERSE_SEARCH:
1362 * Repeat previous search, in reverse direction.
1364 save_search_type = search_type;
1365 search_type = SRCH_REVERSE(search_type);
1367 search_type = save_search_type;
1370 case A_T_REVERSE_SEARCH:
1372 * Repeat previous search,
1373 * multiple files in reverse direction.
1375 save_search_type = search_type;
1376 search_type = SRCH_REVERSE(search_type);
1377 search_type |= SRCH_PAST_EOF;
1379 search_type = save_search_type;
1390 if (ch_getflags() & CH_HELPFILE)
1393 save_hshift = hshift;
1395 (void) edit(FAKE_HELPFILE);
1401 * Edit a new file. Get the filename.
1405 error("Command not available", NULL_PARG);
1408 start_mca(A_EXAMINE, "Examine: ", ml_examine, 0);
1412 error("Command not available", NULL_PARG);
1418 * Invoke an editor on the input file.
1423 error("Command not available", NULL_PARG);
1426 if (ch_getflags() & CH_HELPFILE)
1428 if (strcmp(get_filename(curr_ifile), "-") == 0)
1430 error("Cannot edit standard input", NULL_PARG);
1433 if (curr_altfilename != NULL)
1435 error("WARNING: This file was viewed via LESSOPEN",
1438 start_mca(A_SHELL, "!", ml_shell, 0);
1440 * Expand the editor prototype string
1441 * and pass it to the system to execute.
1442 * (Make sure the screen is displayed so the
1443 * expansion of "+%lm" works.)
1447 lsystem(pr_expand(editproto, 0), (char*)NULL);
1450 error("Command not available", NULL_PARG);
1456 * Examine next file.
1461 error("No next file", NULL_PARG);
1467 if (edit_next((int) number))
1469 if (get_quit_at_eof() && eof_displayed() &&
1470 !(ch_getflags() & CH_HELPFILE))
1472 parg.p_string = (number > 1) ? "(N-th) " : "";
1473 error("No %snext file", &parg);
1479 * Examine previous file.
1484 error("No previous file", NULL_PARG);
1490 if (edit_prev((int) number))
1492 parg.p_string = (number > 1) ? "(N-th) " : "";
1493 error("No %sprevious file", &parg);
1501 tagfile = nexttag((int) number);
1502 if (tagfile == NULL)
1504 error("No next tag", NULL_PARG);
1507 if (edit(tagfile) == 0)
1509 POSITION pos = tagsearch();
1510 if (pos != NULL_POSITION)
1511 jump_loc(pos, jump_sline);
1514 error("Command not available", NULL_PARG);
1522 tagfile = prevtag((int) number);
1523 if (tagfile == NULL)
1525 error("No previous tag", NULL_PARG);
1528 if (edit(tagfile) == 0)
1530 POSITION pos = tagsearch();
1531 if (pos != NULL_POSITION)
1532 jump_loc(pos, jump_sline);
1535 error("Command not available", NULL_PARG);
1541 * Examine a particular file.
1545 if (edit_index((int) number))
1546 error("No such file", NULL_PARG);
1550 if (ch_getflags() & CH_HELPFILE)
1552 old_ifile = curr_ifile;
1553 new_ifile = getoff_ifile(curr_ifile);
1554 if (new_ifile == NULL_IFILE)
1559 if (edit_ifile(new_ifile) != 0)
1561 reedit_ifile(old_ifile);
1564 del_ifile(old_ifile);
1568 optflag = OPT_TOGGLE;
1576 * Report a flag setting.
1578 optflag = OPT_NO_TOGGLE;
1586 * Set an initial command for new files.
1588 start_mca(A_FIRSTCMD, "+", (void*)NULL, 0);
1599 error("Command not available", NULL_PARG);
1602 start_mca(A_SHELL, "!", ml_shell, 0);
1606 error("Command not available", NULL_PARG);
1614 if (ch_getflags() & CH_HELPFILE)
1616 start_mca(A_SETMARK, "mark: ", (void*)NULL, 0);
1618 if (c == erase_char || c == erase2_char ||
1619 c == kill_char || c == '\n' || c == '\r')
1628 start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0);
1630 if (c == erase_char || c == erase2_char ||
1631 c == kill_char || c == '\n' || c == '\r')
1641 error("Command not available", NULL_PARG);
1644 start_mca(A_PIPE, "|mark: ", (void*)NULL, 0);
1646 if (c == erase_char || c == erase2_char || c == kill_char)
1648 if (c == '\n' || c == '\r')
1653 start_mca(A_PIPE, "!", ml_shell, 0);
1657 error("Command not available", NULL_PARG);
1663 start_mca(action, "Brackets: ", (void*)NULL, 0);
1669 shift_count = number;
1671 number = (shift_count > 0) ?
1672 shift_count : sc_width / 2;
1673 if (number > hshift)
1681 shift_count = number;
1683 number = (shift_count > 0) ?
1684 shift_count : sc_width / 2;
1691 * The command is incomplete (more chars are needed).
1692 * Display the current char, so the user knows
1693 * what's going on, and get another character.
1695 if (mca != A_PREFIX)
1698 start_mca(A_PREFIX, " ", (void*)NULL,