Changeset 9c35ef in git for Singular/febase.cc


Ignore:
Timestamp:
Aug 3, 1999, 6:33:45 PM (25 years ago)
Author:
Olaf Bachmann <obachman@…>
Branches:
(u'fieker-DuVal', '117eb8c30fc9e991c4decca4832b1d19036c4c65')(u'spielwiese', '38dfc5131670d387a89455159ed1e071997eec94')
Children:
b29464faa2e2d8875cccfe3b0e20da4aa8dcd0ee
Parents:
41d8a646a293686ab005a0cbfe1dcd9d35786779
Message:
* implementation of resources and help system


git-svn-id: file:///usr/local/Singular/svn/trunk@3394 2c84dea3-7e68-4137-9b89-c4e89433aadc
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Singular/febase.cc

    r41d8a6 r9c35ef  
    22*  Computer Algebra System SINGULAR     *
    33****************************************/
    4 /* $Id: febase.cc,v 1.76 1999-07-20 12:29:48 Singular Exp $ */
     4/* $Id: febase.cc,v 1.77 1999-08-03 16:33:40 obachman Exp $ */
    55/*
    66* ABSTRACT: i/o system
     
    2525#include "subexpr.h"
    2626#include "ipshell.h"
    27 
    28 #include "si_paths.h"
    29 
    30 #ifndef MAXPATHLEN
    31 #define MAXPATHLEN 1024
    32 #endif
    33 
    34 #ifndef MAXNAMLEN
    35 #define MAXNAMLEN MAXPATHLEN
    36 #endif
     27#include "version.h"
     28
    3729
    3830#define fePutChar(c) fputc((uchar)(c),stdout)
     
    10092#include "febase.inc"
    10193
    102 #ifndef __MWERKS__
    103 /*****************************************************************
    104  *
    105  * PATH STUFF
    106  *
    107  *****************************************************************/
    108 
    109 // Define to chatter about path stuff
    110 // #define PATH_DEBUG
    111 static char* feArgv0 = NULL;
    112 static char* feExpandedExecutable = NULL;
    113 static char* feBinDir = NULL;
    114 static char* feSearchPath = NULL;
    115 static char* feInfoProgram = NULL;
    116 static char* feInfoFile = NULL;
    117 static char* feInfoCall = NULL;
    118 
    119 extern "C" char* find_executable(const char* argv0);
    120 static char* feRemovePathnameHead(const char* expanded_executable);
    121 static char* CleanUpPath(char* path);
    122 static char* CleanUpName(char* filename);
    123 
    124 inline char* feGetExpandedExecutable(const char* argv0)
    125 {
    126   return (argv0 != NULL ? find_executable(argv0) : (char* ) NULL);
    127 }
    128 
    129 inline char* feGetBinDir(const char* expanded_executable)
    130 {
    131   return feRemovePathnameHead(expanded_executable);
    132 }
    133 
    134 // Return the file search path for singular w.r.t. the following steps:
    135 // 1.) SINGULARPATH Env Variable
    136 // 2.) bindir/LIB
    137 // 3.) bindir/LIB/VERSION
    138 // 4.) bindir/../../Singular/LIB
    139 // 5.) bindir/../../Singular/LIB/VERSION
    140 // 6.) ROOT_DIR/Singular/LIB/
    141 // 7.) ROOT_DIR/Singular/LIB/VERSION
    142 // 8.) Go through all dirs and remove duplicates dirs resp.
    143 //     those which do not exist
    144 static char* feGetSearchPath(const char* bindir)
    145 {
    146   char *env = NULL, *path, *opath;
    147   int plength = 0, tmp;
    148 
    149 #ifdef MSDOS
    150     env=getenv("SPATH");
    151 #else
    152     env=getenv("SINGULARPATH");
    153 #endif
    154 #ifdef PATH_DEBUG
    155     PrintS("I'm going to chatter about the Search path:\n");
    156 #endif
    157     if (env != NULL)
    158       plength = strlen(env);
    159 
    160     if (bindir != NULL)
    161       plength += 4*strlen(bindir);
    162 
    163     plength += 2*strlen(SINGULAR_ROOT_DIR)
    164       + 3*(strlen(S_VERSION1) + 1)
    165       + 24         + 36          + 12       + 6          + 7;
    166       // == 6*/LIB + 4*/Singular + 2*/../.. + for colons + some room to breath
    167 
    168     opath = (char*) AllocL(plength*sizeof(char));
    169     path = opath;
    170 
    171     if (env != NULL)
    172     {
    173       strcpy(path, env);
    174       path += strlen(path);
    175       *path=FS_SEP;
    176       path++;
    177 #ifdef PATH_DEBUG
    178       *(path +1) = '\0';
    179       Print("Got from env var: %s\n", opath);
    180 #endif
    181     }
    182 
    183     if (bindir != NULL)
    184     {
    185       sprintf(
    186         path,
    187         "%s/LIB%c%s/LIB/%s%c%s/../../Singular/LIB%c%s/../../Singular/LIB/%s%c",
    188         bindir, FS_SEP,
    189         bindir, S_VERSION1, FS_SEP,
    190         bindir, FS_SEP,
    191         bindir, S_VERSION1, FS_SEP);
    192 #ifdef PATH_DEBUG
    193       Print("From bindir: %s\n", path);
    194 #endif
    195       path += strlen(path);
    196     }
    197 
    198     sprintf(path, "%s/Singular/LIB%c%s/Singular/LIB/%s",
    199             SINGULAR_ROOT_DIR, FS_SEP,
    200             SINGULAR_ROOT_DIR, S_VERSION1);
    201 #ifdef PATH_DEBUG
    202     Print("From rootdir: %s\n", path);
    203 #endif
    204     return CleanUpPath(opath);
    205 }
    206 
    207 static void mystrcpy(char* d, char* s)
    208 {
    209   assume(d != NULL && s != NULL);
    210   while (*s != '\0')
    211   {
    212     *d = *s;
    213     d++;
    214     s++;
    215   }
    216   *d = '\0';
    217 }
    218 
    219 // Return location of file singular.hlp. Search for it as follows:
    220 // bindir/../doc/singular.hlp
    221 // bindir/../info/singular.hlp
    222 // bindir/../../Singular/doc/$version/singular.hlp
    223 // bindir/../../Singular/doc/singular.hlp
    224 // bindir/../../info/singular.hlp
    225 // ROOTDIR/Singular/doc/$version/singular.hlp
    226 // ROOTDIR/Singular/doc/singular.hlp
    227 // ROOTDIR/doc/singular.hlp
    228 // ROOTDIR/info/singular.hlp
    229 // Singular search path
    230 #ifdef WINNT
    231 static char * feFixFileName(char *hlpdir)
    232 {
    233   if(strncmp(hlpdir,"//",2)==0)
    234   {
    235     hlpdir[0]=hlpdir[2];
    236     hlpdir[1]=':';
    237     mystrcpy(hlpdir+2,hlpdir+3);
    238   }
    239   return hlpdir;
    240 }
    241 #else
    242 #define  feFixFileName(A) (A)
    243 #endif
    244 
    245 static char* feGetInfoFile(const char* bindir)
    246 {
    247   char* hlpfile = (char*) AllocL(max((bindir != NULL ? strlen(bindir) : 0),
    248                                      strlen(SINGULAR_ROOT_DIR))
    249                                   + 50);
    250 
    251 #ifdef PATH_DEBUG
    252   PrintS("Search for singular.hlp\n");
    253 #endif
    254 
    255   if (bindir != NULL)
    256   {
    257     // bindir/../doc/singular.hlp
    258     sprintf(hlpfile,"%s/../doc/singular.hlp", bindir);
    259 #ifdef PATH_DEBUG
    260     Print("trying %s -- %s\n", hlpfile, ( access(CleanUpName(hlpfile), R_OK) ? "no" : "yes"));
    261 #endif
    262     if (! access(CleanUpName(hlpfile), R_OK)) return feFixFileName(hlpfile);
    263 
    264     // bindir/../info/singular.hlp
    265     sprintf(hlpfile,"%s/../info/singular.hlp", bindir);
    266 #ifdef PATH_DEBUG
    267     Print("trying %s -- %s\n", hlpfile, ( access(CleanUpName(hlpfile), R_OK) ? "no" : "yes"));
    268 #endif
    269     if (! access(CleanUpName(hlpfile), R_OK)) return feFixFileName(hlpfile);
    270 
    271     // bindir/../../Singular/doc/$version/singular.hlp
    272     sprintf(hlpfile,"%s/../../Singular/doc/%s/singular.hlp",bindir,S_VERSION1);
    273 #ifdef PATH_DEBUG
    274     Print("trying %s -- %s\n", hlpfile, ( access(CleanUpName(hlpfile), R_OK) ? "no" : "yes"));
    275 #endif
    276     if (! access(CleanUpName(hlpfile), R_OK)) return feFixFileName(hlpfile);
    277 
    278     // bindir/../../Singular/doc/singular.hlp
    279     sprintf(hlpfile,"%s/../../Singular/doc/singular.hlp", bindir);
    280 #ifdef PATH_DEBUG
    281     Print("trying %s -- %s\n", hlpfile, ( access(CleanUpName(hlpfile), R_OK) ? "no" : "yes"));
    282 #endif
    283     if (! access(CleanUpName(hlpfile), R_OK)) return feFixFileName(hlpfile);
    284 
    285     // bindir/../../info/singular.hlp
    286     sprintf(hlpfile,"%s/../../info/singular.hlp", bindir);
    287 #ifdef PATH_DEBUG
    288     Print("trying %s -- %s\n", hlpfile, ( access(CleanUpName(hlpfile), R_OK) ? "no" : "yes"));
    289 #endif
    290     if (! access(CleanUpName(hlpfile), R_OK)) return feFixFileName(hlpfile);
    291 
    292     // ROOTDIR/Singular/doc/$version/singular.hlp
    293     sprintf(hlpfile,"%s/Singular/doc/%s/singular.hlp", SINGULAR_ROOT_DIR, S_VERSION1);
    294 #ifdef PATH_DEBUG
    295     Print("trying %s -- %s\n", hlpfile, ( access(CleanUpName(hlpfile), R_OK) ? "no" : "yes"));
    296 #endif
    297     if (! access(CleanUpName(hlpfile), R_OK)) return feFixFileName(hlpfile);
    298 
    299     // ROOTDIR/Singular/doc/singular.hlp
    300     sprintf(hlpfile,"%s/Singular/doc/singular.hlp", SINGULAR_ROOT_DIR);
    301 #ifdef PATH_DEBUG
    302     Print("trying %s -- %s\n", hlpfile, ( access(CleanUpName(hlpfile), R_OK) ? "no" : "yes"));
    303 #endif
    304     if (! access(CleanUpName(hlpfile), R_OK)) return feFixFileName(hlpfile);
    305 
    306     // ROOTDIR/doc/singular.hlp
    307     sprintf(hlpfile,"%s/doc/singular.hlp", SINGULAR_ROOT_DIR);
    308  #ifdef PATH_DEBUG
    309     Print("trying %s -- %s\n", hlpfile, ( access(CleanUpName(hlpfile), R_OK) ? "no" : "yes"));
    310 #endif
    311    if (! access(CleanUpName(hlpfile) , R_OK)) return feFixFileName(hlpfile);
    312 
    313     // ROOTDIR/info/singular.hlp
    314     sprintf(hlpfile,"%s/info/singular.hlp", SINGULAR_ROOT_DIR);
    315  #ifdef PATH_DEBUG
    316     Print("trying %s -- %s\n", hlpfile, ( access(CleanUpName(hlpfile), R_OK) ? "no" : "yes"));
    317 #endif
    318    if (! access(CleanUpName(hlpfile) , R_OK)) return feFixFileName(hlpfile);
    319   }
    320 
    321   // still here? Try all dirs in the search path
    322   FILE *file = feFopen("singular.hlp", "r", hlpfile, 0);
    323   if (file != NULL)
    324   {
    325     fclose(file);
    326     return feFixFileName(hlpfile);
    327   }
    328   *hlpfile = '\0';
    329   return hlpfile;
    330 }
    331 
    332 #ifdef WINNT
    333 #define INFOPROG "info.exe"
    334 #else
    335 #define INFOPROG "info"
    336 #endif
    337 
    338 // we first look into bindir, if nothing found there, we use HAVE_INFO
    339 static char* feGetInfoProgram(const char* bindir)
    340 {
    341   char infoprog[MAXPATHLEN];
    342   if (bindir != NULL)
    343   {
    344     sprintf(infoprog, "%s/%s", bindir, INFOPROG);
    345     if (! access(infoprog, X_OK)) return mstrdup(infoprog);
    346   }
    347 
    348   sprintf(infoprog, "%s/%s", SINGULAR_BIN_DIR, INFOPROG);
    349   if (! access(infoprog, X_OK)) return mstrdup(infoprog);
    350 
    351 #ifdef HAVE_INFO
    352   sprintf(infoprog, "%s", HAVE_INFO);
    353   if (! access(infoprog, X_OK)) return mstrdup(infoprog);
    354 #endif
    355   // nothing found, let's try "info"
    356   sprintf(infoprog, "info");
    357   return mstrdup(infoprog);
    358 }
    359 
    360 #if defined(WINNT) && defined(__GNUC__)
    361 // add utility function of Cygwin32:
    362 extern "C" int cygwin32_posix_path_list_p (const char *path);
    363 #endif
    364 
    365 #ifdef WINNT
    366 static void feExpandPath(char *dir)
    367 {
    368   char *path=getenv("PATH");
    369   char buf[MAXNAMLEN];
    370   if (path==NULL)
    371   {
    372     strcpy(buf,dir);
    373   }
    374   else
    375   {
    376     #if defined(WINNT) && defined(__GNUC__)
    377     char path_delim = cygwin32_posix_path_list_p (path) ? ':' : ';';
    378     #else
    379     char path_delim=FS_SEP;
    380     #endif
    381     sprintf(buf,"%s%c%s",path,path_delim,dir);
    382   }
    383   setenv("PATH",buf,1);
    384 }
    385 #endif
    386 
    387 //
    388 // public routines
    389 //
    390 void feInitPaths(const char* argv0)
    391 {
    392   feArgv0 = mstrdup(argv0);
    393   #ifdef WINNT
    394   // add the bindir and the BIN_DIR to the current PATH:
    395   feExpandPath(feGetBinDir()); // can only be called after setting feArgv0
    396   feExpandPath(SINGULAR_BIN_DIR);
    397   #endif
    398 }
    399 
    400 char* feGetExpandedExecutable()
    401 {
    402   if (feExpandedExecutable == NULL)
    403     feExpandedExecutable = feGetExpandedExecutable(feArgv0);
    404   return feExpandedExecutable;
    405 }
    406 
    407 char* feGetBinDir()
    408 {
    409   if (feBinDir == NULL)
    410     feBinDir = feGetBinDir(feGetExpandedExecutable());
    411   return feBinDir;
    412 }
    413 
    414 char* feGetSearchPath()
    415 {
    416   if (feSearchPath == NULL)
    417     feSearchPath = feGetSearchPath(feGetBinDir());
    418   return feSearchPath;
    419 }
    420 
    421 char* feGetInfoProgram()
    422 {
    423   if (feInfoProgram == NULL)
    424     feInfoProgram = feGetInfoProgram(feGetBinDir());
    425   return feInfoProgram;
    426 }
    427 
    428 char* feGetInfoFile()
    429 {
    430   if (feInfoFile == NULL)
    431     feInfoFile = feGetInfoFile(feGetBinDir());
    432   return feInfoFile;
    433 }
    434 
    435 char* feGetInfoCall(const char* what)
    436 {
    437   if (feInfoCall == NULL)
    438     feInfoCall = (char*) AllocL(strlen(feGetInfoProgram())
    439                                 + strlen(feGetInfoFile())
    440                                 + 100);
    441   char *infofile = feGetInfoFile();
    442 
    443   if (what != NULL && strcmp(what, "index") != 0)
    444     sprintf(feInfoCall,
    445             "%s %s %s Index %s",
    446             feGetInfoProgram(),
    447             (*infofile != '\0' ? "-f" : ""),
    448             (*infofile != '\0' ? infofile : "Singular"),
    449             what);
    450   else
    451     sprintf(feInfoCall,
    452             "%s %s %s",
    453             feGetInfoProgram(),
    454             (*infofile != '\0' ? "-f" : ""),
    455             (*infofile != '\0' ? infofile : "Singular"));
    456 
    457 #ifdef PATH_DEBUG
    458   Print("Info call with: %s \n", feInfoCall);
    459 #endif
    460   return feInfoCall;
    461 }
    462 
    463 //
    464 // auxillary routines
    465 //
    466 static char* feRemovePathnameHead(const char* ef)
    467 {
    468   if (ef != NULL)
    469   {
    470     char* ret = mstrdup(ef);
    471     char* p = strrchr(ret, DIR_SEP);
    472     if (p != NULL) *p = '\0';
    473     return ret;
    474   }
    475   return NULL;
    476 }
    477 
    478 // remove duplicates dir resp. those which do not exist
    479 static char* CleanUpPath(char* path)
    480 {
    481 #ifdef PATH_DEBUG
    482   Print("Entered CleanUpPath with: %s\n", path);
    483 #endif
    484   if (path == NULL) return path;
    485 
    486   int n_comps = 1, i, j;
    487   char* opath = path;
    488   char** path_comps;
    489 
    490   for (; *path != '\0'; path++)
    491   {
    492     if (*path == FS_SEP) n_comps++;
    493   }
    494 
    495 
    496   path_comps = (char**) AllocL(n_comps*sizeof(char*));
    497   path_comps[0]=opath;
    498   path=opath;
    499   i = 1;
    500 
    501   if (i < n_comps)
    502   {
    503     while (1)
    504     {
    505       if (*path == FS_SEP)
    506       {
    507         *path = '\0';
    508         path_comps[i] = path+1;
    509         i++;
    510         if (i == n_comps) break;
    511       }
    512       path++;
    513     }
    514   }
    515 
    516   for (i=0; i<n_comps; i++)
    517     path_comps[i] = CleanUpName(path_comps[i]);
    518 #ifdef PATH_DEBUG
    519   PrintS("After CleanUpName: ");
    520   for (i=0; i<n_comps; i++)
    521     Print("%s:", path_comps[i]);
    522   Print("\n");
    523 #endif
    524 
    525   for (i=0; i<n_comps;)
    526   {
    527 #ifdef PATH_DEBUG
    528     if (access(path_comps[i], X_OK))
    529       Print("remove %d:%s -- can not access\n", i, path_comps[i]);
    530 #endif
    531     if ( ! access(path_comps[i], X_OK))
    532     {
    533       // x- permission is granted -- we assume that it is a dir
    534       for (j=0; j<i; j++)
    535       {
    536         if (strcmp(path_comps[j], path_comps[i]) == 0)
    537         {
    538           // found a duplicate
    539 #ifdef PATH_DEBUG
    540           Print("remove %d:%s -- equal to %d:%s\n", j, path_comps[j], i, path_comps[i]);
    541 #endif
    542           j = i+1;
    543           break;
    544         }
    545       }
    546       if (j == i)
    547       {
    548         i++;
    549         continue;
    550       }
    551     }
    552     // now we can either not access or found a duplicate
    553     path_comps[i] = NULL;
    554     for (j=i+1; j<n_comps; j++)
    555         path_comps[j-1] = path_comps[j];
    556     n_comps--;
    557   }
    558 
    559   // assemble everything again
    560   for (path=opath, i=0;i<n_comps-1;i++)
    561   {
    562     strcpy(path, path_comps[i]);
    563     path += strlen(path);
    564     *path = FS_SEP;
    565     path++;
    566   }
    567   if (n_comps) strcpy(path, path_comps[i]);
    568   FreeL(path_comps);
    569 #ifdef PATH_DEBUG
    570   Print("SearchPath is: %s\n", opath);
    571 #endif
    572   return opath;
    573 }
    574 
    575 static char* CleanUpName(char* fname)
    576 {
    577   char* fn, *s;
    578 
    579   for (fn = fname; *fn != '\0'; fn++)
    580   {
    581     if (*fn == '/')
    582     {
    583       if (*(fn+1) == '\0')
    584       {
    585         if (fname != fn) *fn = '\0';
    586         break;
    587       }
    588       if (*(fn + 1) == '/' && (fname != fn))
    589       {
    590         mystrcpy(fn, fn+1);
    591         fn--;
    592       }
    593       else if (*(fn+1) == '.')
    594       {
    595         if (*(fn+2) == '.' && (*(fn + 3) == '/' || *(fn + 3) == '\0'))
    596         {
    597           *fn = '\0';
    598           s = strrchr(fname, '/');
    599           if (s != NULL)
    600           {
    601             mystrcpy(s+1, fn + (*(fn + 3) != '\0' ? 4 : 3));
    602             fn = s-1;
    603           }
    604           else
    605           {
    606             *fn = '/';
    607           }
    608         }
    609         else if (*(fn+2) == '/' || *(fn+2) == '\0')
    610         {
    611           mystrcpy(fn+1, fn+3);
    612           fn--;
    613         }
    614       }
    615     }
    616   }
    617   return fname;
    618 }
    619 #endif
    62094/*****************************************************************
    62195 *
     
    626100FILE * feFopen(char *path, char *mode, char *where,int useWerror)
    627101{
    628 #ifdef __MWERKS__
    629   FILE * f=myfopen(path,mode);
    630   if (f!=NULL)
    631   {
    632     if (where!=NULL) strcpy(where,path);
    633     return f;
    634   }
    635   char *res;
    636   int idat=strlen(SINGULAR_DATADIR),
    637       ilib=strlen(VERSION_DIR),
    638       ipath=strlen(path);
    639   int ialloc = idat+ilib+ipath+1;
    640   char *env=getenv("SINGULARPATH");
    641   int ienv=0, ii=0;
    642   if (env!=NULL)
    643   {
    644     ienv=strlen(env);
    645     ii=ienv;
    646   }
    647   if (ii<idat) ii = idat;
    648   if (ii==0)
    649   {
    650     if (useWerror)
    651       Werror("cannot open `%s`",path);
    652     return f;
    653   }
    654   res=(char*) AllocL(ialloc);
    655   if (ienv!=0)
    656   {
    657     memcpy(res,env,ienv);
    658     memcpy(res+ienv,path,ipath);
    659     res[ienv+ipath]='\0';
    660     f=myfopen(res,mode);
    661   }
    662   if ((f==NULL)&&(idat!=0))
    663   {
    664     memcpy(res,SINGULAR_DATADIR,idat);
    665     memcpy(res+idat,path,ipath);
    666     res[idat+ipath]='\0';
    667     f=myfopen(res,mode);
    668   }
    669   if ((f==NULL)&&(idat!=0))
    670   {
    671     memcpy(res,SINGULAR_DATADIR,idat);
    672     memcpy(res+idat,VERSION_DIR,ilib);
    673     idat += ilib;
    674     memcpy(res+idat,path,ipath);
    675     res[idat+ipath]='\0';
    676     f=myfopen(res,mode);
    677   }
    678   if (f==NULL)
    679   {
    680     if (useWerror)
    681       Werror("cannot open `%s`",res);
    682   }
    683   else if (where!=NULL)
    684     strcpy(where,res);
    685   FreeL(res);
    686 #else
    687   BOOLEAN tilde = FALSE;
    688102  char longpath[MAXPATHLEN];
    689103  if (path[0]=='~')
     
    703117  {
    704118    char found = 0;
    705     char* spath = feGetSearchPath();
     119    char* spath = feResource('s');
    706120    char *s;
    707121
     
    713127      char *p,*q;
    714128      p = spath;
    715       while( (q=strchr(p, FS_SEP)) != NULL)
     129      while( (q=strchr(p, fePathSep)) != NULL)
    716130      {
    717131        *q = '\0';
    718132        strcpy(s,p);
    719         *q = FS_SEP;
     133        *q = fePathSep;
    720134        strcat(s, DIR_SEPP);
    721135        strcat(s, path);
     
    750164  if ((f==NULL)&&(useWerror))
    751165    Werror("cannot open `%s`",path);
    752 #endif
    753166  return f;
    754167}
     
    944357}
    945358
     359void fePrintReportBug(char* msg, char* file, int line)
     360{
     361  WarnS("YOU HAVE FOUND A BUG IN SINGULAR.");
     362  WarnS("Please, email the following output to singular@mathematik.uni-kl.de");
     363  Warn("Bug occured at %s:%d", file, line);
     364  Warn("Message: %s", msg);
     365  Warn("Version: %s %s (%d) %s %s", S_UNAME, S_VERSION1,
     366       SINGULAR_VERSION_ID,__DATE__,__TIME__);
     367}
     368
    946369extern "C" {
    947370void assume_violation(char* file, int line)
Note: See TracChangeset for help on using the changeset viewer.