2 * Copyright (C) 1984-2002 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, 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;
74 static void multi_search();
77 * Move the cursor to lower left before executing a command.
78 * This looks nicer if the command takes a long time before
79 * updating the screen.
90 * Set up the display to start a new multi-character command.
93 start_mca(action, prompt, mlist, cmdflags)
102 set_mlist(mlist, cmdflags);
108 return (mca != 0 && mca != A_PREFIX);
112 * Set up the display to start a new search command.
117 if (search_type & SRCH_FORW)
124 if (search_type & SRCH_NO_MATCH)
125 cmd_putstr("Non-match ");
126 if (search_type & SRCH_FIRST_FILE)
127 cmd_putstr("First-file ");
128 if (search_type & SRCH_PAST_EOF)
129 cmd_putstr("EOF-ignore ");
130 if (search_type & SRCH_NO_MOVE)
131 cmd_putstr("Keep-pos ");
132 if (search_type & SRCH_NO_REGEX)
133 cmd_putstr("Regex-off ");
135 if (search_type & SRCH_FORW)
139 set_mlist(ml_search, 0);
143 * Set up the display to start a new toggle-option command.
152 no_prompt = (optflag & OPT_NO_PROMPT);
153 flag = (optflag & ~OPT_NO_PROMPT);
154 dash = (flag == OPT_NO_TOGGLE) ? "_" : "-";
176 * Execute a multicharacter command.
190 multi_search(cbuf, (int) number);
194 * Skip leading spaces or + signs in the string.
196 while (*cbuf == '+' || *cbuf == ' ')
198 if (every_first_cmd != NULL)
199 free(every_first_cmd);
201 every_first_cmd = NULL;
203 every_first_cmd = save(cbuf);
206 toggle_option(optchar, cbuf, optflag);
210 match_brac(cbuf[0], cbuf[1], 1, (int) number);
213 match_brac(cbuf[1], cbuf[0], 0, (int) number);
221 /* If tag structure is loaded then clean it up. */
229 * !! just uses whatever is in shellcmd.
230 * Otherwise, copy cmdbuf to shellcmd,
231 * expanding any special characters ("%" or "#").
235 if (shellcmd != NULL)
237 shellcmd = fexpand(cbuf);
242 if (shellcmd == NULL)
243 lsystem("", "!done");
245 lsystem(shellcmd, "!done");
252 (void) pipe_mark(pipec, cbuf);
253 error("|done", NULL_PARG);
260 * Add a character to a multi-character command.
275 * Not in a multicharacter command.
281 * In the prefix of a command.
282 * This not considered a multichar command
283 * (even tho it uses cmdbuf, etc.).
284 * It is handled in the commands() switch.
290 * Entering digits of a number.
291 * Terminated by a non-digit.
293 if ((c < '0' || c > '9') &&
294 editchar(c, EC_PEEK|EC_NOHISTORY|EC_NOCOMPLETE|EC_NORIGHTLEFT) == A_INVALID)
297 * Not part of the number.
298 * Treat as a normal command character.
309 * Special case for the TOGGLE_OPTION command.
310 * If the option letter which was entered is a
311 * single-char option, execute the command immediately,
312 * so user doesn't have to hit RETURN.
313 * If the first char is + or -, this indicates
314 * OPT_UNSET or OPT_SET respectively, instead of OPT_TOGGLE.
315 * "--" begins inputting a long option name.
317 if (optchar == '\0' && len_cmdbuf() == 0)
319 flag = (optflag & ~OPT_NO_PROMPT);
320 if (flag == OPT_NO_TOGGLE)
325 /* "__" = long option name. */
336 optflag = (flag == OPT_UNSET) ?
337 OPT_TOGGLE : OPT_UNSET;
342 optflag = (flag == OPT_SET) ?
343 OPT_TOGGLE : OPT_SET;
347 optflag ^= OPT_NO_PROMPT;
351 /* "--" = long option name. */
361 * We're getting a long option name.
362 * See if we've matched an option name yet.
363 * If so, display the complete name and stop
364 * accepting chars until user hits RETURN.
370 if (c == '\n' || c == '\r')
373 * When the user hits RETURN, make sure
374 * we've matched an option name, then
375 * pretend he just entered the equivalent
380 parg.p_string = get_cmdbuf();
381 error("There is no --%s option", &parg);
392 * Already have a match for the name.
393 * Don't accept anything but erase/kill.
395 if (c == erase_char || c == kill_char)
400 * Add char to cmd buffer and try to match
403 if (cmd_char(c) == CC_QUIT)
407 o = findopt_name(&p, &oname, NULL);
412 * Remember the option letter and
413 * display the full option name.
415 optchar = o->oletter;
416 if (!lc && islower(optchar))
417 optchar = toupper(optchar);
420 for (p = oname; *p != '\0'; p++)
423 if (!lc && islower(c))
425 if (cmd_char(c) != CC_OK)
433 if (c == erase_char || c == kill_char)
436 /* We already have the option letter. */
441 if ((optflag & ~OPT_NO_PROMPT) != OPT_TOGGLE ||
442 single_char_option(c))
444 toggle_option(c, "", optflag);
448 * Display a prompt appropriate for the option letter.
450 if ((p = opt_prompt(c)) == NULL)
457 start_mca(A_OPT_TOGGLE, p, (void*)NULL, 0);
463 * Special case for search commands.
464 * Certain characters as the first char of
465 * the pattern have special meaning:
466 * ! Toggle the NO_MATCH flag
467 * * Toggle the PAST_EOF flag
468 * @ Toggle the FIRST_FILE flag
470 if (len_cmdbuf() > 0)
472 * Only works for the first char of the pattern.
479 case CONTROL('E'): /* ignore END of file */
481 flag = SRCH_PAST_EOF;
483 case CONTROL('F'): /* FIRST file */
485 flag = SRCH_FIRST_FILE;
487 case CONTROL('K'): /* KEEP position */
490 case CONTROL('R'): /* Don't use REGULAR EXPRESSIONS */
491 flag = SRCH_NO_REGEX;
493 case CONTROL('N'): /* NOT match */
495 flag = SRCH_NO_MATCH;
508 * Any other multicharacter command
509 * is terminated by a newline.
511 if (c == '\n' || c == '\r')
514 * Execute the command.
521 * Append the char to the command buffer.
523 if (cmd_char(c) == CC_QUIT)
525 * Abort the multi-char command.
529 if ((mca == A_F_BRACKET || mca == A_B_BRACKET) && len_cmdbuf() >= 2)
532 * Special case for the bracket-matching commands.
533 * Execute the command after getting exactly two
534 * characters from the user.
541 * Need another character.
547 * Make sure the screen is displayed.
553 * If nothing is displayed yet, display starting from initial_scrpos.
557 if (initial_scrpos.pos == NULL_POSITION)
559 * {{ Maybe this should be:
560 * jump_loc(ch_zero(), jump_sline);
561 * but this behavior seems rather unexpected
562 * on the first screen. }}
564 jump_loc(ch_zero(), 1);
566 jump_loc(initial_scrpos.pos, initial_scrpos.ln);
567 } else if (screen_trashed)
570 save_top_scroll = top_scroll;
573 top_scroll = save_top_scroll;
578 * Display the appropriate prompt.
585 if (ungotp != NULL && ungotp > ungot)
588 * No prompt necessary if commands are from
589 * ungotten chars rather than from the user.
595 * Make sure the screen is displayed.
598 bottompos = position(BOTTOM_PLUS_ONE);
601 * If the -E flag is set and we've hit EOF on the last file, quit.
603 if ((quit_at_eof == OPT_ONPLUS || quit_if_one_screen) &&
604 hit_eof && !(ch_getflags() & CH_HELPFILE) &&
605 next_ifile(curr_ifile) == NULL_IFILE)
607 quit_if_one_screen = FALSE;
608 #if 0 /* This doesn't work well because some "te"s clear the screen. */
610 * If the -e flag is set and we've hit EOF on the last file,
611 * and the file is squished (shorter than the screen), quit.
613 if (quit_at_eof && squished &&
614 next_ifile(curr_ifile) == NULL_IFILE)
618 #if MSDOS_COMPILER==WIN32C
620 * In Win32, display the file name in the window title.
622 if (!(ch_getflags() & CH_HELPFILE))
623 SetConsoleTitle(pr_expand("Less?f - %f.", 0));
626 * Select the proper prompt and display it.
641 * Display the less version message.
648 parg.p_string = version;
649 error("less %s", &parg);
653 * Get command character.
654 * The character normally comes from the keyboard,
655 * but may come from ungotten characters
656 * (characters previously given to ungetcc or ungetsc).
663 * Normal case: no ungotten chars, so get one from the user.
669 * Return the next ungotten char.
674 * We have just run out of ungotten chars.
677 if (len_cmdbuf() == 0 || !empty_screen())
680 * Command is incomplete, so try to complete it.
686 * We have a number but no command. Treat as #g.
693 * We have "/string" but no newline. Add the \n.
699 * Some other incomplete command. Let user complete it.
706 * "Unget" a command character.
707 * The next getcc() will return this character.
715 if (ungotp >= ungot + sizeof(ungot))
717 error("ungetcc overflow", NULL_PARG);
724 * Unget a whole string of command characters.
725 * The next sequence of getcc()'s will return this string.
733 for (p = s + strlen(s) - 1; p >= s; p--)
738 * Search for a pattern, possibly in multiple files.
739 * If SRCH_FIRST_FILE is set, begin searching at the first file.
740 * If SRCH_PAST_EOF is set, continue the search thru multiple files.
743 multi_search(pattern, n)
752 save_ifile = save_curr_ifile();
754 if (search_type & SRCH_FIRST_FILE)
757 * Start at the first (or last) file
758 * in the command line list.
760 if (search_type & SRCH_FORW)
761 nomore = edit_first();
763 nomore = edit_last();
766 unsave_ifile(save_ifile);
770 search_type &= ~SRCH_FIRST_FILE;
775 n = search(search_type, pattern, n);
777 * The SRCH_NO_MOVE flag doesn't "stick": it gets cleared
778 * after being used once. This allows "n" to work after
779 * using a /@@ search.
781 search_type &= ~SRCH_NO_MOVE;
787 unsave_ifile(save_ifile);
793 * Some kind of error in the search.
794 * Error message has been printed by search().
798 if ((search_type & SRCH_PAST_EOF) == 0)
800 * We didn't find a match, but we're
801 * supposed to search only one file.
805 * Move on to the next file.
807 if (search_type & SRCH_FORW)
808 nomore = edit_next(1);
810 nomore = edit_prev(1);
818 * Print an error message if we haven't already.
821 error("Pattern not found", NULL_PARG);
826 * Restore the file we were originally viewing.
828 reedit_ifile(save_ifile);
833 * Main command processor.
834 * Accept and execute commands until a quit command.
843 int save_search_type;
851 search_type = SRCH_FORW;
852 wscroll = (sc_height + 1) / 2;
853 newaction = A_NOACTION;
863 * See if any signals need processing.
869 quit(QUIT_SAVED_STATUS);
873 * See if window size changed, for systems that don't
879 * Display prompt and accept a character.
885 if (newaction == A_NOACTION)
892 if (newaction != A_NOACTION)
895 newaction = A_NOACTION;
899 * If we are in a multicharacter command, call mca_char.
900 * Otherwise we call fcmd_decode to determine the
901 * action to be performed.
908 * Need another character.
914 * Command has been handled by mca_char.
915 * Start clean with a prompt.
920 * Not a multi-char command
921 * (at least, not anymore).
927 * Decode the command character and decide what to do.
932 * We're in a multichar command.
933 * Add the character to the command buffer
934 * and display it on the screen.
935 * If the user backspaces past the start
936 * of the line, abort the command.
938 if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0)
944 * Don't use cmd_char if we're starting fresh
945 * at the beginning of a command, because we
946 * don't want to echo the command until we know
947 * it is a multichar command. We also don't
948 * want erase_char/kill_char to be treated
949 * as line editing characters.
956 action = fcmd_decode(cbuf, &extra);
958 * If an "extra" string was returned,
959 * process it as a string of command characters.
965 * Clear the cmdbuf string.
966 * (But not if we're in the prefix of a command,
967 * because the partial command string is kept there.)
969 if (action != A_PREFIX)
976 * First digit of a number.
978 start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE);
983 * Forward one window (and set the window size).
986 swindow = (int) number;
990 * Forward one screen.
993 number = get_swindow();
996 set_attnpos(bottompos);
997 forward((int) number, 0, 1);
1002 * Backward one window (and set the window size).
1005 swindow = (int) number;
1009 * Backward one screen.
1012 number = get_swindow();
1014 backward((int) number, 0, 1);
1019 * Forward N (default 1) line.
1024 if (show_attn == OPT_ONPLUS && number > 1)
1025 set_attnpos(bottompos);
1026 forward((int) number, 0, 0);
1031 * Backward N (default 1) line.
1036 backward((int) number, 0, 0);
1041 * Force forward N (default 1) line.
1046 if (show_attn == OPT_ONPLUS && number > 1)
1047 set_attnpos(bottompos);
1048 forward((int) number, 1, 0);
1053 * Force backward N (default 1) line.
1058 backward((int) number, 1, 0);
1063 * Force forward one screen.
1066 number = get_swindow();
1068 if (show_attn == OPT_ONPLUS)
1069 set_attnpos(bottompos);
1070 forward((int) number, 1, 0);
1075 * Forward forever, ignoring EOF.
1077 if (ch_getflags() & CH_HELPFILE)
1087 * This gets us back in "F mode" after processing
1088 * a non-abort signal (e.g. window-change).
1090 if (sigs && !ABORT_SIGS())
1091 newaction = A_F_FOREVER;
1097 * (default same as last 'd' or 'u' command).
1100 wscroll = (int) number;
1102 if (show_attn == OPT_ONPLUS)
1103 set_attnpos(bottompos);
1104 forward(wscroll, 0, 0);
1110 * (default same as last 'd' or 'u' command).
1113 wscroll = (int) number;
1115 backward(wscroll, 0, 0);
1120 * Flush buffers, then repaint screen.
1121 * Don't flush the buffers on a pipe!
1123 if (ch_getflags() & CH_CANSEEK)
1142 * Go to line N, default beginning of file.
1152 * Go to a specified percentage into the file.
1159 jump_percent((int) number);
1164 * Go to line N, default end of file.
1175 * Go to a specified byte position in the file.
1180 jump_line_loc((POSITION) number, jump_sline);
1185 * Print file name, etc.
1187 if (ch_getflags() & CH_HELPFILE)
1190 parg.p_string = eq_message();
1196 * Print version number, without the "@(#)".
1206 if (curr_ifile != NULL_IFILE &&
1207 ch_getflags() & CH_HELPFILE)
1210 * Quit while viewing the help file
1211 * just means return to viewing the
1214 if (edit_prev(1) == 0)
1223 * Define abbreviation for a commonly used sequence below.
1225 #define DO_SEARCH() if (number <= 0) number = 1; \
1228 multi_search((char *)NULL, (int) number);
1233 * Search forward for a pattern.
1234 * Get the first char of the pattern.
1236 search_type = SRCH_FORW;
1245 * Search backward for a pattern.
1246 * Get the first char of the pattern.
1248 search_type = SRCH_BACK;
1255 case A_AGAIN_SEARCH:
1257 * Repeat previous search.
1262 case A_T_AGAIN_SEARCH:
1264 * Repeat previous search, multiple files.
1266 search_type |= SRCH_PAST_EOF;
1270 case A_REVERSE_SEARCH:
1272 * Repeat previous search, in reverse direction.
1274 save_search_type = search_type;
1275 search_type = SRCH_REVERSE(search_type);
1277 search_type = save_search_type;
1280 case A_T_REVERSE_SEARCH:
1282 * Repeat previous search,
1283 * multiple files in reverse direction.
1285 save_search_type = search_type;
1286 search_type = SRCH_REVERSE(search_type);
1287 search_type |= SRCH_PAST_EOF;
1289 search_type = save_search_type;
1300 if (ch_getflags() & CH_HELPFILE)
1303 (void) edit(FAKE_HELPFILE);
1309 * Edit a new file. Get the filename.
1313 error("Command not available", NULL_PARG);
1316 start_mca(A_EXAMINE, "Examine: ", ml_examine, 0);
1320 error("Command not available", NULL_PARG);
1326 * Invoke an editor on the input file.
1331 error("Command not available", NULL_PARG);
1334 if (ch_getflags() & CH_HELPFILE)
1336 if (strcmp(get_filename(curr_ifile), "-") == 0)
1338 error("Cannot edit standard input", NULL_PARG);
1341 if (curr_altfilename != NULL)
1343 error("Cannot edit file processed with LESSOPEN",
1347 start_mca(A_SHELL, "!", ml_shell, 0);
1349 * Expand the editor prototype string
1350 * and pass it to the system to execute.
1351 * (Make sure the screen is displayed so the
1352 * expansion of "+%lm" works.)
1356 lsystem(pr_expand(editproto, 0), (char*)NULL);
1359 error("Command not available", NULL_PARG);
1365 * Examine next file.
1370 error("No next file", NULL_PARG);
1376 if (edit_next((int) number))
1378 if (quit_at_eof && hit_eof &&
1379 !(ch_getflags() & CH_HELPFILE))
1381 parg.p_string = (number > 1) ? "(N-th) " : "";
1382 error("No %snext file", &parg);
1388 * Examine previous file.
1393 error("No previous file", NULL_PARG);
1399 if (edit_prev((int) number))
1401 parg.p_string = (number > 1) ? "(N-th) " : "";
1402 error("No %sprevious file", &parg);
1410 tagfile = nexttag((int) number);
1411 if (tagfile == NULL)
1413 error("No next tag", NULL_PARG);
1416 if (edit(tagfile) == 0)
1418 POSITION pos = tagsearch();
1419 if (pos != NULL_POSITION)
1420 jump_loc(pos, jump_sline);
1423 error("Command not available", NULL_PARG);
1431 tagfile = prevtag((int) number);
1432 if (tagfile == NULL)
1434 error("No previous tag", NULL_PARG);
1437 if (edit(tagfile) == 0)
1439 POSITION pos = tagsearch();
1440 if (pos != NULL_POSITION)
1441 jump_loc(pos, jump_sline);
1444 error("Command not available", NULL_PARG);
1450 * Examine a particular file.
1454 if (edit_index((int) number))
1455 error("No such file", NULL_PARG);
1459 if (ch_getflags() & CH_HELPFILE)
1461 old_ifile = curr_ifile;
1462 new_ifile = getoff_ifile(curr_ifile);
1463 if (new_ifile == NULL_IFILE)
1468 if (edit_ifile(new_ifile) != 0)
1470 reedit_ifile(old_ifile);
1473 del_ifile(old_ifile);
1477 optflag = OPT_TOGGLE;
1485 * Report a flag setting.
1487 optflag = OPT_NO_TOGGLE;
1495 * Set an initial command for new files.
1497 start_mca(A_FIRSTCMD, "+", (void*)NULL, 0);
1508 error("Command not available", NULL_PARG);
1511 start_mca(A_SHELL, "!", ml_shell, 0);
1515 error("Command not available", NULL_PARG);
1523 if (ch_getflags() & CH_HELPFILE)
1525 start_mca(A_SETMARK, "mark: ", (void*)NULL, 0);
1527 if (c == erase_char || c == kill_char ||
1528 c == '\n' || c == '\r')
1537 start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0);
1539 if (c == erase_char || c == kill_char ||
1540 c == '\n' || c == '\r')
1549 error("Command not available", NULL_PARG);
1552 start_mca(A_PIPE, "|mark: ", (void*)NULL, 0);
1554 if (c == erase_char || c == kill_char)
1556 if (c == '\n' || c == '\r')
1561 start_mca(A_PIPE, "!", ml_shell, 0);
1565 error("Command not available", NULL_PARG);
1571 start_mca(action, "Brackets: ", (void*)NULL, 0);
1577 shift_count = number;
1579 number = (shift_count > 0) ?
1580 shift_count : sc_width / 2;
1581 if (number > hshift)
1589 shift_count = number;
1591 number = (shift_count > 0) ?
1592 shift_count : sc_width / 2;
1599 * The command is incomplete (more chars are needed).
1600 * Display the current char, so the user knows
1601 * what's going on, and get another character.
1603 if (mca != A_PREFIX)
1606 start_mca(A_PREFIX, " ", (void*)NULL,