minor rewording, no fixes
[unix-history] / usr / src / usr.bin / more / command.c
CommitLineData
bfe13c81
KB
1/*
2 * Copyright (c) 1988 Mark Nudleman
3 * Copyright (c) 1988 Regents of the University of California.
4 * All rights reserved.
5 *
f15db449 6 * %sccs.include.redist.c%
bfe13c81
KB
7 */
8
9#ifndef lint
bc0c28c1 10static char sccsid[] = "@(#)command.c 5.21 (Berkeley) %G%";
bfe13c81
KB
11#endif /* not lint */
12
bc258617
KB
13#include <sys/param.h>
14#include <stdio.h>
15#include <ctype.h>
16#include <less.h>
f5506e0f 17#include "pathnames.h"
bfe13c81
KB
18
19#define NO_MCA 0
20#define MCA_DONE 1
21#define MCA_MORE 2
22
bc258617 23extern int erase_char, kill_char, werase_char;
bfe13c81
KB
24extern int ispipe;
25extern int sigs;
26extern int quit_at_eof;
27extern int hit_eof;
28extern int sc_width;
29extern int sc_height;
30extern int sc_window;
31extern int curr_ac;
32extern int ac;
33extern int quitting;
34extern int scroll;
bfe13c81
KB
35extern int screen_trashed; /* The screen has been overwritten */
36
37static char cmdbuf[120]; /* Buffer for holding a multi-char command */
bfe13c81
KB
38static char *cp; /* Pointer into cmdbuf */
39static int cmd_col; /* Current column of the multi-char command */
bc258617 40static int longprompt; /* if stat command instead of prompt */
bfe13c81
KB
41static int mca; /* The multicharacter command (action) */
42static int last_mca; /* The previous mca */
43static int number; /* The number typed by the user */
44static int wsearch; /* Search for matches (1) or non-matches (0) */
45
dce1e0a2
KB
46#define CMD_RESET cp = cmdbuf /* reset command buffer to empty */
47#define CMD_EXEC lower_left(); flush()
bfe13c81 48
dce1e0a2 49/* backspace in command buffer. */
bc258617 50static
bfe13c81
KB
51cmd_erase()
52{
dce1e0a2
KB
53 /*
54 * backspace past beginning of the string: this usually means
55 * abort the command.
56 */
bfe13c81 57 if (cp == cmdbuf)
bc258617 58 return(1);
bfe13c81 59
dce1e0a2 60 /* erase an extra character, for the carat. */
bc258617 61 if (CONTROL_CHAR(*--cp)) {
bfe13c81 62 backspace();
bc258617 63 --cmd_col;
bfe13c81 64 }
dce1e0a2 65
bfe13c81 66 backspace();
bc258617
KB
67 --cmd_col;
68 return(0);
bfe13c81
KB
69}
70
dce1e0a2 71/* set up the display to start a new multi-character command. */
bfe13c81
KB
72start_mca(action, prompt)
73 int action;
74 char *prompt;
75{
76 lower_left();
77 clear_eol();
78 putstr(prompt);
79 cmd_col = strlen(prompt);
80 mca = action;
81}
82
83/*
dce1e0a2 84 * process a single character of a multi-character command, such as
bfe13c81
KB
85 * a number, or the pattern of a search command.
86 */
bc258617 87static
bfe13c81
KB
88cmd_char(c)
89 int c;
90{
91 if (c == erase_char)
bc258617
KB
92 return(cmd_erase());
93 /* in this order, in case werase == erase_char */
94 if (c == werase_char) {
95 if (cp > cmdbuf) {
96 while (isspace(cp[-1]) && !cmd_erase());
97 while (!isspace(cp[-1]) && !cmd_erase());
98 while (isspace(cp[-1]) && !cmd_erase());
99 }
100 return(cp == cmdbuf);
101 }
102 if (c == kill_char) {
103 while (!cmd_erase());
104 return(1);
105 }
106 /*
107 * No room in the command buffer, or no room on the screen;
108 * {{ Could get fancy here; maybe shift the displayed line
109 * and make room for more chars, like ksh. }}
110 */
111 if (cp >= &cmdbuf[sizeof(cmdbuf)-1] || cmd_col >= sc_width-3)
bfe13c81 112 bell();
bc258617 113 else {
bfe13c81 114 *cp++ = c;
bc258617 115 if (CONTROL_CHAR(c)) {
bfe13c81
KB
116 putchr('^');
117 cmd_col++;
bc258617 118 c = CARAT_CHAR(c);
bfe13c81
KB
119 }
120 putchr(c);
121 cmd_col++;
122 }
bc258617 123 return(0);
bfe13c81
KB
124}
125
bfe13c81
KB
126prompt()
127{
00f91e6d 128 extern int linenums, short_file;
7a3f847f 129 extern char *current_name, *firstsearch, *next_name;
bc258617
KB
130 off_t len, pos, ch_length(), position(), forw_line();
131 char pbuf[40];
bfe13c81
KB
132
133 /*
bc258617
KB
134 * if nothing is displayed yet, display starting from line 1;
135 * if search string provided, go there instead.
bfe13c81 136 */
bc258617
KB
137 if (position(TOP) == NULL_POSITION) {
138 if (forw_line((off_t)0) == NULL_POSITION)
139 return(0);
140 if (!firstsearch || !search(1, firstsearch, 1, 1))
141 jump_back(1);
142 }
bfe13c81
KB
143 else if (screen_trashed)
144 repaint();
145
bc258617 146 /* if no -e flag and we've hit EOF on the last file, quit. */
00f91e6d 147 if ((!quit_at_eof || short_file) && hit_eof && curr_ac + 1 >= ac)
bfe13c81
KB
148 quit();
149
bc258617 150 /* select the proper prompt and display it. */
bfe13c81
KB
151 lower_left();
152 clear_eol();
bc258617
KB
153 if (longprompt) {
154 so_enter();
155 putstr(current_name);
156 putstr(":");
157 if (!ispipe) {
158 (void)sprintf(pbuf, " file %d/%d", curr_ac + 1, ac);
159 putstr(pbuf);
160 }
161 if (linenums) {
6d713a99 162 (void)sprintf(pbuf, " line %d", currline(BOTTOM));
bc258617
KB
163 putstr(pbuf);
164 }
6d713a99 165 if ((pos = position(BOTTOM)) != NULL_POSITION) {
bc258617
KB
166 (void)sprintf(pbuf, " byte %ld", pos);
167 putstr(pbuf);
168 if (!ispipe && (len = ch_length())) {
6d713a99
KB
169 (void)sprintf(pbuf, "/%ld pct %ld%%",
170 len, ((100 * pos) / len));
bc258617
KB
171 putstr(pbuf);
172 }
173 }
174 so_exit();
175 longprompt = 0;
176 }
bc258617 177 else {
bfe13c81 178 so_enter();
bc258617
KB
179 putstr(current_name);
180 if (hit_eof)
7a3f847f 181 if (next_name) {
d93c9cef
CL
182 putstr(": END (next file: ");
183 putstr(next_name);
184 putstr(")");
7a3f847f
KB
185 }
186 else
187 putstr(": END");
e80c9812
KB
188 else if (!ispipe &&
189 (pos = position(BOTTOM)) != NULL_POSITION &&
190 (len = ch_length())) {
191 (void)sprintf(pbuf, " (%ld%%)", ((100 * pos) / len));
bc258617
KB
192 putstr(pbuf);
193 }
bfe13c81
KB
194 so_exit();
195 }
bc258617 196 return(1);
bfe13c81
KB
197}
198
dce1e0a2 199/* get command character. */
bc258617 200static
bfe13c81
KB
201getcc()
202{
62b94f40
KB
203 extern int cmdstack;
204 int ch;
205
206 /* left over from error() routine. */
207 if (cmdstack) {
208 ch = cmdstack;
209 cmdstack = NULL;
210 return(ch);
211 }
bc258617 212 if (cp > cmdbuf && position(TOP) == NULL_POSITION) {
bfe13c81 213 /*
bc258617
KB
214 * Command is incomplete, so try to complete it.
215 * There are only two cases:
216 * 1. We have "/string" but no newline. Add the \n.
217 * 2. We have a number but no command. Treat as #g.
218 * (This is all pretty hokey.)
bfe13c81 219 */
bc258617
KB
220 if (mca != A_DIGIT)
221 /* Not a number; must be search string */
222 return('\n');
223 else
224 /* A number; append a 'g' */
225 return('g');
bfe13c81 226 }
bc258617 227 return(getchr());
bfe13c81
KB
228}
229
dce1e0a2 230/* execute a multicharacter command. */
bc258617 231static
bfe13c81
KB
232exec_mca()
233{
bc258617
KB
234 extern int file;
235 extern char *tagfile;
bfe13c81 236 register char *p;
bc258617 237 char *glob();
bfe13c81
KB
238
239 *cp = '\0';
dce1e0a2 240 CMD_EXEC;
bc258617 241 switch (mca) {
bfe13c81 242 case A_F_SEARCH:
bc258617 243 (void)search(1, cmdbuf, number, wsearch);
bfe13c81
KB
244 break;
245 case A_B_SEARCH:
bc258617 246 (void)search(0, cmdbuf, number, wsearch);
bfe13c81
KB
247 break;
248 case A_EXAMINE:
bc258617
KB
249 for (p = cmdbuf; isspace(*p); ++p);
250 (void)edit(glob(p));
251 break;
252 case A_TAGFILE:
253 for (p = cmdbuf; isspace(*p); ++p);
254 findtag(p);
255 if (tagfile == NULL)
256 break;
257 if (edit(tagfile))
258 (void)tagsearch();
bfe13c81 259 break;
bfe13c81
KB
260 }
261}
262
dce1e0a2 263/* add a character to a multi-character command. */
bc258617 264static
bfe13c81
KB
265mca_char(c)
266 int c;
267{
bc258617
KB
268 switch (mca) {
269 case 0: /* not in a multicharacter command. */
270 case A_PREFIX: /* in the prefix of a command. */
271 return(NO_MCA);
bfe13c81
KB
272 case A_DIGIT:
273 /*
274 * Entering digits of a number.
275 * Terminated by a non-digit.
276 */
bc258617
KB
277 if (!isascii(c) || !isdigit(c) &&
278 c != erase_char && c != kill_char && c != werase_char) {
bfe13c81
KB
279 /*
280 * Not part of the number.
281 * Treat as a normal command character.
282 */
dce1e0a2
KB
283 *cp = '\0';
284 number = atoi(cmdbuf);
285 CMD_RESET;
bfe13c81 286 mca = 0;
bc258617 287 return(NO_MCA);
bfe13c81
KB
288 }
289 break;
290 }
291
292 /*
293 * Any other multicharacter command
294 * is terminated by a newline.
295 */
bc258617 296 if (c == '\n' || c == '\r') {
bfe13c81 297 exec_mca();
bc258617 298 return(MCA_DONE);
bfe13c81 299 }
dce1e0a2
KB
300
301 /* append the char to the command buffer. */
bfe13c81 302 if (cmd_char(c))
bc258617 303 return(MCA_DONE);
dce1e0a2 304
bc258617 305 return(MCA_MORE);
bfe13c81
KB
306}
307
308/*
309 * Main command processor.
310 * Accept and execute commands until a quit command, then return.
311 */
bfe13c81
KB
312commands()
313{
314 register int c;
315 register int action;
316
317 last_mca = 0;
318 scroll = (sc_height + 1) / 2;
319
bc258617 320 for (;;) {
bfe13c81
KB
321 mca = 0;
322 number = 0;
323
324 /*
325 * See if any signals need processing.
326 */
bc258617 327 if (sigs) {
bfe13c81
KB
328 psignals();
329 if (quitting)
330 quit();
331 }
bfe13c81
KB
332 /*
333 * Display prompt and accept a character.
334 */
dce1e0a2 335 CMD_RESET;
bc258617
KB
336 if (!prompt()) {
337 next_file(1);
338 continue;
339 }
bfe13c81
KB
340 noprefix();
341 c = getcc();
342
f5167107 343again: if (sigs)
bfe13c81
KB
344 continue;
345
346 /*
347 * If we are in a multicharacter command, call mca_char.
348 * Otherwise we call cmd_decode to determine the
349 * action to be performed.
350 */
351 if (mca)
bc258617 352 switch (mca_char(c)) {
bfe13c81
KB
353 case MCA_MORE:
354 /*
355 * Need another character.
356 */
357 c = getcc();
358 goto again;
359 case MCA_DONE:
360 /*
361 * Command has been handled by mca_char.
362 * Start clean with a prompt.
363 */
364 continue;
365 case NO_MCA:
366 /*
367 * Not a multi-char command
368 * (at least, not anymore).
369 */
370 break;
371 }
372
dce1e0a2
KB
373 /* decode the command character and decide what to do. */
374 switch (action = cmd_decode(c)) {
375 case A_DIGIT: /* first digit of a number */
bfe13c81
KB
376 start_mca(A_DIGIT, ":");
377 goto again;
dce1e0a2
KB
378 case A_F_SCREEN: /* forward one screen */
379 CMD_EXEC;
380 if (number <= 0 && (number = sc_window) <= 0)
bfe13c81 381 number = sc_height - 1;
bfe13c81
KB
382 forward(number, 1);
383 break;
dce1e0a2
KB
384 case A_B_SCREEN: /* backward one screen */
385 CMD_EXEC;
386 if (number <= 0 && (number = sc_window) <= 0)
bfe13c81 387 number = sc_height - 1;
bfe13c81
KB
388 backward(number, 1);
389 break;
dce1e0a2
KB
390 case A_F_LINE: /* forward N (default 1) line */
391 CMD_EXEC;
392 forward(number <= 0 ? 1 : number, 0);
bfe13c81 393 break;
dce1e0a2
KB
394 case A_B_LINE: /* backward N (default 1) line */
395 CMD_EXEC;
396 backward(number <= 0 ? 1 : number, 0);
bfe13c81 397 break;
dce1e0a2
KB
398 case A_F_SCROLL: /* forward N lines */
399 CMD_EXEC;
bfe13c81
KB
400 if (number > 0)
401 scroll = number;
bfe13c81
KB
402 forward(scroll, 0);
403 break;
dce1e0a2
KB
404 case A_B_SCROLL: /* backward N lines */
405 CMD_EXEC;
bfe13c81
KB
406 if (number > 0)
407 scroll = number;
bfe13c81
KB
408 backward(scroll, 0);
409 break;
dce1e0a2
KB
410 case A_FREPAINT: /* flush buffers and repaint */
411 if (!ispipe) {
bfe13c81
KB
412 ch_init(0, 0);
413 clr_linenum();
414 }
dce1e0a2 415 /* FALLTHROUGH */
e810d3c9 416 case A_REPAINT: /* repaint the screen */
dce1e0a2 417 CMD_EXEC;
bfe13c81
KB
418 repaint();
419 break;
dce1e0a2
KB
420 case A_GOLINE: /* go to line N, default 1 */
421 CMD_EXEC;
bfe13c81
KB
422 if (number <= 0)
423 number = 1;
bfe13c81
KB
424 jump_back(number);
425 break;
dce1e0a2
KB
426 case A_PERCENT: /* go to percent of file */
427 CMD_EXEC;
bfe13c81
KB
428 if (number < 0)
429 number = 0;
dce1e0a2 430 else if (number > 100)
bfe13c81 431 number = 100;
bfe13c81
KB
432 jump_percent(number);
433 break;
dce1e0a2
KB
434 case A_GOEND: /* go to line N, default end */
435 CMD_EXEC;
bfe13c81
KB
436 if (number <= 0)
437 jump_forw();
438 else
439 jump_back(number);
440 break;
bc258617
KB
441 case A_STAT: /* print file name, etc. */
442 longprompt = 1;
443 continue;
bc258617 444 case A_QUIT: /* exit */
bfe13c81 445 quit();
dce1e0a2 446 case A_F_SEARCH: /* search for a pattern */
bfe13c81 447 case A_B_SEARCH:
bfe13c81
KB
448 if (number <= 0)
449 number = 1;
450 start_mca(action, (action==A_F_SEARCH) ? "/" : "?");
451 last_mca = mca;
452 wsearch = 1;
453 c = getcc();
bc258617 454 if (c == '!') {
bfe13c81 455 /*
dce1e0a2
KB
456 * Invert the sense of the search; set wsearch
457 * to 0 and get a new character for the start
458 * of the pattern.
bfe13c81
KB
459 */
460 start_mca(action,
dce1e0a2 461 (action == A_F_SEARCH) ? "!/" : "!?");
bfe13c81
KB
462 wsearch = 0;
463 c = getcc();
464 }
465 goto again;
dce1e0a2 466 case A_AGAIN_SEARCH: /* repeat previous search */
bfe13c81
KB
467 if (number <= 0)
468 number = 1;
469 if (wsearch)
470 start_mca(last_mca,
dce1e0a2 471 (last_mca == A_F_SEARCH) ? "/" : "?");
bfe13c81
KB
472 else
473 start_mca(last_mca,
dce1e0a2
KB
474 (last_mca == A_F_SEARCH) ? "!/" : "!?");
475 CMD_EXEC;
bc258617
KB
476 (void)search(mca == A_F_SEARCH, (char *)NULL,
477 number, wsearch);
bfe13c81 478 break;
dce1e0a2 479 case A_HELP: /* help */
bfe13c81
KB
480 lower_left();
481 clear_eol();
482 putstr("help");
dce1e0a2 483 CMD_EXEC;
bfe13c81
KB
484 help();
485 break;
dce1e0a2
KB
486 case A_TAGFILE: /* tag a new file */
487 CMD_RESET;
bc258617
KB
488 start_mca(A_TAGFILE, "Tag: ");
489 c = getcc();
490 goto again;
dce1e0a2
KB
491 case A_FILE_LIST: /* show list of file names */
492 CMD_EXEC;
e810d3c9
KB
493 showlist();
494 repaint();
495 break;
dce1e0a2
KB
496 case A_EXAMINE: /* edit a new file */
497 CMD_RESET;
bfe13c81
KB
498 start_mca(A_EXAMINE, "Examine: ");
499 c = getcc();
500 goto again;
dce1e0a2
KB
501 case A_VISUAL: /* invoke the editor */
502 if (ispipe) {
bfe13c81
KB
503 error("Cannot edit standard input");
504 break;
505 }
dce1e0a2 506 CMD_EXEC;
1a2c8d49 507 editfile();
bfe13c81
KB
508 ch_init(0, 0);
509 clr_linenum();
510 break;
dce1e0a2 511 case A_NEXT_FILE: /* examine next file */
bfe13c81
KB
512 if (number <= 0)
513 number = 1;
514 next_file(number);
515 break;
dce1e0a2 516 case A_PREV_FILE: /* examine previous file */
bfe13c81
KB
517 if (number <= 0)
518 number = 1;
519 prev_file(number);
520 break;
dce1e0a2 521 case A_SETMARK: /* set a mark */
bfe13c81
KB
522 lower_left();
523 clear_eol();
524 start_mca(A_SETMARK, "mark: ");
525 c = getcc();
526 if (c == erase_char || c == kill_char)
527 break;
528 setmark(c);
529 break;
dce1e0a2 530 case A_GOMARK: /* go to mark */
bfe13c81
KB
531 lower_left();
532 clear_eol();
533 start_mca(A_GOMARK, "goto mark: ");
534 c = getcc();
535 if (c == erase_char || c == kill_char)
536 break;
537 gomark(c);
538 break;
bfe13c81
KB
539 case A_PREFIX:
540 /*
541 * The command is incomplete (more chars are needed).
dce1e0a2
KB
542 * Display the current char so the user knows what's
543 * going on and get another character.
bfe13c81
KB
544 */
545 if (mca != A_PREFIX)
dce1e0a2
KB
546 start_mca(A_PREFIX, "");
547 if (CONTROL_CHAR(c)) {
bfe13c81 548 putchr('^');
bc258617 549 c = CARAT_CHAR(c);
bfe13c81
KB
550 }
551 putchr(c);
552 c = getcc();
553 goto again;
bfe13c81
KB
554 default:
555 bell();
556 break;
557 }
558 }
559}
1a2c8d49 560
1a2c8d49
KB
561editfile()
562{
bc258617 563 extern char *current_file;
1a2c8d49
KB
564 static int dolinenumber;
565 static char *editor;
566 int c;
bc258617 567 char buf[MAXPATHLEN * 2 + 20], *getenv();
1a2c8d49
KB
568
569 if (editor == NULL) {
570 editor = getenv("EDITOR");
571 /* pass the line number to vi */
572 if (editor == NULL || *editor == '\0') {
f5506e0f 573 editor = _PATH_VI;
1a2c8d49
KB
574 dolinenumber = 1;
575 }
576 else
577 dolinenumber = 0;
578 }
579 if (dolinenumber && (c = currline(MIDDLE)))
580 (void)sprintf(buf, "%s +%d %s", editor, c, current_file);
581 else
582 (void)sprintf(buf, "%s %s", editor, current_file);
583 lsystem(buf);
584}
e810d3c9 585
e810d3c9
KB
586showlist()
587{
588 extern int sc_width;
589 extern char **av;
590 register int indx, width;
591 int len;
592 char *p;
593
594 if (ac <= 0) {
595 error("No files provided as arguments.");
596 return;
597 }
598 for (width = indx = 0; indx < ac;) {
599 p = strcmp(av[indx], "-") ? av[indx] : "stdin";
600 len = strlen(p) + 1;
601 if (curr_ac == indx)
602 len += 2;
603 if (width + len + 1 >= sc_width) {
604 if (!width) {
605 if (curr_ac == indx)
606 putchr('[');
607 putstr(p);
608 if (curr_ac == indx)
609 putchr(']');
610 ++indx;
611 }
612 width = 0;
613 putchr('\n');
614 continue;
615 }
616 if (width)
617 putchr(' ');
618 if (curr_ac == indx)
619 putchr('[');
620 putstr(p);
621 if (curr_ac == indx)
622 putchr(']');
623 width += len;
624 ++indx;
625 }
626 putchr('\n');
627 error((char *)NULL);
628}