puller.c: Fix trace printf in find_pair() to reflect actual arguments
[geda-pcb/pcjc2.git] / lib / m4lib_to_newlib.sh
blob6ca4b81fce02a0848b980f14f97f5023d6c6c9b6
1 #!/bin/sh
3 # This script is used to extract all elements from an "oldlib" (M4)
4 # style library and place them in individual "newlib" style files.
6 outd=/tmp/newlib
7 contents=pcblib.contents
8 AWK=${AWK:-awk}
9 PCB=${PCB:-pcb}
11 usage() {
12 cat << EOF
13 Usage:
14 $0 [-h | --help]
15 $0 [-v | --version]
16 $0 [-c|--contents contents_file] [-o|--output output_directory] [-p|--png] [-d|--dpi]
18 Extracts all footprints from an m4 library and creates a "newlib" style
19 library.
21 The following options are supported:
23 -a | --awk awk : Specifies the awk implementation to use. Defaults to "${AWK}".
25 -c | --contents file : Specifies the contents file to be use as an input.
26 Default is "${contents}".
28 -d | --dpi : Specifies that the png output should use a fixed pixels
29 per inch scaling instead of a fixed maximum size. This
30 option is useful when comparing the before and after footprints
31 when making footprint library changes.
33 -h | --help : Outputs this message and exits.
35 -o | --output dir : Specifies the directory that the newlib library will be
36 written to. This directory must exist and be empty.
37 Default is "${outd}".
39 -P | --pcb pcb : Specifies the pcb binary to use for creating png previews. Defaults to
40 "${PCB}"
42 -p | --png : Generates png previews for all the footprints.
44 -v | --version : Displays the version of this script and exits.
46 EOF
49 version() {
50 $AWK '/# [\$]Id:.*$/ {sub(/,v/,""); \
51 print $3 " Version "$4", "$5}' $0
54 do_png=0
55 png_flag="--xy-max 200"
57 while test $# -gt 0 ; do
58 case $1 in
59 -a|--awk )
60 AWK="$2"
61 shift 2
64 -c|--contents )
65 contents=$2
66 shift 2
69 -d|--dpi )
70 png_flag="--dpi 1000"
71 shift
74 -h|--help )
75 usage
76 exit 0
79 -o|--output )
80 outd=$2
81 shift 2
84 -P|--pcb )
85 PCB="$2"
86 shift 2
89 -p|--png )
90 do_png=1
91 shift
94 -v|--version )
95 version
96 exit 0
99 -* )
100 echo "ERROR: $0: Unknown option $1"
101 usage
102 exit 1
106 break
108 esac
109 done
112 if test -d ${outd} ; then
113 echo "Output directory ${outd} already exists"
114 exit 1
115 else
116 mkdir -p ${outd}
118 outd_full="`cd $outd && pwd`"
120 $AWK '
122 BEGIN {
123 first = 1;
124 libind = "";
127 # we have to use this trick because variables like outd are not yet defined
128 # in BEGIN.
129 first == 1 {
130 brokenurl = "broken.html";
131 broken = outd "/" brokenurl;
132 print "" > broken;
134 ind = outd "/index.html";
136 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">" > ind;
137 print "<HTML><HEAD><TITLE>PCB Footprint Library</TITLE></HEAD>" >> ind;
138 print "<BODY bgcolor=\"#FFFFFF\" text=\"#000000\">" >> ind;
139 print "<H1>PCB Footprint Library</H1>" >> ind;
140 print "<UL>" >> ind;
141 print "" >> ind;
143 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">" > broken;
144 print "<HTML><BODY>" >> broken;
145 print "<TABLE BORDER=2>" >> broken;
146 print "<TR><TD><EM>Library</EM></TD>" >> broken;
147 print " <TD><EM>Comment</EM></TD>" >> broken;
148 print " <TD><EM>Footprint Name</EM></TD>" >> broken;
149 print " <TD><EM>Broken Command</EM></TD>" >> broken;
150 print "</TR>" >> broken;
151 print "" >> broken;
153 first = 0;
156 # we are starting a new library
157 /^TYPE=/ {
158 finish_libind();
159 lib=$0;
160 gsub(/TYPE=~/, "", lib);
161 txtdir = lib;
162 urldir = lib;
163 gsub(/ /, "%20", urldir);
165 libind = outd "/" lib "/index.html";
166 #gsub(/ /, "\\ ", libind);
168 dir = outd "/" lib ;
169 gsub(/ /,"\\ ", dir);
170 print "Processing library: " lib " and creating " libind;
171 system("mkdir -p " dir);
173 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">" > libind;
174 print "<HTML><HEAD><TITLE>PCB " lib " Footprint Library</TITLE></HEAD>" >> libind;
175 print "<BODY bgcolor=\"#FFFFFF\" text=\"#000000\">" >> libind;
176 print "<H1>PCB " lib " Footprint Library</H1>" >> libind;
178 print "<TABLE BORDER=2>" >> libind;
179 print "<TR>" >> libind;
180 print " <TD><EM>Comment</EM></TD>" >> libind;
181 print " <TD><EM>Footprint Name</EM></TD>" >> libind;
182 print "</TR>" >> libind;
183 print "" >> libind;
185 print "<LI><A HREF=\"" txtdir "/index.html\">~" lib "</A></LI>" >> ind;
187 next;
191 line=$0;
192 split(line, a, "[:]");
194 template = a[1];
195 package = a[2];
196 comp = a[3];
197 comment = a[4];
199 # pick out the name of the footprint
200 match (comment, /(.*)\[(.*)\]/, fp);
201 comp = fp[2];
202 comment = a[3] ", " fp[1];
204 txtcomp = comp;
205 urlcomp = comp;
207 # escape the spaces in for URLs and also filenames
208 gsub(/ /, "%20", urlcomp);
209 gsub(/ /, "\\ ", comp);
212 # extract the footprint
213 # path library template value package
214 templ = a[1];
215 gsub(/ /, "\\ ", templ);
217 pkg = a[2];
218 gsub(/ /, "\\ ", pkg);
221 # skip the QFP builder menu
222 skip = 0;
223 if( templ == "menu_qfp" ) {
224 cmd1 = "Skipping QFP builder menu";
225 rc = 1;
226 } else {
227 cmd1 = "sh " cmd_path "/QueryLibrary.sh . pcblib " templ " " comp " " pkg;
228 cmd = cmd1 " > " dir "/" comp ".fp";
229 rc = system( cmd );
232 if( rc != 0) {
233 printf("<TR><TD>~%s</TD>\n", lib) >> broken;
234 printf(" <TD>%s</TD>\n", comp) >> broken;
235 printf(" <TD>%s</TD>\n", comment) >> broken;
236 printf(" <TD>%s</TD>\n", cmd1) >> broken;
237 printf("</TR>\n") >> broken;
239 # no need to go further with this footprint. It is broken.
240 next;
241 } else {
242 # generate the web index
243 printf(" <TR>\n <TD>%s</TD>\n", comment) >> libind;
244 printf(" <TD><A HREF=\"%s.fp\">%s.fp</A>", txtcomp, txtcomp) >> libind;
245 if( do_png ) {
246 printf("(<A HREF=\"%s.png\">preview</A>)", txtcomp) >> libind;
248 printf("</TD>\n </TR>\n") >> libind;
251 # Now create a layout with that element and print it.
252 if( do_png ) {
253 layout = "temp.pcb" ;
254 laytmpl = "footprint.pcb" ;
255 compfile = dir "/" comp ".fp";
256 pngfile = dir "/" comp ".png";
257 compfile2 = compfile;
258 gsub(/\\/, "", compfile2);
260 printf(" ===> %s\n", compfile);
261 printf("") > layout;
262 pok = 1;
263 while ( (getline < laytmpl) == 1 ) {
264 if( $0 ~ /ELEMENT/ ) {
265 pok = 0;
267 if( pok ) {
268 print >> layout ;
271 close( laytmpl );
273 while( (x = getline < compfile2) == 1 ) {
274 print >> layout;
276 close( compfile2 );
278 pok = 0;
279 while( (getline < laytmpl) == 1 ) {
280 if( pok ) {
281 print >> layout;
283 if( $0 ~ /ELEMENT/ ) {
284 pok = 1;
287 close( laytmpl );
288 close( layout );
290 cmd = PCB " -x png --outfile temp.png ${png_flag} --only-visible " layout " 2>&1 > /dev/null" ;
291 rc = system( cmd );
292 if( rc != 0) {
293 printf("<TR><TD>~%s</TD>\n", lib) >> broken;
294 printf(" <TD>%s</TD>\n", comp) >> broken;
295 printf(" <TD>%s</TD>\n", comment) >> broken;
296 printf(" <TD>%s</TD>\n", cmd) >> broken;
297 printf("</TR>\n") >> broken;
298 } else {
299 system( "mv temp.png " pngfile " ; rm " layout);
304 END {
305 print "" >> ind;
306 print "</UL>" >> ind;
307 print "" >> ind;
308 print "<P>For a list of footprints with either m4 syntax errors" >> ind;
309 print "or PCB syntax errors see <A HREF=\"" brokenurl "\">the broken log file.</A></P>" >> ind;
310 print "</BODY></HTML>" >> ind;
311 close( ind );
313 print "" >> broken;
314 print "</TABLE>" >> broken;
315 print "" >> broken;
316 print "</BODY></HTML>" >> broken;
317 close( broken );
319 finish_libind();
322 function finish_libind() {
323 if(libind != "") {
324 print "" >> libind;
325 print "</TABLE>" >> libind;
326 print "" >> libind;
327 print "</BODY></HTML>" >> libind;
328 close( libind );
332 ' cmd_path=./ do_png=$do_png outd="$outd_full" awk=$AWK PCB="${PCB}" $contents