xtermcap.c revision 894e0ac8
1/* $XTermId: xtermcap.c,v 1.48 2014/05/03 12:43:20 tom Exp $ */ 2 3/* 4 * Copyright 2007-2011,2014 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 char name[80]; 227 Cardinal want = XtNumber(table); 228 Cardinal have; 229 char *fkey; 230#ifdef USE_TERMCAP 231 char *area = screen->tcap_area; 232#endif 233 234 TRACE(("loadTermcapStrings\n")); 235 if ((screen->tcap_fkeys = TypeCallocN(char *, want)) != 0) { 236 for (have = 0; have < want; ++have) { 237#ifndef USE_TERMCAP 238 fkey = tigetstr(strcpy(name, table[have].ti)); 239#else 240 fkey = tgetstr(strcpy(name, table[have].tc), &area); 241#endif 242 if (fkey != 0 && fkey != NO_STRING) { 243 screen->tcap_fkeys[have] = x_strdup(fkey); 244 } else { 245 screen->tcap_fkeys[have] = NO_STRING; 246 } 247 } 248 } else { 249 result = False; 250 } 251 } 252 return result; 253} 254#endif 255 256#if OPT_TCAP_QUERY 257static Boolean 258keyIsDistinct(XtermWidget xw, int which) 259{ 260 Boolean result = True; 261 262 switch (xw->keyboard.type) { 263 case keyboardIsTermcap: 264#if OPT_TCAP_FKEYS 265 if (table[which].param == SHIFT) { 266 TScreen *screen = TScreenOf(xw); 267 Cardinal k; 268 char *fkey; 269 270 if (loadTermcapStrings(screen) 271 && screen->tcap_fkeys[which] != NO_STRING) { 272 for (k = 0; k < XtNumber(table); k++) { 273 if (table[k].code == table[which].code 274 && table[k].param == 0) { 275 if ((fkey = screen->tcap_fkeys[k]) != NO_STRING 276 && !strcmp(fkey, screen->tcap_fkeys[which])) { 277 TRACE(("shifted/unshifted keys do not differ\n")); 278 result = False; 279 } 280 break; 281 } 282 } 283 } else { 284 /* there is no data for the shifted key */ 285 result = -1; 286 } 287 } 288#endif 289 break; 290 /* 291 * The vt220-keyboard will not return distinct key sequences for 292 * shifted cursor-keys. Just pretend they do not exist, since some 293 * programs may be confused if we return the same data for 294 * shifted/unshifted keys. 295 */ 296 case keyboardIsVT220: 297 if (table[which].param == SHIFT) { 298 TRACE(("shifted/unshifted keys do not differ\n")); 299 result = False; 300 } 301 break; 302 case keyboardIsLegacy: 303 case keyboardIsDefault: 304 case keyboardIsHP: 305 case keyboardIsSCO: 306 case keyboardIsSun: 307 break; 308 } 309 310 return result; 311} 312 313static int 314lookupTcapByName(const char *name) 315{ 316 int result = -2; 317 Cardinal j; 318 319 if (!IsEmpty(name)) { 320 for (j = 0; j < XtNumber(table); j++) { 321 if (!strcmp(table[j].ti, name) || !strcmp(table[j].tc, name)) { 322 result = (int) j; 323 break; 324 } 325 } 326 } 327 328 if (result >= 0) { 329 TRACE(("lookupTcapByName(%s) tc=%s, ti=%s code %#x, param %#x\n", 330 name, 331 table[result].tc, 332 table[result].ti, 333 table[result].code, 334 table[result].param)); 335 } else { 336 TRACE(("lookupTcapByName(%s) FAIL\n", name)); 337 } 338 return result; 339} 340 341/* 342 * Parse the termcap/terminfo name from the string, returning a positive number 343 * (the keysym) if found, otherwise -1. Update the string pointer. 344 * Returns the (shift, control) state in *state. 345 * 346 * This does not attempt to construct control/shift modifiers to construct 347 * function-key values. Instead, it sets the *fkey flag to pass to Input() 348 * and bypass the lookup of keysym altogether. 349 */ 350int 351xtermcapKeycode(XtermWidget xw, const char **params, unsigned *state, Bool *fkey) 352{ 353 const TCAPINFO *data; 354 int which; 355 int code = -1; 356 char *name; 357 const char *p; 358 359 TRACE(("xtermcapKeycode(%s)\n", *params)); 360 361 /* Convert hex encoded name to ascii */ 362 name = x_decode_hex(*params, &p); 363 *params = p; 364 365 *state = 0; 366 *fkey = False; 367 368 if (!IsEmpty(name) && (*p == 0 || *p == ';')) { 369 if ((which = lookupTcapByName(name)) >= 0) { 370 if (keyIsDistinct(xw, which)) { 371 data = table + which; 372 code = data->code; 373 *state = xtermParamToState(xw, data->param); 374 if (IsFunctionKey(code)) { 375 *fkey = True; 376 } else if (code < 0) { 377 *fkey = True; 378 code = XK_Fn((-code)); 379 } 380#if OPT_SUN_FUNC_KEYS 381 if (*fkey && xw->keyboard.type == keyboardIsSun) { 382 int num = code - XK_Fn(0); 383 384 /* match function-key case in sunfuncvalue() */ 385 if (num > 20) { 386 if (num <= 30 || num > 47) { 387 code = -1; 388 } else { 389 code -= 10; 390 switch (num) { 391 case 37: /* khome */ 392 case 39: /* kpp */ 393 case 41: /* kb2 */ 394 case 43: /* kend */ 395 case 45: /* knp */ 396 code = -1; 397 break; 398 } 399 } 400 } 401 } 402#endif 403 } else { 404 TRACE(("... name ok, data not ok\n")); 405 code = -1; 406 } 407 } else { 408 TRACE(("... name not ok\n")); 409 code = -2; 410 } 411 } else { 412 TRACE(("... name not ok\n")); 413 code = -2; 414 } 415 416 TRACE(("... xtermcapKeycode(%s, %u, %d) -> %#06x\n", 417 name, *state, *fkey, code)); 418 free(name); 419 return code; 420} 421#endif /* OPT_TCAP_QUERY */ 422 423#if OPT_TCAP_FKEYS 424static int 425nextTcapByCode(int code, unsigned param, int last) 426{ 427 int result = -1; 428 int n; 429 430 TRACE(("lookupTcapByCode %#x:%#x\n", code, param)); 431 for (n = last + 1; n < (int) XtNumber(table); n++) { 432 if (table[n].code == code && 433 table[n].param == param) { 434 TRACE(("->lookupTcapByCode %d:%s\n", n, table[n].ti)); 435 result = n; 436 break; 437 } 438 } 439 return result; 440} 441 442static int 443firstTcapByCode(int code, unsigned param) 444{ 445 return nextTcapByCode(code, param, -1); 446} 447 448int 449xtermcapString(XtermWidget xw, int keycode, unsigned mask) 450{ 451 int result = 0; 452 unsigned param = xtermStateToParam(xw, mask); 453 int which; 454 455 if ((which = firstTcapByCode(keycode, param)) >= 0) { 456 TScreen *screen = TScreenOf(xw); 457 char *fkey; 458 459 if (loadTermcapStrings(screen)) { 460 do { 461 if ((fkey = screen->tcap_fkeys[which]) != NO_STRING) { 462 StringInput(xw, (Char *) fkey, strlen(fkey)); 463 result = 1; 464 break; 465 } 466 } while ((which = nextTcapByCode(keycode, param, which)) >= 0); 467 } 468 } 469 470 TRACE(("xtermcapString(keycode=%#x, mask=%#x) ->%d\n", 471 keycode, mask, result)); 472 473 return result; 474} 475#endif /* OPT_TCAP_FKEYS */ 476 477#endif /* OPT_TCAP_QUERY || OPT_TCAP_FKEYS */ 478 479/* 480 * If we're linked to terminfo, tgetent() will return an empty buffer. We 481 * cannot use that to adjust the $TERMCAP variable. 482 */ 483Bool 484get_termcap(XtermWidget xw, char *name) 485{ 486#if USE_TERMINFO 487 int ignored = 0; 488#endif 489 char *buffer = get_tcap_buffer(xw); 490 491 *buffer = 0; /* initialize, in case we're using terminfo's tgetent */ 492 493#if USE_EXTENDED_NAMES 494 use_extended_names(TRUE); 495#endif 496 if (!IsEmpty(name)) { 497 if (TcapInit(buffer, name)) { 498 TRACE(("get_termcap(%s) succeeded (%s)\n", name, 499 (*buffer 500 ? "ok:termcap, we can update $TERMCAP" 501 : "assuming this is terminfo"))); 502 return True; 503 } else { 504 *buffer = 0; /* just in case */ 505 } 506 } 507 return False; 508} 509 510/* 511 * Retrieve the termcap-buffer. 512 */ 513char * 514get_tcap_buffer(XtermWidget xw) 515{ 516 TScreen *screen = TScreenOf(xw); 517 char *buffer; 518 519#if OPT_TEK4014 520 if (TEK4014_ACTIVE(xw)) { 521 buffer = TekScreenOf(tekWidget)->tcapbuf; 522 } else 523#endif 524 { 525 buffer = screen->tcapbuf; 526 } 527 return buffer; 528} 529 530/* 531 * Retrieve the erase-key, for initialization in main program. 532 */ 533char * 534get_tcap_erase(XtermWidget xw GCC_UNUSED) 535{ 536#ifdef USE_TERMCAP 537 char *area = TScreenOf(xw)->tcap_area; 538#endif 539 char *fkey; 540 541#ifndef USE_TERMCAP 542 fkey = tigetstr("kbs"); 543#else 544 fkey = tgetstr("kb", &area); 545#endif 546 547 if (fkey == NO_STRING) 548 fkey = 0; 549 if (fkey != 0) 550 fkey = x_strdup(fkey); 551 return fkey; 552} 553 554/* 555 * A legal termcap (or terminfo) name consists solely of graphic characters, 556 * excluding the punctuation used to delimit fields of the source description. 557 */ 558static Bool 559isLegalTcapName(const char *name) 560{ 561 Bool result = False; 562 563 if (*name != '\0') { 564 result = True; 565 while (*name != '\0') { 566 if (isgraph(CharOf(*name))) { 567 if (strchr("\\|,:'\"", *name) != 0) { 568 result = False; 569 break; 570 } 571 } else { 572 result = False; 573 break; 574 } 575 ++name; 576 } 577 } 578 579 return result; 580} 581 582void 583set_termcap(XtermWidget xw, const char *name) 584{ 585 Boolean success = False; 586#if USE_TERMINFO 587 int ignored = 0; 588#else 589 TScreen *screen = TScreenOf(xw); 590 char buffer[sizeof(screen->tcapbuf)]; 591#endif 592 593 TRACE(("set_termcap(%s)\n", NonNull(name))); 594 if (IsEmpty(name)) { 595 Bell(xw, XkbBI_MinorError, 0); 596 } else { 597 const char *temp; 598 char *value; 599 600 if ((value = x_decode_hex(name, &temp)) != 0) { 601 if (*temp == '\0' && isLegalTcapName(value)) { 602 if (TcapInit(buffer, value)) { 603#if !USE_TERMINFO 604 memcpy(screen->tcapbuf, buffer, sizeof(buffer)); 605#endif 606 free_termcap(xw); 607 success = True; 608 } 609 } 610 free(value); 611 } 612 } 613 if (!success) 614 Bell(xw, XkbBI_MinorError, 0); 615} 616 617void 618free_termcap(XtermWidget xw) 619{ 620#if OPT_TCAP_FKEYS 621 TScreen *screen = TScreenOf(xw); 622 623 if (screen->tcap_fkeys != 0) { 624 Cardinal want = XtNumber(table); 625 Cardinal have; 626 char *fkey; 627 628 for (have = 0; have < want; ++have) { 629 fkey = screen->tcap_fkeys[have]; 630 if (fkey != 0 && fkey != NO_STRING) { 631 free(fkey); 632 } 633 } 634 free(screen->tcap_fkeys); 635 screen->tcap_fkeys = 0; 636 } 637#endif 638} 639