blob: 99097f00fc1d3e9844a1ae03e9e3f4a93c280991 [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2018-2023,2024 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05304 * *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
27 * authorization. *
28 ****************************************************************************/
29
30/****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996-on *
Steve Kondikae271bc2015-11-15 02:50:53 +010034 * and: Juergen Pfeifer 2009 *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053035 ****************************************************************************/
36
37/*
38 * Terminal setup routines common to termcap and terminfo:
39 *
40 * use_env(bool)
Steve Kondikae271bc2015-11-15 02:50:53 +010041 * use_tioctl(bool)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053042 * setupterm(char *, int, int *)
43 */
44
45#include <curses.priv.h>
46#include <tic.h> /* for MAX_NAME_SIZE */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053047
48#if HAVE_LOCALE_H
49#include <locale.h>
50#endif
51
micky3879b9f5e72025-07-08 18:04:53 -040052MODULE_ID("$Id: lib_setup.c,v 1.240 2024/04/20 17:04:05 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053053
54/****************************************************************************
55 *
56 * Terminal size computation
57 *
58 ****************************************************************************/
59
60#if HAVE_SIZECHANGE
61# if !defined(sun) || !TERMIOS
62# if HAVE_SYS_IOCTL_H
63# include <sys/ioctl.h>
64# endif
65# endif
66#endif
67
68#if NEED_PTEM_H
micky3879b9f5e72025-07-08 18:04:53 -040069 /* On SCO, they neglected to define struct winsize in termios.h -- it is only
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053070 * in termio.h and ptem.h (the former conflicts with other definitions).
71 */
72# include <sys/stream.h>
73# include <sys/ptem.h>
74#endif
75
76#if HAVE_LANGINFO_CODESET
77#include <langinfo.h>
78#endif
79
80/*
81 * SCO defines TIOCGSIZE and the corresponding struct. Other systems (SunOS,
82 * Solaris, IRIX) define TIOCGWINSZ and struct winsize.
83 */
84#ifdef TIOCGSIZE
85# define IOCTL_WINSIZE TIOCGSIZE
86# define STRUCT_WINSIZE struct ttysize
87# define WINSIZE_ROWS(n) (int)n.ts_lines
88# define WINSIZE_COLS(n) (int)n.ts_cols
89#else
90# ifdef TIOCGWINSZ
91# define IOCTL_WINSIZE TIOCGWINSZ
92# define STRUCT_WINSIZE struct winsize
93# define WINSIZE_ROWS(n) (int)n.ws_row
94# define WINSIZE_COLS(n) (int)n.ws_col
95# endif
96#endif
97
98/*
99 * Reduce explicit use of "cur_term" global variable.
100 */
101#undef CUR
micky3879b9f5e72025-07-08 18:04:53 -0400102#define CUR TerminalType(termp).
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530103
104/*
105 * Wrap global variables in this module.
106 */
107#if USE_REENTRANT
Steve Kondikae271bc2015-11-15 02:50:53 +0100108
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530109NCURSES_EXPORT(char *)
110NCURSES_PUBLIC_VAR(ttytype) (void)
111{
112 static char empty[] = "";
Steve Kondikae271bc2015-11-15 02:50:53 +0100113 char *result = empty;
114
115#if NCURSES_SP_FUNCS
116 if (CURRENT_SCREEN) {
117 TERMINAL *termp = TerminalOf(CURRENT_SCREEN);
118 if (termp != 0) {
micky3879b9f5e72025-07-08 18:04:53 -0400119 result = TerminalType(termp).term_names;
Steve Kondikae271bc2015-11-15 02:50:53 +0100120 }
121 }
122#else
123 if (cur_term != 0) {
micky3879b9f5e72025-07-08 18:04:53 -0400124 result = TerminalType(cur_term).term_names;
Steve Kondikae271bc2015-11-15 02:50:53 +0100125 }
126#endif
127 return result;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530128}
Steve Kondikae271bc2015-11-15 02:50:53 +0100129
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530130NCURSES_EXPORT(int *)
Steve Kondikae271bc2015-11-15 02:50:53 +0100131_nc_ptr_Lines(SCREEN *sp)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530132{
Steve Kondikae271bc2015-11-15 02:50:53 +0100133 return ptrLines(sp);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530134}
Steve Kondikae271bc2015-11-15 02:50:53 +0100135
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530136NCURSES_EXPORT(int)
137NCURSES_PUBLIC_VAR(LINES) (void)
138{
Steve Kondikae271bc2015-11-15 02:50:53 +0100139 return *_nc_ptr_Lines(CURRENT_SCREEN);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530140}
Steve Kondikae271bc2015-11-15 02:50:53 +0100141
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530142NCURSES_EXPORT(int *)
Steve Kondikae271bc2015-11-15 02:50:53 +0100143_nc_ptr_Cols(SCREEN *sp)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530144{
Steve Kondikae271bc2015-11-15 02:50:53 +0100145 return ptrCols(sp);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530146}
Steve Kondikae271bc2015-11-15 02:50:53 +0100147
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530148NCURSES_EXPORT(int)
149NCURSES_PUBLIC_VAR(COLS) (void)
150{
Steve Kondikae271bc2015-11-15 02:50:53 +0100151 return *_nc_ptr_Cols(CURRENT_SCREEN);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530152}
Steve Kondikae271bc2015-11-15 02:50:53 +0100153
154NCURSES_EXPORT(int *)
155_nc_ptr_Tabsize(SCREEN *sp)
156{
157 return ptrTabsize(sp);
158}
159
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530160NCURSES_EXPORT(int)
161NCURSES_PUBLIC_VAR(TABSIZE) (void)
162{
Steve Kondikae271bc2015-11-15 02:50:53 +0100163 return *_nc_ptr_Tabsize(CURRENT_SCREEN);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530164}
165#else
166NCURSES_EXPORT_VAR(char) ttytype[NAMESIZE] = "";
167NCURSES_EXPORT_VAR(int) LINES = 0;
168NCURSES_EXPORT_VAR(int) COLS = 0;
Steve Kondikae271bc2015-11-15 02:50:53 +0100169NCURSES_EXPORT_VAR(int) TABSIZE = 8;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530170#endif
171
172#if NCURSES_EXT_FUNCS
173NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +0100174NCURSES_SP_NAME(set_tabsize) (NCURSES_SP_DCLx int value)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530175{
176 int code = OK;
micky3879b9f5e72025-07-08 18:04:53 -0400177 if (value <= 0) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530178 code = ERR;
micky3879b9f5e72025-07-08 18:04:53 -0400179 } else {
180#if USE_REENTRANT
181 if (SP_PARM) {
182 SP_PARM->_TABSIZE = value;
183 } else {
184 code = ERR;
185 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530186#else
micky3879b9f5e72025-07-08 18:04:53 -0400187 (void) SP_PARM;
188 TABSIZE = value;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530189#endif
micky3879b9f5e72025-07-08 18:04:53 -0400190 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530191 return code;
192}
Steve Kondikae271bc2015-11-15 02:50:53 +0100193
194#if NCURSES_SP_FUNCS
195NCURSES_EXPORT(int)
196set_tabsize(int value)
197{
198 return NCURSES_SP_NAME(set_tabsize) (CURRENT_SCREEN, value);
199}
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530200#endif
Steve Kondikae271bc2015-11-15 02:50:53 +0100201#endif /* NCURSES_EXT_FUNCS */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530202
203#if USE_SIGWINCH
204/*
205 * If we have a pending SIGWINCH, set the flag in each screen.
206 */
207NCURSES_EXPORT(int)
208_nc_handle_sigwinch(SCREEN *sp)
209{
210 SCREEN *scan;
211
212 if (_nc_globals.have_sigwinch) {
213 _nc_globals.have_sigwinch = 0;
214
215 for (each_screen(scan)) {
216 scan->_sig_winch = TRUE;
217 }
218 }
219
220 return (sp ? sp->_sig_winch : 0);
221}
222
223#endif
224
225NCURSES_EXPORT(void)
Steve Kondikae271bc2015-11-15 02:50:53 +0100226NCURSES_SP_NAME(use_env) (NCURSES_SP_DCLx bool f)
227{
micky3879b9f5e72025-07-08 18:04:53 -0400228 START_TRACE();
Steve Kondikae271bc2015-11-15 02:50:53 +0100229 T((T_CALLED("use_env(%p,%d)"), (void *) SP_PARM, (int) f));
230#if NCURSES_SP_FUNCS
Steve Kondikae271bc2015-11-15 02:50:53 +0100231 if (IsPreScreen(SP_PARM)) {
232 SP_PARM->_use_env = f;
233 }
234#else
235 _nc_prescreen.use_env = f;
236#endif
237 returnVoid;
238}
239
240NCURSES_EXPORT(void)
241NCURSES_SP_NAME(use_tioctl) (NCURSES_SP_DCLx bool f)
242{
micky3879b9f5e72025-07-08 18:04:53 -0400243 START_TRACE();
Steve Kondikae271bc2015-11-15 02:50:53 +0100244 T((T_CALLED("use_tioctl(%p,%d)"), (void *) SP_PARM, (int) f));
245#if NCURSES_SP_FUNCS
Steve Kondikae271bc2015-11-15 02:50:53 +0100246 if (IsPreScreen(SP_PARM)) {
micky3879b9f5e72025-07-08 18:04:53 -0400247 SP_PARM->use_tioctl = f;
Steve Kondikae271bc2015-11-15 02:50:53 +0100248 }
249#else
250 _nc_prescreen.use_tioctl = f;
251#endif
252 returnVoid;
253}
254
255#if NCURSES_SP_FUNCS
256NCURSES_EXPORT(void)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530257use_env(bool f)
258{
Steve Kondikae271bc2015-11-15 02:50:53 +0100259 START_TRACE();
micky3879b9f5e72025-07-08 18:04:53 -0400260 T((T_CALLED("use_env(%d)"), (int) f));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530261 _nc_prescreen.use_env = f;
262 returnVoid;
263}
264
265NCURSES_EXPORT(void)
Steve Kondikae271bc2015-11-15 02:50:53 +0100266use_tioctl(bool f)
267{
Steve Kondikae271bc2015-11-15 02:50:53 +0100268 START_TRACE();
micky3879b9f5e72025-07-08 18:04:53 -0400269 T((T_CALLED("use_tioctl(%d)"), (int) f));
Steve Kondikae271bc2015-11-15 02:50:53 +0100270 _nc_prescreen.use_tioctl = f;
271 returnVoid;
272}
273#endif
274
micky3879b9f5e72025-07-08 18:04:53 -0400275#if !(defined(USE_TERM_DRIVER) || defined(EXP_WIN32_DRIVER))
276static void
277_nc_default_screensize(TERMINAL *termp, int *linep, int *colp)
278{
279 /* if we can't get dynamic info about the size, use static */
280 if (*linep <= 0) {
281 *linep = (int) lines;
282 }
283 if (*colp <= 0) {
284 *colp = (int) columns;
285 }
286
287 /* the ultimate fallback, assume fixed 24x80 size */
288 if (*linep <= 0) {
289 *linep = 24;
290 }
291 if (*colp <= 0) {
292 *colp = 80;
293 }
294}
295
296#if defined(USE_CHECK_SIZE) && defined(user6) && defined(user7)
297static const char *
298skip_csi(const char *value)
299{
300 if (UChar(*value) == CSI_CHR) {
301 ++value;
302 } else if (*value == ESC_CHR && value[1] == L_BLOCK) {
303 value += 2;
304 }
305 return value;
306}
307
308static bool
309is_expected(const char *value, const char *expected)
310{
311 bool result = FALSE;
312 if (VALID_STRING(value)) {
313 const char *skipped = skip_csi(value);
314 if (skipped != value) {
315 if (!strcmp(skipped, expected))
316 result = TRUE;
317 }
318 }
319 return result;
320}
321
322static bool
323get_position(TERMINAL *termp, int fd, int *row, int *col)
324{
325 bool result = FALSE;
326 size_t need = strlen(user7);
327 int have;
328
329 have = (int) write(fd, user7, need);
330
331 if (have == (int) need) {
332 int y, x;
333 char buf[20];
334 char *s;
335 char cc;
336 const char *skipped;
337 int scanned;
338
339 s = memset(buf, '\0', sizeof(buf));
340 do {
341 size_t ask = (sizeof(buf) - 1 - (size_t) (s - buf));
342 int got = (int) read(fd, s, ask);
343 if (got == 0)
344 break;
345 s += got;
346 *s = '\0';
347 } while (strchr(buf, 'R') == NULL && (size_t) (s + 1 - buf) < sizeof(buf));
348 T(("CPR response %s", _nc_visbuf(buf)));
349 skipped = skip_csi(buf);
350 cc = '\0';
351 if (skipped != buf
352 && *skipped != '\0'
353 && (scanned = sscanf(skip_csi(buf), "%d;%d%c", &y, &x, &cc)) == 3
354 && (cc == 'R')) {
355 *row = y;
356 *col = x;
357 result = TRUE;
358 }
359 }
360 T(("get_position %s %d,%d", result ? "OK" : "ERR", *row, *col));
361 return result;
362}
363
364static bool
365set_position(NCURSES_SP_DCLx TERMINAL *termp, int row, int col)
366{
367 bool result;
368 char *actual = TIPARM_2(cursor_address, row, col);
369 T((T_CALLED("set_position %d,%d)"), row, col));
370#if NCURSES_SP_FUNCS
371 result = (NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "set_position",
372 actual) == OK);
373 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
374#else
375 /* This does not support padding because without sp-funcs, we have only
376 * the interface using stdio, but we are not guaranteed that Filedes
377 * is the same as fileno(stdout).
378 */
379 result = FALSE;
380 if (actual != NULL) {
381 size_t want = strlen(actual);
382 int have = (int) write(termp->Filedes, actual, want);
383 result = ((int) want == have);
384 }
385#endif
386 returnBool(result);
387}
388
389/*
390 * This is a little more complicated than one might expect, because we do this
391 * before setting up the terminal modes, etc., and cannot use the timeout or
392 * buffering functions.
393 *
394 * We check if the terminal description has the ECMA-48 CPR (cursor position
395 * report) in u7 and the response in u6. The two variations of is_expected()
396 * cover the termcap style and terminfo style, and are equivalent as far as we
397 * are concerned. For analyzing the response, we wait (a short time) for 'R'
398 * to be echoed, and then check if we received two integers in the response.
399 *
400 * In principle, this could run on "any" ECMA-48 terminal, but in practice,
401 * there is a scenario using GNU screen where it uses ncurses with a partially
402 * configured pseudo-terminal, and the CPR response goes to the wrong place.
403 * So we do a simple check to exclude pseudo-terminals.
404 */
405static void
406_nc_check_screensize(SCREEN *sp, TERMINAL *termp, int *linep, int *colp)
407{
408 int fd = termp->Filedes;
409 TTY saved;
410 const char *name = NULL;
411
412 if (IsRealTty(fd, name)
413 && VALID_STRING(cursor_address)
414 && is_expected(user7, "6n")
415 && (is_expected(user6, "%i%d;%dR") ||
416 is_expected(user6, "%i%p1%d;%p2%dR"))
417 && GET_TTY(fd, &saved) == OK) {
418 int current_y = -1, current_x = -1;
419 int updated_y = -1, updated_x = -1;
420 TTY alter = saved;
421
422#if NCURSES_SP_FUNCS
423 if (sp == NULL) {
424 sp = new_prescr();
425 sp->_term = termp;
426 NCURSES_SP_NAME(baudrate) (NCURSES_SP_ARG);
427 }
428#else
429 (void) sp;
430#endif
431
432 T(("trying CPR (u7/u6) with %s", name));
433 alter.c_lflag &= (unsigned) ~(ECHO | ICANON | ISIG | IEXTEN);
434 alter.c_iflag &= (unsigned) ~(IXON | BRKINT | PARMRK);
435 alter.c_cc[VMIN] = 0;
436 alter.c_cc[VTIME] = 1;
437 SET_TTY(fd, &alter);
438
439 if (get_position(termp, fd, &current_y, &current_x)
440 && set_position(NCURSES_SP_ARGx termp, 9999, 9999)
441 && get_position(termp, fd, &updated_y, &updated_x)) {
442 *linep = updated_y;
443 *colp = updated_x;
444 set_position(NCURSES_SP_ARGx termp, current_y, current_x);
445 }
446 /* restore tty modes */
447 SET_TTY(fd, &saved);
448 } else {
449 T(("NOT trying CPR with fd %d (%s): %s",
450 fd, NonNull(name), NC_ISATTY(fd) ? "tty" : "not a tty"));
451 }
452
453 _nc_default_screensize(termp, linep, colp);
454}
455#else /* !USE_CHECK_SIZE */
456#define _nc_check_screensize(sp, termp, linep, colp) /* nothing */
457#endif
458#endif /* !(defined(USE_TERM_DRIVER) || defined(EXP_WIN32_DRIVER)) */
459
Steve Kondikae271bc2015-11-15 02:50:53 +0100460NCURSES_EXPORT(void)
461_nc_get_screensize(SCREEN *sp,
462#ifdef USE_TERM_DRIVER
micky3879b9f5e72025-07-08 18:04:53 -0400463 TERMINAL *termp,
Steve Kondikae271bc2015-11-15 02:50:53 +0100464#endif
465 int *linep, int *colp)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530466/* Obtain lines/columns values from the environment and/or terminfo entry */
467{
Steve Kondikae271bc2015-11-15 02:50:53 +0100468#ifdef USE_TERM_DRIVER
469 TERMINAL_CONTROL_BLOCK *TCB;
470 int my_tabsize;
471
472 assert(termp != 0 && linep != 0 && colp != 0);
473 TCB = (TERMINAL_CONTROL_BLOCK *) termp;
474
475 my_tabsize = TCB->info.tabsize;
476 TCB->drv->td_size(TCB, linep, colp);
477
478#if USE_REENTRANT
479 if (sp != 0) {
480 sp->_TABSIZE = my_tabsize;
481 }
482#else
483 (void) sp;
484 TABSIZE = my_tabsize;
485#endif
486 T(("TABSIZE = %d", my_tabsize));
487#else /* !USE_TERM_DRIVER */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530488 TERMINAL *termp = cur_term;
489 int my_tabsize;
micky3879b9f5e72025-07-08 18:04:53 -0400490 bool useEnv = _nc_prescreen.use_env;
491 bool useTioctl = _nc_prescreen.use_tioctl;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530492
micky3879b9f5e72025-07-08 18:04:53 -0400493 T((T_CALLED("_nc_get_screensize (%p)"), (void *) sp));
494#ifdef EXP_WIN32_DRIVER
495 /* If we are here, then Windows console is used in terminfo mode.
496 We need to figure out the size using the console API
497 */
498 _nc_console_size(linep, colp);
499 T(("screen size: winconsole lines = %d columns = %d", *linep, *colp));
500#else
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530501 /* figure out the size of the screen */
502 T(("screen size: terminfo lines = %d columns = %d", lines, columns));
503
Steve Kondikae271bc2015-11-15 02:50:53 +0100504 *linep = (int) lines;
505 *colp = (int) columns;
micky3879b9f5e72025-07-08 18:04:53 -0400506#endif
Steve Kondikae271bc2015-11-15 02:50:53 +0100507
micky3879b9f5e72025-07-08 18:04:53 -0400508#if NCURSES_SP_FUNCS
509 if (sp) {
510 useEnv = sp->_use_env;
511 useTioctl = sp->use_tioctl;
512 }
513#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530514
micky3879b9f5e72025-07-08 18:04:53 -0400515 T(("useEnv:%d useTioctl:%d", useEnv, useTioctl));
516 if (useEnv || useTioctl) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530517#ifdef __EMX__
Steve Kondikae271bc2015-11-15 02:50:53 +0100518 {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530519 int screendata[2];
520 _scrsize(screendata);
521 *colp = screendata[0];
Steve Kondikae271bc2015-11-15 02:50:53 +0100522 *linep = ((sp != 0 && sp->_filtered)
523 ? 1
524 : screendata[1]);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530525 T(("EMX screen size: environment LINES = %d COLUMNS = %d",
526 *linep, *colp));
527 }
528#endif
529#if HAVE_SIZECHANGE
Steve Kondikae271bc2015-11-15 02:50:53 +0100530 /* try asking the OS */
531 if (NC_ISATTY(cur_term->Filedes)) {
532 STRUCT_WINSIZE size;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530533
Steve Kondikae271bc2015-11-15 02:50:53 +0100534 errno = 0;
535 do {
536 if (ioctl(cur_term->Filedes, IOCTL_WINSIZE, &size) >= 0) {
537 *linep = ((sp != 0 && sp->_filtered)
538 ? 1
539 : WINSIZE_ROWS(size));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530540 *colp = WINSIZE_COLS(size);
Steve Kondikae271bc2015-11-15 02:50:53 +0100541 T(("SYS screen size: environment LINES = %d COLUMNS = %d",
542 *linep, *colp));
543 break;
544 }
545 } while
546 (errno == EINTR);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530547 }
548#endif /* HAVE_SIZECHANGE */
549
micky3879b9f5e72025-07-08 18:04:53 -0400550 if (useEnv) {
551 int value;
552
553 if (useTioctl) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100554 /*
555 * If environment variables are used, update them.
556 */
557 if ((sp == 0 || !sp->_filtered) && _nc_getenv_num("LINES") > 0) {
558 _nc_setenv_num("LINES", *linep);
559 }
560 if (_nc_getenv_num("COLUMNS") > 0) {
561 _nc_setenv_num("COLUMNS", *colp);
562 }
563 }
564
565 /*
566 * Finally, look for environment variables.
567 *
568 * Solaris lets users override either dimension with an environment
569 * variable.
570 */
571 if ((value = _nc_getenv_num("LINES")) > 0) {
572 *linep = value;
573 T(("screen size: environment LINES = %d", *linep));
574 }
575 if ((value = _nc_getenv_num("COLUMNS")) > 0) {
576 *colp = value;
577 T(("screen size: environment COLUMNS = %d", *colp));
578 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100579
micky3879b9f5e72025-07-08 18:04:53 -0400580 _nc_default_screensize(termp, linep, colp);
581 } else {
582 _nc_check_screensize(sp, termp, linep, colp);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530583 }
584
585 /*
586 * Put the derived values back in the screen-size caps, so
587 * tigetnum() and tgetnum() will do the right thing.
588 */
micky3879b9f5e72025-07-08 18:04:53 -0400589 lines = (NCURSES_INT2) (*linep);
590 columns = (NCURSES_INT2) (*colp);
591#if NCURSES_EXT_NUMBERS
592#define OldNumber(termp,name) \
593 (termp)->type.Numbers[(&name - (termp)->type2.Numbers)]
594 OldNumber(termp, lines) = (short) (*linep);
595 OldNumber(termp, columns) = (short) (*colp);
596#endif
597 } else {
598 _nc_check_screensize(sp, termp, linep, colp);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530599 }
600
601 T(("screen size is %dx%d", *linep, *colp));
602
603 if (VALID_NUMERIC(init_tabs))
604 my_tabsize = (int) init_tabs;
605 else
606 my_tabsize = 8;
607
608#if USE_REENTRANT
609 if (sp != 0)
610 sp->_TABSIZE = my_tabsize;
611#else
612 TABSIZE = my_tabsize;
613#endif
614 T(("TABSIZE = %d", TABSIZE));
micky3879b9f5e72025-07-08 18:04:53 -0400615 returnVoid;
Steve Kondikae271bc2015-11-15 02:50:53 +0100616#endif /* USE_TERM_DRIVER */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530617}
618
619#if USE_SIZECHANGE
620NCURSES_EXPORT(void)
621_nc_update_screensize(SCREEN *sp)
622{
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530623 int new_lines;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530624 int new_cols;
625
Steve Kondikae271bc2015-11-15 02:50:53 +0100626#ifdef USE_TERM_DRIVER
627 int old_lines;
628 int old_cols;
629
630 assert(sp != 0);
631
632 CallDriver_2(sp, td_getsize, &old_lines, &old_cols);
633
634#else
635 TERMINAL *termp = cur_term;
636 int old_lines = lines;
637 int old_cols = columns;
638#endif
639
micky3879b9f5e72025-07-08 18:04:53 -0400640 if (sp != 0) {
641 TINFO_GET_SIZE(sp, sp->_term, &new_lines, &new_cols);
642 /*
643 * See is_term_resized() and resizeterm().
644 * We're doing it this way because those functions belong to the upper
645 * ncurses library, while this resides in the lower terminfo library.
646 */
647 if (sp->_resize != 0) {
648 if ((new_lines != old_lines) || (new_cols != old_cols)) {
649 sp->_resize(NCURSES_SP_ARGx new_lines, new_cols);
650 } else if (sp->_sig_winch && (sp->_ungetch != 0)) {
651 sp->_ungetch(SP_PARM, KEY_RESIZE); /* so application can know this */
652 }
653 sp->_sig_winch = FALSE;
Steve Kondikae271bc2015-11-15 02:50:53 +0100654 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530655 }
656}
micky3879b9f5e72025-07-08 18:04:53 -0400657#endif /* USE_SIZECHANGE */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530658
659/****************************************************************************
660 *
661 * Terminal setup
662 *
663 ****************************************************************************/
664
Steve Kondikae271bc2015-11-15 02:50:53 +0100665#if NCURSES_USE_DATABASE || NCURSES_USE_TERMCAP
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530666/*
667 * Return 1 if entry found, 0 if not found, -1 if database not accessible,
668 * just like tgetent().
669 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100670int
micky3879b9f5e72025-07-08 18:04:53 -0400671_nc_setup_tinfo(const char *const tn, TERMTYPE2 *const tp)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530672{
673 char filename[PATH_MAX];
micky3879b9f5e72025-07-08 18:04:53 -0400674 int status = _nc_read_entry2(tn, filename, tp);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530675
676 /*
677 * If we have an entry, force all of the cancelled strings to null
678 * pointers so we don't have to test them in the rest of the library.
679 * (The terminfo compiler bypasses this logic, since it must know if
680 * a string is cancelled, for merging entries).
681 */
682 if (status == TGETENT_YES) {
683 unsigned n;
micky3879b9f5e72025-07-08 18:04:53 -0400684 T(("_nc_setup_tinfo - resetting invalid booleans/strings"));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530685 for_each_boolean(n, tp) {
686 if (!VALID_BOOLEAN(tp->Booleans[n]))
687 tp->Booleans[n] = FALSE;
688 }
689 for_each_string(n, tp) {
690 if (tp->Strings[n] == CANCELLED_STRING)
691 tp->Strings[n] = ABSENT_STRING;
692 }
693 }
694 return (status);
695}
696#endif
697
698/*
micky3879b9f5e72025-07-08 18:04:53 -0400699 * Take the real command character out of the CC environment variable
700 * and substitute it in for the prototype given in 'command_character'.
701 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100702void
micky3879b9f5e72025-07-08 18:04:53 -0400703_nc_tinfo_cmdch(TERMINAL *termp, int proto)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530704{
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530705 char *tmp;
706
Steve Kondikae271bc2015-11-15 02:50:53 +0100707 /*
708 * Only use the character if the string is a single character,
709 * since it is fairly common for developers to set the C compiler
710 * name as an environment variable - using the same symbol.
711 */
712 if ((tmp = getenv("CC")) != 0 && strlen(tmp) == 1) {
micky3879b9f5e72025-07-08 18:04:53 -0400713 unsigned i;
714 char CC = *tmp;
715
Steve Kondikae271bc2015-11-15 02:50:53 +0100716 for_each_string(i, &(termp->type)) {
micky3879b9f5e72025-07-08 18:04:53 -0400717 tmp = termp->type.Strings[i];
718 if (VALID_STRING(tmp)) {
719 for (; *tmp; ++tmp) {
720 if (UChar(*tmp) == proto)
721 *tmp = CC;
722 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530723 }
724 }
725 }
726}
727
728/*
729 * Find the locale which is in effect.
730 */
731NCURSES_EXPORT(char *)
732_nc_get_locale(void)
733{
734 char *env;
735#if HAVE_LOCALE_H
736 /*
737 * This is preferable to using getenv() since it ensures that we are using
738 * the locale which was actually initialized by the application.
739 */
740 env = setlocale(LC_CTYPE, 0);
741#else
micky3879b9f5e72025-07-08 18:04:53 -0400742 if (((env = getenv("LANG")) != 0 && *env != '\0')
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530743 || ((env = getenv("LC_CTYPE")) != 0 && *env != '\0')
micky3879b9f5e72025-07-08 18:04:53 -0400744 || ((env = getenv("LC_ALL")) != 0 && *env != '\0')) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530745 ;
746 }
747#endif
748 T(("_nc_get_locale %s", _nc_visbuf(env)));
749 return env;
750}
751
752/*
753 * Check if we are running in a UTF-8 locale.
754 */
755NCURSES_EXPORT(int)
756_nc_unicode_locale(void)
757{
micky3879b9f5e72025-07-08 18:04:53 -0400758 static bool initialized = FALSE;
759 static int result = 0;
760
761 if (!initialized) {
762#if defined(_NC_WINDOWS) && USE_WIDEC_SUPPORT
763 result = 1;
Steve Kondikae271bc2015-11-15 02:50:53 +0100764#elif HAVE_LANGINFO_CODESET
micky3879b9f5e72025-07-08 18:04:53 -0400765 char *env = nl_langinfo(CODESET);
766 result = !strcmp(env, "UTF-8");
767 T(("_nc_unicode_locale(%s) ->%d", env, result));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530768#else
micky3879b9f5e72025-07-08 18:04:53 -0400769 char *env = _nc_get_locale();
770 if (env != 0) {
771 if (strstr(env, ".UTF-8") != 0) {
772 result = 1;
773 T(("_nc_unicode_locale(%s) ->%d", env, result));
774 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530775 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530776#endif
micky3879b9f5e72025-07-08 18:04:53 -0400777 initialized = TRUE;
778 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530779 return result;
780}
781
782#define CONTROL_N(s) ((s) != 0 && strstr(s, "\016") != 0)
783#define CONTROL_O(s) ((s) != 0 && strstr(s, "\017") != 0)
784
785/*
786 * Check for known broken cases where a UTF-8 locale breaks the alternate
787 * character set.
788 */
789NCURSES_EXPORT(int)
micky3879b9f5e72025-07-08 18:04:53 -0400790_nc_locale_breaks_acs(TERMINAL *termp)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530791{
Steve Kondikae271bc2015-11-15 02:50:53 +0100792 const char *env_name = "NCURSES_NO_UTF8_ACS";
793 const char *env;
794 int value;
795 int result = 0;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530796
Steve Kondikae271bc2015-11-15 02:50:53 +0100797 T((T_CALLED("_nc_locale_breaks_acs:%d"), result));
798 if (getenv(env_name) != 0) {
799 result = _nc_getenv_num(env_name);
800 } else if ((value = tigetnum("U8")) >= 0) {
801 result = value; /* use extension feature */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530802 } else if ((env = getenv("TERM")) != 0) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100803 if (strstr(env, "linux")) {
804 result = 1; /* always broken */
805 } else if (strstr(env, "screen") != 0
806 && ((env = getenv("TERMCAP")) != 0
807 && strstr(env, "screen") != 0)
808 && strstr(env, "hhII00") != 0) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530809 if (CONTROL_N(enter_alt_charset_mode) ||
810 CONTROL_O(enter_alt_charset_mode) ||
811 CONTROL_N(set_attributes) ||
Steve Kondikae271bc2015-11-15 02:50:53 +0100812 CONTROL_O(set_attributes)) {
813 result = 1;
814 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530815 }
816 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100817 returnCode(result);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530818}
819
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530820NCURSES_EXPORT(int)
micky3879b9f5e72025-07-08 18:04:53 -0400821TINFO_SETUP_TERM(TERMINAL **tp,
822 const char *tname,
Steve Kondikae271bc2015-11-15 02:50:53 +0100823 int Filedes,
824 int *errret,
825 int reuse)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530826{
Steve Kondikae271bc2015-11-15 02:50:53 +0100827#ifdef USE_TERM_DRIVER
828 TERMINAL_CONTROL_BLOCK *TCB = 0;
Steve Kondikae271bc2015-11-15 02:50:53 +0100829#endif
830 TERMINAL *termp;
831 SCREEN *sp = 0;
micky3879b9f5e72025-07-08 18:04:53 -0400832 char *myname;
Steve Kondikae271bc2015-11-15 02:50:53 +0100833 int code = ERR;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530834
835 START_TRACE();
Steve Kondikae271bc2015-11-15 02:50:53 +0100836
837#ifdef USE_TERM_DRIVER
838 T((T_CALLED("_nc_setupterm_ex(%p,%s,%d,%p)"),
839 (void *) tp, _nc_visbuf(tname), Filedes, (void *) errret));
840
841 if (tp == 0) {
842 ret_error0(TGETENT_ERR,
843 "Invalid parameter, internal error.\n");
844 } else
845 termp = *tp;
846#else
847 termp = cur_term;
848 T((T_CALLED("setupterm(%s,%d,%p)"), _nc_visbuf(tname), Filedes, (void *) errret));
849#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530850
851 if (tname == 0) {
852 tname = getenv("TERM");
micky3879b9f5e72025-07-08 18:04:53 -0400853#if defined(EXP_WIN32_DRIVER)
854 if (!VALID_TERM_ENV(tname, NO_TERMINAL)) {
855 T(("Failure with TERM=%s", NonNull(tname)));
856 ret_error0(TGETENT_ERR, "TERM environment variable not set.\n");
857 }
858#elif defined(USE_TERM_DRIVER)
859 if (!NonEmpty(tname))
Steve Kondikae271bc2015-11-15 02:50:53 +0100860 tname = "unknown";
861#else
micky3879b9f5e72025-07-08 18:04:53 -0400862 if (!NonEmpty(tname)) {
863 T(("Failure with TERM=%s", NonNull(tname)));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530864 ret_error0(TGETENT_ERR, "TERM environment variable not set.\n");
865 }
micky3879b9f5e72025-07-08 18:04:53 -0400866#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530867 }
micky3879b9f5e72025-07-08 18:04:53 -0400868 myname = strdup(tname);
869 if (myname == NULL || strlen(myname) > MAX_NAME_SIZE) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530870 ret_error(TGETENT_ERR,
micky3879b9f5e72025-07-08 18:04:53 -0400871 "TERM environment must be 1..%d characters.\n",
872 MAX_NAME_SIZE,
873 free(myname));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530874 }
875
micky3879b9f5e72025-07-08 18:04:53 -0400876 T(("your terminal name is %s", myname));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530877
878 /*
879 * Allow output redirection. This is what SVr3 does. If stdout is
880 * directed to a file, screen updates go to standard error.
881 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100882 if (Filedes == STDOUT_FILENO && !NC_ISATTY(Filedes))
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530883 Filedes = STDERR_FILENO;
micky3879b9f5e72025-07-08 18:04:53 -0400884#if defined(EXP_WIN32_DRIVER)
885 if (Filedes != STDERR_FILENO && NC_ISATTY(Filedes))
886 _setmode(Filedes, _O_BINARY);
887#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530888
889 /*
890 * Check if we have already initialized to use this terminal. If so, we
891 * do not need to re-read the terminfo entry, or obtain TTY settings.
892 *
893 * This is an improvement on SVr4 curses. If an application mixes curses
894 * and termcap calls, it may call both initscr and tgetent. This is not
895 * really a good thing to do, but can happen if someone tries using ncurses
896 * with the readline library. The problem we are fixing is that when
897 * tgetent calls setupterm, the resulting Ottyb struct in cur_term is
898 * zeroed. A subsequent call to endwin uses the zeroed terminal settings
899 * rather than the ones saved in initscr. So we check if cur_term appears
900 * to contain terminal settings for the same output file as our current
901 * call - and copy those terminal settings. (SVr4 curses does not do this,
902 * however applications that are working around the problem will still work
903 * properly with this feature).
904 */
905 if (reuse
Steve Kondikae271bc2015-11-15 02:50:53 +0100906 && (termp != 0)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530907 && termp->Filedes == Filedes
908 && termp->_termname != 0
micky3879b9f5e72025-07-08 18:04:53 -0400909 && !strcmp(termp->_termname, myname)
910 && _nc_name_match(TerminalType(termp).term_names, myname, "|")) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530911 T(("reusing existing terminal information and mode-settings"));
Steve Kondikae271bc2015-11-15 02:50:53 +0100912 code = OK;
913#ifdef USE_TERM_DRIVER
914 TCB = (TERMINAL_CONTROL_BLOCK *) termp;
915#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530916 } else {
Steve Kondikae271bc2015-11-15 02:50:53 +0100917#ifdef USE_TERM_DRIVER
918 TERMINAL_CONTROL_BLOCK *my_tcb;
micky3879b9f5e72025-07-08 18:04:53 -0400919 termp = 0;
920 if ((my_tcb = typeCalloc(TERMINAL_CONTROL_BLOCK, 1)) != 0)
921 termp = &(my_tcb->term);
Steve Kondikae271bc2015-11-15 02:50:53 +0100922#else
micky3879b9f5e72025-07-08 18:04:53 -0400923 int status;
924
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530925 termp = typeCalloc(TERMINAL, 1);
Steve Kondikae271bc2015-11-15 02:50:53 +0100926#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530927 if (termp == 0) {
micky3879b9f5e72025-07-08 18:04:53 -0400928 ret_error1(TGETENT_ERR,
929 "Not enough memory to create terminal structure.\n",
930 myname, free(myname));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530931 }
micky3879b9f5e72025-07-08 18:04:53 -0400932 ++_nc_globals.terminal_count;
933#if HAVE_SYSCONF
934 {
935 long limit;
936#ifdef LINE_MAX
937 limit = LINE_MAX;
938#else
939 limit = _nc_globals.getstr_limit;
940#endif
941#ifdef _SC_LINE_MAX
942 if (limit < sysconf(_SC_LINE_MAX))
943 limit = sysconf(_SC_LINE_MAX);
944#endif
945 if (_nc_globals.getstr_limit < (int) limit)
946 _nc_globals.getstr_limit = (int) limit;
947 }
948#endif /* HAVE_SYSCONF */
949 T(("using %d for getstr limit", _nc_globals.getstr_limit));
950
Steve Kondikae271bc2015-11-15 02:50:53 +0100951#ifdef USE_TERM_DRIVER
952 INIT_TERM_DRIVER();
micky3879b9f5e72025-07-08 18:04:53 -0400953 /*
954 * _nc_get_driver() will call td_CanHandle() for each driver, and win_driver
955 * needs file descriptor to do the test, so set it before calling.
956 */
957 termp->Filedes = (short) Filedes;
Steve Kondikae271bc2015-11-15 02:50:53 +0100958 TCB = (TERMINAL_CONTROL_BLOCK *) termp;
micky3879b9f5e72025-07-08 18:04:53 -0400959 code = _nc_globals.term_driver(TCB, myname, errret);
Steve Kondikae271bc2015-11-15 02:50:53 +0100960 if (code == OK) {
micky3879b9f5e72025-07-08 18:04:53 -0400961 termp->_termname = strdup(myname);
Steve Kondikae271bc2015-11-15 02:50:53 +0100962 } else {
micky3879b9f5e72025-07-08 18:04:53 -0400963 ret_error1(errret ? *errret : TGETENT_ERR,
964 "Could not find any driver to handle terminal.\n",
965 myname, free(myname));
Steve Kondikae271bc2015-11-15 02:50:53 +0100966 }
967#else
968#if NCURSES_USE_DATABASE || NCURSES_USE_TERMCAP
micky3879b9f5e72025-07-08 18:04:53 -0400969 status = _nc_setup_tinfo(myname, &TerminalType(termp));
970 T(("_nc_setup_tinfo returns %d", status));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530971#else
micky3879b9f5e72025-07-08 18:04:53 -0400972 T(("no database available"));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530973 status = TGETENT_NO;
974#endif
975
976 /* try fallback list if entry on disk */
977 if (status != TGETENT_YES) {
micky3879b9f5e72025-07-08 18:04:53 -0400978 const TERMTYPE2 *fallback = _nc_fallback2(myname);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530979
980 if (fallback) {
micky3879b9f5e72025-07-08 18:04:53 -0400981 T(("found fallback entry"));
982 _nc_copy_termtype2(&(TerminalType(termp)), fallback);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530983 status = TGETENT_YES;
984 }
985 }
986
987 if (status != TGETENT_YES) {
988 del_curterm(termp);
989 if (status == TGETENT_ERR) {
micky3879b9f5e72025-07-08 18:04:53 -0400990 free(myname);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530991 ret_error0(status, "terminals database is inaccessible\n");
992 } else if (status == TGETENT_NO) {
micky3879b9f5e72025-07-08 18:04:53 -0400993 ret_error1(status, "unknown terminal type.\n",
994 myname, free(myname));
995 } else {
996 free(myname);
997 ret_error0(status, "unexpected return-code\n");
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530998 }
999 }
micky3879b9f5e72025-07-08 18:04:53 -04001000#if NCURSES_EXT_NUMBERS
1001 _nc_export_termtype2(&termp->type, &TerminalType(termp));
1002#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301003#if !USE_REENTRANT
micky3879b9f5e72025-07-08 18:04:53 -04001004 save_ttytype(termp);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301005#endif
1006
Steve Kondikae271bc2015-11-15 02:50:53 +01001007 termp->Filedes = (short) Filedes;
micky3879b9f5e72025-07-08 18:04:53 -04001008 termp->_termname = strdup(myname);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301009
1010 set_curterm(termp);
1011
micky3879b9f5e72025-07-08 18:04:53 -04001012 if (VALID_STRING(command_character))
Steve Kondikae271bc2015-11-15 02:50:53 +01001013 _nc_tinfo_cmdch(termp, UChar(*command_character));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301014
1015 /*
1016 * If an application calls setupterm() rather than initscr() or
1017 * newterm(), we will not have the def_prog_mode() call in
1018 * _nc_setupscreen(). Do it now anyway, so we can initialize the
micky3879b9f5e72025-07-08 18:04:53 -04001019 * baudrate. Also get the shell-mode so that erasechar() works.
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301020 */
Steve Kondikae271bc2015-11-15 02:50:53 +01001021 if (NC_ISATTY(Filedes)) {
micky3879b9f5e72025-07-08 18:04:53 -04001022 NCURSES_SP_NAME(def_shell_mode) (NCURSES_SP_ARG);
1023 NCURSES_SP_NAME(def_prog_mode) (NCURSES_SP_ARG);
1024 NCURSES_SP_NAME(baudrate) (NCURSES_SP_ARG);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301025 }
Steve Kondikae271bc2015-11-15 02:50:53 +01001026 code = OK;
1027#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301028 }
1029
Steve Kondikae271bc2015-11-15 02:50:53 +01001030#ifdef USE_TERM_DRIVER
1031 *tp = termp;
1032 NCURSES_SP_NAME(set_curterm) (sp, termp);
1033 TCB->drv->td_init(TCB);
1034#else
1035 sp = SP;
1036#endif
1037
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301038 /*
1039 * We should always check the screensize, just in case.
1040 */
Steve Kondikae271bc2015-11-15 02:50:53 +01001041 TINFO_GET_SIZE(sp, termp, ptrLines(sp), ptrCols(sp));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301042
1043 if (errret)
1044 *errret = TGETENT_YES;
1045
Steve Kondikae271bc2015-11-15 02:50:53 +01001046#ifndef USE_TERM_DRIVER
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301047 if (generic_type) {
Steve Kondikae271bc2015-11-15 02:50:53 +01001048 /*
1049 * BSD 4.3's termcap contains mis-typed "gn" for wy99. Do a sanity
1050 * check before giving up.
1051 */
1052 if ((VALID_STRING(cursor_address)
1053 || (VALID_STRING(cursor_down) && VALID_STRING(cursor_home)))
1054 && VALID_STRING(clear_screen)) {
micky3879b9f5e72025-07-08 18:04:53 -04001055 ret_error1(TGETENT_YES, "terminal is not really generic.\n",
1056 myname, free(myname));
Steve Kondikae271bc2015-11-15 02:50:53 +01001057 } else {
1058 del_curterm(termp);
micky3879b9f5e72025-07-08 18:04:53 -04001059 ret_error1(TGETENT_NO, "I need something more specific.\n",
1060 myname, free(myname));
Steve Kondikae271bc2015-11-15 02:50:53 +01001061 }
1062 } else if (hard_copy) {
micky3879b9f5e72025-07-08 18:04:53 -04001063 ret_error1(TGETENT_YES, "I can't handle hardcopy terminals.\n",
1064 myname, free(myname));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301065 }
Steve Kondikae271bc2015-11-15 02:50:53 +01001066#endif
micky3879b9f5e72025-07-08 18:04:53 -04001067 free(myname);
Steve Kondikae271bc2015-11-15 02:50:53 +01001068 returnCode(code);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301069}
1070
micky3879b9f5e72025-07-08 18:04:53 -04001071#ifdef USE_PTHREADS
1072/*
1073 * Returns a non-null pointer unless a new screen should be allocated because
1074 * no match was found in the pre-screen cache.
1075 */
1076NCURSES_EXPORT(SCREEN *)
1077_nc_find_prescr(void)
1078{
1079 SCREEN *result = 0;
1080 PRESCREEN_LIST *p;
1081 pthread_t id = GetThreadID();
1082 for (p = _nc_prescreen.allocated; p != 0; p = p->next) {
1083 if (p->id == id) {
1084 result = p->sp;
1085 break;
1086 }
1087 }
1088 return result;
1089}
1090
1091/*
1092 * Tells ncurses to forget that this thread was associated with the pre-screen
1093 * cache. It does not modify the pre-screen cache itself, since that is used
1094 * for creating new screens.
1095 */
1096NCURSES_EXPORT(void)
1097_nc_forget_prescr(void)
1098{
1099 PRESCREEN_LIST *p, *q;
1100 pthread_t id = GetThreadID();
1101 _nc_lock_global(screen);
1102 for (p = _nc_prescreen.allocated, q = 0; p != 0; q = p, p = p->next) {
1103 if (p->id == id) {
1104 if (q) {
1105 q->next = p->next;
1106 } else {
1107 _nc_prescreen.allocated = p->next;
1108 }
1109 free(p);
1110 break;
1111 }
1112 }
1113 _nc_unlock_global(screen);
1114}
1115#endif /* USE_PTHREADS */
1116
Steve Kondikae271bc2015-11-15 02:50:53 +01001117#if NCURSES_SP_FUNCS
1118/*
1119 * In case of handling multiple screens, we need to have a screen before
micky3879b9f5e72025-07-08 18:04:53 -04001120 * initialization in _nc_setupscreen takes place. This is to extend the
1121 * substitute for some of the stuff in _nc_prescreen, especially for slk and
1122 * ripoff handling which should be done per screen.
Steve Kondikae271bc2015-11-15 02:50:53 +01001123 */
1124NCURSES_EXPORT(SCREEN *)
1125new_prescr(void)
1126{
micky3879b9f5e72025-07-08 18:04:53 -04001127 SCREEN *sp;
Steve Kondikae271bc2015-11-15 02:50:53 +01001128
1129 START_TRACE();
1130 T((T_CALLED("new_prescr()")));
1131
micky3879b9f5e72025-07-08 18:04:53 -04001132 _nc_lock_global(screen);
1133 if ((sp = _nc_find_prescr()) == 0) {
Steve Kondikae271bc2015-11-15 02:50:53 +01001134 sp = _nc_alloc_screen_sp();
micky3879b9f5e72025-07-08 18:04:53 -04001135 T(("_nc_alloc_screen_sp %p", (void *) sp));
Steve Kondikae271bc2015-11-15 02:50:53 +01001136 if (sp != 0) {
micky3879b9f5e72025-07-08 18:04:53 -04001137#ifdef USE_PTHREADS
1138 PRESCREEN_LIST *p = typeCalloc(PRESCREEN_LIST, 1);
1139 if (p != 0) {
1140 p->id = GetThreadID();
1141 p->sp = sp;
1142 p->next = _nc_prescreen.allocated;
1143 _nc_prescreen.allocated = p;
1144 }
1145#else
1146 _nc_prescreen.allocated = sp;
1147#endif
Steve Kondikae271bc2015-11-15 02:50:53 +01001148 sp->rsp = sp->rippedoff;
1149 sp->_filtered = _nc_prescreen.filter_mode;
1150 sp->_use_env = _nc_prescreen.use_env;
1151#if NCURSES_NO_PADDING
1152 sp->_no_padding = _nc_prescreen._no_padding;
1153#endif
1154 sp->slk_format = 0;
1155 sp->_slk = 0;
1156 sp->_prescreen = TRUE;
1157 SP_PRE_INIT(sp);
1158#if USE_REENTRANT
1159 sp->_TABSIZE = _nc_prescreen._TABSIZE;
1160 sp->_ESCDELAY = _nc_prescreen._ESCDELAY;
1161#endif
1162 }
micky3879b9f5e72025-07-08 18:04:53 -04001163 } else {
1164 T(("_nc_alloc_screen_sp %p (reuse)", (void *) sp));
Steve Kondikae271bc2015-11-15 02:50:53 +01001165 }
micky3879b9f5e72025-07-08 18:04:53 -04001166 _nc_unlock_global(screen);
Steve Kondikae271bc2015-11-15 02:50:53 +01001167 returnSP(sp);
1168}
1169#endif
1170
1171#ifdef USE_TERM_DRIVER
1172/*
1173 * This entrypoint is called from tgetent() to allow a special case of reusing
1174 * the same TERMINAL data (see comment).
1175 */
1176NCURSES_EXPORT(int)
micky3879b9f5e72025-07-08 18:04:53 -04001177_nc_setupterm(const char *tname,
Steve Kondikae271bc2015-11-15 02:50:53 +01001178 int Filedes,
1179 int *errret,
1180 int reuse)
1181{
micky3879b9f5e72025-07-08 18:04:53 -04001182 int rc = ERR;
Steve Kondikae271bc2015-11-15 02:50:53 +01001183 TERMINAL *termp = 0;
micky3879b9f5e72025-07-08 18:04:53 -04001184
1185 _nc_init_pthreads();
1186 _nc_lock_global(prescreen);
1187 START_TRACE();
1188 if (TINFO_SETUP_TERM(&termp, tname, Filedes, errret, reuse) == OK) {
1189 _nc_forget_prescr();
1190 if (NCURSES_SP_NAME(set_curterm) (CURRENT_SCREEN_PRE, termp) != 0) {
1191 rc = OK;
1192 }
1193 }
1194 _nc_unlock_global(prescreen);
1195
1196 return rc;
Steve Kondikae271bc2015-11-15 02:50:53 +01001197}
1198#endif
1199
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301200/*
1201 * setupterm(termname, Filedes, errret)
1202 *
1203 * Find and read the appropriate object file for the terminal
1204 * Make cur_term point to the structure.
1205 */
1206NCURSES_EXPORT(int)
micky3879b9f5e72025-07-08 18:04:53 -04001207setupterm(const char *tname, int Filedes, int *errret)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301208{
Steve Kondikae271bc2015-11-15 02:50:53 +01001209 START_TRACE();
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301210 return _nc_setupterm(tname, Filedes, errret, FALSE);
1211}