2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/nshlib/nsh_envcmds.c
|
|
|
|
*
|
2021-06-10 16:34:12 +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
|
|
|
*
|
2021-06-10 16:34:12 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-03-18 20:46:25 +01:00
|
|
|
*
|
2021-06-10 16:34:12 +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 <unistd.h>
|
|
|
|
#include <string.h>
|
2015-11-23 18:18:32 +01:00
|
|
|
#include <ctype.h>
|
2011-03-18 20:46:25 +01:00
|
|
|
#include <libgen.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "nsh.h"
|
2012-02-02 17:04:09 +01:00
|
|
|
#include "nsh_console.h"
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2021-09-07 05:29:30 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define CHAR_ESCAPE(s, c) \
|
|
|
|
case (c): \
|
|
|
|
*(s)++ = (c); \
|
|
|
|
break
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-11 20:10:10 +01:00
|
|
|
#ifndef CONFIG_DISABLE_ENVIRON
|
2011-03-18 20:46:25 +01:00
|
|
|
static const char g_pwd[] = "PWD";
|
2018-11-07 18:28:28 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_CD
|
2011-03-18 20:46:25 +01:00
|
|
|
static const char g_oldpwd[] = "OLDPWD";
|
2018-11-07 18:28:28 +01:00
|
|
|
#endif
|
2021-08-01 09:28:27 +02:00
|
|
|
static const char g_home[] = CONFIG_LIBC_HOMEDIR;
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-09-07 05:29:30 +02:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_ECHO
|
|
|
|
static void str_escape(FAR char *s)
|
|
|
|
{
|
|
|
|
FAR char *q;
|
|
|
|
int l;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
for (q = s; *q; q++)
|
|
|
|
{
|
|
|
|
if (*q != '\\')
|
|
|
|
{
|
|
|
|
*s++ = *q;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (*++q)
|
|
|
|
{
|
|
|
|
case '0':
|
|
|
|
for (c = 0, l = 3; l && q[1] >= '0' && q[1] <= '8'; l--, q++)
|
|
|
|
{
|
|
|
|
c = 8 * c + (q[1] - '0');
|
|
|
|
}
|
|
|
|
|
|
|
|
*s++ = c;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'x':
|
|
|
|
for (c = 0, l = 2; l && isxdigit(q[1]); l--, q++)
|
|
|
|
{
|
|
|
|
if (isdigit(q[1]))
|
|
|
|
{
|
|
|
|
c = 16 * c + (q[1] - '0');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c = 16 * c + (tolower(q[1]) - 'a' + 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*s++ = c;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\0':
|
|
|
|
*s++ = '\\';
|
|
|
|
*s++ = '\0';
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
*s++ = '\\';
|
|
|
|
*s++ = *q;
|
|
|
|
break;
|
|
|
|
|
|
|
|
CHAR_ESCAPE(s, '\a');
|
|
|
|
CHAR_ESCAPE(s, '\b');
|
|
|
|
CHAR_ESCAPE(s, '\f');
|
|
|
|
CHAR_ESCAPE(s, '\n');
|
|
|
|
CHAR_ESCAPE(s, '\r');
|
|
|
|
CHAR_ESCAPE(s, '\v');
|
|
|
|
CHAR_ESCAPE(s, '\\');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*s = '\0';
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_getwd
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-11 20:10:10 +01:00
|
|
|
#ifndef CONFIG_DISABLE_ENVIRON
|
2022-09-22 05:38:12 +02:00
|
|
|
static inline FAR const char *nsh_getwd(FAR const char *wd)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2022-09-22 05:38:12 +02:00
|
|
|
FAR const char *val;
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/* If no working directory is defined, then default to the home directory */
|
|
|
|
|
|
|
|
val = getenv(wd);
|
2018-10-02 00:22:04 +02:00
|
|
|
if (val == NULL)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
|
|
|
val = g_home;
|
|
|
|
}
|
2014-01-04 15:38:05 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-10-02 19:54:18 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_dumpvar
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(CONFIG_NSH_VARS) && !defined(CONFIG_NSH_DISABLE_SET)
|
|
|
|
static int nsh_dumpvar(FAR struct nsh_vtbl_s *vtbl, FAR void *arg,
|
|
|
|
FAR const char *pair)
|
|
|
|
{
|
2022-08-22 15:17:00 +02:00
|
|
|
UNUSED(arg);
|
|
|
|
|
2018-10-02 19:54:18 +02:00
|
|
|
nsh_output(vtbl, "%s\n", pair);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_getwd
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-11 20:10:10 +01:00
|
|
|
#ifndef CONFIG_DISABLE_ENVIRON
|
2011-03-18 20:46:25 +01:00
|
|
|
FAR const char *nsh_getcwd(void)
|
|
|
|
{
|
|
|
|
return nsh_getwd(g_pwd);
|
|
|
|
}
|
|
|
|
#endif
|
2012-06-15 01:27:02 +02:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_getfullpath
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-11 20:10:10 +01:00
|
|
|
#ifndef CONFIG_DISABLE_ENVIRON
|
2013-07-19 19:43:04 +02:00
|
|
|
FAR char *nsh_getfullpath(FAR struct nsh_vtbl_s *vtbl,
|
|
|
|
FAR const char *relpath)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2022-09-22 05:38:12 +02:00
|
|
|
FAR const char *wd;
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/* 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 */
|
2014-01-04 15:38:05 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
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
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-11 20:10:10 +01:00
|
|
|
#ifndef CONFIG_DISABLE_ENVIRON
|
2013-07-19 19:43:04 +02:00
|
|
|
void nsh_freefullpath(FAR char *fullpath)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2013-07-19 19:43:04 +02:00
|
|
|
if (fullpath)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2013-07-19 19:43:04 +02:00
|
|
|
free(fullpath);
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_cd
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-11 20:10:10 +01:00
|
|
|
#ifndef CONFIG_DISABLE_ENVIRON
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_CD
|
2022-09-22 05:38:12 +02:00
|
|
|
int cmd_cd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2018-10-01 21:30:25 +02:00
|
|
|
FAR const char *path = argv[1];
|
|
|
|
FAR char *alloc = NULL;
|
|
|
|
FAR char *fullpath = NULL;
|
2011-03-18 20:46:25 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-01 21:30:25 +02:00
|
|
|
/* Set the new working directory */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2011-04-14 00:47:04 +02:00
|
|
|
ret = chdir(path);
|
|
|
|
if (ret != 0)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "chdir", NSH_ERRNO);
|
2011-03-18 20:46:25 +01:00
|
|
|
ret = ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free any memory that was allocated */
|
|
|
|
|
|
|
|
if (alloc)
|
|
|
|
{
|
|
|
|
free(alloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fullpath)
|
|
|
|
{
|
|
|
|
nsh_freefullpath(fullpath);
|
|
|
|
}
|
2014-01-04 15:38:05 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_echo
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_ECHO
|
2022-09-22 05:38:12 +02:00
|
|
|
int cmd_echo(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2021-09-07 05:29:30 +02:00
|
|
|
int newline = 1;
|
|
|
|
int escape = 0;
|
2018-01-10 14:23:28 +01:00
|
|
|
|
2023-07-27 08:42:41 +02:00
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
|
|
|
|
while (argc > 0 && argv[0][0] == '-')
|
2018-01-10 14:23:28 +01:00
|
|
|
{
|
2023-07-27 08:42:41 +02:00
|
|
|
FAR char const *temp = argv[0] + 1;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; temp[i]; i++)
|
2021-09-07 05:29:30 +02:00
|
|
|
{
|
2023-07-27 08:42:41 +02:00
|
|
|
switch (temp[i])
|
|
|
|
{
|
|
|
|
case 'e':
|
|
|
|
case 'E':
|
|
|
|
case 'n':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto do_echo;
|
|
|
|
}
|
2021-09-07 05:29:30 +02:00
|
|
|
}
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2023-07-27 08:42:41 +02:00
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
goto do_echo;
|
|
|
|
}
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2023-07-27 08:42:41 +02:00
|
|
|
while (*temp)
|
2018-01-10 14:23:28 +01:00
|
|
|
{
|
2023-07-27 08:42:41 +02:00
|
|
|
switch (*temp++)
|
|
|
|
{
|
|
|
|
case 'e':
|
|
|
|
escape = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'E':
|
|
|
|
escape = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
newline = 0;
|
|
|
|
break;
|
|
|
|
}
|
2018-01-10 14:23:28 +01:00
|
|
|
}
|
|
|
|
|
2023-07-27 08:42:41 +02:00
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
do_echo:
|
|
|
|
while (argc > 0)
|
|
|
|
{
|
2021-09-07 05:29:30 +02:00
|
|
|
if (escape)
|
|
|
|
{
|
2023-07-27 08:42:41 +02:00
|
|
|
str_escape(argv[0]);
|
2021-09-07 05:29:30 +02:00
|
|
|
}
|
|
|
|
|
2023-07-27 08:42:41 +02:00
|
|
|
nsh_output(vtbl, "%s", argv[0]);
|
|
|
|
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
if (argc > 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, " ");
|
|
|
|
}
|
2018-01-10 14:23:28 +01:00
|
|
|
}
|
|
|
|
|
2021-09-07 05:29:30 +02:00
|
|
|
if (newline)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2018-01-10 14:23:28 +01:00
|
|
|
nsh_output(vtbl, "\n");
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
2014-01-04 15:38:05 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-09-30 18:55:23 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_env
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_DISABLE_ENV
|
2022-09-22 05:38:12 +02:00
|
|
|
int cmd_env(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2018-09-30 18:55:23 +02:00
|
|
|
{
|
2022-08-22 15:17:00 +02:00
|
|
|
UNUSED(argc);
|
|
|
|
|
2018-09-30 18:55:23 +02:00
|
|
|
return nsh_catfile(vtbl, argv[0],
|
|
|
|
CONFIG_NSH_PROC_MOUNTPOINT "/self/group/env");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_pwd
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-11 20:10:10 +01:00
|
|
|
#ifndef CONFIG_DISABLE_ENVIRON
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_PWD
|
2022-09-22 05:38:12 +02:00
|
|
|
int cmd_pwd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2022-08-22 15:17:00 +02:00
|
|
|
UNUSED(argc);
|
|
|
|
UNUSED(argv);
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
nsh_output(vtbl, "%s\n", nsh_getcwd());
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_set
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_SET
|
2022-09-22 05:38:12 +02:00
|
|
|
int cmd_set(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2015-11-23 18:18:32 +01:00
|
|
|
FAR char *value;
|
2017-04-06 02:25:59 +02:00
|
|
|
int ret = OK;
|
2018-10-01 21:30:25 +02:00
|
|
|
#ifdef NSH_HAVE_VARS
|
2017-04-06 02:25:59 +02:00
|
|
|
int ndx = 1;
|
2017-04-09 19:54:59 +02:00
|
|
|
#endif
|
2018-10-01 21:30:25 +02:00
|
|
|
|
2017-04-06 02:25:59 +02:00
|
|
|
#ifndef CONFIG_NSH_DISABLESCRIPT
|
|
|
|
FAR char *popt;
|
|
|
|
const char opts[] = NSH_NP_SET_OPTIONS;
|
|
|
|
int op;
|
2015-11-23 18:18:32 +01:00
|
|
|
|
2018-10-02 19:54:18 +02:00
|
|
|
#ifdef CONFIG_NSH_VARS
|
|
|
|
/* Set with no arguments will show all of the NSH variables */
|
2018-10-01 21:30:25 +02:00
|
|
|
|
2018-10-02 19:54:18 +02:00
|
|
|
if (argc == 1)
|
|
|
|
{
|
|
|
|
ret = nsh_foreach_var(vtbl, nsh_dumpvar, NULL);
|
|
|
|
nsh_output(vtbl, "\n");
|
|
|
|
return ret < 0 ? ERROR : OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2018-10-01 21:30:25 +02:00
|
|
|
#ifdef NSH_HAVE_VARS
|
2017-04-06 02:25:59 +02:00
|
|
|
/* Support set [{+|-}{e|x|xe|ex}] [<name> <value>] */
|
2015-11-23 18:18:32 +01:00
|
|
|
|
2017-04-06 02:25:59 +02:00
|
|
|
if (argc == 2 || argc == 4)
|
2017-04-09 20:03:18 +02:00
|
|
|
#else
|
|
|
|
/* Support set [{+|-}{e|x|xe|ex}] */
|
|
|
|
|
|
|
|
#endif
|
2017-04-06 02:25:59 +02:00
|
|
|
{
|
|
|
|
if (strlen(argv[1]) < 2)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtargrequired, argv[0], "set", NSH_ERRNO);
|
2017-04-06 02:25:59 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
op = argv[1][0];
|
|
|
|
if (op != '-' && op != '+')
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0], "set", NSH_ERRNO);
|
2017-04-06 02:25:59 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
value = &argv[1][1];
|
2020-04-13 05:56:53 +02:00
|
|
|
while (*value && *value != ' ')
|
2017-04-06 02:25:59 +02:00
|
|
|
{
|
|
|
|
popt = strchr(opts, *value++);
|
|
|
|
if (popt == NULL)
|
|
|
|
{
|
2020-04-13 05:56:53 +02:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid,
|
|
|
|
argv[0], "set", NSH_ERRNO);
|
2017-04-06 02:25:59 +02:00
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (op == '+')
|
|
|
|
{
|
2020-04-13 05:56:53 +02:00
|
|
|
vtbl->np.np_flags |= 1 << (popt - opts);
|
2017-04-06 02:25:59 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-13 05:56:53 +02:00
|
|
|
vtbl->np.np_flags &= ~(1 << (popt - opts));
|
2017-04-06 02:25:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 21:30:25 +02:00
|
|
|
#ifdef NSH_HAVE_VARS
|
2017-04-06 02:25:59 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
ndx = 2;
|
|
|
|
}
|
2017-04-09 19:54:59 +02:00
|
|
|
#endif
|
2017-04-06 02:25:59 +02:00
|
|
|
}
|
2020-04-13 05:56:53 +02:00
|
|
|
}
|
|
|
|
}
|
2017-04-09 19:54:59 +02:00
|
|
|
|
2018-10-01 21:30:25 +02:00
|
|
|
#ifdef NSH_HAVE_VARS
|
2017-04-06 02:25:59 +02:00
|
|
|
if (ret == OK && (argc == 3 || argc == 4))
|
|
|
|
#endif
|
2017-04-09 19:54:59 +02:00
|
|
|
#endif /* CONFIG_NSH_DISABLESCRIPT */
|
2018-10-01 21:30:25 +02:00
|
|
|
#ifdef NSH_HAVE_VARS
|
2017-04-06 02:25:59 +02:00
|
|
|
{
|
2018-10-02 00:22:04 +02:00
|
|
|
#if defined(CONFIG_NSH_VARS) && !defined(CONFIG_DISABLE_ENVIRON)
|
|
|
|
FAR char *oldvalue;
|
|
|
|
#endif
|
|
|
|
|
2017-04-06 02:25:59 +02:00
|
|
|
/* Trim whitespace from the value */
|
2015-11-23 18:18:32 +01:00
|
|
|
|
2020-04-13 05:56:53 +02:00
|
|
|
value = nsh_trimspaces(argv[ndx + 1]);
|
2015-11-23 18:18:32 +01:00
|
|
|
|
2018-10-02 00:22:04 +02:00
|
|
|
#ifdef CONFIG_NSH_VARS
|
|
|
|
#ifndef CONFIG_DISABLE_ENVIRON
|
|
|
|
/* Check if the NSH variable has already been promoted to an group-
|
|
|
|
* wide environment variable.
|
2018-10-02 14:53:16 +02:00
|
|
|
*
|
|
|
|
* REVISIT: Is this the correct behavior? Bash would create/modify
|
|
|
|
* a local variable that shadows the environment variable.
|
2018-10-02 00:22:04 +02:00
|
|
|
*/
|
2018-10-01 21:30:25 +02:00
|
|
|
|
2020-04-13 05:56:53 +02:00
|
|
|
oldvalue = getenv(argv[ndx]);
|
|
|
|
if (oldvalue == NULL)
|
2018-10-02 00:22:04 +02:00
|
|
|
#endif
|
2018-10-01 21:30:25 +02:00
|
|
|
{
|
2018-10-02 00:22:04 +02:00
|
|
|
/* Set the NSH variable */
|
2018-10-01 21:30:25 +02:00
|
|
|
|
2018-10-02 00:22:04 +02:00
|
|
|
ret = nsh_setvar(vtbl, argv[ndx], value);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "nsh_setvar",
|
2018-10-02 00:22:04 +02:00
|
|
|
NSH_ERRNO_OF(-ret));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NSH_VARS */
|
2014-01-04 15:38:05 +01:00
|
|
|
|
2018-10-02 00:22:04 +02:00
|
|
|
#if !defined(CONFIG_DISABLE_ENVIRON)
|
|
|
|
#ifdef CONFIG_NSH_VARS
|
|
|
|
else
|
|
|
|
#endif
|
2017-04-06 02:25:59 +02:00
|
|
|
{
|
2018-10-02 00:22:04 +02:00
|
|
|
/* Set the environment variable */
|
|
|
|
|
|
|
|
ret = setenv(argv[ndx], value, TRUE);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "setenv",
|
2018-10-02 00:22:04 +02:00
|
|
|
NSH_ERRNO);
|
|
|
|
}
|
2017-04-06 02:25:59 +02:00
|
|
|
}
|
2018-10-02 00:22:04 +02:00
|
|
|
#endif /* !CONFIG_DISABLE_ENVIRON */
|
2018-10-01 21:30:25 +02:00
|
|
|
}
|
|
|
|
#endif /* NSH_HAVE_VARS */
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2018-10-01 21:30:25 +02:00
|
|
|
#endif /* CONFIG_NSH_DISABLE_SET */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_unset
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_UNSET
|
2022-09-22 05:38:12 +02:00
|
|
|
int cmd_unset(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2022-08-22 15:17:00 +02:00
|
|
|
UNUSED(argc);
|
|
|
|
|
2018-10-02 19:16:15 +02:00
|
|
|
#if defined(CONFIG_NSH_VARS) || !defined(CONFIG_DISABLE_ENVIRON)
|
2018-10-01 21:30:25 +02:00
|
|
|
int status;
|
2018-10-02 19:16:15 +02:00
|
|
|
#endif
|
2018-10-01 21:30:25 +02:00
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
#if defined(CONFIG_NSH_VARS)
|
|
|
|
/* Unset NSH variable */
|
|
|
|
|
|
|
|
status = nsh_unsetvar(vtbl, argv[1]);
|
|
|
|
if (status < 0 && status != -ENOENT)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "nsh_unsetvar",
|
|
|
|
NSH_ERRNO_OF(-status));
|
2018-10-01 21:30:25 +02:00
|
|
|
ret = ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(CONFIG_DISABLE_ENVIRON)
|
|
|
|
/* Unset environment variable */
|
|
|
|
|
|
|
|
status = unsetenv(argv[1]);
|
|
|
|
if (status < 0)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "unsetenv", NSH_ERRNO);
|
2018-10-01 21:30:25 +02:00
|
|
|
ret = ERROR;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
2018-10-01 21:30:25 +02:00
|
|
|
#endif
|
2014-01-04 15:38:05 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
2018-10-01 21:30:25 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_export
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-10-01 23:44:41 +02:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_EXPORT
|
2022-09-22 05:38:12 +02:00
|
|
|
int cmd_export(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2018-10-01 21:30:25 +02:00
|
|
|
{
|
|
|
|
FAR const char *value = "";
|
|
|
|
int status;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Get the value from the command line if provided. argc may be either 2
|
|
|
|
* or 3
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (argc == 3)
|
|
|
|
{
|
|
|
|
value = argv[2];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FAR const char *tmp;
|
|
|
|
|
|
|
|
/* Try to get the value from the NSH variable */
|
|
|
|
|
|
|
|
tmp = nsh_getvar(vtbl, argv[1]);
|
|
|
|
if (tmp != NULL)
|
|
|
|
{
|
|
|
|
value = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the environment variable to the selected value */
|
|
|
|
|
|
|
|
status = setenv(argv[1], value, TRUE);
|
|
|
|
if (status < 0)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "unsetenv", NSH_ERRNO);
|
2018-10-01 21:30:25 +02:00
|
|
|
ret = ERROR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-02 14:53:16 +02:00
|
|
|
/* Unset NSH variable.
|
|
|
|
*
|
|
|
|
* REVISIT: Is this the correct behavior? Bash would retain
|
|
|
|
* a local variable that shadows the environment variable.
|
|
|
|
*/
|
2018-10-01 21:30:25 +02:00
|
|
|
|
|
|
|
status = nsh_unsetvar(vtbl, argv[1]);
|
|
|
|
if (status < 0 && status != -ENOENT)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "nsh_unsetvar",
|
2018-10-01 21:30:25 +02:00
|
|
|
NSH_ERRNO_OF(-status));
|
|
|
|
ret = ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2018-12-09 01:53:54 +01:00
|
|
|
#endif
|