-
Notifications
You must be signed in to change notification settings - Fork 0
/
showInvisibles
executable file
·463 lines (369 loc) · 12.9 KB
/
showInvisibles
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#!/usr/bin/env perl -w
#
# showInvisibles: make control and whitespace chars visible.
# 2007-01-16: Written by Steven J. DeRose.
#
use strict;
use Getopt::Long;
use HTML::Entities;
use sjdUtils;
use alogging;
our %metadata = (
'title' => "showInvisibles",
'description' => "Make control, whitespace, nonASCII chars visible.",
'rightsHolder' => "Steven J. DeRose",
'creator' => "http://viaf.org/viaf/50334488",
'type' => "http://purl.org/dc/dcmitype/Software",
'language' => "Perl 5.18",
'created' => "2007-01-16",
'modified' => "2022-07-25",
'publisher' => "http://github.com/sderose",
'license' => "https://creativecommons.org/licenses/by-sa/3.0/"
);
our $VERSION_DATE = $metadata{'modified'};
=pod
=head1 Usage
showinvisible [options]
Make various characters visible, such as control characters, whitespace,
non-ASCII, etc. Replace them by backslash codes, Unicode control pictures, etc.
By default:
=over
=item * control characters are converted to the Unicode "control pictures"
(miniature mnemonics). Suppress this with --no-controls.
=item * Non-ASCII characters are converted according to
I<--oformat>, which default to "PY2" (2-hex-digit Python escapes like \\xFF,
or larger \\uFFFF or \\U000FFFFF when needed).
=back
By default, characters are converted to UTF-8, then the UTF-8 bytes
are coded as \xNN.
By default, ASCII characters SPACE, LF, and TAB I<are not> replaced.
But see I<-s> to replace all of them, or I<--spaceAs>, I<--lfAs>, and I<tabAs>
to do just those in a variety of ways).
=head1 Options
=over
=item * B<--color>
Colorize non-ASCII characters in the output.
Not the default, even if environment variable C<CLI_COLOR> is set.
See also I<--whichColor>.
=item * B<--controls>
Map C0 control characters (U+0000 to U+001F) to Unicode control Pictures.
Default; use --no-controls to turn off. See also I<--lfAs> and <--spaceAs>.
=item * B<---iencoding> I<e>
Assume the input character encoding is I<e>.
The output encoding is always UTF-8.
=item * B<--lfAs> I<l>
(not yet implemented) Display newline/linefeed (U+000A) as:
I<LF> (a little "LF", U+2424) (default)
I<OK> leave as literal newline
I<NL> (a little "NL", U+2440 -- the default)
=item * B<--oformat> [x]
Choose a different way to represent the invisible characters.
The default is "PY4".
Taking BULLET (U+2022) as an example, the formats and their results are:
UPLUS: U+02022
UTF: \\xe2\\x80\\xa2
URI: %e2%80%a2
DENT: •
XENT: •
NENT: • (per HTML 4. If no name, uses XENT)
PY2: \\u2022 (would be '\\xFF' for values under 255; expands as needed)
PY4: \\u2022
PY8: \\U00002022
PYBR: \\x{2022}
=item * B<--pics>
Show Unicode control pictures instead of entities, for
control characters and spaces (default). I<--no-pics> to turn off.
=item * B<--quiet> OR B<-q>
Suppress most messages.
=item * B<-s> OR B<--allSpaces>
Convert the ASCII whitespace characters SPACE, TAB, and LINEFEED.
This is short for I<--spaceAs SP --tabAS HT --lfAs LF>.
More unusual whitespace controls, such as CR, VT, FF and the many
non-ASCII whitespace characters, are always converted.
=item * B<--spaceAs> I<s>
Display spaces (U+0020) in one of the following ways (default is "OK"):
I<OK> leave as literal space
I<BL> ('b' with a slash through it, U+2422 -- the default)
I<UN> (an underscore with the ends turned up, U+2423)
I<SP> (a little "SP", U+2420)
I<NBSP> (a NO-BREAK SPACE, U+00A0)
I<COLOR> (experimental, different colors for different whitespace chars)
=item * B<--tabAs> I<l>
Display TAB (U+0009) as:
I<HT> (a little "HT", U+2409) (default)
I<OK> leave as literal tab
=item * B<--uri>
Superceded by I<--oFormat URI>.
Show URI-style C<%xx> escapes for control chars.
However, not yet smart enough to convert high Unicode characters to
UTF-8 and then URI-escape that UTF-8.
=item * B<--verbose> OR B<-v>
Add more detailed messages.
=item * B<--version>
Display version info and exit.
=item * B<--whichColor> I<name>
Choose the color to use with I<--color>. See C<colorManager.pm> and
F<colorNames.md> for details and
a list of the names. Generally, the form is C<[fg]/[bg]/[effect]>.
=back
=head1 Related Commands
C<showInvisibles.py> -- replacement for this.
C<sjdUtils.pm> provides functions to do the transformations.
C<colorManager.pm> provides ANSI terminal color support.
Library calls (see `man 3`) strvis(), etc.
=head1 Known bugs and limitations
For UTF-8, shows \\xFF-style escapes for each byte, not \\UFFFF or \\x{FFFF}
for the code point as a whole.
=head1 History
2007-01-16: Written by Steven J. DeRose.
2007-12-31 sjd: Getopt, version, etc.
2010-09-27 sjd: Cleanup, --base, --pad, --color, factor out makeCharRef().
2011-01-24 sjd: Add control pictures and alternates. binmode STDOUT.
2012-01-23 sjd: Fix --color and --base. Use sjdUtils.
Optimize color-escaping instead of doing on/off for every char.
2012-02-24 sjd: Count and report C1, >65535, whitespace.
2012-10-23 sjd: Simplify. Ditch counting, -base, -names, -pad. Use sjdUtils.
2013-06-21: Add --color, --whichColor.
2014-09-01: Add --iencoding. Multifile. Fix --color.
2015-09-11: Drop -s, add --spaceAs OK, fix code points. --lfAs likewise.
2019-11-16: Update color use.
2020-11-19: New layout.
2021-04-07: Add --oformat and its various choices.
2022-07-25: Fix buffering that made it a no-op. Add --tabAs and --lfAs.
Fix --oformat choices. Support -s/--allSpaces.
2024-03-24: Add --controls. Fix binmode args.
=head1 To do
Option to color-code white-space and/or newlines differently?
See C<colorizeWhiteSpace()>, below.
=head1 Rights
Copyright 2007-01-16 by Steven J. DeRose. This work is licensed under a
Creative Commons Attribution-Share Alike 3.0 Unported License.
For further information on this license, see
L<https://creativecommons.org/licenses/by-sa/3.0>.
For the most recent version, see L<http://www.derose.net/steve/utilities> or
L<https://github.com/sderose>.
=cut
# Output format names
#
my %formats = (
"UPLUS" => 1, "UTF" => 1, "URI" => 1, "DENT" => 1, "XENT" => 1,
"NENT" => 1, "PY2" => 1, "PY4" => 1, "PY8" => 1, "PYBR" => 1,
);
###############################################################################
# Process options
#
my $color = 0;
my $controls = 1;
my $iencoding = "utf-8";
my $lfAs = "NL";
my $oencoding = "utf8";
my $oFormat = "PY2";
my $pics = 1;
my $quiet = 0;
my $spaceAs = "OK";
my $tabAs = "OK";
my $uri = 0;
my $verbose = 0;
my $whichColor = "/yellow";
Getopt::Long::Configure ("ignore_case");
my $result = GetOptions(
"color!" => \$color,
"controls!" => \$controls,
"h|help|?" => sub { system "perldoc $0"; exit; },
"iencoding=s" => \$iencoding,
"lfAs=s" => \$lfAs,
"oencoding=s" => \$oencoding,
"oFormat|outputFormat|output-format=s" => \$oFormat,
"pics|pix!" => \$pics,
"q|quiet!" => \$quiet,
"s|allSpaces!" => sub { $lfAs = "LF"; $tabAs = "HT"; $spaceAs = "SP"; },
"spaceAs=s" => \$spaceAs,
"tabAs=s" => \$tabAs,
"uri!" => \$uri,
"v|verbose+" => \$verbose,
"version" => sub {
die "Version of $VERSION_DATE, by Steven J. DeRose.\n";
},
"whichColor=s" => \$whichColor,
);
($result) || die "Bad options.\n";
if ($oencoding) {
print "";
binmode(STDOUT, ":encoding($oencoding)");
}
(exists $formats{$oFormat}) || die
"Unknown --oFormat '$oFormat'. Known: [ " . join(keys(%formats), ", ") . "].\n";
$lfAs = uc($lfAs);
($lfAs =~ m/^(LF|NL|OK)$/i) || (length($lfAs)==1) ||
die "-lfAs must be 'LF' or 'NL' or 'OK'.\n";
$spaceAs = uc($spaceAs);
($spaceAs =~ m/^(BL|UN|SP|OK|COLOR)$/) || (length($spaceAs)==1) ||
die "-spaceAs must be 'BL', 'UN', 'SP', 'COLOR', or 'OK'.\n";
$tabAs = uc($tabAs);
($tabAs =~ m/^(HT|OK)$/i) || (length($tabAs)==1) ||
die "-tabAs must be 'HT' or 'OK'.\n";
my %spaceCodes = (
"SP" => chr(0x2420), # SP
"B" => chr(0x2422), # B/
"U" => chr(0x2423), # _
"OK" => ' ',
"NBSP" => chr(0x00A0), # NO-BREAK SPACE
);
my %lfCodes = (
"LF" => chr(0x240A),
"NL" => chr(0x2424),
"OK" => "\n",
);
my %tabCodes = (
"HT" => chr(0x2409),
"OK" => "\t",
);
my @names = ( # currently unused
"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
"BS", "HT", "NL", "VT", "FF", "CR", "SO", "SI",
"DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
"CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US",
"SP"
);
my $con = my $coff = "";
if ($color) {
sjdUtils::setColors(1);
$con = sjdUtils::getColorString($whichColor);
$coff = sjdUtils::getColorString("off");
}
# Apply colorizing to whitespaces.
#
my %spaceMap = (
' ' => sjdUtils::getColorString('red'),
"\t" => sjdUtils::getColorString('yellow'),
"\r" => sjdUtils::getColorString('green'),
"\n" => sjdUtils::getColorString('blue'),
#"\s" => sjdUtils::getColorString('/magenta'),
);
my $offSequence = sjdUtils::getColorString('default');
sub colorizeWhiteSpace {
my ($s) = @_;
for my $x (keys %spaceMap) {
my $y = $spaceMap{$x};
$s =~ s/($x+)/$y$1$offSequence/g;
}
}
sub makeDisplayable {
# Convert just one character.
my ($c, $fmt) = @_;
my $n = ord($c);
if ($n < 0x20 && !$controls) {
return $c;
}
if ($n == 0x20) {
if ($spaceAs eq "COLOR") { return $c; }
return $spaceCodes{$spaceAs};
}
if ($n == 0x0A) {
return $lfCodes{$lfAs};
}
if ($n == 0x09) {
return $tabCodes{$tabAs};
}
if ($n < 32 && $pics) {
return chr(0x2400 + $n);
}
if ($fmt eq "UPLUS") { # U+2022
return sprintf("U+%04x", $n);
}
elsif ($fmt eq "UTF") { # \xe2\x80\xa2
return getUTF8local($n, "\\x");
}
elsif ($fmt eq "URI") { # %e2%80%a2
return getUTF8local($n, "%");
}
elsif ($fmt eq "DENT") { # •
return sprintf("&#%04x;", $n);
}
elsif ($fmt eq "XENT") { # •
($n > 0xFFFF) && warn "Char out of range for \\x: $n.\n";
return sprintf("&#x%04x;", $n);
}
elsif ($fmt eq "NENT") { # •
my $buf = HTML::Entities::encode(chr($n));
if ($buf =~ m/^&#/) { $buf = sprintf("&#x%04x;", $n); }
return $buf;
}
elsif ($fmt eq "PY2") { # \x40
if ($n > 0xFFFF) { return sprintf("\\U%08x", $n); }
elsif ($n > 0xFF) { return sprintf("\\u%04x", $n); }
return sprintf("\\x%02x", $n);
}
elsif ($fmt eq "PY4") { # \u2022
if ($n > 0xFFFF) { return sprintf("\\U%08x", $n); }
return sprintf("\\u%04x", $n);
}
elsif ($fmt eq "PY8") { # \U00002022
return sprintf("\\U%08x", $n);
}
elsif ($fmt eq "PYBR") { # \x{2022}
return sprintf("\\x{%2x}", $n);
}
else {
die "Unknown --oformat '$fmt'.\n";
}
}
sub getUTF8local { # From "ord" script
my ($n, $sep) = @_;
if (!defined $sep) { $sep = "%"; }
my $utf8form= Encode::encode('utf8', chr($n));
my $ux = ();
for (my $i=0; $i<length($utf8form); $i++) {
$ux .= sprintf("%s%02x", $sep, ord(substr($utf8form,$i,1)));
}
return($ux);
}
###############################################################################
# Main
#
if (!$ARGV[0]) {
push @ARGV, "-";
if (-t STDIN && !$quiet) {
vMsg(0, "Waiting for STDIN...");
}
}
if ($verbose > 1) {
warn "Testing colors:\n";
for my $x (keys %spaceMap) {
my $y = $spaceMap{$x};
warn sprintf(" %sHello%sSequence\n", $x, $y);
}
}
while (my $file = shift) {
open(my $fh, "<$file") || die "File '$file' could not be opened.\n";
($iencoding) && binmode($fh, ":encoding($iencoding)");
while (my $rec = <$fh>) {
my $buf = "";
for (my $i=0; $i<length($rec); $i++) {
my $c = substr($rec, $i, 1);
if ($c =~ m/[\x21-\x7E]/) {
$buf .= $c;
}
else {
my $printable = makeDisplayable($c, $oFormat);
if ($printable ne $c) {
#vMsg(2, sprintf("recoded '%s' (0x%04x) as '%s' (sp %s).",
# $c, ord($c), $printable, $spaceAs));
}
$buf .= $printable;
}
}
if ($spaceAs eq "COLOR") {
$buf = colorizeWhiteSpace($buf);
}
elsif ($uri) {
$buf = sjdUtils::escapeURI($buf, "DFT", $spaceAs, $lfAs);
}
if ($color) {
$buf =~ s/(\\x[0-9a-f][0-9a-f]|[^[:ascii:]]+)/$con$1$coff/gi;
}
print $buf . ($controls ? "\n":"");
} # EOF
close($fh);
}
exit;