1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: silink.cc,v 1.6 1997-04-02 15:07:50 Singular Exp $ */ |
---|
5 | |
---|
6 | /* |
---|
7 | * ABSTRACT |
---|
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 | |
---|
25 | /* declarations */ |
---|
26 | static BOOLEAN DumpAscii(FILE *fd, idhdl h); |
---|
27 | static BOOLEAN DumpAsciiIdhdl(FILE *fd, idhdl h); |
---|
28 | static char* GetIdString(idhdl h); |
---|
29 | static int DumpRhs(FILE *fd, idhdl h); |
---|
30 | static BOOLEAN DumpQring(FILE *fd, idhdl h, char *type_str); |
---|
31 | |
---|
32 | /* =============== general utilities ====================================== */ |
---|
33 | void GetCmdArgs(int *argc, char ***argv, char *str) |
---|
34 | { |
---|
35 | int i = 0, sl = strlen(str)+1, j; |
---|
36 | char *s2=mstrdup(str); |
---|
37 | |
---|
38 | #ifdef HAVE_MPSR |
---|
39 | char *appl = strstr(s2, "-MPapplication"); |
---|
40 | if (appl != NULL) |
---|
41 | { |
---|
42 | *(appl-1) = '\0'; |
---|
43 | i = 2; |
---|
44 | } |
---|
45 | #endif |
---|
46 | |
---|
47 | if (strtok(s2, " ") != NULL) |
---|
48 | { |
---|
49 | i++; |
---|
50 | while (strtok(NULL," ") != NULL) i++; |
---|
51 | } |
---|
52 | *argc = i; |
---|
53 | if (i>0) |
---|
54 | { |
---|
55 | *argv = (char **) Alloc0(i*sizeof(char *)); |
---|
56 | #ifdef HAVE_MPSR |
---|
57 | if (appl != NULL) i -= 2; |
---|
58 | #endif |
---|
59 | if (i>0) |
---|
60 | { |
---|
61 | strcpy(s2,str); |
---|
62 | *argv[0] = mstrdup(strtok(s2, " ")); |
---|
63 | for(j = 1; j <i; j++) |
---|
64 | (*argv)[j] = mstrdup(strtok(NULL, " ")); |
---|
65 | } |
---|
66 | } |
---|
67 | else |
---|
68 | *argv = NULL; |
---|
69 | |
---|
70 | #ifdef HAVE_MPSR |
---|
71 | if (appl != NULL) |
---|
72 | { |
---|
73 | (*argv)[*argc -2] = mstrdup("-MPapplication"); |
---|
74 | (*argv)[*argc -1] = mstrdup(&(appl[14])); |
---|
75 | } |
---|
76 | #endif |
---|
77 | |
---|
78 | FreeL(s2); |
---|
79 | } |
---|
80 | /* ====================================================================== */ |
---|
81 | si_link_extension si_link_root=NULL; |
---|
82 | BOOLEAN slInit(si_link l, char *istr) |
---|
83 | { |
---|
84 | char **argv; |
---|
85 | int argc; |
---|
86 | |
---|
87 | if (istr!=NULL) |
---|
88 | { |
---|
89 | GetCmdArgs(&argc, &argv, istr); |
---|
90 | // let's parse the results |
---|
91 | if ( argc <2 ) |
---|
92 | { |
---|
93 | if (argc == 0) |
---|
94 | { |
---|
95 | argv = (char **) Alloc(2*sizeof(char *)); |
---|
96 | argv[0]=mstrdup(""); |
---|
97 | } |
---|
98 | else if (argc == 1) |
---|
99 | { |
---|
100 | char *n=argv[0]; |
---|
101 | Free((ADDRESS)argv,sizeof(char *)); |
---|
102 | argv = (char **) Alloc(2*sizeof(char *)); |
---|
103 | argv[0]=n; |
---|
104 | } |
---|
105 | argv[1]=mstrdup("ascii"); |
---|
106 | argc=2; |
---|
107 | } |
---|
108 | l->argc = argc; |
---|
109 | l->argv = argv; |
---|
110 | } |
---|
111 | if (l->name==NULL) |
---|
112 | l->name = mstrdup(argv[0]); |
---|
113 | |
---|
114 | l->ref = 0; |
---|
115 | |
---|
116 | BOOLEAN not_found=TRUE; |
---|
117 | si_link_extension s=si_link_root; |
---|
118 | while (s!=NULL) |
---|
119 | { |
---|
120 | if (!(not_found=s->Init(l,s))) |
---|
121 | { |
---|
122 | l->m=s; |
---|
123 | l->linkType=s->index; |
---|
124 | break; |
---|
125 | } |
---|
126 | s=s->next; |
---|
127 | } |
---|
128 | if (not_found) Werror("unknown link type `%s`",argv[1]); |
---|
129 | return not_found; |
---|
130 | } |
---|
131 | |
---|
132 | void slCleanUp(si_link l) |
---|
133 | { |
---|
134 | (l->ref)--; |
---|
135 | if (l->ref < 0) |
---|
136 | { |
---|
137 | if (SI_LINK_OPEN_P(l)) slClose(l); |
---|
138 | if (l->name != NULL) FreeL((ADDRESS)l->name); |
---|
139 | l->name=NULL; |
---|
140 | if (l->argc!=0 && l->argv != NULL) |
---|
141 | { |
---|
142 | int i=l->argc-1; |
---|
143 | while(i>=0) |
---|
144 | { |
---|
145 | if (l->argv[i] != NULL) FreeL((ADDRESS)l->argv[i]); |
---|
146 | i--; |
---|
147 | } |
---|
148 | Free((ADDRESS)l->argv,l->argc*sizeof(char *)); |
---|
149 | } |
---|
150 | l->argv=NULL; |
---|
151 | l->argc=0; |
---|
152 | l->ref=0; |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | void slKill(si_link l) |
---|
158 | { |
---|
159 | if (l->ref == 0) |
---|
160 | { |
---|
161 | slCleanUp(l); |
---|
162 | Free((ADDRESS)l, sizeof(ip_link)); |
---|
163 | } |
---|
164 | else |
---|
165 | slCleanUp(l); |
---|
166 | } |
---|
167 | |
---|
168 | //-------------------------------------------------------------------------- |
---|
169 | BOOLEAN slOpenRead(si_link l) |
---|
170 | { |
---|
171 | if(SI_LINK_R_OPEN_P(l)) return FALSE; // already open r |
---|
172 | si_link_extension s=si_link_root; |
---|
173 | while ((s!=NULL) && (s->index!=l->linkType)) s=s->next; |
---|
174 | if ((s==NULL)||(s->OpenRead==NULL)) |
---|
175 | return TRUE; |
---|
176 | l->m=s; |
---|
177 | return s->OpenRead(l); |
---|
178 | } |
---|
179 | |
---|
180 | BOOLEAN slOpenWrite(si_link l) |
---|
181 | { |
---|
182 | if(SI_LINK_W_OPEN_P(l)) return FALSE; // already open w |
---|
183 | si_link_extension s=si_link_root; |
---|
184 | while ((s!=NULL) && (s->index!=l->linkType)) s=s->next; |
---|
185 | if ((s==NULL)||(s->OpenWrite==NULL)) |
---|
186 | return TRUE; |
---|
187 | l->m=s; |
---|
188 | return s->OpenWrite(l); |
---|
189 | } |
---|
190 | |
---|
191 | BOOLEAN slClose(si_link l) |
---|
192 | { |
---|
193 | if(! SI_LINK_OPEN_P(l)) return FALSE; // already closed |
---|
194 | si_link_extension s=l->m; |
---|
195 | if (s==NULL) |
---|
196 | { |
---|
197 | s=si_link_root; |
---|
198 | while ((s!=NULL) && (s->index!=l->linkType)) s=s->next; |
---|
199 | } |
---|
200 | if ((s==NULL)||(s->Close==NULL)) |
---|
201 | return TRUE; |
---|
202 | return s->Close(l); |
---|
203 | } |
---|
204 | |
---|
205 | leftv slRead(si_link l,leftv a) |
---|
206 | { |
---|
207 | leftv v = NULL; |
---|
208 | if( ! SI_LINK_R_OPEN_P(l)) // open r ? |
---|
209 | { |
---|
210 | if (slOpenRead(l)) return NULL; |
---|
211 | } |
---|
212 | if ((SI_LINK_R_OPEN_P(l))&&(l->m!=NULL)) |
---|
213 | { // open r |
---|
214 | if (a==NULL) |
---|
215 | { |
---|
216 | if (l->m->Read!=NULL) |
---|
217 | v=l->m->Read(l); |
---|
218 | } |
---|
219 | else |
---|
220 | { |
---|
221 | if (l->m->Read2!=NULL) |
---|
222 | v=l->m->Read2(l,a); |
---|
223 | } |
---|
224 | } |
---|
225 | // here comes the eval: |
---|
226 | if (v != NULL) |
---|
227 | { |
---|
228 | v->Eval(); |
---|
229 | } |
---|
230 | return v; |
---|
231 | } |
---|
232 | |
---|
233 | BOOLEAN slWrite(si_link l, leftv v) |
---|
234 | { |
---|
235 | if(! SI_LINK_W_OPEN_P(l)) // open w ? |
---|
236 | { |
---|
237 | if (slOpenWrite(l)) return TRUE; |
---|
238 | } |
---|
239 | if((SI_LINK_W_OPEN_P(l))&&(l->m!=NULL)) |
---|
240 | { // now open w |
---|
241 | if (l->m->Write!=NULL) |
---|
242 | return l->m->Write(l,v); |
---|
243 | } |
---|
244 | return TRUE; |
---|
245 | } |
---|
246 | |
---|
247 | BOOLEAN slDump(si_link l) |
---|
248 | { |
---|
249 | if(! SI_LINK_W_OPEN_P(l)) // open w ? |
---|
250 | { |
---|
251 | if (slOpenWrite(l)) return TRUE; |
---|
252 | } |
---|
253 | if((SI_LINK_W_OPEN_P(l))&&(l->m!=NULL)) |
---|
254 | { // now open w |
---|
255 | if (l->m->Dump!=NULL) |
---|
256 | return l->m->Dump(l); |
---|
257 | } |
---|
258 | return TRUE; |
---|
259 | } |
---|
260 | |
---|
261 | BOOLEAN slGetDump(si_link l) |
---|
262 | { |
---|
263 | if(! SI_LINK_R_OPEN_P(l)) // open w ? |
---|
264 | { |
---|
265 | if (slOpenRead(l)) return TRUE; |
---|
266 | } |
---|
267 | if((SI_LINK_R_OPEN_P(l))&&(l->m!=NULL)) |
---|
268 | { // now open w |
---|
269 | if (l->m->GetDump!=NULL) |
---|
270 | return l->m->GetDump(l); |
---|
271 | } |
---|
272 | return TRUE; |
---|
273 | } |
---|
274 | |
---|
275 | /* =============== ASCII ============================================= */ |
---|
276 | BOOLEAN slOpenWriteAscii(si_link l) |
---|
277 | { |
---|
278 | if(SI_LINK_R_OPEN_P(l)&&(l->name[0]!='\0')) |
---|
279 | { |
---|
280 | Werror("cannot open input file %s as output file",l->name); |
---|
281 | return TRUE; // already open r |
---|
282 | } |
---|
283 | FILE *outfile; |
---|
284 | char *mode="a"; |
---|
285 | char *filename=l->name; |
---|
286 | if(filename[0]=='\0') |
---|
287 | { |
---|
288 | SI_LINK_SET_W_OPEN_P(l); /*open, write */ |
---|
289 | l->data=(void *)stdout; |
---|
290 | return FALSE; |
---|
291 | } |
---|
292 | else if(filename[0]=='>') |
---|
293 | { |
---|
294 | if (filename[1]=='>') |
---|
295 | filename+=2; |
---|
296 | else |
---|
297 | { |
---|
298 | filename++; |
---|
299 | mode="w"; |
---|
300 | } |
---|
301 | } |
---|
302 | int i=2; |
---|
303 | while (i<l->argc) |
---|
304 | { |
---|
305 | if(strncmp(l->argv[i],"mode:",5)==0) |
---|
306 | mode=l->argv[i]+5; |
---|
307 | else if(strcmp(l->argv[i],"showPath")!=0) |
---|
308 | Warn("ignore link property %s",l->argv[i]); |
---|
309 | i++; |
---|
310 | } |
---|
311 | outfile=fopen(filename,mode); |
---|
312 | if (outfile!=NULL) |
---|
313 | { |
---|
314 | SI_LINK_SET_W_OPEN_P(l); /*open, write */ |
---|
315 | l->data=(void *)outfile; |
---|
316 | return FALSE; |
---|
317 | } |
---|
318 | return TRUE; |
---|
319 | } |
---|
320 | BOOLEAN slOpenReadAscii(si_link l) |
---|
321 | { |
---|
322 | if(SI_LINK_W_OPEN_P(l)) |
---|
323 | { |
---|
324 | Werror("cannot open output file %s as input file",l->name); |
---|
325 | return TRUE; // already open w |
---|
326 | } |
---|
327 | if (l->name[0]!='\0') |
---|
328 | { |
---|
329 | char *where=NULL; |
---|
330 | int i=2; |
---|
331 | while (i<l->argc) |
---|
332 | { |
---|
333 | if(strcmp(l->argv[i],"showPath")==0) |
---|
334 | where=(char *)Alloc(120); |
---|
335 | else if(strncmp(l->argv[i],"mode:",5)!=0) |
---|
336 | Warn("ignore link property %s",l->argv[i]); |
---|
337 | i++; |
---|
338 | } |
---|
339 | FILE *rfile=feFopen(l->name,"r",where); |
---|
340 | if (where!=NULL) |
---|
341 | { |
---|
342 | Print("open %s, success:%d\n",where,rfile!=NULL); |
---|
343 | Free((ADDRESS)where,120); |
---|
344 | } |
---|
345 | if (rfile!=NULL) |
---|
346 | { |
---|
347 | l->data=(void *)rfile; |
---|
348 | SI_LINK_SET_R_OPEN_P(l); |
---|
349 | return FALSE; |
---|
350 | } |
---|
351 | Werror("cannot open %s",l->name); |
---|
352 | l->data=NULL; |
---|
353 | return TRUE; |
---|
354 | } |
---|
355 | SI_LINK_SET_R_OPEN_P(l); |
---|
356 | return FALSE; |
---|
357 | } |
---|
358 | BOOLEAN slCloseAscii(si_link l) |
---|
359 | { |
---|
360 | SI_LINK_SET_CLOSE_P(l); |
---|
361 | if (l->name[0]!='\0') |
---|
362 | { |
---|
363 | return (fclose((FILE *)l->data)!=0); |
---|
364 | } |
---|
365 | return FALSE; |
---|
366 | } |
---|
367 | leftv slReadAscii(si_link l) |
---|
368 | { |
---|
369 | FILE * fp=(FILE *)l->data; |
---|
370 | char * buf=NULL; |
---|
371 | if (fp!=NULL) |
---|
372 | { |
---|
373 | fseek(fp,0L,SEEK_END); |
---|
374 | long len=ftell(fp); |
---|
375 | fseek(fp,0L,SEEK_SET); |
---|
376 | buf=(char *)AllocL((int)len+1); |
---|
377 | if (BVERBOSE(V_READING)) Print("//Reading %d chars\n",len); |
---|
378 | fread( buf, len, 1, fp); |
---|
379 | buf[len]='\0'; |
---|
380 | } |
---|
381 | else |
---|
382 | { |
---|
383 | PrintS("? "); mflush(); |
---|
384 | buf=(char *)AllocL(80); |
---|
385 | fe_fgets_stdin(buf,80); |
---|
386 | } |
---|
387 | leftv v=(leftv)Alloc0(sizeof(sleftv)); |
---|
388 | v->rtyp=STRING_CMD; |
---|
389 | v->data=buf; |
---|
390 | return v; |
---|
391 | } |
---|
392 | BOOLEAN slWriteAscii(si_link l, leftv v) |
---|
393 | { |
---|
394 | FILE *outfile=(FILE *)l->data; |
---|
395 | BOOLEAN err=FALSE; |
---|
396 | char *s; |
---|
397 | while (v!=NULL) |
---|
398 | { |
---|
399 | s = v->String(); |
---|
400 | // free v ?? |
---|
401 | if (s!=NULL) |
---|
402 | { |
---|
403 | fprintf(outfile,"%s\n",s); |
---|
404 | FreeL((ADDRESS)s); |
---|
405 | } |
---|
406 | else |
---|
407 | { |
---|
408 | Werror("cannot convert to string"); |
---|
409 | err=TRUE; |
---|
410 | } |
---|
411 | v = v->next; |
---|
412 | } |
---|
413 | fflush(outfile); |
---|
414 | return err; |
---|
415 | } |
---|
416 | |
---|
417 | BOOLEAN slInitALink(si_link l,si_link_extension s) |
---|
418 | { |
---|
419 | if (strcmp(l->argv[1], s->name) == 0) |
---|
420 | { |
---|
421 | return FALSE; |
---|
422 | } |
---|
423 | return TRUE; |
---|
424 | } |
---|
425 | |
---|
426 | BOOLEAN slDumpAscii(si_link l) |
---|
427 | { |
---|
428 | FILE *fd = (FILE *) l->data; |
---|
429 | idhdl h = idroot, rh = currRingHdl; |
---|
430 | BOOLEAN status = DumpAscii(fd, h); |
---|
431 | |
---|
432 | if (currRingHdl != rh) rSetHdl(rh, TRUE); |
---|
433 | fflush(fd); |
---|
434 | |
---|
435 | return status; |
---|
436 | } |
---|
437 | |
---|
438 | // we do that recursively, to dump ids in the the order in which they |
---|
439 | // were actually defined |
---|
440 | static BOOLEAN DumpAscii(FILE *fd, idhdl h) |
---|
441 | { |
---|
442 | if (h == NULL) return FALSE; |
---|
443 | |
---|
444 | if (DumpAscii(fd, IDNEXT(h))) return TRUE; |
---|
445 | |
---|
446 | // need to set the ring before writing it, othersie we get in |
---|
447 | // trouble with minpoly |
---|
448 | if (IDTYP(h) == RING_CMD || IDTYP(h) == QRING_CMD) |
---|
449 | rSetHdl(h, TRUE); |
---|
450 | |
---|
451 | if (DumpAsciiIdhdl(fd, h)) return TRUE; |
---|
452 | |
---|
453 | if (IDTYP(h) == RING_CMD || IDTYP(h) == QRING_CMD) |
---|
454 | { |
---|
455 | rSetHdl(h, TRUE); |
---|
456 | return DumpAscii(fd, IDRING(h)->idroot); |
---|
457 | } |
---|
458 | else |
---|
459 | return FALSE; |
---|
460 | } |
---|
461 | |
---|
462 | static BOOLEAN DumpAsciiIdhdl(FILE *fd, idhdl h) |
---|
463 | { |
---|
464 | char *type_str = GetIdString(h); |
---|
465 | idtyp type_id = IDTYP(h); |
---|
466 | |
---|
467 | // we do not throw an error if a wrong type was attempted to be dumped |
---|
468 | if (type_str == NULL) return FALSE; |
---|
469 | |
---|
470 | // handle qrings separately |
---|
471 | if (type_id == QRING_CMD) return DumpQring(fd, h, type_str); |
---|
472 | |
---|
473 | // put type and name |
---|
474 | if (fprintf(fd, "%s %s", type_str, IDID(h)) == EOF) return TRUE; |
---|
475 | |
---|
476 | // for matricies, append the dimension |
---|
477 | if (type_id == MATRIX_CMD) |
---|
478 | { |
---|
479 | ideal id = IDIDEAL(h); |
---|
480 | if (fprintf(fd, "[%d][%d]", id->nrows, id->ncols)== EOF) return TRUE; |
---|
481 | } |
---|
482 | |
---|
483 | // check for empty list; |
---|
484 | if ( ! (type_id == LIST_CMD && IDLIST(h)->nr < 0)) |
---|
485 | { |
---|
486 | // write the equal sign |
---|
487 | if (fprintf(fd, " = ") == EOF) return TRUE; |
---|
488 | // and the right hand side |
---|
489 | if (DumpRhs(fd, h) == EOF) return TRUE; |
---|
490 | } |
---|
491 | |
---|
492 | // semicolon und tschuess |
---|
493 | if (fprintf(fd, ";\n") == EOF) return TRUE; |
---|
494 | |
---|
495 | return FALSE; |
---|
496 | } |
---|
497 | |
---|
498 | static char* GetIdString(idhdl h) |
---|
499 | { |
---|
500 | idtyp type = IDTYP(h); |
---|
501 | |
---|
502 | switch(type) |
---|
503 | { |
---|
504 | case LIST_CMD: |
---|
505 | { |
---|
506 | lists l = IDLIST(h); |
---|
507 | int i, nl = l->nr + 1; |
---|
508 | char *name; |
---|
509 | |
---|
510 | for (i=0; i<nl; i++) |
---|
511 | if (GetIdString((idhdl) &(l->m[i])) == NULL) return NULL; |
---|
512 | } |
---|
513 | case INT_CMD: |
---|
514 | case INTVEC_CMD: |
---|
515 | case INTMAT_CMD: |
---|
516 | case STRING_CMD: |
---|
517 | case RING_CMD: |
---|
518 | case QRING_CMD: |
---|
519 | case PROC_CMD: |
---|
520 | case NUMBER_CMD: |
---|
521 | case POLY_CMD: |
---|
522 | case IDEAL_CMD: |
---|
523 | case VECTOR_CMD: |
---|
524 | case MODUL_CMD: |
---|
525 | case MATRIX_CMD: |
---|
526 | case MAP_CMD: |
---|
527 | return Tok2Cmdname(type); |
---|
528 | |
---|
529 | case LINK_CMD: |
---|
530 | return NULL; |
---|
531 | |
---|
532 | default: |
---|
533 | Warn("Can not dump data of type %s", Tok2Cmdname(IDTYP(h))); |
---|
534 | return NULL; |
---|
535 | } |
---|
536 | } |
---|
537 | |
---|
538 | static BOOLEAN DumpQring(FILE *fd, idhdl h, char *type_str) |
---|
539 | { |
---|
540 | char *ideal_str = iiStringMatrix((matrix) IDRING(h)->qideal, 1); |
---|
541 | |
---|
542 | if (ideal_str == NULL) return TRUE; |
---|
543 | if (fprintf(fd, "%s temp_ideal = %s;\n", Tok2Cmdname(IDEAL_CMD), ideal_str) |
---|
544 | == EOF) return TRUE; |
---|
545 | if (fprintf(fd, "attrib(temp_ideal, \"isSB\", 1);\n") == EOF) return TRUE; |
---|
546 | if (fprintf(fd, "%s %s = temp_ideal;\n", type_str, IDID(h)) == EOF) |
---|
547 | return TRUE; |
---|
548 | else |
---|
549 | return FALSE; |
---|
550 | } |
---|
551 | |
---|
552 | |
---|
553 | static int DumpRhs(FILE *fd, idhdl h) |
---|
554 | { |
---|
555 | idtyp type_id = IDTYP(h); |
---|
556 | |
---|
557 | if (type_id == LIST_CMD) |
---|
558 | { |
---|
559 | lists l = IDLIST(h); |
---|
560 | int i, nl = l->nr; |
---|
561 | idhdl h2; |
---|
562 | |
---|
563 | for (i=0; i<nl; i++) |
---|
564 | { |
---|
565 | h2 = (idhdl) &(l->m[i]); |
---|
566 | |
---|
567 | if ( ! (IDTYP(h2) == LIST_CMD && IDLIST(h2)->nr < 0)) |
---|
568 | { |
---|
569 | if (DumpRhs(fd, h2) == EOF) return EOF; |
---|
570 | fprintf(fd,", "); |
---|
571 | } |
---|
572 | } |
---|
573 | h2 = (idhdl) &(l->m[i]); |
---|
574 | if ( ! (IDTYP(h2) == LIST_CMD && IDLIST(h2)->nr < 0)) |
---|
575 | { |
---|
576 | if (DumpRhs(fd, h2) == EOF) return EOF; |
---|
577 | } |
---|
578 | } |
---|
579 | else if (type_id == PROC_CMD) |
---|
580 | { |
---|
581 | char *pstr = IDSTRING(h), c; |
---|
582 | fputc('"', fd); |
---|
583 | while (*pstr != '\0') |
---|
584 | { |
---|
585 | if (*pstr == '"') fputc('\\', fd); |
---|
586 | fputc(*pstr, fd); |
---|
587 | pstr++; |
---|
588 | } |
---|
589 | fputc('"', fd); |
---|
590 | } |
---|
591 | else |
---|
592 | { |
---|
593 | char *rhs = ((leftv) h)->String(); |
---|
594 | |
---|
595 | if (rhs == NULL) return EOF; |
---|
596 | |
---|
597 | if (type_id == STRING_CMD) fprintf(fd,"\""); |
---|
598 | if (type_id == MAP_CMD) fprintf(fd, "%s,", IDMAP(h)->preimage); |
---|
599 | |
---|
600 | if (fprintf(fd, "%s", rhs) == EOF) return EOF; |
---|
601 | FreeL(rhs); |
---|
602 | |
---|
603 | if (type_id == STRING_CMD) fprintf(fd,"\""); |
---|
604 | if ((type_id == RING_CMD || type_id == QRING_CMD) && |
---|
605 | IDRING(h)->minpoly != NULL) |
---|
606 | { |
---|
607 | StringSetS(""); |
---|
608 | nWrite(IDRING(h)->minpoly); |
---|
609 | rhs = StringAppend(""); |
---|
610 | if (fprintf(fd, "; minpoly = %s", rhs) == EOF) return EOF; |
---|
611 | } |
---|
612 | } |
---|
613 | return 1; |
---|
614 | } |
---|
615 | |
---|
616 | BOOLEAN slGetDumpAscii(si_link l) |
---|
617 | { |
---|
618 | if (l->name[0] == '\0') |
---|
619 | { |
---|
620 | Werror("Can not get dump from stdin"); |
---|
621 | return TRUE; |
---|
622 | } |
---|
623 | else |
---|
624 | { |
---|
625 | BOOLEAN status = iiPStart(NULL, l->name, NULL); |
---|
626 | |
---|
627 | if (status) |
---|
628 | return TRUE; |
---|
629 | else |
---|
630 | { |
---|
631 | // lets reset the file pointer to the beginning to reflect that |
---|
632 | // we are finished with reading |
---|
633 | FILE *f = (FILE *) l->data; |
---|
634 | fseek(f, 0L, SEEK_END); |
---|
635 | return FALSE; |
---|
636 | } |
---|
637 | } |
---|
638 | } |
---|
639 | |
---|
640 | /*-------------------------------------------------------------------*/ |
---|
641 | void slExtensionInit(si_link_extension s) |
---|
642 | { |
---|
643 | s->index=si_link_root->index+10; |
---|
644 | s->next=si_link_root; |
---|
645 | si_link_root=s; |
---|
646 | } |
---|
647 | |
---|
648 | #ifdef HAVE_DBM |
---|
649 | #include "sing_dbm.h" |
---|
650 | #endif |
---|
651 | |
---|
652 | #ifdef HAVE_MPSR |
---|
653 | #include "sing_mp.h" |
---|
654 | #endif |
---|
655 | |
---|
656 | void slStandardInit() |
---|
657 | { |
---|
658 | si_link_root=(si_link_extension)Alloc0(sizeof(*si_link_root)); |
---|
659 | si_link_root->Init=slInitALink; |
---|
660 | si_link_root->OpenRead=slOpenReadAscii; |
---|
661 | si_link_root->OpenWrite=slOpenWriteAscii; |
---|
662 | si_link_root->Close=slCloseAscii; |
---|
663 | si_link_root->Read=slReadAscii; |
---|
664 | //si_link_root->Read2=NULL; |
---|
665 | si_link_root->Write=slWriteAscii; |
---|
666 | si_link_root->Dump=slDumpAscii; |
---|
667 | si_link_root->GetDump=slGetDumpAscii; |
---|
668 | si_link_root->name="ascii"; |
---|
669 | si_link_root->index=1; |
---|
670 | #ifdef HAVE_DBM |
---|
671 | #ifndef HAVE_MODULE_DBM |
---|
672 | slExtensionInit(dbInit()); |
---|
673 | #endif |
---|
674 | #endif |
---|
675 | #ifdef HAVE_MPSR |
---|
676 | slExtensionInit(slInitMPFile()); |
---|
677 | slExtensionInit(slInitMPTcp()); |
---|
678 | #endif |
---|
679 | } |
---|