gsch2pcb: Make --m4-file and -m4-pcbdir arguments work again.
[geda-gaf/peter-b.git] / utils / scripts / pcb_backannotate
blobeac08d48a66c1fac7135fb97e25f3a69416aa21f
1 #!/usr/bin/perl -w
3 # $Id$
5 # Copyright (C) 2003, 2006 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 annotation files from the PCB
23 # program (http://pcb.gpleda.org) to backannotate
24 # changes to gEDA schematics.
26 # It is heavily based on the pads_backannotate program which is also
27 # part of gEDA
29 # for parsing input options
30 use Getopt::Long;
32 # for ceil function
33 use POSIX;
35 # don't allow -he to be interpreted as --help
36 $Getopt::Long::autoabbrev=0;
38 &GetOptions( ("help" => \&usage,
39 "nocopy" => \$nocopy,
40 "verbose" => \$verbose,
41 "version" => \&version
42 ) );
44 usage() if $Getopt::Long::error;
45 usage() unless @ARGV;
47 # Annotation file name
48 $eco = shift( @ARGV );
50 # if no schematic names follow, exit
51 usage() unless @ARGV;
53 # make sure the input netlist exists and we can open it
54 $i = 0;
55 while( @ARGV ) {
56 $fname[$i] = shift( @ARGV );
57 die "Schematic file $fname[$i] does not exist or can not be read"
58 unless -r $fname[$i];
59 $i++;
62 $filecnt = $i;
65 if( $verbose ){ print "Loading PCB annotation file file: $eco\n"; }
66 #$eco_state="DEFAULT";
67 open( ECO, "$eco" ) or die "Can't open PCB annotation file $eco !\n";
69 my $ver = 0;
70 while( $line = <ECO> ) {
71 if( $line =~ /^\*FILEVERSION\* / ) {
72 $ver = $line;
73 $ver =~ s/^\*FILEVERSION\*\s*//;
74 $ver =~ s/[^0-9]*$//;
75 } elsif( $line =~ /^\*VERSION\* 20060814/ ) {
76 $ver = 20060814;
79 close( ECO );
81 if( $ver == 0) {
82 print "ERROR: Unable to determine annotation file version.\n";
83 exit( 1 );
86 print "Annotation file version is $ver\n" if $verbose ;
87 if( $ver != 20061031 && $ver != 20060814 ) {
88 print "ERROR: This version of the program is unable to process\n";
89 print " PCB annotation files of version $ver.\n";
90 exit( 1 );
94 # reopen and parse the file this time
95 open( ECO, "$eco" ) or die "Can't open PCB annotation file $eco !\n";
96 while( $line = <ECO> ) {
97 # if( $verbose ){ print "$line\n"; }
98 if( $line =~ /^\*COMMENT\*/ ) {
99 print "Skipping comment line: $line\n";
100 next;
101 } elsif( $line =~ /^\*VERSION\* 20060814/ || $line =~ /^\*FILEVERSION\* [0-9]*/) {
102 # the version line. Note that the very first version of the annotation
103 # file used *VERSION* but it was quickly changed to FILEVERSION to avoid
104 # confusing it with the program version.
105 next;
106 } elsif( $line =~ /^\*RENAME\*/ ) {
107 # rename refdes in design (forward/backward annotation)
108 #$eco_state="RENAME";
109 print " *RENAME* (Refdes Renumber)\n" if $verbose;
110 $line =~ s/^\*RENAME\*\s*//;
111 parseRENAME($line);
112 next;
113 } elsif( $line =~ /^\*WARN\*/ ) {
114 # A warning generated by PCB
115 print "Found the following warning in the annotation file:\n";
116 print "$line\n";
117 next;
118 } elsif( $line =~ /^\*/ ) {
119 print "WARNING: Unknown command line:\n";
120 print " $line\n";
121 $eco_state="DEFAULT";
122 next;
123 } else {
124 # this must be a data line
125 #if( $verbose ){ print "Processing data line: $line"; }
129 close( ECO );
131 for($i=0; $i < $filecnt; $i++) {
132 print "Processing schematic file #", $i+1, ": $fname[$i]\n";
133 open(NETLIST,"$fname[$i]") or
134 die "Can't open schematic $fname[$i]: $!\n";
136 # open output netlist
137 $outfname="$fname[$i].new";
138 open(OUTSCH,">$outfname") or die "Can't open $outfname: $!\n";
140 while($line = <NETLIST>) {
141 $line = executeRENAME($line);
142 print OUTSCH "$line";
144 close(NETLIST);
145 close(OUTSCH);
147 if( $nocopy ) {
148 print "Leaving page #",$i+1," output in $outfname\n";
150 else {
151 system("mv $outfname $fname[$i]");
156 print "\n---- Gate Swapping ----\n";
157 executeSWPGATES();
158 print "\n---- Pin Swapping ----\n";
159 executeSWPPINS();
160 print "\nBackannotation finished ", scalar localtime, "\n";
162 exit;
165 #######################################################################
167 # Subroutines
169 #######################################################################
171 #---------------------------------
172 # executeRENAME(line)
173 #---------------------------------
175 sub executeRENAME {
176 my $line = shift(@_);
178 return $line unless defined %cmd_rename;
179 return $line unless $line =~ /^refdes=/;
181 # pick out the reference designator
182 $refdes = $line;
183 $refdes =~ s/^refdes=//;
184 $refdes =~ s/[\r\n]*$//;
186 # see if its listed in our hash of reference designators to be
187 # renamed
188 return $line unless exists $cmd_rename{$refdes};
190 print "executeRENAME(): Renaming $refdes to $cmd_rename{$refdes}\n"
191 if $verbose;
192 return "refdes=$cmd_rename{$refdes}\n";
196 #---------------------------------
197 # executeSWPGATES()
198 #---------------------------------
200 sub executeSWPGATES {
201 my $key;
203 foreach $key (keys %cmd_swap_gates ) {
204 print "Please manually swap gates $key and $cmd_swap_gates{$key}\n";
208 #---------------------------------
209 # executeSWPPINS()
210 #---------------------------------
212 sub executeSWPPINS {
213 my $key;
214 my @pins;
216 foreach $key (keys %cmd_swap_pins ) {
217 @pins = split '\.',,$cmd_swap_pins{$key};
218 print "Please manually swap pins $pins[0] and $pins[1] on $key\n";
222 #---------------------------------
223 # parseRENAME(line)
224 #---------------------------------
226 sub parseRENAME {
227 my $line = shift(@_);
228 my @refs;
229 @refs = split ' ',,$line;
231 $refs[0] =~ s/"//g; # "
232 $refs[1] =~ s/"//g; # "
234 print "parseRENAME(): Scheduling rename of $refs[0] to $refs[1]\n"
235 if $verbose;
236 $cmd_rename{$refs[0]} = $refs[1];
240 #---------------------------------
241 # parseSWPGATES(line)
242 #---------------------------------
244 sub parseSWPGATES {
245 my $line = shift(@_);
246 my @refs;
247 @refs = split ' ',,$line;
249 print "parseSWPGATES(): Scheduling swap of gate $refs[0] with $refs[1]\n"
250 if $verbose;
251 $cmd_swap_gates{$refs[0]} = $refs[1];
255 #---------------------------------
256 # parseSWPPINS(line)
257 #---------------------------------
259 sub parseSWPPINS {
260 my $line = shift(@_);
261 @refs = split ' ',,$line;
262 @pins = split '\.',,$refs[1];
264 print "parseSWPPINS(): Scheduling swap of pins on $refs[0] : pins ",
265 "$pins[0] and $pins[1]\n" if $verbose;
266 $cmd_swap_pins{$refs[0]} = $refs[1];
270 #---------------------------------
271 # usage()
273 # prints program usage
274 #---------------------------------
276 sub usage {
277 my $pname = $0;
278 $pname =~ s/.*\///g;
280 print <<EOF;
282 Usage:
284 $pname [--nocopy] [--verbose] change.log file1.sch [file2.sch ...]
285 $pname --help\n
286 $pname --version\n
288 $pname reads a PCB annotation file and backannotates changes\n
289 to a gEDA schematic.
291 $pname accepts the following options:
293 --help Displays this help message.
295 --nocopy If given, this flag leaves the modified files in new files
296 whose names are generated by appending a \".new\" to the
297 original file names. The default is to overwrite the original.
299 --verbose Enables verbose output.
301 --version Shows the version of this program.
304 $pname was written by Dan McMahill <dmcmahill\@netbsd.org>
309 exit;
312 #---------------------------------
313 # version()
315 # prints program version
316 #---------------------------------
318 sub version {
319 open(PROG,"$0") or die "Could not open \"$0\" to find version\n\n";
320 my $pname = $0;
321 $pname =~ s/.*\///g;
323 while($line = <PROG>) {
324 if( $line =~ /^#\s*\$Id.*\$/) {
325 @words = split ' ',,$line;
326 $version = $words[3];
327 $date = $words[4];
328 print "$pname ($0): Version $version, $date\n";
329 exit;
332 print "Could not determine version of \"$0\"\n\n";
333 exit;