source: git/Singular/misc.cc @ 2c694a2

spielwiese
Last change on this file since 2c694a2 was 477993, checked in by Olaf Bachmann <obachman@…>, 25 years ago
* new Makefile target: Singulart whcih enables mtrack * Macros for allocating intvecs: NewIntvec git-svn-id: file:///usr/local/Singular/svn/trunk@3767 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 13.3 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  fe_reset_input_mode();
102  if (i<=0)
103  {
104    #ifdef HAVE_TCL
105    if (!tclmode)
106    #endif
107      if (BVERBOSE(0))
108      {
109        if (i==0)
110          printf("Auf Wiedersehen.\n");
111        else
112          printf("\n$Bye.\n");
113      }
114    #ifdef sun
115      #ifndef __svr4__
116        _cleanup();
117        _exit(0);
118      #endif
119    #endif
120    exit(0);
121  }
122  else
123  {
124    #ifdef HAVE_TCL
125    if (!tclmode)
126    #endif
127      printf("\nhalt %d\n",i);
128  }
129  exit(i);
130}
131}
132
133/*2
134* the renice routine for very large jobs
135* works only on unix machines,
136* testet on : linux, HP 9.0
137*
138*#ifndef MSDOS
139*#ifndef macintosh
140*#include <sys/times.h>
141*#include <sys/resource.h>
142*extern "C" int setpriority(int,int,int);
143*void very_nice()
144*{
145*#ifndef NO_SETPRIORITY
146*  setpriority(PRIO_PROCESS,0,19);
147*#endif
148*  sleep(10);
149*}
150*#else
151*void very_nice(){}
152*#endif
153*#else
154*void very_nice(){}
155*#endif
156*/
157
158/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
159#ifdef buildin_rand
160/*
161 *
162 *  A prime modulus multiplicative linear congruential
163 *  generator (PMMLCG), or "Lehmer generator".
164 *  Implementation directly derived from the article:
165 *
166 *        S. K. Park and K. W. Miller
167 *        Random Number Generators: Good Ones are Hard to Find
168 *        CACM vol 31, #10. Oct. 1988. pp 1192-1201.
169 *
170 *  Using the following multiplier and modulus, we obtain a
171 *  generator which:
172 *
173 *        1)  Has a full period: 1 to 2^31 - 2.
174 *        2)  Is testably "random" (see the article).
175 *        3)  Has a known implementation by E. L. Schrage.
176 */
177
178
179#define  A        16807L        /*  A "good" multiplier          */
180#define  M   2147483647L        /*  Modulus: 2^31 - 1          */
181#define  Q       127773L        /*  M / A                  */
182#define  R         2836L        /*  M % A                  */
183
184
185int siSeed = 1L;
186
187
188int siRand()
189{
190  siSeed = A * (siSeed % Q) - R * (siSeed / Q);
191
192  if ( siSeed < 0 )
193    siSeed += M;
194
195  return( siSeed );
196}
197#endif
198
199void singular_example(char *str)
200{
201  char *s=str;
202  while (*s==' ') s++;
203  char *ss=s;
204  while (*ss!='\0') ss++;
205  while (*ss<=' ')
206  {
207    *ss='\0';
208    ss--;
209  }
210#ifdef HAVE_NAMESPACES
211  idhdl h, ns;
212  iiname2hdl(s, &ns, &h);
213#else /* HAVE_NAMESPACES */
214  idhdl h=idroot->get(s,myynest);
215#endif /* HAVE_NAMESPACES */
216  if ((h!=NULL) && (IDTYP(h)==PROC_CMD))
217  {
218    char *lib=iiGetLibName(IDPROC(h));
219    if((lib!=NULL)&&(*lib!='\0'))
220    {
221      Print("// proc %s from lib %s\n",s,lib);
222      s=iiGetLibProcBuffer(IDPROC(h), 2);
223      if (s!=NULL)
224      {
225        if (strlen(s)>5)
226        {
227          iiEStart(s,IDPROC(h));
228          return;
229        }
230        else FreeL((ADDRESS)s);
231      }
232    }
233  }
234  Werror("%s not found",str);
235}
236
237
238struct soptionStruct
239{
240  char * name;
241  int   setval;
242  int   resetval;
243};
244
245struct soptionStruct optionStruct[]=
246{
247  {"prot",         Sy_bit(OPT_PROT),           ~Sy_bit(OPT_PROT)   },
248  {"redSB",        Sy_bit(OPT_REDSB),          ~Sy_bit(OPT_REDSB)   },
249  /* 2 Gebauer/Moeller */
250  {"notSugar",     Sy_bit(OPT_NOT_SUGAR),      ~Sy_bit(OPT_NOT_SUGAR)   },
251  {"interrupt",    Sy_bit(OPT_INTERRUPT),      ~Sy_bit(OPT_INTERRUPT)   },
252  {"sugarCrit",    Sy_bit(OPT_SUGARCRIT),      ~Sy_bit(OPT_SUGARCRIT)   },
253  {"teach",     Sy_bit(OPT_DEBUG),          ~Sy_bit(OPT_DEBUG)  },
254  /* 7 cancel unit */
255  {"morePairs",    Sy_bit(OPT_MOREPAIRS),      ~Sy_bit(OPT_MOREPAIRS)   },
256  /* 9 return SB in syz, quotient, intersect */
257  {"returnSB",     Sy_bit(OPT_RETURN_SB),      ~Sy_bit(OPT_RETURN_SB)  },
258  {"fastHC",       Sy_bit(OPT_FASTHC),         ~Sy_bit(OPT_FASTHC)  },
259  /* 11-19 sort in L/T */
260  /* 20 redBest */
261  {"keepvars",     Sy_bit(OPT_KEEPVARS),       ~Sy_bit(OPT_KEEPVARS) },
262  {"staircaseBound",Sy_bit(OPT_STAIRCASEBOUND),~Sy_bit(OPT_STAIRCASEBOUND)  },
263  {"multBound",    Sy_bit(OPT_MULTBOUND),      ~Sy_bit(OPT_MULTBOUND)  },
264  {"degBound",     Sy_bit(OPT_DEGBOUND),       ~Sy_bit(OPT_DEGBOUND)  },
265  /* 25 no redTail(p)/redTail(s) */
266  {"redTail",      Sy_bit(OPT_REDTAIL),        ~Sy_bit(OPT_REDTAIL)  },
267  {"intStrategy",  Sy_bit(OPT_INTSTRATEGY),    ~Sy_bit(OPT_INTSTRATEGY)  },
268  /* 30: use not regularity for syz */
269  {"notRegularity",Sy_bit(OPT_NOTREGULARITY),  ~Sy_bit(OPT_NOTREGULARITY)  },
270  {"weightM",      Sy_bit(OPT_WEIGHTM),        ~Sy_bit(OPT_WEIGHTM)  },
271/*special for "none" and also end marker for showOption:*/
272  {"ne",           0,                          0 }
273};
274
275struct soptionStruct verboseStruct[]=
276{
277  {"mem",      Sy_bit(V_SHOW_MEM),  ~Sy_bit(V_SHOW_MEM)   },
278  {"yacc",     Sy_bit(V_YACC),      ~Sy_bit(V_YACC)       },
279  {"redefine", Sy_bit(V_REDEFINE),  ~Sy_bit(V_REDEFINE)   },
280  {"reading",  Sy_bit(V_READING),   ~Sy_bit(V_READING)    },
281  {"loadLib",  Sy_bit(V_LOAD_LIB),  ~Sy_bit(V_LOAD_LIB)   },
282  {"debugLib", Sy_bit(V_DEBUG_LIB), ~Sy_bit(V_DEBUG_LIB)  },
283  {"loadProc", Sy_bit(V_LOAD_PROC), ~Sy_bit(V_LOAD_PROC)  },
284  {"defRes",   Sy_bit(V_DEF_RES),   ~Sy_bit(V_DEF_RES)    },
285  {"debugMem", Sy_bit(V_DEBUG_MEM), ~Sy_bit(V_DEBUG_MEM)  },
286  {"usage",    Sy_bit(V_SHOW_USE),  ~Sy_bit(V_SHOW_USE)   },
287  {"Imap",     Sy_bit(V_IMAP),      ~Sy_bit(V_IMAP)       },
288  {"prompt",   Sy_bit(V_PROMPT),    ~Sy_bit(V_PROMPT)     },
289  {"notWarnSB",Sy_bit(V_NSB),       ~Sy_bit(V_NSB)        },
290/*special for "none" and also end marker for showOption:*/
291  {"ne",         0,          0 }
292};
293
294BOOLEAN setOption(leftv res, leftv v)
295{
296  char *n;
297  do
298  {
299    if (v->Typ()==STRING_CMD)
300    {
301      n=(char *)v->CopyD(STRING_CMD);
302    }
303    else
304    {
305      if (v->name==NULL)
306        return TRUE;
307      if (v->rtyp==0)
308      {
309        n=v->name;
310        v->name=NULL;
311      }
312      else
313      {
314        n=mstrdup(v->name);
315      }
316    }
317
318    int i;
319
320    if(strcmp(n,"get")==0)
321    {
322      intvec *w=NewIntvec1(2);
323      (*w)[0]=test;
324      (*w)[1]=verbose;
325      res->rtyp=INTVEC_CMD;
326      res->data=(void *)w;
327      goto okay;
328    }
329    if(strcmp(n,"set")==0)
330    {
331      if((v->next!=NULL)
332      &&(v->next->Typ()==INTVEC_CMD))
333      {
334        v=v->next;
335        intvec *w=(intvec*)v->Data();
336        test=(*w)[0];
337        verbose=(*w)[1];
338
339        if (TEST_OPT_INTSTRATEGY && (currRing!=NULL)
340        && rField_has_simple_inverse())
341        {
342          test &=~Sy_bit(OPT_INTSTRATEGY);
343        }
344        goto okay;
345      }
346    }
347    if(strcmp(n,"none")==0)
348    {
349      test=0;
350      verbose=0;
351      goto okay;
352    }
353    for (i=0; (i==0) || (optionStruct[i-1].setval!=0); i++)
354    {
355      if (strcmp(n,optionStruct[i].name)==0)
356      {
357        if (optionStruct[i].setval & validOpts)
358        {
359          test |= optionStruct[i].setval;
360        }
361        else
362          Warn("cannot set option");
363        if (TEST_OPT_INTSTRATEGY && (currRing!=NULL)
364        && rField_has_simple_inverse())
365        {
366          test &=~Sy_bit(OPT_INTSTRATEGY);
367        }
368        goto okay;
369      }
370      else if ((strncmp(n,"no",2)==0)
371      && (strcmp(n+2,optionStruct[i].name)==0))
372      {
373        if (optionStruct[i].setval & validOpts)
374        {
375          test &= optionStruct[i].resetval;
376        }
377        else
378          Warn("cannot clear option");
379        goto okay;
380      }
381    }
382    for (i=0; (i==0) || (verboseStruct[i-1].setval!=0); i++)
383    {
384      if (strcmp(n,verboseStruct[i].name)==0)
385      {
386        verbose |= verboseStruct[i].setval;
387        #ifdef YYDEBUG
388        #if YYDEBUG
389        if (BVERBOSE(V_YACC)) yydebug=1;
390        else                  yydebug=0;
391        #endif
392        #endif
393        goto okay;
394      }
395      else if ((strncmp(n,"no",2)==0)
396      && (strcmp(n+2,verboseStruct[i].name)==0))
397      {
398        verbose &= verboseStruct[i].resetval;
399        #ifdef YYDEBUG
400        #if YYDEBUG
401        if (BVERBOSE(V_YACC)) yydebug=1;
402        else                  yydebug=0;
403        #endif
404        #endif
405        goto okay;
406      }
407    }
408    Werror("unknown option `%s`",n);
409  okay:
410    FreeL((ADDRESS)n);
411    v=v->next;
412  } while (v!=NULL);
413  #ifdef HAVE_TCL
414    if (tclmode)
415    {
416      BITSET tmp;
417      int i;
418      StringSetS("");
419      if ((test!=0)||(verbose!=0))
420      {
421        tmp=test;
422        if(tmp)
423        {
424          for (i=0; optionStruct[i].setval!=0; i++)
425          {
426            if (optionStruct[i].setval & test)
427            {
428              StringAppend(" %s",optionStruct[i].name);
429              tmp &=optionStruct[i].resetval;
430            }
431          }
432        }
433        tmp=verbose;
434        if (tmp)
435        {
436          for (i=0; verboseStruct[i].setval!=0; i++)
437          {
438            if (verboseStruct[i].setval & tmp)
439            {
440              StringAppend(" %s",verboseStruct[i].name);
441              tmp &=verboseStruct[i].resetval;
442            }
443          }
444        }
445        PrintTCLS('O',StringAppendS(""));
446        StringSetS("");
447      }
448      else
449      {
450        PrintTCLS('O'," ");
451      }
452    }
453  #endif
454  return FALSE;
455}
456
457char * showOption()
458{
459  int i;
460  BITSET tmp;
461
462  StringSetS("//options:");
463  if ((test!=0)||(verbose!=0))
464  {
465    tmp=test;
466    if(tmp)
467    {
468      for (i=0; optionStruct[i].setval!=0; i++)
469      {
470        if (optionStruct[i].setval & test)
471        {
472          StringAppend(" %s",optionStruct[i].name);
473          tmp &=optionStruct[i].resetval;
474        }
475      }
476      for (i=0; i<32; i++)
477      {
478        if (tmp & Sy_bit(i)) StringAppend(" %d",i);
479      }
480    }
481    tmp=verbose;
482    if (tmp)
483    {
484      for (i=0; verboseStruct[i].setval!=0; i++)
485      {
486        if (verboseStruct[i].setval & tmp)
487        {
488          StringAppend(" %s",verboseStruct[i].name);
489          tmp &=verboseStruct[i].resetval;
490        }
491      }
492      for (i=1; i<32; i++)
493      {
494        if (tmp & Sy_bit(i)) StringAppend(" %d",i+32);
495      }
496    }
497    return mstrdup(StringAppendS(""));
498  }
499  else
500    return mstrdup(StringAppendS(" none"));
501}
502
503char * versionString()
504{
505  char* str = StringSetS("");
506  StringAppend("Singular for %s version %s  (%lu)  %s %s\nwith\n",
507               S_UNAME, S_VERSION1,
508               feVersionId,__DATE__,__TIME__);
509  StringAppend("\t");
510#ifdef HAVE_FACTORY
511              StringAppend("factory(%s),", factoryVersion);
512#endif
513#ifdef HAVE_LIBFAC_P
514              StringAppend("libfac(%s,%s),\n\t",libfac_version,libfac_date);
515#endif
516#if defined (__GNU_MP_VERSION) && defined (__GNU_MP_VERSION_MINOR)
517              StringAppend("GMP(%d.%d),",__GNU_MP_VERSION,__GNU_MP_VERSION_MINOR);
518#elif defined (HAVE_SMALLGMP)
519              StringAppendS("SmallGMP(2.0.2.0),");
520#else
521              StringAppendS("GMP(1.3),");
522#endif
523#ifdef HAVE_MPSR
524              StringAppend("MP(%s),",MP_VERSION);
525#endif
526#if defined(HAVE_DYN_RL)
527              StringAppendS("d-readline,");
528#else
529  #if defined(HAVE_READLINE) && !defined(FEREAD)
530              StringAppendS("libreadline,");
531  #else
532    #ifdef HAVE_FEREAD
533              StringAppendS("emulated libreadline,");
534    #endif
535  #endif
536#endif
537#ifdef SRING
538              StringAppendS("super algebra,");
539#endif
540#ifdef DRING
541              StringAppendS("Weyl algebra,");
542#endif
543#ifdef HAVE_DBM
544              StringAppendS("DBM,\n\t");
545#else
546              StringAppendS("\n\t");
547#endif
548#ifdef HAVE_NAMESPACES
549              StringAppendS("Namespaces,");
550#endif
551#ifdef HAVE_DYNAMIC_LOADING
552              StringAppendS("DynamicLoading,");
553#endif
554#ifdef TEST
555              StringAppendS("TESTs,");
556#endif
557#if YYDEBUG
558              StringAppendS("YYDEBUG=1,");
559#endif
560#ifdef HAVE_ASSUME
561             StringAppendS("ASSUME,");
562#endif
563#ifdef MDEBUG
564              StringAppend("MDEBUG=%d,",MDEBUG);
565#endif
566#ifdef MTRACK
567              StringAppendS("MTRACK,");
568#endif
569#ifdef PDEBUG
570              StringAppendS("PDEBUG,");
571#endif
572#ifdef KDEBUG
573              StringAppendS("KDEBUG,");
574#endif
575#ifndef __OPTIMIZE__
576#ifdef __MWERKS__
577              StringAppendS(" Codewarrior 2.0,");
578#else
579              StringAppendS("-g,");
580#endif
581#endif
582              StringAppend("random=%d\n",siRandomStart);
583
584              feStringAppendResources(0);
585              feStringAppendBrowsers(0);
586              StringAppend("\n");
587              return str;
588}
Note: See TracBrowser for help on using the repository browser.