Changeset 9c35ef in git for Singular/febase.cc
- Timestamp:
- Aug 3, 1999, 6:33:45 PM (25 years ago)
- Branches:
- (u'fieker-DuVal', '117eb8c30fc9e991c4decca4832b1d19036c4c65')(u'spielwiese', '38dfc5131670d387a89455159ed1e071997eec94')
- Children:
- b29464faa2e2d8875cccfe3b0e20da4aa8dcd0ee
- Parents:
- 41d8a646a293686ab005a0cbfe1dcd9d35786779
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Singular/febase.cc
r41d8a6 r9c35ef 2 2 * Computer Algebra System SINGULAR * 3 3 ****************************************/ 4 /* $Id: febase.cc,v 1.7 6 1999-07-20 12:29:48 SingularExp $ */4 /* $Id: febase.cc,v 1.77 1999-08-03 16:33:40 obachman Exp $ */ 5 5 /* 6 6 * ABSTRACT: i/o system … … 25 25 #include "subexpr.h" 26 26 #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 37 29 38 30 #define fePutChar(c) fputc((uchar)(c),stdout) … … 100 92 #include "febase.inc" 101 93 102 #ifndef __MWERKS__103 /*****************************************************************104 *105 * PATH STUFF106 *107 *****************************************************************/108 109 // Define to chatter about path stuff110 // #define PATH_DEBUG111 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 Variable136 // 2.) bindir/LIB137 // 3.) bindir/LIB/VERSION138 // 4.) bindir/../../Singular/LIB139 // 5.) bindir/../../Singular/LIB/VERSION140 // 6.) ROOT_DIR/Singular/LIB/141 // 7.) ROOT_DIR/Singular/LIB/VERSION142 // 8.) Go through all dirs and remove duplicates dirs resp.143 // those which do not exist144 static char* feGetSearchPath(const char* bindir)145 {146 char *env = NULL, *path, *opath;147 int plength = 0, tmp;148 149 #ifdef MSDOS150 env=getenv("SPATH");151 #else152 env=getenv("SINGULARPATH");153 #endif154 #ifdef PATH_DEBUG155 PrintS("I'm going to chatter about the Search path:\n");156 #endif157 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 breath167 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_DEBUG178 *(path +1) = '\0';179 Print("Got from env var: %s\n", opath);180 #endif181 }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_DEBUG193 Print("From bindir: %s\n", path);194 #endif195 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_DEBUG202 Print("From rootdir: %s\n", path);203 #endif204 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.hlp221 // bindir/../info/singular.hlp222 // bindir/../../Singular/doc/$version/singular.hlp223 // bindir/../../Singular/doc/singular.hlp224 // bindir/../../info/singular.hlp225 // ROOTDIR/Singular/doc/$version/singular.hlp226 // ROOTDIR/Singular/doc/singular.hlp227 // ROOTDIR/doc/singular.hlp228 // ROOTDIR/info/singular.hlp229 // Singular search path230 #ifdef WINNT231 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 #else242 #define feFixFileName(A) (A)243 #endif244 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_DEBUG252 PrintS("Search for singular.hlp\n");253 #endif254 255 if (bindir != NULL)256 {257 // bindir/../doc/singular.hlp258 sprintf(hlpfile,"%s/../doc/singular.hlp", bindir);259 #ifdef PATH_DEBUG260 Print("trying %s -- %s\n", hlpfile, ( access(CleanUpName(hlpfile), R_OK) ? "no" : "yes"));261 #endif262 if (! access(CleanUpName(hlpfile), R_OK)) return feFixFileName(hlpfile);263 264 // bindir/../info/singular.hlp265 sprintf(hlpfile,"%s/../info/singular.hlp", bindir);266 #ifdef PATH_DEBUG267 Print("trying %s -- %s\n", hlpfile, ( access(CleanUpName(hlpfile), R_OK) ? "no" : "yes"));268 #endif269 if (! access(CleanUpName(hlpfile), R_OK)) return feFixFileName(hlpfile);270 271 // bindir/../../Singular/doc/$version/singular.hlp272 sprintf(hlpfile,"%s/../../Singular/doc/%s/singular.hlp",bindir,S_VERSION1);273 #ifdef PATH_DEBUG274 Print("trying %s -- %s\n", hlpfile, ( access(CleanUpName(hlpfile), R_OK) ? "no" : "yes"));275 #endif276 if (! access(CleanUpName(hlpfile), R_OK)) return feFixFileName(hlpfile);277 278 // bindir/../../Singular/doc/singular.hlp279 sprintf(hlpfile,"%s/../../Singular/doc/singular.hlp", bindir);280 #ifdef PATH_DEBUG281 Print("trying %s -- %s\n", hlpfile, ( access(CleanUpName(hlpfile), R_OK) ? "no" : "yes"));282 #endif283 if (! access(CleanUpName(hlpfile), R_OK)) return feFixFileName(hlpfile);284 285 // bindir/../../info/singular.hlp286 sprintf(hlpfile,"%s/../../info/singular.hlp", bindir);287 #ifdef PATH_DEBUG288 Print("trying %s -- %s\n", hlpfile, ( access(CleanUpName(hlpfile), R_OK) ? "no" : "yes"));289 #endif290 if (! access(CleanUpName(hlpfile), R_OK)) return feFixFileName(hlpfile);291 292 // ROOTDIR/Singular/doc/$version/singular.hlp293 sprintf(hlpfile,"%s/Singular/doc/%s/singular.hlp", SINGULAR_ROOT_DIR, S_VERSION1);294 #ifdef PATH_DEBUG295 Print("trying %s -- %s\n", hlpfile, ( access(CleanUpName(hlpfile), R_OK) ? "no" : "yes"));296 #endif297 if (! access(CleanUpName(hlpfile), R_OK)) return feFixFileName(hlpfile);298 299 // ROOTDIR/Singular/doc/singular.hlp300 sprintf(hlpfile,"%s/Singular/doc/singular.hlp", SINGULAR_ROOT_DIR);301 #ifdef PATH_DEBUG302 Print("trying %s -- %s\n", hlpfile, ( access(CleanUpName(hlpfile), R_OK) ? "no" : "yes"));303 #endif304 if (! access(CleanUpName(hlpfile), R_OK)) return feFixFileName(hlpfile);305 306 // ROOTDIR/doc/singular.hlp307 sprintf(hlpfile,"%s/doc/singular.hlp", SINGULAR_ROOT_DIR);308 #ifdef PATH_DEBUG309 Print("trying %s -- %s\n", hlpfile, ( access(CleanUpName(hlpfile), R_OK) ? "no" : "yes"));310 #endif311 if (! access(CleanUpName(hlpfile) , R_OK)) return feFixFileName(hlpfile);312 313 // ROOTDIR/info/singular.hlp314 sprintf(hlpfile,"%s/info/singular.hlp", SINGULAR_ROOT_DIR);315 #ifdef PATH_DEBUG316 Print("trying %s -- %s\n", hlpfile, ( access(CleanUpName(hlpfile), R_OK) ? "no" : "yes"));317 #endif318 if (! access(CleanUpName(hlpfile) , R_OK)) return feFixFileName(hlpfile);319 }320 321 // still here? Try all dirs in the search path322 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 WINNT333 #define INFOPROG "info.exe"334 #else335 #define INFOPROG "info"336 #endif337 338 // we first look into bindir, if nothing found there, we use HAVE_INFO339 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_INFO352 sprintf(infoprog, "%s", HAVE_INFO);353 if (! access(infoprog, X_OK)) return mstrdup(infoprog);354 #endif355 // 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 #endif364 365 #ifdef WINNT366 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 else375 {376 #if defined(WINNT) && defined(__GNUC__)377 char path_delim = cygwin32_posix_path_list_p (path) ? ':' : ';';378 #else379 char path_delim=FS_SEP;380 #endif381 sprintf(buf,"%s%c%s",path,path_delim,dir);382 }383 setenv("PATH",buf,1);384 }385 #endif386 387 //388 // public routines389 //390 void feInitPaths(const char* argv0)391 {392 feArgv0 = mstrdup(argv0);393 #ifdef WINNT394 // add the bindir and the BIN_DIR to the current PATH:395 feExpandPath(feGetBinDir()); // can only be called after setting feArgv0396 feExpandPath(SINGULAR_BIN_DIR);397 #endif398 }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 else451 sprintf(feInfoCall,452 "%s %s %s",453 feGetInfoProgram(),454 (*infofile != '\0' ? "-f" : ""),455 (*infofile != '\0' ? infofile : "Singular"));456 457 #ifdef PATH_DEBUG458 Print("Info call with: %s \n", feInfoCall);459 #endif460 return feInfoCall;461 }462 463 //464 // auxillary routines465 //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 exist479 static char* CleanUpPath(char* path)480 {481 #ifdef PATH_DEBUG482 Print("Entered CleanUpPath with: %s\n", path);483 #endif484 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_DEBUG519 PrintS("After CleanUpName: ");520 for (i=0; i<n_comps; i++)521 Print("%s:", path_comps[i]);522 Print("\n");523 #endif524 525 for (i=0; i<n_comps;)526 {527 #ifdef PATH_DEBUG528 if (access(path_comps[i], X_OK))529 Print("remove %d:%s -- can not access\n", i, path_comps[i]);530 #endif531 if ( ! access(path_comps[i], X_OK))532 {533 // x- permission is granted -- we assume that it is a dir534 for (j=0; j<i; j++)535 {536 if (strcmp(path_comps[j], path_comps[i]) == 0)537 {538 // found a duplicate539 #ifdef PATH_DEBUG540 Print("remove %d:%s -- equal to %d:%s\n", j, path_comps[j], i, path_comps[i]);541 #endif542 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 duplicate553 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 again560 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_DEBUG570 Print("SearchPath is: %s\n", opath);571 #endif572 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 else605 {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 #endif620 94 /***************************************************************** 621 95 * … … 626 100 FILE * feFopen(char *path, char *mode, char *where,int useWerror) 627 101 { 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 #else687 BOOLEAN tilde = FALSE;688 102 char longpath[MAXPATHLEN]; 689 103 if (path[0]=='~') … … 703 117 { 704 118 char found = 0; 705 char* spath = fe GetSearchPath();119 char* spath = feResource('s'); 706 120 char *s; 707 121 … … 713 127 char *p,*q; 714 128 p = spath; 715 while( (q=strchr(p, FS_SEP)) != NULL)129 while( (q=strchr(p, fePathSep)) != NULL) 716 130 { 717 131 *q = '\0'; 718 132 strcpy(s,p); 719 *q = FS_SEP;133 *q = fePathSep; 720 134 strcat(s, DIR_SEPP); 721 135 strcat(s, path); … … 750 164 if ((f==NULL)&&(useWerror)) 751 165 Werror("cannot open `%s`",path); 752 #endif753 166 return f; 754 167 } … … 944 357 } 945 358 359 void 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 946 369 extern "C" { 947 370 void assume_violation(char* file, int line)
Note: See TracChangeset
for help on using the changeset viewer.