-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash2perl
executable file
·323 lines (267 loc) · 8.5 KB
/
bash2perl
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
#!/usr/bin/env perl -w
#
# bash2perl: Rudimentary conversion of a Bash script to Perl.
# 2006: Written by Steven J. DeRose.
#
use strict;
use Getopt::Long;
our %metadata = (
'title' => "bash2perl",
'description' => "Rudimentary conversion of a Bash script to Perl.",
'rightsHolder' => "Steven J. DeRose",
'creator' => "http://viaf.org/viaf/50334488",
'type' => "http://purl.org/dc/dcmitype/Software",
'language' => "Perl 5",
'created' => "2006",
'modified' => "2022-10-06",
'publisher' => "http://github.com/sderose",
'license' => "https://creativecommons.org/licenses/by-sa/3.0/"
);
our $VERSION_DATE = $metadata{'modified'};
=pod
=head1 Usage
bash2perl [options] [file]
Do a rudimentary conversion of a Bash script to Perl.
This just covers the tedious, simply changes. so it should save a lot
of pointless typing, but the hard parts are left to you.
Look for '#PROB' marking things it knows it didn't convert right.
=head1 Options
=over
=item * B<--quiet> OR B<-q>
Suppress most messages.
=item * B<--version>
Display version info and exit.
=back
=head1 To do
case; PROB cases.
$ENV:
${}
line-final \
-e, etc.
=head1 History
2006-??: Written by Steven J. DeRose.
2007-07-25 sjd: Draft a little more.
2007-10-31 sjd: strict, Getopt, cleanup.
2010-09-12, 2012-09-05 sjd: Clean up.
2012-09-05: ?
2022-10-06: New layout. No longer maintained.
=head1 Ownership
This work by Steven J. DeRose is licensed under a Creative Commons
Attribution-Share Alike 3.0 Unported License. For further information on
this license, see L<http://creativecommons.org/licenses/by-sa/3.0/>.
For the most recent version, see L<http://www.derose.net/steve/utilities/>.
=cut
###############################################################################
#
my $quiet = 0;
my $verbose = 0;
my %getoptHash = (
"h|help|?" => sub { system "perldoc $0"; exit; },
"q|quiet!" => \$quiet,
"v|verbose+" => \$verbose,
"version" => sub {
die "Version of $VERSION_DATE, by Steven J. DeRose.\n";
},
);
Getopt::Long::Configure ("ignore_case");
GetOptions(%getoptHash) || die("Bad options.\n");
###############################################################################
#
my $file = shift;
(-f $file) || die "bash2perl: Couldn't find file '$file'.\n";
# Useful regexes and other constants
my $bashVar = "\\$\w+";
my $PROB = "\t #PROBLEM";
# List some common environment variables, so we can distinguish them from
# any other shell variables (or should we just convert anything?). Or,
# We could extract variables and test if they're defined (but there might well
# be references to undefined shell variables, too). Doesn't seem to be
# a good solution.
#
my $envVars =
" BASH BASH_VERSION CDPATH CLASSPATH" .
" COLUMNS DISPLAY EDITOR HISTCONTROL" .
" CPU CVSROOT CVSROOT_DIR CVS_CONVERTER DISPLAY" .
" EDITOR HISTCONTROL HISTFILE HISTFILESIZE HISTSIZE" .
" HOME HOST HOSTNAME HOSTTYPE LANG" .
" LD_LIBRARY_PATH LD_RUN_PATH LESS LESSCLOSE" .
" LESSKEY LESSOPEN LESS_ADVANCED_PREPROCESSOR LINES" .
" LPDEST MACHTYPE MAIL MANPATH OSTYPE" .
" PAGER PATH PERL5LIB POSTSCRIPT PRINTER PWD" .
" SHELL SHLVL" .
" SSH_AUTH_SOCK SSH_CLIENT SSH_CONNECTION SSH_TTY" .
" TERM USER VISUAL WINDOWMANAGER" .
" XAUTHLOCALHOSTNAME XCURSOR_THEME XKEYSYMDB ";
###############################################################################
# Main
#
my $shebang = "#!/usr/bin/env perl -w\n";
my $rec = <>;
(substr($rec,0,2) eq "#!") || die "First line doesn't start right.\n";
print $shebang;
print "#\n";
print "# ((converted from Bash to Perl by bash2perl $VERSION_DATE, " .
"by Steven J. DeRose.\n";
print "#\n";
my $probCount = 0;
my %subList = ();
my $lineCount = 0;
my $unknownCount = 0;
while ($rec = <>) {
$lineCount++;
chomp $rec;
while ($rec =~ m/\\$/) { # continued-line
$rec .= <>;
$lineCount++;
chomp $rec;
}
$rec =~ s/\s*;\s*$//;
(my $indent = $rec) =~ s/[^\s].*$//;
my @tokens = split(/\s+/, $rec);
my $key = $tokens[0];
my $key1 = substr($key,0,1);
if (substr($key1,0,1) eq "#") { # comment
if (substr($key1,1,1) eq "!") {
$rec = $shebang .
"# (converted from Bash by bash2perl, $VERSION_DATE, " .
"by Steven J. DeRose.\n";
}
print "$rec\n";
next;
}
# Remove any final comment
my $commentPart = "";
if ($rec =~ m/[^\\]\#/) {
($commentPart = $rec) =~ s/^.*[^\\]\#/\#/;
$rec =~ s/([^\\])\#.*$/\1/;
}
if ($rec =~ m/()\s+\{/) { # subroutine
$rec = $indent . "sub " . $rec;
}
elsif ($rec =~ m/^\s*\w+=/) { # assignment
$rec = "\$" . $rec;
$rec =~ s/=/ = /;
}
# Now match on the first token to see if it's a keyword
elsif ($key1 eq "`") { # `
}
elsif ($key1 eq "\{") { # for sub/function
}
elsif ($key1 eq "\}") { #
}
elsif ($key =~ m/[a-zA-Z_]+=/) { # assignment
$rec =~ s/([a-zA-Z_]+)=(.*);?$/$\1 = \2\;/;
}
elsif ($key eq "if") { # flow...
$rec = fixCondition($rec);
}
elsif ($key eq "elif") {
$rec = fixCondition($rec);
$rec =~ s/elif/else if/;
}
elsif ($key eq "fi") {
$rec =~ s/fi/\}/;
}
elsif ($key eq "for") {
$rec =~ s/\s+in\s+(.*);\s*do/(\1) {/;
}
elsif ($key eq "case") {
$rec .= $PROB;
}
elsif ($key eq "esac") {
$rec =~ s/esac/\}/;
}
elsif ($key eq "while") {
$rec .= $PROB;
}
elsif ($key eq "until") {
$rec .= $PROB;
}
elsif ($key eq "done") {
$rec =~ s/done/\}/;
}
elsif ($key eq "exit") {
$rec .= ";";
}
elsif ($key eq "return") {
$rec .= ";";
}
elsif ($key eq "echo") { # echo
$rec =~ s/echo/print/;
$rec =~ s/\"\s*$/\\n\"/;
$rec .= ";";
}
elsif ($key eq "let") { # assignments
$rec =~ s/let\s+/\$/;
$rec =~ s/=/ = /;
}
elsif ($key eq "set") {
$rec .= $PROB;
}
elsif ($key eq "unset") {
$rec .= $PROB;
}
elsif ($key eq "export") {
$rec .= $PROB;
}
elsif ($key eq "cd") { # miscellaneous
$rec =~ s/^\s+cd\s+/$indent chdir /;
$rec .= ";";
}
elsif ($key eq "shift") {
$rec .= ";";
}
elsif ($key eq "system") {
$rec .= ";";
}
elsif ($key eq "function") {
$rec =~ s/function/sub/;
$subList{$tokens[1]} = 1;
}
elsif ($key eq "sub") {
$subList{$tokens[1]} = 1;
}
elsif ($key eq "local") {
$rec =~ s/local/my/;
# need to add commas
}
elsif ($key eq "alias") { # problem commands
$rec .= $PROB;
}
elsif ($subList{$key}) { # ???
# Ok
}
else {
($verbose) && warn "Unknown keyword '$key' in line $lineCount.\n";
$unknownCount++;
chomp $rec;
$rec =~ s/^\s*(.*)\$/ "system \"$rec\"\n/;
}
# General cleanup
$rec =~ s/\$([0-9]+)/$ARGV[{$1-1}]/g; # parameter names
my $lastchar = substr($rec,length($rec)-1); # eh
if (index("{};",$lastchar) < 0) {
$rec .= ";";
}
print "$rec";
if ($commentPart) { print "\t $commentPart"; }
print "\n";
} # while not EOF
warn "Done, $lineCount lines, $unknownCount unidentified (system?)\n";
exit;
###############################################################################
# Bash conditions have complicated syntax... Handle some of it here.
sub fixCondition {
my $bashCond = "\\[ ([^]]*) \\]";
if ($rec =~ m/$bashCond/o) {
$rec =~ s/$bashCond\s*/(\1)/g;
}
else {
$rec =~ s/([a-z]*)\s+(.*);\s*then/\1 (`\2`) \{/;
}
# Now enclose the whole condition and fix the "then"
$rec =~ s/;\s*then/) \{/;
$rec =~ s/^(\s*[elsif]*)\s+/\1 (/;
# Change operators as needed
# eq etc.
}