1 /* $OpenBSD: test_gets.c,v 1.5 2017/07/07 23:55:21 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 <assert.h> 19 #include <errno.h> 20 #include <locale.h> 21 #include <stdio.h> 22 #include <wchar.h> 23 #include <err.h> 24 25 #include "chared.c" 26 27 /* 28 * Glue for unit tests of libedit/chared.c. 29 * Rather than linking in all the various libedit modules, 30 * provide dummies for those functions called in chared.c. 31 * Most aren't actually called in c_gets(). 32 */ 33 34 #define EL EditLine *el __attribute__((__unused__)) 35 #define UU __attribute__((__unused__)) 36 37 int hist_enlargebuf(EL, size_t oldsz UU, size_t newsz UU) { return 1; } 38 void re_refresh(EL) { } 39 void re_refresh_cursor(EL) { } 40 void terminal_beep(EL) { putchar('\a'); } 41 42 el_action_t 43 ed_end_of_file(EditLine *el, wint_t c UU) { 44 *el->el_line.lastchar = '\0'; 45 return CC_EOF; 46 } 47 48 int 49 el_wgetc(EL, wchar_t *cp) { 50 return (*cp = getwchar()) != WEOF ? 1 : feof(stdin) ? 0 : -1; 51 } 52 53 #undef EL 54 #undef UU 55 56 /* 57 * Unit test steering program for editline/chared.c, c_gets(). 58 */ 59 60 int 61 main() 62 { 63 EditLine el; 64 wchar_t buf[EL_BUFSIZ]; 65 int i, len; 66 67 if (setlocale(LC_CTYPE, "") == NULL) 68 err(1, "setlocale"); 69 if (ch_init(&el) == -1) 70 err(1, "ch_init"); 71 while (feof(stdin) == 0) { 72 errno = 0; 73 if ((len = c_gets(&el, buf, L"$")) == -1) { 74 if (feof(stdin)) 75 fputs("eof:", stdout); 76 if (ferror(stdin)) { 77 fputs("error:", stdout); 78 clearerr(stdin); 79 } 80 printf("%d:", errno); 81 } 82 printf("%d:", len); 83 if (len > 0) { 84 for (i = 0; i < len; i++) 85 putwchar(buf[i]); 86 putchar(':'); 87 for (i = 1; i <= len; i++) 88 putwchar(el.el_line.buffer[i]); 89 puts(":"); 90 } else 91 puts("::"); 92 assert(el.el_line.buffer[0] == '\0'); 93 assert(el.el_line.lastchar == el.el_line.buffer); 94 assert(el.el_line.cursor == el.el_line.buffer); 95 } 96 return 0; 97 } 98