Fkey

John R. MacMillan jrmacmillan at watdragon.waterloo.edu
Mon Sep 12 11:04:21 AEST 1988


This is one of those "I wonder if that would work..." programs.
It did; if you're in emacs or gmacs mode of the ksh, this lets
you use the function keys.

It's none too pretty; I'm posting it in the hope that some
inventive soul can think of something genuinely useful to
do with it.


#!/bin/sh
#  This is a shar file.  To extract, sh this file
#
#  Contents:
#	README
#	Makefile
#	demo
#	fkey.c
#
#  Wrapped by john at mystic ; Sun Sep 11 20:55:43 EST 1988
#
if [ -f "README" ] ; then
	echo "shar: Will not overwrite existing file README"
else
	echo "shar: extracting README (1686 characters)"
	sed 's/^X//' <<'BOP_BOP_A_LOO_WOP_BO_LOP_BAM_BOOM' > README
XThis is a quick hack to let UNIXpc users who use the ksh in emacs (or
Xgmacs) mode use the function keys.  A wee bit specialized, but...
X
XThe idea is that ksh lets you alias _O to something, and expands it if
Xyou type ESC O (O can be any letter ksh isn't already using).  The
Xeight function keys on the UNIXpc put out ESC O [c-j] (unshifted).
XSo with an appropriate _O alias, you can get the function keys to do
Xthings.
X
XFkey lets you do this. Usage is "fkey [name]".  Fkey reads one char
Xfrom stdin (the extra character that the function key put out), and
Xgets the value of the environment variable "name_c" where name is
Xargv[1] or argv[0] and c is the character read.
X
XIf the first character of name_c is a '$', fkey assumes the rest is
Xname of another environment variable, and prints it.  If the first
Xcharacter is not a '$', fkey execs 'ksh -c "name_c"'.
X
XThis is easier to demonstrate than explain (for me, anyway!) so make
Xsure you're in ksh [eg]macs mode, and source the file "demo".  The
Xfunction keys and the Rfrsh key should now do things.  Take a look at
Xdemo to see what's going on.
X
XNote that since the function is executing in a subshell, things like
Xcd won't be effective.
X
XNote that you can use this with a bunch of the other keys on the
XUNIXpc.  There are a bunch other than the function keys that use ESC O, 
Xand some use ESC N and ESC P.  Some however (eg. cursor keys) use
XESC [ which won't work.
X
XI wrote this as an educational exercise (to see if it would work), and
Xdecided to post it to see if anyone more ingenious than myself can
Xmake it do anything truly useful.
X
XSuggestions welcome.
X
XJohn R. MacMillan
Xjohn%mystic at math.waterloo.edu
X...!watmath!mystic!john
BOP_BOP_A_LOO_WOP_BO_LOP_BAM_BOOM
	set -- `wc -c README`
	if [ "$1" != "1686" ] ; then
		echo "shar: README unpacked with wrong size!"
	fi
fi
if [ -f "Makefile" ] ; then
	echo "shar: Will not overwrite existing file Makefile"
else
	echo "shar: extracting Makefile (131 characters)"
	sed 's/^X//' <<'BOP_BOP_A_LOO_WOP_BO_LOP_BAM_BOOM' > Makefile
XCFLAGS=-O
XLDFLAGS=-s
X
Xfkey:	fkey.o
X	$(LD) $(LDFLAGS) /lib/crt0s.o -o $@ fkey.o /lib/shlib.ifile
X
Xclean:
X	rm -f fkey *.o core *.out
BOP_BOP_A_LOO_WOP_BO_LOP_BAM_BOOM
	set -- `wc -c Makefile`
	if [ "$1" != "131" ] ; then
		echo "shar: Makefile unpacked with wrong size!"
	fi
fi
if [ -f "demo" ] ; then
	echo "shar: Will not overwrite existing file demo"
else
	echo "shar: extracting demo (598 characters)"
	sed 's/^X//' <<'BOP_BOP_A_LOO_WOP_BO_LOP_BAM_BOOM' > demo
X#
X# Sample uses of fkey for ESC O c keys (function keys and others)
X#
X
Xset -o emacs
X
Xalias	_O='fkey
X'
X
XClear=`tput clear`
X
Xfkey_c='$PWD'
Xfkey_d='$Clear'
Xfkey_e='ls -l'
X
Xfkey_f='echo *.c'
Xfkey_g="ps -fu$LOGNAME"
X
Xfkey_h='date'
Xfkey_i='du'
Xfkey_j='echo I could not think of anything to put on this key'
X
Xexport	Clear fkey_c fkey_d fkey_e fkey_f fkey_g fkey_h fkey_i fkey_j
X
X#
X# Use of parameter to fkey; in this case, allow Clear/Rfrsh key to do
X# a clear.  Instead of doing it this way, you could link fkey to nkey
X# (say) and use it as above
X#
X
Xalias _N='fkey NKEY
X'
X
XNKEY_a='clear'
X
Xexport NKEY_a
BOP_BOP_A_LOO_WOP_BO_LOP_BAM_BOOM
	set -- `wc -c demo`
	if [ "$1" != "598" ] ; then
		echo "shar: demo unpacked with wrong size!"
	fi
fi
if [ -f "fkey.c" ] ; then
	echo "shar: Will not overwrite existing file fkey.c"
else
	echo "shar: extracting fkey.c (1880 characters)"
	sed 's/^X//' <<'BOP_BOP_A_LOO_WOP_BO_LOP_BAM_BOOM' > fkey.c
X/*
X * Allow use of the UNIXpc function keys when using ksh in
X * emacs or gmacs mode.
X *
X * Usage: fkey [name]
X *
X * combined with alias _O='fkey
X * '
X * and environment var(s) fkey_c, executes the value of fkey_c when the
X * function key that generates ESC O c is pressed.
X *
X * If "name" is specified, use environment var name_c.
X *
X * John R. MacMillan
X */
X
X#include <sys/termio.h>
X#include <string.h>
X
X#ifndef	NULL
X# define	NULL	(0)
X#endif
X#define	BUFSIZE	50
X#define	MAXARGS	100
X
Xmain( argc, argv )
Xint	argc;
Xchar	**argv;
X{
X	struct	termio	old, new;
X	char	key, *name, buffer[BUFSIZE], *bufp, *cmd;
X	int	nchars, status;
X	char	*strcpy(), *getenv();
X	int	ioctl(), read(), strlen(), puts(), fork(), execl(), wait();
X	void	exit();
X
X	if ( argc == 1 )
X		name = *argv;
X	else
X		name = *++argv;
X
X	/*
X	 * Set up the terminal to read the char that should be buffered
X	 */
X
X	if ( ioctl( 0, TCGETA, &old ) == -1 )
X		exit( -1 );
X	new = old;
X	new.c_lflag &= ~ICANON;
X	new.c_cc[VMIN]=1;
X	new.c_cc[VTIME]=0;
X	if ( ioctl( 0, TCSETA, &new ) == -1 )
X		exit( -1 );
X
X	/*
X	 * Read the char and reset the terminal
X	 */
X
X	nchars = read( 0, &key, 1 );
X	if ( ioctl( 0, TCSETA, &old ) == -1 )
X		exit( -1 );
X
X	if ( nchars != 1 )
X		exit( nchars );
X
X	/*
X	 * Create the name of the environment variable
X	 */
X
X	bufp = strcpy( buffer, name );
X	bufp += strlen( buffer );
X	*bufp++ = '_';
X	*bufp++ = key;
X	*bufp = '\0';
X
X	/*
X	 * Get the variable
X	 */
X
X	if ( (cmd = getenv( buffer )) == NULL )
X		exit( 1 );
X
X	if ( *cmd == '$' ) {
X		/*
X		 * Print an environment variable
X		 */
X		cmd = getenv( ++cmd );
X		(void) puts( cmd );
X	} else {
X		/*
X		 * Get the ksh to execute it
X		 */
X		switch( fork() ) {
X		case -1:
X			exit( -1 );
X			break;
X		case 0:
X			(void) execl( "/bin/ksh", "ksh", "-c", cmd, 0 );
X			exit( -1 );
X			break;
X		default:
X			(void) wait( &status );
X			exit( status>>8 );
X		}
X	}
X	exit( 0 );
X
X/*NOTREACHED*/
X}
BOP_BOP_A_LOO_WOP_BO_LOP_BAM_BOOM
	set -- `wc -c fkey.c`
	if [ "$1" != "1880" ] ; then
		echo "shar: fkey.c unpacked with wrong size!"
	fi
fi
exit

-- 
John R. MacMillan
jrmacmillan at dragon.waterloo.edu		If the universe fits, wear it.
...!watmath!dragon!jrmacmillan



More information about the Unix-pc.sources mailing list