1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id$ */ |
---|
5 | /* |
---|
6 | * ABSTRACT: management of resources |
---|
7 | */ |
---|
8 | |
---|
9 | #include <stdlib.h> |
---|
10 | #include <unistd.h> |
---|
11 | #include <string.h> |
---|
12 | |
---|
13 | #include "config.h" |
---|
14 | |
---|
15 | #include <auxiliary.h> |
---|
16 | |
---|
17 | #include "feResource.h" |
---|
18 | |
---|
19 | |
---|
20 | |
---|
21 | #ifdef AIX_4 |
---|
22 | #define HAVE_PUTENV 1 |
---|
23 | #endif |
---|
24 | |
---|
25 | #if defined(HPUX_10) || defined(HPUX_9) |
---|
26 | extern "C" int setenv(const char *name, const char *value, int overwrite); |
---|
27 | #endif |
---|
28 | |
---|
29 | |
---|
30 | #include <reporter.h> |
---|
31 | #if !defined(ESINGULAR) && !defined(TSINGULAR) |
---|
32 | #include <omalloc.h> |
---|
33 | #else |
---|
34 | char* feResource(const char id, int warn = -1); |
---|
35 | char* feResource(const char* key, int warn = -1); |
---|
36 | #endif |
---|
37 | |
---|
38 | // define RESOURCE_DEBUG for chattering about resource management |
---|
39 | // #define RESOURCE_DEBUG |
---|
40 | |
---|
41 | #if defined(MAKE_DISTRIBUTION) |
---|
42 | #if defined(ix86_Win) && ! defined(__CYGWIN__) |
---|
43 | #define SINGULAR_DEFAULT_DIR "/Singular/"S_VERSION1 |
---|
44 | #else // unix |
---|
45 | #define SINGULAR_DEFAULT_DIR "/usr/local/Singular/"S_VERSION1 |
---|
46 | #endif |
---|
47 | #else // ! defined(MAKE_DISTRIBUTION) |
---|
48 | #define SINGULAR_DEFAULT_DIR S_ROOT_DIR |
---|
49 | #endif // defined(MAKE_DISTRIBUTION) |
---|
50 | |
---|
51 | /***************************************************************** |
---|
52 | * |
---|
53 | * Declarations: Data structures |
---|
54 | * |
---|
55 | *****************************************************************/ |
---|
56 | typedef enum {feResUndef = 0, feResBinary, feResDir, feResFile, feResUrl, feResPath} feResourceType; |
---|
57 | |
---|
58 | typedef struct feResourceConfig_s |
---|
59 | { |
---|
60 | const char* key; // key to identify resource |
---|
61 | const char id; // char id to identify resource |
---|
62 | feResourceType type; // type of Resource |
---|
63 | const char* env; // env variable to look for |
---|
64 | const char* fmt; // format string -- see below for epxlaination |
---|
65 | char* value; // what it was set to: may be changed |
---|
66 | } feResourceConfig_s; |
---|
67 | typedef feResourceConfig_s * feResourceConfig; |
---|
68 | |
---|
69 | // feSprintf transforms format strings as follows: |
---|
70 | // 1.) substrings of the form %c (c being a letter) are replaced by respective resource value |
---|
71 | // 2.) substrings of the form $string are replaced by value of resp. env variable |
---|
72 | |
---|
73 | // feCleanResource makes furthermore the following transformations (except for URL resources) |
---|
74 | // 1.) '/' characters are replaced by respective directory - separators |
---|
75 | // 2.) ';' characters are replaced by respective path separators |
---|
76 | static feResourceConfig_s feResourceConfigs[] = |
---|
77 | { |
---|
78 | {"SearchPath", 's', feResPath, NULL, |
---|
79 | "$SINGULARPATH;" |
---|
80 | "%b/LIB;" |
---|
81 | "%b/MOD;" |
---|
82 | "%r/LIB;" |
---|
83 | "%r/../LIB;" |
---|
84 | "%d/LIB;" |
---|
85 | "%d/../LIB" |
---|
86 | ""}, |
---|
87 | {"Singular", 'S', feResBinary,"SINGULAR_EXECUTABLE", "%d/"S_UNAME"/Singular",(char *)""}, |
---|
88 | {"BinDir", 'b', feResDir, "SINGULAR_BIN_DIR", "%d/"S_UNAME, (char *)""}, |
---|
89 | {"RootDir", 'r', feResDir, "SINGULAR_ROOT_DIR", "%b/..", (char *)""}, |
---|
90 | {"DefaultDir",'d', feResDir, "SINGULAR_DEFAULT_DIR", SINGULAR_DEFAULT_DIR, (char *)""}, |
---|
91 | {"InfoFile", 'i', feResFile, "SINGULAR_INFO_FILE", "%r/info/singular.hlp", (char *)""}, |
---|
92 | {"IdxFile", 'x', feResFile, "SINGULAR_IDX_FILE", "%r/doc/singular.idx", (char *)""}, |
---|
93 | {"HtmlDir", 'h', feResDir, "SINGULAR_HTML_DIR", "%r/html", (char *)""}, |
---|
94 | #ifdef ix86_Win |
---|
95 | {"HtmlHelpFile",'C', feResFile, "SINGULAR_CHM_FILE", "%r/doc/Manual.chm", (char *)""}, |
---|
96 | #endif |
---|
97 | {"ManualUrl", 'u', feResUrl, "SINGULAR_URL", "http://www.singular.uni-kl.de/Manual/"S_VERSION1, (char *)""}, |
---|
98 | {"ExDir", 'm', feResDir, "SINGULAR_EXAMPLES_DIR","%r/examples", (char *)""}, |
---|
99 | {"Path", 'p', feResPath, NULL, "%b;$PATH", (char *)""}, |
---|
100 | |
---|
101 | #ifdef ESINGULAR |
---|
102 | {"emacs", 'E', feResBinary,"ESINGULAR_EMACS", "%b/emacs", (char *)""}, |
---|
103 | {"xemacs", 'A', feResBinary,"ESINGULAR_EMACS", "%b/xemacs", (char *)""}, |
---|
104 | {"SingularEmacs",'M', feResBinary,"ESINGULAR_SINGULAR", "%b/Singular", (char *)""}, |
---|
105 | {"EmacsLoad", 'l', feResFile, "ESINGULAR_EMACS_LOAD", "%e/.emacs-singular", (char *)""}, |
---|
106 | {"EmacsDir", 'e', feResDir, "ESINGULAR_EMACS_DIR", "%r/emacs", (char *)""}, |
---|
107 | #elif defined(TSINGULAR) |
---|
108 | {"SingularXterm",'M', feResBinary,"TSINGULAR_SINGULAR", "%b/Singular", (char *)""}, |
---|
109 | #ifdef ix86_Win |
---|
110 | {"rxvt", 'X', feResBinary,"RXVT", "%b/rxvt", (char *)""}, |
---|
111 | #else |
---|
112 | {"xterm", 'X', feResBinary,"XTERM", "%b/xterm", (char *)""}, |
---|
113 | #endif |
---|
114 | #else |
---|
115 | {"EmacsDir", 'e', feResDir, "SINGULAR_EMACS_DIR", "%r/emacs", (char *)""}, |
---|
116 | #endif |
---|
117 | {NULL, 0, feResUndef, NULL, NULL, NULL}, // must be the last record |
---|
118 | }; |
---|
119 | |
---|
120 | |
---|
121 | /***************************************************************** |
---|
122 | * |
---|
123 | * Declarations: Local variables / functions |
---|
124 | * |
---|
125 | *****************************************************************/ |
---|
126 | char* feArgv0=NULL; |
---|
127 | #define MAXRESOURCELEN 5*MAXPATHLEN |
---|
128 | |
---|
129 | static feResourceConfig feGetResourceConfig(const char id); |
---|
130 | static feResourceConfig feGetResourceConfig(const char* key); |
---|
131 | static char* feResource(feResourceConfig config, int warn); |
---|
132 | static char* feResourceDefault(feResourceConfig config); |
---|
133 | static char* feInitResource(feResourceConfig config, int warn); |
---|
134 | static char* feGetExpandedExecutable(); |
---|
135 | static BOOLEAN feVerifyResourceValue(feResourceType type, char* value); |
---|
136 | static char* feCleanResourceValue(feResourceType type, char* value); |
---|
137 | static char* feCleanUpFile(char* fname); |
---|
138 | static char* feCleanUpPath(char* path); |
---|
139 | static void mystrcpy(char* d, char* s); |
---|
140 | static char* feSprintf(char* s, const char* fmt, int warn = -1); |
---|
141 | #if defined(ix86_Win) && defined(__GNUC__) |
---|
142 | // utility function of Cygwin32: |
---|
143 | extern "C" int cygwin32_posix_path_list_p (const char *path); |
---|
144 | #endif |
---|
145 | |
---|
146 | /***************************************************************** |
---|
147 | * |
---|
148 | * Public functions |
---|
149 | * |
---|
150 | *****************************************************************/ |
---|
151 | char* feResource(const char* key, int warn) |
---|
152 | { |
---|
153 | return feResource(feGetResourceConfig(key), warn); |
---|
154 | } |
---|
155 | |
---|
156 | char* feGetResource(const char id) |
---|
157 | { |
---|
158 | return feResource(feGetResourceConfig(id), -1); |
---|
159 | } |
---|
160 | |
---|
161 | char* feResource(const char id, int warn) |
---|
162 | { |
---|
163 | return feResource(feGetResourceConfig(id), warn); |
---|
164 | } |
---|
165 | |
---|
166 | char* feResourceDefault(const char id) |
---|
167 | { |
---|
168 | return feResourceDefault(feGetResourceConfig(id)); |
---|
169 | } |
---|
170 | |
---|
171 | char* feResourceDefault(const char* key) |
---|
172 | { |
---|
173 | return feResourceDefault(feGetResourceConfig(key)); |
---|
174 | } |
---|
175 | |
---|
176 | void feInitResources(char* argv0) |
---|
177 | { |
---|
178 | #if defined(ix86_Win) && defined(__GNUC__) |
---|
179 | if (cygwin32_posix_path_list_p (getenv("PATH"))) |
---|
180 | fePathSep = ':'; |
---|
181 | #endif |
---|
182 | feArgv0 = omStrDup(argv0); |
---|
183 | #ifdef RESOURCE_DEBUG |
---|
184 | printf("feInitResources: entering with argv0=%s=\n", argv0); |
---|
185 | #endif |
---|
186 | // init some Resources |
---|
187 | feResource('b'); |
---|
188 | feResource('r'); |
---|
189 | // don't complain about stuff when initializing SingularPath |
---|
190 | feResource('s',0); |
---|
191 | |
---|
192 | #if defined(HAVE_SETENV) || defined(HAVE_PUTENV) |
---|
193 | char* path = feResource('p'); |
---|
194 | #ifdef RESOURCE_DEBUG |
---|
195 | printf("feInitResources: setting path with argv0=%s=\n", path); |
---|
196 | #endif |
---|
197 | #ifdef HAVE_PUTENV |
---|
198 | if (path != NULL) { char *s=(char *)omAlloc0(strlen(path)+6); |
---|
199 | sprintf(s,"PATH=%s",path); |
---|
200 | putenv(s); |
---|
201 | } |
---|
202 | #else |
---|
203 | if (path != NULL) setenv("PATH", path, 1); |
---|
204 | #endif |
---|
205 | #endif |
---|
206 | } |
---|
207 | |
---|
208 | void feReInitResources() |
---|
209 | { |
---|
210 | int i = 0; |
---|
211 | while (feResourceConfigs[i].key != NULL) |
---|
212 | { |
---|
213 | if (feResourceConfigs[i].value[0] != '\0') |
---|
214 | { |
---|
215 | if (feResourceConfigs[i].value != NULL) |
---|
216 | omFree(feResourceConfigs[i].value); |
---|
217 | feResourceConfigs[i].value = (char *)""; |
---|
218 | } |
---|
219 | i++; |
---|
220 | } |
---|
221 | #ifdef RESOURCE_DEBUG |
---|
222 | printf("feInitResources: entering with argv0=%s=\n", feArgv0); |
---|
223 | #endif |
---|
224 | // init some Resources |
---|
225 | feResource('b'); |
---|
226 | feResource('r'); |
---|
227 | // don't complain about stuff when initializing SingularPath |
---|
228 | feResource('s',0); |
---|
229 | } |
---|
230 | |
---|
231 | /***************************************************************** |
---|
232 | * |
---|
233 | * Local functions |
---|
234 | * |
---|
235 | *****************************************************************/ |
---|
236 | static feResourceConfig feGetResourceConfig(const char id) |
---|
237 | { |
---|
238 | int i = 0; |
---|
239 | while (feResourceConfigs[i].key != NULL) |
---|
240 | { |
---|
241 | if (feResourceConfigs[i].id == id) return &(feResourceConfigs[i]); |
---|
242 | i++; |
---|
243 | } |
---|
244 | return NULL; |
---|
245 | } |
---|
246 | |
---|
247 | static feResourceConfig feGetResourceConfig(const char* key) |
---|
248 | { |
---|
249 | int i = 0; |
---|
250 | while (feResourceConfigs[i].key != NULL) |
---|
251 | { |
---|
252 | if (strcmp(feResourceConfigs[i].key, key) == 0) |
---|
253 | return &(feResourceConfigs[i]); |
---|
254 | i++; |
---|
255 | } |
---|
256 | return NULL; |
---|
257 | } |
---|
258 | |
---|
259 | static char* feResource(feResourceConfig config, int warn) |
---|
260 | { |
---|
261 | if (config == NULL) return NULL; |
---|
262 | if (config->value != NULL && *(config->value) != '\0') return config->value; |
---|
263 | return feInitResource(config, warn); |
---|
264 | } |
---|
265 | |
---|
266 | static char* feResourceDefault(feResourceConfig config) |
---|
267 | { |
---|
268 | if (config == NULL) return NULL; |
---|
269 | char* value = (char*) omAlloc(MAXRESOURCELEN); |
---|
270 | feSprintf(value, config->fmt, -1); |
---|
271 | return value; |
---|
272 | } |
---|
273 | |
---|
274 | static char* feInitResource(feResourceConfig config, int warn) |
---|
275 | { |
---|
276 | assume(config != NULL); |
---|
277 | #ifdef RESOURCE_DEBUG |
---|
278 | printf("feInitResource: entering for %s\n", config->key); |
---|
279 | #endif |
---|
280 | |
---|
281 | char value[MAXRESOURCELEN]; |
---|
282 | // now we have to work |
---|
283 | // First, check Environment variable |
---|
284 | if (config->env != NULL) |
---|
285 | { |
---|
286 | char* evalue = getenv(config->env); |
---|
287 | if (evalue != NULL) |
---|
288 | { |
---|
289 | #ifdef RESOURCE_DEBUG |
---|
290 | printf("feInitResource: Found value from env:%s\n", evalue); |
---|
291 | #endif |
---|
292 | strcpy(value, evalue); |
---|
293 | if (config->type == feResBinary // do not verify binaries |
---|
294 | || |
---|
295 | feVerifyResourceValue(config->type, |
---|
296 | feCleanResourceValue(config->type, value))) |
---|
297 | { |
---|
298 | #ifdef RESOURCE_DEBUG |
---|
299 | printf("feInitResource: Set value of %s to =%s=\n", config->key, value); |
---|
300 | #endif |
---|
301 | config->value = omStrDup(value); |
---|
302 | return config->value; |
---|
303 | } |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | *value = '\0'; |
---|
308 | // Special treatment of executable |
---|
309 | if (config->id == 'S') |
---|
310 | { |
---|
311 | char* executable = feGetExpandedExecutable(); |
---|
312 | if (executable != NULL) |
---|
313 | { |
---|
314 | #ifdef RESOURCE_DEBUG |
---|
315 | printf("exec:%s\n", executable); |
---|
316 | #endif |
---|
317 | strcpy(value, executable); |
---|
318 | #ifdef RESOURCE_DEBUG |
---|
319 | printf("value:%s\n", value); |
---|
320 | #endif |
---|
321 | omFree(executable); |
---|
322 | } |
---|
323 | } |
---|
324 | // and bindir |
---|
325 | else if (config->id == 'b') |
---|
326 | { |
---|
327 | char* executable = feResource('S'); |
---|
328 | #ifdef RESOURCE_DEBUG |
---|
329 | printf("feInitResource: Get %s from %s\n", config->key, executable); |
---|
330 | #endif |
---|
331 | if (executable != NULL) |
---|
332 | { |
---|
333 | strcpy(value, executable); |
---|
334 | executable = strrchr(value, DIR_SEP); |
---|
335 | if (executable != NULL) *executable = '\0'; |
---|
336 | } |
---|
337 | } |
---|
338 | |
---|
339 | #ifdef RESOURCE_DEBUG |
---|
340 | printf("value:%s\n", value); |
---|
341 | #endif |
---|
342 | |
---|
343 | if (*value == '\0' && config->fmt != NULL ) |
---|
344 | { |
---|
345 | feSprintf(value, config->fmt, warn); |
---|
346 | } |
---|
347 | else if (config->fmt == NULL) |
---|
348 | { |
---|
349 | sprintf(value, "Wrong Resource Specification of %s", config->key); |
---|
350 | dReportBug(value); |
---|
351 | return NULL; |
---|
352 | } |
---|
353 | |
---|
354 | // Clean and verify |
---|
355 | if (feVerifyResourceValue(config->type, |
---|
356 | feCleanResourceValue(config->type, value))) |
---|
357 | { |
---|
358 | #ifdef RESOURCE_DEBUG |
---|
359 | printf("feInitResource: Set value of %s to =%s=\n", config->key, value); |
---|
360 | #endif |
---|
361 | config->value = omStrDup(value); |
---|
362 | return config->value; |
---|
363 | } |
---|
364 | else if (config->type == feResBinary) |
---|
365 | { |
---|
366 | // for binaries, search through PATH once more |
---|
367 | char* executable = omFindExec(config->key, value); |
---|
368 | if (executable != NULL) |
---|
369 | { |
---|
370 | if (feVerifyResourceValue(config->type, |
---|
371 | feCleanResourceValue(config->type, value))) |
---|
372 | { |
---|
373 | config->value = omStrDup(value); |
---|
374 | #ifdef RESOURCE_DEBUG |
---|
375 | printf("feInitResource: Set value of %s to =%s=\n", config->key, config->value); |
---|
376 | #endif |
---|
377 | return config->value; |
---|
378 | } |
---|
379 | } |
---|
380 | } |
---|
381 | |
---|
382 | // issue warning if explicitely requested, or if |
---|
383 | // this value is gotten for the first time |
---|
384 | if (warn > 0 || (warn < 0 && config->value != NULL)) |
---|
385 | { |
---|
386 | Warn("Could not get %s. ", config->key); |
---|
387 | Warn("Either set environment variable %s to %s,", |
---|
388 | config->env, config->key); |
---|
389 | feSprintf(value, config->fmt, warn); |
---|
390 | Warn("or make sure that %s is at %s", config->key, value); |
---|
391 | } |
---|
392 | #ifdef RESOURCE_DEBUG |
---|
393 | printf("feInitResource: Set value of %s to NULL", config->key); |
---|
394 | #endif |
---|
395 | config->value = NULL; |
---|
396 | return NULL; |
---|
397 | } |
---|
398 | |
---|
399 | static char* feGetExpandedExecutable() |
---|
400 | { |
---|
401 | if (feArgv0 == NULL || *feArgv0 == '\0') |
---|
402 | { |
---|
403 | if (feArgv0 == NULL) dReportBug("feArgv0 == NULL"); |
---|
404 | else dReportBug("feArgv0 == ''"); |
---|
405 | return NULL; |
---|
406 | } |
---|
407 | #ifdef ix86_Win // stupid WINNT sometimes gives you argv[0] within "" |
---|
408 | if (*feArgv0 == '"') |
---|
409 | { |
---|
410 | int l = strlen(feArgv0); |
---|
411 | if (feArgv0[l-1] == '"') |
---|
412 | { |
---|
413 | feArgv0[l-1] = '\0'; |
---|
414 | feArgv0++; |
---|
415 | } |
---|
416 | } |
---|
417 | #endif |
---|
418 | #ifdef RESOURCE_DEBUG |
---|
419 | printf("feGetExpandedExecutable: calling find_exec with =%s=\n", feArgv0); |
---|
420 | #endif |
---|
421 | char executable[MAXRESOURCELEN]; |
---|
422 | char* value = omFindExec(feArgv0, executable); |
---|
423 | #ifdef RESOURCE_DEBUG |
---|
424 | printf("feGetExpandedExecutable: find_exec exited with =%s=%d\n", executable, access(executable, X_OK)); |
---|
425 | #endif |
---|
426 | if (value == NULL) |
---|
427 | { |
---|
428 | char message[MAXRESOURCELEN]; |
---|
429 | sprintf(message, "Could not get expanded executable from %s", feArgv0); |
---|
430 | dReportBug(message); |
---|
431 | return NULL; |
---|
432 | } |
---|
433 | return omStrDup(value); |
---|
434 | } |
---|
435 | |
---|
436 | |
---|
437 | static BOOLEAN feVerifyResourceValue(feResourceType type, char* value) |
---|
438 | { |
---|
439 | #ifdef RESOURCE_DEBUG |
---|
440 | printf("feVerifyResourceValue: entering with =%s=\n", value); |
---|
441 | printf("%d:%d\n", access(value, R_OK), access(value, X_OK)); |
---|
442 | #endif |
---|
443 | switch(type) |
---|
444 | { |
---|
445 | case feResUrl: |
---|
446 | case feResPath: |
---|
447 | return TRUE; |
---|
448 | |
---|
449 | case feResFile: |
---|
450 | return ! access(value, R_OK); |
---|
451 | |
---|
452 | case feResBinary: |
---|
453 | case feResDir: |
---|
454 | return ! access(value, X_OK); |
---|
455 | |
---|
456 | default: |
---|
457 | return FALSE; |
---|
458 | } |
---|
459 | } |
---|
460 | |
---|
461 | /***************************************************************** |
---|
462 | * |
---|
463 | * Cleaning/Transformations of resource values |
---|
464 | * |
---|
465 | *****************************************************************/ |
---|
466 | |
---|
467 | static char* feCleanResourceValue(feResourceType type, char* value) |
---|
468 | { |
---|
469 | if (value == NULL || *value == '\0') return value; |
---|
470 | #ifdef RESOURCE_DEBUG |
---|
471 | printf("Clean value:%s\n", value); |
---|
472 | #endif |
---|
473 | #ifdef ix86_Win |
---|
474 | #ifdef RESOURCE_DEBUG |
---|
475 | printf("Clean WINNT value:%s\n", value); |
---|
476 | #endif |
---|
477 | if (type == feResBinary) |
---|
478 | { |
---|
479 | int l = strlen(value); |
---|
480 | if (l < 4 || (strcmp(&value[l-4], ".exe") != 0 && |
---|
481 | strcmp(&value[l-4], ".EXE") != 0)) |
---|
482 | strcat(value, ".exe"); |
---|
483 | } |
---|
484 | #endif |
---|
485 | if (type == feResFile || type == feResBinary || type == feResDir) |
---|
486 | return feCleanUpFile(value); |
---|
487 | if (type == feResPath) |
---|
488 | return feCleanUpPath(value); |
---|
489 | return value; |
---|
490 | } |
---|
491 | |
---|
492 | static char* feCleanUpFile(char* fname) |
---|
493 | { |
---|
494 | char* fn, *s; |
---|
495 | |
---|
496 | #ifdef RESOURCE_DEBUG |
---|
497 | printf("feCleanUpFile: entering with =%s=\n", fname); |
---|
498 | #endif |
---|
499 | // Remove unnecessary .. and // |
---|
500 | for (fn = fname; *fn != '\0'; fn++) |
---|
501 | { |
---|
502 | if (*fn == '/') |
---|
503 | { |
---|
504 | if (*(fn+1) == '\0') |
---|
505 | { |
---|
506 | if (fname != fn) *fn = '\0'; |
---|
507 | break; |
---|
508 | } |
---|
509 | if (*(fn + 1) == '/' && (fname != fn)) |
---|
510 | { |
---|
511 | mystrcpy(fn, fn+1); |
---|
512 | fn--; |
---|
513 | } |
---|
514 | else if (*(fn+1) == '.') |
---|
515 | { |
---|
516 | if (*(fn+2) == '.' && (*(fn + 3) == '/' || *(fn + 3) == '\0')) |
---|
517 | { |
---|
518 | *fn = '\0'; |
---|
519 | s = strrchr(fname, '/'); |
---|
520 | if (s != NULL) |
---|
521 | { |
---|
522 | mystrcpy(s+1, fn + (*(fn + 3) != '\0' ? 4 : 3)); |
---|
523 | fn = s-1; |
---|
524 | } |
---|
525 | else |
---|
526 | { |
---|
527 | *fn = '/'; |
---|
528 | } |
---|
529 | } |
---|
530 | else if (*(fn+2) == '/' || *(fn+2) == '\0') |
---|
531 | { |
---|
532 | mystrcpy(fn+1, fn+3); |
---|
533 | fn--; |
---|
534 | } |
---|
535 | } |
---|
536 | } |
---|
537 | } |
---|
538 | |
---|
539 | #ifdef RESOURCE_DEBUG |
---|
540 | printf("feCleanUpFile: leaving with =%s=\n", fname); |
---|
541 | #endif |
---|
542 | return fname; |
---|
543 | } |
---|
544 | |
---|
545 | // remove duplicates dir resp. those which do not exist |
---|
546 | static char* feCleanUpPath(char* path) |
---|
547 | { |
---|
548 | #ifdef RESOURCE_DEBUG |
---|
549 | printf("feCleanUpPath: entering with: =%s=\n", path); |
---|
550 | #endif |
---|
551 | if (path == NULL) return path; |
---|
552 | |
---|
553 | int n_comps = 1, i, j; |
---|
554 | char* opath = path; |
---|
555 | char** path_comps; |
---|
556 | |
---|
557 | for (; *path != '\0'; path++) |
---|
558 | { |
---|
559 | if (*path == fePathSep) n_comps++; |
---|
560 | else if (*path == ';') |
---|
561 | { |
---|
562 | *path = fePathSep; |
---|
563 | n_comps++; |
---|
564 | } |
---|
565 | } |
---|
566 | |
---|
567 | path_comps = (char**) omAlloc(n_comps*sizeof(char*)); |
---|
568 | path_comps[0]=opath; |
---|
569 | path=opath; |
---|
570 | i = 1; |
---|
571 | |
---|
572 | if (i < n_comps) |
---|
573 | { |
---|
574 | while (1) |
---|
575 | { |
---|
576 | if (*path == fePathSep) |
---|
577 | { |
---|
578 | *path = '\0'; |
---|
579 | path_comps[i] = path+1; |
---|
580 | i++; |
---|
581 | if (i == n_comps) break; |
---|
582 | } |
---|
583 | path++; |
---|
584 | } |
---|
585 | } |
---|
586 | |
---|
587 | for (i=0; i<n_comps; i++) |
---|
588 | path_comps[i] = feCleanUpFile(path_comps[i]); |
---|
589 | #ifdef RESOURCE_DEBUG |
---|
590 | PrintS("feCleanUpPath: after CleanUpName: "); |
---|
591 | for (i=0; i<n_comps; i++) |
---|
592 | Print("%s:", path_comps[i]); |
---|
593 | Print("\n"); |
---|
594 | #endif |
---|
595 | |
---|
596 | for (i=0; i<n_comps;) |
---|
597 | { |
---|
598 | #ifdef RESOURCE_DEBUG |
---|
599 | if (access(path_comps[i], X_OK | R_OK)) |
---|
600 | Print("feCleanUpPath: remove %d:%s -- can not access\n", i, path_comps[i]); |
---|
601 | #endif |
---|
602 | if ( ! access(path_comps[i], X_OK | R_OK)) |
---|
603 | { |
---|
604 | // x- permission is granted -- we assume that it is a dir |
---|
605 | for (j=0; j<i; j++) |
---|
606 | { |
---|
607 | if (strcmp(path_comps[j], path_comps[i]) == 0) |
---|
608 | { |
---|
609 | // found a duplicate |
---|
610 | #ifdef RESOURCE_DEBUG |
---|
611 | Print("feCleanUpPath: remove %d:%s -- equal to %d:%s\n", j, path_comps[j], i, path_comps[i]); |
---|
612 | #endif |
---|
613 | j = i+1; |
---|
614 | break; |
---|
615 | } |
---|
616 | } |
---|
617 | if (j == i) |
---|
618 | { |
---|
619 | i++; |
---|
620 | continue; |
---|
621 | } |
---|
622 | } |
---|
623 | // now we can either not access or found a duplicate |
---|
624 | path_comps[i] = NULL; |
---|
625 | for (j=i+1; j<n_comps; j++) |
---|
626 | path_comps[j-1] = path_comps[j]; |
---|
627 | n_comps--; |
---|
628 | } |
---|
629 | |
---|
630 | |
---|
631 | // assemble everything again |
---|
632 | for (path=opath, i=0;i<n_comps-1;i++) |
---|
633 | { |
---|
634 | mystrcpy(path, path_comps[i]); |
---|
635 | path += strlen(path); |
---|
636 | *path = fePathSep; |
---|
637 | path++; |
---|
638 | } |
---|
639 | if (n_comps) |
---|
640 | { |
---|
641 | mystrcpy(path, path_comps[i]); |
---|
642 | } |
---|
643 | else |
---|
644 | { |
---|
645 | *opath = '\0'; |
---|
646 | } |
---|
647 | omFree(path_comps); |
---|
648 | #ifdef RESOURCE_DEBUG |
---|
649 | Print("feCleanUpPath: leaving with path=%s=\n", opath); |
---|
650 | #endif |
---|
651 | return opath; |
---|
652 | } |
---|
653 | |
---|
654 | // strcpy where source and destination may overlap |
---|
655 | static void mystrcpy(char* d, char* s) |
---|
656 | { |
---|
657 | assume(d != NULL && s != NULL); |
---|
658 | while (*s != '\0') |
---|
659 | { |
---|
660 | *d = *s; |
---|
661 | d++; |
---|
662 | s++; |
---|
663 | } |
---|
664 | *d = '\0'; |
---|
665 | } |
---|
666 | |
---|
667 | /***************************************************************** |
---|
668 | * |
---|
669 | * feSprintf |
---|
670 | * |
---|
671 | *****************************************************************/ |
---|
672 | static char* feSprintf(char* s, const char* fmt, int warn) |
---|
673 | { |
---|
674 | char* s_in = s; |
---|
675 | if (fmt == NULL) return NULL; |
---|
676 | |
---|
677 | while (*fmt != '\0') |
---|
678 | { |
---|
679 | *s = *fmt; |
---|
680 | |
---|
681 | if (*fmt == '%' && *(fmt + 1) != '\0') |
---|
682 | { |
---|
683 | fmt++; |
---|
684 | char* r = feResource(*fmt, warn); |
---|
685 | if (r != NULL) |
---|
686 | { |
---|
687 | strcpy(s, r); |
---|
688 | s += strlen(r) - 1; |
---|
689 | } |
---|
690 | else |
---|
691 | { |
---|
692 | s++; |
---|
693 | *s = *fmt; |
---|
694 | } |
---|
695 | } |
---|
696 | else if (*fmt == '$' && *(fmt + 1) != '\0') |
---|
697 | { |
---|
698 | fmt++; |
---|
699 | char* v = s + 1; |
---|
700 | while (*fmt == '_' || |
---|
701 | (*fmt >= 'A' && *fmt <= 'Z') || |
---|
702 | (*fmt >= 'a' && *fmt <= 'z')) |
---|
703 | { |
---|
704 | *v = *fmt; |
---|
705 | v++; |
---|
706 | fmt++; |
---|
707 | } |
---|
708 | fmt--; |
---|
709 | *v = '\0'; |
---|
710 | v = getenv(s + 1); |
---|
711 | if (v != NULL) strcpy(s, v); |
---|
712 | s += strlen(s) - 1; |
---|
713 | } |
---|
714 | s++; |
---|
715 | fmt++; |
---|
716 | } |
---|
717 | *s = '\0'; |
---|
718 | return s_in; |
---|
719 | } |
---|
720 | |
---|
721 | void feStringAppendResources(int warn) |
---|
722 | { |
---|
723 | int i = 0; |
---|
724 | char* r; |
---|
725 | StringAppend("%-10s:\t%s\n", "argv[0]", feArgv0); |
---|
726 | while (feResourceConfigs[i].key != NULL) |
---|
727 | { |
---|
728 | r = feResource(feResourceConfigs[i].key, warn); |
---|
729 | StringAppend("%-10s:\t%s\n", feResourceConfigs[i].key, |
---|
730 | (r != NULL ? r : "")); |
---|
731 | i++; |
---|
732 | } |
---|
733 | } |
---|