xref: /onnv-gate/usr/src/tools/cscope-fast/command.c (revision 13093:48f2dbca79a2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*13093SRoger.Faulkner@Oracle.COM  * Common Development and Distribution License (the "License").
6*13093SRoger.Faulkner@Oracle.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate 
220Sstevel@tonic-gate /*
23*13093SRoger.Faulkner@Oracle.COM  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
26*13093SRoger.Faulkner@Oracle.COM /*	Copyright (c) 1988 AT&T	*/
27*13093SRoger.Faulkner@Oracle.COM /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  *	cscope - interactive C symbol or text cross-reference
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  *	command functions
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include <curses.h>	/* KEY_.* */
360Sstevel@tonic-gate #include <fcntl.h>	/* O_RDONLY */
370Sstevel@tonic-gate #include <unistd.h>
380Sstevel@tonic-gate #include <stdio.h>
390Sstevel@tonic-gate #include "global.h"
400Sstevel@tonic-gate #include "library.h"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate BOOL	caseless;		/* ignore letter case when searching */
430Sstevel@tonic-gate BOOL	*change;		/* change this line */
440Sstevel@tonic-gate BOOL	changing;		/* changing text */
450Sstevel@tonic-gate char	newpat[PATLEN + 1];	/* new pattern */
460Sstevel@tonic-gate char	pattern[PATLEN + 1];	/* symbol or text pattern */
470Sstevel@tonic-gate 
480Sstevel@tonic-gate static	char	appendprompt[] = "Append to file: ";
490Sstevel@tonic-gate static	char	pipeprompt[] = "Pipe to shell command: ";
500Sstevel@tonic-gate static	char	readprompt[] = "Read from file: ";
510Sstevel@tonic-gate static	char	selectionprompt[] = "Selection: ";
520Sstevel@tonic-gate static	char	toprompt[] = "To: ";
530Sstevel@tonic-gate 
540Sstevel@tonic-gate static void scrollbar(MOUSEEVENT *p);
550Sstevel@tonic-gate 
560Sstevel@tonic-gate /* execute the command */
570Sstevel@tonic-gate 
580Sstevel@tonic-gate BOOL
command(int commandc)590Sstevel@tonic-gate command(int commandc)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate 	char	filename[PATHLEN + 1];	/* file path name */
620Sstevel@tonic-gate 	MOUSEEVENT *p;			/* mouse data */
630Sstevel@tonic-gate 	int	c, i;
640Sstevel@tonic-gate 	FILE	*file;
650Sstevel@tonic-gate 	HISTORY *curritem, *item;	/* command history */
660Sstevel@tonic-gate 	char	*s;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	switch (commandc) {
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	case ctrl('C'):	/* toggle caseless mode */
710Sstevel@tonic-gate 		if (caseless == NO) {
720Sstevel@tonic-gate 			caseless = YES;
730Sstevel@tonic-gate 			putmsg2("Caseless mode is now ON");
740Sstevel@tonic-gate 		} else {
750Sstevel@tonic-gate 			caseless = NO;
760Sstevel@tonic-gate 			putmsg2("Caseless mode is now OFF");
770Sstevel@tonic-gate 		}
780Sstevel@tonic-gate 		egrepcaseless(caseless);	/* turn on/off -i flag */
790Sstevel@tonic-gate 		return (NO);
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	case ctrl('R'):	/* rebuild the cross reference */
820Sstevel@tonic-gate 		if (isuptodate == YES) {
830Sstevel@tonic-gate 			putmsg("The -d option prevents rebuilding the "
840Sstevel@tonic-gate 			    "symbol database");
850Sstevel@tonic-gate 			return (NO);
860Sstevel@tonic-gate 		}
870Sstevel@tonic-gate 		exitcurses();
880Sstevel@tonic-gate 		freefilelist();		/* remake the source file list */
890Sstevel@tonic-gate 		makefilelist();
900Sstevel@tonic-gate 		rebuild();
910Sstevel@tonic-gate 		if (errorsfound == YES) {
920Sstevel@tonic-gate 			errorsfound = NO;
930Sstevel@tonic-gate 			askforreturn();
940Sstevel@tonic-gate 		}
950Sstevel@tonic-gate 		entercurses();
960Sstevel@tonic-gate 		putmsg("");		/* clear any previous message */
970Sstevel@tonic-gate 		totallines = 0;
980Sstevel@tonic-gate 		topline = nextline = 1;
990Sstevel@tonic-gate 		break;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	case ctrl('X'):	/* mouse selection */
1020Sstevel@tonic-gate 		if ((p = getmouseevent()) == NULL) {
1030Sstevel@tonic-gate 			return (NO);	/* unknown control sequence */
1040Sstevel@tonic-gate 		}
1050Sstevel@tonic-gate 		/* if the button number is a scrollbar tag */
1060Sstevel@tonic-gate 		if (p->button == '0') {
1070Sstevel@tonic-gate 			scrollbar(p);
1080Sstevel@tonic-gate 			break;
1090Sstevel@tonic-gate 		}
1100Sstevel@tonic-gate 		/* ignore a sweep */
1110Sstevel@tonic-gate 		if (p->x2 >= 0) {
1120Sstevel@tonic-gate 			return (NO);
1130Sstevel@tonic-gate 		}
1140Sstevel@tonic-gate 		/* if this is a line selection */
1150Sstevel@tonic-gate 		if (p->y1 < FLDLINE) {
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 			/* find the selected line */
1180Sstevel@tonic-gate 			/* note: the selection is forced into range */
1190Sstevel@tonic-gate 			for (i = disprefs - 1; i > 0; --i) {
1200Sstevel@tonic-gate 				if (p->y1 >= displine[i]) {
1210Sstevel@tonic-gate 					break;
1220Sstevel@tonic-gate 				}
1230Sstevel@tonic-gate 			}
1240Sstevel@tonic-gate 			/* display it in the file with the editor */
1250Sstevel@tonic-gate 			editref(i);
1260Sstevel@tonic-gate 		} else {	/* this is an input field selection */
1270Sstevel@tonic-gate 			field = mouseselection(p, FLDLINE, FIELDS);
1280Sstevel@tonic-gate 			setfield();
1290Sstevel@tonic-gate 			resetcmd();
1300Sstevel@tonic-gate 			return (NO);
1310Sstevel@tonic-gate 		}
1320Sstevel@tonic-gate 		break;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	case '\t':	/* go to next input field */
1350Sstevel@tonic-gate 	case '\n':
1360Sstevel@tonic-gate 	case '\r':
1370Sstevel@tonic-gate 	case ctrl('N'):
1380Sstevel@tonic-gate 	case KEY_DOWN:
1390Sstevel@tonic-gate 	case KEY_ENTER:
1400Sstevel@tonic-gate 	case KEY_RIGHT:
1410Sstevel@tonic-gate 		field = (field + 1) % FIELDS;
1420Sstevel@tonic-gate 		setfield();
1430Sstevel@tonic-gate 		resetcmd();
1440Sstevel@tonic-gate 		return (NO);
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	case ctrl('P'):	/* go to previous input field */
1470Sstevel@tonic-gate 	case KEY_UP:
1480Sstevel@tonic-gate 	case KEY_LEFT:
1490Sstevel@tonic-gate 		field = (field + (FIELDS - 1)) % FIELDS;
1500Sstevel@tonic-gate 		setfield();
1510Sstevel@tonic-gate 		resetcmd();
1520Sstevel@tonic-gate 		return (NO);
1530Sstevel@tonic-gate 	case KEY_HOME:	/* go to first input field */
1540Sstevel@tonic-gate 		field = 0;
1550Sstevel@tonic-gate 		setfield();
1560Sstevel@tonic-gate 		resetcmd();
1570Sstevel@tonic-gate 		return (NO);
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	case KEY_LL:	/* go to last input field */
1600Sstevel@tonic-gate 		field = FIELDS - 1;
1610Sstevel@tonic-gate 		setfield();
1620Sstevel@tonic-gate 		resetcmd();
1630Sstevel@tonic-gate 		return (NO);
1640Sstevel@tonic-gate 	case ' ':	/* display next page */
1650Sstevel@tonic-gate 	case '+':
1660Sstevel@tonic-gate 	case ctrl('V'):
1670Sstevel@tonic-gate 	case KEY_NPAGE:
1680Sstevel@tonic-gate 		/* don't redisplay if there are no lines */
1690Sstevel@tonic-gate 		if (totallines == 0) {
1700Sstevel@tonic-gate 			return (NO);
1710Sstevel@tonic-gate 		}
1720Sstevel@tonic-gate 		/*
1730Sstevel@tonic-gate 		 * note: seekline() is not used to move to the next
1740Sstevel@tonic-gate 		 * page because display() leaves the file pointer at
1750Sstevel@tonic-gate 		 * the next page to optimize paging forward
1760Sstevel@tonic-gate 		 */
1770Sstevel@tonic-gate 		break;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	case '-':	/* display previous page */
1800Sstevel@tonic-gate 	case KEY_PPAGE:
1810Sstevel@tonic-gate 		/* don't redisplay if there are no lines */
1820Sstevel@tonic-gate 		if (totallines == 0) {
1830Sstevel@tonic-gate 			return (NO);
1840Sstevel@tonic-gate 		}
1850Sstevel@tonic-gate 		i = topline;		/* save the current top line */
1860Sstevel@tonic-gate 		nextline = topline;	/* go back to this page */
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 		/* if on first page but not at beginning, go to beginning */
1890Sstevel@tonic-gate 		if (nextline > 1 && nextline <= mdisprefs) {
1900Sstevel@tonic-gate 			nextline = 1;
1910Sstevel@tonic-gate 		} else {	/* go back the maximum displayable lines */
1920Sstevel@tonic-gate 			nextline -= mdisprefs;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 			/* if this was the first page, go to the last page */
1950Sstevel@tonic-gate 			if (nextline < 1) {
1960Sstevel@tonic-gate 				nextline = totallines - mdisprefs + 1;
1970Sstevel@tonic-gate 				if (nextline < 1) {
1980Sstevel@tonic-gate 					nextline = 1;
1990Sstevel@tonic-gate 				}
2000Sstevel@tonic-gate 				/* old top is past last line */
2010Sstevel@tonic-gate 				i = totallines + 1;
2020Sstevel@tonic-gate 			}
2030Sstevel@tonic-gate 		}
2040Sstevel@tonic-gate 		/*
2050Sstevel@tonic-gate 		 * move down til the bottom line is just before the
2060Sstevel@tonic-gate 		 * previous top line
2070Sstevel@tonic-gate 		 */
2080Sstevel@tonic-gate 		c = nextline;
2090Sstevel@tonic-gate 		for (;;) {
2100Sstevel@tonic-gate 			seekline(nextline);
2110Sstevel@tonic-gate 			display();
2120Sstevel@tonic-gate 			if (i - bottomline <= 0) {
2130Sstevel@tonic-gate 				break;
2140Sstevel@tonic-gate 			}
2150Sstevel@tonic-gate 			nextline = ++c;
2160Sstevel@tonic-gate 		}
2170Sstevel@tonic-gate 		return (NO);	/* display already up to date */
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	case '>':	/* write or append the lines to a file */
2200Sstevel@tonic-gate 		if (totallines == 0) {
2210Sstevel@tonic-gate 			putmsg("There are no lines to write to a file");
2220Sstevel@tonic-gate 		} else {	/* get the file name */
2230Sstevel@tonic-gate 			(void) move(PRLINE, 0);
2240Sstevel@tonic-gate 			(void) addstr("Write to file: ");
2250Sstevel@tonic-gate 			s = "w";
2260Sstevel@tonic-gate 			if ((c = mygetch()) == '>') {
2270Sstevel@tonic-gate 				(void) move(PRLINE, 0);
2280Sstevel@tonic-gate 				(void) addstr(appendprompt);
2290Sstevel@tonic-gate 				c = '\0';
2300Sstevel@tonic-gate 				s = "a";
2310Sstevel@tonic-gate 			}
2320Sstevel@tonic-gate 			if (c != '\r' && c != '\n' && c != KEY_ENTER &&
2330Sstevel@tonic-gate 			    c != KEY_BREAK &&
234*13093SRoger.Faulkner@Oracle.COM 			    getaline(newpat, COLS - sizeof (appendprompt), c,
2350Sstevel@tonic-gate 			    NO) > 0) {
2360Sstevel@tonic-gate 				shellpath(filename, sizeof (filename), newpat);
2370Sstevel@tonic-gate 				if ((file = fopen(filename, s)) == NULL) {
2380Sstevel@tonic-gate 					cannotopen(filename);
2390Sstevel@tonic-gate 				} else {
2400Sstevel@tonic-gate 					seekline(1);
2410Sstevel@tonic-gate 					while ((c = getc(refsfound)) != EOF) {
2420Sstevel@tonic-gate 						(void) putc(c, file);
2430Sstevel@tonic-gate 					}
2440Sstevel@tonic-gate 					seekline(topline);
2450Sstevel@tonic-gate 					(void) fclose(file);
2460Sstevel@tonic-gate 				}
2470Sstevel@tonic-gate 			}
2480Sstevel@tonic-gate 			clearprompt();
2490Sstevel@tonic-gate 		}
2500Sstevel@tonic-gate 		return (NO);	/* return to the previous field */
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	case '<':	/* read lines from a file */
2530Sstevel@tonic-gate 		(void) move(PRLINE, 0);
2540Sstevel@tonic-gate 		(void) addstr(readprompt);
255*13093SRoger.Faulkner@Oracle.COM 		if (getaline(newpat, COLS - sizeof (readprompt), '\0',
2560Sstevel@tonic-gate 		    NO) > 0) {
2570Sstevel@tonic-gate 			clearprompt();
2580Sstevel@tonic-gate 			shellpath(filename, sizeof (filename), newpat);
2590Sstevel@tonic-gate 			if (readrefs(filename) == NO) {
2600Sstevel@tonic-gate 				putmsg2("Ignoring an empty file");
2610Sstevel@tonic-gate 				return (NO);
2620Sstevel@tonic-gate 			}
2630Sstevel@tonic-gate 			return (YES);
2640Sstevel@tonic-gate 		}
2650Sstevel@tonic-gate 		clearprompt();
2660Sstevel@tonic-gate 		return (NO);
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	case '^':	/* pipe the lines through a shell command */
2690Sstevel@tonic-gate 	case '|':	/* pipe the lines to a shell command */
2700Sstevel@tonic-gate 		if (totallines == 0) {
2710Sstevel@tonic-gate 			putmsg("There are no lines to pipe to a shell command");
2720Sstevel@tonic-gate 			return (NO);
2730Sstevel@tonic-gate 		}
2740Sstevel@tonic-gate 		/* get the shell command */
2750Sstevel@tonic-gate 		(void) move(PRLINE, 0);
2760Sstevel@tonic-gate 		(void) addstr(pipeprompt);
277*13093SRoger.Faulkner@Oracle.COM 		if (getaline(newpat,
2780Sstevel@tonic-gate 		    COLS - sizeof (pipeprompt), '\0', NO) == 0) {
2790Sstevel@tonic-gate 			clearprompt();
2800Sstevel@tonic-gate 			return (NO);
2810Sstevel@tonic-gate 		}
2820Sstevel@tonic-gate 		/* if the ^ command, redirect output to a temp file */
2830Sstevel@tonic-gate 		if (commandc == '^') {
2840Sstevel@tonic-gate 			(void) strcat(strcat(newpat, " >"), temp2);
2850Sstevel@tonic-gate 		}
2860Sstevel@tonic-gate 		exitcurses();
2870Sstevel@tonic-gate 		if ((file = mypopen(newpat, "w")) == NULL) {
2880Sstevel@tonic-gate 			(void) fprintf(stderr,
2890Sstevel@tonic-gate 			    "cscope: cannot open pipe to shell command: %s\n",
2900Sstevel@tonic-gate 			    newpat);
2910Sstevel@tonic-gate 		} else {
2920Sstevel@tonic-gate 			seekline(1);
2930Sstevel@tonic-gate 			while ((c = getc(refsfound)) != EOF) {
2940Sstevel@tonic-gate 				(void) putc(c, file);
2950Sstevel@tonic-gate 			}
2960Sstevel@tonic-gate 			seekline(topline);
2970Sstevel@tonic-gate 			(void) mypclose(file);
2980Sstevel@tonic-gate 		}
2990Sstevel@tonic-gate 		if (commandc == '^') {
3000Sstevel@tonic-gate 			if (readrefs(temp2) == NO) {
3010Sstevel@tonic-gate 				putmsg("Ignoring empty output of ^ command");
3020Sstevel@tonic-gate 			}
3030Sstevel@tonic-gate 		}
3040Sstevel@tonic-gate 		askforreturn();
3050Sstevel@tonic-gate 		entercurses();
3060Sstevel@tonic-gate 		break;
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	case ctrl('L'):	/* redraw screen */
3090Sstevel@tonic-gate 	case KEY_CLEAR:
3100Sstevel@tonic-gate 		(void) clearok(curscr, TRUE);
3110Sstevel@tonic-gate 		(void) wrefresh(curscr);
3120Sstevel@tonic-gate 		drawscrollbar(topline, bottomline, totallines);
3130Sstevel@tonic-gate 		return (NO);
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	case '!':	/* shell escape */
3160Sstevel@tonic-gate 		(void) execute(shell, shell, (char *)NULL);
3170Sstevel@tonic-gate 		seekline(topline);
3180Sstevel@tonic-gate 		break;
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	case '?':	/* help */
3210Sstevel@tonic-gate 		(void) clear();
3220Sstevel@tonic-gate 		help();
3230Sstevel@tonic-gate 		(void) clear();
3240Sstevel@tonic-gate 		seekline(topline);
3250Sstevel@tonic-gate 		break;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	case ctrl('E'):	/* edit all lines */
3280Sstevel@tonic-gate 		editall();
3290Sstevel@tonic-gate 		break;
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	case ctrl('A'):	/* repeat last pattern */
3320Sstevel@tonic-gate 	case ctrl('Y'):	/* (old command) */
3330Sstevel@tonic-gate 		if (*pattern != '\0') {
3340Sstevel@tonic-gate 			(void) addstr(pattern);
3350Sstevel@tonic-gate 			goto repeat;
3360Sstevel@tonic-gate 		}
3370Sstevel@tonic-gate 		break;
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	case ctrl('B'):		/* cmd history back */
3400Sstevel@tonic-gate 	case ctrl('F'):		/* cmd history fwd */
3410Sstevel@tonic-gate 		curritem = currentcmd();
3420Sstevel@tonic-gate 		item = (commandc == ctrl('F')) ? nextcmd() : prevcmd();
3430Sstevel@tonic-gate 		clearmsg2();
3440Sstevel@tonic-gate 		if (curritem == item) {
3450Sstevel@tonic-gate 			/* inform user that we're at history end */
3460Sstevel@tonic-gate 			putmsg2(
3470Sstevel@tonic-gate 			    "End of input field and search pattern history");
3480Sstevel@tonic-gate 		}
3490Sstevel@tonic-gate 		if (item) {
3500Sstevel@tonic-gate 			field = item->field;
3510Sstevel@tonic-gate 			setfield();
3520Sstevel@tonic-gate 			atfield();
3530Sstevel@tonic-gate 			(void) addstr(item->text);
3540Sstevel@tonic-gate 			(void) strcpy(pattern, item->text);
3550Sstevel@tonic-gate 			switch (c = mygetch()) {
3560Sstevel@tonic-gate 			case '\r':
3570Sstevel@tonic-gate 			case '\n':
3580Sstevel@tonic-gate 			case KEY_ENTER:
3590Sstevel@tonic-gate 				goto repeat;
3600Sstevel@tonic-gate 			default:
3610Sstevel@tonic-gate 				ungetch(c);
3620Sstevel@tonic-gate 				atfield();
3630Sstevel@tonic-gate 				(void) clrtoeol(); /* clear current field */
3640Sstevel@tonic-gate 				break;
3650Sstevel@tonic-gate 			}
3660Sstevel@tonic-gate 		}
3670Sstevel@tonic-gate 		return (NO);
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	case '\\':	/* next character is not a command */
3700Sstevel@tonic-gate 		(void) addch('\\');	/* display the quote character */
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 		/* get a character from the terminal */
3730Sstevel@tonic-gate 		if ((commandc = mygetch()) == EOF) {
3740Sstevel@tonic-gate 			return (NO);	/* quit */
3750Sstevel@tonic-gate 		}
3760Sstevel@tonic-gate 		(void) addstr("\b \b");	/* erase the quote character */
3770Sstevel@tonic-gate 		goto ispat;
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	case '.':
3800Sstevel@tonic-gate 		atfield();	/* move back to the input field */
3810Sstevel@tonic-gate 		/* FALLTHROUGH */
3820Sstevel@tonic-gate 	default:
3830Sstevel@tonic-gate 		/* edit a selected line */
3840Sstevel@tonic-gate 		if (isdigit(commandc) && commandc != '0' && !mouse) {
3850Sstevel@tonic-gate 			if (returnrequired == NO) {
3860Sstevel@tonic-gate 				editref(commandc - '1');
3870Sstevel@tonic-gate 			} else {
3880Sstevel@tonic-gate 				(void) move(PRLINE, 0);
3890Sstevel@tonic-gate 				(void) addstr(selectionprompt);
390*13093SRoger.Faulkner@Oracle.COM 				if (getaline(newpat,
3910Sstevel@tonic-gate 				    COLS - sizeof (selectionprompt), commandc,
3920Sstevel@tonic-gate 				    NO) > 0 &&
3930Sstevel@tonic-gate 				    (i = atoi(newpat)) > 0) {
3940Sstevel@tonic-gate 					editref(i - 1);
3950Sstevel@tonic-gate 				}
3960Sstevel@tonic-gate 				clearprompt();
3970Sstevel@tonic-gate 			}
3980Sstevel@tonic-gate 		} else if (isprint(commandc)) {
3990Sstevel@tonic-gate 			/* this is the start of a pattern */
4000Sstevel@tonic-gate ispat:
401*13093SRoger.Faulkner@Oracle.COM 			if (getaline(newpat, COLS - fldcolumn - 1, commandc,
4020Sstevel@tonic-gate 			    caseless) > 0) {
4030Sstevel@tonic-gate 					(void) strcpy(pattern, newpat);
4040Sstevel@tonic-gate 					resetcmd();	/* reset history */
4050Sstevel@tonic-gate repeat:
4060Sstevel@tonic-gate 				addcmd(field, pattern);	/* add to history */
4070Sstevel@tonic-gate 				if (field == CHANGE) {
4080Sstevel@tonic-gate 					/* prompt for the new text */
4090Sstevel@tonic-gate 					(void) move(PRLINE, 0);
4100Sstevel@tonic-gate 					(void) addstr(toprompt);
411*13093SRoger.Faulkner@Oracle.COM 					(void) getaline(newpat,
4120Sstevel@tonic-gate 					    COLS - sizeof (toprompt), '\0', NO);
4130Sstevel@tonic-gate 				}
4140Sstevel@tonic-gate 				/* search for the pattern */
4150Sstevel@tonic-gate 				if (search() == YES) {
4160Sstevel@tonic-gate 					switch (field) {
4170Sstevel@tonic-gate 					case DEFINITION:
4180Sstevel@tonic-gate 					case FILENAME:
4190Sstevel@tonic-gate 						if (totallines > 1) {
4200Sstevel@tonic-gate 							break;
4210Sstevel@tonic-gate 						}
4220Sstevel@tonic-gate 						topline = 1;
4230Sstevel@tonic-gate 						editref(0);
4240Sstevel@tonic-gate 						break;
4250Sstevel@tonic-gate 					case CHANGE:
4260Sstevel@tonic-gate 						return (changestring());
4270Sstevel@tonic-gate 					}
4280Sstevel@tonic-gate 				} else if (field == FILENAME &&
4290Sstevel@tonic-gate 				    access(newpat, READ) == 0) {
4300Sstevel@tonic-gate 					/* try to edit the file anyway */
4310Sstevel@tonic-gate 					edit(newpat, "1");
4320Sstevel@tonic-gate 				}
4330Sstevel@tonic-gate 			} else {	/* no pattern--the input was erased */
4340Sstevel@tonic-gate 				return (NO);
4350Sstevel@tonic-gate 			}
4360Sstevel@tonic-gate 		} else {	/* control character */
4370Sstevel@tonic-gate 			return (NO);
4380Sstevel@tonic-gate 		}
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 	return (YES);
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate /* clear the prompt line */
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate void
clearprompt(void)4460Sstevel@tonic-gate clearprompt(void)
4470Sstevel@tonic-gate {
4480Sstevel@tonic-gate 	(void) move(PRLINE, 0);
4490Sstevel@tonic-gate 	(void) clrtoeol();
4500Sstevel@tonic-gate }
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate /* read references from a file */
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate BOOL
readrefs(char * filename)4550Sstevel@tonic-gate readrefs(char *filename)
4560Sstevel@tonic-gate {
4570Sstevel@tonic-gate 	FILE	*file;
4580Sstevel@tonic-gate 	int	c;
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	if ((file = fopen(filename, "r")) == NULL) {
4610Sstevel@tonic-gate 		cannotopen(filename);
4620Sstevel@tonic-gate 		return (NO);
4630Sstevel@tonic-gate 	}
4640Sstevel@tonic-gate 	if ((c = getc(file)) == EOF) {	/* if file is empty */
4650Sstevel@tonic-gate 		return (NO);
4660Sstevel@tonic-gate 	}
4670Sstevel@tonic-gate 	totallines = 0;
4680Sstevel@tonic-gate 	nextline = 1;
4690Sstevel@tonic-gate 	if (writerefsfound() == YES) {
4700Sstevel@tonic-gate 		(void) putc(c, refsfound);
4710Sstevel@tonic-gate 		while ((c = getc(file)) != EOF) {
4720Sstevel@tonic-gate 			(void) putc(c, refsfound);
4730Sstevel@tonic-gate 		}
4740Sstevel@tonic-gate 		(void) fclose(file);
4750Sstevel@tonic-gate 		(void) freopen(temp1, "r", refsfound);
4760Sstevel@tonic-gate 		countrefs();
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 	return (YES);
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate /* change one text string to another */
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate BOOL
changestring(void)4840Sstevel@tonic-gate changestring(void)
4850Sstevel@tonic-gate {
4860Sstevel@tonic-gate 	char	buf[PATLEN + 1];	/* input buffer */
4870Sstevel@tonic-gate 	char	newfile[PATHLEN + 1];	/* new file name */
4880Sstevel@tonic-gate 	char	oldfile[PATHLEN + 1];	/* old file name */
4890Sstevel@tonic-gate 	char	linenum[NUMLEN + 1];	/* file line number */
4900Sstevel@tonic-gate 	char	msg[MSGLEN + 1];	/* message */
4910Sstevel@tonic-gate 	FILE	*script;		/* shell script file */
4920Sstevel@tonic-gate 	BOOL	anymarked = NO;		/* any line marked */
4930Sstevel@tonic-gate 	MOUSEEVENT *p;			/* mouse data */
4940Sstevel@tonic-gate 	int	c, i;
4950Sstevel@tonic-gate 	char	*s;
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	/* open the temporary file */
4980Sstevel@tonic-gate 	if ((script = fopen(temp2, "w")) == NULL) {
4990Sstevel@tonic-gate 		cannotopen(temp2);
5000Sstevel@tonic-gate 		return (NO);
5010Sstevel@tonic-gate 	}
5020Sstevel@tonic-gate 	/* create the line change indicators */
5030Sstevel@tonic-gate 	change = (BOOL *)mycalloc((unsigned)totallines, sizeof (BOOL));
5040Sstevel@tonic-gate 	changing = YES;
5050Sstevel@tonic-gate 	initmenu();
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	/* until the quit command is entered */
5080Sstevel@tonic-gate 	for (;;) {
5090Sstevel@tonic-gate 		/* display the current page of lines */
5100Sstevel@tonic-gate 		display();
5110Sstevel@tonic-gate 	same:
5120Sstevel@tonic-gate 		/* get a character from the terminal */
5130Sstevel@tonic-gate 		(void) move(PRLINE, 0);
5140Sstevel@tonic-gate 		(void) addstr(
5150Sstevel@tonic-gate 		    "Select lines to change (press the ? key for help): ");
5160Sstevel@tonic-gate 		if ((c = mygetch()) == EOF || c == ctrl('D') ||
5170Sstevel@tonic-gate 		    c == ctrl('Z')) {
5180Sstevel@tonic-gate 			break;	/* change lines */
5190Sstevel@tonic-gate 		}
5200Sstevel@tonic-gate 		/* see if the input character is a command */
5210Sstevel@tonic-gate 		switch (c) {
5220Sstevel@tonic-gate 		case ' ':	/* display next page */
5230Sstevel@tonic-gate 		case '+':
5240Sstevel@tonic-gate 		case ctrl('V'):
5250Sstevel@tonic-gate 		case KEY_NPAGE:
5260Sstevel@tonic-gate 		case '-':	/* display previous page */
5270Sstevel@tonic-gate 		case KEY_PPAGE:
5280Sstevel@tonic-gate 		case '!':	/* shell escape */
5290Sstevel@tonic-gate 		case '?':	/* help */
5300Sstevel@tonic-gate 			(void) command(c);
5310Sstevel@tonic-gate 			break;
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 		case ctrl('L'):	/* redraw screen */
5340Sstevel@tonic-gate 		case KEY_CLEAR:
5350Sstevel@tonic-gate 			(void) command(c);
5360Sstevel@tonic-gate 			goto same;
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 		case ESC:	/* kept for backwards compatibility */
5390Sstevel@tonic-gate 			/* FALLTHROUGH */
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 		case '\r':	/* don't change lines */
5420Sstevel@tonic-gate 		case '\n':
5430Sstevel@tonic-gate 		case KEY_ENTER:
5440Sstevel@tonic-gate 		case KEY_BREAK:
5450Sstevel@tonic-gate 		case ctrl('G'):
5460Sstevel@tonic-gate 			clearprompt();
5470Sstevel@tonic-gate 			goto nochange;
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 		case '*':	/* mark/unmark all displayed lines */
5500Sstevel@tonic-gate 			for (i = 0; topline + i < nextline; ++i) {
5510Sstevel@tonic-gate 				mark(i);
5520Sstevel@tonic-gate 			}
5530Sstevel@tonic-gate 			goto same;
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 		case 'a':	/* mark/unmark all lines */
5560Sstevel@tonic-gate 			for (i = 0; i < totallines; ++i) {
5570Sstevel@tonic-gate 				if (change[i] == NO) {
5580Sstevel@tonic-gate 					change[i] = YES;
5590Sstevel@tonic-gate 				} else {
5600Sstevel@tonic-gate 					change[i] = NO;
5610Sstevel@tonic-gate 				}
5620Sstevel@tonic-gate 			}
5630Sstevel@tonic-gate 			/* show that all have been marked */
5640Sstevel@tonic-gate 			seekline(totallines);
5650Sstevel@tonic-gate 			break;
5660Sstevel@tonic-gate 		case ctrl('X'):	/* mouse selection */
5670Sstevel@tonic-gate 			if ((p = getmouseevent()) == NULL) {
5680Sstevel@tonic-gate 				goto same;	/* unknown control sequence */
5690Sstevel@tonic-gate 			}
5700Sstevel@tonic-gate 			/* if the button number is a scrollbar tag */
5710Sstevel@tonic-gate 			if (p->button == '0') {
5720Sstevel@tonic-gate 				scrollbar(p);
5730Sstevel@tonic-gate 				break;
5740Sstevel@tonic-gate 			}
5750Sstevel@tonic-gate 			/* find the selected line */
5760Sstevel@tonic-gate 			/* note: the selection is forced into range */
5770Sstevel@tonic-gate 			for (i = disprefs - 1; i > 0; --i) {
5780Sstevel@tonic-gate 				if (p->y1 >= displine[i]) {
5790Sstevel@tonic-gate 					break;
5800Sstevel@tonic-gate 				}
5810Sstevel@tonic-gate 			}
5820Sstevel@tonic-gate 			mark(i);
5830Sstevel@tonic-gate 			goto same;
5840Sstevel@tonic-gate 		default:
5850Sstevel@tonic-gate 			/* if a line was selected */
5860Sstevel@tonic-gate 			if (isdigit(c) && c != '0' && !mouse) {
5870Sstevel@tonic-gate 				if (returnrequired == NO) {
5880Sstevel@tonic-gate 					mark(c - '1');
5890Sstevel@tonic-gate 				} else {
5900Sstevel@tonic-gate 					clearprompt();
5910Sstevel@tonic-gate 					(void) move(PRLINE, 0);
5920Sstevel@tonic-gate 					(void) addstr(selectionprompt);
593*13093SRoger.Faulkner@Oracle.COM 					if (getaline(buf,
5940Sstevel@tonic-gate 					    COLS - sizeof (selectionprompt), c,
5950Sstevel@tonic-gate 					    NO) > 0 &&
5960Sstevel@tonic-gate 					    (i = atoi(buf)) > 0) {
5970Sstevel@tonic-gate 						mark(i - 1);
5980Sstevel@tonic-gate 					}
5990Sstevel@tonic-gate 				}
6000Sstevel@tonic-gate 			}
6010Sstevel@tonic-gate 			goto same;
6020Sstevel@tonic-gate 		}
6030Sstevel@tonic-gate 	}
6040Sstevel@tonic-gate 	/* for each line containing the old text */
6050Sstevel@tonic-gate 	(void) fprintf(script, "ed - <<\\!\nH\n");
6060Sstevel@tonic-gate 	*oldfile = '\0';
6070Sstevel@tonic-gate 	seekline(1);
6080Sstevel@tonic-gate 	for (i = 0; fscanf(refsfound, "%s%*s%s%*[^\n]", newfile, linenum) == 2;
6090Sstevel@tonic-gate 	    ++i) {
6100Sstevel@tonic-gate 		/* see if the line is to be changed */
6110Sstevel@tonic-gate 		if (change[i] == YES) {
6120Sstevel@tonic-gate 			anymarked = YES;
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 			/* if this is a new file */
6150Sstevel@tonic-gate 			if (strcmp(newfile, oldfile) != 0) {
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 				/* make sure it can be changed */
6180Sstevel@tonic-gate 				if (access(newfile, WRITE) != 0) {
6190Sstevel@tonic-gate 					(void) sprintf(msg,
6200Sstevel@tonic-gate 					    "Cannot write to file %s",
6210Sstevel@tonic-gate 					    newfile);
6220Sstevel@tonic-gate 					putmsg(msg);
6230Sstevel@tonic-gate 					anymarked = NO;
6240Sstevel@tonic-gate 					break;
6250Sstevel@tonic-gate 				}
6260Sstevel@tonic-gate 				/* if there was an old file */
6270Sstevel@tonic-gate 				if (*oldfile != '\0') {
6280Sstevel@tonic-gate 					(void) fprintf(script,
6290Sstevel@tonic-gate 					    "w\n");	/* save it */
6300Sstevel@tonic-gate 				}
6310Sstevel@tonic-gate 				/* edit the new file */
6320Sstevel@tonic-gate 				(void) strcpy(oldfile, newfile);
6330Sstevel@tonic-gate 				(void) fprintf(script, "e %s\n", oldfile);
6340Sstevel@tonic-gate 			}
6350Sstevel@tonic-gate 			/* output substitute command */
6360Sstevel@tonic-gate 			(void) fprintf(script,
6370Sstevel@tonic-gate 			    "%ss/", linenum);	/* change */
6380Sstevel@tonic-gate 			for (s = pattern; *s != '\0'; ++s) {	/* old text */
6390Sstevel@tonic-gate 				if (*s == '/') {
6400Sstevel@tonic-gate 					(void) putc('\\', script);
6410Sstevel@tonic-gate 				}
6420Sstevel@tonic-gate 				(void) putc(*s, script);
6430Sstevel@tonic-gate 			}
6440Sstevel@tonic-gate 			(void) putc('/', script);			/* to */
6450Sstevel@tonic-gate 			for (s = newpat; *s != '\0'; ++s) {	/* new text */
6460Sstevel@tonic-gate 				if (strchr("/\\&", *s) != NULL) {
6470Sstevel@tonic-gate 					(void) putc('\\', script);
6480Sstevel@tonic-gate 				}
6490Sstevel@tonic-gate 				(void) putc(*s, script);
6500Sstevel@tonic-gate 			}
6510Sstevel@tonic-gate 			(void) fprintf(script, "/gp\n");	/* and print */
6520Sstevel@tonic-gate 		}
6530Sstevel@tonic-gate 	}
6540Sstevel@tonic-gate 	(void) fprintf(script, "w\nq\n!\n");	/* write and quit */
6550Sstevel@tonic-gate 	(void) fclose(script);
6560Sstevel@tonic-gate 	clearprompt();
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	/* if any line was marked */
6590Sstevel@tonic-gate 	if (anymarked == YES) {
6600Sstevel@tonic-gate 		/* edit the files */
6610Sstevel@tonic-gate 		(void) refresh();
6620Sstevel@tonic-gate 		(void) fprintf(stderr, "Changed lines:\n\r");
6630Sstevel@tonic-gate 		(void) execute(shell, shell, temp2, (char *)NULL);
6640Sstevel@tonic-gate 		askforreturn();
6650Sstevel@tonic-gate 	}
6660Sstevel@tonic-gate nochange:
6670Sstevel@tonic-gate 	changing = NO;
6680Sstevel@tonic-gate 	initmenu();
6690Sstevel@tonic-gate 	free(change);
6700Sstevel@tonic-gate 	seekline(topline);
6710Sstevel@tonic-gate 	return (YES);	/* clear any marks on exit without change */
6720Sstevel@tonic-gate }
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate /* mark/unmark this displayed line to be changed */
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate void
mark(int i)6770Sstevel@tonic-gate mark(int i)
6780Sstevel@tonic-gate {
6790Sstevel@tonic-gate 	int	j;
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	j = i + topline - 1;
6820Sstevel@tonic-gate 	if (j < totallines) {
6830Sstevel@tonic-gate 		(void) move(displine[i], selectlen);
6840Sstevel@tonic-gate 		if (change[j] == NO) {
6850Sstevel@tonic-gate 			change[j] = YES;
6860Sstevel@tonic-gate 			(void) addch('>');
6870Sstevel@tonic-gate 		} else {
6880Sstevel@tonic-gate 			change[j] = NO;
6890Sstevel@tonic-gate 			(void) addch(' ');
6900Sstevel@tonic-gate 		}
6910Sstevel@tonic-gate 	}
6920Sstevel@tonic-gate }
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate /* scrollbar actions */
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate static void
scrollbar(MOUSEEVENT * p)6970Sstevel@tonic-gate scrollbar(MOUSEEVENT *p)
6980Sstevel@tonic-gate {
6990Sstevel@tonic-gate 	/* reposition list if it makes sense */
7000Sstevel@tonic-gate 	if (totallines == 0) {
7010Sstevel@tonic-gate 		return;
7020Sstevel@tonic-gate 	}
7030Sstevel@tonic-gate 	switch (p->percent) {
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	case 101: /* scroll down one page */
7060Sstevel@tonic-gate 		if (nextline + mdisprefs > totallines) {
7070Sstevel@tonic-gate 			nextline = totallines - mdisprefs + 1;
7080Sstevel@tonic-gate 		}
7090Sstevel@tonic-gate 		break;
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	case 102: /* scroll up one page */
7120Sstevel@tonic-gate 		nextline = topline - mdisprefs;
7130Sstevel@tonic-gate 		if (nextline < 1) {
7140Sstevel@tonic-gate 			nextline = 1;
7150Sstevel@tonic-gate 		}
7160Sstevel@tonic-gate 		break;
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	case 103: /* scroll down one line */
7190Sstevel@tonic-gate 		nextline = topline + 1;
7200Sstevel@tonic-gate 		break;
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	case 104: /* scroll up one line */
7230Sstevel@tonic-gate 		if (topline > 1) {
7240Sstevel@tonic-gate 			nextline = topline - 1;
7250Sstevel@tonic-gate 		}
7260Sstevel@tonic-gate 		break;
7270Sstevel@tonic-gate 	default:
7280Sstevel@tonic-gate 		nextline = p->percent * totallines / 100;
7290Sstevel@tonic-gate 	}
7300Sstevel@tonic-gate 	seekline(nextline);
7310Sstevel@tonic-gate }
732