source: git/Singular/misc.cc @ fdca1c0

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