1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1999 by Sun Microsystems, Inc.
28*7c478bd9Sstevel@tonic-gate  * All rights reserved.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate /*
34*7c478bd9Sstevel@tonic-gate  *	cscope - interactive C symbol or text cross-reference
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  *	command functions
37*7c478bd9Sstevel@tonic-gate  */
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include <curses.h>	/* KEY_.* */
40*7c478bd9Sstevel@tonic-gate #include <fcntl.h>	/* O_RDONLY */
41*7c478bd9Sstevel@tonic-gate #include <unistd.h>
42*7c478bd9Sstevel@tonic-gate #include <stdio.h>
43*7c478bd9Sstevel@tonic-gate #include "global.h"
44*7c478bd9Sstevel@tonic-gate #include "library.h"
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate BOOL	caseless;		/* ignore letter case when searching */
47*7c478bd9Sstevel@tonic-gate BOOL	*change;		/* change this line */
48*7c478bd9Sstevel@tonic-gate BOOL	changing;		/* changing text */
49*7c478bd9Sstevel@tonic-gate char	newpat[PATLEN + 1];	/* new pattern */
50*7c478bd9Sstevel@tonic-gate char	pattern[PATLEN + 1];	/* symbol or text pattern */
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate static	char	appendprompt[] = "Append to file: ";
53*7c478bd9Sstevel@tonic-gate static	char	pipeprompt[] = "Pipe to shell command: ";
54*7c478bd9Sstevel@tonic-gate static	char	readprompt[] = "Read from file: ";
55*7c478bd9Sstevel@tonic-gate static	char	selectionprompt[] = "Selection: ";
56*7c478bd9Sstevel@tonic-gate static	char	toprompt[] = "To: ";
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate static void scrollbar(MOUSEEVENT *p);
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate /* execute the command */
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate BOOL
63*7c478bd9Sstevel@tonic-gate command(int commandc)
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate 	char	filename[PATHLEN + 1];	/* file path name */
66*7c478bd9Sstevel@tonic-gate 	MOUSEEVENT *p;			/* mouse data */
67*7c478bd9Sstevel@tonic-gate 	int	c, i;
68*7c478bd9Sstevel@tonic-gate 	FILE	*file;
69*7c478bd9Sstevel@tonic-gate 	HISTORY *curritem, *item;	/* command history */
70*7c478bd9Sstevel@tonic-gate 	char	*s;
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	switch (commandc) {
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	case ctrl('C'):	/* toggle caseless mode */
75*7c478bd9Sstevel@tonic-gate 		if (caseless == NO) {
76*7c478bd9Sstevel@tonic-gate 			caseless = YES;
77*7c478bd9Sstevel@tonic-gate 			putmsg2("Caseless mode is now ON");
78*7c478bd9Sstevel@tonic-gate 		} else {
79*7c478bd9Sstevel@tonic-gate 			caseless = NO;
80*7c478bd9Sstevel@tonic-gate 			putmsg2("Caseless mode is now OFF");
81*7c478bd9Sstevel@tonic-gate 		}
82*7c478bd9Sstevel@tonic-gate 		egrepcaseless(caseless);	/* turn on/off -i flag */
83*7c478bd9Sstevel@tonic-gate 		return (NO);
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	case ctrl('R'):	/* rebuild the cross reference */
86*7c478bd9Sstevel@tonic-gate 		if (isuptodate == YES) {
87*7c478bd9Sstevel@tonic-gate 			putmsg("The -d option prevents rebuilding the "
88*7c478bd9Sstevel@tonic-gate 			    "symbol database");
89*7c478bd9Sstevel@tonic-gate 			return (NO);
90*7c478bd9Sstevel@tonic-gate 		}
91*7c478bd9Sstevel@tonic-gate 		exitcurses();
92*7c478bd9Sstevel@tonic-gate 		freefilelist();		/* remake the source file list */
93*7c478bd9Sstevel@tonic-gate 		makefilelist();
94*7c478bd9Sstevel@tonic-gate 		rebuild();
95*7c478bd9Sstevel@tonic-gate 		if (errorsfound == YES) {
96*7c478bd9Sstevel@tonic-gate 			errorsfound = NO;
97*7c478bd9Sstevel@tonic-gate 			askforreturn();
98*7c478bd9Sstevel@tonic-gate 		}
99*7c478bd9Sstevel@tonic-gate 		entercurses();
100*7c478bd9Sstevel@tonic-gate 		putmsg("");		/* clear any previous message */
101*7c478bd9Sstevel@tonic-gate 		totallines = 0;
102*7c478bd9Sstevel@tonic-gate 		topline = nextline = 1;
103*7c478bd9Sstevel@tonic-gate 		break;
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	case ctrl('X'):	/* mouse selection */
106*7c478bd9Sstevel@tonic-gate 		if ((p = getmouseevent()) == NULL) {
107*7c478bd9Sstevel@tonic-gate 			return (NO);	/* unknown control sequence */
108*7c478bd9Sstevel@tonic-gate 		}
109*7c478bd9Sstevel@tonic-gate 		/* if the button number is a scrollbar tag */
110*7c478bd9Sstevel@tonic-gate 		if (p->button == '0') {
111*7c478bd9Sstevel@tonic-gate 			scrollbar(p);
112*7c478bd9Sstevel@tonic-gate 			break;
113*7c478bd9Sstevel@tonic-gate 		}
114*7c478bd9Sstevel@tonic-gate 		/* ignore a sweep */
115*7c478bd9Sstevel@tonic-gate 		if (p->x2 >= 0) {
116*7c478bd9Sstevel@tonic-gate 			return (NO);
117*7c478bd9Sstevel@tonic-gate 		}
118*7c478bd9Sstevel@tonic-gate 		/* if this is a line selection */
119*7c478bd9Sstevel@tonic-gate 		if (p->y1 < FLDLINE) {
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 			/* find the selected line */
122*7c478bd9Sstevel@tonic-gate 			/* note: the selection is forced into range */
123*7c478bd9Sstevel@tonic-gate 			for (i = disprefs - 1; i > 0; --i) {
124*7c478bd9Sstevel@tonic-gate 				if (p->y1 >= displine[i]) {
125*7c478bd9Sstevel@tonic-gate 					break;
126*7c478bd9Sstevel@tonic-gate 				}
127*7c478bd9Sstevel@tonic-gate 			}
128*7c478bd9Sstevel@tonic-gate 			/* display it in the file with the editor */
129*7c478bd9Sstevel@tonic-gate 			editref(i);
130*7c478bd9Sstevel@tonic-gate 		} else {	/* this is an input field selection */
131*7c478bd9Sstevel@tonic-gate 			field = mouseselection(p, FLDLINE, FIELDS);
132*7c478bd9Sstevel@tonic-gate 			setfield();
133*7c478bd9Sstevel@tonic-gate 			resetcmd();
134*7c478bd9Sstevel@tonic-gate 			return (NO);
135*7c478bd9Sstevel@tonic-gate 		}
136*7c478bd9Sstevel@tonic-gate 		break;
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	case '\t':	/* go to next input field */
139*7c478bd9Sstevel@tonic-gate 	case '\n':
140*7c478bd9Sstevel@tonic-gate 	case '\r':
141*7c478bd9Sstevel@tonic-gate 	case ctrl('N'):
142*7c478bd9Sstevel@tonic-gate 	case KEY_DOWN:
143*7c478bd9Sstevel@tonic-gate 	case KEY_ENTER:
144*7c478bd9Sstevel@tonic-gate 	case KEY_RIGHT:
145*7c478bd9Sstevel@tonic-gate 		field = (field + 1) % FIELDS;
146*7c478bd9Sstevel@tonic-gate 		setfield();
147*7c478bd9Sstevel@tonic-gate 		resetcmd();
148*7c478bd9Sstevel@tonic-gate 		return (NO);
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	case ctrl('P'):	/* go to previous input field */
151*7c478bd9Sstevel@tonic-gate 	case KEY_UP:
152*7c478bd9Sstevel@tonic-gate 	case KEY_LEFT:
153*7c478bd9Sstevel@tonic-gate 		field = (field + (FIELDS - 1)) % FIELDS;
154*7c478bd9Sstevel@tonic-gate 		setfield();
155*7c478bd9Sstevel@tonic-gate 		resetcmd();
156*7c478bd9Sstevel@tonic-gate 		return (NO);
157*7c478bd9Sstevel@tonic-gate 	case KEY_HOME:	/* go to first input field */
158*7c478bd9Sstevel@tonic-gate 		field = 0;
159*7c478bd9Sstevel@tonic-gate 		setfield();
160*7c478bd9Sstevel@tonic-gate 		resetcmd();
161*7c478bd9Sstevel@tonic-gate 		return (NO);
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	case KEY_LL:	/* go to last input field */
164*7c478bd9Sstevel@tonic-gate 		field = FIELDS - 1;
165*7c478bd9Sstevel@tonic-gate 		setfield();
166*7c478bd9Sstevel@tonic-gate 		resetcmd();
167*7c478bd9Sstevel@tonic-gate 		return (NO);
168*7c478bd9Sstevel@tonic-gate 	case ' ':	/* display next page */
169*7c478bd9Sstevel@tonic-gate 	case '+':
170*7c478bd9Sstevel@tonic-gate 	case ctrl('V'):
171*7c478bd9Sstevel@tonic-gate 	case KEY_NPAGE:
172*7c478bd9Sstevel@tonic-gate 		/* don't redisplay if there are no lines */
173*7c478bd9Sstevel@tonic-gate 		if (totallines == 0) {
174*7c478bd9Sstevel@tonic-gate 			return (NO);
175*7c478bd9Sstevel@tonic-gate 		}
176*7c478bd9Sstevel@tonic-gate 		/*
177*7c478bd9Sstevel@tonic-gate 		 * note: seekline() is not used to move to the next
178*7c478bd9Sstevel@tonic-gate 		 * page because display() leaves the file pointer at
179*7c478bd9Sstevel@tonic-gate 		 * the next page to optimize paging forward
180*7c478bd9Sstevel@tonic-gate 		 */
181*7c478bd9Sstevel@tonic-gate 		break;
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 	case '-':	/* display previous page */
184*7c478bd9Sstevel@tonic-gate 	case KEY_PPAGE:
185*7c478bd9Sstevel@tonic-gate 		/* don't redisplay if there are no lines */
186*7c478bd9Sstevel@tonic-gate 		if (totallines == 0) {
187*7c478bd9Sstevel@tonic-gate 			return (NO);
188*7c478bd9Sstevel@tonic-gate 		}
189*7c478bd9Sstevel@tonic-gate 		i = topline;		/* save the current top line */
190*7c478bd9Sstevel@tonic-gate 		nextline = topline;	/* go back to this page */
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 		/* if on first page but not at beginning, go to beginning */
193*7c478bd9Sstevel@tonic-gate 		if (nextline > 1 && nextline <= mdisprefs) {
194*7c478bd9Sstevel@tonic-gate 			nextline = 1;
195*7c478bd9Sstevel@tonic-gate 		} else {	/* go back the maximum displayable lines */
196*7c478bd9Sstevel@tonic-gate 			nextline -= mdisprefs;
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 			/* if this was the first page, go to the last page */
199*7c478bd9Sstevel@tonic-gate 			if (nextline < 1) {
200*7c478bd9Sstevel@tonic-gate 				nextline = totallines - mdisprefs + 1;
201*7c478bd9Sstevel@tonic-gate 				if (nextline < 1) {
202*7c478bd9Sstevel@tonic-gate 					nextline = 1;
203*7c478bd9Sstevel@tonic-gate 				}
204*7c478bd9Sstevel@tonic-gate 				/* old top is past last line */
205*7c478bd9Sstevel@tonic-gate 				i = totallines + 1;
206*7c478bd9Sstevel@tonic-gate 			}
207*7c478bd9Sstevel@tonic-gate 		}
208*7c478bd9Sstevel@tonic-gate 		/*
209*7c478bd9Sstevel@tonic-gate 		 * move down til the bottom line is just before the
210*7c478bd9Sstevel@tonic-gate 		 * previous top line
211*7c478bd9Sstevel@tonic-gate 		 */
212*7c478bd9Sstevel@tonic-gate 		c = nextline;
213*7c478bd9Sstevel@tonic-gate 		for (;;) {
214*7c478bd9Sstevel@tonic-gate 			seekline(nextline);
215*7c478bd9Sstevel@tonic-gate 			display();
216*7c478bd9Sstevel@tonic-gate 			if (i - bottomline <= 0) {
217*7c478bd9Sstevel@tonic-gate 				break;
218*7c478bd9Sstevel@tonic-gate 			}
219*7c478bd9Sstevel@tonic-gate 			nextline = ++c;
220*7c478bd9Sstevel@tonic-gate 		}
221*7c478bd9Sstevel@tonic-gate 		return (NO);	/* display already up to date */
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 	case '>':	/* write or append the lines to a file */
224*7c478bd9Sstevel@tonic-gate 		if (totallines == 0) {
225*7c478bd9Sstevel@tonic-gate 			putmsg("There are no lines to write to a file");
226*7c478bd9Sstevel@tonic-gate 		} else {	/* get the file name */
227*7c478bd9Sstevel@tonic-gate 			(void) move(PRLINE, 0);
228*7c478bd9Sstevel@tonic-gate 			(void) addstr("Write to file: ");
229*7c478bd9Sstevel@tonic-gate 			s = "w";
230*7c478bd9Sstevel@tonic-gate 			if ((c = mygetch()) == '>') {
231*7c478bd9Sstevel@tonic-gate 				(void) move(PRLINE, 0);
232*7c478bd9Sstevel@tonic-gate 				(void) addstr(appendprompt);
233*7c478bd9Sstevel@tonic-gate 				c = '\0';
234*7c478bd9Sstevel@tonic-gate 				s = "a";
235*7c478bd9Sstevel@tonic-gate 			}
236*7c478bd9Sstevel@tonic-gate 			if (c != '\r' && c != '\n' && c != KEY_ENTER &&
237*7c478bd9Sstevel@tonic-gate 			    c != KEY_BREAK &&
238*7c478bd9Sstevel@tonic-gate 			    getline(newpat, COLS - sizeof (appendprompt), c,
239*7c478bd9Sstevel@tonic-gate 			    NO) > 0) {
240*7c478bd9Sstevel@tonic-gate 				shellpath(filename, sizeof (filename), newpat);
241*7c478bd9Sstevel@tonic-gate 				if ((file = fopen(filename, s)) == NULL) {
242*7c478bd9Sstevel@tonic-gate 					cannotopen(filename);
243*7c478bd9Sstevel@tonic-gate 				} else {
244*7c478bd9Sstevel@tonic-gate 					seekline(1);
245*7c478bd9Sstevel@tonic-gate 					while ((c = getc(refsfound)) != EOF) {
246*7c478bd9Sstevel@tonic-gate 						(void) putc(c, file);
247*7c478bd9Sstevel@tonic-gate 					}
248*7c478bd9Sstevel@tonic-gate 					seekline(topline);
249*7c478bd9Sstevel@tonic-gate 					(void) fclose(file);
250*7c478bd9Sstevel@tonic-gate 				}
251*7c478bd9Sstevel@tonic-gate 			}
252*7c478bd9Sstevel@tonic-gate 			clearprompt();
253*7c478bd9Sstevel@tonic-gate 		}
254*7c478bd9Sstevel@tonic-gate 		return (NO);	/* return to the previous field */
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate 	case '<':	/* read lines from a file */
257*7c478bd9Sstevel@tonic-gate 		(void) move(PRLINE, 0);
258*7c478bd9Sstevel@tonic-gate 		(void) addstr(readprompt);
259*7c478bd9Sstevel@tonic-gate 		if (getline(newpat, COLS - sizeof (readprompt), '\0',
260*7c478bd9Sstevel@tonic-gate 		    NO) > 0) {
261*7c478bd9Sstevel@tonic-gate 			clearprompt();
262*7c478bd9Sstevel@tonic-gate 			shellpath(filename, sizeof (filename), newpat);
263*7c478bd9Sstevel@tonic-gate 			if (readrefs(filename) == NO) {
264*7c478bd9Sstevel@tonic-gate 				putmsg2("Ignoring an empty file");
265*7c478bd9Sstevel@tonic-gate 				return (NO);
266*7c478bd9Sstevel@tonic-gate 			}
267*7c478bd9Sstevel@tonic-gate 			return (YES);
268*7c478bd9Sstevel@tonic-gate 		}
269*7c478bd9Sstevel@tonic-gate 		clearprompt();
270*7c478bd9Sstevel@tonic-gate 		return (NO);
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate 	case '^':	/* pipe the lines through a shell command */
273*7c478bd9Sstevel@tonic-gate 	case '|':	/* pipe the lines to a shell command */
274*7c478bd9Sstevel@tonic-gate 		if (totallines == 0) {
275*7c478bd9Sstevel@tonic-gate 			putmsg("There are no lines to pipe to a shell command");
276*7c478bd9Sstevel@tonic-gate 			return (NO);
277*7c478bd9Sstevel@tonic-gate 		}
278*7c478bd9Sstevel@tonic-gate 		/* get the shell command */
279*7c478bd9Sstevel@tonic-gate 		(void) move(PRLINE, 0);
280*7c478bd9Sstevel@tonic-gate 		(void) addstr(pipeprompt);
281*7c478bd9Sstevel@tonic-gate 		if (getline(newpat,
282*7c478bd9Sstevel@tonic-gate 		    COLS - sizeof (pipeprompt), '\0', NO) == 0) {
283*7c478bd9Sstevel@tonic-gate 			clearprompt();
284*7c478bd9Sstevel@tonic-gate 			return (NO);
285*7c478bd9Sstevel@tonic-gate 		}
286*7c478bd9Sstevel@tonic-gate 		/* if the ^ command, redirect output to a temp file */
287*7c478bd9Sstevel@tonic-gate 		if (commandc == '^') {
288*7c478bd9Sstevel@tonic-gate 			(void) strcat(strcat(newpat, " >"), temp2);
289*7c478bd9Sstevel@tonic-gate 		}
290*7c478bd9Sstevel@tonic-gate 		exitcurses();
291*7c478bd9Sstevel@tonic-gate 		if ((file = mypopen(newpat, "w")) == NULL) {
292*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
293*7c478bd9Sstevel@tonic-gate 			    "cscope: cannot open pipe to shell command: %s\n",
294*7c478bd9Sstevel@tonic-gate 			    newpat);
295*7c478bd9Sstevel@tonic-gate 		} else {
296*7c478bd9Sstevel@tonic-gate 			seekline(1);
297*7c478bd9Sstevel@tonic-gate 			while ((c = getc(refsfound)) != EOF) {
298*7c478bd9Sstevel@tonic-gate 				(void) putc(c, file);
299*7c478bd9Sstevel@tonic-gate 			}
300*7c478bd9Sstevel@tonic-gate 			seekline(topline);
301*7c478bd9Sstevel@tonic-gate 			(void) mypclose(file);
302*7c478bd9Sstevel@tonic-gate 		}
303*7c478bd9Sstevel@tonic-gate 		if (commandc == '^') {
304*7c478bd9Sstevel@tonic-gate 			if (readrefs(temp2) == NO) {
305*7c478bd9Sstevel@tonic-gate 				putmsg("Ignoring empty output of ^ command");
306*7c478bd9Sstevel@tonic-gate 			}
307*7c478bd9Sstevel@tonic-gate 		}
308*7c478bd9Sstevel@tonic-gate 		askforreturn();
309*7c478bd9Sstevel@tonic-gate 		entercurses();
310*7c478bd9Sstevel@tonic-gate 		break;
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate 	case ctrl('L'):	/* redraw screen */
313*7c478bd9Sstevel@tonic-gate 	case KEY_CLEAR:
314*7c478bd9Sstevel@tonic-gate 		(void) clearok(curscr, TRUE);
315*7c478bd9Sstevel@tonic-gate 		(void) wrefresh(curscr);
316*7c478bd9Sstevel@tonic-gate 		drawscrollbar(topline, bottomline, totallines);
317*7c478bd9Sstevel@tonic-gate 		return (NO);
318*7c478bd9Sstevel@tonic-gate 
319*7c478bd9Sstevel@tonic-gate 	case '!':	/* shell escape */
320*7c478bd9Sstevel@tonic-gate 		(void) execute(shell, shell, (char *)NULL);
321*7c478bd9Sstevel@tonic-gate 		seekline(topline);
322*7c478bd9Sstevel@tonic-gate 		break;
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate 	case '?':	/* help */
325*7c478bd9Sstevel@tonic-gate 		(void) clear();
326*7c478bd9Sstevel@tonic-gate 		help();
327*7c478bd9Sstevel@tonic-gate 		(void) clear();
328*7c478bd9Sstevel@tonic-gate 		seekline(topline);
329*7c478bd9Sstevel@tonic-gate 		break;
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate 	case ctrl('E'):	/* edit all lines */
332*7c478bd9Sstevel@tonic-gate 		editall();
333*7c478bd9Sstevel@tonic-gate 		break;
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate 	case ctrl('A'):	/* repeat last pattern */
336*7c478bd9Sstevel@tonic-gate 	case ctrl('Y'):	/* (old command) */
337*7c478bd9Sstevel@tonic-gate 		if (*pattern != '\0') {
338*7c478bd9Sstevel@tonic-gate 			(void) addstr(pattern);
339*7c478bd9Sstevel@tonic-gate 			goto repeat;
340*7c478bd9Sstevel@tonic-gate 		}
341*7c478bd9Sstevel@tonic-gate 		break;
342*7c478bd9Sstevel@tonic-gate 
343*7c478bd9Sstevel@tonic-gate 	case ctrl('B'):		/* cmd history back */
344*7c478bd9Sstevel@tonic-gate 	case ctrl('F'):		/* cmd history fwd */
345*7c478bd9Sstevel@tonic-gate 		curritem = currentcmd();
346*7c478bd9Sstevel@tonic-gate 		item = (commandc == ctrl('F')) ? nextcmd() : prevcmd();
347*7c478bd9Sstevel@tonic-gate 		clearmsg2();
348*7c478bd9Sstevel@tonic-gate 		if (curritem == item) {
349*7c478bd9Sstevel@tonic-gate 			/* inform user that we're at history end */
350*7c478bd9Sstevel@tonic-gate 			putmsg2(
351*7c478bd9Sstevel@tonic-gate 			    "End of input field and search pattern history");
352*7c478bd9Sstevel@tonic-gate 		}
353*7c478bd9Sstevel@tonic-gate 		if (item) {
354*7c478bd9Sstevel@tonic-gate 			field = item->field;
355*7c478bd9Sstevel@tonic-gate 			setfield();
356*7c478bd9Sstevel@tonic-gate 			atfield();
357*7c478bd9Sstevel@tonic-gate 			(void) addstr(item->text);
358*7c478bd9Sstevel@tonic-gate 			(void) strcpy(pattern, item->text);
359*7c478bd9Sstevel@tonic-gate 			switch (c = mygetch()) {
360*7c478bd9Sstevel@tonic-gate 			case '\r':
361*7c478bd9Sstevel@tonic-gate 			case '\n':
362*7c478bd9Sstevel@tonic-gate 			case KEY_ENTER:
363*7c478bd9Sstevel@tonic-gate 				goto repeat;
364*7c478bd9Sstevel@tonic-gate 			default:
365*7c478bd9Sstevel@tonic-gate 				ungetch(c);
366*7c478bd9Sstevel@tonic-gate 				atfield();
367*7c478bd9Sstevel@tonic-gate 				(void) clrtoeol(); /* clear current field */
368*7c478bd9Sstevel@tonic-gate 				break;
369*7c478bd9Sstevel@tonic-gate 			}
370*7c478bd9Sstevel@tonic-gate 		}
371*7c478bd9Sstevel@tonic-gate 		return (NO);
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate 	case '\\':	/* next character is not a command */
374*7c478bd9Sstevel@tonic-gate 		(void) addch('\\');	/* display the quote character */
375*7c478bd9Sstevel@tonic-gate 
376*7c478bd9Sstevel@tonic-gate 		/* get a character from the terminal */
377*7c478bd9Sstevel@tonic-gate 		if ((commandc = mygetch()) == EOF) {
378*7c478bd9Sstevel@tonic-gate 			return (NO);	/* quit */
379*7c478bd9Sstevel@tonic-gate 		}
380*7c478bd9Sstevel@tonic-gate 		(void) addstr("\b \b");	/* erase the quote character */
381*7c478bd9Sstevel@tonic-gate 		goto ispat;
382*7c478bd9Sstevel@tonic-gate 
383*7c478bd9Sstevel@tonic-gate 	case '.':
384*7c478bd9Sstevel@tonic-gate 		atfield();	/* move back to the input field */
385*7c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
386*7c478bd9Sstevel@tonic-gate 	default:
387*7c478bd9Sstevel@tonic-gate 		/* edit a selected line */
388*7c478bd9Sstevel@tonic-gate 		if (isdigit(commandc) && commandc != '0' && !mouse) {
389*7c478bd9Sstevel@tonic-gate 			if (returnrequired == NO) {
390*7c478bd9Sstevel@tonic-gate 				editref(commandc - '1');
391*7c478bd9Sstevel@tonic-gate 			} else {
392*7c478bd9Sstevel@tonic-gate 				(void) move(PRLINE, 0);
393*7c478bd9Sstevel@tonic-gate 				(void) addstr(selectionprompt);
394*7c478bd9Sstevel@tonic-gate 				if (getline(newpat,
395*7c478bd9Sstevel@tonic-gate 				    COLS - sizeof (selectionprompt), commandc,
396*7c478bd9Sstevel@tonic-gate 				    NO) > 0 &&
397*7c478bd9Sstevel@tonic-gate 				    (i = atoi(newpat)) > 0) {
398*7c478bd9Sstevel@tonic-gate 					editref(i - 1);
399*7c478bd9Sstevel@tonic-gate 				}
400*7c478bd9Sstevel@tonic-gate 				clearprompt();
401*7c478bd9Sstevel@tonic-gate 			}
402*7c478bd9Sstevel@tonic-gate 		} else if (isprint(commandc)) {
403*7c478bd9Sstevel@tonic-gate 			/* this is the start of a pattern */
404*7c478bd9Sstevel@tonic-gate ispat:
405*7c478bd9Sstevel@tonic-gate 			if (getline(newpat, COLS - fldcolumn - 1, commandc,
406*7c478bd9Sstevel@tonic-gate 			    caseless) > 0) {
407*7c478bd9Sstevel@tonic-gate 					(void) strcpy(pattern, newpat);
408*7c478bd9Sstevel@tonic-gate 					resetcmd();	/* reset history */
409*7c478bd9Sstevel@tonic-gate repeat:
410*7c478bd9Sstevel@tonic-gate 				addcmd(field, pattern);	/* add to history */
411*7c478bd9Sstevel@tonic-gate 				if (field == CHANGE) {
412*7c478bd9Sstevel@tonic-gate 					/* prompt for the new text */
413*7c478bd9Sstevel@tonic-gate 					(void) move(PRLINE, 0);
414*7c478bd9Sstevel@tonic-gate 					(void) addstr(toprompt);
415*7c478bd9Sstevel@tonic-gate 					(void) getline(newpat,
416*7c478bd9Sstevel@tonic-gate 					    COLS - sizeof (toprompt), '\0', NO);
417*7c478bd9Sstevel@tonic-gate 				}
418*7c478bd9Sstevel@tonic-gate 				/* search for the pattern */
419*7c478bd9Sstevel@tonic-gate 				if (search() == YES) {
420*7c478bd9Sstevel@tonic-gate 					switch (field) {
421*7c478bd9Sstevel@tonic-gate 					case DEFINITION:
422*7c478bd9Sstevel@tonic-gate 					case FILENAME:
423*7c478bd9Sstevel@tonic-gate 						if (totallines > 1) {
424*7c478bd9Sstevel@tonic-gate 							break;
425*7c478bd9Sstevel@tonic-gate 						}
426*7c478bd9Sstevel@tonic-gate 						topline = 1;
427*7c478bd9Sstevel@tonic-gate 						editref(0);
428*7c478bd9Sstevel@tonic-gate 						break;
429*7c478bd9Sstevel@tonic-gate 					case CHANGE:
430*7c478bd9Sstevel@tonic-gate 						return (changestring());
431*7c478bd9Sstevel@tonic-gate 					}
432*7c478bd9Sstevel@tonic-gate 				} else if (field == FILENAME &&
433*7c478bd9Sstevel@tonic-gate 				    access(newpat, READ) == 0) {
434*7c478bd9Sstevel@tonic-gate 					/* try to edit the file anyway */
435*7c478bd9Sstevel@tonic-gate 					edit(newpat, "1");
436*7c478bd9Sstevel@tonic-gate 				}
437*7c478bd9Sstevel@tonic-gate 			} else {	/* no pattern--the input was erased */
438*7c478bd9Sstevel@tonic-gate 				return (NO);
439*7c478bd9Sstevel@tonic-gate 			}
440*7c478bd9Sstevel@tonic-gate 		} else {	/* control character */
441*7c478bd9Sstevel@tonic-gate 			return (NO);
442*7c478bd9Sstevel@tonic-gate 		}
443*7c478bd9Sstevel@tonic-gate 	}
444*7c478bd9Sstevel@tonic-gate 	return (YES);
445*7c478bd9Sstevel@tonic-gate }
446*7c478bd9Sstevel@tonic-gate 
447*7c478bd9Sstevel@tonic-gate /* clear the prompt line */
448*7c478bd9Sstevel@tonic-gate 
449*7c478bd9Sstevel@tonic-gate void
450*7c478bd9Sstevel@tonic-gate clearprompt(void)
451*7c478bd9Sstevel@tonic-gate {
452*7c478bd9Sstevel@tonic-gate 	(void) move(PRLINE, 0);
453*7c478bd9Sstevel@tonic-gate 	(void) clrtoeol();
454*7c478bd9Sstevel@tonic-gate }
455*7c478bd9Sstevel@tonic-gate 
456*7c478bd9Sstevel@tonic-gate /* read references from a file */
457*7c478bd9Sstevel@tonic-gate 
458*7c478bd9Sstevel@tonic-gate BOOL
459*7c478bd9Sstevel@tonic-gate readrefs(char *filename)
460*7c478bd9Sstevel@tonic-gate {
461*7c478bd9Sstevel@tonic-gate 	FILE	*file;
462*7c478bd9Sstevel@tonic-gate 	int	c;
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate 	if ((file = fopen(filename, "r")) == NULL) {
465*7c478bd9Sstevel@tonic-gate 		cannotopen(filename);
466*7c478bd9Sstevel@tonic-gate 		return (NO);
467*7c478bd9Sstevel@tonic-gate 	}
468*7c478bd9Sstevel@tonic-gate 	if ((c = getc(file)) == EOF) {	/* if file is empty */
469*7c478bd9Sstevel@tonic-gate 		return (NO);
470*7c478bd9Sstevel@tonic-gate 	}
471*7c478bd9Sstevel@tonic-gate 	totallines = 0;
472*7c478bd9Sstevel@tonic-gate 	nextline = 1;
473*7c478bd9Sstevel@tonic-gate 	if (writerefsfound() == YES) {
474*7c478bd9Sstevel@tonic-gate 		(void) putc(c, refsfound);
475*7c478bd9Sstevel@tonic-gate 		while ((c = getc(file)) != EOF) {
476*7c478bd9Sstevel@tonic-gate 			(void) putc(c, refsfound);
477*7c478bd9Sstevel@tonic-gate 		}
478*7c478bd9Sstevel@tonic-gate 		(void) fclose(file);
479*7c478bd9Sstevel@tonic-gate 		(void) freopen(temp1, "r", refsfound);
480*7c478bd9Sstevel@tonic-gate 		countrefs();
481*7c478bd9Sstevel@tonic-gate 	}
482*7c478bd9Sstevel@tonic-gate 	return (YES);
483*7c478bd9Sstevel@tonic-gate }
484*7c478bd9Sstevel@tonic-gate 
485*7c478bd9Sstevel@tonic-gate /* change one text string to another */
486*7c478bd9Sstevel@tonic-gate 
487*7c478bd9Sstevel@tonic-gate BOOL
488*7c478bd9Sstevel@tonic-gate changestring(void)
489*7c478bd9Sstevel@tonic-gate {
490*7c478bd9Sstevel@tonic-gate 	char	buf[PATLEN + 1];	/* input buffer */
491*7c478bd9Sstevel@tonic-gate 	char	newfile[PATHLEN + 1];	/* new file name */
492*7c478bd9Sstevel@tonic-gate 	char	oldfile[PATHLEN + 1];	/* old file name */
493*7c478bd9Sstevel@tonic-gate 	char	linenum[NUMLEN + 1];	/* file line number */
494*7c478bd9Sstevel@tonic-gate 	char	msg[MSGLEN + 1];	/* message */
495*7c478bd9Sstevel@tonic-gate 	FILE	*script;		/* shell script file */
496*7c478bd9Sstevel@tonic-gate 	BOOL	anymarked = NO;		/* any line marked */
497*7c478bd9Sstevel@tonic-gate 	MOUSEEVENT *p;			/* mouse data */
498*7c478bd9Sstevel@tonic-gate 	int	c, i;
499*7c478bd9Sstevel@tonic-gate 	char	*s;
500*7c478bd9Sstevel@tonic-gate 
501*7c478bd9Sstevel@tonic-gate 	/* open the temporary file */
502*7c478bd9Sstevel@tonic-gate 	if ((script = fopen(temp2, "w")) == NULL) {
503*7c478bd9Sstevel@tonic-gate 		cannotopen(temp2);
504*7c478bd9Sstevel@tonic-gate 		return (NO);
505*7c478bd9Sstevel@tonic-gate 	}
506*7c478bd9Sstevel@tonic-gate 	/* create the line change indicators */
507*7c478bd9Sstevel@tonic-gate 	change = (BOOL *)mycalloc((unsigned)totallines, sizeof (BOOL));
508*7c478bd9Sstevel@tonic-gate 	changing = YES;
509*7c478bd9Sstevel@tonic-gate 	initmenu();
510*7c478bd9Sstevel@tonic-gate 
511*7c478bd9Sstevel@tonic-gate 	/* until the quit command is entered */
512*7c478bd9Sstevel@tonic-gate 	for (;;) {
513*7c478bd9Sstevel@tonic-gate 		/* display the current page of lines */
514*7c478bd9Sstevel@tonic-gate 		display();
515*7c478bd9Sstevel@tonic-gate 	same:
516*7c478bd9Sstevel@tonic-gate 		/* get a character from the terminal */
517*7c478bd9Sstevel@tonic-gate 		(void) move(PRLINE, 0);
518*7c478bd9Sstevel@tonic-gate 		(void) addstr(
519*7c478bd9Sstevel@tonic-gate 		    "Select lines to change (press the ? key for help): ");
520*7c478bd9Sstevel@tonic-gate 		if ((c = mygetch()) == EOF || c == ctrl('D') ||
521*7c478bd9Sstevel@tonic-gate 		    c == ctrl('Z')) {
522*7c478bd9Sstevel@tonic-gate 			break;	/* change lines */
523*7c478bd9Sstevel@tonic-gate 		}
524*7c478bd9Sstevel@tonic-gate 		/* see if the input character is a command */
525*7c478bd9Sstevel@tonic-gate 		switch (c) {
526*7c478bd9Sstevel@tonic-gate 		case ' ':	/* display next page */
527*7c478bd9Sstevel@tonic-gate 		case '+':
528*7c478bd9Sstevel@tonic-gate 		case ctrl('V'):
529*7c478bd9Sstevel@tonic-gate 		case KEY_NPAGE:
530*7c478bd9Sstevel@tonic-gate 		case '-':	/* display previous page */
531*7c478bd9Sstevel@tonic-gate 		case KEY_PPAGE:
532*7c478bd9Sstevel@tonic-gate 		case '!':	/* shell escape */
533*7c478bd9Sstevel@tonic-gate 		case '?':	/* help */
534*7c478bd9Sstevel@tonic-gate 			(void) command(c);
535*7c478bd9Sstevel@tonic-gate 			break;
536*7c478bd9Sstevel@tonic-gate 
537*7c478bd9Sstevel@tonic-gate 		case ctrl('L'):	/* redraw screen */
538*7c478bd9Sstevel@tonic-gate 		case KEY_CLEAR:
539*7c478bd9Sstevel@tonic-gate 			(void) command(c);
540*7c478bd9Sstevel@tonic-gate 			goto same;
541*7c478bd9Sstevel@tonic-gate 
542*7c478bd9Sstevel@tonic-gate 		case ESC:	/* kept for backwards compatibility */
543*7c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
544*7c478bd9Sstevel@tonic-gate 
545*7c478bd9Sstevel@tonic-gate 		case '\r':	/* don't change lines */
546*7c478bd9Sstevel@tonic-gate 		case '\n':
547*7c478bd9Sstevel@tonic-gate 		case KEY_ENTER:
548*7c478bd9Sstevel@tonic-gate 		case KEY_BREAK:
549*7c478bd9Sstevel@tonic-gate 		case ctrl('G'):
550*7c478bd9Sstevel@tonic-gate 			clearprompt();
551*7c478bd9Sstevel@tonic-gate 			goto nochange;
552*7c478bd9Sstevel@tonic-gate 
553*7c478bd9Sstevel@tonic-gate 		case '*':	/* mark/unmark all displayed lines */
554*7c478bd9Sstevel@tonic-gate 			for (i = 0; topline + i < nextline; ++i) {
555*7c478bd9Sstevel@tonic-gate 				mark(i);
556*7c478bd9Sstevel@tonic-gate 			}
557*7c478bd9Sstevel@tonic-gate 			goto same;
558*7c478bd9Sstevel@tonic-gate 
559*7c478bd9Sstevel@tonic-gate 		case 'a':	/* mark/unmark all lines */
560*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < totallines; ++i) {
561*7c478bd9Sstevel@tonic-gate 				if (change[i] == NO) {
562*7c478bd9Sstevel@tonic-gate 					change[i] = YES;
563*7c478bd9Sstevel@tonic-gate 				} else {
564*7c478bd9Sstevel@tonic-gate 					change[i] = NO;
565*7c478bd9Sstevel@tonic-gate 				}
566*7c478bd9Sstevel@tonic-gate 			}
567*7c478bd9Sstevel@tonic-gate 			/* show that all have been marked */
568*7c478bd9Sstevel@tonic-gate 			seekline(totallines);
569*7c478bd9Sstevel@tonic-gate 			break;
570*7c478bd9Sstevel@tonic-gate 		case ctrl('X'):	/* mouse selection */
571*7c478bd9Sstevel@tonic-gate 			if ((p = getmouseevent()) == NULL) {
572*7c478bd9Sstevel@tonic-gate 				goto same;	/* unknown control sequence */
573*7c478bd9Sstevel@tonic-gate 			}
574*7c478bd9Sstevel@tonic-gate 			/* if the button number is a scrollbar tag */
575*7c478bd9Sstevel@tonic-gate 			if (p->button == '0') {
576*7c478bd9Sstevel@tonic-gate 				scrollbar(p);
577*7c478bd9Sstevel@tonic-gate 				break;
578*7c478bd9Sstevel@tonic-gate 			}
579*7c478bd9Sstevel@tonic-gate 			/* find the selected line */
580*7c478bd9Sstevel@tonic-gate 			/* note: the selection is forced into range */
581*7c478bd9Sstevel@tonic-gate 			for (i = disprefs - 1; i > 0; --i) {
582*7c478bd9Sstevel@tonic-gate 				if (p->y1 >= displine[i]) {
583*7c478bd9Sstevel@tonic-gate 					break;
584*7c478bd9Sstevel@tonic-gate 				}
585*7c478bd9Sstevel@tonic-gate 			}
586*7c478bd9Sstevel@tonic-gate 			mark(i);
587*7c478bd9Sstevel@tonic-gate 			goto same;
588*7c478bd9Sstevel@tonic-gate 		default:
589*7c478bd9Sstevel@tonic-gate 			/* if a line was selected */
590*7c478bd9Sstevel@tonic-gate 			if (isdigit(c) && c != '0' && !mouse) {
591*7c478bd9Sstevel@tonic-gate 				if (returnrequired == NO) {
592*7c478bd9Sstevel@tonic-gate 					mark(c - '1');
593*7c478bd9Sstevel@tonic-gate 				} else {
594*7c478bd9Sstevel@tonic-gate 					clearprompt();
595*7c478bd9Sstevel@tonic-gate 					(void) move(PRLINE, 0);
596*7c478bd9Sstevel@tonic-gate 					(void) addstr(selectionprompt);
597*7c478bd9Sstevel@tonic-gate 					if (getline(buf,
598*7c478bd9Sstevel@tonic-gate 					    COLS - sizeof (selectionprompt), c,
599*7c478bd9Sstevel@tonic-gate 					    NO) > 0 &&
600*7c478bd9Sstevel@tonic-gate 					    (i = atoi(buf)) > 0) {
601*7c478bd9Sstevel@tonic-gate 						mark(i - 1);
602*7c478bd9Sstevel@tonic-gate 					}
603*7c478bd9Sstevel@tonic-gate 				}
604*7c478bd9Sstevel@tonic-gate 			}
605*7c478bd9Sstevel@tonic-gate 			goto same;
606*7c478bd9Sstevel@tonic-gate 		}
607*7c478bd9Sstevel@tonic-gate 	}
608*7c478bd9Sstevel@tonic-gate 	/* for each line containing the old text */
609*7c478bd9Sstevel@tonic-gate 	(void) fprintf(script, "ed - <<\\!\nH\n");
610*7c478bd9Sstevel@tonic-gate 	*oldfile = '\0';
611*7c478bd9Sstevel@tonic-gate 	seekline(1);
612*7c478bd9Sstevel@tonic-gate 	for (i = 0; fscanf(refsfound, "%s%*s%s%*[^\n]", newfile, linenum) == 2;
613*7c478bd9Sstevel@tonic-gate 	    ++i) {
614*7c478bd9Sstevel@tonic-gate 		/* see if the line is to be changed */
615*7c478bd9Sstevel@tonic-gate 		if (change[i] == YES) {
616*7c478bd9Sstevel@tonic-gate 			anymarked = YES;
617*7c478bd9Sstevel@tonic-gate 
618*7c478bd9Sstevel@tonic-gate 			/* if this is a new file */
619*7c478bd9Sstevel@tonic-gate 			if (strcmp(newfile, oldfile) != 0) {
620*7c478bd9Sstevel@tonic-gate 
621*7c478bd9Sstevel@tonic-gate 				/* make sure it can be changed */
622*7c478bd9Sstevel@tonic-gate 				if (access(newfile, WRITE) != 0) {
623*7c478bd9Sstevel@tonic-gate 					(void) sprintf(msg,
624*7c478bd9Sstevel@tonic-gate 					    "Cannot write to file %s",
625*7c478bd9Sstevel@tonic-gate 					    newfile);
626*7c478bd9Sstevel@tonic-gate 					putmsg(msg);
627*7c478bd9Sstevel@tonic-gate 					anymarked = NO;
628*7c478bd9Sstevel@tonic-gate 					break;
629*7c478bd9Sstevel@tonic-gate 				}
630*7c478bd9Sstevel@tonic-gate 				/* if there was an old file */
631*7c478bd9Sstevel@tonic-gate 				if (*oldfile != '\0') {
632*7c478bd9Sstevel@tonic-gate 					(void) fprintf(script,
633*7c478bd9Sstevel@tonic-gate 					    "w\n");	/* save it */
634*7c478bd9Sstevel@tonic-gate 				}
635*7c478bd9Sstevel@tonic-gate 				/* edit the new file */
636*7c478bd9Sstevel@tonic-gate 				(void) strcpy(oldfile, newfile);
637*7c478bd9Sstevel@tonic-gate 				(void) fprintf(script, "e %s\n", oldfile);
638*7c478bd9Sstevel@tonic-gate 			}
639*7c478bd9Sstevel@tonic-gate 			/* output substitute command */
640*7c478bd9Sstevel@tonic-gate 			(void) fprintf(script,
641*7c478bd9Sstevel@tonic-gate 			    "%ss/", linenum);	/* change */
642*7c478bd9Sstevel@tonic-gate 			for (s = pattern; *s != '\0'; ++s) {	/* old text */
643*7c478bd9Sstevel@tonic-gate 				if (*s == '/') {
644*7c478bd9Sstevel@tonic-gate 					(void) putc('\\', script);
645*7c478bd9Sstevel@tonic-gate 				}
646*7c478bd9Sstevel@tonic-gate 				(void) putc(*s, script);
647*7c478bd9Sstevel@tonic-gate 			}
648*7c478bd9Sstevel@tonic-gate 			(void) putc('/', script);			/* to */
649*7c478bd9Sstevel@tonic-gate 			for (s = newpat; *s != '\0'; ++s) {	/* new text */
650*7c478bd9Sstevel@tonic-gate 				if (strchr("/\\&", *s) != NULL) {
651*7c478bd9Sstevel@tonic-gate 					(void) putc('\\', script);
652*7c478bd9Sstevel@tonic-gate 				}
653*7c478bd9Sstevel@tonic-gate 				(void) putc(*s, script);
654*7c478bd9Sstevel@tonic-gate 			}
655*7c478bd9Sstevel@tonic-gate 			(void) fprintf(script, "/gp\n");	/* and print */
656*7c478bd9Sstevel@tonic-gate 		}
657*7c478bd9Sstevel@tonic-gate 	}
658*7c478bd9Sstevel@tonic-gate 	(void) fprintf(script, "w\nq\n!\n");	/* write and quit */
659*7c478bd9Sstevel@tonic-gate 	(void) fclose(script);
660*7c478bd9Sstevel@tonic-gate 	clearprompt();
661*7c478bd9Sstevel@tonic-gate 
662*7c478bd9Sstevel@tonic-gate 	/* if any line was marked */
663*7c478bd9Sstevel@tonic-gate 	if (anymarked == YES) {
664*7c478bd9Sstevel@tonic-gate 		/* edit the files */
665*7c478bd9Sstevel@tonic-gate 		(void) refresh();
666*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Changed lines:\n\r");
667*7c478bd9Sstevel@tonic-gate 		(void) execute(shell, shell, temp2, (char *)NULL);
668*7c478bd9Sstevel@tonic-gate 		askforreturn();
669*7c478bd9Sstevel@tonic-gate 	}
670*7c478bd9Sstevel@tonic-gate nochange:
671*7c478bd9Sstevel@tonic-gate 	changing = NO;
672*7c478bd9Sstevel@tonic-gate 	initmenu();
673*7c478bd9Sstevel@tonic-gate 	free(change);
674*7c478bd9Sstevel@tonic-gate 	seekline(topline);
675*7c478bd9Sstevel@tonic-gate 	return (YES);	/* clear any marks on exit without change */
676*7c478bd9Sstevel@tonic-gate }
677*7c478bd9Sstevel@tonic-gate 
678*7c478bd9Sstevel@tonic-gate /* mark/unmark this displayed line to be changed */
679*7c478bd9Sstevel@tonic-gate 
680*7c478bd9Sstevel@tonic-gate void
681*7c478bd9Sstevel@tonic-gate mark(int i)
682*7c478bd9Sstevel@tonic-gate {
683*7c478bd9Sstevel@tonic-gate 	int	j;
684*7c478bd9Sstevel@tonic-gate 
685*7c478bd9Sstevel@tonic-gate 	j = i + topline - 1;
686*7c478bd9Sstevel@tonic-gate 	if (j < totallines) {
687*7c478bd9Sstevel@tonic-gate 		(void) move(displine[i], selectlen);
688*7c478bd9Sstevel@tonic-gate 		if (change[j] == NO) {
689*7c478bd9Sstevel@tonic-gate 			change[j] = YES;
690*7c478bd9Sstevel@tonic-gate 			(void) addch('>');
691*7c478bd9Sstevel@tonic-gate 		} else {
692*7c478bd9Sstevel@tonic-gate 			change[j] = NO;
693*7c478bd9Sstevel@tonic-gate 			(void) addch(' ');
694*7c478bd9Sstevel@tonic-gate 		}
695*7c478bd9Sstevel@tonic-gate 	}
696*7c478bd9Sstevel@tonic-gate }
697*7c478bd9Sstevel@tonic-gate 
698*7c478bd9Sstevel@tonic-gate /* scrollbar actions */
699*7c478bd9Sstevel@tonic-gate 
700*7c478bd9Sstevel@tonic-gate static void
701*7c478bd9Sstevel@tonic-gate scrollbar(MOUSEEVENT *p)
702*7c478bd9Sstevel@tonic-gate {
703*7c478bd9Sstevel@tonic-gate 	/* reposition list if it makes sense */
704*7c478bd9Sstevel@tonic-gate 	if (totallines == 0) {
705*7c478bd9Sstevel@tonic-gate 		return;
706*7c478bd9Sstevel@tonic-gate 	}
707*7c478bd9Sstevel@tonic-gate 	switch (p->percent) {
708*7c478bd9Sstevel@tonic-gate 
709*7c478bd9Sstevel@tonic-gate 	case 101: /* scroll down one page */
710*7c478bd9Sstevel@tonic-gate 		if (nextline + mdisprefs > totallines) {
711*7c478bd9Sstevel@tonic-gate 			nextline = totallines - mdisprefs + 1;
712*7c478bd9Sstevel@tonic-gate 		}
713*7c478bd9Sstevel@tonic-gate 		break;
714*7c478bd9Sstevel@tonic-gate 
715*7c478bd9Sstevel@tonic-gate 	case 102: /* scroll up one page */
716*7c478bd9Sstevel@tonic-gate 		nextline = topline - mdisprefs;
717*7c478bd9Sstevel@tonic-gate 		if (nextline < 1) {
718*7c478bd9Sstevel@tonic-gate 			nextline = 1;
719*7c478bd9Sstevel@tonic-gate 		}
720*7c478bd9Sstevel@tonic-gate 		break;
721*7c478bd9Sstevel@tonic-gate 
722*7c478bd9Sstevel@tonic-gate 	case 103: /* scroll down one line */
723*7c478bd9Sstevel@tonic-gate 		nextline = topline + 1;
724*7c478bd9Sstevel@tonic-gate 		break;
725*7c478bd9Sstevel@tonic-gate 
726*7c478bd9Sstevel@tonic-gate 	case 104: /* scroll up one line */
727*7c478bd9Sstevel@tonic-gate 		if (topline > 1) {
728*7c478bd9Sstevel@tonic-gate 			nextline = topline - 1;
729*7c478bd9Sstevel@tonic-gate 		}
730*7c478bd9Sstevel@tonic-gate 		break;
731*7c478bd9Sstevel@tonic-gate 	default:
732*7c478bd9Sstevel@tonic-gate 		nextline = p->percent * totallines / 100;
733*7c478bd9Sstevel@tonic-gate 	}
734*7c478bd9Sstevel@tonic-gate 	seekline(nextline);
735*7c478bd9Sstevel@tonic-gate }
736