8f2641fa |
1 | #!/usr/bin/env perl |
ccad13ab |
2 | # ri_once.pl |
3 | # Wouter Teepe (wouter@teepe.com) |
4 | # |
5 | # A simple configure script to fix the ri_once issue |
6 | # |
7 | # $Id$ |
8 | ############################################################ |
9 | |
10 | $debug = 0; |
11 | |
12 | # sets the xterm color (only used when $debug=1) |
13 | sub color { |
14 | $t = $_[0]; |
15 | if ($t == 0) { |
16 | print "\e[0m"; |
17 | } else {if ($t == 1) { |
18 | print "\e[0m\e[31;43m"; |
19 | } else {if ($t == 2) { |
20 | print "\e[0m\e[30;42m"; |
21 | } else {if ($t == 3) { |
22 | print "\e[0m\e[33;41m"; |
23 | }}}} |
24 | } |
25 | |
26 | # prints arg1 with color arg2 (only used when $debug=1) |
27 | sub myprintdebug { |
28 | $line = $_[0]; |
29 | $color = $_[1]; |
30 | while ($line ne "") { |
31 | $pos = index($line, "\n"); |
32 | if ($pos == -1) { |
33 | &color($color); |
34 | print $line; |
35 | $line = ""; |
36 | } else { |
37 | &color($color); |
38 | $str = substr($line, 0, $pos); |
39 | print $str; |
40 | print "\e[30;40m\n"; |
41 | $line = substr($line, $pos+1); |
42 | } |
43 | } |
44 | |
45 | } |
46 | |
47 | # print arg1 to term or $out |
48 | sub myprint { |
49 | if ($debug) { |
50 | &myprintdebug; |
51 | } else { |
52 | $out .= $_[0]; |
53 | } |
54 | } |
55 | |
56 | # parse php code fore include's and require's |
57 | sub code { |
58 | $lastbyte = 0; |
59 | while ($phpcode ne "") { |
60 | $inc = index($phpcode, 'include'); |
61 | $req = index($phpcode, 'require'); |
62 | if (($req == -1) or (($inc != -1) and ($inc < $req))) { |
63 | $index = $inc; |
64 | } else { |
65 | $index = $req; |
66 | } |
67 | if ($index != -1) { |
68 | &myprint(substr($phpcode, 0, $index), 1); |
69 | if ($index > 0) { |
70 | $r = ord(substr($phpcode, $index-1, 1)); |
71 | } else { |
72 | $r = $lastbyte; |
73 | } |
74 | $falsematch = 0; |
75 | if ((($r >= 125) and ($r <= 255)) or ($r == ord("_")) or |
76 | (($r >= ord("A")) and ($r <= ord("Z"))) or |
77 | (($r >= ord("a")) and ($r <= ord("z"))) or |
78 | (($r >= ord("1")) and ($r <= ord("9")))) { |
79 | $falsematch = 1; |
80 | } |
81 | if (!$falsematch) { |
82 | $o = index($phpcode, '(', $index); |
83 | if ($o == -1) { |
84 | $p = substr($phpcode, $index+7); |
85 | } else { |
86 | $p = substr($phpcode, $index+7, $o-$index-7); |
87 | } |
88 | if (!($p =~ /^\s*$/)) { |
89 | $falsematch = 1; |
90 | } |
91 | } |
92 | if (!$falsematch) { |
93 | $mod++; |
94 | &myprint('/* \'_once\' Added by ri_once */ ', 3); |
95 | } |
96 | &myprint(substr($phpcode, $index, 7), 1); |
97 | if (!$falsematch) { |
98 | &myprint('_once', 3); |
99 | } |
100 | $lastbyte = ord('e'); |
101 | $phpcode = substr($phpcode, $index+7); |
102 | } else { |
103 | &myprint($phpcode, 1); |
104 | $phpcode = ''; |
105 | } |
106 | } |
107 | } |
108 | |
109 | # parse php block for comments and strings |
110 | sub php { |
111 | while ($htmlcomment ne "") { |
112 | $len = length($htmlcomment); |
113 | $doublequote = index($htmlcomment, '"'); |
114 | $singlequote = index($htmlcomment, "'"); |
115 | $pound = index($htmlcomment, '#'); |
116 | $slashslash = index($htmlcomment, '//'); |
117 | $slashstar = index($htmlcomment, '/*'); |
118 | if ($doublequote == -1) { $doublequote = $len; } |
119 | if ($singlequote == -1) { $singlequote = $len; } |
120 | if ($pound == -1) { $pound = $len; } |
121 | if ($slashslash == -1) { $slashslash = $len; } |
122 | if ($slashstar == -1) { $slashstar = $len; } |
123 | |
124 | $pos = $doublequote; $end = '"'; $sl = 1; $el = 1; |
125 | if ($pos > $singlequote) { $pos = $singlequote; $end = "'"; } |
126 | if ($pos > $pound) { $pos = $pound; $end = "\n"; } |
127 | if ($pos > $slashslash) { $pos = $slashslash; $end = "\n"; $sl = 2; } |
128 | if ($pos > $slashstar) { $pos = $slashstar; $end = '*/'; $sl = 2; $el = 2; } |
129 | |
130 | if ($pos < $len) { |
131 | $phpcode = substr($htmlcomment, 0, $pos); |
132 | $rest = substr($htmlcomment, $pos); |
133 | $eoc = index($rest, $end, $sl); |
134 | if (($end = '"') or ($end = "'")) { |
135 | while (($eoc > 0) and (substr($rest, $eoc-1, 1) eq '\\')) { |
136 | $eoc = index($rest, $end, $eoc+1); |
137 | } |
138 | } |
139 | if ($eoc == -1) { $eoc = length($rest); } |
140 | $phpcomment = substr($rest, 0, $eoc+$el); |
141 | $htmlcomment = substr($rest, $eoc+$el); |
142 | &code; |
143 | &myprint($phpcomment, 2); |
144 | } else { |
145 | $phpcode = $htmlcomment; |
146 | &code; |
147 | $htmlcomment = ''; |
148 | } |
149 | } |
150 | } |
151 | |
152 | # parse html file for php blocks |
153 | sub html { |
154 | while ($text ne '') { |
155 | $index = index($text, '<?'); |
156 | if ($index != -1) { |
157 | $html = substr($text, 0, $index+2); |
158 | $text = substr($text, $index+2); |
159 | &myprint($html, 0); |
160 | $index = index($text, '?>'); |
161 | if ($index == -1) { $index = length($text); } |
162 | $htmlcomment = substr($text, 0, $index); |
163 | $text = substr($text, $index); |
164 | $type = substr($htmlcomment, 0, 3); |
165 | if (uc($type) eq "PHP") { |
166 | &myprint($type, 0); |
167 | $htmlcomment = substr($htmlcomment, 3); |
168 | } else { if (substr($htmlcomment, 0, 1) eq '=') { |
169 | $mod++; |
170 | &myprint('php /* \'=\' Modified to \'php blah echo\'by ri_once */ echo ', 3); |
171 | $htmlcomment = substr($htmlcomment, 1); |
172 | } else { |
173 | $mod++; |
174 | &myprint('php /* \'php\' Added by ri_once */', 3); |
175 | }} |
176 | &php; |
177 | } else { |
178 | &myprint($text, 0); |
179 | $text =''; |
180 | } |
181 | } |
182 | } |
183 | |
184 | # process a file |
185 | sub dofile { |
186 | $file = $_[0]; |
187 | |
188 | open (FILE, '<'.$file); |
189 | $text = ''; |
190 | $htmlcomment = ''; |
191 | $phpcode = ''; |
192 | $out = ''; |
193 | $mod = 0; |
194 | while ($line = <FILE>) { |
195 | $text .= $line; |
196 | } |
197 | close (FILE); |
198 | |
199 | &html; |
200 | if ($debug) { |
201 | &color(0); |
202 | print "\n"; |
203 | } else { |
204 | if ($mod) { |
205 | $out = "<?php /* Modified at $mod places by ri_once */ ?>\n" . $out; |
206 | # $mode = (stat($file))[2]; |
207 | rename($file, $file.'.before_ri_once'); |
208 | open (FILE, '>'.$file); |
209 | print FILE $out; |
210 | close(FILE); |
211 | # chmod($stats[2], $file); |
212 | } |
213 | } |
214 | } |
215 | |
216 | # process a directory recursively |
217 | sub dodir { |
36baad63 |
218 | my $dirname; |
219 | my $file; |
220 | my $full; |
221 | my @files; |
222 | my $i; |
ccad13ab |
223 | $dirname = $_[0]; |
224 | $dirname =~ s/\/$//; |
225 | opendir(DIR, $dirname) or die "can't opendir $dirname: $!"; |
36baad63 |
226 | $i = 0; |
ccad13ab |
227 | while (defined($file = readdir(DIR))) { |
36baad63 |
228 | @files[$i++] = $file; |
229 | } |
230 | $i = 0; |
231 | while (defined($file = @files[$i++])) { |
ccad13ab |
232 | next if $file =~ /^\.\.?$/; |
233 | $full = $dirname.'/'.$file; |
36baad63 |
234 | # print "found: $full\n"; |
ccad13ab |
235 | if (-d $full) { |
36baad63 |
236 | # print "doing dir: $full\n"; |
ccad13ab |
237 | &dodir($full); |
238 | } else { if ($file =~ /.*\.php$/) { |
36baad63 |
239 | # print "doing file: $full\n"; |
ccad13ab |
240 | &dofile($full); |
241 | }} |
242 | } |
243 | closedir(DIR); |
244 | } |
245 | |
246 | #&dofile($ARGV[0]); |
247 | &dodir($ARGV[0]); |
248 | |