]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
daemon/io_tty_process_input: simplify looping logic
authorVladimír Čunát <vladimir.cunat@nic.cz>
Wed, 16 Sep 2020 10:49:50 +0000 (12:49 +0200)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Fri, 2 Oct 2020 06:50:27 +0000 (08:50 +0200)
daemon/io.c

index 145a37e6a983f9ee36e353ec1895f26d48ea5a30..4fb10c1b7c5ac907265a3f47f2cc4e69b9afa0f2 100644 (file)
@@ -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");
        }