command.c revision 1.7 1 /* $NetBSD: command.c,v 1.7 2003/08/06 13:36:54 itojun 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.7 2003/08/06 13:36:54 itojun 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)snprintf(pbuf, sizeof(pbuf), " file %d/%d",
189 curr_ac + 1, ac);
190 putstr(pbuf);
191 }
192 if (linenums) {
193 (void)snprintf(pbuf, sizeof(pbuf), " line %d",
194 currline(BOTTOM));
195 putstr(pbuf);
196 }
197 if ((pos = position(BOTTOM)) != NULL_POSITION) {
198 (void)snprintf(pbuf, sizeof(pbuf), " byte %lld",
199 (long long)pos);
200 putstr(pbuf);
201 if (!ispipe && (len = ch_length())) {
202 (void)snprintf(pbuf, sizeof(pbuf),
203 "/%lld pct %lld%%", (long long)len,
204 (long long)((100 * pos) / len));
205 putstr(pbuf);
206 }
207 }
208 so_exit();
209 longprompt = 0;
210 }
211 else {
212 so_enter();
213 putstr(current_name);
214 if (hit_eof)
215 if (next_name) {
216 putstr(": END (next file: ");
217 putstr(next_name);
218 putstr(")");
219 }
220 else
221 putstr(": END");
222 else if (!ispipe &&
223 (pos = position(BOTTOM)) != NULL_POSITION &&
224 (len = ch_length())) {
225 (void)snprintf(pbuf, sizeof(pbuf), " (%lld%%)",
226 (long long)((100 * pos) / len));
227 putstr(pbuf);
228 }
229 so_exit();
230 }
231 return(1);
232 }
233
234 /* get command character. */
235 static int
236 getcc()
237 {
238 int ch;
239
240 /* left over from error() routine. */
241 if (cmdstack) {
242 ch = cmdstack;
243 cmdstack = NULL;
244 return(ch);
245 }
246 if (cp > cmdbuf && position(TOP) == NULL_POSITION) {
247 /*
248 * Command is incomplete, so try to complete it.
249 * There are only two cases:
250 * 1. We have "/string" but no newline. Add the \n.
251 * 2. We have a number but no command. Treat as #g.
252 * (This is all pretty hokey.)
253 */
254 if (mca != A_DIGIT)
255 /* Not a number; must be search string */
256 return('\n');
257 else
258 /* A number; append a 'g' */
259 return('g');
260 }
261 return(getchr());
262 }
263
264 /* execute a multicharacter command. */
265 static void
266 exec_mca()
267 {
268 char *p;
269
270 *cp = '\0';
271 CMD_EXEC;
272 switch (mca) {
273 case A_F_SEARCH:
274 (void)search(1, cmdbuf, number, wsearch);
275 break;
276 case A_B_SEARCH:
277 (void)search(0, cmdbuf, number, wsearch);
278 break;
279 case A_EXAMINE:
280 for (p = cmdbuf; isspace(*p); ++p);
281 (void)edit(glob(p));
282 break;
283 }
284 }
285
286 /* add a character to a multi-character command. */
287 static int
288 mca_char(c)
289 int c;
290 {
291 switch (mca) {
292 case 0: /* not in a multicharacter command. */
293 case A_PREFIX: /* in the prefix of a command. */
294 return(NO_MCA);
295 case A_DIGIT:
296 /*
297 * Entering digits of a number.
298 * Terminated by a non-digit.
299 */
300 if (!isascii(c) || (!isdigit(c) &&
301 c != erase_char && c != kill_char && c != werase_char)) {
302 /*
303 * Not part of the number.
304 * Treat as a normal command character.
305 */
306 *cp = '\0';
307 number = atoi(cmdbuf);
308 CMD_RESET;
309 mca = 0;
310 return(NO_MCA);
311 }
312 break;
313 }
314
315 /*
316 * Any other multicharacter command
317 * is terminated by a newline.
318 */
319 if (c == '\n' || c == '\r') {
320 exec_mca();
321 return(MCA_DONE);
322 }
323
324 /* append the char to the command buffer. */
325 if (cmd_char(c))
326 return(MCA_DONE);
327
328 return(MCA_MORE);
329 }
330
331 /*
332 * Main command processor.
333 * Accept and execute commands until a quit command, then return.
334 */
335 void
336 commands()
337 {
338 int c;
339 int action;
340
341 last_mca = 0;
342 scroll = (sc_height + 1) / 2;
343
344 for (;;) {
345 mca = 0;
346 number = 0;
347
348 /*
349 * See if any signals need processing.
350 */
351 if (sigs) {
352 psignals();
353 if (quitting)
354 quit();
355 }
356 /*
357 * Display prompt and accept a character.
358 */
359 CMD_RESET;
360 if (!prompt()) {
361 next_file(1);
362 continue;
363 }
364 noprefix();
365 c = getcc();
366
367 again: if (sigs)
368 continue;
369
370 /*
371 * If we are in a multicharacter command, call mca_char.
372 * Otherwise we call cmd_decode to determine the
373 * action to be performed.
374 */
375 if (mca)
376 switch (mca_char(c)) {
377 case MCA_MORE:
378 /*
379 * Need another character.
380 */
381 c = getcc();
382 goto again;
383 case MCA_DONE:
384 /*
385 * Command has been handled by mca_char.
386 * Start clean with a prompt.
387 */
388 continue;
389 case NO_MCA:
390 /*
391 * Not a multi-char command
392 * (at least, not anymore).
393 */
394 break;
395 }
396
397 /* decode the command character and decide what to do. */
398 switch (action = cmd_decode(c)) {
399 case A_DIGIT: /* first digit of a number */
400 start_mca(A_DIGIT, ":");
401 goto again;
402 case A_F_SCREEN: /* forward one screen */
403 CMD_EXEC;
404 if (number <= 0 && (number = sc_window) <= 0)
405 number = sc_height - 1;
406 forward(number, 1);
407 break;
408 case A_B_SCREEN: /* backward one screen */
409 CMD_EXEC;
410 if (number <= 0 && (number = sc_window) <= 0)
411 number = sc_height - 1;
412 backward(number, 1);
413 break;
414 case A_F_LINE: /* forward N (default 1) line */
415 CMD_EXEC;
416 forward(number <= 0 ? 1 : number, 0);
417 break;
418 case A_B_LINE: /* backward N (default 1) line */
419 CMD_EXEC;
420 backward(number <= 0 ? 1 : number, 0);
421 break;
422 case A_F_SCROLL: /* forward N lines */
423 CMD_EXEC;
424 if (number > 0)
425 scroll = number;
426 forward(scroll, 0);
427 break;
428 case A_B_SCROLL: /* backward N lines */
429 CMD_EXEC;
430 if (number > 0)
431 scroll = number;
432 backward(scroll, 0);
433 break;
434 case A_FREPAINT: /* flush buffers and repaint */
435 if (!ispipe) {
436 ch_init(0, 0);
437 clr_linenum();
438 }
439 /* FALLTHROUGH */
440 case A_REPAINT: /* repaint the screen */
441 CMD_EXEC;
442 repaint();
443 break;
444 case A_GOLINE: /* go to line N, default 1 */
445 CMD_EXEC;
446 if (number <= 0)
447 number = 1;
448 jump_back(number);
449 break;
450 case A_PERCENT: /* go to percent of file */
451 CMD_EXEC;
452 if (number < 0)
453 number = 0;
454 else if (number > 100)
455 number = 100;
456 jump_percent(number);
457 break;
458 case A_GOEND: /* go to line N, default end */
459 CMD_EXEC;
460 if (number <= 0)
461 jump_forw();
462 else
463 jump_back(number);
464 break;
465 case A_STAT: /* print file name, etc. */
466 longprompt = 1;
467 continue;
468 case A_QUIT: /* exit */
469 quit();
470 case A_F_SEARCH: /* search for a pattern */
471 case A_B_SEARCH:
472 if (number <= 0)
473 number = 1;
474 start_mca(action, (action==A_F_SEARCH) ? "/" : "?");
475 last_mca = mca;
476 wsearch = 1;
477 c = getcc();
478 if (c == '!') {
479 /*
480 * Invert the sense of the search; set wsearch
481 * to 0 and get a new character for the start
482 * of the pattern.
483 */
484 start_mca(action,
485 (action == A_F_SEARCH) ? "!/" : "!?");
486 wsearch = 0;
487 c = getcc();
488 }
489 goto again;
490 case A_AGAIN_SEARCH: /* repeat previous search */
491 if (number <= 0)
492 number = 1;
493 if (wsearch)
494 start_mca(last_mca,
495 (last_mca == A_F_SEARCH) ? "/" : "?");
496 else
497 start_mca(last_mca,
498 (last_mca == A_F_SEARCH) ? "!/" : "!?");
499 CMD_EXEC;
500 (void)search(mca == A_F_SEARCH, NULL,
501 number, wsearch);
502 break;
503 case A_HELP: /* help */
504 lower_left();
505 clear_eol();
506 putstr("help");
507 CMD_EXEC;
508 help();
509 break;
510 case A_FILE_LIST: /* show list of file names */
511 CMD_EXEC;
512 showlist();
513 repaint();
514 break;
515 case A_EXAMINE: /* edit a new file */
516 CMD_RESET;
517 start_mca(A_EXAMINE, "Examine: ");
518 c = getcc();
519 goto again;
520 case A_VISUAL: /* invoke the editor */
521 if (ispipe) {
522 error("Cannot edit standard input");
523 break;
524 }
525 CMD_EXEC;
526 editfile();
527 ch_init(0, 0);
528 clr_linenum();
529 break;
530 case A_NEXT_FILE: /* examine next file */
531 if (number <= 0)
532 number = 1;
533 next_file(number);
534 break;
535 case A_PREV_FILE: /* examine previous file */
536 if (number <= 0)
537 number = 1;
538 prev_file(number);
539 break;
540 case A_SETMARK: /* set a mark */
541 lower_left();
542 clear_eol();
543 start_mca(A_SETMARK, "mark: ");
544 c = getcc();
545 if (c == erase_char || c == kill_char)
546 break;
547 setmark(c);
548 break;
549 case A_GOMARK: /* go to mark */
550 lower_left();
551 clear_eol();
552 start_mca(A_GOMARK, "goto mark: ");
553 c = getcc();
554 if (c == erase_char || c == kill_char)
555 break;
556 gomark(c);
557 break;
558 case A_PREFIX:
559 /*
560 * The command is incomplete (more chars are needed).
561 * Display the current char so the user knows what's
562 * going on and get another character.
563 */
564 if (mca != A_PREFIX)
565 start_mca(A_PREFIX, "");
566 if (CONTROL_CHAR(c)) {
567 putchr('^');
568 c = CARAT_CHAR(c);
569 }
570 putchr(c);
571 c = getcc();
572 goto again;
573 default:
574 bell();
575 break;
576 }
577 }
578 }
579
580 void
581 editfile()
582 {
583 static int dolinenumber;
584 static char *editor;
585 int c;
586 char buf[MAXPATHLEN * 2 + 20];
587
588 if (editor == NULL) {
589 editor = getenv("EDITOR");
590 /* pass the line number to vi */
591 if (editor == NULL || *editor == '\0') {
592 editor = _PATH_VI;
593 dolinenumber = 1;
594 }
595 else
596 dolinenumber = 0;
597 }
598 if (dolinenumber && (c = currline(MIDDLE)))
599 (void)snprintf(buf, sizeof(buf), "%s +%d %s", editor, c,
600 current_file);
601 else
602 (void)snprintf(buf, sizeof(buf), "%s %s", editor, current_file);
603 lsystem(buf);
604 }
605
606 void
607 showlist()
608 {
609 int indx, width;
610 int len;
611 char *p;
612
613 if (ac <= 0) {
614 error("No files provided as arguments.");
615 return;
616 }
617 for (width = indx = 0; indx < ac;) {
618 p = strcmp(av[indx], "-") ? av[indx] : "stdin";
619 len = strlen(p) + 1;
620 if (curr_ac == indx)
621 len += 2;
622 if (width + len + 1 >= sc_width) {
623 if (!width) {
624 if (curr_ac == indx)
625 putchr('[');
626 putstr(p);
627 if (curr_ac == indx)
628 putchr(']');
629 ++indx;
630 }
631 width = 0;
632 putchr('\n');
633 continue;
634 }
635 if (width)
636 putchr(' ');
637 if (curr_ac == indx)
638 putchr('[');
639 putstr(p);
640 if (curr_ac == indx)
641 putchr(']');
642 width += len;
643 ++indx;
644 }
645 putchr('\n');
646 error(NULL);
647 }
648