a question about using curses lib

brad at bradley.UUCP brad at bradley.UUCP
Tue Mar 1 05:11:00 AEST 1988


well the first file worked on my venix/86 machine...but my vax
died....so I fixed it...here it is

======curses.c======
#include	<curses.h>
#include	<ctype.h>
#include	<signal.h>
#include	<setjmp.h>
#include	"curses.h"	/* this is my include */

/* this file sets up terminal */

extern char *malloc();

char *function_keys[8]; /* where we store what each function key is */
char *other_keys[5];	/* KEYUP, KEYDOWN, KEYRIGHT, KEYLEFT, KEYHOME */

/* used to see if more chars come in */
int cur_alrm();
jmp_buf cur_buf;
/*  */


/* get function returns the number of function keys it found */
/* returns -1 if it has a major error */
get_function_key()
{
	int err, i;
	extern char *tgetstr(), *getenv();
	char *cp, bbp[1024], *sp, bp[1024], key[16];

	err = tgetent(bbp,getenv("TERM"));
	if(err != 1) {
		/* not all is well */
		fprintf(stderr,"tgetent returned %d\n", err);
		return(-1);
	}
	err = 0;
	for(i=0;i<8;i++) {
		sprintf(key,"k%d", i);
		sp = bp;
		cp = tgetstr(key,&sp);
		if(cp) {
			err++;
			function_keys[i] = malloc(strlen(cp) +1);
			if(function_keys[i] == (char *) NULL) {
				perror("malloc on function_keys");
				return(-1);
			}
			strcpy(function_keys[i], cp);
		}
	}
	/* now we get arrow keys & home */
	sp = bp;
	cp = tgetstr("ku", &bp);
	if(cp ) {
		other_keys[0] = malloc(strlen(cp)+1);
		if(other_keys[0] == (char *) NULL ) {
			perror("malloc on other_keys");
			return(-1);
		}
		strcpy(other_keys[0], cp);
	}
	sp = bp;
	cp = tgetstr("kd", &sp);
	if(cp ) {
		other_keys[1] = malloc(strlen(cp)+1);
		if(other_keys[1] == (char *) NULL ) {
			perror("malloc on other_keys");
			return(-1);
		}
		strcpy(other_keys[1], cp);
	}
	sp = bp;
	cp = tgetstr("kr", &sp);
	if(cp ) {
		other_keys[2] = malloc(strlen(cp)+1);
		if(other_keys[2] == (char *) NULL ) {
			perror("malloc on other_keys");
			return(-1);
		}
		strcpy(other_keys[2], cp);
	}
	sp = bp;
	cp = tgetstr("kl", &sp);
	if(cp ) {
		other_keys[3] = malloc(strlen(cp)+1);
		if(other_keys[3] == (char *) NULL ) {
			perror("malloc on other_keys");
			return(-1);
		}
		strcpy(other_keys[3], cp);
	}
	sp = bp;
	cp = tgetstr("kh", &sp);
	if(cp ) {
		other_keys[4] = malloc(strlen(cp)+1);
		if(other_keys[4] == (char *) NULL ) {
			perror("malloc on other_keys");
			return(-1);
		}
		strcpy(other_keys[4], cp);
	}
	return(err);
}

/*	mgets		version 1.0
 *
 * mgets is similiar to gets execpt that it is always checking
 * if function keys are entered as well as ESC's and returns
 *
 * returns are as follows
	MAXRT	12	 maxiumn number of chars typed in 
	RTNO	13	 Return with no chars 
	RT	14	 return with chars
	TAB	16	 tab no chars
	TABCH	17	 tab with chars
	DELCH	18	 del with chars
	DEL	19
 */

/*
 * Mon Sep 23 19:08:21 CDT 1985
 * modified such that a ^L will redraw the screen
 */
mgets(maxcnt,str,doecho)
int maxcnt, doecho;
char *str;
{
	char m_buffer[256];
	int m_count = 0;
	char *cp,c;
	register int i, j;
	WINDOW *mwin;
			/* i = character count, */
	cp = str;
	*cp = 0;
	for(i=0;i<maxcnt;) {
		c = getch();
		/* addition for ^L */
		if(c == '\f') {
			mwin = newwin(0,0,0,0);
			wclear(mwin);
			wrefresh(mwin);
			delwin(mwin);
			touchwin(stdscr);
			refresh();
			continue;
		}
		if(iscntrl(c)) {
			switch(c) {
			case '\010':
				if(cp > str) {
					cp--;
					if(iscntrl(*cp)) {
					    i--;
					    printw("\010 \010");
					}
					printw("\010 \010");
					refresh();
					i--;
				}
				break;
			case '\n':
			case '\r':
				*cp = '\0';
				j = compare_func(str,doecho);
				if(j != -1)
					return(j);
				if(cp > str)
					return(RT);
				return(RTNO);
			case '\t':
				*cp = '\0';
				if(cp > str)
					return(TABCH);
				return(TAB);
			case '\177':
				*cp = '\0';
				if(cp > str)
					return(DELCH);
				return(DEL);
			default:
				*cp++ = c;
				if(doecho) {
					addch('^');
					addch(c | 0100);
					refresh();
				}
				i++;
				*cp = 0;
				j = compare_func(str,doecho);
				if(j != -1)
					return(j);
				break;
			}
		}
		else {
			switch(c) {
			case '\r':
				*cp++ = c;
				*cp = '\0';
				j = compare_func(str,doecho);
				if(j != -1)
					return(j);
				cp--;
				*cp = 0;
				if(cp > str)
					return(RT);
				return(RTNO);
				break;
			case '\n':
				*cp++ = c;
				*cp = '\0';
				j = compare_func(str,doecho);
				if(j != -1)
					return(j);
				cp--;
				*cp = 0;
				if(cp > str)
					return(RT);
				return(RTNO);
				break;
			case '\t':
				*cp = '\0';
				if(cp > str)
					return(TABCH);
				return(TAB);
			default:
				*cp++ = c;
				if(doecho) {
					if(c < ' ') {
						addch('^');
						addch(c | 0100);
					} else
						addch(c);
					refresh();
				}
				i++;
				*cp = 0;
				j = compare_func(str,doecho);
				if(j != -1)
					return(j);
				break;
			}
			refresh();
		}
		*cp = '\0';
	}
	*cp = '\0';
	/* once here I want to make sure that we don't have a
	 * function key typed.  What we do is do a timed input.
	 * I keep taking chars until nothing is gotten with
	 * 1 sec.  We are going to use the SIGALRM
	 */
	if(func_compare(str)) 
		return(MAXRT);	/* looks like it is not a funt key */
	signal(SIGALRM, cur_alrm);
	strcpy(m_buffer, str);
	m_count = cp - str;
	for(;;) {
		if(setjmp(cur_buf)) {	/* 1 sec has pasted */
			return(MAXRT);
		}
		alarm(1);
		c = getch();	/* wait for char */
		alarm(0);
		if(doecho) {
			if(c < ' ') {
				addch('^');
			addch(c | 0100);
			} else
				addch(c);
			refresh();
		}
		m_buffer[m_count++] = c;
		m_buffer[m_count] = 0;
		j = compare_func(m_buffer,doecho);
		if(j != -1)
			return(j);
	}
	return(MAXRT);
}
/* compare_func - returns -1 if not a function key
 *		  returns 0-7 for function key 1-8
 */
compare_func(str,doecho)
char *str;
int doecho;
{
	int i;
	char *cp;

	for(i=0;i<8;i++) {
		if(tindex(str,function_keys[i]) != -1) {
			/* erase chars on screen */
			if(doecho) {
				for(cp = str; *cp;cp++) {
					if(iscntrl(*cp)) {
					    printw("\010 \010");
					}
					printw("\010 \010");
				}
				refresh();
			}
			return(i);
		}
	}
	/* check arrow and home keys */
	for(i=0;i<5;i++) {
		if(tindex(str,other_keys[i])!= -1) {
			if(doecho) {
				/* erase chars on screen */
				for(cp = str; *cp ;cp++) {
					if(iscntrl(*cp)) {
					    printw("\010 \010");
					}
					printw("\010 \010");
				}
				refresh();
			}
			return(i+KEYBASE);
		}
	}
	return(-1);	/* not it ? */
}
/* func_compare - returns -1 if not a function key (ie close to it)
 *		  returns 0   if it looks like it might be a fun key
 */
func_compare(str)
char *str;
{
	int i;
	char *cp;

	for(i=0;i<8;i++) {
		if(tindex(function_keys[i],str) != -1) {
			return(0);
		}
	}
	/* check arrow and home keys */
	for(i=0;i<5;i++) {
		if(tindex(other_keys[i],str)!= -1) {
			return(0);
		}
	}
	return(-1);	/* not it ? */
}
cur_alrm()
{
	longjmp(cur_buf,1);
}



More information about the Comp.unix.questions mailing list