Skip to content
Snippets Groups Projects
nsh_main.c 32.8 KiB
Newer Older
/****************************************************************************
 * examples/nsh/nsh_main.c
patacongo's avatar
patacongo committed
 *
patacongo's avatar
patacongo committed
 *   Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
patacongo's avatar
patacongo committed
 *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
patacongo's avatar
patacongo committed
 * 3. Neither the name NuttX nor the names of its contributors may be
patacongo's avatar
patacongo committed
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/
patacongo's avatar
patacongo committed

/****************************************************************************
patacongo's avatar
patacongo committed
 * Included Files
 ****************************************************************************/
patacongo's avatar
patacongo committed

#include <nuttx/config.h>
#include <sys/types.h>
patacongo's avatar
patacongo committed
#include <sys/stat.h>
patacongo's avatar
patacongo committed
#include <stdio.h>
patacongo's avatar
patacongo committed
#include <stdlib.h>
patacongo's avatar
patacongo committed
#include <unistd.h>
patacongo's avatar
patacongo committed
#include <string.h>
patacongo's avatar
patacongo committed
#include <sched.h>
patacongo's avatar
patacongo committed
#include <fcntl.h>
patacongo's avatar
patacongo committed
#include <errno.h>
#include <debug.h>

#ifndef CONFIG_DISABLE_PTHREAD
#  include <pthread.h>
#endif
patacongo's avatar
patacongo committed

#include "nsh.h"
patacongo's avatar
patacongo committed

/****************************************************************************
patacongo's avatar
patacongo committed
 * Definitions
 ****************************************************************************/
patacongo's avatar
patacongo committed

/* Argument list size
 *
 *   argv[0]:      The command name. 
 *   argv[1]:      The beginning of argument (up to NSH_MAX_ARGUMENTS)
 *   argv[argc-3]: Possibly '>' or '>>'
 *   argv[argc-2]: Possibly <file>
 *   argv[argc-1]: Possibly '&' (if pthreads are enabled)
 *   argv[argc]:   NULL terminating pointer
 *
 * Maximum size is NSH_MAX_ARGUMENTS+5
 */

#ifndef CONFIG_DISABLE_PTHREAD
#  define MAX_ARGV_ENTRIES (NSH_MAX_ARGUMENTS+5)
#else
#  define MAX_ARGV_ENTRIES (NSH_MAX_ARGUMENTS+4)
#endif

#if CONFIG_RR_INTERVAL > 0
# define SCHED_NSH SCHED_RR
#else
# define SCHED_NSH SCHED_FIFO
#endif

/****************************************************************************
 * Private Types
 ****************************************************************************/
  const char *cmd;        /* Name of the command */
  cmd_t       handler;    /* Function that handles the command */
  ubyte       minargs;    /* Minimum number of arguments (including command) */
  ubyte       maxargs;    /* Maximum number of arguments (including command) */
  const char *usage;      /* Usage instructions for 'help' command */
#ifndef CONFIG_DISABLE_PTHREAD
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

/****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/
static int cmd_help(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
static int cmd_exit(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
static int cmd_unrecognized(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
/****************************************************************************
patacongo's avatar
patacongo committed
 * Private Data
 ****************************************************************************/
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
static const char g_delim[]      = " \t\n";
static const char g_redirect1[]  = ">";
static const char g_redirect2[]  = ">>";
static const char g_exitstatus[] = "$?";
static const char g_success[]    = "0";
static const char g_failure[]    = "1";

static const struct cmdmap_s g_cmdmap[] =
{
#if CONFIG_NFILE_DESCRIPTORS > 0
patacongo's avatar
patacongo committed
  { "cat",      cmd_cat,      2, NSH_MAX_ARGUMENTS, "<path> [<path> [<path> ...]]" },
patacongo's avatar
patacongo committed
#ifndef CONFIG_DISABLE_ENVIRON
  { "cd",       cmd_cd,       1, 2, "[<dir-path>|-|~|..]" },
#endif
patacongo's avatar
patacongo committed
  { "cp",       cmd_cp,       3, 3, "<source-path> <dest-path>" },
patacongo's avatar
patacongo committed
#ifndef CONFIG_DISABLE_ENVIRON
patacongo's avatar
patacongo committed
  { "echo",     cmd_echo,     0, NSH_MAX_ARGUMENTS, "[<string|$name> [<string|$name>...]]" },
patacongo's avatar
patacongo committed
#else
patacongo's avatar
patacongo committed
  { "echo",     cmd_echo,     0, NSH_MAX_ARGUMENTS, "[<string> [<string>...]]" },
patacongo's avatar
patacongo committed
#endif
patacongo's avatar
patacongo committed
  { "exec",     cmd_exec,     2, 3, "<hex-address>" },
  { "exit",     cmd_exit,     1, 1, NULL },
  { "help",     cmd_help,     1, 1, NULL },
patacongo's avatar
patacongo committed
#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0
  { "ifconfig", cmd_ifconfig, 1, 1, NULL },
patacongo's avatar
patacongo committed
#endif
#if CONFIG_NFILE_DESCRIPTORS > 0
patacongo's avatar
patacongo committed
  { "ls",       cmd_ls,       1, 5, "[-lRs] <dir-path>" },
  { "mb",       cmd_mb,       2, 3, "<hex-address>[=<hex-value>][ <hex-byte-count>]" },
patacongo's avatar
patacongo committed
  { "mem",      cmd_mem,      1, 1, NULL },
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && CONFIG_NFILE_DESCRIPTORS > 0
patacongo's avatar
patacongo committed
  { "mkdir",    cmd_mkdir,    2, 2, "<path>" },
patacongo's avatar
patacongo committed
#ifdef CONFIG_FS_FAT
patacongo's avatar
patacongo committed
  { "mkfatfs",  cmd_mkfatfs,  2, 2, "<path>" },
patacongo's avatar
patacongo committed
#endif
  { "mkfifo",   cmd_mkfifo,   2, 2, "<path>" },
#endif
  { "mh",       cmd_mh,       2, 3, "<hex-address>[=<hex-value>][ <hex-byte-count>]" },
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && CONFIG_NFILE_DESCRIPTORS > 0
patacongo's avatar
patacongo committed
#ifdef CONFIG_FS_FAT /* Need at least one filesytem in configuration */
patacongo's avatar
patacongo committed
  { "mount",    cmd_mount,    4, 5, "-t <fstype> <block-device> <dir-path>" },
patacongo's avatar
patacongo committed
#endif
  { "mw",       cmd_mw,       2, 3, "<hex-address>[=<hex-value>][ <hex-byte-count>]" },
patacongo's avatar
patacongo committed
  { "ps",       cmd_ps,       1, 1, NULL },
patacongo's avatar
patacongo committed
#if CONFIG_NFILE_DESCRIPTORS > 0 && !defined(CONFIG_DISABLE_ENVIRON)
  { "pwd",      cmd_pwd,      1, 1, NULL },
#endif
patacongo's avatar
patacongo committed
#ifndef CONFIG_DISABLE_ENVIRON
patacongo's avatar
patacongo committed
  { "set",      cmd_set,      3, 3, "<name> <value>" },
patacongo's avatar
patacongo committed
#endif
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && CONFIG_NFILE_DESCRIPTORS > 0
patacongo's avatar
patacongo committed
  { "rm",       cmd_rm,       2, 2, "<file-path>" },
  { "rmdir",    cmd_rmdir,    2, 2, "<dir-path>" },
patacongo's avatar
patacongo committed
#endif
patacongo's avatar
patacongo committed
#if  CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_NFILE_STREAMS > 0
  { "sh",       cmd_sh,       2, 2, "<script-path>" },
patacongo's avatar
patacongo committed
#endif  /* CONFIG_NFILE_DESCRIPTORS && CONFIG_NFILE_STREAMS */
patacongo's avatar
patacongo committed
#ifndef CONFIG_DISABLE_SIGNALS
  { "sleep",    cmd_sleep,    2, 2, "<sec>" },
#endif /* CONFIG_DISABLE_SIGNALS */
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && CONFIG_NFILE_DESCRIPTORS > 0
patacongo's avatar
patacongo committed
# ifdef CONFIG_FS_FAT /* Need at least one filesytem in configuration */
patacongo's avatar
patacongo committed
  { "umount",   cmd_umount,   2, 2, "<dir-path>" },
patacongo's avatar
patacongo committed
# endif
patacongo's avatar
patacongo committed
#endif
patacongo's avatar
patacongo committed
#ifndef CONFIG_DISABLE_ENVIRON
patacongo's avatar
patacongo committed
  { "unset",    cmd_unset,    2, 2, "<name>" },
patacongo's avatar
patacongo committed
#ifndef CONFIG_DISABLE_SIGNALS
  { "usleep",   cmd_usleep,   2, 2, "<usec>" },
#endif /* CONFIG_DISABLE_SIGNALS */
patacongo's avatar
patacongo committed
  { NULL,     NULL,           1, 1, NULL }
/****************************************************************************
patacongo's avatar
patacongo committed
 * Public Data
 ****************************************************************************/

patacongo's avatar
patacongo committed
const char g_nshgreeting[]       = "NuttShell (NSH)\n";
patacongo's avatar
patacongo committed
const char g_nshprompt[]         = "nsh> ";
patacongo's avatar
patacongo committed
const char g_fmtargrequired[]    = "nsh: %s: missing required argument(s)\n";
const char g_fmtarginvalid[]     = "nsh: %s: argument invalid\n";
const char g_fmtargrange[]       = "nsh: %s: value out of range\n";
patacongo's avatar
patacongo committed
const char g_fmtcmdnotfound[]    = "nsh: %s: command not found\n";
const char g_fmtcmdnotimpl[]     = "nsh: %s: command not implemented\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";
#ifdef CONFIG_EXAMPLES_NSH_STRERROR
patacongo's avatar
patacongo committed
const char g_fmtcmdfailed[]      = "nsh: %s: %s failed: %s\n";
#else
const char g_fmtcmdfailed[]      = "nsh: %s: %s failed: %d\n";
#endif
patacongo's avatar
patacongo committed
const char g_fmtcmdoutofmemory[] = "nsh: %s: out of memory\n";
const char g_fmtinternalerror[]  = "nsh: %s: Internal error\n";

/****************************************************************************
patacongo's avatar
patacongo committed
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Name: cmd_help
 ****************************************************************************/
static int cmd_help(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
  const struct cmdmap_s *ptr;

  nsh_output(vtbl, "NSH command forms:\n");
#ifndef CONFIG_DISABLE_PTHREAD
  nsh_output(vtbl, "  [nice [-d <niceness>>]] <cmd> [> <file>|>> <file>] [&]\n");
#else
  nsh_output(vtbl, "  <cmd> [> <file>|>> <file>]\n");
#endif
  nsh_output(vtbl, "OR\n");
  nsh_output(vtbl, "  if <cmd>\n");
  nsh_output(vtbl, "  then\n");
  nsh_output(vtbl, "    [sequence of <cmd>]\n");
  nsh_output(vtbl, "  else\n");
  nsh_output(vtbl, "    [sequence of <cmd>]\n");
  nsh_output(vtbl, "  fi\n");
patacongo's avatar
patacongo committed
  nsh_output(vtbl, "Where <cmd> is one of:\n");
  for (ptr = g_cmdmap; ptr->cmd; ptr++)
    {
      if (ptr->usage)
        {
patacongo's avatar
patacongo committed
          nsh_output(vtbl, "  %s %s\n", ptr->cmd, ptr->usage);
patacongo's avatar
patacongo committed
          nsh_output(vtbl, "  %s\n", ptr->cmd);
  return OK;
/****************************************************************************
 * Name: cmd_unrecognized
 ****************************************************************************/
static int cmd_unrecognized(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
patacongo's avatar
patacongo committed
  nsh_output(vtbl, g_fmtcmdnotfound, argv[0]);
  return ERROR;
patacongo's avatar
patacongo committed
/****************************************************************************
 * Name: cmd_exit
 ****************************************************************************/

static int cmd_exit(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
patacongo's avatar
patacongo committed
{
  nsh_exit(vtbl);
  return OK;
patacongo's avatar
patacongo committed
/****************************************************************************
 * Name: nsh_execute
 ****************************************************************************/

static int nsh_execute(FAR struct nsh_vtbl_s *vtbl, int argc, char *argv[])
patacongo's avatar
patacongo committed
{
   const struct cmdmap_s *cmdmap;
   const char            *cmd;
   cmd_t                  handler = cmd_unrecognized;
   int                    ret;
patacongo's avatar
patacongo committed

   /* The form of argv is:
patacongo's avatar
patacongo committed
    *
    * argv[0]:      The command name.  This is argv[0] when the arguments
patacongo's avatar
patacongo committed
    *               are, finally, received by the command vtblr
    * argv[1]:      The beginning of argument (up to NSH_MAX_ARGUMENTS)
patacongo's avatar
patacongo committed
    * argv[argc]:   NULL terminating pointer
patacongo's avatar
patacongo committed
    */

   cmd = argv[0];
patacongo's avatar
patacongo committed

   /* See if the command is one that we understand */

   for (cmdmap = g_cmdmap; cmdmap->cmd; cmdmap++)
     {
       if (strcmp(cmdmap->cmd, cmd) == 0)
         {
           /* Check if a valid number of arguments was provided.  We
            * do this simple, imperfect checking here so that it does
            * not have to be performed in each command.
            */

           if (argc < cmdmap->minargs)
             {
               /* Fewer than the minimum number were provided */

patacongo's avatar
patacongo committed
               nsh_output(vtbl, g_fmtargrequired, cmd);
patacongo's avatar
patacongo committed
               return ERROR;
             }
           else if (argc > cmdmap->maxargs)
             {
               /* More than the maximum number were provided */

patacongo's avatar
patacongo committed
               nsh_output(vtbl, g_fmttoomanyargs, cmd);
patacongo's avatar
patacongo committed
               return ERROR;
             }
           else
             {
               /* A valid number of arguments were provided (this does
                * not mean they are right).
                */

               handler = cmdmap->handler;
               break;
             }
         }
     }

   ret = handler(vtbl, argc, argv);
   return ret;
/****************************************************************************
 * Name: nsh_releaseargs
 ****************************************************************************/

#ifndef CONFIG_DISABLE_PTHREAD
static void nsh_releaseargs(struct cmdarg_s *arg)
{
  FAR struct nsh_vtbl_s *vtbl = arg->vtbl;
  int i;

  /* 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)
    {
      (void)close(arg->fd);
    }

  /* Released the cloned vtbl instance */

  nsh_release(vtbl);

  /* Release the cloned args */

  for (i = 0; i < arg->argc; i++)
    {
      free(arg->argv[i]);
    }
  free(arg);
}
#endif

/****************************************************************************
 * Name: nsh_child
 ****************************************************************************/

#ifndef CONFIG_DISABLE_PTHREAD
static pthread_addr_t nsh_child(pthread_addr_t arg)
{
  struct cmdarg_s *carg = (struct cmdarg_s *)arg;
  int ret;

  dbg("BG %s\n", carg->argv[0]);

  /* Execute the specified command on the child thread */

  ret = nsh_execute(carg->vtbl, carg->argc, carg->argv);

  /* Released the cloned arguments */

  dbg("BG %s complete\n", carg->argv[0]);
  nsh_releaseargs(carg);
  return (void*)ret;
}
#endif

/****************************************************************************
 * Name: nsh_cloneargs
 ****************************************************************************/

static inline struct cmdarg_s *nsh_cloneargs(FAR struct nsh_vtbl_s *vtbl,
                                             int fd, int argc, char *argv[])
{
  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]);
        }
    }
  return ret;
}

patacongo's avatar
patacongo committed
/****************************************************************************
patacongo's avatar
patacongo committed
 * Name: nsh_argument
patacongo's avatar
patacongo committed
 ****************************************************************************/

char *nsh_argument(FAR struct nsh_vtbl_s *vtbl, char **saveptr)
patacongo's avatar
patacongo committed
{
patacongo's avatar
patacongo committed
  char *pbegin = *saveptr;
  char *pend   = NULL;
  const char *term;
patacongo's avatar
patacongo committed
#ifndef CONFIG_DISABLE_ENVIRON
  boolean quoted = FALSE;
#endif
patacongo's avatar
patacongo committed

  /* Find the beginning of the next token */

  for (;
       *pbegin && strchr(g_delim, *pbegin) != NULL;
       pbegin++);

  /* If we are at the end of the string with nothing
   * but delimiters found, then return NULL.
   */

  if (!*pbegin)
    {
      return NULL;
    }

patacongo's avatar
patacongo committed
  /* Does the token begin with '>' -- redirection of output? */
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
  if (*pbegin == '>')
    {
      /* Yes.. does it begin with ">>"? */

      if (*(pbegin + 1) == '>')
        {
          *saveptr = pbegin + 2;
patacongo's avatar
patacongo committed
          pbegin   = (char*)g_redirect2;
patacongo's avatar
patacongo committed
        }
      else
        {
          *saveptr = pbegin + 1;
patacongo's avatar
patacongo committed
          pbegin   = (char*)g_redirect1;
patacongo's avatar
patacongo committed
        }
    }
patacongo's avatar
patacongo committed

  /* Does the token begin with '#' -- comment */

  else if (*pbegin == '#')
    {
      /* Return NULL meaning that we are at the end of the line */

      *saveptr = pbegin;
      pbegin   = NULL;
    }
patacongo's avatar
patacongo committed
  else
    {
patacongo's avatar
patacongo committed
      /* Otherwise, we are going to have to parse to find the end of
       * the token.  Does the token begin with '"'?
       */
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
      if (*pbegin == '"')
        {
          /* Yes.. then only another '"' can terminate the string */
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
          pbegin++;
          term = "\"";
#ifndef CONFIG_DISABLE_ENVIRON
          quoted = TRUE;
#endif
        }
      else
        {
          /* No, then any of the usual terminators will terminate the argument */
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
          term = g_delim;
        }
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
      /* Find the end of the string */
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
      for (pend = pbegin + 1;
           *pend && strchr(term, *pend) == NULL;
           pend++);
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
      /* pend either points to the end of the string or to
       * the first delimiter after the string.
       */
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
      if (*pend)
        {
          /* Turn the delimiter into a null terminator */

          *pend++ = '\0';
        }

      /* Save the pointer where we left off */

      *saveptr = pend;

patacongo's avatar
patacongo committed
      /* Check for references to environment variables */
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
      if (pbegin[0] == '$' && !quoted)
patacongo's avatar
patacongo committed
        {
patacongo's avatar
patacongo committed
          /* Check for built-in variables */

          if (strcmp(pbegin, g_exitstatus) == 0)
patacongo's avatar
patacongo committed
              if (vtbl->np.np_fail)
                {
                  return (char*)g_failure;
                }
              else
                {
                  return (char*)g_success;
                }
patacongo's avatar
patacongo committed
          /* Not a built-in? Return the value of the environment variable with this name */
#ifndef CONFIG_DISABLE_ENVIRON
          else
            {
patacongo's avatar
patacongo committed
              char *value = getenv(pbegin+1);
              if (value)
                {
                  return value;
                }
              else
                {
                  return (char*)"";
                }
patacongo's avatar
patacongo committed
#endif
patacongo's avatar
patacongo committed
        }
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
  /* Return the beginning of the token. */
patacongo's avatar
patacongo committed

  return pbegin;
/****************************************************************************
 * Name: nsh_cmdenabled
 ****************************************************************************/

static inline boolean nsh_cmdenabled(FAR struct nsh_vtbl_s *vtbl)
{
  struct nsh_parser_s *np = &vtbl->np;
  boolean ret = !np->np_st[np->np_ndx].ns_disabled;
  if (ret)
    {
      switch (np->np_st[np->np_ndx].ns_state)
        {
          case NSH_PARSER_NORMAL :
          case NSH_PARSER_IF:
          default:
            break;

          case NSH_PARSER_THEN:
            ret = !np->np_st[np->np_ndx].ns_ifcond;
            break;

          case NSH_PARSER_ELSE:
            ret = np->np_st[np->np_ndx].ns_ifcond;
            break;
        }
    }
  return ret;
}

/****************************************************************************
 * Name: nsh_ifthenelse
 ****************************************************************************/

static inline int nsh_ifthenelse(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd, FAR char **saveptr)
{
  struct nsh_parser_s *np = &vtbl->np;
  FAR char *cmd = *ppcmd;
  boolean disabled;

  if (cmd)
    {
      /* Check if the command is preceeded by "if" */

      if (strcmp(cmd, "if") == 0)
        {
          /* Get the cmd following the if */

          *ppcmd = nsh_argument(vtbl, saveptr);
          if (!*ppcmd)
            {
              nsh_output(vtbl, g_fmtarginvalid, "if");
patacongo's avatar
patacongo committed
              goto errout;
            }

          /* Verify that "if" is valid in this context */

          if (np->np_st[np->np_ndx].ns_state != NSH_PARSER_NORMAL &&
              np->np_st[np->np_ndx].ns_state != NSH_PARSER_THEN &&
              np->np_st[np->np_ndx].ns_state != NSH_PARSER_ELSE)
            {
              nsh_output(vtbl, g_fmtcontext, "if");
patacongo's avatar
patacongo committed
              goto errout;

          /* Check if we have exceeded the maximum depth of nesting */

          if (np->np_ndx >= CONFIG_EXAMPLES_NSH_NESTDEPTH-1)
            {
              nsh_output(vtbl, g_fmtdeepnesting, "if");
              goto errout;
            }

          /* "Push" the old state and set the new state */

          disabled                          = !nsh_cmdenabled(vtbl);
          np->np_ndx++;
          np->np_st[np->np_ndx].ns_state    = NSH_PARSER_IF;
          np->np_st[np->np_ndx].ns_disabled = disabled;
          np->np_st[np->np_ndx].ns_ifcond   = FALSE;
        }
      else if (strcmp(cmd, "then") == 0)
        {
          /* Get the cmd following the then -- there shouldn't be one */

          *ppcmd = nsh_argument(vtbl, saveptr);
          if (*ppcmd)
            {
              nsh_output(vtbl, g_fmtarginvalid, "then");
patacongo's avatar
patacongo committed
              goto errout;
            }

          /* Verify that "then" is valid in this context */

          if (np->np_st[np->np_ndx].ns_state != NSH_PARSER_IF)
            {
              nsh_output(vtbl, g_fmtcontext, "then");
patacongo's avatar
patacongo committed
              goto errout;
          np->np_st[np->np_ndx].ns_state = NSH_PARSER_THEN;
        }
      else if (strcmp(cmd, "else") == 0)
        {
          /* Get the cmd following the else -- there shouldn't be one */

          *ppcmd = nsh_argument(vtbl, saveptr);
          if (*ppcmd)
            {
              nsh_output(vtbl, g_fmtarginvalid, "else");
patacongo's avatar
patacongo committed
              goto errout;
            }

          /* Verify that "then" is valid in this context */

          if (np->np_st[np->np_ndx].ns_state != NSH_PARSER_THEN)
            {
              nsh_output(vtbl, g_fmtcontext, "else");
patacongo's avatar
patacongo committed
              goto errout;
          np->np_st[np->np_ndx].ns_state = NSH_PARSER_ELSE;
        }
      else if (strcmp(cmd, "fi") == 0)
        {
          /* Get the cmd following the fi -- there should be one */

          *ppcmd = nsh_argument(vtbl, saveptr);
          if (*ppcmd)
            {
              nsh_output(vtbl, g_fmtarginvalid, "fi");
patacongo's avatar
patacongo committed
              goto errout;
            }

          /* Verify that "fi" is valid in this context */

          if (np->np_st[np->np_ndx].ns_state != NSH_PARSER_THEN &&
              np->np_st[np->np_ndx].ns_state != NSH_PARSER_ELSE)
            {
              nsh_output(vtbl, g_fmtcontext, "fi");
patacongo's avatar
patacongo committed
              goto errout;

          if (np->np_ndx < 1) /* Shouldn't happen */
            {
              nsh_output(vtbl, g_fmtinternalerror, "if");
              goto errout;
            }

          /* "Pop" the previous state */

          np->np_ndx--;
      else if (np->np_st[np->np_ndx].ns_state == NSH_PARSER_IF)
        {
          nsh_output(vtbl, g_fmtcontext, cmd);
patacongo's avatar
patacongo committed
          goto errout;
patacongo's avatar
patacongo committed

errout:
  np->np_ndx               = 0;
  np->np_st[0].ns_state    = NSH_PARSER_NORMAL;
  np->np_st[0].ns_disabled = FALSE;
  np->np_st[0].ns_ifcond   = FALSE;
patacongo's avatar
patacongo committed
  return ERROR;
}

/****************************************************************************
 * Name: nsh_saveresult
 ****************************************************************************/

patacongo's avatar
patacongo committed
static inline int nsh_saveresult(FAR struct nsh_vtbl_s *vtbl, boolean result)
  struct nsh_parser_s *np = &vtbl->np;

  if (np->np_st[np->np_ndx].ns_state == NSH_PARSER_IF)
      np->np_fail = FALSE;
      np->np_st[np->np_ndx].ns_ifcond = result;
patacongo's avatar
patacongo committed
      return OK;
    }
  else
    {
      np->np_fail = result;
patacongo's avatar
patacongo committed
      return result ? ERROR : OK;
    }
}

/****************************************************************************
 * Name: nsh_nice
 ****************************************************************************/

#ifndef CONFIG_DISABLE_PTHREAD
static inline int nsh_nice(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd, FAR char **saveptr)
{
  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) */

          cmd = nsh_argument(vtbl, saveptr);
          if (cmd && strcmp(cmd, "-d") == 0)
            {
              FAR char *val = nsh_argument(vtbl, saveptr);
              if (val)
                {
                  char *endptr;
                  vtbl->np.np_nice = (int)strtol(val, &endptr, 0);
patacongo's avatar
patacongo committed
                  if (vtbl->np.np_nice > 19 || vtbl->np.np_nice < -20 ||
                      endptr == val || *endptr != '\0')
                    {
                      nsh_output(vtbl, g_fmtarginvalid, "nice");
                      return ERROR;
                    }
                  cmd = nsh_argument(vtbl, saveptr);
                }
            }

          /* Return the real command name */

          *ppcmd = cmd;
        }
    }
  return OK;
}
/****************************************************************************
patacongo's avatar
patacongo committed
 * Public Functions
 ****************************************************************************/
patacongo's avatar
patacongo committed

/****************************************************************************
patacongo's avatar
patacongo committed
 * Name: user_initialize
 ****************************************************************************/
patacongo's avatar
patacongo committed

void user_initialize(void)
{
  /* stub */
}

/****************************************************************************
patacongo's avatar
patacongo committed
 * Name: user_start
 ****************************************************************************/
patacongo's avatar
patacongo committed

int user_start(int argc, char *argv[])
patacongo's avatar
patacongo committed
{
patacongo's avatar
patacongo committed
  int mid_priority;
  int ret;
patacongo's avatar
patacongo committed

  /* Set the priority of this task to something in the middle so that 'nice'
   * can both raise and lower the priority.
   */

  mid_priority = (sched_get_priority_max(SCHED_NSH) + sched_get_priority_min(SCHED_NSH)) >> 1;
patacongo's avatar
patacongo committed
    {
      struct sched_param param;

      param.sched_priority = mid_priority;
      (void)sched_setscheduler(0, SCHED_NSH, &param);
patacongo's avatar
patacongo committed
    }

  /* If both the console and telnet are selected as front-ends, then run
   * the telnet front end on another thread.
   */

#if defined(CONFIG_EXAMPLES_NSH_CONSOLE) && defined(CONFIG_EXAMPLES_NSH_TELNET)
# ifndef CONFIG_CUSTOM_STACK
  ret = task_create("nsh_telnetmain", mid_priority, CONFIG_EXAMPLES_NSH_STACKSIZE,
                    nsh_telnetmain, NULL);
# else
  ret = task_create("nsh_telnetmain", mid_priority, nsh_telnetmain, NULL);
# endif
  if (ret < 0)
   {
     fprintf(stderr, g_fmtcmdfailed, "user_start", "task_create", NSH_ERRNO);
   }

  /* If only the telnet front-end is selected, run it on this thread */

#elif defined(CONFIG_EXAMPLES_NSH_TELNET)
  return nsh_telnetmain(0, NULL);
#endif

/* If the serial console front end is selected, then run it on this thread */

#ifdef CONFIG_EXAMPLES_NSH_CONSOLE
  return nsh_consolemain(0, NULL);
#endif
patacongo's avatar
patacongo committed
/****************************************************************************
 * Name: nsh_parse
 ****************************************************************************/
patacongo's avatar
patacongo committed
int nsh_parse(FAR struct nsh_vtbl_s *vtbl, char *cmdline)
  FAR char *argv[MAX_ARGV_ENTRIES];
patacongo's avatar
patacongo committed
  FAR char *saveptr;
  FAR char *cmd;
patacongo's avatar
patacongo committed
  FAR char *redirfile = NULL;
patacongo's avatar
patacongo committed
  int       fd = -1;
patacongo's avatar
patacongo committed
  int       oflags = 0;
patacongo's avatar
patacongo committed
  int       argc;
  int       ret;

  /* Initialize parser state */

  memset(argv, 0, MAX_ARGV_ENTRIES*sizeof(FAR char *));
  vtbl->np.np_bg       = FALSE;
  vtbl->np.np_redirect = FALSE;
patacongo's avatar
patacongo committed
  /* Parse out the command at the beginning of the line */
patacongo's avatar
patacongo committed
  saveptr = cmdline;
  cmd = nsh_argument(vtbl, &saveptr);
patacongo's avatar
patacongo committed

  /* Handler if-then-else-fi */
patacongo's avatar
patacongo committed

  if (nsh_ifthenelse(vtbl, &cmd, &saveptr) != 0)
    {
      goto errout;
    }
patacongo's avatar
patacongo committed

  /* Handle nice */
#ifndef CONFIG_DISABLE_PTHREAD
  if (nsh_nice(vtbl, &cmd, &saveptr) != 0)
    {
      goto errout;
patacongo's avatar
patacongo committed
    }
  /* Check if any command was provided -OR- if command processing is
   * currently disabled.
   */
  if (!cmd || !nsh_cmdenabled(vtbl))
patacongo's avatar
patacongo committed
    {
      /* An empty line is not an error and an unprocessed command cannot
       * generate an error, but neither should they change the last
       * command status.
       */

      return OK;
patacongo's avatar
patacongo committed
    }
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
  /* Parse all of the arguments following the command name.  The form
   * of argv is:
   *
   *   argv[0]:      The command name. 
   *   argv[1]:      The beginning of argument (up to NSH_MAX_ARGUMENTS)
   *   argv[argc-3]: Possibly '>' or '>>'
   *   argv[argc-2]: Possibly <file>
   *   argv[argc-1]: Possibly '&'
   *   argv[argc]:   NULL terminating pointer
patacongo's avatar
patacongo committed
   *
   * Maximum size is NSH_MAX_ARGUMENTS+5
patacongo's avatar
patacongo committed
   */

  argv[0] = cmd;
  for (argc = 1; argc < MAX_ARGV_ENTRIES-1; argc++)
patacongo's avatar
patacongo committed
    {
      argv[argc] = nsh_argument(vtbl, &saveptr);
patacongo's avatar
patacongo committed
      if (!argv[argc])
patacongo's avatar
patacongo committed
          break;
patacongo's avatar
patacongo committed
    }
  argv[argc] = NULL;
patacongo's avatar
patacongo committed
  /* Check if the command should run in background */
patacongo's avatar
patacongo committed

  if (argc > 1 && strcmp(argv[argc-1], "&") == 0)
patacongo's avatar
patacongo committed
    {
      vtbl->np.np_bg = TRUE;
patacongo's avatar
patacongo committed
      argv[argc-1] = NULL;
      argc--;
    }

  /* Check if the output was re-directed using > or >> */

  if (argc > 2)
patacongo's avatar
patacongo committed
    {
      /* Check for redirection to a new file */

      if (strcmp(argv[argc-2], g_redirect1) == 0)
          vtbl->np.np_redirect = TRUE;
          oflags               = O_WRONLY|O_CREAT|O_TRUNC;
patacongo's avatar
patacongo committed
          redirfile            = nsh_getfullpath(vtbl, argv[argc-1]);
          argc                -= 2;
patacongo's avatar
patacongo committed
      /* Check for redirection by appending to an existing file */

      else if (strcmp(argv[argc-2], g_redirect2) == 0)
patacongo's avatar
patacongo committed
        {
          vtbl->np.np_redirect = TRUE;
          oflags               = O_WRONLY|O_CREAT|O_APPEND;
patacongo's avatar
patacongo committed
          redirfile            = nsh_getfullpath(vtbl, argv[argc-1]);
          argc                -= 2;
patacongo's avatar
patacongo committed
        }
patacongo's avatar
patacongo committed
    }

  /* Redirected output? */

  if (vtbl->np.np_redirect)
patacongo's avatar
patacongo committed
    {
      /* 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.
       */
patacongo's avatar
patacongo committed
      fd = open(redirfile, oflags, 0666);
patacongo's avatar
patacongo committed
      nsh_freefullpath(redirfile);
      redirfile = NULL;

patacongo's avatar
patacongo committed
      if (fd < 0)
patacongo's avatar
patacongo committed
          nsh_output(vtbl, g_fmtcmdfailed, cmd, "open", NSH_ERRNO);
          goto errout;
patacongo's avatar
patacongo committed
        }
    }