Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apps/nshlib: Using lib_get_tempbuffer() to save stack space #65

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/lp503x/lp503x_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ static int lp503x_cmd_help(FAR char *parg)
int main(int argc, FAR char *argv[])
{
bool running;
char buffer[CONFIG_NSH_LINELEN];
char buffer[LINE_MAX];
int len;
int x;
char *cmd;
Expand Down
2 changes: 1 addition & 1 deletion netutils/rexec/rexec.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ static int do_rexec(FAR struct rexec_arg_s *arg)

int main(int argc, FAR char **argv)
{
char cmd[CONFIG_NSH_LINELEN];
char cmd[LINE_MAX];
struct rexec_arg_s arg;
int option;
int i;
Expand Down
6 changes: 0 additions & 6 deletions nshlib/nsh.h
Original file line number Diff line number Diff line change
Expand Up @@ -377,12 +377,6 @@
# define NSH_HERRNO_OF(err) (err)
#endif

/* Maximum size of one command line (telnet or serial) */

#ifndef CONFIG_NSH_LINELEN
# define CONFIG_NSH_LINELEN 80
#endif

/* The maximum number of nested if-then[-else]-fi sequences that
* are permissible.
*/
Expand Down
2 changes: 1 addition & 1 deletion nshlib/nsh_console.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ struct console_stdio_s

/* Line input buffer */

char cn_line[CONFIG_NSH_LINELEN];
char cn_line[LINE_MAX];
};

/****************************************************************************
Expand Down
5 changes: 2 additions & 3 deletions nshlib/nsh_login.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ int nsh_login(FAR struct console_stdio_s *pstate)

/* readline() returns EOF on failure */

ret = readline_fd(pstate->cn_line, CONFIG_NSH_LINELEN,
ret = readline_fd(pstate->cn_line, LINE_MAX,
INFD(pstate), OUTFD(pstate));
if (ret != EOF)
{
Expand Down Expand Up @@ -207,8 +207,7 @@ int nsh_login(FAR struct console_stdio_s *pstate)
}

password[0] = '\0';
ret = readline_fd(pstate->cn_line, CONFIG_NSH_LINELEN,
INFD(pstate), -1);
ret = readline_fd(pstate->cn_line, LINE_MAX, INFD(pstate), -1);

/* Enable echo again after password */

Expand Down
8 changes: 4 additions & 4 deletions nshlib/nsh_mmcmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ int cmd_free(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)

int cmd_memdump(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
{
char arg[CONFIG_NSH_LINELEN] = "";
char arg[LINE_MAX] = "";
int i;

if (argc == 1)
{
strlcpy(arg, "used", CONFIG_NSH_LINELEN);
strlcpy(arg, "used", LINE_MAX);
}
else if (argc >= 2 && (strcmp(argv[1], "-h") == 0 ||
strcmp(argv[1], "help") == 0))
Expand All @@ -73,10 +73,10 @@ int cmd_memdump(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
{
for (i = 1; i < argc; i++)
{
strlcat(arg, argv[i], CONFIG_NSH_LINELEN);
strlcat(arg, argv[i], LINE_MAX);
if (i < argc - 1)
{
strlcat(arg, " ", CONFIG_NSH_LINELEN);
strlcat(arg, " ", LINE_MAX);
}
}
}
Expand Down
64 changes: 45 additions & 19 deletions nshlib/nsh_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
int argc, FAR char *argv[],
FAR const struct nsh_param_s *param)
{
int fd_out = STDOUT_FILENO;
int fd_in = STDIN_FILENO;
int ret;

/* DO NOT CHANGE THE ORDERING OF THE FOLLOWING STEPS
Expand Down Expand Up @@ -603,15 +605,23 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
{
FAR char *sh_argv[4];
FAR char *sh_cmd = "sh";
char sh_arg2[CONFIG_NSH_LINELEN];
FAR char *sh_arg2;

DEBUGASSERT(strncmp(argv[0], sh_cmd, 3) != 0);

sh_arg2 = lib_get_tempbuffer(LINE_MAX);
if (sh_arg2 == NULL)
{
nsh_error(vtbl, g_fmtcmdoutofmemory, sh_cmd);
ret = -errno;
goto close_redir;
}

sh_arg2[0] = '\0';

for (ret = 0; ret < argc; ret++)
{
strlcat(sh_arg2, argv[ret], sizeof(sh_arg2));
strlcat(sh_arg2, argv[ret], LINE_MAX);

if (ret < argc - 1)
{
Expand All @@ -628,16 +638,15 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
* dispatch the backgroud by sh -c ""
*/

return nsh_execute(vtbl, 4, sh_argv, param);
ret = nsh_execute(vtbl, 4, sh_argv, param);
lib_put_tempbuffer(sh_arg2);
return ret;
}
else
#endif
{
uint8_t save[SAVE_SIZE];

int fd_in = STDIN_FILENO;
int fd_out = STDOUT_FILENO;

/* Redirected output? */

if (vtbl->np.np_redir_out)
Expand All @@ -655,7 +664,8 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
{
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "open",
NSH_ERRNO);
return nsh_saveresult(vtbl, true);
ret = errno;
goto close_redir;
}
}
else
Expand All @@ -681,7 +691,8 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
{
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "open",
NSH_ERRNO);
return nsh_saveresult(vtbl, true);
ret = errno;
goto close_redir;
}
}
else
Expand Down Expand Up @@ -714,22 +725,27 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
{
nsh_undirect(vtbl, save);
}
}

/* Mark errors so that it is possible to test for non-zero return
* values in nsh scripts.
*/
close_redir:

if (ret < 0)
{
return nsh_saveresult(vtbl, true);
}
/* Closing fds opened for redirection if necessary */

if (fd_out > STDOUT_FILENO)
{
close(fd_out);
}

if (fd_in > STDIN_FILENO)
{
close(fd_in);
}

/* Return success if the command succeeded (or at least, starting of the
* command task succeeded).
*/

return nsh_saveresult(vtbl, false);
return nsh_saveresult(vtbl, ret != OK);
}

/****************************************************************************
Expand Down Expand Up @@ -2454,7 +2470,7 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline)
#endif

#ifdef CONFIG_SCHED_INSTRUMENTATION_DUMP
char tracebuf[CONFIG_NSH_LINELEN + 1];
char tracebuf[LINE_MAX + 1];

strlcpy(tracebuf, cmdline, sizeof(tracebuf));
sched_note_beginex(NOTE_TAG_APP, tracebuf);
Expand Down Expand Up @@ -2677,7 +2693,7 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline)
{
FAR char *arg;
FAR char *sh_argv[4];
char sh_arg2[CONFIG_NSH_LINELEN];
FAR char *sh_arg2;

if (argv[argc][g_pipeline1_len])
{
Expand All @@ -2695,11 +2711,19 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline)
goto dynlist_free;
}

sh_arg2 = lib_get_tempbuffer(LINE_MAX);
if (sh_arg2 == NULL)
{
nsh_error(vtbl, g_fmtcmdoutofmemory, cmd);
ret = -errno;
goto dynlist_free;
}

sh_arg2[0] = '\0';

for (ret = 0; ret < argc; ret++)
{
strlcat(sh_arg2, argv[ret], sizeof(sh_arg2));
strlcat(sh_arg2, argv[ret], LINE_MAX);

if (ret < argc - 1)
{
Expand All @@ -2715,6 +2739,7 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline)
ret = pipe2(pipefd, 0);
if (ret < 0)
{
lib_put_tempbuffer(sh_arg2);
ret = -errno;
goto dynlist_free;
}
Expand All @@ -2727,6 +2752,7 @@ static int nsh_parse_command(FAR struct nsh_vtbl_s *vtbl, FAR char *cmdline)
vtbl->np.np_bg = true;

ret = nsh_execute(vtbl, 4, sh_argv, &param);
lib_put_tempbuffer(sh_arg2);

vtbl->np.np_bg = bg_save;

Expand Down
2 changes: 1 addition & 1 deletion nshlib/nsh_script.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ int nsh_script(FAR struct nsh_vtbl_s *vtbl, FAR const FAR char *cmd,

/* Now read the next line from the script file */

ret = readline_fd(buffer, CONFIG_NSH_LINELEN, vtbl->np.np_fd, -1);
ret = readline_fd(buffer, LINE_MAX, vtbl->np.np_fd, -1);
if (ret >= 0)
{
/* Parse process the command. NOTE: this is recursive...
Expand Down
4 changes: 2 additions & 2 deletions nshlib/nsh_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ int nsh_session(FAR struct console_stdio_s *pstate,
* occurs. Either will cause the session to terminate.
*/

ret = cle_fd(pstate->cn_line, nsh_prompt(), CONFIG_NSH_LINELEN,
ret = cle_fd(pstate->cn_line, nsh_prompt(), LINE_MAX,
INFD(pstate), OUTFD(pstate));
if (ret == EOF)
{
Expand All @@ -226,7 +226,7 @@ int nsh_session(FAR struct console_stdio_s *pstate,
* will cause the session to terminate.
*/

ret = readline_fd(pstate->cn_line, CONFIG_NSH_LINELEN,
ret = readline_fd(pstate->cn_line, LINE_MAX,
INFD(pstate), OUTFD(pstate));
if (ret == EOF)
{
Expand Down
4 changes: 2 additions & 2 deletions nshlib/nsh_telnetlogin.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ int nsh_telnetlogin(FAR struct console_stdio_s *pstate)
write(OUTFD(pstate), g_userprompt, strlen(g_userprompt));

username[0] = '\0';
if (readline_fd(pstate->cn_line, CONFIG_NSH_LINELEN,
if (readline_fd(pstate->cn_line, LINE_MAX,
INFD(pstate), OUTFD(pstate)) >= 0)

{
Expand Down Expand Up @@ -212,7 +212,7 @@ int nsh_telnetlogin(FAR struct console_stdio_s *pstate)
}

password[0] = '\0';
ret = readline_fd(pstate->cn_line, CONFIG_NSH_LINELEN,
ret = readline_fd(pstate->cn_line, LINE_MAX,
INFD(pstate), OUTFD(pstate));

/* Enable echo again after password */
Expand Down
4 changes: 2 additions & 2 deletions nshlib/nsh_timcmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ int cmd_timedatectl(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
#ifndef CONFIG_NSH_DISABLE_WATCH
int cmd_watch(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
{
char buffer[CONFIG_NSH_LINELEN];
char buffer[LINE_MAX];
int interval = 2;
int count = -1;
FAR char *cmd;
Expand Down Expand Up @@ -593,7 +593,7 @@ int cmd_watch(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)

for (i = 0; i < count; i++)
{
strlcpy(buffer, cmd, CONFIG_NSH_LINELEN);
strlcpy(buffer, cmd, LINE_MAX);
ret = nsh_parse(vtbl, buffer);
if (ret < 0)
{
Expand Down
2 changes: 1 addition & 1 deletion system/nxcamera/nxcamera_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ static int nxcamera_cmd_help(FAR struct nxcamera_s *pcam, FAR char *parg)

int main(int argc, FAR char *argv[])
{
char buffer[CONFIG_NSH_LINELEN];
char buffer[LINE_MAX];
int len;
int x;
bool running = true;
Expand Down
2 changes: 1 addition & 1 deletion system/nxlooper/nxlooper_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ static int nxlooper_cmd_help(FAR struct nxlooper_s *plooper, char *parg)

int main(int argc, FAR char *argv[])
{
char buffer[CONFIG_NSH_LINELEN];
char buffer[LINE_MAX];
int len;
int x;
int running;
Expand Down
2 changes: 1 addition & 1 deletion system/nxplayer/nxplayer_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ static int nxplayer_cmd_help(FAR struct nxplayer_s *pplayer, char *parg)

int main(int argc, FAR char *argv[])
{
char buffer[CONFIG_NSH_LINELEN];
char buffer[LINE_MAX];
int len;
int x;
int running;
Expand Down
2 changes: 1 addition & 1 deletion system/nxrecorder/nxrecorder_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ static int nxrecorder_cmd_help(FAR struct nxrecorder_s *precorder,

int main(int argc, FAR char *argv[])
{
char buffer[CONFIG_NSH_LINELEN];
char buffer[LINE_MAX];
int len;
int x;
int running;
Expand Down
14 changes: 2 additions & 12 deletions system/readline/readline.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,6 @@

#include "system/readline.h"

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

/* Maximum size of one command line (telnet or serial) */

#ifndef CONFIG_NSH_LINELEN
# define CONFIG_NSH_LINELEN 80
#endif

/****************************************************************************
* Public Functions
****************************************************************************/
Expand All @@ -57,14 +47,14 @@

FAR char *readline(FAR const char *prompt)
{
FAR char *line = malloc(CONFIG_NSH_LINELEN);
FAR char *line = malloc(LINE_MAX);

if (line != NULL)
{
#ifdef CONFIG_READLINE_TABCOMPLETION
FAR const char *orig = readline_prompt(prompt);
#endif
if (readline_fd(line, CONFIG_NSH_LINELEN,
if (readline_fd(line, LINE_MAX,
STDIN_FILENO, STDOUT_FILENO) == 0)
{
free(line);
Expand Down
Loading
Loading