1 /*	$OpenBSD: test_read_char.c,v 1.5 2017/07/05 15:31:45 bluhm Exp $	*/
2 /*
3  * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <err.h>
19 #include <locale.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 
23 #include "read.c"
24 #include "glue.c"
25 
26 /*
27  * Unit test steering program for editline/read.c, read_char().
28  * Reads from standard input until read_char() returns 0.
29  * Writes the code points read to standard output in %x format.
30  * If EILSEQ is set after read_char(), indicating that there were some
31  * garbage bytes before the character, the code point gets * prefixed.
32  * The return value is indicated by appending to the code point:
33  * a comma for 1, a full stop for 0, [%d] otherwise.
34  * Errors out on unexpected failure (setlocale failure, malloc
35  * failure, or unexpected errno).
36  * Since ENOMSG is very unlikely to occur, it is used to make
37  * sure that read_char() doesn't clobber errno.
38  */
39 
40 int
41 main(void)
42 {
43 	EditLine el;
44 	int irc;
45 	wchar_t cp;
46 
47 	if (setlocale(LC_CTYPE, "") == NULL)
48 		err(1, "setlocale");
49 	el.el_flags = CHARSET_IS_UTF8;
50 	el.el_infd = STDIN_FILENO;
51 	if ((el.el_signal = calloc(1, sizeof(*el.el_signal))) == NULL)
52 		err(1, NULL);
53 	do {
54 		errno = ENOMSG;
55 		irc = read_char(&el, &cp);
56 		switch (errno) {
57 		case ENOMSG:
58 			break;
59 		case EILSEQ:
60 			putchar('*');
61 			break;
62 		default:
63 			err(1, NULL);
64 		}
65 		printf("%x", cp);
66 		switch (irc) {
67 		case 1:
68 			putchar(',');
69 			break;
70 		case 0:
71 			putchar('.');
72 			break;
73 		default:
74 			printf("[%d]", irc);
75 			break;
76 		}
77 	} while (irc != 0);
78 	return 0;
79 }
80