2
* Copyright (C) 1984-2000 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 int 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, 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, number);
213
match_brac(cbuf[1], cbuf[0], 0, number);
220
/* If tag structure is loaded then clean it up. */
227
* !! just uses whatever is in shellcmd.
228
* Otherwise, copy cmdbuf to shellcmd,
229
* expanding any special characters ("%" or "#").
233
if (shellcmd != NULL)
235
shellcmd = fexpand(cbuf);
240
if (shellcmd == NULL)
241
lsystem("", "!done");
243
lsystem(shellcmd, "!done");
250
(void) pipe_mark(pipec, cbuf);
251
error("|done", NULL_PARG);
258
* Add a character to a multi-character command.
273
* Not in a multicharacter command.
279
* In the prefix of a command.
280
* This not considered a multichar command
281
* (even tho it uses cmdbuf, etc.).
282
* It is handled in the commands() switch.
288
* Entering digits of a number.
289
* Terminated by a non-digit.
291
if ((c < '0' || c > '9') &&
292
editchar(c, EC_PEEK|EC_NOHISTORY|EC_NOCOMPLETE|EC_NORIGHTLEFT) == A_INVALID)
295
* Not part of the number.
296
* Treat as a normal command character.
307
* Special case for the TOGGLE_OPTION command.
308
* If the option letter which was entered is a
309
* single-char option, execute the command immediately,
310
* so user doesn't have to hit RETURN.
311
* If the first char is + or -, this indicates
312
* OPT_UNSET or OPT_SET respectively, instead of OPT_TOGGLE.
313
* "--" begins inputting a long option name.
315
if (optchar == '\0' && len_cmdbuf() == 0)
317
flag = (optflag & ~OPT_NO_PROMPT);
318
if (flag == OPT_NO_TOGGLE)
323
/* "__" = long option name. */
334
optflag = (flag == OPT_UNSET) ?
335
OPT_TOGGLE : OPT_UNSET;
340
optflag = (flag == OPT_SET) ?
341
OPT_TOGGLE : OPT_SET;
345
optflag ^= OPT_NO_PROMPT;
349
/* "--" = long option name. */
359
* We're getting a long option name.
360
* See if we've matched an option name yet.
361
* If so, display the complete name and stop
362
* accepting chars until user hits RETURN.
368
if (c == '\n' || c == '\r')
371
* When the user hits RETURN, make sure
372
* we've matched an option name, then
373
* pretend he just entered the equivalent
378
parg.p_string = get_cmdbuf();
379
error("There is no --%s option", &parg);
390
* Already have a match for the name.
391
* Don't accept anything but erase/kill.
393
if (c == erase_char || c == kill_char)
398
* Add char to cmd buffer and try to match
401
if (cmd_char(c) == CC_QUIT)
405
o = findopt_name(&p, &oname, NULL);
410
* Remember the option letter and
411
* display the full option name.
413
optchar = o->oletter;
414
if (!lc && islower(optchar))
415
optchar = toupper(optchar);
418
for (p = oname; *p != '\0'; p++)
421
if (!lc && islower(c))
423
if (cmd_char(c) != CC_OK)
431
if (c == erase_char || c == kill_char)
434
/* We already have the option letter. */
439
if ((optflag & ~OPT_NO_PROMPT) != OPT_TOGGLE ||
440
single_char_option(c))
442
toggle_option(c, "", optflag);
446
* Display a prompt appropriate for the option letter.
448
if ((p = opt_prompt(c)) == NULL)
455
start_mca(A_OPT_TOGGLE, p, (void*)NULL, 0);
461
* Special case for search commands.
462
* Certain characters as the first char of
463
* the pattern have special meaning:
464
* ! Toggle the NO_MATCH flag
465
* * Toggle the PAST_EOF flag
466
* @ Toggle the FIRST_FILE flag
468
if (len_cmdbuf() > 0)
470
* Only works for the first char of the pattern.
477
case CONTROL('E'): /* ignore END of file */
479
flag = SRCH_PAST_EOF;
481
case CONTROL('F'): /* FIRST file */
483
flag = SRCH_FIRST_FILE;
485
case CONTROL('K'): /* KEEP position */
488
case CONTROL('R'): /* Don't use REGULAR EXPRESSIONS */
489
flag = SRCH_NO_REGEX;
491
case CONTROL('N'): /* NOT match */
493
flag = SRCH_NO_MATCH;
506
* Any other multicharacter command
507
* is terminated by a newline.
509
if (c == '\n' || c == '\r')
512
* Execute the command.
519
* Append the char to the command buffer.
521
if (cmd_char(c) == CC_QUIT)
523
* Abort the multi-char command.
527
if ((mca == A_F_BRACKET || mca == A_B_BRACKET) && len_cmdbuf() >= 2)
530
* Special case for the bracket-matching commands.
531
* Execute the command after getting exactly two
532
* characters from the user.
539
* Need another character.
545
* Make sure the screen is displayed.
551
* If nothing is displayed yet, display starting from initial_scrpos.
555
if (initial_scrpos.pos == NULL_POSITION)
557
* {{ Maybe this should be:
558
* jump_loc(ch_zero(), jump_sline);
559
* but this behavior seems rather unexpected
560
* on the first screen. }}
562
jump_loc(ch_zero(), 1);
564
jump_loc(initial_scrpos.pos, initial_scrpos.ln);
565
} else if (screen_trashed)
568
save_top_scroll = top_scroll;
571
top_scroll = save_top_scroll;
576
* Display the appropriate prompt.
583
if (ungotp != NULL && ungotp > ungot)
586
* No prompt necessary if commands are from
587
* ungotten chars rather than from the user.
593
* Make sure the screen is displayed.
596
bottompos = position(BOTTOM_PLUS_ONE);
599
* If the -E flag is set and we've hit EOF on the last file, quit.
601
if ((quit_at_eof == OPT_ONPLUS || quit_if_one_screen) &&
602
hit_eof && !(ch_getflags() & CH_HELPFILE) &&
603
next_ifile(curr_ifile) == NULL_IFILE)
605
quit_if_one_screen = FALSE;
606
#if 0 /* This doesn't work well because some "te"s clear the screen. */
608
* If the -e flag is set and we've hit EOF on the last file,
609
* and the file is squished (shorter than the screen), quit.
611
if (quit_at_eof && squished &&
612
next_ifile(curr_ifile) == NULL_IFILE)
616
#if MSDOS_COMPILER==WIN32C
618
* In Win32, display the file name in the window title.
620
if (!(ch_getflags() & CH_HELPFILE))
621
SetConsoleTitle(pr_expand("Less?f - %f.", 0));
624
* Select the proper prompt and display it.
639
* Display the less version message.
646
parg.p_string = version;
647
error("less %s", &parg);
651
* Get command character.
652
* The character normally comes from the keyboard,
653
* but may come from ungotten characters
654
* (characters previously given to ungetcc or ungetsc).
661
* Normal case: no ungotten chars, so get one from the user.
667
* Return the next ungotten char.
672
* We have just run out of ungotten chars.
675
if (len_cmdbuf() == 0 || !empty_screen())
678
* Command is incomplete, so try to complete it.
684
* We have a number but no command. Treat as #g.
691
* We have "/string" but no newline. Add the \n.
697
* Some other incomplete command. Let user complete it.
704
* "Unget" a command character.
705
* The next getcc() will return this character.
713
if (ungotp >= ungot + sizeof(ungot))
715
error("ungetcc overflow", NULL_PARG);
722
* Unget a whole string of command characters.
723
* The next sequence of getcc()'s will return this string.
731
for (p = s + strlen(s) - 1; p >= s; p--)
736
* Search for a pattern, possibly in multiple files.
737
* If SRCH_FIRST_FILE is set, begin searching at the first file.
738
* If SRCH_PAST_EOF is set, continue the search thru multiple files.
741
multi_search(pattern, n)
750
save_ifile = save_curr_ifile();
752
if (search_type & SRCH_FIRST_FILE)
755
* Start at the first (or last) file
756
* in the command line list.
758
if (search_type & SRCH_FORW)
759
nomore = edit_first();
761
nomore = edit_last();
764
unsave_ifile(save_ifile);
768
search_type &= ~SRCH_FIRST_FILE;
773
n = search(search_type, pattern, n);
775
* The SRCH_NO_MOVE flag doesn't "stick": it gets cleared
776
* after being used once. This allows "n" to work after
777
* using a /@@ search.
779
search_type &= ~SRCH_NO_MOVE;
785
unsave_ifile(save_ifile);
791
* Some kind of error in the search.
792
* Error message has been printed by search().
796
if ((search_type & SRCH_PAST_EOF) == 0)
798
* We didn't find a match, but we're
799
* supposed to search only one file.
803
* Move on to the next file.
805
if (search_type & SRCH_FORW)
806
nomore = edit_next(1);
808
nomore = edit_prev(1);
816
* Print an error message if we haven't already.
819
error("Pattern not found", NULL_PARG);
824
* Restore the file we were originally viewing.
826
reedit_ifile(save_ifile);
831
* Main command processor.
832
* Accept and execute commands until a quit command.
841
int save_search_type;
849
search_type = SRCH_FORW;
850
wscroll = (sc_height + 1) / 2;
851
newaction = A_NOACTION;
861
* See if any signals need processing.
867
quit(QUIT_SAVED_STATUS);
871
* See if window size changed, for systems that don't
877
* Display prompt and accept a character.
883
if (newaction == A_NOACTION)
890
if (newaction != A_NOACTION)
893
newaction = A_NOACTION;
897
* If we are in a multicharacter command, call mca_char.
898
* Otherwise we call fcmd_decode to determine the
899
* action to be performed.
906
* Need another character.
912
* Command has been handled by mca_char.
913
* Start clean with a prompt.
918
* Not a multi-char command
919
* (at least, not anymore).
925
* Decode the command character and decide what to do.
930
* We're in a multichar command.
931
* Add the character to the command buffer
932
* and display it on the screen.
933
* If the user backspaces past the start
934
* of the line, abort the command.
936
if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0)
942
* Don't use cmd_char if we're starting fresh
943
* at the beginning of a command, because we
944
* don't want to echo the command until we know
945
* it is a multichar command. We also don't
946
* want erase_char/kill_char to be treated
947
* as line editing characters.
954
action = fcmd_decode(cbuf, &extra);
956
* If an "extra" string was returned,
957
* process it as a string of command characters.
963
* Clear the cmdbuf string.
964
* (But not if we're in the prefix of a command,
965
* because the partial command string is kept there.)
967
if (action != A_PREFIX)
974
* First digit of a number.
976
start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE);
981
* Forward one window (and set the window size).
988
* Forward one screen.
991
number = get_swindow();
994
set_attnpos(bottompos);
995
forward(number, 0, 1);
1000
* Backward one window (and set the window size).
1007
* Backward one screen.
1010
number = get_swindow();
1012
backward(number, 0, 1);
1017
* Forward N (default 1) line.
1022
if (show_attn == OPT_ONPLUS && number > 1)
1023
set_attnpos(bottompos);
1024
forward(number, 0, 0);
1029
* Backward N (default 1) line.
1034
backward(number, 0, 0);
1039
* Force forward N (default 1) line.
1044
if (show_attn == OPT_ONPLUS && number > 1)
1045
set_attnpos(bottompos);
1046
forward(number, 1, 0);
1051
* Force backward N (default 1) line.
1056
backward(number, 1, 0);
1061
* Force forward one screen.
1064
number = get_swindow();
1066
if (show_attn == OPT_ONPLUS)
1067
set_attnpos(bottompos);
1068
forward(number, 1, 0);
1073
* Forward forever, ignoring EOF.
1075
if (ch_getflags() & CH_HELPFILE)
1085
* This gets us back in "F mode" after processing
1086
* a non-abort signal (e.g. window-change).
1088
if (sigs && !ABORT_SIGS())
1089
newaction = A_F_FOREVER;
1095
* (default same as last 'd' or 'u' command).
1100
if (show_attn == OPT_ONPLUS)
1101
set_attnpos(bottompos);
1102
forward(wscroll, 0, 0);
1108
* (default same as last 'd' or 'u' command).
1113
backward(wscroll, 0, 0);
1118
* Flush buffers, then repaint screen.
1119
* Don't flush the buffers on a pipe!
1121
if (ch_getflags() & CH_CANSEEK)
1140
* Go to line N, default beginning of file.
1150
* Go to a specified percentage into the file.
1157
jump_percent(number);
1162
* Go to line N, default end of file.
1173
* Go to a specified byte position in the file.
1178
jump_line_loc((POSITION)number, jump_sline);
1183
* Print file name, etc.
1185
if (ch_getflags() & CH_HELPFILE)
1188
parg.p_string = eq_message();
1194
* Print version number, without the "@(#)".
1204
if (curr_ifile != NULL_IFILE &&
1205
ch_getflags() & CH_HELPFILE)
1208
* Quit while viewing the help file
1209
* just means return to viewing the
1212
if (edit_prev(1) == 0)
1221
* Define abbreviation for a commonly used sequence below.
1223
#define DO_SEARCH() if (number <= 0) number = 1; \
1226
multi_search((char *)NULL, number);
1231
* Search forward for a pattern.
1232
* Get the first char of the pattern.
1234
search_type = SRCH_FORW;
1243
* Search backward for a pattern.
1244
* Get the first char of the pattern.
1246
search_type = SRCH_BACK;
1253
case A_AGAIN_SEARCH:
1255
* Repeat previous search.
1260
case A_T_AGAIN_SEARCH:
1262
* Repeat previous search, multiple files.
1264
search_type |= SRCH_PAST_EOF;
1268
case A_REVERSE_SEARCH:
1270
* Repeat previous search, in reverse direction.
1272
save_search_type = search_type;
1273
search_type = SRCH_REVERSE(search_type);
1275
search_type = save_search_type;
1278
case A_T_REVERSE_SEARCH:
1280
* Repeat previous search,
1281
* multiple files in reverse direction.
1283
save_search_type = search_type;
1284
search_type = SRCH_REVERSE(search_type);
1285
search_type |= SRCH_PAST_EOF;
1287
search_type = save_search_type;
1298
if (ch_getflags() & CH_HELPFILE)
1301
(void) edit(FAKE_HELPFILE);
1307
* Edit a new file. Get the filename.
1311
error("Command not available", NULL_PARG);
1314
start_mca(A_EXAMINE, "Examine: ", ml_examine, 0);
1318
error("Command not available", NULL_PARG);
1324
* Invoke an editor on the input file.
1329
error("Command not available", NULL_PARG);
1332
if (ch_getflags() & CH_HELPFILE)
1334
if (strcmp(get_filename(curr_ifile), "-") == 0)
1336
error("Cannot edit standard input", NULL_PARG);
1339
if (curr_altfilename != NULL)
1341
error("Cannot edit file processed with LESSOPEN",
1345
start_mca(A_SHELL, "!", ml_shell, 0);
1347
* Expand the editor prototype string
1348
* and pass it to the system to execute.
1349
* (Make sure the screen is displayed so the
1350
* expansion of "+%lm" works.)
1354
lsystem(pr_expand(editproto, 0), (char*)NULL);
1357
error("Command not available", NULL_PARG);
1363
* Examine next file.
1367
error("No next file", NULL_PARG);
1372
if (edit_next(number))
1374
if (quit_at_eof && hit_eof &&
1375
!(ch_getflags() & CH_HELPFILE))
1377
parg.p_string = (number > 1) ? "(N-th) " : "";
1378
error("No %snext file", &parg);
1384
* Examine previous file.
1388
error("No previous file", NULL_PARG);
1393
if (edit_prev(number))
1395
parg.p_string = (number > 1) ? "(N-th) " : "";
1396
error("No %sprevious file", &parg);
1403
tagfile = nexttag(number);
1404
if (tagfile == NULL)
1406
error("No next tag", NULL_PARG);
1409
if (edit(tagfile) == 0)
1411
POSITION pos = tagsearch();
1412
if (pos != NULL_POSITION)
1413
jump_loc(pos, jump_sline);
1420
tagfile = prevtag(number);
1421
if (tagfile == NULL)
1423
error("No previous tag", NULL_PARG);
1426
if (edit(tagfile) == 0)
1428
POSITION pos = tagsearch();
1429
if (pos != NULL_POSITION)
1430
jump_loc(pos, jump_sline);
1436
* Examine a particular file.
1440
if (edit_index(number))
1441
error("No such file", NULL_PARG);
1445
if (ch_getflags() & CH_HELPFILE)
1447
old_ifile = curr_ifile;
1448
new_ifile = getoff_ifile(curr_ifile);
1449
if (new_ifile == NULL_IFILE)
1454
if (edit_ifile(new_ifile) != 0)
1456
reedit_ifile(old_ifile);
1459
del_ifile(old_ifile);
1463
optflag = OPT_TOGGLE;
1471
* Report a flag setting.
1473
optflag = OPT_NO_TOGGLE;
1481
* Set an initial command for new files.
1483
start_mca(A_FIRSTCMD, "+", (void*)NULL, 0);
1494
error("Command not available", NULL_PARG);
1497
start_mca(A_SHELL, "!", ml_shell, 0);
1501
error("Command not available", NULL_PARG);
1509
if (ch_getflags() & CH_HELPFILE)
1511
start_mca(A_SETMARK, "mark: ", (void*)NULL, 0);
1513
if (c == erase_char || c == kill_char ||
1514
c == '\n' || c == '\r')
1523
start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0);
1525
if (c == erase_char || c == kill_char ||
1526
c == '\n' || c == '\r')
1535
error("Command not available", NULL_PARG);
1538
start_mca(A_PIPE, "|mark: ", (void*)NULL, 0);
1540
if (c == erase_char || c == kill_char)
1542
if (c == '\n' || c == '\r')
1547
start_mca(A_PIPE, "!", ml_shell, 0);
1551
error("Command not available", NULL_PARG);
1557
start_mca(action, "Brackets: ", (void*)NULL, 0);
1563
shift_count = number;
1565
number = (shift_count > 0) ?
1566
shift_count : sc_width / 2;
1567
if (number > hshift)
1575
shift_count = number;
1577
number = (shift_count > 0) ?
1578
shift_count : sc_width / 2;
1585
* The command is incomplete (more chars are needed).
1586
* Display the current char, so the user knows
1587
* what's going on, and get another character.
1589
if (mca != A_PREFIX)
1592
start_mca(A_PREFIX, " ", (void*)NULL,