2 * Copyright (C) 1984-2007 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;
32 extern int jump_sline;
35 extern int top_scroll;
36 extern int ignore_eoi;
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;
59 static char ungot[UNGOT_SIZE];
60 static char *ungotp = NULL;
62 static char *shellcmd = NULL; /* For holding last shell command for "!!" */
64 static int mca; /* The multicharacter command (action) */
65 static int search_type; /* The previous type of search */
66 static LINENUM number; /* The number typed by the user */
67 static long fraction; /* The fractional part of the number */
70 static int optgetname;
71 static POSITION bottompos;
72 static int save_hshift;
77 static void multi_search();
80 * Move the cursor to start of prompt line before executing a command.
81 * This looks nicer if the command takes a long time before
82 * updating the screen.
93 * Set up the display to start a new multi-character command.
96 start_mca(action, prompt, mlist, cmdflags)
106 set_mlist(mlist, cmdflags);
112 return (mca != 0 && mca != A_PREFIX);
116 * Set up the display to start a new search command.
121 if (search_type & SRCH_FORW)
129 if (search_type & SRCH_NO_MATCH)
130 cmd_putstr("Non-match ");
131 if (search_type & SRCH_FIRST_FILE)
132 cmd_putstr("First-file ");
133 if (search_type & SRCH_PAST_EOF)
134 cmd_putstr("EOF-ignore ");
135 if (search_type & SRCH_NO_MOVE)
136 cmd_putstr("Keep-pos ");
137 if (search_type & SRCH_NO_REGEX)
138 cmd_putstr("Regex-off ");
140 if (search_type & SRCH_FORW)
144 set_mlist(ml_search, 0);
148 * Set up the display to start a new toggle-option command.
157 no_prompt = (optflag & OPT_NO_PROMPT);
158 flag = (optflag & ~OPT_NO_PROMPT);
159 dash = (flag == OPT_NO_TOGGLE) ? "_" : "-";
182 * Execute a multicharacter command.
196 multi_search(cbuf, (int) number);
200 * Skip leading spaces or + signs in the string.
202 while (*cbuf == '+' || *cbuf == ' ')
204 if (every_first_cmd != NULL)
205 free(every_first_cmd);
207 every_first_cmd = NULL;
209 every_first_cmd = save(cbuf);
212 toggle_option(optchar, cbuf, optflag);
216 match_brac(cbuf[0], cbuf[1], 1, (int) number);
219 match_brac(cbuf[1], cbuf[0], 0, (int) number);
227 /* If tag structure is loaded then clean it up. */
235 * !! just uses whatever is in shellcmd.
236 * Otherwise, copy cmdbuf to shellcmd,
237 * expanding any special characters ("%" or "#").
241 if (shellcmd != NULL)
243 shellcmd = fexpand(cbuf);
248 if (shellcmd == NULL)
249 lsystem("", "!done");
251 lsystem(shellcmd, "!done");
258 (void) pipe_mark(pipec, cbuf);
259 error("|done", NULL_PARG);
266 * Add a character to a multi-character command.
281 * Not in a multicharacter command.
287 * In the prefix of a command.
288 * This not considered a multichar command
289 * (even tho it uses cmdbuf, etc.).
290 * It is handled in the commands() switch.
296 * Entering digits of a number.
297 * Terminated by a non-digit.
299 if (!((c >= '0' && c <= '9') || c == '.') &&
300 editchar(c, EC_PEEK|EC_NOHISTORY|EC_NOCOMPLETE|EC_NORIGHTLEFT) == A_INVALID)
303 * Not part of the number.
304 * Treat as a normal command character.
306 number = cmd_int(&fraction);
315 * Special case for the TOGGLE_OPTION command.
316 * If the option letter which was entered is a
317 * single-char option, execute the command immediately,
318 * so user doesn't have to hit RETURN.
319 * If the first char is + or -, this indicates
320 * OPT_UNSET or OPT_SET respectively, instead of OPT_TOGGLE.
321 * "--" begins inputting a long option name.
323 if (optchar == '\0' && len_cmdbuf() == 0)
325 flag = (optflag & ~OPT_NO_PROMPT);
326 if (flag == OPT_NO_TOGGLE)
331 /* "__" = long option name. */
342 optflag = (flag == OPT_UNSET) ?
343 OPT_TOGGLE : OPT_UNSET;
348 optflag = (flag == OPT_SET) ?
349 OPT_TOGGLE : OPT_SET;
353 optflag ^= OPT_NO_PROMPT;
357 /* "--" = long option name. */
367 * We're getting a long option name.
368 * See if we've matched an option name yet.
369 * If so, display the complete name and stop
370 * accepting chars until user hits RETURN.
376 if (c == '\n' || c == '\r')
379 * When the user hits RETURN, make sure
380 * we've matched an option name, then
381 * pretend he just entered the equivalent
386 parg.p_string = get_cmdbuf();
387 error("There is no --%s option", &parg);
398 * Already have a match for the name.
399 * Don't accept anything but erase/kill.
401 if (c == erase_char ||
408 * Add char to cmd buffer and try to match
411 if (cmd_char(c) == CC_QUIT)
414 lc = ASCII_IS_LOWER(p[0]);
415 o = findopt_name(&p, &oname, NULL);
420 * Remember the option letter and
421 * display the full option name.
423 optchar = o->oletter;
424 if (!lc && ASCII_IS_LOWER(optchar))
425 optchar = ASCII_TO_UPPER(optchar);
428 for (p = oname; *p != '\0'; p++)
431 if (!lc && ASCII_IS_LOWER(c))
432 c = ASCII_TO_UPPER(c);
433 if (cmd_char(c) != CC_OK)
441 if (c == erase_char || c == erase2_char || c == kill_char)
444 /* We already have the option letter. */
449 if ((optflag & ~OPT_NO_PROMPT) != OPT_TOGGLE ||
450 single_char_option(c))
452 toggle_option(c, "", optflag);
456 * Display a prompt appropriate for the option letter.
458 if ((p = opt_prompt(c)) == NULL)
465 start_mca(A_OPT_TOGGLE, p, (void*)NULL, 0);
471 * Special case for search commands.
472 * Certain characters as the first char of
473 * the pattern have special meaning:
474 * ! Toggle the NO_MATCH flag
475 * * Toggle the PAST_EOF flag
476 * @ Toggle the FIRST_FILE flag
478 if (len_cmdbuf() > 0)
480 * Only works for the first char of the pattern.
487 case CONTROL('E'): /* ignore END of file */
489 flag = SRCH_PAST_EOF;
491 case CONTROL('F'): /* FIRST file */
493 flag = SRCH_FIRST_FILE;
495 case CONTROL('K'): /* KEEP position */
498 case CONTROL('R'): /* Don't use REGULAR EXPRESSIONS */
499 flag = SRCH_NO_REGEX;
501 case CONTROL('N'): /* NOT match */
503 flag = SRCH_NO_MATCH;
516 * Any other multicharacter command
517 * is terminated by a newline.
519 if (c == '\n' || c == '\r')
522 * Execute the command.
529 * Append the char to the command buffer.
531 if (cmd_char(c) == CC_QUIT)
533 * Abort the multi-char command.
537 if ((mca == A_F_BRACKET || mca == A_B_BRACKET) && len_cmdbuf() >= 2)
540 * Special case for the bracket-matching commands.
541 * Execute the command after getting exactly two
542 * characters from the user.
549 * Need another character.
555 * Discard any buffered file data.
560 if (!(ch_getflags() & CH_CANSEEK))
570 * Make sure the screen is displayed.
576 * If nothing is displayed yet, display starting from initial_scrpos.
580 if (initial_scrpos.pos == NULL_POSITION)
582 * {{ Maybe this should be:
583 * jump_loc(ch_zero(), jump_sline);
584 * but this behavior seems rather unexpected
585 * on the first screen. }}
587 jump_loc(ch_zero(), 1);
589 jump_loc(initial_scrpos.pos, initial_scrpos.ln);
590 } else if (screen_trashed)
592 int save_top_scroll = top_scroll;
593 int save_ignore_eoi = ignore_eoi;
596 if (screen_trashed == 2)
598 /* Special case used by ignore_eoi: re-open the input file
599 * and jump to the end of the file. */
604 top_scroll = save_top_scroll;
605 ignore_eoi = save_ignore_eoi;
610 * Display the appropriate prompt.
617 if (ungotp != NULL && ungotp > ungot)
620 * No prompt necessary if commands are from
621 * ungotten chars rather than from the user.
627 * Make sure the screen is displayed.
630 bottompos = position(BOTTOM_PLUS_ONE);
633 * If we've hit EOF on the last file, and the -E flag is set
634 * (or -F is set and this is the first prompt), then quit.
635 * {{ Relying on "first prompt" to detect a single-screen file
636 * fails if +G is used, for example. }}
638 if ((get_quit_at_eof() == OPT_ONPLUS || quit_if_one_screen) &&
639 hit_eof && !(ch_getflags() & CH_HELPFILE) &&
640 next_ifile(curr_ifile) == NULL_IFILE)
642 quit_if_one_screen = FALSE;
643 #if 0 /* This doesn't work well because some "te"s clear the screen. */
645 * If the -e flag is set and we've hit EOF on the last file,
646 * and the file is squished (shorter than the screen), quit.
648 if (get_quit_at_eof() && squished &&
649 next_ifile(curr_ifile) == NULL_IFILE)
653 #if MSDOS_COMPILER==WIN32C
655 * In Win32, display the file name in the window title.
657 if (!(ch_getflags() & CH_HELPFILE))
658 SetConsoleTitle(pr_expand("Less?f - %f.", 0));
661 * Select the proper prompt and display it.
664 * If the previous action was a forward movement,
665 * don't clear the bottom line of the display;
666 * just print the prompt since the forward movement guarantees
667 * that we're in the right position to display the prompt.
668 * Clearing the line could cause a problem: for example, if the last
669 * line displayed ended at the right screen edge without a newline,
670 * then clearing would clear the last displayed line rather than
678 if (p == NULL || *p == '\0')
682 at_enter(AT_STANDOUT);
690 * Display the less version message.
697 parg.p_string = version;
698 error("less %s", &parg);
702 * Get command character.
703 * The character normally comes from the keyboard,
704 * but may come from ungotten characters
705 * (characters previously given to ungetcc or ungetsc).
712 * Normal case: no ungotten chars, so get one from the user.
718 * Return the next ungotten char.
723 * We have just run out of ungotten chars.
726 if (len_cmdbuf() == 0 || !empty_screen())
729 * Command is incomplete, so try to complete it.
735 * We have a number but no command. Treat as #g.
742 * We have "/string" but no newline. Add the \n.
748 * Some other incomplete command. Let user complete it.
755 * "Unget" a command character.
756 * The next getcc() will return this character.
764 if (ungotp >= ungot + sizeof(ungot))
766 error("ungetcc overflow", NULL_PARG);
773 * Unget a whole string of command characters.
774 * The next sequence of getcc()'s will return this string.
782 for (p = s + strlen(s) - 1; p >= s; p--)
787 * Search for a pattern, possibly in multiple files.
788 * If SRCH_FIRST_FILE is set, begin searching at the first file.
789 * If SRCH_PAST_EOF is set, continue the search thru multiple files.
792 multi_search(pattern, n)
801 save_ifile = save_curr_ifile();
803 if (search_type & SRCH_FIRST_FILE)
806 * Start at the first (or last) file
807 * in the command line list.
809 if (search_type & SRCH_FORW)
810 nomore = edit_first();
812 nomore = edit_last();
815 unsave_ifile(save_ifile);
819 search_type &= ~SRCH_FIRST_FILE;
824 n = search(search_type, pattern, n);
826 * The SRCH_NO_MOVE flag doesn't "stick": it gets cleared
827 * after being used once. This allows "n" to work after
828 * using a /@@ search.
830 search_type &= ~SRCH_NO_MOVE;
836 unsave_ifile(save_ifile);
842 * Some kind of error in the search.
843 * Error message has been printed by search().
847 if ((search_type & SRCH_PAST_EOF) == 0)
849 * We didn't find a match, but we're
850 * supposed to search only one file.
854 * Move on to the next file.
856 if (search_type & SRCH_FORW)
857 nomore = edit_next(1);
859 nomore = edit_prev(1);
867 * Print an error message if we haven't already.
870 error("Pattern not found", NULL_PARG);
875 * Restore the file we were originally viewing.
877 reedit_ifile(save_ifile);
880 unsave_ifile(save_ifile);
885 * Main command processor.
886 * Accept and execute commands until a quit command.
895 int save_search_type;
903 search_type = SRCH_FORW;
904 wscroll = (sc_height + 1) / 2;
905 newaction = A_NOACTION;
915 * See if any signals need processing.
921 quit(QUIT_SAVED_STATUS);
925 * See if window size changed, for systems that don't
931 * Display prompt and accept a character.
937 if (newaction == A_NOACTION)
944 if (newaction != A_NOACTION)
947 newaction = A_NOACTION;
951 * If we are in a multicharacter command, call mca_char.
952 * Otherwise we call fcmd_decode to determine the
953 * action to be performed.
960 * Need another character.
966 * Command has been handled by mca_char.
967 * Start clean with a prompt.
972 * Not a multi-char command
973 * (at least, not anymore).
979 * Decode the command character and decide what to do.
984 * We're in a multichar command.
985 * Add the character to the command buffer
986 * and display it on the screen.
987 * If the user backspaces past the start
988 * of the line, abort the command.
990 if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0)
996 * Don't use cmd_char if we're starting fresh
997 * at the beginning of a command, because we
998 * don't want to echo the command until we know
999 * it is a multichar command. We also don't
1000 * want erase_char/kill_char to be treated
1001 * as line editing characters.
1008 action = fcmd_decode(cbuf, &extra);
1010 * If an "extra" string was returned,
1011 * process it as a string of command characters.
1017 * Clear the cmdbuf string.
1018 * (But not if we're in the prefix of a command,
1019 * because the partial command string is kept there.)
1021 if (action != A_PREFIX)
1028 * First digit of a number.
1030 start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE);
1035 * Forward one window (and set the window size).
1038 swindow = (int) number;
1042 * Forward one screen.
1045 number = get_swindow();
1048 set_attnpos(bottompos);
1049 forward((int) number, 0, 1);
1054 * Backward one window (and set the window size).
1057 swindow = (int) number;
1061 * Backward one screen.
1064 number = get_swindow();
1066 backward((int) number, 0, 1);
1071 * Forward N (default 1) line.
1076 if (show_attn == OPT_ONPLUS && number > 1)
1077 set_attnpos(bottompos);
1078 forward((int) number, 0, 0);
1083 * Backward N (default 1) line.
1088 backward((int) number, 0, 0);
1093 * Force forward N (default 1) line.
1098 if (show_attn == OPT_ONPLUS && number > 1)
1099 set_attnpos(bottompos);
1100 forward((int) number, 1, 0);
1105 * Force backward N (default 1) line.
1110 backward((int) number, 1, 0);
1115 * Force forward one screen.
1118 number = get_swindow();
1120 if (show_attn == OPT_ONPLUS)
1121 set_attnpos(bottompos);
1122 forward((int) number, 1, 0);
1127 * Forward forever, ignoring EOF.
1129 if (ch_getflags() & CH_HELPFILE)
1142 * This gets us back in "F mode" after processing
1143 * a non-abort signal (e.g. window-change).
1145 if (sigs && !ABORT_SIGS())
1146 newaction = A_F_FOREVER;
1152 * (default same as last 'd' or 'u' command).
1155 wscroll = (int) number;
1157 if (show_attn == OPT_ONPLUS)
1158 set_attnpos(bottompos);
1159 forward(wscroll, 0, 0);
1165 * (default same as last 'd' or 'u' command).
1168 wscroll = (int) number;
1170 backward(wscroll, 0, 0);
1175 * Flush buffers, then repaint screen.
1176 * Don't flush the buffers on a pipe!
1190 * Go to line N, default beginning of file.
1200 * Go to a specified percentage into the file.
1213 jump_percent((int) number, fraction);
1218 * Go to line N, default end of file.
1229 * Go to a specified byte position in the file.
1234 jump_line_loc((POSITION) number, jump_sline);
1239 * Print file name, etc.
1241 if (ch_getflags() & CH_HELPFILE)
1244 parg.p_string = eq_message();
1250 * Print version number, without the "@(#)".
1260 if (curr_ifile != NULL_IFILE &&
1261 ch_getflags() & CH_HELPFILE)
1264 * Quit while viewing the help file
1265 * just means return to viewing the
1268 hshift = save_hshift;
1269 if (edit_prev(1) == 0)
1278 * Define abbreviation for a commonly used sequence below.
1280 #define DO_SEARCH() \
1281 if (number <= 0) number = 1; \
1284 multi_search((char *)NULL, (int) number);
1289 * Search forward for a pattern.
1290 * Get the first char of the pattern.
1292 search_type = SRCH_FORW;
1301 * Search backward for a pattern.
1302 * Get the first char of the pattern.
1304 search_type = SRCH_BACK;
1311 case A_AGAIN_SEARCH:
1313 * Repeat previous search.
1318 case A_T_AGAIN_SEARCH:
1320 * Repeat previous search, multiple files.
1322 search_type |= SRCH_PAST_EOF;
1326 case A_REVERSE_SEARCH:
1328 * Repeat previous search, in reverse direction.
1330 save_search_type = search_type;
1331 search_type = SRCH_REVERSE(search_type);
1333 search_type = save_search_type;
1336 case A_T_REVERSE_SEARCH:
1338 * Repeat previous search,
1339 * multiple files in reverse direction.
1341 save_search_type = search_type;
1342 search_type = SRCH_REVERSE(search_type);
1343 search_type |= SRCH_PAST_EOF;
1345 search_type = save_search_type;
1356 if (ch_getflags() & CH_HELPFILE)
1359 save_hshift = hshift;
1361 (void) edit(FAKE_HELPFILE);
1367 * Edit a new file. Get the filename.
1371 error("Command not available", NULL_PARG);
1374 start_mca(A_EXAMINE, "Examine: ", ml_examine, 0);
1378 error("Command not available", NULL_PARG);
1384 * Invoke an editor on the input file.
1389 error("Command not available", NULL_PARG);
1392 if (ch_getflags() & CH_HELPFILE)
1394 if (strcmp(get_filename(curr_ifile), "-") == 0)
1396 error("Cannot edit standard input", NULL_PARG);
1399 if (curr_altfilename != NULL)
1401 error("WARNING: This file was viewed via LESSOPEN",
1404 start_mca(A_SHELL, "!", ml_shell, 0);
1406 * Expand the editor prototype string
1407 * and pass it to the system to execute.
1408 * (Make sure the screen is displayed so the
1409 * expansion of "+%lm" works.)
1413 lsystem(pr_expand(editproto, 0), (char*)NULL);
1416 error("Command not available", NULL_PARG);
1422 * Examine next file.
1427 error("No next file", NULL_PARG);
1433 if (edit_next((int) number))
1435 if (get_quit_at_eof() && hit_eof &&
1436 !(ch_getflags() & CH_HELPFILE))
1438 parg.p_string = (number > 1) ? "(N-th) " : "";
1439 error("No %snext file", &parg);
1445 * Examine previous file.
1450 error("No previous file", NULL_PARG);
1456 if (edit_prev((int) number))
1458 parg.p_string = (number > 1) ? "(N-th) " : "";
1459 error("No %sprevious file", &parg);
1467 tagfile = nexttag((int) number);
1468 if (tagfile == NULL)
1470 error("No next tag", NULL_PARG);
1473 if (edit(tagfile) == 0)
1475 POSITION pos = tagsearch();
1476 if (pos != NULL_POSITION)
1477 jump_loc(pos, jump_sline);
1480 error("Command not available", NULL_PARG);
1488 tagfile = prevtag((int) number);
1489 if (tagfile == NULL)
1491 error("No previous tag", NULL_PARG);
1494 if (edit(tagfile) == 0)
1496 POSITION pos = tagsearch();
1497 if (pos != NULL_POSITION)
1498 jump_loc(pos, jump_sline);
1501 error("Command not available", NULL_PARG);
1507 * Examine a particular file.
1511 if (edit_index((int) number))
1512 error("No such file", NULL_PARG);
1516 if (ch_getflags() & CH_HELPFILE)
1518 old_ifile = curr_ifile;
1519 new_ifile = getoff_ifile(curr_ifile);
1520 if (new_ifile == NULL_IFILE)
1525 if (edit_ifile(new_ifile) != 0)
1527 reedit_ifile(old_ifile);
1530 del_ifile(old_ifile);
1534 optflag = OPT_TOGGLE;
1542 * Report a flag setting.
1544 optflag = OPT_NO_TOGGLE;
1552 * Set an initial command for new files.
1554 start_mca(A_FIRSTCMD, "+", (void*)NULL, 0);
1565 error("Command not available", NULL_PARG);
1568 start_mca(A_SHELL, "!", ml_shell, 0);
1572 error("Command not available", NULL_PARG);
1580 if (ch_getflags() & CH_HELPFILE)
1582 start_mca(A_SETMARK, "mark: ", (void*)NULL, 0);
1584 if (c == erase_char || c == erase2_char ||
1585 c == kill_char || c == '\n' || c == '\r')
1594 start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0);
1596 if (c == erase_char || c == erase2_char ||
1597 c == kill_char || c == '\n' || c == '\r')
1607 error("Command not available", NULL_PARG);
1610 start_mca(A_PIPE, "|mark: ", (void*)NULL, 0);
1612 if (c == erase_char || c == erase2_char || c == kill_char)
1614 if (c == '\n' || c == '\r')
1619 start_mca(A_PIPE, "!", ml_shell, 0);
1623 error("Command not available", NULL_PARG);
1629 start_mca(action, "Brackets: ", (void*)NULL, 0);
1635 shift_count = number;
1637 number = (shift_count > 0) ?
1638 shift_count : sc_width / 2;
1639 if (number > hshift)
1647 shift_count = number;
1649 number = (shift_count > 0) ?
1650 shift_count : sc_width / 2;
1657 * The command is incomplete (more chars are needed).
1658 * Display the current char, so the user knows
1659 * what's going on, and get another character.
1661 if (mca != A_PREFIX)
1664 start_mca(A_PREFIX, " ", (void*)NULL,