Indentation fix, cleanup.
[AROS.git] / tools / MetaMake / var.c
blob15669b0f83708ff7e19b753b72acbb9bb9a23578
1 /* MetaMake - A Make extension
2 Copyright © 1995-2004, The AROS Development Team. All rights reserved.
4 This file is part of MetaMake.
6 MetaMake 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, or (at your option)
9 any later version.
11 MetaMake 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 GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "config.h"
23 #include <assert.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #ifdef HAVE_STRING_H
27 # include <string.h>
28 #else
29 # include <strings.h>
30 #endif
32 #include "var.h"
33 #include "mem.h"
34 #include "mmake.h"
37 /* Functions */
38 char *
39 getvar (struct List * varlist, const char * varname)
41 static char buffer[256];
42 char *env_val;
43 struct Var * var = FindNode (varlist, varname);
45 if (var)
46 return var->value;
48 env_val = getenv(varname);
49 if(env_val)
51 return env_val;
53 if (verbose)
54 printf("[MMAKE] Variable %s doesn't exist\n", varname);
55 sprintf (buffer, "?$(%s)", varname);
57 return buffer;
60 char *
61 substvars (struct List * varlist, const char * str)
63 static char buffer[4096];
64 char varname[256];
65 const char * src;
66 char * dest, * vptr;
68 assert (str);
70 src = str;
71 dest = buffer;
73 while (*src)
75 if (*src == '$')
77 src += 2;
78 vptr = varname;
80 while (*src && *src != ')')
82 *vptr ++ = *src ++;
84 if (*src)
85 src ++;
87 *vptr = 0;
89 strcpy (dest, getvar (varlist, varname));
90 dest += strlen (dest);
92 else
93 *dest ++ = *src ++;
95 assert (dest<buffer+sizeof(buffer));
98 *dest = 0;
100 return buffer;
103 void
104 setvar (struct List * varlist, const char * name, const char * val)
106 struct Var * var;
108 assert (name);
110 #if 0
111 printf ("assign %s=%s\n", name, val);
112 #endif
114 var = addnodeoncesize (varlist, name, sizeof(struct Var));
115 SETSTR (var->value, val);
117 #if 0
118 printf ("vars=");
119 printvarlist (varlist);
120 #endif
123 void
124 printvarlist (struct List * l)
126 struct Var * n;
128 ForeachNode (l,n)
130 printf (" %s=%s\n", n->node.name, n->value);
134 void
135 freevarlist (struct List * l)
137 struct Var * node, * next;
139 ForeachNodeSafe(l,node,next)
141 Remove (node);
143 xfree (node->node.name);
144 cfree (node->value);
145 xfree (node);
149 char **
150 getargs (const char * line, int * argc, struct List * vars)
152 static char * argv[256];
153 static char * buffer = NULL;
154 char * src;
155 int arg;
157 cfree (buffer);
158 buffer = NULL;
160 if (!line)
161 return NULL;
163 if (vars)
164 buffer = xstrdup (substvars (vars, line));
165 else
166 buffer = xstrdup (line);
168 assert (buffer);
170 src = buffer;
171 arg = 0;
173 while (*src)
175 while (isspace (*src))
176 src ++;
178 if (!*src)
179 break;
181 assert (arg < 255);
182 argv[arg++] = src;
184 if (*src == '"')
186 while (*src && *src != '"')
187 src ++;
189 else
191 while (*src && !isspace (*src))
192 src ++;
195 if (*src)
196 *src++ = 0;
199 argv[arg] = NULL;
201 if (argc)
202 *argc = arg;
204 return argv;