2013-01-29 23:11:04 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/nshlib/nsh_script.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007-2009, 2011-2013 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* 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 "nsh.h"
|
|
|
|
#include "nsh_console.h"
|
|
|
|
|
2019-02-11 20:10:10 +01:00
|
|
|
#if CONFIG_NFILE_STREAMS > 0 && !defined(CONFIG_NSH_DISABLESCRIPT)
|
2013-01-29 23:11:04 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_script
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Execute the NSH script at path.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nsh_script(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
|
|
|
|
FAR const char *path)
|
|
|
|
{
|
2014-01-17 20:24:44 +01:00
|
|
|
FAR char *fullpath;
|
|
|
|
FAR FILE *savestream;
|
|
|
|
FAR char *buffer;
|
|
|
|
FAR char *pret;
|
2013-01-29 23:11:04 +01:00
|
|
|
int ret = ERROR;
|
|
|
|
|
|
|
|
/* The path to the script may be relative to the current working directory */
|
|
|
|
|
|
|
|
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 */
|
|
|
|
|
|
|
|
savestream = vtbl->np.np_stream;
|
|
|
|
|
2013-01-29 23:11:04 +01:00
|
|
|
/* Open the file containing the script */
|
|
|
|
|
2014-01-17 20:24:44 +01:00
|
|
|
vtbl->np.np_stream = fopen(fullpath, "r");
|
|
|
|
if (!vtbl->np.np_stream)
|
2013-01-29 23:11:04 +01:00
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, cmd, "fopen", NSH_ERRNO);
|
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 */
|
|
|
|
|
|
|
|
vtbl->np.np_stream = 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
|
|
|
/* Flush any output generated by the previous line */
|
2013-01-29 23:11:04 +01:00
|
|
|
|
|
|
|
fflush(stdout);
|
2014-01-17 22:56:32 +01:00
|
|
|
|
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
|
|
|
|
* script file. Note that ftell will return -1 on failure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
vtbl->np.np_foffs = ftell(vtbl->np.np_stream);
|
|
|
|
vtbl->np.np_loffs = 0;
|
|
|
|
|
|
|
|
if (vtbl->np.np_foffs < 0)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, "loop", "ftell", 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 */
|
|
|
|
|
2014-01-17 20:24:44 +01:00
|
|
|
pret = fgets(buffer, CONFIG_NSH_LINELEN, vtbl->np.np_stream);
|
2013-01-29 23:11:04 +01:00
|
|
|
if (pret)
|
|
|
|
{
|
|
|
|
/* 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
|
|
|
{
|
|
|
|
nsh_output(vtbl,"%s", buffer);
|
|
|
|
}
|
|
|
|
|
2013-01-29 23:11:04 +01:00
|
|
|
ret = nsh_parse(vtbl, buffer);
|
|
|
|
}
|
|
|
|
}
|
2017-04-06 02:31:47 +02:00
|
|
|
while (pret && (ret == OK || (vtbl->np.np_flags & NSH_PFLAG_IGNORE)));
|
2014-01-17 20:24:44 +01:00
|
|
|
|
|
|
|
/* Close the script file */
|
|
|
|
|
|
|
|
fclose(vtbl->np.np_stream);
|
|
|
|
|
|
|
|
/* Restore the parent script stream */
|
|
|
|
|
|
|
|
vtbl->np.np_stream = 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NSH_ROMFSETC
|
|
|
|
int nsh_initscript(FAR struct nsh_vtbl_s *vtbl)
|
|
|
|
{
|
|
|
|
static bool initialized;
|
|
|
|
bool already;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Atomic test and set of the initialized flag */
|
|
|
|
|
|
|
|
sched_lock();
|
|
|
|
already = initialized;
|
|
|
|
initialized = true;
|
|
|
|
sched_unlock();
|
|
|
|
|
|
|
|
/* If we have not already executed the init script, then do so now */
|
|
|
|
|
|
|
|
if (!already)
|
|
|
|
{
|
|
|
|
ret = nsh_script(vtbl, "init", NSH_INITPATH);
|
2020-04-16 20:24:04 +02:00
|
|
|
|
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
|
|
|
|
|
2020-04-16 20:24:04 +02:00
|
|
|
#if defined(CONFIG_NSH_ARCHINIT) && defined(CONFIG_BOARDCTL_FINALINIT)
|
|
|
|
/* Perform architecture-specific final-initialization (if configured) */
|
|
|
|
|
|
|
|
boardctl(BOARDIOC_FINALINIT, 0);
|
|
|
|
#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)
|
|
|
|
{
|
|
|
|
return nsh_script(vtbl, "login", NSH_RCPATH);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /* CONFIG_NSH_ROMFSETC */
|
|
|
|
|
2019-02-11 20:10:10 +01:00
|
|
|
#endif /* CONFIG_NFILE_STREAMS > 0 && !CONFIG_NSH_DISABLESCRIPT */
|