From decwrl!ucbvax!ucsd!usc!cs.utexas.edu!uunet!allbery Mon May  7 07:41:01 PDT 1990
Article 1530 of comp.sources.misc:
Path: decwrl!ucbvax!ucsd!usc!cs.utexas.edu!uunet!allbery
From: daniel@island.UUCP (Dan Smith "Happy Birthday James Brown G.O.S.")
Newsgroups: comp.sources.misc
Subject: v12i050: Grabchars 1.9, get keystrokes direct from user BSD/SYSV part 2
Message-ID: <87553@uunet.UU.NET>
Date: 5 May 90 17:28:08 GMT
Sender: allbery@uunet.UU.NET
Lines: 264
Approved: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)

Posting-number: Volume 12, Issue 50
Submitted-by: daniel@island.UUCP (Dan Smith "Happy Birthday James Brown G.O.S.")
Archive-name: grabchars-1.9/part02

	This is part 2 of the grabchars kit...

				Daniel

   dansmith@well.sf.ca.us   daniel@island.uu.net   unicom!daniel@pacbell.com
ph: (415) 332 3278 (h), 491 1000 (w) disclaimer: Island's coffee was laced :-)

#! /bin/sh

# Make a new directory for the grabchars sources, cd to it, and run kits 1
# thru 2 through sh.  When all 2 kits have been run, read README.

echo "This is grabchars 1.9 kit 2 (of 2).  If kit 2 is complete, the line"
echo '"'"End of kit 2 (of 2)"'" will echo at the end.'
echo ""
export PATH || (echo "You didn't use sh, you clunch." ; kill $$)
mkdir web 2>/dev/null
echo Extracting demo
sed >demo <<'!STUFFY!FUNK!' -e 's/X//'
X#!/bin/csh -f
Xclear
Xcat << GUMBY
X
X	Grabchars demo...
X
X	get one character with "grabchars"
XGUMBY
Xgrabchars
Xecho " status returned was $status"
X
Xcat << POKEY
X
X	grab a vowel with "grabchars -caeiou"
X
X	Type something that isn't a vowel at first...
X
XPOKEY
Xgrabchars -caeiou
Xecho " status returned was $status"
X
Xcat << WILMA
X
X	prompt the user  with "grabchars -p 'give me any character >> '"
XWILMA
Xgrabchars -p 'give me any character >> '
Xecho " status returned was $status"
X
Xcat << FRED
X
X	prompt through stderr  with "grabchars -q 'give me any character >> '",
Xso that we can set the variable "user_char"...
XFRED
Xset user_char=`grabchars -q 'give me any character >> '`
Xecho " status returned was $status"
Xecho '$user_char = '$user_char
X
Xcat << BETTY
X
X	enter three characters...  "grabchars -n3"
X
XBETTY
Xgrabchars -n3
Xecho " status returned was $status"
X
Xcat << DINO
X
X	map 5 characters to upper case.... "grabchars -n5 -U"
X
XDINO
Xgrabchars -n5 -U
X
Xcat <<  BARNEY
X
X	enter 10 characters within 3 seconds... "grabchars -n10 -t3"
X
X
XBARNEY
Xgrabchars -n10 -t3
Xset really_typed=$status
Xif ($really_typed == 10) then
X	echo 'hey\! you got 10\!?'
Xelse
X	echo "    $really_typed returned...means that grabchars timed out..."
Xendif
X
Xdef_test:
Xcat << RADRED
X
X	let the user pick a default...
X	using "grabchars -d yes -p 'just hit return '...
X
XRADRED
X#	the '-f' here is used to flush any previous input...
Xgrabchars -f -d yes -p 'just hit return '
Xif ($status != 3) then
X	echo whoops\!  Let\'s try that again...
X	goto def_test
Xendif
X
Xcat << MUSKRAT
X
X	same idea, but let this timeout in four seconds...
X	using "grabchars -f -d always -t 4"
X
X	(don't type anything...)
X
XMUSKRAT
Xgrabchars -f -d always -t 4
X
Xcat << PEBBLES
X
X
X	The last one...  get two numbers with a ten second timeout...
X	trying "grabchars -c 0123456789 -n2 -t10 -p 'give me 2 numbers >> '
X
XPEBBLES
Xgrabchars -c 0123456789 -n2 -t10 -p 'give me 2 numbers >> '
Xecho " status returned was $status"
Xecho ""
Xecho test/demo done...enjoy\!
!STUFFY!FUNK!
chmod 755 demo
echo Extracting globals.c
sed >globals.c <<'!STUFFY!FUNK!' -e 's/X//'
X/*
X**	$Header: globals.c,v 1.9 89/12/29 21:14:44 daniel grabchars_1_9 $
X**
X**	globals.c - declare global variables for grabchars
X**
X**	Dan Smith (daniel@island.uu.net), November 29, 1988
X*/
X#include <stdio.h>
X#include "grabchars.h"	/* where we typedef FLAGS */
X
XFILE *outfile, *otherout;
XFLAG *flags;
X
Xint exit_stat;
Xchar valid_chars[128], default_string[128];
X
X#ifdef DV_ERASE
Xchar *erase_buf;	/* DV: Malloc'ed later */
X#endif
X
X/*
X**	this gets in the way sitting in grabchars.c, so I moved it here.
X**	If you add anything, keep in mind that you're trying to squeeze
X**	everything onto a 24 line window/terminal... The "-h" doc
X**	is just a convenience, any option that grabchars doesn't already
X**	use will give the help screen...
X*/
Xchar *usage_statement[] = {
X"usage: grabchars		gets one keystroke",
X"	-b			output to stdout and stderr",
X"	-c<valid characters>	only <valid chars> are returned",
X"	-d<char(s)>		default char or string to return",
X"	-e			output to stderr instead of stdout",
X"	-f			flush any previous input before reading",
X"	-h			help screen",
X"	-n<number>		number of characters to read",
X"	-p<prompt>		prompt to help user",
X"	-q<prompt>		prompt to help user (through stderr)",
X"	-r			RETURN key exits (use with -n)",
X"	-s			silent, just return status",
X"	-t<seconds>		timeout after <seconds>",
X"	-E			honor erase/kill characters", /* DV */
X"	-U/-L			upper/lower case mapping on input",
X"grabchars -c aeiou		get one of the vowels",
X"grabchars -c i			get the letter 'i'",
X"grabchars '-penter a letter '	print the prompt \"enter a letter \"",
X"grabchars -n4			get four characters",
X"grabchars -t2			timeout after two seconds",
X" ",
X"print a prompt and grab three characters...",
X"grabchars -p 'enter three characters >> ' -n 3",
X0
X};
X
Xstatic char my_copyright[]=
X"grabchars 1.9, (c) Dan Smith (daniel@island.uu.net), 1990";
!STUFFY!FUNK!
echo Extracting grabchars.h
sed >grabchars.h <<'!STUFFY!FUNK!' -e 's/X//'
X/*
X**	$Header: grabchars.h,v 1.9 89/12/29 21:15:25 daniel grabchars_1_9 $
X**
X**	grabchars.h - setup for grabchars
X**
X**	Dan Smith (daniel@island.uu.net), November 29, 1988
X*/
X
X#define mk_lower(c) (isupper (c) ? tolower (c) : c)
X#define mk_upper(c) (islower (c) ? toupper (c) : c)
X
X/* define SIGRET=void in Makefile for Sun 4.X */
X#ifndef SIGRET
X#define SIGRET int
X#endif
X
Xstruct flag_type {
X	int both;	/* output to stdout and stderr */
X	int check;	/* filter input */
X	int dflt;	/* use following char or string as default */
X	int used_dflt;	/* if editing on and used default don't echo stderr */
X	int flush;	/* if set flush input buffer */
X	int ret_key;	/* RETURN key exits when set */
X	int silent;	/* be quiet, just return a status */
X	int erase;	/* DV: honor erase/kill characters */
X	int lower;	/* map chars to lower case */
X	int upper;	/* map chars to upper case */
X};
X
Xtypedef struct flag_type FLAG;
X
!STUFFY!FUNK!
echo Extracting MANIFEST
sed >MANIFEST <<'!STUFFY!FUNK!' -e 's/X//'
XAfter all the grabchars kits are run you should have the following files:
X
XFilename		Kit Description
X--------		--- -----------
XConfig                   1 configure grabchars
XMANIFEST                 2 Packing list
XMakefile.dist            1 Config makes Makefile from this
XREADME                   1 general idea of all things grabby
XTODO                     1 some thoughts on improvements
Xdemo                     2 test/familiarize with grabchars
Xglobals.c                2 global vars
Xgrabchars.1              1 man page
Xgrabchars.c              1 main program
Xgrabchars.h              2 defines
Xsys.c                    1 system calls
Xweb/README               1 README for the two types of script exmaples
Xweb/gensource            1 called by mkmenu
Xweb/mkmenu               1 use to generate menu-oriented csh scripts
Xweb/script.template      1 example script
!STUFFY!FUNK!
echo ""
echo "End of kit 2 (of 2)"
cat /dev/null >kit2isdone
run=''
config=''
for iskit in 1 2; do
    if test -f kit${iskit}isdone; then
	run="$run $iskit"
    else
	todo="$todo $iskit"
    fi
done
case $todo in
    '')
	echo "You have run all your kits.  Please read README and then type Config."
	chmod 755 Config
	;;
    *)  echo "You have run$run."
	echo "You still need to run$todo."
	;;
esac
: Someone might mail this, so...
exit


