]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - avr/command.c
Expression eval: Repair unary + and -
[z180-stamp.git] / avr / command.c
index 2b53adfe921beb3d5a837021dbf0bea087593d5f..cd2ee39c81be86cc8035e4a738c4697db7174ec8 100644 (file)
@@ -1,22 +1,31 @@
+/*
+ * (C) Copyright 2014, 2016 Leo C. <erbl259-lmu@yahoo.de>
+ *
+ * (C) Copyright 2000-2009
+ * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
+ *
+ * SPDX-License-Identifier:    GPL-2.0
+ */
+
 /*
  *  Command Processor Table
  */
 
+#include "command.h"
 #include "common.h"
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
-#include <stdio.h>
-#include <avr/pgmspace.h>
+#include <setjmp.h>
 
 #include "config.h"
 #include "print-utils.h"
 #include "con-utils.h"
-#ifdef CONFIG_AUTO_COMPLETE
 #include "env.h"
-#endif
 #include "debug.h"
-#include "command.h"
+
+
+jmp_buf cmd_jbuf;
 
 
 static void print_usage_line(const FLASH char *name, int width,
@@ -36,7 +45,7 @@ int strcmp_PP(const FLASH char *s1, const FLASH char *s2)
 {
        unsigned char c1, c2;
 
-       while ((c1 = *(const FLASH unsigned char *)s1++) 
+       while ((c1 = *(const FLASH unsigned char *)s1++)
                        == (c2 = *(const FLASH unsigned char *)s2++))
                if (c1 == 0)
                        return 0;
@@ -44,9 +53,9 @@ int strcmp_PP(const FLASH char *s1, const FLASH char *s2)
        return c1 - c2;
 }
 
-int cmpstringp(const void *p1, const void *p2)
+int cmpstring_PP(const void *p1, const void *p2)
 {
-       return strcmp_PP((*(const FLASH cmd_tbl_t **) p1)->name, 
+       return strcmp_PP((*(const FLASH cmd_tbl_t **) p1)->name,
                         (*(const FLASH cmd_tbl_t **) p2)->name);
 }
 
@@ -74,6 +83,9 @@ command_ret_t _do_help(cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp,
 
        (void) flag;
 
+       char *optenv = getenv_str(PSTR("cmd"));
+       bool opt_debug = optenv && strstr_P(optenv, PSTR("debug")) != NULL;
+
        if (argc == 1) {        /*show list of commands */
                cmd_tbl_t *cmd_array[cmd_items];
                int i;
@@ -88,23 +100,25 @@ command_ret_t _do_help(cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp,
                }
 
                /* Sort command list */
-               qsort(cmd_array, cmd_items, sizeof (cmd_tbl_t *), cmpstringp);
+               qsort(cmd_array, cmd_items, sizeof (cmd_tbl_t *), cmpstring_PP);
 
                /* print short help (usage) */
                for (i = 0; i < cmd_items; i++) {
-                       const FLASH char *usage = cmd_array[i]->usage;
-
-                       /* allow user abort */
-                       if (ctrlc ())
-                               return CMD_RET_FAILURE;
-                       if (usage == NULL)
-                               continue;
-#ifdef GCC_BUG_61443 
-                       print_usage_line(cmd_array[i]->name, max_len, usage);
+                       if (opt_debug || cmd_array[i]->name[0] != '!') {
+                               const FLASH char *usage = cmd_array[i]->usage;
+
+                               /* allow user abort */
+                               if (ctrlc ())
+                                       return CMD_RET_FAILURE;
+                               if (usage == NULL)
+                                       continue;
+#ifdef GCC_BUG_61443
+                               print_usage_line(cmd_array[i]->name, max_len, usage);
 #else
-                       printf_P(PSTR("%-" stringify(8) /*FIXME*/ "S - %S\n"), 
-                                       cmd_array[i]->name, usage);
+                               printf_P(PSTR("%-" stringify(8) /*FIXME*/ "S - %S\n"),
+                                               cmd_array[i]->name, usage);
 #endif
+                       }
                }
                return CMD_RET_SUCCESS;
        }
@@ -112,8 +126,9 @@ command_ret_t _do_help(cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp,
         * command help (long version)
         */
        for (i = 1; i < argc; ++i) {
-               if ((cmdtp = find_cmd_tbl (argv[i], cmd_start, cmd_items )) != NULL) {
-                       rcode = cmd_usage(cmdtp);
+               if ((cmdtp = find_cmd_tbl (argv[i], cmd_start, cmd_items )) != NULL &&
+                                       (opt_debug || cmdtp->name[0] != '!')) {
+                       cmd_usage(cmdtp);
                } else {
                        printf_P(PSTR("Unknown command '%s' - try 'help'"
                                " without arguments.\n\n"), argv[i]
@@ -134,6 +149,9 @@ cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len)
        size_t len;
        uint_fast8_t n_found = 0;
 
+       char *optenv = getenv_str(PSTR("cmd"));
+       bool opt_debug = optenv && strstr_P(optenv, PSTR("debug")) != NULL;
+
        if (!cmd)
                return NULL;
 
@@ -142,17 +160,17 @@ cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len)
        for (cmdtp = table;
             cmdtp != table + table_len;
             cmdtp++) {
-               if (strncmp_P (cmd, cmdtp->name, len) == 0) {
-                       if (len == strlen (cmdtp->name))
+               if (strncmp_P(cmd, cmdtp->name, len) == 0 &&
+                                       (opt_debug || cmdtp->name[0] != '!')) {
+                       if (len == strlen_P(cmdtp->name))
                                return cmdtp;   /* full match */
 
                        cmdtp_temp = cmdtp;     /* abbreviated command ? */
                        n_found++;
                }
        }
-       if (n_found == 1) {                     /* exactly one match */
+       if (n_found == 1)                       /* exactly one match */
                return cmdtp_temp;
-       }
 
        return NULL;    /* not found or ambiguous command */
 }
@@ -469,8 +487,8 @@ command_ret_t cmd_call(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]
        command_ret_t result;
 
        result = (cmdtp->cmd)(cmdtp, flag, argc, argv);
-       if (result != CMD_RET_SUCCESS)
-               debug("Command failed, result=%d\n", result);
+//     if (result != CMD_RET_SUCCESS)
+//             debug("Command failed, result=%d\n", result);
        return result;
 }
 
@@ -507,6 +525,9 @@ command_ret_t cmd_process(int flag, int argc, char * const argv[],
        }
 #endif
 
+       if (setjmp(cmd_jbuf) != 0)
+               return CMD_RET_FAILURE;
+
        /* If OK so far, then do the command */
        if (!rc) {
                rc = cmd_call(cmdtp, flag, argc, argv);