From: Vladimír Čunát Date: Wed, 16 Sep 2020 10:49:50 +0000 (+0200) Subject: daemon/io_tty_process_input: simplify looping logic X-Git-Tag: v5.2.0~28^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f06534f6865308b20ce4cf0c70cd965674915d8f;p=thirdparty%2Fknot-resolver.git daemon/io_tty_process_input: simplify looping logic --- diff --git a/daemon/io.c b/daemon/io.c index 145a37e6a..4fb10c1b7 100644 --- a/daemon/io.c +++ b/daemon/io.c @@ -524,6 +524,7 @@ void io_tty_process_input(uv_stream_t *stream, ssize_t nread, const uv_buf_t *bu } char *pbuf = data->buf + data->blen; + lua_State *L = the_worker->engine->L; while (cmd != NULL) { /* Last command is incomplete - save it and execute later */ if (incomplete_cmd && cmd_next == NULL) { @@ -532,12 +533,10 @@ void io_tty_process_input(uv_stream_t *stream, ssize_t nread, const uv_buf_t *bu data->buf = mp_ptr(data->pool->ctx); data->blen = data->blen + strlen(cmd); - cmd = cmd_next; /* There is new incomplete command */ if (commands[nread - 1] == '\n') incomplete_cmd = false; - cmd_next = strtok(NULL, "\n"); - continue; + goto next_iter; } /* Process incomplete command from previously call */ @@ -557,12 +556,9 @@ void io_tty_process_input(uv_stream_t *stream, ssize_t nread, const uv_buf_t *bu /* Pseudo-command for switching to "binary output"; */ if (strcmp(cmd, "__binary") == 0) { data->mode = io_mode_binary; - cmd = cmd_next; - cmd_next = strtok(NULL, "\n"); - continue; + goto next_iter; } - lua_State *L = the_worker->engine->L; int ret = engine_cmd(L, cmd, false); const char *message = ""; if (lua_gettop(L) > 0) { @@ -573,17 +569,12 @@ void io_tty_process_input(uv_stream_t *stream, ssize_t nread, const uv_buf_t *bu if (data->mode == io_mode_binary) { size_t len_s = strlen(message); if (len_s > UINT32_MAX) { - cmd = cmd_next; - cmd_next = strtok(NULL, "\n"); - continue; + goto next_iter; } uint32_t len_n = htonl(len_s); fwrite(&len_n, sizeof(len_n), 1, out); fwrite(message, len_s, 1, out); - lua_settop(L, 0); - cmd = cmd_next; - cmd_next = strtok(NULL, "\n"); - continue; + goto next_iter; } /* Log to remote socket if connected */ if (stream_fd != STDIN_FILENO) { @@ -604,7 +595,8 @@ void io_tty_process_input(uv_stream_t *stream, ssize_t nread, const uv_buf_t *bu fprintf(fp_out, "\n"); fprintf(fp_out, "%s", delim); } - lua_settop(L, 0); + next_iter: + lua_settop(L, 0); /* not required in some cases but harmless */ cmd = cmd_next; cmd_next = strtok(NULL, "\n"); }