xref: /openbsd/lib/libcurses/term.priv.h (revision c7ef0cfc)
1 /****************************************************************************
2  * Copyright 2021 Thomas E. Dickey                                          *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 
29 /*
30  * $Id: term.priv.h,v 1.1 2023/10/17 09:52:08 nicm Exp $
31  *
32  *	term.priv.h
33  *
34  *	Header file for terminfo library objects which are private to
35  *	the library.
36  *
37  */
38 
39 #ifndef _TERM_PRIV_H
40 #define _TERM_PRIV_H 1
41 /* *INDENT-OFF* */
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 #include <ncurses_cfg.h>
48 
49 #undef NCURSES_OPAQUE
50 #define NCURSES_INTERNALS 1
51 #define NCURSES_OPAQUE 0
52 
53 #include <limits.h>		/* PATH_MAX */
54 #include <signal.h>		/* sig_atomic_t */
55 #include <time.h>		/* time_t */
56 #include <term.h>		/* time_t */
57 
58 #ifdef USE_PTHREADS
59 #if USE_REENTRANT
60 #include <pthread.h>
61 #endif
62 #endif
63 
64 /*
65  * State of tparm().
66  */
67 #define STACKSIZE 20
68 
69 typedef struct {
70 	union {
71 		int	num;
72 		char *	str;
73 	} data;
74 	bool num_type;
75 } STACK_FRAME;
76 
77 #define NUM_VARS 26
78 
79 typedef struct {
80 	const char *	tparam_base;
81 
82 	STACK_FRAME	stack[STACKSIZE];
83 	int		stack_ptr;
84 
85 	char *		out_buff;
86 	size_t		out_size;
87 	size_t		out_used;
88 
89 	char *		fmt_buff;
90 	size_t		fmt_size;
91 
92 	int		static_vars[NUM_VARS];
93 #ifdef TRACE
94 	const char *	tname;
95 #endif
96 } TPARM_STATE;
97 
98 typedef struct {
99 	char *		text;
100 	size_t		size;
101 } TRACEBUF;
102 
103 typedef struct {
104 	const char *	name;
105 	char *		value;
106 } ITERATOR_VARS;
107 
108 /*
109  * Internals for term.h
110  */
111 typedef struct term {			/* describe an actual terminal */
112 	TERMTYPE	type;		/* terminal type description */
113 	short		Filedes;	/* file description being written to */
114 	TTY		Ottyb;		/* original state of the terminal */
115 	TTY		Nttyb;		/* current state of the terminal */
116 	int		_baudrate;	/* used to compute padding */
117 	char *		_termname;	/* used for termname() */
118 	TPARM_STATE	tparm_state;
119 #if NCURSES_EXT_COLORS
120 	TERMTYPE2	type2;		/* extended terminal type description */
121 #endif
122 #undef TERMINAL
123 } TERMINAL;
124 
125 /*
126  * Internals for soft-keys
127  */
128 typedef	struct {
129 	WINDOW *	win;		/* the window used in the hook      */
130 	int		line;		/* lines to take, < 0 => from bottom*/
131 	int		(*hook)(WINDOW *, int); /* callback for user	    */
132 } ripoff_t;
133 
134 /*
135  * Internals for tgetent
136  */
137 typedef struct {
138 	long		sequence;
139 	bool		last_used;
140 	char *		fix_sgr0;	/* this holds the filtered sgr0 string */
141 	char *		last_bufp;	/* help with fix_sgr0 leak */
142 	TERMINAL *	last_term;
143 } TGETENT_CACHE;
144 
145 #define TGETENT_MAX 4
146 
147 #include <term_entry.h>		/* dbdLAST */
148 
149 #ifdef USE_TERM_DRIVER
150 struct DriverTCB; /* Terminal Control Block forward declaration */
151 #endif
152 
153 /*
154  * Global data which is not specific to a screen.
155  */
156 typedef struct {
157 	SIG_ATOMIC_T	have_sigtstp;
158 	SIG_ATOMIC_T	have_sigwinch;
159 	SIG_ATOMIC_T	cleanup_nested;
160 
161 	bool		init_signals;
162 	bool		init_screen;
163 
164 	char *		comp_sourcename;
165 	char *		comp_termtype;
166 
167 	bool		have_tic_directory;
168 	bool		keep_tic_directory;
169 	const char *	tic_directory;
170 
171 	char *		dbi_list;
172 	int		dbi_size;
173 
174 	char *		first_name;
175 	char **		keyname_table;
176 	int		init_keyname;
177 
178 	int		slk_format;
179 
180 	int		getstr_limit;	/* getstr_limit based on POSIX LINE_MAX */
181 
182 	char *		safeprint_buf;
183 	size_t		safeprint_used;
184 
185 	TGETENT_CACHE	tgetent_cache[TGETENT_MAX];
186 	int		tgetent_index;
187 	long		tgetent_sequence;
188 	int		terminal_count;
189 
190 	char *		dbd_blob;	/* string-heap for dbd_list[] */
191 	char **		dbd_list;	/* distinct places to look for data */
192 	int		dbd_size;	/* length of dbd_list[] */
193 	time_t		dbd_time;	/* cache last updated */
194 	ITERATOR_VARS	dbd_vars[dbdLAST];
195 
196 #if HAVE_TSEARCH
197 	void *		cached_tparm;
198 	int		count_tparm;
199 #endif /* HAVE_TSEARCH */
200 
201 #ifdef USE_TERM_DRIVER
202 	int		(*term_driver)(struct DriverTCB*, const char*, int*);
203 #endif
204 
205 #define WINDOWLIST struct _win_list
206 
207 #ifndef USE_SP_WINDOWLIST
208 	WINDOWLIST *	_nc_windowlist;
209 #define WindowList(sp)	_nc_globals._nc_windowlist
210 #endif
211 
212 #if USE_HOME_TERMINFO
213 	char *		home_terminfo;
214 #endif
215 
216 #if !USE_SAFE_SPRINTF
217 	int		safeprint_cols;
218 	int		safeprint_rows;
219 #endif
220 
221 #ifdef USE_PTHREADS
222 	pthread_mutex_t	mutex_curses;
223 	pthread_mutex_t	mutex_prescreen;
224 	pthread_mutex_t	mutex_screen;
225 	pthread_mutex_t	mutex_update;
226 	pthread_mutex_t	mutex_tst_tracef;
227 	pthread_mutex_t	mutex_tracef;
228 	int		nested_tracef;
229 	int		use_pthreads;
230 #define _nc_use_pthreads	_nc_globals.use_pthreads
231 #if USE_PTHREADS_EINTR
232 	pthread_t	read_thread;	/* The reading thread */
233 #endif
234 #endif
235 #if USE_WIDEC_SUPPORT
236 	char		key_name[MB_LEN_MAX + 1];
237 #endif
238 
239 #ifdef TRACE
240 	bool		trace_opened;
241 	char		trace_fname[PATH_MAX];
242 	int		trace_level;
243 	FILE *		trace_fp;
244 	int		trace_fd;
245 
246 	char *		tracearg_buf;
247 	size_t		tracearg_used;
248 
249 	TRACEBUF *	tracebuf_ptr;
250 	size_t		tracebuf_used;
251 
252 	char		tracechr_buf[40];
253 
254 	char *		tracedmp_buf;
255 	size_t		tracedmp_used;
256 
257 	unsigned char *	tracetry_buf;
258 	size_t		tracetry_used;
259 
260 	char		traceatr_color_buf[2][80];
261 	int		traceatr_color_sel;
262 	int		traceatr_color_last;
263 #if !defined(USE_PTHREADS) && USE_REENTRANT
264 	int		nested_tracef;
265 #endif
266 #endif	/* TRACE */
267 
268 #if NO_LEAKS
269 	bool		leak_checking;
270 #endif
271 } NCURSES_GLOBALS;
272 
273 extern NCURSES_EXPORT_VAR(NCURSES_GLOBALS) _nc_globals;
274 
275 #define N_RIPS 5
276 
277 #ifdef USE_PTHREADS
278 typedef struct _prescreen_list {
279 	struct _prescreen_list *next;
280 	pthread_t	id;
281 	struct screen *	sp;
282 } PRESCREEN_LIST;
283 #endif
284 
285 /*
286  * Global data which can be swept up into a SCREEN when one is created.
287  * It may be modified before the next SCREEN is created.
288  */
289 typedef struct {
290 #ifdef USE_PTHREADS
291 	PRESCREEN_LIST *allocated;
292 #else
293 	struct screen * allocated;
294 #endif
295 	bool		use_env;
296 	bool		filter_mode;
297 	attr_t		previous_attr;
298 	TPARM_STATE	tparm_state;
299 	TTY *		saved_tty;	/* savetty/resetty information	  */
300 	bool		use_tioctl;
301 	NCURSES_SP_OUTC	_outch;		/* output handler if not putc */
302 #ifndef USE_SP_RIPOFF
303 	ripoff_t	rippedoff[N_RIPS];
304 	ripoff_t *	rsp;
305 #endif
306 #if NCURSES_NO_PADDING
307 	bool		_no_padding;	/* flag to set if padding disabled */
308 #endif
309 #if BROKEN_LINKER || USE_REENTRANT
310 	chtype *	real_acs_map;
311 	int		_LINES;
312 	int		_COLS;
313 	int		_TABSIZE;
314 	int		_ESCDELAY;
315 	TERMINAL *	_cur_term;
316 #endif
317 #ifdef TRACE
318 #if BROKEN_LINKER || USE_REENTRANT
319 	long		_outchars;
320 	const char *	_tputs_trace;
321 #endif
322 #endif
323 } NCURSES_PRESCREEN;
324 
325 extern NCURSES_EXPORT_VAR(NCURSES_PRESCREEN) _nc_prescreen;
326 
327 extern NCURSES_EXPORT(void) _nc_free_tparm(TERMINAL*);
328 
329 #ifdef __cplusplus
330 }
331 #endif
332 
333 /* *INDENT-ON* */
334 
335 #endif /* _TERM_PRIV_H */
336