From a26e39b34c562147c65ebb2adc2ff7b32e6f1b08 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Sat, 19 Feb 2011 17:03:12 +0000 Subject: [PATCH] break line tokenization out into its own dedicated function git-svn-id: https://urjtag.svn.sourceforge.net/svnroot/urjtag/trunk@1885 b68d4a1b-bc3d-0410-92ed-d4ac073336b7 --- urjtag/ChangeLog | 4 +++ urjtag/include/urjtag/parse.h | 16 ++++++++++ urjtag/src/global/parse.c | 58 ++++++++++++++++++++++++----------- 3 files changed, 60 insertions(+), 18 deletions(-) diff --git a/urjtag/ChangeLog b/urjtag/ChangeLog index 4c3be4ec..028a2346 100644 --- a/urjtag/ChangeLog +++ b/urjtag/ChangeLog @@ -3,6 +3,10 @@ * src/cmd/cmd_bfin.c (cmd_bfin_run): Make sure the part is a Blackfin before we try calling functions that don't exist and segfault. + * include/urjtag/parse.h, src/global/parse.c: Split tokenization of lines out + of urj_parse_line and into its own function urj_tokenize_lines. This lets + us reuse this code elsewhere. + 2011-02-18 Mike Frysinger * src/bfin/bfin-part.c (_bfin_part_init): Add missing "void" to param list. diff --git a/urjtag/include/urjtag/parse.h b/urjtag/include/urjtag/parse.h index 141a882b..fa7524ae 100644 --- a/urjtag/include/urjtag/parse.h +++ b/urjtag/include/urjtag/parse.h @@ -29,6 +29,22 @@ #include "types.h" +/** + * Turn a string into a bunch of tokens (space delimited). Returns the + * number of tokens via token_cnt. Memory is all allocated as needed, so + * tokens should be passed to urj_tokens_free() when finished. + * + * @return + * URJ_STATUS_OK on success + * URJ_STATUS_ERROR on error + */ +int urj_tokenize_line (const char *line, char ***tokens, size_t *token_cnt); + +/** + * Free memory allocated for the tokens by urj_tokenize_line(). + */ +void urj_tokens_free (char **tokens); + /** * Take care of tokenizing the line before sending to urj_cmd_run(). * diff --git a/urjtag/src/global/parse.c b/urjtag/src/global/parse.c index a190d2cb..b7d2714f 100644 --- a/urjtag/src/global/parse.c +++ b/urjtag/src/global/parse.c @@ -41,20 +41,22 @@ #include int -urj_parse_line (urj_chain_t *chain, const char *line) +urj_tokenize_line (const char *line, char ***tokens, size_t *token_cnt) { - int l, i, r, tcnt; - int escape = 0, quote_single = 0, quote_double = 0; + size_t l, i; + int escape, quote_single, quote_double; char **a; const char *c; - char *d; - char *sline; + char *d, *sline; - if (line == NULL) + if (!line || !tokens || !token_cnt) { - urj_error_set (URJ_ERROR_INVALID, "NULL line"); + urj_error_set (URJ_ERROR_INVALID, "NULL input(s)"); return URJ_STATUS_FAIL; } + + *token_cnt = 0; + l = strlen (line); if (l == 0) return URJ_STATUS_OK; @@ -69,9 +71,9 @@ urj_parse_line (urj_chain_t *chain, const char *line) } /* count and copy the tokens */ + escape = quote_single = quote_double = 0; c = line; d = sline; - tcnt = 0; while (1) { /* eat up leading spaces */ @@ -111,38 +113,59 @@ urj_parse_line (urj_chain_t *chain, const char *line) } /* mark the end to the destination string */ *d++ = '\0'; - tcnt++; + ++*token_cnt; } - if (tcnt == 0) + if (*token_cnt == 0) { free (sline); return URJ_STATUS_OK; } /* allocate the token pointer table */ - a = malloc ((tcnt + 1) * sizeof (char *)); + l = (*token_cnt + 1) * sizeof (*a); + *tokens = a = malloc (l); if (a == NULL) { - urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "malloc(%zd) fails", - (size_t) ((tcnt + 1) * sizeof (char *))); + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "malloc(%zd) fails", l); return URJ_STATUS_FAIL; } /* find the starting points for the tokens */ d = sline; - for (i = 0; i < tcnt; i++) + for (i = 0; i < *token_cnt; i++) { a[i] = d; while (*d++ != '\0') ; } - a[tcnt] = NULL; + a[*token_cnt] = NULL; + + return URJ_STATUS_OK; +} + +void +urj_tokens_free (char **tokens) +{ + free (tokens[0]); + free (tokens); +} + +int +urj_parse_line (urj_chain_t *chain, const char *line) +{ + int r; + size_t tcnt; + char **a; + + r = urj_tokenize_line (line, &a, &tcnt); + if (r != URJ_STATUS_OK || tcnt == 0) + return r; r = urj_cmd_run (chain, a); urj_log (URJ_LOG_LEVEL_DEBUG, "Return in urj_parse_line r=%d\n", r); - free (a); - free (sline); + + urj_tokens_free (a); return r; } @@ -313,4 +336,3 @@ urj_parse_include (urj_chain_t *chain, const char *filename, int ignore_path) return r; } -