]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - avr/cli.c
some cleanup, more debugging
[z180-stamp.git] / avr / cli.c
index 922f9e1971ee7087ada1d0ab67488f358629e4ba..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"
@@ -156,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;
                }
 
@@ -237,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     */
@@ -315,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);
@@ -362,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
@@ -381,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);
 }
@@ -400,7 +403,7 @@ void cli_loop(void)
 {
        char *lastcommand = NULL;
        int len;
-       int flag;
+       uint_fast8_t flag;
        int rc = 1;
 
        for (;;) {