int command_exec(int argc, char *argv[]) {
- stat_t status = STAT_INVALID_OR_MALFORMED_COMMAND;
-
putchar('\n');
int i = command_find(argv[0]);
uint8_t minArgs = pgm_read_byte(&commands[i].minArgs);
uint8_t maxArgs = pgm_read_byte(&commands[i].maxArgs);
- if (argc <= minArgs) {
- printf_P(PSTR("Too few arguments\n"));
- return status;
-
- } else if (maxArgs < argc - 1) {
- printf_P(PSTR("Too many arguments\n"));
- return status;
-
- } else {
+ if (argc <= minArgs) return STAT_TOO_FEW_ARGUMENTS;
+ else if (maxArgs < argc - 1) return STAT_TOO_MANY_ARGUMENTS;
+ else {
command_cb_t cb = pgm_read_word(&commands[i].cb);
return cb(argc, argv);
}
- } else if (argc != 1) {
- printf_P(PSTR("Unknown command '%s' or invalid arguments\n"), argv[0]);
- return status;
- }
+ } else if (argc != 1)
+ return STAT_INVALID_OR_MALFORMED_COMMAND;
// Get or set variable
char *value = strchr(argv[0], '=');
return STAT_OK;
}
- printf_P(PSTR("Unknown command or variable '%s'\n"), argv[0]);
-
- return status;
+ return STAT_UNRECOGNIZED_NAME;
}
if (argc == 2) {
int i = command_find(argv[1]);
- if (i == -1) {
- printf_P(PSTR("Command not found\n"));
- return STAT_INVALID_OR_MALFORMED_COMMAND;
+ if (i == -1) return STAT_UNRECOGNIZED_NAME;
+ else print_command_help(i);
- } else print_command_help(i);
-
- return 0;
+ return STAT_OK;
}
puts_P(PSTR("\nCommands:"));
puts_P(PSTR("\nVariables:"));
vars_print_help();
- return 0;
+ return STAT_OK;
}
switch (err) {
case STAT_EAGAIN: return true;
case STAT_OK: case STAT_NOOP: break;
- default: printf_P(PSTR("%S\n"), status_to_pgmstr(err));
+ default: status_error(err); break;
}
return false;
STAT_MSG(PREP_LINE_MOVE_TIME_IS_NAN, "Move time is NAN")
// Generic data input errors
-STAT_MSG(UNRECOGNIZED_NAME, "Unrecognized command or config name")
+STAT_MSG(UNRECOGNIZED_NAME, "Unrecognized command or variable name")
STAT_MSG(INVALID_OR_MALFORMED_COMMAND, "Invalid or malformed command")
+STAT_MSG(TOO_MANY_ARGUMENTS, "Too many arguments to command")
+STAT_MSG(TOO_FEW_ARGUMENTS, "Too few arguments to command")
STAT_MSG(BAD_NUMBER_FORMAT, "Bad number format")
STAT_MSG(PARAMETER_IS_READ_ONLY, "Parameter is read-only")
STAT_MSG(PARAMETER_CANNOT_BE_READ, "Parameter cannot be read")
stat_t status_error_P(const char *location, const char *msg, stat_t status) {
- printf_P(PSTR("\n{\"error\": %d, \"code\": %d"),
+ printf_P(PSTR("\n{\"error\": \"%S\", \"code\": %d"),
status_to_pgmstr(status), status);
if (msg) printf_P(PSTR(", \"msg\": %S"), msg);