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