freebsd-skq/contrib/tcsh/tc.bind.c

537 lines
12 KiB
C
Raw Normal View History

2009-07-10 21:47:20 +00:00
/* $Header: /p/tcsh/cvsroot/tcsh/tc.bind.c,v 3.45 2009/06/25 21:15:37 christos Exp $ */
/*
* tc.bind.c: Key binding functions
*/
/*-
* Copyright (c) 1980, 1991 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
2002-07-24 16:23:10 +00:00
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "sh.h"
2009-07-10 21:47:20 +00:00
RCSID("$tcsh: tc.bind.c,v 3.45 2009/06/25 21:15:37 christos Exp $")
#include "ed.h"
#include "ed.defns.h"
2007-03-11 22:33:41 +00:00
static void printkey (const KEYCMD *, CStr *);
static KEYCMD parsecmd (Char *);
static void bad_spec (const Char *);
static CStr *parsestring (const Char *, CStr *);
static CStr *parsebind (const Char *, CStr *);
static void print_all_keys (void);
static void printkeys (KEYCMD *, int, int);
static void bindkey_usage (void);
static void list_functions (void);
extern int MapsAreInited;
/*ARGSUSED*/
void
2007-03-11 22:33:41 +00:00
dobindkey(Char **v, struct command *c)
{
KEYCMD *map;
2005-04-24 19:41:08 +00:00
int ntype, no, removeb, key, bindk;
Char *par;
Char p;
KEYCMD cmd;
CStr in;
CStr out;
uChar ch;
USE(c);
if (!MapsAreInited)
ed_InitMaps();
map = CcKeyMap;
ntype = XK_CMD;
2005-04-24 19:41:08 +00:00
key = removeb = bindk = 0;
for (no = 1, par = v[no];
par != NULL && (*par++ & CHAR) == '-'; no++, par = v[no]) {
if ((p = (*par & CHAR)) == '-') {
no++;
break;
}
else
switch (p) {
case 'b':
2005-04-24 19:41:08 +00:00
bindk = 1;
break;
case 'k':
key = 1;
break;
case 'a':
map = CcAltMap;
break;
case 's':
ntype = XK_STR;
break;
case 'c':
ntype = XK_EXE;
break;
case 'r':
2005-04-24 19:41:08 +00:00
removeb = 1;
break;
case 'v':
ed_InitVIMaps();
return;
case 'e':
ed_InitEmacsMaps();
return;
case 'd':
#ifdef VIDEFAULT
ed_InitVIMaps();
#else /* EMACSDEFAULT */
ed_InitEmacsMaps();
#endif /* VIDEFAULT */
return;
case 'l':
list_functions();
return;
default:
bindkey_usage();
return;
}
}
if (!v[no]) {
print_all_keys();
return;
}
if (key) {
if (!IsArrowKey(v[no]))
xprintf(CGETS(20, 1, "Invalid key name `%S'\n"), v[no]);
2007-03-11 22:33:41 +00:00
in.buf = Strsave(v[no++]);
in.len = Strlen(in.buf);
}
else {
2005-04-24 19:41:08 +00:00
if (bindk) {
if (parsebind(v[no++], &in) == NULL)
return;
}
else {
if (parsestring(v[no++], &in) == NULL)
return;
}
}
2007-03-11 22:33:41 +00:00
cleanup_push(in.buf, xfree);
2005-04-24 19:41:08 +00:00
#ifndef WINNT_NATIVE
if (in.buf[0] > 0xFF) {
bad_spec(in.buf);
2007-03-11 22:33:41 +00:00
cleanup_until(in.buf);
2005-04-24 19:41:08 +00:00
return;
}
#endif
ch = (uChar) in.buf[0];
2005-04-24 19:41:08 +00:00
if (removeb) {
2007-03-11 22:33:41 +00:00
if (key)
(void) ClearArrowKeys(&in);
2007-03-11 22:33:41 +00:00
else if (in.len > 1) {
(void) DeleteXkey(&in);
}
else if (map[ch] == F_XKEY) {
(void) DeleteXkey(&in);
map[ch] = F_UNASSIGNED;
}
else {
map[ch] = F_UNASSIGNED;
}
2007-03-11 22:33:41 +00:00
cleanup_until(in.buf);
return;
}
if (!v[no]) {
if (key)
PrintArrowKeys(&in);
else
printkey(map, &in);
2007-03-11 22:33:41 +00:00
cleanup_until(in.buf);
return;
}
if (v[no + 1]) {
bindkey_usage();
2007-03-11 22:33:41 +00:00
cleanup_until(in.buf);
return;
}
switch (ntype) {
case XK_STR:
case XK_EXE:
2007-03-11 22:33:41 +00:00
if (parsestring(v[no], &out) == NULL) {
cleanup_until(in.buf);
return;
2007-03-11 22:33:41 +00:00
}
cleanup_push(out.buf, xfree);
if (key) {
if (SetArrowKeys(&in, XmapStr(&out), ntype) == -1)
2005-04-24 19:41:08 +00:00
xprintf(CGETS(20, 2, "Bad key name: %S\n"), in.buf);
2007-03-11 22:33:41 +00:00
else
cleanup_ignore(out.buf);
}
else
AddXkey(&in, XmapStr(&out), ntype);
map[ch] = F_XKEY;
break;
case XK_CMD:
2007-03-11 22:33:41 +00:00
if ((cmd = parsecmd(v[no])) == 0) {
cleanup_until(in.buf);
return;
2007-03-11 22:33:41 +00:00
}
if (key)
(void) SetArrowKeys(&in, XmapCmd((int) cmd), ntype);
else {
if (in.len > 1) {
AddXkey(&in, XmapCmd((int) cmd), ntype);
map[ch] = F_XKEY;
}
else {
ClearXkey(map, &in);
map[ch] = cmd;
}
}
break;
default:
abort();
break;
}
2007-03-11 22:33:41 +00:00
cleanup_until(in.buf);
if (key)
BindArrowKeys();
}
static void
2007-03-11 22:33:41 +00:00
printkey(const KEYCMD *map, CStr *in)
{
2005-04-24 19:41:08 +00:00
struct KeyFuncs *fp;
if (in->len < 2) {
2007-03-11 22:33:41 +00:00
unsigned char *unparsed;
unparsed = unparsestring(in, STRQQ);
cleanup_push(unparsed, xfree);
for (fp = FuncNames; fp->name; fp++) {
if (fp->func == map[(uChar) *(in->buf)]) {
2007-03-11 22:33:41 +00:00
xprintf("%s\t->\t%s\n", unparsed, fp->name);
}
}
2007-03-11 22:33:41 +00:00
cleanup_until(unparsed);
}
2007-03-11 22:33:41 +00:00
else
PrintXkey(in);
}
static KEYCMD
2007-03-11 22:33:41 +00:00
parsecmd(Char *str)
{
2005-04-24 19:41:08 +00:00
struct KeyFuncs *fp;
for (fp = FuncNames; fp->name; fp++) {
if (strcmp(short2str(str), fp->name) == 0) {
return (KEYCMD) fp->func;
}
}
xprintf(CGETS(20, 3, "Bad command name: %S\n"), str);
return 0;
}
static void
2007-03-11 22:33:41 +00:00
bad_spec(const Char *str)
{
xprintf(CGETS(20, 4, "Bad key spec %S\n"), str);
}
static CStr *
2007-03-11 22:33:41 +00:00
parsebind(const Char *s, CStr *str)
{
2007-03-11 22:33:41 +00:00
struct Strbuf b = Strbuf_INIT;
2007-03-11 22:33:41 +00:00
cleanup_push(&b, Strbuf_cleanup);
if (Iscntrl(*s)) {
2007-03-11 22:33:41 +00:00
Strbuf_append1(&b, *s);
goto end;
}
switch (*s) {
case '^':
s++;
#ifdef IS_ASCII
2007-03-11 22:33:41 +00:00
Strbuf_append1(&b, (*s == '?') ? '\177' : ((*s & CHAR) & 0237));
#else
2007-03-11 22:33:41 +00:00
Strbuf_append1(&b, (*s == '?') ? CTL_ESC('\177')
: _toebcdic[_toascii[*s & CHAR] & 0237]);
#endif
break;
case 'F':
case 'M':
case 'X':
case 'C':
#ifdef WINNT_NATIVE
case 'N':
#endif /* WINNT_NATIVE */
2007-03-11 22:33:41 +00:00
if (s[1] != '-' || s[2] == '\0')
goto bad_spec;
s += 2;
switch (s[-2]) {
case 'F': case 'f': /* Turn into ^[str */
2007-03-11 22:33:41 +00:00
Strbuf_append1(&b, CTL_ESC('\033'));
Strbuf_append(&b, s);
break;
case 'C': case 'c': /* Turn into ^c */
#ifdef IS_ASCII
2007-03-11 22:33:41 +00:00
Strbuf_append1(&b, (*s == '?') ? '\177' : ((*s & CHAR) & 0237));
#else
2007-03-11 22:33:41 +00:00
Strbuf_append1(&b, (*s == '?') ? CTL_ESC('\177')
: _toebcdic[_toascii[*s & CHAR] & 0237]);
#endif
break;
case 'X' : case 'x': /* Turn into ^Xc */
#ifdef IS_ASCII
2007-03-11 22:33:41 +00:00
Strbuf_append1(&b, 'X' & 0237);
#else
2007-03-11 22:33:41 +00:00
Strbuf_append1(&b, _toebcdic[_toascii['X'] & 0237]);
#endif
2007-03-11 22:33:41 +00:00
Strbuf_append1(&b, *s);
break;
case 'M' : case 'm': /* Turn into 0x80|c */
if (!NoNLSRebind) {
2007-03-11 22:33:41 +00:00
Strbuf_append1(&b, CTL_ESC('\033'));
Strbuf_append1(&b, *s);
} else {
#ifdef IS_ASCII
2007-03-11 22:33:41 +00:00
Strbuf_append1(&b, *s | 0x80);
#else
2007-03-11 22:33:41 +00:00
Strbuf_append1(&b, _toebcdic[_toascii[*s] | 0x80]);
#endif
}
break;
#ifdef WINNT_NATIVE
case 'N' : case 'n': /* NT */
{
Char bnt;
bnt = nt_translate_bindkey(s);
if (bnt != 0)
2007-03-11 22:33:41 +00:00
Strbuf_append1(&b, bnt);
else
bad_spec(s);
}
break;
#endif /* WINNT_NATIVE */
default:
abort();
}
break;
default:
2007-03-11 22:33:41 +00:00
goto bad_spec;
}
2007-03-11 22:33:41 +00:00
end:
cleanup_ignore(&b);
cleanup_until(&b);
Strbuf_terminate(&b);
str->buf = xrealloc(b.s, (b.len + 1) * sizeof (*str->buf));
str->len = b.len;
return str;
2007-03-11 22:33:41 +00:00
bad_spec:
bad_spec(s);
cleanup_until(&b);
return NULL;
}
static CStr *
2007-03-11 22:33:41 +00:00
parsestring(const Char *str, CStr *buf)
{
2007-03-11 22:33:41 +00:00
struct Strbuf b = Strbuf_INIT;
const Char *p;
2005-04-24 19:41:08 +00:00
eChar es;
if (*str == 0) {
2009-07-10 21:47:20 +00:00
xprintf("%s", CGETS(20, 5, "Null string specification\n"));
return NULL;
}
2007-03-11 22:33:41 +00:00
cleanup_push(&b, Strbuf_cleanup);
for (p = str; *p != 0; p++) {
if ((*p & CHAR) == '\\' || (*p & CHAR) == '^') {
2007-03-11 22:33:41 +00:00
if ((es = parseescape(&p)) == CHAR_ERR) {
cleanup_until(&b);
return 0;
2007-03-11 22:33:41 +00:00
} else
Strbuf_append1(&b, es);
}
else
2007-03-11 22:33:41 +00:00
Strbuf_append1(&b, *p & CHAR);
}
2007-03-11 22:33:41 +00:00
cleanup_ignore(&b);
cleanup_until(&b);
Strbuf_terminate(&b);
buf->buf = xrealloc(b.s, (b.len + 1) * sizeof (*buf->buf));
buf->len = b.len;
return buf;
}
static void
2007-03-11 22:33:41 +00:00
print_all_keys(void)
{
int prev, i;
CStr nilstr;
nilstr.buf = NULL;
nilstr.len = 0;
2009-07-10 21:47:20 +00:00
xprintf("%s", CGETS(20, 6, "Standard key bindings\n"));
prev = 0;
for (i = 0; i < 256; i++) {
if (CcKeyMap[prev] == CcKeyMap[i])
continue;
printkeys(CcKeyMap, prev, i - 1);
prev = i;
}
printkeys(CcKeyMap, prev, i - 1);
2009-07-10 21:47:20 +00:00
xprintf("%s", CGETS(20, 7, "Alternative key bindings\n"));
prev = 0;
for (i = 0; i < 256; i++) {
if (CcAltMap[prev] == CcAltMap[i])
continue;
printkeys(CcAltMap, prev, i - 1);
prev = i;
}
printkeys(CcAltMap, prev, i - 1);
2009-07-10 21:47:20 +00:00
xprintf("%s", CGETS(20, 8, "Multi-character bindings\n"));
PrintXkey(NULL); /* print all Xkey bindings */
2009-07-10 21:47:20 +00:00
xprintf("%s", CGETS(20, 9, "Arrow key bindings\n"));
PrintArrowKeys(&nilstr);
}
static void
2007-03-11 22:33:41 +00:00
printkeys(KEYCMD *map, int first, int last)
{
2005-04-24 19:41:08 +00:00
struct KeyFuncs *fp;
Char firstbuf[2], lastbuf[2];
CStr fb, lb;
2007-03-11 22:33:41 +00:00
unsigned char *unparsed;
fb.buf = firstbuf;
lb.buf = lastbuf;
firstbuf[0] = (Char) first;
firstbuf[1] = 0;
lastbuf[0] = (Char) last;
lastbuf[1] = 0;
fb.len = 1;
lb.len = 1;
2007-03-11 22:33:41 +00:00
unparsed = unparsestring(&fb, STRQQ);
cleanup_push(unparsed, xfree);
if (map[first] == F_UNASSIGNED) {
if (first == last)
2007-03-11 22:33:41 +00:00
xprintf(CGETS(20, 10, "%-15s-> is undefined\n"), unparsed);
cleanup_until(unparsed);
return;
}
for (fp = FuncNames; fp->name; fp++) {
if (fp->func == map[first]) {
2007-03-11 22:33:41 +00:00
if (first == last)
xprintf("%-15s-> %s\n", unparsed, fp->name);
else {
2007-03-11 22:33:41 +00:00
unsigned char *p;
p = unparsestring(&lb, STRQQ);
cleanup_push(p, xfree);
xprintf("%-4s to %-7s-> %s\n", unparsed, p, fp->name);
}
2007-03-11 22:33:41 +00:00
cleanup_until(unparsed);
return;
}
}
2007-03-11 22:33:41 +00:00
xprintf(CGETS(20, 11, "BUG!!! %s isn't bound to anything.\n"), unparsed);
if (map == CcKeyMap)
xprintf("CcKeyMap[%d] == %d\n", first, CcKeyMap[first]);
2007-03-11 22:33:41 +00:00
else
xprintf("CcAltMap[%d] == %d\n", first, CcAltMap[first]);
2007-03-11 22:33:41 +00:00
cleanup_until(unparsed);
}
static void
2007-03-11 22:33:41 +00:00
bindkey_usage(void)
{
2009-07-10 21:47:20 +00:00
xprintf("%s", CGETS(20, 12,
"Usage: bindkey [options] [--] [KEY [COMMAND]]\n"));
2009-07-10 21:47:20 +00:00
xprintf("%s", CGETS(20, 13,
" -a list or bind KEY in alternative key map\n"));
2009-07-10 21:47:20 +00:00
xprintf("%s", CGETS(20, 14,
" -b interpret KEY as a C-, M-, F- or X- key name\n"));
2009-07-10 21:47:20 +00:00
xprintf("%s", CGETS(20, 15,
" -s interpret COMMAND as a literal string to be output\n"));
2009-07-10 21:47:20 +00:00
xprintf("%s", CGETS(20, 16,
" -c interpret COMMAND as a builtin or external command\n"));
2009-07-10 21:47:20 +00:00
xprintf("%s", CGETS(20, 17,
" -v bind all keys to vi bindings\n"));
2009-07-10 21:47:20 +00:00
xprintf("%s", CGETS(20, 18,
" -e bind all keys to emacs bindings\n"));
2009-07-10 21:47:20 +00:00
xprintf("%s", CGETS(20, 19,
" -d bind all keys to default editor's bindings\n"));
2009-07-10 21:47:20 +00:00
xprintf("%s", CGETS(20, 20,
" -l list editor commands with descriptions\n"));
2009-07-10 21:47:20 +00:00
xprintf("%s", CGETS(20, 21,
" -r remove KEY's binding\n"));
2009-07-10 21:47:20 +00:00
xprintf("%s", CGETS(20, 22,
" -k interpret KEY as a symbolic arrow-key name\n"));
2009-07-10 21:47:20 +00:00
xprintf("%s", CGETS(20, 23,
" -- force a break from option processing\n"));
2009-07-10 21:47:20 +00:00
xprintf("%s", CGETS(20, 24,
" -u (or any invalid option) this message\n"));
xprintf("\n");
2009-07-10 21:47:20 +00:00
xprintf("%s", CGETS(20, 25,
"Without KEY or COMMAND, prints all bindings\n"));
2009-07-10 21:47:20 +00:00
xprintf("%s", CGETS(20, 26,
"Without COMMAND, prints the binding for KEY.\n"));
}
static void
2007-03-11 22:33:41 +00:00
list_functions(void)
{
2005-04-24 19:41:08 +00:00
struct KeyFuncs *fp;
for (fp = FuncNames; fp->name; fp++) {
xprintf("%s\n %s\n", fp->name, fp->desc);
}
}