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 c_macro_t *ma; 38 int irc; 39 Char ch; 40 el_action_t cmdnum; 41 42 if (setlocale(LC_CTYPE, "") == NULL) 43 err(1, "setlocale"); 44 45 el.el_errno = ENOMSG; 46 el.el_flags = CHARSET_IS_UTF8; 47 el.el_infd = STDIN_FILENO; 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 ma = &el.el_chared.c_macro; 60 ma->level = -1; 61 ma->offset = 0; 62 if ((ma->macro = calloc(EL_MAXMACRO, sizeof(*ma->macro))) == NULL) 63 err(1, NULL); 64 65 if (read_init(&el) != 0) 66 err(1, "read_init"); 67 68 do { 69 irc = read_getcmd(&el, &cmdnum, &ch); 70 switch (irc) { 71 case OKCMD: 72 fputs("OK ", stdout); 73 switch (cmdnum) { 74 case ED_COMMAND: 75 fputs("command", stdout); 76 break; 77 case ED_INSERT: 78 fputs("insert", stdout); 79 break; 80 default: 81 printf("cmdnum=%u", cmdnum); 82 break; 83 } 84 printf(" L'%lc'", ch); 85 break; 86 case 0: 87 fputs("EOF", stdout); 88 break; 89 default: 90 printf("ret(%d)", irc); 91 break; 92 } 93 if (el.el_errno != 0) 94 printf(" el_errno=%d", el.el_errno); 95 if (ma->level > -1) 96 printf(" macro[%d]=%ls(%d)", ma->level, 97 *ma->macro, ma->offset); 98 putchar('\n'); 99 } while (irc); 100 101 return 0; 102 } 103