summaryrefslogtreecommitdiffstats
path: root/regress/lib/libedit/chared/test_gets.c
blob: f8c918eb053751fc59ea2f398a0d1a787195e186 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*	$OpenBSD: test_gets.c,v 1.5 2017/07/07 23:55:21 bluhm Exp $	*/
/*
 * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <assert.h>
#include <errno.h>
#include <locale.h>
#include <stdio.h>
#include <wchar.h>
#include <err.h>

#include "chared.c"

/*
 * Glue for unit tests of libedit/chared.c.
 * Rather than linking in all the various libedit modules,
 * provide dummies for those functions called in chared.c.
 * Most aren't actually called in c_gets().
 */

#define EL EditLine *el __attribute__((__unused__))
#define UU __attribute__((__unused__))

int hist_enlargebuf(EL, size_t oldsz UU, size_t newsz UU) { return 1; }
void re_refresh(EL) { }
void re_refresh_cursor(EL) { }
void terminal_beep(EL) { putchar('\a'); }

el_action_t
ed_end_of_file(EditLine *el, wint_t c UU) {
	*el->el_line.lastchar = '\0';
	return CC_EOF;
}

int
el_wgetc(EL, wchar_t *cp) {
	return (*cp = getwchar()) != WEOF ? 1 : feof(stdin) ? 0 : -1;
}

#undef EL
#undef UU

/*
 * Unit test steering program for editline/chared.c, c_gets().
 */

int
main()
{
	EditLine el;
	wchar_t buf[EL_BUFSIZ];
	int i, len;

	if (setlocale(LC_CTYPE, "") == NULL)
		err(1, "setlocale");
	if (ch_init(&el) == -1)
		err(1, "ch_init");
	while (feof(stdin) == 0) {
		errno = 0;
		if ((len = c_gets(&el, buf, L"$")) == -1) {
			if (feof(stdin))
				fputs("eof:", stdout);
			if (ferror(stdin)) {
				fputs("error:", stdout);
				clearerr(stdin);
			}
			printf("%d:", errno);
		}
		printf("%d:", len);
		if (len > 0) {
			for (i = 0; i < len; i++)
				putwchar(buf[i]);
			putchar(':');
			for (i = 1; i <= len; i++)
				putwchar(el.el_line.buffer[i]);
			puts(":");
		} else
			puts("::");
		assert(el.el_line.buffer[0] == '\0');
		assert(el.el_line.lastchar == el.el_line.buffer);
		assert(el.el_line.cursor == el.el_line.buffer);
	}
	return 0;
}