1 /****************************************************************************
2  * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc.              *
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  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  *     and: Thomas E. Dickey                        1996-on                 *
33  ****************************************************************************/
34 
35 #include <curses.priv.h>
36 #include <term.h>		/* ena_acs, acs_chars */
37 
38 MODULE_ID("$Id: lib_acs.c,v 1.36 2008/08/16 19:22:55 tom Exp $")
39 
40 #if BROKEN_LINKER || USE_REENTRANT
41 #define MyBuffer _nc_prescreen.real_acs_map
42 NCURSES_EXPORT_VAR(chtype *)
43 _nc_acs_map(void)
44 {
45     if (MyBuffer == 0)
46 	MyBuffer = typeCalloc(chtype, ACS_LEN);
47     return MyBuffer;
48 }
49 #undef MyBuffer
50 #else
51 NCURSES_EXPORT_VAR(chtype) acs_map[ACS_LEN] =
52 {
53     0
54 };
55 #endif
56 
57 NCURSES_EXPORT(void)
58 _nc_init_acs(void)
59 {
60     chtype *fake_map = acs_map;
61     chtype *real_map = SP != 0 ? SP->_acs_map : fake_map;
62     int j;
63 
64     T(("initializing ACS map"));
65 
66     /*
67      * If we're using this from curses (rather than terminfo), we are storing
68      * the mapping information in the SCREEN struct so we can decide how to
69      * render it.
70      */
71     if (real_map != fake_map) {
72 	for (j = 1; j < ACS_LEN; ++j) {
73 	    real_map[j] = 0;
74 	    fake_map[j] = A_ALTCHARSET | j;
75 	    if (SP)
76 		SP->_screen_acs_map[j] = FALSE;
77 	}
78     } else {
79 	for (j = 1; j < ACS_LEN; ++j) {
80 	    real_map[j] = 0;
81 	}
82     }
83 
84     /*
85      * Initializations for a UNIX-like multi-terminal environment.  Use
86      * ASCII chars and count on the terminfo description to do better.
87      */
88     real_map['l'] = '+';	/* should be upper left corner */
89     real_map['m'] = '+';	/* should be lower left corner */
90     real_map['k'] = '+';	/* should be upper right corner */
91     real_map['j'] = '+';	/* should be lower right corner */
92     real_map['u'] = '+';	/* should be tee pointing left */
93     real_map['t'] = '+';	/* should be tee pointing right */
94     real_map['v'] = '+';	/* should be tee pointing up */
95     real_map['w'] = '+';	/* should be tee pointing down */
96     real_map['q'] = '-';	/* should be horizontal line */
97     real_map['x'] = '|';	/* should be vertical line */
98     real_map['n'] = '+';	/* should be large plus or crossover */
99     real_map['o'] = '~';	/* should be scan line 1 */
100     real_map['s'] = '_';	/* should be scan line 9 */
101     real_map['`'] = '+';	/* should be diamond */
102     real_map['a'] = ':';	/* should be checker board (stipple) */
103     real_map['f'] = '\'';	/* should be degree symbol */
104     real_map['g'] = '#';	/* should be plus/minus */
105     real_map['~'] = 'o';	/* should be bullet */
106     real_map[','] = '<';	/* should be arrow pointing left */
107     real_map['+'] = '>';	/* should be arrow pointing right */
108     real_map['.'] = 'v';	/* should be arrow pointing down */
109     real_map['-'] = '^';	/* should be arrow pointing up */
110     real_map['h'] = '#';	/* should be board of squares */
111     real_map['i'] = '#';	/* should be lantern symbol */
112     real_map['0'] = '#';	/* should be solid square block */
113     /* these defaults were invented for ncurses */
114     real_map['p'] = '-';	/* should be scan line 3 */
115     real_map['r'] = '-';	/* should be scan line 7 */
116     real_map['y'] = '<';	/* should be less-than-or-equal-to */
117     real_map['z'] = '>';	/* should be greater-than-or-equal-to */
118     real_map['{'] = '*';	/* should be greek pi */
119     real_map['|'] = '!';	/* should be not-equal */
120     real_map['}'] = 'f';	/* should be pound-sterling symbol */
121 
122     if (ena_acs != NULL) {
123 	TPUTS_TRACE("ena_acs");
124 	putp(ena_acs);
125     }
126 #if NCURSES_EXT_FUNCS
127     /*
128      * Linux console "supports" the "PC ROM" character set by the coincidence
129      * that smpch/rmpch and smacs/rmacs have the same values.  ncurses has
130      * no codepage support (see SCO Merge for an example).  Outside of the
131      * values defined in acsc, there are no definitions for the "PC ROM"
132      * character set (assumed by some applications to be codepage 437), but we
133      * allow those applications to use those codepoints.
134      *
135      * test/blue.c uses this feature.
136      */
137 #define PCH_KLUDGE(a,b) (a != 0 && b != 0 && !strcmp(a,b))
138     if (PCH_KLUDGE(enter_pc_charset_mode, enter_alt_charset_mode) &&
139 	PCH_KLUDGE(exit_pc_charset_mode, exit_alt_charset_mode)) {
140 	size_t i;
141 	for (i = 1; i < ACS_LEN; ++i) {
142 	    if (real_map[i] == 0) {
143 		real_map[i] = i;
144 		if (real_map != fake_map) {
145 		    if (SP != 0)
146 			SP->_screen_acs_map[i] = TRUE;
147 		}
148 	    }
149 	}
150     }
151 #endif
152 
153     if (acs_chars != NULL) {
154 	size_t i = 0;
155 	size_t length = strlen(acs_chars);
156 
157 	while (i + 1 < length) {
158 	    if (acs_chars[i] != 0 && UChar(acs_chars[i]) < ACS_LEN) {
159 		real_map[UChar(acs_chars[i])] = UChar(acs_chars[i + 1]) | A_ALTCHARSET;
160 		if (SP != 0)
161 		    SP->_screen_acs_map[UChar(acs_chars[i])] = TRUE;
162 	    }
163 	    i += 2;
164 	}
165     }
166 #ifdef TRACE
167     /* Show the equivalent mapping, noting if it does not match the
168      * given attribute, whether by re-ordering or duplication.
169      */
170     if (USE_TRACEF(TRACE_CALLS)) {
171 	size_t n, m;
172 	char show[ACS_LEN * 2 + 1];
173 	for (n = 1, m = 0; n < ACS_LEN; n++) {
174 	    if (real_map[n] != 0) {
175 		show[m++] = (char) n;
176 		show[m++] = (char) ChCharOf(real_map[n]);
177 	    }
178 	}
179 	show[m] = 0;
180 	if (acs_chars == NULL || strcmp(acs_chars, show))
181 	    _tracef("%s acs_chars %s",
182 		    (acs_chars == NULL) ? "NULL" : "READ",
183 		    _nc_visbuf(acs_chars));
184 	_tracef("%s acs_chars %s",
185 		(acs_chars == NULL)
186 		? "NULL"
187 		: (strcmp(acs_chars, show)
188 		   ? "DIFF"
189 		   : "SAME"),
190 		_nc_visbuf(show));
191 	_nc_unlock_global(tracef);
192     }
193 #endif /* TRACE */
194 }
195