Use "pngdt" as the base name for png.datatype's include files.
[AROS.git] / tools / MetaMake / var.c
blobf9a1f88ca135ebf8d11de44499f4eacf7b15341f
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 (!quiet)
54 printf("[MMAKE] Variable %s doesn't exist\n", varname);
55 sprintf (buffer, "?$(%s)", varname);
56 return buffer;
59 char *
60 substvars (struct List * varlist, const char * str)
62 static char buffer[4096];
63 char varname[256];
64 const char * src;
65 char * dest, * vptr;
67 assert (str);
69 src = str;
70 dest = buffer;
72 while (*src)
74 if (*src == '$')
76 src += 2;
77 vptr = varname;
79 while (*src && *src != ')')
81 *vptr ++ = *src ++;
83 if (*src)
84 src ++;
86 *vptr = 0;
88 strcpy (dest, getvar (varlist, varname));
89 dest += strlen (dest);
91 else
92 *dest ++ = *src ++;
94 assert (dest<buffer+sizeof(buffer));
97 *dest = 0;
99 return buffer;
102 void
103 setvar (struct List * varlist, const char * name, const char * val)
105 struct Var * var;
107 assert (name);
109 #if 0
110 printf ("assign %s=%s\n", name, val);
111 #endif
113 var = addnodeoncesize (varlist, name, sizeof(struct Var));
114 SETSTR (var->value, val);
116 #if 0
117 printf ("vars=");
118 printvarlist (varlist);
119 #endif
122 void
123 printvarlist (struct List * l)
125 struct Var * n;
127 ForeachNode (l,n)
129 printf (" %s=%s\n", n->node.name, n->value);
133 void
134 freevarlist (struct List * l)
136 struct Var * node, * next;
138 ForeachNodeSafe(l,node,next)
140 Remove (node);
142 xfree (node->node.name);
143 cfree (node->value);
144 xfree (node);
148 char **
149 getargs (const char * line, int * argc, struct List * vars)
151 static char * argv[256];
152 static char * buffer = NULL;
153 char * src;
154 int arg;
156 cfree (buffer);
157 buffer = NULL;
159 if (!line)
160 return NULL;
162 if (vars)
163 buffer = xstrdup (substvars (vars, line));
164 else
165 buffer = xstrdup (line);
167 assert (buffer);
169 src = buffer;
170 arg = 0;
172 while (*src)
174 while (isspace (*src))
175 src ++;
177 if (!*src)
178 break;
180 assert (arg < 255);
181 argv[arg++] = src;
183 if (*src == '"')
185 while (*src && *src != '"')
186 src ++;
188 else
190 while (*src && !isspace (*src))
191 src ++;
194 if (*src)
195 *src++ = 0;
198 argv[arg] = NULL;
200 if (argc)
201 *argc = arg;
203 return argv;