comparison pads2gpcb/parttype.c @ 63:455a0051f9d2

pads2gpcb: PARTTYPE parsing implemented, compiles
author Mychaela Falconia <falcon@ivan.Harhan.ORG>
date Sun, 31 Jan 2016 07:32:08 +0000
parents
children 2b71943a311b
comparison
equal deleted inserted replaced
62:9d7e2937883d 63:455a0051f9d2
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <ctype.h>
4 #include <string.h>
5 #include <strings.h>
6 #include "globals.h"
7 #include "struct.h"
8
9 extern struct part_decal *find_decal_by_name();
10 extern char *savestr();
11
12 static struct part_type *our_parttype;
13 static int ngates, nsignals;
14
15 static void
16 enter_parttype()
17 {
18 struct part_type *p, **pp;
19 char *name = input_line_fields[0];
20
21 for (pp = &part_type_list; p = *pp; pp = &p->next)
22 if (!strcmp(p->name, name)) {
23 fprintf(stderr,
24 "%s line %d: part type name \"%s\" already defined\n",
25 input_filename, input_lineno, name);
26 exit(1);
27 }
28 p = malloc(sizeof(struct part_type) + strlen(name) + 1);
29 if (!p) {
30 perror("malloc of struct part_type");
31 exit(1);
32 }
33 bzero(p, sizeof(struct part_type));
34 p->name = (char *)(p + 1);
35 strcpy(p->name, name);
36 *pp = p;
37 our_parttype = p;
38 }
39
40 static void
41 find_decals()
42 {
43 char *cp, *np;
44 struct part_decal *decal;
45
46 for (cp = input_line_fields[1]; cp; cp = np) {
47 np = index(cp, ':');
48 if (np)
49 *np++ = '\0';
50 if (!*cp)
51 continue;
52 decal = find_decal_by_name(cp);
53 if (!decal) {
54 fprintf(stderr,
55 "%s line %d: part type definition refers to unknown decal name \"%s\"\n",
56 input_filename, input_lineno, cp);
57 exit(1);
58 }
59 if (our_parttype->ndecals >= MAX_DECALS_PER_PART_TYPE) {
60 fprintf(stderr,
61 "%s line %d: MAX_DECALS_PER_PART_TYPE exceeded\n",
62 input_filename, input_lineno);
63 exit(1);
64 }
65 our_parttype->decals[our_parttype->ndecals++] = decal;
66 }
67 if (!our_parttype->ndecals) {
68 fprintf(stderr,
69 "%s line %d: empty list of decals in part type def\n",
70 input_filename, input_lineno);
71 exit(1);
72 }
73 }
74
75 static void
76 process_header_line()
77 {
78 if (input_line_nfields < 6) {
79 fprintf(stderr,
80 "%s line %d: expected beginning of part type definition, wrong # of fields\n",
81 input_filename, input_lineno);
82 exit(1);
83 }
84 enter_parttype();
85 printf("Processing part type %s\n", our_parttype->name);
86 find_decals();
87 ngates = atoi(input_line_fields[3]);
88 nsignals = atoi(input_line_fields[4]);
89 our_parttype->num_alpha_pins = atoi(input_line_fields[5]);
90 }
91
92 static void
93 get_line_internal()
94 {
95 if (!get_input_line()) {
96 fprintf(stderr,
97 "error: EOF in the middle of a part type definition\n");
98 exit(1);
99 }
100 }
101
102 static void
103 process_one_gate_def()
104 {
105 int gatepins;
106
107 get_line_internal();
108 parse_input_line_fields();
109 if (input_line_nfields != 3 || strcmp(input_line_fields[0], "G")) {
110 fprintf(stderr, "%s line %d: expected gate def header line\n",
111 input_filename, input_lineno);
112 exit(1);
113 }
114 gatepins = atoi(input_line_fields[2]);
115 while (gatepins > 0) {
116 get_line_internal();
117 parse_input_line_fields();
118 gatepins -= input_line_nfields;
119 }
120 }
121
122 static void
123 alloc_alpha_pins_array()
124 {
125 our_parttype->alpha_pins =
126 malloc(sizeof(char *) * our_parttype->num_alpha_pins);
127 if (!our_parttype->alpha_pins) {
128 perror("malloc of alpha_pins array");
129 exit(1);
130 }
131 }
132
133 static void
134 read_alpha_pins()
135 {
136 int idx, i;
137
138 for (idx = 0; idx < our_parttype->num_alpha_pins; ) {
139 get_line_internal();
140 parse_input_line_fields();
141 for (i = 0; i < input_line_nfields; i++) {
142 if (idx >= our_parttype->num_alpha_pins) {
143 fprintf(stderr,
144 "%s line %d: alpha_pins array overflow\n",
145 input_filename, input_lineno);
146 exit(1);
147 }
148 our_parttype->alpha_pins[idx++] =
149 savestr(input_line_fields[i]);
150 }
151 }
152 }
153
154 static void
155 process_one_parttype()
156 {
157 int i;
158
159 process_header_line();
160 for (i = 0; i < ngates; i++)
161 process_one_gate_def();
162 for (i = 0; i < nsignals; i++)
163 get_line_internal();
164 if (our_parttype->num_alpha_pins) {
165 alloc_alpha_pins_array();
166 read_alpha_pins();
167 }
168 }
169
170 process_parttype_section()
171 {
172 for (;;) {
173 if (!get_input_line()) {
174 fprintf(stderr, "error: EOF in PARTTYPE section\n");
175 exit(1);
176 }
177 if (input_line_buf[0] == '*') {
178 parse_starline();
179 if (strcmp(input_line_starkw, "REMARK"))
180 break;
181 else
182 continue;
183 }
184 parse_input_line_fields();
185 if (input_line_nfields)
186 process_one_parttype();
187 }
188 }