source: git/Singular/misc.cc @ 706c95

spielwiese
Last change on this file since 706c95 was 706c95, checked in by Hans Schönemann <hannes@…>, 25 years ago
* hannes: random.lib - back to old version randomLast: (faster, less output in tests) moved help routines into seperate files: fehelp.* git-svn-id: file:///usr/local/Singular/svn/trunk@3384 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 13.6 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT:
6*/
7
8#include <string.h>
9#include <unistd.h>
10#include <stdio.h>
11#include <stddef.h>
12#include <stdlib.h>
13#include <time.h>
14#include <limits.h>
15
16#include "mod2.h"
17#include "tok.h"
18#include "febase.h"
19#include "cntrlc.h"
20#include "mmemory.h"
21#include "page.h"
22#include "ipid.h"
23#include "ipshell.h"
24#include "kstd1.h"
25#include "subexpr.h"
26#include "timer.h"
27#include "intvec.h"
28#include "ring.h"
29#define SI_DONT_HAVE_GLOBAL_VARS
30
31//#ifdef HAVE_LIBPARSER
32//#  include "libparse.h"
33//#endif /* HAVE_LIBPARSER */
34
35#ifdef HAVE_FACTORY
36#include <factory.h>
37#endif
38
39/* version strings */
40#ifdef HAVE_LIBFAC_P
41  extern const char * libfac_version;
42  extern const char * libfac_date;
43#endif
44extern "C" {
45#include <gmp.h>
46}
47#ifdef HAVE_MPSR
48#include <MP_Config.h>
49#endif
50
51/*0 implementation*/
52
53/*2
54* initialize components of Singular
55*/
56int inits(void)
57{
58  int t;
59/*4 signal handler:*/
60  init_signals();
61/*4 randomize: */
62  t=initTimer();
63  /*t=(int)time(NULL);*/
64  if (t==0) t=1;
65#ifdef HAVE_RTIMER
66  initRTimer();
67#endif
68#ifdef buildin_rand
69  siSeed=t;
70#else
71  srand((unsigned int)t);
72#endif
73#ifdef HAVE_FACTORY
74  factoryseed(t);
75#endif
76/*4 private data of other modules*/
77  memset(&sLastPrinted,0,sizeof(sleftv));
78  sLastPrinted.rtyp=NONE;
79#ifdef HAVE_MPSR
80  extern void mpsr_Init();
81  mpsr_Init();
82#endif
83  return t;
84}
85
86/*2
87* the global exit routine of Singular
88*/
89extern "C" {
90void m2_end(short i)
91{
92  #ifdef PAGE_TEST
93  mmEndStat();
94  #endif
95  #ifdef HAVE_TCL
96  if (tclmode)
97  {
98    PrintTCL('Q',0,NULL);
99  }
100  #endif
101  if (i<=0)
102  {
103    #ifdef HAVE_TCL
104    if (!tclmode)
105    #endif
106      if (BVERBOSE(0))
107      {
108        if (i==0)
109          printf("Auf Wiedersehen.\n");
110        else
111          printf("\n$Bye.\n");
112      }
113    #ifndef macintosh
114      #ifdef HAVE_FEREAD
115        #ifdef HAVE_ATEXIT
116          fe_reset_input_mode();
117        #else
118          fe_reset_input_mode(0,NULL);
119        #endif
120      #else
121        #ifdef HAVE_READLINE
122          fe_reset_input_mode();
123        #endif
124      #endif
125    #endif
126    #ifdef sun
127      #ifndef __svr4__
128        _cleanup();
129        _exit(0);
130      #endif
131    #endif
132    exit(0);
133  }
134  else
135  {
136    #ifdef HAVE_TCL
137    if (!tclmode)
138    #endif
139      printf("\nhalt %d\n",i);
140  }
141  exit(i);
142}
143}
144
145/*2
146* the renice routine for very large jobs
147* works only on unix machines,
148* testet on : linux, HP 9.0
149*
150*#ifndef MSDOS
151*#ifndef macintosh
152*#include <sys/times.h>
153*#include <sys/resource.h>
154*extern "C" int setpriority(int,int,int);
155*void very_nice()
156*{
157*#ifndef NO_SETPRIORITY
158*  setpriority(PRIO_PROCESS,0,19);
159*#endif
160*  sleep(10);
161*}
162*#else
163*void very_nice(){}
164*#endif
165*#else
166*void very_nice(){}
167*#endif
168*/
169
170/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
171#ifdef buildin_rand
172/*
173 *
174 *  A prime modulus multiplicative linear congruential
175 *  generator (PMMLCG), or "Lehmer generator".
176 *  Implementation directly derived from the article:
177 *
178 *        S. K. Park and K. W. Miller
179 *        Random Number Generators: Good Ones are Hard to Find
180 *        CACM vol 31, #10. Oct. 1988. pp 1192-1201.
181 *
182 *  Using the following multiplier and modulus, we obtain a
183 *  generator which:
184 *
185 *        1)  Has a full period: 1 to 2^31 - 2.
186 *        2)  Is testably "random" (see the article).
187 *        3)  Has a known implementation by E. L. Schrage.
188 */
189
190
191#define  A        16807L        /*  A "good" multiplier          */
192#define  M   2147483647L        /*  Modulus: 2^31 - 1          */
193#define  Q       127773L        /*  M / A                  */
194#define  R         2836L        /*  M % A                  */
195
196
197int siSeed = 1L;
198
199
200int siRand()
201{
202  siSeed = A * (siSeed % Q) - R * (siSeed / Q);
203
204  if ( siSeed < 0 )
205    siSeed += M;
206
207  return( siSeed );
208}
209#endif
210
211void singular_example(char *str)
212{
213  char *s=str;
214  while (*s==' ') s++;
215  char *ss=s;
216  while (*ss!='\0') ss++;
217  while (*ss<=' ')
218  {
219    *ss='\0';
220    ss--;
221  }
222#ifdef HAVE_NAMESPACES
223  idhdl h, ns;
224  iiname2hdl(s, &ns, &h);
225#else /* HAVE_NAMESPACES */
226  idhdl h=idroot->get(s,myynest);
227#endif /* HAVE_NAMESPACES */
228  if ((h!=NULL) && (IDTYP(h)==PROC_CMD))
229  {
230    char *lib=iiGetLibName(IDPROC(h));
231    if((lib!=NULL)&&(*lib!='\0'))
232    {
233      Print("// proc %s from lib %s\n",s,lib);
234      s=iiGetLibProcBuffer(IDPROC(h), 2);
235      if (s!=NULL)
236      {
237        if (strlen(s)>5)
238        {
239          iiEStart(s,IDPROC(h));
240          return;
241        }
242        else FreeL((ADDRESS)s);
243      }
244    }
245  }
246  Werror("%s not found",s);
247}
248
249
250struct soptionStruct
251{
252  char * name;
253  int   setval;
254  int   resetval;
255};
256
257struct soptionStruct optionStruct[]=
258{
259  {"prot",         Sy_bit(OPT_PROT),           ~Sy_bit(OPT_PROT)   },
260  {"redSB",        Sy_bit(OPT_REDSB),          ~Sy_bit(OPT_REDSB)   },
261  /* 2 Gebauer/Moeller */
262  {"notSugar",     Sy_bit(OPT_NOT_SUGAR),      ~Sy_bit(OPT_NOT_SUGAR)   },
263  {"interrupt",    Sy_bit(OPT_INTERRUPT),      ~Sy_bit(OPT_INTERRUPT)   },
264  {"sugarCrit",    Sy_bit(OPT_SUGARCRIT),      ~Sy_bit(OPT_SUGARCRIT)   },
265  {"teach",     Sy_bit(OPT_DEBUG),          ~Sy_bit(OPT_DEBUG)  },
266  /* 7 cancel unit */
267  {"morePairs",    Sy_bit(OPT_MOREPAIRS),      ~Sy_bit(OPT_MOREPAIRS)   },
268  /* 9 return SB in syz, quotient, intersect */
269  {"returnSB",     Sy_bit(OPT_RETURN_SB),      ~Sy_bit(OPT_RETURN_SB)  },
270  {"fastHC",       Sy_bit(OPT_FASTHC),         ~Sy_bit(OPT_FASTHC)  },
271  /* 11-19 sort in L/T */
272  /* 20 redBest */
273  {"keepvars",     Sy_bit(OPT_KEEPVARS),       ~Sy_bit(OPT_KEEPVARS) },
274  {"staircaseBound",Sy_bit(OPT_STAIRCASEBOUND),~Sy_bit(OPT_STAIRCASEBOUND)  },
275  {"multBound",    Sy_bit(OPT_MULTBOUND),      ~Sy_bit(OPT_MULTBOUND)  },
276  {"degBound",     Sy_bit(OPT_DEGBOUND),       ~Sy_bit(OPT_DEGBOUND)  },
277  /* 25 no redTail(p)/redTail(s) */
278  {"redTail",      Sy_bit(OPT_REDTAIL),        ~Sy_bit(OPT_REDTAIL)  },
279  {"intStrategy",  Sy_bit(OPT_INTSTRATEGY),    ~Sy_bit(OPT_INTSTRATEGY)  },
280  /* 27 stop at HC (finiteDeterminacyTest) */
281  {"minRes",       Sy_bit(OPT_MINRES),         ~Sy_bit(OPT_MINRES)  },
282  /* 30: use not regularity for syz */
283  {"notRegularity",Sy_bit(OPT_NOTREGULARITY),  ~Sy_bit(OPT_NOTREGULARITY)  },
284  {"weightM",      Sy_bit(OPT_WEIGHTM),        ~Sy_bit(OPT_WEIGHTM)  },
285/*special for "none" and also end marker for showOption:*/
286  {"ne",           0,                          0 }
287};
288
289struct soptionStruct verboseStruct[]=
290{
291  {"mem",      Sy_bit(V_SHOW_MEM),  ~Sy_bit(V_SHOW_MEM)   },
292  {"yacc",     Sy_bit(V_YACC),      ~Sy_bit(V_YACC)       },
293  {"redefine", Sy_bit(V_REDEFINE),  ~Sy_bit(V_REDEFINE)   },
294  {"reading",  Sy_bit(V_READING),   ~Sy_bit(V_READING)    },
295  {"loadLib",  Sy_bit(V_LOAD_LIB),  ~Sy_bit(V_LOAD_LIB)   },
296  {"debugLib", Sy_bit(V_DEBUG_LIB), ~Sy_bit(V_DEBUG_LIB)  },
297  {"loadProc", Sy_bit(V_LOAD_PROC), ~Sy_bit(V_LOAD_PROC)  },
298  {"defRes",   Sy_bit(V_DEF_RES),   ~Sy_bit(V_DEF_RES)    },
299  {"debugMem", Sy_bit(V_DEBUG_MEM), ~Sy_bit(V_DEBUG_MEM)  },
300  {"usage",    Sy_bit(V_SHOW_USE),  ~Sy_bit(V_SHOW_USE)   },
301  {"Imap",     Sy_bit(V_IMAP),      ~Sy_bit(V_IMAP)       },
302  {"prompt",   Sy_bit(V_PROMPT),    ~Sy_bit(V_PROMPT)     },
303  {"notWarnSB",Sy_bit(V_NSB),       ~Sy_bit(V_NSB)        },
304/*special for "none" and also end marker for showOption:*/
305  {"ne",         0,          0 }
306};
307
308BOOLEAN setOption(leftv res, leftv v)
309{
310  char *n;
311  do
312  {
313    if (v->Typ()==STRING_CMD)
314    {
315      n=(char *)v->CopyD();
316    }
317    else
318    {
319      if (v->name==NULL)
320        return TRUE;
321      if (v->rtyp==0)
322      {
323        n=v->name;
324        v->name=NULL;
325      }
326      else
327      {
328        n=mstrdup(v->name);
329      }
330    }
331
332    int i;
333
334    if(strcmp(n,"get")==0)
335    {
336      intvec *w=new intvec(2);
337      (*w)[0]=test;
338      (*w)[1]=verbose;
339      res->rtyp=INTVEC_CMD;
340      res->data=(void *)w;
341      goto okay;
342    }
343    if(strcmp(n,"set")==0)
344    {
345      if((v->next!=NULL)
346      &&(v->next->Typ()==INTVEC_CMD))
347      {
348        v=v->next;
349        intvec *w=(intvec*)v->Data();
350        test=(*w)[0];
351        verbose=(*w)[1];
352
353        if (TEST_OPT_INTSTRATEGY && (currRing!=NULL)
354        && rField_has_simple_inverse())
355        {
356          test &=~Sy_bit(OPT_INTSTRATEGY);
357        }
358        goto okay;
359      }
360    }
361    if(strcmp(n,"none")==0)
362    {
363      test=0;
364      verbose=0;
365      goto okay;
366    }
367    for (i=0; (i==0) || (optionStruct[i-1].setval!=0); i++)
368    {
369      if (strcmp(n,optionStruct[i].name)==0)
370      {
371        if (optionStruct[i].setval & validOpts)
372        {
373          test |= optionStruct[i].setval;
374        }
375        else
376          Warn("cannot set option");
377        if (TEST_OPT_INTSTRATEGY && (currRing!=NULL)
378        && rField_has_simple_inverse())
379        {
380          test &=~Sy_bit(OPT_INTSTRATEGY);
381        }
382        goto okay;
383      }
384      else if ((strncmp(n,"no",2)==0)
385      && (strcmp(n+2,optionStruct[i].name)==0))
386      {
387        if (optionStruct[i].setval & validOpts)
388        {
389          test &= optionStruct[i].resetval;
390        }
391        else
392          Warn("cannot clear option");
393        goto okay;
394      }
395    }
396    for (i=0; (i==0) || (verboseStruct[i-1].setval!=0); i++)
397    {
398      if (strcmp(n,verboseStruct[i].name)==0)
399      {
400        verbose |= verboseStruct[i].setval;
401        #ifdef YYDEBUG
402        #if YYDEBUG
403        if (BVERBOSE(V_YACC)) yydebug=1;
404        else                  yydebug=0;
405        #endif
406        #endif
407        goto okay;
408      }
409      else if ((strncmp(n,"no",2)==0)
410      && (strcmp(n+2,verboseStruct[i].name)==0))
411      {
412        verbose &= verboseStruct[i].resetval;
413        #ifdef YYDEBUG
414        #if YYDEBUG
415        if (BVERBOSE(V_YACC)) yydebug=1;
416        else                  yydebug=0;
417        #endif
418        #endif
419        goto okay;
420      }
421    }
422    Werror("unknown option `%s`",n);
423  okay:
424    FreeL((ADDRESS)n);
425    v=v->next;
426  } while (v!=NULL);
427  #ifdef HAVE_TCL
428    if (tclmode)
429    {
430      BITSET tmp;
431      int i;
432      StringSetS("");
433      if ((test!=0)||(verbose!=0))
434      {
435        tmp=test;
436        if(tmp)
437        {
438          for (i=0; optionStruct[i].setval!=0; i++)
439          {
440            if (optionStruct[i].setval & test)
441            {
442              StringAppend(" %s",optionStruct[i].name);
443              tmp &=optionStruct[i].resetval;
444            }
445          }
446        }
447        tmp=verbose;
448        if (tmp)
449        {
450          for (i=0; verboseStruct[i].setval!=0; i++)
451          {
452            if (verboseStruct[i].setval & tmp)
453            {
454              StringAppend(" %s",verboseStruct[i].name);
455              tmp &=verboseStruct[i].resetval;
456            }
457          }
458        }
459        PrintTCLS('O',StringAppendS(""));
460        StringSetS("");
461      }
462      else
463      {
464        PrintTCLS('O'," ");
465      }
466    }
467  #endif
468  return FALSE;
469}
470
471char * showOption()
472{
473  int i;
474  BITSET tmp;
475
476  StringSetS("//options:");
477  if ((test!=0)||(verbose!=0))
478  {
479    tmp=test;
480    if(tmp)
481    {
482      for (i=0; optionStruct[i].setval!=0; i++)
483      {
484        if (optionStruct[i].setval & test)
485        {
486          StringAppend(" %s",optionStruct[i].name);
487          tmp &=optionStruct[i].resetval;
488        }
489      }
490      for (i=0; i<32; i++)
491      {
492        if (tmp & Sy_bit(i)) StringAppend(" %d",i);
493      }
494    }
495    tmp=verbose;
496    if (tmp)
497    {
498      for (i=0; verboseStruct[i].setval!=0; i++)
499      {
500        if (verboseStruct[i].setval & tmp)
501        {
502          StringAppend(" %s",verboseStruct[i].name);
503          tmp &=verboseStruct[i].resetval;
504        }
505      }
506      for (i=1; i<32; i++)
507      {
508        if (tmp & Sy_bit(i)) StringAppend(" %d",i+32);
509      }
510    }
511    return mstrdup(StringAppendS(""));
512  }
513  else
514    return mstrdup(StringAppendS(" none"));
515}
516
517char * versionString()
518{
519  StringSetS("\t");
520#ifdef HAVE_FACTORY
521              StringAppend("factory(%s),", factoryVersion);
522#endif
523#ifdef HAVE_LIBFAC_P
524              StringAppend("libfac(%s,%s),\n\t",libfac_version,libfac_date);
525#endif
526#if defined (__GNU_MP_VERSION) && defined (__GNU_MP_VERSION_MINOR)
527              StringAppend("GMP(%d.%d),",__GNU_MP_VERSION,__GNU_MP_VERSION_MINOR);
528#elif defined (HAVE_SMALLGMP)
529              StringAppendS("SmallGMP(2.0.2.0),");
530#else
531              StringAppendS("GMP(1.3),");
532#endif
533#ifdef HAVE_MPSR
534              StringAppend("MP(%s),",MP_VERSION);
535#endif
536#if defined(HAVE_READLINE) && !defined(FEREAD)
537              StringAppendS("libreadline,");
538#else
539#ifdef HAVE_FEREAD
540              StringAppendS("emulated libreadline,");
541#endif
542#endif
543#ifdef SRING
544              StringAppendS("super algebra,");
545#endif
546#ifdef DRING
547              StringAppendS("Weyl algebra,");
548#endif
549#ifdef HAVE_DBM
550              StringAppendS("DBM,\n\t");
551#else
552              StringAppendS("\n\t");
553#endif
554#ifdef HAVE_INFO
555              StringAppendS("info,");
556#endif
557#ifdef HAVE_NAMESPACES
558              StringAppendS("Namespaces,");
559#endif
560#ifdef HAVE_DYNAMIC_LOADING
561              StringAppendS("DynamicLoading,");
562#endif
563#ifdef TEST
564              StringAppendS("TESTs,");
565#endif
566#if YYDEBUG
567              StringAppendS("YYDEBUG=1,");
568#endif
569#ifdef MDEBUG
570              StringAppend("MDEBUG=%d,",MDEBUG);
571#endif
572#ifdef MTRACK
573              StringAppendS("MTRACK,");
574#endif
575#ifdef PDEBUG
576              StringAppendS("PDEBUG,");
577#endif
578#ifdef KDEBUG
579              StringAppendS("KDEBUG,");
580#endif
581#ifdef TEST_MAC_ORDER
582              StringAppendS("mac_order,");
583#endif
584#ifndef __OPTIMIZE__
585#ifdef __MWERKS__
586              StringAppendS(" Codewarrior 2.0,");
587#else
588              StringAppendS("-g,");
589#endif
590#endif
591              StringAppend("random=%d\n",siRandomStart);
592#ifndef __MWERKS__
593#ifdef HAVE_INFO
594              StringAppend("InfoFile   : %s\n", feGetInfoFile());
595              StringAppend("InfoProgram: %s\n", feGetInfoProgram());
596#endif
597              StringAppend("Singular   : %s\n",feGetExpandedExecutable());
598              return StringAppend("SearchPath : %s", feGetSearchPath());
599#endif
600}
Note: See TracBrowser for help on using the repository browser.