xtermcap.c revision 2e4f8982
1/* $XTermId: xtermcap.c,v 1.49 2016/05/22 18:31:20 tom Exp $ */ 2 3/* 4 * Copyright 2007-2014,2016 by Thomas E. Dickey 5 * 6 * All Rights Reserved 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining a 9 * copy of this software and associated documentation files (the 10 * "Software"), to deal in the Software without restriction, including 11 * without limitation the rights to use, copy, modify, merge, publish, 12 * distribute, sublicense, and/or sell copies of the Software, and to 13 * permit persons to whom the Software is furnished to do so, subject to 14 * the following conditions: 15 * 16 * The above copyright notice and this permission notice shall be included 17 * in all copies or substantial portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 22 * IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY 23 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 * 27 * Except as contained in this notice, the name(s) of the above copyright 28 * holders shall not be used in advertising or otherwise to promote the 29 * sale, use or other dealings in this Software without prior written 30 * authorization. 31 */ 32 33#include <xtermcap.h> 34#include <data.h> 35 36#include <X11/keysym.h> 37#include <ctype.h> 38 39#ifdef VMS 40#include <X11/keysymdef.h> 41#endif 42 43#include <xstrings.h> 44 45#if USE_TERMINFO && defined(NCURSES_VERSION) && defined(HAVE_USE_EXTENDED_NAMES) 46#define USE_EXTENDED_NAMES 1 47#else 48#define USE_EXTENDED_NAMES 0 49#endif 50 51#if USE_TERMINFO 52#define TcapInit(buffer, name) (setupterm(name, fileno(stdout), &ignored) == OK) 53#else 54#define TcapInit(buffer, name) (tgetent(buffer, name) == 1) 55#endif 56 57#define NO_STRING (char *)(-1) 58 59#if OPT_TCAP_QUERY || OPT_TCAP_FKEYS 60 61#define SHIFT (MOD_NONE + MOD_SHIFT) 62 63typedef struct { 64 const char *tc; 65 const char *ti; 66 int code; 67 unsigned param; /* see xtermStateToParam() */ 68} TCAPINFO; 69/* *INDENT-OFF* */ 70#define DATA(tc,ti,x,y) { tc, ti, x, y } 71static const TCAPINFO table[] = { 72 /* tcap terminfo code state */ 73 DATA( "%1", "khlp", XK_Help, 0 ), 74 DATA( "#1", "kHLP", XK_Help, SHIFT ), 75 DATA( "@0", "kfnd", XK_Find, 0 ), 76 DATA( "*0", "kFND", XK_Find, SHIFT ), 77 DATA( "*6", "kslt", XK_Select, 0 ), 78 DATA( "#6", "kSLT", XK_Select, SHIFT ), 79 80 DATA( "kh", "khome", XK_Home, 0 ), 81 DATA( "#2", "kHOM", XK_Home, SHIFT ), 82 DATA( "@7", "kend", XK_End, 0 ), 83 DATA( "*7", "kEND", XK_End, SHIFT ), 84 85 DATA( "kl", "kcub1", XK_Left, 0 ), 86 DATA( "kr", "kcuf1", XK_Right, 0 ), 87 DATA( "ku", "kcuu1", XK_Up, 0 ), 88 DATA( "kd", "kcud1", XK_Down, 0 ), 89 90 DATA( "#4", "kLFT", XK_Left, SHIFT ), 91 DATA( "%i", "kRIT", XK_Right, SHIFT ), 92 DATA( "kF", "kind", XK_Down, SHIFT ), 93 DATA( "kR", "kri", XK_Up, SHIFT ), 94 95 DATA( "k1", "kf1", XK_Fn(1), 0 ), 96 DATA( "k2", "kf2", XK_Fn(2), 0 ), 97 DATA( "k3", "kf3", XK_Fn(3), 0 ), 98 DATA( "k4", "kf4", XK_Fn(4), 0 ), 99 DATA( "k5", "kf5", XK_Fn(5), 0 ), 100 DATA( "k6", "kf6", XK_Fn(6), 0 ), 101 DATA( "k7", "kf7", XK_Fn(7), 0 ), 102 DATA( "k8", "kf8", XK_Fn(8), 0 ), 103 DATA( "k9", "kf9", XK_Fn(9), 0 ), 104 DATA( "k;", "kf10", XK_Fn(10), 0 ), 105 106 DATA( "F1", "kf11", XK_Fn(11), 0 ), 107 DATA( "F2", "kf12", XK_Fn(12), 0 ), 108 DATA( "F3", "kf13", XK_Fn(13), 0 ), 109 DATA( "F4", "kf14", XK_Fn(14), 0 ), 110 DATA( "F5", "kf15", XK_Fn(15), 0 ), 111 DATA( "F6", "kf16", XK_Fn(16), 0 ), 112 DATA( "F7", "kf17", XK_Fn(17), 0 ), 113 DATA( "F8", "kf18", XK_Fn(18), 0 ), 114 DATA( "F9", "kf19", XK_Fn(19), 0 ), 115 DATA( "FA", "kf20", XK_Fn(20), 0 ), 116 DATA( "FB", "kf21", XK_Fn(21), 0 ), 117 DATA( "FC", "kf22", XK_Fn(22), 0 ), 118 DATA( "FD", "kf23", XK_Fn(23), 0 ), 119 DATA( "FE", "kf24", XK_Fn(24), 0 ), 120 DATA( "FF", "kf25", XK_Fn(25), 0 ), 121 DATA( "FG", "kf26", XK_Fn(26), 0 ), 122 DATA( "FH", "kf27", XK_Fn(27), 0 ), 123 DATA( "FI", "kf28", XK_Fn(28), 0 ), 124 DATA( "FJ", "kf29", XK_Fn(29), 0 ), 125 DATA( "FK", "kf30", XK_Fn(30), 0 ), 126 DATA( "FL", "kf31", XK_Fn(31), 0 ), 127 DATA( "FM", "kf32", XK_Fn(32), 0 ), 128 DATA( "FN", "kf33", XK_Fn(33), 0 ), 129 DATA( "FO", "kf34", XK_Fn(34), 0 ), 130 DATA( "FP", "kf35", XK_Fn(35), 0 ), 131 132 DATA( "FQ", "kf36", -36, 0 ), 133 DATA( "FR", "kf37", -37, 0 ), 134 DATA( "FS", "kf38", -38, 0 ), 135 DATA( "FT", "kf39", -39, 0 ), 136 DATA( "FU", "kf40", -40, 0 ), 137 DATA( "FV", "kf41", -41, 0 ), 138 DATA( "FW", "kf42", -42, 0 ), 139 DATA( "FX", "kf43", -43, 0 ), 140 DATA( "FY", "kf44", -44, 0 ), 141 DATA( "FZ", "kf45", -45, 0 ), 142 DATA( "Fa", "kf46", -46, 0 ), 143 DATA( "Fb", "kf47", -47, 0 ), 144 DATA( "Fc", "kf48", -48, 0 ), 145 DATA( "Fd", "kf49", -49, 0 ), 146 DATA( "Fe", "kf50", -50, 0 ), 147 DATA( "Ff", "kf51", -51, 0 ), 148 DATA( "Fg", "kf52", -52, 0 ), 149 DATA( "Fh", "kf53", -53, 0 ), 150 DATA( "Fi", "kf54", -54, 0 ), 151 DATA( "Fj", "kf55", -55, 0 ), 152 DATA( "Fk", "kf56", -56, 0 ), 153 DATA( "Fl", "kf57", -57, 0 ), 154 DATA( "Fm", "kf58", -58, 0 ), 155 DATA( "Fn", "kf59", -59, 0 ), 156 DATA( "Fo", "kf60", -60, 0 ), 157 DATA( "Fp", "kf61", -61, 0 ), 158 DATA( "Fq", "kf62", -62, 0 ), 159 DATA( "Fr", "kf63", -63, 0 ), 160 161 DATA( "K1", "ka1", XK_KP_Home, 0 ), 162 DATA( "K4", "kc1", XK_KP_End, 0 ), 163 DATA( "K3", "ka3", XK_KP_Prior, 0 ), 164 DATA( "K5", "kc3", XK_KP_Next, 0 ), 165 166#ifdef XK_ISO_Left_Tab 167 DATA( "kB", "kcbt", XK_ISO_Left_Tab, 0 ), 168#endif 169 DATA( "kC", "kclr", XK_Clear, 0 ), 170 DATA( "kD", "kdch1", XK_Delete, 0 ), 171 DATA( "kI", "kich1", XK_Insert, 0 ), 172 173 DATA( "kN", "knp", XK_Next, 0 ), 174 DATA( "kP", "kpp", XK_Prior, 0 ), 175 DATA( "%c", "kNXT", XK_Next, SHIFT ), 176 DATA( "%e", "kPRV", XK_Prior, SHIFT ), 177 178 DATA( "&8", "kund", XK_Undo, 0 ), 179 DATA( "kb", "kbs", XK_BackSpace, 0 ), 180# if OPT_TCAP_QUERY && OPT_ISO_COLORS 181 /* XK_COLORS is a fake code. */ 182 DATA( "Co", "colors", XK_COLORS, 0 ), 183# endif 184 DATA( "TN", "name", XK_TCAPNAME, 0 ), 185#if USE_EXTENDED_NAMES 186#define DEXT(name, parm, code) DATA("", name, code, parm) 187#define D1ST(name, parm, code) DEXT("k" #name, parm, code) 188#define DMOD(name, parm, code) DEXT("k" #name #parm, parm, code) 189 190#define DGRP(name, code) \ 191 D1ST(name, 2, code), \ 192 DMOD(name, 3, code), \ 193 DMOD(name, 4, code), \ 194 DMOD(name, 5, code), \ 195 DMOD(name, 6, code), \ 196 DMOD(name, 7, code), \ 197 DMOD(name, 8, code) 198 199 /* the terminfo codes here are ncurses extensions */ 200 /* ignore the termcap names, which are empty */ 201 DATA( "", "kUP", XK_Up, SHIFT ), 202 DATA( "", "kDN", XK_Up, SHIFT ), 203 204 DGRP(DN, XK_Down), 205 DGRP(LFT, XK_Left), 206 DGRP(RIT, XK_Right), 207 DGRP(UP, XK_Up), 208 DGRP(DC, XK_Delete), 209 DGRP(END, XK_End), 210 DGRP(HOM, XK_Home), 211 DGRP(IC, XK_Insert), 212 DGRP(NXT, XK_Next), 213 DGRP(PRV, XK_Prior), 214#endif 215}; 216#undef DATA 217/* *INDENT-ON* */ 218 219#if OPT_TCAP_FKEYS 220static Boolean 221loadTermcapStrings(TScreen *screen) 222{ 223 Boolean result = True; 224 225 if (screen->tcap_fkeys == 0) { 226 Cardinal want = XtNumber(table); 227 Cardinal have; 228#ifdef USE_TERMCAP 229 char *area = screen->tcap_area; 230#endif 231 232 TRACE(("loadTermcapStrings\n")); 233 if ((screen->tcap_fkeys = TypeCallocN(char *, want)) != 0) { 234 235 for (have = 0; have < want; ++have) { 236 char name[80]; 237 char *fkey; 238 239#ifndef USE_TERMCAP 240 fkey = tigetstr(strcpy(name, table[have].ti)); 241#else 242 fkey = tgetstr(strcpy(name, table[have].tc), &area); 243#endif 244 if (fkey != 0 && fkey != NO_STRING) { 245 screen->tcap_fkeys[have] = x_strdup(fkey); 246 } else { 247 screen->tcap_fkeys[have] = NO_STRING; 248 } 249 } 250 } else { 251 result = False; 252 } 253 } 254 return result; 255} 256#endif 257 258#if OPT_TCAP_QUERY 259static Boolean 260keyIsDistinct(XtermWidget xw, int which) 261{ 262 Boolean result = True; 263 264 switch (xw->keyboard.type) { 265 case keyboardIsTermcap: 266#if OPT_TCAP_FKEYS 267 if (table[which].param == SHIFT) { 268 TScreen *screen = TScreenOf(xw); 269 Cardinal k; 270 271 if (loadTermcapStrings(screen) 272 && screen->tcap_fkeys[which] != NO_STRING) { 273 274 for (k = 0; k < XtNumber(table); k++) { 275 276 if (table[k].code == table[which].code 277 && table[k].param == 0) { 278 char *fkey; 279 280 if ((fkey = screen->tcap_fkeys[k]) != NO_STRING 281 && !strcmp(fkey, screen->tcap_fkeys[which])) { 282 TRACE(("shifted/unshifted keys do not differ\n")); 283 result = False; 284 } 285 break; 286 } 287 } 288 } else { 289 /* there is no data for the shifted key */ 290 result = -1; 291 } 292 } 293#endif 294 break; 295 /* 296 * The vt220-keyboard will not return distinct key sequences for 297 * shifted cursor-keys. Just pretend they do not exist, since some 298 * programs may be confused if we return the same data for 299 * shifted/unshifted keys. 300 */ 301 case keyboardIsVT220: 302 if (table[which].param == SHIFT) { 303 TRACE(("shifted/unshifted keys do not differ\n")); 304 result = False; 305 } 306 break; 307 case keyboardIsLegacy: 308 case keyboardIsDefault: 309 case keyboardIsHP: 310 case keyboardIsSCO: 311 case keyboardIsSun: 312 break; 313 } 314 315 return result; 316} 317 318static int 319lookupTcapByName(const char *name) 320{ 321 int result = -2; 322 Cardinal j; 323 324 if (!IsEmpty(name)) { 325 for (j = 0; j < XtNumber(table); j++) { 326 if (!strcmp(table[j].ti, name) || !strcmp(table[j].tc, name)) { 327 result = (int) j; 328 break; 329 } 330 } 331 } 332 333 if (result >= 0) { 334 TRACE(("lookupTcapByName(%s) tc=%s, ti=%s code %#x, param %#x\n", 335 name, 336 table[result].tc, 337 table[result].ti, 338 table[result].code, 339 table[result].param)); 340 } else { 341 TRACE(("lookupTcapByName(%s) FAIL\n", name)); 342 } 343 return result; 344} 345 346/* 347 * Parse the termcap/terminfo name from the string, returning a positive number 348 * (the keysym) if found, otherwise -1. Update the string pointer. 349 * Returns the (shift, control) state in *state. 350 * 351 * This does not attempt to construct control/shift modifiers to construct 352 * function-key values. Instead, it sets the *fkey flag to pass to Input() 353 * and bypass the lookup of keysym altogether. 354 */ 355int 356xtermcapKeycode(XtermWidget xw, const char **params, unsigned *state, Bool *fkey) 357{ 358 const TCAPINFO *data; 359 int code = -1; 360 char *name; 361 const char *p; 362 363 TRACE(("xtermcapKeycode(%s)\n", *params)); 364 365 /* Convert hex encoded name to ascii */ 366 name = x_decode_hex(*params, &p); 367 *params = p; 368 369 *state = 0; 370 *fkey = False; 371 372 if (!IsEmpty(name) && (*p == 0 || *p == ';')) { 373 int which; 374 375 if ((which = lookupTcapByName(name)) >= 0) { 376 if (keyIsDistinct(xw, which)) { 377 data = table + which; 378 code = data->code; 379 *state = xtermParamToState(xw, data->param); 380 if (IsFunctionKey(code)) { 381 *fkey = True; 382 } else if (code < 0) { 383 *fkey = True; 384 code = XK_Fn((-code)); 385 } 386#if OPT_SUN_FUNC_KEYS 387 if (*fkey && xw->keyboard.type == keyboardIsSun) { 388 int num = code - XK_Fn(0); 389 390 /* match function-key case in sunfuncvalue() */ 391 if (num > 20) { 392 if (num <= 30 || num > 47) { 393 code = -1; 394 } else { 395 code -= 10; 396 switch (num) { 397 case 37: /* khome */ 398 case 39: /* kpp */ 399 case 41: /* kb2 */ 400 case 43: /* kend */ 401 case 45: /* knp */ 402 code = -1; 403 break; 404 } 405 } 406 } 407 } 408#endif 409 } else { 410 TRACE(("... name ok, data not ok\n")); 411 code = -1; 412 } 413 } else { 414 TRACE(("... name not ok\n")); 415 code = -2; 416 } 417 } else { 418 TRACE(("... name not ok\n")); 419 code = -2; 420 } 421 422 TRACE(("... xtermcapKeycode(%s, %u, %d) -> %#06x\n", 423 name, *state, *fkey, code)); 424 free(name); 425 return code; 426} 427#endif /* OPT_TCAP_QUERY */ 428 429#if OPT_TCAP_FKEYS 430static int 431nextTcapByCode(int code, unsigned param, int last) 432{ 433 int result = -1; 434 int n; 435 436 TRACE(("lookupTcapByCode %#x:%#x\n", code, param)); 437 for (n = last + 1; n < (int) XtNumber(table); n++) { 438 if (table[n].code == code && 439 table[n].param == param) { 440 TRACE(("->lookupTcapByCode %d:%s\n", n, table[n].ti)); 441 result = n; 442 break; 443 } 444 } 445 return result; 446} 447 448static int 449firstTcapByCode(int code, unsigned param) 450{ 451 return nextTcapByCode(code, param, -1); 452} 453 454int 455xtermcapString(XtermWidget xw, int keycode, unsigned mask) 456{ 457 int result = 0; 458 unsigned param = xtermStateToParam(xw, mask); 459 int which; 460 461 if ((which = firstTcapByCode(keycode, param)) >= 0) { 462 TScreen *screen = TScreenOf(xw); 463 464 if (loadTermcapStrings(screen)) { 465 do { 466 char *fkey; 467 468 if ((fkey = screen->tcap_fkeys[which]) != NO_STRING) { 469 StringInput(xw, (Char *) fkey, strlen(fkey)); 470 result = 1; 471 break; 472 } 473 } while ((which = nextTcapByCode(keycode, param, which)) >= 0); 474 } 475 } 476 477 TRACE(("xtermcapString(keycode=%#x, mask=%#x) ->%d\n", 478 keycode, mask, result)); 479 480 return result; 481} 482#endif /* OPT_TCAP_FKEYS */ 483 484#endif /* OPT_TCAP_QUERY || OPT_TCAP_FKEYS */ 485 486/* 487 * If we're linked to terminfo, tgetent() will return an empty buffer. We 488 * cannot use that to adjust the $TERMCAP variable. 489 */ 490Bool 491get_termcap(XtermWidget xw, char *name) 492{ 493#if USE_TERMINFO 494 int ignored = 0; 495#endif 496 char *buffer = get_tcap_buffer(xw); 497 498 *buffer = 0; /* initialize, in case we're using terminfo's tgetent */ 499 500#if USE_EXTENDED_NAMES 501 use_extended_names(TRUE); 502#endif 503 if (!IsEmpty(name)) { 504 if (TcapInit(buffer, name)) { 505 TRACE(("get_termcap(%s) succeeded (%s)\n", name, 506 (*buffer 507 ? "ok:termcap, we can update $TERMCAP" 508 : "assuming this is terminfo"))); 509 return True; 510 } else { 511 *buffer = 0; /* just in case */ 512 } 513 } 514 return False; 515} 516 517/* 518 * Retrieve the termcap-buffer. 519 */ 520char * 521get_tcap_buffer(XtermWidget xw) 522{ 523 TScreen *screen = TScreenOf(xw); 524 char *buffer; 525 526#if OPT_TEK4014 527 if (TEK4014_ACTIVE(xw)) { 528 buffer = TekScreenOf(tekWidget)->tcapbuf; 529 } else 530#endif 531 { 532 buffer = screen->tcapbuf; 533 } 534 return buffer; 535} 536 537/* 538 * Retrieve the erase-key, for initialization in main program. 539 */ 540char * 541get_tcap_erase(XtermWidget xw GCC_UNUSED) 542{ 543#ifdef USE_TERMCAP 544 char *area = TScreenOf(xw)->tcap_area; 545#endif 546 char *fkey; 547 548#ifndef USE_TERMCAP 549 fkey = tigetstr("kbs"); 550#else 551 fkey = tgetstr("kb", &area); 552#endif 553 554 if (fkey == NO_STRING) 555 fkey = 0; 556 if (fkey != 0) 557 fkey = x_strdup(fkey); 558 return fkey; 559} 560 561/* 562 * A legal termcap (or terminfo) name consists solely of graphic characters, 563 * excluding the punctuation used to delimit fields of the source description. 564 */ 565static Bool 566isLegalTcapName(const char *name) 567{ 568 Bool result = False; 569 570 if (*name != '\0') { 571 result = True; 572 while (*name != '\0') { 573 if (isgraph(CharOf(*name))) { 574 if (strchr("\\|,:'\"", *name) != 0) { 575 result = False; 576 break; 577 } 578 } else { 579 result = False; 580 break; 581 } 582 ++name; 583 } 584 } 585 586 return result; 587} 588 589void 590set_termcap(XtermWidget xw, const char *name) 591{ 592 Boolean success = False; 593#if USE_TERMINFO 594 int ignored = 0; 595#else 596 TScreen *screen = TScreenOf(xw); 597 char buffer[sizeof(screen->tcapbuf)]; 598#endif 599 600 TRACE(("set_termcap(%s)\n", NonNull(name))); 601 if (IsEmpty(name)) { 602 Bell(xw, XkbBI_MinorError, 0); 603 } else { 604 const char *temp; 605 char *value; 606 607 if ((value = x_decode_hex(name, &temp)) != 0) { 608 if (*temp == '\0' && isLegalTcapName(value)) { 609 if (TcapInit(buffer, value)) { 610#if !USE_TERMINFO 611 memcpy(screen->tcapbuf, buffer, sizeof(buffer)); 612#endif 613 free_termcap(xw); 614 success = True; 615 } 616 } 617 free(value); 618 } 619 } 620 if (!success) 621 Bell(xw, XkbBI_MinorError, 0); 622} 623 624void 625free_termcap(XtermWidget xw) 626{ 627#if OPT_TCAP_FKEYS 628 TScreen *screen = TScreenOf(xw); 629 630 if (screen->tcap_fkeys != 0) { 631 Cardinal want = XtNumber(table); 632 Cardinal have; 633 634 for (have = 0; have < want; ++have) { 635 char *fkey = screen->tcap_fkeys[have]; 636 if (fkey != 0 && fkey != NO_STRING) { 637 free(fkey); 638 } 639 } 640 free(screen->tcap_fkeys); 641 screen->tcap_fkeys = 0; 642 } 643#endif 644} 645