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 08A75A051C; Sat, 20 Jun 2020 23:05:37 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 3B7941BFC4; Sat, 20 Jun 2020 23:05:24 +0200 (CEST) Received: from mail-lj1-f195.google.com (mail-lj1-f195.google.com [209.85.208.195]) by dpdk.org (Postfix) with ESMTP id CB2D11BFB4 for ; Sat, 20 Jun 2020 23:05:20 +0200 (CEST) Received: by mail-lj1-f195.google.com with SMTP id q19so15299487lji.2 for ; Sat, 20 Jun 2020 14:05:20 -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=OWta57S2jjUk6vpSfbmVEIaMYEfVHjL6mAOmufu/c90/VfRd5AkRIjvznTHRu5Ooqa NSIbm7xG8ecV6ors37grHWNbYGW5X6YVyn6sKiUVJb7z6dlf7oc5hnAyxkQGd0r0OGM0 pCbtcfYoHDgKQ1SS+EJOR37GLQezV3gy7lIOO0H345Gs94f2y/6voBrY6NP8Dq9xA0nL eJ/iawrtd4ZGXplSiPo6nyWmXihbr7a5UwUCMBohl2WjqNbtKKWExS8EfYKCQSMCrY4f 7xsrQ0sFLKU+DqsuI5VcfLffnjDuXlth9l+pol2InGQsChw8F+euvopYEmwRPIP5Y0Iu fNEg== 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=tQyC8i2c1vS8t1HRtbyRinHyF3jGV+yhhsKaBoQJS9tpITrEIExXXwk6ZaBRIt1u1B zmdUyVu+20I72VeWXJXwckDdtUT4CILX3BJ/8R1Inyyiqj0f8OHjIdW4HGomInDVq3Im ONoJrewxB7IeUqdKVk0RX4tpA3Ez3Va3sZ6FFD4Ca9KwbddUN/eY8nM7tFwy4OQmpIqg xBQ9tEeFl3TDatvsLzW4q7G6KNvSMVQyJwB3W1OrmE1P27SAckQaS/c+K36ty8l2do5U panfGjvv/1ROoRQ5WjdOO0xPh0MNHgjJ3BnNKXfX1OgDYd5k/+vrIwRYPfNMlrFHYBjt 4oVw== X-Gm-Message-State: AOAM530yZZFe67GljBTFHtINgb6gOzBflhBsuHl1OEXoC0hTiClYjD7G QsOAPTfRm3yQl8yKom2xlDspN1YyedwDEw== X-Google-Smtp-Source: ABdhPJwa4AV++SBsTrXb4w4BgMBtVD3Tl0OiCMNCMgG8D0au0/T0nCRNFKUH2+eLv3CxAXezCvrd+g== X-Received: by 2002:a2e:7107:: with SMTP id m7mr4625112ljc.342.1592687120253; Sat, 20 Jun 2020 14:05:20 -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 d8sm2653359lfk.27.2020.06.20.14.05.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Jun 2020 14:05:19 -0700 (PDT) From: Dmitry Kozlyuk To: dev@dpdk.org Cc: Dmitry Malloy , Narcisa Ana Maria Vasile , Fady Bader , Tal Shnaiderman , Dmitry Kozlyuk , Olivier Matz Date: Sun, 21 Jun 2020 00:05:05 +0300 Message-Id: <20200620210511.13134-3-dmitry.kozliuk@gmail.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20200620210511.13134-1-dmitry.kozliuk@gmail.com> References: <20200620210511.13134-1-dmitry.kozliuk@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH 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