3 * Copyright (C) 1984-2007 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_if_one_screen;
33 extern int jump_sline;
36 extern int top_scroll;
37 extern int ignore_eoi;
41 extern int less_is_more;
42 extern char *every_first_cmd;
43 extern char *curr_altfilename;
44 extern char version[];
45 extern struct scrpos initial_scrpos;
46 extern IFILE curr_ifile;
47 extern void constant *ml_search;
48 extern void constant *ml_examine;
49 #if SHELL_ESCAPE || PIPEC
50 extern void constant *ml_shell;
54 extern char *editproto;
56 extern int screen_trashed; /* The screen has been overwritten */
57 extern int shift_count;
59 extern int forw_prompt;
61 static char ungot[UNGOT_SIZE];
62 static char *ungotp = NULL;
64 static char *shellcmd = NULL; /* For holding last shell command for "!!" */
66 static int mca; /* The multicharacter command (action) */
67 static int search_type; /* The previous type of search */
68 static LINENUM number; /* The number typed by the user */
69 static long fraction; /* The fractional part of the number */
72 static int optgetname;
73 static POSITION bottompos;
74 static int save_hshift;
79 static void multi_search();
82 * Move the cursor to start of prompt line before executing a command.
83 * This looks nicer if the command takes a long time before
84 * updating the screen.
95 * Set up the display to start a new multi-character command.
98 start_mca(action, prompt, mlist, cmdflags)
108 set_mlist(mlist, cmdflags);
114 return (mca != 0 && mca != A_PREFIX);
118 * Set up the display to start a new search command.
123 if (search_type & SRCH_FORW)
131 if (search_type & SRCH_NO_MATCH)
132 cmd_putstr("Non-match ");
133 if (search_type & SRCH_FIRST_FILE)
134 cmd_putstr("First-file ");
135 if (search_type & SRCH_PAST_EOF)
136 cmd_putstr("EOF-ignore ");
137 if (search_type & SRCH_NO_MOVE)
138 cmd_putstr("Keep-pos ");
139 if (search_type & SRCH_NO_REGEX)
140 cmd_putstr("Regex-off ");
142 if (search_type & SRCH_FORW)
146 set_mlist(ml_search, 0);
150 * Set up the display to start a new toggle-option command.
159 no_prompt = (optflag & OPT_NO_PROMPT);
160 flag = (optflag & ~OPT_NO_PROMPT);
161 dash = (flag == OPT_NO_TOGGLE) ? "_" : "-";
184 * Execute a multicharacter command.
198 multi_search(cbuf, (int) number);
202 * Skip leading spaces or + signs in the string.
204 while (*cbuf == '+' || *cbuf == ' ')
206 if (every_first_cmd != NULL)
207 free(every_first_cmd);
209 every_first_cmd = NULL;
211 every_first_cmd = save(cbuf);
214 toggle_option(optchar, cbuf, optflag);
218 match_brac(cbuf[0], cbuf[1], 1, (int) number);
221 match_brac(cbuf[1], cbuf[0], 0, (int) number);
229 /* If tag structure is loaded then clean it up. */
237 * !! just uses whatever is in shellcmd.
238 * Otherwise, copy cmdbuf to shellcmd,
239 * expanding any special characters ("%" or "#").
243 if (shellcmd != NULL)
245 shellcmd = fexpand(cbuf);
250 if (shellcmd == NULL)
251 lsystem("", "!done");
253 lsystem(shellcmd, "!done");
260 (void) pipe_mark(pipec, cbuf);
261 error("|done", NULL_PARG);
268 * Add a character to a multi-character command.
283 * Not in a multicharacter command.
289 * In the prefix of a command.
290 * This not considered a multichar command
291 * (even tho it uses cmdbuf, etc.).
292 * It is handled in the commands() switch.
298 * Entering digits of a number.
299 * Terminated by a non-digit.
301 if (!((c >= '0' && c <= '9') || c == '.') &&
302 editchar(c, EC_PEEK|EC_NOHISTORY|EC_NOCOMPLETE|EC_NORIGHTLEFT) == A_INVALID)
305 * Not part of the number.
306 * Treat as a normal command character.
308 number = cmd_int(&fraction);
317 * Special case for the TOGGLE_OPTION command.
318 * If the option letter which was entered is a
319 * single-char option, execute the command immediately,
320 * so user doesn't have to hit RETURN.
321 * If the first char is + or -, this indicates
322 * OPT_UNSET or OPT_SET respectively, instead of OPT_TOGGLE.
323 * "--" begins inputting a long option name.
325 if (optchar == '\0' && len_cmdbuf() == 0)
327 flag = (optflag & ~OPT_NO_PROMPT);
328 if (flag == OPT_NO_TOGGLE)
333 /* "__" = long option name. */
344 optflag = (flag == OPT_UNSET) ?
345 OPT_TOGGLE : OPT_UNSET;
350 optflag = (flag == OPT_SET) ?
351 OPT_TOGGLE : OPT_SET;
355 optflag ^= OPT_NO_PROMPT;
359 /* "--" = long option name. */
369 * We're getting a long option name.
370 * See if we've matched an option name yet.
371 * If so, display the complete name and stop
372 * accepting chars until user hits RETURN.
378 if (c == '\n' || c == '\r')
381 * When the user hits RETURN, make sure
382 * we've matched an option name, then
383 * pretend he just entered the equivalent
388 parg.p_string = get_cmdbuf();
389 error("There is no --%s option", &parg);
400 * Already have a match for the name.
401 * Don't accept anything but erase/kill.
403 if (c == erase_char ||
410 * Add char to cmd buffer and try to match
413 if (cmd_char(c) == CC_QUIT)
416 lc = ASCII_IS_LOWER(p[0]);
417 o = findopt_name(&p, &oname, NULL);
422 * Remember the option letter and
423 * display the full option name.
425 optchar = o->oletter;
426 if (!lc && ASCII_IS_LOWER(optchar))
427 optchar = ASCII_TO_UPPER(optchar);
430 for (p = oname; *p != '\0'; p++)
433 if (!lc && ASCII_IS_LOWER(c))
434 c = ASCII_TO_UPPER(c);
435 if (cmd_char(c) != CC_OK)
443 if (c == erase_char || c == erase2_char || c == kill_char)
446 /* We already have the option letter. */
451 if ((optflag & ~OPT_NO_PROMPT) != OPT_TOGGLE ||
452 single_char_option(c))
454 toggle_option(c, "", optflag);
458 * Display a prompt appropriate for the option letter.
460 if ((p = opt_prompt(c)) == NULL)
467 start_mca(A_OPT_TOGGLE, p, (void*)NULL, 0);
473 * Special case for search commands.
474 * Certain characters as the first char of
475 * the pattern have special meaning:
476 * ! Toggle the NO_MATCH flag
477 * * Toggle the PAST_EOF flag
478 * @ Toggle the FIRST_FILE flag
480 if (len_cmdbuf() > 0)
482 * Only works for the first char of the pattern.
492 case CONTROL('E'): /* ignore END of file */
493 flag = SRCH_PAST_EOF;
498 case CONTROL('F'): /* FIRST file */
499 flag = SRCH_FIRST_FILE;
501 case CONTROL('K'): /* KEEP position */
504 case CONTROL('R'): /* Don't use REGULAR EXPRESSIONS */
505 flag = SRCH_NO_REGEX;
507 case CONTROL('N'): /* NOT match */
509 flag = SRCH_NO_MATCH;
522 * Any other multicharacter command
523 * is terminated by a newline.
525 if (c == '\n' || c == '\r')
528 * Execute the command.
535 * Append the char to the command buffer.
537 if (cmd_char(c) == CC_QUIT)
539 * Abort the multi-char command.
543 if ((mca == A_F_BRACKET || mca == A_B_BRACKET) && len_cmdbuf() >= 2)
546 * Special case for the bracket-matching commands.
547 * Execute the command after getting exactly two
548 * characters from the user.
555 * Need another character.
561 * Discard any buffered file data.
566 if (!(ch_getflags() & CH_CANSEEK))
576 * Make sure the screen is displayed.
582 * If nothing is displayed yet, display starting from initial_scrpos.
586 if (initial_scrpos.pos == NULL_POSITION)
588 * {{ Maybe this should be:
589 * jump_loc(ch_zero(), jump_sline);
590 * but this behavior seems rather unexpected
591 * on the first screen. }}
593 jump_loc(ch_zero(), 1);
595 jump_loc(initial_scrpos.pos, initial_scrpos.ln);
596 } else if (screen_trashed)
598 int save_top_scroll = top_scroll;
599 int save_ignore_eoi = ignore_eoi;
602 if (screen_trashed == 2)
604 /* Special case used by ignore_eoi: re-open the input file
605 * and jump to the end of the file. */
610 top_scroll = save_top_scroll;
611 ignore_eoi = save_ignore_eoi;
616 * Display the appropriate prompt.
623 if (ungotp != NULL && ungotp > ungot)
626 * No prompt necessary if commands are from
627 * ungotten chars rather than from the user.
633 * Make sure the screen is displayed.
636 bottompos = position(BOTTOM_PLUS_ONE);
639 * If we've hit EOF on the last file, and the -E flag is set
640 * (or -F is set and this is the first prompt), then quit.
641 * {{ Relying on "first prompt" to detect a single-screen file
642 * fails if +G is used, for example. }}
644 if ((get_quit_at_eof() == OPT_ONPLUS || quit_if_one_screen) &&
645 hit_eof && !(ch_getflags() & CH_HELPFILE) &&
646 next_ifile(curr_ifile) == NULL_IFILE)
648 quit_if_one_screen = FALSE;
649 #if 0 /* This doesn't work well because some "te"s clear the screen. */
651 * If the -e flag is set and we've hit EOF on the last file,
652 * and the file is squished (shorter than the screen), quit.
654 if (get_quit_at_eof() && squished &&
655 next_ifile(curr_ifile) == NULL_IFILE)
659 #if MSDOS_COMPILER==WIN32C
661 * In Win32, display the file name in the window title.
663 if (!(ch_getflags() & CH_HELPFILE))
664 SetConsoleTitle(pr_expand("Less?f - %f.", 0));
667 * Select the proper prompt and display it.
670 * If the previous action was a forward movement,
671 * don't clear the bottom line of the display;
672 * just print the prompt since the forward movement guarantees
673 * that we're in the right position to display the prompt.
674 * Clearing the line could cause a problem: for example, if the last
675 * line displayed ended at the right screen edge without a newline,
676 * then clearing would clear the last displayed line rather than
684 if (p == NULL || *p == '\0')
688 at_enter(AT_STANDOUT);
696 * Display the less version message.
703 parg.p_string = version;
704 error("less %s", &parg);
708 * Get command character.
709 * The character normally comes from the keyboard,
710 * but may come from ungotten characters
711 * (characters previously given to ungetcc or ungetsc).
718 * Normal case: no ungotten chars, so get one from the user.
724 * Return the next ungotten char.
729 * We have just run out of ungotten chars.
732 if (len_cmdbuf() == 0 || !empty_screen())
735 * Command is incomplete, so try to complete it.
741 * We have a number but no command. Treat as #g.
748 * We have "/string" but no newline. Add the \n.
754 * Some other incomplete command. Let user complete it.
761 * "Unget" a command character.
762 * The next getcc() will return this character.
770 if (ungotp >= ungot + sizeof(ungot))
772 error("ungetcc overflow", NULL_PARG);
779 * Unget a whole string of command characters.
780 * The next sequence of getcc()'s will return this string.
788 for (p = s + strlen(s) - 1; p >= s; p--)
793 * Search for a pattern, possibly in multiple files.
794 * If SRCH_FIRST_FILE is set, begin searching at the first file.
795 * If SRCH_PAST_EOF is set, continue the search thru multiple files.
798 multi_search(pattern, n)
807 save_ifile = save_curr_ifile();
809 if (search_type & SRCH_FIRST_FILE)
812 * Start at the first (or last) file
813 * in the command line list.
815 if (search_type & SRCH_FORW)
816 nomore = edit_first();
818 nomore = edit_last();
821 unsave_ifile(save_ifile);
825 search_type &= ~SRCH_FIRST_FILE;
830 n = search(search_type, pattern, n);
832 * The SRCH_NO_MOVE flag doesn't "stick": it gets cleared
833 * after being used once. This allows "n" to work after
834 * using a /@@ search.
836 search_type &= ~SRCH_NO_MOVE;
842 unsave_ifile(save_ifile);
848 * Some kind of error in the search.
849 * Error message has been printed by search().
853 if ((search_type & SRCH_PAST_EOF) == 0)
855 * We didn't find a match, but we're
856 * supposed to search only one file.
860 * Move on to the next file.
862 if (search_type & SRCH_FORW)
863 nomore = edit_next(1);
865 nomore = edit_prev(1);
873 * Print an error message if we haven't already.
876 error("Pattern not found", NULL_PARG);
881 * Restore the file we were originally viewing.
883 reedit_ifile(save_ifile);
886 unsave_ifile(save_ifile);
891 * Main command processor.
892 * Accept and execute commands until a quit command.
901 int save_search_type;
909 search_type = SRCH_FORW;
910 wscroll = (sc_height + 1) / 2;
911 newaction = A_NOACTION;
921 * See if any signals need processing.
927 quit(QUIT_SAVED_STATUS);
931 * See if window size changed, for systems that don't
937 * Display prompt and accept a character.
943 if (newaction == A_NOACTION)
950 if (newaction != A_NOACTION)
953 newaction = A_NOACTION;
957 * If we are in a multicharacter command, call mca_char.
958 * Otherwise we call fcmd_decode to determine the
959 * action to be performed.
966 * Need another character.
972 * Command has been handled by mca_char.
973 * Start clean with a prompt.
978 * Not a multi-char command
979 * (at least, not anymore).
985 * Decode the command character and decide what to do.
990 * We're in a multichar command.
991 * Add the character to the command buffer
992 * and display it on the screen.
993 * If the user backspaces past the start
994 * of the line, abort the command.
996 if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0)
1002 * Don't use cmd_char if we're starting fresh
1003 * at the beginning of a command, because we
1004 * don't want to echo the command until we know
1005 * it is a multichar command. We also don't
1006 * want erase_char/kill_char to be treated
1007 * as line editing characters.
1014 action = fcmd_decode(cbuf, &extra);
1016 * If an "extra" string was returned,
1017 * process it as a string of command characters.
1023 * Clear the cmdbuf string.
1024 * (But not if we're in the prefix of a command,
1025 * because the partial command string is kept there.)
1027 if (action != A_PREFIX)
1034 * First digit of a number.
1036 start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE);
1041 * Forward one window (and set the window size).
1044 swindow = (int) number;
1048 * Forward one screen.
1051 number = get_swindow();
1054 set_attnpos(bottompos);
1055 forward((int) number, 0, 1);
1060 * Backward one window (and set the window size).
1063 swindow = (int) number;
1067 * Backward one screen.
1070 number = get_swindow();
1072 backward((int) number, 0, 1);
1077 * Forward N (default 1) line.
1082 if (show_attn == OPT_ONPLUS && number > 1)
1083 set_attnpos(bottompos);
1084 forward((int) number, 0, 0);
1089 * Backward N (default 1) line.
1094 backward((int) number, 0, 0);
1099 * Force forward N (default 1) line.
1104 if (show_attn == OPT_ONPLUS && number > 1)
1105 set_attnpos(bottompos);
1106 forward((int) number, 1, 0);
1111 * Force backward N (default 1) line.
1116 backward((int) number, 1, 0);
1121 * Force forward one screen.
1124 number = get_swindow();
1126 if (show_attn == OPT_ONPLUS)
1127 set_attnpos(bottompos);
1128 forward((int) number, 1, 0);
1133 * Forward forever, ignoring EOF.
1135 if (ch_getflags() & CH_HELPFILE)
1148 * This gets us back in "F mode" after processing
1149 * a non-abort signal (e.g. window-change).
1151 if (sigs && !ABORT_SIGS())
1152 newaction = A_F_FOREVER;
1158 * (default same as last 'd' or 'u' command).
1161 wscroll = (int) number;
1163 if (show_attn == OPT_ONPLUS)
1164 set_attnpos(bottompos);
1165 forward(wscroll, 0, 0);
1171 * (default same as last 'd' or 'u' command).
1174 wscroll = (int) number;
1176 backward(wscroll, 0, 0);
1181 * Flush buffers, then repaint screen.
1182 * Don't flush the buffers on a pipe!
1196 * Go to line N, default beginning of file.
1206 * Go to a specified percentage into the file.
1219 jump_percent((int) number, fraction);
1224 * Go to line N, default end of file.
1235 * Go to a specified byte position in the file.
1240 jump_line_loc((POSITION) number, jump_sline);
1245 * Print file name, etc.
1247 if (ch_getflags() & CH_HELPFILE)
1250 parg.p_string = eq_message();
1256 * Print version number, without the "@(#)".
1266 if (curr_ifile != NULL_IFILE &&
1267 ch_getflags() & CH_HELPFILE)
1270 * Quit while viewing the help file
1271 * just means return to viewing the
1274 hshift = save_hshift;
1275 if (edit_prev(1) == 0)
1284 * Define abbreviation for a commonly used sequence below.
1286 #define DO_SEARCH() \
1287 if (number <= 0) number = 1; \
1290 multi_search((char *)NULL, (int) number);
1295 * Search forward for a pattern.
1296 * Get the first char of the pattern.
1298 search_type = SRCH_FORW;
1307 * Search backward for a pattern.
1308 * Get the first char of the pattern.
1310 search_type = SRCH_BACK;
1317 case A_AGAIN_SEARCH:
1319 * Repeat previous search.
1324 case A_T_AGAIN_SEARCH:
1326 * Repeat previous search, multiple files.
1328 search_type |= SRCH_PAST_EOF;
1332 case A_REVERSE_SEARCH:
1334 * Repeat previous search, in reverse direction.
1336 save_search_type = search_type;
1337 search_type = SRCH_REVERSE(search_type);
1339 search_type = save_search_type;
1342 case A_T_REVERSE_SEARCH:
1344 * Repeat previous search,
1345 * multiple files in reverse direction.
1347 save_search_type = search_type;
1348 search_type = SRCH_REVERSE(search_type);
1349 search_type |= SRCH_PAST_EOF;
1351 search_type = save_search_type;
1362 if (ch_getflags() & CH_HELPFILE)
1365 save_hshift = hshift;
1367 (void) edit(FAKE_HELPFILE);
1373 * Edit a new file. Get the filename.
1377 error("Command not available", NULL_PARG);
1380 start_mca(A_EXAMINE, "Examine: ", ml_examine, 0);
1384 error("Command not available", NULL_PARG);
1390 * Invoke an editor on the input file.
1395 error("Command not available", NULL_PARG);
1398 if (ch_getflags() & CH_HELPFILE)
1400 if (strcmp(get_filename(curr_ifile), "-") == 0)
1402 error("Cannot edit standard input", NULL_PARG);
1405 if (curr_altfilename != NULL)
1407 error("WARNING: This file was viewed via LESSOPEN",
1410 start_mca(A_SHELL, "!", ml_shell, 0);
1412 * Expand the editor prototype string
1413 * and pass it to the system to execute.
1414 * (Make sure the screen is displayed so the
1415 * expansion of "+%lm" works.)
1419 lsystem(pr_expand(editproto, 0), (char*)NULL);
1422 error("Command not available", NULL_PARG);
1428 * Examine next file.
1433 error("No next file", NULL_PARG);
1439 if (edit_next((int) number))
1441 if (get_quit_at_eof() && hit_eof &&
1442 !(ch_getflags() & CH_HELPFILE))
1444 parg.p_string = (number > 1) ? "(N-th) " : "";
1445 error("No %snext file", &parg);
1451 * Examine previous file.
1456 error("No previous file", NULL_PARG);
1462 if (edit_prev((int) number))
1464 parg.p_string = (number > 1) ? "(N-th) " : "";
1465 error("No %sprevious file", &parg);
1473 tagfile = nexttag((int) number);
1474 if (tagfile == NULL)
1476 error("No next tag", NULL_PARG);
1479 if (edit(tagfile) == 0)
1481 POSITION pos = tagsearch();
1482 if (pos != NULL_POSITION)
1483 jump_loc(pos, jump_sline);
1486 error("Command not available", NULL_PARG);
1494 tagfile = prevtag((int) number);
1495 if (tagfile == NULL)
1497 error("No previous tag", NULL_PARG);
1500 if (edit(tagfile) == 0)
1502 POSITION pos = tagsearch();
1503 if (pos != NULL_POSITION)
1504 jump_loc(pos, jump_sline);
1507 error("Command not available", NULL_PARG);
1513 * Examine a particular file.
1517 if (edit_index((int) number))
1518 error("No such file", NULL_PARG);
1522 if (ch_getflags() & CH_HELPFILE)
1524 old_ifile = curr_ifile;
1525 new_ifile = getoff_ifile(curr_ifile);
1526 if (new_ifile == NULL_IFILE)
1531 if (edit_ifile(new_ifile) != 0)
1533 reedit_ifile(old_ifile);
1536 del_ifile(old_ifile);
1540 optflag = OPT_TOGGLE;
1548 * Report a flag setting.
1550 optflag = OPT_NO_TOGGLE;
1558 * Set an initial command for new files.
1560 start_mca(A_FIRSTCMD, "+", (void*)NULL, 0);
1571 error("Command not available", NULL_PARG);
1574 start_mca(A_SHELL, "!", ml_shell, 0);
1578 error("Command not available", NULL_PARG);
1586 if (ch_getflags() & CH_HELPFILE)
1588 start_mca(A_SETMARK, "mark: ", (void*)NULL, 0);
1590 if (c == erase_char || c == erase2_char ||
1591 c == kill_char || c == '\n' || c == '\r')
1600 start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0);
1602 if (c == erase_char || c == erase2_char ||
1603 c == kill_char || c == '\n' || c == '\r')
1612 error("Command not available", NULL_PARG);
1615 start_mca(A_PIPE, "|mark: ", (void*)NULL, 0);
1617 if (c == erase_char || c == erase2_char || c == kill_char)
1619 if (c == '\n' || c == '\r')
1624 start_mca(A_PIPE, "!", ml_shell, 0);
1628 error("Command not available", NULL_PARG);
1634 start_mca(action, "Brackets: ", (void*)NULL, 0);
1640 shift_count = number;
1642 number = (shift_count > 0) ?
1643 shift_count : sc_width / 2;
1644 if (number > hshift)
1652 shift_count = number;
1654 number = (shift_count > 0) ?
1655 shift_count : sc_width / 2;
1662 * The command is incomplete (more chars are needed).
1663 * Display the current char, so the user knows
1664 * what's going on, and get another character.
1666 if (mca != A_PREFIX)
1669 start_mca(A_PREFIX, " ", (void*)NULL,