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