2007-07-01 22:05:11 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* nsh_envcmds.c
|
|
|
|
*
|
2009-12-15 20:00:40 +01:00
|
|
|
* Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
|
2007-07-01 22:05:11 +02:00
|
|
|
* 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.
|
2008-02-20 19:02:55 +01:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-07-01 22:05:11 +02:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2008-08-23 17:16:10 +02:00
|
|
|
#include <unistd.h>
|
2007-07-01 22:05:11 +02:00
|
|
|
#include <string.h>
|
2008-08-23 17:16:10 +02:00
|
|
|
#include <libgen.h>
|
2007-07-01 22:05:11 +02:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "nsh.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0 && !defined(CONFIG_DISABLE_ENVIRON)
|
|
|
|
static const char g_pwd[] = "PWD";
|
|
|
|
static const char g_oldpwd[] = "OLDPWD";
|
|
|
|
static const char g_home[] = CONFIG_LIB_HOMEDIR;
|
|
|
|
#endif
|
|
|
|
|
2007-07-01 22:05:11 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_getwd
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0 && !defined(CONFIG_DISABLE_ENVIRON)
|
|
|
|
static inline FAR const char *nsh_getwd(const char *wd)
|
|
|
|
{
|
|
|
|
const char *val;
|
|
|
|
|
|
|
|
/* If no working directory is defined, then default to the home directory */
|
|
|
|
|
|
|
|
val = getenv(wd);
|
|
|
|
if (!val)
|
|
|
|
{
|
|
|
|
val = g_home;
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_getdirpath
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline char *nsh_getdirpath(FAR struct nsh_vtbl_s *vtbl,
|
|
|
|
const char *dirpath, const char *relpath)
|
|
|
|
{
|
|
|
|
char *alloc;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
/* Handle the special case where the dirpath is simply */
|
|
|
|
|
|
|
|
if (strcmp(dirpath, "/") == 0)
|
|
|
|
{
|
|
|
|
len = strlen(relpath) + 2;
|
|
|
|
alloc = (char*)malloc(len);
|
|
|
|
if (alloc)
|
|
|
|
{
|
|
|
|
sprintf(alloc, "/%s", relpath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len = strlen(dirpath) + strlen(relpath) + 2;
|
|
|
|
alloc = (char*)malloc(len);
|
|
|
|
if (alloc)
|
|
|
|
{
|
|
|
|
sprintf(alloc, "%s/%s", dirpath, relpath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!alloc)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdoutofmemory, "nsh_getdirpath");
|
|
|
|
}
|
|
|
|
return alloc;
|
|
|
|
}
|
|
|
|
|
2007-07-01 22:05:11 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_getwd
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0 && !defined(CONFIG_DISABLE_ENVIRON)
|
|
|
|
FAR const char *nsh_getcwd(void)
|
|
|
|
{
|
|
|
|
return nsh_getwd(g_pwd);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_getfullpath
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0 && !defined(CONFIG_DISABLE_ENVIRON)
|
|
|
|
char *nsh_getfullpath(FAR struct nsh_vtbl_s *vtbl, const char *relpath)
|
|
|
|
{
|
|
|
|
const char *wd;
|
|
|
|
|
|
|
|
/* Handle some special cases */
|
|
|
|
|
|
|
|
if (!relpath || relpath[0] == '\0')
|
|
|
|
{
|
|
|
|
/* No relative path provided */
|
|
|
|
|
|
|
|
return strdup(g_home);
|
|
|
|
}
|
|
|
|
else if (relpath[0] == '/')
|
|
|
|
{
|
|
|
|
return strdup(relpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the path to the current working directory */
|
|
|
|
|
|
|
|
wd = nsh_getcwd();
|
|
|
|
|
|
|
|
/* Fake the '.' directory */
|
|
|
|
|
|
|
|
if (strcmp(relpath, ".") == 0)
|
|
|
|
{
|
|
|
|
return strdup(wd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the full path */
|
|
|
|
|
|
|
|
return nsh_getdirpath(vtbl, wd, relpath);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_freefullpath
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0 && !defined(CONFIG_DISABLE_ENVIRON)
|
|
|
|
void nsh_freefullpath(char *relpath)
|
|
|
|
{
|
|
|
|
if (relpath)
|
|
|
|
{
|
|
|
|
free(relpath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_cd
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0 && !defined(CONFIG_DISABLE_ENVIRON)
|
2008-11-14 16:11:26 +01:00
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_CD
|
2008-08-23 17:16:10 +02:00
|
|
|
int cmd_cd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
const char *path = argv[1];
|
|
|
|
char *alloc = NULL;
|
|
|
|
char *fullpath = NULL;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Check for special arguments */
|
|
|
|
|
|
|
|
if (argc < 2 || strcmp(path, "~") == 0)
|
|
|
|
{
|
|
|
|
path = g_home;
|
|
|
|
}
|
|
|
|
else if (strcmp(path, "-") == 0)
|
|
|
|
{
|
|
|
|
alloc = strdup(nsh_getwd(g_oldpwd));
|
|
|
|
path = alloc;
|
|
|
|
}
|
|
|
|
else if (strcmp(path, "..") == 0)
|
|
|
|
{
|
|
|
|
alloc = strdup(nsh_getcwd());
|
|
|
|
path = dirname(alloc);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fullpath = nsh_getfullpath(vtbl, path);
|
|
|
|
path = fullpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the new workding directory */
|
|
|
|
|
|
|
|
if (chdir(path) != 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "chdir", NSH_ERRNO);
|
|
|
|
ret = ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free any memory that was allocated */
|
|
|
|
|
|
|
|
if (alloc)
|
|
|
|
{
|
|
|
|
free(alloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fullpath)
|
|
|
|
{
|
|
|
|
nsh_freefullpath(fullpath);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
2008-11-14 16:11:26 +01:00
|
|
|
#endif
|
2008-08-23 17:16:10 +02:00
|
|
|
|
2007-07-01 22:05:11 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_echo
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-11-14 16:11:26 +01:00
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_ECHO
|
2008-08-17 20:59:50 +02:00
|
|
|
int cmd_echo(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
2007-07-01 22:05:11 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* echo each argument, separated by a space as it must have been on the
|
|
|
|
* command line
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
{
|
2008-08-17 18:17:44 +02:00
|
|
|
nsh_output(vtbl, "%s ", argv[i]);
|
2007-07-01 22:05:11 +02:00
|
|
|
}
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, "\n");
|
2008-08-17 20:59:50 +02:00
|
|
|
return OK;
|
2007-07-01 22:05:11 +02:00
|
|
|
}
|
2008-11-14 16:11:26 +01:00
|
|
|
#endif
|
2007-07-01 22:05:11 +02:00
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_pwd
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0 && !defined(CONFIG_DISABLE_ENVIRON)
|
2008-11-14 16:11:26 +01:00
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_PWD
|
2008-08-23 17:16:10 +02:00
|
|
|
int cmd_pwd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, "%s\n", nsh_getcwd());
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
2008-11-14 16:11:26 +01:00
|
|
|
#endif
|
2008-08-23 17:16:10 +02:00
|
|
|
|
2007-07-01 22:05:11 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_set
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_ENVIRON
|
2008-11-14 16:11:26 +01:00
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_SET
|
2008-08-17 20:59:50 +02:00
|
|
|
int cmd_set(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
2007-07-01 22:05:11 +02:00
|
|
|
{
|
2008-08-17 20:59:50 +02:00
|
|
|
int ret = setenv(argv[1], argv[2], TRUE);
|
|
|
|
if (ret < 0)
|
2007-07-01 22:05:11 +02:00
|
|
|
{
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "setenv", NSH_ERRNO);
|
2007-07-01 22:05:11 +02:00
|
|
|
}
|
2008-08-17 20:59:50 +02:00
|
|
|
return ret;
|
2007-07-01 22:05:11 +02:00
|
|
|
}
|
|
|
|
#endif
|
2008-11-14 16:11:26 +01:00
|
|
|
#endif
|
2007-07-01 22:05:11 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_unset
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_ENVIRON
|
2008-11-14 16:11:26 +01:00
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_UNSET
|
2008-08-17 20:59:50 +02:00
|
|
|
int cmd_unset(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
2007-07-01 22:05:11 +02:00
|
|
|
{
|
2008-08-17 20:59:50 +02:00
|
|
|
int ret = unsetenv(argv[1]);
|
|
|
|
if (ret < 0)
|
2007-07-01 22:05:11 +02:00
|
|
|
{
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "unsetenv", NSH_ERRNO);
|
2007-07-01 22:05:11 +02:00
|
|
|
}
|
2008-08-17 20:59:50 +02:00
|
|
|
return ret;
|
2007-07-01 22:05:11 +02:00
|
|
|
}
|
|
|
|
#endif
|
2008-11-14 16:11:26 +01:00
|
|
|
#endif
|