Add a mark at simulation end for vcd/lxt/lxt2 files.
[iverilog.git] / driver / substit.c
blob0b9ef84d5ccf23ea0b8d23d8cddbc432cfb882a8
1 /*
2 * Copyright (c) 2002 Stephen Williams (steve@icarus.com)
4 * This source code is free software; you can redistribute it
5 * and/or modify it in source code form under the terms of the GNU
6 * General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 #ifdef HAVE_CVS_IDENT
20 #ident "$Id: substit.c,v 1.5 2003/12/19 01:27:10 steve Exp $"
21 #endif
23 # include <string.h>
24 # include <stdlib.h>
25 #ifdef HAVE_MALLOC_H
26 # include <malloc.h>
27 #endif
30 char* substitutions(const char*str)
32 size_t nbuf = strlen(str) + 1;
33 char*buf = malloc(nbuf);
34 char*cp = buf;
36 while (*str) {
38 if ((str[0] == '$') && (str[1] == '(')) {
39 /* If I find a $(x) string in the source, replace
40 it in the destination with the contents of the
41 environment variable x. */
42 char*name;
43 char*value;
44 const char*ep = strchr(str, ')');
45 str += 2;
47 name = malloc(ep-str+1);
48 strncpy(name, str, ep-str);
49 name[ep-str] = 0;
51 str = ep + 1;
53 value = getenv(name);
54 free(name);
55 if (value == 0)
56 continue;
58 if (strlen(value) >= (nbuf - (cp-buf))) {
59 size_t old_size = cp - buf;
60 nbuf = (cp - buf) + strlen(value) + 1;
61 buf = realloc(buf, nbuf);
62 cp = buf + old_size;
65 strcpy(cp, value);
66 cp += strlen(cp);
68 } else {
69 if ( cp == (buf + nbuf) ) {
70 size_t old_size = nbuf;
71 nbuf = old_size + 32;
72 buf = realloc(buf, nbuf);
73 cp = buf + old_size;
76 *cp++ = *str++;
80 /* Add the trailing nul to the string, and reallocate the
81 buffer to be a tight fit. */
82 if ( cp == (buf + nbuf) ) {
83 size_t old_size = nbuf;
84 nbuf = old_size + 1;
85 buf = realloc(buf, nbuf);
86 buf[old_size] = 0;
87 } else {
88 *cp++ = 0;
89 nbuf = cp - buf;
90 buf = realloc(buf, nbuf);
93 return buf;
98 * $Log: substit.c,v $
99 * Revision 1.5 2003/12/19 01:27:10 steve
100 * Fix various unsigned compare warnings.
102 * Revision 1.4 2002/08/12 01:35:01 steve
103 * conditional ident string using autoconfig.
105 * Revision 1.3 2002/08/11 23:47:04 steve
106 * Add missing Log and Ident strings.
108 * Revision 1.2 2002/06/25 01:33:01 steve
109 * include malloc.h only when available.
111 * Revision 1.1 2002/06/23 20:10:51 steve
112 * Variable substitution in command files.