source: git/Singular/misc.cc @ faab65

spielwiese
Last change on this file since faab65 was faab65, checked in by Olaf Bachmann <obachman@…>, 25 years ago
1998-04-16 Olaf Bachmann <obachman@mathematik.uni-kl.de> * Got rid off strstr definition git-svn-id: file:///usr/local/Singular/svn/trunk@1383 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 16.4 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        char buf[256];
381        fseek(fp, 0, SEEK_SET);
382#else /* HAVE_LIBPARSER */
383        { char buf[256];
384#endif /* HAVE_LIBPARSER */
385        Warn( "library %s has an old format. Please fix it for the next time",
386              str);
387        BOOLEAN found=FALSE;
388        while (fgets( buf, sizeof(buf), fp))
389          {
390            if (strncmp(buf,"//",2)==0)
391              {
392                if (found) return;
393              }
394            else if ((strncmp(buf,"proc ",5)==0)||(strncmp(buf,"LIB ",4)==0))
395              {
396                if (!found) Warn("no help part in library found");
397                return;
398              }
399            else
400              {
401                found=TRUE;
402                PrintS(buf);
403              }
404          }
405      } else {
406        fclose( yylpin );
407        PrintS(text_buffer);
408        FreeL(text_buffer);
409      }
410    }
411  /* --------- everything else is for the manual ----------------*/
412    else
413    {
414#ifdef buildin_help
415      singular_manual(str);
416#else
417      char tmp[150];
418      char tmp2[150];
419      char strstr[100];
420      sprintf(tmp,"%s/singular.hlp", SINGULAR_INFODIR);
421      if (strcmp(str,"index")==0)
422         strstr[0]='\0';
423       else
424         sprintf(strstr," Index \"%s\"",str);
425      if (!access(tmp, R_OK))
426      {
427        sprintf(tmp, "info -f %s/singular.hlp %s", SINGULAR_INFODIR, strstr);
428      }
429      else
430      {
431        FILE *fd = feFopen("singular.hlp", "r", tmp2, FALSE);
432        if (fd != NULL)
433        {
434          fclose(fd);
435          sprintf(tmp, "info -f %s %s", tmp2,strstr);
436        }
437        else
438          sprintf(tmp,"info singular %s",strstr);
439      }
440      system(tmp);
441#ifndef MSDOS
442      //sprintf(tmp,"clear");
443      //system(tmp);
444#endif
445#endif
446    }
447  }
448}
449
450
451struct soptionStruct
452{
453  char * name;
454  int   setval;
455  int   resetval;
456};
457
458struct soptionStruct optionStruct[]=
459{
460  {"prot",         Sy_bit(OPT_PROT),           ~Sy_bit(OPT_PROT)   },
461  {"redSB",        Sy_bit(OPT_REDSB),          ~Sy_bit(OPT_REDSB)   },
462  /* 2 Gebauer/Moeller */
463  {"notSugar",     Sy_bit(OPT_NOT_SUGAR),      ~Sy_bit(OPT_NOT_SUGAR)   },
464  {"interrupt",    Sy_bit(OPT_INTERRUPT),      ~Sy_bit(OPT_INTERRUPT)   },
465  {"sugarCrit",    Sy_bit(OPT_SUGARCRIT),      ~Sy_bit(OPT_SUGARCRIT)   },
466  {"teach",     Sy_bit(OPT_DEBUG),          ~Sy_bit(OPT_DEBUG)  },
467  /* 7 cancel unit */
468  {"morePairs",    Sy_bit(OPT_MOREPAIRS),      ~Sy_bit(OPT_MOREPAIRS)   },
469  /* 9 return SB in syz, quotient, intersect */
470  {"returnSB",     Sy_bit(OPT_RETURN_SB),      ~Sy_bit(OPT_RETURN_SB)  },
471  {"fastHC",       Sy_bit(OPT_FASTHC),         ~Sy_bit(OPT_FASTHC)  },
472  /* 11-19 sort in L/T */
473  /* 20 redBest */
474  {"keepvars",     Sy_bit(OPT_KEEPVARS),       ~Sy_bit(OPT_KEEPVARS) },
475  {"staircaseBound",Sy_bit(OPT_STAIRCASEBOUND),~Sy_bit(OPT_STAIRCASEBOUND)  },
476  {"multBound",    Sy_bit(OPT_MULTBOUND),      ~Sy_bit(OPT_MULTBOUND)  },
477  {"degBound",     Sy_bit(OPT_DEGBOUND),       ~Sy_bit(OPT_DEGBOUND)  },
478  /* 25 no redTail(p)/redTail(s) */
479  {"redTail",      Sy_bit(OPT_REDTAIL),        ~Sy_bit(OPT_REDTAIL)  },
480  {"intStrategy",  Sy_bit(OPT_INTSTRATEGY),    ~Sy_bit(OPT_INTSTRATEGY)  },
481  /* 27 stop at HC (finiteDeterminacyTest) */
482  {"minRes",       Sy_bit(OPT_MINRES),         ~Sy_bit(OPT_MINRES)  },
483  /* 30: use not regularity for syz */
484  {"notRegularity",Sy_bit(OPT_NOTREGULARITY),  ~Sy_bit(OPT_NOTREGULARITY)  },
485  {"weightM",      Sy_bit(OPT_WEIGHTM),        ~Sy_bit(OPT_WEIGHTM)  },
486/*special for "none" and also end marker for showOption:*/
487  {"ne",           0,                          0 }
488};
489
490struct soptionStruct verboseStruct[]=
491{
492  {"mem",      Sy_bit(V_SHOW_MEM),  ~Sy_bit(V_SHOW_MEM)   },
493  {"yacc",     Sy_bit(V_YACC),      ~Sy_bit(V_YACC)       },
494  {"redefine", Sy_bit(V_REDEFINE),  ~Sy_bit(V_REDEFINE)   },
495  {"reading",  Sy_bit(V_READING),   ~Sy_bit(V_READING)    },
496  {"loadLib",  Sy_bit(V_LOAD_LIB),  ~Sy_bit(V_LOAD_LIB)   },
497  {"debugLib", Sy_bit(V_DEBUG_LIB), ~Sy_bit(V_DEBUG_LIB)  },
498  {"loadProc", Sy_bit(V_LOAD_PROC), ~Sy_bit(V_LOAD_PROC)  },
499  {"defRes",   Sy_bit(V_DEF_RES),   ~Sy_bit(V_DEF_RES)    },
500  {"debugMem", Sy_bit(V_DEBUG_MEM), ~Sy_bit(V_DEBUG_MEM)  },
501  {"usage",    Sy_bit(V_SHOW_USE),  ~Sy_bit(V_SHOW_USE)   },
502  {"Imap",     Sy_bit(V_IMAP),      ~Sy_bit(V_IMAP)       },
503  {"prompt",   Sy_bit(V_PROMPT),    ~Sy_bit(V_PROMPT)     },
504  {"notWarnSB",Sy_bit(V_NSB),       ~Sy_bit(V_NSB)        },
505/*special for "none" and also end marker for showOption:*/
506  {"ne",         0,          0 }
507};
508
509BOOLEAN setOption(leftv res, leftv v)
510{
511  char *n;
512  do
513  {
514    n=v->name;
515    if (n==NULL) return TRUE;
516    v->name=NULL;
517
518    int i;
519
520    if(strcmp(n,"get")==0)
521    {
522      intvec *w=new intvec(2);
523      (*w)[0]=test;
524      (*w)[1]=verbose;
525      res->rtyp=INTVEC_CMD;
526      res->data=(void *)w;
527      goto okay;
528    }
529    if(strcmp(n,"set")==0)
530    {
531      if((v->next!=NULL)
532      &&(v->next->Typ()==INTVEC_CMD))
533      {
534        v=v->next;
535        intvec *w=(intvec*)v->Data();
536        test=(*w)[0];
537        verbose=(*w)[1];
538
539        if (TEST_OPT_INTSTRATEGY && (currRing!=NULL) && (currRing->ch>=2))
540        {
541          test &=~Sy_bit(OPT_INTSTRATEGY);
542        }
543        goto okay;
544      }
545    }
546    if(strcmp(n,"none")==0)
547    {
548      test=0;
549      verbose=0;
550      goto okay;
551    }
552    for (i=0; (i==0) || (optionStruct[i-1].setval!=0); i++)
553    {
554      if (strcmp(n,optionStruct[i].name)==0)
555      {
556        if (optionStruct[i].setval & validOpts)
557          test |= optionStruct[i].setval;
558        else
559          Warn("cannot set option");
560        if (TEST_OPT_INTSTRATEGY && (currRing!=NULL) && (currRing->ch>=2))
561        {
562          test &=~Sy_bit(OPT_INTSTRATEGY);
563        }
564        goto okay;
565      }
566      else if ((strncmp(n,"no",2)==0)
567      && (strcmp(n+2,optionStruct[i].name)==0))
568      {
569        if (optionStruct[i].setval & validOpts)
570        {
571          test &= optionStruct[i].resetval;
572        }
573        else
574          Warn("cannot clear option");
575        goto okay;
576      }
577    }
578    for (i=0; (i==0) || (verboseStruct[i-1].setval!=0); i++)
579    {
580      if (strcmp(n,verboseStruct[i].name)==0)
581      {
582        verbose |= verboseStruct[i].setval;
583        #ifdef YYDEBUG
584        #if YYDEBUG
585        if (BVERBOSE(V_YACC)) yydebug=1;
586        else                  yydebug=0;
587        #endif
588        #endif
589        goto okay;
590      }
591      else if ((strncmp(n,"no",2)==0)
592      && (strcmp(n+2,verboseStruct[i].name)==0))
593      {
594        verbose &= verboseStruct[i].resetval;
595        #ifdef YYDEBUG
596        #if YYDEBUG
597        if (BVERBOSE(V_YACC)) yydebug=1;
598        else                  yydebug=0;
599        #endif
600        #endif
601        goto okay;
602      }
603    }
604    Werror("unknown option `%s`",n);
605  okay:
606    FreeL((ADDRESS)n);
607    v=v->next;
608  } while (v!=NULL);
609  return FALSE;
610}
611
612void showOption()
613{
614  int i;
615  BITSET tmp;
616
617  PrintS("//options:");
618  if ((test!=0)||(verbose!=0))
619  {
620    tmp=test;
621    if(tmp)
622    {
623      for (i=0; optionStruct[i].setval!=0; i++)
624      {
625        if (optionStruct[i].setval & test)
626        {
627          Print(" %s",optionStruct[i].name);
628          tmp &=optionStruct[i].resetval;
629        }
630      }
631      for (i=0; i<32; i++)
632      {
633        if (tmp & Sy_bit(i)) Print(" %d",i);
634      }
635    }
636    tmp=verbose;
637    if (tmp)
638    {
639      for (i=0; verboseStruct[i].setval!=0; i++)
640      {
641        if (verboseStruct[i].setval & tmp)
642        {
643          Print(" %s",verboseStruct[i].name);
644          tmp &=verboseStruct[i].resetval;
645        }
646      }
647      for (i=1; i<32; i++)
648      {
649        if (tmp & Sy_bit(i)) Print(" %d",i+32);
650      }
651    }
652    PrintLn();
653  }
654  else
655    PrintS(" none\n");
656}
657
658char * versionString()
659{
660  StringSet("\t");
661#ifdef HAVE_FACTORY
662              StringAppend("factory(%s),", factoryVersion);
663#endif
664#ifdef HAVE_LIBFAC_P
665              StringAppend("libfac(%s,%s),\n\t",libfac_version,libfac_date);
666#endif
667#ifdef HAVE_GMP
668#if defined (__GNU_MP_VERSION) && defined (__GNU_MP_VERSION_MINOR)
669              StringAppend("GMP(%d.%d),",__GNU_MP_VERSION,__GNU_MP_VERSION_MINOR);
670#elif defined (HAVE_SMALLGMP)
671              StringAppendS("SmallGMP(2.0.2.0),");
672#else
673              StringAppendS("GMP(1.3),");
674#endif
675#endif
676#ifdef HAVE_MPSR
677              StringAppend("MP(%s),\n\t",MP_VERSION);
678#endif
679#if defined(HAVE_READLINE) && !defined(FEREAD)
680              StringAppendS("libreadline,\n\t");
681#endif
682#ifdef HAVE_FEREAD
683              StringAppendS("emulated libreadline,\n");
684#endif
685#ifdef SRING
686              StringAppendS("super algebra,");
687#endif
688#ifdef DRING
689              StringAppendS("Weyl algebra,");
690#endif
691#ifdef HAVE_DBM
692              StringAppendS("DBM,");
693#endif
694#ifdef HAVE_INFO
695              StringAppendS("info,");
696#endif
697#ifdef TEST
698              StringAppendS("TESTs,");
699#endif
700#if YYDEBUG
701              StringAppendS("YYDEBUG=1,");
702#endif
703#ifdef MDEBUG
704              StringAppend("MDEBUG=%d,",MDEBUG);
705#endif
706#ifdef PDEBUG
707              StringAppend("PDEBUG,");
708#endif
709#ifdef KDEBUG
710              StringAppend("KDEBUG,");
711#endif
712#ifdef TEST_MAC_ORDER
713              StringAppendS("mac_order,");
714#endif
715#ifndef __OPTIMIZE__
716              StringAppend("-g,");
717#endif
718              StringAppend("random=%d\n",siRandomStart);
719              return StringAppend("search path %s", feGetSearchPath());
720}
Note: See TracBrowser for help on using the repository browser.