gnetlist: Fix some "set but not used" warnings.
[geda-gaf/peter-b.git] / gnetlist / src / s_net.c
blob0654d695cf6d48882f918651725bdaa439b67890
1 /* gEDA - GPL Electronic Design Automation
2 * gnetlist - gEDA Netlist
3 * Copyright (C) 1998-2010 Ales Hvezda
4 * Copyright (C) 1998-2010 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <config.h>
22 #include <missing.h>
24 #include <stdio.h>
25 #include <ctype.h>
26 #ifdef HAVE_STRING_H
27 #include <string.h>
28 #endif
29 #ifdef HAVE_STDLIB_H
30 #include <stdlib.h>
31 #endif
32 #ifdef HAVE_ASSERT_H
33 #include <assert.h>
34 #endif
36 #include <libgeda/libgeda.h>
38 #include "../include/globals.h"
39 #include "../include/prototype.h"
41 #ifdef HAVE_LIBDMALLOC
42 #include <dmalloc.h>
43 #endif
45 static int unnamed_net_counter = 1;
46 static int unnamed_bus_counter = 1;
47 static int unnamed_pin_counter = 1;
49 #define MAX_UNNAMED_NETS 99999999
50 #define MAX_UNNAMED_PINS 99999999
52 /* hack rename this to be s_return_tail */
53 /* update object_tail or any list of that matter */
54 NET *s_net_return_tail(NET * head)
56 NET *n_current = NULL;
57 NET *ret_struct = NULL;
59 n_current = head;
60 while (n_current != NULL) { /* goto end of list */
61 ret_struct = n_current;
62 n_current = n_current->next;
65 return (ret_struct);
68 /* hack rename this to be s_return_head */
69 /* update object_tail or any list of that matter */
70 NET *s_net_return_head(NET * tail)
72 NET *n_current = NULL;
73 NET *ret_struct = NULL;
75 n_current = tail;
76 while (n_current != NULL) { /* goto end of list */
77 ret_struct = n_current;
78 n_current = n_current->prev;
81 return (ret_struct);
85 NET *s_net_add(NET * ptr)
87 NET *new_node;
89 new_node = (NET *) g_malloc(sizeof(NET));
91 /* setup node information */
92 new_node->net_name = NULL;
93 new_node->pin_label = NULL;
94 new_node->net_name_has_priority = FALSE;
95 new_node->nid = 0;
96 new_node->connected_to = NULL;
98 /* Setup link list stuff */
99 new_node->next = NULL;
101 if (ptr == NULL) {
102 new_node->prev = NULL; /* setup previous link */
103 return (new_node);
104 } else {
105 new_node->prev = ptr; /* setup previous link */
106 ptr->next = new_node;
107 return (ptr->next);
111 void s_net_print(NET * ptr)
113 NET *n_current = NULL;
115 n_current = ptr;
117 if (n_current == NULL) {
118 return;
121 while (n_current != NULL) {
123 if (n_current->nid != -1) {
125 #if DEBUG
126 if (n_current->net_name) {
127 printf(" %s [%d]\n", n_current->net_name, n_current->nid);
129 #endif
131 if (n_current->connected_to) {
132 printf(" %s [%d]\n", n_current->connected_to, n_current->nid);
136 n_current = n_current->next;
141 /* object being a pin */
142 char *s_net_return_connected_string(TOPLEVEL * pr_current, OBJECT * object,
143 char *hierarchy_tag)
145 OBJECT *o_current;
146 char *pinnum = NULL;
147 char *uref = NULL;
148 SCM scm_uref;
149 char *temp_uref = NULL;
150 char *string;
151 char *misc;
153 o_current = object;
155 pinnum = o_attrib_search_object_attribs_by_name (o_current, "pinnumber", 0);
157 #if DEBUG
158 printf("found pinnum: %s\n", pinnum);
159 #endif
161 scm_uref = g_scm_c_get_uref(pr_current, o_current->parent);
163 if (scm_is_string( scm_uref )) {
164 temp_uref = scm_to_utf8_string (scm_uref);
167 /* apply the hierarchy name to the uref */
168 uref = s_hierarchy_create_uref(pr_current, temp_uref, hierarchy_tag);
170 if (uref && pinnum) {
171 string = g_strdup_printf("%s %s", uref, pinnum);
172 } else {
173 if (pinnum) {
174 string = g_strdup_printf("POWER %s", pinnum);
175 } else {
176 if (hierarchy_tag) {
177 misc =
178 s_hierarchy_create_uref(pr_current, "U?",
179 hierarchy_tag);
180 string = g_strdup_printf("%s ?", misc);
181 g_free(misc);
182 } else {
183 string = g_strdup("U? ?");
186 fprintf(stderr, "Missing Attributes (refdes and pin number)\n");
190 g_free(pinnum);
192 g_free(uref);
194 g_free(temp_uref);
196 return (string);
199 int s_net_find(NET * net_head, NET * node)
201 NET *n_current;
203 n_current = net_head;
204 while (n_current != NULL) {
205 if (n_current->nid == node->nid) {
206 return (TRUE);
209 n_current = n_current->next;
211 return (FALSE);
214 char *s_net_name_search(TOPLEVEL * pr_current, NET * net_head)
216 NET *n_current;
217 char *name = NULL;
219 n_current = net_head;
222 while (n_current != NULL) {
224 if (n_current->net_name) {
226 if (name == NULL) {
228 name = n_current->net_name;
230 } else if (strcmp(name, n_current->net_name) != 0) {
233 #if DEBUG
234 fprintf(stderr, "Found a net with two names!\n");
235 fprintf(stderr, "Net called: [%s] and [%s]\n",
236 name, n_current->net_name);
237 #endif
240 /* only rename if this net name has priority */
241 /* AND, you are using net= attributes as the */
242 /* netnames which have priority */
243 if (pr_current->net_naming_priority == NETATTRIB_ATTRIBUTE) {
245 #if DEBUG
246 printf("\nNETATTRIB_ATTRIBUTE\n");
247 #endif
248 if (n_current->net_name_has_priority) {
250 #if DEBUG
251 fprintf(stderr, "Net is now called: [%s]\n",
252 n_current->net_name);
254 /* this show how to rename nets */
255 printf("\nRENAME all nets: %s -> %s\n", name,
256 n_current->net_name);
257 #endif
258 s_rename_add(name, n_current->net_name);
260 name = n_current->net_name;
262 } else {
264 #if DEBUG
265 printf
266 ("\nFound a net name called [%s], but it doesn't have priority\n",
267 n_current->net_name);
268 #endif
270 /* do the rename anyways, this might cause problems */
271 /* this will rename net which have the same label= */
272 if (!s_rename_search
273 (name, n_current->net_name, TRUE)) {
274 fprintf(stderr,
275 "Found duplicate net name, renaming [%s] to [%s]\n",
276 name, n_current->net_name);
277 s_rename_add(name, n_current->net_name);
278 name = n_current->net_name;
282 } else { /* NETNAME_ATTRIBUTE */
284 #if DEBUG
285 printf("\nNETNAME_ATTRIBUTE\n");
286 #endif
288 /* here we want to rename the net */
289 /* that has priority to the label */
290 /* name */
291 if (n_current->net_name_has_priority) {
293 #if DEBUG /* this shows how to rename nets */
294 printf("\nRENAME all nets: %s -> %s (priority)\n",
295 n_current->net_name, name);
296 #endif
298 s_rename_add(n_current->net_name, name);
300 } else {
302 #if DEBUG /* this shows how to rename nets */
303 printf
304 ("\nRENAME all nets: %s -> %s (not priority)\n",
305 name, n_current->net_name);
306 #endif
307 /* do the rename anyways, this might cause problems */
308 /* this will rename net which have the same label= */
309 if (!s_rename_search
310 (name, n_current->net_name, TRUE)) {
311 fprintf(stderr,
312 "Found duplicate net name, renaming [%s] to [%s]\n",
313 name, n_current->net_name);
315 s_rename_add(name, n_current->net_name);
316 name = n_current->net_name;
320 #if DEBUG
321 fprintf(stderr, "Net is now called: [%s]\n", name);
322 #endif
328 n_current = n_current->next;
331 if (name) {
332 return (name);
333 } else {
334 return (NULL);
338 char *s_net_name (TOPLEVEL * pr_current, NETLIST * netlist_head,
339 NET * net_head, char *hierarchy_tag, int type)
341 char *string = NULL;
342 NET *n_start;
343 NETLIST *nl_current;
344 CPINLIST *pl_current;
345 char *net_name = NULL;
346 int found = 0;
347 char *temp;
348 int *unnamed_counter;
349 char *unnamed_string;
351 net_name = s_net_name_search(pr_current, net_head);
353 if (net_name) {
354 return (net_name);
357 #if DEBUG
358 printf("didn't find named net\n");
359 #endif
361 /* didn't find a name */
362 /* go looking for another net which might have already been named */
363 /* ie you don't want to create a new unnamed net if the net has */
364 /* already been named */
365 nl_current = netlist_head;
366 while (nl_current != NULL) {
367 if (nl_current->cpins) {
368 pl_current = nl_current->cpins;
369 while (pl_current != NULL) {
370 if (pl_current->nets) {
371 n_start = pl_current->nets;
372 if (n_start->next && net_head->next) {
373 found = s_net_find(n_start->next, net_head->next);
375 if (found) {
376 net_name =
377 s_net_name_search(pr_current, n_start);
378 if (net_name) {
379 return (net_name);
386 pl_current = pl_current->next;
389 nl_current = nl_current->next;
393 #if DEBUG
394 printf("didn't find previously named\n");
395 #endif
397 /* AND we don't want to assign a dangling pin */
398 /* which is signified by having only a head node */
399 /* which is just a place holder */
400 /* and the head node shows up here */
402 if (net_head->nid == -1 && net_head->prev == NULL
403 && net_head->next == NULL) {
404 string = g_strdup_printf("unconnected_pin-%d",
405 unnamed_pin_counter++);
407 return (string);
411 switch (type) {
412 case PIN_TYPE_NET:
413 unnamed_counter = &unnamed_net_counter;
414 unnamed_string = pr_current->unnamed_netname;
415 break;
416 case PIN_TYPE_BUS:
417 unnamed_counter = &unnamed_bus_counter;
418 unnamed_string = pr_current->unnamed_busname;
419 break;
420 default:
421 g_critical ("Incorrect connectivity type %i in s_name_nets()\n", type);
422 return NULL;
425 /* have we exceeded the number of unnamed nets? */
426 if (*unnamed_counter < MAX_UNNAMED_NETS) {
428 if (netlist_mode == SPICE) {
429 string = g_strdup_printf("%d", (*unnamed_counter)++);
430 } else {
431 temp = g_strdup_printf ("%s%d", unnamed_string, (*unnamed_counter)++);
432 if (hierarchy_tag) {
433 string = s_hierarchy_create_netname (pr_current, temp, hierarchy_tag);
434 g_free (temp);
435 } else {
436 string = temp;
440 } else {
441 fprintf(stderr, "Increase number of unnamed nets (s_net.c)\n");
442 exit(-1);
445 return string;