Added Hebrew keymap.
[AROS.git] / tools / MetaMake / var.c
blob900e7c3824e8bf0017492bf056e80f5678b5b6cc
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 (!strncmp(varname, "OPT_", 4))
54 buffer[0] = '\0';
55 else
57 if (!quiet)
58 printf("[MMAKE] Variable %s doesn't exist\n", varname);
59 sprintf (buffer, "?$(%s)", varname);
61 return buffer;
64 char *
65 substvars (struct List * varlist, const char * str)
67 static char buffer[4096];
68 char varname[256];
69 const char * src;
70 char * dest, * vptr;
72 assert (str);
74 src = str;
75 dest = buffer;
77 while (*src)
79 if (*src == '$')
81 src += 2;
82 vptr = varname;
84 while (*src && *src != ')')
86 *vptr ++ = *src ++;
88 if (*src)
89 src ++;
91 *vptr = 0;
93 strcpy (dest, getvar (varlist, varname));
94 dest += strlen (dest);
96 else
97 *dest ++ = *src ++;
99 assert (dest<buffer+sizeof(buffer));
102 *dest = 0;
104 return buffer;
107 void
108 setvar (struct List * varlist, const char * name, const char * val)
110 struct Var * var;
112 assert (name);
114 #if 0
115 printf ("assign %s=%s\n", name, val);
116 #endif
118 var = addnodeoncesize (varlist, name, sizeof(struct Var));
119 SETSTR (var->value, val);
121 #if 0
122 printf ("vars=");
123 printvarlist (varlist);
124 #endif
127 void
128 printvarlist (struct List * l)
130 struct Var * n;
132 ForeachNode (l,n)
134 printf (" %s=%s\n", n->node.name, n->value);
138 void
139 freevarlist (struct List * l)
141 struct Var * node, * next;
143 ForeachNodeSafe(l,node,next)
145 Remove (node);
147 xfree (node->node.name);
148 cfree (node->value);
149 xfree (node);
153 char **
154 getargs (const char * line, int * argc, struct List * vars)
156 static char * argv[256];
157 static char * buffer = NULL;
158 char * src;
159 int arg;
161 cfree (buffer);
162 buffer = NULL;
164 if (!line)
165 return NULL;
167 if (vars)
168 buffer = xstrdup (substvars (vars, line));
169 else
170 buffer = xstrdup (line);
172 assert (buffer);
174 src = buffer;
175 arg = 0;
177 while (*src)
179 while (isspace (*src))
180 src ++;
182 if (!*src)
183 break;
185 assert (arg < 255);
186 argv[arg++] = src;
188 if (*src == '"')
190 while (*src && *src != '"')
191 src ++;
193 else
195 while (*src && !isspace (*src))
196 src ++;
199 if (*src)
200 *src++ = 0;
203 argv[arg] = NULL;
205 if (argc)
206 *argc = arg;
208 return argv;