1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: silink.cc,v 1.45 2005-03-25 17:13:05 levandov Exp $ */ |
---|
5 | |
---|
6 | /* |
---|
7 | * ABSTRACT: general interface to links |
---|
8 | */ |
---|
9 | |
---|
10 | #include <stdio.h> |
---|
11 | #include <string.h> |
---|
12 | #include "mod2.h" |
---|
13 | #include "tok.h" |
---|
14 | #include <omalloc.h> |
---|
15 | #include "febase.h" |
---|
16 | #include "subexpr.h" |
---|
17 | #include "ipid.h" |
---|
18 | #include "silink.h" |
---|
19 | #include "ipshell.h" |
---|
20 | #include "matpol.h" |
---|
21 | #include "ring.h" |
---|
22 | #include "lists.h" |
---|
23 | #include "ideals.h" |
---|
24 | #include "numbers.h" |
---|
25 | #include "intvec.h" |
---|
26 | |
---|
27 | // #ifdef HAVE_DBM |
---|
28 | // #ifdef ix86_Win |
---|
29 | // #define USE_GDBM |
---|
30 | // #endif |
---|
31 | // #endif |
---|
32 | |
---|
33 | omBin s_si_link_extension_bin = omGetSpecBin(sizeof(s_si_link_extension)); |
---|
34 | omBin sip_link_bin = omGetSpecBin(sizeof(sip_link)); |
---|
35 | omBin ip_link_bin = omGetSpecBin(sizeof(ip_link)); |
---|
36 | |
---|
37 | /* declarations */ |
---|
38 | static BOOLEAN DumpAscii(FILE *fd, idhdl h); |
---|
39 | static BOOLEAN DumpAsciiIdhdl(FILE *fd, idhdl h); |
---|
40 | static char* GetIdString(idhdl h); |
---|
41 | static int DumpRhs(FILE *fd, idhdl h); |
---|
42 | static BOOLEAN DumpQring(FILE *fd, idhdl h, char *type_str); |
---|
43 | static BOOLEAN DumpAsciiMaps(FILE *fd, idhdl h, idhdl rhdl); |
---|
44 | static si_link_extension slTypeInit(si_link_extension s, const char* type); |
---|
45 | |
---|
46 | /* ====================================================================== */ |
---|
47 | si_link_extension si_link_root=NULL; |
---|
48 | |
---|
49 | BOOLEAN slInit(si_link l, char *istr) |
---|
50 | { |
---|
51 | char *type = NULL, *mode = NULL, *name = NULL; |
---|
52 | int i = 0, j; |
---|
53 | |
---|
54 | // set mode and type |
---|
55 | if (istr != NULL) |
---|
56 | { |
---|
57 | // find the first colon char in istr |
---|
58 | i = 0; |
---|
59 | while (istr[i] != ':' && istr[i] != '\0') i++; |
---|
60 | if (istr[i] == ':') |
---|
61 | { |
---|
62 | // if found, set type |
---|
63 | if (i > 0) |
---|
64 | { |
---|
65 | istr[i] = '\0'; |
---|
66 | type = omStrDup(istr); |
---|
67 | istr[i] = ':'; |
---|
68 | } |
---|
69 | // and check for mode |
---|
70 | j = ++i; |
---|
71 | while (istr[j] != ' ' && istr[j] != '\0') j++; |
---|
72 | if (j > i) |
---|
73 | { |
---|
74 | mode = omStrDup(&(istr[i])); |
---|
75 | mode[j - i] = '\0'; |
---|
76 | } |
---|
77 | // and for the name |
---|
78 | while (istr[j] == ' '&& istr[j] != '\0') j++; |
---|
79 | if (istr[j] != '\0') name = omStrDup(&(istr[j])); |
---|
80 | } |
---|
81 | else // no colon find -- string is entire name |
---|
82 | { |
---|
83 | j=0; |
---|
84 | while (istr[j] == ' '&& istr[j] != '\0') j++; |
---|
85 | if (istr[j] != '\0') name = omStrDup(&(istr[j])); |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | // set the link extension |
---|
90 | if (type != NULL) |
---|
91 | { |
---|
92 | si_link_extension s = si_link_root; |
---|
93 | si_link_extension prev = s; |
---|
94 | |
---|
95 | while (strcmp(s->type, type) != 0) |
---|
96 | { |
---|
97 | if (s->next == NULL) |
---|
98 | { |
---|
99 | prev = s; |
---|
100 | s = NULL; |
---|
101 | break; |
---|
102 | } |
---|
103 | else |
---|
104 | { |
---|
105 | s = s->next; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | if (s != NULL) |
---|
110 | l->m = s; |
---|
111 | else |
---|
112 | { |
---|
113 | l->m = slTypeInit(prev, type); |
---|
114 | } |
---|
115 | omFree(type); |
---|
116 | } |
---|
117 | else |
---|
118 | l->m = si_link_root; |
---|
119 | |
---|
120 | if (l->m == NULL) return TRUE; |
---|
121 | |
---|
122 | l->name = (name != NULL ? name : omStrDup("")); |
---|
123 | l->mode = (mode != NULL ? mode : omStrDup("")); |
---|
124 | l->ref = 1; |
---|
125 | return FALSE; |
---|
126 | } |
---|
127 | |
---|
128 | void slCleanUp(si_link l) |
---|
129 | { |
---|
130 | (l->ref)--; |
---|
131 | if (l->ref == 0) |
---|
132 | { |
---|
133 | if (SI_LINK_OPEN_P(l)) |
---|
134 | { |
---|
135 | if (l->m->Kill != NULL) l->m->Kill(l); |
---|
136 | else if (l->m->Close != NULL) l->m->Close(l); |
---|
137 | } |
---|
138 | omFree((ADDRESS)l->name); |
---|
139 | omFree((ADDRESS)l->mode); |
---|
140 | memset((void *) l, 0, sizeof(ip_link)); |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | void slKill(si_link l) |
---|
145 | { |
---|
146 | slCleanUp(l); |
---|
147 | if (l->ref == 0) |
---|
148 | omFreeBin((ADDRESS)l, ip_link_bin); |
---|
149 | } |
---|
150 | |
---|
151 | char* slStatus(si_link l, char *request) |
---|
152 | { |
---|
153 | if (l == NULL) return "empty link"; |
---|
154 | else if (l->m == NULL) return "unknown link type"; |
---|
155 | else if (strcmp(request, "type") == 0) return l->m->type; |
---|
156 | else if (strcmp(request, "mode") == 0) return l->mode; |
---|
157 | else if (strcmp(request, "name") == 0) return l->name; |
---|
158 | else if (strcmp(request, "open") == 0) |
---|
159 | { |
---|
160 | if (SI_LINK_OPEN_P(l)) return "yes"; |
---|
161 | else return "no"; |
---|
162 | } |
---|
163 | else if (strcmp(request, "openread") == 0) |
---|
164 | { |
---|
165 | if (SI_LINK_R_OPEN_P(l)) return "yes"; |
---|
166 | else return "no"; |
---|
167 | } |
---|
168 | else if (strcmp(request, "openwrite") == 0) |
---|
169 | { |
---|
170 | if (SI_LINK_W_OPEN_P(l)) return "yes"; |
---|
171 | else return "no"; |
---|
172 | } |
---|
173 | else if (l->m->Status == NULL) return "unknown status request"; |
---|
174 | else return l->m->Status(l, request); |
---|
175 | } |
---|
176 | |
---|
177 | //-------------------------------------------------------------------------- |
---|
178 | BOOLEAN slOpen(si_link l, short flag) |
---|
179 | { |
---|
180 | BOOLEAN res; |
---|
181 | |
---|
182 | if (l->m == NULL) slInit(l, ""); |
---|
183 | |
---|
184 | if (SI_LINK_OPEN_P(l)) |
---|
185 | { |
---|
186 | Warn("open: link of type: %s, mode: %s, name: %s is already open", |
---|
187 | l->m->type, l->mode, l->name); |
---|
188 | return FALSE; |
---|
189 | } |
---|
190 | else if (l->m->Open != NULL) |
---|
191 | res = l->m->Open(l, flag); |
---|
192 | else |
---|
193 | res = TRUE; |
---|
194 | |
---|
195 | if (res) |
---|
196 | Werror("open: Error for link of type: %s, mode: %s, name: %s", |
---|
197 | l->m->type, l->mode, l->name); |
---|
198 | return res; |
---|
199 | } |
---|
200 | |
---|
201 | BOOLEAN slClose(si_link l) |
---|
202 | { |
---|
203 | BOOLEAN res; |
---|
204 | |
---|
205 | if(! SI_LINK_OPEN_P(l)) |
---|
206 | return FALSE; |
---|
207 | else if (l->m->Close != NULL) |
---|
208 | res = l->m->Close(l); |
---|
209 | else |
---|
210 | res = TRUE; |
---|
211 | |
---|
212 | if (res) |
---|
213 | Werror("close: Error for link of type: %s, mode: %s, name: %s", |
---|
214 | l->m->type, l->mode, l->name); |
---|
215 | return res; |
---|
216 | } |
---|
217 | |
---|
218 | leftv slRead(si_link l, leftv a) |
---|
219 | { |
---|
220 | char *mode; |
---|
221 | leftv v = NULL; |
---|
222 | if( ! SI_LINK_R_OPEN_P(l)) // open r ? |
---|
223 | { |
---|
224 | #ifdef HAVE_DBM |
---|
225 | #ifdef USE_GDBM |
---|
226 | if (! SI_LINK_CLOSE_P(l)) |
---|
227 | { |
---|
228 | if (slClose(l)) return NULL; |
---|
229 | } |
---|
230 | #endif |
---|
231 | #endif |
---|
232 | if (slOpen(l, SI_LINK_READ)) return NULL; |
---|
233 | } |
---|
234 | |
---|
235 | if (SI_LINK_R_OPEN_P(l)) |
---|
236 | { // open r |
---|
237 | if (a==NULL) |
---|
238 | { |
---|
239 | if (l->m->Read != NULL) v = l->m->Read(l); |
---|
240 | } |
---|
241 | else |
---|
242 | { |
---|
243 | if (l->m->Read2 != NULL) v = l->m->Read2(l,a); |
---|
244 | } |
---|
245 | } |
---|
246 | else |
---|
247 | { |
---|
248 | Werror("read: Error to open link of type %s, mode: %s, name: %s for reading", |
---|
249 | l->m->type, l->mode, l->name); |
---|
250 | return NULL; |
---|
251 | } |
---|
252 | |
---|
253 | // here comes the eval: |
---|
254 | if (v != NULL) |
---|
255 | { |
---|
256 | if (v->Eval() && !errorreported) |
---|
257 | WerrorS("eval: failed"); |
---|
258 | } |
---|
259 | else |
---|
260 | Werror("read: Error for link of type %s, mode: %s, name: %s", |
---|
261 | l->m->type, l->mode, l->name); |
---|
262 | return v; |
---|
263 | } |
---|
264 | |
---|
265 | BOOLEAN slWrite(si_link l, leftv v) |
---|
266 | { |
---|
267 | BOOLEAN res; |
---|
268 | |
---|
269 | if(! SI_LINK_W_OPEN_P(l)) // open w ? |
---|
270 | { |
---|
271 | #ifdef HAVE_DBM |
---|
272 | #ifdef USE_GDBM |
---|
273 | if (! SI_LINK_CLOSE_P(l)) |
---|
274 | { |
---|
275 | if (slClose(l)) return TRUE; |
---|
276 | } |
---|
277 | #endif |
---|
278 | #endif |
---|
279 | if (slOpen(l, SI_LINK_WRITE)) return TRUE; |
---|
280 | } |
---|
281 | |
---|
282 | if (SI_LINK_W_OPEN_P(l)) |
---|
283 | { // now open w |
---|
284 | if (l->m->Write != NULL) |
---|
285 | res = l->m->Write(l,v); |
---|
286 | else |
---|
287 | res = TRUE; |
---|
288 | |
---|
289 | if (res) |
---|
290 | Werror("write: Error for link of type %s, mode: %s, name: %s", |
---|
291 | l->m->type, l->mode, l->name); |
---|
292 | return res; |
---|
293 | } |
---|
294 | else |
---|
295 | { |
---|
296 | Werror("write: Error to open link of type %s, mode: %s, name: %s for writing", |
---|
297 | l->m->type, l->mode, l->name); |
---|
298 | return TRUE; |
---|
299 | } |
---|
300 | } |
---|
301 | |
---|
302 | BOOLEAN slDump(si_link l) |
---|
303 | { |
---|
304 | BOOLEAN res; |
---|
305 | |
---|
306 | if(! SI_LINK_W_OPEN_P(l)) // open w ? |
---|
307 | { |
---|
308 | if (slOpen(l, SI_LINK_WRITE)) return TRUE; |
---|
309 | } |
---|
310 | |
---|
311 | if(SI_LINK_W_OPEN_P(l)) |
---|
312 | { // now open w |
---|
313 | if (l->m->Dump != NULL) |
---|
314 | res = l->m->Dump(l); |
---|
315 | else |
---|
316 | res = TRUE; |
---|
317 | |
---|
318 | if (res) |
---|
319 | Werror("dump: Error for link of type %s, mode: %s, name: %s", |
---|
320 | l->m->type, l->mode, l->name); |
---|
321 | return res; |
---|
322 | } |
---|
323 | else |
---|
324 | { |
---|
325 | Werror("dump: Error to open link of type %s, mode: %s, name: %s for writing", |
---|
326 | l->m->type, l->mode, l->name); |
---|
327 | return TRUE; |
---|
328 | } |
---|
329 | } |
---|
330 | |
---|
331 | BOOLEAN slGetDump(si_link l) |
---|
332 | { |
---|
333 | BOOLEAN res; |
---|
334 | |
---|
335 | if(! SI_LINK_R_OPEN_P(l)) // open r ? |
---|
336 | { |
---|
337 | if (slOpen(l, SI_LINK_READ)) return TRUE; |
---|
338 | } |
---|
339 | |
---|
340 | if(SI_LINK_R_OPEN_P(l)) |
---|
341 | { // now open r |
---|
342 | if (l->m->GetDump != NULL) |
---|
343 | res = l->m->GetDump(l); |
---|
344 | else |
---|
345 | res = TRUE; |
---|
346 | |
---|
347 | if (res) |
---|
348 | Werror("getdump: Error for link of type %s, mode: %s, name: %s", |
---|
349 | l->m->type, l->mode, l->name); |
---|
350 | //res|=slClose(l); |
---|
351 | return res; |
---|
352 | } |
---|
353 | else |
---|
354 | { |
---|
355 | Werror("dump: Error open link of type %s, mode: %s, name: %s for reading", |
---|
356 | l->m->type, l->mode, l->name); |
---|
357 | return TRUE; |
---|
358 | } |
---|
359 | } |
---|
360 | |
---|
361 | |
---|
362 | /* =============== ASCII ============================================= */ |
---|
363 | BOOLEAN slOpenAscii(si_link l, short flag) |
---|
364 | { |
---|
365 | char *mode; |
---|
366 | if (flag & SI_LINK_OPEN) |
---|
367 | { |
---|
368 | if (l->mode[0] != '\0' && (strcmp(l->mode, "r") == 0)) |
---|
369 | flag = SI_LINK_READ; |
---|
370 | else flag = SI_LINK_WRITE; |
---|
371 | } |
---|
372 | |
---|
373 | if (flag == SI_LINK_READ) mode = "r"; |
---|
374 | else if (strcmp(l->mode, "w") == 0) mode = "w"; |
---|
375 | else mode = "a"; |
---|
376 | |
---|
377 | |
---|
378 | if (l->name[0] == '\0') |
---|
379 | { |
---|
380 | // stdin or stdout |
---|
381 | if (flag == SI_LINK_READ) |
---|
382 | { |
---|
383 | l->data = (void *) stdin; |
---|
384 | mode = "r"; |
---|
385 | } |
---|
386 | else |
---|
387 | { |
---|
388 | l->data = (void *) stdout; |
---|
389 | mode = "a"; |
---|
390 | } |
---|
391 | } |
---|
392 | else |
---|
393 | { |
---|
394 | // normal ascii link to a file |
---|
395 | FILE *outfile; |
---|
396 | char *filename=l->name; |
---|
397 | |
---|
398 | if(filename[0]=='>') |
---|
399 | { |
---|
400 | if (filename[1]=='>') |
---|
401 | { |
---|
402 | filename+=2; |
---|
403 | mode = "a"; |
---|
404 | } |
---|
405 | else |
---|
406 | { |
---|
407 | filename++; |
---|
408 | mode="w"; |
---|
409 | } |
---|
410 | } |
---|
411 | outfile=myfopen(filename,mode); |
---|
412 | if (outfile!=NULL) |
---|
413 | l->data = (void *) outfile; |
---|
414 | else |
---|
415 | return TRUE; |
---|
416 | } |
---|
417 | |
---|
418 | omFree(l->mode); |
---|
419 | l->mode = omStrDup(mode); |
---|
420 | SI_LINK_SET_OPEN_P(l, flag); |
---|
421 | return FALSE; |
---|
422 | } |
---|
423 | |
---|
424 | BOOLEAN slCloseAscii(si_link l) |
---|
425 | { |
---|
426 | SI_LINK_SET_CLOSE_P(l); |
---|
427 | if (l->name[0] != '\0') |
---|
428 | { |
---|
429 | return (fclose((FILE *)l->data)!=0); |
---|
430 | } |
---|
431 | return FALSE; |
---|
432 | } |
---|
433 | |
---|
434 | leftv slReadAscii2(si_link l, leftv pr) |
---|
435 | { |
---|
436 | FILE * fp=(FILE *)l->data; |
---|
437 | char * buf=NULL; |
---|
438 | if (fp!=NULL && l->name[0] != '\0') |
---|
439 | { |
---|
440 | fseek(fp,0L,SEEK_END); |
---|
441 | long len=ftell(fp); |
---|
442 | fseek(fp,0L,SEEK_SET); |
---|
443 | buf=(char *)omAlloc((int)len+1); |
---|
444 | if (BVERBOSE(V_READING)) |
---|
445 | Print("//Reading %d chars\n",len); |
---|
446 | myfread( buf, len, 1, fp); |
---|
447 | buf[len]='\0'; |
---|
448 | } |
---|
449 | else |
---|
450 | { |
---|
451 | #ifdef HAVE_TCL |
---|
452 | if(tclmode) |
---|
453 | { |
---|
454 | WerrorS("reading from STDIN in TCL-mode not implemented"); |
---|
455 | buf=omStrDup(""); |
---|
456 | } |
---|
457 | else |
---|
458 | #endif |
---|
459 | { |
---|
460 | if (pr->Typ()==STRING_CMD) |
---|
461 | { |
---|
462 | buf=(char *)omAlloc(80); |
---|
463 | fe_fgets_stdin((char *)pr->Data(),buf,80); |
---|
464 | } |
---|
465 | else |
---|
466 | { |
---|
467 | WerrorS("read(<link>,<string>) expected"); |
---|
468 | buf=omStrDup(""); |
---|
469 | } |
---|
470 | } |
---|
471 | } |
---|
472 | leftv v=(leftv)omAlloc0Bin(sleftv_bin); |
---|
473 | v->rtyp=STRING_CMD; |
---|
474 | v->data=buf; |
---|
475 | return v; |
---|
476 | } |
---|
477 | |
---|
478 | leftv slReadAscii(si_link l) |
---|
479 | { |
---|
480 | sleftv tmp; |
---|
481 | memset(&tmp,0,sizeof(sleftv)); |
---|
482 | tmp.rtyp=STRING_CMD; |
---|
483 | tmp.data=(void*) "? "; |
---|
484 | return slReadAscii2(l,&tmp); |
---|
485 | } |
---|
486 | |
---|
487 | BOOLEAN slWriteAscii(si_link l, leftv v) |
---|
488 | { |
---|
489 | FILE *outfile=(FILE *)l->data; |
---|
490 | BOOLEAN err=FALSE; |
---|
491 | char *s; |
---|
492 | while (v!=NULL) |
---|
493 | { |
---|
494 | s = v->String(); |
---|
495 | // free v ?? |
---|
496 | if (s!=NULL) |
---|
497 | { |
---|
498 | fprintf(outfile,"%s\n",s); |
---|
499 | omFree((ADDRESS)s); |
---|
500 | } |
---|
501 | else |
---|
502 | { |
---|
503 | Werror("cannot convert to string"); |
---|
504 | err=TRUE; |
---|
505 | } |
---|
506 | v = v->next; |
---|
507 | } |
---|
508 | fflush(outfile); |
---|
509 | return err; |
---|
510 | } |
---|
511 | |
---|
512 | char* slStatusAscii(si_link l, char* request) |
---|
513 | { |
---|
514 | if (strcmp(request, "read") == 0) |
---|
515 | { |
---|
516 | if (SI_LINK_R_OPEN_P(l)) return "ready"; |
---|
517 | else return "not ready"; |
---|
518 | } |
---|
519 | else if (strcmp(request, "write") == 0) |
---|
520 | { |
---|
521 | if (SI_LINK_W_OPEN_P(l)) return "ready"; |
---|
522 | else return "not ready"; |
---|
523 | } |
---|
524 | else return "unknown status request"; |
---|
525 | } |
---|
526 | |
---|
527 | /*------------------ Dumping in Ascii format -----------------------*/ |
---|
528 | |
---|
529 | BOOLEAN slDumpAscii(si_link l) |
---|
530 | { |
---|
531 | FILE *fd = (FILE *) l->data; |
---|
532 | idhdl h = IDROOT, rh = currRingHdl; |
---|
533 | BOOLEAN status = DumpAscii(fd, h); |
---|
534 | |
---|
535 | if (! status ) status = DumpAsciiMaps(fd, h, NULL); |
---|
536 | |
---|
537 | if (currRingHdl != rh) rSetHdl(rh); |
---|
538 | fprintf(fd, "RETURN();\n"); |
---|
539 | fflush(fd); |
---|
540 | |
---|
541 | return status; |
---|
542 | } |
---|
543 | |
---|
544 | // we do that recursively, to dump ids in the the order in which they |
---|
545 | // were actually defined |
---|
546 | static BOOLEAN DumpAscii(FILE *fd, idhdl h) |
---|
547 | { |
---|
548 | if (h == NULL) return FALSE; |
---|
549 | |
---|
550 | if (DumpAscii(fd, IDNEXT(h))) return TRUE; |
---|
551 | |
---|
552 | // need to set the ring before writing it, otherwise we get in |
---|
553 | // trouble with minpoly |
---|
554 | if (IDTYP(h) == RING_CMD || IDTYP(h) == QRING_CMD) |
---|
555 | rSetHdl(h); |
---|
556 | |
---|
557 | if (DumpAsciiIdhdl(fd, h)) return TRUE; |
---|
558 | |
---|
559 | if (IDTYP(h) == RING_CMD || IDTYP(h) == QRING_CMD) |
---|
560 | return DumpAscii(fd, IDRING(h)->idroot); |
---|
561 | else |
---|
562 | return FALSE; |
---|
563 | } |
---|
564 | |
---|
565 | static BOOLEAN DumpAsciiMaps(FILE *fd, idhdl h, idhdl rhdl) |
---|
566 | { |
---|
567 | if (h == NULL) return FALSE; |
---|
568 | if (DumpAsciiMaps(fd, IDNEXT(h), rhdl)) return TRUE; |
---|
569 | |
---|
570 | if (IDTYP(h) == RING_CMD || IDTYP(h) == QRING_CMD) |
---|
571 | return DumpAsciiMaps(fd, IDRING(h)->idroot, h); |
---|
572 | else if (IDTYP(h) == MAP_CMD) |
---|
573 | { |
---|
574 | char *rhs; |
---|
575 | rSetHdl(rhdl); |
---|
576 | rhs = h->String(); |
---|
577 | |
---|
578 | if (fprintf(fd, "setring %s;\n", IDID(rhdl)) == EOF) return TRUE; |
---|
579 | if (fprintf(fd, "%s %s = %s, %s;\n", Tok2Cmdname(MAP_CMD), IDID(h), |
---|
580 | IDMAP(h)->preimage, rhs) == EOF) |
---|
581 | { |
---|
582 | omFree(rhs); |
---|
583 | return TRUE; |
---|
584 | } |
---|
585 | else |
---|
586 | { |
---|
587 | omFree(rhs); |
---|
588 | return FALSE; |
---|
589 | } |
---|
590 | } |
---|
591 | else return FALSE; |
---|
592 | } |
---|
593 | |
---|
594 | static BOOLEAN DumpAsciiIdhdl(FILE *fd, idhdl h) |
---|
595 | { |
---|
596 | char *type_str = GetIdString(h); |
---|
597 | idtyp type_id = IDTYP(h); |
---|
598 | |
---|
599 | #ifdef HAVE_NS |
---|
600 | if ((type_id == PACKAGE_CMD) &&(strcmp(IDID(h), "Top") == 0)) |
---|
601 | return FALSE; |
---|
602 | #endif |
---|
603 | |
---|
604 | // we do not throw an error if a wrong type was attempted to be dumped |
---|
605 | if (type_str == NULL) |
---|
606 | return FALSE; |
---|
607 | |
---|
608 | // handle qrings separately |
---|
609 | if (type_id == QRING_CMD) |
---|
610 | return DumpQring(fd, h, type_str); |
---|
611 | |
---|
612 | #ifndef HAVE_NS |
---|
613 | // do not dump LIB string |
---|
614 | if (type_id == STRING_CMD && strcmp("LIB", IDID(h)) == 0) |
---|
615 | return FALSE; |
---|
616 | #endif |
---|
617 | |
---|
618 | // put type and name |
---|
619 | if (fprintf(fd, "%s %s", type_str, IDID(h)) == EOF) |
---|
620 | return TRUE; |
---|
621 | // for matricies, append the dimension |
---|
622 | if (type_id == MATRIX_CMD) |
---|
623 | { |
---|
624 | ideal id = IDIDEAL(h); |
---|
625 | if (fprintf(fd, "[%d][%d]", id->nrows, id->ncols)== EOF) return TRUE; |
---|
626 | } |
---|
627 | else if (type_id == INTMAT_CMD) |
---|
628 | { |
---|
629 | if (fprintf(fd, "[%d][%d]", IDINTVEC(h)->rows(), IDINTVEC(h)->cols()) |
---|
630 | == EOF) return TRUE; |
---|
631 | } |
---|
632 | |
---|
633 | #ifdef HAVE_NS |
---|
634 | if (type_id == PACKAGE_CMD) |
---|
635 | { |
---|
636 | return (fprintf(fd, ";\n") == EOF); |
---|
637 | } |
---|
638 | #endif |
---|
639 | |
---|
640 | // write the equal sign |
---|
641 | if (fprintf(fd, " = ") == EOF) return TRUE; |
---|
642 | |
---|
643 | // and the right hand side |
---|
644 | if (DumpRhs(fd, h) == EOF) return TRUE; |
---|
645 | |
---|
646 | // semicolon und tschuess |
---|
647 | if (fprintf(fd, ";\n") == EOF) return TRUE; |
---|
648 | |
---|
649 | return FALSE; |
---|
650 | } |
---|
651 | |
---|
652 | static char* GetIdString(idhdl h) |
---|
653 | { |
---|
654 | idtyp type = IDTYP(h); |
---|
655 | |
---|
656 | switch(type) |
---|
657 | { |
---|
658 | case LIST_CMD: |
---|
659 | { |
---|
660 | lists l = IDLIST(h); |
---|
661 | int i, nl = l->nr + 1; |
---|
662 | char *name; |
---|
663 | |
---|
664 | for (i=0; i<nl; i++) |
---|
665 | if (GetIdString((idhdl) &(l->m[i])) == NULL) return NULL; |
---|
666 | } |
---|
667 | case PACKAGE_CMD: |
---|
668 | case INT_CMD: |
---|
669 | case INTVEC_CMD: |
---|
670 | case INTMAT_CMD: |
---|
671 | case STRING_CMD: |
---|
672 | case RING_CMD: |
---|
673 | case QRING_CMD: |
---|
674 | case PROC_CMD: |
---|
675 | case NUMBER_CMD: |
---|
676 | case POLY_CMD: |
---|
677 | case IDEAL_CMD: |
---|
678 | case VECTOR_CMD: |
---|
679 | case MODUL_CMD: |
---|
680 | case MATRIX_CMD: |
---|
681 | return Tok2Cmdname(type); |
---|
682 | |
---|
683 | case MAP_CMD: |
---|
684 | case LINK_CMD: |
---|
685 | return NULL; |
---|
686 | |
---|
687 | default: |
---|
688 | Warn("Error dump data of type %s", Tok2Cmdname(IDTYP(h))); |
---|
689 | return NULL; |
---|
690 | } |
---|
691 | } |
---|
692 | |
---|
693 | static BOOLEAN DumpQring(FILE *fd, idhdl h, char *type_str) |
---|
694 | { |
---|
695 | char *ring_str = h->String(); |
---|
696 | if (fprintf(fd, "%s temp_ring = %s;\n", Tok2Cmdname(RING_CMD), ring_str) |
---|
697 | == EOF) return TRUE; |
---|
698 | if (fprintf(fd, "%s temp_ideal = %s;\n", Tok2Cmdname(IDEAL_CMD), |
---|
699 | iiStringMatrix((matrix) IDRING(h)->qideal, 1)) |
---|
700 | == EOF) return TRUE; |
---|
701 | if (fprintf(fd, "attrib(temp_ideal, \"isSB\", 1);\n") == EOF) return TRUE; |
---|
702 | if (fprintf(fd, "%s %s = temp_ideal;\n", type_str, IDID(h)) == EOF) |
---|
703 | return TRUE; |
---|
704 | if (fprintf(fd, "kill temp_ring;\n") == EOF) return TRUE; |
---|
705 | else |
---|
706 | { |
---|
707 | omFree(ring_str); |
---|
708 | return FALSE; |
---|
709 | } |
---|
710 | } |
---|
711 | |
---|
712 | |
---|
713 | static int DumpRhs(FILE *fd, idhdl h) |
---|
714 | { |
---|
715 | idtyp type_id = IDTYP(h); |
---|
716 | |
---|
717 | if (type_id == LIST_CMD) |
---|
718 | { |
---|
719 | lists l = IDLIST(h); |
---|
720 | int i, nl = l->nr; |
---|
721 | |
---|
722 | fprintf(fd, "list("); |
---|
723 | |
---|
724 | for (i=0; i<nl; i++) |
---|
725 | { |
---|
726 | if (DumpRhs(fd, (idhdl) &(l->m[i])) == EOF) return EOF; |
---|
727 | fprintf(fd, ","); |
---|
728 | } |
---|
729 | if (nl > 0) |
---|
730 | { |
---|
731 | if (DumpRhs(fd, (idhdl) &(l->m[nl])) == EOF) return EOF; |
---|
732 | } |
---|
733 | fprintf(fd, ")"); |
---|
734 | } |
---|
735 | else if (type_id == STRING_CMD) |
---|
736 | { |
---|
737 | char *pstr = IDSTRING(h), c; |
---|
738 | fputc('"', fd); |
---|
739 | while (*pstr != '\0') |
---|
740 | { |
---|
741 | if (*pstr == '"' || *pstr == '\\') fputc('\\', fd); |
---|
742 | fputc(*pstr, fd); |
---|
743 | pstr++; |
---|
744 | } |
---|
745 | fputc('"', fd); |
---|
746 | } |
---|
747 | else if (type_id == PROC_CMD) |
---|
748 | { |
---|
749 | procinfov pi = IDPROC(h); |
---|
750 | if (pi->language == LANG_SINGULAR) { |
---|
751 | if( pi->data.s.body==NULL) iiGetLibProcBuffer(pi); |
---|
752 | char *pstr = pi->data.s.body, c; |
---|
753 | fputc('"', fd); |
---|
754 | while (*pstr != '\0') { |
---|
755 | if (*pstr == '"' || *pstr == '\\') fputc('\\', fd); |
---|
756 | fputc(*pstr, fd); |
---|
757 | pstr++; |
---|
758 | } |
---|
759 | fputc('"', fd); |
---|
760 | } else fputs("(null)", fd); |
---|
761 | } |
---|
762 | else |
---|
763 | { |
---|
764 | char *rhs = h->String(); |
---|
765 | |
---|
766 | if (rhs == NULL) return EOF; |
---|
767 | |
---|
768 | if (type_id == INTVEC_CMD) fprintf(fd, "intvec("); |
---|
769 | |
---|
770 | if (fprintf(fd, "%s", rhs) == EOF) return EOF; |
---|
771 | omFree(rhs); |
---|
772 | |
---|
773 | if ((type_id == RING_CMD || type_id == QRING_CMD) && |
---|
774 | IDRING(h)->minpoly != NULL) |
---|
775 | { |
---|
776 | StringSetS(""); |
---|
777 | nWrite(IDRING(h)->minpoly); |
---|
778 | rhs = StringAppendS(""); |
---|
779 | if (fprintf(fd, "; minpoly = %s", rhs) == EOF) return EOF; |
---|
780 | } |
---|
781 | else if (type_id == INTVEC_CMD) fprintf(fd, ")"); |
---|
782 | } |
---|
783 | return 1; |
---|
784 | } |
---|
785 | |
---|
786 | BOOLEAN slGetDumpAscii(si_link l) |
---|
787 | { |
---|
788 | if (l->name[0] == '\0') |
---|
789 | { |
---|
790 | Werror("getdump: Can not get dump from stdin"); |
---|
791 | return TRUE; |
---|
792 | } |
---|
793 | else |
---|
794 | { |
---|
795 | BOOLEAN status = newFile(l->name); |
---|
796 | if (status) |
---|
797 | return TRUE; |
---|
798 | |
---|
799 | int old_echo=si_echo; |
---|
800 | si_echo=0; |
---|
801 | |
---|
802 | status=yyparse(); |
---|
803 | |
---|
804 | si_echo=old_echo; |
---|
805 | |
---|
806 | if (status) |
---|
807 | return TRUE; |
---|
808 | else |
---|
809 | { |
---|
810 | // lets reset the file pointer to the end to reflect that |
---|
811 | // we are finished with reading |
---|
812 | FILE *f = (FILE *) l->data; |
---|
813 | fseek(f, 0L, SEEK_END); |
---|
814 | return FALSE; |
---|
815 | } |
---|
816 | } |
---|
817 | } |
---|
818 | |
---|
819 | |
---|
820 | /*------------Initialization at Start-up time------------------------*/ |
---|
821 | |
---|
822 | #include "slInit.h" |
---|
823 | |
---|
824 | static si_link_extension slTypeInit(si_link_extension s, const char* type) |
---|
825 | { |
---|
826 | assume(s != NULL); |
---|
827 | s->next = NULL; |
---|
828 | si_link_extension ns = (si_link_extension)omAlloc0Bin(s_si_link_extension_bin); |
---|
829 | |
---|
830 | #ifdef HAVE_MPSR |
---|
831 | if (strcmp(type, "MPfile") == 0) |
---|
832 | s->next = slInitMPFileExtension(ns); |
---|
833 | else if (strcmp(type, "MPtcp") == 0) |
---|
834 | s->next = slInitMPTcpExtension(ns); |
---|
835 | #ifdef HAVE_DBM |
---|
836 | else |
---|
837 | #endif |
---|
838 | #endif |
---|
839 | #ifdef HAVE_DBM |
---|
840 | if (strcmp(type, "DBM") == 0) |
---|
841 | s->next = slInitDBMExtension(ns); |
---|
842 | #endif |
---|
843 | #if defined(HAVE_DBM) || defined(HAVE_MPSR) |
---|
844 | else |
---|
845 | #endif |
---|
846 | { |
---|
847 | Warn("Found unknown link type: %s", type); |
---|
848 | Warn("Use default link type: %s", si_link_root->type); |
---|
849 | omFreeBin(ns, s_si_link_extension_bin); |
---|
850 | return si_link_root; |
---|
851 | } |
---|
852 | |
---|
853 | if (s->next == NULL) |
---|
854 | { |
---|
855 | Werror("Can not initialize link type %s", type); |
---|
856 | omFreeBin(ns, s_si_link_extension_bin); |
---|
857 | return NULL; |
---|
858 | } |
---|
859 | return s->next; |
---|
860 | } |
---|
861 | |
---|
862 | void slStandardInit() |
---|
863 | { |
---|
864 | si_link_extension s; |
---|
865 | si_link_root=(si_link_extension)omAlloc0Bin(s_si_link_extension_bin); |
---|
866 | si_link_root->Open=slOpenAscii; |
---|
867 | si_link_root->Close=slCloseAscii; |
---|
868 | si_link_root->Kill=slCloseAscii; |
---|
869 | si_link_root->Read=slReadAscii; |
---|
870 | si_link_root->Read2=slReadAscii2; |
---|
871 | si_link_root->Write=slWriteAscii; |
---|
872 | si_link_root->Dump=slDumpAscii; |
---|
873 | si_link_root->GetDump=slGetDumpAscii; |
---|
874 | si_link_root->Status=slStatusAscii; |
---|
875 | si_link_root->type="ASCII"; |
---|
876 | s = si_link_root; |
---|
877 | s->next = NULL; |
---|
878 | } |
---|