source: git/Singular/links/silink.cc @ 6b288b

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