source: git/Singular/ipid.cc @ c90500

spielwiese
Last change on this file since c90500 was c90500, checked in by Zoltan Kovacs <kovzol@…>, 11 years ago
Reverting 26c8e6010d08437813f7b3dcc02d4afa785e062d (Disallow "link" command when --no-shell is set on command line) and adding suggested change from Hans Schoenemann instead
  • Property mode set to 100644
File size: 15.3 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT: identfier handling
6*/
7
8#include <string.h>
9
10#include "config.h"
11#include <kernel/mod2.h>
12#include <omalloc/omalloc.h>
13#include <Singular/tok.h>
14#include <misc/options.h>
15#include <Singular/ipshell.h>
16#include <misc/intvec.h>
17#include <kernel/febase.h>
18#include <coeffs/numbers.h>
19#include <coeffs/bigintmat.h>
20#include <kernel/longrat.h>
21#include <kernel/polys.h>
22#include <polys/monomials/ring.h>
23#include <kernel/ideals.h>
24#include <polys/matpol.h>
25#include <Singular/lists.h>
26#include <Singular/attrib.h>
27#include <Singular/silink.h>
28#include <kernel/syz.h>
29#include <Singular/ipid.h>
30#include <Singular/blackbox.h>
31
32#ifdef HAVE_DYNAMIC_LOADING
33#include <polys/mod_raw.h>
34#endif /* HAVE_DYNAMIC_LOADING */
35
36omBin sip_command_bin = omGetSpecBin(sizeof(sip_command));
37omBin sip_package_bin = omGetSpecBin(sizeof(sip_package));
38//omBin ip_package_bin = omGetSpecBin(sizeof(ip_package));
39omBin idrec_bin = omGetSpecBin(sizeof(idrec));
40
41coeffs coeffs_BIGINT;
42
43FILE   *feFilePending; /*temp. storage for grammar.y */
44
45proclevel *procstack=NULL;
46#define TEST
47//idhdl idroot = NULL;
48
49idhdl currPackHdl = NULL;
50idhdl basePackHdl = NULL;
51package currPack =NULL;
52package basePack =NULL;
53idhdl currRingHdl = NULL;
54const char* iiNoName="_";
55
56void paCleanUp(package pack);
57
58/*0 implementation*/
59
60int iiS2I(const char *s)
61{
62  int i;
63  i=s[0];
64  if (s[1]!='\0')
65  {
66    i=(i<<8)+s[1];
67    if (s[2]!='\0')
68    {
69      i=(i<<8)+s[2];
70      if (s[3]!='\0')
71      {
72        i=(i<<8)+s[3];
73      }
74    }
75  }
76  return i;
77}
78
79idhdl idrec::get(const char * s, int level)
80{
81  assume(s!=NULL);
82  assume((level>=0) && (level<=1000)); //not really, but if it isnt in that bounds..
83  idhdl h = this;
84  idhdl found=NULL;
85  int l;
86  const char *id_;
87  int i=iiS2I(s);
88  int less4=(i < (1<<24));
89  while (h!=NULL)
90  {
91    omCheckAddr((ADDRESS)IDID(h));
92    l=IDLEV(h);
93    if ((l==0)||(l==level))
94    {
95      if (i==h->id_i)
96      {
97        id_=IDID(h);
98        if (less4 || (0 == strcmp(s+4,id_+4)))
99        {
100          if (l==level) return h;
101          found=h;
102        }
103      }
104    }
105    h = IDNEXT(h);
106  }
107  return found;
108}
109
110//idrec::~idrec()
111//{
112//  if (id!=NULL)
113//  {
114//    omFree((ADDRESS)id);
115//    id=NULL;
116//  }
117//  /* much more !! */
118//}
119
120void *idrecDataInit(int t)
121{
122  switch (t)
123  {
124    //the type with init routines:
125    case INTVEC_CMD:
126    case INTMAT_CMD:
127      return (void *)new intvec();
128    case BIGINTMAT_CMD:
129      return (void *)new bigintmat();
130    case NUMBER_CMD:
131      return (void *) nInit(0);
132    case BIGINT_CMD:
133      return (void *) n_Init(0, coeffs_BIGINT);
134    case IDEAL_CMD:
135    case MODUL_CMD:
136    case MATRIX_CMD:
137      return (void*) idInit(1,1);
138    case MAP_CMD:
139    {
140      map m = (map)idInit(1,1);
141      m->preimage = omStrDup(IDID(currRingHdl));
142      return (void *)m;
143    }
144    case STRING_CMD:
145      return (void *)omAlloc0(1);
146    case LIST_CMD:
147    {
148      lists l=(lists)omAllocBin(slists_bin);
149      l->Init();
150      return (void*)l;
151    }
152    //the types with the standard init: set the struct to zero
153    case LINK_CMD:
154      return (void*) omAlloc0Bin(sip_link_bin);
155    case RING_CMD:
156      return (void*) omAlloc0Bin(sip_sring_bin);
157    case PACKAGE_CMD:
158      return (void*) omAlloc0Bin(sip_package_bin);
159    case PROC_CMD:
160      return (void *) omAlloc0Bin(procinfo_bin);
161    case RESOLUTION_CMD:
162      return  (void *)omAlloc0(sizeof(ssyStrategy));
163    //other types: without init (int,script,poly,def,package)
164    case INT_CMD:
165    case DEF_CMD:
166    case POLY_CMD:
167    case VECTOR_CMD:
168    case QRING_CMD:
169       return (void*)0L;
170    default:
171      {
172        if (t>MAX_TOK)
173        {
174#ifdef BLACKBOX_DEVEL
175          Print("bb-type %d\n",t);
176#endif
177          blackbox *bb=getBlackboxStuff(t);
178          if (bb!=NULL)
179             return (void *)bb->blackbox_Init(bb);
180        }
181        else
182          Werror("unknown type %d",t);
183        break;
184      }
185  }
186  return (void *)0L;
187}
188idhdl idrec::set(const char * s, int level, int t, BOOLEAN init)
189{
190  //printf("define %s, %x, level: %d, typ: %d\n", s,s,level,t);
191  idhdl h = (idrec *)omAlloc0Bin(idrec_bin);
192  IDID(h)   = s;
193  IDTYP(h)  = t;
194  IDLEV(h)  = level;
195  IDNEXT(h) = this;
196  h->id_i=iiS2I(s);
197  if (init)
198  {
199    if ((t==IDEAL_CMD)||(t==MODUL_CMD))
200      IDFLAG(h) = Sy_bit(FLAG_STD);
201    IDSTRING(h)=(char *)idrecDataInit(t);
202    // additional settings:--------------------------------------
203#if 0
204    // this leads to a memory leak
205    if (t == QRING_CMD)
206    {
207      // IDRING(h)=rCopy(currRing);
208      /* QRING_CMD is ring dep => currRing !=NULL */
209    }
210    else
211#endif
212    if (t == PROC_CMD)
213    {
214      IDPROC(h)->language=LANG_NONE;
215    }
216    else if (t == PACKAGE_CMD)
217    {
218      IDPACKAGE(h)->language=LANG_NONE;
219      IDPACKAGE(h)->loaded = FALSE;
220    }
221  }
222  // --------------------------------------------------------
223  return  h;
224}
225
226char * idrec::String(BOOLEAN typed)
227{
228  sleftv tmp;
229  memset(&tmp,0,sizeof(sleftv));
230  tmp.rtyp=IDTYP(this);
231  tmp.data=IDDATA(this);
232  tmp.name=IDID(this);
233  return tmp.String(NULL, typed);
234}
235
236idhdl enterid(const char * s, int lev, int t, idhdl* root, BOOLEAN init, BOOLEAN search)
237{
238  if (s==NULL) return NULL;
239  idhdl h;
240  s=omStrDup(s);
241  idhdl *save_root=root;
242  if (t==PACKAGE_CMD)
243  {
244    if (root!=&(basePack->idroot))
245    {
246      root=&(basePack->idroot);
247    }
248  }
249  // is it already defined in root ?
250  if ((h=(*root)->get(s,lev))!=NULL)
251  {
252    if (IDLEV(h)==lev)
253    {
254      if ((IDTYP(h) == t)||(t==DEF_CMD))
255      {
256        if ((IDTYP(h)==PACKAGE_CMD)
257        && (strcmp(s,"Top")==0))
258        {
259          goto errlabel;
260        }
261        if (BVERBOSE(V_REDEFINE))
262          Warn("redefining %s **",s);
263        if (s==IDID(h)) IDID(h)=NULL;
264        killhdl2(h,root,currRing);
265      }
266      else
267        goto errlabel;
268    }
269  }
270  // is it already defined in currRing->idroot ?
271  else if (search && (currRing!=NULL)&&((*root) != currRing->idroot))
272  {
273    if ((h=currRing->idroot->get(s,lev))!=NULL)
274    {
275      if (IDLEV(h)==lev)
276      {
277        if ((IDTYP(h) == t)||(t==DEF_CMD))
278        {
279          if (BVERBOSE(V_REDEFINE))
280            Warn("redefining %s **",s);
281          IDID(h)=NULL;
282          killhdl2(h,&currRing->idroot,currRing);
283        }
284        else
285          goto errlabel;
286      }
287    }
288  }
289  // is it already defined in idroot ?
290  else if (search && (*root != IDROOT))
291  {
292    if ((h=IDROOT->get(s,lev))!=NULL)
293    {
294      if (IDLEV(h)==lev)
295      {
296        if ((IDTYP(h) == t)||(t==DEF_CMD))
297        {
298          if (BVERBOSE(V_REDEFINE))
299            Warn("redefining `%s` **",s);
300          if (s==IDID(h)) IDID(h)=NULL;
301          killhdl2(h,&IDROOT,NULL);
302        }
303        else
304          goto errlabel;
305      }
306    }
307  }
308  *root = (*root)->set(s, lev, t, init);
309#ifndef NDEBUG
310  checkall();
311#endif
312  return *root;
313
314  errlabel:
315    //Werror("identifier `%s` in use(lev h=%d,typ=%d,t=%d, curr=%d)",s,IDLEV(h),IDTYP(h),t,lev);
316    Werror("identifier `%s` in use",s);
317    //listall();
318    omFree((ADDRESS)s);
319    return NULL;
320}
321void killid(const char * id, idhdl * ih)
322{
323  if (id!=NULL)
324  {
325    idhdl h = (*ih)->get(id,myynest);
326
327    // id not found in global list, is it defined in current ring ?
328    if (h==NULL)
329    {
330      if ((currRing!=NULL) && (*ih != (currRing->idroot)))
331      {
332        h = currRing->idroot->get(id,myynest);
333        if (h!=NULL)
334        {
335          killhdl2(h,&(currRing->idroot),currRing);
336          return;
337        }
338      }
339      Werror("`%s` is not defined",id);
340      return;
341    }
342    killhdl2(h,ih,currRing);
343  }
344  else
345    Werror("kill what ?");
346}
347
348void killhdl(idhdl h, package proot)
349{
350  int t=IDTYP(h);
351  if (((BEGIN_RING<t) && (t<END_RING) && (t!=QRING_CMD))
352  || ((t==LIST_CMD) && (lRingDependend((lists)IDDATA(h)))))
353    killhdl2(h,&currRing->idroot,currRing);
354  else
355  {
356    if(t==PACKAGE_CMD)
357    {
358      killhdl2(h,&(basePack->idroot),NULL);
359    }
360    else
361    {
362      idhdl s=proot->idroot;
363      while ((s!=h) && (s!=NULL)) s=s->next;
364      if (s!=NULL)
365        killhdl2(h,&(proot->idroot),NULL);
366      else if (basePack!=proot)
367      {
368        idhdl s=basePack->idroot;
369        while ((s!=h) && (s!=NULL)) s=s->next;
370        if (s!=NULL)
371          killhdl2(h,&(basePack->idroot),currRing);
372        else
373          killhdl2(h,&(currRing->idroot),currRing);
374       }
375    }
376  }
377}
378
379void killhdl2(idhdl h, idhdl * ih, ring r)
380{
381  //printf("kill %s, id %x, typ %d lev: %d\n",IDID(h),(int)IDID(h),IDTYP(h),IDLEV(h));
382  idhdl hh;
383
384  if (h->attribute!=NULL)
385  {
386    //h->attribute->killAll(r); MEMORY LEAK!
387    h->attribute=NULL;
388  }
389  if (IDTYP(h) == PACKAGE_CMD)
390  {
391    if (strcmp(IDID(h),"Top")==0)
392    {
393      WarnS("can not kill `Top`");
394      return;
395    }
396    // any objects defined for this package ?
397    if ((IDPACKAGE(h)->ref<=0)  &&  (IDPACKAGE(h)->idroot!=NULL))
398    {
399      if (currPack==IDPACKAGE(h))
400      {
401        currPack=basePack;
402        currPackHdl=NULL;
403      }
404      idhdl * hd = &IDRING(h)->idroot;
405      idhdl  hdh = IDNEXT(*hd);
406      idhdl  temp;
407      while (hdh!=NULL)
408      {
409        temp = IDNEXT(hdh);
410        killhdl2(hdh,&(IDPACKAGE(h)->idroot),NULL);
411        hdh = temp;
412      }
413      killhdl2(*hd,hd,NULL);
414      if (IDPACKAGE(h)->libname!=NULL) omFree((ADDRESS)(IDPACKAGE(h)->libname));
415    }
416    paKill(IDPACKAGE(h));
417    if (currPackHdl==h) currPackHdl=packFindHdl(currPack);
418    iiCheckPack(currPack);
419  }
420  else if ((IDTYP(h)==RING_CMD)||(IDTYP(h)==QRING_CMD))
421    rKill(h);
422  else
423    s_internalDelete(IDTYP(h),IDDATA(h),r);
424  //  general  -------------------------------------------------------------
425  // now dechain it and delete idrec
426  if (IDID(h)!=NULL) // OB: ?????
427    omFree((ADDRESS)IDID(h));
428  IDID(h)=NULL;
429  IDDATA(h)=NULL;
430  if (h == (*ih))
431  {
432    // h is at the beginning of the list
433    *ih = IDNEXT(h) /* ==*ih */;
434  }
435  else if (ih!=NULL)
436  {
437    // h is somethere in the list:
438    hh = *ih;
439    loop
440    {
441      if (hh==NULL)
442      {
443        PrintS(">>?<< not found for kill\n");
444        return;
445      }
446      idhdl hhh = IDNEXT(hh);
447      if (hhh == h)
448      {
449        IDNEXT(hh) = IDNEXT(hhh);
450        break;
451      }
452      hh = hhh;
453    }
454  }
455  omFreeBin((ADDRESS)h, idrec_bin);
456}
457
458idhdl ggetid(const char *n, BOOLEAN local, idhdl *packhdl)
459{
460  idhdl h = IDROOT->get(n,myynest);
461  idhdl h2=NULL;
462  *packhdl = NULL;
463  if ((currRing!=NULL) && ((h==NULL)||(IDLEV(h)!=myynest)))
464  {
465    h2 = currRing->idroot->get(n,myynest);
466  }
467  if (h2==NULL) return h;
468  return h2;
469}
470
471idhdl ggetid(const char *n)
472{
473  idhdl h = IDROOT->get(n,myynest);
474  if ((h!=NULL)&&(IDLEV(h)==myynest)) return h;
475  idhdl h2=NULL;
476  if (currRing!=NULL)
477  {
478    h2 = currRing->idroot->get(n,myynest);
479  }
480  if (h2!=NULL) return h2;
481  if (h!=NULL) return h;
482  if (basePack!=currPack)
483    return basePack->idroot->get(n,myynest);
484  return NULL;
485}
486
487void ipListFlag(idhdl h)
488{
489  if (hasFlag(h,FLAG_STD)) PrintS(" (SB)");
490#ifdef HAVE_PLURAL
491  if (hasFlag(h,FLAG_TWOSTD)) PrintS(" (2SB)");
492#endif
493}
494
495lists ipNameList(idhdl root)
496{
497  idhdl h=root;
498  /* compute the length */
499  int l=0;
500  while (h!=NULL) { l++; h=IDNEXT(h); }
501  /* allocate list */
502  lists L=(lists)omAllocBin(slists_bin);
503  L->Init(l);
504  /* copy names */
505  h=root;
506  l=0;
507  while (h!=NULL)
508  {
509    /* list is initialized with 0 => no need to clear anything */
510    L->m[l].rtyp=STRING_CMD;
511    L->m[l].data=omStrDup(IDID(h));
512    l++;
513    h=IDNEXT(h);
514  }
515  return L;
516}
517
518/*
519* move 'tomove' from root1 list to root2 list
520*/
521static int ipSwapId(idhdl tomove, idhdl &root1, idhdl &root2)
522{
523  idhdl h;
524  /* search 'tomove' in root2 : if found -> do nothing */
525  h=root2;
526  while ((h!=NULL) && (h!=tomove)) h=IDNEXT(h);
527  if (h!=NULL) return FALSE; /*okay */
528  /* search predecessor of h in root1, remove 'tomove' */
529  h=root1;
530  if (tomove==h)
531  {
532    root1=IDNEXT(h);
533  }
534  else
535  {
536    while ((h!=NULL) && (IDNEXT(h)!=tomove)) h=IDNEXT(h);
537    if (h==NULL) return TRUE; /* not in the list root1 -> do nothing */
538    IDNEXT(h)=IDNEXT(tomove);
539  }
540  /* add to root2 list */
541  IDNEXT(tomove)=root2;
542  root2=tomove;
543  return FALSE;
544}
545
546void  ipMoveId(idhdl tomove)
547{
548  if ((currRing!=NULL)&&(tomove!=NULL))
549  {
550    if (((QRING_CMD!=IDTYP(tomove)) && RingDependend(IDTYP(tomove)))
551    || ((IDTYP(tomove)==LIST_CMD) && (lRingDependend(IDLIST(tomove)))))
552    {
553      /*move 'tomove' to ring id's*/
554      if (ipSwapId(tomove,IDROOT,currRing->idroot))
555      ipSwapId(tomove,basePack->idroot,currRing->idroot);
556    }
557    else
558    {
559      /*move 'tomove' to global id's*/
560      ipSwapId(tomove,currRing->idroot,IDROOT);
561    }
562  }
563}
564
565const char * piProcinfo(procinfov pi, const char *request)
566{
567  if(pi == NULL) return "empty proc";
568  else if (strcmp(request, "libname")  == 0) return pi->libname;
569  else if (strcmp(request, "procname") == 0) return pi->procname;
570  else if (strcmp(request, "type")     == 0)
571  {
572    switch (pi->language)
573    {
574      case LANG_SINGULAR: return "singular"; break;
575      case LANG_C:        return "object";   break;
576      case LANG_NONE:     return "none";     break;
577      default:            return "unknow language";
578    }
579  }
580  else if (strcmp(request, "ref")      == 0)
581  {
582    char p[8];
583    sprintf(p, "%d", pi->ref);
584    return omStrDup(p);  // MEMORY-LEAK
585  }
586  return "??";
587}
588
589void piCleanUp(procinfov pi)
590{
591  (pi->ref)--;
592  if (pi->ref <= 0)
593  {
594    if (pi->libname != NULL) // OB: ????
595      omFree((ADDRESS)pi->libname);
596    if (pi->procname != NULL) // OB: ????
597      omFree((ADDRESS)pi->procname);
598
599    if( pi->language == LANG_SINGULAR)
600    {
601      if (pi->data.s.body != NULL) // OB: ????
602        omFree((ADDRESS)pi->data.s.body);
603    }
604    if( pi->language == LANG_C)
605    {
606    }
607    memset((void *) pi, 0, sizeof(procinfo));
608    pi->language=LANG_NONE;
609  }
610}
611
612BOOLEAN piKill(procinfov pi)
613{
614  Voice *p=currentVoice;
615  while (p!=NULL)
616  {
617    if (p->pi==pi && pi->ref <= 1)
618    {
619      Warn("`%s` in use, can not be killed",pi->procname);
620      return TRUE;
621    }
622    p=p->next;
623  }
624  piCleanUp(pi);
625  if (pi->ref <= 0)
626    omFreeBin((ADDRESS)pi,  procinfo_bin);
627  return FALSE;
628}
629
630void paCleanUp(package pack)
631{
632  (pack->ref)--;
633  if (pack->ref < 0)
634  {
635#ifndef HAVE_STATIC
636    if( pack->language == LANG_C)
637    {
638      Print("//dlclose(%s)\n",pack->libname);
639#ifdef HAVE_DYNAMIC_LOADING
640      dynl_close (pack->handle);
641#endif /* HAVE_DYNAMIC_LOADING */
642    }
643#endif /* HAVE_STATIC */
644    omfree((ADDRESS)pack->libname);
645    memset((void *) pack, 0, sizeof(sip_package));
646    pack->language=LANG_NONE;
647  }
648}
649
650void proclevel::push(char *n)
651{
652  //Print("push %s\n",n);
653  proclevel *p=(proclevel*)omAlloc0(sizeof(proclevel));
654  p->cRing=currRing;
655  p->cRingHdl=currRingHdl;
656  p->name=n;
657  p->cPackHdl=currPackHdl;
658  p->cPack=currPack;
659  p->next=this;
660  procstack=p;
661}
662void proclevel::pop()
663{
664  //Print("pop %s\n",name);
665  //if (currRing!=::currRing) PrintS("currRing wrong\n");;
666  //::currRing=this->currRing;
667  //if (r==NULL) Print("set ring to NULL at lev %d(%s)\n",myynest,name);
668  //::currRingHdl=this->currRingHdl;
669  //if((::currRingHdl==NULL)||(IDRING(::currRingHdl)!=(::currRing)))
670  //  ::currRingHdl=rFindHdl(::currRing,NULL,NULL);
671  //Print("restore pack=%s,1.obj=%s\n",IDID(currPackHdl),IDID(currPack->idroot));
672  currPackHdl=this->cPackHdl;
673  currPack=this->cPack;
674  iiCheckPack(currPack);
675  proclevel *p=this;
676  procstack=next;
677  omFreeSize(p,sizeof(proclevel));
678}
679
680idhdl packFindHdl(package r)
681{
682  idhdl h=basePack->idroot;
683  while (h!=NULL)
684  {
685    if ((IDTYP(h)==PACKAGE_CMD)
686        && (IDPACKAGE(h)==r))
687      return h;
688    h=IDNEXT(h);
689  }
690  return NULL;
691}
Note: See TracBrowser for help on using the repository browser.