diff mbox series

[v3,2/7] cmdline: add internal wrappers for terminal handling

Message ID 20200928215052.23627-3-dmitry.kozliuk@gmail.com (mailing list archive)
State Accepted, archived
Delegated to: Thomas Monjalon
Headers show
Series cmdline: support Windows | expand

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Dmitry Kozlyuk Sept. 28, 2020, 9:50 p.m. UTC
Add 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/cmdline_os_unix.c | 27 +++++++++++++++++++++++++++
 lib/librte_cmdline/cmdline_private.h | 16 ++++++++++++++++
 lib/librte_cmdline/cmdline_socket.c  | 16 +++++-----------
 lib/librte_cmdline/cmdline_vt100.c   |  1 -
 lib/librte_cmdline/meson.build       |  4 ++++
 5 files changed, 52 insertions(+), 12 deletions(-)
 create mode 100644 lib/librte_cmdline/cmdline_os_unix.c
 create mode 100644 lib/librte_cmdline/cmdline_private.h
diff mbox series

Patch

diff --git a/lib/librte_cmdline/cmdline_os_unix.c b/lib/librte_cmdline/cmdline_os_unix.c
new file mode 100644
index 000000000..d50eb1c65
--- /dev/null
+++ b/lib/librte_cmdline/cmdline_os_unix.c
@@ -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 cmdline *cl)
+{
+	struct termios term;
+
+	tcgetattr(0, &cl->oldterm);
+
+	memcpy(&term, &cl->oldterm, sizeof(term));
+	term.c_lflag &= ~(ICANON | ECHO | ISIG);
+	tcsetattr(0, TCSANOW, &term);
+
+	setbuf(stdin, NULL);
+}
+
+void
+terminal_restore(const struct cmdline *cl)
+{
+	tcsetattr(fileno(stdin), TCSANOW, &cl->oldterm);
+}
diff --git a/lib/librte_cmdline/cmdline_private.h b/lib/librte_cmdline/cmdline_private.h
new file mode 100644
index 000000000..50326e8a4
--- /dev/null
+++ b/lib/librte_cmdline/cmdline_private.h
@@ -0,0 +1,16 @@ 
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright (c) 2020 Dmitry Kozlyuk
+ */
+
+#ifndef _CMDLINE_PRIVATE_H_
+#define _CMDLINE_PRIVATE_H_
+
+#include <cmdline.h>
+
+/* Disable buffering and echoing, save previous settings to oldterm. */
+void terminal_adjust(struct cmdline *cl);
+
+/* Restore terminal settings form oldterm. */
+void terminal_restore(const struct cmdline *cl);
+
+#endif
diff --git a/lib/librte_cmdline/cmdline_socket.c b/lib/librte_cmdline/cmdline_socket.c
index 6c89d2171..998e8ade2 100644
--- a/lib/librte_cmdline/cmdline_socket.c
+++ b/lib/librte_cmdline/cmdline_socket.c
@@ -13,6 +13,7 @@ 
 #include <fcntl.h>
 
 #include "cmdline.h"
+#include "cmdline_private.h"
 #include "cmdline_socket.h"
 
 struct cmdline *
@@ -36,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);
 
 	return cl;
 }
@@ -55,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);
 }
diff --git a/lib/librte_cmdline/cmdline_vt100.c b/lib/librte_cmdline/cmdline_vt100.c
index 662fc7345..bb968dd5f 100644
--- a/lib/librte_cmdline/cmdline_vt100.c
+++ b/lib/librte_cmdline/cmdline_vt100.c
@@ -10,7 +10,6 @@ 
 #include <string.h>
 #include <stdarg.h>
 #include <ctype.h>
-#include <termios.h>
 
 #include "cmdline_vt100.h"
 
diff --git a/lib/librte_cmdline/meson.build b/lib/librte_cmdline/meson.build
index 7fc54ff1a..5c9e8886d 100644
--- a/lib/librte_cmdline/meson.build
+++ b/lib/librte_cmdline/meson.build
@@ -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']