Debian Codegraph @

scripts/cg3-autobin.pl.in

raw
1 #!/usr/bin/perl
2 # -*- mode: cperl; indent-tabs-mode: t; tab-width: 3; cperl-indent-level: 3; -*-
3 BEGIN { $| = 1; }
4 use warnings;
5 use strict;
6 use List::Util qw(max);
7 use File::Basename;
8 use File::Spec;
9 use Getopt::Long;
10 use Digest::SHA qw(sha1_hex);
11 Getopt::Long::Configure('no_ignore_case');
12
13 # This is updated by the update-revision.pl script.
14 my $revision = @REVISION@;
15
16 # Generate list with:
17 # vislcg3 --help 2>&1 | perl -wpne 'if (/^ / && /-(\w), --([-\w]+)/) {print "$2|$1=s\n"} elsif (/^ / && /--([-\w]+)/) {print "$1=s\n"} s/^.*$//s;' | perl -wpne 's/^/"/; s/$/",/;'
18 # and then trim =s from those that don't take args
19 my %h = ();
20 GetOptions (\%h,
21 "help|h",
22 "version|V",
23 "min-binary-revision",
24 "grammar|g=s",
25 "grammar-out=s",
26 "grammar-bin=s",
27 "grammar-only",
28 "ordered",
29 "unsafe|u",
30 "sections|s=s",
31 "rules=s",
32 "rule=s",
33 "debug|d:s",
34 "verbose|v:s",
35 "quiet",
36 "vislcg-compat|2",
37 "stdin|I=s",
38 "stdout|O=s",
39 "stderr|E=s",
40 "codepage-all|C=s",
41 "codepage-grammar=s",
42 "codepage-input=s",
43 "codepage-output=s",
44 "no-mappings",
45 "no-corrections",
46 "no-before-sections",
47 "no-sections",
48 "no-after-sections",
49 "trace|t:s",
50 "trace-name-only",
51 "trace-no-removed",
52 "trace-encl",
53 "deleted",
54 "dry-run",
55 "single-run",
56 "max-runs=s",
57 "statistics|S",
58 "optimize-unsafe|Z",
59 "optimize-safe|z",
60 "prefix|p=s",
61 "unicode-tags",
62 "unique-tags",
63 "num-windows=s",
64 "always-span",
65 "soft-limit=s",
66 "hard-limit=s",
67 "text-delimit|T:s",
68 "dep-delimit|D:s",
69 "dep-original",
70 "dep-allow-loops",
71 "dep-no-crossing",
72 "no-magic-readings",
73 "no-pass-origin|o",
74 "split-mappings",
75 "show-end-tags|e",
76 "show-unused-sets",
77 "show-tags",
78 "show-tag-hashes",
79 "show-set-hashes",
80 "dump-ast",
81 "no-break|B",
82 );
83
84 if (defined $h{'grammar-bin'}) {
85 die "Error: Cannot use --grammar-bin with the autobin wrapper !\n";
86 }
87
88 if (! defined $h{'grammar'}) {
89 die "Error: Missing --grammar or -g !\n";
90 }
91 my $grammar = $h{'grammar'};
92
93 if (! (-r $h{'grammar'})) {
94 die "Error: Cannot read file ".$h{'grammar'}." !\n";
95 }
96
97 my $args = " ";
98 while (my ($k,$v) = each(%h)) {
99 if ($k ne 'grammar') {
100 $args .= " --$k $v";
101 }
102 }
103
104 sub file_get_contents {
105 my ($fname) = @_;
106 local $/ = undef;
107 open FILE, '<:encoding(UTF-8)', $fname or die "Could not open ${fname}: $!\n";
108 my $data = <FILE>;
109 close FILE;
110 return $data;
111 }
112
113 sub mtime_rec {
114 my ($fn) = @_;
115 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($fn);
116 my $g = file_get_contents($fn);
117 $g =~ s@#[^\n]*\n@\n@g;
118 #print STDERR "DEBUG: $fn mtime $mtime\n";
119
120 while ($g =~ m@(?:^|\n|;)INCLUDE(?:\s+STATIC)?\s+(.+?)\s+;@gsi) {
121 my $ifn = $1;
122 #print STDERR "DEBUG: Including $ifn (pre-expand)\n";
123 if ($ifn =~ m@[~*\$]@) {
124 $ifn = glob($ifn);
125 }
126 if ($ifn !~ m@^/@) {
127 $ifn = dirname($fn).'/'.$ifn;
128 }
129 #print STDERR "DEBUG: Including $ifn\n";
130 $mtime = max($mtime, mtime_rec($ifn));
131 }
132
133 return $mtime;
134 }
135
136 my $mtime = mtime_rec($h{'grammar'});
137 #print STDERR "DEBUG: mtime $mtime\n";
138
139 my $bn = File::Spec->tmpdir()."/".sha1_hex($args.$h{'grammar'}).".".$revision.".".$mtime.".cg3b";
140
141 my $bin = 'vislcg3';
142 if (-x '/usr/bin/vislcg3') {
143 $bin = '/usr/bin/vislcg3';
144 }
145 if (-x '/usr/local/bin/vislcg3') {
146 $bin = '/usr/local/bin/vislcg3';
147 }
148 if (-x '~/bin/vislcg3') {
149 $bin = '~/bin/vislcg3';
150 }
151
152 if (!(-r $bn) && !(-r $bn.".lock")) {
153 open(F, ">".$bn.".lock") && close F;
154 `$bin $args --grammar $grammar --grammar-only --grammar-bin $bn`;
155 unlink $bn.".lock";
156 }
157
158 my $cmd = '';
159 if (-r $bn && !(-r $bn.".lock")) {
160 # print STDERR "CG3 AutoBin using $bn\n";
161 $cmd = "cat /dev/stdin | $bin $args --grammar $bn";
162 }
163 else {
164 print STDERR "CG3 AutoBin failed - falling back to normal\n";
165 $cmd = "cat /dev/stdin | $bin $args --grammar $grammar";
166 }
167
168 if (defined $h{'stdin'}) {
169 $cmd =~ s@^cat /dev/stdin | @@;
170 }
171
172 open CG, "$cmd|" or die $!;
173 while (<CG>) {
174 print;
175 }
176 close CG;