File: fixdeprecated.pl

package info (click to toggle)
emboss 6.6.0%2Bdfsg-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 571,584 kB
  • sloc: ansic: 460,579; java: 29,383; perl: 13,573; sh: 12,753; makefile: 3,294; csh: 706; asm: 351; xml: 239; pascal: 237; modula3: 8
file content (226 lines) | stat: -rwxr-xr-x 5,377 bytes parent folder | download | duplicates (8)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/perl -w

use English;

$basefile = $ARGV[0];
$basefile =~ s/[.]c$//;

open (DEP, "$ENV{HOME}/devemboss/deprecated.txt") || die "Cannot open deprecated.txt";
open (SRC, "$basefile.c") || die "Cannot open $basefile.c";
open (OLDSRC, ">$basefile.save") || die "Cannot open $basefile.save";
#open (DBG, ">fixdeprecated.dbg") || die "Cannot open fixdeprecated.dbg";

$patcnt=0;
$notecnt=0;
$gonecnt=0;
$subcnt=0;
$subcntall=0;
$new=0;

%gone=();
%note=();
%argtest=();
%pat=();
$ok = 1;
while (<DEP>){
    if (/^[\#](\S+)/) {
	if($1 eq $basefile) {$ok = 0}
	else {$ok = 1}
	next;
    }
    if(!$ok) {next}
    if(/^(\S+)\s+[-]$/) {
	$oldname = "$1\\s*[\\(]";
	$gone{$oldname} = 1;
	$gonecnt++;
    }
    elsif(/^(\S+)\s+[=](\S+)\s*(\S+)\s+(\S+)$/) {
	$oldname = "$1";
	$oldpat = "$1\\s*[\\(]([^\\(\\)]*([\\(][^\\)]*[\\)][^\\(\\)]*)*)[\\)]";
	$newname = "$2";
	$oldargs = $3;
	$newargs = $4;
	$subcntall++;
	if(defined($argtest{$oldpat})) {
	    $argtest{$oldpat} .= ";$oldname $newname $oldargs $newargs";
	}
	else {
	    $subcnt++;
	    $argtest{$oldpat} = "$oldname $newname $oldargs $newargs";
	}
    }
    elsif(/^(\S+)\s+[@](\S+)\s*$/) {
	$oldname = "$1\\s*[\\(]";
	$newname = "$2";
	$note{$oldname} = $newname;
	$notecnt++;
    }
    elsif(/^(\S+)\s+(\S+)\s*$/) {
	$oldname = "$1\\s*[\\(]";
	$newname = "$2(";
	$pat{$oldname} = $newname;
	$patcnt++;
    }
}

close DEP;

print "$basefile.c: Using $patcnt patterns, $subcntall edits for $subcnt functions and $gonecnt removals\n";

$savesrc = "";
$cnt=0;
while (<SRC>) {
    $cnt++;
    $savesrc .= $_;
    foreach $g (keys(%gone)) {
	while(/$g/g) {
	    ($gout) = ($g =~ /([^\\]+)/);
	    print "$basefile.c: No replacement for obsolete $gout in line $cnt\n";
	}
    }
}
close SRC;
print OLDSRC $savesrc;

close OLDSRC;

open (NEWSRC, ">$basefile.c") || die "Cannot open $basefile.c for writing";

$savepos = 0;
foreach $n (sort (keys ( %argtest))) {
    pos($savesrc) = $savepos;
    if($savesrc =~ /$n/gs) {
	$savepos = pos($savesrc);
#	print DBG "a matched $n at '$&'\n";
	@subarg = split(/;/, $argtest{$n});
	foreach $x (@subarg) {
	    ($oldn, $newn, $olda, $newa) = split(/ /, $x);
#	    print DBG "oldn '$oldn' newn '$newn' olda '$olda' newa '$newa'\n";
	    $nkey = "$oldn\_$newn";
	    @olda = split(/,/, $olda);
	    @newa = split(/,/, $newa);
	    $arga = "";
	    $i = 0;
	    foreach $a (@olda) {
#		print DBG "Building '$a'\n";
		if($i) {$arga .= ","}
		if($a eq "n") {
		    $arga .= "\\s*NULL\\s*";
		}
		elsif ($a =~ s/^'(.*)'/$1/) {
		    $fix = $1;
		    $fix =~ s/^-/\[-\]/;
		    $arga .= "\\s*$fix\\s*";
		}
		else {
		    $arga .= "[^\\),]*([\\(][^\\)]*[\\)][^\\),]*)*";
		}
#		print DBG "So far '$arga'\n";
		$i++;
	    }

	    $pata = "($oldn"."\\s*[\\(])(".$arga.")[\\)]";
#	    print DBG "b testing $pata\n";
	    pos($savesrc) = 0;	# search from the start
	    while($savesrc =~ /$pata/gs) {
#		print DBG "b matched $pata at '$&'\n";
		$savepre = $PREMATCH;
		$savepost = $POSTMATCH;
		$arglist = $2;
#		print DBG "$oldn '$arglist'\n";
		$i = 0;
		$pat = "[^\\(\\),]*([\\(][^\\)]*[\\)][^,]*)*[^,]*,";
		while ($arglist =~ /$pat/g) {
		    $p = $POSTMATCH;
		    $ai = $&;
		    $ai =~ s/^(\s*)//;
		    $apre[$i] = $1;
		    $ai =~ s/([\s]*)$//;
		    $apost[$i] = $1;
		    $ai =~ s/,$//;
#		    print DBG "arg[$i] '$ai'\n";
		    $ao[$i++] = $ai;
		    $ai = $p;
		}
		$ai =~ s/^(\s*)//;
		$apre[$i] = $1;
		$ai =~ s/([,\s]*)$//;
		$apost[$i] = $1;
		$ao[$i] = $ai;
#		print DBG "Remaining '$ai'\n";
		$newtext = "$newn(";
#		for ($i=0; $i <= $#ao; $i++) {
#		    print DBG "saved ai[$i] '$apre[$i]' '$ao[$i]' '$apost[$i]'\n";
#		}
		$ok = 1;
#		print DBG "Processing newa '$newa' $#newa\n";
		for ($i=0; $i <= $#newa; $i++) {
		    if($newa[$i] =~ /^\d+$/) {
			$j = $newa[$i] - 1;
			$newtext .= $apre[$i];
			$newtext .= $ao[$j];
			if($i < $#newa) {$newtext .= ","}
			$newtext .= $apost[$i];
		    }
		    elsif($newa[$i] =~ /\[(\d+)\]/) {
			$j = $1 - 1;
			$pre = $PREMATCH;
			$post = $POSTMATCH;
			$x = $newa[$i];
			$newtext .= $apre[$i];
			$newtext .= $pre;
			$newtext .= "(";
			$newtext .= $ao[$j];
			$newtext .= ")";
			$newtext .= $post;
			$newtext .= $apost[$i];
		    }
		    elsif($newa[$i] =~ /[*]/) {
#			print DBG "** Cannot define arg $i '$newa[$i]': edit by hand";
			$ok = 0;
		    }
		    else {
#			print DBG "** Cannot understand arg $i '$newa[$i]': edit by hand";
			$ok = 0;
			next;
		    }
#		    print DBG "+ newa[$i] '$newa[$i]' '$newtext'\n";
		}
#		print DBG "ok:$ok newtext '$newtext'\n";
		if($ok) {
		    $savesrc = $savepre . $newtext . ")" . $savepost;
		    pos($savesrc) = length($savepre) + length($newtext);
		    $subdone{$nkey}++;
		}
	    }
	}
    }
#	if(!defined($notedone{$n})) {
#	    ($nout) = ($n =~ /([^\\]+)/);
#	    print "$basefile.c: Replace $nout with $note{$n}\n";
#	}
}

foreach $p (sort (keys ( %pat))) {
    $repcnt=0;
    while($savesrc =~ /$p/g) {$repcnt++}
    if($repcnt) {
	$savesrc =~ s/$p/$pat{$p}/g;
	($pa) = ($p =~ /(^[^\\]+)/);
	($pb) = ($pat{$p} =~ /(^[^\(]+)/);
	print "$basefile.c: Rename ($repcnt times) $pa to $pb\n";
	$new+=$repcnt;
    }
}

print NEWSRC $savesrc;

close NEWSRC;

foreach $n (sort (keys ( %subdone))) {
    ($na,$nb) = ($n =~ /([^_]+)_([^_]+)/);
    $new+=$subdone{$n};
    print "$basefile.c: Replace ($subdone{$n} times) $na with $nb\n";
}
print "$new lines replaced\n";
#close DBG;