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