xref: /openbsd-src/lib/libcurses/tinfo/lib_acs.c (revision c7ef0cfc17afcba97172c25e1e3a943e893bc632)
1 /* $OpenBSD: lib_acs.c,v 1.6 2023/10/17 09:52:09 nicm Exp $ */
2 
3 /****************************************************************************
4  * Copyright 2018-2019,2020 Thomas E. Dickey                                *
5  * Copyright 1998-2014,2017 Free Software Foundation, Inc.                  *
6  *                                                                          *
7  * Permission is hereby granted, free of charge, to any person obtaining a  *
8  * copy of this software and associated documentation files (the            *
9  * "Software"), to deal in the Software without restriction, including      *
10  * without limitation the rights to use, copy, modify, merge, publish,      *
11  * distribute, distribute with modifications, sublicense, and/or sell       *
12  * copies of the Software, and to permit persons to whom the Software is    *
13  * furnished to do so, subject to the following conditions:                 *
14  *                                                                          *
15  * The above copyright notice and this permission notice shall be included  *
16  * in all copies or substantial portions of the Software.                   *
17  *                                                                          *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
21  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
24  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
25  *                                                                          *
26  * Except as contained in this notice, the name(s) of the above copyright   *
27  * holders shall not be used in advertising or otherwise to promote the     *
28  * sale, use or other dealings in this Software without prior written       *
29  * authorization.                                                           *
30  ****************************************************************************/
31 
32 /****************************************************************************
33  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
34  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
35  *     and: Thomas E. Dickey                        1996-on                 *
36  *     and: Juergen Pfeifer                         2008                    *
37  ****************************************************************************/
38 
39 #include <curses.priv.h>
40 
41 #ifndef CUR
42 #define CUR SP_TERMTYPE
43 #endif
44 
45 MODULE_ID("$Id: lib_acs.c,v 1.6 2023/10/17 09:52:09 nicm Exp $")
46 
47 #if BROKEN_LINKER || USE_REENTRANT
48 #define MyBuffer _nc_prescreen.real_acs_map
NCURSES_EXPORT(chtype *)49 NCURSES_EXPORT(chtype *)
50 NCURSES_PUBLIC_VAR(acs_map) (void)
51 {
52     if (MyBuffer == 0)
53 	MyBuffer = typeCalloc(chtype, ACS_LEN);
54     return MyBuffer;
55 }
56 #undef MyBuffer
57 #else
58 NCURSES_EXPORT_VAR (chtype) acs_map[ACS_LEN] =
59 {
60     0
61 };
62 #endif
63 
64 #ifdef USE_TERM_DRIVER
65 NCURSES_EXPORT(chtype)
NCURSES_SP_NAME(_nc_acs_char)66 NCURSES_SP_NAME(_nc_acs_char) (NCURSES_SP_DCLx int c)
67 {
68     chtype *map;
69     if (c < 0 || c >= ACS_LEN)
70 	return (chtype) 0;
71     map = (SP_PARM != 0) ? SP_PARM->_acs_map :
72 #if BROKEN_LINKER || USE_REENTRANT
73 	_nc_prescreen.real_acs_map
74 #else
75 	acs_map
76 #endif
77 	;
78     return map[c];
79 }
80 #endif /* USE_TERM_DRIVER */
81 
82 NCURSES_EXPORT(void)
NCURSES_SP_NAME(_nc_init_acs)83 NCURSES_SP_NAME(_nc_init_acs) (NCURSES_SP_DCL0)
84 {
85     chtype *fake_map = acs_map;
86     chtype *real_map = SP_PARM != 0 ? SP_PARM->_acs_map : fake_map;
87     int j;
88 
89     T(("initializing ACS map"));
90 
91     /*
92      * If we're using this from curses (rather than terminfo), we are storing
93      * the mapping information in the SCREEN struct so we can decide how to
94      * render it.
95      */
96     if (real_map != fake_map) {
97 	for (j = 1; j < ACS_LEN; ++j) {
98 	    real_map[j] = 0;
99 	    fake_map[j] = A_ALTCHARSET | (chtype) j;
100 	    if (SP_PARM)
101 		SP_PARM->_screen_acs_map[j] = FALSE;
102 	}
103     } else {
104 	for (j = 1; j < ACS_LEN; ++j) {
105 	    real_map[j] = 0;
106 	}
107     }
108 
109     /*
110      * Initializations for a UNIX-like multi-terminal environment.  Use
111      * ASCII chars and count on the terminfo description to do better.
112      */
113     real_map['l'] = '+';	/* should be upper left corner */
114     real_map['m'] = '+';	/* should be lower left corner */
115     real_map['k'] = '+';	/* should be upper right corner */
116     real_map['j'] = '+';	/* should be lower right corner */
117     real_map['u'] = '+';	/* should be tee pointing left */
118     real_map['t'] = '+';	/* should be tee pointing right */
119     real_map['v'] = '+';	/* should be tee pointing up */
120     real_map['w'] = '+';	/* should be tee pointing down */
121     real_map['q'] = '-';	/* should be horizontal line */
122     real_map['x'] = '|';	/* should be vertical line */
123     real_map['n'] = '+';	/* should be large plus or crossover */
124     real_map['o'] = '~';	/* should be scan line 1 */
125     real_map['s'] = '_';	/* should be scan line 9 */
126     real_map['`'] = '+';	/* should be diamond */
127     real_map['a'] = ':';	/* should be checker board (stipple) */
128     real_map['f'] = '\'';	/* should be degree symbol */
129     real_map['g'] = '#';	/* should be plus/minus */
130     real_map['~'] = 'o';	/* should be bullet */
131     real_map[','] = '<';	/* should be arrow pointing left */
132     real_map['+'] = '>';	/* should be arrow pointing right */
133     real_map['.'] = 'v';	/* should be arrow pointing down */
134     real_map['-'] = '^';	/* should be arrow pointing up */
135     real_map['h'] = '#';	/* should be board of squares */
136     real_map['i'] = '#';	/* should be lantern symbol */
137     real_map['0'] = '#';	/* should be solid square block */
138     /* these defaults were invented for ncurses */
139     real_map['p'] = '-';	/* should be scan line 3 */
140     real_map['r'] = '-';	/* should be scan line 7 */
141     real_map['y'] = '<';	/* should be less-than-or-equal-to */
142     real_map['z'] = '>';	/* should be greater-than-or-equal-to */
143     real_map['{'] = '*';	/* should be greek pi */
144     real_map['|'] = '!';	/* should be not-equal */
145     real_map['}'] = 'f';	/* should be pound-sterling symbol */
146     /* thick-line-drawing */
147     real_map['L'] = '+';	/* upper left corner */
148     real_map['M'] = '+';	/* lower left corner */
149     real_map['K'] = '+';	/* upper right corner */
150     real_map['J'] = '+';	/* lower right corner */
151     real_map['T'] = '+';	/* tee pointing left */
152     real_map['U'] = '+';	/* tee pointing right */
153     real_map['V'] = '+';	/* tee pointing up */
154     real_map['W'] = '+';	/* tee pointing down */
155     real_map['Q'] = '-';	/* horizontal line */
156     real_map['X'] = '|';	/* vertical line */
157     real_map['N'] = '+';	/* large plus or crossover */
158     /* double-line-drawing */
159     real_map['C'] = '+';	/* upper left corner */
160     real_map['D'] = '+';	/* lower left corner */
161     real_map['B'] = '+';	/* upper right corner */
162     real_map['A'] = '+';	/* lower right corner */
163     real_map['G'] = '+';	/* tee pointing left */
164     real_map['F'] = '+';	/* tee pointing right */
165     real_map['H'] = '+';	/* tee pointing up */
166     real_map['I'] = '+';	/* tee pointing down */
167     real_map['R'] = '-';	/* horizontal line */
168     real_map['Y'] = '|';	/* vertical line */
169     real_map['E'] = '+';	/* large plus or crossover */
170 
171 #ifdef USE_TERM_DRIVER
172     CallDriver_2(SP_PARM, td_initacs, real_map, fake_map);
173 #else
174     if (ena_acs != NULL) {
175 	NCURSES_PUTP2("ena_acs", ena_acs);
176     }
177 #if NCURSES_EXT_FUNCS && defined(enter_pc_charset_mode) && defined(exit_pc_charset_mode)
178     /*
179      * Linux console "supports" the "PC ROM" character set by the coincidence
180      * that smpch/rmpch and smacs/rmacs have the same values.  ncurses has
181      * no codepage support (see SCO Merge for an example).  Outside of the
182      * values defined in acsc, there are no definitions for the "PC ROM"
183      * character set (assumed by some applications to be codepage 437), but we
184      * allow those applications to use those codepoints.
185      *
186      * test/blue.c uses this feature.
187      */
188 #define PCH_KLUDGE(a,b) (a != 0 && b != 0 && !strcmp(a,b))
189     if (PCH_KLUDGE(enter_pc_charset_mode, enter_alt_charset_mode) &&
190 	PCH_KLUDGE(exit_pc_charset_mode, exit_alt_charset_mode)) {
191 	size_t i;
192 	for (i = 1; i < ACS_LEN; ++i) {
193 	    if (real_map[i] == 0) {
194 		real_map[i] = (chtype) i;
195 		if (real_map != fake_map) {
196 		    if (SP != 0)
197 			SP->_screen_acs_map[i] = TRUE;
198 		}
199 	    }
200 	}
201     }
202 #endif
203 
204     if (acs_chars != NULL) {
205 	size_t i = 0;
206 	size_t length = strlen(acs_chars);
207 
208 	while (i + 1 < length) {
209 	    if (acs_chars[i] != 0 && UChar(acs_chars[i]) < ACS_LEN) {
210 		real_map[UChar(acs_chars[i])] = UChar(acs_chars[i + 1]) | A_ALTCHARSET;
211 		T(("#%d real_map[%s] = %s",
212 		   (int) i,
213 		   _tracechar(UChar(acs_chars[i])),
214 		   _tracechtype(real_map[UChar(acs_chars[i])])));
215 		if (SP != 0) {
216 		    SP->_screen_acs_map[UChar(acs_chars[i])] = TRUE;
217 		}
218 	    }
219 	    i += 2;
220 	}
221     }
222 #ifdef TRACE
223     /* Show the equivalent mapping, noting if it does not match the
224      * given attribute, whether by re-ordering or duplication.
225      */
226     if (USE_TRACEF(TRACE_CALLS)) {
227 	size_t n, m;
228 	char show[ACS_LEN * 2 + 1];
229 	for (n = 1, m = 0; n < ACS_LEN; n++) {
230 	    if (real_map[n] != 0) {
231 		show[m++] = (char) n;
232 		show[m++] = (char) ChCharOf(real_map[n]);
233 	    }
234 	}
235 	show[m] = 0;
236 	if (acs_chars == NULL || strcmp(acs_chars, show))
237 	    _tracef("%s acs_chars %s",
238 		    (acs_chars == NULL) ? "NULL" : "READ",
239 		    _nc_visbuf(acs_chars));
240 	_tracef("%s acs_chars %s",
241 		(acs_chars == NULL)
242 		? "NULL"
243 		: (strcmp(acs_chars, show)
244 		   ? "DIFF"
245 		   : "SAME"),
246 		_nc_visbuf(show));
247 	_nc_unlock_global(tracef);
248     }
249 #endif /* TRACE */
250 #endif
251 }
252 
253 #if NCURSES_SP_FUNCS
254 NCURSES_EXPORT(void)
_nc_init_acs(void)255 _nc_init_acs(void)
256 {
257     NCURSES_SP_NAME(_nc_init_acs) (CURRENT_SCREEN);
258 }
259 #endif
260 
261 #if !NCURSES_WCWIDTH_GRAPHICS
262 NCURSES_EXPORT(int)
_nc_wacs_width(unsigned ch)263 _nc_wacs_width(unsigned ch)
264 {
265     int result;
266     switch (ch) {
267     case 0x00a3:		/* FALLTHRU - ncurses pound-sterling symbol */
268     case 0x00b0:		/* FALLTHRU - VT100 degree symbol */
269     case 0x00b1:		/* FALLTHRU - VT100 plus/minus */
270     case 0x00b7:		/* FALLTHRU - VT100 bullet */
271     case 0x03c0:		/* FALLTHRU - ncurses greek pi */
272     case 0x2190:		/* FALLTHRU - Teletype arrow pointing left */
273     case 0x2191:		/* FALLTHRU - Teletype arrow pointing up */
274     case 0x2192:		/* FALLTHRU - Teletype arrow pointing right */
275     case 0x2193:		/* FALLTHRU - Teletype arrow pointing down */
276     case 0x2260:		/* FALLTHRU - ncurses not-equal */
277     case 0x2264:		/* FALLTHRU - ncurses less-than-or-equal-to */
278     case 0x2265:		/* FALLTHRU - ncurses greater-than-or-equal-to */
279     case 0x23ba:		/* FALLTHRU - VT100 scan line 1 */
280     case 0x23bb:		/* FALLTHRU - ncurses scan line 3 */
281     case 0x23bc:		/* FALLTHRU - ncurses scan line 7 */
282     case 0x23bd:		/* FALLTHRU - VT100 scan line 9 */
283     case 0x2500:		/* FALLTHRU - VT100 horizontal line */
284     case 0x2501:		/* FALLTHRU - thick horizontal line */
285     case 0x2502:		/* FALLTHRU - VT100 vertical line */
286     case 0x2503:		/* FALLTHRU - thick vertical line */
287     case 0x250c:		/* FALLTHRU - VT100 upper left corner */
288     case 0x250f:		/* FALLTHRU - thick upper left corner */
289     case 0x2510:		/* FALLTHRU - VT100 upper right corner */
290     case 0x2513:		/* FALLTHRU - thick upper right corner */
291     case 0x2514:		/* FALLTHRU - VT100 lower left corner */
292     case 0x2517:		/* FALLTHRU - thick lower left corner */
293     case 0x2518:		/* FALLTHRU - VT100 lower right corner */
294     case 0x251b:		/* FALLTHRU - thick lower right corner */
295     case 0x251c:		/* FALLTHRU - VT100 tee pointing left */
296     case 0x2523:		/* FALLTHRU - thick tee pointing left */
297     case 0x2524:		/* FALLTHRU - VT100 tee pointing right */
298     case 0x252b:		/* FALLTHRU - thick tee pointing right */
299     case 0x252c:		/* FALLTHRU - VT100 tee pointing down */
300     case 0x2533:		/* FALLTHRU - thick tee pointing down */
301     case 0x2534:		/* FALLTHRU - VT100 tee pointing up */
302     case 0x253b:		/* FALLTHRU - thick tee pointing up */
303     case 0x253c:		/* FALLTHRU - VT100 large plus or crossover */
304     case 0x254b:		/* FALLTHRU - thick large plus or crossover */
305     case 0x2550:		/* FALLTHRU - double horizontal line */
306     case 0x2551:		/* FALLTHRU - double vertical line */
307     case 0x2554:		/* FALLTHRU - double upper left corner */
308     case 0x2557:		/* FALLTHRU - double upper right corner */
309     case 0x255a:		/* FALLTHRU - double lower left corner */
310     case 0x255d:		/* FALLTHRU - double lower right corner */
311     case 0x2560:		/* FALLTHRU - double tee pointing right */
312     case 0x2563:		/* FALLTHRU - double tee pointing left */
313     case 0x2566:		/* FALLTHRU - double tee pointing down */
314     case 0x2569:		/* FALLTHRU - double tee pointing up */
315     case 0x256c:		/* FALLTHRU - double large plus or crossover */
316     case 0x2592:		/* FALLTHRU - VT100 checker board (stipple) */
317     case 0x25ae:		/* FALLTHRU - Teletype solid square block */
318     case 0x25c6:		/* FALLTHRU - VT100 diamond */
319     case 0x2603:		/* FALLTHRU - Teletype lantern symbol */
320 	result = 1;
321 	break;
322     default:
323 	result = wcwidth(ch);
324 	break;
325     }
326     return result;
327 }
328 #endif /* !NCURSES_WCWIDTH_GRAPHICS */
329