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