From da34360952c0fbbd8effc2789ed72b86c8045531 Mon Sep 17 00:00:00 2001 From: Steven Murdoch Date: Tue, 30 Aug 2011 14:55:51 +0100 Subject: [PATCH] Factor out and re-write code for splitting lines from a handle Now handles non-printable characters and will not output a spurious new-line if given a partial line. --- src/common/util.c | 89 ++++++++++++++++++++++++++++++++------------ src/common/util.h | 1 + src/test/test_util.c | 65 ++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 23 deletions(-) diff --git a/src/common/util.c b/src/common/util.c index 87c6fb5c17..aa344bc9c6 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -3597,6 +3597,60 @@ tor_read_all_from_process_stderr(const process_handle_t process_handle, #endif } +/* Split buf into lines, and add to smartlist. The buffer buf will be + * modified. The resulting smartlist will consist of pointers to buf, so there + * is no need to free the contents of sl. buf must be a NULL terminated + * string. len should be set to the length of the buffer excluding the + * NULL. Non-printable characters (including NULL) will be replaced with "." */ + +int +tor_split_lines(smartlist_t *sl, char *buf, int len) +{ + /* Index in buf of the start of the current line */ + int start = 0; + /* Index in buf of the current character being processed */ + int cur = 0; + /* Are we currently in a line */ + char in_line = 0; + + /* Loop over string */ + while (cur < len) { + /* Loop until end of line or end of string */ + for (; cur < len; cur++) { + if (in_line) { + if ('\r' == buf[cur] || '\n' == buf[cur]) { + /* End of line */ + buf[cur] = '\0'; + /* Point cur to the next line */ + cur++; + /* Line starts at start and ends with a null */ + break; + } else { + if (!TOR_ISPRINT(buf[cur])) + buf[cur] = '.'; + } + } else { + if ('\r' == buf[cur] || '\n' == buf[cur]) { + /* Skip leading vertical space */ + ; + } else { + in_line = 1; + start = cur; + if (!TOR_ISPRINT(buf[cur])) + buf[cur] = '.'; + } + } + } + /* We are at the end of the line or end of string. If in_line is true there + * is a line which starts at buf+start and ends at a NULL. cur points to + * the character after the NULL. */ + if (in_line) + smartlist_add(sl, (void *)(buf+start)); + in_line = 0; + } + return smartlist_len(sl); +} + #ifdef MS_WINDOWS /** Read from stream, and send lines to log at the specified log level. * Returns -1 if there is a error reading, and 0 otherwise. @@ -3606,7 +3660,7 @@ log_from_handle(HANDLE *pipe, int severity) { char buf[256]; int pos; - int start, cur, next; + smartlist_t *lines; pos = tor_read_all_handle(pipe, buf, sizeof(buf) - 1, NULL); if (pos < 0) { @@ -3626,28 +3680,17 @@ log_from_handle(HANDLE *pipe, int severity) buf[pos] = '\0'; log_debug(LD_GENERAL, "Subprocess had %d bytes to say", pos); - /* Split buf into lines and log each one */ - next = 0; // Start of the next line - while (next < pos) { - start = next; // Look for the end of this line - for (cur=start; cur", + j, i, tests[i].split_line[j]); + /* Check that the line is as expected */ + tt_str_op(tests[i].split_line[j], ==, line); + j++; + }); + /* Check that we didn't miss some lines */ + tt_assert(tests[i].split_line[j] == NULL); + tor_free(orig_line); + smartlist_free(sl); + } + + done: + ; +} + static void test_util_di_ops(void) { @@ -1691,6 +1755,7 @@ struct testcase_t util_tests[] = { UTIL_TEST(spawn_background_fail, 0), UTIL_TEST(spawn_background_partial_read, 0), UTIL_TEST(join_cmdline, 0), + UTIL_TEST(split_lines, 0), END_OF_TESTCASES }; -- 2.47.3