source: git/Singular/links/silink.cc @ 246bbb

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