[net.sources] Function Key Program

bill (03/08/83)

Here is a program to load the function keys of an Ann Arbor Ambassador.
It is provided for those who have tired of translating DCS strings by hand.
This has been used on 4.1,4.1c,2.8,2.9 bsd systems and seems to be innocous.

Send any remarks to ucbvax!william or v:william@Berkeley.

	Bill Jolitz.

--------------------------------------aaakeys.c
/*
 * aaakeys:
 *	Program to load the function keys of an Ann Arbor Ambassador.
 *	Some parts borrowed from termlib (thanks guys). This program uses
 *	termcap styled escapes to load the function keys instead of
 *	doing the idiotic translations by hand.
 *
 *	Probably this program should be made terminal independant,
 *	and suck in strings from other sources(command line,environment)
 *	but it's useful enough as it is.
 *
 *		W. F. Jolitz 11/82
 *		Symmetric Computer Systems
 *		Los Gatos, CA 95030
 */

# include <ctype.h>
# include <stdio.h>

/*
 * Function key name strings.
 * The ordinal number of this array plus ' '
 * corresponds to the ASCII byte value
 * used to set the function key.
 */

char *lookup[] = {
/* ' ' */	"header",
/* '!' */	"trailer",
/* '"' */	"reserved",
/* '#' */	"enq",
/* '$' */	"da",
/* '%' */	"send",
/* '&' */	"reset",
/* ''' */	"setup",
/* '(' */	"break",
/* ')' */	"shift-break",
/* '*' */	"pause",
/* '+' */	"return",
/* ',' */	"move-up",
/* '-' */	"shift-move-up",
/* '.' */	"move-down",
/* '/' */	"shift-move-down",
/* '0' */	"shift-0",
/* '1' */	"shift-1",
/* '2' */	"shift-2",
/* '3' */	"shift-3",
/* '4' */	"shift-4",
/* '5' */	"shift-5",
/* '6' */	"shift-6",
/* '7' */	"shift-7",
/* '8' */	"shift-8",
/* '9' */	"shift-9",
/* ':' */	"period",
/* ';' */	"tab",
/* '<' */	"enter",
/* '=' */	"shift-tab",
/* '>' */	"erase",
/* '?' */	"shift-erase",
/* '@' */	"edit",
/* 'A' */	"delete",
/* 'B' */	"shift-delete",
/* 'C' */	"insert",
/* 'D' */	"shift-insert",
/* 'E' */	"print",
/* 'F' */	"shift-print",
/* 'G' */	"control-shift-7",
/* 'H' */	"pf1",
/* 'I' */	"pf2",
/* 'J' */	"pf3",
/* 'K' */	"pf4",
/* 'L' */	"pf5",
/* 'M' */	"pf6",
/* 'N' */	"pf7",
/* 'O' */	"pf8",
/* 'P' */	"pf9",
/* 'Q' */	"pf10",
/* 'R' */	"pf11",
/* 'S' */	"pf12",
/* 'T' */	"shift-pf1",
/* 'U' */	"shift-pf2",
/* 'V' */	"shift-pf3",
/* 'W' */	"shift-pf4",
/* 'X' */	"shift-pf5",
/* 'Y' */	"shift-pf6",
/* 'Z' */	"shift-pf7",
/* '[' */	"shift-pf8",
/* '\' */	"shift-pf9",
/* ']' */	"shift-pf10",
/* '^' */	"shift-pf11",
/* '_' */	"shift-pf12",
		"\0"
};

main(argc,argv) int argc; char **argv; {
	register char *cp,c;
	char string[512], func[8], line[512], name[80];
	char getstrcol();
	FILE *fp;

	cp = getenv("HOME");
	strcpy(string,cp);
	strcat(string,"/.aaakeys");
	if (( fp  = freopen(string,"r",stdin)) == NULL) {
		printf("aaakeys: cannot open ~/.aaakeys\n");
		exit(1);
	}

	while ((c = getstrcol(name)) == ':') {
		if ((c = getstrcol(line)) == ':') {	
			if ((c = getstrcol(func)) == '\n') 
				dostr(name,line,func);
			else error("bad programming string phrase",func);
		}
		else error("bad function key phrase",line);
	}
}

/*
 * getstrcol:
 *	Fetch a string up to colon or newline.
 */

char
getstrcol(cp) register char *cp; {
	register char c; register back = 0;

	if ( (c = getchar()) != -1)
		while (c > 0 && ( back || (c != ':' && c != '\n'))) {
			if(back)back=0;
			if(c == '\\') back++;
			*cp++ = c;
			c = getchar();
		};
	*cp = '\0';
	return(c);
}

/*
 * dostr:
 *	Process a valid function key
 */

dostr(key,line,name) char *key,*line,*name; {
	register char **lp;
	char buffer[512], buffer2[512], *decode();

	lp = &lookup;
	while (**lp != '\0') {
		if ( **lp == *key && strcmp(*lp,key) == 0) {
			decode(line,buffer);
			translate(buffer,buffer2);
			printf("\033P`%c%s\033\\",' '+(lp-lookup),
				buffer2);
			return;
		}
		lp++;
	}
	printf("can't find `%s'\n",key);
}

/*
 * Indicate where to complain.
 */

error(s,n) char *s,*n; { printf("%s:  ->%s<-\n",s,n); exit(1); }

/*
 * Decode does the grung work to decode the
 * string escapes.
 */

static char *
decode(str, area)
	register char *str;
	char *area;
{
	register char *cp;
	register int c;
	register char *dp;
	int i;

	cp = area;
	while ((c = *str++) && c != ':') {
		switch (c) {

		case '^':
			c = *str++ & 037;
			break;

		case '\\':
			dp = "E\033^^\\\\::n\nr\rt\tb\bf\f";
			c = *str++;
nextc:
			if (*dp++ == c) {
				c = *dp++;
				break;
			}
			dp++;
			if (*dp)
				goto nextc;
			if (isdigit(c)) {
				c -= '0', i = 2;
				do
					c <<= 3, c |= *str++ - '0';
				while (--i && isdigit(*str));
			}
			break;
		}
		*cp++ = c;
	}
	*cp++ = 0;
	str = area;
	area = cp;
	return (str);
}

/*
 * translate:
 *	convert an ascii string into an downloadable ("DCS")
 *	string for the Ambassador.
 */

translate(in,out) register char *in, *out; {
	register shift = 0;

	while (*in) {

		if (shift && ((*in >= '`') && (*in <= 'z'))) {
			*out++ = toupper(*in++);
			continue;
		}
		else if (shift) {
			*out++ = '}';
			shift = 0;
		}

		if ( *in >= ' ' && *in <= '_') {
			*out++ = *in++;
			continue;
		}
		if ( *in < ' ') {
			*out++ = '~';
			*out++ = *in++ + ' ';
			continue;
		}
		if ( *in >= '`' && *in <= 'z') {
			*out++ = '{';
			shift++ ;
			*out++ = toupper(*in++);
			continue;
		} 
			
	}
	if(shift) *out++ = '}';
	*out = '\0';
}
-----------------------------------------------aaakeys.1
.TH AAAKEYS LOCAL 
.SH NAME
aaakeys \- safe and sane program to load Ambassador function keys
.SH SYNOPSIS
.B aaakeys
.SH DESCRIPTION
.I aaakeys
loads function keys from a file in the users home directory.
This program translates strings in the file "~/.aaakeys" into
a form suitable for a Ann Arbor Ambassador.
.SH FILE FORMAT
The download file has a format not unlike a termcap(5) file.
Lines are of the form <keyname>:<programmed string>, where
<programmed string> consist of any ascii characters and
termcap escapes, while <keyname> can be any of these strings:
.nf

break control-shift-7 da delete edit enq
enter erase header insert move-down move-up
pause period pf1 pf2 pf3 pf4 pf5 pf6 pf7
pf8 pf9 pf10 pf11 pf12 print reserved
reset return send setup shift-0 shift-1
shift-2 shift-3 shift-4 shift-5 shift-6
shift-7 shift-8 shift-9 shift-break
shift-delete shift-erase shift-insert
shift-move-down shift-move-up shift-pf1
shift-pf2 shift-pf3 shift-pf4 shift-pf5
shift-pf6 shift-pf7 shift-pf8 shift-pf9
shift-pf10 shift-pf11 shift-pf12
shift-print shift-tab tab trailer
.fi
.SH FILES
 .aaakeys
.SH "SEE ALSO"
termcap(5)
.SH DIAGNOSTICS
.nf
bad programming string phrase
bad function key phrase
cannot open ~/.aaakeys
can't find `function key'
.fi
-----------------------------------------------