10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*13093SRoger.Faulkner@Oracle.COM * Common Development and Distribution License (the "License").
6*13093SRoger.Faulkner@Oracle.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate
220Sstevel@tonic-gate /*
23*13093SRoger.Faulkner@Oracle.COM * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
26*13093SRoger.Faulkner@Oracle.COM /* Copyright (c) 1988 AT&T */
27*13093SRoger.Faulkner@Oracle.COM /* All Rights Reserved */
280Sstevel@tonic-gate
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * cscope - interactive C symbol cross-reference
310Sstevel@tonic-gate *
320Sstevel@tonic-gate * terminal input functions
330Sstevel@tonic-gate */
340Sstevel@tonic-gate
350Sstevel@tonic-gate #include "global.h"
360Sstevel@tonic-gate #include <curses.h> /* KEY_BACKSPACE, KEY_BREAK, and KEY_ENTER */
370Sstevel@tonic-gate #include <setjmp.h> /* jmp_buf */
380Sstevel@tonic-gate
390Sstevel@tonic-gate static jmp_buf env; /* setjmp/longjmp buffer */
400Sstevel@tonic-gate static int prevchar; /* previous, ungotten character */
410Sstevel@tonic-gate
420Sstevel@tonic-gate /* catch the interrupt signal */
430Sstevel@tonic-gate
440Sstevel@tonic-gate /*ARGSUSED*/
450Sstevel@tonic-gate SIGTYPE
catchint(int sig)460Sstevel@tonic-gate catchint(int sig)
470Sstevel@tonic-gate {
480Sstevel@tonic-gate (void) signal(SIGINT, catchint);
490Sstevel@tonic-gate longjmp(env, 1);
500Sstevel@tonic-gate }
510Sstevel@tonic-gate
520Sstevel@tonic-gate /* unget a character */
530Sstevel@tonic-gate
540Sstevel@tonic-gate int
ungetch(int c)550Sstevel@tonic-gate ungetch(int c)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate prevchar = c;
580Sstevel@tonic-gate return (0);
590Sstevel@tonic-gate }
600Sstevel@tonic-gate
610Sstevel@tonic-gate /* get a character from the terminal */
620Sstevel@tonic-gate
630Sstevel@tonic-gate int
mygetch(void)640Sstevel@tonic-gate mygetch(void)
650Sstevel@tonic-gate {
660Sstevel@tonic-gate SIGTYPE (*savesig)(); /* old value of signal */
670Sstevel@tonic-gate int c;
680Sstevel@tonic-gate
690Sstevel@tonic-gate /* change an interrupt signal to a break key character */
700Sstevel@tonic-gate if (setjmp(env) == 0) {
710Sstevel@tonic-gate savesig = signal(SIGINT, catchint);
720Sstevel@tonic-gate (void) refresh(); /* update the display */
730Sstevel@tonic-gate reinitmouse(); /* curses can change the menu number */
740Sstevel@tonic-gate if (prevchar) {
750Sstevel@tonic-gate c = prevchar;
760Sstevel@tonic-gate prevchar = 0;
770Sstevel@tonic-gate } else {
780Sstevel@tonic-gate c = getch(); /* get a character from the terminal */
790Sstevel@tonic-gate }
800Sstevel@tonic-gate } else { /* longjmp to here from signal handler */
810Sstevel@tonic-gate c = KEY_BREAK;
820Sstevel@tonic-gate }
830Sstevel@tonic-gate (void) signal(SIGINT, savesig);
840Sstevel@tonic-gate return (c);
850Sstevel@tonic-gate }
860Sstevel@tonic-gate
870Sstevel@tonic-gate /* get a line from the terminal in non-canonical mode */
880Sstevel@tonic-gate
890Sstevel@tonic-gate int
getaline(char s[],size_t size,int firstchar,BOOL iscaseless)90*13093SRoger.Faulkner@Oracle.COM getaline(char s[], size_t size, int firstchar, BOOL iscaseless)
910Sstevel@tonic-gate {
920Sstevel@tonic-gate int c, i = 0;
930Sstevel@tonic-gate int j;
940Sstevel@tonic-gate
950Sstevel@tonic-gate /* if a character already has been typed */
960Sstevel@tonic-gate if (firstchar != '\0') {
970Sstevel@tonic-gate if (iscaseless == YES) {
980Sstevel@tonic-gate firstchar = tolower(firstchar);
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate (void) addch((unsigned)firstchar); /* display it */
1010Sstevel@tonic-gate s[i++] = firstchar; /* save it */
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate /* until the end of the line is reached */
1040Sstevel@tonic-gate while ((c = mygetch()) != '\r' && c != '\n' && c != KEY_ENTER &&
1050Sstevel@tonic-gate c != '\003' && c != KEY_BREAK) {
1060Sstevel@tonic-gate if (c == erasechar() || c == '\b' || /* erase */
1070Sstevel@tonic-gate c == KEY_BACKSPACE) {
1080Sstevel@tonic-gate if (i > 0) {
1090Sstevel@tonic-gate (void) addstr("\b \b");
1100Sstevel@tonic-gate --i;
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate } else if (c == killchar()) { /* kill */
1130Sstevel@tonic-gate for (j = 0; j < i; ++j) {
1140Sstevel@tonic-gate (void) addch('\b');
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate for (j = 0; j < i; ++j) {
1170Sstevel@tonic-gate (void) addch(' ');
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate for (j = 0; j < i; ++j) {
1200Sstevel@tonic-gate (void) addch('\b');
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate i = 0;
1230Sstevel@tonic-gate } else if (isprint(c) || c == '\t') { /* printable */
1240Sstevel@tonic-gate if (iscaseless == YES) {
1250Sstevel@tonic-gate c = tolower(c);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate /* if it will fit on the line */
1280Sstevel@tonic-gate if (i < size) {
1290Sstevel@tonic-gate (void) addch((unsigned)c); /* display it */
1300Sstevel@tonic-gate s[i++] = c; /* save it */
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate } else if (c == ctrl('X')) {
1330Sstevel@tonic-gate /* mouse */
1340Sstevel@tonic-gate (void) getmouseevent(); /* ignore it */
1350Sstevel@tonic-gate } else if (c == EOF) { /* end-of-file */
1360Sstevel@tonic-gate break;
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate /* return on an empty line to allow a command to be entered */
1390Sstevel@tonic-gate if (firstchar != '\0' && i == 0) {
1400Sstevel@tonic-gate break;
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate s[i] = '\0';
1440Sstevel@tonic-gate return (i);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate /* ask user to enter a character after reading the message */
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate void
askforchar(void)1500Sstevel@tonic-gate askforchar(void)
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate (void) addstr("Type any character to continue: ");
1530Sstevel@tonic-gate (void) mygetch();
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate /* ask user to press the RETURN key after reading the message */
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate void
askforreturn(void)1590Sstevel@tonic-gate askforreturn(void)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate if (linemode == NO) {
1620Sstevel@tonic-gate (void) fprintf(stderr, "Press the RETURN key to continue: ");
1630Sstevel@tonic-gate (void) getchar();
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate /* expand the ~ and $ shell meta characters in a path */
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate void
shellpath(char * out,int limit,char * in)1700Sstevel@tonic-gate shellpath(char *out, int limit, char *in)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate char *lastchar;
1730Sstevel@tonic-gate char *s, *v;
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate /* skip leading white space */
1760Sstevel@tonic-gate while (isspace(*in)) {
1770Sstevel@tonic-gate ++in;
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate lastchar = out + limit - 1;
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate /*
1820Sstevel@tonic-gate * a tilde (~) by itself represents $HOME; followed by a name it
1830Sstevel@tonic-gate * represents the $LOGDIR of that login name
1840Sstevel@tonic-gate */
1850Sstevel@tonic-gate if (*in == '~') {
1860Sstevel@tonic-gate *out++ = *in++; /* copy the ~ because it may not be expanded */
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate /* get the login name */
1890Sstevel@tonic-gate s = out;
1900Sstevel@tonic-gate while (s < lastchar && *in != '/' && *in != '\0' &&
1910Sstevel@tonic-gate !isspace(*in)) {
1920Sstevel@tonic-gate *s++ = *in++;
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate *s = '\0';
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate /* if the login name is null, then use $HOME */
1970Sstevel@tonic-gate if (*out == '\0') {
1980Sstevel@tonic-gate v = getenv("HOME");
1990Sstevel@tonic-gate } else { /* get the home directory of the login name */
2000Sstevel@tonic-gate v = logdir(out);
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate /* copy the directory name */
2030Sstevel@tonic-gate if (v != NULL) {
2040Sstevel@tonic-gate (void) strcpy(out - 1, v);
2050Sstevel@tonic-gate out += strlen(v) - 1;
2060Sstevel@tonic-gate } else {
2070Sstevel@tonic-gate /* login not found so ~ must be part of the file name */
2080Sstevel@tonic-gate out += strlen(out);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate /* get the rest of the path */
2120Sstevel@tonic-gate while (out < lastchar && *in != '\0' && !isspace(*in)) {
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate /* look for an environment variable */
2150Sstevel@tonic-gate if (*in == '$') {
2160Sstevel@tonic-gate /* copy the $ because it may not be expanded */
2170Sstevel@tonic-gate *out++ = *in++;
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate /* get the variable name */
2200Sstevel@tonic-gate s = out;
2210Sstevel@tonic-gate while (s < lastchar && *in != '/' && *in != '\0' &&
2220Sstevel@tonic-gate !isspace(*in)) {
2230Sstevel@tonic-gate *s++ = *in++;
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate *s = '\0';
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate /* get its value */
2280Sstevel@tonic-gate if ((v = getenv(out)) != NULL) {
2290Sstevel@tonic-gate (void) strcpy(out - 1, v);
2300Sstevel@tonic-gate out += strlen(v) - 1;
2310Sstevel@tonic-gate } else {
2320Sstevel@tonic-gate /*
2330Sstevel@tonic-gate * var not found, so $ must be part of
2340Sstevel@tonic-gate * the file name
2350Sstevel@tonic-gate */
2360Sstevel@tonic-gate out += strlen(out);
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate } else { /* ordinary character */
2390Sstevel@tonic-gate *out++ = *in++;
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate *out = '\0';
2430Sstevel@tonic-gate }
244