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 DF2D5A052B; Thu, 30 Jul 2020 23:07:41 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 981EB1C012; Thu, 30 Jul 2020 23:07:28 +0200 (CEST) Received: from mail-lf1-f67.google.com (mail-lf1-f67.google.com [209.85.167.67]) by dpdk.org (Postfix) with ESMTP id E12AD4C93 for ; Thu, 30 Jul 2020 23:07:24 +0200 (CEST) Received: by mail-lf1-f67.google.com with SMTP id j22so9828686lfm.2 for ; Thu, 30 Jul 2020 14:07:24 -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=XYCaABoYdedZPSIo4qVlr3ueWLydrJpQ6CzLrsrSUPc=; b=WJt8x5x5SkucYxlMm8LamdakOGmgC3J6LGadUKF036LeyBhS5xwZLNRijUQoewV//j 5FeKfT0YK+CyfKky7LwHxoGmJysAEmFdtueQftMTCnw3eVo8q2ic9iiz0jpfeF/f/6/D LMrv88dMEFQWEDiD4LXT8pbwMlGHX7lUNuMCYV3a5ajIUdyGcUKMlSuWEW6S/cx+NASZ VA9LMk0hOc15xlhh+2AJt6r0VVRDvBlqFnuxymdINDIeIPUSoNNc9rr9VdEE5GxsR7gA /IdCjoQGZwbsBKoFFZ8OvJJnJmujotC71XqQ1U4IUCW7QoubzOinFfkxSfr0V2Cviifc hN+w== 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=XYCaABoYdedZPSIo4qVlr3ueWLydrJpQ6CzLrsrSUPc=; b=UQCXDWvJfBFUwsg93QRiDitE+u4hUDO/c6nzmUZhFZKrm+16ZnoviUSCNi2weZco7Q ZZ28RMiF7LVyxesEAE3A5PbH36WNmrYo075LLEI8md6tkce0q4Olzwk0/J51pmN5tviB FyaJGbbIe2+Cr1AWgObwlLX4vJg02ca8owTQdmifgiIPkdLLB6NyGPFJqvfdFdnTk6i9 ecrZM3eUTKds1+G7UBLezvn9VQF6F6JLbqLi75z1Y956Ezo2p7xoRs6d/t8XJcB31Ca0 IVxH3cFzyE4rHU0O7uSr1RykR+xqEtBgFTmi6GoXjjv7qfBoftn2Woy58BJ9V22i4ZvK xqKQ== X-Gm-Message-State: AOAM532+zZ8IRLR0w136tU2hdEnfzUQi6AIwaqjDK+jM2niwuc4qfHwK mZZzx7VXryxeUiO2EGWGtIe/2hfKqCswSQ== X-Google-Smtp-Source: ABdhPJz6Uk1jSXNEJv0iGFfNyV6r24IN38nNDPdBi9jCu4UJRgrFE6NT8Ls75nd3SGNgk/8LWD/4Qw== X-Received: by 2002:a19:228a:: with SMTP id i132mr255178lfi.178.1596143244184; Thu, 30 Jul 2020 14:07:24 -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 y9sm1257763lji.49.2020.07.30.14.07.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Jul 2020 14:07:23 -0700 (PDT) From: Dmitry Kozlyuk To: dev@dpdk.org Cc: Dmitry Malloy , Narcisa Ana Maria Vasile , Fady Bader , Tal Shnaiderman , "Kadam, Pallavi" , Dmitry Kozlyuk , Olivier Matz Date: Fri, 31 Jul 2020 00:06:46 +0300 Message-Id: <20200730210652.14568-3-dmitry.kozliuk@gmail.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20200730210652.14568-1-dmitry.kozliuk@gmail.com> References: <20200620210511.13134-1-dmitry.kozliuk@gmail.com> <20200730210652.14568-1-dmitry.kozliuk@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH v2 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" 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 --- 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 diff --git a/lib/librte_cmdline/Makefile b/lib/librte_cmdline/Makefile index 619d9a242..3d8e84c07 100644 --- a/lib/librte_cmdline/Makefile +++ b/lib/librte_cmdline/Makefile @@ -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 diff --git a/lib/librte_cmdline/cmdline_os_unix.c b/lib/librte_cmdline/cmdline_os_unix.c new file mode 100644 index 000000000..ca47bd19f --- /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 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); +} diff --git a/lib/librte_cmdline/cmdline_private.h b/lib/librte_cmdline/cmdline_private.h index 3b1c70e9f..adc552845 100644 --- a/lib/librte_cmdline/cmdline_private.h +++ b/lib/librte_cmdline/cmdline_private.h @@ -10,13 +10,23 @@ #include #include +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 diff --git a/lib/librte_cmdline/cmdline_socket.c b/lib/librte_cmdline/cmdline_socket.c index 5e4b734d6..e73666f15 100644 --- a/lib/librte_cmdline/cmdline_socket.c +++ b/lib/librte_cmdline/cmdline_socket.c @@ -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); } 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.25.4