source: git/Singular/silink.cc @ 3542f7

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