2 * Copyright (C) 1984-2005 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_at_eof;
27 extern int quit_if_one_screen;
33 extern int jump_sline;
36 extern int top_scroll;
37 extern int ignore_eoi;
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 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 */
68 static int optgetname;
69 static POSITION bottompos;
70 static int save_hshift;
75 static void multi_search();
78 * Move the cursor to lower left before executing a command.
79 * This looks nicer if the command takes a long time before
80 * updating the screen.
91 * Set up the display to start a new multi-character command.
94 start_mca(action, prompt, mlist, cmdflags)
103 set_mlist(mlist, cmdflags);
109 return (mca != 0 && mca != A_PREFIX);
113 * Set up the display to start a new search command.
118 if (search_type & SRCH_FORW)
125 if (search_type & SRCH_NO_MATCH)
126 cmd_putstr("Non-match ");
127 if (search_type & SRCH_FIRST_FILE)
128 cmd_putstr("First-file ");
129 if (search_type & SRCH_PAST_EOF)
130 cmd_putstr("EOF-ignore ");
131 if (search_type & SRCH_NO_MOVE)
132 cmd_putstr("Keep-pos ");
133 if (search_type & SRCH_NO_REGEX)
134 cmd_putstr("Regex-off ");
136 if (search_type & SRCH_FORW)
140 set_mlist(ml_search, 0);
144 * Set up the display to start a new toggle-option command.
153 no_prompt = (optflag & OPT_NO_PROMPT);
154 flag = (optflag & ~OPT_NO_PROMPT);
155 dash = (flag == OPT_NO_TOGGLE) ? "_" : "-";
177 * Execute a multicharacter command.
191 multi_search(cbuf, (int) number);
195 * Skip leading spaces or + signs in the string.
197 while (*cbuf == '+' || *cbuf == ' ')
199 if (every_first_cmd != NULL)
200 free(every_first_cmd);
202 every_first_cmd = NULL;
204 every_first_cmd = save(cbuf);
207 toggle_option(optchar, cbuf, optflag);
211 match_brac(cbuf[0], cbuf[1], 1, (int) number);
214 match_brac(cbuf[1], cbuf[0], 0, (int) number);
222 /* If tag structure is loaded then clean it up. */
230 * !! just uses whatever is in shellcmd.
231 * Otherwise, copy cmdbuf to shellcmd,
232 * expanding any special characters ("%" or "#").
236 if (shellcmd != NULL)
238 shellcmd = fexpand(cbuf);
243 if (shellcmd == NULL)
244 lsystem("", "!done");
246 lsystem(shellcmd, "!done");
253 (void) pipe_mark(pipec, cbuf);
254 error("|done", NULL_PARG);
261 * Add a character to a multi-character command.
276 * Not in a multicharacter command.
282 * In the prefix of a command.
283 * This not considered a multichar command
284 * (even tho it uses cmdbuf, etc.).
285 * It is handled in the commands() switch.
291 * Entering digits of a number.
292 * Terminated by a non-digit.
294 if ((c < '0' || c > '9') &&
295 editchar(c, EC_PEEK|EC_NOHISTORY|EC_NOCOMPLETE|EC_NORIGHTLEFT) == A_INVALID)
298 * Not part of the number.
299 * Treat as a normal command character.
310 * Special case for the TOGGLE_OPTION command.
311 * If the option letter which was entered is a
312 * single-char option, execute the command immediately,
313 * so user doesn't have to hit RETURN.
314 * If the first char is + or -, this indicates
315 * OPT_UNSET or OPT_SET respectively, instead of OPT_TOGGLE.
316 * "--" begins inputting a long option name.
318 if (optchar == '\0' && len_cmdbuf() == 0)
320 flag = (optflag & ~OPT_NO_PROMPT);
321 if (flag == OPT_NO_TOGGLE)
326 /* "__" = long option name. */
337 optflag = (flag == OPT_UNSET) ?
338 OPT_TOGGLE : OPT_UNSET;
343 optflag = (flag == OPT_SET) ?
344 OPT_TOGGLE : OPT_SET;
348 optflag ^= OPT_NO_PROMPT;
352 /* "--" = long option name. */
362 * We're getting a long option name.
363 * See if we've matched an option name yet.
364 * If so, display the complete name and stop
365 * accepting chars until user hits RETURN.
371 if (c == '\n' || c == '\r')
374 * When the user hits RETURN, make sure
375 * we've matched an option name, then
376 * pretend he just entered the equivalent
381 parg.p_string = get_cmdbuf();
382 error("There is no --%s option", &parg);
393 * Already have a match for the name.
394 * Don't accept anything but erase/kill.
396 if (c == erase_char ||
403 * Add char to cmd buffer and try to match
406 if (cmd_char(c) == CC_QUIT)
409 lc = ASCII_IS_LOWER(p[0]);
410 o = findopt_name(&p, &oname, NULL);
415 * Remember the option letter and
416 * display the full option name.
418 optchar = o->oletter;
419 if (!lc && ASCII_IS_LOWER(optchar))
420 optchar = ASCII_TO_UPPER(optchar);
423 for (p = oname; *p != '\0'; p++)
426 if (!lc && ASCII_IS_LOWER(c))
427 c = ASCII_TO_UPPER(c);
428 if (cmd_char(c) != CC_OK)
436 if (c == erase_char || c == erase2_char || c == kill_char)
439 /* We already have the option letter. */
444 if ((optflag & ~OPT_NO_PROMPT) != OPT_TOGGLE ||
445 single_char_option(c))
447 toggle_option(c, "", optflag);
451 * Display a prompt appropriate for the option letter.
453 if ((p = opt_prompt(c)) == NULL)
460 start_mca(A_OPT_TOGGLE, p, (void*)NULL, 0);
466 * Special case for search commands.
467 * Certain characters as the first char of
468 * the pattern have special meaning:
469 * ! Toggle the NO_MATCH flag
470 * * Toggle the PAST_EOF flag
471 * @ Toggle the FIRST_FILE flag
473 if (len_cmdbuf() > 0)
475 * Only works for the first char of the pattern.
482 case CONTROL('E'): /* ignore END of file */
484 flag = SRCH_PAST_EOF;
486 case CONTROL('F'): /* FIRST file */
488 flag = SRCH_FIRST_FILE;
490 case CONTROL('K'): /* KEEP position */
493 case CONTROL('R'): /* Don't use REGULAR EXPRESSIONS */
494 flag = SRCH_NO_REGEX;
496 case CONTROL('N'): /* NOT match */
498 flag = SRCH_NO_MATCH;
511 * Any other multicharacter command
512 * is terminated by a newline.
514 if (c == '\n' || c == '\r')
517 * Execute the command.
524 * Append the char to the command buffer.
526 if (cmd_char(c) == CC_QUIT)
528 * Abort the multi-char command.
532 if ((mca == A_F_BRACKET || mca == A_B_BRACKET) && len_cmdbuf() >= 2)
535 * Special case for the bracket-matching commands.
536 * Execute the command after getting exactly two
537 * characters from the user.
544 * Need another character.
550 * Make sure the screen is displayed.
556 * If nothing is displayed yet, display starting from initial_scrpos.
560 if (initial_scrpos.pos == NULL_POSITION)
562 * {{ Maybe this should be:
563 * jump_loc(ch_zero(), jump_sline);
564 * but this behavior seems rather unexpected
565 * on the first screen. }}
567 jump_loc(ch_zero(), 1);
569 jump_loc(initial_scrpos.pos, initial_scrpos.ln);
570 } else if (screen_trashed)
573 save_top_scroll = top_scroll;
576 top_scroll = save_top_scroll;
581 * Display the appropriate prompt.
588 if (ungotp != NULL && ungotp > ungot)
591 * No prompt necessary if commands are from
592 * ungotten chars rather than from the user.
598 * Make sure the screen is displayed.
601 bottompos = position(BOTTOM_PLUS_ONE);
604 * If we've hit EOF on the last file, and the -E flag is set
605 * (or -F is set and this is the first prompt), then quit.
606 * {{ Relying on "first prompt" to detect a single-screen file
607 * fails if +G is used, for example. }}
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.
636 if (p == NULL || *p == '\0')
640 at_enter(AT_STANDOUT);
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);
837 unsave_ifile(save_ifile);
842 * Main command processor.
843 * Accept and execute commands until a quit command.
852 int save_search_type;
860 search_type = SRCH_FORW;
861 wscroll = (sc_height + 1) / 2;
862 newaction = A_NOACTION;
872 * See if any signals need processing.
878 quit(QUIT_SAVED_STATUS);
882 * See if window size changed, for systems that don't
888 * Display prompt and accept a character.
894 if (newaction == A_NOACTION)
901 if (newaction != A_NOACTION)
904 newaction = A_NOACTION;
908 * If we are in a multicharacter command, call mca_char.
909 * Otherwise we call fcmd_decode to determine the
910 * action to be performed.
917 * Need another character.
923 * Command has been handled by mca_char.
924 * Start clean with a prompt.
929 * Not a multi-char command
930 * (at least, not anymore).
936 * Decode the command character and decide what to do.
941 * We're in a multichar command.
942 * Add the character to the command buffer
943 * and display it on the screen.
944 * If the user backspaces past the start
945 * of the line, abort the command.
947 if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0)
953 * Don't use cmd_char if we're starting fresh
954 * at the beginning of a command, because we
955 * don't want to echo the command until we know
956 * it is a multichar command. We also don't
957 * want erase_char/kill_char to be treated
958 * as line editing characters.
965 action = fcmd_decode(cbuf, &extra);
967 * If an "extra" string was returned,
968 * process it as a string of command characters.
974 * Clear the cmdbuf string.
975 * (But not if we're in the prefix of a command,
976 * because the partial command string is kept there.)
978 if (action != A_PREFIX)
985 * First digit of a number.
987 start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE);
992 * Forward one window (and set the window size).
995 swindow = (int) number;
999 * Forward one screen.
1002 number = get_swindow();
1005 set_attnpos(bottompos);
1006 forward((int) number, 0, 1);
1011 * Backward one window (and set the window size).
1014 swindow = (int) number;
1018 * Backward one screen.
1021 number = get_swindow();
1023 backward((int) number, 0, 1);
1028 * Forward N (default 1) line.
1033 if (show_attn == OPT_ONPLUS && number > 1)
1034 set_attnpos(bottompos);
1035 forward((int) number, 0, 0);
1040 * Backward N (default 1) line.
1045 backward((int) number, 0, 0);
1050 * Force forward N (default 1) line.
1055 if (show_attn == OPT_ONPLUS && number > 1)
1056 set_attnpos(bottompos);
1057 forward((int) number, 1, 0);
1062 * Force backward N (default 1) line.
1067 backward((int) number, 1, 0);
1072 * Force forward one screen.
1075 number = get_swindow();
1077 if (show_attn == OPT_ONPLUS)
1078 set_attnpos(bottompos);
1079 forward((int) number, 1, 0);
1084 * Forward forever, ignoring EOF.
1086 if (ch_getflags() & CH_HELPFILE)
1096 * This gets us back in "F mode" after processing
1097 * a non-abort signal (e.g. window-change).
1099 if (sigs && !ABORT_SIGS())
1100 newaction = A_F_FOREVER;
1106 * (default same as last 'd' or 'u' command).
1109 wscroll = (int) number;
1111 if (show_attn == OPT_ONPLUS)
1112 set_attnpos(bottompos);
1113 forward(wscroll, 0, 0);
1119 * (default same as last 'd' or 'u' command).
1122 wscroll = (int) number;
1124 backward(wscroll, 0, 0);
1129 * Flush buffers, then repaint screen.
1130 * Don't flush the buffers on a pipe!
1132 if (ch_getflags() & CH_CANSEEK)
1151 * Go to line N, default beginning of file.
1161 * Go to a specified percentage into the file.
1168 jump_percent((int) number);
1173 * Go to line N, default end of file.
1184 * Go to a specified byte position in the file.
1189 jump_line_loc((POSITION) number, jump_sline);
1194 * Print file name, etc.
1196 if (ch_getflags() & CH_HELPFILE)
1199 parg.p_string = eq_message();
1205 * Print version number, without the "@(#)".
1215 if (curr_ifile != NULL_IFILE &&
1216 ch_getflags() & CH_HELPFILE)
1219 * Quit while viewing the help file
1220 * just means return to viewing the
1223 hshift = save_hshift;
1224 if (edit_prev(1) == 0)
1233 * Define abbreviation for a commonly used sequence below.
1235 #define DO_SEARCH() if (number <= 0) number = 1; \
1238 multi_search((char *)NULL, (int) number);
1243 * Search forward for a pattern.
1244 * Get the first char of the pattern.
1246 search_type = SRCH_FORW;
1255 * Search backward for a pattern.
1256 * Get the first char of the pattern.
1258 search_type = SRCH_BACK;
1265 case A_AGAIN_SEARCH:
1267 * Repeat previous search.
1272 case A_T_AGAIN_SEARCH:
1274 * Repeat previous search, multiple files.
1276 search_type |= SRCH_PAST_EOF;
1280 case A_REVERSE_SEARCH:
1282 * Repeat previous search, in reverse direction.
1284 save_search_type = search_type;
1285 search_type = SRCH_REVERSE(search_type);
1287 search_type = save_search_type;
1290 case A_T_REVERSE_SEARCH:
1292 * Repeat previous search,
1293 * multiple files in reverse direction.
1295 save_search_type = search_type;
1296 search_type = SRCH_REVERSE(search_type);
1297 search_type |= SRCH_PAST_EOF;
1299 search_type = save_search_type;
1310 if (ch_getflags() & CH_HELPFILE)
1313 save_hshift = hshift;
1315 (void) edit(FAKE_HELPFILE);
1321 * Edit a new file. Get the filename.
1325 error("Command not available", NULL_PARG);
1328 start_mca(A_EXAMINE, "Examine: ", ml_examine, 0);
1332 error("Command not available", NULL_PARG);
1338 * Invoke an editor on the input file.
1343 error("Command not available", NULL_PARG);
1346 if (ch_getflags() & CH_HELPFILE)
1348 if (strcmp(get_filename(curr_ifile), "-") == 0)
1350 error("Cannot edit standard input", NULL_PARG);
1353 if (curr_altfilename != NULL)
1355 error("WARNING: This file was viewed via LESSOPEN",
1358 start_mca(A_SHELL, "!", ml_shell, 0);
1360 * Expand the editor prototype string
1361 * and pass it to the system to execute.
1362 * (Make sure the screen is displayed so the
1363 * expansion of "+%lm" works.)
1367 lsystem(pr_expand(editproto, 0), (char*)NULL);
1370 error("Command not available", NULL_PARG);
1376 * Examine next file.
1381 error("No next file", NULL_PARG);
1387 if (edit_next((int) number))
1389 if (quit_at_eof && hit_eof &&
1390 !(ch_getflags() & CH_HELPFILE))
1392 parg.p_string = (number > 1) ? "(N-th) " : "";
1393 error("No %snext file", &parg);
1399 * Examine previous file.
1404 error("No previous file", NULL_PARG);
1410 if (edit_prev((int) number))
1412 parg.p_string = (number > 1) ? "(N-th) " : "";
1413 error("No %sprevious file", &parg);
1421 tagfile = nexttag((int) number);
1422 if (tagfile == NULL)
1424 error("No next tag", NULL_PARG);
1427 if (edit(tagfile) == 0)
1429 POSITION pos = tagsearch();
1430 if (pos != NULL_POSITION)
1431 jump_loc(pos, jump_sline);
1434 error("Command not available", NULL_PARG);
1442 tagfile = prevtag((int) number);
1443 if (tagfile == NULL)
1445 error("No previous tag", NULL_PARG);
1448 if (edit(tagfile) == 0)
1450 POSITION pos = tagsearch();
1451 if (pos != NULL_POSITION)
1452 jump_loc(pos, jump_sline);
1455 error("Command not available", NULL_PARG);
1461 * Examine a particular file.
1465 if (edit_index((int) number))
1466 error("No such file", NULL_PARG);
1470 if (ch_getflags() & CH_HELPFILE)
1472 old_ifile = curr_ifile;
1473 new_ifile = getoff_ifile(curr_ifile);
1474 if (new_ifile == NULL_IFILE)
1479 if (edit_ifile(new_ifile) != 0)
1481 reedit_ifile(old_ifile);
1484 del_ifile(old_ifile);
1488 optflag = OPT_TOGGLE;
1496 * Report a flag setting.
1498 optflag = OPT_NO_TOGGLE;
1506 * Set an initial command for new files.
1508 start_mca(A_FIRSTCMD, "+", (void*)NULL, 0);
1519 error("Command not available", NULL_PARG);
1522 start_mca(A_SHELL, "!", ml_shell, 0);
1526 error("Command not available", NULL_PARG);
1534 if (ch_getflags() & CH_HELPFILE)
1536 start_mca(A_SETMARK, "mark: ", (void*)NULL, 0);
1538 if (c == erase_char || c == erase2_char ||
1539 c == kill_char || c == '\n' || c == '\r')
1548 start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0);
1550 if (c == erase_char || c == erase2_char ||
1551 c == kill_char || c == '\n' || c == '\r')
1560 error("Command not available", NULL_PARG);
1563 start_mca(A_PIPE, "|mark: ", (void*)NULL, 0);
1565 if (c == erase_char || c == erase2_char || c == kill_char)
1567 if (c == '\n' || c == '\r')
1572 start_mca(A_PIPE, "!", ml_shell, 0);
1576 error("Command not available", NULL_PARG);
1582 start_mca(action, "Brackets: ", (void*)NULL, 0);
1588 shift_count = number;
1590 number = (shift_count > 0) ?
1591 shift_count : sc_width / 2;
1592 if (number > hshift)
1600 shift_count = number;
1602 number = (shift_count > 0) ?
1603 shift_count : sc_width / 2;
1610 * The command is incomplete (more chars are needed).
1611 * Display the current char, so the user knows
1612 * what's going on, and get another character.
1614 if (mca != A_PREFIX)
1617 start_mca(A_PREFIX, " ", (void*)NULL,