gschem: Remove postscript-font-scale option.
[geda-gaf/whiteaudio.git] / utils / scripts / pcb_backannotate
blob31018bb6721e82a078f68f3382ee4aa941617291
1 #!/usr/bin/perl -w
3 # Copyright (C) 2003, 2006, 2010 Dan McMahill
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 # This script is used to process annotation files from the PCB
21 # program (http://pcb.gpleda.org) to backannotate
22 # changes to gEDA schematics.
24 # It is heavily based on the pads_backannotate program which is also
25 # part of gEDA
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 # make the options be case sensitive
37 $Getopt::Long::ignorecase=0;
39 &GetOptions( ('h|help' => \&usage,
40 'n|nocopy' => \$nocopy,
41 'v|verbose' => \$verbose,
42 'V|version' => \&version
43 ) );
45 usage() if $Getopt::Long::error;
46 usage() unless @ARGV;
48 # Annotation file name
49 $eco = shift( @ARGV );
51 # if no schematic names follow, exit
52 usage() unless @ARGV;
54 # make sure the input netlist exists and we can open it
55 $i = 0;
56 while( @ARGV ) {
57 $fname[$i] = shift( @ARGV );
58 die "Schematic file $fname[$i] does not exist or can not be read"
59 unless -r $fname[$i];
60 $i++;
63 $filecnt = $i;
66 if( $verbose ){ print "Loading PCB annotation file file: $eco\n"; }
67 my $eco_state="DEFAULT";
68 open( ECO, "$eco" ) or die "Can't open PCB annotation file $eco !\n";
70 my $ver = 0;
71 while( $line = <ECO> ) {
72 if( $line =~ /^\*FILEVERSION\* / ) {
73 $ver = $line;
74 $ver =~ s/^\*FILEVERSION\*\s*//;
75 $ver =~ s/[^0-9]*$//;
76 } elsif( $line =~ /^\*VERSION\* 20060814/ ) {
77 $ver = 20060814;
80 close( ECO );
82 if( $ver == 0) {
83 print "ERROR: Unable to determine annotation file version.\n";
84 exit( 1 );
87 print "Annotation file version is $ver\n" if $verbose ;
88 if( $ver != 20061031 && $ver != 20060814 ) {
89 print "ERROR: This version of the program is unable to process\n";
90 print " PCB annotation files of version $ver.\n";
91 exit( 1 );
95 # reopen and parse the file this time
96 open( ECO, "$eco" ) or die "Can't open PCB annotation file $eco !\n";
97 while( $line = <ECO> ) {
98 # if( $verbose ){ print "$line\n"; }
99 if( $line =~ /^\*COMMENT\*/ ) {
100 print "Skipping comment line: $line\n";
101 next;
102 } elsif( $line =~ /^\*VERSION\* 20060814/ || $line =~ /^\*FILEVERSION\* [0-9]*/) {
103 # the version line. Note that the very first version of the annotation
104 # file used *VERSION* but it was quickly changed to FILEVERSION to avoid
105 # confusing it with the program version.
106 next;
107 } elsif( $line =~ /^\*RENAME\*/ ) {
108 # rename refdes in design (forward/backward annotation)
109 #$eco_state="RENAME";
110 print " *RENAME* (Refdes Renumber)\n" if $verbose;
111 $line =~ s/^\*RENAME\*\s*//;
112 parseRENAME($line);
113 next;
114 } elsif( $line =~ /^\*WARN\*/ ) {
115 # A warning generated by PCB
116 print "Found the following warning in the annotation file:\n";
117 print "$line\n";
118 next;
119 } elsif( $line =~ /^\*/ ) {
120 print "WARNING: Unknown command line:\n";
121 print " $line\n";
122 $eco_state="DEFAULT";
123 next;
124 } else {
125 # this must be a data line
126 #if( $verbose ){ print "Processing data line: $line"; }
130 close( ECO );
132 for($i=0; $i < $filecnt; $i++) {
133 print "Processing schematic file #", $i+1, ": $fname[$i]\n";
134 open(NETLIST,"$fname[$i]") or
135 die "Can't open schematic $fname[$i]: $!\n";
137 # open output netlist
138 $outfname="$fname[$i].new";
139 open(OUTSCH,">$outfname") or die "Can't open $outfname: $!\n";
141 while($line = <NETLIST>) {
142 $line = executeRENAME($line);
143 print OUTSCH "$line";
145 close(NETLIST);
146 close(OUTSCH);
148 if( $nocopy ) {
149 print "Leaving page #",$i+1," output in $outfname\n";
151 else {
152 system("mv $outfname $fname[$i]");
157 print "\n---- Gate Swapping ----\n";
158 executeSWPGATES();
159 print "\n---- Pin Swapping ----\n";
160 executeSWPPINS();
161 print "\nBackannotation finished ", scalar localtime, "\n";
163 exit;
166 #######################################################################
168 # Subroutines
170 #######################################################################
172 #---------------------------------
173 # executeRENAME(line)
174 #---------------------------------
176 sub executeRENAME {
177 my $line = shift(@_);
179 return $line unless defined %cmd_rename;
180 return $line unless $line =~ /^refdes=/;
182 # pick out the reference designator
183 $refdes = $line;
184 $refdes =~ s/^refdes=//;
185 $refdes =~ s/[\r\n]*$//;
187 # see if its listed in our hash of reference designators to be
188 # renamed
189 return $line unless exists $cmd_rename{$refdes};
191 print "executeRENAME(): Renaming $refdes to $cmd_rename{$refdes}\n"
192 if $verbose;
193 return "refdes=$cmd_rename{$refdes}\n";
197 #---------------------------------
198 # executeSWPGATES()
199 #---------------------------------
201 sub executeSWPGATES {
202 my $key;
204 foreach $key (keys %cmd_swap_gates ) {
205 print "Please manually swap gates $key and $cmd_swap_gates{$key}\n";
209 #---------------------------------
210 # executeSWPPINS()
211 #---------------------------------
213 sub executeSWPPINS {
214 my $key;
215 my @pins;
217 foreach $key (keys %cmd_swap_pins ) {
218 @pins = split '\.',,$cmd_swap_pins{$key};
219 print "Please manually swap pins $pins[0] and $pins[1] on $key\n";
223 #---------------------------------
224 # parseRENAME(line)
225 #---------------------------------
227 sub parseRENAME {
228 my $line = shift(@_);
229 my @refs;
230 @refs = split ' ',,$line;
232 $refs[0] =~ s/"//g; # "
233 $refs[1] =~ s/"//g; # "
235 print "parseRENAME(): Scheduling rename of $refs[0] to $refs[1]\n"
236 if $verbose;
237 $cmd_rename{$refs[0]} = $refs[1];
241 #---------------------------------
242 # parseSWPGATES(line)
243 #---------------------------------
245 sub parseSWPGATES {
246 my $line = shift(@_);
247 my @refs;
248 @refs = split ' ',,$line;
250 print "parseSWPGATES(): Scheduling swap of gate $refs[0] with $refs[1]\n"
251 if $verbose;
252 $cmd_swap_gates{$refs[0]} = $refs[1];
256 #---------------------------------
257 # parseSWPPINS(line)
258 #---------------------------------
260 sub parseSWPPINS {
261 my $line = shift(@_);
262 @refs = split ' ',,$line;
263 @pins = split '\.',,$refs[1];
265 print "parseSWPPINS(): Scheduling swap of pins on $refs[0] : pins ",
266 "$pins[0] and $pins[1]\n" if $verbose;
267 $cmd_swap_pins{$refs[0]} = $refs[1];
271 #---------------------------------
272 # usage()
274 # prints program usage
275 #---------------------------------
277 sub usage {
278 my $pname = $0;
279 $pname =~ s/.*\///g;
281 print <<EOF;
283 Usage:
285 $pname [-n|--nocopy] [-v|--verbose] change.log file1.sch [file2.sch ...]
286 $pname -h|--help\n
287 $pname -V|--version\n
289 $pname reads a PCB annotation file and backannotates changes\n
290 to a gEDA schematic.
292 $pname accepts the following options:
295 --help Displays this help message.
298 --nocopy If given, this flag leaves the modified files in new files
299 whose names are generated by appending a \".new\" to the
300 original file names. The default is to overwrite the original.
303 --verbose Enables verbose output.
306 --version Shows the version of this program.
309 $pname was written by Dan McMahill <dmcmahill\@netbsd.org>
314 exit;
317 #---------------------------------
318 # version()
320 # prints program version
321 #---------------------------------
323 sub version {
324 open(PROG,"$0") or die "Could not open \"$0\" to find version\n\n";
325 my $pname = $0;
326 $pname =~ s/.*\///g;
328 while($line = <PROG>) {
329 if( $line =~ /^#\s*\$Id.*\$/) {
330 @words = split ' ',,$line;
331 $version = $words[3];
332 $date = $words[4];
333 print "$pname ($0): Version $version, $date\n";
334 exit;
337 print "Could not determine version of \"$0\"\n\n";
338 exit;