add KrnMayGetChar implementation
[AROS.git] / tools / MetaMake / var.c
blob84237f49505db95a74ac0753b3c623babfb35875
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"
35 /* Functions */
36 char *
37 getvar (struct List * varlist, const char * varname)
39 static char buffer[256];
40 char *env_val;
41 struct Var * var = FindNode (varlist, varname);
43 if (var)
44 return var->value;
46 env_val = getenv(varname);
47 if(env_val)
49 return env_val;
51 sprintf (buffer, "?$(%s)", varname);
52 return buffer;
55 char *
56 substvars (struct List * varlist, const char * str)
58 static char buffer[4096];
59 char varname[256];
60 const char * src;
61 char * dest, * vptr;
63 assert (str);
65 src = str;
66 dest = buffer;
68 while (*src)
70 if (*src == '$')
72 src += 2;
73 vptr = varname;
75 while (*src && *src != ')')
77 *vptr ++ = *src ++;
79 if (*src)
80 src ++;
82 *vptr = 0;
84 strcpy (dest, getvar (varlist, varname));
85 dest += strlen (dest);
87 else
88 *dest ++ = *src ++;
90 assert (dest<buffer+sizeof(buffer));
93 *dest = 0;
95 return buffer;
98 void
99 setvar (struct List * varlist, const char * name, const char * val)
101 struct Var * var;
103 assert (name);
105 #if 0
106 printf ("assign %s=%s\n", name, val);
107 #endif
109 var = addnodeoncesize (varlist, name, sizeof(struct Var));
110 SETSTR (var->value, val);
112 #if 0
113 printf ("vars=");
114 printvarlist (varlist);
115 #endif
118 void
119 printvarlist (struct List * l)
121 struct Var * n;
123 ForeachNode (l,n)
125 printf (" %s=%s\n", n->node.name, n->value);
129 void
130 freevarlist (struct List * l)
132 struct Var * node, * next;
134 ForeachNodeSafe(l,node,next)
136 Remove (node);
138 xfree (node->node.name);
139 cfree (node->value);
140 xfree (node);
144 char **
145 getargs (const char * line, int * argc, struct List * vars)
147 static char * argv[256];
148 static char * buffer = NULL;
149 char * src;
150 int arg;
152 cfree (buffer);
153 buffer = NULL;
155 if (!line)
156 return NULL;
158 if (vars)
159 buffer = xstrdup (substvars (vars, line));
160 else
161 buffer = xstrdup (line);
163 assert (buffer);
165 src = buffer;
166 arg = 0;
168 while (*src)
170 while (isspace (*src))
171 src ++;
173 if (!*src)
174 break;
176 assert (arg < 255);
177 argv[arg++] = src;
179 if (*src == '"')
181 while (*src && *src != '"')
182 src ++;
184 else
186 while (*src && !isspace (*src))
187 src ++;
190 if (*src)
191 *src++ = 0;
194 argv[arg] = NULL;
196 if (argc)
197 *argc = arg;
199 return argv;