source: git/Singular/misc.cc @ fdc537

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