__USE_LIBASN int main(int argc, char *argv[]) { char buf[BUFSIZ]; mmatic *mm = mmatic_create(); xstr *xs = xstr_create("", mm); json *js = json_create(mm); while (fgets(buf, BUFSIZ, stdin)) xstr_append(xs, buf); ut *parsed = json_parse(js, xstr_string(xs)); if (ut_ok(parsed)) printf("%s", json_print(js, parsed)); else printf("%s\n", ut_err(parsed)); return 0; }
/** Parse config file * @retval 0 success * @retval 1 syntax error * @retval 2 logic error * @retval 3 other error */ static int parse_config(struct mg *mg) { FILE *fp; xstr *xs; char buf[4096], *str; json *js; ut *cfg; /* read file contents, ignoring empty lines and comments */ fp = fopen(mg->options.conf_file, "r"); if (!fp) { dbg(0, "%s: fopen() failed: %s\n", mg->options.conf_file, strerror(errno)); return 3; } xs = xstr_create("{", mg->mmtmp); while (fgets(buf, sizeof buf, fp)) { str = pjf_trim(buf); if (!str || !str[0] || str[0] == '#') continue; xstr_append(xs, str); } xstr_append_char(xs, '}'); fclose(fp); /* parse config file as loose JSON */ js = json_create(mg->mmtmp); json_setopt(js, JSON_LOOSE, 1); cfg = json_parse(js, xstr_string(xs)); if (!ut_ok(cfg)) { dbg(0, "parsing config file failed: %s\n", ut_err(cfg)); return 1; } /* parse config */ return (parse_config_ut(mg, cfg) ? 2 : 0); }
/** Common part of request parser, usually after JSON representation is made available in req->params * @param req the request * @param leave leave the req->params */ static bool common(struct req *req, bool leave) { ut *ut; /* guarantee that req->params is ok */ if (!ut_ok(req->params)) { req->reply = req->params; req->params = NULL; return false; } else if (ut_type(req->params) != T_HASH) { return errcode(JSON_RPC_INVALID_REQUEST); } /* JSON-RPC argument check */ if ((ut = uth_get(req->params, "service"))) /* used by Qooxdoo */ req->service = ut_char(ut); if ((ut = uth_get(req->params, "method"))) req->method = ut_char(ut); if ((ut = uth_get(req->params, "id"))) req->id = ut_char(ut); /* XXX: dont check jsonrpc=2.0 */ if (!leave) { if ((ut = uth_get(req->params, "params"))) req->params = ut; else req->params = uth_set_thash(req->params, "params", NULL); /* create empty hash */ if (ut_is_tlist(req->params) && thash_get(req->http.headers, "X-Qooxdoo-Response-Type")) { tlist *tl = ut_tlist(req->params); req->params = tlist_shift(tl); } } return true; }