Home | History | Annotate | Line # | Download | only in more
command.c revision 1.2
      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 <string.h>
     42 #include <ctype.h>
     43 #include <less.h>
     44 #include "pathnames.h"
     45 
     46 #define	NO_MCA		0
     47 #define	MCA_DONE	1
     48 #define	MCA_MORE	2
     49 
     50 extern int erase_char, kill_char, werase_char;
     51 extern int ispipe;
     52 extern int sigs;
     53 extern int quit_at_eof;
     54 extern int hit_eof;
     55 extern int sc_width;
     56 extern int sc_height;
     57 extern int sc_window;
     58 extern int curr_ac;
     59 extern int ac;
     60 extern int quitting;
     61 extern int scroll;
     62 extern int screen_trashed;	/* The screen has been overwritten */
     63 
     64 static char cmdbuf[120];	/* Buffer for holding a multi-char command */
     65 static char *cp;		/* Pointer into cmdbuf */
     66 static int cmd_col;		/* Current column of the multi-char command */
     67 static int longprompt;		/* if stat command instead of prompt */
     68 static int mca;			/* The multicharacter command (action) */
     69 static int last_mca;		/* The previous mca */
     70 static int number;		/* The number typed by the user */
     71 static int wsearch;		/* Search for matches (1) or non-matches (0) */
     72 
     73 #define	CMD_RESET	cp = cmdbuf	/* reset command buffer to empty */
     74 #define	CMD_EXEC	lower_left(); flush()
     75 
     76 /* backspace in command buffer. */
     77 static
     78 cmd_erase()
     79 {
     80 	/*
     81 	 * backspace past beginning of the string: this usually means
     82 	 * abort the command.
     83 	 */
     84 	if (cp == cmdbuf)
     85 		return(1);
     86 
     87 	/* erase an extra character, for the carat. */
     88 	if (CONTROL_CHAR(*--cp)) {
     89 		backspace();
     90 		--cmd_col;
     91 	}
     92 
     93 	backspace();
     94 	--cmd_col;
     95 	return(0);
     96 }
     97 
     98 /* set up the display to start a new multi-character command. */
     99 start_mca(action, prompt)
    100 	int action;
    101 	char *prompt;
    102 {
    103 	lower_left();
    104 	clear_eol();
    105 	putstr(prompt);
    106 	cmd_col = strlen(prompt);
    107 	mca = action;
    108 }
    109 
    110 /*
    111  * process a single character of a multi-character command, such as
    112  * a number, or the pattern of a search command.
    113  */
    114 static
    115 cmd_char(c)
    116 	int c;
    117 {
    118 	if (c == erase_char)
    119 		return(cmd_erase());
    120 	/* in this order, in case werase == erase_char */
    121 	if (c == werase_char) {
    122 		if (cp > cmdbuf) {
    123 			while (isspace(cp[-1]) && !cmd_erase());
    124 			while (!isspace(cp[-1]) && !cmd_erase());
    125 			while (isspace(cp[-1]) && !cmd_erase());
    126 		}
    127 		return(cp == cmdbuf);
    128 	}
    129 	if (c == kill_char) {
    130 		while (!cmd_erase());
    131 		return(1);
    132 	}
    133 	/*
    134 	 * No room in the command buffer, or no room on the screen;
    135 	 * {{ Could get fancy here; maybe shift the displayed line
    136 	 * and make room for more chars, like ksh. }}
    137 	 */
    138 	if (cp >= &cmdbuf[sizeof(cmdbuf)-1] || cmd_col >= sc_width-3)
    139 		bell();
    140 	else {
    141 		*cp++ = c;
    142 		if (CONTROL_CHAR(c)) {
    143 			putchr('^');
    144 			cmd_col++;
    145 			c = CARAT_CHAR(c);
    146 		}
    147 		putchr(c);
    148 		cmd_col++;
    149 	}
    150 	return(0);
    151 }
    152 
    153 prompt()
    154 {
    155 	extern int linenums, short_file;
    156 	extern char *current_name, *firstsearch, *next_name;
    157 	off_t len, pos, ch_length(), position(), forw_line();
    158 	char pbuf[40];
    159 
    160 	/*
    161 	 * if nothing is displayed yet, display starting from line 1;
    162 	 * if search string provided, go there instead.
    163 	 */
    164 	if (position(TOP) == NULL_POSITION) {
    165 		if (forw_line((off_t)0) == NULL_POSITION)
    166 			return(0);
    167 		if (!firstsearch || !search(1, firstsearch, 1, 1))
    168 			jump_back(1);
    169 	}
    170 	else if (screen_trashed)
    171 		repaint();
    172 
    173 	/* if no -e flag and we've hit EOF on the last file, quit. */
    174 	if ((!quit_at_eof || short_file) && hit_eof && curr_ac + 1 >= ac)
    175 		quit();
    176 
    177 	/* select the proper prompt and display it. */
    178 	lower_left();
    179 	clear_eol();
    180 	if (longprompt) {
    181 		so_enter();
    182 		putstr(current_name);
    183 		putstr(":");
    184 		if (!ispipe) {
    185 			(void)sprintf(pbuf, " file %d/%d", curr_ac + 1, ac);
    186 			putstr(pbuf);
    187 		}
    188 		if (linenums) {
    189 			(void)sprintf(pbuf, " line %d", currline(BOTTOM));
    190 			putstr(pbuf);
    191 		}
    192 		if ((pos = position(BOTTOM)) != NULL_POSITION) {
    193 			(void)sprintf(pbuf, " byte %qd", pos);
    194 			putstr(pbuf);
    195 			if (!ispipe && (len = ch_length())) {
    196 				(void)sprintf(pbuf, "/%qd pct %qd%%",
    197 				    len, ((100 * pos) / len));
    198 				putstr(pbuf);
    199 			}
    200 		}
    201 		so_exit();
    202 		longprompt = 0;
    203 	}
    204 	else {
    205 		so_enter();
    206 		putstr(current_name);
    207 		if (hit_eof)
    208 			if (next_name) {
    209 				putstr(": END (next file: ");
    210 				putstr(next_name);
    211 				putstr(")");
    212 			}
    213 			else
    214 				putstr(": END");
    215 		else if (!ispipe &&
    216 		    (pos = position(BOTTOM)) != NULL_POSITION &&
    217 		    (len = ch_length())) {
    218 			(void)sprintf(pbuf, " (%qd%%)", ((100 * pos) / len));
    219 			putstr(pbuf);
    220 		}
    221 		so_exit();
    222 	}
    223 	return(1);
    224 }
    225 
    226 /* get command character. */
    227 static
    228 getcc()
    229 {
    230 	extern int cmdstack;
    231 	int ch;
    232 	off_t position();
    233 
    234 	/* left over from error() routine. */
    235 	if (cmdstack) {
    236 		ch = cmdstack;
    237 		cmdstack = NULL;
    238 		return(ch);
    239 	}
    240 	if (cp > cmdbuf && position(TOP) == NULL_POSITION) {
    241 		/*
    242 		 * Command is incomplete, so try to complete it.
    243 		 * There are only two cases:
    244 		 * 1. We have "/string" but no newline.  Add the \n.
    245 		 * 2. We have a number but no command.  Treat as #g.
    246 		 * (This is all pretty hokey.)
    247 		 */
    248 		if (mca != A_DIGIT)
    249 			/* Not a number; must be search string */
    250 			return('\n');
    251 		else
    252 			/* A number; append a 'g' */
    253 			return('g');
    254 	}
    255 	return(getchr());
    256 }
    257 
    258 /* execute a multicharacter command. */
    259 static
    260 exec_mca()
    261 {
    262 	extern int file;
    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 	}
    280 }
    281 
    282 /* add a character to a multi-character command. */
    283 static
    284 mca_char(c)
    285 	int c;
    286 {
    287 	switch (mca) {
    288 	case 0:			/* not in a multicharacter command. */
    289 	case A_PREFIX:		/* in the prefix of a command. */
    290 		return(NO_MCA);
    291 	case A_DIGIT:
    292 		/*
    293 		 * Entering digits of a number.
    294 		 * Terminated by a non-digit.
    295 		 */
    296 		if (!isascii(c) || !isdigit(c) &&
    297 		    c != erase_char && c != kill_char && c != werase_char) {
    298 			/*
    299 			 * Not part of the number.
    300 			 * Treat as a normal command character.
    301 			 */
    302 			*cp = '\0';
    303 			number = atoi(cmdbuf);
    304 			CMD_RESET;
    305 			mca = 0;
    306 			return(NO_MCA);
    307 		}
    308 		break;
    309 	}
    310 
    311 	/*
    312 	 * Any other multicharacter command
    313 	 * is terminated by a newline.
    314 	 */
    315 	if (c == '\n' || c == '\r') {
    316 		exec_mca();
    317 		return(MCA_DONE);
    318 	}
    319 
    320 	/* append the char to the command buffer. */
    321 	if (cmd_char(c))
    322 		return(MCA_DONE);
    323 
    324 	return(MCA_MORE);
    325 }
    326 
    327 /*
    328  * Main command processor.
    329  * Accept and execute commands until a quit command, then return.
    330  */
    331 commands()
    332 {
    333 	register int c;
    334 	register 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, (char *)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 editfile()
    576 {
    577 	extern char *current_file;
    578 	static int dolinenumber;
    579 	static char *editor;
    580 	int c;
    581 	char buf[MAXPATHLEN * 2 + 20], *getenv();
    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 showlist()
    601 {
    602 	extern int sc_width;
    603 	extern char **av;
    604 	register int indx, width;
    605 	int len;
    606 	char *p;
    607 
    608 	if (ac <= 0) {
    609 		error("No files provided as arguments.");
    610 		return;
    611 	}
    612 	for (width = indx = 0; indx < ac;) {
    613 		p = strcmp(av[indx], "-") ? av[indx] : "stdin";
    614 		len = strlen(p) + 1;
    615 		if (curr_ac == indx)
    616 			len += 2;
    617 		if (width + len + 1 >= sc_width) {
    618 			if (!width) {
    619 				if (curr_ac == indx)
    620 					putchr('[');
    621 				putstr(p);
    622 				if (curr_ac == indx)
    623 					putchr(']');
    624 				++indx;
    625 			}
    626 			width = 0;
    627 			putchr('\n');
    628 			continue;
    629 		}
    630 		if (width)
    631 			putchr(' ');
    632 		if (curr_ac == indx)
    633 			putchr('[');
    634 		putstr(p);
    635 		if (curr_ac == indx)
    636 			putchr(']');
    637 		width += len;
    638 		++indx;
    639 	}
    640 	putchr('\n');
    641 	error((char *)NULL);
    642 }
    643