source: git/Singular/misc.cc @ 8f769bb

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