Expose permanent debugging code to the C compiler.
[geda-gaf/berndj.git] / gnetlist / src / s_net.c
blob08a5d8a7c1fe302d4126c157402a81d0181bc32d
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_STRING_H
26 #include <string.h>
27 #endif
28 #ifdef HAVE_STDLIB_H
29 #include <stdlib.h>
30 #endif
31 #ifdef HAVE_ASSERT_H
32 #include <assert.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 static int unnamed_net_counter = 1;
45 static int unnamed_pin_counter = 1;
47 #define MAX_UNNAMED_NETS 99999999
48 #define MAX_UNNAMED_PINS 99999999
50 NET *s_net_add(NET * ptr)
52 NET *new_node;
54 new_node = (NET *) g_malloc(sizeof(NET));
56 /* setup node information */
57 new_node->net_name = NULL;
58 new_node->pin_label = NULL;
59 new_node->net_name_has_priority = FALSE;
60 new_node->nid = 0;
61 new_node->connected_to = NULL;
63 /* Setup link list stuff */
64 new_node->next = NULL;
66 if (ptr == NULL) {
67 new_node->prev = NULL; /* setup previous link */
68 return (new_node);
69 } else {
70 new_node->prev = ptr; /* setup previous link */
71 ptr->next = new_node;
72 return (ptr->next);
76 void s_net_print(NET * ptr)
78 NET *n_current = NULL;
80 n_current = ptr;
82 if (n_current == NULL) {
83 return;
86 while (n_current != NULL) {
87 if (n_current->nid != -1) {
88 if (GEDA_DEBUG) {
89 if (n_current->net_name) {
90 printf(" %s [%d]\n", n_current->net_name, n_current->nid);
94 if (n_current->connected_to) {
95 printf(" %s [%d]\n", n_current->connected_to, n_current->nid);
99 n_current = n_current->next;
104 /* object being a pin */
105 char *s_net_return_connected_string(TOPLEVEL * pr_current, OBJECT * object,
106 char const *hierarchy_tag)
108 OBJECT *o_current;
109 OBJECT *o_pinnum_object;
110 char *pinnum = NULL;
111 char *uref = NULL;
112 SCM scm_uref;
113 char *temp_uref = NULL;
114 char *string;
115 char *misc;
117 o_current = object;
119 /* this function only searches the single o_current */
120 pinnum = o_attrib_search_name_single(o_current, "pinnumber",
121 &o_pinnum_object);
123 if (GEDA_DEBUG) {
124 printf("found pinnum: %s\n", pinnum);
127 scm_uref = g_scm_c_get_uref(pr_current, o_current->complex_parent);
129 if (scm_is_string( scm_uref )) {
130 temp_uref = scm_to_locale_string( scm_uref );
133 /* apply the hierarchy name to the uref */
134 uref = s_hierarchy_create_uref(pr_current, temp_uref, hierarchy_tag);
136 if (uref && pinnum) {
137 string = g_strdup_printf("%s %s", uref, pinnum);
138 } else {
139 if (pinnum) {
140 string = g_strdup_printf("POWER %s", pinnum);
141 } else {
142 if (hierarchy_tag) {
143 misc =
144 s_hierarchy_create_uref(pr_current, "U?",
145 hierarchy_tag);
146 string = g_strdup_printf("%s ?", misc);
147 g_free(misc);
148 } else {
149 string = g_strdup("U? ?");
152 fprintf(stderr, "Missing Attributes (refdes and pin number)\n");
156 g_free(pinnum);
158 g_free(uref);
160 g_free(temp_uref);
162 return (string);
165 int s_net_find(NET * net_head, NET * node)
167 NET *n_current;
169 n_current = net_head;
170 while (n_current != NULL) {
171 if (n_current->nid == node->nid) {
172 return (TRUE);
175 n_current = n_current->next;
177 return (FALSE);
180 char *s_net_name_search(TOPLEVEL * pr_current, NET * net_head)
182 NET *n_current;
183 char *name = NULL;
185 n_current = net_head;
187 while (n_current != NULL) {
188 if (n_current->net_name) {
189 if (name == NULL) {
190 name = n_current->net_name;
191 } else if (strcmp(name, n_current->net_name) != 0) {
192 if (GEDA_DEBUG) {
193 fprintf(stderr, "Found a net with two names!\n");
194 fprintf(stderr, "Net called: [%s] and [%s]\n",
195 name, n_current->net_name);
198 /* only rename if this net name has priority */
199 /* AND, you are using net= attributes as the */
200 /* netnames which have priority */
201 if (pr_current->net_naming_priority == NETATTRIB_ATTRIBUTE) {
202 if (GEDA_DEBUG) {
203 printf("\nNETATTRIB_ATTRIBUTE\n");
205 if (n_current->net_name_has_priority) {
206 if (GEDA_DEBUG) {
207 fprintf(stderr, "Net is now called: [%s]\n",
208 n_current->net_name);
210 /* this show how to rename nets */
211 printf("\nRENAME all nets: %s -> %s\n", name,
212 n_current->net_name);
214 s_rename_add(name, n_current->net_name);
216 name = n_current->net_name;
217 } else {
218 if (GEDA_DEBUG) {
219 printf
220 ("\nFound a net name called [%s], but it doesn't have priority\n",
221 n_current->net_name);
224 /* do the rename anyways, this might cause problems */
225 /* this will rename net which have the same label= */
226 if (!s_rename_search
227 (name, n_current->net_name, TRUE)) {
228 fprintf(stderr,
229 "Found duplicate net name, renaming [%s] to [%s]\n",
230 name, n_current->net_name);
231 s_rename_add(name, n_current->net_name);
232 name = n_current->net_name;
235 } else { /* NETNAME_ATTRIBUTE */
236 if (GEDA_DEBUG) {
237 printf("\nNETNAME_ATTRIBUTE\n");
240 /* here we want to rename the net */
241 /* that has priority to the label */
242 /* name */
243 if (n_current->net_name_has_priority) {
244 if (GEDA_DEBUG) {
245 /* this shows how to rename nets */
246 printf("\nRENAME all nets: %s -> %s (priority)\n",
247 n_current->net_name, name);
250 s_rename_add(n_current->net_name, name);
251 } else {
252 if (GEDA_DEBUG) {
253 /* this shows how to rename nets */
254 printf
255 ("\nRENAME all nets: %s -> %s (not priority)\n",
256 name, n_current->net_name);
258 /* do the rename anyways, this might cause problems */
259 /* this will rename net which have the same label= */
260 if (!s_rename_search
261 (name, n_current->net_name, TRUE)) {
262 fprintf(stderr,
263 "Found duplicate net name, renaming [%s] to [%s]\n",
264 name, n_current->net_name);
266 s_rename_add(name, n_current->net_name);
267 name = n_current->net_name;
271 if (GEDA_DEBUG) {
272 fprintf(stderr, "Net is now called: [%s]\n", name);
278 n_current = n_current->next;
281 if (name) {
282 return (name);
283 } else {
284 return (NULL);
288 char *s_net_name(TOPLEVEL *pr_current, NETLIST *netlist,
289 NET * net_head, char const *hierarchy_tag)
291 char *string;
292 NET *n_start;
293 NETLIST *nl_current;
294 CPINLIST *pl_current;
295 char *net_name = NULL;
296 int found = 0;
297 char *temp;
298 char *misc;
300 net_name = s_net_name_search(pr_current, net_head);
302 if (net_name) {
303 return (net_name);
306 if (GEDA_DEBUG) {
307 printf("didn't find named net\n");
310 /* didn't find a name */
311 /* go looking for another net which might have already been named */
312 /* ie you don't want to create a new unnamed net if the net has */
313 /* already been named */
314 nl_current = netlist;
315 while (nl_current != NULL) {
316 if (nl_current->cpins) {
317 pl_current = nl_current->cpins;
318 while (pl_current != NULL) {
319 if (pl_current->nets) {
320 n_start = pl_current->nets;
321 if (n_start->next && net_head->next) {
322 found = s_net_find(n_start->next, net_head->next);
324 if (found) {
325 net_name =
326 s_net_name_search(pr_current, n_start);
327 if (net_name) {
328 return (net_name);
335 pl_current = pl_current->next;
338 nl_current = nl_current->next;
341 if (GEDA_DEBUG) {
342 printf("didn't find previously named\n");
345 /* AND we don't want to assign a dangling pin */
346 /* which is signified by having only a head node */
347 /* which is just a place holder */
348 /* and the head node shows up here */
350 if (net_head->nid == -1 && net_head->prev == NULL
351 && net_head->next == NULL) {
352 string = g_strdup_printf("unconnected_pin-%d",
353 unnamed_pin_counter++);
355 return (string);
359 /* have we exceeded the number of unnamed nets? */
360 if (unnamed_net_counter < MAX_UNNAMED_NETS) {
362 if (netlist_mode == SPICE) {
363 string =
364 g_strdup_printf("%d", unnamed_net_counter++);
366 return (string);
367 } else {
368 if (hierarchy_tag) {
369 temp = g_strdup_printf("%s%d", pr_current->unnamed_netname,
370 unnamed_net_counter++);
372 misc =
373 s_hierarchy_create_netname(pr_current, temp,
374 hierarchy_tag);
375 string = g_strdup(misc);
376 g_free(misc);
377 } else {
378 string = g_strdup_printf("%s%d", pr_current->unnamed_netname,
379 unnamed_net_counter++);
382 return (string);
385 } else {
386 fprintf(stderr, "Increase number of unnamed nets (s_net.c)\n");
387 exit(-1);
390 return (NULL);