source: git/libpolys/resources/feResource.cc @ d9e190

spielwiese
Last change on this file since d9e190 was d9e190, checked in by Mohamed Barakat <mohamed.barakat@…>, 13 years ago
got rid of program_invocation_name (non-portable) and replaced it by an educated guess changed the test generation script to save a copy of the program name in main
  • Property mode set to 100644
File size: 19.1 KB
Line 
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#include <unistd.h>
13#include <sys/param.h>
14
15#include "config.h"
16#include <misc/auxiliary.h>
17
18#include "feResource.h"
19
20
21#ifdef AIX_4
22#define HAVE_PUTENV 1
23#endif
24
25#if defined(HPUX_10) || defined(HPUX_9)
26extern "C" int setenv(const char *name, const char *value, int overwrite);
27#endif
28
29
30#include <reporter/reporter.h>
31#if !defined(ESINGULAR) && !defined(TSINGULAR)
32#include <omalloc/omalloc.h>
33#else
34char* feResource(const char id, int warn = -1);
35char* 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 *****************************************************************/
56typedef enum {feResUndef = 0, feResBinary, feResDir, feResFile, feResUrl, feResPath} feResourceType;
57
58typedef 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;
67typedef 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
76static 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 *****************************************************************/
126char* feArgv0=NULL;
127#define MAXRESOURCELEN 5*MAXPATHLEN
128
129static feResourceConfig feGetResourceConfig(const char id);
130static feResourceConfig feGetResourceConfig(const char* key);
131static char* feResource(feResourceConfig config, int warn);
132static char* feResourceDefault(feResourceConfig config);
133static char* feInitResource(feResourceConfig config, int warn);
134static char* feGetExpandedExecutable();
135static BOOLEAN feVerifyResourceValue(feResourceType type, char* value);
136static char* feCleanResourceValue(feResourceType type, char* value);
137static char* feCleanUpFile(char* fname);
138static char* feCleanUpPath(char* path);
139static void mystrcpy(char* d, char* s);
140static char* feSprintf(char* s, const char* fmt, int warn = -1);
141#if defined(ix86_Win) && defined(__GNUC__)
142// utility function of Cygwin32:
143extern "C" int cygwin32_posix_path_list_p (const char *path);
144#endif
145
146/*****************************************************************
147 *
148 * Public functions
149 *
150 *****************************************************************/
151char* feResource(const char* key, int warn)
152{
153  return feResource(feGetResourceConfig(key), warn);
154}
155
156char* feResource(const char id, int warn)
157{
158  return feResource(feGetResourceConfig(id), warn);
159}
160
161char* feGetResource(const char id)
162{
163  return feResource(feGetResourceConfig(id), -1);
164}
165
166char* feResourceDefault(const char id)
167{
168  return feResourceDefault(feGetResourceConfig(id));
169}
170
171char* feResourceDefault(const char* key)
172{
173  return feResourceDefault(feGetResourceConfig(key));
174}
175
176void 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  if (argv0==NULL)
183  {
184    feArgv0 = (char*)omAlloc0(MAXPATHLEN+strlen("/Singular"));
185    getcwd(feArgv0,MAXPATHLEN);
186    strcpy(feArgv0+strlen(feArgv0),"/Singular");
187  }
188  else
189    feArgv0 = omStrDup(argv0);
190#ifdef RESOURCE_DEBUG
191  printf("feInitResources: entering with argv0=%s=\n", feArgv0);
192#endif
193  // init some Resources
194  feResource('b');
195  feResource('r');
196  // don't complain about stuff when initializing SingularPath
197  feResource('s',0);
198
199#if defined(HAVE_SETENV) || defined(HAVE_PUTENV)
200  char* path = feResource('p');
201#ifdef RESOURCE_DEBUG
202  printf("feInitResources: setting path with argv0=%s=\n", path);
203#endif
204#ifdef HAVE_PUTENV
205  if (path != NULL) { char *s=(char *)omAlloc0(strlen(path)+6);
206                      sprintf(s,"PATH=%s",path);
207                      putenv(s);
208                    }
209#else
210  if (path != NULL) setenv("PATH", path, 1);
211#endif
212#endif
213}
214
215void feReInitResources()
216{
217  int i = 0;
218  while (feResourceConfigs[i].key != NULL)
219  {
220    if (feResourceConfigs[i].value[0] != '\0')
221    {
222      if (feResourceConfigs[i].value != NULL)
223        omFree(feResourceConfigs[i].value);
224      feResourceConfigs[i].value = (char *)"";
225    }
226    i++;
227  }
228#ifdef RESOURCE_DEBUG
229  printf("feInitResources: entering with argv0=%s=\n", feArgv0);
230#endif
231  // init some Resources
232  feResource('b');
233  feResource('r');
234  // don't complain about stuff when initializing SingularPath
235  feResource('s',0);
236}
237
238/*****************************************************************
239 *
240 * Local functions
241 *
242 *****************************************************************/
243static feResourceConfig feGetResourceConfig(const char id)
244{
245  int i = 0;
246  while (feResourceConfigs[i].key != NULL)
247  {
248    if (feResourceConfigs[i].id == id) return &(feResourceConfigs[i]);
249    i++;
250  }
251  return NULL;
252}
253
254static feResourceConfig feGetResourceConfig(const char* key)
255{
256  int i = 0;
257  while (feResourceConfigs[i].key != NULL)
258  {
259    if (strcmp(feResourceConfigs[i].key, key) == 0)
260      return &(feResourceConfigs[i]);
261    i++;
262  }
263  return NULL;
264}
265
266static char* feResource(feResourceConfig config, int warn)
267{
268  if (config == NULL) return NULL;
269  if (config->value != NULL && *(config->value) != '\0') return config->value;
270  return feInitResource(config, warn);
271}
272
273static char* feResourceDefault(feResourceConfig config)
274{
275  if (config == NULL) return NULL;
276  char* value = (char*) omAlloc(MAXRESOURCELEN);
277  feSprintf(value, config->fmt, -1);
278  return value;
279}
280
281static char* feInitResource(feResourceConfig config, int warn)
282{
283  assume(config != NULL);
284#ifdef RESOURCE_DEBUG
285  printf("feInitResource: entering for %s\n", config->key);
286#endif
287
288  char value[MAXRESOURCELEN];
289  // now we have to work
290  // First, check Environment variable
291  if (config->env != NULL)
292  {
293    char* evalue = getenv(config->env);
294    if (evalue != NULL)
295    {
296#ifdef RESOURCE_DEBUG
297      printf("feInitResource: Found value from env:%s\n", evalue);
298#endif
299      strcpy(value, evalue);
300      if (config->type == feResBinary  // do not verify binaries
301          ||
302          feVerifyResourceValue(config->type,
303                                feCleanResourceValue(config->type, value)))
304      {
305#ifdef RESOURCE_DEBUG
306        printf("feInitResource: Set value of %s to =%s=\n", config->key, value);
307#endif
308        config->value = omStrDup(value);
309        return config->value;
310      }
311    }
312  }
313
314  *value = '\0';
315  // Special treatment of executable
316  if (config->id == 'S')
317  {
318    char* executable = feGetExpandedExecutable();
319    if (executable != NULL)
320    {
321#ifdef RESOURCE_DEBUG
322      printf("exec:%s\n", executable);
323#endif
324      strcpy(value, executable);
325#ifdef RESOURCE_DEBUG
326      printf("value:%s\n", value);
327#endif
328      omFree(executable);
329    }
330  }
331  // and bindir
332  else if (config->id == 'b')
333  {
334    char* executable = feResource('S');
335#ifdef RESOURCE_DEBUG
336      printf("feInitResource: Get %s from %s\n", config->key, executable);
337#endif
338    if (executable != NULL)
339    {
340      strcpy(value, executable);
341      executable = strrchr(value, DIR_SEP);
342      if (executable != NULL) *executable = '\0';
343    }
344  }
345
346#ifdef RESOURCE_DEBUG
347  printf("value:%s\n", value);
348#endif
349
350  if (*value == '\0' && config->fmt != NULL )
351  {
352    feSprintf(value, config->fmt, warn);
353  }
354  else if (config->fmt == NULL)
355  {
356    sprintf(value, "Wrong Resource Specification of %s", config->key);
357    dReportBug(value);
358    return NULL;
359  }
360
361  // Clean and verify
362  if (feVerifyResourceValue(config->type,
363                            feCleanResourceValue(config->type, value)))
364  {
365#ifdef RESOURCE_DEBUG
366    printf("feInitResource: Set value of %s to =%s=\n", config->key, value);
367#endif
368    config->value = omStrDup(value);
369    return config->value;
370  }
371  else if (config->type == feResBinary)
372  {
373    // for binaries, search through PATH once more
374    char* executable = omFindExec(config->key, value);
375    if (executable != NULL)
376    {
377      if (feVerifyResourceValue(config->type,
378                                feCleanResourceValue(config->type, value)))
379      {
380        config->value = omStrDup(value);
381#ifdef RESOURCE_DEBUG
382        printf("feInitResource: Set value of %s to =%s=\n", config->key, config->value);
383#endif
384        return config->value;
385      }
386    }
387  }
388
389  // issue warning if explicitely requested, or if
390  // this value is gotten for the first time
391  if (warn > 0 || (warn < 0 && config->value != NULL))
392  {
393    Warn("Could not get %s. ", config->key);
394    Warn("Either set environment variable %s to %s,",
395         config->env, config->key);
396    feSprintf(value, config->fmt, warn);
397    Warn("or make sure that %s is at %s", config->key, value);
398  }
399#ifdef RESOURCE_DEBUG
400      printf("feInitResource: Set value of %s to NULL", config->key);
401#endif
402  config->value = NULL;
403  return NULL;
404}
405
406static char* feGetExpandedExecutable()
407{
408  if (feArgv0 == NULL || *feArgv0 == '\0')
409  {
410    if (feArgv0 == NULL) dReportBug("feArgv0 == NULL");
411    else dReportBug("feArgv0 == ''");
412    return NULL;
413  }
414#ifdef ix86_Win // stupid WINNT sometimes gives you argv[0] within ""
415  if (*feArgv0 == '"')
416  {
417    int l = strlen(feArgv0);
418    if (feArgv0[l-1] == '"')
419    {
420      feArgv0[l-1] = '\0';
421      feArgv0++;
422    }
423  }
424#endif
425#ifdef RESOURCE_DEBUG
426  printf("feGetExpandedExecutable: calling find_exec with =%s=\n", feArgv0);
427#endif
428  char executable[MAXRESOURCELEN];
429  char* value = omFindExec(feArgv0, executable);
430#ifdef RESOURCE_DEBUG
431  printf("feGetExpandedExecutable: find_exec exited with =%s=%d\n", executable, access(executable, X_OK));
432#endif
433  if (value == NULL)
434  {
435    char message[MAXRESOURCELEN];
436    sprintf(message, "Could not get expanded executable from %s", feArgv0);
437    dReportBug(message);
438    return NULL;
439  }
440  return omStrDup(value);
441}
442
443
444static BOOLEAN feVerifyResourceValue(feResourceType type, char* value)
445{
446#ifdef RESOURCE_DEBUG
447  printf("feVerifyResourceValue: entering with =%s=\n", value);
448  printf("%d:%d\n", access(value, R_OK), access(value, X_OK));
449#endif
450  switch(type)
451  {
452      case feResUrl:
453      case feResPath:
454        return TRUE;
455
456      case feResFile:
457        return ! access(value, R_OK);
458
459      case feResBinary:
460      case feResDir:
461        return ! access(value, X_OK);
462
463      default:
464        return FALSE;
465  }
466}
467
468/*****************************************************************
469 *
470 * Cleaning/Transformations of resource values
471 *
472 *****************************************************************/
473
474static char* feCleanResourceValue(feResourceType type, char* value)
475{
476  if (value == NULL || *value == '\0') return value;
477#ifdef RESOURCE_DEBUG
478      printf("Clean value:%s\n", value);
479#endif
480#ifdef ix86_Win
481#ifdef RESOURCE_DEBUG
482      printf("Clean WINNT value:%s\n", value);
483#endif
484  if (type == feResBinary)
485  {
486    int l = strlen(value);
487    if (l < 4 || (strcmp(&value[l-4], ".exe") != 0 &&
488                  strcmp(&value[l-4], ".EXE") != 0))
489      strcat(value, ".exe");
490  }
491#endif
492  if (type == feResFile || type == feResBinary || type == feResDir)
493    return feCleanUpFile(value);
494  if (type == feResPath)
495    return feCleanUpPath(value);
496  return value;
497}
498
499static char* feCleanUpFile(char* fname)
500{
501  char* fn, *s;
502
503#ifdef RESOURCE_DEBUG
504  printf("feCleanUpFile: entering with =%s=\n", fname);
505#endif
506  // Remove unnecessary .. and //
507  for (fn = fname; *fn != '\0'; fn++)
508  {
509    if (*fn == '/')
510    {
511      if (*(fn+1) == '\0')
512      {
513        if (fname != fn) *fn = '\0';
514        break;
515      }
516      if (*(fn + 1) == '/' && (fname != fn))
517      {
518        mystrcpy(fn, fn+1);
519        fn--;
520      }
521      else if (*(fn+1) == '.')
522      {
523        if (*(fn+2) == '.' && (*(fn + 3) == '/' || *(fn + 3) == '\0'))
524        {
525          *fn = '\0';
526          s = strrchr(fname, '/');
527          if (s != NULL)
528          {
529            mystrcpy(s+1, fn + (*(fn + 3) != '\0' ? 4 : 3));
530            fn = s-1;
531          }
532          else
533          {
534            *fn = '/';
535          }
536        }
537        else if (*(fn+2) == '/' || *(fn+2) == '\0')
538        {
539          mystrcpy(fn+1, fn+3);
540          fn--;
541        }
542      }
543    }
544  }
545
546#ifdef RESOURCE_DEBUG
547  printf("feCleanUpFile: leaving with =%s=\n", fname);
548#endif
549  return fname;
550}
551
552// remove duplicates dir resp. those which do not exist
553static char* feCleanUpPath(char* path)
554{
555#ifdef RESOURCE_DEBUG
556  printf("feCleanUpPath: entering with: =%s=\n", path);
557#endif
558  if (path == NULL) return path;
559
560  int n_comps = 1, i, j;
561  char* opath = path;
562  char** path_comps;
563
564  for (; *path != '\0'; path++)
565  {
566    if (*path == fePathSep) n_comps++;
567    else if (*path == ';')
568    {
569      *path = fePathSep;
570      n_comps++;
571    }
572  }
573
574  path_comps = (char**) omAlloc(n_comps*sizeof(char*));
575  path_comps[0]=opath;
576  path=opath;
577  i = 1;
578
579  if (i < n_comps)
580  {
581    while (1)
582    {
583      if (*path == fePathSep)
584      {
585        *path = '\0';
586        path_comps[i] = path+1;
587        i++;
588        if (i == n_comps) break;
589      }
590      path++;
591    }
592  }
593
594  for (i=0; i<n_comps; i++)
595    path_comps[i] = feCleanUpFile(path_comps[i]);
596#ifdef RESOURCE_DEBUG
597  PrintS("feCleanUpPath: after CleanUpName: ");
598  for (i=0; i<n_comps; i++)
599    Print("%s:", path_comps[i]);
600  Print("\n");
601#endif
602
603  for (i=0; i<n_comps;)
604  {
605#ifdef RESOURCE_DEBUG
606    if (access(path_comps[i], X_OK | R_OK))
607      Print("feCleanUpPath: remove %d:%s -- can not access\n", i, path_comps[i]);
608#endif
609    if ( ! access(path_comps[i], X_OK | R_OK))
610    {
611      // x- permission is granted -- we assume that it is a dir
612      for (j=0; j<i; j++)
613      {
614        if (strcmp(path_comps[j], path_comps[i]) == 0)
615        {
616          // found a duplicate
617#ifdef RESOURCE_DEBUG
618          Print("feCleanUpPath: remove %d:%s -- equal to %d:%s\n", j, path_comps[j], i, path_comps[i]);
619#endif
620          j = i+1;
621          break;
622        }
623      }
624      if (j == i)
625      {
626        i++;
627        continue;
628      }
629    }
630    // now we can either not access or found a duplicate
631    path_comps[i] = NULL;
632    for (j=i+1; j<n_comps; j++)
633        path_comps[j-1] = path_comps[j];
634    n_comps--;
635  }
636
637
638  // assemble everything again
639  for (path=opath, i=0;i<n_comps-1;i++)
640  {
641    mystrcpy(path, path_comps[i]);
642    path += strlen(path);
643    *path = fePathSep;
644    path++;
645  }
646  if (n_comps)
647  {
648    mystrcpy(path, path_comps[i]);
649  }
650  else
651  {
652    *opath = '\0';
653  }
654  omFree(path_comps);
655#ifdef RESOURCE_DEBUG
656  Print("feCleanUpPath: leaving with path=%s=\n", opath);
657#endif
658  return opath;
659}
660
661// strcpy where source and destination may overlap
662static void mystrcpy(char* d, char* s)
663{
664  assume(d != NULL && s != NULL);
665  while (*s != '\0')
666  {
667    *d = *s;
668    d++;
669    s++;
670  }
671  *d = '\0';
672}
673
674/*****************************************************************
675 *
676 * feSprintf
677 *
678 *****************************************************************/
679static char* feSprintf(char* s, const char* fmt, int warn)
680{
681  char* s_in = s;
682  if (fmt == NULL) return NULL;
683
684  while (*fmt != '\0')
685  {
686    *s = *fmt;
687
688    if (*fmt == '%' && *(fmt + 1) != '\0')
689    {
690      fmt++;
691      char* r = feResource(*fmt, warn);
692      if (r != NULL)
693      {
694        strcpy(s, r);
695        s += strlen(r) - 1;
696      }
697      else
698      {
699        s++;
700        *s = *fmt;
701      }
702    }
703    else if (*fmt == '$' && *(fmt + 1) != '\0')
704    {
705      fmt++;
706      char* v = s + 1;
707      while (*fmt == '_' ||
708             (*fmt >= 'A' && *fmt <= 'Z') ||
709             (*fmt >= 'a' && *fmt <= 'z'))
710      {
711        *v = *fmt;
712        v++;
713        fmt++;
714      }
715      fmt--;
716      *v = '\0';
717      v = getenv(s + 1);
718      if (v != NULL) strcpy(s, v);
719      s += strlen(s) - 1;
720    }
721    s++;
722    fmt++;
723  }
724  *s = '\0';
725  return s_in;
726}
727
728void feStringAppendResources(int warn)
729{
730  int i = 0;
731  char* r;
732  StringAppend("%-10s:\t%s\n", "argv[0]", feArgv0);
733  while (feResourceConfigs[i].key != NULL)
734  {
735    r = feResource(feResourceConfigs[i].key, warn);
736    StringAppend("%-10s:\t%s\n", feResourceConfigs[i].key,
737                 (r != NULL ? r : ""));
738    i++;
739  }
740}
Note: See TracBrowser for help on using the repository browser.