1 /* $OpenBSD: lib_setup.c,v 1.13 2021/03/10 20:16:08 millert Exp $ */ 2 3 /**************************************************************************** 4 * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc. * 5 * * 6 * Permission is hereby granted, free of charge, to any person obtaining a * 7 * copy of this software and associated documentation files (the * 8 * "Software"), to deal in the Software without restriction, including * 9 * without limitation the rights to use, copy, modify, merge, publish, * 10 * distribute, distribute with modifications, sublicense, and/or sell * 11 * copies of the Software, and to permit persons to whom the Software is * 12 * furnished to do so, subject to the following conditions: * 13 * * 14 * The above copyright notice and this permission notice shall be included * 15 * in all copies or substantial portions of the Software. * 16 * * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 20 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 24 * * 25 * Except as contained in this notice, the name(s) of the above copyright * 26 * holders shall not be used in advertising or otherwise to promote the * 27 * sale, use or other dealings in this Software without prior written * 28 * authorization. * 29 ****************************************************************************/ 30 31 /**************************************************************************** 32 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 33 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 34 * and: Thomas E. Dickey 1996-on * 35 ****************************************************************************/ 36 37 /* 38 * Terminal setup routines common to termcap and terminfo: 39 * 40 * use_env(bool) 41 * setupterm(char *, int, int *) 42 */ 43 44 #include <curses.priv.h> 45 #include <tic.h> /* for MAX_NAME_SIZE */ 46 #include <term_entry.h> 47 48 #if SVR4_TERMIO && !defined(_POSIX_SOURCE) 49 #define _POSIX_SOURCE 50 #endif 51 52 #if HAVE_LOCALE_H 53 #include <locale.h> 54 #endif 55 56 #include <term.h> /* lines, columns, cur_term */ 57 58 MODULE_ID("$Id: lib_setup.c,v 1.13 2021/03/10 20:16:08 millert Exp $") 59 60 /**************************************************************************** 61 * 62 * Terminal size computation 63 * 64 ****************************************************************************/ 65 66 #if HAVE_SIZECHANGE 67 # if !defined(sun) || !TERMIOS 68 # if HAVE_SYS_IOCTL_H 69 # include <sys/ioctl.h> 70 # endif 71 # endif 72 #endif 73 74 #if NEED_PTEM_H 75 /* On SCO, they neglected to define struct winsize in termios.h -- it's only 76 * in termio.h and ptem.h (the former conflicts with other definitions). 77 */ 78 # include <sys/stream.h> 79 # include <sys/ptem.h> 80 #endif 81 82 #if HAVE_LANGINFO_CODESET 83 #include <langinfo.h> 84 #endif 85 86 /* 87 * SCO defines TIOCGSIZE and the corresponding struct. Other systems (SunOS, 88 * Solaris, IRIX) define TIOCGWINSZ and struct winsize. 89 */ 90 #ifdef TIOCGSIZE 91 # define IOCTL_WINSIZE TIOCGSIZE 92 # define STRUCT_WINSIZE struct ttysize 93 # define WINSIZE_ROWS(n) (int)n.ts_lines 94 # define WINSIZE_COLS(n) (int)n.ts_cols 95 #else 96 # ifdef TIOCGWINSZ 97 # define IOCTL_WINSIZE TIOCGWINSZ 98 # define STRUCT_WINSIZE struct winsize 99 # define WINSIZE_ROWS(n) (int)n.ws_row 100 # define WINSIZE_COLS(n) (int)n.ws_col 101 # endif 102 #endif 103 104 /* 105 * Reduce explicit use of "cur_term" global variable. 106 */ 107 #undef CUR 108 #define CUR termp->type. 109 110 /* 111 * Wrap global variables in this module. 112 */ 113 #if USE_REENTRANT 114 NCURSES_EXPORT(char *) 115 NCURSES_PUBLIC_VAR(ttytype) (void) 116 { 117 static char empty[] = ""; 118 return cur_term ? cur_term->type.term_names : empty; 119 } 120 NCURSES_EXPORT(int *) 121 _nc_ptr_Lines(void) 122 { 123 return ptrLines(); 124 } 125 NCURSES_EXPORT(int) 126 NCURSES_PUBLIC_VAR(LINES) (void) 127 { 128 return *_nc_ptr_Lines(); 129 } 130 NCURSES_EXPORT(int *) 131 _nc_ptr_Cols(void) 132 { 133 return ptrCols(); 134 } 135 NCURSES_EXPORT(int) 136 NCURSES_PUBLIC_VAR(COLS) (void) 137 { 138 return *_nc_ptr_Cols(); 139 } 140 NCURSES_EXPORT(int) 141 NCURSES_PUBLIC_VAR(TABSIZE) (void) 142 { 143 return SP ? SP->_TABSIZE : 8; 144 } 145 #else 146 NCURSES_EXPORT_VAR(char) ttytype[NAMESIZE] = ""; 147 NCURSES_EXPORT_VAR(int) LINES = 0; 148 NCURSES_EXPORT_VAR(int) COLS = 0; 149 NCURSES_EXPORT_VAR(int) TABSIZE = 0; 150 #endif 151 152 #if NCURSES_EXT_FUNCS 153 NCURSES_EXPORT(int) 154 set_tabsize(int value) 155 { 156 int code = OK; 157 #if USE_REENTRANT 158 if (SP) { 159 SP->_TABSIZE = value; 160 } else { 161 code = ERR; 162 } 163 #else 164 TABSIZE = value; 165 #endif 166 return code; 167 } 168 #endif 169 170 #if USE_SIGWINCH 171 /* 172 * If we have a pending SIGWINCH, set the flag in each screen. 173 */ 174 NCURSES_EXPORT(int) 175 _nc_handle_sigwinch(SCREEN *sp) 176 { 177 SCREEN *scan; 178 179 if (_nc_globals.have_sigwinch) { 180 _nc_globals.have_sigwinch = 0; 181 182 for (each_screen(scan)) { 183 scan->_sig_winch = TRUE; 184 } 185 } 186 187 return (sp ? sp->_sig_winch : 0); 188 } 189 190 #endif 191 192 NCURSES_EXPORT(void) 193 use_env(bool f) 194 { 195 T((T_CALLED("use_env()"))); 196 _nc_prescreen.use_env = f; 197 returnVoid; 198 } 199 200 NCURSES_EXPORT(void) 201 _nc_get_screensize(SCREEN *sp, int *linep, int *colp) 202 /* Obtain lines/columns values from the environment and/or terminfo entry */ 203 { 204 TERMINAL *termp = cur_term; 205 int my_tabsize; 206 207 /* figure out the size of the screen */ 208 T(("screen size: terminfo lines = %d columns = %d", lines, columns)); 209 210 if (!_nc_prescreen.use_env) { 211 *linep = (int) lines; 212 *colp = (int) columns; 213 } else { /* usually want to query LINES and COLUMNS from environment */ 214 int value; 215 216 *linep = *colp = 0; 217 218 /* first, look for environment variables */ 219 if ((value = _nc_getenv_num("LINES")) > 0) { 220 *linep = value; 221 } 222 if ((value = _nc_getenv_num("COLUMNS")) > 0) { 223 *colp = value; 224 } 225 T(("screen size: environment LINES = %d COLUMNS = %d", *linep, *colp)); 226 227 #ifdef __EMX__ 228 if (*linep <= 0 || *colp <= 0) { 229 int screendata[2]; 230 _scrsize(screendata); 231 *colp = screendata[0]; 232 *linep = screendata[1]; 233 T(("EMX screen size: environment LINES = %d COLUMNS = %d", 234 *linep, *colp)); 235 } 236 #endif 237 #if HAVE_SIZECHANGE 238 /* if that didn't work, maybe we can try asking the OS */ 239 if (*linep <= 0 || *colp <= 0) { 240 if (isatty(cur_term->Filedes)) { 241 STRUCT_WINSIZE size; 242 243 errno = 0; 244 do { 245 if (ioctl(cur_term->Filedes, IOCTL_WINSIZE, &size) < 0 246 && errno != EINTR) 247 goto failure; 248 } while 249 (errno == EINTR); 250 251 /* 252 * Solaris lets users override either dimension with an 253 * environment variable. 254 */ 255 if (*linep <= 0) 256 *linep = (sp != 0 && sp->_filtered) ? 1 : WINSIZE_ROWS(size); 257 if (*colp <= 0) 258 *colp = WINSIZE_COLS(size); 259 } 260 /* FALLTHRU */ 261 failure:; 262 } 263 #endif /* HAVE_SIZECHANGE */ 264 265 /* if we can't get dynamic info about the size, use static */ 266 if (*linep <= 0) { 267 *linep = (int) lines; 268 } 269 if (*colp <= 0) { 270 *colp = (int) columns; 271 } 272 273 /* the ultimate fallback, assume fixed 24x80 size */ 274 if (*linep <= 0) { 275 *linep = 24; 276 } 277 if (*colp <= 0) { 278 *colp = 80; 279 } 280 281 /* 282 * Put the derived values back in the screen-size caps, so 283 * tigetnum() and tgetnum() will do the right thing. 284 */ 285 lines = (short) (*linep); 286 columns = (short) (*colp); 287 } 288 289 T(("screen size is %dx%d", *linep, *colp)); 290 291 if (VALID_NUMERIC(init_tabs)) 292 my_tabsize = (int) init_tabs; 293 else 294 my_tabsize = 8; 295 296 #if USE_REENTRANT 297 if (sp != 0) 298 sp->_TABSIZE = my_tabsize; 299 #else 300 TABSIZE = my_tabsize; 301 #endif 302 T(("TABSIZE = %d", TABSIZE)); 303 } 304 305 #if USE_SIZECHANGE 306 NCURSES_EXPORT(void) 307 _nc_update_screensize(SCREEN *sp) 308 { 309 TERMINAL *termp = cur_term; 310 int old_lines = lines; 311 int new_lines; 312 int old_cols = columns; 313 int new_cols; 314 315 _nc_get_screensize(sp, &new_lines, &new_cols); 316 317 /* 318 * See is_term_resized() and resizeterm(). 319 * We're doing it this way because those functions belong to the upper 320 * ncurses library, while this resides in the lower terminfo library. 321 */ 322 if (sp != 0 323 && sp->_resize != 0) { 324 if ((new_lines != old_lines) || (new_cols != old_cols)) { 325 sp->_resize(new_lines, new_cols); 326 } else if (sp->_sig_winch && (sp->_ungetch != 0)) { 327 sp->_ungetch(SP, KEY_RESIZE); /* so application can know this */ 328 } 329 sp->_sig_winch = FALSE; 330 } 331 } 332 #endif 333 334 /**************************************************************************** 335 * 336 * Terminal setup 337 * 338 ****************************************************************************/ 339 340 #define ret_error(code, fmt, arg) if (errret) {\ 341 *errret = code;\ 342 returnCode(ERR);\ 343 } else {\ 344 fprintf(stderr, fmt, arg);\ 345 exit(EXIT_FAILURE);\ 346 } 347 348 #define ret_error0(code, msg) if (errret) {\ 349 *errret = code;\ 350 returnCode(ERR);\ 351 } else {\ 352 fprintf(stderr, msg);\ 353 exit(EXIT_FAILURE);\ 354 } 355 356 #if USE_DATABASE || USE_TERMCAP 357 /* 358 * Return 1 if entry found, 0 if not found, -1 if database not accessible, 359 * just like tgetent(). 360 */ 361 static int 362 grab_entry(const char *const tn, TERMTYPE *const tp) 363 { 364 char filename[PATH_MAX]; 365 int status = _nc_read_entry(tn, filename, tp); 366 367 /* 368 * If we have an entry, force all of the cancelled strings to null 369 * pointers so we don't have to test them in the rest of the library. 370 * (The terminfo compiler bypasses this logic, since it must know if 371 * a string is cancelled, for merging entries). 372 */ 373 if (status == TGETENT_YES) { 374 unsigned n; 375 for_each_boolean(n, tp) { 376 if (!VALID_BOOLEAN(tp->Booleans[n])) 377 tp->Booleans[n] = FALSE; 378 } 379 for_each_string(n, tp) { 380 if (tp->Strings[n] == CANCELLED_STRING) 381 tp->Strings[n] = ABSENT_STRING; 382 } 383 } 384 return (status); 385 } 386 #endif 387 388 /* 389 ** do_prototype() 390 ** 391 ** Take the real command character out of the CC environment variable 392 ** and substitute it in for the prototype given in 'command_character'. 393 */ 394 static void 395 do_prototype(TERMINAL * termp) 396 { 397 unsigned i; 398 char CC; 399 char proto; 400 char *tmp; 401 402 if ((tmp = getenv("CC")) != 0) { 403 if ((CC = *tmp) != 0) { 404 proto = *command_character; 405 406 for_each_string(i, &(termp->type)) { 407 for (tmp = termp->type.Strings[i]; *tmp; tmp++) { 408 if (*tmp == proto) 409 *tmp = CC; 410 } 411 } 412 } 413 } 414 } 415 416 /* 417 * Find the locale which is in effect. 418 */ 419 NCURSES_EXPORT(char *) 420 _nc_get_locale(void) 421 { 422 char *env; 423 #if HAVE_LOCALE_H 424 /* 425 * This is preferable to using getenv() since it ensures that we are using 426 * the locale which was actually initialized by the application. 427 */ 428 env = setlocale(LC_CTYPE, 0); 429 #else 430 if (((env = getenv("LC_ALL")) != 0 && *env != '\0') 431 || ((env = getenv("LC_CTYPE")) != 0 && *env != '\0') 432 || ((env = getenv("LANG")) != 0 && *env != '\0')) { 433 ; 434 } 435 #endif 436 T(("_nc_get_locale %s", _nc_visbuf(env))); 437 return env; 438 } 439 440 /* 441 * Check if we are running in a UTF-8 locale. 442 */ 443 NCURSES_EXPORT(int) 444 _nc_unicode_locale(void) 445 { 446 int result = 0; 447 #if HAVE_LANGINFO_CODESET 448 char *env = nl_langinfo(CODESET); 449 result = !strcmp(env, "UTF-8"); 450 T(("_nc_unicode_locale(%s) ->%d", env, result)); 451 #else 452 char *env = _nc_get_locale(); 453 if (env != 0) { 454 if (strstr(env, ".UTF-8") != 0) { 455 result = 1; 456 T(("_nc_unicode_locale(%s) ->%d", env, result)); 457 } 458 } 459 #endif 460 return result; 461 } 462 463 #define CONTROL_N(s) ((s) != 0 && strstr(s, "\016") != 0) 464 #define CONTROL_O(s) ((s) != 0 && strstr(s, "\017") != 0) 465 466 /* 467 * Check for known broken cases where a UTF-8 locale breaks the alternate 468 * character set. 469 */ 470 NCURSES_EXPORT(int) 471 _nc_locale_breaks_acs(TERMINAL * termp) 472 { 473 char *env; 474 475 if ((env = getenv("NCURSES_NO_UTF8_ACS")) != 0) { 476 return atoi(env); 477 } else if ((env = getenv("TERM")) != 0) { 478 if (strstr(env, "linux")) 479 return 1; /* always broken */ 480 if (strstr(env, "screen") != 0 481 && ((env = getenv("TERMCAP")) != 0 482 && strstr(env, "screen") != 0) 483 && strstr(env, "hhII00") != 0) { 484 if (CONTROL_N(enter_alt_charset_mode) || 485 CONTROL_O(enter_alt_charset_mode) || 486 CONTROL_N(set_attributes) || 487 CONTROL_O(set_attributes)) 488 return 1; 489 } 490 } 491 return 0; 492 } 493 494 /* 495 * This entrypoint is called from tgetent() to allow a special case of reusing 496 * the same TERMINAL data (see comment). 497 */ 498 NCURSES_EXPORT(int) 499 _nc_setupterm(NCURSES_CONST char *tname, int Filedes, int *errret, bool reuse) 500 { 501 TERMINAL *termp; 502 int status; 503 504 START_TRACE(); 505 T((T_CALLED("setupterm(%s,%d,%p)"), _nc_visbuf(tname), Filedes, errret)); 506 507 if (tname == 0) { 508 tname = getenv("TERM"); 509 if (tname == 0 || *tname == '\0') { 510 ret_error0(TGETENT_ERR, "TERM environment variable not set.\n"); 511 } 512 } 513 514 if (strlen(tname) > MAX_NAME_SIZE) { 515 ret_error(TGETENT_ERR, 516 "TERM environment must be <= %d characters.\n", 517 MAX_NAME_SIZE); 518 } 519 520 T(("your terminal name is %s", tname)); 521 522 /* 523 * Allow output redirection. This is what SVr3 does. If stdout is 524 * directed to a file, screen updates go to standard error. 525 */ 526 if (Filedes == STDOUT_FILENO && !isatty(Filedes)) 527 Filedes = STDERR_FILENO; 528 529 /* 530 * Check if we have already initialized to use this terminal. If so, we 531 * do not need to re-read the terminfo entry, or obtain TTY settings. 532 * 533 * This is an improvement on SVr4 curses. If an application mixes curses 534 * and termcap calls, it may call both initscr and tgetent. This is not 535 * really a good thing to do, but can happen if someone tries using ncurses 536 * with the readline library. The problem we are fixing is that when 537 * tgetent calls setupterm, the resulting Ottyb struct in cur_term is 538 * zeroed. A subsequent call to endwin uses the zeroed terminal settings 539 * rather than the ones saved in initscr. So we check if cur_term appears 540 * to contain terminal settings for the same output file as our current 541 * call - and copy those terminal settings. (SVr4 curses does not do this, 542 * however applications that are working around the problem will still work 543 * properly with this feature). 544 */ 545 if (reuse 546 && (termp = cur_term) != 0 547 && termp->Filedes == Filedes 548 && termp->_termname != 0 549 && !strcmp(termp->_termname, tname) 550 && _nc_name_match(termp->type.term_names, tname, "|")) { 551 T(("reusing existing terminal information and mode-settings")); 552 } else { 553 554 termp = typeCalloc(TERMINAL, 1); 555 556 if (termp == 0) { 557 ret_error0(TGETENT_ERR, 558 "Not enough memory to create terminal structure.\n"); 559 } 560 #if USE_DATABASE || USE_TERMCAP 561 status = grab_entry(tname, &termp->type); 562 #else 563 status = TGETENT_NO; 564 #endif 565 566 /* try fallback list if entry on disk */ 567 if (status != TGETENT_YES) { 568 const TERMTYPE *fallback = _nc_fallback(tname); 569 570 if (fallback) { 571 termp->type = *fallback; 572 status = TGETENT_YES; 573 } 574 } 575 576 if (status != TGETENT_YES) { 577 del_curterm(termp); 578 if (status == TGETENT_ERR) { 579 ret_error0(status, "terminals database is inaccessible\n"); 580 } else if (status == TGETENT_NO) { 581 ret_error(status, "'%s': unknown terminal type.\n", tname); 582 } 583 } 584 #if !USE_REENTRANT 585 strncpy(ttytype, termp->type.term_names, NAMESIZE - 1); 586 ttytype[NAMESIZE - 1] = '\0'; 587 #endif 588 589 termp->Filedes = Filedes; 590 termp->_termname = strdup(tname); 591 592 set_curterm(termp); 593 594 if (command_character && getenv("CC")) 595 do_prototype(termp); 596 597 /* 598 * If an application calls setupterm() rather than initscr() or 599 * newterm(), we will not have the def_prog_mode() call in 600 * _nc_setupscreen(). Do it now anyway, so we can initialize the 601 * baudrate. 602 */ 603 if (isatty(Filedes)) { 604 def_prog_mode(); 605 baudrate(); 606 } 607 } 608 609 /* 610 * We should always check the screensize, just in case. 611 */ 612 _nc_get_screensize(SP, ptrLines(), ptrCols()); 613 614 if (errret) 615 *errret = TGETENT_YES; 616 617 if (generic_type) { 618 ret_error(TGETENT_NO, "'%s': I need something more specific.\n", tname); 619 } 620 if (hard_copy) { 621 ret_error(TGETENT_YES, "'%s': I can't handle hardcopy terminals.\n", tname); 622 } 623 returnCode(OK); 624 } 625 626 /* 627 * setupterm(termname, Filedes, errret) 628 * 629 * Find and read the appropriate object file for the terminal 630 * Make cur_term point to the structure. 631 */ 632 NCURSES_EXPORT(int) 633 setupterm(NCURSES_CONST char *tname, int Filedes, int *errret) 634 { 635 return _nc_setupterm(tname, Filedes, errret, FALSE); 636 } 637