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 * Make sure the screen is displayed.
567 * If nothing is displayed yet, display starting from initial_scrpos.
571 if (initial_scrpos.pos == NULL_POSITION)
573 * {{ Maybe this should be:
574 * jump_loc(ch_zero(), jump_sline);
575 * but this behavior seems rather unexpected
576 * on the first screen. }}
578 jump_loc(ch_zero(), 1);
580 jump_loc(initial_scrpos.pos, initial_scrpos.ln);
581 } else if (screen_trashed)
584 save_top_scroll = top_scroll;
587 top_scroll = save_top_scroll;
592 * Display the appropriate prompt.
599 if (ungotp != NULL && ungotp > ungot)
602 * No prompt necessary if commands are from
603 * ungotten chars rather than from the user.
609 * Make sure the screen is displayed.
612 bottompos = position(BOTTOM_PLUS_ONE);
615 * If we've hit EOF on the last file, and the -E flag is set
616 * (or -F is set and this is the first prompt), then quit.
617 * {{ Relying on "first prompt" to detect a single-screen file
618 * fails if +G is used, for example. }}
620 if ((get_quit_at_eof() == OPT_ONPLUS || quit_if_one_screen) &&
621 hit_eof && !(ch_getflags() & CH_HELPFILE) &&
622 next_ifile(curr_ifile) == NULL_IFILE)
624 quit_if_one_screen = FALSE;
625 #if 0 /* This doesn't work well because some "te"s clear the screen. */
627 * If the -e flag is set and we've hit EOF on the last file,
628 * and the file is squished (shorter than the screen), quit.
630 if (get_quit_at_eof() && squished &&
631 next_ifile(curr_ifile) == NULL_IFILE)
635 #if MSDOS_COMPILER==WIN32C
637 * In Win32, display the file name in the window title.
639 if (!(ch_getflags() & CH_HELPFILE))
640 SetConsoleTitle(pr_expand("Less?f - %f.", 0));
643 * Select the proper prompt and display it.
646 * If the previous action was a forward movement,
647 * don't clear the bottom line of the display;
648 * just print the prompt since the forward movement guarantees
649 * that we're in the right position to display the prompt.
650 * Clearing the line could cause a problem: for example, if the last
651 * line displayed ended at the right screen edge without a newline,
652 * then clearing would clear the last displayed line rather than
660 if (p == NULL || *p == '\0')
664 at_enter(AT_STANDOUT);
672 * Display the less version message.
679 parg.p_string = version;
680 error("less %s", &parg);
684 * Get command character.
685 * The character normally comes from the keyboard,
686 * but may come from ungotten characters
687 * (characters previously given to ungetcc or ungetsc).
694 * Normal case: no ungotten chars, so get one from the user.
700 * Return the next ungotten char.
705 * We have just run out of ungotten chars.
708 if (len_cmdbuf() == 0 || !empty_screen())
711 * Command is incomplete, so try to complete it.
717 * We have a number but no command. Treat as #g.
724 * We have "/string" but no newline. Add the \n.
730 * Some other incomplete command. Let user complete it.
737 * "Unget" a command character.
738 * The next getcc() will return this character.
746 if (ungotp >= ungot + sizeof(ungot))
748 error("ungetcc overflow", NULL_PARG);
755 * Unget a whole string of command characters.
756 * The next sequence of getcc()'s will return this string.
764 for (p = s + strlen(s) - 1; p >= s; p--)
769 * Search for a pattern, possibly in multiple files.
770 * If SRCH_FIRST_FILE is set, begin searching at the first file.
771 * If SRCH_PAST_EOF is set, continue the search thru multiple files.
774 multi_search(pattern, n)
783 save_ifile = save_curr_ifile();
785 if (search_type & SRCH_FIRST_FILE)
788 * Start at the first (or last) file
789 * in the command line list.
791 if (search_type & SRCH_FORW)
792 nomore = edit_first();
794 nomore = edit_last();
797 unsave_ifile(save_ifile);
801 search_type &= ~SRCH_FIRST_FILE;
806 n = search(search_type, pattern, n);
808 * The SRCH_NO_MOVE flag doesn't "stick": it gets cleared
809 * after being used once. This allows "n" to work after
810 * using a /@@ search.
812 search_type &= ~SRCH_NO_MOVE;
818 unsave_ifile(save_ifile);
824 * Some kind of error in the search.
825 * Error message has been printed by search().
829 if ((search_type & SRCH_PAST_EOF) == 0)
831 * We didn't find a match, but we're
832 * supposed to search only one file.
836 * Move on to the next file.
838 if (search_type & SRCH_FORW)
839 nomore = edit_next(1);
841 nomore = edit_prev(1);
849 * Print an error message if we haven't already.
852 error("Pattern not found", NULL_PARG);
857 * Restore the file we were originally viewing.
859 reedit_ifile(save_ifile);
862 unsave_ifile(save_ifile);
867 * Main command processor.
868 * Accept and execute commands until a quit command.
877 int save_search_type;
885 search_type = SRCH_FORW;
886 wscroll = (sc_height + 1) / 2;
887 newaction = A_NOACTION;
897 * See if any signals need processing.
903 quit(QUIT_SAVED_STATUS);
907 * See if window size changed, for systems that don't
913 * Display prompt and accept a character.
919 if (newaction == A_NOACTION)
926 if (newaction != A_NOACTION)
929 newaction = A_NOACTION;
933 * If we are in a multicharacter command, call mca_char.
934 * Otherwise we call fcmd_decode to determine the
935 * action to be performed.
942 * Need another character.
948 * Command has been handled by mca_char.
949 * Start clean with a prompt.
954 * Not a multi-char command
955 * (at least, not anymore).
961 * Decode the command character and decide what to do.
966 * We're in a multichar command.
967 * Add the character to the command buffer
968 * and display it on the screen.
969 * If the user backspaces past the start
970 * of the line, abort the command.
972 if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0)
978 * Don't use cmd_char if we're starting fresh
979 * at the beginning of a command, because we
980 * don't want to echo the command until we know
981 * it is a multichar command. We also don't
982 * want erase_char/kill_char to be treated
983 * as line editing characters.
990 action = fcmd_decode(cbuf, &extra);
992 * If an "extra" string was returned,
993 * process it as a string of command characters.
999 * Clear the cmdbuf string.
1000 * (But not if we're in the prefix of a command,
1001 * because the partial command string is kept there.)
1003 if (action != A_PREFIX)
1010 * First digit of a number.
1012 start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE);
1017 * Forward one window (and set the window size).
1020 swindow = (int) number;
1024 * Forward one screen.
1027 number = get_swindow();
1030 set_attnpos(bottompos);
1031 forward((int) number, 0, 1);
1036 * Backward one window (and set the window size).
1039 swindow = (int) number;
1043 * Backward one screen.
1046 number = get_swindow();
1048 backward((int) number, 0, 1);
1053 * Forward N (default 1) line.
1058 if (show_attn == OPT_ONPLUS && number > 1)
1059 set_attnpos(bottompos);
1060 forward((int) number, 0, 0);
1065 * Backward N (default 1) line.
1070 backward((int) number, 0, 0);
1075 * Force forward N (default 1) line.
1080 if (show_attn == OPT_ONPLUS && number > 1)
1081 set_attnpos(bottompos);
1082 forward((int) number, 1, 0);
1087 * Force backward N (default 1) line.
1092 backward((int) number, 1, 0);
1097 * Force forward one screen.
1100 number = get_swindow();
1102 if (show_attn == OPT_ONPLUS)
1103 set_attnpos(bottompos);
1104 forward((int) number, 1, 0);
1109 * Forward forever, ignoring EOF.
1111 if (ch_getflags() & CH_HELPFILE)
1121 * This gets us back in "F mode" after processing
1122 * a non-abort signal (e.g. window-change).
1124 if (sigs && !ABORT_SIGS())
1125 newaction = A_F_FOREVER;
1131 * (default same as last 'd' or 'u' command).
1134 wscroll = (int) number;
1136 if (show_attn == OPT_ONPLUS)
1137 set_attnpos(bottompos);
1138 forward(wscroll, 0, 0);
1144 * (default same as last 'd' or 'u' command).
1147 wscroll = (int) number;
1149 backward(wscroll, 0, 0);
1154 * Flush buffers, then repaint screen.
1155 * Don't flush the buffers on a pipe!
1157 if (ch_getflags() & CH_CANSEEK)
1176 * Go to line N, default beginning of file.
1186 * Go to a specified percentage into the file.
1199 jump_percent((int) number, fraction);
1204 * Go to line N, default end of file.
1215 * Go to a specified byte position in the file.
1220 jump_line_loc((POSITION) number, jump_sline);
1225 * Print file name, etc.
1227 if (ch_getflags() & CH_HELPFILE)
1230 parg.p_string = eq_message();
1236 * Print version number, without the "@(#)".
1246 if (curr_ifile != NULL_IFILE &&
1247 ch_getflags() & CH_HELPFILE)
1250 * Quit while viewing the help file
1251 * just means return to viewing the
1254 hshift = save_hshift;
1255 if (edit_prev(1) == 0)
1264 * Define abbreviation for a commonly used sequence below.
1266 #define DO_SEARCH() if (number <= 0) number = 1; \
1269 multi_search((char *)NULL, (int) number);
1274 * Search forward for a pattern.
1275 * Get the first char of the pattern.
1277 search_type = SRCH_FORW;
1286 * Search backward for a pattern.
1287 * Get the first char of the pattern.
1289 search_type = SRCH_BACK;
1296 case A_AGAIN_SEARCH:
1298 * Repeat previous search.
1303 case A_T_AGAIN_SEARCH:
1305 * Repeat previous search, multiple files.
1307 search_type |= SRCH_PAST_EOF;
1311 case A_REVERSE_SEARCH:
1313 * Repeat previous search, in reverse direction.
1315 save_search_type = search_type;
1316 search_type = SRCH_REVERSE(search_type);
1318 search_type = save_search_type;
1321 case A_T_REVERSE_SEARCH:
1323 * Repeat previous search,
1324 * multiple files in reverse direction.
1326 save_search_type = search_type;
1327 search_type = SRCH_REVERSE(search_type);
1328 search_type |= SRCH_PAST_EOF;
1330 search_type = save_search_type;
1341 if (ch_getflags() & CH_HELPFILE)
1344 save_hshift = hshift;
1346 (void) edit(FAKE_HELPFILE);
1352 * Edit a new file. Get the filename.
1356 error("Command not available", NULL_PARG);
1359 start_mca(A_EXAMINE, "Examine: ", ml_examine, 0);
1363 error("Command not available", NULL_PARG);
1369 * Invoke an editor on the input file.
1374 error("Command not available", NULL_PARG);
1377 if (ch_getflags() & CH_HELPFILE)
1379 if (strcmp(get_filename(curr_ifile), "-") == 0)
1381 error("Cannot edit standard input", NULL_PARG);
1384 if (curr_altfilename != NULL)
1386 error("WARNING: This file was viewed via LESSOPEN",
1389 start_mca(A_SHELL, "!", ml_shell, 0);
1391 * Expand the editor prototype string
1392 * and pass it to the system to execute.
1393 * (Make sure the screen is displayed so the
1394 * expansion of "+%lm" works.)
1398 lsystem(pr_expand(editproto, 0), (char*)NULL);
1401 error("Command not available", NULL_PARG);
1407 * Examine next file.
1412 error("No next file", NULL_PARG);
1418 if (edit_next((int) number))
1420 if (get_quit_at_eof() && hit_eof &&
1421 !(ch_getflags() & CH_HELPFILE))
1423 parg.p_string = (number > 1) ? "(N-th) " : "";
1424 error("No %snext file", &parg);
1430 * Examine previous file.
1435 error("No previous file", NULL_PARG);
1441 if (edit_prev((int) number))
1443 parg.p_string = (number > 1) ? "(N-th) " : "";
1444 error("No %sprevious file", &parg);
1452 tagfile = nexttag((int) number);
1453 if (tagfile == NULL)
1455 error("No next tag", NULL_PARG);
1458 if (edit(tagfile) == 0)
1460 POSITION pos = tagsearch();
1461 if (pos != NULL_POSITION)
1462 jump_loc(pos, jump_sline);
1465 error("Command not available", NULL_PARG);
1473 tagfile = prevtag((int) number);
1474 if (tagfile == NULL)
1476 error("No previous 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);
1492 * Examine a particular file.
1496 if (edit_index((int) number))
1497 error("No such file", NULL_PARG);
1501 if (ch_getflags() & CH_HELPFILE)
1503 old_ifile = curr_ifile;
1504 new_ifile = getoff_ifile(curr_ifile);
1505 if (new_ifile == NULL_IFILE)
1510 if (edit_ifile(new_ifile) != 0)
1512 reedit_ifile(old_ifile);
1515 del_ifile(old_ifile);
1519 optflag = OPT_TOGGLE;
1527 * Report a flag setting.
1529 optflag = OPT_NO_TOGGLE;
1537 * Set an initial command for new files.
1539 start_mca(A_FIRSTCMD, "+", (void*)NULL, 0);
1550 error("Command not available", NULL_PARG);
1553 start_mca(A_SHELL, "!", ml_shell, 0);
1557 error("Command not available", NULL_PARG);
1565 if (ch_getflags() & CH_HELPFILE)
1567 start_mca(A_SETMARK, "mark: ", (void*)NULL, 0);
1569 if (c == erase_char || c == erase2_char ||
1570 c == kill_char || c == '\n' || c == '\r')
1579 start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0);
1581 if (c == erase_char || c == erase2_char ||
1582 c == kill_char || c == '\n' || c == '\r')
1591 error("Command not available", NULL_PARG);
1594 start_mca(A_PIPE, "|mark: ", (void*)NULL, 0);
1596 if (c == erase_char || c == erase2_char || c == kill_char)
1598 if (c == '\n' || c == '\r')
1603 start_mca(A_PIPE, "!", ml_shell, 0);
1607 error("Command not available", NULL_PARG);
1613 start_mca(action, "Brackets: ", (void*)NULL, 0);
1619 shift_count = number;
1621 number = (shift_count > 0) ?
1622 shift_count : sc_width / 2;
1623 if (number > hshift)
1631 shift_count = number;
1633 number = (shift_count > 0) ?
1634 shift_count : sc_width / 2;
1641 * The command is incomplete (more chars are needed).
1642 * Display the current char, so the user knows
1643 * what's going on, and get another character.
1645 if (mca != A_PREFIX)
1648 start_mca(A_PREFIX, " ", (void*)NULL,