source: git/Singular/misc.cc @ f69a29

spielwiese
Last change on this file since f69a29 was f69a29, checked in by Hans Schönemann <hannes@…>, 26 years ago
* hannes: changed -v header al little bit git-svn-id: file:///usr/local/Singular/svn/trunk@1593 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 16.6 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT:
6*/
7
8#include <string.h>
9#include "mod2.h"
10#ifndef macintosh
11#include <unistd.h>
12#endif
13#include <stdio.h>
14#include <stddef.h>
15#include <stdlib.h>
16#include <time.h>
17#include <limits.h>
18
19#include "tok.h"
20#include "febase.h"
21#include "cntrlc.h"
22#include "mmemory.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#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
44#ifdef HAVE_GMP
45extern "C" {
46#include <gmp.h>
47}
48#endif
49#ifdef HAVE_MPSR
50#include <MP_Config.h>
51#endif
52
53/*0 implementation*/
54
55/*2
56* initialize components of Singular
57*/
58int inits(void)
59{
60  int t;
61/*4 signal handler:*/
62  init_signals();
63/*4 randomize: */
64  t=initTimer();
65  /*t=(int)time(NULL);*/
66  if (t==0) t=1;
67#ifdef HAVE_RTIMER
68  initRTimer();
69#endif
70#ifdef buildin_rand
71  siSeed=t;
72#else
73  srand((unsigned int)t);
74#endif
75#ifdef HAVE_FACTORY
76  factoryseed(t);
77#endif
78/*4 private data of other modules*/
79  memset(&sLastPrinted,0,sizeof(sleftv));
80  sLastPrinted.rtyp=NONE;
81#ifdef HAVE_MPSR
82  extern void mpsr_Init();
83  mpsr_Init();
84#endif
85  return t;
86}
87
88/*2
89* the global exit routine of Singular
90*/
91extern "C" {
92void m2_end(short i)
93{
94  if (i==0)
95  {
96    #ifdef HAVE_TCL
97    if (tclmode)
98      PrintTCL('Q',0,NULL);
99    else
100    #endif
101      printf("Auf Wiedersehen.\n");
102  }
103  else
104  {
105    #ifdef HAVE_TCL
106    if (tclmode)
107      PrintTCL('Q',0,NULL);
108    else
109    #endif
110      printf("\nhalt %d\n",i);
111  }
112  exit(i);
113}
114}
115
116/*2
117* the renice routine for very large jobs
118* works only on unix machines,
119* testet on : linux, HP 9.0
120*
121*#ifndef MSDOS
122*#ifndef macintosh
123*#include <sys/times.h>
124*#include <sys/resource.h>
125*extern "C" int setpriority(int,int,int);
126*void very_nice()
127*{
128*#ifndef NO_SETPRIORITY
129*  setpriority(PRIO_PROCESS,0,19);
130*#endif
131*  sleep(10);
132*}
133*#else
134*void very_nice(){}
135*#endif
136*#else
137*void very_nice(){}
138*#endif
139*/
140
141/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
142#ifdef buildin_rand
143/*
144 *
145 *  A prime modulus multiplicative linear congruential
146 *  generator (PMMLCG), or "Lehmer generator".
147 *  Implementation directly derived from the article:
148 *
149 *        S. K. Park and K. W. Miller
150 *        Random Number Generators: Good Ones are Hard to Find
151 *        CACM vol 31, #10. Oct. 1988. pp 1192-1201.
152 *
153 *  Using the following multiplier and modulus, we obtain a
154 *  generator which:
155 *
156 *        1)  Has a full period: 1 to 2^31 - 2.
157 *        2)  Is testably "random" (see the article).
158 *        3)  Has a known implementation by E. L. Schrage.
159 */
160
161
162#define  A        16807L        /*  A "good" multiplier          */
163#define  M   2147483647L        /*  Modulus: 2^31 - 1          */
164#define  Q       127773L        /*  M / A                  */
165#define  R         2836L        /*  M % A                  */
166
167
168int siSeed = 1L;
169
170
171int siRand()
172{
173  siSeed = A * (siSeed % Q) - R * (siSeed / Q);
174
175  if ( siSeed < 0 )
176    siSeed += M;
177
178  return( siSeed );
179}
180#endif
181
182/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
183#define HELP_OK        0
184
185#ifdef buildin_help
186
187#define FIN_INDEX    '\037'
188#define not  !
189#define HELP_NOT_OPEN  1
190#define HELP_NOT_FOUND 2
191#ifndef macintosh
192#define Index_File     SINGULAR_INFODIR "/singular.hlp"
193#define Help_File      SINGULAR_INFODIR "/singular.hlp"
194#else
195#define Index_File     SINGULAR_INFODIR "singular.hlp"
196#define Help_File      SINGULAR_INFODIR "singular.hlp"
197#endif
198#define BUF_LEN        128
199#define IDX_LEN        64
200#define MAX_LINES      21
201
202static int compare(char *s1,char *s2)
203{
204  for(;*s1==*s2;s1++,s2++)
205     if(*s2=='\0') return(0);
206  return(*s2);
207}
208
209#ifdef macintosh
210static char tolow(char p)
211#else
212static inline char tolow(char p)
213#endif
214{
215  if (('A'<=p)&&(p<='Z')) return p | 040;
216  return p;
217}
218
219/*************************************************/
220static int show(unsigned long offset,FILE *help, char *close)
221{ char buffer[BUF_LEN+1];
222  int  lines = 0;
223
224  if( help== NULL)
225    if( (help = feFopen(Help_File, "r")) == NULL)
226      return HELP_NOT_OPEN;
227
228  fseek(help,  (long)(offset+1), (int)0);
229  while( !feof(help)
230        && *fgets(buffer, BUF_LEN, help) != EOF
231        && buffer[0] != FIN_INDEX)
232  {
233    printf("%s", buffer);
234    if(lines++> MAX_LINES)
235    {
236#ifdef macintosh
237      printf("\n Press <RETURN> to continue or x and <RETURN> to exit help.\n");
238#else
239      printf("\n Press <RETURN> to continue or x to exit help.\n");
240#endif
241      fflush(stdout);
242      *close = (char)getchar();
243      if(*close=='x')
244      {
245        getchar();
246        break;
247      }
248      lines=0;
249    }
250  }
251  if(*close!='x')
252  {
253#ifdef macintosh
254    printf("\nEnd of part. Press <RETURN> to continue or x and <RETURN> to exit help.\n");
255#else
256    printf("\nEnd of part. Press <RETURN> to continue or x to exit help.\n");
257#endif
258    fflush(stdout);
259    *close = (char)getchar();
260    if(*close=='x')
261      getchar();
262  }
263  return HELP_OK;
264}
265
266/*************************************************/
267int singular_manual(char *str)
268{ FILE *index=NULL,*help=NULL;
269  unsigned long offset;
270  char *p,close;
271  int done = 0;
272  char buffer[BUF_LEN+1],
273       Index[IDX_LEN+1],
274       String[IDX_LEN+1];
275
276  if( (index = feFopen(Index_File, "r",NULL,TRUE)) == NULL)
277  {
278    return HELP_NOT_OPEN;
279  }
280
281  for(p=str; *p; p++) *p = tolow(*p);/* */
282  do
283  {
284    p--;
285  }
286  while ((p != str) && (*p<=' '));
287  p++;
288  *p='\0';
289  (void)sprintf(String, " %s ", str);
290
291  while(!feof(index)
292        && *fgets(buffer, BUF_LEN, index) != EOF
293        && buffer[0] != FIN_INDEX);
294
295  while(!feof(index))
296  {
297    (void)fgets(buffer, BUF_LEN, index); /* */
298    (void)sscanf(buffer, "Node:%[^\177]\177%ld\n", Index, &offset);
299    for(p=Index; *p; p++) *p = tolow(*p);/* */
300    (void)strcat(Index, " ");
301    if( strstr(Index, String)!=NULL)
302    {
303      done++; (void)show(offset, help, &close);
304    }
305    Index[0]='\0';
306    if(close=='x')
307    break;
308  }
309  (void)fclose(index);
310  (void)fclose(help);
311  if(not done)
312  {
313    Warn("`%s` not found",String);
314    return HELP_NOT_FOUND;
315  }
316  return HELP_OK;
317}
318#endif
319/*************************************************/
320
321void singular_help(char *str,BOOLEAN example)
322{
323  char *s=str;
324  while (*s==' ') s++;
325  char *ss=s;
326  while (*ss!='\0') ss++;
327  while (*ss<=' ')
328  {
329    *ss='\0';
330    ss--;
331  }
332  /* --------- is it a proc ? --------------------------------*/
333  idhdl h=idroot->get(s,myynest);
334  if ((h!=NULL) && (IDTYP(h)==PROC_CMD))
335  {
336    char *lib=iiGetLibName(IDPROC(h));
337    Print("// proc %s ",s);
338    if((lib==NULL)||(*lib=='\0'))
339    {
340      PrintLn();
341    }
342    else
343    {
344      Print("from lib %s\n",lib);
345      s=iiGetLibProcBuffer(IDPROC(h), example ? 2 : 0);
346      if (!example)
347      {
348        PrintS(s);
349        FreeL((ADDRESS)s);
350      }
351      else
352      {
353        if (s!=NULL)
354        {
355          if (strlen(s)>5) iiEStart(s,IDPROC(h));
356          else FreeL((ADDRESS)s);
357        }
358      }
359    }
360  }
361  else if (example)
362  {
363    Werror("%s not found",s);
364  }
365  else
366  {
367  /* --------- is it a library ? --------------------------------*/
368    char libnamebuf[128];
369    FILE *fp=feFopen(str,"rb", libnamebuf);
370    if (fp!=NULL)
371    {
372#ifdef HAVE_LIBPARSER
373      extern FILE *yylpin;
374      lib_style_types lib_style; // = OLD_LIBSTYLE;
375
376      yylpin = fp;
377      yylplex(str, libnamebuf, &lib_style, GET_INFO);
378      reinit_yylp();
379      if(lib_style == OLD_LIBSTYLE)
380      {
381        char buf[256];
382        fseek(fp, 0, SEEK_SET);
383#else /* HAVE_LIBPARSER */
384        { char buf[256];
385#endif /* HAVE_LIBPARSER */
386        Warn( "library %s has an old format. Please fix it for the next time",
387              str);
388        BOOLEAN found=FALSE;
389        while (fgets( buf, sizeof(buf), fp))
390        {
391          if (strncmp(buf,"//",2)==0)
392          {
393            if (found) return;
394          }
395          else if ((strncmp(buf,"proc ",5)==0)||(strncmp(buf,"LIB ",4)==0))
396          {
397            if (!found) Warn("no help part in library found");
398            return;
399          }
400          else
401          {
402            found=TRUE;
403            PrintS(buf);
404          }
405        }
406      }
407#ifdef HAVE_LIBPARSER
408      else
409      {
410        fclose( yylpin );
411        PrintS(text_buffer);
412        FreeL(text_buffer);
413      }
414#endif /* HAVE_LIBPARSER */
415    }
416  /* --------- everything else is for the manual ----------------*/
417    else
418    {
419#ifdef buildin_help
420      singular_manual(str);
421#else
422      char tmp[150];
423      char tmp2[150];
424      char strstr[100];
425      sprintf(tmp,"%s/singular.hlp", SINGULAR_INFODIR);
426      if (strcmp(str,"index")==0)
427         strstr[0]='\0';
428       else
429         sprintf(strstr," Index \"%s\"",str);
430      if (!access(tmp, R_OK))
431      {
432        sprintf(tmp, "info -f %s/singular.hlp %s", SINGULAR_INFODIR, strstr);
433      }
434      else
435      {
436        FILE *fd = feFopen("singular.hlp", "r", tmp2, FALSE);
437        if (fd != NULL)
438        {
439          fclose(fd);
440          sprintf(tmp, "info -f %s %s", tmp2,strstr);
441        }
442        else
443          sprintf(tmp,"info singular %s",strstr);
444      }
445      system(tmp);
446#ifndef MSDOS
447      //sprintf(tmp,"clear");
448      //system(tmp);
449#endif
450#endif
451    }
452  }
453}
454
455
456struct soptionStruct
457{
458  char * name;
459  int   setval;
460  int   resetval;
461};
462
463struct soptionStruct optionStruct[]=
464{
465  {"prot",         Sy_bit(OPT_PROT),           ~Sy_bit(OPT_PROT)   },
466  {"redSB",        Sy_bit(OPT_REDSB),          ~Sy_bit(OPT_REDSB)   },
467  /* 2 Gebauer/Moeller */
468  {"notSugar",     Sy_bit(OPT_NOT_SUGAR),      ~Sy_bit(OPT_NOT_SUGAR)   },
469  {"interrupt",    Sy_bit(OPT_INTERRUPT),      ~Sy_bit(OPT_INTERRUPT)   },
470  {"sugarCrit",    Sy_bit(OPT_SUGARCRIT),      ~Sy_bit(OPT_SUGARCRIT)   },
471  {"teach",     Sy_bit(OPT_DEBUG),          ~Sy_bit(OPT_DEBUG)  },
472  /* 7 cancel unit */
473  {"morePairs",    Sy_bit(OPT_MOREPAIRS),      ~Sy_bit(OPT_MOREPAIRS)   },
474  /* 9 return SB in syz, quotient, intersect */
475  {"returnSB",     Sy_bit(OPT_RETURN_SB),      ~Sy_bit(OPT_RETURN_SB)  },
476  {"fastHC",       Sy_bit(OPT_FASTHC),         ~Sy_bit(OPT_FASTHC)  },
477  /* 11-19 sort in L/T */
478  /* 20 redBest */
479  {"keepvars",     Sy_bit(OPT_KEEPVARS),       ~Sy_bit(OPT_KEEPVARS) },
480  {"staircaseBound",Sy_bit(OPT_STAIRCASEBOUND),~Sy_bit(OPT_STAIRCASEBOUND)  },
481  {"multBound",    Sy_bit(OPT_MULTBOUND),      ~Sy_bit(OPT_MULTBOUND)  },
482  {"degBound",     Sy_bit(OPT_DEGBOUND),       ~Sy_bit(OPT_DEGBOUND)  },
483  /* 25 no redTail(p)/redTail(s) */
484  {"redTail",      Sy_bit(OPT_REDTAIL),        ~Sy_bit(OPT_REDTAIL)  },
485  {"intStrategy",  Sy_bit(OPT_INTSTRATEGY),    ~Sy_bit(OPT_INTSTRATEGY)  },
486  /* 27 stop at HC (finiteDeterminacyTest) */
487  {"minRes",       Sy_bit(OPT_MINRES),         ~Sy_bit(OPT_MINRES)  },
488  /* 30: use not regularity for syz */
489  {"notRegularity",Sy_bit(OPT_NOTREGULARITY),  ~Sy_bit(OPT_NOTREGULARITY)  },
490  {"weightM",      Sy_bit(OPT_WEIGHTM),        ~Sy_bit(OPT_WEIGHTM)  },
491/*special for "none" and also end marker for showOption:*/
492  {"ne",           0,                          0 }
493};
494
495struct soptionStruct verboseStruct[]=
496{
497  {"mem",      Sy_bit(V_SHOW_MEM),  ~Sy_bit(V_SHOW_MEM)   },
498  {"yacc",     Sy_bit(V_YACC),      ~Sy_bit(V_YACC)       },
499  {"redefine", Sy_bit(V_REDEFINE),  ~Sy_bit(V_REDEFINE)   },
500  {"reading",  Sy_bit(V_READING),   ~Sy_bit(V_READING)    },
501  {"loadLib",  Sy_bit(V_LOAD_LIB),  ~Sy_bit(V_LOAD_LIB)   },
502  {"debugLib", Sy_bit(V_DEBUG_LIB), ~Sy_bit(V_DEBUG_LIB)  },
503  {"loadProc", Sy_bit(V_LOAD_PROC), ~Sy_bit(V_LOAD_PROC)  },
504  {"defRes",   Sy_bit(V_DEF_RES),   ~Sy_bit(V_DEF_RES)    },
505  {"debugMem", Sy_bit(V_DEBUG_MEM), ~Sy_bit(V_DEBUG_MEM)  },
506  {"usage",    Sy_bit(V_SHOW_USE),  ~Sy_bit(V_SHOW_USE)   },
507  {"Imap",     Sy_bit(V_IMAP),      ~Sy_bit(V_IMAP)       },
508  {"prompt",   Sy_bit(V_PROMPT),    ~Sy_bit(V_PROMPT)     },
509  {"notWarnSB",Sy_bit(V_NSB),       ~Sy_bit(V_NSB)        },
510/*special for "none" and also end marker for showOption:*/
511  {"ne",         0,          0 }
512};
513
514BOOLEAN setOption(leftv res, leftv v)
515{
516  char *n;
517  do
518  {
519    n=v->name;
520    if (n==NULL) return TRUE;
521    v->name=NULL;
522
523    int i;
524
525    if(strcmp(n,"get")==0)
526    {
527      intvec *w=new intvec(2);
528      (*w)[0]=test;
529      (*w)[1]=verbose;
530      res->rtyp=INTVEC_CMD;
531      res->data=(void *)w;
532      goto okay;
533    }
534    if(strcmp(n,"set")==0)
535    {
536      if((v->next!=NULL)
537      &&(v->next->Typ()==INTVEC_CMD))
538      {
539        v=v->next;
540        intvec *w=(intvec*)v->Data();
541        test=(*w)[0];
542        verbose=(*w)[1];
543
544        if (TEST_OPT_INTSTRATEGY && (currRing!=NULL) && (currRing->ch>=2))
545        {
546          test &=~Sy_bit(OPT_INTSTRATEGY);
547        }
548        goto okay;
549      }
550    }
551    if(strcmp(n,"none")==0)
552    {
553      test=0;
554      verbose=0;
555      goto okay;
556    }
557    for (i=0; (i==0) || (optionStruct[i-1].setval!=0); i++)
558    {
559      if (strcmp(n,optionStruct[i].name)==0)
560      {
561        if (optionStruct[i].setval & validOpts)
562          test |= optionStruct[i].setval;
563        else
564          Warn("cannot set option");
565        if (TEST_OPT_INTSTRATEGY && (currRing!=NULL) && (currRing->ch>=2))
566        {
567          test &=~Sy_bit(OPT_INTSTRATEGY);
568        }
569        goto okay;
570      }
571      else if ((strncmp(n,"no",2)==0)
572      && (strcmp(n+2,optionStruct[i].name)==0))
573      {
574        if (optionStruct[i].setval & validOpts)
575        {
576          test &= optionStruct[i].resetval;
577        }
578        else
579          Warn("cannot clear option");
580        goto okay;
581      }
582    }
583    for (i=0; (i==0) || (verboseStruct[i-1].setval!=0); i++)
584    {
585      if (strcmp(n,verboseStruct[i].name)==0)
586      {
587        verbose |= verboseStruct[i].setval;
588        #ifdef YYDEBUG
589        #if YYDEBUG
590        if (BVERBOSE(V_YACC)) yydebug=1;
591        else                  yydebug=0;
592        #endif
593        #endif
594        goto okay;
595      }
596      else if ((strncmp(n,"no",2)==0)
597      && (strcmp(n+2,verboseStruct[i].name)==0))
598      {
599        verbose &= verboseStruct[i].resetval;
600        #ifdef YYDEBUG
601        #if YYDEBUG
602        if (BVERBOSE(V_YACC)) yydebug=1;
603        else                  yydebug=0;
604        #endif
605        #endif
606        goto okay;
607      }
608    }
609    Werror("unknown option `%s`",n);
610  okay:
611    FreeL((ADDRESS)n);
612    v=v->next;
613  } while (v!=NULL);
614  return FALSE;
615}
616
617void showOption()
618{
619  int i;
620  BITSET tmp;
621
622  PrintS("//options:");
623  if ((test!=0)||(verbose!=0))
624  {
625    tmp=test;
626    if(tmp)
627    {
628      for (i=0; optionStruct[i].setval!=0; i++)
629      {
630        if (optionStruct[i].setval & test)
631        {
632          Print(" %s",optionStruct[i].name);
633          tmp &=optionStruct[i].resetval;
634        }
635      }
636      for (i=0; i<32; i++)
637      {
638        if (tmp & Sy_bit(i)) Print(" %d",i);
639      }
640    }
641    tmp=verbose;
642    if (tmp)
643    {
644      for (i=0; verboseStruct[i].setval!=0; i++)
645      {
646        if (verboseStruct[i].setval & tmp)
647        {
648          Print(" %s",verboseStruct[i].name);
649          tmp &=verboseStruct[i].resetval;
650        }
651      }
652      for (i=1; i<32; i++)
653      {
654        if (tmp & Sy_bit(i)) Print(" %d",i+32);
655      }
656    }
657    PrintLn();
658  }
659  else
660    PrintS(" none\n");
661}
662
663char * versionString()
664{
665  StringSet("\t");
666#ifdef HAVE_FACTORY
667              StringAppend("factory(%s),", factoryVersion);
668#endif
669#ifdef HAVE_LIBFAC_P
670              StringAppend("libfac(%s,%s),\n\t",libfac_version,libfac_date);
671#endif
672#ifdef HAVE_GMP
673#if defined (__GNU_MP_VERSION) && defined (__GNU_MP_VERSION_MINOR)
674              StringAppend("GMP(%d.%d),",__GNU_MP_VERSION,__GNU_MP_VERSION_MINOR);
675#elif defined (HAVE_SMALLGMP)
676              StringAppendS("SmallGMP(2.0.2.0),");
677#else
678              StringAppendS("GMP(1.3),");
679#endif
680#endif
681#ifdef HAVE_MPSR
682              StringAppend("MP(%s),",MP_VERSION);
683#endif
684#if defined(HAVE_READLINE) && !defined(FEREAD)
685              StringAppendS("libreadline,\n\t");
686#else
687#ifdef HAVE_FEREAD
688              StringAppendS("emulated libreadline,\n\t");
689#else
690              StringAppendS("\n\t");
691#endif
692#endif
693#ifdef SRING
694              StringAppendS("super algebra,");
695#endif
696#ifdef DRING
697              StringAppendS("Weyl algebra,");
698#endif
699#ifdef HAVE_DBM
700              StringAppendS("DBM,");
701#endif
702#ifdef HAVE_INFO
703              StringAppendS("info,");
704#endif
705#ifdef TEST
706              StringAppendS("TESTs,");
707#endif
708#if YYDEBUG
709              StringAppendS("YYDEBUG=1,");
710#endif
711#ifdef MDEBUG
712              StringAppend("MDEBUG=%d,",MDEBUG);
713#endif
714#ifdef PDEBUG
715              StringAppend("PDEBUG,");
716#endif
717#ifdef KDEBUG
718              StringAppend("KDEBUG,");
719#endif
720#ifdef TEST_MAC_ORDER
721              StringAppendS("mac_order,");
722#endif
723#ifndef __OPTIMIZE__
724              StringAppend("-g,");
725#endif
726              StringAppend("random=%d\n",siRandomStart);
727              return StringAppend("search path %s", feGetSearchPath());
728}
Note: See TracBrowser for help on using the repository browser.