xref: /titanic_54/usr/src/tools/cscope-fast/input.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 2004 Sun Microsystems, Inc.  All rights reserved.
28*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
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 cross-reference
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  *	terminal input functions
37*7c478bd9Sstevel@tonic-gate  */
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include "global.h"
40*7c478bd9Sstevel@tonic-gate #include <curses.h>	/* KEY_BACKSPACE, KEY_BREAK, and KEY_ENTER */
41*7c478bd9Sstevel@tonic-gate #include <setjmp.h>	/* jmp_buf */
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate static	jmp_buf	env;		/* setjmp/longjmp buffer */
44*7c478bd9Sstevel@tonic-gate static	int	prevchar;	/* previous, ungotten character */
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate /* catch the interrupt signal */
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
49*7c478bd9Sstevel@tonic-gate SIGTYPE
50*7c478bd9Sstevel@tonic-gate catchint(int sig)
51*7c478bd9Sstevel@tonic-gate {
52*7c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, catchint);
53*7c478bd9Sstevel@tonic-gate 	longjmp(env, 1);
54*7c478bd9Sstevel@tonic-gate }
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate /* unget a character */
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate int
59*7c478bd9Sstevel@tonic-gate ungetch(int c)
60*7c478bd9Sstevel@tonic-gate {
61*7c478bd9Sstevel@tonic-gate 	prevchar = c;
62*7c478bd9Sstevel@tonic-gate 	return (0);
63*7c478bd9Sstevel@tonic-gate }
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate /* get a character from the terminal */
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate int
68*7c478bd9Sstevel@tonic-gate mygetch(void)
69*7c478bd9Sstevel@tonic-gate {
70*7c478bd9Sstevel@tonic-gate 	SIGTYPE	(*savesig)();		/* old value of signal */
71*7c478bd9Sstevel@tonic-gate 	int	c;
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	/* change an interrupt signal to a break key character */
74*7c478bd9Sstevel@tonic-gate 	if (setjmp(env) == 0) {
75*7c478bd9Sstevel@tonic-gate 		savesig = signal(SIGINT, catchint);
76*7c478bd9Sstevel@tonic-gate 		(void) refresh();	/* update the display */
77*7c478bd9Sstevel@tonic-gate 		reinitmouse();	/* curses can change the menu number */
78*7c478bd9Sstevel@tonic-gate 		if (prevchar) {
79*7c478bd9Sstevel@tonic-gate 			c = prevchar;
80*7c478bd9Sstevel@tonic-gate 			prevchar = 0;
81*7c478bd9Sstevel@tonic-gate 		} else {
82*7c478bd9Sstevel@tonic-gate 			c = getch();	/* get a character from the terminal */
83*7c478bd9Sstevel@tonic-gate 		}
84*7c478bd9Sstevel@tonic-gate 	} else {	/* longjmp to here from signal handler */
85*7c478bd9Sstevel@tonic-gate 		c = KEY_BREAK;
86*7c478bd9Sstevel@tonic-gate 	}
87*7c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, savesig);
88*7c478bd9Sstevel@tonic-gate 	return (c);
89*7c478bd9Sstevel@tonic-gate }
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate /* get a line from the terminal in non-canonical mode */
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate int
94*7c478bd9Sstevel@tonic-gate getline(char s[], size_t size, int firstchar, BOOL iscaseless)
95*7c478bd9Sstevel@tonic-gate {
96*7c478bd9Sstevel@tonic-gate 	int	c, i = 0;
97*7c478bd9Sstevel@tonic-gate 	int	j;
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	/* if a character already has been typed */
100*7c478bd9Sstevel@tonic-gate 	if (firstchar != '\0') {
101*7c478bd9Sstevel@tonic-gate 		if (iscaseless == YES) {
102*7c478bd9Sstevel@tonic-gate 			firstchar = tolower(firstchar);
103*7c478bd9Sstevel@tonic-gate 		}
104*7c478bd9Sstevel@tonic-gate 		(void) addch((unsigned)firstchar);	/* display it */
105*7c478bd9Sstevel@tonic-gate 		s[i++] = firstchar;	/* save it */
106*7c478bd9Sstevel@tonic-gate 	}
107*7c478bd9Sstevel@tonic-gate 	/* until the end of the line is reached */
108*7c478bd9Sstevel@tonic-gate 	while ((c = mygetch()) != '\r' && c != '\n' && c != KEY_ENTER &&
109*7c478bd9Sstevel@tonic-gate 	    c != '\003' && c != KEY_BREAK) {
110*7c478bd9Sstevel@tonic-gate 		if (c == erasechar() || c == '\b' ||		/* erase */
111*7c478bd9Sstevel@tonic-gate 		    c == KEY_BACKSPACE) {
112*7c478bd9Sstevel@tonic-gate 			if (i > 0) {
113*7c478bd9Sstevel@tonic-gate 				(void) addstr("\b \b");
114*7c478bd9Sstevel@tonic-gate 				--i;
115*7c478bd9Sstevel@tonic-gate 			}
116*7c478bd9Sstevel@tonic-gate 		} else if (c == killchar()) {			/* kill */
117*7c478bd9Sstevel@tonic-gate 			for (j = 0; j < i; ++j) {
118*7c478bd9Sstevel@tonic-gate 				(void) addch('\b');
119*7c478bd9Sstevel@tonic-gate 			}
120*7c478bd9Sstevel@tonic-gate 			for (j = 0; j < i; ++j) {
121*7c478bd9Sstevel@tonic-gate 				(void) addch(' ');
122*7c478bd9Sstevel@tonic-gate 			}
123*7c478bd9Sstevel@tonic-gate 			for (j = 0; j < i; ++j) {
124*7c478bd9Sstevel@tonic-gate 				(void) addch('\b');
125*7c478bd9Sstevel@tonic-gate 			}
126*7c478bd9Sstevel@tonic-gate 			i = 0;
127*7c478bd9Sstevel@tonic-gate 		} else if (isprint(c) || c == '\t') {		/* printable */
128*7c478bd9Sstevel@tonic-gate 			if (iscaseless == YES) {
129*7c478bd9Sstevel@tonic-gate 				c = tolower(c);
130*7c478bd9Sstevel@tonic-gate 			}
131*7c478bd9Sstevel@tonic-gate 			/* if it will fit on the line */
132*7c478bd9Sstevel@tonic-gate 			if (i < size) {
133*7c478bd9Sstevel@tonic-gate 				(void) addch((unsigned)c);	/* display it */
134*7c478bd9Sstevel@tonic-gate 				s[i++] = c;		/* save it */
135*7c478bd9Sstevel@tonic-gate 			}
136*7c478bd9Sstevel@tonic-gate 		} else if (c == ctrl('X')) {
137*7c478bd9Sstevel@tonic-gate 			/* mouse */
138*7c478bd9Sstevel@tonic-gate 			(void) getmouseevent(); 	/* ignore it */
139*7c478bd9Sstevel@tonic-gate 		} else if (c == EOF) {			/* end-of-file */
140*7c478bd9Sstevel@tonic-gate 			break;
141*7c478bd9Sstevel@tonic-gate 		}
142*7c478bd9Sstevel@tonic-gate 		/* return on an empty line to allow a command to be entered */
143*7c478bd9Sstevel@tonic-gate 		if (firstchar != '\0' && i == 0) {
144*7c478bd9Sstevel@tonic-gate 			break;
145*7c478bd9Sstevel@tonic-gate 		}
146*7c478bd9Sstevel@tonic-gate 	}
147*7c478bd9Sstevel@tonic-gate 	s[i] = '\0';
148*7c478bd9Sstevel@tonic-gate 	return (i);
149*7c478bd9Sstevel@tonic-gate }
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate /* ask user to enter a character after reading the message */
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate void
154*7c478bd9Sstevel@tonic-gate askforchar(void)
155*7c478bd9Sstevel@tonic-gate {
156*7c478bd9Sstevel@tonic-gate 	(void) addstr("Type any character to continue: ");
157*7c478bd9Sstevel@tonic-gate 	(void) mygetch();
158*7c478bd9Sstevel@tonic-gate }
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate /* ask user to press the RETURN key after reading the message */
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate void
163*7c478bd9Sstevel@tonic-gate askforreturn(void)
164*7c478bd9Sstevel@tonic-gate {
165*7c478bd9Sstevel@tonic-gate 	if (linemode == NO) {
166*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Press the RETURN key to continue: ");
167*7c478bd9Sstevel@tonic-gate 		(void) getchar();
168*7c478bd9Sstevel@tonic-gate 	}
169*7c478bd9Sstevel@tonic-gate }
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate /* expand the ~ and $ shell meta characters in a path */
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate void
174*7c478bd9Sstevel@tonic-gate shellpath(char *out, int limit, char *in)
175*7c478bd9Sstevel@tonic-gate {
176*7c478bd9Sstevel@tonic-gate 	char	*lastchar;
177*7c478bd9Sstevel@tonic-gate 	char	*s, *v;
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	/* skip leading white space */
180*7c478bd9Sstevel@tonic-gate 	while (isspace(*in)) {
181*7c478bd9Sstevel@tonic-gate 		++in;
182*7c478bd9Sstevel@tonic-gate 	}
183*7c478bd9Sstevel@tonic-gate 	lastchar = out + limit - 1;
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 	/*
186*7c478bd9Sstevel@tonic-gate 	 * a tilde (~) by itself represents $HOME; followed by a name it
187*7c478bd9Sstevel@tonic-gate 	 * represents the $LOGDIR of that login name
188*7c478bd9Sstevel@tonic-gate 	 */
189*7c478bd9Sstevel@tonic-gate 	if (*in == '~') {
190*7c478bd9Sstevel@tonic-gate 		*out++ = *in++;	/* copy the ~ because it may not be expanded */
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 		/* get the login name */
193*7c478bd9Sstevel@tonic-gate 		s = out;
194*7c478bd9Sstevel@tonic-gate 		while (s < lastchar && *in != '/' && *in != '\0' &&
195*7c478bd9Sstevel@tonic-gate 		    !isspace(*in)) {
196*7c478bd9Sstevel@tonic-gate 			*s++ = *in++;
197*7c478bd9Sstevel@tonic-gate 		}
198*7c478bd9Sstevel@tonic-gate 		*s = '\0';
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 		/* if the login name is null, then use $HOME */
201*7c478bd9Sstevel@tonic-gate 		if (*out == '\0') {
202*7c478bd9Sstevel@tonic-gate 			v = getenv("HOME");
203*7c478bd9Sstevel@tonic-gate 		} else {	/* get the home directory of the login name */
204*7c478bd9Sstevel@tonic-gate 			v = logdir(out);
205*7c478bd9Sstevel@tonic-gate 		}
206*7c478bd9Sstevel@tonic-gate 		/* copy the directory name */
207*7c478bd9Sstevel@tonic-gate 		if (v != NULL) {
208*7c478bd9Sstevel@tonic-gate 			(void) strcpy(out - 1, v);
209*7c478bd9Sstevel@tonic-gate 			out += strlen(v) - 1;
210*7c478bd9Sstevel@tonic-gate 		} else {
211*7c478bd9Sstevel@tonic-gate 			/* login not found so ~ must be part of the file name */
212*7c478bd9Sstevel@tonic-gate 			out += strlen(out);
213*7c478bd9Sstevel@tonic-gate 		}
214*7c478bd9Sstevel@tonic-gate 	}
215*7c478bd9Sstevel@tonic-gate 	/* get the rest of the path */
216*7c478bd9Sstevel@tonic-gate 	while (out < lastchar && *in != '\0' && !isspace(*in)) {
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate 		/* look for an environment variable */
219*7c478bd9Sstevel@tonic-gate 		if (*in == '$') {
220*7c478bd9Sstevel@tonic-gate 			/* copy the $ because it may not be expanded */
221*7c478bd9Sstevel@tonic-gate 			*out++ = *in++;
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 			/* get the variable name */
224*7c478bd9Sstevel@tonic-gate 			s = out;
225*7c478bd9Sstevel@tonic-gate 			while (s < lastchar && *in != '/' && *in != '\0' &&
226*7c478bd9Sstevel@tonic-gate 			    !isspace(*in)) {
227*7c478bd9Sstevel@tonic-gate 				*s++ = *in++;
228*7c478bd9Sstevel@tonic-gate 			}
229*7c478bd9Sstevel@tonic-gate 			*s = '\0';
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate 			/* get its value */
232*7c478bd9Sstevel@tonic-gate 			if ((v = getenv(out)) != NULL) {
233*7c478bd9Sstevel@tonic-gate 				(void) strcpy(out - 1, v);
234*7c478bd9Sstevel@tonic-gate 				out += strlen(v) - 1;
235*7c478bd9Sstevel@tonic-gate 			} else {
236*7c478bd9Sstevel@tonic-gate 				/*
237*7c478bd9Sstevel@tonic-gate 				 * var not found, so $ must be part of
238*7c478bd9Sstevel@tonic-gate 				 * the file name
239*7c478bd9Sstevel@tonic-gate 				 */
240*7c478bd9Sstevel@tonic-gate 				out += strlen(out);
241*7c478bd9Sstevel@tonic-gate 			}
242*7c478bd9Sstevel@tonic-gate 		} else {	/* ordinary character */
243*7c478bd9Sstevel@tonic-gate 			*out++ = *in++;
244*7c478bd9Sstevel@tonic-gate 		}
245*7c478bd9Sstevel@tonic-gate 	}
246*7c478bd9Sstevel@tonic-gate 	*out = '\0';
247*7c478bd9Sstevel@tonic-gate }
248