This file is indexed.

/usr/bin/refdes_renum is in geda-utils 1:1.8.2-5.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#!/usr/bin/perl -w
#
# $Id$
#
# Copyright (C) 2003 Dan McMahill
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02111-1301 USA.


# This script is used to renumber devices in a gschem (part of gEDA)
# schematic.  This program works on single as well as multi-sheet 
# schematics.
#
# Usage:
#    renum.pl file1.sch [file2.sch [file3.sch ...]]
#

# for parsing input options
use Getopt::Long;

# for ceil function
use POSIX;

# don't allow -he to be interpreted as --help
$Getopt::Long::autoabbrev=0;

#  my $clear; # reset all refdes
&GetOptions(("help" => \&usage, 
	     "nocopy" => \$nocopy,
	     "pgskip:100" => \$pgskip,
	     "verbose" => \$verbose,
	     "version" => \&version,
             "clear"   => \$clear,
	     "gentle" => \$gentle,
	     "force" => \$force,
	     ));

usage() if $Getopt::Long::error;


# By default pgskip is empty.  But if the user gives
# the --pgskip option with no value, then assign pgskip
# to 100.  
if (defined($pgskip) && ($pgskip == 0)) {  
    $pgskip = 100; 
}


# This makes --gentle the default behavior.  To 
# force refdes renumbering, use --force.
$gentle = 1;
if ( defined($force) || defined($clear) ) {
    undef($gentle);
}

# Print usage string in event user didn't call out any args
usage() unless @ARGV;

# Make sure the input schematic exists and we can read it.
# Also count number of files to open.
$i=0;
while(@ARGV) {
  $fname[$i]=shift(@ARGV);
  die_sdb(1, "Schematic file $fname[$i] does not exist or can not be read")
    unless -r $fname[$i];
  $i++;
}
$filecnt = $i;  # number of files to open
if($verbose) {print "Found $filecnt files to process\n";}

# To refdes an entire design, we normally just loop through all files
# and process each one in order.  However if --gentle option is set, 
# we need to make two passes through all files.  First to create a hash
# of the highest refdeses used, and second to update unnumbered 
# refdeses.  The hash is used to know where to start the refdes numbering.
#
# It gets more complicated.  If --pgskip is not set, then we only need
# to find the highest refdes overall.  But if --pgskip is set, then 
# we need to know the highest refdes on each *page*.  This is accomplished
# on the first scan by creating the variable $highdev{$i, $pre} when --pgskip
# is defined, but using the variable $highdev{$pre} when it is not.
#
if (defined($gentle)) {  # First scan through design to build hash
    for($i=0; $i < $filecnt; $i++) {   # $i is the file index.
	print "Scanning input file #".($i+1).": $fname[$i]\n";
	open(NETLIST,"$fname[$i]") or die_sdb(1, "Can't open $fname[$i]: $!\n");

	while($line = <NETLIST>) {  # Iterate over lines found in .sch file.
	    unless( $line =~ /^refdes/) {
		next;
	    }
	    # Found refdes line.  Eat the newline.
	    $line =~ s/\n//;

	    # Now extract component prefix and suffix.
	    if($verbose) {print ("Matching $line ....\n");}
	    $line =~ /refdes=([a-zA-Z_]+)(\d+|\??)/i;
	    my $pre = $1;
	    my $suf = $2 ? $2 : "?";
	    if($verbose) {print "Refdes line \"$line\" has pre=$pre suf=$suf\n";}

	    # Now put highest refdes found into $highdev hash.
	    if (defined($pgskip)) {
		if ($suf eq "?") {
		    # Part has no pre-existing refdes.
		    # Skip to next part.
		    next;
		} elsif (!defined($highdev{$i, $pre})) { 
                    # refdes is not cached, but refdes exists on part.  
		    # Create new cache entry
		    if ($suf < $pgskip) {
			# if pgskip=100, on pg 1, and suf = 23, set highdev=123.
			$highdev{$i, $pre} = ($i+1)*$pgskip + $suf;
		    } elsif ( ( ($i+1)*$pgskip < $suf) && 
			      ( ($i+2)*$pgskip > $suf) ) { 
			# if pgskip=100, we're on pg 2, and suf = 204, set highdev=204.
			$highdev{$i, $pre} = $suf;
		    } else {
			# I don't know what to do!
			die_sdb(3, "Encountered refdes incompatable with --pgskip setting.  Refdes=$pre$suf.\n");
		    }
			
		} elsif ($highdev{$i, $pre} < $suf) {
		    # part has refdes and refdes is higher than cached
                    # value.  Store this new value as highest value.
		    $highdev{$i, $pre} = $suf;
		} elsif ($highdev{$i, $pre} >= $suf) {
		    # part has refdes, but refdes is lower than cached
                    # high value.  Leave it alone.
		    next;
		} else {
		    # Should never get here
		    die_sdb(4, "Invalid refdes with --pgskip set!  Exiting....\n");
		}

		if($verbose) {
		    print "On page ".($i+1).", caching highest refdes $pre$highdev{$i, $pre}\n";
		}
	    } else {
		if ($suf eq "?") {
		    # Part has no pre-existing refdes.
		    next;
		} elsif (!defined($highdev{$pre}) ) { 
                    # refdes is not cached, and refdes exists on part.  Create new
                    # cache entry
		    $highdev{$pre} = $suf;
		} elsif ($highdev{$pre} < $suf) {
		    # part has refdes and refdes is higher than cached
                    # value.  Store this new value as highest value.
		    $highdev{$pre} = $suf;
		} elsif ($highdev{$pre} >= $suf) {
		    # part has refdes, but refdes is lower than cached
                    # high value.  Leave it alone.
		    next;
		} else {
		    # Should never get here
		    die_sdb(4, "Invalid refdes!  Exiting....\n");
		}
		if($verbose) {
		    print "Caching highest refdes $pre$highdev{$pre}\n";
		}
	    }
	    
	}  # while($line = <NETLIST>)
	close(NETLIST);
    }  # for($i=0; $i < $filecnt; $i++)
} # if (defined($gentle))


# OK, now we can read through the netlist file again, assign refdeses,
# and write the output file.
for($i=0; $i < $filecnt; $i++) {   # $i is the file index.
    print "Now processing input file #".($i+1).": $fname[$i]\n";
    open(NETLIST,"$fname[$i]") or die_sdb(1, "Can't open $fname[$i]: $!\n");
    
    # open output netlist
    $outfname="$fname[$i].renum";
    open(OUTNET,">$outfname") or die_sdb(2, "Can't open $outfname: $!\n");
    
    # Iterate over lines found in .sch file.
    while($line = <NETLIST>) {  
	unless( $line =~ /^refdes/) {
	    print OUTNET $line;
	    next;
	}

	# Found refdes line.  Eat the newline.
	$line =~ s/\n//;
	
	# Now extract component prefix and suffix.
	if($verbose) {print ("Processing $line ....\n");}
	$line =~ /refdes=([a-zA-Z_]+)(\d+|\??)/i;
	my $pre = $1;
	my $suf = $2 ? $2 : "?";
	if($verbose) {print "Refdes line \"$line\" has pre=$pre suf=$suf\n";}

	# Now finally update refdes
	if ($clear) {
	    # Just overwrite all refdeses upon clear
	    if($verbose) {print ("Clearing refdes=$pre$suf\n");}
	    print OUTNET "refdes=$pre?\n";
	} elsif (defined($pgskip) && defined($gentle)) {
	    # If highdev exists, then start devcnt there, otherwise, start
	    # devcnt at correct value for each page.
	    if (!defined($devcnt{$i, $pre}) && defined($highdev{$i, $pre})) {
		$devcnt{$i, $pre} = $highdev{$i, $pre};
	    } elsif (!defined($devcnt{$i, $pre}) && !defined($highdev{$i, $pre})) {
		$devcnt{$i, $pre} = ($i+1)*$pgskip ;
	    }
	    
	    if ($suf eq "?") {
		$devcnt{$i, $pre}++;
		if ($devcnt{$i, $pre} >= ($i+2)*$pgskip) {
		    print STDERR "ERROR:  You are numbering more than $pgskip elements with\n";
		    print STDERR "prefix $pre on this sheet.  You will need to either\n";
		    print STDERR "reduce this number or specify a larger step with the\n";
		    print STDERR "--pgskip argument.\n";
		    die_sdb(3, "");
		}
		print "Renumbering $line to $pre$devcnt{$i, $pre}\n" if($verbose);
		print OUTNET "refdes=$pre$devcnt{$i, $pre}\n";
	    } else {
		print "Leaving line=$line alone\n" if($verbose);
		print OUTNET "refdes=$pre$suf\n";
	    }

	} elsif (defined($pgskip) && !defined($gentle)) {
	    if (!defined($devcnt{$i, $pre})) {
		$devcnt{$i, $pre} = ($i+1)*$pgskip ;
	    }
	    $devcnt{$i, $pre}++;
	    if ($devcnt{$i, $pre} >= ($i+2)*$pgskip) {
		print STDERR "ERROR:  You are numbering more than $pgskip elements with\n";
		print STDERR "prefix $pre on this sheet.  You will need to either\n";
		print STDERR "reduce this number or specify a larger step with the\n";
		print STDERR "--pgskip argument.\n";
		die_sdb(3, "");
	    }
	    print "Renumbering $line to $pre$devcnt{$i, $pre}\n" if($verbose);
	    print OUTNET "refdes=$pre$devcnt{$i, $pre}\n";

	} elsif (!defined($pgskip) && defined($gentle)) {
	    if (!defined($devcnt{$pre}) && defined($highdev{$pre})) {
		$devcnt{$pre} = $highdev{$pre};
	    } elsif (!defined($devcnt{$pre}) && !defined($highdev{$pre})) {
		$devcnt{$pre} = 0;
	    }
	    if ($suf eq "?") {
		$devcnt{$pre}++;
		print "Renumbering $line to $pre$devcnt{$pre}\n" if($verbose);
		print OUTNET "refdes=$pre$devcnt{$pre}\n";
	    } else {
		print "Leaving line=$line alone\n" if($verbose);
		print OUTNET "refdes=$pre$suf\n";
	    }
	} elsif (!defined($pgskip) && !defined($gentle)) {
	    if (!defined($devcnt{$pre})) {
		$devcnt{$pre} = 0 ;
	    }
	    $devcnt{$pre}++;
	    print "Renumbering $line to $pre$devcnt{$pre}\n" if($verbose);
	    print OUTNET "refdes=$pre$devcnt{$pre}\n";

	} else {
	    die_sdb(4, "Invalid state when trying to update refdes!  Exiting.....\n");
	}
    }  # while($line = <NETLIST>)
    close(NETLIST);
    close(OUTNET);

    
    # make this an option to not overwrite the original
    if( $nocopy ) {
	print "Leaving page #",$i+1," output in $outfname\n";
	}
    else {
	system("mv $outfname $fname[$i]");
    }
}

exit(0);


#######################################################################
#
# Subroutines
#
#######################################################################

#---------------------------------
# usage()
#
# prints program usage
#---------------------------------

sub usage {
  my $pname = $0;
  $pname =~ s/.*\///g;

  print "Usage:\n\n";
  print "\t$pname [--nocopy] [--pgskip [number] ] file1 [file2 [file3 ... ] ]\n";
  print "\t$pname --help\n";
  print "\t$pname --version\n";
  print "\n";
  print "$pname reads a gschem schematic file or files and renumbers all reference\n";
  print "designators.  The reference designators are numbered starting with 1 and the\n";
  print "old schematic file is replaced by the modified schematic file.\n";
  print "\n";
  print "$pname accepts the following options:\n";
  print "\n";
  print "    --help      Displays this help message.\n";
  print "\n";
  print "    --nocopy    If given, this flag leaves the modified files in new files\n";
  print "                whose names are generated by appending a \".renum\" to the\n";
  print "                original file names.  The default is to overwrite the original.\n";
  print "\n";
  print "    --pgskip    When this flag is used, components on the first schematic sheet\n";
  print "                are numbered starting with 101.  On the second sheet, they start\n";
  print "                with 201, etc  Specifying a value gives the step between pages.\n";
  print "                For example --pgskip 10 will start with 11, 21, 31, etc.\n";
  print "\n";
  print "    --gentle    This flag tells refdes_renum to leave any refdeses\n";
  print "                alone if they already have numbers.  Use this option to number\n";
  print "                new components in a schematic which has already been numbered.\n";
  print "                Note that --gentle is set by default!\n";
  print "\n";
  print "    --force     Set this flag to renumber all refdeses, whether they are already\n";
  print "                numbered or not.\n";
  print "\n";
  print "    --verbose   Enables verbose output.\n";
  print "\n";
  print "    --version   Shows the version of this program.\n";
  print "\n\n";
  print "Return codes:\n\n";
  print "\t$pname returns the following codes to the shell upon completion:\n";
  print "\n";
  print "          0   Program ran successfully.\n";
  print "          1   Error opening or reading input file.\n";
  print "          2   Error opening or writing output file.\n";
  print "          3   Too many components for --pgskip setting.\n";
  print "          4   Internal error (program bug encountered).\n";
  print "\n";
  print "Usage examples:\n\n";
  print "\tTo renumber the reference designators in a single page schematic\n";
  print "\tmysch.sch:\n";
  print "\n";
  print "\t\t$pname mysch.sch\n";
  print "\n\n";
  print "\tTo renumber the reference designators in schematic pages pg1.sch,\n";
  print "\tpg2.sch and pg3.sch, with the reference designators on pg1.sch\n";
  print "\tbeginning at 101, the designators on pg2.sch beginning at 201,\n";
  print "\tand the designators on pg3.sch beginning at 301:\n";
  print "\n";
  print "\t\t$pname --pgskip pg1.sch pg2.sch pg3.sch\n";
  print "\n\n";
  print "$pname was written by Dan McMahill <dmcmahill\@netbsd.org>\n";
  print "\n\n";
  exit(0);
}

#---------------------------------
# version()
#
# prints program version
#---------------------------------

sub version {
  open(PROG,"$0") or die_sdb(4, "Could not open \"$0\" to find version\n\n");
  my $pname = $0;
  $pname =~ s/.*\///g;

  while($line = <PROG>) {
    if( $line =~ /^#\s*\$Id.*\$/) {
      @words = split ' ',,$line;
      $version = $words[3];
      $date = $words[4];
      print "$pname ($0):  Version $version, $date\n";
      exit(0);
    }
  }
  die_sdb(4, "Could not determine version of \"$0\"\n\n");
}

#-------------------------------------------
# die_sdb(rc, string)
#
# Local version of die.  Sets return code
# for shell, then calls die(string)
#--------------------------------------------
sub die_sdb {
    my $rc = $_[0];
    my $string = $_[1];
    $! = $rc;
    die($string);
}

# ----------------------------------------------
#
# Change Log
#
# $Log$
# Revision 1.8  2007-04-17 20:19:23  pcjc2
# Merge changes from noscreen branch
#
# Revision 1.2.6.2  2007/04/17 16:19:01  pcjc2
# Sync with trunk
#
# Revision 1.7  2007/04/15 23:20:31  sdb
# Made --gentle the default behavior of refdes_renum.
#
# Revision 1.7  2007/04/15 SDB
# Made --gentle default behavior.  Now user must use --force
# to get old behavior, which renumbers all refdeses whether
# already numbered or not.
#
# Revision 1.6  2007/04/15 00:42:22  sdb
# Added --gentle flag to refdes_renum, which won't renumber refdeses
# which already have a number.
#
# Revision 1.5  4.9.2007 sdb.
# Added --gentle flag, and include logic to handle case
# where user mixes --gentle with --pgskip.
#
# Revision 1.4  2007/02/24 18:43:17  pcjc2
# Merge changes to date from noscreen branch.
#
# Revision 1.2.6.1  2007/02/11 23:59:10  pcjc2
# Sync with trunc
#
# Revision 1.3  2007/02/10 20:46:17  sdb
# Added --clear option to clear refdeses.  (* jcl *)
#
# Revision 1.2  2005/12/21 00:09:56  danmc
# - Fix a bug where when using the --pgskip option, components which were
#   present on page n, but not on pages n+1 through n+j, and present again
#   on page n+j+1 got numbered in a strange way.  For example, J101 on page
#   1, no connectors on page 2, J201 on page 3 instead of J301.  Noted by
#   Stuart Brorson.
#
# - While here allow the user to change the default increment from 100 to whatever
#   they want.
#
# - Also fix a bug where exactly 101 components with the same refdes prefix
#   would cause a duplicate refdes on the next page.
#
# Revision 1.1  2003/02/21 03:21:12  ahvezda
# Added scripts/refdes_renum written by Dan McMahill
#
#
#