Extract struct terminal and associated functions that set up, save, and
restore terminal parameters. Use existing code as Unix implementation.
Signed-off-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
---
lib/librte_cmdline/Makefile | 4 ++++
lib/librte_cmdline/cmdline_os_unix.c | 27 +++++++++++++++++++++++++++
lib/librte_cmdline/cmdline_private.h | 12 +++++++++++-
lib/librte_cmdline/cmdline_socket.c | 15 ++++-----------
lib/librte_cmdline/cmdline_vt100.c | 1 -
lib/librte_cmdline/meson.build | 4 ++++
6 files changed, 50 insertions(+), 13 deletions(-)
create mode 100644 lib/librte_cmdline/cmdline_os_unix.c
@@ -23,6 +23,10 @@ SRCS-$(CONFIG_RTE_LIBRTE_CMDLINE) += cmdline_vt100.c
SRCS-$(CONFIG_RTE_LIBRTE_CMDLINE) += cmdline_socket.c
SRCS-$(CONFIG_RTE_LIBRTE_CMDLINE) += cmdline_parse_portlist.c
+ifneq ($(CONFIG_RTE_EXEC_ENV_WINDOWS),y)
+SRCS-$(CONFIG_RTE_LIBRTE_CMDLINE) += cmdline_os_unix.c
+endif
+
LDLIBS += -lrte_net -lrte_eal
# install includes
new file mode 100644
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright (c) 2020 Dmitry Kozlyuk
+ */
+
+#include <string.h>
+
+#include "cmdline_private.h"
+
+void
+terminal_adjust(struct terminal *oldterm)
+{
+ struct termios term;
+
+ tcgetattr(0, &oldterm->termios);
+
+ memcpy(&term, &oldterm->termios, sizeof(term));
+ term.c_lflag &= ~(ICANON | ECHO | ISIG);
+ tcsetattr(0, TCSANOW, &term);
+
+ setbuf(stdin, NULL);
+}
+
+void
+terminal_restore(const struct terminal *oldterm)
+{
+ tcsetattr(fileno(stdin), TCSANOW, &oldterm->termios);
+}
@@ -10,13 +10,23 @@
#include <cmdline_rdline.h>
#include <cmdline_parse.h>
+struct terminal {
+ struct termios termios;
+};
+
+/* Disable buffering and echoing, save previous settings to oldterm. */
+void terminal_adjust(struct terminal *oldterm);
+
+/* Restore terminal settings form oldterm. */
+void terminal_restore(const struct terminal *oldterm);
+
struct cmdline {
int s_in;
int s_out;
cmdline_parse_ctx_t *ctx;
struct rdline rdl;
char prompt[RDLINE_PROMPT_SIZE];
- struct termios oldterm;
+ struct terminal oldterm;
};
#endif
@@ -37,18 +37,11 @@ struct cmdline *
cmdline_stdin_new(cmdline_parse_ctx_t *ctx, const char *prompt)
{
struct cmdline *cl;
- struct termios oldterm, term;
-
- tcgetattr(0, &oldterm);
- memcpy(&term, &oldterm, sizeof(term));
- term.c_lflag &= ~(ICANON | ECHO | ISIG);
- tcsetattr(0, TCSANOW, &term);
- setbuf(stdin, NULL);
cl = cmdline_new(ctx, prompt, 0, 1);
- if (cl)
- memcpy(&cl->oldterm, &oldterm, sizeof(term));
+ if (cl != NULL)
+ terminal_adjust(&cl->oldterm);
return cl;
}
@@ -56,8 +49,8 @@ cmdline_stdin_new(cmdline_parse_ctx_t *ctx, const char *prompt)
void
cmdline_stdin_exit(struct cmdline *cl)
{
- if (!cl)
+ if (cl == NULL)
return;
- tcsetattr(fileno(stdin), TCSANOW, &cl->oldterm);
+ terminal_restore(&cl->oldterm);
}
@@ -10,7 +10,6 @@
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
-#include <termios.h>
#include "cmdline_vt100.h"
@@ -25,4 +25,8 @@ headers = files('cmdline.h',
'cmdline_cirbuf.h',
'cmdline_parse_portlist.h')
+if not is_windows
+ sources += files('cmdline_os_unix.c')
+endif
+
deps += ['net']