source: git/Singular/misc.cc @ 84a6a7

spielwiese
Last change on this file since 84a6a7 was 3aed2f, checked in by Michael Brickenstein <bricken@…>, 18 years ago
*bricken: coefStrat git-svn-id: file:///usr/local/Singular/svn/trunk@8989 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 16.7 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
15#include "mod2.h"
16#include <mylimits.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
54//extern "C" {
55//#include <gmp.h>
56//}
57#include <si_gmp.h>
58#ifdef HAVE_MPSR
59#include <MP_Config.h>
60#endif
61
62/*2
63* initialize components of Singular
64*/
65int inits(void)
66{
67  int t;
68/*4 signal handler:*/
69  init_signals();
70/*4 randomize: */
71  t=initTimer();
72  /*t=(int)time(NULL);*/
73  if (t==0) t=1;
74#ifdef HAVE_RTIMER
75  initRTimer();
76#endif
77#ifdef buildin_rand
78  siSeed=t;
79#else
80  srand((unsigned int)t);
81#endif
82#ifdef HAVE_FACTORY
83  factoryseed(t);
84#endif
85/*4 private data of other modules*/
86  memset(&sLastPrinted,0,sizeof(sleftv));
87  sLastPrinted.rtyp=NONE;
88  return t;
89}
90
91/*2
92* the renice routine for very large jobs
93* works only on unix machines,
94* testet on : linux, HP 9.0
95*
96*#ifndef MSDOS
97*#include <sys/times.h>
98*#include <sys/resource.h>
99*extern "C" int setpriority(int,int,int);
100*void very_nice()
101*{
102*#ifndef NO_SETPRIORITY
103*  setpriority(PRIO_PROCESS,0,19);
104*#endif
105*  sleep(10);
106*}
107*#else
108*void very_nice(){}
109*#endif
110*/
111
112void singular_example(char *str)
113{
114  assume(str!=NULL);
115  char *s=str;
116  while (*s==' ') s++;
117  char *ss=s;
118  while (*ss!='\0') ss++;
119  while (*ss<=' ')
120  {
121    *ss='\0';
122    ss--;
123  }
124  idhdl h=IDROOT->get(s,myynest);
125  if ((h!=NULL) && (IDTYP(h)==PROC_CMD))
126  {
127    char *lib=iiGetLibName(IDPROC(h));
128    if((lib!=NULL)&&(*lib!='\0'))
129    {
130      Print("// proc %s from lib %s\n",s,lib);
131      s=iiGetLibProcBuffer(IDPROC(h), 2);
132      if (s!=NULL)
133      {
134        if (strlen(s)>5)
135        {
136          iiEStart(s,IDPROC(h));
137          return;
138        }
139        else omFree((ADDRESS)s);
140      }
141    }
142  }
143  else
144  {
145    char sing_file[MAXPATHLEN];
146    FILE *fd=NULL;
147    char *res_m=feResource('m', 0);
148    if (res_m!=NULL)
149    {
150      sprintf(sing_file, "%s/%s.sing", res_m, s);
151      fd = feFopen(sing_file, "r");
152    }
153    if (fd != NULL)
154    {
155
156      int old_echo = si_echo;
157      int length, got;
158      char* s;
159
160      fseek(fd, 0, SEEK_END);
161      length = ftell(fd);
162      fseek(fd, 0, SEEK_SET);
163      s = (char*) omAlloc((length+20)*sizeof(char));
164      got = fread(s, sizeof(char), length, fd);
165      fclose(fd);
166      if (got != length)
167      {
168        Werror("Error while reading file %s", sing_file);
169        omFree(s);
170      }
171      else
172      {
173        s[length] = '\0';
174        strcat(s, "\n;return();\n\n");
175        si_echo = 2;
176        iiEStart(s, NULL);
177        si_echo = old_echo;
178      }
179    }
180    else
181    {
182      Werror("no example for %s", str);
183    }
184  }
185}
186
187
188struct soptionStruct
189{
190  char * name;
191  unsigned   setval;
192  unsigned   resetval;
193};
194
195struct soptionStruct optionStruct[]=
196{
197  {"prot",         Sy_bit(OPT_PROT),           ~Sy_bit(OPT_PROT)   },
198  {"redSB",        Sy_bit(OPT_REDSB),          ~Sy_bit(OPT_REDSB)   },
199  {"notBuckets",   Sy_bit(OPT_NOT_BUCKETS),    ~Sy_bit(OPT_NOT_BUCKETS)   },
200  {"notSugar",     Sy_bit(OPT_NOT_SUGAR),      ~Sy_bit(OPT_NOT_SUGAR)   },
201  {"interrupt",    Sy_bit(OPT_INTERRUPT),      ~Sy_bit(OPT_INTERRUPT)   },
202  {"sugarCrit",    Sy_bit(OPT_SUGARCRIT),      ~Sy_bit(OPT_SUGARCRIT)   },
203  {"teach",     Sy_bit(OPT_DEBUG),          ~Sy_bit(OPT_DEBUG)  },
204  /* 9 return SB in syz, quotient, intersect */
205  {"returnSB",     Sy_bit(OPT_RETURN_SB),      ~Sy_bit(OPT_RETURN_SB)  },
206  {"fastHC",       Sy_bit(OPT_FASTHC),         ~Sy_bit(OPT_FASTHC)  },
207  /* 11-19 sort in L/T */
208  {"keepvars",     Sy_bit(OPT_KEEPVARS),       ~Sy_bit(OPT_KEEPVARS) },
209  {"staircaseBound",Sy_bit(OPT_STAIRCASEBOUND),~Sy_bit(OPT_STAIRCASEBOUND)  },
210  {"multBound",    Sy_bit(OPT_MULTBOUND),      ~Sy_bit(OPT_MULTBOUND)  },
211  {"degBound",     Sy_bit(OPT_DEGBOUND),       ~Sy_bit(OPT_DEGBOUND)  },
212  /* 25 no redTail(p)/redTail(s) */
213  {"redTail",      Sy_bit(OPT_REDTAIL),        ~Sy_bit(OPT_REDTAIL)  },
214  {"redThrough",   Sy_bit(OPT_REDTHROUGH),     ~Sy_bit(OPT_REDTHROUGH)  },
215  {"lazy",         Sy_bit(OPT_OLDSTD),         ~Sy_bit(OPT_OLDSTD)  },
216  {"intStrategy",  Sy_bit(OPT_INTSTRATEGY),    ~Sy_bit(OPT_INTSTRATEGY)  },
217  {"infRedTail",   Sy_bit(OPT_INFREDTAIL),     ~Sy_bit(OPT_INFREDTAIL)  },
218  /* 30: use not regularity for syz */
219  {"notRegularity",Sy_bit(OPT_NOTREGULARITY),  ~Sy_bit(OPT_NOTREGULARITY)  },
220  {"weightM",      Sy_bit(OPT_WEIGHTM),        ~Sy_bit(OPT_WEIGHTM)  },
221/*special for "none" and also end marker for showOption:*/
222  {"ne",           0,                          0 }
223};
224
225struct soptionStruct verboseStruct[]=
226{
227  {"mem",      Sy_bit(V_SHOW_MEM),  ~Sy_bit(V_SHOW_MEM)   },
228  {"yacc",     Sy_bit(V_YACC),      ~Sy_bit(V_YACC)       },
229  {"redefine", Sy_bit(V_REDEFINE),  ~Sy_bit(V_REDEFINE)   },
230  {"reading",  Sy_bit(V_READING),   ~Sy_bit(V_READING)    },
231  {"loadLib",  Sy_bit(V_LOAD_LIB),  ~Sy_bit(V_LOAD_LIB)   },
232  {"debugLib", Sy_bit(V_DEBUG_LIB), ~Sy_bit(V_DEBUG_LIB)  },
233  {"loadProc", Sy_bit(V_LOAD_PROC), ~Sy_bit(V_LOAD_PROC)  },
234  {"defRes",   Sy_bit(V_DEF_RES),   ~Sy_bit(V_DEF_RES)    },
235  {"debugMem", Sy_bit(V_DEBUG_MEM), ~Sy_bit(V_DEBUG_MEM)  },
236  {"usage",    Sy_bit(V_SHOW_USE),  ~Sy_bit(V_SHOW_USE)   },
237  {"Imap",     Sy_bit(V_IMAP),      ~Sy_bit(V_IMAP)       },
238  {"prompt",   Sy_bit(V_PROMPT),    ~Sy_bit(V_PROMPT)     },
239  {"notWarnSB",Sy_bit(V_NSB),       ~Sy_bit(V_NSB)        },
240  {"contentSB",Sy_bit(V_CONTENTSB), ~Sy_bit(V_CONTENTSB)  },
241  {"cancelunit",Sy_bit(V_CANCELUNIT),~Sy_bit(V_CANCELUNIT)},
242  {"modpsolve",Sy_bit(V_MODPSOLVSB),~Sy_bit(V_MODPSOLVSB)},
243  {"geometricSB",Sy_bit(V_UPTORADICAL),~Sy_bit(V_UPTORADICAL)},
244  {"findMonomials",Sy_bit(V_FINDMONOM),~Sy_bit(V_FINDMONOM)},
245  {"coefStrat",Sy_bit(V_COEFSTRAT),~Sy_bit(V_COEFSTRAT)},
246/*special for "none" and also end marker for showOption:*/
247  {"ne",         0,          0 }
248};
249
250BOOLEAN setOption(leftv res, leftv v)
251{
252  char *n;
253  do
254  {
255    if (v->Typ()==STRING_CMD)
256    {
257      n=(char *)v->CopyD(STRING_CMD);
258    }
259    else
260    {
261      if (v->name==NULL)
262        return TRUE;
263      if (v->rtyp==0)
264      {
265        n=v->name;
266        v->name=NULL;
267      }
268      else
269      {
270        n=omStrDup(v->name);
271      }
272    }
273
274    int i;
275
276    if(strcmp(n,"get")==0)
277    {
278      intvec *w=new intvec(2);
279      (*w)[0]=test;
280      (*w)[1]=verbose;
281      res->rtyp=INTVEC_CMD;
282      res->data=(void *)w;
283      goto okay;
284    }
285    if(strcmp(n,"set")==0)
286    {
287      if((v->next!=NULL)
288      &&(v->next->Typ()==INTVEC_CMD))
289      {
290        v=v->next;
291        intvec *w=(intvec*)v->Data();
292        test=(*w)[0];
293        verbose=(*w)[1];
294
295        if (TEST_OPT_INTSTRATEGY && (currRing!=NULL)
296        && rField_has_simple_inverse()
297#ifdef HAVE_RING2TOM
298        && !rField_is_Ring_2toM(currRing)
299#endif
300        ) {
301          test &=~Sy_bit(OPT_INTSTRATEGY);
302        }
303        goto okay;
304      }
305    }
306    if(strcmp(n,"none")==0)
307    {
308      test=0;
309      verbose=0;
310      goto okay;
311    }
312    for (i=0; (i==0) || (optionStruct[i-1].setval!=0); i++)
313    {
314      if (strcmp(n,optionStruct[i].name)==0)
315      {
316        if (optionStruct[i].setval & validOpts)
317        {
318          test |= optionStruct[i].setval;
319          // optOldStd disables redthrough
320          if (optionStruct[i].setval == Sy_bit(OPT_OLDSTD))
321            test &= ~Sy_bit(OPT_REDTHROUGH);
322        }
323        else
324          Warn("cannot set option");
325        if (TEST_OPT_INTSTRATEGY && (currRing!=NULL)
326        && rField_has_simple_inverse()
327#ifdef HAVE_RING2TOM
328        && !rField_is_Ring_2toM(currRing)
329#endif
330        ) {
331          test &=~Sy_bit(OPT_INTSTRATEGY);
332        }
333        goto okay;
334      }
335      else if ((strncmp(n,"no",2)==0)
336      && (strcmp(n+2,optionStruct[i].name)==0))
337      {
338        if (optionStruct[i].setval & validOpts)
339        {
340          test &= optionStruct[i].resetval;
341        }
342        else
343          Warn("cannot clear option");
344        goto okay;
345      }
346    }
347    for (i=0; (i==0) || (verboseStruct[i-1].setval!=0); i++)
348    {
349      if (strcmp(n,verboseStruct[i].name)==0)
350      {
351        verbose |= verboseStruct[i].setval;
352        #ifdef YYDEBUG
353        #if YYDEBUG
354        if (BVERBOSE(V_YACC)) yydebug=1;
355        else                  yydebug=0;
356        #endif
357        #endif
358        goto okay;
359      }
360      else if ((strncmp(n,"no",2)==0)
361      && (strcmp(n+2,verboseStruct[i].name)==0))
362      {
363        verbose &= verboseStruct[i].resetval;
364        #ifdef YYDEBUG
365        #if YYDEBUG
366        if (BVERBOSE(V_YACC)) yydebug=1;
367        else                  yydebug=0;
368        #endif
369        #endif
370        goto okay;
371      }
372    }
373    Werror("unknown option `%s`",n);
374  okay:
375    if (currRing != NULL)
376      currRing->options = test & TEST_RINGDEP_OPTS;
377    omFree((ADDRESS)n);
378    v=v->next;
379  } while (v!=NULL);
380  #ifdef HAVE_TCL
381    if (tclmode)
382    {
383      BITSET tmp;
384      int i;
385      StringSetS("");
386      if ((test!=0)||(verbose!=0))
387      {
388        tmp=test;
389        if(tmp)
390        {
391          for (i=0; optionStruct[i].setval!=0; i++)
392          {
393            if (optionStruct[i].setval & test)
394            {
395              StringAppend(" %s",optionStruct[i].name);
396              tmp &=optionStruct[i].resetval;
397            }
398          }
399        }
400        tmp=verbose;
401        if (tmp)
402        {
403          for (i=0; verboseStruct[i].setval!=0; i++)
404          {
405            if (verboseStruct[i].setval & tmp)
406            {
407              StringAppend(" %s",verboseStruct[i].name);
408              tmp &=verboseStruct[i].resetval;
409            }
410          }
411        }
412        PrintTCLS('O',StringAppendS(""));
413        StringSetS("");
414      }
415      else
416      {
417        PrintTCLS('O'," ");
418      }
419    }
420  #endif
421    // set global variable to show memory usage
422    if (BVERBOSE(V_SHOW_MEM)) om_sing_opt_show_mem = 1;
423    else om_sing_opt_show_mem = 0;
424  return FALSE;
425}
426
427char * showOption()
428{
429  int i;
430  BITSET tmp;
431
432  StringSetS("//options:");
433  if ((test!=0)||(verbose!=0))
434  {
435    tmp=test;
436    if(tmp)
437    {
438      for (i=0; optionStruct[i].setval!=0; i++)
439      {
440        if (optionStruct[i].setval & test)
441        {
442          StringAppend(" %s",optionStruct[i].name);
443          tmp &=optionStruct[i].resetval;
444        }
445      }
446      for (i=0; i<32; i++)
447      {
448        if (tmp & Sy_bit(i)) StringAppend(" %d",i);
449      }
450    }
451    tmp=verbose;
452    if (tmp)
453    {
454      for (i=0; verboseStruct[i].setval!=0; i++)
455      {
456        if (verboseStruct[i].setval & tmp)
457        {
458          StringAppend(" %s",verboseStruct[i].name);
459          tmp &=verboseStruct[i].resetval;
460        }
461      }
462      for (i=1; i<32; i++)
463      {
464        if (tmp & Sy_bit(i)) StringAppend(" %d",i+32);
465      }
466    }
467    return omStrDup(StringAppendS(""));
468  }
469  else
470    return omStrDup(StringAppendS(" none"));
471}
472
473char * versionString()
474{
475  char* str = StringSetS("");
476  StringAppend("Singular for %s version %s  (%lu)  %s\nwith\n",
477               S_UNAME, S_VERSION1,
478               feVersionId,singular_date);
479  StringAppend("\t");
480#ifdef HAVE_FACTORY
481              StringAppend("factory(%s),", factoryVersion);
482#endif
483#ifdef HAVE_LIBFAC_P
484              StringAppend("libfac(%s,%s),\n\t",libfac_version,libfac_date);
485#endif
486#if defined (__GNU_MP_VERSION) && defined (__GNU_MP_VERSION_MINOR)
487              StringAppend("GMP(%d.%d),",__GNU_MP_VERSION,__GNU_MP_VERSION_MINOR);
488#else
489              StringAppendS("GMP(1.3),");
490#endif
491#ifdef HAVE_NTL
492#include "NTL/version.h"
493              StringAppend("NTL(%s),",NTL_VERSION);
494#endif
495#ifdef HAVE_MPSR
496              StringAppend("MP(%s),",MP_VERSION);
497#endif
498#if defined(HAVE_DYN_RL)
499              if (fe_fgets_stdin==fe_fgets_dummy)
500                StringAppendS("no input,");
501              else if (fe_fgets_stdin==fe_fgets)
502                StringAppendS("fgets,");
503              if (fe_fgets_stdin==fe_fgets_stdin_drl)
504                StringAppendS("dynamic readline,");
505              #ifdef HAVE_FEREAD
506              else if (fe_fgets_stdin==fe_fgets_stdin_emu)
507                StringAppendS("emulated readline,");
508              #endif
509              else
510                StringAppendS("unknown fgets method,");
511#else
512  #if defined(HAVE_READLINE) && !defined(FEREAD)
513              StringAppendS("static readline,");
514  #else
515    #ifdef HAVE_FEREAD
516              StringAppendS("emulated readline,");
517    #else
518              StringAppendS("fgets,");
519    #endif
520  #endif
521#endif
522#ifdef HAVE_PLURAL
523              StringAppendS("Plural,");
524#endif
525#ifdef HAVE_DBM
526              StringAppendS("DBM,\n\t");
527#else
528              StringAppendS("\n\t");
529#endif
530#ifdef HAVE_NS
531              StringAppendS("namespaces,");
532#endif
533#ifdef HAVE_DYNAMIC_LOADING
534              StringAppendS("dynamic modules,");
535#endif
536              if (p_procs_dynamic) StringAppendS("dynamic p_Procs,");
537#ifdef TEST
538              StringAppendS("TESTs,");
539#endif
540#if YYDEBUG
541              StringAppendS("YYDEBUG=1,");
542#endif
543#ifdef HAVE_ASSUME
544             StringAppendS("ASSUME,");
545#endif
546#ifdef MDEBUG
547              StringAppend("MDEBUG=%d,",MDEBUG);
548#endif
549#ifdef OM_CHECK
550              StringAppend("OM_CHECK=%d,",OM_CHECK);
551#endif
552#ifdef OM_TRACK
553              StringAppend("OM_TRACK=%d,",OM_TRACK);
554#endif
555#ifdef OM_NDEBUG
556              StringAppend("OM_NDEBUG,");
557#endif
558#ifdef PDEBUG
559              StringAppendS("PDEBUG,");
560#endif
561#ifdef KDEBUG
562              StringAppendS("KDEBUG,");
563#endif
564#ifndef __OPTIMIZE__
565              StringAppendS("-g,");
566#endif
567#ifdef HAVE_EIGENVAL
568              StringAppendS("eigenvalues,");
569#endif
570#ifdef HAVE_GMS
571              StringAppendS("Gauss-Manin system,");
572#endif
573              StringAppend("random=%d\n",siRandomStart);
574              StringAppend("\tCC=%s,\n\tCXX=%s"
575#ifdef __GNUC__
576              "(" __VERSION__ ")"
577#endif
578              "\n",CC,CXX);
579              feStringAppendResources(0);
580              feStringAppendBrowsers(0);
581              StringAppend("\n");
582              return str;
583}
584
585#ifdef HAVE_NS
586void listall(int showproc)
587{
588      idhdl hh=basePack->idroot;
589      PrintS("====== Top ==============\n");
590      while (hh!=NULL)
591      {
592        if (showproc || (IDTYP(hh)!=PROC_CMD))
593        {
594          if (IDDATA(hh)==(void *)currRing) PrintS("(R)");
595          else if (IDDATA(hh)==(void *)currPack) PrintS("(P)");
596          else PrintS("   ");
597          Print("::%s, typ %s level %d data %x",
598                 IDID(hh),Tok2Cmdname(IDTYP(hh)),IDLEV(hh),IDDATA(hh));
599          if ((IDTYP(hh)==RING_CMD)
600          || (IDTYP(hh)==QRING_CMD))
601            Print(" ref: %d\n",IDRING(hh)->ref);
602          else
603            PrintLn();
604        }
605        hh=IDNEXT(hh);
606      }
607      hh=basePack->idroot;
608      while (hh!=NULL)
609      {
610        if (IDDATA(hh)==(void *)basePack)
611          Print("(T)::%s, typ %s level %d data %x\n",
612          IDID(hh),Tok2Cmdname(IDTYP(hh)),IDLEV(hh),IDDATA(hh));
613        else
614        if ((IDTYP(hh)==RING_CMD)
615        || (IDTYP(hh)==QRING_CMD)
616        || (IDTYP(hh)==PACKAGE_CMD))
617        {
618          Print("====== %s ==============\n",IDID(hh));
619          idhdl h2=IDRING(hh)->idroot;
620          while (h2!=NULL)
621          {
622            if (showproc || (IDTYP(h2)!=PROC_CMD))
623            {
624              if ((IDDATA(h2)==(void *)currRing)
625              && ((IDTYP(h2)==RING_CMD)||(IDTYP(h2)==QRING_CMD)))
626                PrintS("(R)");
627              else if (IDDATA(h2)==(void *)currPack) PrintS("(P)");
628              else PrintS("   ");
629              Print("%s::%s, typ %s level %d data %x\n",
630              IDID(hh),IDID(h2),Tok2Cmdname(IDTYP(h2)),IDLEV(h2),IDDATA(h2));
631            }
632            h2=IDNEXT(h2);
633          }
634        }
635        hh=IDNEXT(hh);
636      }
637      Print("currRing:%x, currPack:%x,basePack:%x\n",currRing,currPack,basePack);
638      iiCheckPack(currPack);
639}
640#ifndef NDEBUG
641void checkall()
642{
643      idhdl hh=basePack->idroot;
644      while (hh!=NULL)
645      {
646        omCheckAddr(hh);
647        omCheckAddr(IDID(hh));
648        if (RingDependend(IDTYP(hh))) Print("%s typ %d in Top\n",IDID(hh),IDTYP(hh));
649        hh=IDNEXT(hh);
650      }
651      hh=basePack->idroot;
652      while (hh!=NULL)
653      {
654        if (IDTYP(hh)==PACKAGE_CMD)
655        {
656          idhdl h2=IDPACKAGE(hh)->idroot;
657          while (h2!=NULL)
658          {
659            omCheckAddr(h2);
660            omCheckAddr(IDID(h2));
661            if (RingDependend(IDTYP(h2))) Print("%s typ %d in %s\n",IDID(h2),IDTYP(h2),IDID(hh));
662            h2=IDNEXT(h2);
663          }
664        }
665        hh=IDNEXT(hh);
666      }
667}
668#endif
669#endif
Note: See TracBrowser for help on using the repository browser.