]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - avr/cli.c
some cleanup, more debugging
[z180-stamp.git] / avr / cli.c
index 77876afb9687a4e9b53b522c26462cb18c6471a3..b96ca1bcf9ba8dfc247a47724307967283977ad8 100644 (file)
--- a/avr/cli.c
+++ b/avr/cli.c
  */
 
 #include "cli.h"
-#include "common.h"
-
-#include <string.h>
+#include "command.h"
 #include <ctype.h>
-#include <stdlib.h>
-#include <stdio.h>
 
 #include "config.h"
-#include "command.h"
-#include "xmalloc.h"
 #include "debug.h"
 #include "env.h"
 #include "cli_readline.h"
 #define debug_parser(fmt, args...)             \
        debug_cond(DEBUG_PARSER, fmt, ##args)
 
+
+static bool opt_xtrace;
+static bool opt_verbose;
+static int_least8_t command_level;
+
+static void cli_trace_cmd(int_fast8_t level, int argc, char *argv[])
+{
+       while (level-- > 0)
+               putchar('+');
+       for (int_fast8_t i = 0; i < argc; i++)
+               printf_P(PSTR(" %s"), argv[i]);
+       putchar('\n');
+}
+
+
+
+
 static int cli_parse_line(char *line, char *argv[])
 {
        uint_fast8_t state = 0;
@@ -139,7 +150,12 @@ char *process_macros(char *input, char *output)
                        outp = output;
                } else {
                        int outputlen = outp - output;
-                       outp = xrealloc(output, outputlen);
+                       outp = realloc(output, outputlen);
+                       if (outp == NULL) {
+                               free(output);
+                               output = outp;
+                               break;
+                       }
                        output = outp;
                }
 
@@ -220,7 +236,7 @@ char *process_macros(char *input, char *output)
  * @returns
  *
  */
-static int cli_run_command(const char *cmd, int flag)
+static int cli_run_command(const char *cmd, uint_fast8_t flag)
 {
        char *cmdbuf;                   /* working copy of cmd          */
        char *token;                    /* start of token in cmdbuf     */
@@ -232,9 +248,20 @@ static int cli_run_command(const char *cmd, int flag)
        uint_fast8_t inquotes, repeatable = 1;
        int rc = 0;
 
+       opt_verbose = 0;
+       opt_xtrace = 0;
+       char *optenv = getenv_str(PSTR("cli"));
+       if (optenv) {
+               opt_verbose = (strstr_P(optenv, PSTR("verbose")) != NULL);
+               opt_xtrace = (strstr_P(optenv, PSTR("xtrace")) != NULL);
+       }
+
        debug_parser("[RUN_COMMAND] cmd[%p]=\"%s\"\n",
                        cmd, cmd ? cmd : "NULL");
 
+       if (opt_verbose)
+               printf_P(PSTR("%s\n"), cmd, cmd ? cmd : "");
+
        clear_ctrlc();          /* forget any previous Control C */
 
        if (!cmd || !*cmd)
@@ -244,6 +271,7 @@ static int cli_run_command(const char *cmd, int flag)
        if (!cmdbuf)
                return -1;      /* not enough memory */
 
+       ++command_level;
        str = cmdbuf;
 
        /* Process separators and check for invalid
@@ -286,6 +314,10 @@ static int cli_run_command(const char *cmd, int flag)
 
                /* find macros in this token and replace them */
                finaltoken = process_macros(token, finaltoken);
+               if (finaltoken == NULL) {
+                       rc = -1;        /* no command at all */
+                       break;
+               }
 
                /* Extract arguments */
                argc = cli_parse_line(finaltoken, argv);
@@ -294,8 +326,15 @@ static int cli_run_command(const char *cmd, int flag)
                        continue;
                }
 
-               if (cmd_process(flag, argc, argv, &repeatable) != CMD_RET_SUCCESS)
+               if (opt_xtrace)
+                       cli_trace_cmd(command_level, argc, argv);
+
+               rc = cmd_process(flag, argc, argv, &repeatable);
+               if (rc != CMD_RET_SUCCESS) {
+                       if (opt_verbose)
+                               printf_P(PSTR("Command failed, result=%d\n"), rc);
                        rc = -1;
+               }
 
                /* Did the user stop this? */
                if (had_ctrlc()) {
@@ -306,6 +345,7 @@ static int cli_run_command(const char *cmd, int flag)
 
        free(cmdbuf);
        free(finaltoken);
+       --command_level;
 
        return rc ? rc : repeatable;
 }
@@ -325,7 +365,7 @@ static int cli_run_command_list(const char *cmd)
  * @param flag Execution flags (CMD_FLAG_...)
  * @return 0 on success, or != 0 on error.
  */
-int run_command(const char *cmd, int flag)
+int run_command(const char *cmd, uint_fast8_t flag)
 {
        /*
         * cli_run_command can return 0 or 1 for success, so clean up
@@ -344,7 +384,7 @@ int run_command(const char *cmd, int flag)
  * @param flag Execution flags (CMD_FLAG_...)
  * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
  */
-static int run_command_repeatable(const char *cmd, int flag)
+static int run_command_repeatable(const char *cmd, uint_fast8_t flag)
 {
        return cli_run_command(cmd, flag);
 }
@@ -363,11 +403,11 @@ void cli_loop(void)
 {
        char *lastcommand = NULL;
        int len;
-       int flag;
+       uint_fast8_t flag;
        int rc = 1;
 
        for (;;) {
-               len = cli_readline(PSTR(CONFIG_SYS_PROMPT), 1);
+               len = cli_readline(lastcommand ? PSTR(CONFIG_SYS_PROMPT_REPEAT) : PSTR(CONFIG_SYS_PROMPT), 1);
 
                flag = 0;       /* assume no special flags for now */
                if (len > 0) {
@@ -376,9 +416,10 @@ void cli_loop(void)
                } else if (len == 0)
                        flag |= CMD_FLAG_REPEAT;
 
-               if (len == -1)
-                       my_puts_P(PSTR("<INTERRUPT>\n"));
-               else
+               if (len == -1) {
+                       if (opt_verbose)
+                               my_puts_P(PSTR("<INTERRUPT>\n"));
+               } else
                        rc = run_command_repeatable(lastcommand, flag);
 
                if (rc <= 0) {