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
RevLine 
[0e1846]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
[2685a1]16#include "mod2.h"
[0e1846]17#include "tok.h"
18#include "febase.h"
19#include "cntrlc.h"
20#include "mmemory.h"
[93266c5]21#include "page.h"
[0e1846]22#include "ipid.h"
23#include "ipshell.h"
24#include "kstd1.h"
25#include "subexpr.h"
26#include "timer.h"
[58b151c]27#include "intvec.h"
[be0d84]28#include "ring.h"
[8b6255]29#define SI_DONT_HAVE_GLOBAL_VARS
[dc32d42]30
[706c95]31//#ifdef HAVE_LIBPARSER
32//#  include "libparse.h"
33//#endif /* HAVE_LIBPARSER */
[5480da]34
[dc32d42]35#ifdef HAVE_FACTORY
[8b6255]36#include <factory.h>
[dc32d42]37#endif
[0e1846]38
[9ea9c6]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
[0e1846]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);*/
[8b6255]64  if (t==0) t=1;
[34ab5de]65#ifdef HAVE_RTIMER
66  initRTimer();
67#endif
[0e1846]68#ifdef buildin_rand
69  siSeed=t;
70#else
71  srand((unsigned int)t);
72#endif
[8b6255]73#ifdef HAVE_FACTORY
74  factoryseed(t);
[9ea9c6]75#endif
[0e1846]76/*4 private data of other modules*/
77  memset(&sLastPrinted,0,sizeof(sleftv));
[6ae4f5]78  sLastPrinted.rtyp=NONE;
[0e1846]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{
[83c9ec]92  fe_reset_input_mode();
[93266c5]93  #ifdef PAGE_TEST
94  mmEndStat();
95  #endif
[e9ad8a6]96  #ifdef HAVE_TCL
97  if (tclmode)
[c19d54]98  {
[e9ad8a6]99    PrintTCL('Q',0,NULL);
[c19d54]100  }
[e9ad8a6]101  #endif
[8f769bb]102  fe_reset_input_mode();
[e9ad8a6]103  if (i<=0)
[0e1846]104  {
105    #ifdef HAVE_TCL
[e9ad8a6]106    if (!tclmode)
[0e1846]107    #endif
[e9ad8a6]108      if (BVERBOSE(0))
109      {
110        if (i==0)
111          printf("Auf Wiedersehen.\n");
[c19d54]112        else
113          printf("\n$Bye.\n");
[e9ad8a6]114      }
115    #ifdef sun
116      #ifndef __svr4__
117        _cleanup();
118        _exit(0);
119      #endif
120    #endif
121    exit(0);
[0e1846]122  }
123  else
124  {
125    #ifdef HAVE_TCL
[e9ad8a6]126    if (!tclmode)
[0e1846]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
[40edb03]141*#include <sys/times.h>
[0e1846]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
[62fba0c]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));
[c73fc6]220    if((lib!=NULL)&&(*lib!='\0'))
[62fba0c]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  }
[c04b94]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    {
[2ddc2e2]243
[c04b94]244      int old_echo = si_echo;
245      int length, got;
246      char* s;
[2ddc2e2]247
[c04b94]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  }
[62fba0c]273}
274
[0e1846]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)   },
[057e93c]291  {"teach",     Sy_bit(OPT_DEBUG),          ~Sy_bit(OPT_DEBUG)  },
[0e1846]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)  },
[82f0a0]306  {"infRedTail",   Sy_bit(OPT_INFREDTAIL),     ~Sy_bit(OPT_INFREDTAIL)  },
[0e1846]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  {
[3e35a1]338    if (v->Typ()==STRING_CMD)
339    {
[2166ad3]340      n=(char *)v->CopyD(STRING_CMD);
[3e35a1]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    }
[0e1846]356
357    int i;
358
[58b151c]359    if(strcmp(n,"get")==0)
[0e1846]360    {
[477993]361      intvec *w=NewIntvec1(2);
[58b151c]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))
[0e1846]372      {
[58b151c]373        v=v->next;
[9ea9c6]374        intvec *w=(intvec*)v->Data();
[58b151c]375        test=(*w)[0];
376        verbose=(*w)[1];
[9ea9c6]377
[be0d84]378        if (TEST_OPT_INTSTRATEGY && (currRing!=NULL)
[743c32]379        && rField_has_simple_inverse())
[58b151c]380        {
381          test &=~Sy_bit(OPT_INTSTRATEGY);
382        }
[0e1846]383        goto okay;
[9ea9c6]384      }
[58b151c]385    }
[667247]386    if(strcmp(n,"none")==0)
387    {
388      test=0;
389      verbose=0;
390      goto okay;
[9ea9c6]391    }
[58b151c]392    for (i=0; (i==0) || (optionStruct[i-1].setval!=0); i++)
393    {
394      if (strcmp(n,optionStruct[i].name)==0)
[0e1846]395      {
[58b151c]396        if (optionStruct[i].setval & validOpts)
[6227ad]397        {
[58b151c]398          test |= optionStruct[i].setval;
[6227ad]399        }
[58b151c]400        else
401          Warn("cannot set option");
[be0d84]402        if (TEST_OPT_INTSTRATEGY && (currRing!=NULL)
[743c32]403        && rField_has_simple_inverse())
[0e1846]404        {
[58b151c]405          test &=~Sy_bit(OPT_INTSTRATEGY);
406        }
407        goto okay;
[0e1846]408      }
[58b151c]409      else if ((strncmp(n,"no",2)==0)
410      && (strcmp(n+2,optionStruct[i].name)==0))
[0e1846]411      {
[667247]412        if (optionStruct[i].setval & validOpts)
[0e1846]413        {
[58b151c]414          test &= optionStruct[i].resetval;
[0e1846]415        }
[58b151c]416        else
417          Warn("cannot clear option");
418        goto okay;
[0e1846]419      }
420    }
[58b151c]421    for (i=0; (i==0) || (verboseStruct[i-1].setval!=0); i++)
[0e1846]422    {
[58b151c]423      if (strcmp(n,verboseStruct[i].name)==0)
[0e1846]424      {
[58b151c]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
[0e1846]432        goto okay;
433      }
[58b151c]434      else if ((strncmp(n,"no",2)==0)
435      && (strcmp(n+2,verboseStruct[i].name)==0))
[0e1846]436      {
[58b151c]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;
[0e1846]445      }
[58b151c]446    }
447    Werror("unknown option `%s`",n);
[0e1846]448  okay:
449    FreeL((ADDRESS)n);
450    v=v->next;
[9ea9c6]451  } while (v!=NULL);
[1e3015]452  #ifdef HAVE_TCL
453    if (tclmode)
454    {
455      BITSET tmp;
456      int i;
[a79a128]457      StringSetS("");
[1e3015]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        }
[b45d97]484        PrintTCLS('O',StringAppendS(""));
[a79a128]485        StringSetS("");
[1e3015]486      }
487      else
488      {
489        PrintTCLS('O'," ");
490      }
491    }
492  #endif
[0e1846]493  return FALSE;
494}
495
[979a09f]496char * showOption()
[0e1846]497{
498  int i;
499  BITSET tmp;
500
[a79a128]501  StringSetS("//options:");
[58b151c]502  if ((test!=0)||(verbose!=0))
[0e1846]503  {
504    tmp=test;
[58b151c]505    if(tmp)
[0e1846]506    {
507      for (i=0; optionStruct[i].setval!=0; i++)
508      {
509        if (optionStruct[i].setval & test)
510        {
[979a09f]511          StringAppend(" %s",optionStruct[i].name);
[0e1846]512          tmp &=optionStruct[i].resetval;
513        }
514      }
515      for (i=0; i<32; i++)
516      {
[979a09f]517        if (tmp & Sy_bit(i)) StringAppend(" %d",i);
[0e1846]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        {
[979a09f]527          StringAppend(" %s",verboseStruct[i].name);
[0e1846]528          tmp &=verboseStruct[i].resetval;
529        }
530      }
531      for (i=1; i<32; i++)
532      {
[979a09f]533        if (tmp & Sy_bit(i)) StringAppend(" %d",i+32);
[0e1846]534      }
535    }
[b45d97]536    return mstrdup(StringAppendS(""));
[0e1846]537  }
[58b151c]538  else
[b45d97]539    return mstrdup(StringAppendS(" none"));
[0e1846]540}
541
[9ea9c6]542char * versionString()
543{
[b7b08c]544  char* str = StringSetS("");
[923c27]545  StringAppend("Singular for %s version %s  (%lu)  %s\nwith\n",
[b7b08c]546               S_UNAME, S_VERSION1,
[923c27]547               feVersionId,singular_date);
[b7b08c]548  StringAppend("\t");
[9ea9c6]549#ifdef HAVE_FACTORY
[97454d]550              StringAppend("factory(%s),", factoryVersion);
[9ea9c6]551#endif
552#ifdef HAVE_LIBFAC_P
[38cfbb]553              StringAppend("libfac(%s,%s),\n\t",libfac_version,libfac_date);
[9ea9c6]554#endif
555#if defined (__GNU_MP_VERSION) && defined (__GNU_MP_VERSION_MINOR)
[38cfbb]556              StringAppend("GMP(%d.%d),",__GNU_MP_VERSION,__GNU_MP_VERSION_MINOR);
[9ea9c6]557#elif defined (HAVE_SMALLGMP)
[38cfbb]558              StringAppendS("SmallGMP(2.0.2.0),");
[9ea9c6]559#else
[38cfbb]560              StringAppendS("GMP(1.3),");
[9ea9c6]561#endif
562#ifdef HAVE_MPSR
[f69a29]563              StringAppend("MP(%s),",MP_VERSION);
[9ea9c6]564#endif
[f413ec5]565#if defined(HAVE_DYN_RL)
[2ddc2e2]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,");
[f69a29]576#else
[f413ec5]577  #if defined(HAVE_READLINE) && !defined(FEREAD)
[2ddc2e2]578              StringAppendS("readline,");
[f413ec5]579  #else
580    #ifdef HAVE_FEREAD
[2ddc2e2]581              StringAppendS("emulated readline,");
[f413ec5]582    #endif
583  #endif
[38cfbb]584#endif
585#ifdef SRING
586              StringAppendS("super algebra,");
587#endif
588#ifdef DRING
589              StringAppendS("Weyl algebra,");
590#endif
591#ifdef HAVE_DBM
[58124a3]592              StringAppendS("DBM,\n\t");
593#else
594              StringAppendS("\n\t");
[9ea9c6]595#endif
[0a3ddd]596#ifdef HAVE_NAMESPACES
597              StringAppendS("Namespaces,");
598#endif
[cd6b45]599#ifdef HAVE_DYNAMIC_LOADING
[dae004]600              StringAppendS("DynamicLoading,");
[cd6b45]601#endif
[9ea9c6]602#ifdef TEST
[38cfbb]603              StringAppendS("TESTs,");
[9ea9c6]604#endif
605#if YYDEBUG
[38cfbb]606              StringAppendS("YYDEBUG=1,");
[9ea9c6]607#endif
[98555ea]608#ifdef HAVE_ASSUME
[8f769bb]609             StringAppendS("ASSUME,");
[98555ea]610#endif
[9ea9c6]611#ifdef MDEBUG
[38cfbb]612              StringAppend("MDEBUG=%d,",MDEBUG);
613#endif
[58124a3]614#ifdef MTRACK
[b45d97]615              StringAppendS("MTRACK,");
[58124a3]616#endif
[275397]617#ifdef PDEBUG
[b45d97]618              StringAppendS("PDEBUG,");
[275397]619#endif
620#ifdef KDEBUG
[b45d97]621              StringAppendS("KDEBUG,");
[275397]622#endif
[9ea9c6]623#ifndef __OPTIMIZE__
[12310e]624#ifdef __MWERKS__
[b45d97]625              StringAppendS(" Codewarrior 2.0,");
[12310e]626#else
[b45d97]627              StringAppendS("-g,");
[12310e]628#endif
[9ea9c6]629#endif
[38cfbb]630              StringAppend("random=%d\n",siRandomStart);
[9c35ef]631
[9f1850]632              feStringAppendResources(0);
633              feStringAppendBrowsers(0);
[b7b08c]634              StringAppend("\n");
[09f0ee]635              return str;
[9ea9c6]636}
Note: See TracBrowser for help on using the repository browser.