source: git/Singular/misc.cc @ bef194

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