missing ncurses sources
[tomato.git] / release / src / router / libncurses / test / test_addchstr.c
blob815ccc595bf98648942ffe65b45ec144110fd186
1 /****************************************************************************
2 * Copyright (c) 2009,2010 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 ****************************************************************************/
29 * $Id: test_addchstr.c,v 1.13 2010/12/12 01:28:24 tom Exp $
31 * Demonstrate the waddchstr() and waddch functions.
32 * Thomas Dickey - 2009/9/12
35 #include <test.priv.h>
37 #include <linedata.h>
39 #undef MvAddStr
40 #undef MvWAddStr
42 #define AddNStr addchnstr
43 #define AddStr addchstr
44 #define MvAddNStr (void) mvaddchnstr
45 #define MvAddStr (void) mvaddchstr
46 #define MvWAddNStr (void) mvwaddchnstr
47 #define MvWAddStr (void) mvwaddchstr
48 #define WAddNStr waddchnstr
49 #define WAddStr waddchstr
51 #define AddCh addch
52 #define WAddCh waddch
54 #define MY_TABSIZE 8
56 typedef enum {
57 oDefault = 0,
58 oMove = 1,
59 oWindow = 2,
60 oMoveWindow = 3
61 } Options;
63 static bool m_opt = FALSE;
64 static bool pass_ctls = FALSE;
65 static bool w_opt = FALSE;
66 static int n_opt = -1;
68 static attr_t show_attr;
69 static chtype *temp_buffer;
70 static size_t temp_length;
72 #define TempBuffer(source_cast)
74 static size_t
75 ChLen(const char *source)
77 size_t result = strlen(source);
79 if (!pass_ctls) {
80 size_t adjust = 0;
81 size_t n;
83 for (n = 0; n < result; ++n) {
84 const char *s = unctrl(UChar(source[n]));
85 if (s != 0) {
86 adjust += (strlen(s) - 1);
89 result += adjust;
91 return result;
94 static chtype *
95 ChStr(const char *source)
97 if (source != 0) {
98 size_t need = ChLen(source) + 1;
99 int n = 0;
101 if (need > temp_length) {
102 temp_length = need * 2;
103 temp_buffer = typeRealloc(chtype, temp_length, temp_buffer);
105 do {
106 const char *s;
107 chtype ch = UChar(*source++);
108 if (!pass_ctls && (s = unctrl(ch)) != 0) {
109 while (*s != '\0') {
110 temp_buffer[n++] = UChar(*s++);
112 } else {
113 temp_buffer[n++] = ch;
115 } while (source[0] != 0);
116 temp_buffer[n] = 0;
117 } else if (temp_buffer != 0) {
118 free(temp_buffer);
119 temp_buffer = 0;
120 temp_length = 0;
122 return temp_buffer;
125 /* color the strings drawn in the workspace */
126 static chtype *
127 ChStr2(const char *source)
129 size_t len = ChLen(source);
130 size_t n;
131 chtype *result = ChStr(source);
132 for (n = 0; n < len; ++n) {
133 result[n] |= show_attr;
135 return result;
138 static void
139 legend(WINDOW *win, int level, Options state, char *buffer, int length)
141 const char *showstate;
143 switch (state) {
144 default:
145 case oDefault:
146 showstate = "";
147 break;
148 case oMove:
149 showstate = " (mvXXX)";
150 break;
151 case oWindow:
152 showstate = " (winXXX)";
153 break;
154 case oMoveWindow:
155 showstate = " (mvwinXXX)";
156 break;
159 wmove(win, 0, 0);
160 wprintw(win,
161 "The Strings/Chars displays should match. Enter any characters, except:\n");
162 wprintw(win,
163 "down-arrow or ^N to repeat on next line, ^W for inner window, ESC to exit.\n");
164 wclrtoeol(win);
165 wprintw(win, "Level %d,%s added %d characters <%s>", level,
166 showstate, length, buffer);
169 static int
170 ColOf(char *buffer, int length, int margin)
172 int n;
173 int result;
175 for (n = 0, result = margin + 1; n < length; ++n) {
176 int ch = UChar(buffer[n]);
177 switch (ch) {
178 case '\n':
179 /* actually newline should clear the remainder of the line
180 * and move to the next line - but that seems a little awkward
181 * in this example.
183 case '\r':
184 result = 0;
185 break;
186 case '\b':
187 if (result > 0)
188 --result;
189 break;
190 case '\t':
191 result += (MY_TABSIZE - (result % MY_TABSIZE));
192 break;
193 case '\177':
194 result += 2;
195 break;
196 default:
197 ++result;
198 if (ch < 32)
199 ++result;
200 break;
203 return result;
206 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
207 static void
208 test_adds(int level)
210 static bool first = TRUE;
212 int ch;
213 int limit;
214 int row = 1;
215 int col;
216 int row2, col2;
217 int length;
218 char buffer[BUFSIZ];
219 WINDOW *look = 0;
220 WINDOW *work = 0;
221 WINDOW *show = 0;
222 int margin = (2 * MY_TABSIZE) - 1;
223 Options option = (Options) ((unsigned) (m_opt
224 ? oMove
225 : oDefault)
226 | (unsigned) ((w_opt || (level > 0))
227 ? oWindow
228 : oDefault));
230 if (first) {
231 static char cmd[80];
232 setlocale(LC_ALL, "");
234 putenv(strcpy(cmd, "TABSIZE=8"));
236 initscr();
237 (void) cbreak(); /* take input chars one at a time, no wait for \n */
238 (void) noecho(); /* don't echo input */
239 keypad(stdscr, TRUE);
242 limit = LINES - 5;
243 if (level > 0) {
244 look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
245 work = newwin(limit - 2, COLS - (2 * level), 1, level);
246 show = newwin(4, COLS, limit + 1, 0);
247 box(look, 0, 0);
248 wnoutrefresh(look);
249 limit -= 2;
250 } else {
251 work = stdscr;
252 show = derwin(stdscr, 4, COLS, limit + 1, 0);
254 keypad(work, TRUE);
256 for (col = margin + 1; col < COLS; col += MY_TABSIZE)
257 MvWVLine(work, row, col, '.', limit - 2);
259 MvWVLine(work, row, margin, ACS_VLINE, limit - 2);
260 MvWVLine(work, row, margin + 1, ACS_VLINE, limit - 2);
261 limit /= 2;
263 MvWAddChStr(work, 1, 2, ChStr("String"));
264 MvWAddChStr(work, limit + 1, 2, ChStr("Chars"));
265 wnoutrefresh(work);
267 buffer[length = 0] = '\0';
268 legend(show, level, option, buffer, length);
269 wnoutrefresh(show);
271 doupdate();
274 * Show the characters added in color, to distinguish from those that
275 * are shifted.
277 if (has_colors()) {
278 start_color();
279 init_pair(1, COLOR_WHITE, COLOR_BLUE);
280 show_attr = COLOR_PAIR(1);
281 wbkgdset(work, show_attr | ' ');
282 } else {
283 show_attr = A_STANDOUT;
286 while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
287 wmove(work, row, margin + 1);
288 switch (ch) {
289 case key_RECUR:
290 test_adds(level + 1);
292 touchwin(look);
293 touchwin(work);
294 touchwin(show);
296 wnoutrefresh(look);
297 wnoutrefresh(work);
298 wnoutrefresh(show);
300 doupdate();
301 break;
302 case key_NEWLINE:
303 if (row < limit) {
304 ++row;
305 /* put the whole string in, all at once */
306 col2 = margin + 1;
307 switch (option) {
308 case oDefault:
309 if (n_opt > 1) {
310 for (col = 0; col < length; col += n_opt) {
311 col2 = ColOf(buffer, col, margin);
312 if (move(row, col2) != ERR) {
313 AddNStr(ChStr2(buffer + col), LEN(col));
316 } else {
317 if (move(row, col2) != ERR) {
318 AddStr(ChStr2(buffer));
321 break;
322 case oMove:
323 if (n_opt > 1) {
324 for (col = 0; col < length; col += n_opt) {
325 col2 = ColOf(buffer, col, margin);
326 MvAddNStr(row, col2, ChStr2(buffer + col), LEN(col));
328 } else {
329 MvAddStr(row, col2, ChStr2(buffer));
331 break;
332 case oWindow:
333 if (n_opt > 1) {
334 for (col = 0; col < length; col += n_opt) {
335 col2 = ColOf(buffer, col, margin);
336 if (wmove(work, row, col2) != ERR) {
337 WAddNStr(work, ChStr2(buffer + col), LEN(col));
340 } else {
341 if (wmove(work, row, col2) != ERR) {
342 WAddStr(work, ChStr2(buffer));
345 break;
346 case oMoveWindow:
347 if (n_opt > 1) {
348 for (col = 0; col < length; col += n_opt) {
349 col2 = ColOf(buffer, col, margin);
350 MvWAddNStr(work, row, col2, ChStr2(buffer + col),
351 LEN(col));
353 } else {
354 MvWAddStr(work, row, col2, ChStr2(buffer));
356 break;
359 /* do the corresponding single-character add */
360 row2 = limit + row;
361 for (col = 0; col < length; ++col) {
362 col2 = ColOf(buffer, col, margin);
363 switch (option) {
364 case oDefault:
365 if (move(row2, col2) != ERR) {
366 AddCh(UChar(buffer[col]));
368 break;
369 case oMove:
370 MvAddCh(row2, col2, UChar(buffer[col]));
371 break;
372 case oWindow:
373 if (wmove(work, row2, col2) != ERR) {
374 WAddCh(work, UChar(buffer[col]));
376 break;
377 case oMoveWindow:
378 MvWAddCh(work, row2, col2, UChar(buffer[col]));
379 break;
382 } else {
383 beep();
385 break;
386 case KEY_BACKSPACE:
387 ch = '\b';
388 /* FALLTHRU */
389 default:
390 if (ch <= 0 || ch > 255) {
391 beep();
392 break;
394 buffer[length++] = (char) ch;
395 buffer[length] = '\0';
397 /* put the string in, one character at a time */
398 col = ColOf(buffer, length - 1, margin);
399 switch (option) {
400 case oDefault:
401 if (move(row, col) != ERR) {
402 AddStr(ChStr2(buffer + length - 1));
404 break;
405 case oMove:
406 MvAddStr(row, col, ChStr2(buffer + length - 1));
407 break;
408 case oWindow:
409 if (wmove(work, row, col) != ERR) {
410 WAddStr(work, ChStr2(buffer + length - 1));
412 break;
413 case oMoveWindow:
414 MvWAddStr(work, row, col, ChStr2(buffer + length - 1));
415 break;
418 /* do the corresponding single-character add */
419 switch (option) {
420 case oDefault:
421 if (move(limit + row, col) != ERR) {
422 AddCh(UChar(ch));
424 break;
425 case oMove:
426 MvAddCh(limit + row, col, UChar(ch));
427 break;
428 case oWindow:
429 if (wmove(work, limit + row, col) != ERR) {
430 WAddCh(work, UChar(ch));
432 break;
433 case oMoveWindow:
434 MvWAddCh(work, limit + row, col, UChar(ch));
435 break;
438 wnoutrefresh(work);
440 legend(show, level, option, buffer, length);
441 wnoutrefresh(show);
443 doupdate();
444 break;
447 if (level > 0) {
448 delwin(show);
449 delwin(work);
450 delwin(look);
454 static void
455 usage(void)
457 static const char *tbl[] =
459 "Usage: test_addchstr [options]"
461 ,"Options:"
462 ," -f FILE read data from given file"
463 ," -n NUM limit string-adds to NUM bytes on ^N replay"
464 ," -m perform wmove/move separately from add-functions"
465 ," -p pass-thru control characters without using unctrl()"
466 ," -w use window-parameter even when stdscr would be implied"
468 unsigned n;
469 for (n = 0; n < SIZEOF(tbl); ++n)
470 fprintf(stderr, "%s\n", tbl[n]);
471 ExitProgram(EXIT_FAILURE);
475 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
477 int ch;
479 setlocale(LC_ALL, "");
481 while ((ch = getopt(argc, argv, "f:mn:pw")) != -1) {
482 switch (ch) {
483 case 'f':
484 init_linedata(optarg);
485 break;
486 case 'm':
487 m_opt = TRUE;
488 break;
489 case 'n':
490 n_opt = atoi(optarg);
491 if (n_opt == 0)
492 n_opt = -1;
493 break;
494 case 'p':
495 pass_ctls = TRUE;
496 break;
497 case 'w':
498 w_opt = TRUE;
499 break;
500 default:
501 usage();
502 break;
505 if (optind < argc)
506 usage();
508 test_adds(0);
509 endwin();
510 ExitProgram(EXIT_SUCCESS);