source: git/Singular/newstruct.cc @ 69d7df

spielwiese
Last change on this file since 69d7df was 69d7df, checked in by Hans Schoenemann <hannes@…>, 10 years ago
fix: handling of potential ring-dep objects (def,list) in newstruct
  • Property mode set to 100644
File size: 19.4 KB
Line 
1#include <ctype.h>
2
3#ifdef HAVE_CONFIG_H
4#include "singularconfig.h"
5#endif /* HAVE_CONFIG_H */
6#include <kernel/mod2.h>
7#include <Singular/ipid.h>
8#include <Singular/blackbox.h>
9#include <Singular/lists.h>
10#include <Singular/ipid.h>
11#include <Singular/ipshell.h>
12#include <Singular/newstruct.h>
13
14struct newstruct_member_s;
15typedef struct newstruct_member_s *newstruct_member;
16struct  newstruct_member_s
17{
18  newstruct_member next;
19  char *         name;
20  int            typ;
21  int            pos;
22};
23
24struct newstruct_proc_s;
25typedef struct newstruct_proc_a *newstruct_proc;
26struct  newstruct_proc_a
27{
28  newstruct_proc next;
29  int            t; /*tok id */
30  int            args; /* number of args */
31  procinfov      p;
32};
33
34struct newstruct_desc_s
35{
36  newstruct_member member;
37  newstruct_desc   parent;
38  newstruct_proc   procs;
39  int            size; // number of mebers +1
40  int            id;   // the type id assigned to this bb
41};
42
43int newstruct_desc_size()
44{
45  return sizeof(newstruct_desc_s);
46}
47
48char * newstruct_String(blackbox *b, void *d)
49{
50  if (d==NULL) return omStrDup("oo");
51  else
52  {
53    newstruct_desc ad=(newstruct_desc)(b->data);
54
55    newstruct_proc p=ad->procs;
56    while((p!=NULL)&&(p->t!=STRING_CMD))
57      p=p->next;
58
59    if (p!=NULL)
60    {
61      BOOLEAN sl;
62      sleftv tmp;
63      memset(&tmp,0,sizeof(tmp));
64      tmp.rtyp=ad->id;
65      void * newstruct_Copy(blackbox*, void *); //forward declaration
66      tmp.data=(void*)newstruct_Copy(b,d);
67      idrec hh;
68      memset(&hh,0,sizeof(hh));
69      hh.id=Tok2Cmdname(p->t);
70      hh.typ=PROC_CMD;
71      hh.data.pinf=p->p;
72      sl=iiMake_proc(&hh,NULL,&tmp);
73
74      if ((!sl)&& (iiRETURNEXPR.Typ() == STRING_CMD))
75      {
76        char *res = omStrDup((char*)iiRETURNEXPR.CopyD());
77        iiRETURNEXPR.CleanUp();
78        iiRETURNEXPR.Init();
79        return res;
80      }
81      iiRETURNEXPR.CleanUp();
82      iiRETURNEXPR.Init();
83    }
84
85    lists l=(lists)d;
86    newstruct_member a=ad->member;
87    StringSetS("");
88    loop
89    {
90      StringAppendS(a->name);
91      StringAppendS("=");
92      if ((!RingDependend(a->typ))
93      || ((l->m[a->pos-1].data==(void *)currRing)
94         && (currRing!=NULL)))
95      {
96        if (l->m[a->pos].rtyp==LIST_CMD)
97        {
98          StringAppendS("<list>");
99        }
100        else
101        {
102          char *tmp2=omStrDup(l->m[a->pos].String());
103          if ((strlen(tmp2)>80)||(strchr(tmp2,'\n')!=NULL))
104          {
105            StringAppendS("<");
106            StringAppendS(Tok2Cmdname(l->m[a->pos].rtyp));
107            StringAppendS(">");
108          }
109          else StringAppendS(tmp2);
110          omFree(tmp2);
111        }
112      }
113      else StringAppendS("??");
114      if (a->next==NULL) break;
115      StringAppendS("\n");
116      if(errorreported) break;
117      a=a->next;
118    }
119    return StringEndS();
120  }
121}
122lists lCopy_newstruct(lists L)
123{
124  lists N=(lists)omAlloc0Bin(slists_bin);
125  int n=L->nr;
126  ring save_ring=currRing;
127  N->Init(n+1);
128  for(;n>=0;n--)
129  {
130    if (RingDependend(L->m[n].rtyp)
131    ||((L->m[n].rtyp==LIST_CMD)&&lRingDependend((lists)L->m[n].data)))
132    {
133      assume((L->m[n-1].rtyp==RING_CMD) || (L->m[n-1].data==NULL));
134      if(L->m[n-1].data!=NULL)
135      {
136        if (L->m[n-1].data!=(void*)currRing)
137          rChangeCurrRing((ring)(L->m[n-1].data));
138        N->m[n].Copy(&L->m[n]);
139      }
140      else
141      {
142        N->m[n].rtyp=L->m[n].rtyp;
143        N->m[n].data=idrecDataInit(L->m[n].rtyp);
144      }
145    }
146    else if(L->m[n].rtyp==LIST_CMD)
147    {
148      N->m[n].rtyp=L->m[n].rtyp;
149      N->m[n].data=(void *)lCopy((lists)(L->m[n].data));
150    }
151    else if(L->m[n].rtyp>MAX_TOK)
152    {
153      N->m[n].rtyp=L->m[n].rtyp;
154      blackbox *b=getBlackboxStuff(N->m[n].rtyp);
155      N->m[n].data=(void *)b->blackbox_Copy(b,L->m[n].data);
156    }
157    else
158      N->m[n].Copy(&L->m[n]);
159  }
160  if (currRing!=save_ring) rChangeCurrRing(save_ring);
161  return N;
162}
163void * newstruct_Copy(blackbox*, void *d)
164{
165  lists n1=(lists)d;
166  return (void*)lCopy_newstruct(n1);
167}
168
169// Used by newstruct_Assign for overloaded '='
170BOOLEAN newstruct_equal(int op, leftv l, leftv r)
171{
172  blackbox *ll=getBlackboxStuff(op);
173  assume(ll->data != NULL);
174  newstruct_desc nt=(newstruct_desc)ll->data;
175  newstruct_proc p=nt->procs;
176
177  while( (p!=NULL) && ((p->t!='=')||(p->args!=1)) ) p=p->next;
178
179  if (p!=NULL)
180  {
181    BOOLEAN sl;
182    idrec hh;
183    memset(&hh,0,sizeof(hh));
184    hh.id=Tok2Cmdname(p->t);
185    hh.typ=PROC_CMD;
186    hh.data.pinf=p->p;
187    sleftv tmp;
188    memset(&tmp,0,sizeof(sleftv));
189    tmp.Copy(r);
190    sl = iiMake_proc(&hh, NULL, &tmp);
191    if (!sl)
192    {
193      if (iiRETURNEXPR.Typ() == op)
194      {
195        l->Copy(&iiRETURNEXPR);
196        iiRETURNEXPR.Init();
197        return FALSE;
198      }
199      iiRETURNEXPR.CleanUp();
200      iiRETURNEXPR.Init();
201    }
202  }
203  return TRUE;
204}
205
206void lClean_newstruct(lists l)
207{
208  if (l->nr>=0)
209  {
210    int i;
211    ring r=NULL;
212    for(i=l->nr;i>=0;i--)
213    {
214      if ((i>0) && (l->m[i-1].rtyp==RING_CMD))
215        r=(ring)(l->m[i-1].data);
216      else
217        r=NULL;
218      l->m[i].CleanUp(r);
219    }
220    omFreeSize((ADDRESS)l->m, (l->nr+1)*sizeof(sleftv));
221    l->nr=-1;
222  }
223  omFreeBin((ADDRESS)l,slists_bin);
224}
225
226BOOLEAN newstruct_Assign(leftv l, leftv r)
227{
228  if (r->Typ()>MAX_TOK)
229  {
230    blackbox *rr=getBlackboxStuff(r->Typ());
231    if (l->Typ()!=r->Typ())
232    {
233      newstruct_desc rrn=(newstruct_desc)rr->data;
234
235      if (!rrn)
236      {
237        Werror("custom type %s(%d) cannot be assigned to newstruct %s(%d)",
238               Tok2Cmdname(r->Typ()), r->Typ(), Tok2Cmdname(l->Typ()), l->Typ());
239        return TRUE;
240      }
241
242      newstruct_desc rrp=rrn->parent;
243      while ((rrp!=NULL)&&(rrp->id!=l->Typ())) rrp=rrp->parent;
244      if (rrp!=NULL)
245      {
246        if (l->rtyp==IDHDL)
247        {
248          IDTYP((idhdl)l->data)=r->Typ();
249        }
250        else
251        {
252          l->rtyp=r->Typ();
253        }
254      }
255      else                      // unrelated types - look for custom conversion
256      {
257        sleftv tmp;
258        BOOLEAN newstruct_Op1(int, leftv, leftv);  // forward declaration
259        if (! newstruct_Op1(l->Typ(), &tmp, r))  return newstruct_Assign(l, &tmp);
260      }
261    }
262    if (l->Typ()==r->Typ())
263    {
264      if (l->Data()!=NULL)
265      {
266        lists n1=(lists)l->Data();
267        lClean_newstruct(n1);
268      }
269      lists n2=(lists)r->Data();
270      n2=lCopy_newstruct(n2);
271      if (l->rtyp==IDHDL)
272      {
273        IDDATA((idhdl)l->data)=(char *)n2;
274      }
275      else
276      {
277        l->data=(void *)n2;
278      }
279      return FALSE;
280    }
281  }
282
283  else
284  {
285    assume(l->Typ() > MAX_TOK);
286    sleftv tmp;
287    if(!newstruct_equal(l->Typ(), &tmp, r)) return newstruct_Assign(l, &tmp);
288  }
289  Werror("assign %s(%d) = %s(%d)",
290        Tok2Cmdname(l->Typ()),l->Typ(),Tok2Cmdname(r->Typ()),r->Typ());
291  return TRUE;
292}
293
294BOOLEAN newstruct_Op1(int op, leftv res, leftv arg)
295{
296  // interpreter: arg is newstruct
297  blackbox *a=getBlackboxStuff(arg->Typ());
298  newstruct_desc nt=(newstruct_desc)a->data;
299  newstruct_proc p=nt->procs;
300
301  while((p!=NULL) &&( (p->t!=op) || (p->args!=1) )) p=p->next;
302
303  if (p!=NULL)
304  {
305    BOOLEAN sl;
306    sleftv tmp;
307    memset(&tmp,0,sizeof(sleftv));
308    tmp.Copy(arg);
309    idrec hh;
310    memset(&hh,0,sizeof(hh));
311    hh.id=Tok2Cmdname(p->t);
312    hh.typ=PROC_CMD;
313    hh.data.pinf=p->p;
314    sl=iiMake_proc(&hh,NULL,&tmp);
315    if (sl) return TRUE;
316    else
317    {
318      res->Copy(&iiRETURNEXPR);
319      iiRETURNEXPR.Init();
320      return FALSE;
321    }
322  }
323  return blackboxDefaultOp1(op,res,arg);
324}
325
326
327
328BOOLEAN newstruct_Op2(int op, leftv res, leftv a1, leftv a2)
329{
330  // interpreter: a1 or a2 is newstruct
331  blackbox *a=getBlackboxStuff(a1->Typ());
332  newstruct_desc nt;
333  lists al=(lists)a1->Data();
334  if (a!=NULL)
335  {
336    nt=(newstruct_desc)a->data;
337    switch(op)
338    {
339      case '.':
340      {
341        if (a2->name!=NULL)
342        {
343          BOOLEAN search_ring=FALSE;
344          newstruct_member nm=nt->member;
345          while ((nm!=NULL)&&(strcmp(nm->name,a2->name)!=0)) nm=nm->next;
346          if ((nm==NULL) && (strncmp(a2->name,"r_",2)==0))
347          {
348            nm=nt->member;
349            while ((nm!=NULL)&&(strcmp(nm->name,a2->name+2)!=0)) nm=nm->next;
350            if ((nm!=NULL)&&(RingDependend(nm->typ)))
351              search_ring=TRUE;
352            else
353              nm=NULL;
354          }
355          if (nm==NULL)
356          {
357            Werror("member %s not found", a2->name);
358            return TRUE;
359          }
360          if (search_ring)
361          {
362            ring r;
363            res->rtyp=RING_CMD;
364            res->data=al->m[nm->pos-1].data;
365            r=(ring)res->data;
366            if (r==NULL) { res->data=(void *)currRing; r=currRing; }
367            if (r!=NULL) r->ref++;
368            else Werror("ring of this member is not set and no basering found");
369            return r==NULL;
370          }
371          else if (RingDependend(nm->typ)
372          || (al->m[nm->pos].RingDependend()))
373          {
374            if (al->m[nm->pos].data==NULL)
375            {
376              // NULL belongs to any ring
377              ring r=(ring)al->m[nm->pos-1].data;
378              if (r!=NULL)
379              {
380                r->ref--;
381                al->m[nm->pos-1].data=NULL;
382                al->m[nm->pos-1].rtyp=DEF_CMD;
383              }
384            }
385            else
386            {
387              //Print("checking ring at pos %d for dat at pos %d\n",nm->pos-1,nm->pos);
388              if ((al->m[nm->pos-1].data!=(void *)currRing)
389              &&(al->m[nm->pos-1].data!=(void*)0L))
390              {
391                Werror("different ring %lx(data) - %lx(basering)",
392                  (long unsigned)(al->m[nm->pos-1].data),(long unsigned)currRing);
393                return TRUE;
394              }
395            }
396            if ((currRing!=NULL)&&(al->m[nm->pos-1].data==NULL))
397            {
398              // remember the ring, if not already set
399              al->m[nm->pos-1].data=(void *)currRing;
400              al->m[nm->pos-1].rtyp=RING_CMD;
401              currRing->ref++;
402            }
403          }
404          else if ((nm->typ==DEF_CMD)||(nm->typ==LIST_CMD))
405          {
406            if (al->m[nm->pos-1].data==NULL)
407            {
408              al->m[nm->pos-1].data=(void*)currRing;
409              if (currRing!=NULL) currRing->ref++;
410            }
411          }
412          Subexpr r=(Subexpr)omAlloc0Bin(sSubexpr_bin);
413          r->start = nm->pos+1;
414          memcpy(res,a1,sizeof(sleftv));
415          memset(a1,0,sizeof(sleftv));
416          if (res->e==NULL) res->e=r;
417          else
418          {
419            Subexpr sh=res->e;
420            while (sh->next != NULL) sh=sh->next;
421            sh->next=r;
422          }
423          return FALSE;
424        }
425        else
426        {
427          WerrorS("name expected");
428          return TRUE;
429        }
430      }
431    }
432  }
433  else
434  {
435    a=getBlackboxStuff(a2->Typ());
436    nt=(newstruct_desc)a->data;
437    al=(lists)a2->Data();
438  }
439  newstruct_proc p=nt->procs;
440  while((p!=NULL) && ( (p->t!=op) || (p->args!=2) )) p=p->next;
441  if (p!=NULL)
442  {
443    BOOLEAN sl;
444    sleftv tmp;
445    memset(&tmp,0,sizeof(sleftv));
446    tmp.Copy(a1);
447    tmp.next=(leftv)omAlloc0(sizeof(sleftv));
448    tmp.next->Copy(a2);
449    idrec hh;
450    memset(&hh,0,sizeof(hh));
451    hh.id=Tok2Cmdname(p->t);
452    hh.typ=PROC_CMD;
453    hh.data.pinf=p->p;
454    sl=iiMake_proc(&hh,NULL,&tmp);
455    if (sl) return TRUE;
456    else
457    {
458      res->Copy(&iiRETURNEXPR);
459      iiRETURNEXPR.Init();
460      return FALSE;
461    }
462  }
463  return blackboxDefaultOp2(op,res,a1,a2);
464}
465
466// BOOLEAN opM(int op, leftv res, leftv args)
467BOOLEAN newstruct_OpM(int op, leftv res, leftv args)
468{
469  // interpreter: args->1. arg is newstruct
470  blackbox *a=getBlackboxStuff(args->Typ());
471  newstruct_desc nt=(newstruct_desc)a->data;
472  switch(op)
473  {
474    case STRING_CMD:
475    {
476      res->data=(void *)a->blackbox_String(a,args->Data());
477      res->rtyp=STRING_CMD;
478      return FALSE;
479    }
480    default:
481      break;
482  }
483  newstruct_proc p=nt->procs;
484
485  while((p!=NULL) &&( (p->t!=op) || (p->args!=4) )) p=p->next;
486
487  if (p!=NULL)
488  {
489    BOOLEAN sl;
490    sleftv tmp;
491    memset(&tmp,0,sizeof(sleftv));
492    tmp.Copy(args);
493    idrec hh;
494    memset(&hh,0,sizeof(hh));
495    hh.id=Tok2Cmdname(p->t);
496    hh.typ=PROC_CMD;
497    hh.data.pinf=p->p;
498    sl=iiMake_proc(&hh,NULL,&tmp);
499    if (sl) return TRUE;
500    else
501    {
502      res->Copy(&iiRETURNEXPR);
503      iiRETURNEXPR.Init();
504      return FALSE;
505    }
506  }
507  return blackbox_default_OpM(op,res,args);
508}
509
510void newstruct_destroy(blackbox */*b*/, void *d)
511{
512  if (d!=NULL)
513  {
514    lists n=(lists)d;
515    lClean_newstruct(n);
516  }
517}
518
519void *newstruct_Init(blackbox *b)
520{
521  newstruct_desc n=(newstruct_desc)b->data;
522  lists l=(lists)omAlloc0Bin(slists_bin);
523  l->Init(n->size);
524  newstruct_member nm=n->member;
525  while (nm!=NULL)
526  {
527    l->m[nm->pos].rtyp=nm->typ;
528    if (RingDependend(nm->typ) ||(nm->typ==DEF_CMD)||(nm->typ==LIST_CMD))
529      l->m[nm->pos-1].rtyp=RING_CMD;
530    l->m[nm->pos].data=idrecDataInit(nm->typ);
531    nm=nm->next;
532  }
533  return l;
534}
535
536BOOLEAN newstruct_CheckAssign(blackbox */*b*/, leftv L, leftv R)
537{
538  int lt=L->Typ();
539  int rt=R->Typ();
540  if ((lt!=DEF_CMD)&&(lt!=rt))
541  {
542    Werror("can not assign %s(%d) to member of type %s(%d)",
543            Tok2Cmdname(rt),rt,
544            Tok2Cmdname(lt),lt);
545    return TRUE;
546  }
547  return FALSE;
548}
549
550/* check internal structure:
551* BOOLEAN newstruct_Check(blackbox *b, void *d)
552{
553  newstruct_desc n=(newstruct_desc)b->data;
554  lists l=(lists)d;
555  newstruct_member nm=n->member;
556  while (nm!=NULL)
557  {
558    if ((l->m[nm->pos].rtyp!=nm->typ)
559    &&( nm->typ!=DEF_CMD))
560    {
561      Werror("type change in member %s (%s(%d) -> %s(%d))",nm->name,
562          Tok2Cmdname(nm->typ),nm->typ,
563          Tok2Cmdname(l->m[nm->pos].rtyp),l->m[nm->pos].rtyp);
564      return TRUE;
565    }
566    nm=nm->next;
567  }
568  return FALSE;
569}
570*/
571
572BOOLEAN newstruct_serialize(blackbox *b, void *d, si_link f)
573{
574  newstruct_desc dd=(newstruct_desc)b->data;
575  sleftv l;
576  memset(&l,0,sizeof(l));
577  l.rtyp=STRING_CMD;
578  l.data=(void*)getBlackboxName(dd->id);
579  f->m->Write(f, &l);
580  lists ll=(lists)d;
581  memset(&l,0,sizeof(l));
582  l.rtyp=LIST_CMD;
583  l.data=ll;
584  f->m->Write(f, &l);
585  return FALSE;
586}
587
588BOOLEAN newstruct_deserialize(blackbox **/*b*/, void **d, si_link f)
589{
590  // newstruct is serialiazed as a list,
591  // just read a list and take data,
592  // rtyp must be set correctly (to the blackbox id) by routine calling
593  // newstruct_deserialize
594  leftv l=f->m->Read(f);
595  //newstruct_desc n=(newstruct_desc)b->data;
596  //TODO: check compatibility of list l->data with description in n
597  *d=l->data;
598  return FALSE;
599}
600
601void newstruct_Print(blackbox *b,void *d)
602{
603  newstruct_desc dd=(newstruct_desc)b->data;
604  newstruct_proc p=dd->procs;
605  while((p!=NULL)&&(p->t!=PRINT_CMD))
606    p=p->next;
607  if (p!=NULL)
608  {
609    BOOLEAN sl;
610    sleftv tmp;
611    memset(&tmp,0,sizeof(tmp));
612    tmp.rtyp=dd->id;
613    tmp.data=(void*)newstruct_Copy(b,d);
614    idrec hh;
615    memset(&hh,0,sizeof(hh));
616    hh.id=Tok2Cmdname(p->t);
617    hh.typ=PROC_CMD;
618    hh.data.pinf=p->p;
619    sl=iiMake_proc(&hh,NULL,&tmp);
620    if (!sl) iiRETURNEXPR.CleanUp();
621    iiRETURNEXPR.Init();
622  }
623  else
624    blackbox_default_Print(b,d);
625}
626void newstruct_setup(const char *n, newstruct_desc d )
627{
628  blackbox *b=(blackbox*)omAlloc0(sizeof(blackbox));
629  // all undefined entries will be set to default in setBlackboxStuff
630  // the default Print is quite useful,
631  // all other are simply error messages
632  b->blackbox_destroy=newstruct_destroy;
633  b->blackbox_String=newstruct_String;
634  b->blackbox_Print=newstruct_Print;//blackbox_default_Print;
635  b->blackbox_Init=newstruct_Init;
636  b->blackbox_Copy=newstruct_Copy;
637  b->blackbox_Assign=newstruct_Assign;
638  b->blackbox_Op1=newstruct_Op1;
639  b->blackbox_Op2=newstruct_Op2;
640  //b->blackbox_Op3=blackbox_default_Op3;
641  b->blackbox_OpM=newstruct_OpM;
642  b->blackbox_CheckAssign=newstruct_CheckAssign;
643  b->blackbox_serialize=newstruct_serialize;
644  b->blackbox_deserialize=newstruct_deserialize;
645  b->data=d;
646  b->properties=1; // list_like
647  int rt=setBlackboxStuff(b,n);
648  d->id=rt;
649  //Print("create type %d (%s)\n",rt,n);
650}
651
652static newstruct_desc scanNewstructFromString(const char *s, newstruct_desc res)
653{
654  char *ss=omStrDup(s);
655  char *p=ss;
656  char *start;
657  int t;
658  char c;
659  newstruct_member elem;
660
661  idhdl save_ring=currRingHdl;
662  currRingHdl=(idhdl)1; // fake ring detection
663  loop
664  {
665    // read type:
666    while (*p==' ') p++;
667    start=p;
668    while (isalpha(*p)) p++;
669    *p='\0';
670    IsCmd(start,t);
671    if (t==0)
672    {
673      Werror("unknown type `%s`",start);
674      omFree(ss);
675      omFree(res);
676      currRingHdl=save_ring;
677      return NULL;
678    }
679    if (RingDependend(t) || (t==DEF_CMD)||(t==LIST_CMD))
680      res->size++;    // one additional field for the ring (before the data)
681    //Print("found type %s at real-pos %d",start,res->size);
682    elem=(newstruct_member)omAlloc0(sizeof(*elem));
683    // read name:
684    p++;
685    while (*p==' ') p++;
686    start=p;
687    while (isalpha(*p)) p++;
688    c=*p;
689    *p='\0';
690    elem->typ=t;
691    elem->pos=res->size;
692    if (*start=='\0') /*empty name*/
693    {
694      WerrorS("empty name for element");
695      goto error_in_newstruct_def;
696    }
697    elem->name=omStrDup(start);
698    //Print(" name:%s\n",start);
699    elem->next=res->member;
700    res->member=elem;
701    res->size++;
702
703    // next ?
704    *p=c;
705    while (*p==' ') p++;
706    if (*p!=',')
707    {
708      if (*p!='\0')
709      {
710        Werror("unknown character in newstruct:>>%s<<",p);
711        goto error_in_newstruct_def;
712      }
713      break; // end-of-list
714    }
715    p++;
716  }
717  omFree(ss);
718  currRingHdl=save_ring;
719  //Print("new type with %d elements\n",res->size);
720  //newstructShow(res);
721  return res;
722error_in_newstruct_def:
723   omFree(elem);
724   omFree(ss);
725   omFree(res);
726   currRingHdl=save_ring;
727   return NULL;
728}
729newstruct_desc newstructFromString(const char *s)
730{
731  newstruct_desc res=(newstruct_desc)omAlloc0(sizeof(*res));
732  res->size=0;
733
734  return scanNewstructFromString(s,res);
735}
736newstruct_desc newstructChildFromString(const char *parent, const char *s)
737{
738  // find parent:
739  int parent_id=0;
740  blackboxIsCmd(parent,parent_id);
741  if (parent_id<MAX_TOK)
742  {
743    Werror(">>%s< not found",parent);
744    return NULL;
745  }
746  blackbox *parent_bb=getBlackboxStuff(parent_id);
747  // check for the correct type:
748  if (parent_bb->blackbox_destroy!=newstruct_destroy)
749  {
750    Werror(">>%s< is not a user defined type",parent);
751    return NULL;
752  }
753  // setup for scanNewstructFromString:
754  newstruct_desc res=(newstruct_desc)omAlloc0(sizeof(*res));
755  newstruct_desc parent_desc=(newstruct_desc)parent_bb->data;
756  res->size=parent_desc->size;
757  res->member=parent_desc->member;
758  res->parent=parent_desc;
759
760  return scanNewstructFromString(s,res);
761}
762void newstructShow(newstruct_desc d)
763{
764  newstruct_member elem;
765  Print("id: %d\n",d->id);
766  elem=d->member;
767  while (elem!=NULL)
768  {
769    Print(">>%s<< at pos %d, type %d (%s)\n",elem->name,elem->pos,elem->typ,Tok2Cmdname(elem->typ));
770    if (RingDependend(elem->typ)|| (elem->typ==DEF_CMD) ||(elem->typ==LIST_CMD))
771      Print(">>r_%s<< at pos %d, shadow ring\n",elem->name,elem->pos-1);
772    elem=elem->next;
773  }
774}
775
776BOOLEAN newstruct_set_proc(const char *bbname,const char *func, int args,procinfov pr)
777{
778  int id=0;
779  blackboxIsCmd(bbname,id);
780  if (id<MAX_TOK)
781  {
782    Werror(">>%s<< is not a newstruct type",bbname);
783    return TRUE;
784  }
785  blackbox *bb=getBlackboxStuff(id);
786  newstruct_desc desc=(newstruct_desc)bb->data;
787  newstruct_proc p=(newstruct_proc)omAlloc(sizeof(*p));
788  p->next=desc->procs; desc->procs=p;
789
790  idhdl save_ring=currRingHdl;
791  currRingHdl=(idhdl)1; // fake ring detection
792
793  if(!IsCmd(func,p->t))
794  {
795    int t=0;
796    if (func[1]=='\0') p->t=func[0];
797    else if((t=iiOpsTwoChar(func))!=0)
798    {
799      p->t=t;
800    }
801    else
802    {
803      Werror(">>%s<< is not a kernel command",func);
804      currRingHdl = save_ring;
805      return TRUE;
806    }
807  }
808  p->args=args;
809  p->p=pr; pr->ref++;
810  currRingHdl = save_ring;
811  return FALSE;
812}
Note: See TracBrowser for help on using the repository browser.