comparison ueda/unet-utils/unet2pcb.c @ 35:47b5b8310cac

unet2pcb written, compiles
author Space Falcon <falcon@ivan.Harhan.ORG>
date Sun, 09 Aug 2015 06:33:38 +0000
parents
children
comparison
equal deleted inserted replaced
34:bc465afed423 35:47b5b8310cac
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <strings.h>
5 #include <unistd.h>
6 #include "../libunet/unetrd.h"
7 #include "../libunet/nethash.h"
8
9 extern struct net *enter_net_object();
10 extern struct net *find_net_by_name();
11 extern struct net *net_list_head;
12
13 static char *input_filename, *output_filename;
14 static struct unetrd_state rdstate;
15 static struct unetrd_out rdout;
16 static FILE *outFILE;
17
18 struct netextra {
19 struct netmember *head;
20 struct netmember **tailp;
21 };
22
23 struct netmember {
24 char *name;
25 struct netmember *next;
26 };
27
28 static void
29 process_pin_connect(compname)
30 char *compname;
31 {
32 register struct net *n;
33 register struct netextra *nx;
34 register struct netmember *nm;
35
36 n = find_net_by_name(rdout.connect_to_net);
37 nx = (struct netextra *)(n + 1);
38 nm = (struct netmember *) malloc(sizeof(struct netmember) +
39 strlen(compname) +
40 strlen(rdout.objname) + 2);
41 if (!nm) {
42 perror("malloc");
43 exit(1);
44 }
45 nm->name = (char *)(nm + 1);
46 sprintf(nm->name, "%s-%s", compname, rdout.objname);
47 nm->next = 0;
48 *nx->tailp = nm;
49 nx->tailp = &nm->next;
50 }
51
52 static void
53 process_component()
54 {
55 char compname[64];
56
57 strcpy(compname, rdout.objname);
58 for (;;) {
59 if (!read_unet_line(&rdstate, &rdout)) {
60 fprintf(stderr, "%s error: EOF in COMPONENT block\n",
61 input_filename);
62 exit(1);
63 }
64 if (rdout.typecode == UNETOBJ_CLOSINGBRACE)
65 break;
66 switch(rdout.typecode) {
67 case UNETOBJ_PRIMITIVE:
68 case UNETOBJ_ALTNAME:
69 case UNETOBJ_ATTR:
70 continue;
71 case UNETOBJ_PIN:
72 if (rdout.connect_to_net)
73 process_pin_connect(compname);
74 continue;
75 case UNETOBJ_PINMAP:
76 fprintf(stderr,
77 "%s line %d: PINMAP objects not expected in unet2pcb input\n",
78 input_filename, rdstate.lineno);
79 exit(1);
80 default:
81 fprintf(stderr,
82 "%s line %d: object type %s unexpected in COMPONENT block\n",
83 input_filename, rdstate.lineno, rdout.keyword);
84 exit(1);
85 }
86 }
87 }
88
89 static void
90 process_input_unet()
91 {
92 struct net *n;
93 struct netextra *nx;
94
95 open_unet_input_file(input_filename, &rdstate);
96 while (read_unet_line(&rdstate, &rdout)) {
97 switch(rdout.typecode) {
98 case UNETOBJ_CLOSINGBRACE:
99 fprintf(stderr,
100 "%s line %d: unexpected '}' outside of component block\n",
101 input_filename, rdstate.lineno);
102 exit(1);
103 case UNETOBJ_NET:
104 n = enter_net_object(rdout.objname,
105 sizeof(struct netextra));
106 nx = (struct netextra *)(n + 1);
107 nx->head = 0;
108 nx->tailp = &nx->head;
109 continue;
110 case UNETOBJ_COMPONENT:
111 process_component();
112 continue;
113 case UNETOBJ_STARPOINT:
114 fprintf(stderr,
115 "error: STARPOINT objects not expected in unet2pcb input (%s line %d)\n",
116 input_filename, rdstate.lineno);
117 exit(1);
118 default:
119 fprintf(stderr,
120 "%s line %d: unexpected object type %s\n",
121 input_filename, rdstate.lineno, rdout.keyword);
122 exit(1);
123 }
124 }
125 }
126
127 static
128 emit_net(net)
129 struct net *net;
130 {
131 struct netextra *nx;
132 register struct netmember *nm;
133 int linelen, linefresh;
134 register int i;
135
136 nx = (struct netextra *)(net + 1);
137 fprintf(outFILE, "%s\t", net->name);
138 linelen = (strlen(net->name) + 8) & ~7;
139 linefresh = 1;
140 for (nm = nx->head; nm; nm = nm->next) {
141 i = strlen(nm->name) + 1;
142 if (!linefresh && linelen + i > 78) {
143 fputs(" \\\n\t", outFILE);
144 linelen = 8;
145 linefresh = 1;
146 }
147 if (linefresh)
148 i--;
149 else
150 putc(' ', outFILE);
151 fputs(nm->name, outFILE);
152 linelen += i;
153 linefresh = 0;
154 }
155 putc('\n', outFILE);
156 }
157
158 static void
159 generate_output()
160 {
161 register struct net *n;
162
163 if (output_filename) {
164 outFILE = fopen(output_filename, "w");
165 if (!outFILE) {
166 perror(output_filename);
167 exit(1);
168 }
169 } else
170 outFILE = stdout;
171 for (n = net_list_head; n; n = n->nextinlist)
172 emit_net(n);
173 if (outFILE != stdout)
174 fclose(outFILE);
175 }
176
177 main(argc, argv)
178 char **argv;
179 {
180 if (argc < 2 || argc > 3) {
181 fprintf(stderr, "usage: %s input.unet [output-file]\n",
182 argv[0]);
183 exit(1);
184 }
185 input_filename = argv[1];
186 output_filename = argv[2];
187 process_input_unet();
188 generate_output();
189 exit(0);
190 }