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