source: git/Singular/silink.cc @ 80419f

spielwiese
Last change on this file since 80419f was 80419f, checked in by Olaf Bachmann <obachman@…>, 26 years ago
1998-10-14 Olaf Bachmann <obachman@mathematik.uni-kl.de> * sing_mp.cc (slOpenMPLaunch): added slKill as link function * added --MPrsh to slOpenLaunch git-svn-id: file:///usr/local/Singular/svn/trunk@2563 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 16.6 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: silink.cc,v 1.23 1998-10-14 10:18:54 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 */
27static BOOLEAN DumpAscii(FILE *fd, idhdl h);
28static BOOLEAN DumpAsciiIdhdl(FILE *fd, idhdl h);
29static char* GetIdString(idhdl h);
30static int DumpRhs(FILE *fd, idhdl h);
31static BOOLEAN DumpQring(FILE *fd, idhdl h, char *type_str);
32static BOOLEAN DumpAsciiMaps(FILE *fd, idhdl h, idhdl rhdl);
33
34/* ====================================================================== */
35si_link_extension si_link_root=NULL;
36
37BOOLEAN 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
103void 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
119void slKill(si_link l)
120{
121  slCleanUp(l);
122  if (l->ref == 0)
123    Free((ADDRESS)l, sizeof(ip_link));
124}
125
126char* 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//--------------------------------------------------------------------------
153BOOLEAN 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
176BOOLEAN 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
193leftv 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
231BOOLEAN 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
260BOOLEAN 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
289BOOLEAN 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 ============================================= */
320BOOLEAN 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
381BOOLEAN 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
391leftv 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}
427BOOLEAN 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
452char* 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
469BOOLEAN 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
486static 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    return FALSE;
503}
504
505static BOOLEAN DumpAsciiMaps(FILE *fd, idhdl h, idhdl rhdl)
506{
507  if (h == NULL) return FALSE;
508  if (DumpAsciiMaps(fd, IDNEXT(h), rhdl)) return TRUE;
509
510  if (IDTYP(h) == RING_CMD || IDTYP(h) == QRING_CMD)
511    return DumpAsciiMaps(fd, IDRING(h)->idroot, h);
512  else if (IDTYP(h) == MAP_CMD)
513  {
514    char *rhs;
515    rSetHdl(rhdl, TRUE);
516    rhs = ((leftv) h)->String();
517
518    if (fprintf(fd, "setring %s;\n", IDID(rhdl)) == EOF) return TRUE;
519    if (fprintf(fd, "%s %s = %s, %s;\n", Tok2Cmdname(MAP_CMD), IDID(h),
520                IDMAP(h)->preimage, rhs) == EOF)
521    {
522      FreeL(rhs);
523      return TRUE;
524    }
525    else
526    {
527      FreeL(rhs);
528      return FALSE;
529    }
530  }
531  else return FALSE;
532}
533
534static BOOLEAN DumpAsciiIdhdl(FILE *fd, idhdl h)
535{
536  char *type_str = GetIdString(h);
537  idtyp type_id = IDTYP(h);
538
539  // we do not throw an error if a wrong type was attempted to be dumped
540  if (type_str == NULL) return FALSE;
541
542  // handle qrings separately
543  if (type_id == QRING_CMD) return DumpQring(fd, h, type_str);
544
545  // do not dump LIB string
546  if (type_id == STRING_CMD && strcmp("LIB", IDID(h)) == 0)
547  {
548    return FALSE;
549  }
550
551  // put type and name
552  if (fprintf(fd, "%s %s", type_str, IDID(h)) == EOF) return TRUE;
553
554  // for matricies, append the dimension
555  if (type_id == MATRIX_CMD)
556  {
557    ideal id = IDIDEAL(h);
558    if (fprintf(fd, "[%d][%d]", id->nrows, id->ncols)== EOF) return TRUE;
559  }
560  else if (type_id == INTMAT_CMD)
561  {
562    if (fprintf(fd, "[%d][%d]", IDINTVEC(h)->rows(), IDINTVEC(h)->cols())
563        == EOF) return TRUE;
564  }
565  // write the equal sign
566  if (fprintf(fd, " = ") == EOF) return TRUE;
567
568  // and the right hand side
569  if (DumpRhs(fd, h) == EOF) return TRUE;
570
571  // semicolon und tschuess
572  if (fprintf(fd, ";\n") == EOF) return TRUE;
573
574  return FALSE;
575}
576
577static char* GetIdString(idhdl h)
578{
579  idtyp type = IDTYP(h);
580
581  switch(type)
582  {
583      case LIST_CMD:
584      {
585        lists l = IDLIST(h);
586        int i, nl = l->nr + 1;
587        char *name;
588
589        for (i=0; i<nl; i++)
590          if (GetIdString((idhdl) &(l->m[i])) == NULL) return NULL;
591      }
592      case INT_CMD:
593      case INTVEC_CMD:
594      case INTMAT_CMD:
595      case STRING_CMD:
596      case RING_CMD:
597      case QRING_CMD:
598      case PROC_CMD:
599      case NUMBER_CMD:
600      case POLY_CMD:
601      case IDEAL_CMD:
602      case VECTOR_CMD:
603      case MODUL_CMD:
604      case MATRIX_CMD:
605        return Tok2Cmdname(type);
606
607      case MAP_CMD:
608      case LINK_CMD:
609        return NULL;
610
611      default:
612       Warn("Error dump data of type %s", Tok2Cmdname(IDTYP(h)));
613       return NULL;
614  }
615}
616
617static BOOLEAN DumpQring(FILE *fd, idhdl h, char *type_str)
618{
619  char *ring_str = ((leftv) h)->String();
620  if (fprintf(fd, "%s temp_ring = %s;\n", Tok2Cmdname(RING_CMD), ring_str)
621              == EOF) return TRUE;
622  if (fprintf(fd, "%s temp_ideal = %s;\n", Tok2Cmdname(IDEAL_CMD),
623              iiStringMatrix((matrix) IDRING(h)->qideal, 1))
624      == EOF) return TRUE;
625  if (fprintf(fd, "attrib(temp_ideal, \"isSB\", 1);\n") == EOF) return TRUE;
626  if (fprintf(fd, "%s %s = temp_ideal;\n", type_str, IDID(h)) == EOF)
627    return TRUE;
628  if (fprintf(fd, "kill temp_ring;\n") == EOF) return TRUE;
629  else
630  {
631    FreeL(ring_str);
632    return FALSE;
633  }
634}
635
636
637static int DumpRhs(FILE *fd, idhdl h)
638{
639  idtyp type_id = IDTYP(h);
640
641  if (type_id == LIST_CMD)
642  {
643    lists l = IDLIST(h);
644    int i, nl = l->nr;
645
646    fprintf(fd, "list(");
647
648    for (i=0; i<nl; i++)
649    {
650      if (DumpRhs(fd, (idhdl) &(l->m[i])) == EOF) return EOF;
651      fprintf(fd, ",");
652    }
653    if (nl > 0)
654    {
655      if (DumpRhs(fd, (idhdl) &(l->m[nl])) == EOF) return EOF;
656    }
657    fprintf(fd, ")");
658  }
659  else  if (type_id == STRING_CMD)
660  {
661    char *pstr = IDSTRING(h), c;
662    fputc('"', fd);
663    while (*pstr != '\0')
664    {
665      if (*pstr == '"' || *pstr == '\\')  fputc('\\', fd);
666      fputc(*pstr, fd);
667      pstr++;
668    }
669    fputc('"', fd);
670  }
671  else  if (type_id == PROC_CMD)
672  {
673    procinfov pi = IDPROC(h);
674    if (pi->language == LANG_SINGULAR) {
675      if( pi->data.s.body==NULL) iiGetLibProcBuffer(pi);
676      char *pstr = pi->data.s.body, c;
677      fputc('"', fd);
678      while (*pstr != '\0') {
679        if (*pstr == '"' || *pstr == '\\') fputc('\\', fd);
680        fputc(*pstr, fd);
681        pstr++;
682      }
683      fputc('"', fd);
684    } else fputs("(null)", fd);
685  }
686  else
687  {
688    char *rhs = ((leftv) h)->String();
689
690    if (rhs == NULL) return EOF;
691
692    if (type_id == INTVEC_CMD) fprintf(fd, "intvec(");
693
694    if (fprintf(fd, "%s", rhs) == EOF) return EOF;
695    FreeL(rhs);
696
697    if ((type_id == RING_CMD || type_id == QRING_CMD) &&
698        IDRING(h)->minpoly != NULL)
699    {
700      StringSetS("");
701      nWrite(IDRING(h)->minpoly);
702      rhs = StringAppend("");
703      if (fprintf(fd, "; minpoly = %s", rhs) == EOF) return EOF;
704    }
705    else if (type_id == INTVEC_CMD) fprintf(fd, ")");
706  }
707  return 1;
708}
709
710BOOLEAN slGetDumpAscii(si_link l)
711{
712  if (l->name[0] == '\0')
713  {
714    Werror("getdump: Can not get dump from stdin");
715    return TRUE;
716  }
717  else
718  {
719    BOOLEAN status = newFile(l->name);
720    if (status)
721      return TRUE;
722
723    int old_echo=si_echo;
724    si_echo=0;
725
726    status=yyparse();
727
728    si_echo=old_echo;
729
730    if (status)
731      return TRUE;
732    else
733    {
734      // lets reset the file pointer to the end to reflect that
735      // we are finished with reading
736      FILE *f = (FILE *) l->data;
737      fseek(f, 0L, SEEK_END);
738      return FALSE;
739    }
740  }
741}
742
743
744/*------------Initialization at Start-up time------------------------*/
745
746#ifdef HAVE_DBM
747#include "sing_dbm.h"
748#endif
749
750#ifdef HAVE_MPSR
751#include "sing_mp.h"
752#endif
753
754void slStandardInit()
755{
756  si_link_extension s;
757  si_link_root=(si_link_extension)Alloc0(sizeof(*si_link_root));
758  si_link_root->Open=slOpenAscii;
759  si_link_root->Close=slCloseAscii;
760  si_link_root->Kill=slCloseAscii;
761  si_link_root->Read=slReadAscii;
762  si_link_root->Write=slWriteAscii;
763  si_link_root->Dump=slDumpAscii;
764  si_link_root->GetDump=slGetDumpAscii;
765  si_link_root->Status=slStatusAscii;
766  si_link_root->type="ASCII";
767  s = si_link_root;
768#ifdef HAVE_DBM
769#ifndef HAVE_MODULE_DBM
770  s->next = (si_link_extension)Alloc0(sizeof(*si_link_root));
771  s = s->next;
772  slInitDBMExtension(s);
773#endif
774#endif
775#ifdef HAVE_MPSR
776  s->next = (si_link_extension)Alloc0(sizeof(*si_link_root));
777  s = s->next;
778  slInitMPFileExtension(s);
779  s->next = (si_link_extension)Alloc0(sizeof(*si_link_root));
780  s = s->next;
781  slInitMPTcpExtension(s);
782#endif
783}
Note: See TracBrowser for help on using the repository browser.