Reduce visibility of functions used in only one file.
[geda-gaf/berndj.git] / gnetlist / src / s_netattrib.c
blob4c439e3598d72ff6fd1a6a8da7048516eb46dbcf
1 /* gEDA - GPL Electronic Design Automation
2 * gnetlist - gEDA Netlist
3 * Copyright (C) 1998-2007 Ales Hvezda
4 * Copyright (C) 1998-2007 gEDA Contributors (see ChangeLog for details)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
21 #include <config.h>
23 #include <stdio.h>
24 #include <ctype.h>
25 #ifdef HAVE_STDLIB_H
26 #include <stdlib.h>
27 #endif
28 #ifdef HAVE_ASSERT_H
29 #include <assert.h>
30 #endif
31 #ifdef HAVE_STRING_H
32 #include <string.h>
33 #endif
35 #include <libgeda/libgeda.h>
37 #include "../include/globals.h"
38 #include "../include/prototype.h"
40 #ifdef HAVE_LIBDMALLOC
41 #include <dmalloc.h>
42 #endif
44 /* used by the extract functions below */
45 #define DELIMITERS ",; "
47 /* things to do here : */
48 /* write the net alias function */
50 /* be sure to g_free returned string */
51 char *s_netattrib_extract_netname(char *value)
53 char *return_value = NULL;
54 int i = 0;
56 /* a bit larger than needed ... */
57 return_value = g_strdup (value);
59 while (value[i] != ':' && value[i] != '\0') {
60 return_value[i] = value[i];
61 i++;
64 if (value[i] != ':') {
65 fprintf(stderr, "Found malformed net attribute\n");
66 return (g_strdup ("unknown"));
69 return_value[i] = '\0';
71 return (return_value);
75 /* if this function creates a cpinlist list, it will not have a head node */
76 void
77 s_netattrib_create_pins(TOPLEVEL * pr_current, OBJECT * o_current,
78 NETLIST * netlist, char *value,
79 char const *hierarchy_tag)
81 NETLIST *netlist_tail = NULL;
82 CPINLIST *cpinlist_tail = NULL;
83 CPINLIST *new_cpin = NULL;
84 CPINLIST *old_cpin = NULL;
85 char *connected_to = NULL;
86 char *net_name = NULL;
87 char *start_of_pinlist = NULL;
88 char *char_ptr = NULL;
89 char *current_pin = NULL;
91 char_ptr = strchr(value, ':');
93 if (char_ptr == NULL) {
94 return;
97 net_name = s_netattrib_extract_netname(value);
99 /* skip over first : */
100 start_of_pinlist = char_ptr + 1;
101 current_pin = strtok(start_of_pinlist, DELIMITERS);
102 while (current_pin) {
103 netlist_tail = s_netlist_return_tail(netlist);
104 cpinlist_tail = s_cpinlist_return_tail(netlist_tail->cpins);
106 if (netlist->component_uref) {
107 old_cpin =
108 s_cpinlist_search_pin(netlist_tail->cpins, current_pin);
110 if (old_cpin) {
111 if (!old_cpin->nets) {
112 fprintf(stderr,
113 "Ack! internal error! (s_netattrib_create_pins)\n");
116 if (old_cpin->nets->net_name) {
117 fprintf(stderr,
118 "Found a cpinlist head with a netname! [%s]\n",
119 old_cpin->nets->net_name);
120 g_free(old_cpin->nets->net_name);
124 old_cpin->nets->net_name =
125 s_hierarchy_create_netattrib(pr_current, net_name,
126 hierarchy_tag);
127 old_cpin->nets->net_name_has_priority = TRUE;
128 connected_to = g_strdup_printf("%s %s",
129 netlist->component_uref,
130 current_pin);
131 old_cpin->nets->connected_to = g_strdup(connected_to);
132 old_cpin->nets->nid = o_current->sid;
133 g_free(connected_to);
134 } else {
135 new_cpin = s_cpinlist_add(cpinlist_tail);
137 new_cpin->pin_number = g_strdup (current_pin);
138 new_cpin->net_name = NULL;
140 new_cpin->plid = o_current->sid;
142 new_cpin->nets = s_net_add(NULL);
143 new_cpin->nets->net_name_has_priority = TRUE;
144 new_cpin->nets->net_name =
145 s_hierarchy_create_netattrib(pr_current, net_name,
146 hierarchy_tag);
148 connected_to = g_strdup_printf("%s %s",
149 netlist->component_uref,
150 current_pin);
151 new_cpin->nets->connected_to = g_strdup(connected_to);
152 new_cpin->nets->nid = o_current->sid;
154 #if DEBUG
155 printf("Finished creating: %s\n", connected_to);
156 printf("netname: %s %s\n", new_cpin->nets->net_name,
157 hierarchy_tag);
158 #endif
160 g_free(connected_to);
163 } else { /* no uref, means this is a special component */
166 current_pin = strtok(NULL, DELIMITERS);
169 g_free(net_name);
173 void
174 s_netattrib_handle(TOPLEVEL * pr_current, OBJECT * o_current,
175 NETLIST * netlist, char const *hierarchy_tag)
177 char *value;
178 int counter = 0;
180 /* for now just look inside the component */
181 value = o_attrib_search_name(o_current->complex->prim_objs,
182 "net", counter);
184 #if DEBUG
185 printf("found net= %s\n", value);
186 #endif
188 while (value != NULL) {
189 if (value) {
190 s_netattrib_create_pins(pr_current, o_current, netlist, value,
191 hierarchy_tag);
192 g_free(value);
194 counter++;
195 value = o_attrib_search_name(o_current->complex->prim_objs,
196 "net", counter);
197 #if DEBUG
198 printf("found net= %s\n", value);
199 #endif
203 g_free(value);
206 /* for now just look inside the component */
207 counter = 0;
208 value = o_attrib_search_name_single_count(o_current, "net", counter);
209 while (value != NULL) {
210 if (value) {
211 s_netattrib_create_pins(pr_current, o_current, netlist, value,
212 hierarchy_tag);
213 g_free(value);
215 counter++;
216 value =
217 o_attrib_search_name_single_count(o_current, "net", counter);
220 g_free(value);
223 char *s_netattrib_net_search(OBJECT * o_current, char *wanted_pin)
225 char *value = NULL;
226 char *char_ptr = NULL;
227 char *net_name = NULL;
228 char *current_pin = NULL;
229 char *start_of_pinlist = NULL;
230 char *return_value = NULL;
231 int counter = 0;
233 if (o_current == NULL) {
234 return(NULL);
237 if (o_current->complex == NULL) {
238 return(NULL);
241 /* for now just look inside the component */
242 value = o_attrib_search_name(o_current->complex->prim_objs,
243 "net", counter);
244 while (value != NULL) {
245 if (value) {
246 char_ptr = strchr(value, ':');
247 if (char_ptr == NULL) {
248 fprintf(stderr, "Got an invalid net= attrib [net=%s]\nMissing : in net= attrib\n",
249 value);
250 return (NULL);
253 net_name = s_netattrib_extract_netname(value);
255 start_of_pinlist = char_ptr + 1;
256 current_pin = strtok(start_of_pinlist, DELIMITERS);
257 while (current_pin && !return_value) {
258 #if DEBUG
259 printf("looking at: %s\n", current_pin);
260 #endif
261 if (strcmp(current_pin, wanted_pin) == 0) {
262 #if DEBUG
263 printf("found net_name: _%s_\n", net_name);
264 #endif
265 return_value = net_name;
267 current_pin = strtok(NULL, DELIMITERS);
270 g_free(value);
272 counter++;
273 value = o_attrib_search_name(o_current->complex->prim_objs,
274 "net", counter);
278 g_free(value);
281 /* now look outside the component */
282 counter = 0;
283 value = o_attrib_search_name_single_count(o_current, "net", counter);
284 while (value != NULL) {
285 if (value) {
286 char_ptr = strchr(value, ':');
287 if (char_ptr == NULL) {
288 fprintf(stderr, "Got an invalid net= attrib [net=%s]\nMissing : in net= attrib\n",
289 value);
290 return (NULL);
293 net_name = s_netattrib_extract_netname(value);
295 start_of_pinlist = char_ptr + 1;
296 current_pin = strtok(start_of_pinlist, DELIMITERS);
297 while (current_pin) {
299 #if DEBUG
300 printf("looking at: %s\n", current_pin);
301 #endif
302 if (strcmp(current_pin, wanted_pin) == 0) {
303 #if DEBUG
304 printf("found net_name: _%s_\n", net_name);
305 #endif
306 if (return_value) {
307 g_free(return_value);
308 return_value = NULL;
311 return (net_name);
313 current_pin = strtok(NULL, DELIMITERS);
316 g_free(value);
318 counter++;
319 value =
320 o_attrib_search_name_single_count(o_current, "net", counter);
323 g_free(value);
325 if (return_value) {
326 return (return_value);
327 } else {
328 return (NULL);
332 char *s_netattrib_return_netname(TOPLEVEL * pr_current, OBJECT * o_current,
333 char *pinnumber, char const *hierarchy_tag)
335 char *current_pin;
336 char *netname;
337 char *temp_netname;
339 #if DEBUG
340 printf("extract return netname here\n");
341 #endif
343 /* skip over POWER tag */
344 current_pin = strtok(pinnumber, " ");
346 current_pin = strtok(NULL, " ");
347 if (current_pin == NULL) {
348 return (NULL);
350 #if DEBUG
351 printf("inside return_netname: %s\n", current_pin);
352 #endif
354 /* use hierarchy tag here to make this net uniq */
355 temp_netname = s_netattrib_net_search(o_current->complex_parent,
356 current_pin);
358 netname =
359 s_hierarchy_create_netattrib(pr_current, temp_netname,
360 hierarchy_tag);
362 #if DEBUG
363 printf("netname: %s\n", netname);
364 #endif
366 return (netname);