2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
2011-03-20 19:18:19 +01:00
|
|
|
* apps/nshlib/nsh_parse.c
|
2011-03-18 20:46:25 +01:00
|
|
|
*
|
2020-05-18 00:07:36 +02:00
|
|
|
* 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
|
2011-03-18 20:46:25 +01:00
|
|
|
*
|
2020-05-18 00:07:36 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-03-18 20:46:25 +01:00
|
|
|
*
|
2020-05-18 00:07:36 +02:00
|
|
|
* 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.
|
2011-03-18 20:46:25 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
2014-01-10 22:23:26 +01:00
|
|
|
#include <string.h>
|
2011-03-18 20:46:25 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2014-01-11 16:50:54 +01:00
|
|
|
#ifdef CONFIG_NSH_CMDPARMS
|
|
|
|
# include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
|
2014-03-23 20:10:05 +01:00
|
|
|
#include <nuttx/version.h>
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "nshlib/nshlib.h"
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
#include "nsh.h"
|
2012-02-02 17:04:09 +01:00
|
|
|
#include "nsh_console.h"
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2012-07-28 17:18:26 +02:00
|
|
|
* Pre-processor Definitions
|
2011-03-18 20:46:25 +01:00
|
|
|
****************************************************************************/
|
2018-08-06 21:28:31 +02:00
|
|
|
|
2014-01-11 16:50:54 +01:00
|
|
|
/* If CONFIG_NSH_CMDPARMS or CONFIG_NSH_ARGCAT is enabled, then we will need
|
|
|
|
* retain a list of memory allocations to be freed at the completion of
|
|
|
|
* command processing.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#undef HAVE_MEMLIST
|
|
|
|
#if defined(CONFIG_NSH_CMDPARMS) || defined(CONFIG_NSH_ARGCAT)
|
|
|
|
# define HAVE_MEMLIST 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(HAVE_MEMLIST) && !defined(CONFIG_NSH_MAXALLOCS)
|
|
|
|
# ifdef CONFIG_NSH_ARGCAT
|
|
|
|
# define CONFIG_NSH_MAXALLOCS (2*CONFIG_NSH_MAXARGUMENTS)
|
|
|
|
# else
|
|
|
|
# define CONFIG_NSH_MAXALLOCS CONFIG_NSH_MAXARGUMENTS
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Allocation list helper macros */
|
|
|
|
|
|
|
|
#ifdef HAVE_MEMLIST
|
|
|
|
# define NSH_MEMLIST_TYPE struct nsh_memlist_s
|
|
|
|
# define NSH_MEMLIST_INIT(m) memset(&(m), 0, sizeof(struct nsh_memlist_s));
|
|
|
|
# define NSH_MEMLIST_ADD(m,a) nsh_memlist_add(m,a)
|
|
|
|
# define NSH_MEMLIST_FREE(m) nsh_memlist_free(m)
|
|
|
|
#else
|
|
|
|
# define NSH_MEMLIST_TYPE uint8_t
|
|
|
|
# define NSH_MEMLIST_INIT(m) do { (m) = 0; } while (0)
|
|
|
|
# define NSH_MEMLIST_ADD(m,a)
|
2014-01-13 01:09:19 +01:00
|
|
|
# define NSH_MEMLIST_FREE(m)
|
2014-01-11 16:50:54 +01:00
|
|
|
#endif
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2019-12-24 19:18:30 +01:00
|
|
|
/* Do we need g_nullstring[]? */
|
|
|
|
|
|
|
|
#undef NEED_NULLSTRING
|
|
|
|
#if defined(NSH_HAVE_VARS) || defined(CONFIG_NSH_CMDPARMS)
|
|
|
|
# define NEED_NULLSTRING 1
|
|
|
|
#elif !defined(CONFIG_NSH_ARGCAT) || !defined(HAVE_MEMLIST)
|
|
|
|
# define NEED_NULLSTRING 1
|
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-01-10 22:23:26 +01:00
|
|
|
/* These structure describes the parsed command line */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLEBG
|
2011-03-18 20:46:25 +01:00
|
|
|
struct cmdarg_s
|
|
|
|
{
|
|
|
|
FAR struct nsh_vtbl_s *vtbl; /* For front-end interaction */
|
|
|
|
int fd; /* FD for output redirection */
|
|
|
|
int argc; /* Number of arguments in argv */
|
|
|
|
FAR char *argv[MAX_ARGV_ENTRIES]; /* Argument list */
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2014-01-11 16:50:54 +01:00
|
|
|
/* This structure describes the allocation list */
|
|
|
|
|
|
|
|
#ifdef HAVE_MEMLIST
|
|
|
|
struct nsh_memlist_s
|
|
|
|
{
|
|
|
|
int nallocs; /* Number of allocations */
|
|
|
|
FAR char *allocations[CONFIG_NSH_MAXALLOCS];
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-01-11 16:50:54 +01:00
|
|
|
#ifdef HAVE_MEMLIST
|
|
|
|
static void nsh_memlist_add(FAR struct nsh_memlist_s *memlist,
|
2014-01-11 20:18:12 +01:00
|
|
|
FAR char *allocation);
|
2014-01-11 16:50:54 +01:00
|
|
|
static void nsh_memlist_free(FAR struct nsh_memlist_s *memlist);
|
|
|
|
#endif
|
|
|
|
|
2014-01-11 01:42:24 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLEBG
|
|
|
|
static void nsh_releaseargs(struct cmdarg_s *arg);
|
|
|
|
static pthread_addr_t nsh_child(pthread_addr_t arg);
|
|
|
|
static struct cmdarg_s *nsh_cloneargs(FAR struct nsh_vtbl_s *vtbl,
|
|
|
|
int fd, int argc, char *argv[]);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int nsh_saveresult(FAR struct nsh_vtbl_s *vtbl, bool result);
|
|
|
|
static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
|
|
|
|
int argc, FAR char *argv[], FAR const char *redirfile,
|
|
|
|
int oflags);
|
2014-01-11 16:50:54 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NSH_CMDPARMS
|
|
|
|
static FAR char *nsh_filecat(FAR struct nsh_vtbl_s *vtbl, FAR char *s1,
|
|
|
|
FAR const char *filename);
|
|
|
|
static FAR char *nsh_cmdparm(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline,
|
|
|
|
FAR char **allocation);
|
|
|
|
#endif
|
2014-01-13 15:21:18 +01:00
|
|
|
|
2014-01-11 16:50:54 +01:00
|
|
|
#ifdef CONFIG_NSH_ARGCAT
|
2014-01-11 20:18:12 +01:00
|
|
|
static FAR char *nsh_strcat(FAR struct nsh_vtbl_s *vtbl, FAR char *s1,
|
|
|
|
FAR const char *s2);
|
2014-01-11 16:50:54 +01:00
|
|
|
#endif
|
2014-01-13 15:21:18 +01:00
|
|
|
|
2018-08-06 21:28:31 +02:00
|
|
|
#if defined(CONFIG_NSH_QUOTE) && defined(CONFIG_NSH_ARGCAT)
|
|
|
|
static FAR char *nsh_strchr(FAR const char *str, int ch);
|
|
|
|
#else
|
|
|
|
# define nsh_strchr(s,c) strchr(s,c)
|
|
|
|
#endif
|
|
|
|
|
2018-10-01 21:30:25 +02:00
|
|
|
#ifdef NSH_HAVE_VARS
|
2018-10-02 19:16:15 +02:00
|
|
|
static FAR char *nsh_envexpand(FAR struct nsh_vtbl_s *vtbl,
|
2014-01-11 20:18:12 +01:00
|
|
|
FAR char *varname);
|
2014-01-13 01:09:19 +01:00
|
|
|
#endif
|
2014-01-13 15:21:18 +01:00
|
|
|
|
2018-08-06 21:28:31 +02:00
|
|
|
#if defined(CONFIG_NSH_QUOTE) && defined(CONFIG_NSH_ARGCAT)
|
|
|
|
static void nsh_dequote(FAR char *cmdline);
|
|
|
|
#else
|
|
|
|
# define nsh_dequote(c)
|
|
|
|
#endif
|
|
|
|
|
2020-05-18 00:07:36 +02:00
|
|
|
static FAR char *nsh_argexpand(FAR struct nsh_vtbl_s *vtbl,
|
|
|
|
FAR char *cmdline, FAR char **allocation, FAR int *isenvvar);
|
2014-01-11 16:50:54 +01:00
|
|
|
static FAR char *nsh_argument(FAR struct nsh_vtbl_s *vtbl, char **saveptr,
|
2019-01-06 17:00:19 +01:00
|
|
|
FAR NSH_MEMLIST_TYPE *memlist, FAR int *isenvvar);
|
2014-01-11 01:42:24 +01:00
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_DISABLESCRIPT
|
2014-01-18 16:39:16 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_LOOPS
|
2014-01-17 22:56:32 +01:00
|
|
|
static bool nsh_loop_enabled(FAR struct nsh_vtbl_s *vtbl);
|
2014-01-18 16:39:16 +01:00
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_NSH_DISABLE_ITEF
|
2014-01-17 22:56:32 +01:00
|
|
|
static bool nsh_itef_enabled(FAR struct nsh_vtbl_s *vtbl);
|
2014-01-18 16:39:16 +01:00
|
|
|
#endif
|
2014-01-11 01:42:24 +01:00
|
|
|
static bool nsh_cmdenabled(FAR struct nsh_vtbl_s *vtbl);
|
2014-01-18 16:39:16 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_LOOPS
|
2014-01-17 22:56:32 +01:00
|
|
|
static int nsh_loop(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
|
|
|
|
FAR char **saveptr, FAR NSH_MEMLIST_TYPE *memlist);
|
2014-01-18 16:39:16 +01:00
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_NSH_DISABLE_ITEF
|
2014-01-17 22:56:32 +01:00
|
|
|
static int nsh_itef(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
|
|
|
|
FAR char **saveptr, FAR NSH_MEMLIST_TYPE *memlist);
|
2014-01-11 01:42:24 +01:00
|
|
|
#endif
|
2014-01-18 16:39:16 +01:00
|
|
|
#endif
|
2014-01-11 01:42:24 +01:00
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_DISABLEBG
|
|
|
|
static int nsh_nice(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
|
2014-01-11 16:50:54 +01:00
|
|
|
FAR char **saveptr, FAR NSH_MEMLIST_TYPE *memlist);
|
2014-01-11 01:42:24 +01:00
|
|
|
#endif
|
|
|
|
|
2014-01-11 16:50:54 +01:00
|
|
|
#ifdef CONFIG_NSH_CMDPARMS
|
2014-01-12 21:16:05 +01:00
|
|
|
static int nsh_parse_cmdparm(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline,
|
2014-01-11 01:42:24 +01:00
|
|
|
FAR const char *redirfile);
|
|
|
|
#endif
|
2014-01-13 15:21:18 +01:00
|
|
|
|
2014-01-11 01:42:24 +01:00
|
|
|
static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline);
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-01-10 22:23:26 +01:00
|
|
|
static const char g_token_separator[] = " \t\n";
|
2014-01-11 20:18:12 +01:00
|
|
|
#ifndef NSH_DISABLE_SEMICOLON
|
2014-07-01 21:43:40 +02:00
|
|
|
static const char g_line_separator[] = "\"#;\n";
|
2014-01-11 20:18:12 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NSH_ARGCAT
|
|
|
|
static const char g_arg_separator[] = "`$";
|
|
|
|
#endif
|
2014-01-10 22:23:26 +01:00
|
|
|
static const char g_redirect1[] = ">";
|
|
|
|
static const char g_redirect2[] = ">>";
|
2018-10-02 18:21:33 +02:00
|
|
|
#ifdef NSH_HAVE_VARS
|
2014-01-11 20:18:12 +01:00
|
|
|
static const char g_exitstatus[] = "?";
|
2014-01-10 22:23:26 +01:00
|
|
|
static const char g_success[] = "0";
|
|
|
|
static const char g_failure[] = "1";
|
2017-08-12 19:14:30 +02:00
|
|
|
#endif
|
2019-12-24 19:18:30 +01:00
|
|
|
#ifdef NEED_NULLSTRING
|
2019-12-19 14:52:00 +01:00
|
|
|
static const char g_nullstring[] = "";
|
2019-12-19 14:59:30 +01:00
|
|
|
#endif
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-04-15 16:57:53 +02:00
|
|
|
/* If NuttX versioning information is available, Include that information
|
|
|
|
* in the NSH greeting.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if CONFIG_VERSION_MAJOR != 0 || CONFIG_VERSION_MINOR != 0
|
2020-05-18 00:07:36 +02:00
|
|
|
const char g_nshgreeting[] =
|
|
|
|
"\nNuttShell (NSH) NuttX-" CONFIG_VERSION_STRING "\n";
|
2011-04-15 16:57:53 +02:00
|
|
|
#else
|
2016-01-17 15:12:51 +01:00
|
|
|
const char g_nshgreeting[] = "\nNuttShell (NSH)\n";
|
2011-04-15 16:57:53 +02:00
|
|
|
#endif
|
|
|
|
|
2016-01-20 18:18:08 +01:00
|
|
|
/* Fixed Message of the Day (MOTD) */
|
|
|
|
|
|
|
|
#if defined(CONFIG_NSH_MOTD) && !defined(CONFIG_NSH_PLATFORM_MOTD)
|
|
|
|
const char g_nshmotd[] = CONFIG_NSH_MOTD_STRING;
|
|
|
|
#endif
|
|
|
|
|
2012-10-12 18:59:17 +02:00
|
|
|
/* Telnet login prompts */
|
|
|
|
|
2016-01-20 21:42:39 +01:00
|
|
|
#ifdef CONFIG_NSH_LOGIN
|
2012-10-12 18:59:17 +02:00
|
|
|
#if defined(CONFIG_NSH_TELNET_LOGIN) && defined(CONFIG_NSH_TELNET)
|
2020-05-18 00:07:36 +02:00
|
|
|
const char g_telnetgreeting[] =
|
|
|
|
"\nWelcome to NuttShell(NSH) Telnet Server...\n";
|
2016-01-20 21:42:39 +01:00
|
|
|
#endif
|
2016-01-17 15:12:51 +01:00
|
|
|
const char g_userprompt[] = "login: ";
|
|
|
|
const char g_passwordprompt[] = "password: ";
|
|
|
|
const char g_loginsuccess[] = "\nUser Logged-in!\n";
|
|
|
|
const char g_badcredentials[] = "\nInvalid username or password\n";
|
|
|
|
const char g_loginfailure[] = "Login failed!\n";
|
2012-10-12 18:59:17 +02:00
|
|
|
#endif
|
|
|
|
|
2011-04-15 16:57:53 +02:00
|
|
|
/* The NSH prompt */
|
|
|
|
|
2018-08-23 15:34:30 +02:00
|
|
|
const char g_nshprompt[] = CONFIG_NSH_PROMPT_STRING;
|
2011-04-15 16:57:53 +02:00
|
|
|
|
|
|
|
/* Common, message formats */
|
|
|
|
|
2016-01-17 15:12:51 +01:00
|
|
|
const char g_fmtsyntax[] = "nsh: %s: syntax error\n";
|
|
|
|
const char g_fmtargrequired[] = "nsh: %s: missing required argument(s)\n";
|
|
|
|
const char g_fmtnomatching[] = "nsh: %s: no matching %s\n";
|
|
|
|
const char g_fmtarginvalid[] = "nsh: %s: argument invalid\n";
|
|
|
|
const char g_fmtargrange[] = "nsh: %s: value out of range\n";
|
|
|
|
const char g_fmtcmdnotfound[] = "nsh: %s: command not found\n";
|
|
|
|
const char g_fmtnosuch[] = "nsh: %s: no such %s: %s\n";
|
|
|
|
const char g_fmttoomanyargs[] = "nsh: %s: too many arguments\n";
|
|
|
|
const char g_fmtdeepnesting[] = "nsh: %s: nesting too deep\n";
|
|
|
|
const char g_fmtcontext[] = "nsh: %s: not valid in this context\n";
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifdef CONFIG_NSH_STRERROR
|
2016-01-17 15:12:51 +01:00
|
|
|
const char g_fmtcmdfailed[] = "nsh: %s: %s failed: %s\n";
|
2011-03-18 20:46:25 +01:00
|
|
|
#else
|
2016-01-17 14:53:52 +01:00
|
|
|
const char g_fmtcmdfailed[] = "nsh: %s: %s failed: %d\n";
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
2016-01-17 14:53:52 +01:00
|
|
|
const char g_fmtcmdoutofmemory[] = "nsh: %s: out of memory\n";
|
|
|
|
const char g_fmtinternalerror[] = "nsh: %s: Internal error\n";
|
|
|
|
const char g_fmtsignalrecvd[] = "nsh: %s: Interrupted by signal\n";
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-01-11 16:50:54 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_memlist_add
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef HAVE_MEMLIST
|
|
|
|
static void nsh_memlist_add(FAR struct nsh_memlist_s *memlist,
|
2014-01-11 20:18:12 +01:00
|
|
|
FAR char *allocation)
|
2014-01-11 16:50:54 +01:00
|
|
|
{
|
2014-01-11 20:18:12 +01:00
|
|
|
if (memlist && allocation)
|
2014-01-11 16:50:54 +01:00
|
|
|
{
|
|
|
|
int index = memlist->nallocs;
|
|
|
|
if (index < CONFIG_NSH_MAXALLOCS)
|
|
|
|
{
|
2014-01-11 20:18:12 +01:00
|
|
|
memlist->allocations[index] = allocation;
|
2014-01-11 16:50:54 +01:00
|
|
|
memlist->nallocs = index + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_memlist_free
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef HAVE_MEMLIST
|
|
|
|
static void nsh_memlist_free(FAR struct nsh_memlist_s *memlist)
|
|
|
|
{
|
|
|
|
if (memlist)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
|
|
|
|
for (index = 0; index < memlist->nallocs; index++)
|
|
|
|
{
|
|
|
|
free(memlist->allocations[index]);
|
|
|
|
memlist->allocations[index] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memlist->nallocs = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_releaseargs
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLEBG
|
2011-03-18 20:46:25 +01:00
|
|
|
static void nsh_releaseargs(struct cmdarg_s *arg)
|
|
|
|
{
|
|
|
|
FAR struct nsh_vtbl_s *vtbl = arg->vtbl;
|
|
|
|
int i;
|
|
|
|
|
2020-08-13 16:17:42 +02:00
|
|
|
#ifdef CONFIG_FILE_STREAM
|
2011-03-18 20:46:25 +01:00
|
|
|
/* If the output was redirected, then file descriptor should
|
|
|
|
* be closed. The created task has its one, independent copy of
|
|
|
|
* the file descriptor
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (vtbl->np.np_redirect)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
close(arg->fd);
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
2013-09-30 19:34:04 +02:00
|
|
|
#endif
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/* Released the cloned vtbl instance */
|
|
|
|
|
|
|
|
nsh_release(vtbl);
|
|
|
|
|
|
|
|
/* Release the cloned args */
|
|
|
|
|
|
|
|
for (i = 0; i < arg->argc; i++)
|
|
|
|
{
|
|
|
|
free(arg->argv[i]);
|
|
|
|
}
|
2014-01-11 16:50:54 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
free(arg);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_child
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLEBG
|
2011-03-18 20:46:25 +01:00
|
|
|
static pthread_addr_t nsh_child(pthread_addr_t arg)
|
|
|
|
{
|
|
|
|
struct cmdarg_s *carg = (struct cmdarg_s *)arg;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-16 20:12:34 +02:00
|
|
|
_info("BG %s\n", carg->argv[0]);
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/* Execute the specified command on the child thread */
|
|
|
|
|
2014-01-10 22:23:26 +01:00
|
|
|
ret = nsh_command(carg->vtbl, carg->argc, carg->argv);
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/* Released the cloned arguments */
|
|
|
|
|
2016-06-16 20:12:34 +02:00
|
|
|
_info("BG %s complete\n", carg->argv[0]);
|
2011-03-18 20:46:25 +01:00
|
|
|
nsh_releaseargs(carg);
|
2015-04-11 20:13:18 +02:00
|
|
|
return (pthread_addr_t)((uintptr_t)ret);
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_cloneargs
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLEBG
|
2014-01-11 01:42:24 +01:00
|
|
|
static struct cmdarg_s *nsh_cloneargs(FAR struct nsh_vtbl_s *vtbl,
|
|
|
|
int fd, int argc, char *argv[])
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
|
|
|
struct cmdarg_s *ret = (struct cmdarg_s *)zalloc(sizeof(struct cmdarg_s));
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ret->vtbl = vtbl;
|
|
|
|
ret->fd = fd;
|
|
|
|
ret->argc = argc;
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
{
|
|
|
|
ret->argv[i] = strdup(argv[i]);
|
|
|
|
}
|
|
|
|
}
|
2014-01-10 22:23:26 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_saveresult
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int nsh_saveresult(FAR struct nsh_vtbl_s *vtbl, bool result)
|
|
|
|
{
|
|
|
|
struct nsh_parser_s *np = &vtbl->np;
|
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_DISABLESCRIPT
|
2014-01-18 16:39:16 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_LOOPS
|
2014-01-17 22:56:32 +01:00
|
|
|
/* Check if we are waiting for the condition associated with a while
|
|
|
|
* token.
|
|
|
|
*
|
|
|
|
* while <test-cmd>; do <cmd-sequence>; done
|
|
|
|
*
|
|
|
|
* Execute <cmd-sequence> as long as <test-cmd> has an exit status of
|
|
|
|
* zero.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (np->np_lpstate[np->np_lpndx].lp_state == NSH_LOOP_WHILE)
|
|
|
|
{
|
|
|
|
np->np_fail = false;
|
|
|
|
np->np_lpstate[np->np_lpndx].lp_enable = (result == OK);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we are waiting for the condition associated with an until
|
|
|
|
* token.
|
|
|
|
*
|
|
|
|
* until <test-cmd>; do <cmd-sequence>; done
|
|
|
|
*
|
|
|
|
* Execute <cmd-sequence> as long as <test-cmd> has a non-zero exit
|
|
|
|
* status.
|
|
|
|
*/
|
|
|
|
|
2020-05-18 00:07:36 +02:00
|
|
|
else if (np->np_lpstate[np->np_lpndx].lp_state == NSH_LOOP_UNTIL)
|
2014-01-17 22:56:32 +01:00
|
|
|
{
|
|
|
|
np->np_fail = false;
|
|
|
|
np->np_lpstate[np->np_lpndx].lp_enable = (result != OK);
|
|
|
|
return OK;
|
|
|
|
}
|
2014-01-18 16:39:16 +01:00
|
|
|
else
|
|
|
|
#endif
|
2014-01-17 22:56:32 +01:00
|
|
|
|
2014-01-18 16:39:16 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_ITEF
|
2014-01-17 22:56:32 +01:00
|
|
|
/* Check if we are waiting for the condition associated with an if token */
|
|
|
|
|
2014-01-18 16:39:16 +01:00
|
|
|
if (np->np_iestate[np->np_iendx].ie_state == NSH_ITEF_IF)
|
2014-01-10 23:15:14 +01:00
|
|
|
{
|
|
|
|
np->np_fail = false;
|
2018-08-11 15:14:41 +02:00
|
|
|
np->np_iestate[np->np_iendx].ie_ifcond =
|
|
|
|
np->np_iestate[np->np_iendx].ie_inverted ^ result;
|
2014-01-10 23:15:14 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
else
|
2014-01-18 16:39:16 +01:00
|
|
|
#endif
|
2014-01-10 23:15:14 +01:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
np->np_fail = result;
|
|
|
|
return result ? ERROR : OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-01-11 01:42:24 +01:00
|
|
|
* Name: nsh_execute
|
2014-01-10 23:15:14 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
|
|
|
|
int argc, FAR char *argv[],
|
|
|
|
FAR const char *redirfile, int oflags)
|
|
|
|
{
|
2020-08-13 16:17:42 +02:00
|
|
|
#if defined(CONFIG_FILE_STREAM) || !defined(CONFIG_NSH_DISABLEBG)
|
2014-01-10 23:15:14 +01:00
|
|
|
int fd = -1;
|
2014-01-13 01:09:19 +01:00
|
|
|
#endif
|
2014-01-10 23:15:14 +01:00
|
|
|
int ret;
|
|
|
|
|
2020-05-18 03:01:23 +02:00
|
|
|
/* DO NOT CHANGE THE ORDERING OF THE FOLLOWING STEPS
|
|
|
|
*
|
|
|
|
* They must work as follows:
|
|
|
|
*
|
|
|
|
* 1. Load a file from file system if possible. An external command on a
|
|
|
|
* file system with the provided name (and on the defined PATH) takes
|
|
|
|
* precendence over any other source of a command by that name. This
|
|
|
|
* allows the user to replace a built-in command with a command on a`
|
|
|
|
* file system
|
|
|
|
*
|
|
|
|
* 2. If not, run a built-in application of that name if possible. A
|
|
|
|
* built-in application will take precendence over any NSH command.
|
|
|
|
*
|
|
|
|
* 3. If not, run an NSH command line command if possible.
|
|
|
|
*
|
|
|
|
* 4. If not, report that the command was not found.
|
|
|
|
*/
|
|
|
|
|
2020-05-18 08:46:22 +02:00
|
|
|
/* Does this command correspond to a built-in command?
|
|
|
|
* nsh_builtin() returns:
|
2014-01-10 23:15:14 +01:00
|
|
|
*
|
2020-05-18 00:07:36 +02:00
|
|
|
* -1 (ERROR) if the application task corresponding to 'argv[0]' could
|
|
|
|
* not be started (possibly because it does not exist).
|
2014-01-10 23:15:14 +01:00
|
|
|
* 0 (OK) if the application task corresponding to 'argv[0]' was
|
|
|
|
* and successfully started. If CONFIG_SCHED_WAITPID is
|
|
|
|
* defined, this return value also indicates that the
|
|
|
|
* application returned successful status (EXIT_SUCCESS)
|
|
|
|
* 1 If CONFIG_SCHED_WAITPID is defined, then this return value
|
2020-05-18 00:07:36 +02:00
|
|
|
* indicates that the application task was spawned
|
|
|
|
* successfully but returned failure exit status.
|
2014-01-10 23:15:14 +01:00
|
|
|
*
|
2020-05-17 23:44:09 +02:00
|
|
|
* Note the priority is not effected by nice-ness.
|
2014-01-10 23:15:14 +01:00
|
|
|
*/
|
|
|
|
|
2020-05-18 08:46:22 +02:00
|
|
|
#ifdef CONFIG_NSH_BUILTIN_APPS
|
2020-08-13 16:17:42 +02:00
|
|
|
#ifdef CONFIG_FILE_STREAM
|
2020-05-18 08:46:22 +02:00
|
|
|
ret = nsh_builtin(vtbl, argv[0], argv, redirfile, oflags);
|
|
|
|
#else
|
|
|
|
ret = nsh_builtin(vtbl, argv[0], argv, NULL, 0);
|
|
|
|
#endif
|
2014-01-10 23:15:14 +01:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
2020-05-18 08:46:22 +02:00
|
|
|
/* nsh_builtin() returned 0 or 1. This means that the built-in
|
2014-01-10 23:15:14 +01:00
|
|
|
* command was successfully started (although it may not have ran
|
|
|
|
* successfully). So certainly it is not an NSH command.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Save the result: success if 0; failure if 1 */
|
|
|
|
|
|
|
|
return nsh_saveresult(vtbl, ret != OK);
|
|
|
|
}
|
|
|
|
|
2020-05-18 08:46:22 +02:00
|
|
|
/* No, not a built in command (or, at least, we were unable to start a
|
|
|
|
* built-in command of that name). Maybe it is a built-in application
|
|
|
|
* or an NSH command.
|
2014-01-10 23:15:14 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-05-18 08:46:22 +02:00
|
|
|
/* Does this command correspond to an application filename?
|
|
|
|
* nsh_fileapp() returns:
|
2014-01-10 23:15:14 +01:00
|
|
|
*
|
2020-05-18 00:07:36 +02:00
|
|
|
* -1 (ERROR) if the application task corresponding to 'argv[0]' could
|
|
|
|
* not be started (possibly because it doesn not exist).
|
2014-01-10 23:15:14 +01:00
|
|
|
* 0 (OK) if the application task corresponding to 'argv[0]' was
|
|
|
|
* and successfully started. If CONFIG_SCHED_WAITPID is
|
|
|
|
* defined, this return value also indicates that the
|
|
|
|
* application returned successful status (EXIT_SUCCESS)
|
|
|
|
* 1 If CONFIG_SCHED_WAITPID is defined, then this return value
|
2020-05-18 00:07:36 +02:00
|
|
|
* indicates that the application task was spawned
|
|
|
|
* successfully but returned failure exit status.
|
2014-01-10 23:15:14 +01:00
|
|
|
*
|
2020-05-18 03:01:23 +02:00
|
|
|
* Note the priority is not effected by nice-ness.
|
2014-01-10 23:15:14 +01:00
|
|
|
*/
|
|
|
|
|
2020-05-18 08:46:22 +02:00
|
|
|
#ifdef CONFIG_NSH_FILE_APPS
|
|
|
|
ret = nsh_fileapp(vtbl, argv[0], argv, redirfile, oflags);
|
2014-01-10 23:15:14 +01:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
2020-05-18 08:46:22 +02:00
|
|
|
/* nsh_fileapp() returned 0 or 1. This means that the built-in
|
2014-01-10 23:15:14 +01:00
|
|
|
* command was successfully started (although it may not have ran
|
|
|
|
* successfully). So certainly it is not an NSH command.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Save the result: success if 0; failure if 1 */
|
|
|
|
|
|
|
|
return nsh_saveresult(vtbl, ret != OK);
|
|
|
|
}
|
|
|
|
|
2020-05-18 08:46:22 +02:00
|
|
|
/* No, not a file name command (or, at least, we were unable to start a
|
|
|
|
* program of that name). Treat it like an NSH command.
|
2014-01-10 23:15:14 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-08-13 16:17:42 +02:00
|
|
|
#ifdef CONFIG_FILE_STREAM
|
2014-01-10 23:15:14 +01:00
|
|
|
/* Redirected output? */
|
|
|
|
|
|
|
|
if (vtbl->np.np_redirect)
|
|
|
|
{
|
|
|
|
/* Open the redirection file. This file will eventually
|
|
|
|
* be closed by a call to either nsh_release (if the command
|
|
|
|
* is executed in the background) or by nsh_undirect if the
|
|
|
|
* command is executed in the foreground.
|
|
|
|
*/
|
|
|
|
|
|
|
|
fd = open(redirfile, oflags, 0666);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "open", NSH_ERRNO);
|
2014-01-10 23:15:14 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Handle the case where the command is executed in background.
|
2014-01-11 01:42:24 +01:00
|
|
|
* However is app is to be started as built-in new process will
|
2014-01-10 23:15:14 +01:00
|
|
|
* be created anyway, so skip this step.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_DISABLEBG
|
|
|
|
if (vtbl->np.np_bg)
|
|
|
|
{
|
|
|
|
struct sched_param param;
|
|
|
|
struct nsh_vtbl_s *bkgvtbl;
|
|
|
|
struct cmdarg_s *args;
|
|
|
|
pthread_attr_t attr;
|
|
|
|
pthread_t thread;
|
|
|
|
|
|
|
|
/* Get a cloned copy of the vtbl with reference count=1.
|
|
|
|
* after the command has been processed, the nsh_release() call
|
|
|
|
* at the end of nsh_child() will destroy the clone.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bkgvtbl = nsh_clone(vtbl);
|
|
|
|
if (!bkgvtbl)
|
|
|
|
{
|
|
|
|
goto errout_with_redirect;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a container for the command arguments */
|
|
|
|
|
|
|
|
args = nsh_cloneargs(bkgvtbl, fd, argc, argv);
|
|
|
|
if (!args)
|
|
|
|
{
|
|
|
|
nsh_release(bkgvtbl);
|
|
|
|
goto errout_with_redirect;
|
|
|
|
}
|
|
|
|
|
2020-08-13 16:17:42 +02:00
|
|
|
#ifdef CONFIG_FILE_STREAM
|
2014-01-10 23:15:14 +01:00
|
|
|
/* Handle redirection of output via a file descriptor */
|
|
|
|
|
|
|
|
if (vtbl->np.np_redirect)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
nsh_redirect(bkgvtbl, fd, NULL);
|
2014-01-10 23:15:14 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Get the execution priority of this task */
|
|
|
|
|
|
|
|
ret = sched_getparam(0, ¶m);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2020-05-18 00:07:36 +02:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "sched_getparm",
|
|
|
|
NSH_ERRNO);
|
2020-08-11 09:15:16 +02:00
|
|
|
|
|
|
|
/* NOTE: bkgvtbl is released in nsh_relaseargs() */
|
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
nsh_releaseargs(args);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine the priority to execute the command */
|
|
|
|
|
|
|
|
if (vtbl->np.np_nice != 0)
|
|
|
|
{
|
|
|
|
int priority = param.sched_priority - vtbl->np.np_nice;
|
|
|
|
if (vtbl->np.np_nice < 0)
|
|
|
|
{
|
|
|
|
int max_priority = sched_get_priority_max(SCHED_NSH);
|
|
|
|
if (priority > max_priority)
|
|
|
|
{
|
|
|
|
priority = max_priority;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int min_priority = sched_get_priority_min(SCHED_NSH);
|
|
|
|
if (priority < min_priority)
|
|
|
|
{
|
|
|
|
priority = min_priority;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
param.sched_priority = priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up the thread attributes */
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
pthread_attr_init(&attr);
|
|
|
|
pthread_attr_setschedpolicy(&attr, SCHED_NSH);
|
|
|
|
pthread_attr_setschedparam(&attr, ¶m);
|
2014-01-10 23:15:14 +01:00
|
|
|
|
2020-05-18 00:07:36 +02:00
|
|
|
/* Execute the command as a separate thread at the appropriate
|
|
|
|
* priority.
|
|
|
|
*/
|
2014-01-10 23:15:14 +01:00
|
|
|
|
|
|
|
ret = pthread_create(&thread, &attr, nsh_child, (pthread_addr_t)args);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2020-05-18 00:07:36 +02:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "pthread_create",
|
|
|
|
NSH_ERRNO_OF(ret));
|
2020-08-11 09:15:16 +02:00
|
|
|
|
|
|
|
/* NOTE: bkgvtbl is released in nsh_relaseargs() */
|
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
nsh_releaseargs(args);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Detach from the pthread since we are not going to join with it.
|
|
|
|
* Otherwise, we would have a memory leak.
|
|
|
|
*/
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
pthread_detach(thread);
|
2014-01-10 23:15:14 +01:00
|
|
|
|
2020-05-18 00:07:36 +02:00
|
|
|
nsh_output(vtbl, "%s [%d:%d]\n", argv[0], thread,
|
|
|
|
param.sched_priority);
|
2014-01-10 23:15:14 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2020-08-13 16:17:42 +02:00
|
|
|
#ifdef CONFIG_FILE_STREAM
|
2014-01-10 23:15:14 +01:00
|
|
|
uint8_t save[SAVE_SIZE];
|
|
|
|
|
|
|
|
/* Handle redirection of output via a file descriptor */
|
|
|
|
|
|
|
|
if (vtbl->np.np_redirect)
|
|
|
|
{
|
|
|
|
nsh_redirect(vtbl, fd, save);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-05-18 00:07:36 +02:00
|
|
|
/* Then execute the command in "foreground" -- i.e., while the user
|
|
|
|
* waits for the next prompt. nsh_command will return:
|
2014-01-10 23:15:14 +01:00
|
|
|
*
|
2020-02-23 05:51:44 +01:00
|
|
|
* -1 (ERROR) if the command was unsuccessful
|
2014-01-10 23:15:14 +01:00
|
|
|
* 0 (OK) if the command was successful
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = nsh_command(vtbl, argc, argv);
|
|
|
|
|
2020-08-13 16:17:42 +02:00
|
|
|
#ifdef CONFIG_FILE_STREAM
|
2014-01-10 23:15:14 +01:00
|
|
|
/* Restore the original output. Undirect will close the redirection
|
|
|
|
* file descriptor.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (vtbl->np.np_redirect)
|
|
|
|
{
|
|
|
|
nsh_undirect(vtbl, save);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-05-18 00:07:36 +02:00
|
|
|
/* Mark errors so that it is possible to test for non-zero return
|
|
|
|
* values in nsh scripts.
|
2014-01-10 23:15:14 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return success if the command succeeded (or at least, starting of the
|
|
|
|
* command task succeeded).
|
|
|
|
*/
|
|
|
|
|
|
|
|
return nsh_saveresult(vtbl, false);
|
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_DISABLEBG
|
|
|
|
errout_with_redirect:
|
2020-08-13 16:17:42 +02:00
|
|
|
#ifdef CONFIG_FILE_STREAM
|
2014-01-10 23:15:14 +01:00
|
|
|
if (vtbl->np.np_redirect)
|
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
errout:
|
|
|
|
return nsh_saveresult(vtbl, true);
|
|
|
|
}
|
|
|
|
|
2014-01-11 16:50:54 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_filecat
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NSH_CMDPARMS
|
|
|
|
static FAR char *nsh_filecat(FAR struct nsh_vtbl_s *vtbl, FAR char *s1,
|
|
|
|
FAR const char *filename)
|
|
|
|
{
|
|
|
|
struct stat buf;
|
|
|
|
size_t s1size = 0;
|
|
|
|
size_t allocsize;
|
|
|
|
ssize_t nbytesread;
|
|
|
|
FAR char *argument;
|
|
|
|
int index;
|
|
|
|
int fd;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get the size of the string */
|
|
|
|
|
|
|
|
if (s1)
|
|
|
|
{
|
|
|
|
s1size = (size_t)strlen(s1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the size of file */
|
|
|
|
|
|
|
|
ret = stat(filename, &buf);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, "``", "stat", NSH_ERRNO);
|
2014-01-11 16:50:54 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the total allocation size */
|
|
|
|
|
|
|
|
allocsize = s1size + (size_t)buf.st_size + 1;
|
|
|
|
argument = (FAR char *)realloc(s1, allocsize);
|
|
|
|
if (!argument)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdoutofmemory, "``");
|
2014-01-11 16:50:54 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the source file for reading */
|
|
|
|
|
|
|
|
fd = open(filename, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
2021-12-31 15:02:26 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, "``", "open", NSH_ERRNO);
|
2014-01-11 16:50:54 +01:00
|
|
|
goto errout_with_alloc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now copy the file. Loop until the entire file has been transferred to
|
|
|
|
* the allocated string (after the original contents of s1size bytes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (index = s1size; index < allocsize - 1; )
|
|
|
|
{
|
|
|
|
/* Loop until we successfully read something , we encounter the
|
2014-01-11 20:18:12 +01:00
|
|
|
* end-of-file, or until a read error occurs
|
2014-01-11 16:50:54 +01:00
|
|
|
*/
|
2014-01-11 20:18:12 +01:00
|
|
|
|
2014-01-11 16:50:54 +01:00
|
|
|
do
|
|
|
|
{
|
|
|
|
nbytesread = read(fd, &argument[index], IOBUFFERSIZE);
|
|
|
|
if (nbytesread == 0)
|
|
|
|
{
|
|
|
|
/* Unexpected end of file -- Break out of the loop */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (nbytesread < 0)
|
|
|
|
{
|
|
|
|
/* EINTR is not an error (but will still stop the copy) */
|
|
|
|
|
|
|
|
if (errno == EINTR)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtsignalrecvd, "``");
|
2014-01-11 16:50:54 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Read error */
|
|
|
|
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, "``", "read", NSH_ERRNO);
|
2014-01-11 16:50:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
goto errout_with_fd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (nbytesread <= 0);
|
|
|
|
|
|
|
|
/* Update the index based upon the number of bytes read */
|
|
|
|
|
|
|
|
index += nbytesread;
|
|
|
|
}
|
|
|
|
|
2021-12-31 15:02:26 +01:00
|
|
|
/* Remove trailing whitespace */
|
|
|
|
|
|
|
|
for (;
|
|
|
|
index > s1size &&
|
|
|
|
strchr(g_token_separator, argument[index - 1]) != NULL;
|
|
|
|
index--);
|
|
|
|
|
2014-01-11 16:50:54 +01:00
|
|
|
/* Make sure that the new string is null terminated */
|
|
|
|
|
|
|
|
argument[index] = '\0';
|
2014-01-12 21:16:05 +01:00
|
|
|
|
|
|
|
/* Close the temporary file and return the concatenated value */
|
|
|
|
|
|
|
|
close (fd);
|
2014-01-11 16:50:54 +01:00
|
|
|
return argument;
|
|
|
|
|
|
|
|
errout_with_fd:
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
errout_with_alloc:
|
|
|
|
free(argument);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_cmdparm
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NSH_CMDPARMS
|
|
|
|
static FAR char *nsh_cmdparm(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline,
|
|
|
|
FAR char **allocation)
|
|
|
|
{
|
|
|
|
FAR char *tmpfile;
|
|
|
|
FAR char *argument;
|
|
|
|
int ret;
|
|
|
|
|
2020-05-18 00:07:36 +02:00
|
|
|
/* We cannot process the command argument if there is no allocation
|
|
|
|
* pointer.
|
|
|
|
*/
|
2014-01-11 16:50:54 +01:00
|
|
|
|
|
|
|
if (!allocation)
|
|
|
|
{
|
2019-12-19 14:52:00 +01:00
|
|
|
return (FAR char *)g_nullstring;
|
2014-01-11 16:50:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a unique file name using the task ID */
|
|
|
|
|
|
|
|
tmpfile = NULL;
|
2014-11-05 14:49:20 +01:00
|
|
|
ret = asprintf(&tmpfile, "%s/TMP%d.dat", CONFIG_LIBC_TMPDIR, getpid());
|
2014-01-11 16:50:54 +01:00
|
|
|
if (ret < 0 || !tmpfile)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdoutofmemory, "``");
|
2019-12-19 14:52:00 +01:00
|
|
|
return (FAR char *)g_nullstring;
|
2014-01-11 16:50:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Execute the command that will re-direct the output of the command to
|
|
|
|
* the temporary file. This is a simple command that can't handle most
|
|
|
|
* options.
|
|
|
|
*/
|
|
|
|
|
2014-01-12 21:16:05 +01:00
|
|
|
ret = nsh_parse_cmdparm(vtbl, cmdline, tmpfile);
|
2014-01-11 16:50:54 +01:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
/* Report the failure */
|
|
|
|
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, "``", "exec", NSH_ERRNO);
|
2014-01-11 16:50:54 +01:00
|
|
|
free(tmpfile);
|
2019-12-19 14:52:00 +01:00
|
|
|
return (FAR char *)g_nullstring;
|
2014-01-11 16:50:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Concatenate the file contents with the current allocation */
|
|
|
|
|
2021-12-31 15:54:57 +01:00
|
|
|
argument = nsh_filecat(vtbl, *allocation, tmpfile);
|
|
|
|
if (argument == NULL)
|
|
|
|
{
|
|
|
|
argument = (FAR char *)g_nullstring;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*allocation = argument;
|
|
|
|
}
|
2014-01-11 16:50:54 +01:00
|
|
|
|
|
|
|
/* We can now unlink the tmpfile and free the tmpfile string */
|
|
|
|
|
|
|
|
ret = unlink(tmpfile);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, "``", "unlink", NSH_ERRNO);
|
2014-01-11 16:50:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
free(tmpfile);
|
|
|
|
return argument;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_strcat
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NSH_ARGCAT
|
2014-01-11 20:18:12 +01:00
|
|
|
static FAR char *nsh_strcat(FAR struct nsh_vtbl_s *vtbl, FAR char *s1,
|
|
|
|
FAR const char *s2)
|
|
|
|
{
|
|
|
|
FAR char *argument;
|
|
|
|
int s1size = 0;
|
|
|
|
int allocsize;
|
|
|
|
|
|
|
|
/* Get the size of the first string... it might be NULL */
|
|
|
|
|
|
|
|
if (s1)
|
|
|
|
{
|
|
|
|
s1size = strlen(s1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then reallocate the first string so that it is large enough to hold
|
|
|
|
* both (including the NUL terminator).
|
|
|
|
*/
|
|
|
|
|
|
|
|
allocsize = s1size + strlen(s2) + 1;
|
|
|
|
argument = (FAR char *)realloc(s1, allocsize);
|
|
|
|
if (!argument)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdoutofmemory, "$");
|
2014-01-11 20:18:12 +01:00
|
|
|
argument = s1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
argument[s1size] = '\0'; /* (In case s1 was NULL) */
|
|
|
|
strcat(argument, s2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return argument;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-08-06 21:28:31 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_strchr
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(CONFIG_NSH_QUOTE) && defined(CONFIG_NSH_ARGCAT)
|
|
|
|
static FAR char *nsh_strchr(FAR const char *str, int ch)
|
|
|
|
{
|
|
|
|
FAR const char *ptr;
|
|
|
|
bool quoted = false;
|
|
|
|
|
|
|
|
for (ptr = str; ; ptr++)
|
|
|
|
{
|
|
|
|
if (*ptr == '\\' && !quoted)
|
|
|
|
{
|
|
|
|
quoted = true;
|
|
|
|
}
|
|
|
|
else if ((int)*ptr == ch && !quoted)
|
|
|
|
{
|
|
|
|
return (FAR char *)ptr;
|
|
|
|
}
|
|
|
|
else if (*ptr == '\0')
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
quoted = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-01-11 20:18:12 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_envexpand
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-10-01 21:30:25 +02:00
|
|
|
#ifdef NSH_HAVE_VARS
|
2020-05-18 00:07:36 +02:00
|
|
|
static FAR char *nsh_envexpand(FAR struct nsh_vtbl_s *vtbl,
|
|
|
|
FAR char *varname)
|
2014-01-11 20:18:12 +01:00
|
|
|
{
|
|
|
|
/* Check for built-in variables */
|
|
|
|
|
|
|
|
if (strcmp(varname, g_exitstatus) == 0)
|
|
|
|
{
|
|
|
|
if (vtbl->np.np_fail)
|
|
|
|
{
|
|
|
|
return (FAR char *)g_failure;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return (FAR char *)g_success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-02 19:16:15 +02:00
|
|
|
FAR char *value;
|
2018-10-01 21:30:25 +02:00
|
|
|
|
2018-10-02 18:21:33 +02:00
|
|
|
#ifdef CONFIG_NSH_VARS
|
2018-10-01 21:30:25 +02:00
|
|
|
/* Not a built-in? Return the value of the NSH variable with this
|
|
|
|
* name.
|
|
|
|
*/
|
|
|
|
|
|
|
|
value = nsh_getvar(vtbl, varname);
|
|
|
|
if (value != NULL)
|
2014-01-11 20:18:12 +01:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
2018-10-01 21:30:25 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_ENVIRON
|
2018-10-02 18:21:33 +02:00
|
|
|
/* Not an NSH variable? Return the value of the NSH variable
|
|
|
|
* environment variable with this name.
|
|
|
|
*/
|
|
|
|
|
2018-10-01 21:30:25 +02:00
|
|
|
value = getenv(varname);
|
|
|
|
if (value != NULL)
|
2014-01-11 20:18:12 +01:00
|
|
|
{
|
2018-10-01 21:30:25 +02:00
|
|
|
return value;
|
2014-01-11 20:18:12 +01:00
|
|
|
}
|
2018-10-01 21:30:25 +02:00
|
|
|
#endif
|
|
|
|
|
2019-12-19 14:52:00 +01:00
|
|
|
return (FAR char *)g_nullstring;
|
2014-01-11 20:18:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-08-06 21:28:31 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_dequote
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(CONFIG_NSH_QUOTE) && defined(CONFIG_NSH_ARGCAT)
|
|
|
|
static void nsh_dequote(FAR char *cmdline)
|
|
|
|
{
|
|
|
|
FAR char *ptr;
|
|
|
|
bool quoted;
|
|
|
|
|
|
|
|
quoted = false;
|
|
|
|
|
|
|
|
for (ptr = cmdline; *ptr != '\0'; )
|
|
|
|
{
|
|
|
|
if (*ptr == '\\' && !quoted)
|
|
|
|
{
|
|
|
|
FAR char *dest = ptr;
|
|
|
|
FAR const char *src = ptr + 1;
|
|
|
|
char ch;
|
|
|
|
|
|
|
|
/* Move the data to eliminate the quote from the command line */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ch = *src++;
|
|
|
|
*dest++ = ch;
|
|
|
|
}
|
|
|
|
while (ch != '\0');
|
|
|
|
|
|
|
|
/* Remember that the next character is quote (in case it is
|
|
|
|
* another back-slash character).
|
|
|
|
*/
|
|
|
|
|
|
|
|
quoted = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The next character is not quoted because either (1) it was not
|
|
|
|
* preceded by a back-slash, or (2) it was preceded by a quoted
|
|
|
|
* back-slash.
|
|
|
|
*/
|
|
|
|
|
|
|
|
quoted = false;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that the new, possibly shorted string is NUL terminated */
|
|
|
|
|
|
|
|
*ptr = '\0';
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-01-11 20:18:12 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_argexpand
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(CONFIG_NSH_ARGCAT) && defined(HAVE_MEMLIST)
|
2020-05-18 00:07:36 +02:00
|
|
|
static FAR char *nsh_argexpand(FAR struct nsh_vtbl_s *vtbl,
|
|
|
|
FAR char *cmdline, FAR char **allocation,
|
|
|
|
FAR int *isenvvar)
|
2014-01-11 20:18:12 +01:00
|
|
|
{
|
|
|
|
FAR char *working = cmdline;
|
2018-08-06 23:52:02 +02:00
|
|
|
#ifdef CONFIG_NSH_QUOTE
|
|
|
|
FAR char *nextwork;
|
|
|
|
#endif
|
2014-01-11 20:18:12 +01:00
|
|
|
FAR char *argument = NULL;
|
|
|
|
FAR char *ptr;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
/* Loop until all of the commands on the command line have been processed */
|
|
|
|
|
2018-08-06 21:28:31 +02:00
|
|
|
for (; ; )
|
2014-01-11 20:18:12 +01:00
|
|
|
{
|
|
|
|
/* Look for interesting things within the command string. */
|
|
|
|
|
2018-08-06 23:52:02 +02:00
|
|
|
len = strcspn(working, g_arg_separator);
|
|
|
|
ptr = working + len;
|
2018-08-06 21:28:31 +02:00
|
|
|
#ifdef CONFIG_NSH_QUOTE
|
2018-08-06 23:52:02 +02:00
|
|
|
nextwork = ptr + 1;
|
|
|
|
|
2018-08-06 21:28:31 +02:00
|
|
|
/* But ignore these interesting things if they are quoted */
|
|
|
|
|
|
|
|
while (len > 0 && *ptr != '\0')
|
|
|
|
{
|
|
|
|
FAR char *prev = working + len - 1;
|
|
|
|
int bcount;
|
|
|
|
bool quoted;
|
|
|
|
|
|
|
|
/* Check if the current character is quoted */
|
|
|
|
|
|
|
|
for (bcount = 0, quoted = false;
|
|
|
|
bcount < len && *prev == '\\';
|
|
|
|
bcount++, prev--)
|
|
|
|
{
|
|
|
|
quoted ^= true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (quoted)
|
|
|
|
{
|
|
|
|
/* Yes.. skip over it */
|
|
|
|
|
2018-08-06 23:52:02 +02:00
|
|
|
len += strcspn(ptr + 1, g_arg_separator) + 1;
|
|
|
|
ptr = working + len;
|
|
|
|
nextwork = ptr + 1;
|
2018-08-06 21:28:31 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Not quoted.. subject to normal processing */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2014-01-11 20:18:12 +01:00
|
|
|
|
|
|
|
/* If ptr points to the NUL terminator, then there is nothing else
|
|
|
|
* interesting in the argument.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (*ptr == '\0')
|
|
|
|
{
|
|
|
|
/* Was anything previously concatenated? */
|
|
|
|
|
|
|
|
if (argument)
|
|
|
|
{
|
2020-05-18 00:07:36 +02:00
|
|
|
/* Yes, then we probably need to add the last part of the
|
|
|
|
* argument beginning at the last working pointer to the
|
|
|
|
* concatenated argument.
|
2014-01-11 20:18:12 +01:00
|
|
|
*
|
2020-05-18 00:07:36 +02:00
|
|
|
* On failures to allocation memory, nsh_strcat will just
|
|
|
|
* return old value of argument
|
2014-01-11 20:18:12 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
argument = nsh_strcat(vtbl, argument, working);
|
|
|
|
*allocation = argument;
|
2018-08-06 21:28:31 +02:00
|
|
|
|
|
|
|
/* De-quote the returned string */
|
|
|
|
|
|
|
|
nsh_dequote(argument);
|
2014-01-11 20:18:12 +01:00
|
|
|
return argument;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-18 00:07:36 +02:00
|
|
|
/* No.. just return the original string from the command
|
|
|
|
* line.
|
|
|
|
*/
|
2014-01-11 20:18:12 +01:00
|
|
|
|
2018-08-06 21:28:31 +02:00
|
|
|
nsh_dequote(cmdline);
|
2014-01-11 20:18:12 +01:00
|
|
|
return cmdline;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
|
|
|
|
#ifdef CONFIG_NSH_CMDPARMS
|
2020-05-18 00:07:36 +02:00
|
|
|
/* Check for a back-quoted command embedded within the argument
|
|
|
|
* string.
|
|
|
|
*/
|
2014-01-11 20:18:12 +01:00
|
|
|
|
|
|
|
if (*ptr == '`')
|
|
|
|
{
|
2014-03-08 16:24:24 +01:00
|
|
|
FAR char *tmpalloc = NULL;
|
2014-01-11 20:18:12 +01:00
|
|
|
FAR char *result;
|
|
|
|
FAR char *rptr;
|
|
|
|
|
2018-08-06 21:28:31 +02:00
|
|
|
/* Replace the back-quote with a NUL terminator and add the
|
2014-01-11 20:18:12 +01:00
|
|
|
* intervening character to the concatenated string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
*ptr++ = '\0';
|
|
|
|
argument = nsh_strcat(vtbl, argument, working);
|
|
|
|
*allocation = argument;
|
|
|
|
|
2018-08-06 21:28:31 +02:00
|
|
|
/* Find the closing back-quote (must be unquoted) */
|
2014-01-11 20:18:12 +01:00
|
|
|
|
2018-08-06 21:28:31 +02:00
|
|
|
rptr = nsh_strchr(ptr, '`');
|
2014-01-11 20:18:12 +01:00
|
|
|
if (!rptr)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtnomatching, "`", "`");
|
2019-12-19 14:52:00 +01:00
|
|
|
return (FAR char *)g_nullstring;
|
2014-01-11 20:18:12 +01:00
|
|
|
}
|
|
|
|
|
2018-08-06 21:28:31 +02:00
|
|
|
/* Replace the final back-quote with a NUL terminator */
|
2014-01-11 20:18:12 +01:00
|
|
|
|
|
|
|
*rptr = '\0';
|
|
|
|
|
|
|
|
/* Then execute the command to get the sub-string value. On
|
2019-12-19 14:52:00 +01:00
|
|
|
* error, nsh_cmdparm may return g_nullstring but never NULL.
|
2014-01-11 20:18:12 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
result = nsh_cmdparm(vtbl, ptr, &tmpalloc);
|
|
|
|
|
|
|
|
/* Concatenate the result of the operation with the accumulated
|
|
|
|
* string. On failures to allocation memory, nsh_strcat will
|
2018-08-06 21:28:31 +02:00
|
|
|
* just return old value of argument
|
2014-01-11 20:18:12 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
argument = nsh_strcat(vtbl, argument, result);
|
|
|
|
*allocation = argument;
|
|
|
|
working = rptr + 1;
|
|
|
|
|
|
|
|
/* And free any temporary allocations */
|
|
|
|
|
|
|
|
if (tmpalloc)
|
|
|
|
{
|
|
|
|
free(tmpalloc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
|
2018-10-02 19:08:17 +02:00
|
|
|
#ifdef NSH_HAVE_VARS
|
2014-01-11 20:18:12 +01:00
|
|
|
/* Check if we encountered a reference to an environment variable */
|
|
|
|
|
|
|
|
if (*ptr == '$')
|
|
|
|
{
|
2018-10-01 21:30:25 +02:00
|
|
|
FAR const char *envstr;
|
2014-01-11 20:18:12 +01:00
|
|
|
FAR char *rptr;
|
|
|
|
|
|
|
|
/* Replace the dollar sign with a NUL terminator and add the
|
|
|
|
* intervening character to the concatenated string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
*ptr++ = '\0';
|
|
|
|
argument = nsh_strcat(vtbl, argument, working);
|
|
|
|
*allocation = argument;
|
|
|
|
|
|
|
|
/* Find the end of the environment variable reference. If the
|
2019-01-06 16:52:18 +01:00
|
|
|
* dollar sign ('$') is followed by a left bracket ('{') then the
|
|
|
|
* variable name is terminated with the right bracket character
|
2014-01-11 20:18:12 +01:00
|
|
|
* ('}'). Otherwise, the variable name goes to the end of the
|
|
|
|
* argument.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (*ptr == '{')
|
|
|
|
{
|
|
|
|
/* Skip over the left bracket */
|
|
|
|
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
/* Find the closing right bracket */
|
|
|
|
|
2018-08-06 21:28:31 +02:00
|
|
|
rptr = nsh_strchr(ptr, '}');
|
2014-01-11 20:18:12 +01:00
|
|
|
if (!rptr)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtnomatching, "${", "}");
|
2019-12-19 14:52:00 +01:00
|
|
|
return (FAR char *)g_nullstring;
|
2014-01-11 20:18:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Replace the right bracket with a NUL terminator and set the
|
|
|
|
* working pointer to the character after the bracket.
|
|
|
|
*/
|
|
|
|
|
|
|
|
*rptr = '\0';
|
|
|
|
working = rptr + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-06 23:52:02 +02:00
|
|
|
/* Set working to the NUL terminator at the end of the string.
|
|
|
|
*
|
|
|
|
* REVISIT: Needs logic to get the size of the variable name
|
|
|
|
* based on parsing the name string which must be of the form
|
|
|
|
* [a-zA-Z_]+[a-zA-Z0-9_]*
|
|
|
|
*/
|
2014-01-11 20:18:12 +01:00
|
|
|
|
|
|
|
working = ptr + strlen(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then get the value of the environment variable. On errors,
|
|
|
|
* nsh_envexpand will return the NULL string.
|
|
|
|
*/
|
|
|
|
|
2019-01-06 17:00:19 +01:00
|
|
|
if (isenvvar != NULL)
|
|
|
|
{
|
2019-01-06 16:52:18 +01:00
|
|
|
*isenvvar = 1;
|
2019-01-06 17:00:19 +01:00
|
|
|
}
|
2019-01-06 16:52:18 +01:00
|
|
|
|
2014-01-11 20:18:12 +01:00
|
|
|
envstr = nsh_envexpand(vtbl, ptr);
|
|
|
|
|
2018-01-22 15:32:40 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLESCRIPT
|
2017-04-06 02:31:47 +02:00
|
|
|
if ((vtbl->np.np_flags & NSH_PFLAG_SILENT) == 0)
|
2018-01-22 15:32:40 +01:00
|
|
|
#endif
|
2017-04-06 02:31:47 +02:00
|
|
|
{
|
2020-05-18 00:07:36 +02:00
|
|
|
nsh_output(vtbl, " %s=%s\n", ptr, envstr ? envstr : "(null)");
|
2017-04-06 02:31:47 +02:00
|
|
|
}
|
|
|
|
|
2014-01-11 20:18:12 +01:00
|
|
|
/* Concatenate the result of the operation with the accumulated
|
|
|
|
* string. On failures to allocation memory, nsh_strcat will
|
2018-08-06 21:28:31 +02:00
|
|
|
* just return old value of argument
|
2014-01-11 20:18:12 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
argument = nsh_strcat(vtbl, argument, envstr);
|
|
|
|
*allocation = argument;
|
|
|
|
}
|
2014-02-07 17:56:24 +01:00
|
|
|
else
|
2014-01-11 20:18:12 +01:00
|
|
|
#endif
|
2014-02-07 17:56:24 +01:00
|
|
|
{
|
2017-03-09 15:06:53 +01:00
|
|
|
/* Not a special character... skip to the next character in the
|
|
|
|
* cmdline.
|
|
|
|
*/
|
|
|
|
|
2018-08-06 23:52:02 +02:00
|
|
|
#ifdef CONFIG_NSH_QUOTE
|
|
|
|
working = nextwork;
|
|
|
|
#else
|
2017-03-09 15:06:53 +01:00
|
|
|
working++;
|
2018-08-06 23:52:02 +02:00
|
|
|
#endif
|
2014-02-07 17:56:24 +01:00
|
|
|
}
|
2014-01-11 20:18:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2020-05-18 00:07:36 +02:00
|
|
|
static FAR char *nsh_argexpand(FAR struct nsh_vtbl_s *vtbl,
|
|
|
|
FAR char *cmdline, FAR char **allocation,
|
|
|
|
FAR int *isenvvar)
|
2014-01-11 16:50:54 +01:00
|
|
|
{
|
2019-12-19 14:52:00 +01:00
|
|
|
FAR char *argument = (FAR char *)g_nullstring;
|
2018-08-06 21:28:31 +02:00
|
|
|
#ifdef CONFIG_NSH_QUOTE
|
|
|
|
char ch = *cmdline;
|
|
|
|
|
|
|
|
/* A single backslash at the beginning of the line is support, nothing
|
|
|
|
* more.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nsh_dequote(cmdline);
|
|
|
|
if (ch == '\\')
|
|
|
|
{
|
|
|
|
argument = cmdline;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2014-01-11 20:18:12 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NSH_CMDPARMS
|
|
|
|
/* Are we being asked to use the output from another command or program
|
|
|
|
* as an input parameters for this command?
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (*cmdline == '`')
|
|
|
|
{
|
2018-08-06 21:28:31 +02:00
|
|
|
/* Verify that the final character is also a back-quote */
|
2014-01-11 20:18:12 +01:00
|
|
|
|
2018-08-06 21:28:31 +02:00
|
|
|
FAR char *rptr = nsh_strchr(cmdline + 1, '`');
|
2014-01-11 20:18:12 +01:00
|
|
|
if (!rptr || rptr[1] != '\0')
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtnomatching, "`", "`");
|
2019-12-19 14:52:00 +01:00
|
|
|
return (FAR char *)g_nullstring;
|
2014-01-11 20:18:12 +01:00
|
|
|
}
|
|
|
|
|
2018-08-06 21:28:31 +02:00
|
|
|
/* Replace the final back-quote with a NUL terminator */
|
2014-01-11 20:18:12 +01:00
|
|
|
|
|
|
|
*rptr = '\0';
|
|
|
|
|
2014-07-07 23:54:37 +02:00
|
|
|
/* Then execute the command to get the parameter value */
|
2014-01-11 20:18:12 +01:00
|
|
|
|
|
|
|
argument = nsh_cmdparm(vtbl, cmdline + 1, allocation);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
|
2018-10-01 21:30:25 +02:00
|
|
|
#ifdef NSH_HAVE_VARS
|
2014-01-11 20:18:12 +01:00
|
|
|
/* Check for references to environment variables */
|
|
|
|
|
|
|
|
if (*cmdline == '$')
|
|
|
|
{
|
2019-01-06 17:00:19 +01:00
|
|
|
if (isenvvar != NULL)
|
|
|
|
{
|
2019-01-06 16:52:18 +01:00
|
|
|
*isenvvar = 1;
|
2019-01-06 17:00:19 +01:00
|
|
|
}
|
|
|
|
|
2014-01-11 20:18:12 +01:00
|
|
|
argument = nsh_envexpand(vtbl, cmdline + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* The argument to be returned is simply the beginning of the
|
|
|
|
* delimited string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
argument = cmdline;
|
|
|
|
}
|
|
|
|
|
|
|
|
return argument;
|
2014-01-11 16:50:54 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_argument
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-18 00:07:36 +02:00
|
|
|
static FAR char *nsh_argument(FAR struct nsh_vtbl_s *vtbl,
|
|
|
|
FAR char **saveptr,
|
|
|
|
FAR NSH_MEMLIST_TYPE *memlist,
|
|
|
|
FAR int *isenvvar)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2014-01-11 16:50:54 +01:00
|
|
|
FAR char *pbegin = *saveptr;
|
|
|
|
FAR char *pend = NULL;
|
|
|
|
FAR char *allocation = NULL;
|
|
|
|
FAR char *argument = NULL;
|
|
|
|
FAR const char *term;
|
2018-08-06 21:28:31 +02:00
|
|
|
#ifdef CONFIG_NSH_QUOTE
|
|
|
|
FAR char *prev;
|
|
|
|
bool quoted;
|
|
|
|
#endif
|
2014-01-11 20:18:12 +01:00
|
|
|
#ifdef CONFIG_NSH_CMDPARMS
|
|
|
|
bool backquote;
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Find the beginning of the next token */
|
|
|
|
|
|
|
|
for (;
|
2014-01-10 22:23:26 +01:00
|
|
|
*pbegin && strchr(g_token_separator, *pbegin) != NULL;
|
2011-03-18 20:46:25 +01:00
|
|
|
pbegin++);
|
|
|
|
|
2014-01-10 22:23:26 +01:00
|
|
|
/* If we are at the end of the string with nothing but delimiters found,
|
2020-05-18 00:07:36 +02:00
|
|
|
* then return NULL, meaning that there are no further arguments on the
|
|
|
|
* line.
|
2011-03-18 20:46:25 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (!*pbegin)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Does the token begin with '>' -- redirection of output? */
|
|
|
|
|
|
|
|
if (*pbegin == '>')
|
|
|
|
{
|
|
|
|
/* Yes.. does it begin with ">>"? */
|
|
|
|
|
|
|
|
if (*(pbegin + 1) == '>')
|
|
|
|
{
|
|
|
|
*saveptr = pbegin + 2;
|
2014-01-16 14:30:15 +01:00
|
|
|
argument = (FAR char *)g_redirect2;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*saveptr = pbegin + 1;
|
2014-01-16 14:30:15 +01:00
|
|
|
argument = (FAR char *)g_redirect1;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Does the token begin with '#' -- comment */
|
|
|
|
|
|
|
|
else if (*pbegin == '#')
|
|
|
|
{
|
|
|
|
/* Return NULL meaning that we are at the end of the line */
|
|
|
|
|
|
|
|
*saveptr = pbegin;
|
2014-01-16 14:30:15 +01:00
|
|
|
argument = NULL;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
2014-01-11 01:42:24 +01:00
|
|
|
|
2014-01-11 20:18:12 +01:00
|
|
|
/* Otherwise, it is a normal argument and we have to parse using the normal
|
2014-01-11 01:42:24 +01:00
|
|
|
* rules to find the end of the argument.
|
|
|
|
*/
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
else
|
|
|
|
{
|
2014-01-11 01:42:24 +01:00
|
|
|
/* However, the rules are a little different if the next argument is
|
|
|
|
* a quoted string.
|
2011-03-18 20:46:25 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (*pbegin == '"')
|
|
|
|
{
|
2014-01-11 01:42:24 +01:00
|
|
|
/* A quoted string can only be terminated with another quotation
|
2017-10-27 14:41:26 +02:00
|
|
|
* mark. Set pbegin to point at the character after the opening
|
|
|
|
* quote mark.
|
2014-01-11 01:42:24 +01:00
|
|
|
*/
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
pbegin++;
|
|
|
|
term = "\"";
|
2019-01-06 16:52:18 +01:00
|
|
|
|
2019-01-06 17:00:19 +01:00
|
|
|
/* If this is an environment variable in double quotes, we don't
|
|
|
|
* want it split into multiple arguments. So just invalidate the
|
|
|
|
* flag pointer which would otherwise communicate such back up
|
|
|
|
* the call tree.
|
2019-01-06 16:52:18 +01:00
|
|
|
*/
|
2019-01-06 17:00:19 +01:00
|
|
|
|
2019-01-06 16:52:18 +01:00
|
|
|
isenvvar = NULL;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-10-27 14:41:26 +02:00
|
|
|
/* No, then any of the usual separators will terminate the
|
|
|
|
* argument. In this case, pbegin points for the first character
|
|
|
|
* of the token following the previous separator.
|
|
|
|
*/
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2014-01-10 22:23:26 +01:00
|
|
|
term = g_token_separator;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the end of the string */
|
|
|
|
|
2014-01-11 20:18:12 +01:00
|
|
|
#ifdef CONFIG_NSH_CMDPARMS
|
2020-05-18 00:07:36 +02:00
|
|
|
/* Some special care must be exercised to make sure that we do not
|
|
|
|
* break up any back-quote delimited substrings. NOTE that the
|
|
|
|
* absence of a closing back-quote is not detected; That case should
|
|
|
|
* be detected later.
|
2014-01-11 20:18:12 +01:00
|
|
|
*/
|
|
|
|
|
2018-08-06 21:28:31 +02:00
|
|
|
#ifdef CONFIG_NSH_QUOTE
|
|
|
|
quoted = false;
|
|
|
|
backquote = false;
|
|
|
|
|
|
|
|
for (prev = NULL, pend = pbegin; *pend != '\0'; prev = pend, pend++)
|
2014-01-11 20:18:12 +01:00
|
|
|
{
|
2018-08-06 21:28:31 +02:00
|
|
|
/* Check if the current character is quoted */
|
|
|
|
|
|
|
|
if (prev != NULL && *prev == '\\' && !quoted)
|
|
|
|
{
|
|
|
|
/* Do no special checks on the quoted character */
|
|
|
|
|
|
|
|
quoted = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
quoted = false;
|
|
|
|
|
|
|
|
/* Check if the current character is an (unquoted) back-quote */
|
|
|
|
|
|
|
|
if (*pend == '\\' && !quoted)
|
|
|
|
{
|
|
|
|
/* Yes.. Do no special processing on the backspace character */
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Toggle the back-quote flag when one is encountered? */
|
2014-01-11 20:18:12 +01:00
|
|
|
|
|
|
|
if (*pend == '`')
|
|
|
|
{
|
|
|
|
backquote = !backquote;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for a delimiting character only if we are not in a
|
2018-08-06 21:28:31 +02:00
|
|
|
* back-quoted sub-string.
|
2014-01-11 20:18:12 +01:00
|
|
|
*/
|
|
|
|
|
2018-08-06 21:28:31 +02:00
|
|
|
else if (!backquote && nsh_strchr(term, *pend) != NULL)
|
2014-01-11 20:18:12 +01:00
|
|
|
{
|
2018-08-06 21:28:31 +02:00
|
|
|
/* We found a delimiter outside of any back-quoted substring.
|
2014-01-11 20:18:12 +01:00
|
|
|
* Now we can break out of the loop.
|
|
|
|
*/
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2018-08-06 21:28:31 +02:00
|
|
|
backquote = false;
|
|
|
|
|
|
|
|
for (pend = pbegin; *pend != '\0'; pend++)
|
|
|
|
{
|
|
|
|
/* Toggle the back-quote flag when one is encountered? */
|
|
|
|
|
|
|
|
if (*pend == '`')
|
|
|
|
{
|
|
|
|
backquote = !backquote;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for a delimiting character only if we are not in a
|
|
|
|
* back-quoted sub-string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (!backquote && nsh_strchr(term, *pend) != NULL)
|
|
|
|
{
|
|
|
|
/* We found a delimiter outside of any back-quoted substring.
|
|
|
|
* Now we can break out of the loop.
|
|
|
|
*/
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NSH_QUOTE */
|
|
|
|
#else /* CONFIG_NSH_CMDPARMS */
|
|
|
|
|
|
|
|
/* Search the next occurrence of a terminating character (or the end
|
2017-10-27 14:41:26 +02:00
|
|
|
* of the line).
|
|
|
|
*/
|
|
|
|
|
2018-08-06 21:28:31 +02:00
|
|
|
#ifdef CONFIG_NSH_QUOTE
|
|
|
|
quoted = false;
|
|
|
|
|
|
|
|
for (prev = NULL, pend = pbegin; *pend != '\0'; prev = pend, pend++)
|
|
|
|
{
|
|
|
|
/* Check if the current character is quoted */
|
|
|
|
|
|
|
|
if (prev != NULL && *prev == '\\' && !quoted)
|
|
|
|
{
|
|
|
|
/* Do no special checks on the quoted character */
|
|
|
|
|
|
|
|
quoted = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
quoted = false;
|
|
|
|
|
|
|
|
/* Check if the current character is an (unquoted) back-quote */
|
|
|
|
|
|
|
|
if (*pend == '\\' && !quoted)
|
|
|
|
{
|
|
|
|
/* Yes.. Do no special processing on the backspace character */
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for a delimiting character */
|
|
|
|
|
|
|
|
if (nsh_strchr(term, *pend) != NULL)
|
|
|
|
{
|
|
|
|
/* We found a delimiter. Now we can break out of the loop. */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2017-10-27 14:41:26 +02:00
|
|
|
for (pend = pbegin;
|
2018-08-06 21:28:31 +02:00
|
|
|
*pend != '\0' && nsh_strchr(term, *pend) == NULL;
|
2017-10-27 14:41:26 +02:00
|
|
|
pend++)
|
|
|
|
{
|
|
|
|
}
|
2018-08-06 21:28:31 +02:00
|
|
|
|
|
|
|
#endif /* CONFIG_NSH_QUOTE */
|
|
|
|
#endif /* CONFIG_NSH_CMDPARMS */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2014-01-11 01:42:24 +01:00
|
|
|
/* pend either points to the end of the string or to the first
|
|
|
|
* delimiter after the string.
|
2011-03-18 20:46:25 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (*pend)
|
|
|
|
{
|
2014-01-11 01:42:24 +01:00
|
|
|
/* Turn the delimiter into a NUL terminator */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
*pend++ = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the pointer where we left off */
|
|
|
|
|
|
|
|
*saveptr = pend;
|
|
|
|
|
2014-01-11 20:18:12 +01:00
|
|
|
/* Perform expansions as necessary for the argument */
|
2014-01-11 16:50:54 +01:00
|
|
|
|
2019-01-06 16:52:18 +01:00
|
|
|
argument = nsh_argexpand(vtbl, pbegin, &allocation, isenvvar);
|
2014-01-11 16:50:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If any memory was allocated for this argument, make sure that it is
|
2019-01-06 16:52:18 +01:00
|
|
|
* added to the list of memory to be freed at the end of command
|
2014-01-11 16:50:54 +01:00
|
|
|
* processing.
|
|
|
|
*/
|
|
|
|
|
2014-01-11 20:18:12 +01:00
|
|
|
NSH_MEMLIST_ADD(memlist, allocation);
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2014-01-11 16:50:54 +01:00
|
|
|
/* Return the parsed argument. */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2014-01-11 16:50:54 +01:00
|
|
|
return argument;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-01-17 22:56:32 +01:00
|
|
|
* Name: nsh_loop_enabled
|
2011-03-18 20:46:25 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-01-18 16:39:16 +01:00
|
|
|
#if !defined(CONFIG_NSH_DISABLESCRIPT) && !defined(CONFIG_NSH_DISABLE_LOOPS)
|
2014-01-17 22:56:32 +01:00
|
|
|
static bool nsh_loop_enabled(FAR struct nsh_vtbl_s *vtbl)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2014-01-17 22:56:32 +01:00
|
|
|
FAR struct nsh_parser_s *np = &vtbl->np;
|
|
|
|
|
|
|
|
/* If we are looping and the disable bit is set, then we are skipping
|
|
|
|
* all data until we next get to the 'done' token at the end of the
|
|
|
|
* loop.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (np->np_lpstate[np->np_lpndx].lp_state == NSH_LOOP_DO)
|
|
|
|
{
|
|
|
|
/* We have parsed 'do', looking for 'done' */
|
|
|
|
|
|
|
|
return (bool)np->np_lpstate[np->np_lpndx].lp_enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2014-01-18 16:39:16 +01:00
|
|
|
#else
|
|
|
|
# define nsh_loop_enabled(vtbl) true
|
2014-01-17 22:56:32 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_itef_enabled
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-01-18 16:39:16 +01:00
|
|
|
#if !defined(CONFIG_NSH_DISABLESCRIPT) && !defined(CONFIG_NSH_DISABLE_ITEF)
|
2014-01-17 22:56:32 +01:00
|
|
|
static bool nsh_itef_enabled(FAR struct nsh_vtbl_s *vtbl)
|
|
|
|
{
|
|
|
|
FAR struct nsh_parser_s *np = &vtbl->np;
|
2014-01-17 20:24:44 +01:00
|
|
|
bool ret = !np->np_iestate[np->np_iendx].ie_disabled;
|
2011-03-18 20:46:25 +01:00
|
|
|
if (ret)
|
|
|
|
{
|
2014-01-17 20:24:44 +01:00
|
|
|
switch (np->np_iestate[np->np_iendx].ie_state)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2014-01-17 22:56:32 +01:00
|
|
|
case NSH_ITEF_NORMAL:
|
|
|
|
case NSH_ITEF_IF:
|
2011-03-18 20:46:25 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
2014-01-17 22:56:32 +01:00
|
|
|
case NSH_ITEF_THEN:
|
2014-01-17 20:24:44 +01:00
|
|
|
ret = !np->np_iestate[np->np_iendx].ie_ifcond;
|
2011-03-18 20:46:25 +01:00
|
|
|
break;
|
|
|
|
|
2014-01-17 22:56:32 +01:00
|
|
|
case NSH_ITEF_ELSE:
|
2014-01-17 20:24:44 +01:00
|
|
|
ret = np->np_iestate[np->np_iendx].ie_ifcond;
|
2011-03-18 20:46:25 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-01-10 22:23:26 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2014-01-18 16:39:16 +01:00
|
|
|
#else
|
|
|
|
# define nsh_itef_enabled(vtbl) true
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-01-17 22:56:32 +01:00
|
|
|
* Name: nsh_cmdenabled
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_DISABLESCRIPT
|
|
|
|
static bool nsh_cmdenabled(FAR struct nsh_vtbl_s *vtbl)
|
|
|
|
{
|
|
|
|
/* Return true if command processing is enabled on this pass through the
|
|
|
|
* loop AND if command processing is enabled in this part of the if-then-
|
|
|
|
* else-fi sequence.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return (nsh_loop_enabled(vtbl) && nsh_itef_enabled(vtbl));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_loop
|
2011-03-18 20:46:25 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-01-18 16:39:16 +01:00
|
|
|
#if !defined(CONFIG_NSH_DISABLESCRIPT) && !defined(CONFIG_NSH_DISABLE_LOOPS)
|
2014-01-17 22:56:32 +01:00
|
|
|
static int nsh_loop(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
|
|
|
|
FAR char **saveptr, FAR NSH_MEMLIST_TYPE *memlist)
|
|
|
|
{
|
|
|
|
FAR struct nsh_parser_s *np = &vtbl->np;
|
|
|
|
FAR char *cmd = *ppcmd;
|
|
|
|
long offset;
|
|
|
|
bool whilematch;
|
|
|
|
bool untilmatch;
|
|
|
|
bool enable;
|
|
|
|
int ret;
|
|
|
|
|
2018-08-11 17:30:53 +02:00
|
|
|
if (cmd != NULL)
|
2014-01-17 22:56:32 +01:00
|
|
|
{
|
|
|
|
/* Check if the command is preceded by "while" or "until" */
|
|
|
|
|
2017-09-25 15:34:34 +02:00
|
|
|
whilematch = strcmp(cmd, "while") == 0;
|
|
|
|
untilmatch = strcmp(cmd, "until") == 0;
|
2014-01-17 22:56:32 +01:00
|
|
|
|
2017-09-25 15:34:34 +02:00
|
|
|
if (whilematch || untilmatch)
|
2014-01-17 22:56:32 +01:00
|
|
|
{
|
|
|
|
uint8_t state;
|
|
|
|
|
|
|
|
/* Get the cmd following the "while" or "until" */
|
|
|
|
|
2019-01-06 16:52:18 +01:00
|
|
|
*ppcmd = nsh_argument(vtbl, saveptr, memlist, 0);
|
2018-08-11 17:30:53 +02:00
|
|
|
if (*ppcmd == NULL || **ppcmd == '\0')
|
2014-01-17 22:56:32 +01:00
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, cmd);
|
2014-01-17 22:56:32 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that "while" or "until" is valid in this context */
|
|
|
|
|
2014-01-18 16:39:16 +01:00
|
|
|
if (
|
|
|
|
#ifndef CONFIG_NSH_DISABLE_ITEF
|
|
|
|
np->np_iestate[np->np_iendx].ie_state == NSH_ITEF_IF ||
|
|
|
|
#endif
|
2014-01-17 22:56:32 +01:00
|
|
|
np->np_lpstate[np->np_lpndx].lp_state == NSH_LOOP_WHILE ||
|
|
|
|
np->np_lpstate[np->np_lpndx].lp_state == NSH_LOOP_UNTIL ||
|
|
|
|
np->np_stream == NULL || np->np_foffs < 0)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcontext, cmd);
|
2014-01-17 22:56:32 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we have exceeded the maximum depth of nesting */
|
|
|
|
|
2020-05-18 00:07:36 +02:00
|
|
|
if (np->np_lpndx >= CONFIG_NSH_NESTDEPTH - 1)
|
2014-01-17 22:56:32 +01:00
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtdeepnesting, cmd);
|
2014-01-17 22:56:32 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* "Push" the old state and set the new state */
|
|
|
|
|
2018-06-06 14:50:41 +02:00
|
|
|
state = whilematch ? NSH_LOOP_WHILE : NSH_LOOP_UNTIL;
|
2014-01-17 22:56:32 +01:00
|
|
|
enable = nsh_cmdenabled(vtbl);
|
|
|
|
#ifdef NSH_DISABLE_SEMICOLON
|
|
|
|
offset = np->np_foffs;
|
|
|
|
#else
|
|
|
|
offset = np->np_foffs + np->np_loffs;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef NSH_DISABLE_SEMICOLON
|
|
|
|
np->np_jump = false;
|
|
|
|
#endif
|
|
|
|
np->np_lpndx++;
|
2018-08-12 14:15:29 +02:00
|
|
|
np->np_lpstate[np->np_lpndx].lp_state = state;
|
|
|
|
np->np_lpstate[np->np_lpndx].lp_enable = enable;
|
2014-01-18 16:39:16 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_ITEF
|
2018-08-12 14:15:29 +02:00
|
|
|
np->np_lpstate[np->np_lpndx].lp_iendx = np->np_iendx;
|
2014-01-18 16:39:16 +01:00
|
|
|
#endif
|
2018-08-12 14:15:29 +02:00
|
|
|
np->np_lpstate[np->np_lpndx].lp_topoffs = offset;
|
2014-01-17 22:56:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the token is "do" */
|
|
|
|
|
|
|
|
else if (strcmp(cmd, "do") == 0)
|
|
|
|
{
|
2014-01-18 16:39:16 +01:00
|
|
|
/* Get the cmd following the "do" -- there may or may not be one */
|
2014-01-17 22:56:32 +01:00
|
|
|
|
2019-01-06 17:00:19 +01:00
|
|
|
*ppcmd = nsh_argument(vtbl, saveptr, memlist, NULL);
|
2014-01-17 22:56:32 +01:00
|
|
|
|
|
|
|
/* Verify that "do" is valid in this context */
|
|
|
|
|
|
|
|
if (np->np_lpstate[np->np_lpndx].lp_state != NSH_LOOP_WHILE &&
|
|
|
|
np->np_lpstate[np->np_lpndx].lp_state != NSH_LOOP_UNTIL)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcontext, "do");
|
2014-01-17 22:56:32 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
np->np_lpstate[np->np_lpndx].lp_state = NSH_LOOP_DO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the token is "done" */
|
|
|
|
|
|
|
|
else if (strcmp(cmd, "done") == 0)
|
|
|
|
{
|
|
|
|
/* Get the cmd following the "done" -- there should be one */
|
|
|
|
|
2019-01-06 17:00:19 +01:00
|
|
|
*ppcmd = nsh_argument(vtbl, saveptr, memlist, NULL);
|
2014-01-17 22:56:32 +01:00
|
|
|
if (*ppcmd)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, "done");
|
2014-01-17 22:56:32 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that "done" is valid in this context */
|
|
|
|
|
|
|
|
if (np->np_lpstate[np->np_lpndx].lp_state != NSH_LOOP_DO)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcontext, "done");
|
2014-01-17 22:56:32 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (np->np_lpndx < 1) /* Shouldn't happen */
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtinternalerror, "done");
|
2014-01-17 22:56:32 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now what do we do? We either: Do go back to the top of the
|
|
|
|
* loop (if lp_enable == true) or continue past the end of the
|
|
|
|
* loop (if lp_enable == false)
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (np->np_lpstate[np->np_lpndx].lp_enable)
|
|
|
|
{
|
2020-05-18 00:07:36 +02:00
|
|
|
/* Set the new file position to the top of the loop offset */
|
2014-01-17 22:56:32 +01:00
|
|
|
|
2020-05-18 00:07:36 +02:00
|
|
|
ret = fseek(np->np_stream,
|
|
|
|
np->np_lpstate[np->np_lpndx].lp_topoffs,
|
|
|
|
SEEK_SET);
|
|
|
|
if (ret < 0)
|
2014-01-17 22:56:32 +01:00
|
|
|
{
|
2020-05-18 00:07:36 +02:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, "done", "fseek",
|
|
|
|
NSH_ERRNO);
|
2014-01-17 22:56:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NSH_DISABLE_SEMICOLON
|
2020-05-18 00:07:36 +02:00
|
|
|
/* Signal nsh_parse that we need to stop processing the
|
|
|
|
* current line and jump back to the top of the loop.
|
|
|
|
*/
|
2014-01-17 22:56:32 +01:00
|
|
|
|
2020-05-18 00:07:36 +02:00
|
|
|
np->np_jump = true;
|
2014-01-17 22:56:32 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
np->np_lpstate[np->np_lpndx].lp_enable = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* "Pop" the previous state. We do this no matter what we
|
|
|
|
* decided to do
|
|
|
|
*/
|
|
|
|
|
|
|
|
np->np_lpstate[np->np_lpndx].lp_state = NSH_LOOP_NORMAL;
|
|
|
|
np->np_lpndx--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we just parsed "while" or "until", then nothing is acceptable
|
|
|
|
* other than "do"
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (np->np_lpstate[np->np_lpndx].lp_state == NSH_LOOP_WHILE ||
|
|
|
|
np->np_lpstate[np->np_lpndx].lp_state == NSH_LOOP_UNTIL)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcontext, cmd);
|
2014-01-17 22:56:32 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout:
|
|
|
|
#ifndef NSH_DISABLE_SEMICOLON
|
2018-08-12 14:15:29 +02:00
|
|
|
np->np_jump = false;
|
2014-01-17 22:56:32 +01:00
|
|
|
#endif
|
2018-08-12 14:15:29 +02:00
|
|
|
np->np_lpndx = 0;
|
|
|
|
np->np_lpstate[0].lp_state = NSH_LOOP_NORMAL;
|
|
|
|
np->np_lpstate[0].lp_enable = true;
|
|
|
|
np->np_lpstate[0].lp_topoffs = 0;
|
2014-01-17 22:56:32 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_itef
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-01-18 16:39:16 +01:00
|
|
|
#if !defined(CONFIG_NSH_DISABLESCRIPT) && !defined(CONFIG_NSH_DISABLE_ITEF)
|
2014-01-17 22:56:32 +01:00
|
|
|
static int nsh_itef(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
|
|
|
|
FAR char **saveptr, FAR NSH_MEMLIST_TYPE *memlist)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2014-01-17 20:24:44 +01:00
|
|
|
FAR struct nsh_parser_s *np = &vtbl->np;
|
2011-03-18 20:46:25 +01:00
|
|
|
FAR char *cmd = *ppcmd;
|
|
|
|
bool disabled;
|
2018-08-11 15:53:22 +02:00
|
|
|
bool inverted = false;
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2018-08-11 17:30:53 +02:00
|
|
|
if (cmd != NULL)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2014-01-17 20:24:44 +01:00
|
|
|
/* Check if the command is preceded by "if" */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
if (strcmp(cmd, "if") == 0)
|
|
|
|
{
|
|
|
|
/* Get the cmd following the if */
|
|
|
|
|
2019-01-06 17:00:19 +01:00
|
|
|
*ppcmd = nsh_argument(vtbl, saveptr, memlist, NULL);
|
2018-08-11 17:30:53 +02:00
|
|
|
if (*ppcmd == NULL || **ppcmd == '\0')
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, "if");
|
2011-03-18 20:46:25 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2018-08-11 15:14:41 +02:00
|
|
|
/* Check for inverted logic */
|
2018-08-11 15:53:22 +02:00
|
|
|
|
2018-08-11 15:14:41 +02:00
|
|
|
if (strcmp(*ppcmd, "!") == 0)
|
|
|
|
{
|
2018-08-11 15:53:22 +02:00
|
|
|
inverted = true;
|
2018-08-11 15:14:41 +02:00
|
|
|
|
|
|
|
/* Get the next cmd */
|
2018-08-11 15:53:22 +02:00
|
|
|
|
2019-01-06 16:52:18 +01:00
|
|
|
*ppcmd = nsh_argument(vtbl, saveptr, memlist, 0);
|
2018-08-11 17:30:53 +02:00
|
|
|
if (*ppcmd == NULL || **ppcmd == '\0')
|
2018-08-11 15:14:41 +02:00
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, "if");
|
2018-08-11 15:14:41 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/* Verify that "if" is valid in this context */
|
|
|
|
|
2014-01-17 22:56:32 +01:00
|
|
|
if (np->np_iestate[np->np_iendx].ie_state == NSH_ITEF_IF)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcontext, "if");
|
2011-03-18 20:46:25 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we have exceeded the maximum depth of nesting */
|
|
|
|
|
2020-05-18 00:07:36 +02:00
|
|
|
if (np->np_iendx >= CONFIG_NSH_NESTDEPTH - 1)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtdeepnesting, "if");
|
2011-03-18 20:46:25 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* "Push" the old state and set the new state */
|
|
|
|
|
2014-01-17 20:24:44 +01:00
|
|
|
disabled = !nsh_cmdenabled(vtbl);
|
|
|
|
np->np_iendx++;
|
2014-01-17 22:56:32 +01:00
|
|
|
np->np_iestate[np->np_iendx].ie_state = NSH_ITEF_IF;
|
2014-01-17 20:24:44 +01:00
|
|
|
np->np_iestate[np->np_iendx].ie_disabled = disabled;
|
|
|
|
np->np_iestate[np->np_iendx].ie_ifcond = false;
|
2018-08-11 15:53:22 +02:00
|
|
|
np->np_iestate[np->np_iendx].ie_inverted = inverted;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
2014-01-17 20:24:44 +01:00
|
|
|
|
2014-01-17 22:56:32 +01:00
|
|
|
/* Check if the token is "then" */
|
2014-01-17 20:24:44 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
else if (strcmp(cmd, "then") == 0)
|
|
|
|
{
|
2020-05-18 00:07:36 +02:00
|
|
|
/* Get the cmd following the "then" -- there may or may not be
|
|
|
|
* one.
|
|
|
|
*/
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2019-01-06 17:00:19 +01:00
|
|
|
*ppcmd = nsh_argument(vtbl, saveptr, memlist, NULL);
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/* Verify that "then" is valid in this context */
|
|
|
|
|
2014-01-17 22:56:32 +01:00
|
|
|
if (np->np_iestate[np->np_iendx].ie_state != NSH_ITEF_IF)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcontext, "then");
|
2011-03-18 20:46:25 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
2014-01-17 20:24:44 +01:00
|
|
|
|
2014-01-17 22:56:32 +01:00
|
|
|
np->np_iestate[np->np_iendx].ie_state = NSH_ITEF_THEN;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
2014-01-17 20:24:44 +01:00
|
|
|
|
2014-01-17 22:56:32 +01:00
|
|
|
/* Check if the token is "else" */
|
2014-01-17 20:24:44 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
else if (strcmp(cmd, "else") == 0)
|
|
|
|
{
|
2020-05-18 00:07:36 +02:00
|
|
|
/* Get the cmd following the "else" -- there may or may not be
|
|
|
|
* one.
|
|
|
|
*/
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2019-01-06 17:00:19 +01:00
|
|
|
*ppcmd = nsh_argument(vtbl, saveptr, memlist, NULL);
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2014-01-17 20:24:44 +01:00
|
|
|
/* Verify that "else" is valid in this context */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2014-01-17 22:56:32 +01:00
|
|
|
if (np->np_iestate[np->np_iendx].ie_state != NSH_ITEF_THEN)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcontext, "else");
|
2011-03-18 20:46:25 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
2014-01-17 20:24:44 +01:00
|
|
|
|
2014-01-17 22:56:32 +01:00
|
|
|
np->np_iestate[np->np_iendx].ie_state = NSH_ITEF_ELSE;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
2014-01-17 20:24:44 +01:00
|
|
|
|
2014-01-17 22:56:32 +01:00
|
|
|
/* Check if the token is "fi" */
|
2014-01-17 20:24:44 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
else if (strcmp(cmd, "fi") == 0)
|
|
|
|
{
|
|
|
|
/* Get the cmd following the fi -- there should be one */
|
|
|
|
|
2019-01-06 17:00:19 +01:00
|
|
|
*ppcmd = nsh_argument(vtbl, saveptr, memlist, NULL);
|
2011-03-18 20:46:25 +01:00
|
|
|
if (*ppcmd)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, "fi");
|
2011-03-18 20:46:25 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that "fi" is valid in this context */
|
|
|
|
|
2014-01-17 22:56:32 +01:00
|
|
|
if (np->np_iestate[np->np_iendx].ie_state != NSH_ITEF_THEN &&
|
|
|
|
np->np_iestate[np->np_iendx].ie_state != NSH_ITEF_ELSE)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcontext, "fi");
|
2011-03-18 20:46:25 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2014-01-17 20:24:44 +01:00
|
|
|
if (np->np_iendx < 1) /* Shouldn't happen */
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtinternalerror, "if");
|
2011-03-18 20:46:25 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* "Pop" the previous state */
|
|
|
|
|
2014-01-17 20:24:44 +01:00
|
|
|
np->np_iendx--;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
2014-01-17 20:24:44 +01:00
|
|
|
|
2020-05-18 00:07:36 +02:00
|
|
|
/* If we just parsed "if", then nothing is acceptable other than
|
|
|
|
* "then".
|
|
|
|
*/
|
2014-01-17 20:24:44 +01:00
|
|
|
|
2014-01-17 22:56:32 +01:00
|
|
|
else if (np->np_iestate[np->np_iendx].ie_state == NSH_ITEF_IF)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcontext, cmd);
|
2011-03-18 20:46:25 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
2014-01-17 20:24:44 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout:
|
2014-01-17 20:24:44 +01:00
|
|
|
np->np_iendx = 0;
|
2014-01-17 22:56:32 +01:00
|
|
|
np->np_iestate[0].ie_state = NSH_ITEF_NORMAL;
|
2014-01-17 20:24:44 +01:00
|
|
|
np->np_iestate[0].ie_disabled = false;
|
|
|
|
np->np_iestate[0].ie_ifcond = false;
|
2018-08-11 15:14:41 +02:00
|
|
|
np->np_iestate[0].ie_inverted = false;
|
2011-03-18 20:46:25 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_nice
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLEBG
|
2014-01-11 01:42:24 +01:00
|
|
|
static int nsh_nice(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
|
2014-01-11 16:50:54 +01:00
|
|
|
FAR char **saveptr, FAR NSH_MEMLIST_TYPE *memlist)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
|
|
|
FAR char *cmd = *ppcmd;
|
|
|
|
|
|
|
|
vtbl->np.np_nice = 0;
|
|
|
|
if (cmd)
|
|
|
|
{
|
|
|
|
/* Check if the command is preceded by "nice" */
|
|
|
|
|
|
|
|
if (strcmp(cmd, "nice") == 0)
|
|
|
|
{
|
|
|
|
/* Nicenesses range from -20 (most favorable scheduling) to 19
|
|
|
|
* (least favorable). Default is 10.
|
|
|
|
*/
|
|
|
|
|
|
|
|
vtbl->np.np_nice = 10;
|
|
|
|
|
|
|
|
/* Get the cmd (or -d option of nice command) */
|
|
|
|
|
2019-01-06 17:00:19 +01:00
|
|
|
cmd = nsh_argument(vtbl, saveptr, memlist, NULL);
|
2011-03-18 20:46:25 +01:00
|
|
|
if (cmd && strcmp(cmd, "-d") == 0)
|
|
|
|
{
|
2019-01-06 17:00:19 +01:00
|
|
|
FAR char *val = nsh_argument(vtbl, saveptr, memlist, NULL);
|
2011-03-18 20:46:25 +01:00
|
|
|
if (val)
|
|
|
|
{
|
|
|
|
char *endptr;
|
|
|
|
vtbl->np.np_nice = (int)strtol(val, &endptr, 0);
|
|
|
|
if (vtbl->np.np_nice > 19 || vtbl->np.np_nice < -20 ||
|
|
|
|
endptr == val || *endptr != '\0')
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, "nice");
|
2011-03-18 20:46:25 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
2020-05-18 00:07:36 +02:00
|
|
|
|
2019-01-06 17:00:19 +01:00
|
|
|
cmd = nsh_argument(vtbl, saveptr, memlist, NULL);
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
/* Return the real command name */
|
2013-02-03 00:56:54 +01:00
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
*ppcmd = cmd;
|
2013-02-03 14:48:20 +01:00
|
|
|
}
|
2014-01-10 23:15:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
2013-09-30 19:34:04 +02:00
|
|
|
#endif
|
2013-02-03 00:56:54 +01:00
|
|
|
|
2014-01-11 01:42:24 +01:00
|
|
|
/****************************************************************************
|
2014-01-12 21:16:05 +01:00
|
|
|
* Name: nsh_parse_cmdparm
|
2014-01-11 01:42:24 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function parses and executes a simple NSH command. Output is
|
2014-01-11 16:50:54 +01:00
|
|
|
* always redirected. This function supports command parameters like
|
2014-01-11 01:42:24 +01:00
|
|
|
*
|
|
|
|
* set FOO `hello`
|
|
|
|
*
|
|
|
|
* which would set the environment variable FOO to the output from
|
|
|
|
* the hello program
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-01-11 16:50:54 +01:00
|
|
|
#ifdef CONFIG_NSH_CMDPARMS
|
2014-01-12 21:16:05 +01:00
|
|
|
static int nsh_parse_cmdparm(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline,
|
|
|
|
FAR const char *redirfile)
|
2014-01-11 01:42:24 +01:00
|
|
|
{
|
2014-01-11 16:50:54 +01:00
|
|
|
NSH_MEMLIST_TYPE memlist;
|
2014-01-11 01:42:24 +01:00
|
|
|
FAR char *argv[MAX_ARGV_ENTRIES];
|
|
|
|
FAR char *saveptr;
|
|
|
|
FAR char *cmd;
|
2017-02-27 14:23:26 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLEBG
|
2014-01-11 01:42:24 +01:00
|
|
|
bool bgsave;
|
2017-02-27 14:23:26 +01:00
|
|
|
#endif
|
2014-01-11 01:42:24 +01:00
|
|
|
bool redirsave;
|
|
|
|
int argc;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Initialize parser state */
|
|
|
|
|
|
|
|
memset(argv, 0, MAX_ARGV_ENTRIES*sizeof(FAR char *));
|
2014-01-11 16:50:54 +01:00
|
|
|
NSH_MEMLIST_INIT(memlist);
|
2014-01-11 01:42:24 +01:00
|
|
|
|
|
|
|
/* If any options like nice, redirection, or backgrounding are attempted,
|
|
|
|
* these will not be recognized and will just be passed through as
|
|
|
|
* normal, invalid commands or parameters.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_DISABLEBG
|
2014-01-11 16:50:54 +01:00
|
|
|
/* The command is never backgrounded . Remember the current backgrounding
|
2014-01-11 01:42:24 +01:00
|
|
|
* state
|
|
|
|
*/
|
|
|
|
|
|
|
|
bgsave = vtbl->np.np_bg;
|
|
|
|
vtbl->np.np_bg = false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Output is always redirected. Remember the current redirection state */
|
|
|
|
|
|
|
|
redirsave = vtbl->np.np_redirect;
|
|
|
|
vtbl->np.np_redirect = true;
|
|
|
|
|
|
|
|
/* Parse out the command at the beginning of the line */
|
|
|
|
|
|
|
|
saveptr = cmdline;
|
2019-01-06 17:00:19 +01:00
|
|
|
cmd = nsh_argument(vtbl, &saveptr, &memlist, NULL);
|
2014-01-11 01:42:24 +01:00
|
|
|
|
|
|
|
/* Check if any command was provided -OR- if command processing is
|
|
|
|
* currently disabled.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_DISABLESCRIPT
|
|
|
|
if (!cmd || !nsh_cmdenabled(vtbl))
|
|
|
|
#else
|
|
|
|
if (!cmd)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* An empty line is not an error and an unprocessed command cannot
|
2014-01-11 16:50:54 +01:00
|
|
|
* generate an error, but neither should it change the last command
|
|
|
|
* status.
|
2014-01-11 01:42:24 +01:00
|
|
|
*/
|
|
|
|
|
2021-12-31 15:54:57 +01:00
|
|
|
ret = 0;
|
|
|
|
goto exit;
|
2014-01-11 01:42:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse all of the arguments following the command name. The form
|
|
|
|
* of argv is:
|
|
|
|
*
|
|
|
|
* argv[0]: The command name.
|
2020-05-18 00:07:36 +02:00
|
|
|
* argv[1]: The beginning of argument (up to
|
|
|
|
* CONFIG_NSH_MAXARGUMENTS)
|
2014-01-11 01:42:24 +01:00
|
|
|
* argv[argc]: NULL terminating pointer
|
|
|
|
*
|
|
|
|
* Maximum size is CONFIG_NSH_MAXARGUMENTS+1
|
|
|
|
*/
|
|
|
|
|
|
|
|
argv[0] = cmd;
|
2020-05-18 00:07:36 +02:00
|
|
|
for (argc = 1; argc < MAX_ARGV_ENTRIES - 1; argc++)
|
2014-01-11 01:42:24 +01:00
|
|
|
{
|
2019-01-06 17:00:19 +01:00
|
|
|
argv[argc] = nsh_argument(vtbl, &saveptr, &memlist, NULL);
|
2014-01-11 01:42:24 +01:00
|
|
|
if (!argv[argc])
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argv[argc] = NULL;
|
|
|
|
|
|
|
|
/* Check if the maximum number of arguments was exceeded */
|
|
|
|
|
|
|
|
if (argc > CONFIG_NSH_MAXARGUMENTS)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmttoomanyargs, cmd);
|
2014-01-11 01:42:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Then execute the command */
|
|
|
|
|
2020-05-18 00:07:36 +02:00
|
|
|
ret = nsh_execute(vtbl, argc, argv, redirfile,
|
|
|
|
O_WRONLY | O_CREAT | O_TRUNC);
|
2014-01-11 01:42:24 +01:00
|
|
|
|
|
|
|
/* Restore the backgrounding and redirection state */
|
|
|
|
|
2021-12-31 15:54:57 +01:00
|
|
|
exit:
|
2014-01-11 01:42:24 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLEBG
|
|
|
|
vtbl->np.np_bg = bgsave;
|
|
|
|
#endif
|
|
|
|
vtbl->np.np_redirect = redirsave;
|
2014-01-11 16:50:54 +01:00
|
|
|
|
|
|
|
NSH_MEMLIST_FREE(&memlist);
|
2014-01-11 01:42:24 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_parse_command
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function parses and executes one NSH command from the command line.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2013-02-03 00:56:54 +01:00
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline)
|
|
|
|
{
|
2014-01-11 16:50:54 +01:00
|
|
|
NSH_MEMLIST_TYPE memlist;
|
2014-01-10 23:15:14 +01:00
|
|
|
FAR char *argv[MAX_ARGV_ENTRIES];
|
|
|
|
FAR char *saveptr;
|
|
|
|
FAR char *cmd;
|
|
|
|
FAR char *redirfile = NULL;
|
|
|
|
int oflags = 0;
|
|
|
|
int argc;
|
|
|
|
int ret;
|
2020-08-13 16:17:42 +02:00
|
|
|
#ifdef CONFIG_FILE_STREAM
|
2019-06-03 17:29:44 +02:00
|
|
|
bool redirect_save = false;
|
2019-01-12 17:56:48 +01:00
|
|
|
#endif
|
2013-01-15 22:01:37 +01:00
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
/* Initialize parser state */
|
2013-01-15 22:01:37 +01:00
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
memset(argv, 0, MAX_ARGV_ENTRIES*sizeof(FAR char *));
|
2014-01-11 16:50:54 +01:00
|
|
|
NSH_MEMLIST_INIT(memlist);
|
2014-01-11 01:42:24 +01:00
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLEBG
|
|
|
|
vtbl->np.np_bg = false;
|
2013-01-15 22:01:37 +01:00
|
|
|
#endif
|
2014-01-11 01:42:24 +01:00
|
|
|
|
2020-08-13 16:17:42 +02:00
|
|
|
#ifdef CONFIG_FILE_STREAM
|
2014-01-10 23:15:14 +01:00
|
|
|
vtbl->np.np_redirect = false;
|
|
|
|
#endif
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
/* Parse out the command at the beginning of the line */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
saveptr = cmdline;
|
2019-01-06 17:00:19 +01:00
|
|
|
cmd = nsh_argument(vtbl, &saveptr, &memlist, NULL);
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLESCRIPT
|
2014-01-18 16:39:16 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_LOOPS
|
2014-01-17 22:56:32 +01:00
|
|
|
/* Handle while-do-done and until-do-done loops */
|
|
|
|
|
|
|
|
if (nsh_loop(vtbl, &cmd, &saveptr, &memlist) != 0)
|
|
|
|
{
|
|
|
|
NSH_MEMLIST_FREE(&memlist);
|
|
|
|
return nsh_saveresult(vtbl, true);
|
|
|
|
}
|
2014-01-18 16:39:16 +01:00
|
|
|
#endif
|
2014-01-17 22:56:32 +01:00
|
|
|
|
2014-01-18 16:39:16 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_ITEF
|
2014-01-17 22:56:32 +01:00
|
|
|
/* Handle if-then-else-fi */
|
|
|
|
|
|
|
|
if (nsh_itef(vtbl, &cmd, &saveptr, &memlist) != 0)
|
2014-01-10 23:15:14 +01:00
|
|
|
{
|
2014-01-11 16:50:54 +01:00
|
|
|
NSH_MEMLIST_FREE(&memlist);
|
2014-01-10 23:15:14 +01:00
|
|
|
return nsh_saveresult(vtbl, true);
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
2014-01-18 16:39:16 +01:00
|
|
|
|
|
|
|
#endif
|
2013-09-30 19:34:04 +02:00
|
|
|
#endif
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
/* Handle nice */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLEBG
|
2014-01-11 16:50:54 +01:00
|
|
|
if (nsh_nice(vtbl, &cmd, &saveptr, &memlist) != 0)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2014-01-11 16:50:54 +01:00
|
|
|
NSH_MEMLIST_FREE(&memlist);
|
2014-01-10 23:15:14 +01:00
|
|
|
return nsh_saveresult(vtbl, true);
|
|
|
|
}
|
2013-09-30 19:34:04 +02:00
|
|
|
#endif
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
/* Check if any command was provided -OR- if command processing is
|
|
|
|
* currently disabled.
|
|
|
|
*/
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLESCRIPT
|
|
|
|
if (!cmd || !nsh_cmdenabled(vtbl))
|
|
|
|
#else
|
|
|
|
if (!cmd)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* An empty line is not an error and an unprocessed command cannot
|
2014-01-11 16:50:54 +01:00
|
|
|
* generate an error, but neither should it change the last command
|
|
|
|
* status.
|
2014-01-10 23:15:14 +01:00
|
|
|
*/
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2014-01-11 16:50:54 +01:00
|
|
|
NSH_MEMLIST_FREE(&memlist);
|
2014-01-10 23:15:14 +01:00
|
|
|
return OK;
|
|
|
|
}
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
/* Parse all of the arguments following the command name. The form
|
|
|
|
* of argv is:
|
|
|
|
*
|
|
|
|
* argv[0]: The command name.
|
2020-05-18 00:07:36 +02:00
|
|
|
* argv[1]: The beginning of argument (up to
|
|
|
|
* CONFIG_NSH_MAXARGUMENTS)
|
2014-01-10 23:15:14 +01:00
|
|
|
* argv[argc-3]: Possibly '>' or '>>'
|
|
|
|
* argv[argc-2]: Possibly <file>
|
|
|
|
* argv[argc-1]: Possibly '&'
|
|
|
|
* argv[argc]: NULL terminating pointer
|
|
|
|
*
|
|
|
|
* Maximum size is CONFIG_NSH_MAXARGUMENTS+5
|
|
|
|
*/
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
argv[0] = cmd;
|
2020-05-18 00:07:36 +02:00
|
|
|
for (argc = 1; argc < MAX_ARGV_ENTRIES - 1; argc++)
|
2014-01-10 23:15:14 +01:00
|
|
|
{
|
2019-01-06 17:00:19 +01:00
|
|
|
int isenvvar = 0; /* flag for if an environment variable gets expanded */
|
2019-01-06 16:52:18 +01:00
|
|
|
|
|
|
|
argv[argc] = nsh_argument(vtbl, &saveptr, &memlist, &isenvvar);
|
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
if (!argv[argc])
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2014-01-10 23:15:14 +01:00
|
|
|
break;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
2019-01-06 16:52:18 +01:00
|
|
|
|
2019-01-06 17:00:19 +01:00
|
|
|
if (isenvvar != 0)
|
2019-01-06 16:52:18 +01:00
|
|
|
{
|
2020-05-18 00:07:36 +02:00
|
|
|
while (argc < MAX_ARGV_ENTRIES - 1)
|
2019-01-06 16:52:18 +01:00
|
|
|
{
|
|
|
|
FAR char *pbegin = argv[argc];
|
|
|
|
|
|
|
|
/* Find the end of the current token */
|
|
|
|
|
2019-01-06 17:00:19 +01:00
|
|
|
for (; *pbegin && !strchr(g_token_separator, *pbegin);
|
|
|
|
pbegin++)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If end of string, we've processed the last token and we're
|
|
|
|
* done.
|
|
|
|
*/
|
|
|
|
|
2019-01-06 16:52:18 +01:00
|
|
|
if ('\0' == *pbegin)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Terminate the token to complete the argv variable */
|
2019-01-06 17:00:19 +01:00
|
|
|
|
2019-01-06 16:52:18 +01:00
|
|
|
*pbegin = '\0';
|
|
|
|
|
|
|
|
/* We've inserted an extra parameter, so bump the count */
|
2019-01-06 17:00:19 +01:00
|
|
|
|
2019-01-06 16:52:18 +01:00
|
|
|
argc++;
|
|
|
|
|
|
|
|
/* Move to the next character in the string of tokens */
|
2019-01-06 17:00:19 +01:00
|
|
|
|
2019-01-06 16:52:18 +01:00
|
|
|
pbegin++;
|
|
|
|
|
|
|
|
/* Throw away any extra separator chars between tokens */
|
2019-01-06 17:00:19 +01:00
|
|
|
|
|
|
|
for (; *pbegin && strchr(g_token_separator, *pbegin) != NULL;
|
|
|
|
pbegin++)
|
|
|
|
{
|
|
|
|
}
|
2019-01-06 16:52:18 +01:00
|
|
|
|
|
|
|
/* Prepare to loop again on the next argument token */
|
2019-01-06 17:00:19 +01:00
|
|
|
|
2019-01-06 16:52:18 +01:00
|
|
|
argv[argc] = pbegin;
|
|
|
|
}
|
|
|
|
}
|
2014-01-10 23:15:14 +01:00
|
|
|
}
|
2012-07-28 17:18:26 +02:00
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
/* Check if the command should run in background */
|
2013-02-03 14:48:20 +01:00
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLEBG
|
2020-05-18 00:07:36 +02:00
|
|
|
if (argc > 1 && strcmp(argv[argc - 1], "&") == 0)
|
2014-01-10 23:15:14 +01:00
|
|
|
{
|
|
|
|
vtbl->np.np_bg = true;
|
|
|
|
argc--;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
#endif
|
2014-01-10 23:15:14 +01:00
|
|
|
|
2020-08-13 16:17:42 +02:00
|
|
|
#ifdef CONFIG_FILE_STREAM
|
2014-01-10 23:15:14 +01:00
|
|
|
/* Check if the output was re-directed using > or >> */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
if (argc > 2)
|
|
|
|
{
|
|
|
|
/* Check for redirection to a new file */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2020-05-18 00:07:36 +02:00
|
|
|
if (strcmp(argv[argc - 2], g_redirect1) == 0)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2019-01-12 17:56:48 +01:00
|
|
|
redirect_save = vtbl->np.np_redirect;
|
2014-01-10 23:15:14 +01:00
|
|
|
vtbl->np.np_redirect = true;
|
2020-05-18 00:07:36 +02:00
|
|
|
oflags = O_WRONLY | O_CREAT | O_TRUNC;
|
|
|
|
redirfile = nsh_getfullpath(vtbl, argv[argc - 1]);
|
2014-01-10 23:15:14 +01:00
|
|
|
argc -= 2;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
/* Check for redirection by appending to an existing file */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2020-05-18 00:07:36 +02:00
|
|
|
else if (strcmp(argv[argc - 2], g_redirect2) == 0)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2019-01-12 17:56:48 +01:00
|
|
|
redirect_save = vtbl->np.np_redirect;
|
2014-01-10 23:15:14 +01:00
|
|
|
vtbl->np.np_redirect = true;
|
2020-05-18 00:07:36 +02:00
|
|
|
oflags = O_WRONLY | O_CREAT | O_APPEND;
|
|
|
|
redirfile = nsh_getfullpath(vtbl, argv[argc - 1]);
|
2014-01-10 23:15:14 +01:00
|
|
|
argc -= 2;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
2014-01-10 23:15:14 +01:00
|
|
|
}
|
2013-09-30 19:34:04 +02:00
|
|
|
#endif
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2021-12-31 15:54:57 +01:00
|
|
|
/* Last argument vector must be empty */
|
|
|
|
|
|
|
|
argv[argc] = NULL;
|
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
/* Check if the maximum number of arguments was exceeded */
|
2012-07-28 17:18:26 +02:00
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
if (argc > CONFIG_NSH_MAXARGUMENTS)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmttoomanyargs, cmd);
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
/* Then execute the command */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2014-01-10 23:15:14 +01:00
|
|
|
ret = nsh_execute(vtbl, argc, argv, redirfile, oflags);
|
|
|
|
|
|
|
|
/* Free any allocated resources */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2020-08-13 16:17:42 +02:00
|
|
|
#ifdef CONFIG_FILE_STREAM
|
2014-01-10 23:15:14 +01:00
|
|
|
/* Free the redirected output file path */
|
|
|
|
|
|
|
|
if (redirfile)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2014-01-10 23:15:14 +01:00
|
|
|
nsh_freefullpath(redirfile);
|
2019-01-12 17:56:48 +01:00
|
|
|
vtbl->np.np_redirect = redirect_save;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
#endif
|
2013-09-30 19:34:04 +02:00
|
|
|
|
2014-01-11 16:50:54 +01:00
|
|
|
NSH_MEMLIST_FREE(&memlist);
|
2014-01-10 23:15:14 +01:00
|
|
|
return ret;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
2014-01-10 22:23:26 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_parse
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function parses and executes the line of text received from the
|
|
|
|
* user. This may consist of one or more NSH commands. Multiple NSH
|
|
|
|
* commands are separated by semi-colons.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nsh_parse(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline)
|
|
|
|
{
|
|
|
|
#ifdef NSH_DISABLE_SEMICOLON
|
|
|
|
return nsh_parse_command(vtbl, cmdline);
|
|
|
|
|
|
|
|
#else
|
2014-01-18 16:39:16 +01:00
|
|
|
#if !defined(CONFIG_NSH_DISABLESCRIPT) && !defined(CONFIG_NSH_DISABLE_LOOPS)
|
2014-01-17 22:56:32 +01:00
|
|
|
FAR struct nsh_parser_s *np = &vtbl->np;
|
|
|
|
#endif
|
2014-01-10 22:23:26 +01:00
|
|
|
FAR char *start = cmdline;
|
|
|
|
FAR char *working = cmdline;
|
|
|
|
FAR char *ptr;
|
|
|
|
size_t len;
|
|
|
|
int ret;
|
|
|
|
|
2020-05-18 00:07:36 +02:00
|
|
|
/* Loop until all of the commands on the command line have been processed
|
|
|
|
* OR until the end-of-loop has been encountered and we need to reload the
|
|
|
|
* line at the top of the loop.
|
2014-01-17 22:56:32 +01:00
|
|
|
*/
|
2014-01-10 22:23:26 +01:00
|
|
|
|
2014-01-18 16:39:16 +01:00
|
|
|
#if !defined(CONFIG_NSH_DISABLESCRIPT) && !defined(CONFIG_NSH_DISABLE_LOOPS)
|
2014-01-17 22:56:32 +01:00
|
|
|
for (np->np_jump = false; !np->np_jump; )
|
|
|
|
#else
|
2020-05-18 00:07:36 +02:00
|
|
|
for (; ; )
|
2014-01-17 22:56:32 +01:00
|
|
|
#endif
|
2014-01-10 22:23:26 +01:00
|
|
|
{
|
2014-01-18 16:39:16 +01:00
|
|
|
#if !defined(CONFIG_NSH_DISABLESCRIPT) && !defined(CONFIG_NSH_DISABLE_LOOPS)
|
2014-01-17 22:56:32 +01:00
|
|
|
/* Save the offset on the line to the start of the command */
|
|
|
|
|
|
|
|
np->np_loffs = (uint16_t)(working - cmdline);
|
|
|
|
#endif
|
2014-01-10 22:23:26 +01:00
|
|
|
/* A command may be terminated with a newline character, the end of the
|
2014-07-01 21:43:40 +02:00
|
|
|
* line, a semicolon, or a '#' character. NOTE that the set of
|
|
|
|
* delimiting characters includes the quotation mark. We need to
|
|
|
|
* handle quotation marks here because any other delimiter within a
|
|
|
|
* quoted string must be treated as normal text.
|
2014-01-10 22:23:26 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
len = strcspn(working, g_line_separator);
|
|
|
|
ptr = working + len;
|
|
|
|
|
|
|
|
/* Check for the last command on the line. This means that the none
|
2014-07-01 21:43:40 +02:00
|
|
|
* of the delimiting characters was found or that the newline or '#'
|
|
|
|
* character was found. Anything after the newline or '#' character
|
|
|
|
* is ignored (there should not be anything after a newline, of
|
|
|
|
* course).
|
2014-01-10 22:23:26 +01:00
|
|
|
*/
|
|
|
|
|
2014-07-01 21:43:40 +02:00
|
|
|
if (*ptr == '\0' || *ptr == '\n' || *ptr == '#')
|
2014-01-10 22:23:26 +01:00
|
|
|
{
|
|
|
|
/* Parse the last command on the line */
|
|
|
|
|
|
|
|
return nsh_parse_command(vtbl, start);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for a command terminated with ';'. There is probably another
|
|
|
|
* command on the command line after this one.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (*ptr == ';')
|
|
|
|
{
|
|
|
|
/* Terminate the line */
|
|
|
|
|
|
|
|
*ptr++ = '\0';
|
|
|
|
|
|
|
|
/* Parse this command */
|
|
|
|
|
|
|
|
ret = nsh_parse_command(vtbl, start);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
/* nsh_parse_command may return (1) -1 (ERROR) meaning that the
|
|
|
|
* command failed or we failed to start the command application
|
|
|
|
* or (2) 1 meaning that the application task was spawned
|
|
|
|
* successfully but returned failure exit status.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then set the start of the next command on the command line */
|
|
|
|
|
|
|
|
start = ptr;
|
|
|
|
working = ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we encountered a quoted string */
|
|
|
|
|
|
|
|
else /* if (*ptr == '"') */
|
|
|
|
{
|
|
|
|
/* Find the closing quotation mark */
|
|
|
|
|
2018-08-06 21:28:31 +02:00
|
|
|
FAR char *tmp = nsh_strchr(ptr + 1, '"');
|
2014-01-10 22:23:26 +01:00
|
|
|
if (!tmp)
|
|
|
|
{
|
|
|
|
/* No closing quotation mark! */
|
|
|
|
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtnomatching, "\"", "\"");
|
2014-01-10 22:23:26 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, continue parsing after the closing quotation mark */
|
|
|
|
|
2014-01-11 20:18:12 +01:00
|
|
|
working = ++tmp;
|
2014-01-10 22:23:26 +01:00
|
|
|
}
|
|
|
|
}
|
2014-01-17 22:56:32 +01:00
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_DISABLESCRIPT
|
|
|
|
return OK;
|
|
|
|
#endif
|
2014-01-10 22:23:26 +01:00
|
|
|
#endif
|
|
|
|
}
|
2014-01-18 16:39:16 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_break
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if !defined(CONFIG_NSH_DISABLESCRIPT) && !defined(CONFIG_NSH_DISABLE_LOOPS)
|
|
|
|
int cmd_break(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
FAR struct nsh_parser_s *np = &vtbl->np;
|
|
|
|
|
|
|
|
/* Break outside of a loop is ignored */
|
|
|
|
|
|
|
|
if (np->np_lpstate[np->np_lpndx].lp_state == NSH_LOOP_DO)
|
|
|
|
{
|
|
|
|
#ifndef CONFIG_NSH_DISABLE_ITEF
|
|
|
|
/* Yes... pop the original if-then-else-if state */
|
|
|
|
|
|
|
|
np->np_iendx = np->np_lpstate[np->np_lpndx].lp_iendx;
|
|
|
|
#endif
|
|
|
|
/* Disable all command processing until 'done' is encountered. */
|
|
|
|
|
|
|
|
np->np_lpstate[np->np_lpndx].lp_enable = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No syntax errors are detected(?). Break is a nop everywhere except
|
|
|
|
* the supported context.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
2014-02-21 01:14:02 +01:00
|
|
|
#endif
|