comparison ueda/utils/cutelements.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 e2130f1ef720
comparison
equal deleted inserted replaced
-1:000000000000 0:cd92449fdb51
1 /*
2 * This program cuts the "elements PCB" constructed by the
3 * ueda-getfps | ueda-runm4 pipeline into one file per element.
4 *
5 * Alternatively, this program can also be used to extract all elements
6 * out of any PCB layout file.
7 */
8
9 #include <ctype.h>
10 #include <stdio.h>
11 #include <strings.h>
12
13 char *inputname;
14 FILE *inf;
15 int lineno, in_element, parencount;
16 FILE *elemf;
17
18 char *
19 do_element_line(cp)
20 register char *cp;
21 {
22 char copybuf[1024];
23 char opench, closech;
24 char *fields[11], *pcbname;
25 int nfields;
26 register int c;
27 char *err;
28
29 if (in_element) {
30 fprintf(stderr, "%s: line %d: nested Element\n", inputname,
31 lineno);
32 exit(1);
33 }
34 if (parencount) {
35 fprintf(stderr,
36 "%s: line %d: Element not at the top nesting level\n",
37 inputname, lineno);
38 exit(1);
39 }
40
41 while (isspace(*cp))
42 cp++;
43 opench = *cp++;
44 switch (opench) {
45 case '(':
46 closech = ')';
47 break;
48 case '[':
49 closech = ']';
50 break;
51 default:
52 err = "no valid opening char";
53 inv: fprintf(stderr, "%s: line %d: invalid Element line: %s\n",
54 inputname, lineno, err);
55 exit(1);
56 }
57
58 strcpy(copybuf, cp);
59 cp = copybuf;
60
61 for (nfields = 0; ; ) {
62 while (isspace(*cp))
63 cp++;
64 if (!*cp) {
65 badeol: err = "missing closing char";
66 goto inv;
67 }
68 if (*cp == closech) {
69 cp++;
70 break;
71 }
72 if (nfields >= 11) {
73 err = "too many fields";
74 goto inv;
75 }
76 if (*cp == '\"') {
77 cp++;
78 for (fields[nfields++] = cp; c = *cp; cp++)
79 if (c == '\"')
80 break;
81 } else {
82 fields[nfields++] = cp;
83 while ((c = *cp) && !isspace(c) && c != closech)
84 cp++;
85 }
86 if (!c)
87 goto badeol;
88 *cp++ = '\0';
89 if (c == closech)
90 break;
91 }
92
93 switch (nfields) {
94 case 7:
95 pcbname = fields[1];
96 break;
97 case 8:
98 case 9:
99 case 11:
100 pcbname = fields[2];
101 break;
102 default:
103 err = "unrecognized format";
104 goto inv;
105 }
106 open_element(pcbname);
107
108 return(cp);
109 }
110
111 open_element(name)
112 register char *name;
113 {
114 elemf = fopen(name, "w");
115 if (!elemf) {
116 fprintf(stderr, "cannot create file: %s\n", name);
117 exit(1);
118 }
119 in_element = 1;
120 }
121
122 main(argc, argv)
123 char **argv;
124 {
125 char linebuf[1024];
126 register char *cp;
127
128 if (argc > 2) {
129 fprintf(stderr, "usage: %s [collection_file]\n", argv[0]);
130 exit(1);
131 }
132 if (argc == 2) {
133 inputname = argv[1];
134 inf = fopen(inputname, "r");
135 if (!inf) {
136 perror(inputname);
137 exit(1);
138 }
139 } else {
140 inputname = "stdin";
141 inf = stdin;
142 }
143
144 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) {
145 for (cp = linebuf; isspace(*cp); cp++)
146 ;
147 if (!strncmp(cp, "Element", 7) &&
148 (cp[7] == '(' || cp[7] == '[' || isspace(cp[7])))
149 cp = do_element_line(cp + 7);
150 if (in_element)
151 fputs(linebuf, elemf);
152 scan_parens(cp);
153 }
154
155 if (parencount || in_element) {
156 fprintf(stderr, "%s: unclosed structure(s) at the end\n",
157 inputname);
158 exit(1);
159 }
160 exit(0);
161 }
162
163 scan_parens(line)
164 char *line;
165 {
166 register char *cp;
167 register int c;
168
169 for (cp = line; c = *cp; cp++) {
170 if (c == '\'' && cp[1] && cp[2] == '\'') {
171 cp += 2;
172 continue;
173 }
174 if (c == '(' || c == '[')
175 parencount++;
176 else if (c == ')' || c == ']') {
177 parencount--;
178 if (parencount < 0) {
179 fprintf(stderr,
180 "%s: line %d: negative paren count\n",
181 inputname, lineno);
182 exit(1);
183 }
184 if ((parencount == 0) && in_element) {
185 fclose(elemf);
186 in_element = 0;
187 }
188 } else if (c == '#')
189 return;
190 }
191 }