[posix] split readline and stdio handler (#6644)

This commit splits readline and stdio CLI handlers in POSIX app to
better demonstrate how to work with the CLI module.
This commit is contained in:
Yakun Xu
2021-05-19 12:28:09 -07:00
committed by GitHub
parent ea5a5fbd53
commit 178ac73938
6 changed files with 126 additions and 39 deletions
+2 -1
View File
@@ -539,7 +539,8 @@ LOCAL_LDLIBS := \
-lutil
LOCAL_SRC_FILES := \
src/posix/cli.cpp \
src/posix/cli_readline.cpp \
src/posix/cli_stdio.cpp \
src/posix/main.c \
$(NULL)
+2 -1
View File
@@ -129,7 +129,8 @@ ot_cli_CPPFLAGS = \
ot_cli_SOURCES = \
main.c \
cli.cpp \
cli_readline.cpp \
cli_stdio.cpp \
$(NULL)
ot_cli_LDADD = \
+7 -2
View File
@@ -28,13 +28,18 @@
add_executable(ot-cli
main.c
cli.cpp
cli_readline.cpp
cli_stdio.cpp
)
target_include_directories(ot-cli PRIVATE ${COMMON_INCLUDES})
if (READLINE)
target_compile_definitions(ot-cli PRIVATE
$<$<BOOL:${READLINE}>:HAVE_LIB$<UPPER_CASE:${OT_READLINE}>=1>)
endif()
target_compile_definitions(ot-cli PRIVATE
$<$<BOOL:${READLINE}>:HAVE_LIB$<UPPER_CASE:${OT_READLINE}>=1>
${OT_PLATFORM_DEFINES}
)
@@ -39,25 +39,14 @@
#include <string.h>
#include <unistd.h>
#ifndef HAVE_LIBEDIT
#define HAVE_LIBEDIT 0
#endif
#if defined(HAVE_LIBEDIT) || defined(HAVE_LIBREADLINE)
#ifndef HAVE_LIBREADLINE
#define HAVE_LIBREADLINE 0
#endif
#if HAVE_LIBEDIT || HAVE_LIBREADLINE
#define OPENTHREAD_USE_READLINE 1
#if HAVE_LIBEDIT
#if defined(HAVE_LIBEDIT)
#include <editline/readline.h>
#elif HAVE_LIBREADLINE
#elif defined(HAVE_LIBREADLINE)
#include <readline/history.h>
#include <readline/readline.h>
#endif
#else
#define OPENTHREAD_USE_READLINE 0
#endif
#include <openthread/cli.h>
@@ -69,7 +58,6 @@
static const char sPrompt[] = "> ";
#if OPENTHREAD_USE_READLINE
static void InputCallback(char *aLine)
{
if (aLine != nullptr)
@@ -86,7 +74,6 @@ static void InputCallback(char *aLine)
exit(OT_EXIT_SUCCESS);
}
}
#endif
static int OutputCallback(void *aContext, const char *aFormat, va_list aArguments)
{
@@ -97,7 +84,6 @@ static int OutputCallback(void *aContext, const char *aFormat, va_list aArgument
extern "C" void otAppCliInit(otInstance *aInstance)
{
#if OPENTHREAD_USE_READLINE
rl_instream = stdin;
rl_outstream = stdout;
rl_inhibit_completion = true;
@@ -105,15 +91,12 @@ extern "C" void otAppCliInit(otInstance *aInstance)
rl_set_screen_size(0, OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH);
rl_callback_handler_install(sPrompt, InputCallback);
#endif
otCliInit(aInstance, OutputCallback, nullptr);
}
extern "C" void otAppCliDeinit(void)
{
#if OPENTHREAD_USE_READLINE
rl_callback_handler_remove();
#endif
}
extern "C" void otAppCliUpdate(otSysMainloopContext *aMainloop)
@@ -136,22 +119,9 @@ extern "C" void otAppCliProcess(const otSysMainloopContext *aMainloop)
if (FD_ISSET(STDIN_FILENO, &aMainloop->mReadFdSet))
{
#if OPENTHREAD_USE_READLINE
rl_callback_read_char();
#else
char buffer[OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH];
if (fgets(buffer, sizeof(buffer), stdin) != nullptr)
{
otCliInputLine(buffer);
dprintf(STDOUT_FILENO, "%s", sPrompt);
}
else
{
exit(OT_EXIT_SUCCESS);
}
#endif
}
}
#endif // defined(HAVE_LIBEDIT) || defined(HAVE_LIBREADLINE)
#endif // !OPENTHREAD_POSIX_CONFIG_DAEMON_ENABLE
+106
View File
@@ -0,0 +1,106 @@
/*
* Copyright (c) 2021, The OpenThread Authors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "platform/openthread-posix-config.h"
#if !OPENTHREAD_POSIX_CONFIG_DAEMON_ENABLE
#include <openthread/config.h>
#include <openthread/openthread-system.h>
#if !(defined(HAVE_LIBEDIT) || defined(HAVE_LIBREADLINE))
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <openthread/cli.h>
#include "cli/cli_config.h"
#include "common/code_utils.hpp"
#include "openthread-core-config.h"
#include "platform-posix.h"
namespace {
constexpr char sPrompt[] = "> ";
int OutputCallback(void *aContext, const char *aFormat, va_list aArguments)
{
OT_UNUSED_VARIABLE(aContext);
return vdprintf(STDOUT_FILENO, aFormat, aArguments);
}
} // namespace
extern "C" void otAppCliInit(otInstance *aInstance)
{
otCliInit(aInstance, OutputCallback, nullptr);
}
extern "C" void otAppCliDeinit(void)
{
}
extern "C" void otAppCliUpdate(otSysMainloopContext *aMainloop)
{
FD_SET(STDIN_FILENO, &aMainloop->mReadFdSet);
FD_SET(STDIN_FILENO, &aMainloop->mErrorFdSet);
if (aMainloop->mMaxFd < STDIN_FILENO)
{
aMainloop->mMaxFd = STDIN_FILENO;
}
}
extern "C" void otAppCliProcess(const otSysMainloopContext *aMainloop)
{
if (FD_ISSET(STDIN_FILENO, &aMainloop->mErrorFdSet))
{
exit(OT_EXIT_FAILURE);
}
if (FD_ISSET(STDIN_FILENO, &aMainloop->mReadFdSet))
{
char buffer[OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH];
if (fgets(buffer, sizeof(buffer), stdin) != nullptr)
{
otCliInputLine(buffer);
dprintf(STDOUT_FILENO, "%s", sPrompt);
}
else
{
exit(OT_EXIT_SUCCESS);
}
}
}
#endif // !(defined(HAVE_LIBEDIT) || defined(HAVE_LIBREADLINE))
#endif // !OPENTHREAD_POSIX_CONFIG_DAEMON_ENABLE
+5 -1
View File
@@ -55,8 +55,12 @@ add_executable(ot-ctl
client.cpp
)
if (READLINE)
target_compile_definitions(ot-ctl PRIVATE
$<$<BOOL:${READLINE}>:HAVE_LIB$<UPPER_CASE:${OT_READLINE}>=1>)
endif()
target_compile_definitions(ot-ctl PRIVATE
$<$<BOOL:${READLINE}>:HAVE_LIB$<UPPER_CASE:${OT_READLINE}>=1>
${OT_PLATFORM_DEFINES}
)