gsch2pcb: Make --m4-file and -m4-pcbdir arguments work again.
[geda-gaf/peter-b.git] / utils / scripts / pads_backannotate
blob84fe2be4993b5c5a7726a8b9614e080271a8ed41
1 #!/usr/bin/perl -w
3 # $Id$
5 # Copyright (C) 2003 Dan McMahill
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 # This script is used to process PADS PowerPCB .eco files to backannotate
23 # changes to gEDA schematics. Not all ECO file sections are implemented,
24 # and this program is relatively untested. Please proceed with caution.
27 # for parsing input options
28 use Getopt::Long;
30 # for ceil function
31 use POSIX;
33 # don't allow -he to be interpreted as --help
34 $Getopt::Long::autoabbrev=0;
36 &GetOptions( ("help" => \&usage,
37 "nocopy" => \$nocopy,
38 "verbose" => \$verbose,
39 "version" => \&version
40 ) );
42 usage() if $Getopt::Long::error;
43 usage() unless @ARGV;
45 # ECO file name
46 $eco = shift( @ARGV );
48 # if no schematic names follow, exit
49 usage() unless @ARGV;
51 # make sure the input netlist exists and we can open it
52 $i = 0;
53 while( @ARGV ) {
54 $fname[$i] = shift( @ARGV );
55 die "Schematic file $fname[$i] does not exist or can not be read"
56 unless -r $fname[$i];
57 $i++;
60 $filecnt = $i;
63 if( $verbose ){ print "Loading ECO file: $eco\n"; }
64 $eco_state="DEFAULT";
65 $got_end=0;
66 open( ECO, "$eco" ) or die "Can't open ECO file $eco !\n";
68 $line = <ECO>;
69 if( $line =~ /^\*PADS-ECO-V3\.0\*[\n\r]*$/ ) {
70 print "Read ECO header line\n" if $verbose ;
71 } elsif( $line =~ /^\*PADS-ECO-\*/ ) {
72 print "Unknown ECO file version: $line\n";
73 exit 1;
74 } else {
75 print "Invalid ECO file header: $line\n";
76 exit 1;
79 while( $line = <ECO> ) {
80 # if( $verbose ){ print "$line\n"; }
81 if( $line =~ /^\*CHGPART\*/ ) {
82 # change type of part (forward annotation)
83 $eco_state="CHGPART";
84 next;
85 } elsif( $line =~ /^\*DEL_ATTRIBUTE\*/ ) {
86 # delete object attribute (forward annotation)
87 $eco_state="DEL_ATTRIBUTE";
88 next;
89 } elsif( $line =~ /^\*DELPART\*/ ) {
90 # delete part from the design (forward annotation)
91 $eco_state="DELPART";
92 next;
93 } elsif( $line =~ /^\*DELPIN\*/ ) {
94 # delete pins from net (forward annotation)
95 $eco_state="DELPIN";
96 next;
97 } elsif( $line =~ /^\*END\*/ ) {
98 # end of ECO file
99 $eco_state="END";
100 $got_end=1;
101 next;
102 } elsif( $line =~ /^\*JOINNET\*/ ) {
103 # join two nets together (forward annotation)
104 $eco_state="JOINNET";
105 next;
106 } elsif( $line =~ /^\*NET\*/ ) {
107 # add pin to net (forward annotation)
108 $eco_state="NET";
109 next;
110 } elsif( $line =~ /^\*PART\*/ ) {
111 # add part to the design (forward annotation)
112 $eco_state="PART";
113 next;
114 } elsif( $line =~ /^\*REMARK\*/ ) {
115 # don't change state on comment lines
116 print "Skipping *REMARK* line\n" if $verbose;
117 next;
118 } elsif( $line =~ /^\*RENNET\*/ ) {
119 # rename netname (forward annotation)
120 $eco_state="RENNET";
121 next;
122 } elsif( $line =~ /^\*RENPART\*/ ) {
123 # rename part in design (forward/backward annotation)
124 $eco_state="RENPART";
125 print "Starting *RENPART* (Refdes Renumber) section\n" if $verbose;
126 next;
127 } elsif( $line =~ /^\*SET_ATTRIBUTE\*/ ) {
128 # set object attribute (forward annotation)
129 $eco_state="SET_ATTRIBUTE";
130 next;
131 } elsif( $line =~ /^\*SIGNAL\*/ ) {
132 # part of *NET* and also *SPLITNET* (forward annotation)
133 $eco_state="SIGNAL";
134 next;
135 } elsif( $line =~ /^\*SPLITNET\*/ ) {
136 # split net into two new nets (forward annotation)
137 $eco_state="SPLITNET";
138 next;
139 } elsif( $line =~ /^\*SWPGATES\*/ ) {
140 $eco_state="SWPGATES";
141 print "Starting *SWPGATES* (Gate Swapping) section\n" if $verbose;
142 next;
143 } elsif( $line =~ /^\*SWPPINS\*/ ) {
144 $eco_state="SWPPINS";
145 print "Starting *SWPPINS* (Pin Swapping) section\n" if $verbose;
146 next;
147 } elsif( $line =~ /^\*/ ) {
148 print "WARNING: Unknown command line:\n";
149 print " $line\n";
150 $eco_state="DEFAULT";
151 next;
152 } else {
153 # this must be a data line
154 #if( $verbose ){ print "Processing data line: $line"; }
157 if( $eco_state =~ "RENPART" ) {
158 parseRENPART($line);
159 } elsif( $eco_state =~ "SWPGATES" ) {
160 parseSWPGATES($line);
161 } elsif( $eco_state =~ "SWPPINS" ) {
162 parseSWPPINS($line);
163 } else {
164 print "Skipping lines in $eco_state section\n" if $verbose;
168 close( ECO );
170 for($i=0; $i < $filecnt; $i++) {
171 print "Processing schematic file #", $i+1, ": $fname[$i]\n";
172 open(NETLIST,"$fname[$i]") or
173 die "Can't open schematic $fname[$i]: $!\n";
175 # open output netlist
176 $outfname="$fname[$i].new";
177 open(OUTSCH,">$outfname") or die "Can't open $outfname: $!\n";
179 while($line = <NETLIST>) {
180 $line = executeRENPART($line);
181 print OUTSCH "$line";
183 close(NETLIST);
184 close(OUTSCH);
186 if( $nocopy ) {
187 print "Leaving page #",$i+1," output in $outfname\n";
189 else {
190 system("mv $outfname $fname[$i]");
195 print "\n---- Gate Swapping ----\n";
196 executeSWPGATES();
197 print "\n---- Pin Swapping ----\n";
198 executeSWPPINS();
199 print "\nBackannotation finished ", scalar localtime, "\n";
201 exit;
204 #######################################################################
206 # Subroutines
208 #######################################################################
210 #---------------------------------
211 # executeRENPART(line)
212 #---------------------------------
214 sub executeRENPART {
215 my $line = shift(@_);
217 return $line unless defined %cmd_rename;
218 return $line unless $line =~ /^refdes=/;
220 # pick out the reference designator
221 $refdes = $line;
222 $refdes =~ s/^refdes=//;
223 $refdes =~ s/[\r\n]*$//;
225 # see if its listed in our hash of reference designators to be
226 # renamed
227 return $line unless exists $cmd_rename{$refdes};
229 print "executeRENPART(): Renaming $refdes to $cmd_rename{$refdes}\n"
230 if $verbose;
231 return "refdes=$cmd_rename{$refdes}\n";
235 #---------------------------------
236 # executeSWPGATES()
237 #---------------------------------
239 sub executeSWPGATES {
240 my $key;
242 foreach $key (keys %cmd_swap_gates ) {
243 print "Please manually swap gates $key and $cmd_swap_gates{$key}\n";
247 #---------------------------------
248 # executeSWPPINS()
249 #---------------------------------
251 sub executeSWPPINS {
252 my $key;
253 my @pins;
255 foreach $key (keys %cmd_swap_pins ) {
256 @pins = split '\.',,$cmd_swap_pins{$key};
257 print "Please manually swap pins $pins[0] and $pins[1] on $key\n";
261 #---------------------------------
262 # parseRENPART(line)
263 #---------------------------------
265 sub parseRENPART {
266 my $line = shift(@_);
267 my @refs;
268 @refs = split ' ',,$line;
270 print "parseRENPART(): Scheduling rename of $refs[0] to $refs[1]\n"
271 if $verbose;
272 $cmd_rename{$refs[0]} = $refs[1];
276 #---------------------------------
277 # parseSWPGATES(line)
278 #---------------------------------
280 sub parseSWPGATES {
281 my $line = shift(@_);
282 my @refs;
283 @refs = split ' ',,$line;
285 print "parseSWPGATES(): Scheduling swap of gate $refs[0] with $refs[1]\n"
286 if $verbose;
287 $cmd_swap_gates{$refs[0]} = $refs[1];
291 #---------------------------------
292 # parseSWPPINS(line)
293 #---------------------------------
295 sub parseSWPPINS {
296 my $line = shift(@_);
297 @refs = split ' ',,$line;
298 @pins = split '\.',,$refs[1];
300 print "parseSWPPINS(): Scheduling swap of pins on $refs[0] : pins ",
301 "$pins[0] and $pins[1]\n" if $verbose;
302 $cmd_swap_pins{$refs[0]} = $refs[1];
306 #---------------------------------
307 # usage()
309 # prints program usage
310 #---------------------------------
312 sub usage {
313 my $pname = $0;
314 $pname =~ s/.*\///g;
316 print "Usage:\n\n";
317 print "\t$pname [--nocopy] [--verbose] change.eco file1.sch [file2.sch [file3.sch ... ] ]\n";
318 print "\t$pname --help\n";
319 print "\t$pname --version\n";
320 print "\n";
321 print "$pname reads a PADS PowerPCB ECO file and backannotates changes\n";
322 print "to a gEDA schematic.\n";
323 print "\n";
324 print "$pname accepts the following options:\n";
325 print "\n";
326 print " --help Displays this help message.\n";
327 print "\n";
328 print " --nocopy If given, this flag leaves the modified files in new files\n";
329 print " whose names are generated by appending a \".new\" to the\n";
330 print " original file names. The default is to overwrite the original.\n";
331 print "\n";
332 print " --verbose Enables verbose output.\n";
333 print "\n";
334 print " --version Shows the version of this program.\n";
335 print "\n\n";
336 print "$pname was written by Dan McMahill <dmcmahill\@netbsd.org>\n";
337 print "\n\n";
338 exit;
341 #---------------------------------
342 # version()
344 # prints program version
345 #---------------------------------
347 sub version {
348 open(PROG,"$0") or die "Could not open \"$0\" to find version\n\n";
349 my $pname = $0;
350 $pname =~ s/.*\///g;
352 while($line = <PROG>) {
353 if( $line =~ /^#\s*\$Id.*\$/) {
354 @words = split ' ',,$line;
355 $version = $words[3];
356 $date = $words[4];
357 print "$pname ($0): Version $version, $date\n";
358 exit;
361 print "Could not determine version of \"$0\"\n\n";
362 exit;
365 # ----------------------------------------------
367 # Change Log
369 # $Log$
370 # Revision 1.1 2003-08-31 21:39:52 ahvezda
371 # Added pads_backannotate perl script
373 # Revision 1.4 2003/07/19 04:34:55 dan
374 # bring this program up to a usable state. Refdes renumbering is fully
375 # supported and pin/gate swapping while not supported, gives a report of
376 # the manual swaps needed.
378 # Revision 1.3 2003/03/03 03:44:26 dan
379 # Finish up code to handle the RENPART .eco directive. The output files
380 # are still not written, but everything else is in place to handle backannotation
381 # of reference designator renumbers.
383 # Revision 1.2 2003/02/21 12:30:30 dan
384 # fix perl path
386 # Revision 1.1 2003/02/19 23:31:50 dan
387 # start (non functional so far) of a pads to gschem backannotation tool