gsch2pcb: Make --m4-file and -m4-pcbdir arguments work again.
[geda-gaf/peter-b.git] / utils / scripts / gsymfix.pl
blobc09a59ae102d3c4e3b7c11a36192e1a7d49e245d
1 #!/usr/bin/perl -w
3 my $version = "0.8";
5 # This script reads a geda symbol and does the following:
6 # (...see the usage routine below...)
8 # for parsing input options
9 use Getopt::Long;
11 # don't allow -he to be interpreted as --help
12 $Getopt::Long::autoabbrev=0;
14 &GetOptions(("help" => \&usage,
15 "verbose" => \$verbose,
16 "vverbose" => \$vverbose,
17 "version" => \&version
18 ));
20 usage() if $Getopt::Long::error;
21 usage() unless @ARGV;
23 my $found_pinnumber_attr = 0;
24 my $found_pinseq_attr = 0;
25 my $found_pintype_attr = 0;
26 my $found_pinlabel_attr = 0;
27 my $found_numslots_attr = 0;
28 my $found_device_attr = 0;
29 my $found_footprint_attr = 0;
30 my $seqcnt = 0;
31 my $skip_line_out = 0;
32 my $file_line = 0;
34 my $st_scan = 0;
35 my $st_pin_start = 1;
36 my $st_pin_body = 2;
38 my $state = $st_scan; # intial state machine state
40 if($vverbose) { $verbose = 1; }
42 # make sure the input schematic exists and we can read it
43 my $fname=shift(@ARGV);
44 print "\nProcessing symbol file $fname\n";
45 open(NETLIST,"$fname") or die "Can't open $fname: $!\n";
47 # open output netlist
48 $outfname="$fname.fix";
49 open(OUTNET,">$outfname") or die "Can't open $outfname: $!\n";
51 # parse netlist
52 while($line = <NETLIST>) {
53 $file_line++;
54 #==========================
55 if( $state == $st_scan ) {
56 if( $line =~ /^P/) {
57 $state = $st_pin_start;
58 if($vverbose){print "Pin start...\n";}
60 elsif( $line =~ /^numslots=/) {
61 $found_numslots_attr = 1;
63 elsif( $line =~ /^device=/) {
64 $found_device_attr = 1;
66 elsif( $line =~ /^footprint=/) {
67 $found_footprint_attr = 1;
69 print OUTNET $line;
71 #==========================
72 elsif( $state == $st_pin_start ) {
73 if( $line =~ /^{/) {
74 $state = $st_pin_body;
75 $found_pinnumber_attr = 0;
76 $found_pinseq_attr = 0;
77 $found_pintype_attr = 0;
78 $found_pinlabel_attr = 0;
79 print OUTNET $line;
81 else {
82 print STDERR "*** ERROR: No pin left bracket found at line $file_line\n";
83 exit(-1);
86 #==========================
87 elsif( $state == $st_pin_body ) {
88 #----------------------
89 if( $line =~ /^pinnumber/) {
90 $found_pinnumber_attr = 1;
91 $pin_num = $line;
92 $pin_num =~ s/\n//;
93 $pin_num =~ s/^pinnumber=//;
94 $pinnumber_attr_line = $prev_line;
95 if($vverbose){print " pinnumber attribute found ($pin_num)\n";}
97 #----------------------
98 elsif( $line =~ /^pinseq/) {
99 $found_pinseq_attr = 1;
100 if($vverbose){print " Pinseq attribute found\n";}
101 $seqcnt++;
102 if($vverbose){print " Renumbering attr pinseq=$seqcnt\n";}
103 print OUTNET "pinseq=$seqcnt\n";
104 $skip_line_out++;
106 #----------------------
107 elsif( $line =~ /^pintype/) {
108 $found_pintype_attr = 1;
109 if($vverbose){print " pintype attribute found\n";}
111 #----------------------
112 elsif( $line =~ /^pinlabel/) {
113 $found_pinlabel_attr = 1;
114 if($vverbose){print " pinlabel attribute found\n";}
116 #----------------------
117 elsif( $line =~ /^}/) {
118 $state = $st_scan;
119 if( $found_pinnumber_attr == 0 ) {
120 print "*** WARNING: no pinum attribute found at line $file_line\n";
122 if( $found_pinseq_attr == 0 ) {
123 $seqcnt++;
124 if($verbose){print " Pin $pin_num: Adding attr pinseq=$seqcnt\n";}
125 print OUTNET $pinnumber_attr_line;
126 print OUTNET "pinseq=$seqcnt\n";
128 if( $found_pintype_attr == 0 ) {
129 if($verbose){print " Pin $pin_num: Adding attr pintype=io\n";}
130 print OUTNET $pinnumber_attr_line;
131 print OUTNET "pintype=io\n";
133 if( $found_pinlabel_attr == 0 ) {
134 if($verbose){print " Pin $pin_num: Adding attr pinlabel=n_a\n";}
135 print OUTNET $pinnumber_attr_line;
136 print OUTNET "pinlabel=n_a\n";
138 if($vverbose){print "Pin end...\n";}
140 if( $skip_line_out ) {
141 $skip_line_out = 0;
142 } else {
143 print OUTNET $line;
145 $prev_line = $line; # Save line for next pass
149 if( $found_numslots_attr == 0 ) {
150 if($verbose){print " Adding attr numslots=0\n";}
151 print OUTNET "T 600 100 9 10 0 0 0 0 1\n";
152 print OUTNET "numslots=0\n";
154 if( $found_device_attr == 0 ) {
155 if($verbose){print " Adding attr device=none\n";}
156 print OUTNET "T 600 100 9 10 0 0 0 0 1\n";
157 print OUTNET "device=none\n";
159 if( $found_footprint_attr == 0 ) {
160 if($verbose){print " Adding attr footprint=unknown\n";}
161 print OUTNET "T 600 100 9 10 0 0 0 0 1\n";
162 print OUTNET "footprint=unknown\n";
165 close(NETLIST);
166 close(OUTNET);
168 print STDERR "Output written to file $outfname\n\n";
169 exit;
172 #######################################################################
174 # Subroutines
176 #######################################################################
178 #---------------------------------
179 # usage()
181 # prints program usage
182 #---------------------------------
184 sub usage {
185 my $pname = $0;
186 $pname =~ s/.*\///g;
188 print "\nUsage:\n\n";
189 print " $pname [option] symbol_file\n";
190 print "\n";
191 print " $pname reads a geda symbol file and does the following:\n";
192 print "\t- Prints a warning if there is no pinnumber attribute on a pin\n";
193 print "\t- Renumbers pinseq attributes on all pins, starting with 1\n";
194 print "\t- Adds a pinseq attribute to a pin if none exists\n";
195 print "\t- Adds a pinlabel=n_a attribute to a pin if none exists\n";
196 print "\t- Adds a pintype=io attribute to a pin if none exists\n";
197 print "\t- Adds a numslots=0 attribute to the symbol if none exists\n";
198 print "\t- Adds a device=XXX attribute to the symbol if none exists\n";
199 print "\t- Adds a footprint=XXX attribute to the symbol if none exists\n";
200 print " The idea here is to fix common cut and paste issues and fix up\n";
201 print " the symbol so that it will pass gsymcheck with no errors or\n";
202 print " warnings.\n";
203 print "\n";
204 print " $pname accepts the following options:\n";
205 print "\n";
206 print " --help Displays this help message.\n";
207 print "\n";
208 print " --verbose Enables verbose output.\n";
209 print "\n";
210 print " --vverbose Enables *very* verbose output.\n";
211 print "\n";
212 print " --version Shows the version of this program.\n";
213 print "\n";
214 print " $pname was written by Mike Skerritt <mike\@acornpacket.com>\n";
215 print "\n\n";
216 exit;
219 #---------------------------------
220 # version()
222 # prints program version
223 #---------------------------------
225 sub version {
226 my $pname = $0;
227 $pname =~ s/.*\///g;
228 print "$pname ($0): Version $version\n";