• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /****************************************************************************
2  * Copyright 2021,2022 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  * $Id: combine.c,v 1.23 2022/12/10 22:28:50 tom Exp $
30  */
31 
32 #include <test.priv.h>
33 
34 #if USE_WIDEC_SUPPORT
35 
36 #include <wctype.h>
37 #include <dump_window.h>
38 #include <popup_msg.h>
39 
40 static int c_opt;
41 static int r_opt;
42 
43 static int
next_char(int value)44 next_char(int value)
45 {
46     do {
47 	++value;
48     } while (!iswprint((wint_t) value));
49     return value;
50 }
51 
52 static int
prev_char(int value)53 prev_char(int value)
54 {
55     do {
56 	--value;
57     } while (!iswprint((wint_t) value));
58     return value;
59 }
60 
61 static void
do_row(int row,int base_ch,int over_ch)62 do_row(int row, int base_ch, int over_ch)
63 {
64     int col = 0;
65     bool done = FALSE;
66     bool reverse = (r_opt && !(row % 2));
67 
68     move(row, col);
69     printw("[U+%04X]", over_ch);
70     do {
71 	if (c_opt) {
72 	    wchar_t source[2];
73 	    cchar_t target;
74 	    attr_t attr = reverse ? A_REVERSE : A_NORMAL;
75 
76 	    source[1] = 0;
77 
78 	    source[0] = (wchar_t) base_ch;
79 	    setcchar(&target, source, attr, 0, NULL);
80 	    add_wch(&target);
81 
82 	    source[0] = (wchar_t) over_ch;
83 	    setcchar(&target, source, attr, 0, NULL);
84 	    add_wch(&target);
85 	} else {
86 	    wchar_t data[3];
87 
88 	    data[0] = (wchar_t) base_ch;
89 	    data[1] = (wchar_t) over_ch;
90 	    data[2] = 0;
91 	    if (reverse)
92 		attr_on(A_REVERSE, NULL);
93 	    addwstr(data);
94 	    if (reverse)
95 		attr_off(A_REVERSE, NULL);
96 	}
97 	col = getcurx(stdscr);
98 	base_ch = next_char(base_ch);
99 	done = (col + 1 >= COLS);
100     } while (!done);
101 }
102 
103 #define LAST_OVER 0x6f
104 
105 static int
next_over(int value)106 next_over(int value)
107 {
108     if (++value > LAST_OVER)
109 	value = 0;
110     return value;
111 }
112 
113 static int
prev_over(int value)114 prev_over(int value)
115 {
116     if (--value < 0)
117 	value = LAST_OVER;
118     return value;
119 }
120 
121 static void
do_all(int left_at,int over_it)122 do_all(int left_at, int over_it)
123 {
124     int row;
125 
126     for (row = 0; row < LINES; ++row) {
127 	do_row(row, left_at, 0x300 + over_it);
128 	over_it = next_over(over_it);
129     }
130 }
131 
132 static void
show_help(WINDOW * current)133 show_help(WINDOW *current)
134 {
135     /* *INDENT-OFF* */
136     static struct {
137 	int	key;
138 	CONST_FMT char * msg;
139     } help[] = {
140 	{ HELP_KEY_1,	"Show this screen" },
141 	{ CTRL('L'),	"Repaint screen" },
142 	{ '$',		"Scroll to end of combining-character range" },
143 	{ '+',		"Scroll to next combining-character in range" },
144 	{ KEY_DOWN,	"(same as \"+\")" },
145 	{ '-',		"Scroll to previous combining-character in range" },
146 	{ KEY_UP,	"(same as \"-\")" },
147 	{ '0',		"Scroll to beginning of combining-character range" },
148 	{ 'c',		"Toggle command-line option \"-c\"" },
149 	{ 'd',		"Dump screen using scr_dump unless \"-l\" option used" },
150 	{ 'h',		"Scroll test-data left one column" },
151 	{ 'j',		"Scroll test-data down one row" },
152 	{ 'k',		"Scroll test-data up one row" },
153 	{ 'l',		"Scroll test-data right one column" },
154 	{ 'q',		"Quit" },
155 	{ ESCAPE,	"(same as \"q\")" },
156 	{ QUIT,		"(same as \"q\")" },
157 	{ 'r',		"Toggle command-line option \"-r\"" },
158     };
159     /* *INDENT-ON* */
160 
161     char **msgs = typeCalloc(char *, SIZEOF(help) + 3);
162     size_t s;
163     int d = 0;
164 
165     msgs[d++] = strdup("Test diacritic combining-characters range "
166 		       "U+0300..U+036F");
167     msgs[d++] = strdup("");
168     for (s = 0; s < SIZEOF(help); ++s) {
169 	char *name = strdup(keyname(help[s].key));
170 	size_t need = (11 + strlen(name) + strlen(help[s].msg));
171 	msgs[d] = typeMalloc(char, need);
172 	_nc_SPRINTF(msgs[d], _nc_SLIMIT(need) "%-10s%s", name, help[s].msg);
173 	free(name);
174 	++d;
175     }
176     popup_msg2(current, msgs);
177     for (s = 0; msgs[s] != 0; ++s) {
178 	free(msgs[s]);
179     }
180     free(msgs);
181 }
182 
183 static void
usage(int ok)184 usage(int ok)
185 {
186     static const char *msg[] =
187     {
188 	"Usage: combine [options]"
189 	,""
190 	,USAGE_COMMON
191 	,"Demonstrate combining-characters."
192 	,""
193 	,"Options:"
194 	," -c       use cchar_t data rather than wchar_t string"
195 	," -l FILE  log window-dumps to this file"
196 	," -r       draw even-numbered rows in reverse-video"
197     };
198     unsigned n;
199     for (n = 0; n < SIZEOF(msg); ++n) {
200 	fprintf(stderr, "%s\n", msg[n]);
201     }
202     ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
203 }
204 /* *INDENT-OFF* */
VERSION_COMMON()205 VERSION_COMMON()
206 /* *INDENT-ON* */
207 
208 int
209 main(int argc, char *argv[])
210 {
211     int ch;
212     int left_at = ' ';
213     int over_it = 0;
214     bool done = FALSE;
215     bool log_option = FALSE;
216     const char *dump_log = "combine.log";
217 
218     while ((ch = getopt(argc, argv, OPTS_COMMON "cl:r")) != -1) {
219 	switch (ch) {
220 	case 'c':
221 	    c_opt = TRUE;
222 	    break;
223 	case 'l':
224 	    log_option = TRUE;
225 	    if (!open_dump(optarg))
226 		usage(FALSE);
227 	    break;
228 	case 'r':
229 	    r_opt = TRUE;
230 	    break;
231 	case OPTS_VERSION:
232 	    show_version(argv);
233 	    ExitProgram(EXIT_SUCCESS);
234 	default:
235 	    usage(ch == OPTS_USAGE);
236 	    /* NOTREACHED */
237 	}
238     }
239 
240     setlocale(LC_ALL, "");
241     initscr();
242     cbreak();
243     noecho();
244     keypad(stdscr, TRUE);
245 
246     do {
247 	do_all(left_at, over_it);
248 	switch (getch()) {
249 	case HELP_KEY_1:
250 	    show_help(stdscr);
251 	    break;
252 	case 'q':
253 	case QUIT:
254 	case ESCAPE:
255 	    done = TRUE;
256 	    break;
257 	case CTRL('L'):
258 	    redrawwin(stdscr);
259 	    break;
260 	case 'd':
261 	    if (log_option)
262 		dump_window(stdscr);
263 #if HAVE_SCR_DUMP
264 	    else
265 		scr_dump(dump_log);
266 #endif
267 	    break;
268 	case 'h':
269 	    if (left_at > ' ')
270 		left_at = prev_char(left_at);
271 	    break;
272 	case 'l':
273 	    left_at = next_char(left_at);
274 	    break;
275 	case 'c':
276 	    c_opt = !c_opt;
277 	    break;
278 	case 'r':
279 	    r_opt = !r_opt;
280 	    break;
281 	case KEY_HOME:
282 	case '0':
283 	    over_it = 0;
284 	    break;
285 	case KEY_END:
286 	case '$':
287 	    over_it = LAST_OVER;
288 	    break;
289 	case KEY_UP:
290 	case 'k':
291 	case '-':
292 	    over_it = prev_over(over_it);
293 	    break;
294 	case KEY_DOWN:
295 	case 'j':
296 	case '+':
297 	    over_it = next_over(over_it);
298 	    break;
299 	}
300     } while (!done);
301 
302     endwin();
303 
304     ExitProgram(EXIT_SUCCESS);
305 }
306 #else
307 int
main(void)308 main(void)
309 {
310     printf("This program requires wide-curses functions\n");
311     ExitProgram(EXIT_FAILURE);
312 }
313 #endif
314