diff --git a/cmd_parser.c b/cmd_parser.c index 9f72df4..4d2e427 100644 --- a/cmd_parser.c +++ b/cmd_parser.c @@ -1639,3 +1639,37 @@ config_done: clear_command_history(); save_cmd = 1; } + +// Execute multiple commands +// If a command is too long or can't be tokenized, remaining commands are not executed +// Returns the status via `err_status`-variable. +void execute_commands(__xdata uint8_t *p) __banked { + err_status = ERR_OK; + uint8_t cmd_idx = 0; + while (1) { + if (*p == 0 || *p == '\n' || *p == '\r') { + if (cmd_idx) { + cmd_buffer[cmd_idx] = '\0'; + cmd_tokenize(); + if (err_status != ERR_OK) + return; + cmd_parser(); + } + if (*p == 0) + return; + cmd_idx = 0; + } else { + if (cmd_idx < (CMD_BUF_SIZE - 1)) { + cmd_buffer[cmd_idx++] = *p; + } else { + cmd_buffer[CMD_BUF_SIZE - 1] = '\0'; + print_string("ERROR: Command too long: "); + print_string_x(cmd_buffer); + write_char('\n'); + err_status = ERR_CMD_TOO_LONG; + return; + } + } + p++; + }; +} diff --git a/cmd_parser.h b/cmd_parser.h index 5e65c69..a8bf797 100644 --- a/cmd_parser.h +++ b/cmd_parser.h @@ -7,10 +7,13 @@ extern __xdata uint8_t cmd_buffer[CMD_BUF_SIZE]; extern __xdata uint8_t cmd_available; +extern __xdata uint8_t err_status; void cmd_tokenize(void) __banked; void cmd_parser(void) __banked; void execute_config(void) __banked; +void execute_commands(__xdata uint8_t *p) __banked; void print_sw_version(void) __banked; void clear_command_history(void) __banked; + #endif diff --git a/html/system.js b/html/system.js index 807e407..322e057 100644 --- a/html/system.js +++ b/html/system.js @@ -12,18 +12,19 @@ async function ipSub() { if (!checkIp(document.getElementById(ips[i]).value)) return; } + var cmd = ''; for (let i=0; i<3;i++){ - var cmd = ips[i]+' '+document.getElementById(ips[i]).value; - try { - const response = await fetch('/cmd', { - method: 'POST', - body: cmd - }); - console.log('Completed!', response); - fetchIP(); - } catch(err) { - console.error(`Error: ${err}`); - } + cmd += ips[i]+' '+document.getElementById(ips[i]).value+'\n'; + } + try { + const response = await fetch('/cmd', { + method: 'POST', + body: cmd + }); + console.log('Completed!', response); + fetchIP(); + } catch(err) { + console.error(`Error: ${err}`); } } diff --git a/httpd/httpd.c b/httpd/httpd.c index 77db515..fd9828a 100644 --- a/httpd/httpd.c +++ b/httpd/httpd.c @@ -429,17 +429,16 @@ void handle_post(void) } if (is_word(request_path, "cmd")) { - register uint8_t i = 0; p += 4; if (!authenticated) { send_unauthorized(); return; } - while (*p && *p != '\n' && *p != '\r') - cmd_buffer[i++] = *p++; - cmd_buffer[i] = '\0'; - if (i) - cmd_available = 1; + execute_commands(p); + if (err_status != ERR_OK) { + send_bad_request(); + return; + } } else if (is_word(request_path, "login")) { dbg_string("POST login\n"); p += 8; // Read also over "pwd=" diff --git a/rtlplayground.c b/rtlplayground.c index c2eb5d6..18e7e3c 100644 --- a/rtlplayground.c +++ b/rtlplayground.c @@ -28,7 +28,6 @@ extern __code const struct machine machine; extern __xdata uint32_t flash_size; -extern __xdata uint8_t err_status; extern __xdata uint16_t crc_value; __xdata struct machine_runtime machine_detected;