ri_once.pl 6.1 KB

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