[system] / trunk / webwork / system / scripts / pb_new.pl Repository:
ViewVC logotype

Annotation of /trunk/webwork/system/scripts/pb_new.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6 - (view) (download) (as text)

1 : gage 6 #!/usr/local/bin/perl
2 : sam 2
3 : gage 6
4 : sam 2 # 'pb_new' Perl Beautifier
5 :    
6 : gage 6
7 :    
8 : sam 2 # Written by P. Lutus Ashland, Oregon lutusp@arachnoid.com 5/20/96
9 : gage 6
10 : sam 2 # slight changes by A. Pizer (apizer@math.rochester.edu) 10/2/97
11 :    
12 : gage 6
13 :    
14 : sam 2 # This script processes Perl scripts, cleans up and indents, like cb does
15 : gage 6
16 : sam 2 for C
17 :    
18 : gage 6
19 :    
20 : sam 2 # Be careful with this script - it accepts wildcards and processes every
21 : gage 6
22 : sam 2 text file
23 : gage 6
24 : sam 2 # that meets the wildcard criteria. This could be a catastrophe in the
25 : gage 6
26 : sam 2 hands of the unwary.
27 :    
28 : gage 6
29 :    
30 : sam 2 $tabstring = " "; # You may place any tab-stop characters you want here
31 : gage 6
32 : sam 2 # Pizer changed this to 4 spaces
33 : gage 6
34 : sam 2
35 : gage 6
36 : sam 2 if($ARGV[0] eq "") {
37 : gage 6
38 : sam 2 print "usage: file1 file2 etc. or wildcards (replaces originals in
39 : gage 6
40 : sam 2 place)\n";
41 : gage 6
42 : sam 2 }
43 : gage 6
44 : sam 2 else {
45 : gage 6
46 : sam 2 foreach $filename (@ARGV) {
47 : gage 6
48 : sam 2 if(-T $filename) {
49 : gage 6
50 : sam 2 &process($filename);
51 : gage 6
52 : sam 2 }
53 : gage 6
54 : sam 2 }
55 : gage 6
56 : sam 2 }
57 :    
58 : gage 6
59 :    
60 : sam 2 sub process {
61 : gage 6
62 : sam 2 $fn = $_[0];
63 : gage 6
64 : sam 2 undef $/; # so we can grab the entire file at once
65 : gage 6
66 : sam 2 undef @infa; # prevent left-overs
67 : gage 6
68 : sam 2 print STDERR "$fn";
69 : gage 6
70 : sam 2 open (INFILE,$fn);
71 : gage 6
72 : sam 2 @infa = split(/\n/,<INFILE>);
73 : gage 6
74 : sam 2 close INFILE;
75 : gage 6
76 : sam 2
77 : gage 6
78 : sam 2 $/ = "\n"; # restore default
79 : gage 6
80 : sam 2
81 : gage 6
82 : sam 2 open (OUTFILE,">$fn");
83 : gage 6
84 : sam 2 $tabtotal = 0;
85 : gage 6
86 : sam 2 for (@infa) {
87 : gage 6
88 : sam 2
89 : gage 6
90 : sam 2 unless ($_ =~/^\s*#/){ #Pizer changed this to ignore comment lines
91 : gage 6
92 : sam 2 s/^\s*(.*?)\s*$/$1/; # strip leading and trailing spaces
93 : gage 6
94 : sam 2 }
95 : gage 6
96 : sam 2 $a = $_; # copy original string
97 : gage 6
98 : sam 2 $q = $a; # i plan to modify this copy for testing
99 : gage 6
100 : sam 2 $q =~ s/\\\#//g; # remove escaped comment tokens
101 : gage 6
102 : sam 2 $q =~ s/\#.*?$//g; # remove Perl-style comments
103 : gage 6
104 : sam 2 $q =~ s{/\*.*?\*/} []gsx; # remove C-style comments
105 : gage 6
106 : sam 2 $q =~ s/\\\{//g; # remove escaped left braces
107 : gage 6
108 : sam 2 $q =~ s/\\\}//g; # remove escaped right braces
109 : gage 6
110 : sam 2 $q =~ s/\\\(//g; # remove escaped left parentheses
111 : gage 6
112 : sam 2 $q =~ s/\\\)//g; # remove escaped right parentheses
113 : gage 6
114 : sam 2
115 : gage 6
116 : sam 2 $q =~ s/\'.*?\'//g; # remove single-quoted lines
117 : gage 6
118 : sam 2
119 : gage 6
120 : sam 2 # now the remaining braces/parentheses should be structural
121 : gage 6
122 : sam 2
123 : gage 6
124 : sam 2 $delta = -($q =~ s/\}/\}/g); # subtract closing braces
125 : gage 6
126 : sam 2 $delta += ($q =~ s/\{/\{/g); # add opening braces
127 : gage 6
128 : sam 2
129 : gage 6
130 : sam 2 $delta -= ($q =~ s/\)/\)/g); # subtract closing parens
131 : gage 6
132 : sam 2 $delta += ($q =~ s/\(/\(/g); # add opening parens
133 : gage 6
134 : sam 2
135 : gage 6
136 : sam 2 $tabtotal += ($delta < 0)?$delta:0; # subtract closing braces/parentheses
137 : gage 6
138 : sam 2
139 : gage 6
140 : sam 2 $i = ($tabtotal > 0)?$tabtotal:0; # create tab index
141 : gage 6
142 : sam 2
143 : gage 6
144 : sam 2 $tabtotal += ($delta>0)?$delta:0; # add opening braces/parentheses for
145 : gage 6
146 : sam 2 next print
147 : gage 6
148 : sam 2
149 : gage 6
150 : sam 2 unless ($a =~/^\s*#/) { # don't tab comments Pizer change
151 : gage 6
152 : sam 2 print OUTFILE $tabstring x $i; # "tab" out to position
153 : gage 6
154 : sam 2 }
155 : gage 6
156 : sam 2 print OUTFILE "$a\n"; # print original line
157 : gage 6
158 : sam 2 } # -- for (@infa)
159 : gage 6
160 : sam 2
161 : gage 6
162 : sam 2 close OUTFILE;
163 : gage 6
164 : sam 2
165 : gage 6
166 : sam 2 if($tabtotal != 0) {
167 : gage 6
168 : sam 2 print STDERR " Indentation error: $tabtotal\n";
169 : gage 6
170 : sam 2 }
171 : gage 6
172 : sam 2 else {
173 : gage 6
174 : sam 2 print STDERR "\n";
175 : gage 6
176 : sam 2 }
177 : gage 6
178 : sam 2 } # sub process
179 : gage 6

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9