summaryrefslogtreecommitdiffabout
path: root/include
authorSergey Poznyakoff <gray@gnu.org>2018-05-22 10:45:39 (GMT)
committer Sergey Poznyakoff <gray@gnu.org>2018-05-22 11:11:21 (GMT)
commita531c7d1de4780ee53ffbd383329dfff55d425b0 (patch) (side-by-side diff)
tree20290d0b4eb45069bfea28dc96a4c6f362d363b9 /include
parent085d771694655dea82c37247497db32eca228b4a (diff)
downloadgrecs-a531c7d1de4780ee53ffbd383329dfff55d425b0.tar.gz
grecs-a531c7d1de4780ee53ffbd383329dfff55d425b0.tar.bz2
New feature: maxwords
This feature allows the user to limit the number of words returned by a call to wordsplit. When the number of words in expansion reaches the predefined limit, the rest of input line will be expanded and returned as a single last word. For example, to parse a /etc/passwd line: struct wordsplit ws; ws.ws_delim = ":"; ws.ws_maxwords = 7; ws.ws_options = WRDSO_MAXWORDS; wordsplit(str, &ws, WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_DELIM | WRDSF_OPTIONS); * doc/wordsplit.3: Document the maxwords feature. * include/wordsplit.h (wordsplit) <ws_maxwords> <ws_wordi>: New members. (WRDSO_MAXWORDS): New option. * src/wordsplit.c (WSP_RETURN_DELIMS): New macro. (_wsplt_subsplit): Rewrite. (wordsplit_init0): Don't reset node list. (wordsplit_init): Initialize ws_wordi and the node list. (wsnode_insert): Correctly insert lists. (coalesce_segment): Additional safety check. (wsnode_tail_coalesce): New static function. (wordsplit_finish): Postprocess delimiters. (expvar,expcmd): Use new _wsplt_subsplit. (wordsplit_varexp): Don't try to expand delimiter nodes. (skip_delim): Remove delimiter processing. It is now done in wordsplit_finish. (scan_word): New argument 'consume_all' instructs it to consume the rest of input as one token. (wordsplit_process_list): Handle wsp->ws_maxwords setting. This also fixed a long-standing bug: quotes weren't processed in WRDSF_NOSPLIT mode. See the testcase 59 (incremental nosplit). (wordsplit_run): Rewrite. (wordsplit_free): Free node list. * tests/wordsplit.at: Update for the new wsp output format. (incremental nosplit): Expect correct output. Add tests for the maxwords feature. * tests/wsp.c (maxwords): New flag. Print the ws_wordi value as "TOTALS" at the end of each run.
Diffstat (limited to 'include') (more/less context) (ignore whitespace changes)
-rw-r--r--include/wordsplit.h10
1 files changed, 9 insertions, 1 deletions
diff --git a/include/wordsplit.h b/include/wordsplit.h
index 8726cf1..d13ec3c 100644
--- a/include/wordsplit.h
+++ b/include/wordsplit.h
@@ -1,5 +1,5 @@
/* wordsplit - a word splitter
- Copyright (C) 2009-2016 Sergey Poznyakoff
+ Copyright (C) 2009-2018 Sergey Poznyakoff
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
@@ -41,6 +41,11 @@ struct wordsplit
int ws_flags; /* [Input] Flags passed to wordsplit. */
int ws_options; /* [Input] (WRDSF_OPTIONS)
Additional options. */
+ size_t ws_maxwords; /* [Input] (WRDSO_MAXWORDS) Return at most that
+ many words */
+ size_t ws_wordi; /* [Output] (WRDSF_INCREMENTAL) Total number of
+ words returned so far */
+
const char *ws_delim; /* [Input] (WRDSF_DELIM) Word delimiters. */
const char *ws_comment; /* [Input] (WRDSF_COMMENT) Comment characters. */
const char *ws_escape[2]; /* [Input] (WRDSF_ESCAPE) Characters to be escaped
@@ -198,6 +203,9 @@ struct wordsplit
/* Handle hex escapes in words */
#define WRDSO_XESC_WORD 0x00000040
+/* ws_maxwords field is initialized */
+#define WRDSO_MAXWORDS 0x00000080
+
/* Keep backslash in unrecognized escape sequences in quoted strings */
#define WRDSO_BSKEEP_QUOTE 0x00000100
/* Handle octal escapes in quoted strings */

Return to:

Send suggestions and report system problems to the System administrator.