mktar: Use `wc` instead of `du` in summary message
[sunny256-utils.git] / tojson
blobec579f4e10eeee8a2eb7f8f68f0c714308c1bc71
1 #!/usr/bin/env perl
3 #=======================================================================
4 # tojson
5 # File ID: 86075cf2-b00c-11e2-a409-0016d364066c
7 # [Description]
9 # Character set: UTF-8
10 # ©opyleft 2013– Ø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 'help' => 0,
24 'line' => 0,
25 'quiet' => 0,
26 'reverse' => 0,
27 'strip-lf' => 0,
28 'verbose' => 0,
29 'version' => 0,
33 our $progname = $0;
34 $progname =~ s/^.*\/(.*?)$/$1/;
35 our $VERSION = '0.2.0';
37 Getopt::Long::Configure('bundling');
38 GetOptions(
40 'help|h' => \$Opt{'help'},
41 'line|l' => \$Opt{'line'},
42 'quiet|q+' => \$Opt{'quiet'},
43 'reverse|r' => \$Opt{'reverse'},
44 'strip-lf|s' => \$Opt{'strip-lf'},
45 'verbose|v+' => \$Opt{'verbose'},
46 'version' => \$Opt{'version'},
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{'reverse'}) {
64 while (my $curr = <>) {
65 chomp($curr);
66 if ($curr =~ /"/) {
67 msg(2, "curr bef: '$curr'");
68 $curr =~ s/^.*?"(.*)".*?$/json_to_txt($1)/e;
69 msg(2, "curr aft: '$curr'");
70 print($curr);
73 } else {
74 if ($Opt{'line'}) {
75 print("[\n \"");
76 my $print_comma = 0;
77 while (<>) {
78 $Opt{'strip-lf'} && chomp();
79 $print_comma && print("\",\n \"") || ($print_comma = 1);
80 print(txt_to_json($_));
82 print("\"\n]\n");
83 } else {
84 print('"');
85 while (<>) {
86 $Opt{'strip-lf'} && chomp();
87 print(txt_to_json($_));
89 print('"');
92 return $Retval;
93 # }}}
94 } # main()
96 sub txt_to_json {
97 # Convert plain text to JSON {{{
98 my $Txt = shift;
99 $Txt =~ s/\\/\\\\/gs;
100 $Txt =~ s/"/\\"/gs;
101 $Txt =~ s/\x08/\\b/gs;
102 $Txt =~ s/\x09/\\t/gs;
103 $Txt =~ s/\x0a/\\n/gs;
104 $Txt =~ s/\x0c/\\f/gs;
105 $Txt =~ s/\x0d/\\r/gs;
106 $Txt =~ s/([\x00-\x1f])/sprintf('\u%04X', ord($1))/gse;
107 return($Txt);
108 # }}}
109 } # txt_to_json()
111 sub json_to_txt {
112 # Convert JSON back to plain text {{{
113 my $Txt = shift;
114 $Txt =~ s/\\\\/\\/gs;
115 $Txt =~ s/\\r/\x0d/gs;
116 $Txt =~ s/\\f/\x0c/gs;
117 $Txt =~ s/\\n/\x0a/gs;
118 $Txt =~ s/\\t/\x09/gs;
119 $Txt =~ s/\\b/\x08/gs;
120 $Txt =~ s/\\"/"/gs;
121 # $Txt =~ s/([\x00-\x1f])/sprintf('\u%04X', ord($1))/gse;
122 return($Txt);
123 # }}}
124 } # json_to_txt()
126 sub print_version {
127 # Print program version {{{
128 print("$progname $VERSION\n");
129 return;
130 # }}}
131 } # print_version()
133 sub usage {
134 # Send the help message to stdout {{{
135 my $Retval = shift;
137 if ($Opt{'verbose'}) {
138 print("\n");
139 print_version();
141 print(<<"END");
143 Escape plain text for use in JSON.
145 Usage: $progname [options] [file [files [...]]]
147 Options:
149 -h, --help
150 Show this help.
151 -l, --line
152 Convert input to a JSON array containing all lines.
153 -q, --quiet
154 Be more quiet. Can be repeated to increase silence.
155 -r, --reverse
156 Convert from JSON to text.
157 -s, --strip-lf
158 Strip line feeds (\\n) from input when converting to JSON.
159 -v, --verbose
160 Increase level of verbosity. Can be repeated.
161 --version
162 Print version information.
165 exit($Retval);
166 # }}}
167 } # usage()
169 sub msg {
170 # Print a status message to stderr based on verbosity level {{{
171 my ($verbose_level, $Txt) = @_;
173 if ($Opt{'verbose'} >= $verbose_level) {
174 print(STDERR "$progname: $Txt\n");
176 return;
177 # }}}
178 } # msg()
180 __END__
182 # This program is free software; you can redistribute it and/or modify
183 # it under the terms of the GNU General Public License as published by
184 # the Free Software Foundation; either version 2 of the License, or (at
185 # your option) any later version.
187 # This program is distributed in the hope that it will be useful, but
188 # WITHOUT ANY WARRANTY; without even the implied warranty of
189 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
190 # See the GNU General Public License for more details.
192 # You should have received a copy of the GNU General Public License
193 # along with this program.
194 # If not, see L<http://www.gnu.org/licenses/>.
196 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :