2013-01-29 23:11:04 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/nshlib/nsh_script.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
|
2013-01-29 23:11:04 +01:00
|
|
|
*
|
2021-06-10 16:34:12 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-01-29 23:11:04 +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.
|
2013-01-29 23:11:04 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2023-02-04 14:56:06 +01:00
|
|
|
|
2022-06-07 11:10:24 +02:00
|
|
|
#include <fcntl.h>
|
2023-03-06 15:26:11 +01:00
|
|
|
#include <sched.h>
|
2023-02-04 14:56:06 +01:00
|
|
|
#include <unistd.h>
|
2013-01-29 23:11:04 +01:00
|
|
|
|
|
|
|
#include "nsh.h"
|
|
|
|
#include "nsh_console.h"
|
|
|
|
|
2023-02-10 10:50:46 +01:00
|
|
|
#include <system/readline.h>
|
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_DISABLESCRIPT
|
2013-01-29 23:11:04 +01:00
|
|
|
|
2024-09-02 07:29:37 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_ETC_ROMFS
|
|
|
|
static bool g_nsh_script_initialized;
|
|
|
|
#endif
|
|
|
|
|
2022-06-07 11:10:24 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-11-26 05:10:46 +01:00
|
|
|
#if defined(CONFIG_ETC_ROMFS) || defined(CONFIG_NSH_ROMFSRC)
|
2022-06-07 11:10:24 +02:00
|
|
|
static int nsh_script_redirect(FAR struct nsh_vtbl_s *vtbl,
|
|
|
|
FAR const char *cmd,
|
2022-12-28 10:59:08 +01:00
|
|
|
FAR const char *path,
|
|
|
|
bool log)
|
2022-06-07 11:10:24 +02:00
|
|
|
{
|
|
|
|
uint8_t save[SAVE_SIZE];
|
|
|
|
int fd = -1;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (CONFIG_NSH_SCRIPT_REDIRECT_PATH[0])
|
|
|
|
{
|
|
|
|
fd = open(CONFIG_NSH_SCRIPT_REDIRECT_PATH, 0666);
|
|
|
|
if (fd > 0)
|
|
|
|
{
|
2024-08-06 23:29:50 +02:00
|
|
|
nsh_redirect(vtbl, 0, fd, save);
|
2022-06-07 11:10:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-28 10:59:08 +01:00
|
|
|
ret = nsh_script(vtbl, cmd, path, log);
|
2022-06-07 11:10:24 +02:00
|
|
|
if (CONFIG_NSH_SCRIPT_REDIRECT_PATH[0])
|
|
|
|
{
|
|
|
|
if (fd > 0)
|
|
|
|
{
|
|
|
|
nsh_undirect(vtbl, save);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-01-29 23:11:04 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_script
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Execute the NSH script at path.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-09-22 05:38:12 +02:00
|
|
|
int nsh_script(FAR struct nsh_vtbl_s *vtbl, FAR const FAR char *cmd,
|
2022-12-28 10:59:08 +01:00
|
|
|
FAR const char *path, bool log)
|
2013-01-29 23:11:04 +01:00
|
|
|
{
|
2014-01-17 20:24:44 +01:00
|
|
|
FAR char *fullpath;
|
2023-02-10 10:50:46 +01:00
|
|
|
int savestream;
|
2014-01-17 20:24:44 +01:00
|
|
|
FAR char *buffer;
|
2013-01-29 23:11:04 +01:00
|
|
|
int ret = ERROR;
|
|
|
|
|
2020-04-17 05:45:51 +02:00
|
|
|
/* The path to the script may relative to the current working directory */
|
2013-01-29 23:11:04 +01:00
|
|
|
|
|
|
|
fullpath = nsh_getfullpath(vtbl, path);
|
|
|
|
if (!fullpath)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a reference to the common input buffer */
|
|
|
|
|
|
|
|
buffer = nsh_linebuffer(vtbl);
|
|
|
|
if (buffer)
|
|
|
|
{
|
2014-01-17 20:24:44 +01:00
|
|
|
/* Save the parent stream in case of nested script processing */
|
|
|
|
|
2023-02-10 10:50:46 +01:00
|
|
|
savestream = vtbl->np.np_fd;
|
2014-01-17 20:24:44 +01:00
|
|
|
|
2013-01-29 23:11:04 +01:00
|
|
|
/* Open the file containing the script */
|
|
|
|
|
2023-10-25 14:44:15 +02:00
|
|
|
vtbl->np.np_fd = open(fullpath, O_RDOK | O_CLOEXEC);
|
2023-02-10 10:50:46 +01:00
|
|
|
if (vtbl->np.np_fd < 0)
|
2013-01-29 23:11:04 +01:00
|
|
|
{
|
2022-12-28 10:59:08 +01:00
|
|
|
if (log)
|
|
|
|
{
|
2023-02-21 23:08:13 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, cmd, "open", NSH_ERRNO);
|
2022-12-28 10:59:08 +01:00
|
|
|
}
|
2014-01-17 20:24:44 +01:00
|
|
|
|
|
|
|
/* Free the allocated path */
|
|
|
|
|
2013-01-29 23:11:04 +01:00
|
|
|
nsh_freefullpath(fullpath);
|
2014-01-17 20:24:44 +01:00
|
|
|
|
|
|
|
/* Restore the parent script stream */
|
|
|
|
|
2023-02-10 10:50:46 +01:00
|
|
|
vtbl->np.np_fd = savestream;
|
2013-01-29 23:11:04 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop, processing each command line in the script file (or
|
|
|
|
* until an error occurs)
|
|
|
|
*/
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2014-01-18 16:39:16 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_LOOPS
|
2014-01-17 22:56:32 +01:00
|
|
|
/* Get the current file position. This is used to control
|
|
|
|
* looping. If a loop begins in the next line, then this file
|
|
|
|
* offset will be needed to locate the top of the loop in the
|
2023-02-21 23:08:13 +01:00
|
|
|
* script file. Note that lseek will return -1 on failure.
|
2014-01-17 22:56:32 +01:00
|
|
|
*/
|
|
|
|
|
2023-02-10 10:50:46 +01:00
|
|
|
vtbl->np.np_foffs = lseek(vtbl->np.np_fd, 0, SEEK_CUR);
|
2014-01-17 22:56:32 +01:00
|
|
|
vtbl->np.np_loffs = 0;
|
|
|
|
|
2022-12-28 10:59:08 +01:00
|
|
|
if (vtbl->np.np_foffs < 0 && log)
|
2014-01-17 22:56:32 +01:00
|
|
|
{
|
2023-02-21 23:08:13 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, "loop", "lseek", NSH_ERRNO);
|
2014-01-17 22:56:32 +01:00
|
|
|
}
|
2014-01-18 16:39:16 +01:00
|
|
|
#endif
|
2014-01-17 22:56:32 +01:00
|
|
|
|
|
|
|
/* Now read the next line from the script file */
|
|
|
|
|
2023-02-10 10:50:46 +01:00
|
|
|
ret = readline_fd(buffer, CONFIG_NSH_LINELEN, vtbl->np.np_fd, -1);
|
|
|
|
if (ret >= 0)
|
2013-01-29 23:11:04 +01:00
|
|
|
{
|
|
|
|
/* Parse process the command. NOTE: this is recursive...
|
2020-04-18 15:57:26 +02:00
|
|
|
* we got to cmd_source via a call to nsh_parse. So some
|
2013-01-29 23:11:04 +01:00
|
|
|
* considerable amount of stack may be used.
|
|
|
|
*/
|
|
|
|
|
2017-04-06 02:31:47 +02:00
|
|
|
if ((vtbl->np.np_flags & NSH_PFLAG_SILENT) == 0)
|
2017-04-06 02:25:59 +02:00
|
|
|
{
|
2020-04-17 05:45:51 +02:00
|
|
|
nsh_output(vtbl, "%s", buffer);
|
2017-04-06 02:25:59 +02:00
|
|
|
}
|
|
|
|
|
2023-03-01 10:04:49 +01:00
|
|
|
if (vtbl->np.np_flags & NSH_PFLAG_IGNORE)
|
|
|
|
{
|
|
|
|
nsh_parse(vtbl, buffer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = nsh_parse(vtbl, buffer);
|
|
|
|
}
|
2013-01-29 23:11:04 +01:00
|
|
|
}
|
|
|
|
}
|
2023-03-01 10:04:49 +01:00
|
|
|
while (ret >= 0);
|
2014-01-17 20:24:44 +01:00
|
|
|
|
|
|
|
/* Close the script file */
|
|
|
|
|
2023-02-10 10:50:46 +01:00
|
|
|
close(vtbl->np.np_fd);
|
2014-01-17 20:24:44 +01:00
|
|
|
|
|
|
|
/* Restore the parent script stream */
|
|
|
|
|
2023-02-10 10:50:46 +01:00
|
|
|
vtbl->np.np_fd = savestream;
|
2013-01-29 23:11:04 +01:00
|
|
|
}
|
|
|
|
|
2014-01-17 20:24:44 +01:00
|
|
|
/* Free the allocated path */
|
|
|
|
|
2013-01-29 23:11:04 +01:00
|
|
|
nsh_freefullpath(fullpath);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-04-15 15:59:24 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_sysinitscript
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Attempt to execute the configured system initialization script. This
|
|
|
|
* script should be executed once when NSH starts.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-11-26 05:10:46 +01:00
|
|
|
#ifdef CONFIG_ETC_ROMFS
|
2022-04-15 15:59:24 +02:00
|
|
|
int nsh_sysinitscript(FAR struct nsh_vtbl_s *vtbl)
|
|
|
|
{
|
2022-12-28 10:59:08 +01:00
|
|
|
/* Since most existing systems only use the NSH_INITPATH script file.
|
|
|
|
* Do not log error output for a missing NSH_SYSINITPATH script file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return nsh_script_redirect(vtbl, "sysinit", NSH_SYSINITPATH, false);
|
2022-04-15 15:59:24 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-01-29 23:11:04 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_initscript
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Attempt to execute the configured initialization script. This script
|
|
|
|
* should be executed once when NSH starts. nsh_initscript is idempotent
|
|
|
|
* and may, however, be called multiple times (the script will be executed
|
|
|
|
* once.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-11-26 05:10:46 +01:00
|
|
|
#ifdef CONFIG_ETC_ROMFS
|
2013-01-29 23:11:04 +01:00
|
|
|
int nsh_initscript(FAR struct nsh_vtbl_s *vtbl)
|
|
|
|
{
|
|
|
|
bool already;
|
|
|
|
int ret = OK;
|
|
|
|
|
2024-09-02 07:29:37 +02:00
|
|
|
/* Atomic test and set of the g_nsh_script_initialized flag */
|
2013-01-29 23:11:04 +01:00
|
|
|
|
|
|
|
sched_lock();
|
2024-09-02 07:29:37 +02:00
|
|
|
already = g_nsh_script_initialized;
|
|
|
|
g_nsh_script_initialized = true;
|
2013-01-29 23:11:04 +01:00
|
|
|
sched_unlock();
|
|
|
|
|
|
|
|
/* If we have not already executed the init script, then do so now */
|
|
|
|
|
|
|
|
if (!already)
|
|
|
|
{
|
2022-12-28 10:59:08 +01:00
|
|
|
ret = nsh_script_redirect(vtbl, "init", NSH_INITPATH, true);
|
2020-04-17 07:55:15 +02:00
|
|
|
#ifndef CONFIG_NSH_DISABLESCRIPT
|
|
|
|
/* Reset the option flags */
|
|
|
|
|
|
|
|
vtbl->np.np_flags = NSH_NP_SET_OPTIONS_INIT;
|
|
|
|
#endif
|
2013-01-29 23:11:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_loginscript
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Attempt to execute the configured login script. This script
|
|
|
|
* should be executed when each NSH session starts.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NSH_ROMFSRC
|
|
|
|
int nsh_loginscript(FAR struct nsh_vtbl_s *vtbl)
|
|
|
|
{
|
2022-12-28 10:59:08 +01:00
|
|
|
return nsh_script_redirect(vtbl, "login", NSH_RCPATH, true);
|
2013-01-29 23:11:04 +01:00
|
|
|
}
|
|
|
|
#endif
|
2023-11-26 05:10:46 +01:00
|
|
|
#endif /* CONFIG_ETC_ROMFS */
|
2013-01-29 23:11:04 +01:00
|
|
|
|
2023-02-10 10:50:46 +01:00
|
|
|
#endif /* CONFIG_NSH_DISABLESCRIPT */
|