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