1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (c) 2020 Dmitry Kozlyuk 3 */ 4 5 #ifndef _CMDLINE_PRIVATE_H_ 6 #define _CMDLINE_PRIVATE_H_ 7 8 #include <stdarg.h> 9 10 #include <rte_common.h> 11 #include <rte_os_shim.h> 12 #ifdef RTE_EXEC_ENV_WINDOWS 13 #include <rte_windows.h> 14 #else 15 #include <termios.h> 16 #endif 17 18 #include <cmdline.h> 19 20 #define RDLINE_BUF_SIZE 512 21 #define RDLINE_PROMPT_SIZE 32 22 #define RDLINE_VT100_BUF_SIZE 8 23 #define RDLINE_HISTORY_BUF_SIZE BUFSIZ 24 #define RDLINE_HISTORY_MAX_LINE 64 25 26 enum rdline_status { 27 RDLINE_INIT, 28 RDLINE_RUNNING, 29 RDLINE_EXITED 30 }; 31 32 struct rdline { 33 enum rdline_status status; 34 /* rdline bufs */ 35 struct cirbuf left; 36 struct cirbuf right; 37 char left_buf[RDLINE_BUF_SIZE+2]; /* reserve 2 chars for the \n\0 */ 38 char right_buf[RDLINE_BUF_SIZE]; 39 40 char prompt[RDLINE_PROMPT_SIZE]; 41 unsigned int prompt_size; 42 43 char kill_buf[RDLINE_BUF_SIZE]; 44 unsigned int kill_size; 45 46 /* history */ 47 struct cirbuf history; 48 char history_buf[RDLINE_HISTORY_BUF_SIZE]; 49 int history_cur_line; 50 51 /* callbacks and func pointers */ 52 rdline_write_char_t *write_char; 53 rdline_validate_t *validate; 54 rdline_complete_t *complete; 55 56 /* vt100 parser */ 57 struct cmdline_vt100 vt100; 58 59 /* opaque pointer */ 60 void *opaque; 61 }; 62 63 #ifdef RTE_EXEC_ENV_WINDOWS 64 struct terminal { 65 DWORD input_mode; 66 DWORD output_mode; 67 int is_console_input; 68 int is_console_output; 69 }; 70 #endif 71 72 struct cmdline { 73 int s_in; 74 int s_out; 75 cmdline_parse_ctx_t *ctx; 76 struct rdline rdl; 77 char prompt[RDLINE_PROMPT_SIZE]; 78 #ifdef RTE_EXEC_ENV_WINDOWS 79 struct terminal oldterm; 80 char repeated_char; 81 WORD repeat_count; 82 #else 83 struct termios oldterm; 84 #endif 85 }; 86 87 /* Disable buffering and echoing, save previous settings to oldterm. */ 88 void terminal_adjust(struct cmdline *cl); 89 90 /* Restore terminal settings form oldterm. */ 91 void terminal_restore(const struct cmdline *cl); 92 93 /* Check if a single character can be read from input. */ 94 int cmdline_poll_char(struct cmdline *cl); 95 96 /* Read one character from input. */ 97 ssize_t cmdline_read_char(struct cmdline *cl, char *c); 98 99 /* vdprintf(3) */ 100 __rte_format_printf(2, 0) 101 int cmdline_vdprintf(int fd, const char *format, va_list op); 102 103 int rdline_init(struct rdline *rdl, 104 rdline_write_char_t *write_char, 105 rdline_validate_t *validate, 106 rdline_complete_t *complete, 107 void *opaque); 108 109 #endif 110