source: git/Singular/misc.cc @ 457d8d6

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