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
41 #include <libgeda/libgeda.h>
43 #include "../include/globals.h"
44 #include "../include/prototype.h"
45 #include "../include/gettext.h"
55 RENAME
* first_rename
;
59 static SET
* first_set
= NULL
;
60 static SET
* last_set
= NULL
;
62 void s_rename_init(void)
66 fprintf(stderr
, _("ERROR: Overwriting a valid rename list.\n"));
71 void s_rename_destroy_all(void)
78 for (temp
= first_set
->first_rename
; temp
;)
87 first_set
= first_set
->next_set
;
93 void s_rename_next_set(void)
97 new_set
= g_malloc(sizeof(SET
));
98 memset(new_set
,0,sizeof(SET
));
101 last_set
->next_set
= new_set
;
106 first_set
= last_set
= new_set
;
110 void s_rename_print(void)
113 RENAME
* temp_rename
;
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
)
135 for (temp
= last_set
->first_rename
; temp
; temp
= temp
->next
)
137 if (strcmp(src
, temp
->src
) == 0)
142 if (strcmp(dest
, temp
->src
) == 0)
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"));
156 static void s_rename_add_lowlevel (const char *src
, const char *dest
)
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
;
176 last_set
->last_rename
->next
= new_rename
;
177 last_set
->last_rename
= new_rename
;
181 void s_rename_add(char *src
, char *dest
)
189 if (src
== NULL
|| dest
== NULL
)
194 flag
= s_rename_search(src
, dest
, FALSE
);
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!
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
);
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!
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
);
229 s_rename_add_lowlevel(dest
, temp
->dest
);
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
;
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
)
300 for (temp
= last_set
->first_rename
; temp
; temp
= temp
->next
)
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
;
314 RENAME
* temp_rename
;
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
),
326 outerlist
= scm_cons (pairlist
, outerlist
);