a question about using curses lib

brad at bradley.UUCP brad at bradley.UUCP
Tue Mar 1 01:12:00 AEST 1988


I too need a way to use funciton keys on BSD machines and have the
code I right work also on system V machines.....here are some routines
I wrote....they seem to work on BSD....haven't tested on system V,
but will shortly. I suppose I could you keypad as well, but then
more code rewriting would be needed.

Also this version need more work....but you can build on it.


Bradley Smith			UUCP: {cepu,ihnp4,noao,uiucdcs}!bradley!brad
Text Processing			ARPA: cepu!bradley!brad at seas.ucla.edu
Bradley University		PH: (309) 677-2337
Peoria, IL 61625

You never get a second chance to make a first impression.
================cut here==============
# This is a shell archive.  Remove anything before this line,
# then unpack it by saving it in a file and typing "sh file".
#
# Wrapped by bradley!brad on Mon Feb 29 09:10:26 CST 1988
# Contents:  curses.c curses.h
 
echo x - curses.c
sed 's/^@//' > "curses.c" <<'@//E*O*F 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], bp[1024], key[16];

	tgetent(bbp,getenv("TERM"));
	err = 0;
	for(i=0;i<8;i++) {
		sprintf(key,"k%d", i);
		cp = tgetstr(key,bp);
		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 */
	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);
	}
	cp = tgetstr("kd", bp);
	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);
	}
	cp = tgetstr("kr", bp);
	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);
	}
	cp = tgetstr("kl", bp);
	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);
	}
	cp = tgetstr("kh", bp);
	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);
}
@//E*O*F curses.c//
chmod u=rw,g=rw,o= curses.c
 
echo x - curses.h
sed 's/^@//' > "curses.h" <<'@//E*O*F curses.h//'
/*
 * defines for mgets.c
 */

#define	MAXRT	12	/* maxiumn number of chars typed in */
#define RTNO	13	/* Return with no chars */
#define	RT	14	/* return with chars */
#define TAB	15
#define	TABCH	16
#define	DELCH	17	/* del with chars */
#define	DEL	18

/* */
#define	FUN1	0	/* function key 1 */
#define	FUN2	1	/* function key 2 */
#define	FUN3	2	/* function key 3 */
#define	FUN4	3	/* function key 4 */
#define	FUN5	4	/* function key 5 */
#define	FUN6	5	/* function key 6 */
#define	FUN7	6	/* function key 7 */
#define	FUN8	7	/* function key 8 */
/* */
#define KEYBASE		20 /* BASE for BELOW */
#define	KEYEND		(KEYBASE+4) /* highest number for KEYS */
#define KEYUP		(0+KEYBASE)
#define KEYDOWN		(1+KEYBASE)
#define KEYRIGHT	(2+KEYBASE)
#define KEYLEFT		(3+KEYBASE)
#define KEYHOME		(4+KEYBASE)
@//E*O*F curses.h//
chmod u=rw,g=rw,o= curses.h
 
exit 0



More information about the Comp.unix.questions mailing list