#include "sh.h"
RCSID("$tcsh: ed.xmap.c,v 3.37 2009/06/25 21:15:37 christos Exp $")
#include "ed.h"
#include "ed.defns.h"
#ifndef NULL
#define NULL 0
#endif
typedef struct Xmapnode {
Char ch;
int type;
XmapVal val;
struct Xmapnode *next;
struct Xmapnode *sibling;
} XmapNode;
static XmapNode *Xmap = NULL;
static int TraverseMap (XmapNode *, CStr *, XmapVal *);
static int TryNode (XmapNode *, CStr *, XmapVal *, int);
static XmapNode *GetFreeNode (CStr *);
static void PutFreeNode (XmapNode *);
static int TryDeleteNode (XmapNode **, CStr *);
static int Lookup (struct Strbuf *, const CStr *,
const XmapNode *);
static void Enumerate (struct Strbuf *, const XmapNode *);
static void unparsech (struct Strbuf *, Char);
XmapVal *
XmapCmd(int cmd)
{
static XmapVal xm;
xm.cmd = (KEYCMD) cmd;
return &xm;
}
XmapVal *
XmapStr(CStr *str)
{
static XmapVal xm;
xm.str.len = str->len;
xm.str.buf = str->buf;
return &xm;
}
void
ResetXmap(void)
{
PutFreeNode(Xmap);
Xmap = NULL;
DefaultArrowKeys();
return;
}
int
GetXkey(CStr *ch, XmapVal *val)
{
return (TraverseMap(Xmap, ch, val));
}
static int
TraverseMap(XmapNode *ptr, CStr *ch, XmapVal *val)
{
Char tch;
if (ptr->ch == *(ch->buf)) {
if (ptr->next) {
if (GetNextChar(&tch) != 1) {
val->cmd = F_SEND_EOF;
return XK_CMD;
}
*(ch->buf) = tch;
return (TraverseMap(ptr->next, ch, val));
}
else {
*val = ptr->val;
if (ptr->type != XK_CMD)
*(ch->buf) = '\0';
return ptr->type;
}
}
else {
if (ptr->sibling) {
return (TraverseMap(ptr->sibling, ch, val));
}
else {
val->str.buf = NULL;
val->str.len = 0;
return XK_STR;
}
}
}
void
AddXkey(const CStr *Xkey, XmapVal *val, int ntype)
{
CStr cs;
cs.buf = Xkey->buf;
cs.len = Xkey->len;
if (Xkey->len == 0) {
xprintf("%s", CGETS(9, 1, "AddXkey: Null extended-key not allowed.\n"));
return;
}
if (ntype == XK_CMD && val->cmd == F_XKEY) {
xprintf("%s",
CGETS(9, 2, "AddXkey: sequence-lead-in command not allowed\n"));
return;
}
if (Xmap == NULL)
Xmap = GetFreeNode(&cs);
(void) TryNode(Xmap, &cs, val, ntype);
return;
}
static int
TryNode(XmapNode *ptr, CStr *str, XmapVal *val, int ntype)
{
if (ptr->ch != *(str->buf)) {
XmapNode *xm;
for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
if (xm->sibling->ch == *(str->buf))
break;
if (xm->sibling == NULL)
xm->sibling = GetFreeNode(str);
ptr = xm->sibling;
}
str->buf++;
str->len--;
if (str->len == 0) {
size_t len;
if (ptr->next != NULL) {
PutFreeNode(ptr->next);
ptr->next = NULL;
}
switch (ptr->type) {
case XK_STR:
case XK_EXE:
xfree(ptr->val.str.buf);
ptr->val.str.len = 0;
break;
case XK_NOD:
case XK_CMD:
break;
default:
abort();
break;
}
switch (ptr->type = ntype) {
case XK_CMD:
ptr->val = *val;
break;
case XK_STR:
case XK_EXE:
ptr->val.str.len = val->str.len;
len = (val->str.len + 1) * sizeof(*ptr->val.str.buf);
ptr->val.str.buf = xmalloc(len);
(void) memcpy(ptr->val.str.buf, val->str.buf, len);
break;
default:
abort();
break;
}
}
else {
if (ptr->next == NULL)
ptr->next = GetFreeNode(str);
(void) TryNode(ptr->next, str, val, ntype);
}
return (0);
}
void
ClearXkey(KEYCMD *map, const CStr *in)
{
unsigned char c = (unsigned char) *(in->buf);
if ((map[c] == F_XKEY) &&
((map == CcKeyMap && CcAltMap[c] != F_XKEY) ||
(map == CcAltMap && CcKeyMap[c] != F_XKEY)))
(void) DeleteXkey(in);
}
int
DeleteXkey(const CStr *Xkey)
{
CStr s;
s = *Xkey;
if (s.len == 0) {
xprintf("%s",
CGETS(9, 3, "DeleteXkey: Null extended-key not allowed.\n"));
return (-1);
}
if (Xmap == NULL)
return (0);
(void) TryDeleteNode(&Xmap, &s);
return (0);
}
static int
TryDeleteNode(XmapNode **inptr, CStr *str)
{
XmapNode *ptr;
ptr = *inptr;
if (ptr->ch != *(str->buf)) {
XmapNode *xm;
for (xm = ptr; xm->sibling != NULL; xm = xm->sibling)
if (xm->sibling->ch == *(str->buf))
break;
if (xm->sibling == NULL)
return (0);
inptr = &xm->sibling;
ptr = xm->sibling;
}
str->buf++;
str->len--;
if (str->len == 0) {
*inptr = ptr->sibling;
ptr->sibling = NULL;
PutFreeNode(ptr);
return (1);
}
else if (ptr->next != NULL && TryDeleteNode(&ptr->next, str) == 1) {
if (ptr->next != NULL)
return (0);
*inptr = ptr->sibling;
ptr->sibling = NULL;
PutFreeNode(ptr);
return (1);
}
else {
return (0);
}
}
static void
PutFreeNode(XmapNode *ptr)
{
if (ptr == NULL)
return;
if (ptr->next != NULL) {
PutFreeNode(ptr->next);
ptr->next = NULL;
}
PutFreeNode(ptr->sibling);
switch (ptr->type) {
case XK_CMD:
case XK_NOD:
break;
case XK_EXE:
case XK_STR:
xfree(ptr->val.str.buf);
break;
default:
abort();
break;
}
xfree(ptr);
}
static XmapNode *
GetFreeNode(CStr *ch)
{
XmapNode *ptr;
ptr = xmalloc(sizeof(XmapNode));
ptr->ch = ch->buf[0];
ptr->type = XK_NOD;
ptr->val.str.buf = NULL;
ptr->val.str.len = 0;
ptr->next = NULL;
ptr->sibling = NULL;
return (ptr);
}
void
PrintXkey(const CStr *key)
{
struct Strbuf buf = Strbuf_INIT;
CStr cs;
if (key) {
cs.buf = key->buf;
cs.len = key->len;
}
else {
cs.buf = STRNULL;
cs.len = 0;
}
if (Xmap == NULL && cs.len == 0)
return;
Strbuf_append1(&buf, '"');
cleanup_push(&buf, Strbuf_cleanup);
if (Lookup(&buf, &cs, Xmap) <= -1)
xprintf(CGETS(9, 4, "Unbound extended key \"%S\"\n"), cs.buf);
cleanup_until(&buf);
}
static int
Lookup(struct Strbuf *buf, const CStr *str, const XmapNode *ptr)
{
if (ptr == NULL)
return (-1);
if (str->len == 0) {
Enumerate(buf, ptr);
return (0);
}
else {
if (ptr->ch == *(str->buf)) {
unparsech(buf, ptr->ch);
if (ptr->next != NULL) {
CStr tstr;
tstr.buf = str->buf + 1;
tstr.len = str->len - 1;
return (Lookup(buf, &tstr, ptr->next));
}
else {
if (str->len == 1) {
Strbuf_append1(buf, '"');
Strbuf_terminate(buf);
printOne(buf->s, &ptr->val, ptr->type);
return (0);
}
else
return (-1);
}
}
else {
if (ptr->sibling)
return (Lookup(buf, str, ptr->sibling));
else
return (-1);
}
}
}
static void
Enumerate(struct Strbuf *buf, const XmapNode *ptr)
{
size_t old_len;
if (ptr == NULL) {
#ifdef DEBUG_EDIT
xprintf(CGETS(9, 6, "Enumerate: BUG!! Null ptr passed\n!"));
#endif
return;
}
old_len = buf->len;
unparsech(buf, ptr->ch);
if (ptr->next == NULL) {
Strbuf_append1(buf, '"');
Strbuf_terminate(buf);
printOne(buf->s, &ptr->val, ptr->type);
}
else
Enumerate(buf, ptr->next);
if (ptr->sibling) {
buf->len = old_len;
Enumerate(buf, ptr->sibling);
}
}
void
printOne(const Char *key, const XmapVal *val, int ntype)
{
struct KeyFuncs *fp;
static const char *fmt = "%s\n";
xprintf("%-15S-> ", key);
if (val != NULL)
switch (ntype) {
case XK_STR:
case XK_EXE: {
unsigned char *p;
p = unparsestring(&val->str, ntype == XK_STR ? STRQQ : STRBB);
cleanup_push(p, xfree);
xprintf(fmt, p);
cleanup_until(p);
break;
}
case XK_CMD:
for (fp = FuncNames; fp->name; fp++)
if (val->cmd == fp->func)
xprintf(fmt, fp->name);
break;
default:
abort();
break;
}
else
xprintf(fmt, CGETS(9, 7, "no input"));
}
static void
unparsech(struct Strbuf *buf, Char ch)
{
if (ch == 0) {
Strbuf_append1(buf, '^');
Strbuf_append1(buf, '@');
}
else if (Iscntrl(ch)) {
Strbuf_append1(buf, '^');
if (ch == CTL_ESC('\177'))
Strbuf_append1(buf, '?');
else
#ifdef IS_ASCII
Strbuf_append1(buf, ch | 0100);
#else
Strbuf_append1(buf, _toebcdic[_toascii[ch]|0100]);
#endif
}
else if (ch == '^') {
Strbuf_append1(buf, '\\');
Strbuf_append1(buf, '^');
} else if (ch == '\\') {
Strbuf_append1(buf, '\\');
Strbuf_append1(buf, '\\');
} else if (ch == ' ' || (Isprint(ch) && !Isspace(ch))) {
Strbuf_append1(buf, ch);
}
else {
Strbuf_append1(buf, '\\');
Strbuf_append1(buf, ((ch >> 6) & 7) + '0');
Strbuf_append1(buf, ((ch >> 3) & 7) + '0');
Strbuf_append1(buf, (ch & 7) + '0');
}
}
eChar
parseescape(const Char **ptr)
{
const Char *p;
Char c;
p = *ptr;
if ((p[1] & CHAR) == 0) {
xprintf(CGETS(9, 8, "Something must follow: %c\n"), (char)*p);
return CHAR_ERR;
}
if ((*p & CHAR) == '\\') {
p++;
switch (*p & CHAR) {
case 'a':
c = CTL_ESC('\007');
break;
case 'b':
c = CTL_ESC('\010');
break;
case 'e':
c = CTL_ESC('\033');
break;
case 'f':
c = CTL_ESC('\014');
break;
case 'n':
c = CTL_ESC('\012');
break;
case 'r':
c = CTL_ESC('\015');
break;
case 't':
c = CTL_ESC('\011');
break;
case 'v':
c = CTL_ESC('\013');
break;
case '\\':
c = '\\';
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
{
int cnt, val;
Char ch;
for (cnt = 0, val = 0; cnt < 3; cnt++) {
ch = *p++ & CHAR;
if (ch < '0' || ch > '7') {
p--;
break;
}
val = (val << 3) | (ch - '0');
}
if ((val & ~0xff) != 0) {
xprintf("%s", CGETS(9, 9,
"Octal constant does not fit in a char.\n"));
return 0;
}
#ifndef IS_ASCII
if (CTL_ESC(val) != val && adrof(STRwarnebcdic))
xprintf(
"Warning: Octal constant \\%3.3o is interpreted as EBCDIC value.\n", val);
#endif
c = (Char) val;
--p;
}
break;
default:
c = *p;
break;
}
}
else if ((*p & CHAR) == '^' && (Isalpha(p[1] & CHAR) ||
strchr("@^_?\\|[{]}", p[1] & CHAR))) {
p++;
#ifdef IS_ASCII
c = ((*p & CHAR) == '?') ? CTL_ESC('\177') : ((*p & CHAR) & 0237);
#else
c = ((*p & CHAR) == '?') ? CTL_ESC('\177') : _toebcdic[_toascii[*p & CHAR] & 0237];
if (adrof(STRwarnebcdic))
xprintf(
"Warning: Control character ^%c may be interpreted differently in EBCDIC.\n", *p & CHAR );
#endif
}
else
c = *p;
*ptr = p;
return (c);
}
unsigned char *
unparsestring(const CStr *str, const Char *sep)
{
unsigned char *buf, *b;
Char p;
int l;
buf = xmalloc((str->len + 1) * max(4, MB_LEN_MAX));
b = buf;
if (sep[0])
#ifndef WINNT_NATIVE
*b++ = sep[0];
#else
*b++ = CHAR & sep[0];
#endif
for (l = 0; l < str->len; l++) {
p = str->buf[l];
if (Iscntrl(p)) {
*b++ = '^';
if (p == CTL_ESC('\177'))
*b++ = '?';
else
#ifdef IS_ASCII
*b++ = (unsigned char) (p | 0100);
#else
*b++ = _toebcdic[_toascii[p]|0100];
#endif
}
else if (p == '^' || p == '\\') {
*b++ = '\\';
*b++ = (unsigned char) p;
}
else if (p == ' ' || (Isprint(p) && !Isspace(p)))
b += one_wctomb((char *)b, p & CHAR);
else {
*b++ = '\\';
*b++ = ((p >> 6) & 7) + '0';
*b++ = ((p >> 3) & 7) + '0';
*b++ = (p & 7) + '0';
}
}
if (sep[0] && sep[1])
#ifndef WINNT_NATIVE
*b++ = sep[1];
#else
*b++ = CHAR & sep[1];
#endif
*b++ = 0;
return buf;
}