source: git/Singular/links/silink.cc @ bdda8c2

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