xref: /openbsd/regress/lib/libedit/read/test_getcmd.c (revision 3cab2bb3)
1 /*	$OpenBSD: test_getcmd.c,v 1.8 2017/07/05 15:31:45 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 <err.h>
20 #include <errno.h>
21 #include <locale.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 
25 #include "read.c"
26 #include "glue.c"
27 
28 #define N_KEYS 256
29 
30 /*
31  * Unit test steering program for editline/read.c, read_getcmd().
32  */
33 
34 int
35 main()
36 {
37 	EditLine	 el;
38 	struct macros	*ma;
39 	int		 irc;
40 	wchar_t		 ch;
41 	el_action_t	 cmdnum;
42 
43 	if (setlocale(LC_CTYPE, "") == NULL)
44 		err(1, "setlocale");
45 
46 	el.el_flags = CHARSET_IS_UTF8;
47 	el.el_infd = STDIN_FILENO;
48 	el.el_state.metanext = 0;
49 
50 	el.el_map.alt = NULL;
51 	if ((el.el_map.key = calloc(N_KEYS, sizeof(el_action_t))) == NULL)
52 		err(1, NULL);
53 	el.el_map.key[(unsigned char)'c'] = ED_SEQUENCE_LEAD_IN;
54 	el.el_map.key[(unsigned char)'i'] = ED_INSERT;
55 	el.el_map.key[(unsigned char)'s'] = ED_SEQUENCE_LEAD_IN;
56 	el.el_map.current = el.el_map.key;
57 	if ((el.el_signal = calloc(1, sizeof(*el.el_signal))) == NULL)
58 		err(1, NULL);
59 
60 	if (read_init(&el) != 0)
61 		err(1, "read_init");
62 	ma = &el.el_read->macros;
63 	el.el_read->read_errno = ENOMSG;
64 
65 	do {
66 		irc = read_getcmd(&el, &cmdnum, &ch);
67 		switch (irc) {
68 		case 0:
69 			fputs("OK ", stdout);
70 			switch (cmdnum) {
71 			case ED_COMMAND:
72 				fputs("command", stdout);
73 				break;
74 			case ED_INSERT:
75 				fputs("insert", stdout);
76 				break;
77 			default:
78 				printf("cmdnum=%u", cmdnum);
79 				break;
80 			}
81 			printf(" L'%lc'", ch);
82 			break;
83 		case -1:
84 			fputs("EOF", stdout);
85 			break;
86 		default:
87 			printf("ret(%d)", irc);
88 			break;
89 		}
90 		if (el.el_read->read_errno != ENOMSG)
91 			printf(" read_errno=%d", el.el_read->read_errno);
92 		if (ma->level > -1)
93 			printf(" macro[%d]=%ls(%d)", ma->level,
94 			    *ma->macro, ma->offset);
95 		putchar('\n');
96 	} while (irc == 0);
97 
98 	return 0;
99 }
100