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