1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28*0Sstevel@tonic-gate  * Use is subject to license terms.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate /*
34*0Sstevel@tonic-gate  *	cscope - interactive C symbol cross-reference
35*0Sstevel@tonic-gate  *
36*0Sstevel@tonic-gate  *	terminal input functions
37*0Sstevel@tonic-gate  */
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #include "global.h"
40*0Sstevel@tonic-gate #include <curses.h>	/* KEY_BACKSPACE, KEY_BREAK, and KEY_ENTER */
41*0Sstevel@tonic-gate #include <setjmp.h>	/* jmp_buf */
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate static	jmp_buf	env;		/* setjmp/longjmp buffer */
44*0Sstevel@tonic-gate static	int	prevchar;	/* previous, ungotten character */
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate /* catch the interrupt signal */
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate /*ARGSUSED*/
49*0Sstevel@tonic-gate SIGTYPE
50*0Sstevel@tonic-gate catchint(int sig)
51*0Sstevel@tonic-gate {
52*0Sstevel@tonic-gate 	(void) signal(SIGINT, catchint);
53*0Sstevel@tonic-gate 	longjmp(env, 1);
54*0Sstevel@tonic-gate }
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate /* unget a character */
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate int
59*0Sstevel@tonic-gate ungetch(int c)
60*0Sstevel@tonic-gate {
61*0Sstevel@tonic-gate 	prevchar = c;
62*0Sstevel@tonic-gate 	return (0);
63*0Sstevel@tonic-gate }
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate /* get a character from the terminal */
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate int
68*0Sstevel@tonic-gate mygetch(void)
69*0Sstevel@tonic-gate {
70*0Sstevel@tonic-gate 	SIGTYPE	(*savesig)();		/* old value of signal */
71*0Sstevel@tonic-gate 	int	c;
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 	/* change an interrupt signal to a break key character */
74*0Sstevel@tonic-gate 	if (setjmp(env) == 0) {
75*0Sstevel@tonic-gate 		savesig = signal(SIGINT, catchint);
76*0Sstevel@tonic-gate 		(void) refresh();	/* update the display */
77*0Sstevel@tonic-gate 		reinitmouse();	/* curses can change the menu number */
78*0Sstevel@tonic-gate 		if (prevchar) {
79*0Sstevel@tonic-gate 			c = prevchar;
80*0Sstevel@tonic-gate 			prevchar = 0;
81*0Sstevel@tonic-gate 		} else {
82*0Sstevel@tonic-gate 			c = getch();	/* get a character from the terminal */
83*0Sstevel@tonic-gate 		}
84*0Sstevel@tonic-gate 	} else {	/* longjmp to here from signal handler */
85*0Sstevel@tonic-gate 		c = KEY_BREAK;
86*0Sstevel@tonic-gate 	}
87*0Sstevel@tonic-gate 	(void) signal(SIGINT, savesig);
88*0Sstevel@tonic-gate 	return (c);
89*0Sstevel@tonic-gate }
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate /* get a line from the terminal in non-canonical mode */
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate int
94*0Sstevel@tonic-gate getline(char s[], size_t size, int firstchar, BOOL iscaseless)
95*0Sstevel@tonic-gate {
96*0Sstevel@tonic-gate 	int	c, i = 0;
97*0Sstevel@tonic-gate 	int	j;
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	/* if a character already has been typed */
100*0Sstevel@tonic-gate 	if (firstchar != '\0') {
101*0Sstevel@tonic-gate 		if (iscaseless == YES) {
102*0Sstevel@tonic-gate 			firstchar = tolower(firstchar);
103*0Sstevel@tonic-gate 		}
104*0Sstevel@tonic-gate 		(void) addch((unsigned)firstchar);	/* display it */
105*0Sstevel@tonic-gate 		s[i++] = firstchar;	/* save it */
106*0Sstevel@tonic-gate 	}
107*0Sstevel@tonic-gate 	/* until the end of the line is reached */
108*0Sstevel@tonic-gate 	while ((c = mygetch()) != '\r' && c != '\n' && c != KEY_ENTER &&
109*0Sstevel@tonic-gate 	    c != '\003' && c != KEY_BREAK) {
110*0Sstevel@tonic-gate 		if (c == erasechar() || c == '\b' ||		/* erase */
111*0Sstevel@tonic-gate 		    c == KEY_BACKSPACE) {
112*0Sstevel@tonic-gate 			if (i > 0) {
113*0Sstevel@tonic-gate 				(void) addstr("\b \b");
114*0Sstevel@tonic-gate 				--i;
115*0Sstevel@tonic-gate 			}
116*0Sstevel@tonic-gate 		} else if (c == killchar()) {			/* kill */
117*0Sstevel@tonic-gate 			for (j = 0; j < i; ++j) {
118*0Sstevel@tonic-gate 				(void) addch('\b');
119*0Sstevel@tonic-gate 			}
120*0Sstevel@tonic-gate 			for (j = 0; j < i; ++j) {
121*0Sstevel@tonic-gate 				(void) addch(' ');
122*0Sstevel@tonic-gate 			}
123*0Sstevel@tonic-gate 			for (j = 0; j < i; ++j) {
124*0Sstevel@tonic-gate 				(void) addch('\b');
125*0Sstevel@tonic-gate 			}
126*0Sstevel@tonic-gate 			i = 0;
127*0Sstevel@tonic-gate 		} else if (isprint(c) || c == '\t') {		/* printable */
128*0Sstevel@tonic-gate 			if (iscaseless == YES) {
129*0Sstevel@tonic-gate 				c = tolower(c);
130*0Sstevel@tonic-gate 			}
131*0Sstevel@tonic-gate 			/* if it will fit on the line */
132*0Sstevel@tonic-gate 			if (i < size) {
133*0Sstevel@tonic-gate 				(void) addch((unsigned)c);	/* display it */
134*0Sstevel@tonic-gate 				s[i++] = c;		/* save it */
135*0Sstevel@tonic-gate 			}
136*0Sstevel@tonic-gate 		} else if (c == ctrl('X')) {
137*0Sstevel@tonic-gate 			/* mouse */
138*0Sstevel@tonic-gate 			(void) getmouseevent(); 	/* ignore it */
139*0Sstevel@tonic-gate 		} else if (c == EOF) {			/* end-of-file */
140*0Sstevel@tonic-gate 			break;
141*0Sstevel@tonic-gate 		}
142*0Sstevel@tonic-gate 		/* return on an empty line to allow a command to be entered */
143*0Sstevel@tonic-gate 		if (firstchar != '\0' && i == 0) {
144*0Sstevel@tonic-gate 			break;
145*0Sstevel@tonic-gate 		}
146*0Sstevel@tonic-gate 	}
147*0Sstevel@tonic-gate 	s[i] = '\0';
148*0Sstevel@tonic-gate 	return (i);
149*0Sstevel@tonic-gate }
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate /* ask user to enter a character after reading the message */
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate void
154*0Sstevel@tonic-gate askforchar(void)
155*0Sstevel@tonic-gate {
156*0Sstevel@tonic-gate 	(void) addstr("Type any character to continue: ");
157*0Sstevel@tonic-gate 	(void) mygetch();
158*0Sstevel@tonic-gate }
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate /* ask user to press the RETURN key after reading the message */
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate void
163*0Sstevel@tonic-gate askforreturn(void)
164*0Sstevel@tonic-gate {
165*0Sstevel@tonic-gate 	if (linemode == NO) {
166*0Sstevel@tonic-gate 		(void) fprintf(stderr, "Press the RETURN key to continue: ");
167*0Sstevel@tonic-gate 		(void) getchar();
168*0Sstevel@tonic-gate 	}
169*0Sstevel@tonic-gate }
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate /* expand the ~ and $ shell meta characters in a path */
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate void
174*0Sstevel@tonic-gate shellpath(char *out, int limit, char *in)
175*0Sstevel@tonic-gate {
176*0Sstevel@tonic-gate 	char	*lastchar;
177*0Sstevel@tonic-gate 	char	*s, *v;
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 	/* skip leading white space */
180*0Sstevel@tonic-gate 	while (isspace(*in)) {
181*0Sstevel@tonic-gate 		++in;
182*0Sstevel@tonic-gate 	}
183*0Sstevel@tonic-gate 	lastchar = out + limit - 1;
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	/*
186*0Sstevel@tonic-gate 	 * a tilde (~) by itself represents $HOME; followed by a name it
187*0Sstevel@tonic-gate 	 * represents the $LOGDIR of that login name
188*0Sstevel@tonic-gate 	 */
189*0Sstevel@tonic-gate 	if (*in == '~') {
190*0Sstevel@tonic-gate 		*out++ = *in++;	/* copy the ~ because it may not be expanded */
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 		/* get the login name */
193*0Sstevel@tonic-gate 		s = out;
194*0Sstevel@tonic-gate 		while (s < lastchar && *in != '/' && *in != '\0' &&
195*0Sstevel@tonic-gate 		    !isspace(*in)) {
196*0Sstevel@tonic-gate 			*s++ = *in++;
197*0Sstevel@tonic-gate 		}
198*0Sstevel@tonic-gate 		*s = '\0';
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 		/* if the login name is null, then use $HOME */
201*0Sstevel@tonic-gate 		if (*out == '\0') {
202*0Sstevel@tonic-gate 			v = getenv("HOME");
203*0Sstevel@tonic-gate 		} else {	/* get the home directory of the login name */
204*0Sstevel@tonic-gate 			v = logdir(out);
205*0Sstevel@tonic-gate 		}
206*0Sstevel@tonic-gate 		/* copy the directory name */
207*0Sstevel@tonic-gate 		if (v != NULL) {
208*0Sstevel@tonic-gate 			(void) strcpy(out - 1, v);
209*0Sstevel@tonic-gate 			out += strlen(v) - 1;
210*0Sstevel@tonic-gate 		} else {
211*0Sstevel@tonic-gate 			/* login not found so ~ must be part of the file name */
212*0Sstevel@tonic-gate 			out += strlen(out);
213*0Sstevel@tonic-gate 		}
214*0Sstevel@tonic-gate 	}
215*0Sstevel@tonic-gate 	/* get the rest of the path */
216*0Sstevel@tonic-gate 	while (out < lastchar && *in != '\0' && !isspace(*in)) {
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 		/* look for an environment variable */
219*0Sstevel@tonic-gate 		if (*in == '$') {
220*0Sstevel@tonic-gate 			/* copy the $ because it may not be expanded */
221*0Sstevel@tonic-gate 			*out++ = *in++;
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 			/* get the variable name */
224*0Sstevel@tonic-gate 			s = out;
225*0Sstevel@tonic-gate 			while (s < lastchar && *in != '/' && *in != '\0' &&
226*0Sstevel@tonic-gate 			    !isspace(*in)) {
227*0Sstevel@tonic-gate 				*s++ = *in++;
228*0Sstevel@tonic-gate 			}
229*0Sstevel@tonic-gate 			*s = '\0';
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 			/* get its value */
232*0Sstevel@tonic-gate 			if ((v = getenv(out)) != NULL) {
233*0Sstevel@tonic-gate 				(void) strcpy(out - 1, v);
234*0Sstevel@tonic-gate 				out += strlen(v) - 1;
235*0Sstevel@tonic-gate 			} else {
236*0Sstevel@tonic-gate 				/*
237*0Sstevel@tonic-gate 				 * var not found, so $ must be part of
238*0Sstevel@tonic-gate 				 * the file name
239*0Sstevel@tonic-gate 				 */
240*0Sstevel@tonic-gate 				out += strlen(out);
241*0Sstevel@tonic-gate 			}
242*0Sstevel@tonic-gate 		} else {	/* ordinary character */
243*0Sstevel@tonic-gate 			*out++ = *in++;
244*0Sstevel@tonic-gate 		}
245*0Sstevel@tonic-gate 	}
246*0Sstevel@tonic-gate 	*out = '\0';
247*0Sstevel@tonic-gate }
248