gschem: Fix `may be used uninitialized' warning
[geda-gaf.git] / gnetlist-legacy / src / s_rename.c
blob37bfe4e3960b4677b2d4bc5a9f842db793cd95b5
1 /* gEDA - GPL Electronic Design Automation
2 * gnetlist - gEDA Netlist
3 * Copyright (C) 1998-2010 Ales Hvezda
4 * Copyright (C) 1998-2019 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
22 * 2005/05/02 Almost totally reimplemented to support dynamic allocation.
24 * Changes are Copyright (C) 2005 Carlos A. R. Azevedo
27 #include <config.h>
29 #include <stdio.h>
30 #include <ctype.h>
31 #ifdef HAVE_STRING_H
32 #include <string.h>
33 #endif
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37 #ifdef HAVE_ASSERT_H
38 #include <assert.h>
39 #endif
41 #include <libgeda/libgeda.h>
43 #include "../include/globals.h"
44 #include "../include/prototype.h"
45 #include "../include/gettext.h"
47 typedef struct {
48 void * next;
49 char * src;
50 char * dest;
51 } RENAME;
53 typedef struct {
54 void * next_set;
55 RENAME * first_rename;
56 RENAME * last_rename;
57 } SET;
59 static SET * first_set = NULL;
60 static SET * last_set = NULL;
62 void s_rename_init(void)
64 if (first_set)
66 fprintf(stderr, _("ERROR: Overwriting a valid rename list.\n"));
67 exit(-1);
71 void s_rename_destroy_all(void)
73 RENAME * temp;
74 void * to_free;
76 for (; first_set;)
78 for (temp = first_set->first_rename; temp;)
80 g_free(temp->src);
81 g_free(temp->dest);
82 to_free = temp;
83 temp = temp->next;
84 g_free(to_free);
86 to_free = first_set;
87 first_set = first_set->next_set;
88 g_free(to_free);
90 last_set = NULL;
93 void s_rename_next_set(void)
95 SET * new_set;
97 new_set = g_malloc(sizeof(SET));
98 memset(new_set,0,sizeof(SET));
99 if (first_set)
101 last_set->next_set = new_set;
102 last_set = new_set;
104 else
106 first_set = last_set = new_set;
110 void s_rename_print(void)
112 SET * temp_set;
113 RENAME * temp_rename;
114 int i;
116 for (i = 0, temp_set = first_set; temp_set; temp_set = temp_set->next_set, i++)
118 for (temp_rename = temp_set->first_rename; temp_rename; temp_rename = temp_rename->next)
120 printf("%d) Source: _%s_", i, temp_rename->src);
121 printf(" -> Dest: _%s_\n", temp_rename->dest);
126 /* if the src is found, return true */
127 /* if the dest is found, also return true, but warn user */
128 /* If quiet_flag is true than don't print anything */
129 int s_rename_search(char *src, char *dest, int quiet_flag)
131 RENAME * temp;
133 if (last_set)
135 for (temp = last_set->first_rename; temp; temp = temp->next)
137 if (strcmp(src, temp->src) == 0)
139 return (TRUE);
142 if (strcmp(dest, temp->src) == 0)
144 if (!quiet_flag)
146 fprintf(stderr, _("WARNING: Trying to rename something twice:\n\t%s and %s\nare both a src and dest name\n"), dest, temp->src);
147 fprintf(stderr, _("This warning is okay if you have multiple levels of hierarchy!\n"));
149 return (TRUE);
153 return (FALSE);
156 static void s_rename_add_lowlevel (const char *src, const char *dest)
158 RENAME *new_rename;
160 g_return_if_fail(last_set != NULL);
162 new_rename = g_malloc(sizeof (RENAME));
164 g_return_if_fail(new_rename != NULL);
166 new_rename->next = NULL;
167 new_rename->src = g_strdup(src);
168 new_rename->dest = g_strdup(dest);
170 if (last_set->first_rename == NULL)
172 last_set->first_rename = last_set->last_rename = new_rename;
174 else
176 last_set->last_rename->next = new_rename;
177 last_set->last_rename = new_rename;
181 void s_rename_add(char *src, char *dest)
183 int flag;
184 RENAME * last;
185 RENAME * temp;
186 RENAME * new_rename;
187 SET * new_set;
189 if (src == NULL || dest == NULL)
191 return;
194 flag = s_rename_search(src, dest, FALSE);
196 if (flag)
198 /* If found follow the original behaviour, limiting the operation to the current end-of-list */
199 last = last_set->last_rename;
200 for (temp = last_set->first_rename; ; temp = temp->next)
202 if ((strcmp(dest, temp->src) == 0)
203 && (strcmp(src, temp->dest) != 0))
205 /* we found a -> b, while adding c -> a.
206 * hence we would have c -> a -> b, so add c -> b.
207 * avoid renaming if b is same as c!
209 #if DEBUG
210 printf("Found dest [%s] in src [%s] and that had a dest as: [%s]\n"
211 "So you want rename [%s] to [%s]\n",
212 dest, temp->src, temp->dest, src, temp->dest);
213 #endif
214 s_rename_add_lowlevel(src, temp->dest);
217 else if ((strcmp(src, temp->src) == 0)
218 && (strcmp(dest, temp->dest) != 0))
220 /* we found a -> b, while adding a -> c.
221 * hence b <==> c, so add c -> b.
222 * avoid renaming if b is same as c!
224 #if DEBUG
225 printf("Found src [%s] that had a dest as: [%s]\n"
226 "Unify nets by renaming [%s] to [%s]\n",
227 src, temp->dest, dest, temp->dest);
228 #endif
229 s_rename_add_lowlevel(dest, temp->dest);
231 if (temp == last)
233 break;
237 else
239 /* Check for a valid set */
240 if (first_set == NULL)
242 new_set = g_malloc(sizeof(SET));
243 memset(new_set,0,sizeof(SET));
244 first_set = last_set = new_set;
246 new_rename = g_malloc(sizeof(RENAME));
247 new_rename->next = NULL;
248 new_rename->src = g_strdup(src);
249 new_rename->dest = g_strdup(dest);
250 if (last_set->first_rename == NULL)
252 last_set->first_rename = last_set->last_rename = new_rename;
254 else
256 last_set->last_rename->next = new_rename;
257 last_set->last_rename = new_rename;
262 void s_rename_all_lowlevel(NETLIST * netlist_head, char *src, char *dest)
264 NETLIST *nl_current = NULL;
265 CPINLIST *pl_current;
267 nl_current = netlist_head;
269 while (nl_current != NULL)
271 if (nl_current->cpins)
273 pl_current = nl_current->cpins;
274 while (pl_current != NULL)
276 if (pl_current->net_name != NULL)
278 if (strcmp(pl_current->net_name, src) == 0)
280 pl_current->net_name = g_strdup(dest);
283 pl_current = pl_current->next;
286 nl_current = nl_current->next;
290 void s_rename_all(TOPLEVEL * pr_current, NETLIST * netlist_head)
292 RENAME * temp;
294 #if DEBUG
295 s_rename_print();
296 #endif
298 if (last_set)
300 for (temp = last_set->first_rename; temp; temp = temp->next)
302 verbose_print("R");
303 s_rename_all_lowlevel(netlist_head, temp->src, temp->dest);
309 SCM g_get_renamed_nets(SCM scm_level)
311 SCM pairlist = SCM_EOL;
312 SCM outerlist = SCM_EOL;
313 SET * temp_set;
314 RENAME * temp_rename;
315 char *level;
317 level = scm_to_utf8_string (scm_level);
319 for (temp_set = first_set; temp_set; temp_set = temp_set->next_set)
321 for (temp_rename = temp_set->first_rename; temp_rename; temp_rename = temp_rename->next)
323 pairlist = scm_list_n (scm_from_utf8_string (temp_rename->src),
324 scm_from_utf8_string (temp_rename->dest),
325 SCM_UNDEFINED);
326 outerlist = scm_cons (pairlist, outerlist);
330 free (level);
331 return (outerlist);