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