FreeCalypso > hg > ueda-linux
comparison ueda/migration/g2uschem.c @ 0:cd92449fdb51
initial import of ueda and ifctf-part-lib from ifctfvax CVS
author | Space Falcon <falcon@ivan.Harhan.ORG> |
---|---|
date | Mon, 20 Jul 2015 00:24:37 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:cd92449fdb51 |
---|---|
1 #include <ctype.h> | |
2 #include <stdio.h> | |
3 #include <strings.h> | |
4 | |
5 #define MAXLINE 256 | |
6 #define MAXATTRS 16 | |
7 #define ATTRBUF_SIZE 4096 | |
8 | |
9 struct attr { | |
10 int graphparams[8]; | |
11 char *name; | |
12 char *value; | |
13 }; | |
14 | |
15 FILE *inf, *outf; | |
16 char *infname, *outfname; | |
17 int lineno; | |
18 int x_offset, y_offset; | |
19 | |
20 main(argc, argv) | |
21 char **argv; | |
22 { | |
23 char line[MAXLINE]; | |
24 int objlineno; | |
25 struct attr attrs[MAXATTRS]; | |
26 char attrbuf[ATTRBUF_SIZE]; | |
27 int nattrs; | |
28 register int c; | |
29 | |
30 if (argc > 1) { | |
31 infname = argv[1]; | |
32 inf = fopen(infname, "r"); | |
33 if (!inf) { | |
34 perror(infname); | |
35 exit(1); | |
36 } | |
37 } else { | |
38 inf = stdin; | |
39 infname = "stdin"; | |
40 } | |
41 if (argc > 2) { | |
42 outfname = argv[2]; | |
43 outf = fopen(outfname, "w"); | |
44 if (!outf) { | |
45 perror(outfname); | |
46 exit(1); | |
47 } | |
48 } else { | |
49 outf = stdout; | |
50 outfname = "stdout"; | |
51 } | |
52 | |
53 while (getline(line)) { | |
54 if (!isalpha(line[0])) { | |
55 fprintf(stderr, "%s: line %d: gschem object expected\n", | |
56 infname, lineno); | |
57 exit(1); | |
58 } | |
59 objlineno = lineno; | |
60 /* check for attached attributes */ | |
61 c = getc(inf); | |
62 if (c == '{') { | |
63 skipline(); | |
64 nattrs = read_attrs(attrs, attrbuf); | |
65 } else { | |
66 if (c != EOF) | |
67 ungetc(c, inf); | |
68 nattrs = 0; | |
69 } | |
70 /* dispatch by object type */ | |
71 switch (line[0]) { | |
72 case 'v': | |
73 continue; /* ignore */ | |
74 /* simple graphics */ | |
75 case 'L': | |
76 case 'B': | |
77 case 'V': | |
78 case 'A': | |
79 if (nattrs) | |
80 fprintf(stderr, | |
81 "%s: line %d: attributes attached to %c object (ignored)\n", | |
82 infname, objlineno, line[0]); | |
83 handle_simple_graph(line, objlineno); | |
84 continue; | |
85 case 'T': | |
86 handle_T_obj(line, objlineno); | |
87 continue; | |
88 case 'N': | |
89 handle_net(line, objlineno, attrs, nattrs); | |
90 continue; | |
91 case 'U': | |
92 handle_bus(line, objlineno, attrs, nattrs); | |
93 continue; | |
94 case 'C': | |
95 handle_component(line, objlineno, attrs, nattrs); | |
96 continue; | |
97 default: | |
98 fprintf(stderr, | |
99 "%s: line %d: object type %c not supported\n", | |
100 infname, objlineno, line[0]); | |
101 } | |
102 } | |
103 exit(0); | |
104 } | |
105 | |
106 handle_component(objline, objlineno, attrs, nattrs) | |
107 char *objline; | |
108 struct attr *attrs; | |
109 { | |
110 int numparams[5]; | |
111 char *cp, *symname; | |
112 struct attr *refdes; | |
113 register int i; | |
114 | |
115 parse_numline(objline, objlineno, numparams, 5, &cp); | |
116 if (!isspace(*cp)) { | |
117 inv: fprintf(stderr, "%s: line %d: invalid C line\n", infname, | |
118 objlineno); | |
119 exit(1); | |
120 } | |
121 while (isspace(*cp)) | |
122 cp++; | |
123 if (!isalnum(*cp)) | |
124 goto inv; | |
125 for (symname = cp; isalnum(*cp) || *cp == '-' || *cp == '_'; cp++) | |
126 ; | |
127 if (strcmp(cp, ".sym")) | |
128 goto inv; | |
129 *cp = '\0'; | |
130 | |
131 /* weed some out based on the symbol name */ | |
132 if (!strcmp(symname, "nc-x")) | |
133 return; /* we'll do that differently */ | |
134 if (strlen(symname) == 7 && !strncmp(symname, "title-", 6)) { | |
135 /* handle titleblock */ | |
136 fprintf(outf, "Schem graph %c;\n", symname[6]); | |
137 x_offset = numparams[0]; | |
138 y_offset = numparams[1]; | |
139 return; | |
140 } | |
141 | |
142 /* do we have a refdes? */ | |
143 refdes = NULL; | |
144 for (i = 0; i < nattrs; i++) | |
145 if (!strcmp(attrs[i].name, "refdes")) { | |
146 refdes = attrs + i; | |
147 break; | |
148 } | |
149 if (refdes) | |
150 emit_component_instance(numparams, symname, attrs, nattrs, | |
151 refdes, objlineno); | |
152 else | |
153 emit_graph_symbol(numparams, symname, attrs, nattrs, objlineno); | |
154 } | |
155 | |
156 emit_component_instance(numparams, symname, attrs, nattrs, refdes, objlineno) | |
157 int *numparams; | |
158 char *symname; | |
159 struct attr *attrs, *refdes; | |
160 { | |
161 register struct attr *ap; | |
162 register int i; | |
163 | |
164 fprintf(outf, "Component %s graph \"%s\" %d %d", refdes->value, symname, | |
165 numparams[0] - x_offset, numparams[1] - y_offset); | |
166 if (numparams[3]) | |
167 fprintf(outf, " rot %d", numparams[3]); | |
168 if (numparams[4]) | |
169 fputs(" mirror", outf); | |
170 fputs(" {\n", outf); | |
171 | |
172 /* attribute data */ | |
173 for (ap = attrs, i = 0; i < nattrs; ap++, i++) | |
174 if (ap != refdes) | |
175 fprintf(outf, "\t(%s=\"%s\")\n", ap->name, ap->value); | |
176 /* attribute graphics */ | |
177 for (ap = attrs, i = 0; i < nattrs; ap++, i++) | |
178 if (ap->graphparams[4]) | |
179 fprintf(outf, "\tDisplayAttr %s %d %d %d %d %d;\n", | |
180 ap->name, ap->graphparams[0] - x_offset, | |
181 ap->graphparams[1] - y_offset, | |
182 ap->graphparams[3], | |
183 ap->graphparams[6], ap->graphparams[7]); | |
184 fputs("};\n", outf); | |
185 } | |
186 | |
187 emit_graph_symbol(numparams, symname, attrs, nattrs, objlineno) | |
188 int *numparams; | |
189 char *symname; | |
190 struct attr *attrs; | |
191 { | |
192 register struct attr *ap; | |
193 register int i; | |
194 | |
195 fprintf(outf, "GraphSym \"%s\" %d %d", symname, | |
196 numparams[0] - x_offset, numparams[1] - y_offset); | |
197 if (numparams[3]) | |
198 fprintf(outf, " rot %d", numparams[3]); | |
199 if (numparams[4]) | |
200 fputs(" mirror", outf); | |
201 | |
202 if (nattrs) | |
203 fputs(" {\n", outf); | |
204 else { | |
205 fputs(";\n", outf); | |
206 return; | |
207 } | |
208 | |
209 /* attribute data */ | |
210 for (ap = attrs, i = 0; i < nattrs; ap++, i++) | |
211 fprintf(outf, "\t(%s=\"%s\")\n", ap->name, ap->value); | |
212 /* attribute graphics */ | |
213 for (ap = attrs, i = 0; i < nattrs; ap++, i++) | |
214 if (ap->graphparams[4]) | |
215 fprintf(outf, "\tDisplayAttr %s %d %d %d %d %d;\n", | |
216 ap->name, ap->graphparams[0] - x_offset, | |
217 ap->graphparams[1] - y_offset, | |
218 ap->graphparams[3], | |
219 ap->graphparams[6], ap->graphparams[7]); | |
220 fputs("};\n", outf); | |
221 } | |
222 | |
223 handle_net(objline, objlineno, attrs, nattrs) | |
224 char *objline; | |
225 struct attr *attrs; | |
226 { | |
227 int numparams[5]; | |
228 register struct attr *ap; | |
229 register int i; | |
230 | |
231 parse_numline(objline, objlineno, numparams, 5, NULL); | |
232 fprintf(outf, "NetLine (%d,%d) (%d,%d)", numparams[0] - x_offset, | |
233 numparams[1] - y_offset, numparams[2] - x_offset, | |
234 numparams[3] - y_offset); | |
235 if (nattrs) | |
236 fputs(" {\n", outf); | |
237 else { | |
238 fputs(";\n", outf); | |
239 return; | |
240 } | |
241 | |
242 /* attribute data */ | |
243 for (ap = attrs, i = 0; i < nattrs; ap++, i++) | |
244 fprintf(outf, "\t(%s=\"%s\")\n", ap->name, ap->value); | |
245 /* attribute graphics */ | |
246 for (ap = attrs, i = 0; i < nattrs; ap++, i++) | |
247 if (ap->graphparams[4]) | |
248 fprintf(outf, "\tDisplayAttr %s %d %d %d %d %d;\n", | |
249 ap->name, ap->graphparams[0] - x_offset, | |
250 ap->graphparams[1] - y_offset, | |
251 ap->graphparams[3], | |
252 ap->graphparams[6], ap->graphparams[7]); | |
253 fputs("};\n", outf); | |
254 } | |
255 | |
256 handle_bus(objline, objlineno, attrs, nattrs) | |
257 char *objline; | |
258 struct attr *attrs; | |
259 { | |
260 int numparams[6]; | |
261 register struct attr *ap; | |
262 register int i; | |
263 | |
264 parse_numline(objline, objlineno, numparams, 6, NULL); | |
265 fprintf(outf, "BusSeg (%d,%d) (%d,%d)", numparams[0] - x_offset, | |
266 numparams[1] - y_offset, numparams[2] - x_offset, | |
267 numparams[3] - y_offset); | |
268 if (nattrs) | |
269 fputs(" {\n", outf); | |
270 else { | |
271 fputs(";\n", outf); | |
272 return; | |
273 } | |
274 | |
275 /* attribute data */ | |
276 for (ap = attrs, i = 0; i < nattrs; ap++, i++) | |
277 fprintf(outf, "\t(%s=\"%s\")\n", ap->name, ap->value); | |
278 /* attribute graphics */ | |
279 for (ap = attrs, i = 0; i < nattrs; ap++, i++) | |
280 if (ap->graphparams[4]) | |
281 fprintf(outf, "\tDisplayAttr %s %d %d %d %d %d;\n", | |
282 ap->name, ap->graphparams[0] - x_offset, | |
283 ap->graphparams[1] - y_offset, | |
284 ap->graphparams[3], | |
285 ap->graphparams[6], ap->graphparams[7]); | |
286 fputs("};\n", outf); | |
287 } | |
288 | |
289 handle_simple_graph(objline, objlineno) | |
290 char *objline; | |
291 { | |
292 translate_simple_graph_line(objline, objlineno); | |
293 fprintf(outf, "GraphBlockG {\n%s\n}\n", objline); | |
294 } | |
295 | |
296 translate_simple_graph_line(objline, objlineno) | |
297 char *objline; | |
298 { | |
299 int numparams[16]; | |
300 | |
301 switch (objline[0]) { | |
302 case 'L': | |
303 parse_numline(objline, objlineno, numparams, 10, NULL); | |
304 numparams[0] -= x_offset; | |
305 numparams[1] -= y_offset; | |
306 numparams[2] -= x_offset; | |
307 numparams[3] -= y_offset; | |
308 sprintf(objline, "L %d %d %d %d %d %d %d %d %d %d", | |
309 numparams[0], numparams[1], numparams[2], numparams[3], | |
310 numparams[4], numparams[5], numparams[6], numparams[7], | |
311 numparams[8], numparams[9]); | |
312 return; | |
313 case 'B': | |
314 parse_numline(objline, objlineno, numparams, 16, NULL); | |
315 numparams[0] -= x_offset; | |
316 numparams[1] -= y_offset; | |
317 sprintf(objline, | |
318 "B %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d", | |
319 numparams[0], numparams[1], numparams[2], numparams[3], | |
320 numparams[4], numparams[5], numparams[6], numparams[7], | |
321 numparams[8], numparams[9], numparams[10],numparams[11], | |
322 numparams[12],numparams[13],numparams[14], | |
323 numparams[15]); | |
324 return; | |
325 case 'V': | |
326 parse_numline(objline, objlineno, numparams, 15, NULL); | |
327 numparams[0] -= x_offset; | |
328 numparams[1] -= y_offset; | |
329 sprintf(objline, | |
330 "V %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d", | |
331 numparams[0], numparams[1], numparams[2], numparams[3], | |
332 numparams[4], numparams[5], numparams[6], numparams[7], | |
333 numparams[8], numparams[9], numparams[10],numparams[11], | |
334 numparams[12],numparams[13],numparams[14]); | |
335 return; | |
336 case 'A': | |
337 parse_numline(objline, objlineno, numparams, 11, NULL); | |
338 numparams[0] -= x_offset; | |
339 numparams[1] -= y_offset; | |
340 sprintf(objline, "A %d %d %d %d %d %d %d %d %d %d %d", | |
341 numparams[0], numparams[1], numparams[2], numparams[3], | |
342 numparams[4], numparams[5], numparams[6], numparams[7], | |
343 numparams[8], numparams[9], numparams[10]); | |
344 return; | |
345 default: | |
346 fprintf(stderr, | |
347 "unknown type in translate_simple_graph_line()\n"); | |
348 exit(1); | |
349 } | |
350 } | |
351 | |
352 handle_T_obj(objline, objlineno) | |
353 char *objline; | |
354 { | |
355 int numparams[9]; | |
356 char textline[MAXLINE]; | |
357 register int i; | |
358 | |
359 parse_numline(objline, objlineno, numparams, 9, NULL); | |
360 if (numparams[8] < 1) { | |
361 fprintf(stderr, "%s: line %d: T object: num_lines<1!\n", | |
362 infname, objlineno); | |
363 exit(1); | |
364 } | |
365 fprintf(outf, "GraphBlockG {\nT %d %d %d %d %d %d %d %d %d\n", | |
366 numparams[0] - x_offset, numparams[1] - y_offset, numparams[2], | |
367 numparams[3], numparams[4], numparams[5], numparams[6], | |
368 numparams[7], numparams[8]); | |
369 for (i = numparams[8]; i; i--) { | |
370 if (!getline(textline)) { | |
371 fprintf(stderr, "%s: EOF in T object\n", infname); | |
372 exit(1); | |
373 } | |
374 fprintf(outf, "%s\n", textline); | |
375 } | |
376 fputs("}\n", outf); | |
377 } | |
378 | |
379 read_attrs(attrs, attrbuf) | |
380 struct attr *attrs; | |
381 char *attrbuf; | |
382 { | |
383 char Tline[MAXLINE]; | |
384 int Tline_parsed[9]; | |
385 char attrline[MAXLINE]; | |
386 int nattrs, chars_used; | |
387 char *bufptr; | |
388 register char *cp; | |
389 register int i; | |
390 | |
391 for (nattrs = 0, chars_used = 0, bufptr = attrbuf; ; ) { | |
392 if (!getline(Tline)) { | |
393 badeof: fprintf(stderr, "%s: EOF in attribute block\n", | |
394 infname); | |
395 exit(1); | |
396 } | |
397 switch (Tline[0]) { | |
398 case 'T': | |
399 break; | |
400 case '}': | |
401 return(nattrs); | |
402 default: | |
403 fprintf(stderr, | |
404 "%s: line %d: wrong line type in attribute block\n", | |
405 infname, lineno); | |
406 exit(1); | |
407 } | |
408 parse_numline(Tline, lineno, Tline_parsed, 9, NULL); | |
409 if (Tline_parsed[8] < 1) { | |
410 fprintf(stderr, "%s: line %d: T object: num_lines<1!\n", | |
411 infname, lineno); | |
412 exit(1); | |
413 } | |
414 if (Tline_parsed[8] > 1) { | |
415 fprintf(stderr, | |
416 "%s: line %d: multiline text object in attribute block not supported\n", | |
417 infname, lineno); | |
418 exit(1); | |
419 } | |
420 if (!getline(attrline)) | |
421 goto badeof; | |
422 cp = index(attrline, '='); | |
423 if (!cp) /* non-attribute text item */ | |
424 continue; /* ignore these */ | |
425 /* break name and value */ | |
426 *cp++ = '\0'; | |
427 if (nattrs >= MAXATTRS) { | |
428 fprintf(stderr, "%s: line %d: too many attributes\n", | |
429 infname, lineno); | |
430 exit(1); | |
431 } | |
432 for (i = 0; i < 8; i++) | |
433 attrs[nattrs].graphparams[i] = Tline_parsed[i]; | |
434 i = strlen(attrline) + 1; | |
435 chars_used += i; | |
436 if (chars_used > ATTRBUF_SIZE) { | |
437 bufovflo: fprintf(stderr, "%s: line %d: attr text buf overflow\n", | |
438 infname, lineno); | |
439 exit(1); | |
440 } | |
441 bcopy(attrline, bufptr, i); | |
442 attrs[nattrs].name = bufptr; | |
443 bufptr += i; | |
444 i = strlen(cp) + 1; | |
445 chars_used += i; | |
446 if (chars_used > ATTRBUF_SIZE) | |
447 goto bufovflo; | |
448 bcopy(cp, bufptr, i); | |
449 attrs[nattrs].value = bufptr; | |
450 bufptr += i; | |
451 nattrs++; | |
452 } | |
453 } | |
454 | |
455 parse_numline(line, lineno, numarray, nfields, endp) | |
456 char *line; | |
457 int lineno; | |
458 int *numarray; | |
459 int nfields; | |
460 char **endp; | |
461 { | |
462 register char *cp, *np; | |
463 register int i; | |
464 | |
465 for (i = 0, cp = line+1; i < nfields; i++) { | |
466 if (!isspace(*cp)) { | |
467 inv: fprintf(stderr, "%s: line %d: invalid numeric line\n", | |
468 infname, lineno); | |
469 exit(1); | |
470 } | |
471 while (isspace(*cp)) | |
472 cp++; | |
473 np = cp; | |
474 if (*cp == '-') | |
475 cp++; | |
476 if (!isdigit(*cp)) | |
477 goto inv; | |
478 while (isdigit(*cp)) | |
479 cp++; | |
480 numarray[i] = atoi(np); | |
481 } | |
482 if (endp) | |
483 *endp = cp; | |
484 else if (*cp) | |
485 goto inv; | |
486 } | |
487 | |
488 getline(linebuf) | |
489 char *linebuf; | |
490 { | |
491 register char *cp; | |
492 | |
493 if (fgets(linebuf, MAXLINE, inf) == NULL) | |
494 return(0); | |
495 lineno++; | |
496 /* strip trailing newline or other whitespace */ | |
497 cp = index(linebuf, '\0'); | |
498 while (cp > linebuf && isspace(cp[-1])) | |
499 cp--; | |
500 *cp = '\0'; | |
501 return(1); | |
502 } | |
503 | |
504 skipline() | |
505 { | |
506 char junkline[MAXLINE]; | |
507 | |
508 getline(junkline); | |
509 } |