From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from dpdk.org (dpdk.org [92.243.14.124])
	by inbox.dpdk.org (Postfix) with ESMTP id 6A99DA051C;
	Sat, 20 Jun 2020 23:05:47 +0200 (CEST)
Received: from [92.243.14.124] (localhost [127.0.0.1])
	by dpdk.org (Postfix) with ESMTP id A51841BFCA;
	Sat, 20 Jun 2020 23:05:25 +0200 (CEST)
Received: from mail-lj1-f194.google.com (mail-lj1-f194.google.com
 [209.85.208.194]) by dpdk.org (Postfix) with ESMTP id 927D21BFBA
 for <dev@dpdk.org>; Sat, 20 Jun 2020 23:05:21 +0200 (CEST)
Received: by mail-lj1-f194.google.com with SMTP id 9so15267202ljc.8
 for <dev@dpdk.org>; Sat, 20 Jun 2020 14:05:21 -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=9KBD5tDXCqm5NI+e5AeRBqTs67CJlCSTNrTUeRSPhGk=;
 b=dof9+x13oX6Tg2s7FFvW4s2FLPusZBYOcrnErGm7Bq0mZBqYBa8CgjeqD5TIR/RlF7
 iqu5L4oJnunCLmU+vt2VGpOqItXIRbjA2j+shMf3rvpU+ykE+Q2tVNIVRgFt4NeFtPpR
 WgkDvcW/ELqg3a79yRexTMVoEs0rmKC9FkkYG5xaNxs7x5DC7+0MgQXmU24GmOexO1KX
 6+++2e0m8AsUB8p/OcJPYTGigWgSGWOA3f1mzzpRpasN0AunsnBOfwRqZB12trs1FeYG
 ny52zsFM+Wam6+BlFnrgEB3F7eZ1VHhTPxM5GOnlnsCshpSetNjBZIN9UAUZqb1tZP1x
 Gx/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=9KBD5tDXCqm5NI+e5AeRBqTs67CJlCSTNrTUeRSPhGk=;
 b=dIUZFW2iOA0l25oOdMFb6gm3QYPUo1Ngdki8Vl7KidfAgVktRze8N51nEnjbX+KEEq
 TXGmGqFDr+IS/qAER2GDr6f4vYc9dGzOXvZ7lnPyAHcRmS94z/b7Gf2k7YOMr+DHzI1t
 jVM8ZNBLrRLeH8kX16tlgsYzoqlfZkLY7QPXueIIa2157iY1wDDn4+1QeuboDnRuUax2
 16/Bg8Ws1MBuzDHyWOXcaiecR9LWCKrWsM6MDS0+iXImeWhC07g8kOJ4v6vFcCJWiepS
 gBWUU2nUhijHWGJuZ3TmT6dpokbJc6Ae+MC6H+1eKXgtwOZqVbaURDA1QKr7eOMV6eWY
 36Pw==
X-Gm-Message-State: AOAM530rbM3OHN5Z7VzE5iFDQD8m4/1GUt4VFkjbqhyn4jZSCra2yIK3
 tOdNFKWRqP9t8t+tp1Ua5bnPAtIoBZIK/w==
X-Google-Smtp-Source: ABdhPJw60aG+8Q+7BH+5U1mZufo6jWtVHaQArBMJvmmz7IR9DbfgulY4veGu9OLeDhLHa+yCc0FwVA==
X-Received: by 2002:a2e:9dd8:: with SMTP id x24mr5226076ljj.304.1592687121165; 
 Sat, 20 Jun 2020 14:05:21 -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.20
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Sat, 20 Jun 2020 14:05:20 -0700 (PDT)
From: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
To: dev@dpdk.org
Cc: Dmitry Malloy <dmitrym@microsoft.com>,
 Narcisa Ana Maria Vasile <Narcisa.Vasile@microsoft.com>,
 Fady Bader <fady@mellanox.com>, Tal Shnaiderman <talshn@mellanox.com>,
 Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>,
 Olivier Matz <olivier.matz@6wind.com>
Date: Sun, 21 Jun 2020 00:05:06 +0300
Message-Id: <20200620210511.13134-4-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 3/7] cmdline: add internal wrappers for character
	input
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org
Sender: "dev" <dev-bounces@dpdk.org>

poll(3) is a purely Unix facility, so it cannot be directly used by
common code. read(2) is limited in device support outside of Unix.
Create wrapper functions and implement them for Unix.

Signed-off-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
---
 lib/librte_cmdline/cmdline.c         | 12 +++---------
 lib/librte_cmdline/cmdline_os_unix.c | 20 ++++++++++++++++++++
 lib/librte_cmdline/cmdline_private.h |  6 ++++++
 3 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/lib/librte_cmdline/cmdline.c b/lib/librte_cmdline/cmdline.c
index 6f3fdd598..a04719998 100644
--- a/lib/librte_cmdline/cmdline.c
+++ b/lib/librte_cmdline/cmdline.c
@@ -11,7 +11,6 @@
 #include <stdarg.h>
 #include <inttypes.h>
 #include <fcntl.h>
-#include <poll.h>
 #include <errno.h>
 #include <netinet/in.h>
 
@@ -185,7 +184,6 @@ cmdline_quit(struct cmdline *cl)
 int
 cmdline_poll(struct cmdline *cl)
 {
-	struct pollfd pfd;
 	int status;
 	ssize_t read_status;
 	char c;
@@ -195,16 +193,12 @@ cmdline_poll(struct cmdline *cl)
 	else if (cl->rdl.status == RDLINE_EXITED)
 		return RDLINE_EXITED;
 
-	pfd.fd = cl->s_in;
-	pfd.events = POLLIN;
-	pfd.revents = 0;
-
-	status = poll(&pfd, 1, 0);
+	status = cmdline_poll_char(cl);
 	if (status < 0)
 		return status;
 	else if (status > 0) {
 		c = -1;
-		read_status = read(cl->s_in, &c, 1);
+		read_status = cmdline_read_char(cl, &c);
 		if (read_status < 0)
 			return read_status;
 
@@ -226,7 +220,7 @@ cmdline_interact(struct cmdline *cl)
 
 	c = -1;
 	while (1) {
-		if (read(cl->s_in, &c, 1) <= 0)
+		if (cmdline_read_char(cl, &c) <= 0)
 			break;
 		if (cmdline_in(cl, &c, 1) < 0)
 			break;
diff --git a/lib/librte_cmdline/cmdline_os_unix.c b/lib/librte_cmdline/cmdline_os_unix.c
index ca47bd19f..865a89ddd 100644
--- a/lib/librte_cmdline/cmdline_os_unix.c
+++ b/lib/librte_cmdline/cmdline_os_unix.c
@@ -2,7 +2,9 @@
  * Copyright (c) 2020 Dmitry Kozlyuk
  */
 
+#include <poll.h>
 #include <string.h>
+#include <unistd.h>
 
 #include "cmdline_private.h"
 
@@ -25,3 +27,21 @@ terminal_restore(const struct terminal *oldterm)
 {
 	tcsetattr(fileno(stdin), TCSANOW, &oldterm->termios);
 }
+
+int
+cmdline_poll_char(struct cmdline *cl)
+{
+	struct pollfd pfd;
+
+	pfd.fd = cl->s_in;
+	pfd.events = POLLIN;
+	pfd.revents = 0;
+
+	return poll(&pfd, 1, 0);
+}
+
+ssize_t
+cmdline_read_char(struct cmdline *cl, char *c)
+{
+	return read(cl->s_in, c, 1);
+}
diff --git a/lib/librte_cmdline/cmdline_private.h b/lib/librte_cmdline/cmdline_private.h
index adc552845..ecfeb89f6 100644
--- a/lib/librte_cmdline/cmdline_private.h
+++ b/lib/librte_cmdline/cmdline_private.h
@@ -29,4 +29,10 @@ struct cmdline {
 	struct terminal oldterm;
 };
 
+/* Check if a single character can be read from input. */
+int cmdline_poll_char(struct cmdline *cl);
+
+/* Read one character from input. */
+ssize_t cmdline_read_char(struct cmdline *cl, char *c);
+
 #endif
-- 
2.25.4