Commit graph

82 commits

Author SHA1 Message Date
Sergey Poznyakoff
7eaa3c45be Allow the caller to modify variable name constituents.
This new feature makes it possible to expand variables with
dots or other unusual characters in their names.

* README: Update.
* wordsplit.3: Document the use of the ws_namechar member.
* wordsplit.c (is_name_char): New static function.
(ISVARCHR): Removed. Use is_name_char instead.
(_wsplt_seterr): Set errno to EINVAL if WRDSE_USAGE is returned.
(_wsplt_subsplit): Clear the WRDSO_MAXWORDS option in the subsplit.
Pass ws_namechar.
(wordsplit_init): Check for valid ws_namechar content.
(expvar): Take into account ws_namechar when scanning variable name.
Fix name length passed to the _wsplt_setctxerr call.
* wordsplit.h (ws_namechar): New member.
(WRDSO_NAMECHAR): New option bit.
* wsp.c: New options: -D to define an "extra" environment entry
(possibly containing characters not allowed by the shell), and
-namechar to define additional variable name constituents.
* wordsplit.at: Test namechar modifications.
2019-07-24 13:36:25 +03:00
Sergey Poznyakoff
6a7581f2e6 Change the testsuite files to be easily incorporated into an existing testsuite
* README: Rewrite the testuite section.
* wordsplit.at: Remove AT_INIT
* wsp.c: Include wordsplit-version.h before wordsplit.h
2019-07-11 11:14:52 +03:00
Sergey Poznyakoff
6ccb9ad200 Minor fixes. 2019-07-10 19:48:02 +03:00
Sergey Poznyakoff
d36275fe9a Improve docs 2019-07-10 09:54:32 +03:00
Sergey Poznyakoff
5742ab5a03 Modify the VPATH instructions 2019-07-09 22:34:26 +03:00
Sergey Poznyakoff
4cd8bec42c Improve the docs 2019-07-09 12:21:26 +03:00
Sergey Poznyakoff
92f0904c20 Minor fixes.
* wordsplit.c (_wsplt_error): Provide the format attribute.
(wsplt_assign_param): Add missing static qualifier.
2019-07-08 21:41:32 +03:00
Sergey Poznyakoff
813e12d6e6 Update manpage 2019-07-07 16:32:19 +03:00
Sergey Poznyakoff
d5a0b05f52 Add the bootstrap script
* .gitignore: New file.
* bootstrap: New file.
* wordsplit.at: Convert to stand-alone script.
* wsp.c: Support the --version option.
2019-07-07 16:15:32 +03:00
Sergey Poznyakoff
42acf86a10 Get rid of the grecs dependencies 2019-06-25 17:28:26 +03:00
Sergey Poznyakoff
e2f87cfb91 Flatten the source tree 2019-06-25 14:31:09 +03:00
Sergey Poznyakoff
f00da62cde Configurable order of lookups if both WRDSF_ENV and WRDSF_GETVAR are set
* src/wordsplit.h (WRDSO_GETVARPREF): New option.
* src/wordsplit.c (wordsplit_find_env): Rewrite as wsplt_env_lookup
wsplt_env_getvar): New function.
(expvar): Select preference of wsplt_env_lookup vs. wsplt_env_getvar
depending on the value if WRDSO_GETVARPREF option.
2019-06-13 14:20:20 +03:00
Sergey Poznyakoff
188bbb90d6 Bugfix
* src/wordsplit.c (_wsplt_store_errctx): Fix typo in the error message.
(wsplt_assign_var): Fix a +1 offset of the ws_envidx field.
2019-06-13 13:54:18 +03:00
Sergey Poznyakoff
b4fefca052 Special handling for WRDSE_USAGE.
* src/wordsplit.c (wordsplit_run): Initialize ws_errctx early. This
ensures that wordsplit_strerror gets correct data after WRDSE_USAGE.
(wordsplit_free): Return immediately if errno was WRDSE_USAGE.
2019-06-05 16:34:43 +03:00
Sergey Poznyakoff
6c37c44ee0 Minor fix.
* src/wordsplit.c (_wsplt_seterr_sub): Propagate error context.
2019-05-16 09:23:20 +03:00
Sergey Poznyakoff
e4bd61ae6b Introduce error context
* include/wordsplit.h (wordsplit) <ws_errctx>: New field.
* src/wordsplit.c (wordsplit_init): Initialize ws_errctx.
(expvar,wordsplit_pathexpand): Save error context.
(wordsplit_clearerr): Free error context.
(wordsplit_perror): Use error context if available.
* tests/wordsplit.at: Reflect changes.
2019-05-15 16:37:09 +03:00
Sergey Poznyakoff
070813ec0e Bugfix
* src/wordsplit.c (wordsplit_finish): Fix typo.
(wsplt_assign_param): Use _wsplt_seterr
2019-05-15 14:29:33 +03:00
Sergey Poznyakoff
c62f40dee0 Fix behaviour of $* and $@ in quoted context 2019-05-14 22:25:26 +03:00
Sergey Poznyakoff
a19f8397ed Whitespace cleanup 2019-05-14 19:59:07 +03:00
Sergey Poznyakoff
a2c81cc5fa Implement $* and $@ 2019-05-14 19:57:42 +03:00
Sergey Poznyakoff
c884936422 Rewrite positional parameters implementation
This improves 3e07e3ad

* include/wordsplit.h (ws_paramv,ws_paramc)
(ws_parambuf,ws_paramidx,ws_paramsiz): New fields.
(WRDSO_PARAMV,WRDSO_PARAM_NEGIDX): New options.
(WRDSE_BADPARAM): New error code.
(wordsplit_free_parambuf): New proto.
* src/wordsplit.c (wordsplit_init): Initialize new fields.
(wsplt_assign_var): Fix double-free and memory leak.
(expvar): Expand positional parameters.
(begin_var_p): Add '#'
(wordsplit_free_envbuf): Fix condition.
(wordsplit_free_parambuf): New function.
(wordsplit_free): Call wordsplit_free_parambuf.
(_wordsplit_errstr): New error description.
* tests/wordsplit.at: Update wsp invocations.
Test positional parameters.
* tests/wsp.c: Rewrite.
2019-05-14 15:43:25 +03:00
Sergey Poznyakoff
c01a4a61e8 Optionally disable splitting of unexpandable variable and command refs
* include/wordsplit.h (WRDSO_NOVARSPLIT)
(WRDSO_NOCMDSPLIT): New options.
* src/wordsplit.c (scan_word): Treat any variable reference, even
containing whitespace, as a single word if WRDSO_NOVARSPLIT is set.
Ditto for commands and WRDSO_NOCMDSPLIT.
* tests/wordsplit.at: Add new tests.
* tests/wsp.c: Recognize novarsplit and nocmdsplit
options.
For future use: recognize bskeep_words, bskeep_quote, bskeep.
2019-05-13 15:20:24 +03:00
Sergey Poznyakoff
65d0759a8b Return single empty string on empty input in WRDSF_NOSPLIT mode
* src/wordsplit.c (wordsplit_finish): Make sure at one non-null
argument is always returned in WRDSF_NOSPLIT mode.
* tests/wordsplit.at: Add new test.
2019-05-13 14:52:18 +03:00
Sergey Poznyakoff
cfb04c02a5 Improve variable handling.
Positional variables ($N and ${N}) are recognized. Variable names in
curly braces follow the same rules as unadorned ones.
This commit also changes memory reallocation strategy in wsplt_assign_var.
If ws_envbuf needs to be expanded, new allocation size is selected as
3/2 of the previous allocation, if that size is less than max(size_t).
2019-04-20 09:47:40 +03:00
Sergey Poznyakoff
14c94c292e Update docs 2018-11-15 10:14:33 +02:00
Sergey Poznyakoff
5bec17fbbb Port mailutils commit 2c2e3c50d6
* include/wordsplit.h (WRDSO_ARGV): Remove.
* src/wordsplit.c (expcmd): Always split command line into arguments.
This fixes https://savannah.gnu.org/bugs/?54830
* tests/wsp.c: Implement internal commands, instead of
calling shell ones. This fixes https://savannah.gnu.org/bugs/?54829.
* tests/wordsplit.at: Rewrite command expansion tests.
2018-11-15 09:58:01 +02:00
Sergey Poznyakoff
45fd7f0539 Fix a testcase.
* tests/wordsplit.at (append + dooffs + env): Disable environment.
2018-11-15 08:52:02 +02:00
Sergey Poznyakoff
8631a5c741 Minor fixes
* src/wordsplit.c (find_closing_paren): paren is char const *
(wsplt_unquote_char, wsplt_quote_char): mark as static
(exptab) <descr>: mark as const.
2018-07-31 11:35:35 +03:00
Sergey Poznyakoff
384c7f6fcb Reword a comment in wordsplit.h 2018-06-04 11:11:20 +03:00
Sergey Poznyakoff
8923c8c06f Minor fix
* src/wordsplit.c (wordsplit_run): Return consistent error
code.
2018-05-22 14:47:44 +03:00
Sergey Poznyakoff
9ccdca6ec3 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.
2018-05-22 14:11:21 +03:00
Sergey Poznyakoff
ad614e6c72 Fix typo in wordsplit.h 2018-05-21 10:01:40 +03:00
Sergey Poznyakoff
35b47a80c6 Minor improvement
* src/wordsplit.c (expvar): Gracefully handle NULL values in
ENV_KV environment.
* src/wordsplit.h: Fix typo in a comment.
* doc/wordsplit.3: Update.
2018-02-20 16:46:31 +02:00
Sergey Poznyakoff
d97c672079 Pull fixes from mailutils
This includes the following commits pushed between
2015-09-19 and 2017-10-10:

090c7b9a       Allow ws_getvar to set value to NULL and
return MU_WRDSE_OK.
The value is processed as if it were "", i.e.
MU_WRDSE_UNDEF is returned.
64313fdf       Fix MU_WRDSF_INCREMENTAL | MU_WRDSF_NOSPLIT
46d7640f       Add wordsplit_append function
151eb4b9       Fix nested expansions and command expansions
occurring after variable expansions.
ad3cc340       Replace void wordsplit_getwords with
int wordsplit_get_words.

* include/wordsplit.h (wordsplit_get_words): New function.
(wordsplit_getwords): Mark as deprecated.
(wordsplit_append): New function.
* src/wordsplit.c (wordsplit_append): New function. MU 46d7640f.
(expvar): Treat NULL value as "". MU 090c7b9a.

(expcmd): Allow command and variable expansions in subsplit.
(exptab): Change ordering of expansions so that command expansion
occurs first. This fixes nested expansions and command expansions
occurring after variable expansions. MU 151eb4b9.

(wordsplit_process_list): Update wsp->ws_endp in nosplit mode. This
fixes wordsplit MU_WRDSF_INCREMENTAL | MU_WRDSF_NOSPLIT. MU 64313fdf.

(wordsplit_get_words): New function. MU ad3cc340.

* tests/wordsplit.at: Test the above changes.
* tests/wsp.c: Accept extra arguments to append using wordsplit_append.
2017-11-10 10:42:12 +02:00
Sergey Poznyakoff
b24ce9fe1e Bugfix
* src/wordsplit.c: Always pass format strings as first argument
to wsp->ws_error.
2016-11-21 13:00:54 +02:00
Sergey Poznyakoff
89d57077d3 Move wordsplit.h from src to include 2016-07-04 15:41:59 +03:00
Sergey Poznyakoff
ff4a9351bb Update copyright years 2016-07-04 13:39:42 +03:00
Sergey Poznyakoff
a38b3d7418 Remove superfluous i18n marker 2016-02-23 11:16:22 +02:00
Sergey Poznyakoff
85ece41c6d Use size_t for wordc
* src/wordsplit.c (wordsplit_getwords): Word count is size_t
* src/wordsplit.h (wordsplit_getwords): Fix proto.
2016-01-21 08:19:17 +02:00
Sergey Poznyakoff
6d2bc16d3d Bugfix in test suite
* tests/wsp.c (make_env_kv): Fix array size
2015-12-19 16:59:50 +02:00
Sergey Poznyakoff
cb100a7faa Minor improvement
* src/wordsplit.h (WRDSX_WORD, WRDSX_QUOTE): New defines.
* src/wordsplit.c: Use them when initializing ws_escape.
2015-12-17 15:26:56 +02:00
Sergey Poznyakoff
6dba42a631 Update copyright years 2015-12-17 15:26:55 +02:00
Sergey Poznyakoff
cadf077236 Fix ws trimming and return_delim option
* src/wordsplit.c (_WSNF_DELIM): New node flag
(wsnode_flagstr): Print 'd' for that flag.
(expvar): Don't set WRDSF_WS when expanding variable
value.  This fixes, e.g., expansion of a$x if x begins with
a whitespace.
(wsnode_nullelim): A _WSNF_DELIM node breaks join chain.
(wordsplit_trimws): Don't affect nodes within join chains.
(scan_word): Set _WSNF_DELIM for returned delimiters.
* tests/wordsplit.at (TESTWSP): 8th argument - prologue
shell code.  Use it to unset environment variables in
env/getenv tests.
Test ws elimination with return_delims set.
2015-12-17 15:26:28 +02:00
Sergey Poznyakoff
1939ae3326 Minor fixes in documentation 2015-12-17 15:26:28 +02:00
Sergey Poznyakoff
3fca27c20b Minor fixes
* doc/wordsplit.3: Fix some wording.
* tests/wordsplit.at (TESTWSP): Generate unique keywords.
2015-12-17 15:26:28 +02:00
Sergey Poznyakoff
b712b11eef Improve docs, add new function
* src/wordsplit.c (wordsplit_getwords): New functon.
* src/wordsplit.h (wordsplit_getwords): New proto.
* doc/wordsplit.3: Add example section, document wordsplit_getwords.
2015-12-17 15:26:28 +02:00
Sergey Poznyakoff
943d725e7f Finish wordsplit docs, improve tests 2015-12-17 15:26:28 +02:00
Sergey Poznyakoff
9bebcfbc1c Implement default assignments, etc.
* src/wordsplit.c: Implement default assignment, word
expansion in variable defaults, distinction between
${variable:-word} and ${variable-word}.
* doc/wordsplit.3: New file.
* src/wordsplit.h (wordsplit)<ws_envbuf,ws_envidx>
<ws_envsiz>: New members.
(WRDSF_ARGV): Remove.
(WRDSF_OPTIONS): New flag.
(WRDSO_ARGV): New option bit.
* tests/wordsplit.at: Add new tests.
* tests/wsp.c: Set WRDSF_OPTIONS flag if one of the options is requested.
2015-12-17 15:26:28 +02:00
Sergey Poznyakoff
73180a045d Improve wordsplit documentation and debug output. 2015-12-17 15:25:24 +02:00
Sergey Poznyakoff
731d730447 Test pathname expansion 2015-12-17 15:25:24 +02:00