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