diff --git a/src/init.c b/src/init.c index 61bfe5c..d101060 100644 --- a/src/init.c +++ b/src/init.c @@ -44,30 +44,35 @@ static int hyper_stop_pod(struct hyper_pod *pod); static int hyper_set_win_size(char *json, int length) { - struct hyper_win_size ws; struct winsize size; struct hyper_exec *exec; int ret; fprintf(stdout, "call hyper_win_size, json %s, len %d\n", json, length); - if (hyper_parse_winsize(&ws, json, length) < 0) { + JSON_Value *value = hyper_json_parse(json, length); + if (value == NULL) { fprintf(stderr, "set term size failed\n"); - return -1; + ret = -1; + goto out; } + const uint64_t seq = (uint64_t)json_object_get_number(json_object(value), "seq"); - exec = hyper_find_exec_by_seq(&global_pod, ws.seq); + exec = hyper_find_exec_by_seq(&global_pod, seq); if (exec == NULL) { - fprintf(stdout, "can not find exec whose seq is %" PRIu64"\n", ws.seq); - return 0; + fprintf(stdout, "can not find exec whose seq is %" PRIu64"\n", seq); + ret = 0; + goto out; } - size.ws_row = ws.row; - size.ws_col = ws.column; + size.ws_row = (int)json_object_get_number(json_object(value), "row"); + size.ws_col = (int)json_object_get_number(json_object(value), "column"); ret = ioctl(exec->ptyfd, TIOCSWINSZ, &size); if (ret < 0) perror("cannot ioctl to set pty device term size"); +out: + json_value_free(value); return ret; } diff --git a/src/parse.c b/src/parse.c index 379be60..7b28ce8 100644 --- a/src/parse.c +++ b/src/parse.c @@ -1257,68 +1257,6 @@ fail: return NULL; } -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 out; - - if (json_token_streq(json, t, "seq")) { - if (toks[i].type != JSMN_PRIMITIVE) - goto out; - ws->seq = json_token_ll(json, &toks[i]); - } else if (json_token_streq(json, t, "row")) { - if (toks[i].type != JSMN_PRIMITIVE) - goto out; - ws->row = json_token_int(json, &toks[i]); - } else if (json_token_streq(json, t, "column")) { - if (toks[i].type != JSMN_PRIMITIVE) - goto out; - ws->column = json_token_int(json, &toks[i]); - } else { - fprintf(stderr, "get unknown section %s in winsize\n", - json_token_str(json, t)); - goto out; - } - } - - ret = 0; -out: - free(toks); - return ret; -} - struct hyper_exec *hyper_parse_execcmd(char *json, int length) { int i, j, n;