1 /****************************************************************************
2  * Copyright 2018-2019,2020 Thomas E. Dickey                                *
3  * Copyright 1998-2014,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
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                 *
34  *     and: Juergen Pfeifer                         2008                    *
35  ****************************************************************************/
36 
37 #include <curses.priv.h>
38 
39 #ifndef CUR
40 #define CUR SP_TERMTYPE
41 #endif
42 
43 MODULE_ID("$Id: lib_acs.c,v 1.50 2020/02/02 23:34:34 tom Exp $")
44 
45 #if BROKEN_LINKER || USE_REENTRANT
46 #define MyBuffer _nc_prescreen.real_acs_map
47 NCURSES_EXPORT(chtype *)
48 NCURSES_PUBLIC_VAR(acs_map) (void)
49 {
50     if (MyBuffer == 0)
51 	MyBuffer = typeCalloc(chtype, ACS_LEN);
52     return MyBuffer;
53 }
54 #undef MyBuffer
55 #else
56 NCURSES_EXPORT_VAR (chtype) acs_map[ACS_LEN] =
57 {
58     0
59 };
60 #endif
61 
62 #ifdef USE_TERM_DRIVER
63 NCURSES_EXPORT(chtype)
64 NCURSES_SP_NAME(_nc_acs_char) (NCURSES_SP_DCLx int c)
65 {
66     chtype *map;
67     if (c < 0 || c >= ACS_LEN)
68 	return (chtype) 0;
69     map = (SP_PARM != 0) ? SP_PARM->_acs_map :
70 #if BROKEN_LINKER || USE_REENTRANT
71 	_nc_prescreen.real_acs_map
72 #else
73 	acs_map
74 #endif
75 	;
76     return map[c];
77 }
78 #endif /* USE_TERM_DRIVER */
79 
80 NCURSES_EXPORT(void)
81 NCURSES_SP_NAME(_nc_init_acs) (NCURSES_SP_DCL0)
82 {
83     chtype *fake_map = acs_map;
84     chtype *real_map = SP_PARM != 0 ? SP_PARM->_acs_map : fake_map;
85     int j;
86 
87     T(("initializing ACS map"));
88 
89     /*
90      * If we're using this from curses (rather than terminfo), we are storing
91      * the mapping information in the SCREEN struct so we can decide how to
92      * render it.
93      */
94     if (real_map != fake_map) {
95 	for (j = 1; j < ACS_LEN; ++j) {
96 	    real_map[j] = 0;
97 	    fake_map[j] = A_ALTCHARSET | (chtype) j;
98 	    if (SP_PARM)
99 		SP_PARM->_screen_acs_map[j] = FALSE;
100 	}
101     } else {
102 	for (j = 1; j < ACS_LEN; ++j) {
103 	    real_map[j] = 0;
104 	}
105     }
106 
107     /*
108      * Initializations for a UNIX-like multi-terminal environment.  Use
109      * ASCII chars and count on the terminfo description to do better.
110      */
111     real_map['l'] = '+';	/* should be upper left corner */
112     real_map['m'] = '+';	/* should be lower left corner */
113     real_map['k'] = '+';	/* should be upper right corner */
114     real_map['j'] = '+';	/* should be lower right corner */
115     real_map['u'] = '+';	/* should be tee pointing left */
116     real_map['t'] = '+';	/* should be tee pointing right */
117     real_map['v'] = '+';	/* should be tee pointing up */
118     real_map['w'] = '+';	/* should be tee pointing down */
119     real_map['q'] = '-';	/* should be horizontal line */
120     real_map['x'] = '|';	/* should be vertical line */
121     real_map['n'] = '+';	/* should be large plus or crossover */
122     real_map['o'] = '~';	/* should be scan line 1 */
123     real_map['s'] = '_';	/* should be scan line 9 */
124     real_map['`'] = '+';	/* should be diamond */
125     real_map['a'] = ':';	/* should be checker board (stipple) */
126     real_map['f'] = '\'';	/* should be degree symbol */
127     real_map['g'] = '#';	/* should be plus/minus */
128     real_map['~'] = 'o';	/* should be bullet */
129     real_map[','] = '<';	/* should be arrow pointing left */
130     real_map['+'] = '>';	/* should be arrow pointing right */
131     real_map['.'] = 'v';	/* should be arrow pointing down */
132     real_map['-'] = '^';	/* should be arrow pointing up */
133     real_map['h'] = '#';	/* should be board of squares */
134     real_map['i'] = '#';	/* should be lantern symbol */
135     real_map['0'] = '#';	/* should be solid square block */
136     /* these defaults were invented for ncurses */
137     real_map['p'] = '-';	/* should be scan line 3 */
138     real_map['r'] = '-';	/* should be scan line 7 */
139     real_map['y'] = '<';	/* should be less-than-or-equal-to */
140     real_map['z'] = '>';	/* should be greater-than-or-equal-to */
141     real_map['{'] = '*';	/* should be greek pi */
142     real_map['|'] = '!';	/* should be not-equal */
143     real_map['}'] = 'f';	/* should be pound-sterling symbol */
144     /* thick-line-drawing */
145     real_map['L'] = '+';	/* upper left corner */
146     real_map['M'] = '+';	/* lower left corner */
147     real_map['K'] = '+';	/* upper right corner */
148     real_map['J'] = '+';	/* lower right corner */
149     real_map['T'] = '+';	/* tee pointing left */
150     real_map['U'] = '+';	/* tee pointing right */
151     real_map['V'] = '+';	/* tee pointing up */
152     real_map['W'] = '+';	/* tee pointing down */
153     real_map['Q'] = '-';	/* horizontal line */
154     real_map['X'] = '|';	/* vertical line */
155     real_map['N'] = '+';	/* large plus or crossover */
156     /* double-line-drawing */
157     real_map['C'] = '+';	/* upper left corner */
158     real_map['D'] = '+';	/* lower left corner */
159     real_map['B'] = '+';	/* upper right corner */
160     real_map['A'] = '+';	/* lower right corner */
161     real_map['G'] = '+';	/* tee pointing left */
162     real_map['F'] = '+';	/* tee pointing right */
163     real_map['H'] = '+';	/* tee pointing up */
164     real_map['I'] = '+';	/* tee pointing down */
165     real_map['R'] = '-';	/* horizontal line */
166     real_map['Y'] = '|';	/* vertical line */
167     real_map['E'] = '+';	/* large plus or crossover */
168 
169 #ifdef USE_TERM_DRIVER
170     CallDriver_2(SP_PARM, td_initacs, real_map, fake_map);
171 #else
172     if (ena_acs != NULL) {
173 	NCURSES_PUTP2("ena_acs", ena_acs);
174     }
175 #if NCURSES_EXT_FUNCS && defined(enter_pc_charset_mode) && defined(exit_pc_charset_mode)
176     /*
177      * Linux console "supports" the "PC ROM" character set by the coincidence
178      * that smpch/rmpch and smacs/rmacs have the same values.  ncurses has
179      * no codepage support (see SCO Merge for an example).  Outside of the
180      * values defined in acsc, there are no definitions for the "PC ROM"
181      * character set (assumed by some applications to be codepage 437), but we
182      * allow those applications to use those codepoints.
183      *
184      * test/blue.c uses this feature.
185      */
186 #define PCH_KLUDGE(a,b) (a != 0 && b != 0 && !strcmp(a,b))
187     if (PCH_KLUDGE(enter_pc_charset_mode, enter_alt_charset_mode) &&
188 	PCH_KLUDGE(exit_pc_charset_mode, exit_alt_charset_mode)) {
189 	size_t i;
190 	for (i = 1; i < ACS_LEN; ++i) {
191 	    if (real_map[i] == 0) {
192 		real_map[i] = (chtype) i;
193 		if (real_map != fake_map) {
194 		    if (SP != 0)
195 			SP->_screen_acs_map[i] = TRUE;
196 		}
197 	    }
198 	}
199     }
200 #endif
201 
202     if (acs_chars != NULL) {
203 	size_t i = 0;
204 	size_t length = strlen(acs_chars);
205 
206 	while (i + 1 < length) {
207 	    if (acs_chars[i] != 0 && UChar(acs_chars[i]) < ACS_LEN) {
208 		real_map[UChar(acs_chars[i])] = UChar(acs_chars[i + 1]) | A_ALTCHARSET;
209 		T(("#%d real_map[%s] = %s",
210 		   (int) i,
211 		   _tracechar(UChar(acs_chars[i])),
212 		   _tracechtype(real_map[UChar(acs_chars[i])])));
213 		if (SP != 0) {
214 		    SP->_screen_acs_map[UChar(acs_chars[i])] = TRUE;
215 		}
216 	    }
217 	    i += 2;
218 	}
219     }
220 #ifdef TRACE
221     /* Show the equivalent mapping, noting if it does not match the
222      * given attribute, whether by re-ordering or duplication.
223      */
224     if (USE_TRACEF(TRACE_CALLS)) {
225 	size_t n, m;
226 	char show[ACS_LEN * 2 + 1];
227 	for (n = 1, m = 0; n < ACS_LEN; n++) {
228 	    if (real_map[n] != 0) {
229 		show[m++] = (char) n;
230 		show[m++] = (char) ChCharOf(real_map[n]);
231 	    }
232 	}
233 	show[m] = 0;
234 	if (acs_chars == NULL || strcmp(acs_chars, show))
235 	    _tracef("%s acs_chars %s",
236 		    (acs_chars == NULL) ? "NULL" : "READ",
237 		    _nc_visbuf(acs_chars));
238 	_tracef("%s acs_chars %s",
239 		(acs_chars == NULL)
240 		? "NULL"
241 		: (strcmp(acs_chars, show)
242 		   ? "DIFF"
243 		   : "SAME"),
244 		_nc_visbuf(show));
245 	_nc_unlock_global(tracef);
246     }
247 #endif /* TRACE */
248 #endif
249 }
250 
251 #if NCURSES_SP_FUNCS
252 NCURSES_EXPORT(void)
253 _nc_init_acs(void)
254 {
255     NCURSES_SP_NAME(_nc_init_acs) (CURRENT_SCREEN);
256 }
257 #endif
258 
259 #if !NCURSES_WCWIDTH_GRAPHICS
260 NCURSES_EXPORT(int)
261 _nc_wacs_width(unsigned ch)
262 {
263     int result;
264     switch (ch) {
265     case 0x00a3:		/* FALLTHRU - ncurses pound-sterling symbol */
266     case 0x00b0:		/* FALLTHRU - VT100 degree symbol */
267     case 0x00b1:		/* FALLTHRU - VT100 plus/minus */
268     case 0x00b7:		/* FALLTHRU - VT100 bullet */
269     case 0x03c0:		/* FALLTHRU - ncurses greek pi */
270     case 0x2190:		/* FALLTHRU - Teletype arrow pointing left */
271     case 0x2191:		/* FALLTHRU - Teletype arrow pointing up */
272     case 0x2192:		/* FALLTHRU - Teletype arrow pointing right */
273     case 0x2193:		/* FALLTHRU - Teletype arrow pointing down */
274     case 0x2260:		/* FALLTHRU - ncurses not-equal */
275     case 0x2264:		/* FALLTHRU - ncurses less-than-or-equal-to */
276     case 0x2265:		/* FALLTHRU - ncurses greater-than-or-equal-to */
277     case 0x23ba:		/* FALLTHRU - VT100 scan line 1 */
278     case 0x23bb:		/* FALLTHRU - ncurses scan line 3 */
279     case 0x23bc:		/* FALLTHRU - ncurses scan line 7 */
280     case 0x23bd:		/* FALLTHRU - VT100 scan line 9 */
281     case 0x2500:		/* FALLTHRU - VT100 horizontal line */
282     case 0x2501:		/* FALLTHRU - thick horizontal line */
283     case 0x2502:		/* FALLTHRU - VT100 vertical line */
284     case 0x2503:		/* FALLTHRU - thick vertical line */
285     case 0x250c:		/* FALLTHRU - VT100 upper left corner */
286     case 0x250f:		/* FALLTHRU - thick upper left corner */
287     case 0x2510:		/* FALLTHRU - VT100 upper right corner */
288     case 0x2513:		/* FALLTHRU - thick upper right corner */
289     case 0x2514:		/* FALLTHRU - VT100 lower left corner */
290     case 0x2517:		/* FALLTHRU - thick lower left corner */
291     case 0x2518:		/* FALLTHRU - VT100 lower right corner */
292     case 0x251b:		/* FALLTHRU - thick lower right corner */
293     case 0x251c:		/* FALLTHRU - VT100 tee pointing left */
294     case 0x2523:		/* FALLTHRU - thick tee pointing left */
295     case 0x2524:		/* FALLTHRU - VT100 tee pointing right */
296     case 0x252b:		/* FALLTHRU - thick tee pointing right */
297     case 0x252c:		/* FALLTHRU - VT100 tee pointing down */
298     case 0x2533:		/* FALLTHRU - thick tee pointing down */
299     case 0x2534:		/* FALLTHRU - VT100 tee pointing up */
300     case 0x253b:		/* FALLTHRU - thick tee pointing up */
301     case 0x253c:		/* FALLTHRU - VT100 large plus or crossover */
302     case 0x254b:		/* FALLTHRU - thick large plus or crossover */
303     case 0x2550:		/* FALLTHRU - double horizontal line */
304     case 0x2551:		/* FALLTHRU - double vertical line */
305     case 0x2554:		/* FALLTHRU - double upper left corner */
306     case 0x2557:		/* FALLTHRU - double upper right corner */
307     case 0x255a:		/* FALLTHRU - double lower left corner */
308     case 0x255d:		/* FALLTHRU - double lower right corner */
309     case 0x2560:		/* FALLTHRU - double tee pointing right */
310     case 0x2563:		/* FALLTHRU - double tee pointing left */
311     case 0x2566:		/* FALLTHRU - double tee pointing down */
312     case 0x2569:		/* FALLTHRU - double tee pointing up */
313     case 0x256c:		/* FALLTHRU - double large plus or crossover */
314     case 0x2592:		/* FALLTHRU - VT100 checker board (stipple) */
315     case 0x25ae:		/* FALLTHRU - Teletype solid square block */
316     case 0x25c6:		/* FALLTHRU - VT100 diamond */
317     case 0x2603:		/* FALLTHRU - Teletype lantern symbol */
318 	result = 1;
319 	break;
320     default:
321 	result = wcwidth(ch);
322 	break;
323     }
324     return result;
325 }
326 #endif /* !NCURSES_WCWIDTH_GRAPHICS */
327