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