stat_t set_grp(nvObj_t *nv) { if (cfg.comm_mode == TEXT_MODE) return (STAT_INVALID_OR_MALFORMED_COMMAND); for (uint8_t i=0; i<NV_MAX_OBJECTS; i++) { if ((nv = nv->nx) == NULL) break; if (nv->valuetype == TYPE_EMPTY) break; else if (nv->valuetype == TYPE_NULL) // NULL means GET the value nv_get(nv); else { nv_set(nv); nv_persist(nv); } } return (STAT_OK); }
static stat_t _json_parser_kernal(char *str) { stat_t status; int8_t depth; nvObj_t *nv = nv_reset_nv_list(); // get a fresh nvObj list char group[GROUP_LEN+1] = {""}; // group identifier - starts as NUL int8_t i = NV_BODY_LEN; ritorno(_normalize_json_string(str, JSON_OUTPUT_STRING_MAX)); // return if error // parse the JSON command into the nv body do { if (--i == 0) { return (STAT_JSON_TOO_MANY_PAIRS); } // length error // if ((status = _get_nv_pair_strict(nv, &str, &depth)) > STAT_EAGAIN) { // erred out if ((status = _get_nv_pair(nv, &str, &depth)) > STAT_EAGAIN) { // erred out return (status); } // propagate the group from previous NV pair (if relevant) if (group[0] != NUL) { strncpy(nv->group, group, GROUP_LEN); // copy the parent's group to this child } // validate the token and get the index if ((nv->index = nv_get_index(nv->group, nv->token)) == NO_MATCH) { nv->valuetype = TYPE_NULL; return (STAT_UNRECOGNIZED_NAME); } if ((nv_index_is_group(nv->index)) && (nv_group_is_prefixed(nv->token))) { strncpy(group, nv->token, GROUP_LEN); // record the group ID } if ((nv = nv->nx) == NULL) return (STAT_JSON_TOO_MANY_PAIRS);// Not supposed to encounter a NULL } while (status != STAT_OK); // breaks when parsing is complete // execute the command nv = nv_body; if (nv->valuetype == TYPE_NULL){ // means GET the value ritorno(nv_get(nv)); // ritorno returns w/status on any errors } else { cm_parse_clear(*nv->stringp); // parse Gcode and clear alarms if M30 or M2 is found ritorno(cm_is_alarmed()); // return error status if in alarm, shutdown or panic ritorno(nv_set(nv)); // set value or call a function (e.g. gcode) nv_persist(nv); } return (STAT_OK); // only successful commands exit through this point }
/****************************************************************************** * text_parser() - update a config setting from a text block (text mode) * _text_parser_kernal() - helper for above * * Use cases handled: * - $xfr=1200 set a parameter (strict separators)) * - $xfr 1200 set a parameter (relaxed separators) * - $xfr display a parameter * - $x display a group * - ? generate a status report (multiline format) */ stat_t text_parser(char_t *str) { nvObj_t *nv = nv_reset_nv_list(); // returns first object in the body stat_t status = STAT_OK; // trap special displays if (str[0] == '?') { // handle status report case sr_run_text_status_report(); return (STAT_OK); } if (str[0] == 'H') { // print help screens help_general((nvObj_t *)NULL); return (STAT_OK); } // pre-process the command if ((str[0] == '$') && (str[1] == NUL)) { // treat a lone $ as a sys request strcat(str,"sys"); } // parse and execute the command (only processes 1 command per line) ritorno(_text_parser_kernal(str, nv)); // run the parser to decode the command if ((nv->valuetype == TYPE_NULL) || (nv->valuetype == TYPE_PARENT)) { if (nv_get(nv) == STAT_COMPLETE){ // populate value, group values, or run uber-group displays return (STAT_OK); // return for uber-group displays so they don't print twice } } else { // process SET and RUN commands if (cm.machine_state == MACHINE_ALARM) return (STAT_MACHINE_ALARMED); status = nv_set(nv); // set (or run) single value if (status == STAT_OK) { nv_persist(nv); // conditionally persist depending on flags in array } } nv_print_list(status, TEXT_MULTILINE_FORMATTED, JSON_RESPONSE_FORMAT); // print the results return (status); }
int main(int argc, char **argv) { if (argc != 2) return 1; nv_get(argv[1]); return 0; }