1 /*===========================================================================
2 Copyright (c) 1998-2000, The Santa Cruz Operation
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 *Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
11 *Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
15 *Neither name of The Santa Cruz Operation nor the names of its contributors
16 may be used to endorse or promote products derived from this software
17 without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
20 IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 =========================================================================*/
33 /* cscope - interactive C symbol cross-reference
35 * terminal input functions
39 #if defined(USE_NCURSES) && !defined(RENAMED_NCURSES)
44 #include <setjmp.h> /* jmp_buf */
47 #if HAVE_SYS_TERMIOS_H
48 #include <sys/termios.h>
51 static char const rcsid[] = "$Id: input.c,v 1.15 2006/08/20 15:00:34 broeker Exp $";
53 static jmp_buf env; /* setjmp/longjmp buffer */
54 static int prevchar; /* previous, ungotten character */
56 /* Internal prototypes: */
57 static RETSIGTYPE catchint(int sig);
59 /* catch the interrupt signal */
65 (void) sig; /* 'use' it, to avoid a warning */
67 signal(SIGINT, catchint);
71 /* unget a character */
78 /* get a character from the terminal */
82 sighandler_t savesig; /* old value of signal */
85 /* change an interrupt signal to a break key character */
86 if (setjmp(env) == 0) {
87 savesig = signal(SIGINT, catchint);
88 refresh(); /* update the display */
89 mousereinit(); /* curses can change the menu number */
96 /* get a character from the terminal */
98 if ((c == -1) && (errno != EINTR))
102 } else { /* longjmp to here from signal handler */
105 signal(SIGINT, savesig);
110 /* get a line from the terminal in non-canonical mode */
112 mygetline(char p[], char s[], unsigned size, int firstchar, BOOL iscaseless)
115 unsigned int i = 0, j;
116 char *sright; /* substring to the right of the cursor */
117 unsigned int ri = 0; /* position in right-string */
119 /* Inserts and deletes are always performed on the left-string,
120 * but we'll also have a right-string 'sright' to hold characters
121 * which are on the right of the cursor [insertion point].
123 * Think of 'sright' as a stack -- we push chars into it when the cursor
124 * moves left, and we pop chars off it when the cursor moves right again.
125 * At the end of the function, we'll pop off any remaining characters
126 * onto the end of 's'
128 sright = calloc(sizeof(char), size );
132 /* if a character already has been typed */
133 if (firstchar != '\0') {
134 if(iscaseless == YES) {
135 firstchar = tolower(firstchar);
137 addch(firstchar); /* display it */
138 s[i++] = firstchar; /* save it */
140 /* until the end of the line is reached */
141 while ((c = mygetch()) != '\r' && c != '\n' && c != KEY_ENTER) {
142 if (c == KEY_LEFT || c == ctrl('B')) { /* left */
145 /* move this char into the second (rhs) string */
146 sright[ri++] = s[--i];
148 } else if (c == KEY_RIGHT || c == ctrl('F')) { /* right */
149 if (i < size && ri > 0) {
150 /* move this char to the left of the cursor */
151 s[i++] = sright[--ri];
160 sright[ri++] = s[--i];
171 s[i++] = sright[--ri];
174 } else if (c == erasechar() || c == KEY_BACKSPACE
175 || c == DEL || c == ctrl('H') ) {
187 } else if (c == killchar() || c == KEY_BREAK) {
189 for (j = 0; j < i; ++j) {
192 for (j = 0; j < i; ++j) {
195 for (j = 0; j < i; ++j) {
199 } else if (isprint(c) || c == '\t') {
201 if(iscaseless == YES) {
204 /* if it will fit on the line */
206 s[i++] = c; /* save it */
208 addch(c); /* display it */
210 insch(c); /* display it */
211 addch(c); /* advance cursor */
215 } else if (unixpcmouse == YES && c == ESC) { /* mouse */
216 getmouseaction(ESC); /* ignore it */
218 } else if (mouse == YES && c == ctrl('X')) {
219 getmouseaction(ctrl('X')); /* ignore it */
220 } else if (c == EOF) { /* end-of-file */
224 /* return on an empty line to allow a command to be entered */
225 if (firstchar != '\0' && (i+ri) == 0) {
230 /* move any remaining chars on the rhs of the cursor
231 * onto the end of our string
234 s[i++] = sright[--ri];
242 /* ask user to enter a character after reading the message */
247 addstr("Type any character to continue: ");
251 /* ask user to press the RETURN key after reading the message */
256 fprintf(stderr, "Press the RETURN key to continue: ");
258 /* HBB 20060419: message probably messed up the screen --- redraw */
259 if (incurses == YES) {
264 /* expand the ~ and $ shell meta characters in a path */
267 shellpath(char *out, int limit, char *in)
272 /* skip leading white space */
273 while (isspace((unsigned char)*in)) {
276 lastchar = out + limit - 1;
278 /* a tilde (~) by itself represents $HOME; followed by a name it
279 represents the $LOGDIR of that login name */
281 *out++ = *in++; /* copy the ~ because it may not be expanded */
283 /* get the login name */
285 while (s < lastchar && *in != '/' && *in != '\0' && !isspace((unsigned char)*in)) {
290 /* if the login name is null, then use $HOME */
293 } else { /* get the home directory of the login name */
296 /* copy the directory name if it isn't too big */
297 if (v != NULL && strlen(v) < (lastchar - out)) {
299 out += strlen(v) - 1;
301 /* login not found, so ~ must be part of the file name */
305 /* get the rest of the path */
306 while (out < lastchar && *in != '\0' && !isspace((unsigned char)*in)) {
308 /* look for an environment variable */
310 *out++ = *in++; /* copy the $ because it may not be expanded */
312 /* get the variable name */
314 while (s < lastchar && *in != '/' && *in != '\0' &&
315 !isspace((unsigned char)*in)) {
320 /* get its value, but only it isn't too big */
321 if ((v = getenv(out)) != NULL && strlen(v) < (lastchar - out)) {
323 out += strlen(v) - 1;
325 /* var not found, or too big, so assume $ must be part of the
330 else { /* ordinary character */