From f6e948fa044f80e8ebddfb8230e9ceb686a714f1 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Tue, 1 Oct 2013 11:52:35 -0600 Subject: [PATCH] More improvements to the minimal NSH when there is no file system and when print fieldwidths are suppressed --- examples/nsh/nsh_main.c | 5 +++++ nshlib/nsh_console.c | 27 +++++++++++++++++++++++---- nshlib/nsh_mmcmds.c | 6 ++++++ nshlib/nsh_parse.c | 4 ++++ 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/examples/nsh/nsh_main.c b/examples/nsh/nsh_main.c index 7bd34615e..a6ce3442b 100644 --- a/examples/nsh/nsh_main.c +++ b/examples/nsh/nsh_main.c @@ -174,7 +174,12 @@ int nsh_main(int argc, char *argv[]) * is wrong. */ +#if CONFIG_NFILE_DESCRIPTORS > 0 fprintf(stderr, "ERROR: nsh_consolemain() returned: %d\n", ret); +#else + printf("ERROR: nsh_consolemain() returned: %d\n", ret); +#endif + exitval = 1; #endif diff --git a/nshlib/nsh_console.c b/nshlib/nsh_console.c index 410abc19d..41e992678 100644 --- a/nshlib/nsh_console.c +++ b/nshlib/nsh_console.c @@ -198,6 +198,8 @@ static ssize_t nsh_consolewrite(FAR struct nsh_vtbl_s *vtbl, FAR const void *buf } return ret; #else + /* REVISIT: buffer may not be NUL-terminated */ + printf("%s", buffer); return nbytes; #endif @@ -234,13 +236,30 @@ static int nsh_consoleoutput(FAR struct nsh_vtbl_s *vtbl, const char *fmt, ...) return ret; #else - char dest[64 * 16]; + va_list ap; + char *str; + int ret; + + /* Use avsprintf() to allocate a buffer and fill it with the formatted + * data + */ va_start(ap, fmt); - vsprintf(dest, fmt, ap); - va_end(ap); + str = NULL; + (void)avsprintf(&str, fmt, ap); - return printf(dest); + /* Was a string allocated? */ + + if (str) + { + /* Yes.. Print then free the allocated string */ + + printf("%s", str); + free(str); + } + + va_end(ap); + return 0; #endif } diff --git a/nshlib/nsh_mmcmds.c b/nshlib/nsh_mmcmds.c index 545ae60ad..58209ee7c 100644 --- a/nshlib/nsh_mmcmds.c +++ b/nshlib/nsh_mmcmds.c @@ -87,9 +87,15 @@ int cmd_free(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) (void)mallinfo(&mem); #endif +#ifdef CONFIG_NOPRINTF_FIELDWIDTH + nsh_output(vtbl, "\ttotal\tused\tfree\tlargest\n"); + nsh_output(vtbl, "Mem:\t%d\t%d\t%d\t%d\n", + mem.arena, mem.uordblks, mem.fordblks, mem.mxordblk); +#else nsh_output(vtbl, " total used free largest\n"); nsh_output(vtbl, "Mem: %11d%11d%11d%11d\n", mem.arena, mem.uordblks, mem.fordblks, mem.mxordblk); +#endif return OK; } diff --git a/nshlib/nsh_parse.c b/nshlib/nsh_parse.c index d449bf61e..68dca6c08 100644 --- a/nshlib/nsh_parse.c +++ b/nshlib/nsh_parse.c @@ -513,7 +513,11 @@ static inline void help_cmdlist(FAR struct nsh_vtbl_s *vtbl) nsh_output(vtbl, " "); for (j = 0, k = i; j < CMDS_PER_LINE && k < NUM_CMDS; j++, k += NUM_CMD_ROWS) { +#ifdef CONFIG_NOPRINTF_FIELDWIDTH + nsh_output(vtbl, "%s\t", g_cmdmap[k].cmd); +#else nsh_output(vtbl, "%-12s", g_cmdmap[k].cmd); +#endif } nsh_output(vtbl, "\n");