source: git/Singular/misc.cc @ 51c163

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