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