comparison miscutil/protel-parts-condense.c @ 144:ffadaa339478

protel-parts-condense misc utility written
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 20 Sep 2020 02:40:23 +0000
parents
children
comparison
equal deleted inserted replaced
143:7c0fd80782c8 144:ffadaa339478
1 /*
2 * This program reads a Protel netlist (exported from Altium), extracts
3 * the component list portion and emits it in a condensed form.
4 */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <strings.h>
10
11 static char *infname;
12 static FILE *inf;
13 static char linebuf[80], savebuf[6][80];
14 static int lineno;
15
16 static
17 get_line()
18 {
19 char *cp;
20
21 if (!fgets(linebuf, sizeof linebuf, inf))
22 return(0);
23 lineno++;
24 cp = index(linebuf, '\n');
25 if (!cp) {
26 fprintf(stderr, "%s line %d: missing newline\n",
27 infname, lineno);
28 exit(1);
29 }
30 *cp = '\0';
31 if (cp > linebuf && cp[-1] == '\r')
32 *--cp = '\0';
33 return(1);
34 }
35
36 static void
37 get_line_notfirst()
38 {
39 if (!get_line()) {
40 fprintf(stderr, "error: unexpected EOF in input\n");
41 exit(1);
42 }
43 if (!strcmp(linebuf, "[")) {
44 fprintf(stderr, "%s line %d: [ NOT expected\n",
45 infname, lineno);
46 exit(1);
47 }
48 if (!strcmp(linebuf, "(")) {
49 fprintf(stderr, "%s line %d: ( NOT expected\n",
50 infname, lineno);
51 exit(1);
52 }
53 }
54
55 static void
56 check_line_for_nonprint_chars()
57 {
58 char *cp;
59 int c;
60
61 for (cp = linebuf; *cp; cp++) {
62 c = *cp;
63 if ((c < ' ' || c > '~') && c != '\t') {
64 fprintf(stderr, "%s line %d: non-printable char\n",
65 infname, lineno);
66 exit(1);
67 }
68 }
69 }
70
71 static int
72 item_needs_quoting(str)
73 char *str;
74 {
75 char *cp;
76 int c;
77
78 if (!str[0])
79 return(1);
80 for (cp = str; *cp; cp++) {
81 c = *cp;
82 if (c == '\t' || c == ' ' || c == '\"' || c == '\\')
83 return(1);
84 }
85 return(0);
86 }
87
88 static void
89 print_item_quoted(str)
90 char *str;
91 {
92 char *cp;
93 int c;
94
95 putchar('\"');
96 for (cp = str; *cp; cp++) {
97 c = *cp;
98 if (c == '\t') {
99 putchar('\\');
100 putchar('t');
101 continue;
102 }
103 if (c == '\"' || c == '\\')
104 putchar('\\');
105 putchar(c);
106 }
107 putchar('\"');
108 }
109
110 static void
111 print_item(str)
112 char *str;
113 {
114 if (item_needs_quoting(str))
115 print_item_quoted(str);
116 else
117 fputs(str, stdout);
118 }
119
120 static void
121 process_component_block()
122 {
123 int i;
124
125 for (i = 0; i < 6; i++) {
126 get_line_notfirst();
127 if (!strcmp(linebuf, "]")) {
128 fprintf(stderr, "%s line %d: ] NOT expected\n",
129 infname, lineno);
130 exit(1);
131 }
132 if (!strcmp(linebuf, ")")) {
133 fprintf(stderr, "%s line %d: ) NOT expected\n",
134 infname, lineno);
135 exit(1);
136 }
137 check_line_for_nonprint_chars();
138 strcpy(savebuf[i], linebuf);
139 }
140 get_line_notfirst();
141 if (strcmp(linebuf, "]")) {
142 fprintf(stderr, "%s line %d: expected ]\n", infname, lineno);
143 exit(1);
144 }
145 print_item(savebuf[0]);
146 putchar(' ');
147 print_item(savebuf[1]);
148 putchar(' ');
149 print_item(savebuf[2]);
150 putchar(' ');
151 print_item(savebuf[3]);
152 putchar(' ');
153 print_item(savebuf[4]);
154 putchar(' ');
155 print_item(savebuf[5]);
156 putchar('\n');
157 }
158
159 static void
160 skip_net_block()
161 {
162 for (;;) {
163 get_line_notfirst();
164 if (!strcmp(linebuf, "]")) {
165 fprintf(stderr, "%s line %d: ] NOT expected\n",
166 infname, lineno);
167 exit(1);
168 }
169 if (!strcmp(linebuf, ")"))
170 break;
171 }
172 }
173
174 main(argc, argv)
175 char **argv;
176 {
177 if (argc != 2) {
178 fprintf(stderr, "usage: %s protel-netlist-file\n", argv[0]);
179 exit(1);
180 }
181 infname = argv[1];
182 inf = fopen(infname, "r");
183 if (!inf) {
184 perror(infname);
185 exit(1);
186 }
187 for (;;) {
188 if (!get_line())
189 break;
190 if (!strcmp(linebuf, "["))
191 process_component_block();
192 else if (!strcmp(linebuf, "("))
193 skip_net_block();
194 else {
195 fprintf(stderr,
196 "%s line %d: expected beginning of block\n",
197 infname, lineno);
198 exit(1);
199 }
200 }
201 exit(0);
202 }