You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by gn...@apache.org on 2020/04/19 14:07:22 UTC

[incubator-nuttx-apps] 08/09: nshlib: Split shell functionality from nsh to sh

This is an automated email from the ASF dual-hosted git repository.

gnutt pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git

commit de8a56d149343f66736454dedb4b44a352f52c01
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Sun Apr 19 02:02:09 2020 +0800

    nshlib: Split shell functionality from nsh to sh
    
    1.Improve the compatiblity as other OS
    2.Avoid call nsh_initialize more than once
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 include/nshlib/nshlib.h |  2 ++
 system/nsh/Makefile     |  4 +--
 system/nsh/nsh_main.c   | 77 ++++++++++---------------------------------------
 system/nsh/sh_main.c    | 53 ++++++++++++++++++++++++++++++++++
 system/popen/Kconfig    |  2 +-
 system/system/Kconfig   |  2 +-
 6 files changed, 74 insertions(+), 66 deletions(-)

diff --git a/include/nshlib/nshlib.h b/include/nshlib/nshlib.h
index 1e7084d..1e0db81 100644
--- a/include/nshlib/nshlib.h
+++ b/include/nshlib/nshlib.h
@@ -42,6 +42,8 @@
 
 #include <nuttx/config.h>
 
+#include <arpa/inet.h>
+
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
diff --git a/system/nsh/Makefile b/system/nsh/Makefile
index 232404b..166ca6a 100644
--- a/system/nsh/Makefile
+++ b/system/nsh/Makefile
@@ -37,9 +37,9 @@
 
 # NuttShell (NSH) Example
 
-MAINSRC = nsh_main.c
+MAINSRC = nsh_main.c sh_main.c
 
-PROGNAME = $(CONFIG_SYSTEM_NSH_PROGNAME)
+PROGNAME = $(CONFIG_SYSTEM_NSH_PROGNAME) sh
 PRIORITY = $(CONFIG_SYSTEM_NSH_PRIORITY)
 STACKSIZE = $(CONFIG_SYSTEM_NSH_STACKSIZE)
 MODULE = $(CONFIG_SYSTEM_NSH)
diff --git a/system/nsh/nsh_main.c b/system/nsh/nsh_main.c
index 7ebcf80..5c20be0 100644
--- a/system/nsh/nsh_main.c
+++ b/system/nsh/nsh_main.c
@@ -80,17 +80,6 @@
 #  undef CONFIG_SYSTEM_NSH_SYMTAB
 #endif
 
-/* Check if we need to build in support for the system() and/or popen()
- * functions.  In the KERNEL build mode (only), NSH is build as a ELF
- * program and must be capable of executing a single command provided
- * on the command line.
- */
-
-#undef HAVE_NSH_COMMAND
-#if defined(CONFIG_SYSTEM_SYSTEM) || defined(CONFIG_SYSTEM_POPEN)
-#  define HAVE_NSH_COMMAND 1
-#endif
-
 /* C++ initialization requires CXX initializer support */
 
 #if !defined(CONFIG_HAVE_CXX) || !defined(CONFIG_HAVE_CXXINITIALIZE)
@@ -115,11 +104,11 @@ extern const int CONFIG_SYSTEM_NSH_SYMTAB_COUNTNAME;
 #endif
 
 /****************************************************************************
- * Private Functions
+ * Public Functions
  ****************************************************************************/
 
 /****************************************************************************
- * Name: nsh_task
+ * Name: nsh_main
  *
  * Description:
  *   This is the main logic for the case of the NSH task.  It will perform
@@ -128,14 +117,26 @@ extern const int CONFIG_SYSTEM_NSH_SYMTAB_COUNTNAME;
  *
  ****************************************************************************/
 
-static int nsh_task(void)
+int main(int argc, FAR char *argv[])
 {
 #if defined (CONFIG_SYSTEM_NSH_SYMTAB)
   struct boardioc_symtab_s symdesc;
 #endif
+  struct sched_param param;
   int exitval = 0;
   int ret;
 
+  /* Check the task priority that we were started with */
+
+  sched_getparam(0, &param);
+  if (param.sched_priority != CONFIG_SYSTEM_NSH_PRIORITY)
+    {
+      /* If not then set the priority to the configured priority */
+
+      param.sched_priority = CONFIG_SYSTEM_NSH_PRIORITY;
+      sched_setparam(0, &param);
+    }
+
 #if defined(CONFIG_SYSTEM_NSH_CXXINITIALIZE)
   /* Call all C++ static constructors */
 
@@ -190,51 +191,3 @@ static int nsh_task(void)
 
   return exitval;
 }
-
-/****************************************************************************
- * Public Functions
- ****************************************************************************/
-
-/****************************************************************************
- * Name: nsh_main
- ****************************************************************************/
-
-int main(int argc, FAR char *argv[])
-{
-  struct sched_param param;
-
-  /* Check the task priority that we were started with */
-
-  sched_getparam(0, &param);
-  if (param.sched_priority != CONFIG_SYSTEM_NSH_PRIORITY)
-    {
-      /* If not then set the priority to the configured priority */
-
-      param.sched_priority = CONFIG_SYSTEM_NSH_PRIORITY;
-      sched_setparam(0, &param);
-    }
-
-  /* There are two modes that NSH can be executed in:
-   *
-   * 1) As a normal, interactive shell.  In this case, no arguments are
-   *    expected on the command line.  OR
-   * 2) As a single command processor.  In this case, the single command is
-   *    is provided in argv[1].
-   *
-   * NOTE:  The latter mode is only available if CONFIG_SYSTEM_NSH=m.
-   * In that case, this main() function will be built as a process.  The
-   * process will be started with a command by the implementations of the
-   * system() and popen() interfaces.
-   */
-
-#ifdef HAVE_NSH_COMMAND
-  if (argc > 1)
-    {
-      return nsh_system(argc, argv);
-    }
-  else
-#endif
-    {
-      return nsh_task();
-    }
-}
diff --git a/system/nsh/sh_main.c b/system/nsh/sh_main.c
new file mode 100644
index 0000000..345097d
--- /dev/null
+++ b/system/nsh/sh_main.c
@@ -0,0 +1,53 @@
+/****************************************************************************
+ * system/nsh/sh_main.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include "nshlib/nshlib.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: sh_main
+ ****************************************************************************/
+
+int main(int argc, FAR char *argv[])
+{
+  /* There are two modes that NSH can be executed in:
+   *
+   * 1) As a normal, interactive shell.  In this case, no arguments are
+   *    expected on the command line.  OR
+   * 2) As a single command processor.  In this case, the single command is
+   *    is provided in argv[1].
+   *
+   * NOTE:  The latter mode is only available if CONFIG_SYSTEM_NSH=m.
+   * In that case, this main() function will be built as a process.  The
+   * process will be started with a command by the implementations of the
+   * system() and popen() interfaces.
+   */
+
+  return nsh_system(argc, argv);
+}
diff --git a/system/popen/Kconfig b/system/popen/Kconfig
index 9c7527b..14a245f 100644
--- a/system/popen/Kconfig
+++ b/system/popen/Kconfig
@@ -17,7 +17,7 @@ if SYSTEM_POPEN
 
 config SYSTEM_POPEN_SHPATH
 	string "Path to shell command"
-	default "/bin/nsh"
+	default "/bin/sh"
 	depends on SYSTEM_NSH=m
 	---help---
 		This is the full path to the program in a mounted file system that
diff --git a/system/system/Kconfig b/system/system/Kconfig
index 36be798..60af654 100644
--- a/system/system/Kconfig
+++ b/system/system/Kconfig
@@ -16,7 +16,7 @@ if SYSTEM_SYSTEM
 
 config SYSTEM_SYSTEM_SHPATH
 	string "Path to shell command"
-	default "/bin/nsh"
+	default "/bin/sh"
 	depends on SYSTEM_NSH=m
 	---help---
 		This is the full path to the program in a mounted file system that