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