source: git/Singular/misc.cc @ cc94b0a

spielwiese
Last change on this file since cc94b0a was 057e93c, checked in by Hans Schönemann <hannes@…>, 26 years ago
* Fri Feb 27 15:02:10 MET 1998 hannes new input scheme: many modifications to febase.h, febase.inc, febase.cc, scanner.l, grammar.y, iplib.cc, ipshell.{h,cc} git-svn-id: file:///usr/local/Singular/svn/trunk@1183 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 16.3 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT:
6*/
7
8#include <string.h>
9#include "mod2.h"
10#ifndef macintosh
11#include <unistd.h>
12#endif
13#include <stdio.h>
14#include <stddef.h>
15#include <stdlib.h>
16#include <time.h>
17#include <limits.h>
18
19#include "tok.h"
20#include "febase.h"
21#include "cntrlc.h"
22#include "mmemory.h"
23#include "ipid.h"
24#include "ipshell.h"
25#include "kstd1.h"
26#include "subexpr.h"
27#include "timer.h"
28#include "intvec.h"
29#define SI_DONT_HAVE_GLOBAL_VARS
30
31#ifdef HAVE_FACTORY
32#include <factory.h>
33#endif
34
35/* version strings */
36#ifdef HAVE_LIBFAC_P
37  extern const char * libfac_version;
38  extern const char * libfac_date;
39#endif
40#ifdef HAVE_GMP
41extern "C" {
42#include <gmp.h>
43}
44#endif
45#ifdef HAVE_MPSR
46#include <MP_Config.h>
47#endif
48
49/*0 implementation*/
50
51/*2
52* initialize components of Singular
53*/
54int inits(void)
55{
56  int t;
57/*4 signal handler:*/
58  init_signals();
59/*4 randomize: */
60  t=initTimer();
61  /*t=(int)time(NULL);*/
62  if (t==0) t=1;
63#ifdef HAVE_RTIMER
64  initRTimer();
65#endif
66#ifdef buildin_rand
67  siSeed=t;
68#else
69  srand((unsigned int)t);
70#endif
71#ifdef HAVE_FACTORY
72  factoryseed(t);
73#endif
74/*4 private data of other modules*/
75  memset(&sLastPrinted,0,sizeof(sleftv));
76  sLastPrinted.rtyp=NONE;
77#ifdef HAVE_MPSR
78  extern void mpsr_Init();
79  mpsr_Init();
80#endif
81  return t;
82}
83
84/*2
85* the global exit routine of Singular
86*/
87extern "C" {
88void m2_end(short i)
89{
90  if (i==0)
91  {
92    #ifdef HAVE_TCL
93    if (tclmode)
94      PrintTCL('Q',0,NULL);
95    else
96    #endif
97      printf("Auf Wiedersehen.\n");
98  }
99  else
100  {
101    #ifdef HAVE_TCL
102    if (tclmode)
103      PrintTCL('Q',0,NULL);
104    else
105    #endif
106      printf("\nhalt %d\n",i);
107  }
108  exit(i);
109}
110}
111
112/*2
113* the renice routine for very large jobs
114* works only on unix machines,
115* testet on : linux, HP 9.0
116*
117*#ifndef MSDOS
118*#ifndef macintosh
119*#include <sys/times.h>
120*#include <sys/resource.h>
121*extern "C" int setpriority(int,int,int);
122*void very_nice()
123*{
124*#ifndef NO_SETPRIORITY
125*  setpriority(PRIO_PROCESS,0,19);
126*#endif
127*  sleep(10);
128*}
129*#else
130*void very_nice(){}
131*#endif
132*#else
133*void very_nice(){}
134*#endif
135*/
136
137/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
138#ifdef buildin_rand
139/*
140 *
141 *  A prime modulus multiplicative linear congruential
142 *  generator (PMMLCG), or "Lehmer generator".
143 *  Implementation directly derived from the article:
144 *
145 *        S. K. Park and K. W. Miller
146 *        Random Number Generators: Good Ones are Hard to Find
147 *        CACM vol 31, #10. Oct. 1988. pp 1192-1201.
148 *
149 *  Using the following multiplier and modulus, we obtain a
150 *  generator which:
151 *
152 *        1)  Has a full period: 1 to 2^31 - 2.
153 *        2)  Is testably "random" (see the article).
154 *        3)  Has a known implementation by E. L. Schrage.
155 */
156
157
158#define  A        16807L        /*  A "good" multiplier          */
159#define  M   2147483647L        /*  Modulus: 2^31 - 1          */
160#define  Q       127773L        /*  M / A                  */
161#define  R         2836L        /*  M % A                  */
162
163
164int siSeed = 1L;
165
166
167int siRand()
168{
169  siSeed = A * (siSeed % Q) - R * (siSeed / Q);
170
171  if ( siSeed < 0 )
172    siSeed += M;
173
174  return( siSeed );
175}
176#endif
177
178/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
179#define HELP_OK        0
180
181#ifdef buildin_help
182
183#define FIN_INDEX    '\037'
184#define not  !
185#define HELP_NOT_OPEN  1
186#define HELP_NOT_FOUND 2
187#ifndef macintosh
188#define Index_File     SINGULAR_INFODIR "/singular.hlp"
189#define Help_File      SINGULAR_INFODIR "/singular.hlp"
190#else
191#define Index_File     SINGULAR_INFODIR "singular.hlp"
192#define Help_File      SINGULAR_INFODIR "singular.hlp"
193#endif
194#define BUF_LEN        128
195#define IDX_LEN        64
196#define MAX_LINES      21
197
198static int compare(char *s1,char *s2)
199{
200  for(;*s1==*s2;s1++,s2++)
201     if(*s2=='\0') return(0);
202  return(*s2);
203}
204
205#ifdef macintosh
206static char tolow(char p)
207#else
208static inline char tolow(char p)
209#endif
210{
211  if (('A'<=p)&&(p<='Z')) return p | 040;
212  return p;
213}
214
215/*************************************************/
216static int show(unsigned long offset,FILE *help, char *close)
217{ char buffer[BUF_LEN+1];
218  int  lines = 0;
219
220  if( help== NULL)
221    if( (help = feFopen(Help_File, "r")) == NULL)
222      return HELP_NOT_OPEN;
223
224  fseek(help,  (long)(offset+1), (int)0);
225  while( !feof(help)
226        && *fgets(buffer, BUF_LEN, help) != EOF
227        && buffer[0] != FIN_INDEX)
228  {
229    printf("%s", buffer);
230    if(lines++> MAX_LINES)
231    {
232#ifdef macintosh
233      printf("\n Press <RETURN> to continue or x and <RETURN> to exit help.\n");
234#else
235      printf("\n Press <RETURN> to continue or x to exit help.\n");
236#endif
237      fflush(stdout);
238      *close = (char)getchar();
239      if(*close=='x')
240      {
241        getchar();
242        break;
243      }
244      lines=0;
245    }
246  }
247  if(*close!='x')
248  {
249#ifdef macintosh
250    printf("\nEnd of part. Press <RETURN> to continue or x and <RETURN> to exit help.\n");
251#else
252    printf("\nEnd of part. Press <RETURN> to continue or x to exit help.\n");
253#endif
254    fflush(stdout);
255    *close = (char)getchar();
256    if(*close=='x')
257      getchar();
258  }
259  return HELP_OK;
260}
261
262/*************************************************/
263int singular_manual(char *str)
264{ FILE *index=NULL,*help=NULL;
265  unsigned long offset;
266  char *p,close;
267  int done = 0;
268  char buffer[BUF_LEN+1],
269       Index[IDX_LEN+1],
270       String[IDX_LEN+1];
271
272  if( (index = feFopen(Index_File, "r",NULL,TRUE)) == NULL)
273  {
274    return HELP_NOT_OPEN;
275  }
276
277  for(p=str; *p; p++) *p = tolow(*p);/* */
278  do
279  {
280    p--;
281  }
282  while ((p != str) && (*p<=' '));
283  p++;
284  *p='\0';
285  (void)sprintf(String, " %s ", str);
286
287  while(!feof(index)
288        && *fgets(buffer, BUF_LEN, index) != EOF
289        && buffer[0] != FIN_INDEX);
290
291  while(!feof(index))
292  {
293    (void)fgets(buffer, BUF_LEN, index); /* */
294    (void)sscanf(buffer, "Node:%[^\177]\177%ld\n", Index, &offset);
295    for(p=Index; *p; p++) *p = tolow(*p);/* */
296    (void)strcat(Index, " ");
297    if( strstr(Index, String)!=NULL)
298    {
299      done++; (void)show(offset, help, &close);
300    }
301    Index[0]='\0';
302    if(close=='x')
303    break;
304  }
305  (void)fclose(index);
306  (void)fclose(help);
307  if(not done)
308  {
309    Warn("`%s` not found",String);
310    return HELP_NOT_FOUND;
311  }
312  return HELP_OK;
313}
314#endif
315/*************************************************/
316
317void singular_help(char *str,BOOLEAN example)
318{
319  char *s=str;
320  while (*s==' ') s++;
321  char *ss=s;
322  while (*ss!='\0') ss++;
323  while (*ss<=' ')
324  {
325    *ss='\0';
326    ss--;
327  }
328  /* --------- is it a proc ? --------------------------------*/
329  idhdl h=idroot->get(s,myynest);
330  if ((h!=NULL) && (IDTYP(h)==PROC_CMD))
331  {
332    char *lib=iiGetLibName(IDPROC(h));
333    Print("// proc %s ",s);
334    if((lib==NULL)||(*lib=='\0'))
335    {
336      PrintLn();
337    }
338    else
339    {
340      Print("from lib %s\n",lib);
341      s=iiGetLibProcBuffer(IDPROC(h), example ? 2 : 0);
342      if (!example)
343      {
344        PrintS(s);
345        FreeL((ADDRESS)s);
346      }
347      else
348      {
349        if (s!=NULL)
350        {
351          if (strlen(s)>5) iiEStart(s,IDPROC(h));
352          else FreeL((ADDRESS)s);
353        }
354      }
355    }
356  }
357  else if (example)
358  {
359    Werror("%s not found",s);
360  }
361  else
362  {
363  /* --------- is it a library ? --------------------------------*/
364    FILE *fp=feFopen(str,"rb");
365    if (fp!=NULL)
366    {
367      char buf[256];
368      BOOLEAN found=FALSE;
369      while (fgets( buf, sizeof(buf), fp))
370      {
371        if (strncmp(buf,"//",2)==0)
372        {
373          if (found) return;
374        }
375        else if ((strncmp(buf,"proc ",5)==0)||(strncmp(buf,"LIB ",4)==0))
376        {
377          if (!found) Warn("no help part in library found");
378          return;
379        }
380        else
381        {
382          found=TRUE;
383          PrintS(buf);
384        }
385      }
386    }
387  /* --------- everything else is for the manual ----------------*/
388    else
389    {
390#ifdef buildin_help
391      singular_manual(str);
392#else
393      char tmp[150];
394      char tmp2[150];
395      char strstr[100];
396      sprintf(tmp,"%s/singular.hlp", SINGULAR_INFODIR);
397      if (strcmp(str,"index")==0)
398         strstr[0]='\0';
399       else
400         sprintf(strstr," Index \"%s\"",str);
401      if (!access(tmp, R_OK))
402      {
403        sprintf(tmp, "info -f %s/singular.hlp %s", SINGULAR_INFODIR, strstr);
404      }
405      else
406      {
407        FILE *fd = feFopen("singular.hlp", "r", tmp2, FALSE);
408        if (fd != NULL)
409        {
410          fclose(fd);
411          sprintf(tmp, "info -f %s %s", tmp2,strstr);
412        }
413        else
414          sprintf(tmp,"info singular %s",strstr);
415      }
416      system(tmp);
417#ifndef MSDOS
418      //sprintf(tmp,"clear");
419      //system(tmp);
420#endif
421#endif
422    }
423  }
424}
425
426
427struct soptionStruct
428{
429  char * name;
430  int   setval;
431  int   resetval;
432};
433
434struct soptionStruct optionStruct[]=
435{
436  {"prot",         Sy_bit(OPT_PROT),           ~Sy_bit(OPT_PROT)   },
437  {"redSB",        Sy_bit(OPT_REDSB),          ~Sy_bit(OPT_REDSB)   },
438  /* 2 Gebauer/Moeller */
439  {"notSugar",     Sy_bit(OPT_NOT_SUGAR),      ~Sy_bit(OPT_NOT_SUGAR)   },
440  {"interrupt",    Sy_bit(OPT_INTERRUPT),      ~Sy_bit(OPT_INTERRUPT)   },
441  {"sugarCrit",    Sy_bit(OPT_SUGARCRIT),      ~Sy_bit(OPT_SUGARCRIT)   },
442  {"teach",     Sy_bit(OPT_DEBUG),          ~Sy_bit(OPT_DEBUG)  },
443  /* 7 cancel unit */
444  {"morePairs",    Sy_bit(OPT_MOREPAIRS),      ~Sy_bit(OPT_MOREPAIRS)   },
445  /* 9 return SB in syz, quotient, intersect */
446  {"returnSB",     Sy_bit(OPT_RETURN_SB),      ~Sy_bit(OPT_RETURN_SB)  },
447  {"fastHC",       Sy_bit(OPT_FASTHC),         ~Sy_bit(OPT_FASTHC)  },
448  /* 11-19 sort in L/T */
449  /* 20 redBest */
450  {"keepvars",     Sy_bit(OPT_KEEPVARS),       ~Sy_bit(OPT_KEEPVARS) },
451  {"staircaseBound",Sy_bit(OPT_STAIRCASEBOUND),~Sy_bit(OPT_STAIRCASEBOUND)  },
452  {"multBound",    Sy_bit(OPT_MULTBOUND),      ~Sy_bit(OPT_MULTBOUND)  },
453  {"degBound",     Sy_bit(OPT_DEGBOUND),       ~Sy_bit(OPT_DEGBOUND)  },
454  /* 25 no redTail(p)/redTail(s) */
455  {"redTail",      Sy_bit(OPT_REDTAIL),        ~Sy_bit(OPT_REDTAIL)  },
456  {"intStrategy",  Sy_bit(OPT_INTSTRATEGY),    ~Sy_bit(OPT_INTSTRATEGY)  },
457  /* 27 stop at HC (finiteDeterminacyTest) */
458  {"minRes",       Sy_bit(OPT_MINRES),         ~Sy_bit(OPT_MINRES)  },
459  /* 30: use not regularity for syz */
460  {"notRegularity",Sy_bit(OPT_NOTREGULARITY),  ~Sy_bit(OPT_NOTREGULARITY)  },
461  {"weightM",      Sy_bit(OPT_WEIGHTM),        ~Sy_bit(OPT_WEIGHTM)  },
462/*special for "none" and also end marker for showOption:*/
463  {"ne",           0,                          0 }
464};
465
466struct soptionStruct verboseStruct[]=
467{
468  {"mem",      Sy_bit(V_SHOW_MEM),  ~Sy_bit(V_SHOW_MEM)   },
469  {"yacc",     Sy_bit(V_YACC),      ~Sy_bit(V_YACC)       },
470  {"redefine", Sy_bit(V_REDEFINE),  ~Sy_bit(V_REDEFINE)   },
471  {"reading",  Sy_bit(V_READING),   ~Sy_bit(V_READING)    },
472  {"loadLib",  Sy_bit(V_LOAD_LIB),  ~Sy_bit(V_LOAD_LIB)   },
473  {"debugLib", Sy_bit(V_DEBUG_LIB), ~Sy_bit(V_DEBUG_LIB)  },
474  {"loadProc", Sy_bit(V_LOAD_PROC), ~Sy_bit(V_LOAD_PROC)  },
475  {"defRes",   Sy_bit(V_DEF_RES),   ~Sy_bit(V_DEF_RES)    },
476  {"debugMem", Sy_bit(V_DEBUG_MEM), ~Sy_bit(V_DEBUG_MEM)  },
477  {"usage",    Sy_bit(V_SHOW_USE),  ~Sy_bit(V_SHOW_USE)   },
478  {"Imap",     Sy_bit(V_IMAP),      ~Sy_bit(V_IMAP)       },
479  {"prompt",   Sy_bit(V_PROMPT),    ~Sy_bit(V_PROMPT)     },
480  {"notWarnSB",Sy_bit(V_NSB),       ~Sy_bit(V_NSB)        },
481/*special for "none" and also end marker for showOption:*/
482  {"ne",         0,          0 }
483};
484
485BOOLEAN setOption(leftv res, leftv v)
486{
487  char *n;
488  do
489  {
490    n=v->name;
491    if (n==NULL) return TRUE;
492    v->name=NULL;
493
494    int i;
495
496    if(strcmp(n,"get")==0)
497    {
498      intvec *w=new intvec(2);
499      (*w)[0]=test;
500      (*w)[1]=verbose;
501      res->rtyp=INTVEC_CMD;
502      res->data=(void *)w;
503      goto okay;
504    }
505    if(strcmp(n,"set")==0)
506    {
507      if((v->next!=NULL)
508      &&(v->next->Typ()==INTVEC_CMD))
509      {
510        v=v->next;
511        intvec *w=(intvec*)v->Data();
512        test=(*w)[0];
513        verbose=(*w)[1];
514
515        if (TEST_OPT_INTSTRATEGY && (currRing!=NULL) && (currRing->ch>=2))
516        {
517          test &=~Sy_bit(OPT_INTSTRATEGY);
518        }
519        goto okay;
520      }
521    }
522    if(strcmp(n,"none")==0)
523    {
524      test=0;
525      verbose=0;
526      goto okay;
527    }
528    for (i=0; (i==0) || (optionStruct[i-1].setval!=0); i++)
529    {
530      if (strcmp(n,optionStruct[i].name)==0)
531      {
532        if (optionStruct[i].setval & validOpts)
533          test |= optionStruct[i].setval;
534        else
535          Warn("cannot set option");
536        if (TEST_OPT_INTSTRATEGY && (currRing!=NULL) && (currRing->ch>=2))
537        {
538          test &=~Sy_bit(OPT_INTSTRATEGY);
539        }
540        goto okay;
541      }
542      else if ((strncmp(n,"no",2)==0)
543      && (strcmp(n+2,optionStruct[i].name)==0))
544      {
545        if (optionStruct[i].setval & validOpts)
546        {
547          test &= optionStruct[i].resetval;
548        }
549        else
550          Warn("cannot clear option");
551        goto okay;
552      }
553    }
554    for (i=0; (i==0) || (verboseStruct[i-1].setval!=0); i++)
555    {
556      if (strcmp(n,verboseStruct[i].name)==0)
557      {
558        verbose |= verboseStruct[i].setval;
559        #ifdef YYDEBUG
560        #if YYDEBUG
561        if (BVERBOSE(V_YACC)) yydebug=1;
562        else                  yydebug=0;
563        #endif
564        #endif
565        goto okay;
566      }
567      else if ((strncmp(n,"no",2)==0)
568      && (strcmp(n+2,verboseStruct[i].name)==0))
569      {
570        verbose &= verboseStruct[i].resetval;
571        #ifdef YYDEBUG
572        #if YYDEBUG
573        if (BVERBOSE(V_YACC)) yydebug=1;
574        else                  yydebug=0;
575        #endif
576        #endif
577        goto okay;
578      }
579    }
580    Werror("unknown option `%s`",n);
581  okay:
582    FreeL((ADDRESS)n);
583    v=v->next;
584  } while (v!=NULL);
585  return FALSE;
586}
587
588void showOption()
589{
590  int i;
591  BITSET tmp;
592
593  PrintS("//options:");
594  if ((test!=0)||(verbose!=0))
595  {
596    tmp=test;
597    if(tmp)
598    {
599      for (i=0; optionStruct[i].setval!=0; i++)
600      {
601        if (optionStruct[i].setval & test)
602        {
603          Print(" %s",optionStruct[i].name);
604          tmp &=optionStruct[i].resetval;
605        }
606      }
607      for (i=0; i<32; i++)
608      {
609        if (tmp & Sy_bit(i)) Print(" %d",i);
610      }
611    }
612    tmp=verbose;
613    if (tmp)
614    {
615      for (i=0; verboseStruct[i].setval!=0; i++)
616      {
617        if (verboseStruct[i].setval & tmp)
618        {
619          Print(" %s",verboseStruct[i].name);
620          tmp &=verboseStruct[i].resetval;
621        }
622      }
623      for (i=1; i<32; i++)
624      {
625        if (tmp & Sy_bit(i)) Print(" %d",i+32);
626      }
627    }
628    PrintLn();
629  }
630  else
631    PrintS(" none\n");
632}
633
634#ifndef HAVE_STRSTR
635char *strstr(const char *haystack, const char *needle)
636{
637  char *found = strchr(haystack,*needle);
638  if (*(needle+1)!='\0')
639  {
640    while((found !=NULL) && (strncmp(found+1,needle+1,strlen(needle+1))!=0))
641    {
642      found=strchr(found+1,*needle);
643    }
644  }
645  return found;
646}
647#endif
648
649char * versionString()
650{
651  StringSet("\t");
652#ifdef HAVE_FACTORY
653              StringAppend("factory (%s),", factoryVersion);
654#endif
655#ifdef HAVE_LIBFAC_P
656              StringAppend("libfac(%s,%s),\n\t",libfac_version,libfac_date);
657#endif
658#ifdef HAVE_GMP
659#if defined (__GNU_MP_VERSION) && defined (__GNU_MP_VERSION_MINOR)
660              StringAppend("GMP(%d.%d),",__GNU_MP_VERSION,__GNU_MP_VERSION_MINOR);
661#elif defined (HAVE_SMALLGMP)
662              StringAppendS("SmallGMP(2.0.2.0),");
663#else
664              StringAppendS("GMP(1.3),");
665#endif
666#endif
667#ifdef HAVE_MPSR
668              StringAppend("MP(%s),\n\t",MP_VERSION);
669#endif
670#if defined(HAVE_READLINE) && !defined(FEREAD)
671              StringAppendS("libreadline,\n\t");
672#endif
673#ifdef HAVE_FEREAD
674              StringAppendS("emulated libreadline,\n");
675#endif
676#ifdef SRING
677              StringAppendS("super algebra,");
678#endif
679#ifdef DRING
680              StringAppendS("Weyl algebra,");
681#endif
682#ifdef HAVE_DBM
683              StringAppendS("DBM,");
684#endif
685#ifdef HAVE_INFO
686              StringAppendS("info,");
687#endif
688#ifdef TEST
689              StringAppendS("TESTs,");
690#endif
691#if YYDEBUG
692              StringAppendS("YYDEBUG=1,");
693#endif
694#ifdef MDEBUG
695              StringAppend("MDEBUG=%d,",MDEBUG);
696#endif
697#ifdef TEST_MAC_ORDER
698              StringAppendS("mac_order,");
699#endif
700#ifndef __OPTIMIZE__
701              StringAppend("-g,");
702#endif
703              StringAppend("random=%d\n",siRandomStart);
704#ifdef MSDOS
705              char *p=getenv("SPATH");
706#else
707              char *p=getenv("SINGULARPATH");
708#endif
709              if (p!=NULL)
710                return StringAppend("search path:%s:%s",p,SINGULAR_DATADIR);
711              else
712                return StringAppend("search path:%s", SINGULAR_DATADIR);
713}
Note: See TracBrowser for help on using the repository browser.