home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ARM Club 3
/
TheARMClub_PDCD3.iso
/
hensa
/
texted
/
b084_1
/
c
/
input
< prev
next >
Wrap
Text File
|
1991-04-20
|
30KB
|
890 lines
/* Input: Various input routines for MicroEMACS
written by Daniel Lawrence
5/9/86 */
/*
Notes:
MicroEMACS's kernel processes two distinct forms of
characters. One of these is a standard unsigned character
which is used in the edited text. The other form, called
an EMACS Extended Character is a 2 byte value which contains
both an ascii value, and flags for certain prefixes/events.
Bit Usage
--- -----
0 = 7 Standard 8 bit ascii character
8 Control key flag
9 META prefix flag
10 ^X prefix flag
11 Function key flag
12 Mouse prefix
13 Shifted flag (not needed on alpha shifted characters)
14 Alterate prefix (ALT key on PCs)
The machine dependent driver is responsible for returning
a byte stream from the various input devices with various
prefixes/events embedded as escape codes. Zero is used as the
value indicating an escape sequence is next. The format of
an escape sequence is as follows:
0 Escape indicator
<prefix byte> upper byte of extended character
{<col><row>} col, row position if the prefix byte
indicated a mouse event
<event code> value of event
Two successive zeroes are used to indicate an actual
null being input. These values are then interpreted by
getkey() to construct the proper extended character
sequences to pass to the MicroEMACS kernel.
*/
#include <stdio.h>
#include "estruct.h"
#include "etype.h"
#include "edef.h"
#include "elang.h"
#if USG | BSD | V7
#include <sys/types.h>
#include <pwd.h>
#ifndef HELIOS
extern struct passwd *getpwnam();
#endif
#if USG
#define index strchr
#endif
#endif
/*
* Ask a yes or no question in the message line. Return either TRUE, FALSE, or
* ABORT. The ABORT status is returned if the user bumps out of the question
* with a ^G. Used any time a confirmation is required.
*/
PASCAL NEAR mlyesno(prompt)
char *prompt;
{
int c; /* input character */
char buf[NPAT]; /* prompt to user */
for (;;) {
/* build and prompt the user */
strcpy(buf, prompt);
strcat(buf, TEXT162);
/* " [y/n]? " */
mlwrite(buf);
/* get the response */
c = getcmd(); /* getcmd() lets us check for anything that might */
/* generate a 'y' or 'Y' in case use screws up */
if (c == ectoc(abortc)) /* Bail out! */
return(ABORT);
if ((c == 'n') || (c == 'N')
|| (c & (SPEC|ALTD|CTRL|META|CTLX|MOUS)))
return(FALSE); /* ONLY 'y' or 'Y' allowed!!! */
#if FRENCH
if (c=='o' || c=='O')
return(TRUE);
#endif
if (c=='y' || c=='Y')
return(TRUE);
return(FALSE);
}
}
/*
* Write a prompt into the message line, then read back a response. Keep
* track of the physical position of the cursor. If we are in a keyboard
* macro throw the prompt away, and return the remembered response. This
* lets macros run at full speed. The reply is always terminated by a carriage
* return. Handle erase, kill, and abort keys.
*/
PASCAL NEAR mlreply(prompt, buf, nbuf)
char *prompt;
char *buf;
{
return(nextarg(prompt, buf, nbuf, ctoec((int) '\r')));
}
PASCAL NEAR mltreply(prompt, buf, nbuf, eolchar)
char *prompt;
char *buf;
int eolchar;
{
return(nextarg(prompt, buf, nbuf, eolchar));
}
/* ectoc: expanded character to character
collapse the CTRL and SPEC flags back into an ascii code */
PASCAL NEAR ectoc(c)
int c;
{
if (c & CTRL)
c = c & ~(CTRL | 0x40);
if (c & SPEC)
c= c & 255;
return(c);
}
/* ctoec: character to extended character
pull out the CTRL and SPEC prefixes (if possible) */
PASCAL NEAR ctoec(c)
int c;
{
if (c>=0x00 && c<=0x1F)
c = CTRL | (c+'@');
return(c);
}
/* get a command name from the command line. Command completion means
that pressing a <SPACE> will attempt to complete an unfinished command
name if it is unique.
*/
int (PASCAL NEAR *PASCAL NEAR getname(prompt))()
char *prompt; /* string to prompt with */
{
char *sp; /* ptr to the returned string */
sp = complete(prompt, NULL, CMP_COMMAND, NSTRING);
if (sp == NULL)
return(NULL);
return(fncmatch(sp));
}
/* getcbuf: get a completion from the user for a buffer name.
I was goaded into this by lots of other people's
completion code.
*/
BUFFER *PASCAL NEAR getcbuf(prompt, defval, createflag)
char *prompt; /* prompt to user on command line */
char *defval; /* default value to display to user */
int createflag; /* should this create a new buffer? */
{
char *sp; /* ptr to the returned string */
sp = complete(prompt, defval, CMP_BUFFER, NBUFN);
if (sp == NULL)
return(NULL);
return(bfind(sp, createflag, 0));
}
char *PASCAL NEAR gtfilename(prompt)
char *prompt; /* prompt to user on command line */
{
char *sp; /* ptr to the returned string */
sp = complete(prompt, NULL, CMP_FILENAME, NFILEN);
if (sp == NULL)
return(NULL);
return(sp);
}
char *PASCAL NEAR complete(prompt, defval, type, maxlen)
char *prompt; /* prompt to user on command line */
char *defval; /* default value to display to user */
int type; /* type of what we are completing */
int maxlen; /* maximum length of input field */
{
register int c; /* current input character */
int cpos; /* current column on screen output */
static char buf[NSTRING];/* buffer to hold tentative name */
#if USG | BSD | V7
char *home;
struct passwd *pwd;
#endif
#if COMPLET == 0
int status;
#endif
/* if we are executing a command line get the next arg and match it */
if (clexec) {
if (macarg(buf) != TRUE)
return(NULL);
return(buf);
}
#if COMPLET == 0
strcpy(buf, prompt);
if (type != CMP_COMMAND) {
if (defval) {
strcat(buf, "[");
strcat(buf, defval);
strcat(buf, "]: ");
} else {
strcat(buf, ": ");
}
}
status = mlreply(buf, buf, maxlen);
if (status == ABORT)
return(NULL);
if (defval && (buf[0] == 0))
return(defval);
return(buf);
}
#else
/* starting at the beginning of the string buffer */
cpos = 0;
/* if it exists, prompt the user for a buffer name */
if (prompt)
{ if (type == CMP_COMMAND)
mlwrite("%s", prompt);
else if (defval)
mlwrite("%s[%s]: ", prompt, defval);
else
mlwrite("%s: ", prompt);
}
/* build a name string from the keyboard */
while (TRUE) {
c = tgetc();
/* if we are at the end, just match it */
if (c == '\n' || c == '\r') {
if (defval && cpos==0)
return(defval);
else {
buf[cpos] = 0;
return(buf);
}
} else if (c == ectoc(abortc)) { /* Bell, abort */
ctrlg(FALSE, 0);
TTflush();
return(NULL);
} else if (c == 0x7F || c == 0x08) { /* rubout/erase */
if (cpos != 0) {
mlout('\b');
mlout(' ');
mlout('\b');
--ttcol;
--cpos;
TTflush();
}
} else if (c == 0x15) { /* C-U, kill */
while (cpos != 0) {
mlout('\b');
mlout(' ');
mlout('\b');
--cpos;
--ttcol;
}
TTflush();
#if USG | BSD | V7
} else if (c == '/' && type == CMP_FILENAME && buf[0] == '~') {
int i;
if (cpos == 1) {
if (home = (char *)getenv("HOME")) {
mlout('\b'); /* backup over ~ */
mlout(' ');
mlout('\b');
ttcol--;
TTflush();
strcpy(buf, home);
cpos = strlen(buf);
buf[cpos++] = '/';
for (i = 0; i < cpos; i++) {
mlout(buf[i]);
ttcol++;
}
TTflush();
} else
goto nextc;
} else {
buf[cpos] = '\0';
if (pwd = getpwnam(&buf[1])) {
while (cpos != 0) { /* kill */
mlout('\b'); /* line */
mlout(' ');
mlout('\b');
--cpos;
--ttcol;
}
TTflush();
strcpy(buf, pwd->pw_dir);
cpos = strlen(buf);
buf[cpos++] = '/';
for (i = 0; i < cpos; i++) {
mlout(buf[i]);
ttcol++;
}
TTflush();
} else
goto nextc;
}
} else if (((c == ' ') || (c == ectoc(sterm)) || (c == '\t') || (c == '/')) &&
type == CMP_FILENAME && buf[0] == '$') {
int i;
buf[cpos] = '\0';
if (home = (char *)getenv(&buf[1])) {
while (cpos != 0) { /* kill */
mlout('\b'); /* line */
mlout(' ');
mlout('\b');
--cpos;
--ttcol;
}
TTflush();
strcpy(buf, home);
cpos = strlen(buf);
if (c == '/') buf[cpos++] = '/';
for (i = 0; i < cpos; i++) {
mlout(buf[i]);
ttcol++;
}
TTflush();
} else
goto nextc;
#endif
} else if ((c == ' ') || (c == ectoc(sterm)) || (c == '\t')) {
/* attempt a completion */
switch (type) {
case CMP_BUFFER:
comp_buffer(buf, &cpos);
break;
case CMP_COMMAND:
comp_command(buf, &cpos);
break;
case CMP_FILENAME:
comp_file(buf, &cpos);
break;
}
TTflush();
if (buf[cpos - 1] == 0)
return(buf);
} else {
nextc: if (cpos < maxlen && c > ' ') {
buf[cpos++] = c;
mlout(c);
++ttcol;
TTflush();
}
}
}
}
/* comp_command: Attempt a completion on a command name */
comp_command(name, cpos)
char *name; /* command containing the current name to complete */
int *cpos; /* ptr to position of next character to insert */
{
register NBIND *bp; /* trial command to complete */
register int index; /* index into strings to compare */
register int curbind; /* index into the names[] array */
register NBIND *match; /* last command that matches string */
register int matchflag; /* did this command name match? */
register int comflag; /* was there a completion at all? */
/* start attempting completions, one character at a time */
comflag = FALSE;
curbind = 0;
while (*cpos < NSTRING) {
/* first, we start at the first command and scan the list */
match = NULL;
curbind = 0;
while (curbind <= numfunc) {
/* is this a match? */
bp = &names[curbind];
matchflag = TRUE;
for (index = 0; index < *cpos; index++)
if (name[index] != bp->n_name[index]) {
matchflag = FALSE;
break;
}
/* if it is a match */
if (matchflag) {
/* if this is the first match, simply record it */
if (match == NULL) {
match = bp;
name[*cpos] = bp->n_name[*cpos];
} else {
/* if there's a difference, stop here */
if (name[*cpos] != bp->n_name[*cpos])
return;
}
}
/* on to the next command */
curbind++;
}
/* with no match, we are done */
if (match == NULL) {
/* beep if we never matched */
if (comflag == FALSE)
TTbeep();
return;
}
/* if we have completed all the way... go back */
if (name[*cpos] == 0) {
(*cpos)++;
return;
}
/* remember we matched, and complete one character */
comflag = TRUE;
TTputc(name[(*cpos)++]);
TTflush();
}
/* don't allow a completion past the end of the max command name length */
return;
}
/* comp_buffer: Attempt a completion on a buffer name */
comp_buffer(name, cpos)
char *name; /* buffer containing the current name to complete */
int *cpos; /* ptr to position of next character to insert */
{
register BUFFER *bp; /* trial buffer to complete */
register int index; /* index into strings to compare */
register BUFFER *match; /* last buffer that matches string */
register int matchflag; /* did this buffer name match? */
register int comflag; /* was there a completion at all? */
/* start attempting completions, one character at a time */
comflag = FALSE;
while (*cpos < NBUFN) {
/* first, we start at the first buffer and scan the list */
match = NULL;
bp = bheadp;
while (bp) {
/* is this a match? */
matchflag = TRUE;
for (index = 0; index < *cpos; index++)
if (name[index] != bp->b_bname[index]) {
matchflag = FALSE;
break;
}
/* if it is a match */
if (matchflag) {
/* if this is the first match, simply record it */
if (match == NULL) {
match = bp;
name[*cpos] = bp->b_bname[*cpos];
} else {
/* if there's a difference, stop here */
if (name[*cpos] != bp->b_bname[*cpos])
return;
}
}
/* on to the next buffer */
bp = bp->b_bufp;
}
/* with no match, we are done */
if (match == NULL) {
/* beep if we never matched */
if (comflag == FALSE)
TTbeep();
return;
}
/* if we have completed all the way... go back */
if (name[*cpos] == 0) {
(*cpos)++;
return;
}
/* remember we matched, and complete one character */
comflag = TRUE;
TTputc(name[(*cpos)++]);
TTflush();
}
/* don't allow a completion past the end of the max buffer name length */
return;
}
/* comp_file: Attempt a completion on a file name */
comp_file(name, cpos)
char *name; /* file containing the current name to complete */
int *cpos; /* ptr to position of next character to insert */
{
register char *fname; /* trial file to complete */
register int index; /* index into strings to compare */
register int matches; /* number of matches for name */
char longestmatch[NSTRING]; /* temp buffer for longest match */
int longestlen; /* length of longest match (always > *cpos) */
/* everything (or nothing) matches an empty string */
if (*cpos == 0)
return;
/* first, we start at the first file and scan the list */
matches = 0;
name[*cpos] = 0;
fname = getffile(name);
while (fname) {
/* is this a match? */
if (strncmp(name,fname,*cpos) == 0) {
/* count the number of matches */
matches++;
/* if this is the first match, simply record it */
if (matches == 1) {
strcpy(longestmatch,fname);
longestlen = strlen(longestmatch);
} else {
/* if there's a difference, stop here */
if (longestmatch[*cpos] != fname[*cpos])
return;
for (index = (*cpos) + 1; index < longestlen; index++)
if (longestmatch[index] != fname[index]) {
longestlen = index;
longestmatch[longestlen] = 0;
}
}
}
/* on to the next file */
fname = getnfile();
}
/* beep if we never matched */
if (matches == 0) {
TTbeep();
return;
}
/* the longestmatch array contains the longest match so copy and print it */
for ( ; (*cpos < (NSTRING-1)) && (*cpos < longestlen); (*cpos)++) {
name[*cpos] = longestmatch[*cpos];
TTputc(name[*cpos]);
}
name[*cpos] = 0;
/* if only one file matched then increment cpos to signal complete() */
/* that this was a complete match. If a directory was matched then */
/* last character will be the DIRSEPCHAR. In this case we do NOT */
/* want to signal a complete match. */
if ((matches == 1) && (name[(*cpos)-1] != DIRSEPCHAR))
(*cpos)++;
TTflush();
return;
}
#endif
/* tgetc: Get a key from the terminal driver, resolve any keyboard
macro action */
int PASCAL NEAR tgetc()
{
int c; /* fetched character */
/* if we are playing a keyboard macro back, */
if (kbdmode == PLAY) {
/* if there is some left... */
if (kbdptr < kbdend)
return((int)*kbdptr++);
/* at the end of last repitition? */
if (--kbdrep < 1) {
kbdmode = STOP;
#if VISMAC == 0
/* force a screen update after all is done */
update(FALSE);
#endif
} else {
/* reset the macro to the begining for the next rep */
kbdptr = &kbdm[0];
return((int)*kbdptr++);
}
}
/* fetch a character from the terminal driver */
c = TTgetc();
/* record it for $lastkey */
lastkey = c;
/* save it if we need to */
if (kbdmode == RECORD) {
*kbdptr++ = c;
kbdend = kbdptr;
/* don't overrun the buffer */
if (kbdptr == &kbdm[NKBDM - 1]) {
kbdmode = STOP;
TTbeep();
}
}
/* and finally give the char back */
return(c);
}
/* getkey: Get one keystroke. The only prefixs legal here
are the SPEC and CTRL prefixes.
*/
PASCAL NEAR getkey()
{
int c; /* next input character */
int upper; /* upper byte of the extended sequence */
/* get a keystroke */
c = tgetc();
/* if it exists, process an escape sequence */
if (c == 0) {
/* get the event type */
upper = tgetc();
/* mouse events need us to read in the row/col */
if (upper & (MOUS >> 8)) {
/* grab the x/y position of the mouse */
xpos = tgetc();
ypos = tgetc();
}
/* get the event code */
c = tgetc();
/* if it is a function key... map it */
#if 0 /* this should be here...fix it for MSDOS dan! */
if (upper & (SPEC >> 8))
c = (upper << 8) | extcode(c & 255);
else
#endif
c = (upper << 8) | c;
}
/* yank out the control prefix */
if ((c & 255) >=0x00 && (c & 255) <= 0x1F)
c = CTRL | (c+'@');
/* return the character */
return(c);
}
/* GETCMD: Get a command from the keyboard. Process all applicable
prefix keys
*/
PASCAL NEAR getcmd()
{
int c; /* fetched keystroke */
KEYTAB *key; /* ptr to a key entry */
/* get initial character */
c = getkey();
key = getbind(c);
/* resolve META and CTLX prefixes */
if (key) {
if (key->k_ptr.fp == meta) {
c = getkey();
c = upperc(c) | (c & ~255); /* Force to upper */
c |= META;
} else if (key->k_ptr.fp == cex) {
c = getkey();
c = upperc(c) | (c & ~255); /* Force to upper */
c |= CTLX;
}
}
/* return it */
return(c);
}
/* A more generalized prompt/reply function allowing the caller
to specify the proper terminator. If the terminator is not
a return('\r'), return will echo as "<NL>"
*/
PASCAL NEAR getstring(prompt, buf, nbuf, eolchar)
char *prompt;
char *buf;
int eolchar;
{
register int cpos; /* current character position in string */
register int c; /* current input character */
register int quotef; /* are we quoting the next char? */
cpos = 0;
quotef = FALSE;
/* prompt the user for the input string */
if (discmd)
mlwrite(prompt);
else
movecursor(term.t_nrow, 0);
for (;;) {
/* get a character from the user */
c = getkey();
/* if they hit the line terminate, wrap it up */
if (c == eolchar && quotef == FALSE) {
buf[cpos++] = 0;
/* clear the message line */
mlwrite("");
TTflush();
/* if we default the buffer, return FALSE */
if (buf[0] == 0)
return(FALSE);
return(TRUE);
}
/* change from command form back to character form */
c = ectoc(c);
if (c == ectoc(abortc) && quotef == FALSE) {
/* Abort the input? */
ctrlg(FALSE, 0);
TTflush();
return(ABORT);
} else if ((c==0x7F || c==0x08) && quotef==FALSE) {
/* rubout/erase */
if (cpos != 0) {
outstring("\b \b");
--ttcol;
if (buf[--cpos] < 0x20) {
outstring("\b \b");
--ttcol;
}
if (buf[cpos] == '\r') {
outstring("\b\b \b\b");
ttcol -= 2;
}
TTflush();
}
} else if (c == 0x15 && quotef == FALSE) {
/* C-U, kill */
while (cpos != 0) {
outstring("\b \b");
--ttcol;
if (buf[--cpos] < 0x20) {
outstring("\b \b");
--ttcol;
}
}
TTflush();
} else if (c == quotec && quotef == FALSE) {
quotef = TRUE;
} else {
quotef = FALSE;
if (cpos < nbuf-1) {
buf[cpos++] = c;
if ((c < ' ') && (c != '\r')) {
outstring("^");
++ttcol;
c ^= 0x40;
}
if (c != '\r') {
if (disinp)
mlout(c);
} else { /* put out <NL> for <ret> */
outstring("<NL>");
ttcol += 3;
}
++ttcol;
TTflush();
}
}
}
}
PASCAL NEAR outstring(s) /* output a string of input characters */
char *s; /* string to output */
{
if (disinp)
while (*s)
mlout(*s++);
}
PASCAL NEAR ostring(s) /* output a string of output characters */
char *s; /* string to output */
{
if (discmd)
while (*s)
mlout(*s++);
}