]> cloudbase.mooo.com Git - z180-stamp.git/commitdiff
$() expansion deleted (only ${}). setenv_ulong(), setenv_hex()
authorLeo C <erbl259-lmu@yahoo.de>
Sun, 31 Aug 2014 20:37:26 +0000 (22:37 +0200)
committerLeo C <erbl259-lmu@yahoo.de>
Sun, 31 Aug 2014 20:37:26 +0000 (22:37 +0200)
avr/cli.c
avr/env.c
avr/main.c
include/env.h

index be5df40deb6c2d393d59ef7799eb53b862f42940..08e70387df9f7e5518c7e6efc08904f284658a03 100644 (file)
--- a/avr/cli.c
+++ b/avr/cli.c
@@ -56,8 +56,8 @@ static void process_macros(const char *input, char *output)
 #endif
        uint_fast8_t state = 0;         /* 0 = waiting for '$'  */
 
-       /* 1 = waiting for '(' or '{' */
-       /* 2 = waiting for ')' or '}' */
+       /* 1 = waiting for '{' */
+       /* 2 = waiting for '}' */
        /* 3 = waiting for '''  */
        char *output_start = output;
 
@@ -94,7 +94,7 @@ static void process_macros(const char *input, char *output)
                        }
                        break;
                case 1: /* Waiting for (        */
-                       if (c == '(' || c == '{') {
+                       if (c == '{') {
                                state++;
                                varname_start = input;
                        } else {
@@ -109,7 +109,7 @@ static void process_macros(const char *input, char *output)
                        }
                        break;
                case 2: /* Waiting for )        */
-                       if (c == ')' || c == '}') {
+                       if (c == '}') {
                                char envname[CONFIG_SYS_ENV_NAMELEN+1], *envval;
                                /* Varname # of chars */
                                uint_fast8_t envcnt = input - varname_start - 1;
@@ -153,13 +153,21 @@ static void process_macros(const char *input, char *output)
                     strlen(output_start), output_start);
 }
 
- /*
+/**
+ *
+ *
  * WARNING:
  *
  * We must create a temporary copy of the command since the command we get
  * may be the result from getenv(), which returns a pointer directly to
  * the environment data, which may change magicly when the command we run
  * creates or modifies environment variables (like "bootp" does).
+ *
+ *
+ * @param cmd
+ * @param flag
+ * @returns
+ *
  */
 static int cli_run_command(const char *cmd, int flag)
 {
@@ -173,7 +181,7 @@ static int cli_run_command(const char *cmd, int flag)
        uint_fast8_t inquotes, repeatable = 1;
        int rc = 0;
 
-       debug_parser("[RUN_COMMAND] cmd[%p]=\"%s\"\n", 
+       debug_parser("[RUN_COMMAND] cmd[%p]=\"%s\"\n",
                        cmd, cmd ? cmd : "NULL");
 
        clear_ctrlc();          /* forget any previous Control C */
@@ -290,7 +298,7 @@ static int run_command_repeatable(const char *cmd, int flag)
 int run_command_list(const char *cmd, int len)
 {
        (void) len;
-       
+
        return cli_run_command_list(cmd);
 }
 
index 602d845c673c6ddf4b567a4cdc0be48b942fa54d..5d1c5c061793d41d67571ee508de30c902daf9ff 100644 (file)
--- a/avr/env.c
+++ b/avr/env.c
@@ -1,7 +1,6 @@
 #include "common.h"
 #include <string.h>
 #include <stdlib.h>
-
 #include <avr/eeprom.h>
 
 #include "config.h"
@@ -28,7 +27,7 @@
 
 const FLASH char default_env[] = {
        "bootdelay="    "3"             DELIM
-       "bootcmd="      "reset; loadf; go $(startaddr)" DELIM
+       "bootcmd="      "reset; loadf; go ${startaddr}" DELIM
        "baudrate="     "115200"        DELIM
        "startaddr="    "0"             DELIM
        DELIM
@@ -423,35 +422,6 @@ int env_print(const MEMX char *name)
 }
 
 
-command_ret_t do_env_print(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
-{
-       command_ret_t rc = CMD_RET_SUCCESS;
-
-       (void) cmdtp; (void) flag;
-
-       if (argc == 1) {
-               /* print all env vars */
-               int size = env_print(NULL);
-               if (size < 0)
-                       return CMD_RET_FAILURE;
-               printf_P(PSTR("\nEnvironment size: %d/%d bytes\n"),
-                       size, ENV_SIZE);
-               return CMD_RET_SUCCESS;
-       }
-
-       /* print selected env vars */
-       for (int i = 1; i < argc; ++i) {
-               int rc = env_print(argv[i]);
-               if (rc < 0) {
-                       printf_P(PSTR("## Error: \"%s\" not defined\n"), argv[i]);
-                       rc = CMD_RET_FAILURE;
-               }
-       }
-
-       return rc;
-}
-
-
 /**
  * Set or delete environment variable
  *
@@ -552,37 +522,47 @@ int setenv(const char *varname, const char *varvalue)
        return (int) _do_env_set(0, argc, (char * const *)argv);
 }
 
-#if 0
 /**
  * Set an environment variable to an integer value
  *
- * @param varname      Environment variable to set
- * @param value                Value to set it to
+ * @param name Environment variable to set
+ * @param value        Value to set it to
+ * @param radix        Base
  * @return 0 if ok, 1 on error
  */
-int setenv_ulong(const char *varname, unsigned long value)
+static
+int setenv_intval(const MEMX char *name, unsigned long value, int radix)
 {
-       /* TODO: this should be unsigned */
-       char *str = simple_itoa(value);
+       char buf[11];
 
-       return setenv(varname, str);
-}
-#endif
+       ultoa(value, buf, radix);
 
+       return setenv(name, buf);
+}
 
 /**
- * Set an environment variable to an value in hex
+ * Set an environment variable to a decimal integer value
  *
- * @param varname      Environment variable to set
- * @param value                Value to set it to
+ * @param name Environment variable to set
+ * @param value        Value to set it to
  * @return 0 if ok, 1 on error
  */
-int setenv_hex(const MEMX char *varname, unsigned long value)
+int setenv_ulong(const MEMX char *name, unsigned long value)
 {
-       char str[sizeof(unsigned long) *2 + 1];
+       return setenv_intval(name, value, 10);
+}
+
 
-       sprintf_P(str, PSTR("%lx"), value);
-       return setenv(varname, str);
+/**
+ * Set an environment variable to a value in hex
+ *
+ * @param name Environment variable to set
+ * @param value        Value to set it to
+ * @return 0 if ok, 1 on error
+ */
+int setenv_hex(const MEMX char *name, unsigned long value)
+{
+       return setenv_intval(name, value, 16);
 }
 
 
@@ -613,6 +593,35 @@ unsigned long getenv_ulong(const MEMX char *name, int base, unsigned long defaul
 }
 
 
+command_ret_t do_env_print(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+       command_ret_t rc = CMD_RET_SUCCESS;
+
+       (void) cmdtp; (void) flag;
+
+       if (argc == 1) {
+               /* print all env vars */
+               int size = env_print(NULL);
+               if (size < 0)
+                       return CMD_RET_FAILURE;
+               printf_P(PSTR("\nEnvironment size: %d/%d bytes\n"),
+                       size, ENV_SIZE);
+               return CMD_RET_SUCCESS;
+       }
+
+       /* print selected env vars */
+       for (int i = 1; i < argc; ++i) {
+               int rc = env_print(argv[i]);
+               if (rc < 0) {
+                       printf_P(PSTR("## Error: \"%s\" not defined\n"), argv[i]);
+                       rc = CMD_RET_FAILURE;
+               }
+       }
+
+       return rc;
+}
+
+
 command_ret_t do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
        (void) cmdtp;
index a09f05c4e2de94bafabc765767ab88b982faa393..e4c98b8bd8f1fcfb730a649300f484c0b03b686e 100644 (file)
@@ -26,7 +26,7 @@ static uint8_t mcusr;
 /*--------------------------------------------------------------------------*/
 #if DEBUG
 
-__attribute__ ((naked)) __attribute__ ((section (".init3"))) 
+__attribute__ ((naked)) __attribute__ ((section (".init3")))
 void preset_ram (void)
 {
        for (uint8_t *p = RAMSTART; p <= (uint8_t *) RAMEND; p++)
@@ -68,7 +68,7 @@ void setup_avr(void)
        /* TODO: move to init section? */
        mcusr = MCUSR;
        MCUSR = 0;
-       
+
        /* WD */
 
        /* CPU */
@@ -79,7 +79,7 @@ void setup_avr(void)
 
        /* disable unused periphels */
        PRR0 = _BV(PRTIM2) | _BV(PRTIM0) | _BV(PRADC);
-       PRR1 = _BV(PRTIM5) | _BV(PRTIM4) | _BV(PRTIM3) | 
+       PRR1 = _BV(PRTIM5) | _BV(PRTIM4) | _BV(PRTIM3) |
                _BV(PRUSART3) | _BV(PRUSART2) | _BV(PRUSART1);
 
        /* disable analog comparator */
@@ -111,7 +111,7 @@ static int  stored_bootdelay;
 
 /***************************************************************************
  * Watch for 'delay' seconds for autoboot stop.
- * returns: 0 - no key, allow autoboot 
+ * returns: 0 - no key, allow autoboot
  *          1 - got key, abort
  */
 
@@ -207,7 +207,7 @@ int main(void)
 
        if (reset_reason_is_power_on())
                _delay_ms(CONFIG_PWRON_DELAY);
-       
+
        serial_setup(getenv_ulong(PSTR("baudrate"), 10, CONFIG_BAUDRATE));
        sei();
 
@@ -215,7 +215,7 @@ int main(void)
        debug("\n=========================<  (RE)START DEBUG  >=========================\n");
        print_reset_reason();
 #endif
-               
+
 #if DEBUG
        unsigned long i_speed = getenv_ulong(PSTR("i2c_clock"), 10, CONFIG_SYS_I2C_CLOCK);
        debug("### Setting I2C clock Frequency to %lu Hz.\n", i_speed);
@@ -225,6 +225,7 @@ int main(void)
 #endif
 
        printf_P(PSTR("\n(ATMEGA1281+HD64180)_stamp Tester\n"));
-       
+
+
        main_loop();
 }
index 3e775b385f85c10484b1e6a7d6c23cb904773b0e..7c4fee94c858fdd66ef1536b0daf4d0448d2cbc6 100644 (file)
@@ -5,6 +5,8 @@ int env_init(void);
 
 char *getenv(const MEMX char *name);
 unsigned long getenv_ulong(const MEMX char *name, int base, unsigned long default_val);
+int setenv_ulong(const MEMX char *varname, unsigned long value);
+int setenv_hex(const MEMX char *varname, unsigned long value);
 
 #if defined(CONFIG_AUTO_COMPLETE) 
 int env_complete(char *var, int maxv, char *cmdv[], int maxsz, char *buf);