int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { if (!size || data[size - 1]) { return 0; } struct json *j1 = json_from_string((const char *)data); if (j1->type == JSON_STRING) { json_destroy(j1); return 0; } free(json_to_string(j1, JSSF_SORT | JSSF_PRETTY)); struct jsonrpc_msg *msg; char *error = jsonrpc_msg_from_json(j1, &msg); /* Frees 'j1'. */ if (error) { free(error); return 0; } struct json *j2 = jsonrpc_msg_to_json(msg); /* Frees 'msg'. */ if (j2->type == JSON_STRING) { json_destroy(j2); return 0; } free(json_to_string(j2, JSSF_SORT | JSSF_PRETTY)); json_destroy(j2); return 0; }
void jsonrpc_msg_destroy(struct jsonrpc_msg *m) { if (m) { free(m->method); json_destroy(m->params); json_destroy(m->result); json_destroy(m->error); json_destroy(m->id); free(m); } }
int json_destroy(json_t *json) { if ( ! json) return 1; switch (json_typeof(json)) { case JSON_STRING: free(((json_string_t *)json)->value); free(json); break; case JSON_NUMBER: free(json); break; case JSON_OBJECT: { struct json_object_value_t *current; struct json_object_value_t *next; current = ((json_object_t *)json)->first; while (current) { next = current->next; json_destroy(current->value); free(current->key); free(current); current = next; } free(json); } break; case JSON_ARRAY: { struct json_array_value_t *current; struct json_array_value_t *next; current = ((json_array_t *)json)->first; while (current) { next = current->next; json_destroy(current->value); free(current); current = next; } free(json); } break; case JSON_TRUE: case JSON_FALSE: case JSON_NULL: break; } return 0; }
char * jsonrpc_msg_from_json(struct json *json, struct jsonrpc_msg **msgp) { struct json *method = NULL; struct jsonrpc_msg *msg = NULL; struct shash *object; char *error; if (json->type != JSON_OBJECT) { error = xstrdup("message is not a JSON object"); goto exit; } object = json_object(json); method = shash_find_and_delete(object, "method"); if (method && method->type != JSON_STRING) { error = xstrdup("method is not a JSON string"); goto exit; } msg = xzalloc(sizeof *msg); msg->method = method ? xstrdup(method->u.string) : NULL; msg->params = null_from_json_null(shash_find_and_delete(object, "params")); msg->result = null_from_json_null(shash_find_and_delete(object, "result")); msg->error = null_from_json_null(shash_find_and_delete(object, "error")); msg->id = null_from_json_null(shash_find_and_delete(object, "id")); msg->type = (msg->result ? JSONRPC_REPLY : msg->error ? JSONRPC_ERROR : msg->id ? JSONRPC_REQUEST : JSONRPC_NOTIFY); if (!shash_is_empty(object)) { error = xasprintf("message has unexpected member \"%s\"", shash_first(object)->name); goto exit; } error = jsonrpc_msg_is_valid(msg); if (error) { goto exit; } exit: json_destroy(method); json_destroy(json); if (error) { jsonrpc_msg_destroy(msg); msg = NULL; } *msgp = msg; return error; }
/* Schedules 'msg' to be sent on 'rpc' and returns 'rpc''s status (as with * jsonrpc_get_status()). * * If 'msg' cannot be sent immediately, it is appended to a buffer. The caller * is responsible for ensuring that the amount of buffered data is somehow * limited. (jsonrpc_get_backlog() returns the amount of data currently * buffered in 'rpc'.) * * Always takes ownership of 'msg', regardless of success. */ int jsonrpc_send(struct jsonrpc *rpc, struct jsonrpc_msg *msg) { struct ofpbuf *buf; struct json *json; size_t length; char *s; if (rpc->status) { jsonrpc_msg_destroy(msg); return rpc->status; } jsonrpc_log_msg(rpc, "send", msg); json = jsonrpc_msg_to_json(msg); s = json_to_string(json, 0); length = strlen(s); json_destroy(json); buf = xmalloc(sizeof *buf); ofpbuf_use(buf, s, length); buf->size = length; list_push_back(&rpc->output, &buf->list_node); rpc->backlog += length; if (rpc->backlog == length) { jsonrpc_run(rpc); } return rpc->status; }
/* Sends 'request' to 'rpc' then waits for a reply. The return value is 0 if * successful, in which case '*replyp' is set to the reply, which the caller * must eventually free with jsonrpc_msg_destroy(). Otherwise returns a status * value (see jsonrpc_get_status()). * * Discards any message received on 'rpc' that is not a reply to 'request' * (based on message id). * * Always takes ownership of 'request', regardless of success. */ int jsonrpc_transact_block(struct jsonrpc *rpc, struct jsonrpc_msg *request, struct jsonrpc_msg **replyp) { struct jsonrpc_msg *reply = NULL; struct json *id; int error; id = json_clone(request->id); error = jsonrpc_send_block(rpc, request); if (!error) { for (;;) { error = jsonrpc_recv_block(rpc, &reply); if (error) { break; } if ((reply->type == JSONRPC_REPLY || reply->type == JSONRPC_ERROR) && json_equal(id, reply->id)) { break; } jsonrpc_msg_destroy(reply); } } *replyp = error ? NULL : reply; json_destroy(id); return error; }
/* Attempts to parse the content of 'rpc->parser' (which is complete JSON) as a * JSON-RPC message. If successful, returns the JSON-RPC message. On failure, * signals an error on 'rpc' with jsonrpc_error() and returns NULL. */ static struct jsonrpc_msg * jsonrpc_parse_received_message(struct jsonrpc *rpc) { struct jsonrpc_msg *msg; struct json *json; char *error; json = json_parser_finish(rpc->parser); rpc->parser = NULL; if (json->type == JSON_STRING) { VLOG_WARN_RL(&rl, "%s: error parsing stream: %s", rpc->name, json_string(json)); jsonrpc_error(rpc, EPROTO); json_destroy(json); return NULL; } error = jsonrpc_msg_from_json(json, &msg); if (error) { VLOG_WARN_RL(&rl, "%s: received bad JSON-RPC message: %s", rpc->name, error); free(error); jsonrpc_error(rpc, EPROTO); return NULL; } jsonrpc_log_msg(rpc, "received", msg); return msg; }
static void print_and_free_json(struct json *json) { char *string = json_to_string(json, JSSF_SORT); json_destroy(json); puts(string); free(string); }
char* app_display_querymodes() { glfwSetErrorCallback(glfw_error_callback); if (!glfwInit()) return NULL; uint adapter_id = 0; size_t outsz; /* start json data (adapter array) */ json_t jroot = json_create_arr(); /* read adapters */ while (adapter_id == 0) { json_t jadapter = json_create_obj(); json_additem_toarr(jroot, jadapter); json_additem_toobj(jadapter, "name", json_create_str("Graphics Card #1")); json_additem_toobj(jadapter, "id", json_create_num(0)); /* enumerate monitors */ json_t joutputs = json_create_arr(); json_additem_toobj(jadapter, "monitors", joutputs); int output_cnt = 0; GLFWmonitor** monitors = glfwGetMonitors(&output_cnt); for (int i = 0; i < output_cnt; i++) { json_t joutput = json_create_obj(); json_additem_toarr(joutputs, joutput); json_additem_toobj(joutput, "id", json_create_num((fl64)i)); json_additem_toobj(joutput, "name", json_create_str(glfwGetMonitorName(monitors[i]))); /* enumerate modes */ json_t jmodes = json_create_arr(); json_additem_toobj(joutput, "modes", jmodes); int mode_cnt; const GLFWvidmode* modes = glfwGetVideoModes(monitors[i], &mode_cnt); for (int i = 0; i < mode_cnt; i++) { json_t jmode = json_create_obj(); json_additem_toobj(jmode, "width", json_create_num((fl64)modes[i].width)); json_additem_toobj(jmode, "height", json_create_num((fl64)modes[i].height)); json_additem_toobj(jmode, "refresh-rate", json_create_num((fl64)modes[i].refreshRate)); json_additem_toarr(jmodes, jmode); } } adapter_id ++; } char* r = json_savetobuffer(jroot, &outsz, FALSE); json_destroy(jroot); return r; }
static struct json * null_from_json_null(struct json *json) { if (json && json->type == JSON_NULL) { json_destroy(json); return NULL; } return json; }
struct h3d_anim_clip* import_loadclips(const char* json_filepath, uint frame_cnt, OUT uint* clip_cnt) { /* return default clips, which is the whole animation */ if (str_isempty(json_filepath)) { *clip_cnt = 1; return import_defaultclip(frame_cnt); } char* json_data = util_readtextfile(json_filepath, mem_heap()); if (json_data == NULL) { printf(TERM_BOLDYELLOW "Warning: could not open JSON file '%s' for clips," " reseting to default", json_filepath); *clip_cnt = 1; return import_defaultclip(frame_cnt); } json_t jroot = json_parsestring(json_data); FREE(json_data); if (jroot == NULL) { printf(TERM_BOLDYELLOW "Warning: could not read JSON file '%s' for clips," " reseting to default", json_filepath); *clip_cnt = 1; return import_defaultclip(frame_cnt); } /* */ json_t jclips = jroot; uint cnt = json_getarr_count(jclips); if (cnt == 0) { printf(TERM_BOLDYELLOW "Warning: no clip defined in JSON file '%s'," " switching to default", json_filepath); *clip_cnt = 1; return import_defaultclip(frame_cnt); } struct h3d_anim_clip* clips = (struct h3d_anim_clip*)ALLOC(sizeof(struct h3d_anim_clip)*cnt, 0); ASSERT(clips); for (uint i = 0; i < cnt; i++) { json_t jclip = json_getarr_item(jclips, i); strcpy(clips[i].name, json_gets_child(jclip, "name", "[noname]")); clips[i].start = minui(json_geti_child(jclip, "start", 0), frame_cnt-1); clips[i].end = minui(json_geti_child(jclip, "end", frame_cnt), frame_cnt); clips[i].looped = json_getb_child(jclip, "looped", FALSE); } json_destroy(jroot); *clip_cnt = cnt; return clips; }
int json_object_remove(json_t *json, const char *key, int destroy) { json_object_t *obj; struct json_object_value_t *ov; if ( ! json_is_object(json)) return 1; obj = (json_object_t *)json; if (obj->size <= 0) return 1; ov = obj->first; while (ov) { if ( ! strncmp(ov->key, key, strlen(ov->key))) { if (ov->prev) { if (ov->next) { ov->prev->next = ov->next; ov->next->prev = ov->prev; } else { ov->prev->next = NULL; obj->last = ov->prev; } } else { if (ov->next) { ov->next->prev = NULL; obj->first = ov->next; } else { obj->first= NULL; obj->last = NULL; } } if (destroy) { json_destroy(ov->value); } obj->size--; free(ov->key); free(ov); return 0; } ov = ov->next; } return 1; }
static void table_print_json__(const struct table *table, const struct table_style *style) { struct json *json, *headings, *data; size_t x, y; char *s; json = json_object_create(); if (table->caption) { json_object_put_string(json, "caption", table->caption); } if (table->timestamp) { char *s = table_format_timestamp__(); json_object_put_string(json, "time", s); free(s); } headings = json_array_create_empty(); for (x = 0; x < table->n_columns; x++) { const struct column *column = &table->columns[x]; json_array_add(headings, json_string_create(column->heading)); } json_object_put(json, "headings", headings); data = json_array_create_empty(); for (y = 0; y < table->n_rows; y++) { struct json *row = json_array_create_empty(); for (x = 0; x < table->n_columns; x++) { const struct cell *cell = table_cell__(table, y, x); if (cell->text) { json_array_add(row, json_string_create(cell->text)); } else if (cell->json) { json_array_add(row, json_clone(cell->json)); } else { json_array_add(row, json_null_create()); } } json_array_add(data, row); } json_object_put(json, "data", data); s = json_to_string(json, style->json_flags); json_destroy(json); puts(s); free(s); }
int main (void) { json_value_t *obj = json_new(JSON_OBJECT, "[object]"); json_value_t *name = json_new(JSON_STRING, "joseph"); assert(obj); ok("json_new"); char *src = json_stringify(obj); ok("json_stringify"); assert(src); json_destroy(obj); assert(0 == obj->size); ok("json_destroy"); obj = NULL; ok_done(); return 0; }
void webserver_runajax(struct mg_connection* conn, const char* cmd, const char* param1, const char* param2) { pfn_ajax_cmd cmd_fn = prf_find_cmd(cmd); if (cmd_fn != NULL) { json_t j = cmd_fn(param1, param2); if (j != NULL) { size_t json_size; char* json_data = json_savetobuffer(j, &json_size, TRUE); if (json_data != NULL) { mg_write(conn, json_data, (uint)json_size); json_deletebuffer(json_data); } json_destroy(j); } else { mg_send_status(conn, 400); } } else { mg_send_status(conn, 404); } }
/* Schedules 'msg' to be sent on 'rpc' and returns 'rpc''s status (as with * jsonrpc_get_status()). * * If 'msg' cannot be sent immediately, it is appended to a buffer. The caller * is responsible for ensuring that the amount of buffered data is somehow * limited. (jsonrpc_get_backlog() returns the amount of data currently * buffered in 'rpc'.) * * Always takes ownership of 'msg', regardless of success. */ int jsonrpc_send(struct jsonrpc *rpc, struct jsonrpc_msg *msg) { struct ofpbuf *buf; struct json *json; size_t length; char *s; if (rpc->status) { jsonrpc_msg_destroy(msg); return rpc->status; } jsonrpc_log_msg(rpc, "send", msg); json = jsonrpc_msg_to_json(msg); s = json_to_string(json, 0); length = strlen(s); json_destroy(json); buf = xmalloc(sizeof *buf); ofpbuf_use(buf, s, length); buf->size = length; list_push_back(&rpc->output, &buf->list_node); rpc->output_count++; rpc->backlog += length; if (rpc->output_count >= 50) { VLOG_INFO_RL(&rl, "excessive sending backlog, jsonrpc: %s, num of" " msgs: %"PRIuSIZE", backlog: %"PRIuSIZE".", rpc->name, rpc->output_count, rpc->backlog); } if (rpc->backlog == length) { jsonrpc_run(rpc); } return rpc->status; }
void jsonrpc_session_run(struct jsonrpc_session *s) { if (s->pstream) { struct stream *stream; int error; error = pstream_accept(s->pstream, &stream); if (!error) { if (s->rpc || s->stream) { VLOG_INFO_RL(&rl, "%s: new connection replacing active connection", reconnect_get_name(s->reconnect)); jsonrpc_session_disconnect(s); } reconnect_connected(s->reconnect, time_msec()); s->rpc = jsonrpc_open(stream); } else if (error != EAGAIN) { reconnect_listen_error(s->reconnect, time_msec(), error); pstream_close(s->pstream); s->pstream = NULL; } } if (s->rpc) { size_t backlog; int error; backlog = jsonrpc_get_backlog(s->rpc); jsonrpc_run(s->rpc); if (jsonrpc_get_backlog(s->rpc) < backlog) { /* Data previously caught in a queue was successfully sent (or * there's an error, which we'll catch below.) * * We don't count data that is successfully sent immediately as * activity, because there's a lot of queuing downstream from us, * which means that we can push a lot of data into a connection * that has stalled and won't ever recover. */ reconnect_activity(s->reconnect, time_msec()); } error = jsonrpc_get_status(s->rpc); if (error) { reconnect_disconnected(s->reconnect, time_msec(), error); jsonrpc_session_disconnect(s); s->last_error = error; } } else if (s->stream) { int error; stream_run(s->stream); error = stream_connect(s->stream); if (!error) { reconnect_connected(s->reconnect, time_msec()); s->rpc = jsonrpc_open(s->stream); s->stream = NULL; } else if (error != EAGAIN) { reconnect_connect_failed(s->reconnect, time_msec(), error); stream_close(s->stream); s->stream = NULL; s->last_error = error; } } switch (reconnect_run(s->reconnect, time_msec())) { case RECONNECT_CONNECT: jsonrpc_session_connect(s); break; case RECONNECT_DISCONNECT: reconnect_disconnected(s->reconnect, time_msec(), 0); jsonrpc_session_disconnect(s); break; case RECONNECT_PROBE: if (s->rpc) { struct json *params; struct jsonrpc_msg *request; params = json_array_create_empty(); request = jsonrpc_create_request("echo", params, NULL); json_destroy(request->id); request->id = json_string_create("echo"); jsonrpc_send(s->rpc, request); } break; } }
struct ovsdb_error * ovsdb_log_read(struct ovsdb_log *file, struct json **jsonp) { uint8_t expected_sha1[SHA1_DIGEST_SIZE]; uint8_t actual_sha1[SHA1_DIGEST_SIZE]; struct ovsdb_error *error; off_t data_offset; unsigned long data_length; struct json *json; char header[128]; *jsonp = json = NULL; if (file->read_error) { return ovsdb_error_clone(file->read_error); } else if (file->mode == OVSDB_LOG_WRITE) { return OVSDB_BUG("reading file in write mode"); } if (!fgets(header, sizeof header, file->stream)) { if (feof(file->stream)) { error = NULL; } else { error = ovsdb_io_error(errno, "%s: read failed", file->name); } goto error; } if (!parse_header(header, &data_length, expected_sha1)) { error = ovsdb_syntax_error(NULL, NULL, "%s: parse error at offset " "%lld in header line \"%.*s\"", file->name, (long long int) file->offset, (int) strcspn(header, "\n"), header); goto error; } data_offset = file->offset + strlen(header); error = parse_body(file, data_offset, data_length, actual_sha1, &json); if (error) { goto error; } if (memcmp(expected_sha1, actual_sha1, SHA1_DIGEST_SIZE)) { error = ovsdb_syntax_error(NULL, NULL, "%s: %lu bytes starting at " "offset %lld have SHA-1 hash "SHA1_FMT" " "but should have hash "SHA1_FMT, file->name, data_length, (long long int) data_offset, SHA1_ARGS(actual_sha1), SHA1_ARGS(expected_sha1)); goto error; } if (json->type == JSON_STRING) { error = ovsdb_syntax_error(NULL, NULL, "%s: %lu bytes starting at " "offset %lld are not valid JSON (%s)", file->name, data_length, (long long int) data_offset, json->u.string); goto error; } file->prev_offset = file->offset; file->offset = data_offset + data_length; *jsonp = json; return NULL; error: file->read_error = ovsdb_error_clone(error); json_destroy(json); return error; }
int config_destroy() { json_destroy(); return 0; }
int main (int argc, char **argv) { json_value_t *value = NULL; json_value_t *root = NULL; char *filename = NULL; char *key = NULL; char *src = NULL; int i = 0; #define usage() printf("usage: %s <file> [key]\n", argv[0]); if (argc < 2) { usage(); return 1; } // parse opts for (i = 1; i < argc; ++i) { if (EQ(argv[i], "--each")) { each = 1; } else if (EQ(argv[i], "--stream")) { stream = 1; } else if (EQ(argv[i], "--help")) { usage(); return 0; } } if (ferror(stdin)) { return 1; } else if (0 == isatty(0)) { filename = "<stdin>"; tty = 1; if (argc > 1 && '-' != argv[1][0]) { key = argv[1]; } } else { filename = argv[1]; if (0 != fs_exists(filename)) { printf("E: not found - `%s'\n", filename); return 1; } src = fs_read(filename); if (NULL == src) { return 1; } if (argc > 2 && '-' != argv[2][0]) { key = argv[2]; } } do { if (tty) { src = read_stdin(); } if (NULL == src || 0 == strlen(src)) { break; } // proxy source if just streaming stdin // without a key lookup if (NULL == key && stream) { printf("%s", src); continue; } root = json_parse(filename, src); if (NULL == root) { return 1; } else if (root->errno) { json_perror(root); return 1; } if (NULL != key) { value = json_get(root, key); if (NULL == value) { return 1; } free(root); root = value; } else { value = root; } if (1 == each && JSON_ARRAY == value->type) { value = value->values[0]; while (value) { printf("%s\n", json_stringify(value)); value = value->next; } } else { printf("%s\n", json_stringify(value)); } json_destroy(root); free(src); value = NULL; root = NULL; src = NULL; } while (tty); return 0; }
int main (void) { const char *file = "test/fixtures/array.json"; const char *src = NULL; json_value_t *arr = NULL; json_value_t *value = NULL; src = fs_read(file); assert(src); arr = json_parse(file, src); assert(arr); assert(arr->size); ok("json_parse"); value = json_get(arr, "0"); assert(value); assert(EQ("kinkajou", value->as.string)); ok("json_get"); value = json_get(arr, "1"); assert(value); assert(EQ("bradley", value->as.string)); ok("json_get"); value = json_get(arr, "2"); assert(value); assert(EQ("4", value->as.string)); assert(4 == (int) value->as.number); ok("json_get"); value = json_get(arr, "0"); assert(value); ok("json_get"); json_destroy(value); assert(0 == value->errno); assert(2 == arr->size); value = NULL; ok("json_destroy"); value = json_get(arr, "0"); assert(value); ok("json_get"); assert(EQ("bradley", value->as.string)); json_destroy(value); assert(0 == value->errno); assert(1 == arr->size); value = NULL; ok("json_destroy"); value = json_get(arr, "0"); assert(value); ok("json_get"); assert(EQ("4", value->as.string)); assert(4 == (int) value->as.number); json_destroy(value); assert(0 == value->errno); assert(0 == arr->size); value = NULL; ok("json_destroy"); ok_done(); return 0; }
static void cell_destroy(struct cell *cell) { free(cell->text); json_destroy(cell->json); }
static void print_and_free_json(struct json *json) { print_json(json); json_destroy(json); }
void jsonrpc_session_run(struct jsonrpc_session *s) { if (s->pstream) { struct stream *stream; int error; error = pstream_accept(s->pstream, &stream); if (!error) { if (s->rpc || s->stream) { VLOG_INFO_RL(&rl, "%s: new connection replacing active connection", reconnect_get_name(s->reconnect)); jsonrpc_session_disconnect(s); } reconnect_connected(s->reconnect, time_msec()); s->rpc = jsonrpc_open(stream); } else if (error != EAGAIN) { reconnect_listen_error(s->reconnect, time_msec(), error); pstream_close(s->pstream); s->pstream = NULL; } } if (s->rpc) { int error; jsonrpc_run(s->rpc); error = jsonrpc_get_status(s->rpc); if (error) { reconnect_disconnected(s->reconnect, time_msec(), error); jsonrpc_session_disconnect(s); } } else if (s->stream) { int error; stream_run(s->stream); error = stream_connect(s->stream); if (!error) { reconnect_connected(s->reconnect, time_msec()); s->rpc = jsonrpc_open(s->stream); s->stream = NULL; } else if (error != EAGAIN) { reconnect_connect_failed(s->reconnect, time_msec(), error); stream_close(s->stream); s->stream = NULL; } } switch (reconnect_run(s->reconnect, time_msec())) { case RECONNECT_CONNECT: jsonrpc_session_connect(s); break; case RECONNECT_DISCONNECT: reconnect_disconnected(s->reconnect, time_msec(), 0); jsonrpc_session_disconnect(s); break; case RECONNECT_PROBE: if (s->rpc) { struct json *params; struct jsonrpc_msg *request; params = json_array_create_empty(); request = jsonrpc_create_request("echo", params, NULL); json_destroy(request->id); request->id = json_string_create("echo"); jsonrpc_send(s->rpc, request); } break; } }
json_t *json_parse(const char *data, int len) { int i, start; for (start = 0; start < len; start++) { if ( ! isspace(data[start])) break; } for (i = len - 1; i >= start; i++) { if ( ! isspace(data[i])) break; len--; } if (start + 1 > len) return NULL; if (data[start] == '"') { char *value; json_t *json; for (i = start + 1; i < len; i++) { if (data[i] == '"' && data[i - 1] != '\\') { if ((i - start) > 1) { value = strndup(data + start + 1, i - start - 1); if ( ! value) goto error; json = json_string(value); free(value); return json; } else { return json_string(""); } } } } else if (data[start] == '-' || (data[start] >= '0' && data[start] <= '9')) { char *parsed; long double nv; nv = strtold(data, &parsed); i = parsed - data - 1; if (i > len) goto error; return json_number(nv); } else if (data[start] == '[' || data[start] == '{') { int j; int quotation, bracket, brace, done; char *key = NULL; json_t *root, *child; quotation = bracket = brace = done = 0; if (data[start] == '[') root = json_array(); else if (data[start] == '{') root = json_object(); else goto error; done = start + 1; for (i = done; i < len; i++) { if (data[i - 1] != '\\' && data[i] == '"') { quotation = ! quotation; } if (quotation) continue; if (data[i] == '[') { bracket++; } else if (data[i] == '{') { brace++; } else if (data[i] == ':') { if (data[start] == '{' && ! bracket && ! brace) { for (j = done; j < i; j++, done++) if ( ! isspace(data[j])) break; for (j = i - 1; j >= done; j--) { if ( ! isspace(data[j])) break; } key = strndup(&data[done], j - done + 1); if ( ! key) goto error; done = i + 1; } } else if (data[i] == ',' || data[i] == ']' || data[i] == '}') { if (data[start] == '[' && ! bracket && ! brace) { //printf("[%.*s]\n", i - done, &data[done]); child = json_parse(&data[done], i - done); if (child) json_array_set(root, child); done = i; } else if (data[start] == '{' && ! bracket && ! brace) { child = json_parse(&data[done], i - done); if (child) { if ( ! key) { json_destroy(child); goto error; } //printf("key %s {%.*s}\n", key, i - done, &data[done]); json_object_set(root, key, child); free(key); key = NULL; } done = i; } if (data[i] == ',' && ! bracket && ! brace) done++; } if (data[i] == ']') { bracket--; } else if (data[i] == '}') { brace--; } } if (data[start] == '[') bracket++; if (data[start] == '{') brace++; if (bracket || brace) goto error; return root; } else if (len - start >= 4 && ! strncasecmp(data + start, "true", 4)) { return json_true(); } else if (len - start >= 5 && ! strncasecmp(data + start, "false", 5)) { return json_false(); } else if (len - start >= 4 && ! strncasecmp(data + start, "null", 4)) { return json_null(); } error: fprintf(stderr, "parse error : [%.*s]\n", len - start, data + start); return NULL; }
struct init_params* app_config_load(const char* cfg_jsonfile) { struct init_params* params = (struct init_params*)ALLOC(sizeof(struct init_params), 0); ASSERT(params); memset(params, 0x00, sizeof(struct init_params)); char* buffer = util_readtextfile(cfg_jsonfile, mem_heap()); if (buffer == NULL) { err_printf(__FILE__, __LINE__, "loading confing file '%s' failed", cfg_jsonfile); FREE(params); return NULL; } json_t root = json_parsestring(buffer); FREE(buffer); if (root == NULL) { err_printf(__FILE__, __LINE__, "parsing confing file '%s' failed: invalid json", cfg_jsonfile); FREE(params); return NULL; } /* fill paramters from json data */ /* general */ json_t general = json_getitem(root, "general"); if (general != NULL) { if (json_getb_child(general, "debug", FALSE)) BIT_ADD(params->flags, ENG_FLAG_DEBUG); if (json_getb_child(general, "dev", FALSE)) BIT_ADD(params->flags, ENG_FLAG_DEV); if (json_getb_child(general, "console", FALSE)) BIT_ADD(params->flags, ENG_FLAG_CONSOLE); if (json_getb_child(general, "no-physics", FALSE)) BIT_ADD(params->flags, ENG_FLAG_DISABLEPHX); if (json_getb_child(general, "optimize-mem", FALSE)) BIT_ADD(params->flags, ENG_FLAG_OPTIMIZEMEMORY); if (json_getb_child(general, "no-bgload", FALSE)) BIT_ADD(params->flags, ENG_FLAG_DISABLEBGLOAD); params->console_lines_max = json_geti_child(general, "console-lines", 1000); } else { params->console_lines_max = 1000; } /* gfx */ gfx_parseparams(¶ms->gfx, root); /* physics */ phx_parseparams(¶ms->phx, root); /* script */ sct_parseparams(¶ms->sct, root); /* developer */ json_t dev = json_getitem(root, "dev"); if (dev != NULL) { params->dev.webserver_port = json_geti_child(dev, "webserver-port", 8888); params->dev.buffsize_data = json_geti_child(dev, "buffsize-data", 0); params->dev.buffsize_tmp = json_geti_child(dev, "buffsize-tmp", 0); } else { params->dev.webserver_port = 8888; } /* console commands */ json_t console = json_getitem(root, "console"); if (console != NULL) { params->console_cmds_cnt = json_getarr_count(console); params->console_cmds = (char*)ALLOC(params->console_cmds_cnt*128, 0); ASSERT(params->console_cmds != NULL); for (uint i = 0; i < params->console_cmds_cnt; i++) { char* data = params->console_cmds + i*128; strcpy(data, json_gets(json_getarr_item(console, i))); } } json_destroy(root); return params; }
struct json * ovsdb_execute(struct ovsdb *db, const struct ovsdb_session *session, const struct json *params, long long int elapsed_msec, long long int *timeout_msec) { struct ovsdb_execution x; struct ovsdb_error *error; struct json *results; size_t n_operations; size_t i; if (params->type != JSON_ARRAY || !params->u.array.n || params->u.array.elems[0]->type != JSON_STRING || strcmp(params->u.array.elems[0]->u.string, db->schema->name)) { if (params->type != JSON_ARRAY) { error = ovsdb_syntax_error(params, NULL, "array expected"); } else { error = ovsdb_syntax_error(params, NULL, "database name expected " "as first parameter"); } results = ovsdb_error_to_json(error); ovsdb_error_destroy(error); return results; } x.db = db; x.session = session; x.txn = ovsdb_txn_create(db); x.symtab = ovsdb_symbol_table_create(); x.durable = false; x.elapsed_msec = elapsed_msec; x.timeout_msec = LLONG_MAX; results = NULL; results = json_array_create_empty(); n_operations = params->u.array.n - 1; error = NULL; for (i = 1; i <= n_operations; i++) { struct json *operation = params->u.array.elems[i]; struct ovsdb_error *parse_error; struct ovsdb_parser parser; struct json *result; const struct json *op; /* Parse and execute operation. */ ovsdb_parser_init(&parser, operation, "ovsdb operation %"PRIuSIZE" of %"PRIuSIZE, i, n_operations); op = ovsdb_parser_member(&parser, "op", OP_ID); result = json_object_create(); if (op) { const char *op_name = json_string(op); ovsdb_operation_executor *executor = lookup_executor(op_name); if (executor) { error = executor(&x, &parser, result); } else { ovsdb_parser_raise_error(&parser, "No operation \"%s\"", op_name); } } else { ovs_assert(ovsdb_parser_has_error(&parser)); } /* A parse error overrides any other error. * An error overrides any other result. */ parse_error = ovsdb_parser_finish(&parser); if (parse_error) { ovsdb_error_destroy(error); error = parse_error; } if (error) { json_destroy(result); result = ovsdb_error_to_json(error); } if (error && !strcmp(ovsdb_error_get_tag(error), "not supported") && timeout_msec) { ovsdb_txn_abort(x.txn); *timeout_msec = x.timeout_msec; json_destroy(result); json_destroy(results); results = NULL; goto exit; } /* Add result to array. */ json_array_add(results, result); if (error) { break; } } if (!error) { error = ovsdb_txn_commit(x.txn, x.durable); if (error) { json_array_add(results, ovsdb_error_to_json(error)); } } else { ovsdb_txn_abort(x.txn); } while (json_array(results)->n < n_operations) { json_array_add(results, json_null_create()); } exit: ovsdb_error_destroy(error); ovsdb_symbol_table_destroy(x.symtab); return results; }
char* app_display_querymodes() { IDXGIFactory* factory; if (FAILED(CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)&factory))) return NULL; IDXGIAdapter* adapter; uint adapter_id = 0; DXGI_ADAPTER_DESC desc; char gpu_desc[128]; size_t outsz; /* start json data (adapter array) */ json_t jroot = json_create_arr(); /* read adapters */ while (factory->EnumAdapters(adapter_id, &adapter) != DXGI_ERROR_NOT_FOUND) { adapter->GetDesc(&desc); str_widetomb(gpu_desc, desc.Description, sizeof(gpu_desc)); json_t jadapter = json_create_obj(); json_additem_toarr(jroot, jadapter); json_additem_toobj(jadapter, "name", json_create_str(gpu_desc)); json_additem_toobj(jadapter, "id", json_create_num((fl64)adapter_id)); /* enumerate monitors */ json_t joutputs = json_create_arr(); json_additem_toobj(jadapter, "outputs", joutputs); IDXGIOutput* output; uint output_id = 0; while (adapter->EnumOutputs(output_id, &output) != DXGI_ERROR_NOT_FOUND) { json_t joutput = json_create_obj(); json_additem_toarr(joutputs, joutput); json_additem_toobj(joutput, "id", json_create_num((fl64)output_id)); /* enumerate modes */ json_t jmodes = json_create_arr(); json_additem_toobj(joutput, "monitors", jmodes); uint mode_cnt; HRESULT hr = output->GetDisplayModeList(DEFAULT_DISPLAY_FORMAT, 0, &mode_cnt, NULL); if (SUCCEEDED(hr)) { DXGI_MODE_DESC* modes = (DXGI_MODE_DESC*)ALLOC(sizeof(DXGI_MODE_DESC) * mode_cnt, 0); ASSERT(modes); output->GetDisplayModeList(DEFAULT_DISPLAY_FORMAT, 0, &mode_cnt, modes); for (uint i = 0; i < mode_cnt; i++) { if (modes[i].RefreshRate.Denominator != 1) continue; json_t jmode = json_create_obj(); json_additem_toobj(jmode, "width", json_create_num((fl64)modes[i].Width)); json_additem_toobj(jmode, "height", json_create_num((fl64)modes[i].Height)); json_additem_toobj(jmode, "refresh-rate", json_create_num((fl64)modes[i].RefreshRate.Numerator)); json_additem_toarr(jmodes, jmode); } FREE(modes); } output_id ++; } adapter->Release(); adapter_id ++; } factory->Release(); char* r = json_savetobuffer(jroot, &outsz, FALSE); json_destroy(jroot); return r; }