c/src/Makefile: Create and use `INSTALL_TARGETS`
[sunny256-utils.git] / fromhex
blobfe64292768bb7939b3490dc981972bd0db9ec792
1 #!/usr/bin/env perl
3 #=======================================================================
4 # fromhex
5 # File ID: 17c58aec-f743-11dd-a648-000475e441b9
7 # Converts a hexadecimal string to plain text.
9 # Character set: UTF-8
10 # ©opyleft 2006– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of
12 # file for legal stuff.
13 #=======================================================================
15 use strict;
16 use warnings;
17 use Getopt::Long;
19 local $| = 1;
21 our %Opt = (
23 'decimal' => 0,
24 'help' => 0,
25 'quiet' => 0,
26 'unicode' => 0,
27 'verbose' => 0,
28 'version' => 0,
29 'warnings' => 0,
33 our $progname = $0;
34 $progname =~ s/^.*\/(.*?)$/$1/;
35 our $VERSION = '0.1.0';
37 Getopt::Long::Configure('bundling');
38 GetOptions(
40 'decimal|d' => \$Opt{'decimal'},
41 'help|h' => \$Opt{'help'},
42 'quiet|q+' => \$Opt{'quiet'},
43 'unicode|u' => \$Opt{'unicode'},
44 'verbose|v+' => \$Opt{'verbose'},
45 'version' => \$Opt{'version'},
46 'warnings|w' => \$Opt{'warnings'},
48 ) || die("$progname: Option error. Use -h for help.\n");
50 $Opt{'verbose'} -= $Opt{'quiet'};
51 $Opt{'help'} && usage(0);
52 if ($Opt{'version'}) {
53 print_version();
54 exit(0);
57 exit(main());
59 sub main {
60 # {{{
61 my $Retval = 0;
63 if ($Opt{'unicode'}) {
64 binmode(STDOUT, ':utf8');
67 if ($Opt{'decimal'}) {
68 while (<>) {
69 s/(\d+)/print_decimal($1)/seig;
71 } else {
72 if ($Opt{'unicode'}) {
73 if (!$Opt{'warnings'}) {
74 no warnings "utf8";
75 # Not very elegant to duplicate code like this, but 'no
76 # warnings' only works in the local scope.
77 while (<>) {
78 s/([\da-f]+)/print(chr(hex($1)))/seig;
80 } else {
81 while (<>) {
82 s/([\da-f]+)/print(chr(hex($1)))/seig;
85 } else {
86 my $buf = '';
87 while (my $line = <>) {
88 $line =~ s/[^\da-f]//sig;
89 $buf .= $line;
90 $buf =~ s/([\da-f][\da-f])/print(chr(hex($1))), ''/seig;
95 return $Retval;
96 # }}}
97 } # main()
99 sub print_decimal {
100 # {{{
101 my $val = shift;
102 if ($val > 255 && !$Opt{'unicode'}) {
103 warn("$progname: Cannot print byte value $val in bytewise mode, use -u\n");
104 exit(1);
105 } else {
106 if (!$Opt{'warnings'}) {
107 no warnings "utf8";
108 print(chr($val));
109 } else {
110 print(chr($val));
113 return('');
114 # }}}
115 } # print_decimal()
117 sub print_version {
118 # Print program version {{{
119 print("$progname $VERSION\n");
120 return;
121 # }}}
122 } # print_version()
124 sub usage {
125 # Send the help message to stdout {{{
126 my $Retval = shift;
128 if ($Opt{'verbose'}) {
129 print("\n");
130 print_version();
132 print(<<"END");
134 Converts a hexadecimal string to plain text.
136 Usage: $progname [options] [file [files [...]]]
138 Options:
140 -d, --decimal
141 Create bytes from decimal (base 10) numbers.
142 -h, --help
143 Show this help.
144 -q, --quiet
145 Be more quiet. Can be repeated to increase silence.
146 -u, --unicode
147 Use Unicode mode. If any values are higher than U+007F (127), output
148 the character as a UTF-8 sequence instead of bytewise output. I.e.
149 the character U+263A will be output as the bytes "e2 98 ba" instead
150 of "26 3a".
151 -v, --verbose
152 Increase level of verbosity. Can be repeated.
153 --version
154 Print version information.
155 -w, --warnings
156 Enable Perl warnings about invalid UTF-8. Use this if you want to
157 generate safe UTF-8 or be warned about invalid code points in
158 (hexa)decimal input.
161 exit($Retval);
162 # }}}
163 } # usage()
165 sub msg {
166 # Print a status message to stderr based on verbosity level {{{
167 my ($verbose_level, $Txt) = @_;
169 if ($Opt{'verbose'} >= $verbose_level) {
170 print(STDERR "$progname: $Txt\n");
172 return;
173 # }}}
174 } # msg()
176 __END__
178 # This program is free software; you can redistribute it and/or modify
179 # it under the terms of the GNU General Public License as published by
180 # the Free Software Foundation; either version 2 of the License, or (at
181 # your option) any later version.
183 # This program is distributed in the hope that it will be useful, but
184 # WITHOUT ANY WARRANTY; without even the implied warranty of
185 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
186 # See the GNU General Public License for more details.
188 # You should have received a copy of the GNU General Public License
189 # along with this program.
190 # If not, see L<http://www.gnu.org/licenses/>.
192 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :