source: git/Singular/misc.cc @ cb66fa

spielwiese
Last change on this file since cb66fa was cb66fa, checked in by Olaf Bachmann <obachman@…>, 24 years ago
* new implementation of PDEBUG stuff git-svn-id: file:///usr/local/Singular/svn/trunk@4595 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 15.9 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  /* 2 Gebauer/Moeller */
316  {"notSugar",     Sy_bit(OPT_NOT_SUGAR),      ~Sy_bit(OPT_NOT_SUGAR)   },
317  {"interrupt",    Sy_bit(OPT_INTERRUPT),      ~Sy_bit(OPT_INTERRUPT)   },
318  {"sugarCrit",    Sy_bit(OPT_SUGARCRIT),      ~Sy_bit(OPT_SUGARCRIT)   },
319  {"teach",     Sy_bit(OPT_DEBUG),          ~Sy_bit(OPT_DEBUG)  },
320  /* 7 cancel unit */
321  {"morePairs",    Sy_bit(OPT_MOREPAIRS),      ~Sy_bit(OPT_MOREPAIRS)   },
322  /* 9 return SB in syz, quotient, intersect */
323  {"returnSB",     Sy_bit(OPT_RETURN_SB),      ~Sy_bit(OPT_RETURN_SB)  },
324  {"fastHC",       Sy_bit(OPT_FASTHC),         ~Sy_bit(OPT_FASTHC)  },
325  /* 11-19 sort in L/T */
326  /* 20 redBest */
327  {"keepvars",     Sy_bit(OPT_KEEPVARS),       ~Sy_bit(OPT_KEEPVARS) },
328  {"staircaseBound",Sy_bit(OPT_STAIRCASEBOUND),~Sy_bit(OPT_STAIRCASEBOUND)  },
329  {"multBound",    Sy_bit(OPT_MULTBOUND),      ~Sy_bit(OPT_MULTBOUND)  },
330  {"degBound",     Sy_bit(OPT_DEGBOUND),       ~Sy_bit(OPT_DEGBOUND)  },
331  /* 25 no redTail(p)/redTail(s) */
332  {"redTail",      Sy_bit(OPT_REDTAIL),        ~Sy_bit(OPT_REDTAIL)  },
333  {"intStrategy",  Sy_bit(OPT_INTSTRATEGY),    ~Sy_bit(OPT_INTSTRATEGY)  },
334  {"infRedTail",   Sy_bit(OPT_INFREDTAIL),     ~Sy_bit(OPT_INFREDTAIL)  },
335  /* 30: use not regularity for syz */
336  {"notRegularity",Sy_bit(OPT_NOTREGULARITY),  ~Sy_bit(OPT_NOTREGULARITY)  },
337  {"weightM",      Sy_bit(OPT_WEIGHTM),        ~Sy_bit(OPT_WEIGHTM)  },
338/*special for "none" and also end marker for showOption:*/
339  {"ne",           0,                          0 }
340};
341
342struct soptionStruct verboseStruct[]=
343{
344  {"mem",      Sy_bit(V_SHOW_MEM),  ~Sy_bit(V_SHOW_MEM)   },
345  {"yacc",     Sy_bit(V_YACC),      ~Sy_bit(V_YACC)       },
346  {"redefine", Sy_bit(V_REDEFINE),  ~Sy_bit(V_REDEFINE)   },
347  {"reading",  Sy_bit(V_READING),   ~Sy_bit(V_READING)    },
348  {"loadLib",  Sy_bit(V_LOAD_LIB),  ~Sy_bit(V_LOAD_LIB)   },
349  {"debugLib", Sy_bit(V_DEBUG_LIB), ~Sy_bit(V_DEBUG_LIB)  },
350  {"loadProc", Sy_bit(V_LOAD_PROC), ~Sy_bit(V_LOAD_PROC)  },
351  {"defRes",   Sy_bit(V_DEF_RES),   ~Sy_bit(V_DEF_RES)    },
352  {"debugMem", Sy_bit(V_DEBUG_MEM), ~Sy_bit(V_DEBUG_MEM)  },
353  {"usage",    Sy_bit(V_SHOW_USE),  ~Sy_bit(V_SHOW_USE)   },
354  {"Imap",     Sy_bit(V_IMAP),      ~Sy_bit(V_IMAP)       },
355  {"prompt",   Sy_bit(V_PROMPT),    ~Sy_bit(V_PROMPT)     },
356  {"notWarnSB",Sy_bit(V_NSB),       ~Sy_bit(V_NSB)        },
357/*special for "none" and also end marker for showOption:*/
358  {"ne",         0,          0 }
359};
360
361BOOLEAN setOption(leftv res, leftv v)
362{
363  char *n;
364  do
365  {
366    if (v->Typ()==STRING_CMD)
367    {
368      n=(char *)v->CopyD(STRING_CMD);
369    }
370    else
371    {
372      if (v->name==NULL)
373        return TRUE;
374      if (v->rtyp==0)
375      {
376        n=v->name;
377        v->name=NULL;
378      }
379      else
380      {
381        n=omStrDup(v->name);
382      }
383    }
384
385    int i;
386
387    if(strcmp(n,"get")==0)
388    {
389      intvec *w=new intvec(2);
390      (*w)[0]=test;
391      (*w)[1]=verbose;
392      res->rtyp=INTVEC_CMD;
393      res->data=(void *)w;
394      goto okay;
395    }
396    if(strcmp(n,"set")==0)
397    {
398      if((v->next!=NULL)
399      &&(v->next->Typ()==INTVEC_CMD))
400      {
401        v=v->next;
402        intvec *w=(intvec*)v->Data();
403        test=(*w)[0];
404        verbose=(*w)[1];
405
406        if (TEST_OPT_INTSTRATEGY && (currRing!=NULL)
407        && rField_has_simple_inverse())
408        {
409          test &=~Sy_bit(OPT_INTSTRATEGY);
410        }
411        goto okay;
412      }
413    }
414    if(strcmp(n,"none")==0)
415    {
416      test=0;
417      verbose=0;
418      goto okay;
419    }
420    for (i=0; (i==0) || (optionStruct[i-1].setval!=0); i++)
421    {
422      if (strcmp(n,optionStruct[i].name)==0)
423      {
424        if (optionStruct[i].setval & validOpts)
425        {
426          test |= optionStruct[i].setval;
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    omFree((ADDRESS)n);
478    v=v->next;
479  } while (v!=NULL);
480  #ifdef HAVE_TCL
481    if (tclmode)
482    {
483      BITSET tmp;
484      int i;
485      StringSetS("");
486      if ((test!=0)||(verbose!=0))
487      {
488        tmp=test;
489        if(tmp)
490        {
491          for (i=0; optionStruct[i].setval!=0; i++)
492          {
493            if (optionStruct[i].setval & test)
494            {
495              StringAppend(" %s",optionStruct[i].name);
496              tmp &=optionStruct[i].resetval;
497            }
498          }
499        }
500        tmp=verbose;
501        if (tmp)
502        {
503          for (i=0; verboseStruct[i].setval!=0; i++)
504          {
505            if (verboseStruct[i].setval & tmp)
506            {
507              StringAppend(" %s",verboseStruct[i].name);
508              tmp &=verboseStruct[i].resetval;
509            }
510          }
511        }
512        PrintTCLS('O',StringAppendS(""));
513        StringSetS("");
514      }
515      else
516      {
517        PrintTCLS('O'," ");
518      }
519    }
520  #endif
521    // set global variable to show memory usage
522    if (BVERBOSE(V_SHOW_MEM)) om_sing_opt_show_mem = 1;
523    else om_sing_opt_show_mem = 0;
524  return FALSE;
525}
526
527char * showOption()
528{
529  int i;
530  BITSET tmp;
531
532  StringSetS("//options:");
533  if ((test!=0)||(verbose!=0))
534  {
535    tmp=test;
536    if(tmp)
537    {
538      for (i=0; optionStruct[i].setval!=0; i++)
539      {
540        if (optionStruct[i].setval & test)
541        {
542          StringAppend(" %s",optionStruct[i].name);
543          tmp &=optionStruct[i].resetval;
544        }
545      }
546      for (i=0; i<32; i++)
547      {
548        if (tmp & Sy_bit(i)) StringAppend(" %d",i);
549      }
550    }
551    tmp=verbose;
552    if (tmp)
553    {
554      for (i=0; verboseStruct[i].setval!=0; i++)
555      {
556        if (verboseStruct[i].setval & tmp)
557        {
558          StringAppend(" %s",verboseStruct[i].name);
559          tmp &=verboseStruct[i].resetval;
560        }
561      }
562      for (i=1; i<32; i++)
563      {
564        if (tmp & Sy_bit(i)) StringAppend(" %d",i+32);
565      }
566    }
567    return omStrDup(StringAppendS(""));
568  }
569  else
570    return omStrDup(StringAppendS(" none"));
571}
572
573char * versionString()
574{
575  char* str = StringSetS("");
576  StringAppend("Singular for %s version %s  (%lu)  %s\nwith\n",
577               S_UNAME, S_VERSION1,
578               feVersionId,singular_date);
579  StringAppend("\t");
580#ifdef HAVE_FACTORY
581              StringAppend("factory(%s),", factoryVersion);
582#endif
583#ifdef HAVE_LIBFAC_P
584              StringAppend("libfac(%s,%s),\n\t",libfac_version,libfac_date);
585#endif
586#if defined (__GNU_MP_VERSION) && defined (__GNU_MP_VERSION_MINOR)
587              StringAppend("GMP(%d.%d),",__GNU_MP_VERSION,__GNU_MP_VERSION_MINOR);
588#elif defined (HAVE_SMALLGMP)
589              StringAppendS("SmallGMP(2.0.2.0),");
590#else
591              StringAppendS("GMP(1.3),");
592#endif
593#ifdef HAVE_MPSR
594              StringAppend("MP(%s),",MP_VERSION);
595#endif
596#if defined(HAVE_DYN_RL)
597              if (fe_fgets_stdin==fe_fgets_dummy)
598                StringAppendS("no input,");
599              else if (fe_fgets_stdin==fe_fgets)
600                StringAppendS("fgets,");
601              if (fe_fgets_stdin==fe_fgets_stdin_drl)
602                StringAppendS("dynamic readline,");
603              else if (fe_fgets_stdin==fe_fgets_stdin_emu)
604                StringAppendS("emulated readline,");
605              else
606                StringAppendS("unknown fgets method,");
607#else
608  #if defined(HAVE_READLINE) && !defined(FEREAD)
609              StringAppendS("readline,");
610  #else
611    #ifdef HAVE_FEREAD
612              StringAppendS("emulated readline,");
613    #else
614              StringAppendS("fgets,");
615    #endif
616  #endif
617#endif
618#ifdef SRING
619              StringAppendS("super algebra,");
620#endif
621#ifdef DRING
622              StringAppendS("Weyl algebra,");
623#endif
624#ifdef HAVE_DBM
625              StringAppendS("DBM,\n\t");
626#else
627              StringAppendS("\n\t");
628#endif
629#ifdef HAVE_NAMESPACES
630              StringAppendS("Namespaces,");
631#endif
632#ifdef HAVE_DYNAMIC_LOADING
633              StringAppendS("DynamicLoading,");
634#endif
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.