1#!/bin/sh
2# basic tests for echo
3
4# Copyright (C) 2018-2023 Free Software Foundation, Inc.
5
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19prog='env echo'
20
21. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
22print_ver_ echo
23
24
25# Verify the methods of specifying "Escape":
26# Note 4 octal digits are allowed (unlike printf which uses up to 3)
27printf '%s\n' . . . . . | tr . '\033' > exp
28$prog -n -e '\x1b\n\e\n\33\n\033\n\0033\n' > out || fail=1
29compare exp out || fail=1
30
31# Incomplete hex escapes are output as is (unlike printf)
32printf '\\x\n' > exp
33$prog -n -e '\x\n' > out || fail=1
34compare exp out || fail=1
35
36# Always output -- (unlike printf)
37$prog -- 'foo' > out || fail=1
38$prog -n -e -- 'foo\n' >> out || fail=1
39cat <<\EOF > exp
40-- foo
41-- foo
42EOF
43compare exp out || fail=1
44
45# Ensure \c stops processing
46$prog -e 'foo\n\cbar' > out || fail=1
47printf 'foo\n' > exp
48compare exp out || fail=1
49
50# With POSIXLY_CORRECT:
51#   only -n as the first (separate) option enables option processing
52#   -E is ignored
53#   escapes are processed by default
54POSIXLY_CORRECT=1 $prog -n -E 'foo\n' > out || fail=1
55POSIXLY_CORRECT=1 $prog -nE 'foo' >> out || fail=1
56POSIXLY_CORRECT=1 $prog -E -n 'foo' >> out || fail=1
57POSIXLY_CORRECT=1 $prog --version >> out || fail=1
58cat <<\EOF > exp
59foo
60-nE foo
61-E -n foo
62--version
63EOF
64compare exp out || fail=1
65
66# Further test coverage.
67# Output a literal '-' (on a line itself).
68$prog - > out || fail=1
69# Output a literal backslash '\', no newline.
70$prog -n -e '\\' >> out || fail=1
71# Output an empty line (merely to have a newline after the previous test).
72$prog >> out || fail=1
73# Test other characters escaped by a backslash:
74# \a  hex 07  alert, bell
75# \b  hex 08  backspace
76# \e  hex 1b  escape
77# \f  hex 0c  form feed
78# \n  hex 0a  new line
79# \r  hex 0d  carriage return
80# \t  hex 09  horizontal tab
81# \v  hex 0b  vertical tab
82# Convert output, yet checking the exit status of $prog.
83{ $prog -n -e '\a\b\e\f\n\r\t\v' || touch fail; } | od -tx1 >> out || fail=1
84test '!' -f fail || fail=1
85# Output hex values which contain hexadecimal characters to test hextobin().
86# Hex values 4a through 4f are ASCII "JKLMNO".
87$prog -n -e '\x4a\x4b\x4c\x4d\x4e\x4f\x4A\x4B\x4C\x4D\x4E\x4F' >> out || fail=1
88# Output another newline.
89$prog >> out || fail=1
90cat <<\EOF > exp
91-
92\
930000000 07 08 1b 0c 0a 0d 09 0b
940000010
95JKLMNOJKLMNO
96EOF
97compare exp out || fail=1
98
99Exit $fail
100