aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMu Qiao <qiaomuf@gentoo.org>2012-03-26 16:08:40 +0800
committerMu Qiao <qiaomuf@gentoo.org>2012-03-26 16:35:47 +0800
commitc5fb1ffc57b41faa6118e36b2ae925561915fc6a (patch)
tree9469201ad08b72fa4be7cd7f27a38f8224b24c78
parentWalker: allow empty case command (diff)
downloadlibbash-c5fb1ffc57b41faa6118e36b2ae925561915fc6a.tar.gz
libbash-c5fb1ffc57b41faa6118e36b2ae925561915fc6a.tar.bz2
libbash-c5fb1ffc57b41faa6118e36b2ae925561915fc6a.zip
Builtin: fix argument handling of printf builtin
-rw-r--r--scripts/command_execution.bash3
-rw-r--r--src/builtins/printf_builtin.cpp20
2 files changed, 18 insertions, 5 deletions
diff --git a/scripts/command_execution.bash b/scripts/command_execution.bash
index 615b13f..0927642 100644
--- a/scripts/command_execution.bash
+++ b/scripts/command_execution.bash
@@ -62,6 +62,9 @@ eval "echo abc" "def" "xyz"
shopt -s extglob
printf "%s %s\n" abc def
printf "%s %s\n" $FOO001, def
+printf "123-%s" 456 789
+printf "\n"
+printf "123-%s"
((FOO010=1))
echo $FOO010
echo "abc #av### ##" # for comment
diff --git a/src/builtins/printf_builtin.cpp b/src/builtins/printf_builtin.cpp
index db40fff..0d2ef1c 100644
--- a/src/builtins/printf_builtin.cpp
+++ b/src/builtins/printf_builtin.cpp
@@ -41,23 +41,33 @@ int printf_builtin::exec(const std::vector<std::string>& bash_args)
std::stringstream format_string;
cppbash_builtin::transform_escapes(*begin, format_string, false);
boost::format formatter(format_string.str());
+ formatter.exceptions(boost::io::all_error_bits ^ boost::io::too_few_args_bit);
+
+ std::stringstream output;
for(auto iter = begin + 1; iter != bash_args.end(); ++iter)
- formatter = formatter % *iter;
+ try
+ {
+ formatter = formatter % *iter;
+ }
+ catch(const boost::io::too_many_args& e)
+ {
+ output << formatter;
+ formatter.parse(format_string.str());
+ formatter = formatter % *iter;
+ }
+ output << formatter;
if(!(bash_args[0][0] == '-'))
{
- *_out_stream << formatter;
+ *_out_stream << output.str();
}
else if(bash_args[0] == "-v")
{
- std::stringstream output;
- output << formatter;
_walker.set_value(bash_args[1], output.str());
}
else
{
throw libbash::illegal_argument_exception("printf: invalid option: " + bash_args[0]);
}
-
return 0;
}