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