3 * Copyright (C) 1984-2005 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_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;
72 static int save_hshift;
77 static void multi_search();
80 * Move the cursor to lower left 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)
105 set_mlist(mlist, cmdflags);
111 return (mca != 0 && mca != A_PREFIX);
115 * Set up the display to start a new search command.
120 if (search_type & SRCH_FORW)
127 if (search_type & SRCH_NO_MATCH)
128 cmd_putstr("Non-match ");
129 if (search_type & SRCH_FIRST_FILE)
130 cmd_putstr("First-file ");
131 if (search_type & SRCH_PAST_EOF)
132 cmd_putstr("EOF-ignore ");
133 if (search_type & SRCH_NO_MOVE)
134 cmd_putstr("Keep-pos ");
135 if (search_type & SRCH_NO_REGEX)
136 cmd_putstr("Regex-off ");
138 if (search_type & SRCH_FORW)
142 set_mlist(ml_search, 0);
146 * Set up the display to start a new toggle-option command.
155 no_prompt = (optflag & OPT_NO_PROMPT);
156 flag = (optflag & ~OPT_NO_PROMPT);
157 dash = (flag == OPT_NO_TOGGLE) ? "_" : "-";
179 * Execute a multicharacter command.
193 multi_search(cbuf, (int) number);
197 * Skip leading spaces or + signs in the string.
199 while (*cbuf == '+' || *cbuf == ' ')
201 if (every_first_cmd != NULL)
202 free(every_first_cmd);
204 every_first_cmd = NULL;
206 every_first_cmd = save(cbuf);
209 toggle_option(optchar, cbuf, optflag);
213 match_brac(cbuf[0], cbuf[1], 1, (int) number);
216 match_brac(cbuf[1], cbuf[0], 0, (int) number);
224 /* If tag structure is loaded then clean it up. */
232 * !! just uses whatever is in shellcmd.
233 * Otherwise, copy cmdbuf to shellcmd,
234 * expanding any special characters ("%" or "#").
238 if (shellcmd != NULL)
240 shellcmd = fexpand(cbuf);
245 if (shellcmd == NULL)
246 lsystem("", "!done");
248 lsystem(shellcmd, "!done");
255 (void) pipe_mark(pipec, cbuf);
256 error("|done", NULL_PARG);
263 * Add a character to a multi-character command.
278 * Not in a multicharacter command.
284 * In the prefix of a command.
285 * This not considered a multichar command
286 * (even tho it uses cmdbuf, etc.).
287 * It is handled in the commands() switch.
293 * Entering digits of a number.
294 * Terminated by a non-digit.
296 if ((c < '0' || c > '9') &&
297 editchar(c, EC_PEEK|EC_NOHISTORY|EC_NOCOMPLETE|EC_NORIGHTLEFT) == A_INVALID)
300 * Not part of the number.
301 * Treat as a normal command character.
312 * Special case for the TOGGLE_OPTION command.
313 * If the option letter which was entered is a
314 * single-char option, execute the command immediately,
315 * so user doesn't have to hit RETURN.
316 * If the first char is + or -, this indicates
317 * OPT_UNSET or OPT_SET respectively, instead of OPT_TOGGLE.
318 * "--" begins inputting a long option name.
320 if (optchar == '\0' && len_cmdbuf() == 0)
322 flag = (optflag & ~OPT_NO_PROMPT);
323 if (flag == OPT_NO_TOGGLE)
328 /* "__" = long option name. */
339 optflag = (flag == OPT_UNSET) ?
340 OPT_TOGGLE : OPT_UNSET;
345 optflag = (flag == OPT_SET) ?
346 OPT_TOGGLE : OPT_SET;
350 optflag ^= OPT_NO_PROMPT;
354 /* "--" = long option name. */
364 * We're getting a long option name.
365 * See if we've matched an option name yet.
366 * If so, display the complete name and stop
367 * accepting chars until user hits RETURN.
373 if (c == '\n' || c == '\r')
376 * When the user hits RETURN, make sure
377 * we've matched an option name, then
378 * pretend he just entered the equivalent
383 parg.p_string = get_cmdbuf();
384 error("There is no --%s option", &parg);
395 * Already have a match for the name.
396 * Don't accept anything but erase/kill.
398 if (c == erase_char ||
405 * Add char to cmd buffer and try to match
408 if (cmd_char(c) == CC_QUIT)
411 lc = ASCII_IS_LOWER(p[0]);
412 o = findopt_name(&p, &oname, NULL);
417 * Remember the option letter and
418 * display the full option name.
420 optchar = o->oletter;
421 if (!lc && ASCII_IS_LOWER(optchar))
422 optchar = ASCII_TO_UPPER(optchar);
425 for (p = oname; *p != '\0'; p++)
428 if (!lc && ASCII_IS_LOWER(c))
429 c = ASCII_TO_UPPER(c);
430 if (cmd_char(c) != CC_OK)
438 if (c == erase_char || c == erase2_char || c == kill_char)
441 /* We already have the option letter. */
446 if ((optflag & ~OPT_NO_PROMPT) != OPT_TOGGLE ||
447 single_char_option(c))
449 toggle_option(c, "", optflag);
453 * Display a prompt appropriate for the option letter.
455 if ((p = opt_prompt(c)) == NULL)
462 start_mca(A_OPT_TOGGLE, p, (void*)NULL, 0);
468 * Special case for search commands.
469 * Certain characters as the first char of
470 * the pattern have special meaning:
471 * ! Toggle the NO_MATCH flag
472 * * Toggle the PAST_EOF flag
473 * @ Toggle the FIRST_FILE flag
475 if (len_cmdbuf() > 0)
477 * Only works for the first char of the pattern.
487 case CONTROL('E'): /* ignore END of file */
488 flag = SRCH_PAST_EOF;
493 case CONTROL('F'): /* FIRST file */
494 flag = SRCH_FIRST_FILE;
496 case CONTROL('K'): /* KEEP position */
499 case CONTROL('R'): /* Don't use REGULAR EXPRESSIONS */
500 flag = SRCH_NO_REGEX;
502 case CONTROL('N'): /* NOT match */
504 flag = SRCH_NO_MATCH;
517 * Any other multicharacter command
518 * is terminated by a newline.
520 if (c == '\n' || c == '\r')
523 * Execute the command.
530 * Append the char to the command buffer.
532 if (cmd_char(c) == CC_QUIT)
534 * Abort the multi-char command.
538 if ((mca == A_F_BRACKET || mca == A_B_BRACKET) && len_cmdbuf() >= 2)
541 * Special case for the bracket-matching commands.
542 * Execute the command after getting exactly two
543 * characters from the user.
550 * Need another character.
556 * Make sure the screen is displayed.
562 * If nothing is displayed yet, display starting from initial_scrpos.
566 if (initial_scrpos.pos == NULL_POSITION)
568 * {{ Maybe this should be:
569 * jump_loc(ch_zero(), jump_sline);
570 * but this behavior seems rather unexpected
571 * on the first screen. }}
573 jump_loc(ch_zero(), 1);
575 jump_loc(initial_scrpos.pos, initial_scrpos.ln);
576 } else if (screen_trashed)
579 save_top_scroll = top_scroll;
582 top_scroll = save_top_scroll;
587 * Display the appropriate prompt.
594 if (ungotp != NULL && ungotp > ungot)
597 * No prompt necessary if commands are from
598 * ungotten chars rather than from the user.
604 * Make sure the screen is displayed.
607 bottompos = position(BOTTOM_PLUS_ONE);
610 * If we've hit EOF on the last file, and the -E flag is set
611 * (or -F is set and this is the first prompt), then quit.
612 * {{ Relying on "first prompt" to detect a single-screen file
613 * fails if +G is used, for example. }}
615 if ((quit_at_eof == OPT_ONPLUS || quit_if_one_screen) &&
616 hit_eof && !(ch_getflags() & CH_HELPFILE) &&
617 next_ifile(curr_ifile) == NULL_IFILE)
619 quit_if_one_screen = FALSE;
620 #if 0 /* This doesn't work well because some "te"s clear the screen. */
622 * If the -e flag is set and we've hit EOF on the last file,
623 * and the file is squished (shorter than the screen), quit.
625 if (quit_at_eof && squished &&
626 next_ifile(curr_ifile) == NULL_IFILE)
630 #if MSDOS_COMPILER==WIN32C
632 * In Win32, display the file name in the window title.
634 if (!(ch_getflags() & CH_HELPFILE))
635 SetConsoleTitle(pr_expand("Less?f - %f.", 0));
638 * Select the proper prompt and display it.
642 if (p == NULL || *p == '\0')
646 at_enter(AT_STANDOUT);
653 * Display the less version message.
660 parg.p_string = version;
661 error("less %s", &parg);
665 * Get command character.
666 * The character normally comes from the keyboard,
667 * but may come from ungotten characters
668 * (characters previously given to ungetcc or ungetsc).
675 * Normal case: no ungotten chars, so get one from the user.
681 * Return the next ungotten char.
686 * We have just run out of ungotten chars.
689 if (len_cmdbuf() == 0 || !empty_screen())
692 * Command is incomplete, so try to complete it.
698 * We have a number but no command. Treat as #g.
705 * We have "/string" but no newline. Add the \n.
711 * Some other incomplete command. Let user complete it.
718 * "Unget" a command character.
719 * The next getcc() will return this character.
727 if (ungotp >= ungot + sizeof(ungot))
729 error("ungetcc overflow", NULL_PARG);
736 * Unget a whole string of command characters.
737 * The next sequence of getcc()'s will return this string.
745 for (p = s + strlen(s) - 1; p >= s; p--)
750 * Search for a pattern, possibly in multiple files.
751 * If SRCH_FIRST_FILE is set, begin searching at the first file.
752 * If SRCH_PAST_EOF is set, continue the search thru multiple files.
755 multi_search(pattern, n)
764 save_ifile = save_curr_ifile();
766 if (search_type & SRCH_FIRST_FILE)
769 * Start at the first (or last) file
770 * in the command line list.
772 if (search_type & SRCH_FORW)
773 nomore = edit_first();
775 nomore = edit_last();
778 unsave_ifile(save_ifile);
782 search_type &= ~SRCH_FIRST_FILE;
787 n = search(search_type, pattern, n);
789 * The SRCH_NO_MOVE flag doesn't "stick": it gets cleared
790 * after being used once. This allows "n" to work after
791 * using a /@@ search.
793 search_type &= ~SRCH_NO_MOVE;
799 unsave_ifile(save_ifile);
805 * Some kind of error in the search.
806 * Error message has been printed by search().
810 if ((search_type & SRCH_PAST_EOF) == 0)
812 * We didn't find a match, but we're
813 * supposed to search only one file.
817 * Move on to the next file.
819 if (search_type & SRCH_FORW)
820 nomore = edit_next(1);
822 nomore = edit_prev(1);
830 * Print an error message if we haven't already.
833 error("Pattern not found", NULL_PARG);
838 * Restore the file we were originally viewing.
840 reedit_ifile(save_ifile);
843 unsave_ifile(save_ifile);
848 * Main command processor.
849 * Accept and execute commands until a quit command.
858 int save_search_type;
866 search_type = SRCH_FORW;
867 wscroll = (sc_height + 1) / 2;
868 newaction = A_NOACTION;
878 * See if any signals need processing.
884 quit(QUIT_SAVED_STATUS);
888 * See if window size changed, for systems that don't
894 * Display prompt and accept a character.
900 if (newaction == A_NOACTION)
907 if (newaction != A_NOACTION)
910 newaction = A_NOACTION;
914 * If we are in a multicharacter command, call mca_char.
915 * Otherwise we call fcmd_decode to determine the
916 * action to be performed.
923 * Need another character.
929 * Command has been handled by mca_char.
930 * Start clean with a prompt.
935 * Not a multi-char command
936 * (at least, not anymore).
942 * Decode the command character and decide what to do.
947 * We're in a multichar command.
948 * Add the character to the command buffer
949 * and display it on the screen.
950 * If the user backspaces past the start
951 * of the line, abort the command.
953 if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0)
959 * Don't use cmd_char if we're starting fresh
960 * at the beginning of a command, because we
961 * don't want to echo the command until we know
962 * it is a multichar command. We also don't
963 * want erase_char/kill_char to be treated
964 * as line editing characters.
971 action = fcmd_decode(cbuf, &extra);
973 * If an "extra" string was returned,
974 * process it as a string of command characters.
980 * Clear the cmdbuf string.
981 * (But not if we're in the prefix of a command,
982 * because the partial command string is kept there.)
984 if (action != A_PREFIX)
991 * First digit of a number.
993 start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE);
998 * Forward one window (and set the window size).
1001 swindow = (int) number;
1005 * Forward one screen.
1008 number = get_swindow();
1011 set_attnpos(bottompos);
1012 forward((int) number, 0, 1);
1017 * Backward one window (and set the window size).
1020 swindow = (int) number;
1024 * Backward one screen.
1027 number = get_swindow();
1029 backward((int) number, 0, 1);
1034 * Forward N (default 1) line.
1039 if (show_attn == OPT_ONPLUS && number > 1)
1040 set_attnpos(bottompos);
1041 forward((int) number, 0, 0);
1046 * Backward N (default 1) line.
1051 backward((int) number, 0, 0);
1056 * Force forward N (default 1) line.
1061 if (show_attn == OPT_ONPLUS && number > 1)
1062 set_attnpos(bottompos);
1063 forward((int) number, 1, 0);
1068 * Force backward N (default 1) line.
1073 backward((int) number, 1, 0);
1078 * Force forward one screen.
1081 number = get_swindow();
1083 if (show_attn == OPT_ONPLUS)
1084 set_attnpos(bottompos);
1085 forward((int) number, 1, 0);
1090 * Forward forever, ignoring EOF.
1092 if (ch_getflags() & CH_HELPFILE)
1102 * This gets us back in "F mode" after processing
1103 * a non-abort signal (e.g. window-change).
1105 if (sigs && !ABORT_SIGS())
1106 newaction = A_F_FOREVER;
1112 * (default same as last 'd' or 'u' command).
1115 wscroll = (int) number;
1117 if (show_attn == OPT_ONPLUS)
1118 set_attnpos(bottompos);
1119 forward(wscroll, 0, 0);
1125 * (default same as last 'd' or 'u' command).
1128 wscroll = (int) number;
1130 backward(wscroll, 0, 0);
1135 * Flush buffers, then repaint screen.
1136 * Don't flush the buffers on a pipe!
1138 if (ch_getflags() & CH_CANSEEK)
1157 * Go to line N, default beginning of file.
1167 * Go to a specified percentage into the file.
1174 jump_percent((int) number);
1179 * Go to line N, default end of file.
1190 * Go to a specified byte position in the file.
1195 jump_line_loc((POSITION) number, jump_sline);
1200 * Print file name, etc.
1202 if (ch_getflags() & CH_HELPFILE)
1205 parg.p_string = eq_message();
1211 * Print version number, without the "@(#)".
1221 if (curr_ifile != NULL_IFILE &&
1222 ch_getflags() & CH_HELPFILE)
1225 * Quit while viewing the help file
1226 * just means return to viewing the
1229 hshift = save_hshift;
1230 if (edit_prev(1) == 0)
1239 * Define abbreviation for a commonly used sequence below.
1241 #define DO_SEARCH() if (number <= 0) number = 1; \
1244 multi_search((char *)NULL, (int) number);
1249 * Search forward for a pattern.
1250 * Get the first char of the pattern.
1252 search_type = SRCH_FORW;
1261 * Search backward for a pattern.
1262 * Get the first char of the pattern.
1264 search_type = SRCH_BACK;
1271 case A_AGAIN_SEARCH:
1273 * Repeat previous search.
1278 case A_T_AGAIN_SEARCH:
1280 * Repeat previous search, multiple files.
1282 search_type |= SRCH_PAST_EOF;
1286 case A_REVERSE_SEARCH:
1288 * Repeat previous search, in reverse direction.
1290 save_search_type = search_type;
1291 search_type = SRCH_REVERSE(search_type);
1293 search_type = save_search_type;
1296 case A_T_REVERSE_SEARCH:
1298 * Repeat previous search,
1299 * multiple files in reverse direction.
1301 save_search_type = search_type;
1302 search_type = SRCH_REVERSE(search_type);
1303 search_type |= SRCH_PAST_EOF;
1305 search_type = save_search_type;
1316 if (ch_getflags() & CH_HELPFILE)
1319 save_hshift = hshift;
1321 (void) edit(FAKE_HELPFILE);
1327 * Edit a new file. Get the filename.
1331 error("Command not available", NULL_PARG);
1334 start_mca(A_EXAMINE, "Examine: ", ml_examine, 0);
1338 error("Command not available", NULL_PARG);
1344 * Invoke an editor on the input file.
1349 error("Command not available", NULL_PARG);
1352 if (ch_getflags() & CH_HELPFILE)
1354 if (strcmp(get_filename(curr_ifile), "-") == 0)
1356 error("Cannot edit standard input", NULL_PARG);
1359 if (curr_altfilename != NULL)
1361 error("WARNING: This file was viewed via LESSOPEN",
1364 start_mca(A_SHELL, "!", ml_shell, 0);
1366 * Expand the editor prototype string
1367 * and pass it to the system to execute.
1368 * (Make sure the screen is displayed so the
1369 * expansion of "+%lm" works.)
1373 lsystem(pr_expand(editproto, 0), (char*)NULL);
1376 error("Command not available", NULL_PARG);
1382 * Examine next file.
1387 error("No next file", NULL_PARG);
1393 if (edit_next((int) number))
1395 if (quit_at_eof && hit_eof &&
1396 !(ch_getflags() & CH_HELPFILE))
1398 parg.p_string = (number > 1) ? "(N-th) " : "";
1399 error("No %snext file", &parg);
1405 * Examine previous file.
1410 error("No previous file", NULL_PARG);
1416 if (edit_prev((int) number))
1418 parg.p_string = (number > 1) ? "(N-th) " : "";
1419 error("No %sprevious file", &parg);
1427 tagfile = nexttag((int) number);
1428 if (tagfile == NULL)
1430 error("No next tag", NULL_PARG);
1433 if (edit(tagfile) == 0)
1435 POSITION pos = tagsearch();
1436 if (pos != NULL_POSITION)
1437 jump_loc(pos, jump_sline);
1440 error("Command not available", NULL_PARG);
1448 tagfile = prevtag((int) number);
1449 if (tagfile == NULL)
1451 error("No previous tag", NULL_PARG);
1454 if (edit(tagfile) == 0)
1456 POSITION pos = tagsearch();
1457 if (pos != NULL_POSITION)
1458 jump_loc(pos, jump_sline);
1461 error("Command not available", NULL_PARG);
1467 * Examine a particular file.
1471 if (edit_index((int) number))
1472 error("No such file", NULL_PARG);
1476 if (ch_getflags() & CH_HELPFILE)
1478 old_ifile = curr_ifile;
1479 new_ifile = getoff_ifile(curr_ifile);
1480 if (new_ifile == NULL_IFILE)
1485 if (edit_ifile(new_ifile) != 0)
1487 reedit_ifile(old_ifile);
1490 del_ifile(old_ifile);
1494 optflag = OPT_TOGGLE;
1502 * Report a flag setting.
1504 optflag = OPT_NO_TOGGLE;
1512 * Set an initial command for new files.
1514 start_mca(A_FIRSTCMD, "+", (void*)NULL, 0);
1525 error("Command not available", NULL_PARG);
1528 start_mca(A_SHELL, "!", ml_shell, 0);
1532 error("Command not available", NULL_PARG);
1540 if (ch_getflags() & CH_HELPFILE)
1542 start_mca(A_SETMARK, "mark: ", (void*)NULL, 0);
1544 if (c == erase_char || c == erase2_char ||
1545 c == kill_char || c == '\n' || c == '\r')
1554 start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0);
1556 if (c == erase_char || c == erase2_char ||
1557 c == kill_char || c == '\n' || c == '\r')
1566 error("Command not available", NULL_PARG);
1569 start_mca(A_PIPE, "|mark: ", (void*)NULL, 0);
1571 if (c == erase_char || c == erase2_char || c == kill_char)
1573 if (c == '\n' || c == '\r')
1578 start_mca(A_PIPE, "!", ml_shell, 0);
1582 error("Command not available", NULL_PARG);
1588 start_mca(action, "Brackets: ", (void*)NULL, 0);
1594 shift_count = number;
1596 number = (shift_count > 0) ?
1597 shift_count : sc_width / 2;
1598 if (number > hshift)
1606 shift_count = number;
1608 number = (shift_count > 0) ?
1609 shift_count : sc_width / 2;
1616 * The command is incomplete (more chars are needed).
1617 * Display the current char, so the user knows
1618 * what's going on, and get another character.
1620 if (mca != A_PREFIX)
1623 start_mca(A_PREFIX, " ", (void*)NULL,