3 * Copyright (C) 1984-2002 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, kill_char;
27 extern int quit_at_eof;
28 extern int quit_if_one_screen;
34 extern int jump_sline;
37 extern int top_scroll;
38 extern int ignore_eoi;
43 extern char *every_first_cmd;
44 extern char *curr_altfilename;
45 extern char version[];
46 extern struct scrpos initial_scrpos;
47 extern IFILE curr_ifile;
48 extern void constant *ml_search;
49 extern void constant *ml_examine;
50 #if SHELL_ESCAPE || PIPEC
51 extern void constant *ml_shell;
55 extern char *editproto;
57 extern int screen_trashed; /* The screen has been overwritten */
58 extern int shift_count;
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 */
70 static int optgetname;
71 static POSITION bottompos;
76 static void multi_search();
79 * Move the cursor to lower left before executing a command.
80 * This looks nicer if the command takes a long time before
81 * updating the screen.
92 * Set up the display to start a new multi-character command.
95 start_mca(action, prompt, mlist, cmdflags)
104 set_mlist(mlist, cmdflags);
110 return (mca != 0 && mca != A_PREFIX);
114 * Set up the display to start a new search command.
119 if (search_type & SRCH_FORW)
126 if (search_type & SRCH_NO_MATCH)
127 cmd_putstr("Non-match ");
128 if (search_type & SRCH_FIRST_FILE)
129 cmd_putstr("First-file ");
130 if (search_type & SRCH_PAST_EOF)
131 cmd_putstr("EOF-ignore ");
132 if (search_type & SRCH_NO_MOVE)
133 cmd_putstr("Keep-pos ");
134 if (search_type & SRCH_NO_REGEX)
135 cmd_putstr("Regex-off ");
137 if (search_type & SRCH_FORW)
141 set_mlist(ml_search, 0);
145 * Set up the display to start a new toggle-option command.
154 no_prompt = (optflag & OPT_NO_PROMPT);
155 flag = (optflag & ~OPT_NO_PROMPT);
156 dash = (flag == OPT_NO_TOGGLE) ? "_" : "-";
178 * Execute a multicharacter command.
192 multi_search(cbuf, (int) number);
196 * Skip leading spaces or + signs in the string.
198 while (*cbuf == '+' || *cbuf == ' ')
200 if (every_first_cmd != NULL)
201 free(every_first_cmd);
203 every_first_cmd = NULL;
205 every_first_cmd = save(cbuf);
208 toggle_option(optchar, cbuf, optflag);
212 match_brac(cbuf[0], cbuf[1], 1, (int) number);
215 match_brac(cbuf[1], cbuf[0], 0, (int) number);
223 /* If tag structure is loaded then clean it up. */
231 * !! just uses whatever is in shellcmd.
232 * Otherwise, copy cmdbuf to shellcmd,
233 * expanding any special characters ("%" or "#").
237 if (shellcmd != NULL)
239 shellcmd = fexpand(cbuf);
244 if (shellcmd == NULL)
245 lsystem("", "!done");
247 lsystem(shellcmd, "!done");
254 (void) pipe_mark(pipec, cbuf);
255 error("|done", NULL_PARG);
262 * Add a character to a multi-character command.
277 * Not in a multicharacter command.
283 * In the prefix of a command.
284 * This not considered a multichar command
285 * (even tho it uses cmdbuf, etc.).
286 * It is handled in the commands() switch.
292 * Entering digits of a number.
293 * Terminated by a non-digit.
295 if ((c < '0' || c > '9') &&
296 editchar(c, EC_PEEK|EC_NOHISTORY|EC_NOCOMPLETE|EC_NORIGHTLEFT) == A_INVALID)
299 * Not part of the number.
300 * Treat as a normal command character.
311 * Special case for the TOGGLE_OPTION command.
312 * If the option letter which was entered is a
313 * single-char option, execute the command immediately,
314 * so user doesn't have to hit RETURN.
315 * If the first char is + or -, this indicates
316 * OPT_UNSET or OPT_SET respectively, instead of OPT_TOGGLE.
317 * "--" begins inputting a long option name.
319 if (optchar == '\0' && len_cmdbuf() == 0)
321 flag = (optflag & ~OPT_NO_PROMPT);
322 if (flag == OPT_NO_TOGGLE)
327 /* "__" = long option name. */
338 optflag = (flag == OPT_UNSET) ?
339 OPT_TOGGLE : OPT_UNSET;
344 optflag = (flag == OPT_SET) ?
345 OPT_TOGGLE : OPT_SET;
349 optflag ^= OPT_NO_PROMPT;
353 /* "--" = long option name. */
363 * We're getting a long option name.
364 * See if we've matched an option name yet.
365 * If so, display the complete name and stop
366 * accepting chars until user hits RETURN.
372 if (c == '\n' || c == '\r')
375 * When the user hits RETURN, make sure
376 * we've matched an option name, then
377 * pretend he just entered the equivalent
382 parg.p_string = get_cmdbuf();
383 error("There is no --%s option", &parg);
394 * Already have a match for the name.
395 * Don't accept anything but erase/kill.
397 if (c == erase_char || c == kill_char)
402 * Add char to cmd buffer and try to match
405 if (cmd_char(c) == CC_QUIT)
409 o = findopt_name(&p, &oname, NULL);
414 * Remember the option letter and
415 * display the full option name.
417 optchar = o->oletter;
418 if (!lc && islower(optchar))
419 optchar = toupper(optchar);
422 for (p = oname; *p != '\0'; p++)
425 if (!lc && islower(c))
427 if (cmd_char(c) != CC_OK)
435 if (c == erase_char || c == kill_char)
438 /* We already have the option letter. */
443 if ((optflag & ~OPT_NO_PROMPT) != OPT_TOGGLE ||
444 single_char_option(c))
446 toggle_option(c, "", optflag);
450 * Display a prompt appropriate for the option letter.
452 if ((p = opt_prompt(c)) == NULL)
459 start_mca(A_OPT_TOGGLE, p, (void*)NULL, 0);
465 * Special case for search commands.
466 * Certain characters as the first char of
467 * the pattern have special meaning:
468 * ! Toggle the NO_MATCH flag
469 * * Toggle the PAST_EOF flag
470 * @ Toggle the FIRST_FILE flag
472 if (len_cmdbuf() > 0)
474 * Only works for the first char of the pattern.
484 case CONTROL('E'): /* ignore END of file */
485 flag = SRCH_PAST_EOF;
490 case CONTROL('F'): /* FIRST file */
491 flag = SRCH_FIRST_FILE;
493 case CONTROL('K'): /* KEEP position */
496 case CONTROL('R'): /* Don't use REGULAR EXPRESSIONS */
497 flag = SRCH_NO_REGEX;
499 case CONTROL('N'): /* NOT match */
501 flag = SRCH_NO_MATCH;
514 * Any other multicharacter command
515 * is terminated by a newline.
517 if (c == '\n' || c == '\r')
520 * Execute the command.
527 * Append the char to the command buffer.
529 if (cmd_char(c) == CC_QUIT)
531 * Abort the multi-char command.
535 if ((mca == A_F_BRACKET || mca == A_B_BRACKET) && len_cmdbuf() >= 2)
538 * Special case for the bracket-matching commands.
539 * Execute the command after getting exactly two
540 * characters from the user.
547 * Need another character.
553 * Make sure the screen is displayed.
559 * If nothing is displayed yet, display starting from initial_scrpos.
563 if (initial_scrpos.pos == NULL_POSITION)
565 * {{ Maybe this should be:
566 * jump_loc(ch_zero(), jump_sline);
567 * but this behavior seems rather unexpected
568 * on the first screen. }}
570 jump_loc(ch_zero(), 1);
572 jump_loc(initial_scrpos.pos, initial_scrpos.ln);
573 } else if (screen_trashed)
576 save_top_scroll = top_scroll;
579 top_scroll = save_top_scroll;
584 * Display the appropriate prompt.
591 if (ungotp != NULL && ungotp > ungot)
594 * No prompt necessary if commands are from
595 * ungotten chars rather than from the user.
601 * Make sure the screen is displayed.
604 bottompos = position(BOTTOM_PLUS_ONE);
607 * If the -E flag is set and we've hit EOF on the last file, quit.
609 if ((quit_at_eof == OPT_ONPLUS || quit_if_one_screen) &&
610 hit_eof && !(ch_getflags() & CH_HELPFILE) &&
611 next_ifile(curr_ifile) == NULL_IFILE)
613 quit_if_one_screen = FALSE;
614 #if 0 /* This doesn't work well because some "te"s clear the screen. */
616 * If the -e flag is set and we've hit EOF on the last file,
617 * and the file is squished (shorter than the screen), quit.
619 if (quit_at_eof && squished &&
620 next_ifile(curr_ifile) == NULL_IFILE)
624 #if MSDOS_COMPILER==WIN32C
626 * In Win32, display the file name in the window title.
628 if (!(ch_getflags() & CH_HELPFILE))
629 SetConsoleTitle(pr_expand("Less?f - %f.", 0));
632 * Select the proper prompt and display it.
647 * Display the less version message.
654 parg.p_string = version;
655 error("less %s", &parg);
659 * Get command character.
660 * The character normally comes from the keyboard,
661 * but may come from ungotten characters
662 * (characters previously given to ungetcc or ungetsc).
669 * Normal case: no ungotten chars, so get one from the user.
675 * Return the next ungotten char.
680 * We have just run out of ungotten chars.
683 if (len_cmdbuf() == 0 || !empty_screen())
686 * Command is incomplete, so try to complete it.
692 * We have a number but no command. Treat as #g.
699 * We have "/string" but no newline. Add the \n.
705 * Some other incomplete command. Let user complete it.
712 * "Unget" a command character.
713 * The next getcc() will return this character.
721 if (ungotp >= ungot + sizeof(ungot))
723 error("ungetcc overflow", NULL_PARG);
730 * Unget a whole string of command characters.
731 * The next sequence of getcc()'s will return this string.
739 for (p = s + strlen(s) - 1; p >= s; p--)
744 * Search for a pattern, possibly in multiple files.
745 * If SRCH_FIRST_FILE is set, begin searching at the first file.
746 * If SRCH_PAST_EOF is set, continue the search thru multiple files.
749 multi_search(pattern, n)
758 save_ifile = save_curr_ifile();
760 if (search_type & SRCH_FIRST_FILE)
763 * Start at the first (or last) file
764 * in the command line list.
766 if (search_type & SRCH_FORW)
767 nomore = edit_first();
769 nomore = edit_last();
772 unsave_ifile(save_ifile);
776 search_type &= ~SRCH_FIRST_FILE;
781 n = search(search_type, pattern, n);
783 * The SRCH_NO_MOVE flag doesn't "stick": it gets cleared
784 * after being used once. This allows "n" to work after
785 * using a /@@ search.
787 search_type &= ~SRCH_NO_MOVE;
793 unsave_ifile(save_ifile);
799 * Some kind of error in the search.
800 * Error message has been printed by search().
804 if ((search_type & SRCH_PAST_EOF) == 0)
806 * We didn't find a match, but we're
807 * supposed to search only one file.
811 * Move on to the next file.
813 if (search_type & SRCH_FORW)
814 nomore = edit_next(1);
816 nomore = edit_prev(1);
824 * Print an error message if we haven't already.
827 error("Pattern not found", NULL_PARG);
832 * Restore the file we were originally viewing.
834 reedit_ifile(save_ifile);
839 * Main command processor.
840 * Accept and execute commands until a quit command.
849 int save_search_type;
857 search_type = SRCH_FORW;
858 wscroll = (sc_height + 1) / 2;
859 newaction = A_NOACTION;
869 * See if any signals need processing.
875 quit(QUIT_SAVED_STATUS);
879 * See if window size changed, for systems that don't
885 * Display prompt and accept a character.
891 if (newaction == A_NOACTION)
898 if (newaction != A_NOACTION)
901 newaction = A_NOACTION;
905 * If we are in a multicharacter command, call mca_char.
906 * Otherwise we call fcmd_decode to determine the
907 * action to be performed.
914 * Need another character.
920 * Command has been handled by mca_char.
921 * Start clean with a prompt.
926 * Not a multi-char command
927 * (at least, not anymore).
933 * Decode the command character and decide what to do.
938 * We're in a multichar command.
939 * Add the character to the command buffer
940 * and display it on the screen.
941 * If the user backspaces past the start
942 * of the line, abort the command.
944 if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0)
950 * Don't use cmd_char if we're starting fresh
951 * at the beginning of a command, because we
952 * don't want to echo the command until we know
953 * it is a multichar command. We also don't
954 * want erase_char/kill_char to be treated
955 * as line editing characters.
962 action = fcmd_decode(cbuf, &extra);
964 * If an "extra" string was returned,
965 * process it as a string of command characters.
971 * Clear the cmdbuf string.
972 * (But not if we're in the prefix of a command,
973 * because the partial command string is kept there.)
975 if (action != A_PREFIX)
982 * First digit of a number.
984 start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE);
989 * Forward one window (and set the window size).
992 swindow = (int) number;
996 * Forward one screen.
999 number = get_swindow();
1002 set_attnpos(bottompos);
1003 forward((int) number, 0, 1);
1008 * Backward one window (and set the window size).
1011 swindow = (int) number;
1015 * Backward one screen.
1018 number = get_swindow();
1020 backward((int) number, 0, 1);
1025 * Forward N (default 1) line.
1030 if (show_attn == OPT_ONPLUS && number > 1)
1031 set_attnpos(bottompos);
1032 forward((int) number, 0, 0);
1037 * Backward N (default 1) line.
1042 backward((int) number, 0, 0);
1047 * Force forward N (default 1) line.
1052 if (show_attn == OPT_ONPLUS && number > 1)
1053 set_attnpos(bottompos);
1054 forward((int) number, 1, 0);
1059 * Force backward N (default 1) line.
1064 backward((int) number, 1, 0);
1069 * Force forward one screen.
1072 number = get_swindow();
1074 if (show_attn == OPT_ONPLUS)
1075 set_attnpos(bottompos);
1076 forward((int) number, 1, 0);
1081 * Forward forever, ignoring EOF.
1083 if (ch_getflags() & CH_HELPFILE)
1093 * This gets us back in "F mode" after processing
1094 * a non-abort signal (e.g. window-change).
1096 if (sigs && !ABORT_SIGS())
1097 newaction = A_F_FOREVER;
1103 * (default same as last 'd' or 'u' command).
1106 wscroll = (int) number;
1108 if (show_attn == OPT_ONPLUS)
1109 set_attnpos(bottompos);
1110 forward(wscroll, 0, 0);
1116 * (default same as last 'd' or 'u' command).
1119 wscroll = (int) number;
1121 backward(wscroll, 0, 0);
1126 * Flush buffers, then repaint screen.
1127 * Don't flush the buffers on a pipe!
1129 if (ch_getflags() & CH_CANSEEK)
1148 * Go to line N, default beginning of file.
1158 * Go to a specified percentage into the file.
1165 jump_percent((int) number);
1170 * Go to line N, default end of file.
1181 * Go to a specified byte position in the file.
1186 jump_line_loc((POSITION) number, jump_sline);
1191 * Print file name, etc.
1193 if (ch_getflags() & CH_HELPFILE)
1196 parg.p_string = eq_message();
1202 * Print version number, without the "@(#)".
1212 if (curr_ifile != NULL_IFILE &&
1213 ch_getflags() & CH_HELPFILE)
1216 * Quit while viewing the help file
1217 * just means return to viewing the
1220 if (edit_prev(1) == 0)
1229 * Define abbreviation for a commonly used sequence below.
1231 #define DO_SEARCH() if (number <= 0) number = 1; \
1234 multi_search((char *)NULL, (int) number);
1239 * Search forward for a pattern.
1240 * Get the first char of the pattern.
1242 search_type = SRCH_FORW;
1251 * Search backward for a pattern.
1252 * Get the first char of the pattern.
1254 search_type = SRCH_BACK;
1261 case A_AGAIN_SEARCH:
1263 * Repeat previous search.
1268 case A_T_AGAIN_SEARCH:
1270 * Repeat previous search, multiple files.
1272 search_type |= SRCH_PAST_EOF;
1276 case A_REVERSE_SEARCH:
1278 * Repeat previous search, in reverse direction.
1280 save_search_type = search_type;
1281 search_type = SRCH_REVERSE(search_type);
1283 search_type = save_search_type;
1286 case A_T_REVERSE_SEARCH:
1288 * Repeat previous search,
1289 * multiple files in reverse direction.
1291 save_search_type = search_type;
1292 search_type = SRCH_REVERSE(search_type);
1293 search_type |= SRCH_PAST_EOF;
1295 search_type = save_search_type;
1306 if (ch_getflags() & CH_HELPFILE)
1309 (void) edit(FAKE_HELPFILE);
1315 * Edit a new file. Get the filename.
1319 error("Command not available", NULL_PARG);
1322 start_mca(A_EXAMINE, "Examine: ", ml_examine, 0);
1326 error("Command not available", NULL_PARG);
1332 * Invoke an editor on the input file.
1337 error("Command not available", NULL_PARG);
1340 if (ch_getflags() & CH_HELPFILE)
1342 if (strcmp(get_filename(curr_ifile), "-") == 0)
1344 error("Cannot edit standard input", NULL_PARG);
1347 if (curr_altfilename != NULL)
1349 error("Cannot edit file processed with LESSOPEN",
1353 start_mca(A_SHELL, "!", ml_shell, 0);
1355 * Expand the editor prototype string
1356 * and pass it to the system to execute.
1357 * (Make sure the screen is displayed so the
1358 * expansion of "+%lm" works.)
1362 lsystem(pr_expand(editproto, 0), (char*)NULL);
1365 error("Command not available", NULL_PARG);
1371 * Examine next file.
1376 error("No next file", NULL_PARG);
1382 if (edit_next((int) number))
1384 if (quit_at_eof && hit_eof &&
1385 !(ch_getflags() & CH_HELPFILE))
1387 parg.p_string = (number > 1) ? "(N-th) " : "";
1388 error("No %snext file", &parg);
1394 * Examine previous file.
1399 error("No previous file", NULL_PARG);
1405 if (edit_prev((int) number))
1407 parg.p_string = (number > 1) ? "(N-th) " : "";
1408 error("No %sprevious file", &parg);
1416 tagfile = nexttag((int) number);
1417 if (tagfile == NULL)
1419 error("No next tag", NULL_PARG);
1422 if (edit(tagfile) == 0)
1424 POSITION pos = tagsearch();
1425 if (pos != NULL_POSITION)
1426 jump_loc(pos, jump_sline);
1429 error("Command not available", NULL_PARG);
1437 tagfile = prevtag((int) number);
1438 if (tagfile == NULL)
1440 error("No previous tag", NULL_PARG);
1443 if (edit(tagfile) == 0)
1445 POSITION pos = tagsearch();
1446 if (pos != NULL_POSITION)
1447 jump_loc(pos, jump_sline);
1450 error("Command not available", NULL_PARG);
1456 * Examine a particular file.
1460 if (edit_index((int) number))
1461 error("No such file", NULL_PARG);
1465 if (ch_getflags() & CH_HELPFILE)
1467 old_ifile = curr_ifile;
1468 new_ifile = getoff_ifile(curr_ifile);
1469 if (new_ifile == NULL_IFILE)
1474 if (edit_ifile(new_ifile) != 0)
1476 reedit_ifile(old_ifile);
1479 del_ifile(old_ifile);
1483 optflag = OPT_TOGGLE;
1491 * Report a flag setting.
1493 optflag = OPT_NO_TOGGLE;
1501 * Set an initial command for new files.
1503 start_mca(A_FIRSTCMD, "+", (void*)NULL, 0);
1514 error("Command not available", NULL_PARG);
1517 start_mca(A_SHELL, "!", ml_shell, 0);
1521 error("Command not available", NULL_PARG);
1529 if (ch_getflags() & CH_HELPFILE)
1531 start_mca(A_SETMARK, "mark: ", (void*)NULL, 0);
1533 if (c == erase_char || c == kill_char ||
1534 c == '\n' || c == '\r')
1543 start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0);
1545 if (c == erase_char || c == kill_char ||
1546 c == '\n' || c == '\r')
1555 error("Command not available", NULL_PARG);
1558 start_mca(A_PIPE, "|mark: ", (void*)NULL, 0);
1560 if (c == erase_char || c == kill_char)
1562 if (c == '\n' || c == '\r')
1567 start_mca(A_PIPE, "!", ml_shell, 0);
1571 error("Command not available", NULL_PARG);
1577 start_mca(action, "Brackets: ", (void*)NULL, 0);
1583 shift_count = number;
1585 number = (shift_count > 0) ?
1586 shift_count : sc_width / 2;
1587 if (number > hshift)
1595 shift_count = number;
1597 number = (shift_count > 0) ?
1598 shift_count : sc_width / 2;
1605 * The command is incomplete (more chars are needed).
1606 * Display the current char, so the user knows
1607 * what's going on, and get another character.
1609 if (mca != A_PREFIX)
1612 start_mca(A_PREFIX, " ", (void*)NULL,