My Project
Macros | Functions | Variables
feResource.cc File Reference
#include "singular_resourcesconfig.h"
#include "feResource.h"
#include "omFindExec.h"
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/param.h>

Go to the source code of this file.

Macros

#define SINGULAR_DEFAULT_DIR   PREFIX
 
#define MAXRESOURCELEN   5*MAXPATHLEN
 

Functions

static feResourceConfig feGetResourceConfig (const char id)
 
static feResourceConfig feGetResourceConfig (const char *key)
 
static char * feResource (feResourceConfig config, int warn)
 
static char * feResourceDefault (feResourceConfig config)
 
static char * feInitResource (feResourceConfig config, int warn)
 
static char * feGetExpandedExecutable ()
 
static int feVerifyResourceValue (feResourceType type, char *value)
 
static char * feCleanResourceValue (feResourceType type, char *value)
 
static char * feCleanUpFile (char *fname)
 
static char * feCleanUpPath (char *path)
 
static void mystrcpy (char *d, char *s)
 
static char * feSprintf (char *s, const char *fmt, int warn=-1)
 
char * feResource (const char *key, int warn)
 
char * feResource (const char id, int warn)
 
char * feGetResource (const char id, int warn)
 
char * feResourceDefault (const char id)
 
char * feResourceDefault (const char *key)
 
void feInitResources (const char *argv0)
 
void feReInitResources ()
 

Variables

VAR char * feArgv0 = NULL
 
VAR feResourceConfig_s feResourceConfigs []
 

Macro Definition Documentation

◆ MAXRESOURCELEN

#define MAXRESOURCELEN   5*MAXPATHLEN

Definition at line 121 of file feResource.cc.

◆ SINGULAR_DEFAULT_DIR

#define SINGULAR_DEFAULT_DIR   PREFIX

Definition at line 40 of file feResource.cc.

Function Documentation

◆ feCleanResourceValue()

static char * feCleanResourceValue ( feResourceType  type,
char *  value 
)
static

Definition at line 466 of file feResource.cc.

467{
468 if (value == NULL || *value == '\0') return value;
469#ifdef RESOURCE_DEBUG
470 printf("Clean value:%s\n", value);
471#endif
472#ifdef __CYGWIN__
473#ifdef RESOURCE_DEBUG
474 printf("Clean WINNT value:%s\n", value);
475#endif
476 if (type == feResBinary)
477 {
478 int l = strlen(value);
479 if (l < 4 || (strcmp(&value[l-4], ".exe") != 0 &&
480 strcmp(&value[l-4], ".EXE") != 0))
481 strcat(value, ".exe");
482 }
483#endif
484 if (type == feResFile || type == feResBinary || type == feResDir)
485 return feCleanUpFile(value);
486 if (type == feResPath)
487 return feCleanUpPath(value);
488 return value;
489}
#define NULL
Definition: auxiliary.h:104
int l
Definition: cfEzgcd.cc:100
static char * feCleanUpFile(char *fname)
Definition: feResource.cc:491
static char * feCleanUpPath(char *path)
Definition: feResource.cc:549
@ feResBinary
Definition: feResource.h:20
@ feResPath
Definition: feResource.h:20
@ feResDir
Definition: feResource.h:20
@ feResFile
Definition: feResource.h:20

◆ feCleanUpFile()

static char * feCleanUpFile ( char *  fname)
static

Definition at line 491 of file feResource.cc.

492{
493 char* fn;
494
495#ifdef RESOURCE_DEBUG
496 printf("feCleanUpFile: entering with =%s=\n", fname);
497#endif
498 // Remove unnecessary .. and //
499 for (fn = fname; *fn != '\0'; fn++)
500 {
501 if (*fn == '/')
502 {
503 if (*(fn+1) == '\0')
504 {
505 if (fname != fn) *fn = '\0';
506 break;
507 }
508 if (*(fn + 1) == '/' && (fname != fn))
509 {
510 mystrcpy(fn, fn+1);
511 fn--;
512 }
513 else if (*(fn+1) == '.')
514 {
515 if (*(fn+2) == '.' && (*(fn + 3) == '/' || *(fn + 3) == '\0'))
516 {
517 #if 0
518 // this does not work: ./../../mmm will be changed to ./../mmm
519 // but we only want to change ././mmm to ./mmm
520 *fn = '\0';
521 s = strrchr(fname, '/');
522 if (s != NULL)
523 {
524 mystrcpy(s+1, fn + (*(fn + 3) != '\0' ? 4 : 3));
525 fn = s-1;
526 }
527 else
528 {
529 *fn = '/';
530 }
531 #endif
532 }
533 else if (*(fn+2) == '/' || *(fn+2) == '\0')
534 {
535 mystrcpy(fn+1, fn+3);
536 fn--;
537 }
538 }
539 }
540 }
541
542#ifdef RESOURCE_DEBUG
543 printf("feCleanUpFile: leaving with =%s=\n", fname);
544#endif
545 return fname;
546}
const CanonicalForm int s
Definition: facAbsFact.cc:51
static void mystrcpy(char *d, char *s)
Definition: feResource.cc:658

◆ feCleanUpPath()

static char * feCleanUpPath ( char *  path)
static

Definition at line 549 of file feResource.cc.

550{
551#ifdef RESOURCE_DEBUG
552 printf("feCleanUpPath: entering with: =%s=\n", path);
553#endif
554 if (path == NULL) return path;
555
556 int n_comps = 1, i, j;
557 char* opath = path;
558 char** path_comps;
559
560 for (; *path != '\0'; path++)
561 {
562 if (*path == fePathSep) n_comps++;
563 else if (*path == ';')
564 {
565 *path = fePathSep;
566 n_comps++;
567 }
568 }
569
570 path_comps = (char**) malloc(n_comps*sizeof(char*));
571 path_comps[0]=opath;
572 path=opath;
573 i = 1;
574
575 if (i < n_comps)
576 {
577 while (1)
578 {
579 if (*path == fePathSep)
580 {
581 *path = '\0';
582 path_comps[i] = path+1;
583 i++;
584 if (i == n_comps) break;
585 }
586 path++;
587 }
588 }
589
590 for (i=0; i<n_comps; i++)
591 path_comps[i] = feCleanUpFile(path_comps[i]);
592#ifdef RESOURCE_DEBUG
593 printf("feCleanUpPath: after CleanUpName: ");
594 for (i=0; i<n_comps; i++)
595 printf("%s:", path_comps[i]);
596 printf("\n");
597#endif
598
599 for (i=0; i<n_comps;)
600 {
601#ifdef RESOURCE_DEBUG
602 if (access(path_comps[i], X_OK | R_OK))
603 printf("feCleanUpPath: remove %d:%s -- can not access\n", i, path_comps[i]);
604#endif
605 if ( ! access(path_comps[i], X_OK | R_OK))
606 {
607 // x- permission is granted -- we assume that it is a dir
608 for (j=0; j<i; j++)
609 {
610 if (strcmp(path_comps[j], path_comps[i]) == 0)
611 {
612 // found a duplicate
613#ifdef RESOURCE_DEBUG
614 printf("feCleanUpPath: remove %d:%s -- equal to %d:%s\n", j, path_comps[j], i, path_comps[i]);
615#endif
616 j = i+1;
617 break;
618 }
619 }
620 if (j == i)
621 {
622 i++;
623 continue;
624 }
625 }
626 // now we can either not access or found a duplicate
627 path_comps[i] = NULL;
628 for (j=i+1; j<n_comps; j++)
629 path_comps[j-1] = path_comps[j];
630 n_comps--;
631 }
632
633
634 // assemble everything again
635 for (path=opath, i=0;i<n_comps-1;i++)
636 {
637 mystrcpy(path, path_comps[i]);
638 path += strlen(path);
639 *path = fePathSep;
640 path++;
641 }
642 if (n_comps)
643 {
644 mystrcpy(path, path_comps[i]);
645 }
646 else
647 {
648 *opath = '\0';
649 }
650 free(path_comps);
651#ifdef RESOURCE_DEBUG
652 printf("feCleanUpPath: leaving with path=%s=\n", opath);
653#endif
654 return opath;
655}
int i
Definition: cfEzgcd.cc:132
int j
Definition: facHensel.cc:110
const char fePathSep
Definition: feResource.h:58
#define free
Definition: omAllocFunc.c:14
void * malloc(size_t size)
Definition: omalloc.c:92

◆ feGetExpandedExecutable()

static char * feGetExpandedExecutable ( )
static

Definition at line 398 of file feResource.cc.

399{
400 if (feArgv0 == NULL || *feArgv0 == '\0')
401 {
402 if (feArgv0 == NULL)
403 printf("Bug >>feArgv0 == NULL<< at %s:%d\n",__FILE__,__LINE__);
404 else
405 printf("Bug >>feArgv0 == ''<< at %s:%d\n",__FILE__,__LINE__);
406 return NULL;
407 }
408#ifdef __CYGWIN__ // stupid WINNT sometimes gives you argv[0] within ""
409 if (*feArgv0 == '"')
410 {
411 int l = strlen(feArgv0);
412 if (feArgv0[l-1] == '"')
413 {
414 feArgv0[l-1] = '\0';
415 feArgv0++;
416 }
417 }
418#endif
419#ifdef RESOURCE_DEBUG
420 printf("feGetExpandedExecutable: calling find_exec with \"%s\"\n", feArgv0);
421#endif
422 char executable[MAXRESOURCELEN];
423 char* value = omFindExec(feArgv0, executable);
424#ifdef RESOURCE_DEBUG
425 printf("feGetExpandedExecutable: find_exec exited with \"%s\": %d\n", executable, access(executable, X_OK));
426#endif
427 if (value == NULL)
428 {
429 printf("Bug >>Could not get expanded executable from \"%s\"<< at %s:%d\n",feArgv0,__FILE__,__LINE__);
430 return NULL;
431 }
432 return strdup(value);
433}
VAR char * feArgv0
Definition: feResource.cc:19
#define MAXRESOURCELEN
Definition: feResource.cc:121
#define strdup
Definition: omAllocFunc.c:19
char * omFindExec(const char *name, char *exec)
Definition: omFindExec.c:252

◆ feGetResource()

char * feGetResource ( const char  id,
int  warn 
)

Definition at line 155 of file feResource.cc.

156{
157 return feResource(feGetResourceConfig(id), warn);
158}
static feResourceConfig feGetResourceConfig(const char id)
Definition: feResource.cc:235
static char * feResource(feResourceConfig config, int warn)
Definition: feResource.cc:258

◆ feGetResourceConfig() [1/2]

static feResourceConfig feGetResourceConfig ( const char *  key)
static

Definition at line 246 of file feResource.cc.

247{
248 int i = 0;
249 while (feResourceConfigs[i].key != NULL)
250 {
251 if (strcmp(feResourceConfigs[i].key, key) == 0)
252 return &(feResourceConfigs[i]);
253 i++;
254 }
255 return NULL;
256}
VAR feResourceConfig_s feResourceConfigs[]
Definition: feResource.cc:54

◆ feGetResourceConfig() [2/2]

static feResourceConfig feGetResourceConfig ( const char  id)
static

Definition at line 235 of file feResource.cc.

236{
237 int i = 0;
238 while (feResourceConfigs[i].key != NULL)
239 {
240 if (feResourceConfigs[i].id == id) return &(feResourceConfigs[i]);
241 i++;
242 }
243 return NULL;
244}

◆ feInitResource()

static char * feInitResource ( feResourceConfig  config,
int  warn 
)
static

Definition at line 273 of file feResource.cc.

274{
275 /*assume(config != NULL);*/
276#ifdef RESOURCE_DEBUG
277 printf("feInitResource(config->key: '%s', warn: '%d') : entering ...\n", config->key, warn);
278#endif
279
280 char value[MAXRESOURCELEN];
281 // now we have to work
282 // First, check Environment variable
283 if (config->env != NULL)
284 {
285 char* evalue = getenv(config->env);
286 if (evalue != NULL)
287 {
288#ifdef RESOURCE_DEBUG
289 printf("feInitResource(config,warn): Found value from env:%s\n", evalue);
290#endif
291 strcpy(value, evalue);
292 if (config->type == feResBinary // do not verify binaries
293 ||
295 feCleanResourceValue(config->type, value)))
296 {
297#ifdef RESOURCE_DEBUG
298 printf("feInitResource(config,warn): Set value of config (with key: '%s') to '%s'\n", config->key, value);
299#endif
300 config->value = strdup(value);
301 return config->value;
302 }
303 }
304 }
305
306 *value = '\0';
307 // Special treatment of executable
308 if (config->id == 'S')
309 {
310 char* executable = feGetExpandedExecutable();
311 if (executable != NULL)
312 {
313#ifdef RESOURCE_DEBUG
314 printf("exec:%s\n", executable);
315#endif
316 strcpy(value, executable);
317#ifdef RESOURCE_DEBUG
318 printf("value:%s\n", value);
319#endif
320 free(executable);
321 }
322 }
323 // and bindir
324 else if (config->id == 'b')
325 {
326 char* executable = feResource('S');
327#ifdef RESOURCE_DEBUG
328 printf("feInitResource(config,warn): Get '%s' from \"%s\"\n", config->key, executable);
329#endif
330 if (executable != NULL)
331 {
332 strcpy(value, executable);
333 executable = strrchr(value, DIR_SEP);
334 if (executable != NULL) *executable = '\0';
335 }
336 }
337
338#ifdef RESOURCE_DEBUG
339 printf("value:%s\n", value);
340#endif
341
342 if (*value == '\0' && config->fmt != NULL )
343 {
344 feSprintf(value, config->fmt, warn);
345 }
346 else if (config->fmt == NULL)
347 {
348 printf("Bug >>Wrong Resource Specification of '%s'<< at \"%s:%d\"\n",config->key,__FILE__,__LINE__);
349 // TODO: printf -> WarnS???
350 return NULL;
351 }
352
353 // Clean and verify
355 feCleanResourceValue(config->type, value)))
356 {
357#ifdef RESOURCE_DEBUG
358 printf("feInitResource(config,warn): Set value of '%s' to \"%s\"\n", config->key, value);
359#endif
360 config->value = strdup(value);
361 return config->value;
362 }
363 else if (config->type == feResBinary)
364 {
365 // for binaries, search through PATH once more
366 char* executable = omFindExec(config->key, value);
367 if (executable != NULL)
368 {
370 feCleanResourceValue(config->type, value)))
371 {
372 config->value = strdup(value);
373#ifdef RESOURCE_DEBUG
374 printf("feInitResource(config,warn): Set value of '%s' to \"%s\"\n", config->key, config->value);
375#endif
376 return config->value;
377 }
378 }
379 }
380
381 // issue warning if explicitely requested, or if
382 // this value is gotten for the first time
383 if (warn > 0 || (warn < 0 && config->value != NULL))
384 {
385 printf("// ** Could not get '%s'.\n", config->key);
386 printf("// ** Either set environment variable '%s' to '%s',\n",
387 config->env, config->key);
388 feSprintf(value, config->fmt, warn);
389 printf("// ** or make sure that '%s' is at \"%s\"\n", config->key, value);
390 }
391#ifdef RESOURCE_DEBUG
392 printf("feInitResource(config,warn): Set value of '%s' to NULL", config->key);
393#endif
394 config->value = NULL;
395 return NULL;
396}
static char * feSprintf(char *s, const char *fmt, int warn=-1)
Definition: feResource.cc:675
static char * feGetExpandedExecutable()
Definition: feResource.cc:398
static char * feCleanResourceValue(feResourceType type, char *value)
Definition: feResource.cc:466
static int feVerifyResourceValue(feResourceType type, char *value)
Definition: feResource.cc:436
#define DIR_SEP
Definition: feResource.h:6
char * getenv()
size_t config[4]
Definition: vspace.cc:13

◆ feInitResources()

void feInitResources ( const char *  argv0)

Definition at line 170 of file feResource.cc.

171{
172 if (argv0==NULL)
173 {
174 //WarnS("illegal argv[0]==NULL");
175 feArgv0 = (char*)malloc(MAXPATHLEN+strlen("/Singular"));
176 getcwd(feArgv0, MAXPATHLEN);
177 strcat(feArgv0,"/Singular");
178 }
179 else
181#ifdef RESOURCE_DEBUG
182 printf("feInitResources(argv0: '%s'): entering...\n", feArgv0);
183#endif
184 // init some Resources
185 feResource('b');
186 feResource('r');
187 // don't complain about stuff when initializing SingularPath
188 feResource('s',0);
189 feResource('P');
190
191#if defined(HAVE_SETENV) || defined(HAVE_PUTENV)
192 char* path = feResource('p');
193#ifdef RESOURCE_DEBUG
194 printf("feInitResources(argv0): setting path with '%s'\n", path);
195#endif
196#ifdef HAVE_PUTENV
197 if (path != NULL) { char *s=(char *)malloc(strlen(path)+6);
198 sprintf(s,"PATH=%s",path);
199 putenv(s);
200 }
201#else
202 if (path != NULL) setenv("PATH", path, 1);
203#endif
204#endif
205}
char * argv0
#define MAXPATHLEN
Definition: omRet2Info.c:22

◆ feReInitResources()

void feReInitResources ( )

Definition at line 207 of file feResource.cc.

208{
209 int i = 0;
210 while (feResourceConfigs[i].key != NULL)
211 {
212 if ((feResourceConfigs[i].value != NULL)
213 && (feResourceConfigs[i].value[0] != '\0'))
214 {
215 free(feResourceConfigs[i].value);
216 feResourceConfigs[i].value = (char *)"";
217 }
218 i++;
219 }
220#ifdef RESOURCE_DEBUG
221 printf("feInitResources(): entering...\n");
222#endif
223 // init some Resources
224 feResource('b');
225 feResource('r');
226 // don't complain about stuff when initializing SingularPath
227 feResource('s',0);
228}

◆ feResource() [1/3]

char * feResource ( const char *  key,
int  warn 
)

Definition at line 145 of file feResource.cc.

146{
147 return feResource(feGetResourceConfig(key), warn);
148}

◆ feResource() [2/3]

char * feResource ( const char  id,
int  warn 
)

Definition at line 150 of file feResource.cc.

151{
152 return feResource(feGetResourceConfig(id), warn);
153}

◆ feResource() [3/3]

static char * feResource ( feResourceConfig  config,
int  warn 
)
static

Definition at line 258 of file feResource.cc.

259{
260 if (config == NULL) return NULL;
261 if (config->value != NULL && *(config->value) != '\0') return config->value;
262 return feInitResource(config, warn);
263}
static char * feInitResource(feResourceConfig config, int warn)
Definition: feResource.cc:273

◆ feResourceDefault() [1/3]

char * feResourceDefault ( const char *  key)

Definition at line 165 of file feResource.cc.

166{
168}
static char * feResourceDefault(feResourceConfig config)
Definition: feResource.cc:265

◆ feResourceDefault() [2/3]

char * feResourceDefault ( const char  id)

Definition at line 160 of file feResource.cc.

161{
163}

◆ feResourceDefault() [3/3]

static char * feResourceDefault ( feResourceConfig  config)
static

Definition at line 265 of file feResource.cc.

266{
267 if (config == NULL) return NULL;
268 char* value = (char*) malloc(MAXRESOURCELEN);
269 feSprintf(value, config->fmt, -1);
270 return value;
271}

◆ feSprintf()

static char * feSprintf ( char *  s,
const char *  fmt,
int  warn = -1 
)
static

Definition at line 675 of file feResource.cc.

676{
677 char* s_in = s;
678 if (fmt == NULL) return NULL;
679
680 while (*fmt != '\0')
681 {
682 *s = *fmt;
683
684 if (*fmt == '%' && *(fmt + 1) != '\0')
685 {
686 fmt++;
687 char* r = feResource(*fmt, warn);
688 if (r != NULL)
689 {
690 strcpy(s, r);
691 s += strlen(r) - 1;
692 }
693 else
694 {
695 s++;
696 *s = *fmt;
697 }
698 }
699 else if (*fmt == '$' && *(fmt + 1) != '\0')
700 {
701 fmt++;
702 char* v = s + 1;
703 while (*fmt == '_' ||
704 (*fmt >= 'A' && *fmt <= 'Z') ||
705 (*fmt >= 'a' && *fmt <= 'z'))
706 {
707 *v = *fmt;
708 v++;
709 fmt++;
710 }
711 fmt--;
712 *v = '\0';
713 v = getenv(s + 1);
714 if (v != NULL) strcpy(s, v);
715 s += strlen(s) - 1;
716 }
717 s++;
718 fmt++;
719 }
720 *s = '\0';
721 return s_in;
722}
const Variable & v
< [in] a sqrfree bivariate poly
Definition: facBivar.h:39

◆ feVerifyResourceValue()

static int feVerifyResourceValue ( feResourceType  type,
char *  value 
)
static

Definition at line 436 of file feResource.cc.

437{
438#ifdef RESOURCE_DEBUG
439 printf("feVerifyResourceValue(type: %d, value: \"%s\"): entering\n", (int)type, value);
440 printf("Access: ROK: %d, XOK: %d\n", access(value, R_OK), access(value, X_OK));
441#endif
442 switch(type)
443 {
444 case feResUrl:
445 case feResPath:
446 return 1;
447
448 case feResFile:
449 return ! access(value, R_OK);
450
451 case feResBinary:
452 case feResDir:
453 return ! access(value, X_OK);
454
455 default:
456 return 0;
457 }
458}
@ feResUrl
Definition: feResource.h:20

◆ mystrcpy()

static void mystrcpy ( char *  d,
char *  s 
)
static

Definition at line 658 of file feResource.cc.

659{
660 /*assume(d != NULL && s != NULL);*/
661 while (*s != '\0')
662 {
663 *d = *s;
664 d++;
665 s++;
666 }
667 *d = '\0';
668}

Variable Documentation

◆ feArgv0

VAR char* feArgv0 = NULL

Definition at line 19 of file feResource.cc.

◆ feResourceConfigs

VAR feResourceConfig_s feResourceConfigs[]

Definition at line 54 of file feResource.cc.