Skip to content

Commit e2bd270

Browse files
authored
Merge pull request #149 from mbland/printf
Add 2017 to copyright, fix @go.printf unconditional newline and infinite loop bugs
2 parents 2e048ee + b010593 commit e2bd270

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2016, Mike Bland <mbland@acm.org>
1+
Copyright (c) 2016-2017 Mike Bland <mbland@acm.org>
22

33
Permission to use, copy, modify, and/or distribute this software for any purpose
44
with or without fee is hereby granted, provided that the above copyright notice

go-core.bash

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,20 +154,21 @@ declare _GO_INJECT_MODULE_PATH="$_GO_INJECT_MODULE_PATH"
154154
local result
155155
local line
156156
local prefix
157-
local IFS=
158157

159158
if [[ "$#" -eq 0 ]]; then
160159
format="${format//\%/%%}"
161160
fi
162161
printf -v result -- "$format" "$@"
163162
result="${result//$'\r\n'/$'\n'}"
164163

165-
# If `result` ends with a newline, chomp it, since the loop will add one.
166-
while read -r line; do
164+
# Use the ASCII Unit Separator as a delimiter to preserve true line endings.
165+
while IFS= read -r -d $'\x1f' line; do
167166
while [[ "${#line}" -gt "$COLUMNS" ]]; do
168167
prefix="${line:0:$COLUMNS}"
169-
prefix="${prefix% *}"
170-
line="${line#$prefix}"
168+
prefix="${prefix%[[:space:]]*}"
169+
170+
# Trim `line` using an index in case `prefix` contains pattern characters.
171+
line="${line:${#prefix}}"
171172

172173
if [[ "$prefix" =~ [[:space:]]+$ ]]; then
173174
prefix="${prefix%${BASH_REMATCH[0]}}"
@@ -177,8 +178,8 @@ declare _GO_INJECT_MODULE_PATH="$_GO_INJECT_MODULE_PATH"
177178
fi
178179
printf '%s\n' "$prefix"
179180
done
180-
printf '%s\n' "$line"
181-
done <<<"${result%$'\n'}"
181+
printf '%s' "$line"
182+
done <<<"${result//$'\n'/$'\n\x1f'}"$'\x1f'
182183
}
183184

184185
# Prints the stack trace at the point of the call.

libexec/get.d/file

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ _@go.get_file_download_command() {
9696
if [[ -n "$dl_cmd" ]]; then
9797
@go.printf 'Download program not supported: %s\n' "$dl_cmd" >&2
9898
fi
99-
@go.printf 'Please install curl or wget before running "%s".' \
99+
@go.printf 'Please install curl or wget before running "%s".\n' \
100100
"${_GO_CMD_NAME[*]}" >&2
101101
return 1
102102
;;
@@ -144,7 +144,7 @@ _@go.get_file_impl() {
144144
if ! "${__go_get_file_download_cmd[@]}" "$url" 2>"$errfile"; then
145145
errmsg="$(< "$errfile")"
146146
printf '%s\n' "$errmsg" >&2
147-
@go.printf 'Failed to download using %s: %s' \
147+
@go.printf 'Failed to download using %s: %s\n' \
148148
"${__go_get_file_download_cmd[0]}" "$url" >&2
149149

150150
if [[ "${__go_get_file_download_cmd[0]}" != 'curl' ]]; then

tests/core/printf.bats

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,26 @@ teardown() {
4949
COLUMNS=15 run "$TEST_GO_SCRIPT"
5050
assert_success $'123456789012345\n67890\n1234567890'
5151
}
52+
53+
@test "$SUITE: don't add newline if format doesn't include one" {
54+
@go.create_test_go_script "@go.printf '%s' 'foo'" \
55+
"@go.printf '%s' 'bar'" \
56+
"@go.printf '%s' 'baz'"
57+
run "$TEST_GO_SCRIPT"
58+
assert_success 'foobarbaz'
59+
}
60+
61+
@test "$SUITE: don't loop infinitely if line contains pattern characters" {
62+
# `[alias]` and `[builtin]` were previously interpreted as character sets, not
63+
# literal strings in and of themselves, when appering in `$prefix` in the
64+
# expression `${line#$prefix}`.
65+
local test_string=' path '
66+
test_string+='Prints the path to the <command> script, [alias] or [builtin]'
67+
68+
@go.create_test_go_script "@go.printf '%s' '$test_string'"
69+
COLUMNS=27 run "$TEST_GO_SCRIPT"
70+
assert_success ' path Prints the' \
71+
'path to the <command>' \
72+
'script, [alias] or' \
73+
'[builtin]'
74+
}

0 commit comments

Comments
 (0)