xref: /openbsd/regress/lib/libedit/chared/test_gets.c (revision 264ca280)
1 /*
2  * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <assert.h>
18 #include <errno.h>
19 #include <locale.h>
20 #include <stdio.h>
21 #include <wchar.h>
22 
23 #include "chared.c"
24 
25 /*
26  * Glue for unit tests of libedit/chared.c.
27  * Rather than linking in all the various libedit modules,
28  * provide dummies for those functions called in chared.c.
29  * Most aren't actually called in c_gets().
30  * Requires "make obj && make depend" in src/lib/libedit.
31  */
32 
33 #define EL EditLine *el __attribute__((__unused__))
34 #define UU __attribute__((__unused__))
35 
36 int hist_enlargebuf(EL, size_t oldsz UU, size_t newsz UU) { return 1; }
37 void re_refresh(EL) { }
38 void re_refresh_cursor(EL) { }
39 void terminal_beep(EL) { putchar('\a'); }
40 
41 el_action_t
42 ed_end_of_file(EditLine *el, wint_t c UU) {
43 	*el->el_line.lastchar = '\0';
44 	return CC_EOF;
45 }
46 
47 int
48 el_wgetc(EL, wchar_t *cp) {
49 	return (*cp = getwchar()) != WEOF ? 1 : feof(stdin) ? 0 : -1;
50 }
51 
52 #undef EL
53 #undef UU
54 
55 /*
56  * Unit test steering program for editline/chared.c, c_gets().
57  */
58 
59 int
60 main()
61 {
62 	EditLine el;
63 	wchar_t buf[EL_BUFSIZ];
64 	int i, len;
65 
66 	if (setlocale(LC_CTYPE, "") == NULL)
67 		err(1, "setlocale");
68 	if (ch_init(&el) == -1)
69 		err(1, "ch_init");
70 	while (feof(stdin) == 0) {
71 		errno = 0;
72 		if ((len = c_gets(&el, buf, L"$")) == -1) {
73 			if (feof(stdin))
74 				fputs("eof:", stdout);
75 			if (ferror(stdin)) {
76 				fputs("error:", stdout);
77 				clearerr(stdin);
78 			}
79 			printf("%d:", errno);
80 		}
81 		printf("%d:", len);
82 		if (len > 0) {
83 			for (i = 0; i < len; i++)
84 				putwchar(buf[i]);
85 			putchar(':');
86 			for (i = 1; i <= len; i++)
87 				putwchar(el.el_line.buffer[i]);
88 			puts(":");
89 		} else
90 			puts("::");
91 		assert(el.el_line.buffer[0] == '\0');
92 		assert(el.el_line.lastchar == el.el_line.buffer);
93 		assert(el.el_line.cursor == el.el_line.buffer);
94 	}
95 	return 0;
96 }
97