source: git/Singular/misc.cc @ 60200b

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