source: git/Singular/febase.cc @ b98bfd

spielwiese
Last change on this file since b98bfd was b98bfd, checked in by Olaf Bachmann <obachman@…>, 26 years ago
* fixed search for Singular/LIB git-svn-id: file:///usr/local/Singular/svn/trunk@2039 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 19.9 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: febase.cc,v 1.39 1998-06-03 12:58:35 obachman Exp $ */
5/*
6* ABSTRACT: i/o system
7*/
8
9#include "mod2.h"
10
11#include <stdlib.h>
12#include <stdio.h>
13#include <limits.h>
14#include <stdarg.h>
15#ifndef macintosh
16#include <unistd.h>
17#endif
18#ifdef NeXT
19#include <sys/file.h>
20#endif
21
22#include "tok.h"
23#include "febase.h"
24#include "mmemory.h"
25#include "subexpr.h"
26#include "ipshell.h"
27
28
29#ifndef MAXPATHLEN
30#define MAXPATHLEN 1024
31#endif
32
33#define fePutChar(c) fputc((uchar)(c),stdout)
34/*0 implementation */
35
36char fe_promptstr[]
37#ifdef macintosh
38                   =" \n";
39#else
40                   ="  ";
41#endif
42
43#define INITIAL_PRINT_BUFFER 24*1024
44static int feBufferLength=INITIAL_PRINT_BUFFER;
45static char * feBuffer=(char *)Alloc(INITIAL_PRINT_BUFFER);
46
47int     si_echo = 0;
48int     printlevel = 0;
49#ifndef macintosh
50int     pagelength = 24;
51#else
52int     pagelength = -1;
53#endif
54int     colmax = 80;
55char    prompt_char = '>'; /*1 either '>' or '.'*/
56extern "C" {
57BITSET  verbose = 1
58                  | Sy_bit(V_REDEFINE)
59                  | Sy_bit(V_LOAD_LIB)
60                  | Sy_bit(V_SHOW_USE)
61                  | Sy_bit(V_PROMPT)
62/*                  | Sy_bit(V_DEBUG_MEM) */
63;}
64BOOLEAN errorreported = FALSE;
65BOOLEAN feBatch;
66char *  feErrors=NULL;
67int     feErrorsLen=0;
68
69#ifdef macintosh
70static  int lines = 0;
71static  int cols = 0;
72#endif
73
74const char feNotImplemented[]="not implemented";
75
76BOOLEAN feProt = FALSE;
77FILE*   feProtFile;
78BOOLEAN tclmode=FALSE;
79/* TCL-Protocoll (Singular -x): <char type>:<int length>:<string> \n
80*  E:l:s  error - not implemented yet (use N)
81*  W:l:s  warning
82*  N:l:s  stdout
83*  Q:0:   quit
84*  P:0:   prompt >
85*  P:1:   prompt .
86*  R:l:<ring-name> ring change
87* plan:
88*  O:l:<option/no-option> option change (option)
89*  V:l:<option/no-option> option change (verbose)
90*/
91
92#include "febase.inc"
93
94#ifdef macintosh
95#  define  FS_SEP ','
96#  define  DIR_SEP ':'
97#  define  DIR_SEPP ":"
98#else
99#ifdef MSDOS
100#  define  FS_SEP ';'
101#  define  DIR_SEP '\\'
102#  define  DIR_SEPP "\\"
103#else
104#ifdef atarist
105#  define  FS_SEP ';'
106#  define  DIR_SEP '\\'
107#  define  DIR_SEPP "\\"
108#else  /* unix */
109#  define  FS_SEP ':'
110#  define  DIR_SEP '/'
111#  define  DIR_SEPP "/"
112#endif  /* atarist */
113#endif  /* MSDOS */
114#endif  /* macintosh */
115
116
117
118/*****************************************************************
119 *
120 * PATH STUFF
121 *
122 *****************************************************************/
123static char* feArgv0 = NULL;
124static char* feExpandedExecutable = NULL;
125static char* feBinDir = NULL;
126static char* feSearchPath = NULL;
127static char* feInfoProgram = NULL;
128static char* feInfoFile = NULL;
129static char* feInfoCall = NULL;
130
131extern "C" char* find_executable(const char* argv0);
132static char* feRemovePathnameHead(const char* expanded_executable);
133static char* CleanUpPath(char* path);
134static char* CleanUpName(char* filename);
135
136inline char* feGetExpandedExecutable(const char* argv0)
137{
138  return (argv0 != NULL ? find_executable(argv0) : (char* ) NULL);
139}
140
141inline char* feGetBinDir(const char* expanded_executable)
142{
143  return feRemovePathnameHead(expanded_executable);
144}
145
146// Return the file search path for singular w.r.t. the following steps:
147// 1.) SINGULARPATH Env Variable
148// 2.) bindir/LIB
149// 3.) bindir/LIB/VERSION
150// 4.) bindir/../../Singular/LIB
151// 5.) bindir/../../Singular/LIB/VERSION
152// 6.) ROOT_DIR/Singular/LIB/
153// 7.) ROOT_DIR/Singular/LIB/VERSION
154// 8.) Go through all dirs and remove duplicates dirs resp.
155//     those which do not exist
156static char* feGetSearchPath(const char* bindir)
157{   
158  char *env = NULL, *path, *opath;
159  int plength = 0, tmp;
160 
161#ifdef MSDOS
162    env=getenv("SPATH");
163#else
164    env=getenv("SINGULARPATH");
165#endif
166 
167    if (env != NULL)
168      plength = strlen(env);
169
170    if (bindir != NULL)
171      plength += 4*strlen(bindir);
172
173    plength += 2*strlen(SINGULAR_ROOT_DIR) 
174      + 3*(strlen(S_VERSION1) + 1)
175      + 24         + 36          + 12       + 6          + 7; 
176      // == 6*/LIB + 4*/Singular + 2*/../.. + for colons + some room to breath
177 
178    opath = (char*) AllocL(plength*sizeof(char));
179    path = opath;
180 
181    if (env != NULL)
182    {
183      strcpy(path, env);
184      path += strlen(path);
185      *path=FS_SEP;
186      path++;
187    }
188 
189    if (bindir != NULL)
190    {
191      sprintf(
192        path,
193        "%s/LIB%c%s/LIB/%s%c%s/../../Singular/LIB%c%s/../../Singular/LIB/%s%c",
194        bindir, FS_SEP,
195        bindir, S_VERSION1, FS_SEP,
196        bindir, FS_SEP,
197        bindir, S_VERSION1, FS_SEP);
198      path += strlen(path);
199    }
200   
201    sprintf(path, "%s/Singular/LIB%c%sSingular/LIB/%s",
202            SINGULAR_ROOT_DIR, FS_SEP,
203            SINGULAR_ROOT_DIR, S_VERSION1);
204   
205    return CleanUpPath(opath);
206}
207
208// Return location of file singular.hlp. Search for it as follows:
209// bindir/../doc/singular.hlp
210// bindir/../info/singular.hlp
211// bindir/../../doc/singular.hlp
212// bindir/../../info/singular.hlp
213// ROOTDIR/doc/singular.hlp
214// ROOTDIR/info/singular.hlp
215
216static char* feGetInfoFile(const char* bindir)
217{
218  char* hlpfile = (char*) AllocL(max((bindir != NULL ? strlen(bindir) : 0),
219                                     strlen(SINGULAR_ROOT_DIR))
220                                 + 30);
221  if (bindir != NULL)
222  {
223    sprintf(hlpfile,"%s/../doc/singular.hlp", bindir);
224    if (! access(hlpfile, R_OK)) return hlpfile;
225
226    sprintf(hlpfile,"%s/../info/singular.hlp", bindir);
227    if (! access(hlpfile, R_OK)) return hlpfile;
228
229    sprintf(hlpfile,"%s/../../doc/singular.hlp", bindir);
230    if (! access(hlpfile, R_OK)) return hlpfile;
231
232    sprintf(hlpfile,"%s/../../info/singular.hlp", bindir);
233    if (! access(hlpfile, R_OK)) return hlpfile;
234
235    sprintf(hlpfile,"%s/doc/singular.hlp", SINGULAR_ROOT_DIR);
236    if (! access(hlpfile, R_OK)) return hlpfile;
237
238    sprintf(hlpfile,"%s/info/singular.hlp", SINGULAR_ROOT_DIR);
239    if (! access(hlpfile, R_OK)) return hlpfile;
240  }
241  *hlpfile = '\0';
242  return hlpfile;
243}
244
245// we first look into bindir, if nothing found there, we use HAVE_INFO
246static char* feGetInfoProgram(const char* bindir)
247{
248#ifdef HAVE_INFO
249  char* infoprog = (char* ) AllocL(max(strlen(HAVE_INFO), 
250                                       (bindir != NULL ? strlen(bindir) : 0)) 
251                                   + 10);
252#else
253  char* infoprog = (char*) AllocL((bindir != NULL ? strlen(bindir)) + 10);
254#endif
255 
256  if (bindir != NULL)
257  {
258    sprintf(infoprog, "%s/info", bindir);
259    if (! access(infoprog, X_OK)) return infoprog;
260  }
261 
262#ifdef HAVE_INFO
263  sprintf(infoprog, "%s", HAVE_INFO);
264  if (! access(infoprog, X_OK)) return infoprog;
265#endif
266  // nothing found, let's try "info"
267  sprintf(infoprog, "info");
268  return infoprog;
269}
270
271//
272// public routines
273//
274void feInitPaths(const char* argv0)
275{
276  feArgv0 = mstrdup(argv0);
277}
278
279char* feGetExpandedExecutable()
280{
281  if (feExpandedExecutable == NULL)
282    feExpandedExecutable = feGetExpandedExecutable(feArgv0);
283  return feExpandedExecutable;
284}
285
286char* feGetBinDir()
287{
288  if (feBinDir == NULL)
289    feBinDir = feGetBinDir(feGetExpandedExecutable());
290  return feBinDir;
291}
292
293char* feGetSearchPath()
294{
295  if (feSearchPath == NULL)
296    feSearchPath = feGetSearchPath(feGetBinDir());
297  return feSearchPath;
298}
299
300char* feGetInfoProgram()
301{
302  if (feInfoProgram == NULL)
303    feInfoProgram = feGetInfoProgram(feGetBinDir());
304  return feInfoProgram;
305}
306
307char* feGetInfoFile()
308{
309  if (feInfoFile == NULL)
310    feInfoFile = feGetInfoFile(feGetBinDir());
311  return feInfoFile;
312}
313
314char* feGetInfoCall(const char* what)
315{
316  if (feInfoCall == NULL)
317    feInfoCall = (char*) AllocL(strlen(feGetInfoProgram())
318                                + strlen(feGetInfoFile())
319                                + 100);
320  char *infofile = feGetInfoFile();
321 
322  if (what != NULL && strcmp(what, "index") != 0)
323    sprintf(feInfoCall,
324            "%s %s %s Index %s", 
325            feGetInfoProgram(), 
326            (*infofile != '\0' ? "-f" : ""),
327            (*infofile != '\0' ? infofile : "Singular"), 
328            what);
329  else
330    sprintf(feInfoCall,
331            "%s %s %s", 
332            feGetInfoProgram(), 
333            (*infofile != '\0' ? "-f" : ""),
334            (*infofile != '\0' ? infofile : "Singular"));
335  return feInfoCall;
336}
337
338//
339// auxillary routines
340//
341static char* feRemovePathnameHead(const char* ef)
342{
343  if (ef != NULL)
344  {
345    char* ret = mstrdup(ef);
346    char* p = strrchr(ret, DIR_SEP);
347    if (p != NULL) *p = '\0';
348    return ret;
349  }
350  return NULL;
351}
352
353// remove duplicates dir resp. those which do not exist
354static char* CleanUpPath(char* path)
355{
356  if (path == NULL) return path;
357
358  int n_comps = 1, i, j;
359  char* opath = path;
360  char** path_comps;
361 
362  for (; *path != '\0'; path++)
363  {
364    if (*path == FS_SEP) n_comps++;
365  }
366 
367
368  path_comps = (char**) AllocL(n_comps*sizeof(char*));
369  path_comps[0]=opath;
370  path=opath;
371  i = 1;
372 
373  if (i < n_comps)
374  {
375    while (1)
376    {
377      if (*path == FS_SEP)
378      {
379        *path = '\0';
380        path_comps[i] = path+1;
381        i++;
382        if (i == n_comps) break;
383      }
384      path++;
385    }
386  }
387 
388  for (i=0; i<n_comps; i++)
389    path_comps[i] = CleanUpName(path_comps[i]);
390
391 
392  for (i=0; i<n_comps;)
393  {
394    if ( ! access(path_comps[i], X_OK))
395    {
396      // x- permission is granted -- we assume that it is a dir
397      for (j=0; j<i; j++)
398      {
399        if (strcmp(path_comps[j], path_comps[i]) == 0)
400        {
401          // found a duplicate
402          j = i+1;
403          break;
404        }
405      }
406      if (j == i) 
407      {
408        i++;
409        continue;
410      }
411    }
412    // now we can either not access or found a duplicate
413    path_comps[i] = NULL;
414    for (j=i+1; j<n_comps; j++)
415        path_comps[j-1] = path_comps[j];
416    n_comps--;
417  }
418
419  // assemble everything again
420  for (path=opath, i=0;i<n_comps-1;i++)
421  {
422    strcpy(path, path_comps[i]);
423    path += strlen(path);
424    *path = FS_SEP;
425    path++;
426  }
427  if (n_comps) strcpy(path, path_comps[i]);
428  FreeL(path_comps);
429  return opath;
430}
431
432static void mystrcpy(char* d, char* s)
433{
434  assume(d != NULL && s != NULL);
435  while (*s != '\0')
436  {
437    *d = *s;
438    d++;
439    s++;
440  }
441  *d = '\0';
442}
443
444static char* CleanUpName(char* fname)
445{
446  char* fn, *s;
447 
448  for (fn = fname; *fn != '\0'; fn++)
449  {
450    if (*fn == '/')
451    {
452      if (*(fn+1) == '\0')
453      {
454        if (fname != fn) *fn = '\0';
455        break;
456      }
457      if (*(fn + 1) == '/')
458      {
459        mystrcpy(fn, fn+1);
460        fn--;
461      }
462      else if (*(fn+1) == '.')
463      {
464        if (*(fn+2) == '.' && (*(fn + 3) == '/' || *(fn + 3) == '\0'))
465        {
466          *fn = '\0';
467          s = strrchr(fname, '/');
468          if (s != NULL)
469          {
470            mystrcpy(s+1, fn+3);
471            fn = s-1;
472          }
473          else
474          {
475            *fn = '/';
476          }
477        }
478        else if (*(fn+2) == '/' || *(fn+2) == '\0')
479        {
480          mystrcpy(fn+1, fn+3);
481          fn--;
482        }
483      }
484    }
485  }
486  return fname;
487}
488
489/*****************************************************************
490 *
491 * File handling
492 *
493 *****************************************************************/
494
495FILE * feFopen(char *path, char *mode, char *where,int useWerror)
496{
497#ifdef __MWERKS__
498  FILE * f=myfopen(path,mode);
499  if (f!=NULL)
500  {
501    if (where!=NULL) strcpy(where,path);
502    return f;
503  }
504  char *res;
505  int idat=strlen(SINGULAR_DATADIR),ipath=strlen(path);
506  char *env=getenv("SINGULARPATH");
507  int ienv=0, ii=0;
508  if (env!=NULL)
509  {
510    ienv=strlen(env);
511    ii=ienv;
512  }
513  if (ii<idat) ii = idat;
514  if (ii==0)
515  {
516    if (useWerror)
517      Werror("cannot open `%s`",path);
518    return f;
519  }
520  res=(char*) AllocL(ii+ipath+1);
521  if (ienv!=0)
522  {
523    memcpy(res,env,ienv);
524    memcpy(res+ienv,path,ipath);
525    res[ienv+ipath]='\0';
526    f=myfopen(res,mode);
527  }
528  if ((f==NULL)&&(idat!=0))
529  {
530    memcpy(res,SINGULAR_DATADIR,idat);
531    memcpy(res+idat,path,ipath);
532    res[idat+ipath]='\0';
533    f=myfopen(res,mode);
534  }
535  if (f==NULL)
536  {
537    if (useWerror)
538      Werror("cannot open `%s`",res);
539  }
540  else if (where!=NULL)
541    strcpy(where,res);
542  FreeL(res);
543#else
544  BOOLEAN tilde = FALSE;
545  char longpath[MAXPATHLEN];
546  if (path[0]=='~')
547  {
548    char* home = getenv("HOME");
549    if (home != NULL)
550    {
551      strcpy(longpath, home);
552      strcat(longpath, &(path[1]));
553      path = longpath;
554    }
555  }
556  FILE * f=myfopen(path,mode);
557  if (where!=NULL) strcpy(where,path);
558  if ((*mode=='r') && (path[0]!=DIR_SEP)&&(path[0]!='.')
559  &&(f==NULL))
560  {
561    char found = 0;
562    char* spath = feGetSearchPath();
563    char *s;
564
565    if (where==NULL) s=(char *)AllocL(250);
566    else             s=where;
567
568    if (spath!=NULL)
569    {
570      char *p,*q;
571      p = spath;
572      while( (q=strchr(p, FS_SEP)) != NULL)
573      {
574        *q = '\0';
575        strcpy(s,p);
576        *q = FS_SEP;
577        strcat(s, DIR_SEPP);
578        strcat(s, path);
579        #ifndef macintosh
580          if(!access(s, R_OK)) { found++; break; }
581        #else
582          f=fopen(s,mode); /* do not need myfopen: we test only the access */
583          if (f!=NULL)  { found++; fclose(f); break; }
584        #endif
585        p = q+1;
586      }
587      if(!found)
588      {
589        strcpy(s,p);
590        strcat(s, DIR_SEPP);
591        strcat(s, path);
592      }
593      f=myfopen(s,mode);
594      if (f!=NULL)
595      {
596        if (where==NULL) FreeL((ADDRESS)s);
597        return f;
598      }
599    }
600    else
601    {
602      if (where!=NULL) strcpy(s/*where*/,path);
603      f=myfopen(path,mode);
604    }
605    if (where==NULL) FreeL((ADDRESS)s);
606  }
607  if ((f==NULL)&&(useWerror))
608    Werror("cannot open `%s`",path);
609#endif
610  return f;
611}
612
613static char * feBufferStart;
614  /* only used in StringSet(S)/StringAppend(S)*/
615char * StringAppend(char *fmt, ...)
616{
617  va_list ap;
618  char *s = feBufferStart; /*feBuffer + strlen(feBuffer);*/
619  int more;
620  va_start(ap, fmt);
621  if ((more=feBufferStart-feBuffer+strlen(fmt)+100)>feBufferLength)
622  {
623    more = ((more + (4*1024-1))/(4*1024))*(4*1024);
624    int l=s-feBuffer;
625    feBuffer=(char *)ReAlloc((ADDRESS)feBuffer,feBufferLength,
626                                                     more);
627    feBufferLength=more;
628    s=feBuffer+l;
629#ifndef BSD_SPRINTF
630    feBufferStart=s;
631#endif
632  }
633#ifdef BSD_SPRINTF
634  vsprintf(s, fmt, ap);
635  while (*s!='\0') s++;
636  feBufferStart =s;
637#else
638  feBufferStart += vsprintf(s, fmt, ap);
639#endif
640  va_end(ap);
641  return feBuffer;
642}
643
644char * StringAppendS(char *st)
645{
646  /* feBufferStart is feBuffer + strlen(feBuffer);*/
647  int more,l;
648  int ll=feBufferStart-feBuffer;
649  if ((more=ll+2+(l=strlen(st)))>feBufferLength)
650  {
651    more = ((more + (4*1024-1))/(4*1024))*(4*1024);
652    feBuffer=(char *)ReAlloc((ADDRESS)feBuffer,feBufferLength,
653                                                     more);
654    feBufferLength=more;
655    feBufferStart=feBuffer+ll;
656  }
657  strcat(feBufferStart, st);
658  feBufferStart +=l;
659  return feBuffer;
660}
661
662char * StringSet(char *fmt, ...)
663{
664  va_list ap;
665  char *s = feBuffer;
666  va_start(ap, fmt);
667#ifdef BSD_SPRINTF
668  vsprintf(s, fmt, ap);
669  while (*s!='\0') s++;
670  feBufferStart = s;
671#else
672  feBufferStart = feBuffer + vsprintf(s, fmt, ap);
673#endif
674  va_end(ap);
675  return feBuffer;
676}
677
678char * StringSetS(char *st)
679{
680  int more,l;
681  if ((l=strlen(st))>feBufferLength)
682  {
683    more = ((l + (4*1024-1))/(4*1024))*(4*1024);
684    feBuffer=(char *)ReAlloc((ADDRESS)feBuffer,feBufferLength,
685                                                     more);
686    feBufferLength=more;
687  }
688  strcpy(feBuffer,st);
689  feBufferStart=feBuffer+l;
690  return feBuffer;
691}
692
693void PrintTCLS(char c, char *s)
694{
695#ifndef macintosh
696  int l=strlen(s);
697  if (l>0) PrintTCL(c,l,s);
698#endif
699}
700
701extern "C" {
702void WerrorS(const char *s)
703{
704#ifdef HAVE_MPSR
705  if (feBatch)
706  {
707    if (feErrors==NULL)
708    {
709      feErrors=(char *)Alloc(256);
710      feErrorsLen=256;
711      strcpy(feErrors,(char *)s);
712    }
713    else
714    {
715      if (((int)(strlen((char *)s)+strlen(feErrors)))>=feErrorsLen)
716      {
717        feErrors=(char *)ReAlloc(feErrors,feErrorsLen,feErrorsLen+256);
718        feErrorsLen+=256;
719      }
720      strcat(feErrors,(char *)s);
721    }
722    strcat(feErrors,"\n");
723  }
724  else
725#endif
726  {
727#ifdef HAVE_TCL
728    if (tclmode)
729    {
730      //PrintTCLS('E',(char *)s);
731      //PrintTCLS('E',"\n");
732      PrintTCLS('N',(char *)s);
733      PrintTCLS('N',"\n");
734    }
735    else
736#endif
737    {
738      fwrite("   ? ",1,5,stderr);
739      fwrite((char *)s,1,strlen((char *)s),stderr);
740      fwrite("\n",1,1,stderr);
741      fflush(stderr);
742      if (feProt&PROT_O)
743      {
744        fwrite("   ? ",1,5,feProtFile);
745        fwrite((char *)s,1,strlen((char *)s),feProtFile);
746        fwrite("\n",1,1,feProtFile);
747      }
748    }
749  }
750  errorreported = TRUE;
751}
752
753void Werror(char *fmt, ...)
754{
755  va_list ap;
756  va_start(ap, fmt);
757  char *s=(char *)Alloc(256);
758  vsprintf(s, fmt, ap);
759  WerrorS(s);
760  Free(s,256);
761  va_end(ap);
762}
763}
764
765void WarnS(const char *s)
766{
767#ifdef HAVE_TCL
768  if (tclmode)
769  {
770    PrintTCLS('W',s);
771  }
772  else
773#endif
774  {
775    fwrite("// ** ",1,6,stdout);
776    fwrite(s,1,strlen(s),stdout);
777    fwrite("\n",1,1,stdout);
778    fflush(stdout);
779    if (feProt&PROT_O)
780    {
781      fwrite("// ** ",1,6,feProtFile);
782      fwrite(s,1,strlen(s),feProtFile);
783      fwrite("\n",1,1,feProtFile);
784    }
785  }
786}
787
788void Warn(const char *fmt, ...)
789{
790  va_list ap;
791  va_start(ap, fmt);
792  char *s=(char *)Alloc(256);
793  vsprintf(s, fmt, ap);
794  WarnS(s);
795  Free(s,256);
796  va_end(ap);
797}
798
799#ifdef macintosh
800void mwrite(uchar c)
801{
802  if (c == '\n')
803  {
804    cols = 0;
805    if (lines == pagelength)
806    {
807      lines = 0;
808      fePause();
809    }
810    else
811    {
812      lines++;
813      fePutChar(c);
814    }
815  }
816  else
817  {
818    fePutChar(c);
819    cols++;
820    if (cols == colmax)
821    {
822//      cols = 0;   //will be done by mwrite('\n');
823      mwrite('\n');
824    }
825  }
826}
827#endif
828
829void PrintS(char *s)
830{
831#ifdef macintosh
832  char c;
833  while ('\0' != (c = *s++))
834  {
835    mwrite(c);
836  }
837#else
838#ifdef HAVE_TCL
839  if (tclmode)
840  {
841    PrintTCLS('N',s);
842  }
843  else
844#endif
845  {
846    fwrite(s,1,strlen(s),stdout);
847    fflush(stdout);
848    if (feProt&PROT_O)
849    {
850      fwrite(s,1,strlen(s),feProtFile);
851    }
852  }
853#endif
854}
855
856void PrintLn()
857{
858  PrintS("\n");
859}
860
861void Print(char *fmt, ...)
862{
863  va_list ap;
864  va_start(ap, fmt);
865#ifdef HAVE_TCL
866  if(tclmode)
867#endif
868#if (defined(HAVE_TCL) || defined(macintosh))
869  {
870    char *s=(char *)Alloc(strlen(fmt)+256);
871    vsprintf(s,fmt, ap);
872#ifdef HAVE_TCL
873    PrintTCLS('N',s);
874#endif
875#ifdef macintosh
876  char c;
877  while ('\0' != (c = *s++))
878  {
879    mwrite(c);
880  }
881  if (feProt&PROT_O)
882  {
883    vfprintf(feProtFile,fmt,ap);
884  }
885#endif
886  }
887#endif
888#if !defined(macintosh) || defined(HAVE_TCL)
889#ifdef HAVE_TCL
890  else
891#endif
892  {
893    vfprintf(stdout, fmt, ap);
894    fflush(stdout);
895    if (feProt&PROT_O)
896    {
897      vfprintf(feProtFile,fmt,ap);
898    }
899  }
900#endif
901  va_end(ap);
902}
903
904void fePause()
905{
906  uchar c;
907  mflush();
908#ifndef macintosh
909  fputs("pause>",stderr);
910#else
911  fputs("pause>\n",stderr);
912#endif
913  c = fgetc(stdin);
914  if (((c == '\003') || (c == 'C')) || (c == 'c'))
915  {
916    m2_end(1);
917  }
918}
919
920void monitor(char* s, int mode)
921{
922  if (feProt)
923  {
924    fclose(feProtFile);
925  }
926  if ((s!=NULL) && (*s!='\0'))
927  {
928    feProtFile = myfopen(s,"w");
929    if (feProtFile==NULL)
930    {
931      Werror("cannot open %s",s);
932    }
933    else
934      feProt = (BOOLEAN)mode;
935  }
936}
937
938
939char* eati(char *s, int *i)
940{
941  int l=0;
942
943  if    (*s >= '0' && *s <= '9')
944  {
945    *i = 0;
946    while (*s >= '0' && *s <= '9')
947    {
948      *i *= 10;
949      *i += *s++ - '0';
950      l++;
951      if ((l>MAX_INT_LEN)||((*i) <0))
952      {
953        s-=l;
954        Werror("`%s` greater than %d(max. integer representation)",
955                s,INT_MAX);
956        return s;
957      }
958    }
959  }
960  else *i = 1;
961  return s;
962}
963
964#ifndef unix
965// Make sure that mode contains binary option
966FILE *myfopen(char *path, char *mode)
967{
968  char mmode[4];
969  int i;
970  BOOLEAN done = FALSE;
971 
972  for (i=0;;i++)
973  {
974    mmode[i] = mode[i];
975    if (mode[i] == '\0') break;
976    if (mode[i] == 'b') done = TRUE;
977  }
978 
979  if (! done)
980  {
981    mmode[i] = 'b';
982    mmode[i+1] = '\0';
983  }
984  return fopen(path, mmode);
985}
986#endif
987
988// replace "\r\n" by " \n" and "\r" by "\n"
989
990size_t myfread(void *ptr, size_t size, size_t nmemb, FILE *stream)
991{
992  size_t got = fread(ptr, size, nmemb, stream) * size;
993  size_t i;
994
995  for (i=0; i<got; i++)
996  {
997    if ( ((char*) ptr)[i] == '\r')
998    {
999      if (i+1 < got && ((char*) ptr)[i+1] == '\n')
1000        ((char*) ptr)[i] = ' ';
1001      else
1002        ((char*) ptr)[i] = '\n';
1003    }
1004  }
1005  return got;
1006}
Note: See TracBrowser for help on using the repository browser.