[5c3bc3] | 1 | #include <ctype.h> |
---|
| 2 | |
---|
[762407] | 3 | #include "config.h" |
---|
[f5b40a] | 4 | #include <kernel/mod2.h> |
---|
[5c3bc3] | 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 | |
---|
[06c0b3] | 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 | |
---|
[5c3bc3] | 32 | struct newstruct_desc_s |
---|
| 33 | { |
---|
| 34 | newstruct_member member; |
---|
[64cf44] | 35 | newstruct_desc parent; |
---|
[06c0b3] | 36 | newstruct_proc procs; |
---|
[5c3bc3] | 37 | int size; // number of mebers +1 |
---|
[64cf44] | 38 | int id; // the type id assigned to this bb |
---|
[5c3bc3] | 39 | }; |
---|
| 40 | |
---|
[b2aa08] | 41 | int newstruct_desc_size() |
---|
| 42 | { |
---|
| 43 | return sizeof(newstruct_desc_s); |
---|
| 44 | } |
---|
[5c3bc3] | 45 | |
---|
| 46 | char * newstruct_String(blackbox *b, void *d) |
---|
| 47 | { |
---|
| 48 | if (d==NULL) return omStrDup("oo"); |
---|
| 49 | else |
---|
| 50 | { |
---|
[7f581e] | 51 | newstruct_desc ad=(newstruct_desc)(b->data); |
---|
[3877fe] | 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 | |
---|
[5c3bc3] | 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 | { |
---|
[64cf44] | 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); |
---|
[a8f29f] | 102 | if ((strlen(tmp2)>80)||(strchr(tmp2,'\n')!=NULL)) |
---|
| 103 | { |
---|
| 104 | StringAppend("<%s>",Tok2Cmdname(l->m[a->pos].rtyp)); |
---|
| 105 | } |
---|
| 106 | else StringAppendS(tmp2); |
---|
[64cf44] | 107 | omFree(tmp2); |
---|
| 108 | } |
---|
[5c3bc3] | 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 | } |
---|
[a2faa3] | 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 | { |
---|
[5e147a] | 128 | if (RingDependend(L->m[n].rtyp)) |
---|
[a2faa3] | 129 | { |
---|
[5e147a] | 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 | } |
---|
[06c0b3] | 143 | else if(L->m[n].rtyp==LIST_CMD) |
---|
[5e147a] | 144 | { |
---|
| 145 | N->m[n].rtyp=L->m[n].rtyp; |
---|
[06c0b3] | 146 | N->m[n].data=(void *)lCopy((lists)(L->m[n].data)); |
---|
[a2faa3] | 147 | } |
---|
[06c0b3] | 148 | else if(L->m[n].rtyp>MAX_TOK) |
---|
[46eef0] | 149 | { |
---|
| 150 | N->m[n].rtyp=L->m[n].rtyp; |
---|
[06c0b3] | 151 | blackbox *b=getBlackboxStuff(N->m[n].rtyp); |
---|
| 152 | N->m[n].data=(void *)b->blackbox_Copy(b,L->m[n].data); |
---|
[46eef0] | 153 | } |
---|
[a2faa3] | 154 | else |
---|
| 155 | N->m[n].Copy(&L->m[n]); |
---|
| 156 | } |
---|
| 157 | if (currRing!=save_ring) rChangeCurrRing(save_ring); |
---|
| 158 | return N; |
---|
| 159 | } |
---|
[06c0b3] | 160 | void * newstruct_Copy(blackbox*, void *d) |
---|
[5c3bc3] | 161 | { |
---|
| 162 | lists n1=(lists)d; |
---|
[a2faa3] | 163 | return (void*)lCopy_newstruct(n1); |
---|
[5c3bc3] | 164 | } |
---|
| 165 | |
---|
[a04a05] | 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 | |
---|
[5c3bc3] | 196 | BOOLEAN newstruct_Assign(leftv l, leftv r) |
---|
| 197 | { |
---|
| 198 | if (r->Typ()>MAX_TOK) |
---|
| 199 | { |
---|
| 200 | blackbox *rr=getBlackboxStuff(r->Typ()); |
---|
[64cf44] | 201 | if (l->Typ()!=r->Typ()) |
---|
| 202 | { |
---|
| 203 | newstruct_desc rrn=(newstruct_desc)rr->data; |
---|
[90707f] | 204 | |
---|
| 205 | if (!rrn) |
---|
| 206 | { |
---|
[3d69257] | 207 | Werror("custom type %s(%d) cannot be assigned to newstruct %s(%d)", |
---|
[90707f] | 208 | Tok2Cmdname(r->Typ()), r->Typ(), Tok2Cmdname(l->Typ()), l->Typ()); |
---|
| 209 | return TRUE; |
---|
| 210 | } |
---|
| 211 | |
---|
[64cf44] | 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 | } |
---|
[ed47aab] | 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 | } |
---|
[64cf44] | 231 | } |
---|
[5c3bc3] | 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 | } |
---|
[a2faa3] | 239 | lists n2=(lists)r->Data(); |
---|
| 240 | n2=lCopy_newstruct(n2); |
---|
[5c3bc3] | 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 | } |
---|
[7b156fb] | 252 | |
---|
| 253 | else if(l->Typ() > MAX_TOK) |
---|
| 254 | { |
---|
[a04a05] | 255 | assume(l->Typ() > MAX_TOK); |
---|
| 256 | sleftv tmp; |
---|
| 257 | if(!newstruct_equal(l->Typ(), &tmp, r)) return newstruct_Assign(l, &tmp); |
---|
[7b156fb] | 258 | } |
---|
[5c3bc3] | 259 | Werror("assign %s(%d) = %s(%d)", |
---|
| 260 | Tok2Cmdname(l->Typ()),l->Typ(),Tok2Cmdname(r->Typ()),r->Typ()); |
---|
| 261 | return TRUE; |
---|
| 262 | } |
---|
| 263 | |
---|
[0a64d50] | 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 | |
---|
[5c3bc3] | 297 | BOOLEAN newstruct_Op2(int op, leftv res, leftv a1, leftv a2) |
---|
| 298 | { |
---|
[06c0b3] | 299 | // interpreter: a1 or a2 is newstruct |
---|
[5c3bc3] | 300 | blackbox *a=getBlackboxStuff(a1->Typ()); |
---|
[06c0b3] | 301 | newstruct_desc nt; |
---|
[5c3bc3] | 302 | lists al=(lists)a1->Data(); |
---|
[06c0b3] | 303 | if (a!=NULL) |
---|
[5c3bc3] | 304 | { |
---|
[06c0b3] | 305 | nt=(newstruct_desc)a->data; |
---|
| 306 | switch(op) |
---|
[5c3bc3] | 307 | { |
---|
[06c0b3] | 308 | case '.': |
---|
[5c3bc3] | 309 | { |
---|
[06c0b3] | 310 | if (a2->name!=NULL) |
---|
[5c3bc3] | 311 | { |
---|
[06c0b3] | 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)) |
---|
[5e147a] | 316 | { |
---|
[06c0b3] | 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; |
---|
[5e147a] | 323 | } |
---|
[06c0b3] | 324 | if (nm==NULL) |
---|
| 325 | { |
---|
| 326 | Werror("member %s nor found", a2->name); |
---|
| 327 | return TRUE; |
---|
| 328 | } |
---|
| 329 | if (search_ring) |
---|
[5c3bc3] | 330 | { |
---|
[06c0b3] | 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)) |
---|
[5c3bc3] | 365 | { |
---|
[06c0b3] | 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++; |
---|
[5c3bc3] | 370 | } |
---|
| 371 | } |
---|
[06c0b3] | 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 |
---|
[5e147a] | 378 | { |
---|
[06c0b3] | 379 | Subexpr sh=res->e; |
---|
| 380 | while (sh->next != NULL) sh=sh->next; |
---|
| 381 | sh->next=r; |
---|
[5e147a] | 382 | } |
---|
[06c0b3] | 383 | return FALSE; |
---|
[5c3bc3] | 384 | } |
---|
| 385 | else |
---|
| 386 | { |
---|
[06c0b3] | 387 | WerrorS("name expected"); |
---|
| 388 | return TRUE; |
---|
[5c3bc3] | 389 | } |
---|
| 390 | } |
---|
| 391 | } |
---|
| 392 | } |
---|
[06c0b3] | 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; |
---|
[2262ab] | 400 | while((p!=NULL) && ( (p->t!=op) || (p->args!=2) )) p=p->next; |
---|
[06c0b3] | 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 | } |
---|
[5c3bc3] | 422 | return blackboxDefaultOp2(op,res,a1,a2); |
---|
| 423 | } |
---|
[a2faa3] | 424 | |
---|
[5c3bc3] | 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()); |
---|
[06c0b3] | 430 | newstruct_desc nt=(newstruct_desc)a->data; |
---|
[5c3bc3] | 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 | } |
---|
[06c0b3] | 442 | newstruct_proc p=nt->procs; |
---|
[2262ab] | 443 | |
---|
| 444 | while((p!=NULL) &&( (p->t!=op) || (p->args!=4) )) p=p->next; |
---|
| 445 | |
---|
[06c0b3] | 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); |
---|
[5c3bc3] | 486 | } |
---|
[a2faa3] | 487 | |
---|
[5c3bc3] | 488 | void newstruct_destroy(blackbox *b, void *d) |
---|
| 489 | { |
---|
| 490 | if (d!=NULL) |
---|
| 491 | { |
---|
| 492 | lists n=(lists)d; |
---|
[06c0b3] | 493 | lClean_newstruct(n); |
---|
[5c3bc3] | 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_Check(blackbox *b, void *d) |
---|
| 513 | { |
---|
| 514 | newstruct_desc n=(newstruct_desc)b->data; |
---|
| 515 | lists l=(lists)d; |
---|
| 516 | newstruct_member nm=n->member; |
---|
| 517 | while (nm!=NULL) |
---|
| 518 | { |
---|
| 519 | if ((l->m[nm->pos].rtyp!=nm->typ) |
---|
| 520 | &&( nm->typ!=DEF_CMD)) |
---|
| 521 | { |
---|
| 522 | Werror("type change in member %s (%s(%d) -> %s(%d))",nm->name, |
---|
| 523 | Tok2Cmdname(nm->typ),nm->typ, |
---|
[5e147a] | 524 | Tok2Cmdname(l->m[nm->pos].rtyp),l->m[nm->pos].rtyp); |
---|
[5c3bc3] | 525 | return TRUE; |
---|
| 526 | } |
---|
| 527 | nm=nm->next; |
---|
| 528 | } |
---|
| 529 | return FALSE; |
---|
| 530 | } |
---|
[a2faa3] | 531 | |
---|
[f84c3b] | 532 | BOOLEAN newstruct_serialize(blackbox *b, void *d, si_link f) |
---|
| 533 | { |
---|
| 534 | newstruct_desc dd=(newstruct_desc)b->data; |
---|
[a088a12] | 535 | sleftv l; |
---|
| 536 | memset(&l,0,sizeof(l)); |
---|
| 537 | l.rtyp=STRING_CMD; |
---|
| 538 | l.data=(void*)getBlackboxName(dd->id); |
---|
| 539 | f->m->Write(f, &l); |
---|
[f84c3b] | 540 | lists ll=(lists)d; |
---|
[a088a12] | 541 | memset(&l,0,sizeof(l)); |
---|
| 542 | l.rtyp=LIST_CMD; |
---|
| 543 | l.data=ll; |
---|
| 544 | f->m->Write(f, &l); |
---|
[f84c3b] | 545 | return FALSE; |
---|
| 546 | } |
---|
| 547 | |
---|
[a088a12] | 548 | BOOLEAN newstruct_deserialize(blackbox **b, void **d, si_link f) |
---|
[f84c3b] | 549 | { |
---|
[a088a12] | 550 | // newstruct is serialiazed as a list, |
---|
| 551 | // just read a list and take data, |
---|
| 552 | // rtyp must be set correctly (to the blackbox id) by routine calling |
---|
| 553 | // newstruct_deserialize |
---|
| 554 | leftv l=f->m->Read(f); |
---|
| 555 | //newstruct_desc n=(newstruct_desc)b->data; |
---|
| 556 | //TODO: check compatibility of list l->data with description in n |
---|
| 557 | *d=l->data; |
---|
| 558 | return FALSE; |
---|
[f84c3b] | 559 | } |
---|
| 560 | |
---|
[06c0b3] | 561 | void newstruct_Print(blackbox *b,void *d) |
---|
| 562 | { |
---|
| 563 | newstruct_desc dd=(newstruct_desc)b->data; |
---|
| 564 | newstruct_proc p=dd->procs; |
---|
| 565 | while((p!=NULL)&&(p->t!=PRINT_CMD)) |
---|
| 566 | p=p->next; |
---|
| 567 | if (p!=NULL) |
---|
| 568 | { |
---|
| 569 | leftv sl; |
---|
| 570 | sleftv tmp; |
---|
| 571 | memset(&tmp,0,sizeof(tmp)); |
---|
| 572 | tmp.rtyp=dd->id; |
---|
| 573 | tmp.data=(void*)newstruct_Copy(b,d); |
---|
| 574 | idrec hh; |
---|
| 575 | memset(&hh,0,sizeof(hh)); |
---|
| 576 | hh.id=Tok2Cmdname(p->t); |
---|
| 577 | hh.typ=PROC_CMD; |
---|
| 578 | hh.data.pinf=p->p; |
---|
| 579 | sl=iiMake_proc(&hh,NULL,&tmp); |
---|
| 580 | } |
---|
| 581 | else |
---|
| 582 | blackbox_default_Print(b,d); |
---|
| 583 | } |
---|
[5c3bc3] | 584 | void newstruct_setup(const char *n, newstruct_desc d ) |
---|
| 585 | { |
---|
| 586 | blackbox *b=(blackbox*)omAlloc0(sizeof(blackbox)); |
---|
| 587 | // all undefined entries will be set to default in setBlackboxStuff |
---|
[06c0b3] | 588 | // the default Print is quite useful, |
---|
[5c3bc3] | 589 | // all other are simply error messages |
---|
| 590 | b->blackbox_destroy=newstruct_destroy; |
---|
| 591 | b->blackbox_String=newstruct_String; |
---|
[06c0b3] | 592 | b->blackbox_Print=newstruct_Print;//blackbox_default_Print; |
---|
[5c3bc3] | 593 | b->blackbox_Init=newstruct_Init; |
---|
| 594 | b->blackbox_Copy=newstruct_Copy; |
---|
| 595 | b->blackbox_Assign=newstruct_Assign; |
---|
[0a64d50] | 596 | b->blackbox_Op1=newstruct_Op1; |
---|
[5c3bc3] | 597 | b->blackbox_Op2=newstruct_Op2; |
---|
| 598 | //b->blackbox_Op3=blackbox_default_Op3; |
---|
| 599 | b->blackbox_OpM=newstruct_OpM; |
---|
| 600 | b->blackbox_Check=newstruct_Check; |
---|
[f84c3b] | 601 | b->blackbox_serialize=newstruct_serialize; |
---|
| 602 | b->blackbox_deserialize=newstruct_deserialize; |
---|
[5c3bc3] | 603 | b->data=d; |
---|
| 604 | b->properties=1; // list_like |
---|
| 605 | int rt=setBlackboxStuff(b,n); |
---|
[64cf44] | 606 | d->id=rt; |
---|
[114346] | 607 | //Print("create type %d (%s)\n",rt,n); |
---|
[5c3bc3] | 608 | } |
---|
| 609 | |
---|
[64cf44] | 610 | static newstruct_desc scanNewstructFromString(const char *s, newstruct_desc res) |
---|
[5c3bc3] | 611 | { |
---|
| 612 | char *ss=omStrDup(s); |
---|
| 613 | char *p=ss; |
---|
| 614 | char *start; |
---|
| 615 | int t; |
---|
| 616 | char c; |
---|
| 617 | newstruct_member elem; |
---|
[64cf44] | 618 | |
---|
[5c3bc3] | 619 | idhdl save_ring=currRingHdl; |
---|
| 620 | currRingHdl=(idhdl)1; // fake ring detection |
---|
| 621 | loop |
---|
| 622 | { |
---|
| 623 | // read type: |
---|
| 624 | while (*p==' ') p++; |
---|
| 625 | start=p; |
---|
| 626 | while (isalpha(*p)) p++; |
---|
| 627 | *p='\0'; |
---|
| 628 | IsCmd(start,t); |
---|
| 629 | if (t==0) |
---|
| 630 | { |
---|
| 631 | Werror("unknown type `%s`",start); |
---|
| 632 | omFree(ss); |
---|
| 633 | omFree(res); |
---|
| 634 | currRingHdl=save_ring; |
---|
| 635 | return NULL; |
---|
| 636 | } |
---|
| 637 | if (RingDependend(t)) |
---|
| 638 | res->size++; // one additional field for the ring (before the data) |
---|
| 639 | //Print("found type %s at real-pos %d",start,res->size); |
---|
| 640 | elem=(newstruct_member)omAlloc0(sizeof(*elem)); |
---|
| 641 | // read name: |
---|
| 642 | p++; |
---|
| 643 | while (*p==' ') p++; |
---|
| 644 | start=p; |
---|
| 645 | while (isalpha(*p)) p++; |
---|
| 646 | c=*p; |
---|
| 647 | *p='\0'; |
---|
| 648 | elem->typ=t; |
---|
| 649 | elem->pos=res->size; |
---|
| 650 | if (*start=='\0') /*empty name*/ |
---|
| 651 | { |
---|
| 652 | WerrorS("empty name for element"); |
---|
[4878eb] | 653 | goto error_in_newstruct_def; |
---|
[5c3bc3] | 654 | } |
---|
| 655 | elem->name=omStrDup(start); |
---|
| 656 | //Print(" name:%s\n",start); |
---|
| 657 | elem->next=res->member; |
---|
| 658 | res->member=elem; |
---|
| 659 | res->size++; |
---|
| 660 | |
---|
| 661 | // next ? |
---|
| 662 | *p=c; |
---|
| 663 | while (*p==' ') p++; |
---|
[eb5526e] | 664 | if (*p!=',') |
---|
[4878eb] | 665 | { |
---|
[eb5526e] | 666 | if (*p!='\0') |
---|
| 667 | { |
---|
| 668 | Werror("unknown character in newstruct:>>%s<<",p); |
---|
| 669 | goto error_in_newstruct_def; |
---|
| 670 | } |
---|
| 671 | break; // end-of-list |
---|
[4878eb] | 672 | } |
---|
[5c3bc3] | 673 | p++; |
---|
| 674 | } |
---|
| 675 | omFree(ss); |
---|
| 676 | currRingHdl=save_ring; |
---|
[64cf44] | 677 | //Print("new type with %d elements\n",res->size); |
---|
[5c3bc3] | 678 | return res; |
---|
[4878eb] | 679 | error_in_newstruct_def: |
---|
| 680 | omFree(elem); |
---|
| 681 | omFree(ss); |
---|
| 682 | omFree(res); |
---|
| 683 | currRingHdl=save_ring; |
---|
| 684 | return NULL; |
---|
[5c3bc3] | 685 | } |
---|
[eff324] | 686 | newstruct_desc newstructFromString(const char *s) |
---|
[64cf44] | 687 | { |
---|
| 688 | newstruct_desc res=(newstruct_desc)omAlloc0(sizeof(*res)); |
---|
| 689 | res->size=0; |
---|
| 690 | |
---|
[eff324] | 691 | return scanNewstructFromString(s,res); |
---|
[64cf44] | 692 | } |
---|
| 693 | newstruct_desc newstructChildFromString(const char *parent, const char *s) |
---|
[a2faa3] | 694 | { |
---|
[64cf44] | 695 | // find parent: |
---|
| 696 | int parent_id=0; |
---|
| 697 | blackboxIsCmd(parent,parent_id); |
---|
| 698 | if (parent_id<MAX_TOK) |
---|
| 699 | { |
---|
| 700 | Werror(">>%s< not found",parent); |
---|
| 701 | return NULL; |
---|
| 702 | } |
---|
| 703 | blackbox *parent_bb=getBlackboxStuff(parent_id); |
---|
| 704 | // check for the correct type: |
---|
| 705 | if (parent_bb->blackbox_destroy!=newstruct_destroy) |
---|
| 706 | { |
---|
| 707 | Werror(">>%s< is not a user defined type",parent); |
---|
| 708 | return NULL; |
---|
| 709 | } |
---|
| 710 | // setup for scanNewstructFromString: |
---|
| 711 | newstruct_desc res=(newstruct_desc)omAlloc0(sizeof(*res)); |
---|
| 712 | newstruct_desc parent_desc=(newstruct_desc)parent_bb->data; |
---|
| 713 | res->size=parent_desc->size; |
---|
| 714 | res->member=parent_desc->member; |
---|
| 715 | res->parent=parent_desc; |
---|
| 716 | |
---|
| 717 | return scanNewstructFromString(s,res); |
---|
[a2faa3] | 718 | } |
---|
[06c0b3] | 719 | void newstructShow(newstruct_desc d) |
---|
| 720 | { |
---|
| 721 | newstruct_member elem; |
---|
| 722 | Print("id: %d\n",d->id); |
---|
| 723 | elem=d->member; |
---|
| 724 | while (elem!=NULL) |
---|
| 725 | { |
---|
| 726 | Print(">>%s<< at pos %d, type %d\n",elem->name,elem->pos,elem->typ); |
---|
| 727 | elem=elem->next; |
---|
| 728 | } |
---|
| 729 | } |
---|
| 730 | |
---|
| 731 | BOOLEAN newstruct_set_proc(const char *bbname,const char *func, int args,procinfov pr) |
---|
| 732 | { |
---|
| 733 | int id=0; |
---|
| 734 | blackboxIsCmd(bbname,id); |
---|
| 735 | blackbox *bb=getBlackboxStuff(id); |
---|
| 736 | newstruct_desc desc=(newstruct_desc)bb->data; |
---|
| 737 | newstruct_proc p=(newstruct_proc)omAlloc(sizeof(*p)); |
---|
| 738 | p->next=desc->procs; desc->procs=p; |
---|
[632c3a] | 739 | |
---|
| 740 | idhdl save_ring=currRingHdl; |
---|
| 741 | currRingHdl=(idhdl)1; // fake ring detection |
---|
| 742 | |
---|
[06c0b3] | 743 | if(!IsCmd(func,p->t)) |
---|
| 744 | { |
---|
| 745 | int t=0; |
---|
| 746 | if (func[1]=='\0') p->t=func[0]; |
---|
| 747 | else if((t=iiOpsTwoChar(func))!=0) |
---|
| 748 | { |
---|
| 749 | p->t=t; |
---|
| 750 | } |
---|
| 751 | else |
---|
| 752 | { |
---|
| 753 | Werror(">>%s<< is not a kernel command",func); |
---|
[632c3a] | 754 | currRingHdl = save_ring; |
---|
[06c0b3] | 755 | return TRUE; |
---|
| 756 | } |
---|
| 757 | } |
---|
| 758 | p->args=args; |
---|
| 759 | p->p=pr; pr->ref++; |
---|
[632c3a] | 760 | currRingHdl = save_ring; |
---|
[06c0b3] | 761 | return FALSE; |
---|
| 762 | } |
---|