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 #ifndef LINE_MAX 15 /** 16 * The LINE_MAX value is derived from POSIX. 17 * Windows environment may not have POSIX definitions. 18 */ 19 #define LINE_MAX 2048 20 #endif 21 #else 22 #include <termios.h> 23 #endif 24 25 #include <cmdline.h> 26 27 #define RDLINE_BUF_SIZE LINE_MAX 28 #define RDLINE_PROMPT_SIZE 32 29 #define RDLINE_VT100_BUF_SIZE 8 30 #define RDLINE_HISTORY_BUF_SIZE BUFSIZ 31 #define RDLINE_HISTORY_MAX_LINE 64 32 33 struct rdline { 34 volatile enum rdline_status status; 35 /* rdline bufs */ 36 struct cirbuf left; 37 struct cirbuf right; 38 char left_buf[RDLINE_BUF_SIZE+2]; /* reserve 2 chars for the \n\0 */ 39 char right_buf[RDLINE_BUF_SIZE]; 40 41 char prompt[RDLINE_PROMPT_SIZE]; 42 unsigned int prompt_size; 43 44 char kill_buf[RDLINE_BUF_SIZE]; 45 unsigned int kill_size; 46 47 /* history */ 48 struct cirbuf history; 49 char history_buf[RDLINE_HISTORY_BUF_SIZE]; 50 int history_cur_line; 51 52 /* callbacks and func pointers */ 53 rdline_write_char_t *write_char; 54 rdline_validate_t *validate; 55 rdline_complete_t *complete; 56 57 /* vt100 parser */ 58 struct cmdline_vt100 vt100; 59 60 /* opaque pointer */ 61 void *opaque; 62 }; 63 64 #ifdef RTE_EXEC_ENV_WINDOWS 65 struct terminal { 66 DWORD input_mode; 67 DWORD output_mode; 68 int is_console_input; 69 int is_console_output; 70 }; 71 #endif 72 73 struct cmdline { 74 int s_in; 75 int s_out; 76 cmdline_parse_ctx_t *ctx; 77 struct rdline rdl; 78 char prompt[RDLINE_PROMPT_SIZE]; 79 #ifdef RTE_EXEC_ENV_WINDOWS 80 struct terminal oldterm; 81 char repeated_char; 82 WORD repeat_count; 83 #else 84 struct termios oldterm; 85 #endif 86 }; 87 88 /* Disable buffering and echoing, save previous settings to oldterm. */ 89 void terminal_adjust(struct cmdline *cl); 90 91 /* Restore terminal settings form oldterm. */ 92 void terminal_restore(const struct cmdline *cl); 93 94 /* Read one character from input. */ 95 ssize_t cmdline_read_char(struct cmdline *cl, char *c); 96 97 /* Force current cmdline read to unblock. */ 98 void cmdline_cancel(struct cmdline *cl); 99 100 /* vdprintf(3) */ 101 __rte_format_printf(2, 0) 102 int cmdline_vdprintf(int fd, const char *format, va_list op); 103 104 int rdline_init(struct rdline *rdl, 105 rdline_write_char_t *write_char, 106 rdline_validate_t *validate, 107 rdline_complete_t *complete, 108 void *opaque); 109 110 #endif 111