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