Skip to content

Commit 3c80fc3

Browse files
authored
Merge pull request #62 from matejak/double_quotes
Escape double quotes for help messages iff the provided string is unquoted
2 parents 49c80e5 + 71ba74f commit 3c80fc3

File tree

8 files changed

+29
-14
lines changed

8 files changed

+29
-14
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ New features:
55

66
* Allow argbash and argbash-init to be run from symbolic links.
77
* Allow scripts generated by argbash-init with complete separation (`-s -s`) to be run from a symbolic link.
8+
* Double quotes in help messages are escaped (fixes #61).
89

910

1011
2.7.1 (2018-08-15)

src/stuff.m4

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ m4_define([_MAKE_HELP_FUNCTION_POSITIONAL_PART], [m4_lists_foreach_positional(
229229
],
230230
[m4_pushdef([argname1], <m4_dquote(argname0)[[]m4_ifnblank(m4_quote($][1), m4_quote(-$][1))]>)],
231231
[m4_pushdef([argname], m4_if(_arg_type, [inf], [m4_default(_INF_REPR, argname1)], [[argname1($][@)]]))],
232-
[_INDENT_()[printf '\t%s\n' "]argname[: ]_SUBSTITUTE_LF_FOR_NEWLINE_AND_INDENT(_msg)],
232+
[_INDENT_()[printf '\t%s\n' "]argname[: ]_SUBSTITUTE_LF_FOR_NEWLINE_WITH_DISPLAY_INDENT_AND_ESCAPE_DOUBLEQUOTES(_msg)],
233233
[_POS_ARG_HELP_DEFAULTS([argname], _arg_type, _min_argn, _defaults)],
234234
[m4_popdef([argname])],
235235
[m4_popdef([argname1])],
@@ -252,7 +252,7 @@ m4_define([_MAKE_PRINTF_OPTARG_HELP_STATEMENTS], [m4_do(
252252
[string], [],
253253
[_GET_VALUE_DESC([$1])])])])],
254254
[m4_pushdef([_options], [$6([$1], [$2], [$3])])],
255-
[m4_pushdef([_help_msg], [_SUBSTITUTE_LF_FOR_NEWLINE_AND_INDENT([$5])])],
255+
[m4_pushdef([_help_msg], [_SUBSTITUTE_LF_FOR_NEWLINE_WITH_DISPLAY_INDENT_AND_ESCAPE_DOUBLEQUOTES([$5])])],
256256
[m4_case([$3],
257257
[action],
258258
[_INDENT_()[printf '\t%s\n'] "_options: _help_msg"],
@@ -343,7 +343,7 @@ m4_define([_MAKE_HELP], [MAKE_FUNCTION(
343343
[and it makes sense to remind the user how the script is supposed to be called.]],
344344
[print_help], [m4_do(
345345
[m4_ifnblank(m4_expand([_HELP_MSG]),
346-
m4_dquote(_INDENT_()[printf] '%s\n' "_SUBSTITUTE_LF_FOR_NEWLINE_AND_INDENT(_HELP_MSG)"_ENDL_()))],
346+
m4_dquote(_INDENT_()[printf] '%s\n' "_SUBSTITUTE_LF_FOR_NEWLINE_WITH_DISPLAY_INDENT_AND_ESCAPE_DOUBLEQUOTES(_HELP_MSG)"_ENDL_()))],
347347
[_INDENT_()[]printf 'Usage: %s],
348348
[dnl If we have optionals, display them like [--opt1 arg] [--(no-)opt2] ... according to their type. @<:@ becomes square bracket at the end of processing
349349
],
@@ -1079,6 +1079,7 @@ dnl $1: argname macro
10791079
dnl $2: _arg_type
10801080
dnl $3: _min_argn
10811081
dnl $4: _defaults
1082+
dnl TODO: The changed doublequote-escape behavior may have ugly side-effects.
10821083
dnl
10831084
m4_define([_MAKE_DEFAULTS_FOR_MULTIVALUED_ARGUMENTS], [m4_do(
10841085
[@{:@],

src/utilities.m4

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ m4_define([_CHECK_INTEGER_TYPE],
4949
5050
5151
dnl
52-
dnl Encloses string into "" if its first char is not ' or "
52+
dnl If first char of string is not ' or " enclose it into ""
53+
dnl and escape " with \".
54+
dnl
5355
dnl The string is also []-quoted
5456
dnl Property: Quoting a blank input results in blank result
5557
dnl to AVOID it, pass string like ""ls -l or "ls" -l
@@ -58,9 +60,7 @@ dnl $1: String to quote
5860
m4_define([_sh_quote], [m4_do(
5961
[m4_if(
6062
[$1], , ,
61-
m4_index([$1], [']), 0, [[$1]],
62-
m4_index([$1], ["]), 0, [[$1]],
63-
[["$1"]])],
63+
m4_dquote(_sh_quote_also_blanks([$1])))],
6464
)])
6565
6666
@@ -74,7 +74,6 @@ m4_define([_sh_quote_also_blanks], [m4_do(
7474
[["$1"]])],
7575
)])
7676
77-
7877
dnl
7978
dnl Define a macro that is part of the public API
8079
dnl Ensure the replication and also add the macro name to a list of allowed macros
@@ -120,13 +119,17 @@ m4_define([_COMMENT], [m4_ifdef([COMMENT_OUTPUT], [$1])])
120119
dnl
121120
dnl $1: The text to substitute
122121
dnl Regexp: Find beginning of backslashes, match for pairs, and if \\n is left, then substitute it for literal newline.
123-
m4_define([_SUBSTITUTE_LF_FOR_NEWLINE_AND_INDENT], [m4_bpatsubst([[$1]], [\([^\\]\)\(\\\\\)*\\n], [\1\2
124-
])])
122+
dnl The indentation is a display indentation - not source code one.
123+
m4_define([_SUBSTITUTE_LF_FOR_NEWLINE_WITH_DISPLAY_INDENT_AND_ESCAPE_DOUBLEQUOTES],
124+
[m4_bpatsubsts([[$1]],
125+
[\([^\\]\)\(\\\\\)*\\n], m4_expand([[\1\2]_ENDL_() ]),
126+
[\([^\]\)"], [\1\\"])])
125127
126128
127129
m4_define([_CHECK_PASSED_ARGS_COUNT_TOO_FEW],
128130
[m4_fatal([You have passed $2 arguments to macro $1, while it requires at least $3.]m4_ifnblank([$4], [ Call it like: $4]))])
129131
132+
130133
m4_define([_CHECK_PASSED_ARGS_COUNT_TOO_MANY],
131134
[m4_fatal([You have passed $2 arguments to macro $1, while it accepts at most $3.]m4_ifnblank([$4], [ Call it like: $4]))])
132135

tests/regressiontests/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ test-simple: $(TESTDIR)/test-simple.sh
357357
$< -h | grep -q '^ -BEGIN'
358358
$< -h | grep -q -v '^\s*-BEGIN2'
359359
$< -h | grep -q -v 'END2-$$'
360+
$< -h | grep -q '"line 2" END-\\n'
360361
$< -h | grep -q '^ -PBEGIN'
361362
$< -h | grep -q 'PEND-$$'
362363
grep -q '^ esac' $<
@@ -376,6 +377,7 @@ test-simple-dash: $(TESTDIR)/test-simple-dash.sh
376377
$< -h | grep -q '^ -BEGIN'
377378
$< -h | grep -q -v '^\s*-BEGIN2'
378379
$< -h | grep -q -v 'END2-$$'
380+
$< -h | grep -q '"line 2" END-\\n'
379381
$< -h | grep -q '^ -PBEGIN'
380382
$< -h | grep -q 'PEND-$$'
381383
grep -q '^ esac' $<

tests/regressiontests/make/tests/tests-base.m4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ m4_define([test_simple_body], [[
130130
$< -h | grep -q '^ -BEGIN'
131131
$< -h | grep -q -v '^\s*-BEGIN2'
132132
$< -h | grep -q -v 'END2-$$'
133+
$< -h | grep -q '"line 2" END-\\n'
133134
$< -h | grep -q '^ -PBEGIN'
134135
$< -h | grep -q 'PEND-$$'
135136
grep -q '^ esac' $<

tests/regressiontests/test-simple.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash -e
22

33
# ARG_POSITIONAL_SINGLE([pos-arg], [help line PEND-\n-PBEGIN])
4-
# ARG_OPTIONAL_SINGLE([prefix],[o],[help line END-\n-BEGIN line 2END-\\n-2BEGIN],[x])
4+
# ARG_OPTIONAL_SINGLE([prefix],[o],[help line END-\n-BEGIN "line 2" END-\\n-2BEGIN],[x])
55
# ARG_VERSION([echo "$0 FOO"])
66
# ARG_HELP([Testing program m4_fatal(BOOM!)], [m4_fatal([CRASH!])])
77
# ARG_DEFAULTS_POS()

tests/unittests/check-indentation.m4

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,14 @@ assert_equals(_POSSIBLY_REPEATED_COMMENT_BLOCK([comment-topic], [comment at ther
5757
assert_equals(_POSSIBLY_REPEATED_COMMENT_BLOCK([comment-topic], [here], 1, x, [BOMB]), [ # comment at there BOMB
5858
])
5959

60-
assert_equals(_SUBSTITUTE_LF_FOR_NEWLINE_AND_INDENT([first\nsecond]), [first
60+
assert_equals(_SUBSTITUTE_LF_FOR_NEWLINE_WITH_DISPLAY_INDENT_AND_ESCAPE_DOUBLEQUOTES([first\nsecond]), [first
6161
second])
62-
63-
assert_equals(_SUBSTITUTE_LF_FOR_NEWLINE_AND_INDENT([first\\nsecond]), [first\\nsecond])
62+
assert_equals(_SUBSTITUTE_LF_FOR_NEWLINE_WITH_DISPLAY_INDENT_AND_ESCAPE_DOUBLEQUOTES([first\nsecond\nthird]), [first
63+
second
64+
third])
65+
assert_equals(_SUBSTITUTE_LF_FOR_NEWLINE_WITH_DISPLAY_INDENT_AND_ESCAPE_DOUBLEQUOTES([first\\nsecond]), [first\\nsecond])
66+
assert_equals(_SUBSTITUTE_LF_FOR_NEWLINE_WITH_DISPLAY_INDENT_AND_ESCAPE_DOUBLEQUOTES(x "y z"), [x \"y z\"])
67+
assert_equals(_SUBSTITUTE_LF_FOR_NEWLINE_WITH_DISPLAY_INDENT_AND_ESCAPE_DOUBLEQUOTES([x \"m4_ignore() z"]), [x \"m4_ignore() z\"])
6468

6569
assert_equals(m4_quote(_COMMENT_CHAIN([BOMB], [two])), [BOMB,two])
6670

tests/unittests/check-utils.m4

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ assert_equals(m4_lists_foreach([FOO,BAR,BAZ],[fu,ba,za],[fu: ba-za@]),
4141

4242
assert_equals(_sh_quote(), [])
4343
assert_equals(_sh_quote(x), ["x"])
44+
assert_equals(_sh_quote([m4_ignore()]), ["m4_ignore()"])
4445
assert_equals(_sh_quote("x"), ["x"])
46+
assert_equals(_sh_quote("x "f f""), ["x "f f""])
4547
assert_equals(_sh_quote('x'), ['x'])
48+
assert_equals(_sh_quote(['x "m4_ignore()\"']), ['x "m4_ignore()\"'])
4649

4750
assert_equals(_sh_quote_also_blanks(), [""])
4851
assert_equals(_sh_quote_also_blanks(x), ["x"])

0 commit comments

Comments
 (0)