-
Notifications
You must be signed in to change notification settings - Fork 7
/
inifile.c
332 lines (292 loc) · 9.14 KB
/
inifile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Some portions copyright (c) 1994-1995 by Symantec
* Copyright (c) 1999-2011 by Digital Mars
* All Rights Reserved
* http://www.digitalmars.com
* Written by Walter Bright
*
* This source file is made available for personal use
* only. The license is in /dmd/src/dmd/backendlicense.txt
* For any other uses, please contact Digital Mars.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#if _WIN32
#include <windows.h>
#endif
#if __APPLE__
#include <sys/syslimits.h>
#endif
#if __FreeBSD__ || __OpenBSD__ || __sun&&__SVR4
// for PATH_MAX
#include <limits.h>
#endif
#if __sun&&__SVR4
#include <alloca.h>
#endif
#include "root.h"
#include "rmem.h"
#define LOG 0
char *skipspace(const char *p);
#if __GNUC__
char *strupr(char *s)
{
char *t = s;
while (*s)
{
*s = toupper(*s);
s++;
}
return t;
}
#endif
/*****************************
* Read and analyze .ini file.
* Input:
* argv0 program name (argv[0])
* inifile .ini file name
* Returns:
* file name of ini file
* Note: this is a memory leak
*/
const char *inifile(const char *argv0x, const char *inifilex)
{
char *argv0 = (char *)argv0x;
char *inifile = (char *)inifilex; // do const-correct later
char *path; // need path for @P macro
char *filename;
OutBuffer buf;
int envsection = 0;
#if LOG
printf("inifile(argv0 = '%s', inifile = '%s')\n", argv0, inifile);
#endif
if (FileName::absolute(inifile))
{
filename = inifile;
}
else
{
/* Look for inifile in the following sequence of places:
* o current directory
* o home directory
* o directory off of argv0
* o /etc/
*/
if (FileName::exists(inifile))
{
filename = inifile;
}
else
{
filename = FileName::combine(getenv("HOME"), inifile);
if (!FileName::exists(filename))
{
#if _WIN32 // This fix by Tim Matthews
char resolved_name[MAX_PATH + 1];
if(GetModuleFileName(NULL, resolved_name, MAX_PATH + 1) && FileName::exists(resolved_name))
{
filename = (char *)FileName::replaceName(resolved_name, inifile);
if(FileName::exists(filename))
goto Ldone;
}
#endif
filename = (char *)FileName::replaceName(argv0, inifile);
if (!FileName::exists(filename))
{
#if linux || __APPLE__ || __FreeBSD__ || __OpenBSD__ || __sun&&__SVR4
#if __GLIBC__ || __APPLE__ || __FreeBSD__ || __OpenBSD__ || __sun&&__SVR4 // This fix by Thomas Kuehne
/* argv0 might be a symbolic link,
* so try again looking past it to the real path
*/
#if __APPLE__ || __FreeBSD__ || __OpenBSD__ || __sun&&__SVR4
char resolved_name[PATH_MAX + 1];
char* real_argv0 = realpath(argv0, resolved_name);
#else
char* real_argv0 = realpath(argv0, NULL);
#endif
//printf("argv0 = %s, real_argv0 = %p\n", argv0, real_argv0);
if (real_argv0)
{
filename = (char *)FileName::replaceName(real_argv0, inifile);
#if linux
free(real_argv0);
#endif
if (FileName::exists(filename))
goto Ldone;
}
#else
#error use of glibc non-standard extension realpath(char*, NULL)
#endif
if (1){
// Search PATH for argv0
const char *p = getenv("PATH");
#if LOG
printf("\tPATH='%s'\n", p);
#endif
Strings *paths = FileName::splitPath(p);
filename = FileName::searchPath(paths, argv0, 0);
if (!filename)
goto Letc; // argv0 not found on path
filename = (char *)FileName::replaceName(filename, inifile);
if (FileName::exists(filename))
goto Ldone;
}
// Search /etc/ for inifile
Letc:
#endif
filename = FileName::combine((char *)"/etc/", inifile);
Ldone:
;
}
}
}
}
path = FileName::path(filename);
#if LOG
printf("\tpath = '%s', filename = '%s'\n", path, filename);
#endif
File file(filename);
if (file.read())
return filename; // error reading file
// Parse into lines
int eof = 0;
for (size_t i = 0; i < file.len && !eof; i++)
{
size_t linestart = i;
for (; i < file.len; i++)
{
switch (file.buffer[i])
{
case '\r':
break;
case '\n':
// Skip if it was preceded by '\r'
if (i && file.buffer[i - 1] == '\r')
goto Lskip;
break;
case 0:
case 0x1A:
eof = 1;
break;
default:
continue;
}
break;
}
// The line is file.buffer[linestart..i]
char *line;
size_t len;
char *p;
char *pn;
line = (char *)&file.buffer[linestart];
len = i - linestart;
buf.reset();
// First, expand the macros.
// Macros are bracketed by % characters.
for (size_t k = 0; k < len; k++)
{
if (line[k] == '%')
{
for (size_t j = k + 1; j < len; j++)
{
if (line[j] == '%')
{
if (j - k == 3 && memicmp(&line[k + 1], "@P", 2) == 0)
{
// %@P% is special meaning the path to the .ini file
p = path;
if (!*p)
p = (char *)".";
}
else
{ size_t len2 = j - k;
char tmp[10]; // big enough most of the time
if (len2 <= sizeof(tmp))
p = tmp;
else
p = (char *)alloca(len2);
len2--;
memcpy(p, &line[k + 1], len2);
p[len2] = 0;
strupr(p);
p = getenv(p);
if (!p)
p = (char *)"";
}
buf.writestring(p);
k = j;
goto L1;
}
}
}
buf.writeByte(line[k]);
L1:
;
}
// Remove trailing spaces
while (buf.offset && isspace(buf.data[buf.offset - 1]))
buf.offset--;
p = buf.toChars();
// The expanded line is in p.
// Now parse it for meaning.
p = skipspace(p);
switch (*p)
{
case ';': // comment
case 0: // blank
break;
case '[': // look for [Environment]
p = skipspace(p + 1);
for (pn = p; isalnum((unsigned char)*pn); pn++)
;
if (pn - p == 11 &&
memicmp(p, "Environment", 11) == 0 &&
*skipspace(pn) == ']'
)
envsection = 1;
else
envsection = 0;
break;
default:
if (envsection)
{
pn = p;
// Convert name to upper case;
// remove spaces bracketing =
for (p = pn; *p; p++)
{ if (islower((unsigned char)*p))
*p &= ~0x20;
else if (isspace((unsigned char)*p))
memmove(p, p + 1, strlen(p));
else if (*p == '=')
{
p++;
while (isspace((unsigned char)*p))
memmove(p, p + 1, strlen(p));
break;
}
}
putenv(strdup(pn));
#if LOG
printf("\tputenv('%s')\n", pn);
//printf("getenv(\"TEST\") = '%s'\n",getenv("TEST"));
#endif
}
break;
}
Lskip:
;
}
return filename;
}
/********************
* Skip spaces.
*/
char *skipspace(const char *p)
{
while (isspace((unsigned char)*p))
p++;
return (char *)p;
}