-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsheeple.pl
executable file
·376 lines (351 loc) · 9.03 KB
/
sheeple.pl
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
#!/usr/bin/perl -w
$SRC_filename = $ARGV[0];
# DES output to a file for test comparison only
# $DES_filename = 'z';
open (SRC, '<', $SRC_filename) or die "Can not open file.\n";
@file_content = <SRC>;
close (SRC);
# open (DES, '>', $DES_filename) or die "Can not open file.\n";
sub first_line_env {
my ($l) = @_;
if ($l =~ /^\#\!\/.*/) {
print "#!/usr/bin/perl -w\n";
}
}
sub copy_comments {
my ($l) = @_;
if ($l =~ /^\#\s+(.*)/) {
print "$l";
}
}
sub equal_n_dollar {
my ($l) = @_;
if ($l !~ /if .*/){
if ($l =~ /\s*(.*)\=(.*)/) {
if ($for_flag eq 1) {
print " ";
}
if ($while_loop eq 1) {
print " ";
}
my $j = $1;
my $k = $2;
# print "$k\n";
if ($k =~ /^\$(.+)/) {
if ($k =~ /\$\(\((.*)\)\)(.*)/) {
# for seq math
print "\$$j = \$$1\;$2\n";
} else {
# if the second var is starting with dollar
print "\$$j = $k\;\n";
}
} elsif ($k =~ /\`expr (.*)\`(.*)/){
# for seq expr
print "\$$j = $1\;$2\n";
} else {
# plain string assignment which only adds quotes is good
print "\$$j = \'$k\'\;";
print "\n";
}
}
}
}
sub echo_function {
my ($l) = @_;
# match single quotes
if ($l =~ /(\s*)echo \'(.*)\'/) {
# check if in for loop
if ($for_flag eq 1) {
print " ";
}
if ($while_loop eq 1) {
print " ";
}
$t = $2;
# check if contain doule quotes there
# for truth.sh
if ($t =~ /\s*\"(.*)\"(.*)/) {
print 'print "\"';
print "$1";
print '\"';
print "$2";
print '\n";';
print "\n";
} else {
print "print \"$t";
print '\n";';
print "\n";
}
# match double quotes
} elsif ($l =~ /\s*echo \"(.+)\"/) {
if ($for_flag eq 1) {
print " ";
}
if ($while_loop eq 1) {
print " ";
}
print "print \"$1\\n\"\;\n";
# match echo following without any quotes
} elsif ($l =~ /(\s*)echo(\s+)(.*)/) {
# check for loop
if ($for_flag eq 1) {
print " ";
}
if ($while_loop eq 1) {
print " ";
}
# check if
elsif ($if_flag eq 1 && $elif_flag eq 0) {
print " ";
$if_print_flag = 1;
}
elsif ($elif_flag eq 1 && $elif_print_flag eq 1) {
print " ";
$elif_print_flag = 1;
}
print "print \"$3\\n\"\;";
print "\n";
}
}
sub system_function {
my ($l) = @_;
if ($l =~ /pwd/) {
if ($for_flag eq 1) {
print " ";
}
print "system \"pwd\"\;";
print "\n";
}
if ($l =~ /(.*ls.*)/) {
if ($l !~ /else/) {
if ($for_flag eq 1) {
print " ";
}
print "system \"$1\"\;";
print "\n";
}
}
if ($l =~ /id\s+/) {
if ($for_flag eq 1) {
print " ";
}
print "system \"id\"\;";
print "\n";
}
if ($l =~ /date/) {
if ($for_flag eq 1) {
print " ";
}
print "system \"date\"\;";
print "\n";
}
if ($l =~ /(gcc.*)/) {
if ($l !~ /echo gcc/) {
if ($for_flag eq 1) {
print " ";
}
print "system \"$1\"\;";
print "\n";
}
}
}
sub copy_empty_line {
my ($l) = @_;
if ($l =~ /^\s*$/) {
print "\n";
}
}
sub for_loop {
my ($l) = @_;
if ($l =~ /for (.+) in (.+)\n/) {
# check for c files
if ($l =~ /for (.+) in (\*\.c)\n/) {
print "foreach \$$1 (";
print "glob\(\"$2\"\)";
print ") {\n";
} else {
# if not c files just split by space then add quotes
my @vars = split / /, $2;
foreach my $s (@vars) {
if ($s !~ /\d+/) {
$s = "\'$s\'";
}
}
print "foreach \$$1 (";
print join ', ', @vars;
print ") {\n";
}
}
}
sub read_function {
my ($l) = @_;
if ($l =~ /read (.+)/) {
if ($for_flag eq 1) {
print " ";
}
print "\$$1 = <STDIN>\;";
print "\n";
if ($for_flag eq 1) {
print " ";
}
print "chomp \$$1\;";
print "\n";
}
}
sub cd_function {
my ($l) = @_;
if ($l =~ /cd (.+)/) {
if ($for_flag eq 1) {
print " ";
}
print "chdir \'$1\'\;";
print "\n";
}
}
sub exit_function {
my ($l) = @_;
if ($l =~ /exit (.+)/) {
if ($for_flag eq 1) {
print " ";
}
print "exit $1\;";
print "\n";
}
}
sub check_argument {
my ($l) = @_;
# check for var $\d in bash and change to $ARGV[\d]
if ($l =~ /(.*)\$(\d+)(.*)/) {
my $arg_part_1 = $1;
my $n = $2 - 1;
my $arg_part_2 = $3;
my $result = "$arg_part_1\$ARGV\[$n\]$arg_part_2";
return $result;
} elsif ($l =~ /(.*)\"\$\@\"(.*)/) {
my $result = "$1\@ARGV$2";
return $result;
} else {
return $l;
}
}
sub if_condtion {
my ($l) = @_;
if ($l =~ /^if (.+)\n/) {
my $i = $1;
$if_flag = 1;
# if test two var equal
if ($i =~ /test ([a-zA-Z]*) = ([a-zA-Z]*)/){
print "if \(\'$1\' eq \'$2\'\) \{\n"
} elsif ($i =~ /\[ -d (.*) \]/) {
# if [ -d ]
print "if \(\-d \'$1\'\) \{\n";
} elsif ($i =~ /test \-d (.+)/) {
# if test -d
print "if \(\-d \'$1\'\) \{\n";
} elsif ($i =~ /test \-r (.+)/) {
# if test -r
print "if \(\-r \'$1\'\) \{\n";
}
}
if ($l =~ /^elif (.+)/) {
my $i = $1;
$elif_flag = 1;
# if test two var equal
if ($i =~ /test ([a-zA-Z]*) = ([a-zA-Z]*)/){
if ($if_print_flag eq 1 && $if_flag eq 1) {
print " } elsif \(\'$1\' eq \'$2\'\) \{\n";
$elif_print_flag = 1;
}
}
}
}
sub while_loop {
my ($l) = @_;
if ($l =~ /^while (.+)\n/) {
# print $1;
$while_loop = 1;
if ($1 =~ /test (.+)/){
# print $1;
if ($1 =~ /(.*) -(eq|ne|lt|le|gt|ge) (.*)/) {
print "while \($1 ";
my $h = $2;
my $y = $3;
if ($h =~ /le/) {
print '<= ';
} elsif ($h =~ /eq/) {
print '== ';
}
elsif ($h =~ /ne/) {
print '!= ';
}
elsif ($h =~ /lt/) {
print '< ';
}
elsif ($h =~ /gt/) {
print '> ';
}
elsif ($h =~ /ge/) {
print '>= ';
}
print "$y\) {\n";
}
}
}
}
$for_flag = 0;
$if_flag = 0;
$if_print_flag = 0;
$elif_flag = 0;
$elif_print_flag = 0;
$while_loop = 0;
# $user_made_function_flag = 0;
for(my $i = 0; $i <= $#file_content; $i++) {
first_line_env($file_content[$i]);
copy_comments($file_content[$i]);
$file_content[$i] = check_argument($file_content[$i]);
copy_empty_line($file_content[$i]);
# check for loop
if ($file_content[$i] =~ /for (.+) in (.+)/) {
$for_flag = 1;
for_loop($file_content[$i]);
}
if ($file_content[$i] =~ /do\n/) {
next;
}
if ($file_content[$i] =~ /done$|done\n/) {
$for_flag = 0;
print "}\n";
}
# check if condtion
if ($file_content[$i] =~ /then\n/) {
next;
}
if ($file_content[$i] =~ /else\n/) {
if ($if_print_flag eq 1 && $elif_flag eq 0) {
print "\} else \{\n";
}
if ($elif_print_flag eq 1 && $elif_flag eq 1) {
print " \} else \{\n";
}
}
if ($file_content[$i] =~ /fi\n/) {
if ($if_print_flag eq 1 && $elif_flag eq 0) {
print "\}\n";
}
}
if ($file_content[$i] =~ /fi\n/) {
if ($elif_print_flag eq 1 && $elif_flag eq 1) {
print " \}\n";
}
}
read_function($file_content[$i]);
echo_function($file_content[$i]);
equal_n_dollar($file_content[$i]);
system_function($file_content[$i]);
cd_function($file_content[$i]);
exit_function($file_content[$i]);
if_condtion($file_content[$i]);
while_loop($file_content[$i]);
# user_made_function($file_content[$i]);
}
# close (DES);