1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
/*===========================================================================
Copyright (c) 1998-2000, The Santa Cruz Operation
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
*Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
*Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
*Neither name of The Santa Cruz Operation nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
=========================================================================*/
/* cscope - interactive C symbol cross-reference
*
* terminal input functions
*/
#include "global.h"
#if defined(USE_NCURSES) && !defined(RENAMED_NCURSES)
#include <ncurses.h>
#else
#include <curses.h>
#endif
#include <setjmp.h> /* jmp_buf */
#include <stdlib.h>
#include <errno.h>
#if HAVE_SYS_TERMIOS_H
#include <sys/termios.h>
#endif
static char const rcsid[] = "$Id: input.c,v 1.15 2006/08/20 15:00:34 broeker Exp $";
static jmp_buf env; /* setjmp/longjmp buffer */
static int prevchar; /* previous, ungotten character */
/* Internal prototypes: */
static RETSIGTYPE catchint(int sig);
/* catch the interrupt signal */
/*ARGSUSED*/
static RETSIGTYPE
catchint(int sig)
{
(void) sig; /* 'use' it, to avoid a warning */
signal(SIGINT, catchint);
longjmp(env, 1);
}
/* unget a character */
void
myungetch(int c)
{
prevchar = c;
}
/* get a character from the terminal */
int
mygetch(void)
{
sighandler_t savesig; /* old value of signal */
int c;
/* change an interrupt signal to a break key character */
if (setjmp(env) == 0) {
savesig = signal(SIGINT, catchint);
refresh(); /* update the display */
mousereinit(); /* curses can change the menu number */
if(prevchar) {
c = prevchar;
prevchar = 0;
} else {
c = -1;
while (c == -1) {
/* get a character from the terminal */
c = getch();
if ((c == -1) && (errno != EINTR))
break;
}
}
} else { /* longjmp to here from signal handler */
c = KEY_BREAK;
}
signal(SIGINT, savesig);
return(c);
}
/* get a line from the terminal in non-canonical mode */
int
mygetline(char p[], char s[], unsigned size, int firstchar, BOOL iscaseless)
{
int c;
unsigned int i = 0, j;
char *sright; /* substring to the right of the cursor */
unsigned int ri = 0; /* position in right-string */
/* Inserts and deletes are always performed on the left-string,
* but we'll also have a right-string 'sright' to hold characters
* which are on the right of the cursor [insertion point].
*
* Think of 'sright' as a stack -- we push chars into it when the cursor
* moves left, and we pop chars off it when the cursor moves right again.
* At the end of the function, we'll pop off any remaining characters
* onto the end of 's'
*/
sright = calloc(sizeof(char), size );
strcpy ( s, p);
i += strlen(p);
/* if a character already has been typed */
if (firstchar != '\0') {
if(iscaseless == YES) {
firstchar = tolower(firstchar);
}
addch(firstchar); /* display it */
s[i++] = firstchar; /* save it */
}
/* until the end of the line is reached */
while ((c = mygetch()) != '\r' && c != '\n' && c != KEY_ENTER) {
if (c == KEY_LEFT || c == ctrl('B')) { /* left */
if (i > 0) {
addch('\b');
/* move this char into the second (rhs) string */
sright[ri++] = s[--i];
}
} else if (c == KEY_RIGHT || c == ctrl('F')) { /* right */
if (i < size && ri > 0) {
/* move this char to the left of the cursor */
s[i++] = sright[--ri];
addch(s[i-1]);
}
} else if (
#ifdef KEY_HOME
c == KEY_HOME ||
#endif
c == ctrl('A') ) {
while (i > 0) {
sright[ri++] = s[--i];
addch('\b');
addch(s[i]);
addch('\b');
}
} else if (
#ifdef KEY_END
c == KEY_END ||
#endif
c == ctrl('E') ) {
while (ri > 0) {
s[i++] = sright[--ri];
addch(s[i-1]);
}
} else if (c == erasechar() || c == KEY_BACKSPACE
|| c == DEL || c == ctrl('H') ) {
/* erase */
if (i > 0) {
if (ri == 0) {
addstr("\b \b");
} else {
addch('\b');
delch();
}
s[i] = '\0';
--i;
}
} else if (c == killchar() || c == KEY_BREAK) {
/* kill */
for (j = 0; j < i; ++j) {
addch('\b');
}
for (j = 0; j < i; ++j) {
addch(' ');
}
for (j = 0; j < i; ++j) {
addch('\b');
}
i = 0;
} else if (isprint(c) || c == '\t') {
/* printable */
if(iscaseless == YES) {
c = tolower(c);
}
/* if it will fit on the line */
if (i < size) {
s[i++] = c; /* save it */
if (ri == 0) {
addch(c); /* display it */
} else {
insch(c); /* display it */
addch(c); /* advance cursor */
}
}
#if UNIXPC
} else if (unixpcmouse == YES && c == ESC) { /* mouse */
getmouseaction(ESC); /* ignore it */
#endif
} else if (mouse == YES && c == ctrl('X')) {
getmouseaction(ctrl('X')); /* ignore it */
} else if (c == EOF) { /* end-of-file */
break;
}
/* return on an empty line to allow a command to be entered */
if (firstchar != '\0' && (i+ri) == 0) {
break;
}
}
/* move any remaining chars on the rhs of the cursor
* onto the end of our string
*/
while (ri > 0) {
s[i++] = sright[--ri];
}
free(sright);
s[i] = '\0';
return(i);
}
/* ask user to enter a character after reading the message */
void
askforchar(void)
{
addstr("Type any character to continue: ");
mygetch();
}
/* ask user to press the RETURN key after reading the message */
void
askforreturn(void)
{
fprintf(stderr, "Press the RETURN key to continue: ");
getchar();
/* HBB 20060419: message probably messed up the screen --- redraw */
if (incurses == YES) {
redrawwin(curscr);
}
}
/* expand the ~ and $ shell meta characters in a path */
void
shellpath(char *out, int limit, char *in)
{
char *lastchar;
char *s, *v;
/* skip leading white space */
while (isspace((unsigned char)*in)) {
++in;
}
lastchar = out + limit - 1;
/* a tilde (~) by itself represents $HOME; followed by a name it
represents the $LOGDIR of that login name */
if (*in == '~') {
*out++ = *in++; /* copy the ~ because it may not be expanded */
/* get the login name */
s = out;
while (s < lastchar && *in != '/' && *in != '\0' && !isspace((unsigned char)*in)) {
*s++ = *in++;
}
*s = '\0';
/* if the login name is null, then use $HOME */
if (*out == '\0') {
v = getenv("HOME");
} else { /* get the home directory of the login name */
v = logdir(out);
}
/* copy the directory name if it isn't too big */
if (v != NULL && strlen(v) < (lastchar - out)) {
strcpy(out - 1, v);
out += strlen(v) - 1;
} else {
/* login not found, so ~ must be part of the file name */
out += strlen(out);
}
}
/* get the rest of the path */
while (out < lastchar && *in != '\0' && !isspace((unsigned char)*in)) {
/* look for an environment variable */
if (*in == '$') {
*out++ = *in++; /* copy the $ because it may not be expanded */
/* get the variable name */
s = out;
while (s < lastchar && *in != '/' && *in != '\0' &&
!isspace((unsigned char)*in)) {
*s++ = *in++;
}
*s = '\0';
/* get its value, but only it isn't too big */
if ((v = getenv(out)) != NULL && strlen(v) < (lastchar - out)) {
strcpy(out - 1, v);
out += strlen(v) - 1;
} else {
/* var not found, or too big, so assume $ must be part of the
* file name */
out += strlen(out);
}
}
else { /* ordinary character */
*out++ = *in++;
}
}
*out = '\0';
}
|