From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 47FB8A04C0; Mon, 28 Sep 2020 23:52:10 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 8FFA11D6DF; Mon, 28 Sep 2020 23:51:39 +0200 (CEST) Received: from mail-lj1-f196.google.com (mail-lj1-f196.google.com [209.85.208.196]) by dpdk.org (Postfix) with ESMTP id 815071D6BF for ; Mon, 28 Sep 2020 23:51:35 +0200 (CEST) Received: by mail-lj1-f196.google.com with SMTP id u4so2268735ljd.10 for ; Mon, 28 Sep 2020 14:51:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=MCsgmi+B1iYyuYfYEGUH8jUBju8b4BGi/vJmbVVi3P8=; b=PdAdJsABAZjP95ZIT4kHu50yVbd0QCzbIwq6lHA1lGq6cTeu2PZT4h69HmHjSY73Ft KTh1CwvfuHKmI7H9BC1hDI22w1XZmEim29/vQs6tq53E1Ut5xL0jnpegneoXT0MpZrUj 2gUdBEm+Ji2m5EpoxJHH1H983wqmAcBtrCxFak2Gz5iYViW5FrUdYIlG3MMOhlYLW1W4 wv70qtl6Np7EWmOVk0WUlUBHOAbG30f8nov3k82HLuWHwYR1TKL/KGeC3RqBeyzFiQrM eKRJy/KZy/ZqQqvmgkGVcxjiYTvw0bK0fig5+3BxnqcDKKnaypO7rJDnNvQ3y1DTv0fl eICA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=MCsgmi+B1iYyuYfYEGUH8jUBju8b4BGi/vJmbVVi3P8=; b=eIPu4w/Oypnk5VAriHbrI7OHGAmY6lzPwjoRJ8HE4Cem2B0PoeEL2/O5KBXnqSAE8s fmmJVaFjNN5IG5Fz+yY97JQ/mlFgM+XX1MH2AUIb5K4QqBm+pAVh9boiQ20jMXuEuHsU Mhnmx9SvueWxnfN5ZF+tRiKA9Wg7erU4lx7tu/GssbHU5BxbcruqDIIBtfZbG4guMuE7 jxvumMEBqHH3jsx4CqeJ/j/ddmJ/aFEPyv5JwoYfkofGPCE3Jxk8yy2pSbpREaAcbF4g aRm+Mo9AKcvHhRaZWi/XClulPPvIlTsO1XUjs7FmbtjBqrc6W3ckr5klHizFQnosR5Wr mdYQ== X-Gm-Message-State: AOAM5302CbW8EVXYtdwIAN93+kiYdzWGQ5rpmpVMzsCqSV1GQwGhCQhd GQFnULxF+P8Rsz1OgAVps9j069kSYmCzs4dv X-Google-Smtp-Source: ABdhPJyo+uyfQJ0exCmP72DQZoPx8swYlHY/Na74BBNt58L2yfyGtJ8kAHhqPTuY03I2v6nuABVawA== X-Received: by 2002:a2e:9089:: with SMTP id l9mr178015ljg.118.1601329893718; Mon, 28 Sep 2020 14:51:33 -0700 (PDT) Received: from localhost.localdomain (broadband-37-110-65-23.ip.moscow.rt.ru. [37.110.65.23]) by smtp.gmail.com with ESMTPSA id l11sm2954893lfk.113.2020.09.28.14.51.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Sep 2020 14:51:33 -0700 (PDT) From: Dmitry Kozlyuk To: dev@dpdk.org Cc: "Kinsella, Ray" , Khoa To , Stephen Hemminger , Ferruh Yigit , Dmitry Kozlyuk , Olivier Matz Date: Tue, 29 Sep 2020 00:50:47 +0300 Message-Id: <20200928215052.23627-3-dmitry.kozliuk@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200928215052.23627-1-dmitry.kozliuk@gmail.com> References: <20200730210652.14568-1-dmitry.kozliuk@gmail.com> <20200928215052.23627-1-dmitry.kozliuk@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH v3 2/7] cmdline: add internal wrappers for terminal handling X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Add functions that set up, save, and restore terminal parameters. Use existing code as Unix implementation. Signed-off-by: Dmitry Kozlyuk --- 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 --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 + +#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 + +/* 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 #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 #include #include -#include #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'] -- 2.28.0