static int hyper_parse_routes(struct hyper_pod *pod, char *json, jsmntok_t *toks) { int i = 0, j, next_rt; struct hyper_route *rt; if (toks[i].type != JSMN_ARRAY) { fprintf(stdout, "routes need array\n"); return -1; } pod->r_num = toks[i].size; fprintf(stdout, "network routes num %d\n", pod->r_num); pod->rt = calloc(pod->r_num, sizeof(*rt)); if (pod->rt == NULL) { fprintf(stdout, "alloc memory for router failed\n"); return -1; } i++; for (j = 0; j < pod->r_num; j++) { int i_rt; rt = &pod->rt[j]; if (toks[i].type != JSMN_OBJECT) { fprintf(stdout, "routes array need object\n"); return -1; } next_rt = toks[i].size; i++; for (i_rt = 0; i_rt < next_rt; i_rt++, i++) { if (json_token_streq(json, &toks[i], "dest")) { rt->dst = strdup(json_token_str(json, &toks[++i])); fprintf(stdout, "route %d dest is %s\n", j, rt->dst); } else if (json_token_streq(json, &toks[i], "gateway")) { rt->gw = strdup(json_token_str(json, &toks[++i])); fprintf(stdout, "route %d gateway is %s\n", j, rt->gw); } else if (json_token_streq(json, &toks[i], "device")) { rt->device = strdup(json_token_str(json, &toks[++i])); fprintf(stdout, "route %d device is %s\n", j, rt->device); } else { fprintf(stderr, "get unknown section %s in routes\n", json_token_str(json, &toks[i])); return -1; } } } return i; }
static int hyper_parse_interfaces(struct hyper_pod *pod, char *json, jsmntok_t *toks) { int i = 0, j, next_if; struct hyper_interface *iface; if (toks[i].type != JSMN_ARRAY) { fprintf(stdout, "interfaces need array\n"); return -1; } pod->i_num = toks[i].size; fprintf(stdout, "network interfaces num %d\n", pod->i_num); pod->iface = calloc(pod->i_num, sizeof(*iface)); if (pod->iface == NULL) { fprintf(stdout, "alloc memory for interface failed\n"); return -1; } i++; for (j = 0; j < pod->i_num; j++) { int i_if; iface = &pod->iface[j]; if (toks[i].type != JSMN_OBJECT) { fprintf(stdout, "network array need object\n"); return -1; } next_if = toks[i].size; i++; for (i_if = 0; i_if < next_if; i_if++, i++) { if (json_token_streq(json, &toks[i], "device")) { iface->device = strdup(json_token_str(json, &toks[++i])); fprintf(stdout, "net device is %s\n", iface->device); } else if (json_token_streq(json, &toks[i], "ipAddress")) { iface->ipaddr = strdup(json_token_str(json, &toks[++i])); fprintf(stdout, "net ipaddress is %s\n", iface->ipaddr); } else if (json_token_streq(json, &toks[i], "netMask")) { iface->mask = strdup(json_token_str(json, &toks[++i])); fprintf(stdout, "net mask is %s\n", iface->mask); } else { fprintf(stderr, "get unknown section %s in interfaces\n", json_token_str(json, &toks[i])); return -1; } } } return i; }
static int container_parse_fsmap(struct hyper_container *c, char *json, jsmntok_t *toks) { int i = 0, j; if (toks[i].type != JSMN_ARRAY) { fprintf(stdout, "envs need array\n"); return -1; } c->maps = calloc(toks[i].size, sizeof(*c->maps)); if (c->maps == NULL) { fprintf(stderr, "allocate memory for fsmap failed\n"); return -1; } c->maps_num = toks[i].size; fprintf(stdout, "fsmap num %d\n", c->maps_num); i++; for (j = 0; j < c->maps_num; j++) { int i_map, next_map; if (toks[i].type != JSMN_OBJECT) { fprintf(stdout, "fsmap array need object\n"); return -1; } next_map = toks[i].size; i++; for (i_map = 0; i_map < next_map; i_map++, i++) { if (json_token_streq(json, &toks[i], "source")) { c->maps[j].source = strdup(json_token_str(json, &toks[++i])); fprintf(stdout, "maps %d source %s\n", j, c->maps[j].source); } else if (json_token_streq(json, &toks[i], "path")) { c->maps[j].path = strdup(json_token_str(json, &toks[++i])); fprintf(stdout, "maps %d path %s\n", j, c->maps[j].path); } else if (json_token_streq(json, &toks[i], "readOnly")) { if (!json_token_streq(json, &toks[++i], "false")) c->maps[j].readonly = 1; fprintf(stdout, "maps %d readonly %d\n", j, c->maps[j].readonly); } else { fprintf(stdout, "in maps incorrect %s\n", json_token_str(json, &toks[i])); return -1; } } } return i; }
static int container_parse_envs(struct hyper_container *c, char *json, jsmntok_t *toks) { int i = 0, j; if (toks[i].type != JSMN_ARRAY) { fprintf(stdout, "encs need array\n"); return -1; } c->envs = calloc(toks[i].size, sizeof(*c->envs)); if (c->envs == NULL) { fprintf(stderr, "allocate memory for env failed\n"); return -1; } c->envs_num = toks[i].size; fprintf(stdout, "envs num %d\n", c->envs_num); i++; for (j = 0; j < c->envs_num; j++) { int i_env, next_env; if (toks[i].type != JSMN_OBJECT) { fprintf(stdout, "env array need object\n"); return -1; } next_env = toks[i].size; i++; for (i_env = 0; i_env < next_env; i_env++, i++) { if (json_token_streq(json, &toks[i], "env")) { c->envs[j].env = strdup(json_token_str(json, &toks[++i])); fprintf(stdout, "envs %d env %s\n", j, c->envs[j].env); } else if (json_token_streq(json, &toks[i], "value")) { c->envs[j].value = strdup(json_token_str(json, &toks[++i])); fprintf(stdout, "envs %d value %s\n", j, c->envs[j].value); } else { fprintf(stdout, "get unknown section %s in envs\n", json_token_str(json, &toks[i])); return -1; } } } return i; }
static int container_parse_volumes(struct hyper_container *c, char *json, jsmntok_t *toks) { int i = 0, j; if (toks[i].type != JSMN_ARRAY) { fprintf(stdout, "volume need array\n"); return -1; } c->vols = calloc(toks[i].size, sizeof(*c->vols)); if (c->vols == NULL) { fprintf(stderr, "allocate memory for volume failed\n"); return -1; } c->vols_num = toks[i].size; fprintf(stdout, "volumes num %d\n", c->vols_num); i++; for (j = 0; j < c->vols_num; j++) { int i_volume, next_volume; if (toks[i].type != JSMN_OBJECT) { fprintf(stdout, "volume array need object\n"); return -1; } next_volume = toks[i].size; i++; for (i_volume = 0; i_volume < next_volume; i_volume++, i++) { if (json_token_streq(json, &toks[i], "device")) { c->vols[j].device = strdup(json_token_str(json, &toks[++i])); fprintf(stdout, "volume %d device %s\n", j, c->vols[j].device); } else if (json_token_streq(json, &toks[i], "addr")) { c->vols[j].scsiaddr = strdup(json_token_str(json, &toks[++i])); fprintf(stdout, "volume %d scsi id %s\n", j, c->vols[j].scsiaddr); } else if (json_token_streq(json, &toks[i], "mount")) { c->vols[j].mountpoint = strdup(json_token_str(json, &toks[++i])); fprintf(stdout, "volume %d mp %s\n", j, c->vols[j].mountpoint); } else if (json_token_streq(json, &toks[i], "fstype")) { c->vols[j].fstype = strdup(json_token_str(json, &toks[++i])); fprintf(stdout, "volume %d fstype %s\n", j, c->vols[j].fstype); } else if (json_token_streq(json, &toks[i], "readOnly")) { if (!json_token_streq(json, &toks[++i], "false")) c->vols[j].readonly = 1; fprintf(stdout, "volume %d readonly %d\n", j, c->vols[j].readonly); } else { fprintf(stdout, "get unknown section %s in voulmes\n", json_token_str(json, &toks[i])); return -1; } } } return i; }
int main() { int rc; char token[86] = "a"; char query[512]; struct songitem songarray[300]; // 10 = count in query below rc = getToken(token); check(rc == OK, "Could not get token"); /* printf("Token: %s\n", token); */ rc = snprintf(query, sizeof(query), "https://api.vk.com/method/audio.get?count=300&access_token=%s", token); check( ((rc > 0) && (rc < (int)sizeof(query))), "Could not create query"); char *js = jsonFetch(query); jsmntok_t *tokens = json_tokenise(js); typedef enum { START, KEY, STOP, RESPONSE, ARRAY, SONG, TITLE, URL} parse_state; // state is the current state of parser parse_state state = START; // stack is the state we return to when reaching end of an object parse_state stack = STOP; // counters to keep track of how far through parsing we are size_t object_tokens = 0; size_t songs = 0; size_t song_tokens = 0; size_t song_id = 0; for (size_t i = 0, j = 1; j > 0; i++, j--) { jsmntok_t *t = &tokens[i]; // should never reach uninitialized tokens check((t->start != -1 && t->end != -1), "Have reached uninitialized tokens"); if (t->type == JSMN_ARRAY || t->type == JSMN_OBJECT) j += t->size; switch (state) { case START: if (t->type != JSMN_OBJECT) sentinel("Invalid response: root element must be an object."); state = KEY; object_tokens = t->size; if (object_tokens == 0) state = STOP; if (object_tokens % 2 != 0) sentinel("Invalid response: object must have even number of children."); break; case KEY: object_tokens--; if (t->type != JSMN_STRING) sentinel("Invalid response: object keys must be strings."); if (json_token_streq(js, t, "response")) state = RESPONSE; if (object_tokens == 0) state = STOP; break; case RESPONSE: /* printf("songs t->type: %d\n", t->type); */ if (t->type != JSMN_ARRAY) sentinel("Unknown RESPONSE value, expected array of songs."); songs = t->size; state = ARRAY; stack = ARRAY; if (songs == 0) state = STOP; break; case ARRAY: songs--; /* printf("Q_SONGS = %ld\n", songs); */ song_tokens = t->size; state = SONG; /* if (song_tokens != 0) */ /* printf("%ld - song tokens\n", song_tokens); */ if (song_tokens == 0) state = STOP; if (songs == 0) stack = STOP; break; case SONG: case TITLE: case URL: song_tokens--; // Keys are odd numbered tokens within the object if (song_tokens % 2 == 1) { if (t->type == JSMN_STRING && json_token_streq(js, t, "title")) { state = TITLE; } else if (t->type == JSMN_STRING && json_token_streq(js, t, "url")) { state = URL; } } // Values in the TITLE state else if (state == TITLE) { if (t->type != JSMN_STRING) sentinel("Invalid title name."); char *str = json_token_tostr(js, t); /* printf("%s\n", str); */ songarray[song_id].id = song_id+1; snprintf(songarray[song_id].title, sizeof(songarray[song_id].title), "%s", str); state = SONG; } else if (state == URL) { if (t->type != JSMN_STRING) sentinel("Invalid url name."); // token to string char *p_url = json_token_tostr(js, t); // concatanate rubish after 'mp3' char *pch = strstr(p_url, ".mp3"); if (!pch) { sentinel("The links format does not seem to be correct, please try again"); } pch += 4; *pch = '\0'; // stupid procedure to remove '\' while ( (pch = strstr(p_url, "\\")) ) { int idx = (pch-p_url); memmove(&p_url[idx], &p_url[idx+1], strlen(p_url) - (idx)); } /* printf("%s\n", p_url); */ snprintf(songarray[song_id].url, sizeof(songarray[song_id].url), "%s", p_url); /* play_url(p_url); */ song_id++; state = SONG; } if (song_tokens == 0) state = stack; break; case STOP: printf("STOP\n"); break; default: sentinel("Invalid state %u", state); } } // end of for loop int i; pthread_t thread1; int ret1; for (i = 0; i < (int)sizeof(songarray)/(int)sizeof(songitem); i++) { printf("%d - %s\n", songarray[i].id, songarray[i].title); ret1 = pthread_create(&thread1, NULL, play_url2, (void *) songarray[i].url); pthread_join(thread1, NULL); sleep(1); /* play_url(songarray[i].url); */ } cleanup(); return 0; error: return 1; }
int main(void) { //char *js = json_fetch(URL); /*char js[] = "{" "\"following_url\": \"https://api.github.com/users/alisdair/following{/other_user}\"," "\"name\": \"Alisdair McDiarmid\"," "\"location\": \"Glasgow\"," "\"hireable\": true," "\"public_repos\": 49," "\"created_at\": \"2009-03-31T12:36:07Z\"," "\"updated_at\": \"2015-04-15T09:39:22Z\"" "}";*/ char *js = json_fetch_file("test_json.txt"); char token[64]; char name[64]; char location[64]; char hireable[64]; char following_url[64]; int public_repos; double public_gists; char *str; jsmntok_t *tokens = json_tokenise(js); typedef enum { START, KEY, PRINT, SKIP, STOP } parse_state; parse_state state = START; size_t object_tokens = 0; for (size_t i = 0, j = 1; j > 0; i++, j--) { jsmntok_t *t = &tokens[i]; if (t->type == JSMN_ARRAY || t->type == JSMN_OBJECT) j += t->size; switch (state) { case START: state = KEY; object_tokens = t->size; if (object_tokens == 0) state = STOP; break; case KEY: object_tokens--; state = SKIP; for (size_t i = 0; i < sizeof(KEYS)/sizeof(char *); i++) { if (json_token_streq(js, t, KEYS[i])) { printf("%s: ", KEYS[i]); strcpy(token, KEYS[i]); state = PRINT; break; } } break; case SKIP: object_tokens--; state = KEY; if (object_tokens == 0) state = STOP; break; case PRINT: str = json_token_tostr(js, t); puts(str); if(strcmp("name", token) == 0) // token = string { strcpy(name, str); } if(strcmp("location", token) == 0) // token = string { strcpy(location, str); } if(strcmp("public_repos", token) == 0) // token = int { public_repos = atoi(str); } if(strcmp("hireable", token) == 0) // token = string { strcpy(hireable, str); } if(strcmp("following_url", token) == 0) // token = string { strcpy(following_url, str); } if(strcmp("public_gists", token) == 0) // token = double { public_gists = atof(str); } object_tokens--; state = KEY; if (object_tokens == 0) state = STOP; break; case STOP: break; default: break; } } return 0; }
int parse_json_request(Reader& reader, const char* const keys[], const jsmntype_t types[], unsigned count) { char* js = reader.fetch_as_string(); if (!js) return -1; jsmntok_t *tokens = json_tokenise(js); enum parse_state { START, KEY, VALUE, SKIP, STOP }; parse_state state = START; jsmntype_t expected_type = JSMN_OBJECT; int result = 0; int key = -1; for (size_t i = 0, j = 1; j > 0; i++, j--) { jsmntok_t *t = &tokens[i]; if (t->type == JSMN_ARRAY || t->type == JSMN_OBJECT) j += t->size * 2; switch (state) { case START: state = KEY; break; case KEY: state = VALUE; key = -1; for (size_t i = 0; i < count; i++) { if (json_token_streq(js, t, keys[i])) { expected_type = types[i]; if (parsed_key(i)) { key = i; JSON_DEBUG( ( "key: %s %d %d\n", keys[i], i, (int)expected_type ) ); } } } if (key==-1) { JSON_DEBUG( ( "unknown key: %s\n", json_token_tostr(js, t) ) ); result = -1; } break; case VALUE: if (key!=-1) { if (t->type != expected_type) { result = -1; JSON_DEBUG( ( "type mismatch\n" ) ); } else { char *str = json_token_tostr(js, t); if (!parsed_value(key, t, str)) result = -1; } } state = KEY; break; case STOP: // Just consume the tokens break; default: result = -1; } } free(js); return result; }
int read_leap_dump(void) { // printf("%s\n", "Reaadding???"); lock(); int count = 1; char* js = 0; long length; FILE * f = fopen ("../src/leap_data.json", "rb"); if (f) { // printf("%s\n", "File Found"); fseek (f, 0, SEEK_END); length = ftell (f); fseek (f, 0, SEEK_SET); js = malloc (length); if (js) { // printf("%s\n", "File non trivial"); fread (js, 1, length, f); *(js + length) = '\0'; } fclose (f); } jsmntok_t *tokens = json_tokenise(js); /* The GitHub user API response is a single object. States required to * parse this are simple: start of the object, keys, values we want to * print, values we want to skip, and then a marker state for the end. */ typedef enum { START, KEY, PRINT, SKIP, STOP } parse_state; parse_state state = START; size_t object_tokens = 0; size_t i; size_t j; for (i = 0, j = 1; j > 0; i++, j--) { jsmntok_t *t = &tokens[i]; // Should never reach uninitialized tokens log_assert(t->start != -1 && t->end != -1); if (t->type == JSMN_ARRAY || t->type == JSMN_OBJECT) j += t->size; switch (state) { case START: if (t->type != JSMN_OBJECT) log_die("Invalid response: root element must be an object."); state = KEY; object_tokens = t->size; if (object_tokens == 0) state = STOP; if (object_tokens % 2 != 0) log_die("Invalid response: object must have even number of children."); break; case KEY: object_tokens--; if (t->type != JSMN_STRING) log_die("Invalid response: object keys must be strings."); state = SKIP; size_t i; for (i = 0; i < sizeof(KEYS)/sizeof(char *); i++) { if (json_token_streq(js, t, KEYS[i])) { printf("%s: ", KEYS[i]); state = PRINT; break; } } break; case SKIP: if (t->type != JSMN_STRING && t->type != JSMN_PRIMITIVE) log_die("Invalid response: object values must be strings or primitives."); object_tokens--; state = KEY; if (object_tokens == 0) state = STOP; break; case PRINT: if (t->type != JSMN_STRING && t->type != JSMN_PRIMITIVE) log_die("Invalid response: object values must be strings or primitives."); char *str = json_token_tostr(js, t); puts(str); char * pch; printf ("Splitting string \"%s\" into tokens:\n",str); pch = strtok (str,":"); while (pch != NULL) { printf ("%s\n",pch); if (count == 1) { py1 = atof(pch); } if (count == 2) { pz1 = atof(pch); } if (count == 3){ px1 = atof(pch); } pch = strtok (NULL, " "); } object_tokens--; state = KEY; count++; if (object_tokens == 0) state = STOP; break; case STOP: // Just consume the tokens break; default: log_die("Invalid state %u", state); } } unlock(); return 0; }
int hyper_parse_write_file(struct hyper_writter *writter, char *json, int length) { int i, n, ret = -1; jsmn_parser p; int toks_num = 10; jsmntok_t *toks = NULL; memset(writter, 0, sizeof(*writter)); toks = calloc(toks_num, sizeof(jsmntok_t)); if (toks == NULL) { fprintf(stderr, "fail to allocate tokens for write file cmd\n"); goto fail; } jsmn_init(&p); n = jsmn_parse(&p, json, length, toks, toks_num); /* Must be json first */ if (n <= 0) { fprintf(stdout, "jsmn parse failed, n is %d\n", n); goto fail; } writter->len = length - toks[0].end; writter->data = malloc(writter->len); if (writter->data == NULL) { fprintf(stderr, "fail to allocate memory for writter data\n"); goto fail; } memcpy(writter->data, json + toks[0].end, writter->len); fprintf(stdout, "writefile get data len %d %s\n", writter->len, writter->data); for (i = 0; i < n; i++) { jsmntok_t *t = &toks[i]; if (t->type != JSMN_STRING) continue; if (i++ == n) goto fail; if (json_token_streq(json, t, "container")) { writter->id = strdup(json_token_str(json, &toks[i])); fprintf(stdout, "writefile get container %s\n", writter->id); } else if (json_token_streq(json, t, "file")) { writter->file = strdup(json_token_str(json, &toks[i])); fprintf(stdout, "writefile get file %s\n", writter->file); } else { fprintf(stderr, "get unknown section %s in writefile\n", json_token_str(json, t)); goto fail; } } if (writter->id == NULL || writter->file == NULL) { fprintf(stderr, "writefile format incorrect\n"); goto fail; } ret = 0; out: free(toks); return ret; fail: free(writter->id); free(writter->file); free(writter->data); goto out; }
struct hyper_exec *hyper_parse_execcmd(char *json, int length) { int i, j, n, has_seq = 0; struct hyper_exec *exec = NULL; jsmn_parser p; int toks_num = 10; jsmntok_t *toks = NULL; realloc: toks = realloc(toks, toks_num * sizeof(jsmntok_t)); if (toks == NULL) { fprintf(stderr, "allocate tokens for execcmd failed\n"); goto fail; } jsmn_init(&p); n = jsmn_parse(&p, json, length, toks, toks_num); if (n < 0) { fprintf(stdout, "jsmn parse failed, n is %d\n", n); if (n == JSMN_ERROR_NOMEM) { toks_num *= 2; goto realloc; } goto out; } exec = calloc(1, sizeof(*exec)); if (exec == NULL) { fprintf(stderr, "allocate memory for exec cmd failed\n"); goto out; } exec->ptyfd = -1; exec->errfd = -1; exec->e.fd = -1; exec->errev.fd = -1; INIT_LIST_HEAD(&exec->list); for (i = 0, j = 0; i < n; i++) { jsmntok_t *t = &toks[i]; if (t->type != JSMN_STRING) continue; if (json_token_streq(json, t, "container")) { exec->id = strdup(json_token_str(json, &toks[++i])); fprintf(stdout, "get container %s\n", exec->id); } else if (json_token_streq(json, t, "seq")) { has_seq = 1; exec->seq = json_token_ll(json, &toks[++i]); fprintf(stdout, "get seq %"PRIu64"\n", exec->seq); } else if (json_token_streq(json, t, "cmd")) { if (toks[++i].type != JSMN_ARRAY) { fprintf(stdout, "execcmd need array\n"); goto fail; } exec->argv = calloc(toks[i].size + 1, sizeof(*exec->argv)); if (exec->argv == NULL) { fprintf(stdout, "allocate memory for exec cmd argv failed\n"); goto fail; } exec->argc = toks[i].size; exec->argv[exec->argc] = NULL; } else if (j < exec->argc) { exec->argv[j++] = strdup(json_token_str(json, &toks[i])); fprintf(stdout, "argv %d, %s\n", j - 1, exec->argv[j - 1]); } else { fprintf(stderr, "get unknown section %s in exec cmd\n", json_token_str(json, t)); goto fail; } } if (!has_seq) { fprintf(stderr, "execcmd format error, has no seq\n"); goto fail; } out: free(toks); return exec; fail: free(exec->id); for (i = 0; i < exec->argc; i++) free(exec->argv[i]); free(exec->argv); free(exec); exec = NULL; goto out; }
int hyper_parse_winsize(struct hyper_win_size *ws, char *json, int length) { int i, n, ret = -1; jsmn_parser p; int toks_num = 10; jsmntok_t *toks = NULL; memset(ws, 0, sizeof(*ws)); realloc: toks = realloc(toks, toks_num * sizeof(jsmntok_t)); if (toks == NULL) { fprintf(stderr, "allocate tokens for winsize failed\n"); goto out; } jsmn_init(&p); n = jsmn_parse(&p, json, length, toks, toks_num); if (n < 0) { fprintf(stdout, "jsmn parse failed, n is %d\n", n); if (n == JSMN_ERROR_NOMEM) { toks_num *= 2; goto realloc; } goto out; } for (i = 0; i < n; i++) { jsmntok_t *t = &toks[i]; if (t->type != JSMN_STRING) continue; if (i++ == n) goto fail; if (json_token_streq(json, t, "tty")) { if (toks[i].type != JSMN_STRING) goto fail; ws->tty = strdup(json_token_str(json, &toks[i])); } else if (json_token_streq(json, t, "seq")) { if (toks[i].type != JSMN_PRIMITIVE) goto fail; ws->seq = json_token_ll(json, &toks[i]); } else if (json_token_streq(json, t, "row")) { if (toks[i].type != JSMN_PRIMITIVE) goto fail; ws->row = json_token_int(json, &toks[i]); } else if (json_token_streq(json, t, "column")) { if (toks[i].type != JSMN_PRIMITIVE) goto fail; ws->column = json_token_int(json, &toks[i]); } else { fprintf(stderr, "get unknown section %s in winsize\n", json_token_str(json, t)); goto fail; } } ret = 0; out: free(toks); return ret; fail: free(ws->tty); ws->tty = NULL; goto out; }
int hyper_parse_kill_container(struct hyper_killer *killer, char *json, int length) { int i, n, ret = -1; jsmn_parser p; int toks_num = 10; jsmntok_t *toks = NULL; memset(killer, 0, sizeof(*killer)); realloc: toks = realloc(toks, toks_num * sizeof(jsmntok_t)); if (toks == NULL) { fprintf(stderr, "allocate tokens for kill container failed\n"); goto out; } jsmn_init(&p); n = jsmn_parse(&p, json, length, toks, toks_num); if (n < 0) { fprintf(stdout, "jsmn parse failed, n is %d\n", n); if (n == JSMN_ERROR_NOMEM) { toks_num *= 2; goto realloc; } goto out; } for (i = 0; i < n; i++) { jsmntok_t *t = &toks[i]; if (t->type != JSMN_STRING) continue; if (i++ == n) goto fail; if (json_token_streq(json, t, "container")) { if (toks[i].type != JSMN_STRING) goto fail; killer->id = strdup(json_token_str(json, &toks[i])); } else if (json_token_streq(json, t, "signal")) { if (toks[i].type != JSMN_PRIMITIVE) goto fail; killer->signal = json_token_int(json, &toks[i]); } else { fprintf(stderr, "get unknown section %s in kill container\n", json_token_str(json, t)); goto fail; } } ret = 0; out: free(toks); return ret; fail: free(killer->id); killer->id = NULL; goto out; }
int hyper_parse_pod(struct hyper_pod *pod, char *json, int length) { int i, n, next = -1; jsmn_parser p; int toks_num = 100; jsmntok_t *toks = NULL; realloc: toks = realloc(toks, toks_num * sizeof(jsmntok_t)); if (toks == NULL) { fprintf(stderr, "allocate tokens for pod failed\n"); goto out; } fprintf(stdout, "call hyper_start_pod, json %s, len %d\n", json, length); jsmn_init(&p); n = jsmn_parse(&p, json, length, toks, toks_num); if (n < 0) { fprintf(stdout, "jsmn parse failed, n is %d\n", n); if (n == JSMN_ERROR_NOMEM) { toks_num *= 2; goto realloc; } goto out; } pod->policy = POLICY_NEVER; fprintf(stdout, "jsmn parse successed, n is %d\n", n); next = 0; for (i = 0; i < n;) { jsmntok_t *t = &toks[i]; fprintf(stdout, "token %d, type is %d, size is %d\n", i, t->type, t->size); if (t->type != JSMN_STRING) { i++; continue; } if (json_token_streq(json, t, "containers") && t->size == 1) { next = hyper_parse_containers(pod, json, &toks[++i]); if (next < 0) goto out; i += next; } else if (json_token_streq(json, t, "interfaces") && t->size == 1) { next = hyper_parse_interfaces(pod, json, &toks[++i]); if (next < 0) goto out; i += next; } else if (json_token_streq(json, t, "routes") && t->size == 1) { next = hyper_parse_routes(pod, json, &toks[++i]); if (next < 0) goto out; i += next; } else if (json_token_streq(json, t, "dns") && t->size == 1) { next = hyper_parse_dns(pod, json, &toks[++i]); if (next < 0) goto out; i += next; } else if (json_token_streq(json, t, "shareDir") && t->size == 1) { pod->share_tag = strdup(json_token_str(json, &toks[++i])); fprintf(stdout, "share tag is %s\n", pod->share_tag); i++; } else if (json_token_streq(json, t, "hostname") && t->size == 1) { pod->hostname = strdup(json_token_str(json, &toks[++i])); fprintf(stdout, "hostname is %s\n", pod->hostname); i++; } else if (json_token_streq(json, t, "restartPolicy") && t->size == 1) { i++; if (json_token_streq(json, &toks[i], "always") && toks[i].size == 1) pod->policy = POLICY_ALWAYS; else if (json_token_streq(json, &toks[i], "onFailure") && toks[i].size == 1) pod->policy = POLICY_ONFAILURE; fprintf(stdout, "restartPolicy is %" PRIu8 "\n", pod->policy); i++; } else { fprintf(stdout, "get unknown section %s in pod\n", json_token_str(json, &toks[i])); next = -1; break; } } out: free(toks); return next; }
static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container **container, char *json, jsmntok_t *toks) { int i = 0, j, next, next_container; struct hyper_container *c = NULL; jsmntok_t *t; if (toks[i].type != JSMN_OBJECT) { fprintf(stderr, "format incorrect\n"); return -1; } c = calloc(1, sizeof(*c)); if (c == NULL) { fprintf(stdout, "alloc memory for container failed\n"); return -1; } c->exec.init = 1; c->exec.code = -1; c->exec.e.fd = -1; c->exec.errev.fd = -1; c->exec.ptyfd = -1; c->exec.errfd = -1; c->ns = -1; INIT_LIST_HEAD(&c->list); next_container = toks[i].size; fprintf(stdout, "next container %d\n", next_container); i++; for (j = 0; j < next_container; j++) { t = &toks[i]; fprintf(stdout, "%d name %s\n", i, json_token_str(json, t)); if (json_token_streq(json, t, "id") && t->size == 1) { c->id = strdup(json_token_str(json, &toks[++i])); c->exec.id = strdup(c->id); fprintf(stdout, "container id %s\n", c->id); i++; } else if (json_token_streq(json, t, "cmd") && t->size == 1) { next = container_parse_cmd(c, json, &toks[++i]); if (next < 0) goto fail; i += next; } else if (json_token_streq(json, t, "rootfs") && t->size == 1) { c->rootfs = strdup(json_token_str(json, &toks[++i])); fprintf(stdout, "container rootfs %s\n", c->rootfs); i++; } else if (json_token_streq(json, t, "tty") && t->size == 1) { c->exec.seq = json_token_ll(json, &toks[++i]); fprintf(stdout, "container seq %" PRIu64 "\n", c->exec.seq); i++; } else if (json_token_streq(json, t, "stderr") && t->size == 1) { c->exec.errseq = json_token_ll(json, &toks[++i]); fprintf(stdout, "container stderr seq %" PRIu64 "\n", c->exec.errseq); i++; } else if (json_token_streq(json, t, "workdir") && t->size == 1) { c->workdir = strdup(json_token_str(json, &toks[++i])); fprintf(stdout, "container workdir %s\n", c->workdir); i++; } else if (json_token_streq(json, t, "image") && t->size == 1) { c->image = strdup(json_token_str(json, &toks[++i])); fprintf(stdout, "container image %s\n", c->image); i++; } else if (json_token_streq(json, t, "addr") && t->size == 1) { c->scsiaddr = strdup(json_token_str(json, &toks[++i])); fprintf(stdout, "container image scsi id %s\n", c->scsiaddr); i++; } else if (json_token_streq(json, t, "fstype") && t->size == 1) { c->fstype = strdup(json_token_str(json, &toks[++i])); fprintf(stdout, "container fstype %s\n", c->fstype); i++; } else if (json_token_streq(json, t, "volumes") && t->size == 1) { next = container_parse_volumes(c, json, &toks[++i]); if (next < 0) goto fail; i += next; } else if (json_token_streq(json, t, "fsmap") && t->size == 1) { next = container_parse_fsmap(c, json, &toks[++i]); if (next < 0) goto fail; i += next; } else if (json_token_streq(json, t, "envs") && t->size == 1) { next = container_parse_envs(c, json, &toks[++i]); if (next < 0) goto fail; i += next; } else if (json_token_streq(json, t, "sysctl") && t->size == 1) { next = container_parse_sysctl(c, json, &toks[++i]); if (next < 0) goto fail; i += next; } else if (json_token_streq(json, t, "restartPolicy") && t->size == 1) { fprintf(stdout, "restart policy %s\n", json_token_str(json, &toks[++i])); i++; } else { fprintf(stdout, "get unknown section %s in container\n", json_token_str(json, t)); goto fail; } } *container = c; return i; fail: hyper_free_container(c); *container = NULL; return -1; }
int hyper_parse_read_file(struct hyper_reader *reader, char *json, int length) { int i, n, ret = -1; jsmn_parser p; int toks_num = 10; jsmntok_t *toks = NULL; memset(reader, 0, sizeof(*reader)); toks = calloc(toks_num, sizeof(jsmntok_t)); if (toks == NULL) { fprintf(stderr, "fail to allocate tokens for read file cmd\n"); ret = -1; goto fail; } jsmn_init(&p); n = jsmn_parse(&p, json, length, toks, toks_num); if (n < 0) { fprintf(stdout, "jsmn parse failed, n is %d\n", n); ret = -1; goto fail; } for (i = 0; i < n; i++) { jsmntok_t *t = &toks[i]; if (t->type != JSMN_STRING) continue; if (i++ == n) goto fail; if (json_token_streq(json, t, "container")) { reader->id = strdup(json_token_str(json, &toks[i])); fprintf(stdout, "readfile get container %s\n", reader->id); } else if (json_token_streq(json, t, "file")) { reader->file = strdup(json_token_str(json, &toks[i])); fprintf(stdout, "readfile get file %s\n", reader->file); } else { fprintf(stdout, "get unknown section %s in readfile\n", json_token_str(json, t)); goto fail; } } if (reader->id == NULL || reader->file == NULL) { fprintf(stderr, "readfile format incorrect\n"); goto fail; } ret = 0; out: free(toks); return ret; fail: free(reader->id); reader->id = NULL; free(reader->file); reader->file = NULL; goto out; }