Bumped version number and applied patch to cleanup scheme backend.
[geda-gaf/peter-b.git] / utils / scripts / refdes_renum
blob0f23818d58df13d9fe041c99f43f5e9ad3e82cfb
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 renumber devices in a gschem (part of gEDA)
23 # schematic. This program works on single as well as multi-sheet
24 # schematics.
26 # Usage:
27 # renum.pl file1.sch [file2.sch [file3.sch ...]]
30 # for parsing input options
31 use Getopt::Long;
33 # for ceil function
34 use POSIX;
36 # don't allow -he to be interpreted as --help
37 $Getopt::Long::autoabbrev=0;
39 &GetOptions(("help" => \&usage,
40 "nocopy" => \$nocopy,
41 "pgskip:i" => \$pgskip,
42 "verbose" => \$verbose,
43 "version" => \&version
44 ));
46 usage() if $Getopt::Long::error;
48 # set the default increment for sheets
49 unless( $pgskip ) { $pgskip = 100; }
51 usage() unless @ARGV;
55 # make sure the input schematic exists and we can read it
56 $i=0;
57 while(@ARGV) {
58 $fname[$i]=shift(@ARGV);
59 die "Schematic file $fname[$i] does not exist or can not be read"
60 unless -r $fname[$i];
61 $i++;
64 $filecnt = $i;
66 for($i=0; $i < $filecnt; $i++) {
67 print "Processing input file #",$i+1,": $fname[$i]\n";
68 open(NETLIST,"$fname[$i]") or die "Can't open $fname[$i]: $!\n";
70 # open output netlist
71 $outfname="$fname[$i].renum";
72 open(OUTNET,">$outfname") or die "Can't open $outfname: $!\n";
74 while($line = <NETLIST>) {
75 unless( $line =~ /^refdes/) {
76 print OUTNET $line;
77 next;
79 # eat the newline
80 $line =~ s/\n//;
82 # for lines like "refdes=CR37" pick out the "CR" part.
83 # There's probably a more concise way, but the perl book
84 # is at work right now.
85 $pre = $line;
86 $pre =~ s/^refdes=//;
87 $pre =~ s/[0-9\?]*$//;
88 print "Refdes line \"$line\" has pre=\"$pre\"\n" if($verbose);
90 # if we're skipping numbers, then start at 100 for page 1
91 # and we'll jump to 200 for page 2, etc.
92 if( ! $devcnt{$pre} ) { $devcnt{$pre} = $pgskip ? ($i+1)*$pgskip : 0; }
93 $devcnt{$pre}++;
94 print "Renumbering $line to $pre$devcnt{$pre}\n" if($verbose);
95 print OUTNET "refdes=$pre$devcnt{$pre}\n";
97 close(NETLIST);
98 close(OUTNET);
100 # round each element up to the next 100 to help identify what
101 # schematic page a component is on by the refdes.
102 if( $pgskip ) {
103 foreach $dev (keys %devcnt) {
104 my $new_cnt = ($i + 2)*$pgskip;
105 if( $new_cnt < $devcnt{$dev} ) {
106 print STDERR "ERROR: You have more than $pgskip elements with\n";
107 print STDERR "prefix $dev on this sheet. You will need to either\n";
108 print STDERR "reduce this number or specify a larger step with the\n";
109 print STDERR "--pgskip argument.\n";
110 exit( 1 );
112 $devcnt{$dev} = $new_cnt;
113 print "Incremented \"$dev\" counter to $devcnt{$dev}\n" if($verbose);
117 # make this an option to not overwrite the original
118 if( $nocopy ) {
119 print "Leaving page #",$i+1," output in $outfname\n";
121 else {
122 system("mv $outfname $fname[$i]");
126 exit;
129 #######################################################################
131 # Subroutines
133 #######################################################################
135 #---------------------------------
136 # usage()
138 # prints program usage
139 #---------------------------------
141 sub usage {
142 my $pname = $0;
143 $pname =~ s/.*\///g;
145 print "Usage:\n\n";
146 print "\t$pname [--nocopy] [--pgskip [number] ] file1 [file2 [file3 ... ] ]\n";
147 print "\t$pname --help\n";
148 print "\t$pname --version\n";
149 print "\n";
150 print "$pname reads a gschem schematic file or files and renumbers all reference\n";
151 print "designators. The reference designators are numbered starting with 1 and the\n";
152 print "old schematic file is replaced by the modified schematic file.\n";
153 print "\n";
154 print "$pname accepts the following options:\n";
155 print "\n";
156 print " --help Displays this help message.\n";
157 print "\n";
158 print " --nocopy If given, this flag leaves the modified files in new files\n";
159 print " whose names are generated by appending a \".renum\" to the\n";
160 print " original file names. The default is to overwrite the original.\n";
161 print "\n";
162 print " --pgskip When this flag is used, components on the first schematic sheet\n";
163 print " are numbered starting with 101. On the second sheet, they start\n";
164 print " with 201, etc Specifying a value gives the step between pages.\n";
165 print " For example --pgskip 10 will start with 11, 21, 31, etc.\n";
166 print "\n";
167 print " --verbose Enables verbose output.\n";
168 print "\n";
169 print " --version Shows the version of this program.\n";
170 print "\n\n";
171 print "Example:\n\n";
172 print "\t$pname mysch.sch\n";
173 print "\t$pname --pgskip pg1.sch pg2.sch pg3.sch\n";
174 print "\n\n";
175 print "$pname was written by Dan McMahill <dmcmahill\@netbsd.org>\n";
176 print "\n\n";
177 exit;
180 #---------------------------------
181 # version()
183 # prints program version
184 #---------------------------------
186 sub version {
187 open(PROG,"$0") or die "Could not open \"$0\" to find version\n\n";
188 my $pname = $0;
189 $pname =~ s/.*\///g;
191 while($line = <PROG>) {
192 if( $line =~ /^#\s*\$Id.*\$/) {
193 @words = split ' ',,$line;
194 $version = $words[3];
195 $date = $words[4];
196 print "$pname ($0): Version $version, $date\n";
197 exit;
200 print "Could not determine version of \"$0\"\n\n";
201 exit;
204 # ----------------------------------------------
206 # Change Log
208 # $Log$
209 # Revision 1.2 2005-12-21 00:09:56 danmc
210 # - Fix a bug where when using the --pgskip option, components which were
211 # present on page n, but not on pages n+1 through n+j, and present again
212 # on page n+j+1 got numbered in a strange way. For example, J101 on page
213 # 1, no connectors on page 2, J201 on page 3 instead of J301. Noted by
214 # Stuart Brorson.
216 # - While here allow the user to change the default increment from 100 to whatever
217 # they want.
219 # - Also fix a bug where exactly 101 components with the same refdes prefix
220 # would cause a duplicate refdes on the next page.
222 # Revision 1.1 2003/02/21 03:21:12 ahvezda
223 # Added scripts/refdes_renum written by Dan McMahill