source: git/Singular/misc.cc @ 5ecf042

spielwiese
Last change on this file since 5ecf042 was 5ecf042, checked in by Olaf Bachmann <obachman@…>, 23 years ago
* LIB: some categoy changes * libparse: added -c option * reimplementation of reing dpenendent options (put to ring->options) git-svn-id: file:///usr/local/Singular/svn/trunk@4952 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    if (currRing != NULL)
477      currRing->options = test & TEST_RINGDEP_OPTS;
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("static 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("dynamic modules,");
635#endif
636              if (p_procs_dynamic) StringAppendS("dynamic p_Procs,");
637#ifdef TEST
638              StringAppendS("TESTs,");
639#endif
640#if YYDEBUG
641              StringAppendS("YYDEBUG=1,");
642#endif
643#ifdef HAVE_ASSUME
644             StringAppendS("ASSUME,");
645#endif
646#ifdef MDEBUG
647              StringAppend("MDEBUG=%d,",MDEBUG);
648#endif
649#ifdef OM_CHECK
650              StringAppend("OM_CHECK=%d,",OM_CHECK);
651#endif
652#ifdef OM_TRACK
653              StringAppend("OM_TRACK=%d,",OM_TRACK);
654#endif
655#ifdef OM_NDEBUG
656              StringAppend("OM_NDEBUG,");
657#endif
658#ifdef PDEBUG
659              StringAppendS("PDEBUG,");
660#endif
661#ifdef KDEBUG
662              StringAppendS("KDEBUG,");
663#endif
664#ifndef __OPTIMIZE__
665#ifdef __MWERKS__
666              StringAppendS(" Codewarrior 2.0,");
667#else
668              StringAppendS("-g,");
669#endif
670#endif
671              StringAppend("random=%d\n",siRandomStart);
672
673              feStringAppendResources(0);
674              feStringAppendBrowsers(0);
675              StringAppend("\n");
676              return str;
677}
678
679
Note: See TracBrowser for help on using the repository browser.