-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdumpx
executable file
·2563 lines (2180 loc) · 86.3 KB
/
dumpx
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env perl -w
#
# dumpx: A better hex-dump utility, at least for 'text'.
# Long ago (~1980?): Written in C by Steven J. DeRose, inspired by a version
# I think was written by Peter DiCamillo.
#
use strict;
use Getopt::Long;
use Encode;
use charnames ':full';
use Unicode::UCD 'charscript';
use Unicode::UCD 'charblock';
use HTML::Entities;
use sjdUtils;
use alogging;
#use ColorManager;
#my $colorOff = ColorManager::getColorString('off');
our %metadata = (
'title' => "dumpx",
'description' => "A better hex-dump utility, at least for 'text'.",
'rightsHolder' => "Steven J. DeRose",
'creator' => "http://viaf.org/viaf/50334488",
'type' => "http://purl.org/dc/dcmitype/Software",
'language' => "Perl 5",
'created' => "~1980?",
'modified' => "2021-09-16",
'publisher' => "http://github.com/sderose",
'license' => "https://creativecommons.org/licenses/by-sa/3.0/"
);
our $VERSION_DATE = $metadata{'modified'};
=pod
=head1 Usage
dumpx [options] [file]
Dumps a file or portion, converting characters to other forms;
also provides an interactive mode (see I<--user>).
This is very much like the *nix C<od> utility. However, the input is treated
entirely as characters (not bytes or numeric types), so this is much more
useful for 'text'-ish files, including Unicode.
The input is divided into I<chunks> that fill display lines.
If available the script uses environment variables C<$COLUMNS> to
set the chunk length (typically 16), and C<$LINES> to calculate how many
chunks to show before pausing (like C<more>).
See also I<-w>, I<--cwidth>, and especially I<--break> (which cause
a new chunk to be started after every I<newline>, making text files much
more readable).
Each chunk can be displayed in multiple I<forms> on separate lines.
By default, the first line has the relevant offset on the left (see I<-j>,
I<--jl>, I<-A>, and several other options),
and the literal string text of the chunk on the right (see I<--as>).
By default, the characters are shown in two formats:
hexadecimal B<byte> values (I<-h>)
and then literal characters (I<-a>), and the numeric display has some
extra I<--showGutter> space in the middle. Thus:
x000049: 31 32 33 34 35 36 61 62 63 64 65 66 |12345678abcdefgh|
1 2 3 4 5 6 a b c d e f
Unlike C<od>, the representations in all the lines of a group
are aligned, and you shouldn't get big/little-endian issues.
Offsets are shown in the same base as the line they precede. Each such
I<group> of lines, can be followed by a blank line (see I<--blank>).
With I<--break>, a newline character will also start a new "chunk".
In I<--user> (interactive) mode, a screen's-worth of such groups is printed
and then a user command is accepted.
=head2 Why?
od's interface. It's awkward to get it to display based on characters,
and I find its treatment of byte orderand alignment confusing in some settings.
-a lines for text aren't as legible as I'd like. You can't easily go to whatever
offset you want (except at the start). This layout is much more like an
old CMS hex dump program (probably was a local mod at Brown University
by Peter DiCamillo; at any rate it was really good).
=head3 'Literal' display
The 'literal' forms I<-a> (below) and I<--as> (as a right sidebar),
display printable characters as themselves.
By default, control characters are replaced by
the Unicode 'control pictures' (U+2400 and following).
Other unprintable characters are "?" on the right, but will
be replaced by "#" and a footnote number in the I<-a> (below) display.
After each display chunk, each character so replaced is
listed on a separate line, with its footnote number and full Unicode name.
This can be turned off via I<--nonotes>, in which case the characters
will merely be displayed as "??".
To display characters from d160-d255 (sometimes called Latin-1 or G1)
literally, use I<--g1ok> (that's a one, not an el).
See also I<--substitute> and I<--notes>.
B<Note>: Unlike most of my scripts, options for C<dumpx> B<regard> case.
This is for compatibility with C<od>: most options are exactly the same.
However, I<-s>, I<-t>, I<--traditional>, and all options relating
to byte order and word size are not supported.
=head1 Options I<not> like C<od>
(prefix 'no' to negate where applicable)
B<Note>: The C<od>-like options are described in a later section.
=over
=item * B<--addressWidth> I<n>
How many columns to allow for displaying the offset of each chunk.
Default 8; specify 0 to suppress offset display.
I<n> can be given in decimal (999), octal (0777), hex (0xFF), or binary (0b11).
See also B<--addressRadix>, aka I<--address-radix> and I<-A>.
=item * B<--blankLine>
Put a blank line after the group of lines displayed for each chunk.
=item * B<--break>
Start a new output line at each input newline character (not finished).
=item * B<--cc> I<char>
Colorize the character specified (repeatable).
I<n> can be given in decimal (999), octal (0777), hex (0xFF), or binary (0b11).
=item * B<--charForSpace> I<spec>
Set what code point is displayed as the printable form of space (regular space, not
hard space, en/em/thin space, etc.). The number may be specified in decimal, octal,
or hex. Some useful values include:
0x20 -- a literal space
0x2423 -- an underscore with the ends turned up (the default)
0x2420 -- a little "SP"
0x2422 -- "b" with a slash
=item * B<--color> I<arg>
Colorize certain characters in the output, as determined by I<arg>:
I<auto> (newlines, controls, spaces, non-Latin1);
I<xml> (<>&;);
I<controls>;
I<nonascii>;
I<all> (auto, xml, and controls); or
I<none> or I<off>.
You can also give a Unicode character-class name (experimental) to colorize all
characters that are members of that class (such as Bopomofo);
or I<!> plus a character-class name to colorize all characters that are
I<not> of that class. See I<man perlunicode> for more details.
If environment variable I<CLI_COLOR> is set and STDOUT is going to
a terminal, defaults to coloring newlines and control characters
(unfortunately, "| more" is not a terminal).
B<Note>: In transition, may not completely work.
=item * B<--cwidth> I<n>
Display columns to allow per character. For example, 8-bit characters take 2
hexadecimal digits, so I<--cwidth 3> is nice (and is the default).
I<n> can be given in decimal (999), octal (0777), hex (0xFF), or binary (0b11).
=item * B<--dcolumns> I<n>
Number of character spaces in the display. Default: $COLUMNS or 80.
I<n> can be given in decimal (999), octal (0777), hex (0xFF), or binary (0b11).
=item * B<--dlines> I<n>
Number of lines in the display. Default: $LINES or 40.
I<n> can be given in decimal (999), octal (0777), hex (0xFF), or binary (0b11).
=item * B<--G1ok>
Allows output of G1 characters (d160-d255).
(should be subsumed under I<--oencoding>)
=item * B<--iencoding> I<c>
Use a non-Latin-1 character set for display (see also I<--listEncodings>).
Among the likely candidates: ascii, cp1252, cp500, iso-8859-1 (through -16),
MacRoman, UCS-2BE, UCS-2LE, UTF-16, utf8 (note case and hyphenation).
=item * B<--ilineends> I<t>
Are input line-ends m(ac), d(os), or default u(nix)?
(only really matters if using I<--jl>)
=item * B<--jl> I<n> or B<--skip-lines>
Start at line I<n> (see also I<--ilineends>, I<-j>).
I<n> can be given in decimal (999), octal (0777), hex (0xFF), or binary (0b11).
=item * B<--listchars>
Just show a table of Latin-1 characters.
=item * B<--listEncodings>
Show all the encodings supported by I<--iencoding> and I<--oencoding>.
=item * B<--nocolor>
Synonym for I<--color none>.
=item * B<--nog1>
Do not try to display Latin1/G1 characters (d161-255, xA1-FF).
=item * B<--noteColor> I<colorName>
Show notes (added for characters which lack mnemonics or very short names),
with the specified color.
=item * B<--notes>
Enable notes (added for characters which lack mnemonics or very short names).
Default: on (use I<--nonotes> to turn off).
=item * B<oencoding> I<e>
Assume the output can handle enocding I<e> (see also I<--listEncodings>).
B<Note>: Terminal programs may be confused if you request a character set they
don't support.
=item * I<--showAlphaSide> or B<--sas>
Show literal chars more readably
in column on right (default; use I<--noas> to turn off).
=item * B<--showGutter> or B<--sg>
Split the alphabetic display on the right in the middle for legibility.
=item * B<--showHeader> or B<--sh>
Display a relative-offset header aligned over each character-column.
B<Note>: it is not possible to make this show the low-order byte or nibble
or the precise offset; just a column number. It is also only printed before
the first block of each screenful (which may be fine with I<--user>, but
not in general).
=item * B<--lineNumber> or B<--sln>
Display the current line number (in which the start of each block falls).
=item * B<--substitute> I<s>
Print I<s> for unprintable characters. Default: U+00A4 (Currency Sign).
See also I<--useControlSymbols>, I<--html>, and I<--oencoding>.
=item * B<-u>
Show a line with the UCS2 numeric equivalent of any UTF-8 sequences.
=item * B<--unicode>
Synonym for I<--iencoding utf8>.
=item * B<--useCaret> or B<--uc>
Show control characters as 0x01 through 0x1A as "^A" through "^Z".
=item * B<--useControlSymbols> or I<--pix>
Display control characters (C0) as Unicode control pictures (U'24xx).
=item * B<--user> or B<--interactive>
Use interactive mode (unfinished). See below for details.
=item * B<--verbose>
Add debugging messages (repeatable).
=back
=head1 Options like C<od>
Many of C<od>'s options are supported, as listed below. However,
those that parse input data by number of I<bytes>
rather than characters are not
(-b -c -d[SIZE] -f[SIZE] -i -l -o[SIZE] -s -u[SIZE] -x[SIZE]
and their I<-t[TYPE]> equivalents).
The supported C<od>-like options are:
=over
=item * B<-A> I<radix> or (B<--address-radix>)
Base for displaying file offsets (0, 8, 10, or 16)
(if not specified, of specified as 0,
the displayed offset matches the base of following data on line.
Thus, you can get multiple bases in a reasonably mnemonic way.
See also I<--addressWidth> (set that to 0 to suppress offset display entirely).
=item * B<-a> or B<-t a>
Show literal characters below numeric(s).
Unlike C<od>, the high-order bit is I<not> automatically cleared, and
multi-byte encodings are supported (see also I<--iencoding>).
Latin-1 characters are shown as themselves; others will display
a footnote number, and after the display line is printed, the
footnotes are shown. Each footnotes includes its number, the
hex value for the code point (this will not be the same as the
individual utf8 bytes shown by I<-h>!), and the full Unicode name for
the character.
The footnote number in the I<-a> lines, is allowed as much space
as the number of bytes in the UTF-8 form
(numeric display options will show the values of the UTF-8 bytes).
=item * B<-c>
Show C-style escaped characters.
Unlike C<od>, backslash-codes are not decoded.
=item * B<-d>
Show bytes in decimal.
=item * B<-h>
Show bytes in hexadecimal ('-x' also works).
=item * B<--help>
Show this help and exit.
For compatibility with C<od>, the I<-h> option display hex bytes,
rather than invoking help.
=item * B<-j> I<n> or B<--skip-bytes>
Offset in file to start at (but see also I<--jl>, above).
=item * B<--linenum>
Display the line number above each block, in which that blocks begins.
=item * B<-N> I<n> or B<--read-bytes> or B<--readBytes>
Max number of bytes to dump.
=item * B<-o>
Show bytes in octal.
=item * B<-S> or <--strings>
Not supported.
=item * B<-t> or <--format-type>
Not supported.
=item * B<--traditional>
Not supported.
=item * B<-v> or B<--output-duplicates>
Don't skip duplicate lines.
=item * B<--version>
Show version information and exit.
=item * B<-w> I<n> or B<--width>
Bytes per output line (default 16, multiples of 8 are nice).
=back
=head1 Interactive mode (not finished)
To invoke interactive mode, specify the I<--user> option.
=head2 Interactive Commands
These are largely modeled on C<more> and its kin.
=over
=item * quit Exit this program
=item * <Return> Move forward one block (i.e., one screenful, see 'lines')
=item * + n Move forward n blocks
=item * B<- n> Move backward n blocks (default one block)
=item * @ n Move to file offset n
=item * find s Find string (which may include nonzero \\xx byte codes)
=item * next n Find next (with n, finds next n offsets & diffs)
=item * back Return to previous place
=item * diff n m Subtract m from n, display result in hex/decimal/octal
=item * log Append current block to logfile
=item * c Identify the current character's code point and name.
=back
=head3 Also
=over
B<Move among available files>
':d' -- filelist-delete
':n' -- filelist-next
':p' -- filelist-prev
':x' -- filelist-goto
B<Move by record>
' ' -- forward: go down N records (default 1)
'f' -- forward: go down N records (default 1)
'b' -- backward: go up N records (default 1)
'<' -- start: go to top (first record)
'>' -- end: go to bottom (last record)
'g' -- goto: skip to record N
B<Search>
'/' -- find-forward: search forward for regex.
'?' -- find-backward (takes arg)
'n' -- find-next (takes arg)
'N' -- find-previous
'm' -- mark: set a mark at current record (takes arg)
'\'' -- goto-mark (takes arg)
B<Options>
'-' -- option-off
'_' -- option-display
'+' -- option-on
B<Miscellaneous>
'c' -- identify-character
'=' -- show-filename
':f' -- show-filename
'v' -- edit: edit current file
'V' -- version
':e' -- edit-other: edit another file (takes arg)
'E' -- edit-other: edit another file (takes arg)
'q' -- quit
':q' -- quit
'Q' -- quit
':Q' -- quit
'h' -- help
'H' -- help
'r' -- repaint: re-display current record
'!' -- shell: run a shell command (takes arg)
'|' -- shell-region: cat region from current record to mark X
(named by first letter of following arg), to shell (takes arg).
=back
=head2 Setting Interactive-only Options
=over
=item * chunk n Number of bytes per display line (default 16)
=item * header n 0 to hide, 1 to show header line
=item * lines n Number of display lines to show at once
=item * logfile name Set name of log file to write
=item * macro m Start taking commands from specified file
=item * next n Go forward by n blocks
=back
=head1 Related commands
C<od> -- the *nix command most similar to this script.
C<chr> and C<ord> -- utilities to convert between code point numbers (in
various bases) and actual characters and names.
L<http://search.cpan.org/~jhi/perl-5.7.3/ext/Encode/Encode.pm> -- Perl
package for dealing with character encodings.
C<RecordFile.pm> -- should replace the internal 'reader' package.
C<hilite> -- can colorize all kinds of this, such as ranges of character codes,
similar to some features of C<dumpx>.
=head1 Known bugs and limitations
Bash may wrap lines too soon if color is used, apparently because it counts
the length of the color escapes into the line length (?).
No type-checking is done for the interactive I<set option value> command.
Can't count line-numbers when using I<-j> (start at offset).
I<--showHeader> is kind of lame so far.
EBCDIC recoding is unfinished.
=head1 To do
Finishing colorizer & color defaulting.
Integrate RecordFile then delete 'reader' package. Esp. for compression.
--break for non-*nix line-end types (or any code point chosen!)
Add 'column number within line' as a display option.
Integrate HTML::Entities to get char names.
Have a UCS2 mode, with wide areas?
Option to just show plain text *except* where there's funky stuff,
or to recode funky stuff visible inline: the na[uuml]ve\n[tlingit digit 4]...
Low priority:
Sync/Integrate with lessFields? Or at least TabularFormats.pm.
-pix for spaces doesn't work (likewise for null)
Colorize showHeader, and figure out how often to show it.
Auto-adjust for window width and height.
Buffer for scrollback even with STDIN?
Count & report number of duplicate lines in a row.
=head1 History
Long ago (~1980?): Written in C by Steven J. DeRose, inspired by a version
I think was written by Peter DiCamillo.
2006-06-03: Ported to Perl and made options more like Unix od.
2006-09-12: Make -x synonym for -h. Implement $linesToShow.
2006-09-13: Support stdin when no file argument.
2006-10-02: Make color options like ls/grep. Split up help.
2006-11-03: Add -cc.
2006-11-07: Add -color controls.
2007-01-11 sjd: Add -skipLines, -lineend.
2007-02-15 sjd: Add -linenum.
2007-08-29 sjd: Improve -listchars. Add -codepage.
2007-09-05 sjd: Add unicode classes.
2007-11-27 sjd: Start strict.
2008-02-13 sjd: Add perl -w, fix a few bugs, esp. with colorizing.
2008-08-31 sjd: Check for BSD.
2010-02-24 sjd: Clean up options. use strict. fullnames & unicode.
2010-03-01 sjd: Getopt::Long.
2010-05-07 sjd: Control pictures/control symbols. Color fixes. perldoc.
2010-10-26ff sjd: Packagize 'reader'. Start -break. Rename -codePage to
-iencoding, -ltype to -ilineends. Break out dumpOneChunk().
Separate -verbose from (od-like) -v. Add -substitute, -useCaret.
Null-pad at EOF. Fix color bug.
2010-11-02ff sjd: Yet more color bugs. Clean up -user macro stack handling.
Work on -user mode. Offset printing in -user. Add -colHeaderBase.
2011-06-27 sjd: Implement -j.
2011-08-08 sjd: Fix bug in colorizing w/ showABe().
2011-08-29f sjd: Break options into a hash, to help -user mode.
Revamp lines/columns handling, user Find escaping. Add -addressWidth.
Re-org and synchronize option variables, getopt entries, and perldoc.
Rename option variables to match long option names from od.
Colorize and fix bugs in showHead.
2012-02-09 sjd: Start integrating, cleaning up charset handling w/ Encode.
2013-03-28 sjd: Finish -break (nee -newlines). Clean up options. Count
column position in lines. Add interactive command 'c' to identify chars.
Add -lineNum. Fix -showGutter. Move showAlphaSide() call up and clean
up width calculations. Stop showing NULs at EOF. Start real UTF8 support.
2013-06-27f: Sync RecordFile.pm. Add showUCS. Clean up getName, getPrintChar.
Add footnotes for chars without short names. Switch to passing around
byte arrays instead of strings. Generalize colorizing.
Fixing utf8. Make showUCS and showAbe gather up chars! howManyBytes().
2013-08-12: More work on cleaning up colorizing. Make categories consistent.
Sync color options w/ recently-added 'colorizer' package. Lose $cRed, etc.
2014-09-01: Make --decimal not break due to colorizer.
2017-02-08: Switch to my ColorManager package.
2021-09-16: New layout, cleanup.
=head1 Rights
Copyright 2006-06-03 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
###############################################################################
# Store options in a hash (mainly to help w/ user mode)
#
my %args = (
# non-od-like options
"addressWidth" => 8, # How wide to show the offset?
"blankLine" => 1, # Put in an extra blank line
"break" => 0, # Break at newlines
"charForSpace" => chr(0x2423), # underscore-ish form.
"cc" => "", # chars to color
"colorSpace" => 0, # Try to display newlines in Red?
"colorXML" => 0, # Color angle brackets and ampersands
"colorCONTROLS" => 0, # Color chars 00-31 and 128-159
"colorNonASCII" => 1, # Color chars >127
"colorClass" => "", # Perl name for Unicode color class
"colorClassNegated" => "",
"carg" =>
($ENV{CLI_COLOR} && -t STDOUT) ? "auto":"off",
"dcolumns" => $ENV{COLUMNS} || 80,
"dlines" => $ENV{LINES} || 40,
"width" => 0, # Characters per display line
"cwidth" => 3, # Width per char (incl leading space)
"G1ok" => 1, # Ok to print G1 (Latin-1) characters?
"headerBase" => 16,
"iencoding" => "",
"ilineends" => "u",
"skipLines" => 0, # If non-zero, skip to this line num.
"listChars" => 0, # Just show Latin-1 table
"logfilename" => "",
"LOG" => 0,
"newlineDisplayChar" => "?",
"nlines" => 0, # ???
"noteColor" => "/Yellow",
"notes" => 1,
# Is there a better way to check for Unicode support by the console?
"oencoding" => (($ENV{LANG} =~ m/UTF-?8/) ? "utf8":""),
"quiet" => 0,
"showAlphaSide" => 1,
"showGutter" => 0, # Split alpha columns at middle
"showHeader" => 0, # Show column header w/ low-order nibbles
"showLineNumber" => 0,
"skipLines" => 0,
# "subst" => chr(0x2426);
"subst" => chr(0xA4),
"useCaret" => 0, # Show controls as ^A-^Z
"useControlSymbols" => 1, # Show controls as Unicode symbols
"user" => 0, # Run in interactive mode?
"voption" => 0, # Don't leave out duplicate lines
"verbose" => 0, # Add debugging info
# od-like options
"addressRadix" => 0, # What base to use to show file offsets
"linesToShow" => "", # List of line types to display, in order
"outputDuplicates" => 1,
"readBytes" => 0, # 0 means no limit
"skipBytes" => 0, # Where to start from
);
###############################################################################
# Parse options
#
my %getoptHash = (
# non-od-like options
"addressWidth=o" => \$args{addressWidth},
"blankLine!" => \$args{blankLine},
"break|newlines!" => \$args{break},
"color=s" => sub { $args{carg}; },
"nocolor" => sub { $args{carg} = "off"; },
"cc=s" => sub { $args{cc} .= strcvt($_[1]); },
"charForSpace=o" => \$args{charForSpace},
"cwidth=o" => \$args{cwidth},
"G1ok|g1ok!" => \$args{G1ok},
"headerBase=o" => \$args{headerBase},
"help" => sub { system "perldoc $0"; exit; },
"iencoding=s" => \$args{iencoding},
"ilineends=s" => \$args{ilineends},
"listChars!" => sub {
$args{listChars} = 1;
$args{blankLine} = 1;
$args{cwidth} = 4;
},
"listEncodings" => sub {
warn "\nEncodings available:\n";
my $last = ""; my $buf = "";
for my $k (Encode->encodings(":all")) {
my $cur = substr($k,0,2);
if ($cur ne $last) {
warn "$buf\n";
$last = $cur; $buf = "";
}
$buf .= "$k ";
}
warn "$buf\n";
exit;
},
"noteColor=s" => \$args{noteColor},
"notes!" => \$args{notes},
"showAlphaSide|sas!" => \$args{showAlphaSide},
"showGutter|sq!" => \$args{showGutter},
"showHeader|sh!" => \$args{showHeader},
"showLineNumber|sln!" => \$args{showLineNumber},
"substitute=s" => \$args{subst},
"u" => sub { $args{linesToShow} .= "u "; },
"unicode!" => sub { $args{iencoding} = "utf8"; },
"useCaret|uc!" => \$args{useCaret},
"useControlSymbols|pix!" => \$args{useControlSymbols},
"user|interactive!" => \$args{user},
"utf8!" => sub { $args{iencoding} .= "u "; },
"verbose+" => \$args{verbose},
"version!" => sub {
die "Version of $args{version} = $_[1], by Steven J. DeRose.";
},
# od-like options
"a" => sub { $args{linesToShow} .= "a "; },
"A=i" => \$args{addressRadix},
"c" => sub { $args{linesToShow} .= "c "; },
"d" => sub { $args{linesToShow} .= "d "; },
"h" => sub {
$args{linesToShow} .= "h ";
($args{quiet}) || warn
"(did you mean '-help'? '-h' = hexadecimal)\n";
},
"j=o" => \$args{skipBytes},
"jl|skip-lines=i" => sub { $args{skip_lines} = $_[1]; },
"linenum!" => \$args{showLineNumber},
"N|read-bytes|readBytes=o" => \$args{readBytes},
"o" => sub { $args{linesToShow} .= "o "; },
"v|output-duplicates" => sub {
$args{outputDuplicates} = $_[1];
($args{quiet}) || warn
"(did you mean '-verbose'? -v = -output-duplicates)\n";
},
"V+" => \$args{voption},
"w|width=o" => \$args{width},
"x" => sub { $args{linesToShow} .= "h "; },
); # process options
Getopt::Long::Configure ("no_ignore_case");
(GetOptions(%getoptHash)) || die "Bad options.\n";
###############################################################################
# Process options
#
if ($args{"width"} < 1) {
$args{"width"} = 16;
}
my %encodings = ();
for my $k (Encode->encodings(":all")) {
$encodings{$k} = 1;
}
my $COL = new colorizer();
checkOptionValues();
###############################################################################
# Global constants and such
#
my $gapToAlphaSide = " "; # Show between lines and -as side
my $lbar = "|"; # Show before -as
my $rbar = "|"; # Show after -as
my $ldaquo = "\xAB"; #
my $mid = "~"; #
my $rdaquo = "\xBB"; #
my $eofPadChar = chr(0); # Or 0xFFFD?
# Short names for the C0 and C1 control characters.
# PAD, HOP, and SGCI are listed as "XXX" in Unicode (acc. Wikipedia).
my @C0names = (
"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
" BS", " HT", " LF", " VT", " FF", " CR", " SO", " SI",
"DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
"CAN", " EM", "SUB", "ESC", " FS", " GS", " RS", " US");
my @C1names = (
"PAD", "HOP", "BPH", "NBH", "IND", "NEL", "SSA", "ESA",
"HTS", "HTJ", "VTS", "PLD", "PLU", "RI", "SS2", "SS3",
"DCS", "PU1", "PU2", "STS", "CCH", "MW", "SPA", "EPA",
"SOS", "SGCI", "SCI", "CSI", "ST", "OSC", "PM", "APC",
"NBS");
if (scalar @C0names != 32 || scalar @C1names != 33) {
warn "\nname tables broken.\n\n";
}
my %lgMap = (
"ARABIC" => "AR",
"ARMENIAN" => "hy",
"COPTIC" => "CP",
"CYRILLIC" => "CY",
"BENGALI" => "bn",
"DEVANAGARI" => "DV",
"GREEK" => "GR",
"HEBREW" => "HB",
"HIRAGANA" => "HG",
"KATAKANA" => "KK",
"LATIN" => "LA",
# EASTERN ARABIC-INDIC GEORGIAN HANGUL KAERITEN THAI TIBETAN
"OPENING" => "OPEN",
"CLOSING" => "CLOS",
"BRACKET" => "bkt",
"CJK UNIFIED" => "CJKU",
"ENCLOSED" => "ENC",
"SUPPLEMENT" => "SUP",
"SYMBOL" => "SYM",
#black-letter serif sans-serif double-struck inverse centered script
#superscript subscript
#circled bracketed parenthesized squared
#HALFWIDTH (anti)clockwise non-spacing spacing
);
# General state/globals
#
my $bytesDumpedSoFar = 0; # To support -N option ($readBytes)
my $theReader = undef; # Data source
my $ofn = 0; # file name for interactive log
my @notes = (); # Footnotes, for long character names.
###############################################################################
# For -listChars, make fake input data, call usual display code.
#
if ($args{listChars}) {
print "Latin-1 chart:\n";
my $s = "";
for (my $i=1; $i<=255; $i++) {
$s .= chr($i);
}
($args{verbose}) && print "Got string: '$s'.\n";
$theReader = new reader();
$theReader->addText($s);
$theReader->{offset} = 0;
while ($s ne "") {
my $in = substr($s,0,$args{width});
foreach my $typ (split(/ +/,$args{linesToShow})) {
if ($typ eq "h") {showHex(); }
elsif ($typ eq "d") {showDecimal(); }
elsif ($typ eq "o") {showOctal(); }
elsif ($typ eq "c") {showCStyle(); }
elsif ($typ eq "a") {showABe(); }
elsif ($typ eq "u") {showUCS(); }
}
($args{blankLine}) && print("\n");
$s = substr($s,$args{width});
}
print "Done.\n";
exit;
} # listChars
###############################################################################
#
if ($args{skipBytes} && $args{showLineNumber}) {
warn "Can't show line numbers when -j has been used, sorry.\n";
$args{showLineNumber} = 0;
}
if (!$ARGV[0]) {
($args{user}) && die "Can't run interactive AND take data from stdin.\n";
($theReader = new reader("&STDIN")) || die
"Could not create reader object on STDIN.\n";
}
elsif (!-r $ARGV[0]) {
die "Can't find file '$ARGV[0]'.\n";
}
else {
$theReader = new reader($ARGV[0],$args{iencoding});
($theReader) || die
"Could not create reader object on '$ARGV[0]'.\n";
if ($args{recordSep}) {
# $INPUT_RECORD_SEPARATOR = $recordSep;
$/ = $args{recordSep};
}
}
warn "\n";
binmode(STDERR, ":encoding(utf8)");
if ($args{oencoding}) {
print "";
binmode(STDOUT, ":encoding($args{oencoding})");
}
elsif ($args{useControlSymbols}) {
($args{verbose}) && warn
"Forcing -oencoding to 'utf8' for -useControlSymbols.\n";
print "";
binmode(STDOUT, ":encoding(utf8)");
}
elsif ($args{iencoding} eq "utf8") {
($args{verbose}) && warn "Defaulting -oencoding to '" .
$args{iencoding} . "', same as -iencoding.\n";
print "";
binmode(STDOUT, ":encoding($args{iencoding})");
}
if ($args{skipLines} > 0) {
($args{verbose}) && warn "Scanning to line $args{skipLines}...\n";
my $rc = $theReader->seekRecord($args{skipLines});
if ($rc < 0) {
my ($lineNum, $colNum) = $theReader->tellRecord();
die "Couldn't reach starting line $args{skipLines}, EOF at line " .
$lineNum . ", column " . $colNum . ".\n";
}
($args{verbose}) && warn
"Line $args{skipLines} is at offset " . $theReader->tell() . ".\n";
}
elsif ($args{skipBytes}) {
$theReader->seek($args{skipBytes});
}
###############################################################################
# Main
#
if ($args{user}) {
interact();
}
else {
($args{verbose}>1) && warn "Starting...\n";
while ($args{readBytes}==0 || $bytesDumpedSoFar<$args{readBytes}) {
dumpPage() || last;
}
}
exit;
##############################################################################
# Display several chunks of n chars in various forms.
# Return 0 (FALSE) on EOF.
#
sub dumpPage {
($args{linesToShow}) || die "linesToShow not set.\n";
my $pageLoc = $theReader->tell();
if ($args{showLineNumber}) {
my ($lineNum, $colNum) = $theReader->tellRecord();
print "*** At line " . $lineNum . ", column " . $colNum . " ***\n";
}
my $displayLines = 0;
if ($args{showHeader}) {
showColumnHeaders($pageLoc);
$displayLines++;
}
while ($displayLines < $args{nlines}) {
$displayLines++;
my $fileOffset = $theReader->tell();
my ($lineNum, $colNum) = $theReader->tellRecord();
my $bytes = $theReader->readBytes($args{width}, $args{break});
if (!defined $bytes || scalar(@{$bytes}) <= 0) { return(0); }
($args{verbose}>1) && warn sprintf(
"\nAt offset d%08d, x%08x, o%08o (%dK, %dM)\n",
$fileOffset,$fileOffset,$fileOffset,
$fileOffset>>10, $fileOffset>>20);
($args{blankLine}) && print("\n");
dumpOneChunk($bytes,$fileOffset,$lineNum,$colNum);
if ($args{notes}) {
for my $note (@notes) {
print " " . $COL->colorize($note, $args{"noteColor"}) . "\n";
}
}
@notes = ();
$bytesDumpedSoFar += scalar(@{$bytes});
if ($args{readBytes}!=0 &&
$bytesDumpedSoFar > $args{readBytes}) { return(0); }
}
return(1);
} # dumpPage
# Show one chunk (a line's-worth of characters, probably in multiple forms).
#
BEGIN {
my $lastChunk = []; # local static
my $alreadyDidStars = 0;
sub dumpOneChunk {
my ($curChunk,$fileOffset,$lineNum,$colNum) = @_;
# Suppress printing duplicate lines
if (eqArray($lastChunk,$curChunk) && !$args{voption}) {
if (!$alreadyDidStars) {
print " ******* (duplicate line(s)) *******\n";
($args{blankLine}) && print "\n";
}