source: git/Singular/links/silink.cc @ 4da485

spielwiese
Last change on this file since 4da485 was 4da485, checked in by Hans Schoenemann <hannes@…>, 11 years ago
chg: only external interface to links is links/silink.h
  • Property mode set to 100644
File size: 9.1 KB
RevLine 
[0e1846]1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
[32df82]4
[0e1846]5/*
[6ae4f5]6* ABSTRACT: general interface to links
[d754b7]7*/
[0e1846]8
9#include <stdio.h>
10#include <string.h>
[644227]11#include <sys/types.h>
12#include <sys/stat.h>
13#include <unistd.h>
14
[762407]15#include "config.h"
[b1dfaf]16#include <kernel/mod2.h>
[599326]17#include <Singular/tok.h>
[0fb34ba]18#include <misc/options.h>
[b1dfaf]19#include <omalloc/omalloc.h>
[599326]20#include <kernel/febase.h>
21#include <Singular/subexpr.h>
22#include <Singular/ipid.h>
[44ca2f]23#include <Singular/links/silink.h>
[599326]24#include <Singular/ipshell.h>
[0fb34ba]25#include <polys/matpol.h>
26#include <polys/monomials/ring.h>
[599326]27#include <Singular/lists.h>
28#include <kernel/ideals.h>
[0fb34ba]29#include <coeffs/numbers.h>
30#include <misc/intvec.h>
[47367fd]31#include <Singular/links/ssiLink.h>
32#include <Singular/links/pipeLink.h>
[c90500]33#include "feOpt.h"
[286bd57]34
[0c7cb8]35// #ifdef HAVE_DBM
36// #ifdef ix86_Win
37// #define USE_GDBM
38// #endif
39// #endif
[50cbdc]40
[c232af]41omBin s_si_link_extension_bin = omGetSpecBin(sizeof(s_si_link_extension));
42omBin sip_link_bin = omGetSpecBin(sizeof(sip_link));
43omBin ip_link_bin = omGetSpecBin(sizeof(ip_link));
44
[0e1846]45/* ====================================================================== */
[4da485]46static si_link_extension slTypeInit(si_link_extension s, const char* type);
[0e1846]47si_link_extension si_link_root=NULL;
[d754b7]48
[0e1846]49BOOLEAN slInit(si_link l, char *istr)
50{
[d754b7]51  char *type = NULL, *mode = NULL, *name = NULL;
52  int i = 0, j;
[c20843]53
[d754b7]54  // set mode and type
55  if (istr != NULL)
[0e1846]56  {
[d754b7]57    // find the first colon char in istr
58    i = 0;
59    while (istr[i] != ':' && istr[i] != '\0') i++;
60    if (istr[i] == ':')
[0e1846]61    {
[d754b7]62      // if found, set type
63      if (i > 0)
[0e1846]64      {
[d754b7]65        istr[i] = '\0';
[c232af]66        type = omStrDup(istr);
[d754b7]67        istr[i] = ':';
[0e1846]68      }
[d754b7]69      // and check for mode
70      j = ++i;
71      while (istr[j] != ' ' && istr[j] != '\0') j++;
72      if (j > i)
[0e1846]73      {
[c232af]74        mode = omStrDup(&(istr[i]));
[d754b7]75        mode[j - i] = '\0';
[0e1846]76      }
[d754b7]77      // and for the name
78      while (istr[j] == ' '&& istr[j] != '\0') j++;
[c232af]79      if (istr[j] != '\0') name = omStrDup(&(istr[j]));
[d754b7]80    }
81    else // no colon find -- string is entire name
82    {
83      j=0;
84      while (istr[j] == ' '&& istr[j] != '\0') j++;
[c232af]85      if (istr[j] != '\0') name = omStrDup(&(istr[j]));
[0e1846]86    }
87  }
[e6969d]88
[d754b7]89  // set the link extension
90  if (type != NULL)
[0e1846]91  {
[d754b7]92    si_link_extension s = si_link_root;
[6b32990]93    si_link_extension prev = s;
[c20843]94
[6b32990]95    while (strcmp(s->type, type) != 0)
96    {
[50cbdc]97      if (s->next == NULL)
[6b32990]98      {
99        prev = s;
100        s = NULL;
101        break;
102      }
103      else
104      {
105        s = s->next;
106      }
107    }
[d754b7]108
109    if (s != NULL)
110      l->m = s;
111    else
[0e1846]112    {
[6b32990]113      l->m = slTypeInit(prev, type);
[0e1846]114    }
[c232af]115    omFree(type);
[0e1846]116  }
[d754b7]117  else
118    l->m = si_link_root;
119
[6b32990]120  if (l->m == NULL) return TRUE;
[50cbdc]121
[c232af]122  l->name = (name != NULL ? name : omStrDup(""));
123  l->mode = (mode != NULL ? mode : omStrDup(""));
[d754b7]124  l->ref = 1;
125  return FALSE;
[0e1846]126}
127
[e6969d]128void slCleanUp(si_link l)
[0e1846]129{
[e6969d]130  (l->ref)--;
[d754b7]131  if (l->ref == 0)
[0e1846]132  {
[80419f]133    if (SI_LINK_OPEN_P(l))
134    {
135      if (l->m->Kill != NULL) l->m->Kill(l);
136      else if (l->m->Close != NULL) l->m->Close(l);
137    }
[c232af]138    omFree((ADDRESS)l->name);
139    omFree((ADDRESS)l->mode);
[d754b7]140    memset((void *) l, 0, sizeof(ip_link));
[e6969d]141  }
142}
143
144void slKill(si_link l)
145{
[d754b7]146  slCleanUp(l);
[e6969d]147  if (l->ref == 0)
[c232af]148    omFreeBin((ADDRESS)l,  ip_link_bin);
[0e1846]149}
150
[9db0567]151const char* slStatus(si_link l, const char *request)
[0e1846]152{
[d754b7]153  if (l == NULL) return "empty link";
154  else if (l->m == NULL) return "unknown link type";
155  else if (strcmp(request, "type") == 0) return l->m->type;
156  else if (strcmp(request, "mode") == 0) return l->mode;
157  else if (strcmp(request, "name") == 0) return l->name;
[644227]158  else if (strcmp(request, "exists") ==0)
159  {
160    struct stat buf;
161    if (lstat(l->name,&buf)==0) return "yes";
162    else return "no";
163  }
[d754b7]164  else if (strcmp(request, "open") == 0)
165  {
166    if (SI_LINK_OPEN_P(l)) return "yes";
167    else return "no";
168  }
169  else if (strcmp(request, "openread") == 0)
170  {
171    if (SI_LINK_R_OPEN_P(l)) return "yes";
172    else return "no";
173  }
174  else if (strcmp(request, "openwrite") == 0)
175  {
176    if (SI_LINK_W_OPEN_P(l)) return "yes";
177    else return "no";
178  }
179  else if (l->m->Status == NULL) return "unknown status request";
180  else return l->m->Status(l, request);
[0e1846]181}
182
[d754b7]183//--------------------------------------------------------------------------
[b5f276e]184BOOLEAN slOpen(si_link l, short flag, leftv h)
[0e1846]185{
[47a616d]186  BOOLEAN res = TRUE;
187  if (l!=NULL)
188  {
[c20843]189
[47a616d]190    if (l->m == NULL) slInit(l, ((char*)""));
[d754b7]191
[c90500]192    if (feOptValue(FE_OPT_NO_SHELL)) {WerrorS("no links allowed");return TRUE;}
193
[47a616d]194    const char *c="_";;
195    if (h!=NULL) c=h->Name();
196
197    if (SI_LINK_OPEN_P(l))
198    {
199      Warn("open: link of type: %s, mode: %s, name: %s is already open",
[d754b7]200         l->m->type, l->mode, l->name);
[47a616d]201      return FALSE;
202    }
203    else if (l->m->Open != NULL)
204    {
205      res = l->m->Open(l, flag, h);
206      if (res)
207        Werror("open: Error for link %s of type: %s, mode: %s, name: %s",
208             c, l->m->type, l->mode, l->name);
209    }
[d754b7]210  }
211  return res;
[0e1846]212}
213
[df326b]214BOOLEAN slPrepClose(si_link l)
215{
216
217  if(! SI_LINK_OPEN_P(l))
218    return FALSE;
219
220  BOOLEAN res = TRUE;
221  if (l->m->PrepClose != NULL)
222  {
223    res = l->m->PrepClose(l);
224    if (res)
225      Werror("close: Error for link of type: %s, mode: %s, name: %s",
226           l->m->type, l->mode, l->name);
227  }
228  return res;
229}
230
[0e1846]231BOOLEAN slClose(si_link l)
232{
[d754b7]233
[1fc83c0]234  if(! SI_LINK_OPEN_P(l))
235    return FALSE;
[c20843]236
[47a616d]237  BOOLEAN res = TRUE;
238  if (l->m->Close != NULL)
239  {
240    res = l->m->Close(l);
241    if (res)
242      Werror("close: Error for link of type: %s, mode: %s, name: %s",
[d754b7]243           l->m->type, l->mode, l->name);
[47a616d]244  }
[d754b7]245  return res;
[0e1846]246}
247
[d754b7]248leftv slRead(si_link l, leftv a)
[0e1846]249{
250  leftv v = NULL;
251  if( ! SI_LINK_R_OPEN_P(l)) // open r ?
252  {
[fcafdb]253#ifdef HAVE_DBM
254#ifdef USE_GDBM
255    if (! SI_LINK_CLOSE_P(l))
256      {
[50cbdc]257        if (slClose(l)) return NULL;
[fcafdb]258      }
259#endif
260#endif
[b5f276e]261    if (slOpen(l, SI_LINK_READ,NULL)) return NULL;
[0e1846]262  }
[d754b7]263
264  if (SI_LINK_R_OPEN_P(l))
[0e1846]265  { // open r
266    if (a==NULL)
267    {
[d754b7]268      if (l->m->Read != NULL) v = l->m->Read(l);
[0e1846]269    }
270    else
271    {
[d754b7]272      if (l->m->Read2 != NULL) v = l->m->Read2(l,a);
[0e1846]273    }
274  }
[d754b7]275  else
[0e1846]276  {
[d754b7]277    Werror("read: Error to open link of type %s, mode: %s, name: %s for reading",
278           l->m->type, l->mode, l->name);
279    return NULL;
[0e1846]280  }
[c20843]281
[d754b7]282  // here comes the eval:
[1fc83c0]283  if (v != NULL)
[42e60f]284  {
285    if (v->Eval() && !errorreported)
286      WerrorS("eval: failed");
287  }
[d754b7]288  else
289    Werror("read: Error for link of type %s, mode: %s, name: %s",
290           l->m->type, l->mode, l->name);
[0e1846]291  return v;
292}
293
294BOOLEAN slWrite(si_link l, leftv v)
295{
[d754b7]296  BOOLEAN res;
[c20843]297
[0e1846]298  if(! SI_LINK_W_OPEN_P(l)) // open w ?
299  {
[fcafdb]300#ifdef HAVE_DBM
301#ifdef USE_GDBM
302    if (! SI_LINK_CLOSE_P(l))
303      {
[50cbdc]304        if (slClose(l)) return TRUE;
[fcafdb]305      }
306#endif
307#endif
[b5f276e]308    if (slOpen(l, SI_LINK_WRITE,NULL)) return TRUE;
[0e1846]309  }
[d754b7]310
[fcafdb]311  if (SI_LINK_W_OPEN_P(l))
[0e1846]312  { // now open w
[1fc83c0]313    if (l->m->Write != NULL)
314      res = l->m->Write(l,v);
315    else
316      res = TRUE;
[d754b7]317
318    if (res)
319      Werror("write: Error for link of type %s, mode: %s, name: %s",
320             l->m->type, l->mode, l->name);
321    return res;
322  }
323  else
324  {
325    Werror("write: Error to open link of type %s, mode: %s, name: %s for writing",
326           l->m->type, l->mode, l->name);
327    return TRUE;
[0e1846]328  }
329}
330
[286bd57]331BOOLEAN slDump(si_link l)
332{
[d754b7]333  BOOLEAN res;
334
[286bd57]335  if(! SI_LINK_W_OPEN_P(l)) // open w ?
336  {
[b5f276e]337    if (slOpen(l, SI_LINK_WRITE,NULL)) return TRUE;
[286bd57]338  }
[d754b7]339
340  if(SI_LINK_W_OPEN_P(l))
[286bd57]341  { // now open w
[1fc83c0]342    if (l->m->Dump != NULL)
343      res = l->m->Dump(l);
344    else
345      res = TRUE;
[286bd57]346
[d754b7]347    if (res)
348      Werror("dump: Error for link of type %s, mode: %s, name: %s",
349             l->m->type, l->mode, l->name);
[228e0b]350    if (!SI_LINK_R_OPEN_P(l)) slClose(l); // do not close r/w links
[d754b7]351    return res;
[286bd57]352  }
[d754b7]353  else
354  {
355    Werror("dump: Error to open link of type %s, mode: %s, name: %s for writing",
356           l->m->type, l->mode, l->name);
357    return TRUE;
[286bd57]358  }
359}
360
[d754b7]361BOOLEAN slGetDump(si_link l)
[0e1846]362{
[d754b7]363  BOOLEAN res;
364
365  if(! SI_LINK_R_OPEN_P(l)) // open r ?
[0e1846]366  {
[b5f276e]367    if (slOpen(l, SI_LINK_READ,NULL)) return TRUE;
[0e1846]368  }
[d754b7]369
370  if(SI_LINK_R_OPEN_P(l))
371  { // now open r
[1fc83c0]372    if (l->m->GetDump != NULL)
373      res = l->m->GetDump(l);
374    else
375      res = TRUE;
[d754b7]376
377    if (res)
378      Werror("getdump: Error for link of type %s, mode: %s, name: %s",
379             l->m->type, l->mode, l->name);
[6d1357]380    //res|=slClose(l);
[d754b7]381    return res;
[0e1846]382  }
[d754b7]383  else
[0e1846]384  {
[d754b7]385    Werror("dump: Error open link of type %s, mode: %s, name: %s for reading",
386           l->m->type, l->mode, l->name);
387    return TRUE;
[0e1846]388  }
389}
[d754b7]390
391/*------------Initialization at Start-up time------------------------*/
[0e1846]392
[44ca2f]393#include <Singular/links/slInit.h>
[0e1846]394
[6b32990]395static si_link_extension slTypeInit(si_link_extension s, const char* type)
396{
397  assume(s != NULL);
398  s->next = NULL;
399  si_link_extension ns = (si_link_extension)omAlloc0Bin(s_si_link_extension_bin);
[50cbdc]400
[df326b]401  if (0) 0; // dummy
[6b32990]402#ifdef HAVE_DBM
[8ed989]403  else if (strcmp(type, "DBM") == 0)
[6b32990]404    s->next = slInitDBMExtension(ns);
405#endif
[8ed989]406#if 1
407  else if (strcmp(type, "ssi") == 0)
408    s->next = slInitSsiExtension(ns);
[4082e8]409#endif
410#if 1
411  else if (strcmp(type, "|") == 0)
412    s->next = slInitPipeExtension(ns);
[c20843]413#endif
[8ed989]414  else
[6b32990]415  {
416    Warn("Found unknown link type: %s", type);
417    Warn("Use default link type: %s", si_link_root->type);
418    omFreeBin(ns, s_si_link_extension_bin);
419    return si_link_root;
420  }
[50cbdc]421
[6b32990]422  if (s->next == NULL)
423  {
424    Werror("Can not initialize link type %s", type);
425    omFreeBin(ns, s_si_link_extension_bin);
426    return NULL;
427  }
428  return s->next;
429}
[0e1846]430
Note: See TracBrowser for help on using the repository browser.