1#!/bin/bash
2
3# This file is part of util-linux.
4#
5# This file is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This file is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15TS_TOPDIR="${0%/*}/../.."
16TS_DESC="highlighting"
17FILES="$TS_TOPDIR/ts/hexdump/files"
18OPTS="--color=always"
19ADDRFMT='-e "%07.7_Ax\n"'
20#sample input consists of hexdump-ed results of the following py3script:
21#for i in range(256):
22#	print(chr(i), end= ' ')
23
24. $TS_TOPDIR/functions.sh
25ts_init "$*"
26
27ts_check_test_command "$TS_CMD_HEXDUMP"
28
29# on big endian systems some of the subtests have different expected output
30BYTE_ORDER=$($TS_HELPER_SYSINFO byte-order)
31BE_EXT=$(test "$BYTE_ORDER" = "BE" && echo ".BE")
32
33#
34# basic formats /w some highlighting tweaks
35#
36
37# highlight 'A' and 'a'
38ts_init_subtest "1b_octal-1"
39$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
40	-e '"%07.7_ax " 16/1 "%03o_L[red:A,red:a] " "\n"' \
41	$FILES/ascii.in &> $TS_OUTPUT
42ts_finalize_subtest
43
44# highlight bytes with the value of 0x41 (A) and 0x61 (a)
45ts_init_subtest "1b_octal-2"
46TS_EXPECTED+=$BE_EXT
47$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
48	-e '"%07.7_ax " 16/1 "%03o_L[red:0x41,red:0x61] " "\n"' \
49	$FILES/ascii.in &> $TS_OUTPUT
50ts_finalize_subtest
51
52# highlight the bytes at offsets 130 and  194 (decimal)
53ts_init_subtest "1b_octal-3"
54$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
55	-e '"%07.7_ax " 16/1 "%03o_L[red@130,red@194] " "\n"' \
56	$FILES/ascii.in &> $TS_OUTPUT
57ts_finalize_subtest
58
59# highlight the byte at offset 194 if it's value is 0x61 (a)
60# and the byte at offset 130 if it's value is A (0x41)
61ts_init_subtest "1b_octal-4"
62TS_EXPECTED+=$BE_EXT
63$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
64	-e '"%07.7_ax " 16/1 "%03o_L[red:A@130,red:0x61@194] " "\n"' \
65	$FILES/ascii.in &> $TS_OUTPUT
66ts_finalize_subtest
67
68# highlight the byte at offset 194 if it's value is 0x61 (a)
69# and the byte at offset 130 if it's value is C (0x41) (false)
70ts_init_subtest "1b_octal-5"
71TS_EXPECTED+=$BE_EXT
72$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
73	-e '"%07.7_ax " 16/1 "%03o_L[red:C@130,red:0x61@194] " "\n"' \
74	$FILES/ascii.in &> $TS_OUTPUT
75ts_finalize_subtest
76
77# highlight a range longer than the byte count of %o
78ts_init_subtest "1b_octal-6"
79$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
80	-e '"%07.7_ax " 16/1 "%03o_L[red@193-194] " "\n"' \
81	$FILES/ascii.in &> $TS_OUTPUT
82ts_finalize_subtest
83
84# highlight bytes with the value of 0101 (A) and 0141 (a)
85ts_init_subtest "1b_octal-7"
86TS_EXPECTED+=$BE_EXT
87$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
88	-e '"%07.7_ax " 16/1 "%03o_L[red:0101,red:0141] " "\n"' \
89	$FILES/ascii.in &> $TS_OUTPUT
90ts_finalize_subtest
91
92# highlight bytes with the value of 0101 (A), 0x61 (a), or if the character is a caret
93ts_init_subtest "1b_octal-8"
94TS_EXPECTED+=$BE_EXT
95$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
96	-e '"%07.7_ax " 16/1 "%03o_L[red:0101,blue:0x61,green:^] " "\n"' \
97	$FILES/ascii.in &> $TS_OUTPUT
98ts_finalize_subtest
99
100# highlight bytes with the value of 0101 (A), 0x61 (a), or if the character at 196 is not 'c'
101ts_init_subtest "1b_octal-9"
102TS_EXPECTED+=$BE_EXT
103$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
104	-e '"%07.7_ax " 16/1 "%03o_L[red:0101,blue:0x61,!green:c@196] " "\n"' \
105	$FILES/ascii.in &> $TS_OUTPUT
106ts_finalize_subtest
107
108#color the addresses red and the characters in UTIL-LINUX green
109ts_init_subtest "1b_char-1"
110$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
111	-e '"%07.7_ax_L[red] " 16/1 "%3_c_L[green:-,green:I,green:L,green:N,green:T,green:U,green:X] " "\n"' \
112	$FILES/ascii.in &> $TS_OUTPUT
113ts_finalize_subtest
114
115#color the address 0xe0 red
116ts_init_subtest "1b_char-2"
117$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
118	-e '"%07.7_ax_L[red:0xe0] " 16/1 "%3_c " "\n"' \
119	$FILES/ascii.in &> $TS_OUTPUT
120ts_finalize_subtest
121
122#highlight ':' and '@'
123ts_init_subtest "1b_char-3"
124$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
125	-e '"%07.7_ax " 16/1 "%3_c_L[red::@116,red:@@128] " "\n"' \
126	$FILES/ascii.in &> $TS_OUTPUT
127ts_finalize_subtest
128
129# color last address gray, the null byte blue, the spaces brown(-ish) and the text cyan
130ts_init_subtest "canon-1"
131TS_EXPECTED+=$BE_EXT
132$TS_CMD_HEXDUMP $OPTS \
133	-e '"%08.8_Ax_L[gray]\n"' \
134	-e '"%08.8_ax  " 8/1 "%02x_L[blue:0x0,brown:0x20] " "  " 8/1 "%02x_L[brown:0x20] " ' \
135	-e '"  |" 16/1 "%_p_L[cyan]" "|\n"' \
136	$FILES/ascii.in &> $TS_OUTPUT
137ts_finalize_subtest
138
139# color last address gray - if it's 0x280 or blue if it's 0x380
140# color A/a hex dumps blue and A/a in text green
141ts_init_subtest "canon-2"
142$TS_CMD_HEXDUMP $OPTS \
143	-e '"%08.8_Ax_L[blue:0x380,red:0x280]\n"' \
144	-e '"%08.8_ax  " 8/1 "%02x_L[blue:A,blue:a] " "  " 8/1 "%02x_L[blue:A,blue:a] " ' \
145	-e '"  |" 16/1 "%_p_L[green:A,green:a]" "|\n"' \
146	$FILES/ascii.in &> $TS_OUTPUT
147ts_finalize_subtest
148
149# color the current print unit if one of the bytes it prints is at offset 100 (0x64)
150ts_init_subtest "2b_dec-1"
151TS_EXPECTED+=$BE_EXT
152$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
153	-e '"%07.7_ax " 8/2 "  %05u_L[red@100] " "\n"' \
154	$FILES/ascii.in &> $TS_OUTPUT
155ts_finalize_subtest
156
157# color the current print unit if of the bytes it prints are at offsets 98 and 99
158ts_init_subtest "2b_dec-2"
159TS_EXPECTED+=$BE_EXT
160$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
161	-e '"%07.7_ax " 8/2 "  %05u_L[red@98-99] " "\n"' \
162	$FILES/ascii.in &> $TS_OUTPUT
163ts_finalize_subtest
164
165# color the current print unit if some or all of the bytes it prints are at offsets 98 and 99
166ts_init_subtest "2b_dec-3"
167TS_EXPECTED+=$BE_EXT
168$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
169	-e '"%07.7_ax " 8/2 "  %05u_L[red@97-99] " "\n"' \
170	$FILES/ascii.in &> $TS_OUTPUT
171ts_finalize_subtest
172
173# color the current print unit if it prints "A " at 130-131
174ts_init_subtest "2b_dec-4"
175TS_EXPECTED+=$BE_EXT
176$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
177	-e '"%07.7_ax " 8/2 "  %05u_L[red:A @130-131] " "\n"' \
178	$FILES/ascii.in &> $TS_OUTPUT
179ts_finalize_subtest
180
181# color the current print unit if it doesn't print "B " at 130-131
182ts_init_subtest "2b_dec-5"
183TS_EXPECTED+=$BE_EXT
184$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
185	-e '"%07.7_ax " 8/2 "  %05u_L[!red:B @130-131] " "\n"' \
186	$FILES/ascii.in &> $TS_OUTPUT
187ts_finalize_subtest
188
189# specify a string shorter than the range - error
190ts_init_subtest "2b_dec-6"
191$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
192	-e '"%07.7_ax " 8/2 "  %05u_L[red:A@130-131] " "\n"' \
193	$FILES/ascii.in &> $TS_OUTPUT
194ts_finalize_subtest
195
196# specify a negative-length range
197ts_init_subtest "2b_dec-7"
198$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
199	-e '"%07.7_ax " 8/2 "  %05u_L[red:A@131-130] " "\n"' \
200	$FILES/ascii.in &> $TS_OUTPUT
201ts_finalize_subtest
202
203# color the current print unit if of the bytes it prints are at offsets 98-102 (multiple print units)
204ts_init_subtest "2b_dec-8"
205TS_EXPECTED+=$BE_EXT
206$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
207	-e '"%07.7_ax " 8/2 "  %05u_L[red@98-102] " "\n"' \
208	$FILES/ascii.in &> $TS_OUTPUT
209ts_finalize_subtest
210
211# color the current print unit if some or all of the bytes it prints are at offsets 97 and 99 (true)
212ts_init_subtest "4b_dec-1"
213TS_EXPECTED+=$BE_EXT
214$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
215	-e '"%07.7_ax " 8/4 "  %05u_L[red@97-99] " "\n"' \
216	$FILES/ascii.in &> $TS_OUTPUT
217ts_finalize_subtest
218
219# color the current print unit if some or all of the bytes it prints are at offsets 96-99
220ts_init_subtest "4b_dec-2"
221TS_EXPECTED+=$BE_EXT
222$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
223	-e '"%07.7_ax " 8/4 "  %05u_L[red@96-99] " "\n"' \
224	$FILES/ascii.in &> $TS_OUTPUT
225ts_finalize_subtest
226
227# look for @@@@
228ts_init_subtest "4b_dec-3"
229TS_EXPECTED+=$BE_EXT
230$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
231	-e '"%07.7_ax " 8/4 "  %05u_L[red:@@@@] " "\n"' \
232	&> $TS_OUTPUT <<< "@@@@"
233ts_finalize_subtest
234
235# look for @@@@ at 0-3
236ts_init_subtest "4b_dec-4"
237TS_EXPECTED+=$BE_EXT
238$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
239	-e '"%07.7_ax " 8/4 "  %05u_L[red:@@@@@0-3] " "\n"' \
240	&> $TS_OUTPUT <<< "@@@@"
241ts_finalize_subtest
242
243# look for @@@ at 0-3 (wrong byte count error)
244ts_init_subtest "4b_dec-5"
245$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
246	-e '"%07.7_ax " 8/4 "  %05u_L[red:@@@@0-3] " "\n"' \
247	&> $TS_OUTPUT <<< "@@@@"
248ts_finalize_subtest
249
250# look for @@@ at 1-3 - in the format boundaries - correct
251ts_init_subtest "4b_dec-6"
252TS_EXPECTED+=$BE_EXT
253$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
254	-e '"%07.7_ax " 8/4 "  %05u_L[red:@@@@1-3] " "\n"' \
255	&> $TS_OUTPUT <<< "@@@@"
256ts_finalize_subtest
257
258# look for @@@ at 2-4 - correct range length, however steps over 2 print units - no colors
259ts_init_subtest "4b_dec-7"
260TS_EXPECTED+=$BE_EXT
261$TS_CMD_HEXDUMP $OPTS $ADDRFMT \
262	-e '"%07.7_ax " 8/4 "  %05u_L[red:@@@@2-4] " "\n"' \
263	&> $TS_OUTPUT <<< "@@@@"
264ts_finalize_subtest
265
266ts_finalize
267