/
c
executable file
·64 lines (54 loc) · 1.38 KB
/
c
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
#!/usr/bin/perl -w
use Term::ANSIColor;
use Getopt::Long;
$nomain = 0;
$nonu = 0;
$argv = 0;
GetOptions(
"nomain|n" => \$nomain,
"nonu" => \$nonu,
"argv=s" => \$argv,
);
die "$0 [--nomain] [--argv=<argv>] [--nonu] 'C code...'\n" if(!@ARGV);
$libjg = "$ENV{HOME}/cpp/lib/libjg.a";
$libjg = "" unless (-e $libjg);
my $C = ".$$.c";
my $O = ".$$.out";
my @headers = qw/cstdio cstdlib algorithm iostream iomanip vector list map deque sstream fstream fcntl.h/;
sub intr { unlink $C, $O; }
$SIG{INT} = 'intr';
open CODE, "> $C";
my $code;
$code .= "#include <$_>\n" for(@headers);
$code .= "#include \"libjg.h\"\n" if ($libjg);
$code .= "using namespace std;\n".
($nomain ? "" : "\nint main(int argc, char **argv) {\n");
my @lines;
push @lines, split("\n", $_) for(@ARGV);
shift @lines unless($lines[0]);
$code .= ($nomain ? "" : "\t")."$_\n" for(@lines);
$code .= "}\n" unless($nomain);
print CODE $code;
close CODE;
print colored ":: CODE\n", "BOLD BLUE";
open CODE, "< $C";
@code = <CODE>;
$ln = 1;
for(@code) {
$lstr = (!$nonu) ? (sprintf "%3d ", $ln++) : "";
print colored $lstr, "GREEN";
print;
}
close CODE;
print "\n";
$cmd = "g++ -o $O $C";
$cmd .= " \"$libjg\" -I\"$ENV{HOME}/cpp/lib\"" if ($libjg);
system $cmd;
if ($? == 0) {
print colored ":: RESULT\n", "BOLD BLUE";
$cmd = "./$O ";
$cmd .= $argv if ($argv);
system $cmd;
print colored "\n:: FINISHED\n", "BOLD WHITE";
}
&intr;