silink.cc
Go to the documentation of this file.
1 /****************************************
2 * Computer Algebra System SINGULAR *
3 ****************************************/
4 
5 /*
6 * ABSTRACT: general interface to links
7 */
8 
9 #include <kernel/mod2.h>
10 
11 #include <omalloc/omalloc.h>
12 #include <misc/options.h>
13 #include <misc/intvec.h>
14 #include <reporter/si_signals.h>
15 #include <coeffs/numbers.h>
16 
17 #include <polys/matpol.h>
18 #include <polys/monomials/ring.h>
19 
20 #include <kernel/ideals.h>
21 
22 #include <Singular/lists.h>
23 #include <Singular/cntrlc.h>
24 #include <Singular/links/ssiLink.h>
26 #include <Singular/tok.h>
27 #include <Singular/subexpr.h>
28 #include <Singular/ipid.h>
29 #include <Singular/links/silink.h>
30 #include <Singular/ipshell.h>
31 #include "feOpt.h"
32 
33 #include <stdio.h>
34 #include <string.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
38 
39 // #ifdef HAVE_DBM
40 // #ifdef __CYGWIN__
41 // #define USE_GDBM
42 // #endif
43 // #endif
44 
47 omBin ip_link_bin = omGetSpecBin(sizeof(ip_link));
48 
49 /* ====================================================================== */
50 static si_link_extension slTypeInit(si_link_extension s, const char* type);
51 si_link_extension si_link_root=NULL;
52 
53 BOOLEAN slInit(si_link l, char *istr)
54 {
55  char *type = NULL, *mode = NULL, *name = NULL;
56  int i = 0, j;
57 
58  // set mode and type
59  if (istr != NULL)
60  {
61  // find the first colon char in istr
62  i = 0;
63  while (istr[i] != ':' && istr[i] != '\0') i++;
64  if (istr[i] == ':')
65  {
66  // if found, set type
67  if (i > 0)
68  {
69  istr[i] = '\0';
70  type = omStrDup(istr);
71  istr[i] = ':';
72  }
73  // and check for mode
74  j = ++i;
75  while (istr[j] != ' ' && istr[j] != '\0') j++;
76  if (j > i)
77  {
78  mode = omStrDup(&(istr[i]));
79  mode[j - i] = '\0';
80  }
81  // and for the name
82  while (istr[j] == ' '&& istr[j] != '\0') j++;
83  if (istr[j] != '\0') name = omStrDup(&(istr[j]));
84  }
85  else // no colon find -- string is entire name
86  {
87  j=0;
88  while (istr[j] == ' '&& istr[j] != '\0') j++;
89  if (istr[j] != '\0') name = omStrDup(&(istr[j]));
90  }
91  }
92 
93  // set the link extension
94  if (type != NULL)
95  {
96  si_link_extension s = si_link_root;
97  si_link_extension prev = s;
98 
99  while (strcmp(s->type, type) != 0)
100  {
101  if (s->next == NULL)
102  {
103  prev = s;
104  s = NULL;
105  break;
106  }
107  else
108  {
109  s = s->next;
110  }
111  }
112 
113  if (s != NULL)
114  l->m = s;
115  else
116  {
117  l->m = slTypeInit(prev, type);
118  }
119  omFree(type);
120  }
121  else
122  l->m = si_link_root;
123 
124  if (l->m == NULL) return TRUE;
125 
126  l->name = (name != NULL ? name : omStrDup(""));
127  l->mode = (mode != NULL ? mode : omStrDup(""));
128  l->ref = 1;
129  return FALSE;
130 }
131 
133 {
134  defer_shutdown++;
135  (l->ref)--;
136  if (l->ref == 0)
137  {
138  if (SI_LINK_OPEN_P(l))
139  {
140  if (l->m->Close != NULL) l->m->Close(l);
141  }
142  if ((l->data != NULL) && (l->m->Kill != NULL)) l->m->Kill(l);
143  omFree((ADDRESS)l->name);
144  omFree((ADDRESS)l->mode);
145  memset((void *) l, 0, sizeof(ip_link));
146  }
147  defer_shutdown--;
148  if (!defer_shutdown && do_shutdown) m2_end(1);
149 }
150 
152 {
153  defer_shutdown++;
154  slCleanUp(l);
155  if ((l!=NULL) &&(l->ref == 0))
157  defer_shutdown--;
158  if (!defer_shutdown && do_shutdown) m2_end(1);
159 }
160 
161 const char* slStatus(si_link l, const char *request)
162 {
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;
168  else if (strcmp(request, "exists") ==0)
169  {
170  struct stat buf;
171  if (si_lstat(l->name,&buf)==0) return "yes";
172  else return "no";
173  }
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);
191 }
192 
193 //--------------------------------------------------------------------------
195 {
196  if (currRing!=r) rChangeCurrRing(r);
197  return FALSE;
198 }
199 BOOLEAN slOpen(si_link l, short flag, leftv h)
200 {
201  BOOLEAN res = TRUE;
202  if (l!=NULL)
203  {
204 
205  if (l->m == NULL) slInit(l, ((char*)""));
206 
207  if (feOptValue(FE_OPT_NO_SHELL)) {WerrorS("no links allowed");return TRUE;}
208 
209  const char *c="_";;
210  if (h!=NULL) c=h->Name();
211 
212  if (SI_LINK_OPEN_P(l))
213  {
214  Warn("open: link of type: %s, mode: %s, name: %s is already open",
215  l->m->type, l->mode, l->name);
216  return FALSE;
217  }
218  else if (l->m->Open != NULL)
219  {
220  res = l->m->Open(l, flag, h);
221  if (res)
222  Werror("open: Error for link %s of type: %s, mode: %s, name: %s",
223  c, l->m->type, l->mode, l->name);
224  }
225  if (l->m->SetRing==NULL) l->m->SetRing=slSetRingDummy;
226  }
227  return res;
228 }
229 
231 {
232 
233  if(! SI_LINK_OPEN_P(l))
234  return FALSE;
235 
236  BOOLEAN res = TRUE;
237  if (l->m->PrepClose != NULL)
238  {
239  res = l->m->PrepClose(l);
240  if (res)
241  Werror("close: Error for link of type: %s, mode: %s, name: %s",
242  l->m->type, l->mode, l->name);
243  }
244  return res;
245 }
246 
248 {
249 
250  if(! SI_LINK_OPEN_P(l))
251  return FALSE;
252 
253  defer_shutdown++;
254  BOOLEAN res = TRUE;
255  if (l->m->Close != NULL)
256  {
257  res = l->m->Close(l);
258  if (res)
259  Werror("close: Error for link of type: %s, mode: %s, name: %s",
260  l->m->type, l->mode, l->name);
261  }
262  defer_shutdown--;
263  if (!defer_shutdown && do_shutdown) m2_end(1);
265  return res;
266 }
267 
269 {
270  leftv v = NULL;
271  if( ! SI_LINK_R_OPEN_P(l)) // open r ?
272  {
273 #ifdef HAVE_DBM
274 #ifdef USE_GDBM
275  if (! SI_LINK_CLOSE_P(l))
276  {
277  if (slClose(l)) return NULL;
278  }
279 #endif
280 #endif
281  if (slOpen(l, SI_LINK_READ,NULL)) return NULL;
282  }
283 
284  if (SI_LINK_R_OPEN_P(l))
285  { // open r
286  if (a==NULL)
287  {
288  if (l->m->Read != NULL) v = l->m->Read(l);
289  }
290  else
291  {
292  if (l->m->Read2 != NULL) v = l->m->Read2(l,a);
293  }
294  }
295  else
296  {
297  Werror("read: Error to open link of type %s, mode: %s, name: %s for reading",
298  l->m->type, l->mode, l->name);
299  return NULL;
300  }
301 
302  // here comes the eval:
303  if (v != NULL)
304  {
305  if (v->Eval() && !errorreported)
306  WerrorS("eval: failed");
307  }
308  else
309  Werror("read: Error for link of type %s, mode: %s, name: %s",
310  l->m->type, l->mode, l->name);
311  return v;
312 }
313 
315 {
316  BOOLEAN res;
317 
318  if(! SI_LINK_W_OPEN_P(l)) // open w ?
319  {
320 #ifdef HAVE_DBM
321 #ifdef USE_GDBM
322  if (! SI_LINK_CLOSE_P(l))
323  {
324  if (slClose(l)) return TRUE;
325  }
326 #endif
327 #endif
328  if (slOpen(l, SI_LINK_WRITE,NULL)) return TRUE;
329  }
330 
331  if (SI_LINK_W_OPEN_P(l))
332  { // now open w
333  if (l->m->Write != NULL)
334  res = l->m->Write(l,v);
335  else
336  res = TRUE;
337 
338  if (res)
339  Werror("write: Error for link of type %s, mode: %s, name: %s",
340  l->m->type, l->mode, l->name);
341  return res;
342  }
343  else
344  {
345  Werror("write: Error to open link of type %s, mode: %s, name: %s for writing",
346  l->m->type, l->mode, l->name);
347  return TRUE;
348  }
349 }
350 
352 {
353  BOOLEAN res;
354 
355  if(! SI_LINK_W_OPEN_P(l)) // open w ?
356  {
357  if (slOpen(l, SI_LINK_WRITE,NULL)) return TRUE;
358  }
359 
360  if(SI_LINK_W_OPEN_P(l))
361  { // now open w
362  if (l->m->Dump != NULL)
363  res = l->m->Dump(l);
364  else
365  res = TRUE;
366 
367  if (res)
368  Werror("dump: Error for link of type %s, mode: %s, name: %s",
369  l->m->type, l->mode, l->name);
370  if (!SI_LINK_R_OPEN_P(l)) slClose(l); // do not close r/w links
371  return res;
372  }
373  else
374  {
375  Werror("dump: Error to open link of type %s, mode: %s, name: %s for writing",
376  l->m->type, l->mode, l->name);
377  return TRUE;
378  }
379 }
380 
382 {
383  BOOLEAN res;
384 
385  if(! SI_LINK_R_OPEN_P(l)) // open r ?
386  {
387  if (slOpen(l, SI_LINK_READ,NULL)) return TRUE;
388  }
389 
390  if(SI_LINK_R_OPEN_P(l))
391  { // now open r
392  if (l->m->GetDump != NULL)
393  res = l->m->GetDump(l);
394  else
395  res = TRUE;
396 
397  if (res)
398  Werror("getdump: Error for link of type %s, mode: %s, name: %s",
399  l->m->type, l->mode, l->name);
400  //res|=slClose(l);
401  return res;
402  }
403  else
404  {
405  Werror("dump: Error open link of type %s, mode: %s, name: %s for reading",
406  l->m->type, l->mode, l->name);
407  return TRUE;
408  }
409 }
410 
411 /*------------Initialization at Start-up time------------------------*/
412 
413 #include <Singular/links/slInit.h>
414 
415 static si_link_extension slTypeInit(si_link_extension s, const char* type)
416 {
417  assume(s != NULL);
418  s->next = NULL;
419  si_link_extension ns = (si_link_extension)omAlloc0Bin(s_si_link_extension_bin);
420 
421  if (0)
422  ; // dummy
423 #ifdef HAVE_DBM
424  else if (strcmp(type, "DBM") == 0)
425  s->next = slInitDBMExtension(ns);
426 #endif
427 #if 1
428  else if (strcmp(type, "ssi") == 0)
429  s->next = slInitSsiExtension(ns);
430 #endif
431 #if 1
432  else if (strcmp(type, "|") == 0)
433  s->next = slInitPipeExtension(ns);
434 #endif
435  else
436  {
437  Warn("Found unknown link type: %s", type);
438  Warn("Use default link type: %s", si_link_root->type);
440  return si_link_root;
441  }
442 
443  if (s->next == NULL)
444  {
445  Werror("Can not initialize link type %s", type);
447  return NULL;
448  }
449  return s->next;
450 }
451 
const CanonicalForm int s
Definition: facAbsFact.cc:55
Class used for (list of) interpreter objects.
Definition: subexpr.h:82
const poly a
Definition: syzextra.cc:212
omBin_t * omBin
Definition: omStructs.h:12
static void * feOptValue(feOptIndex opt)
Definition: feOpt.h:40
#define FALSE
Definition: auxiliary.h:94
void m2_end(int i)
Definition: misc_ip.cc:1074
#define TRUE
Definition: auxiliary.h:98
void * ADDRESS
Definition: auxiliary.h:115
void WerrorS(const char *s)
Definition: feFopen.cc:24
poly res
Definition: myNF.cc:322
ring currRing
Widely used global variable which specifies the current polynomial ring for Singular interpreter and ...
Definition: polys.cc:10
const ring r
Definition: syzextra.cc:208
volatile BOOLEAN do_shutdown
Definition: cntrlc.cc:83
int j
Definition: myNF.cc:70
#define omFree(addr)
Definition: omAllocDecl.h:261
#define assume(x)
Definition: mod2.h:394
int status int void * buf
Definition: si_signals.h:59
int i
Definition: cfEzgcd.cc:123
char name(const Variable &v)
Definition: factory.h:178
short errorreported
Definition: feFopen.cc:23
void rChangeCurrRing(ring r)
Definition: polys.cc:12
#define omAlloc0Bin(bin)
Definition: omAllocDecl.h:206
#define omGetSpecBin(size)
Definition: omBin.h:11
const Variable & v
< [in] a sqrfree bivariate poly
Definition: facBivar.h:37
#define NULL
Definition: omList.c:10
si_link_extension slInitDBMExtension(si_link_extension s)
volatile int defer_shutdown
Definition: cntrlc.cc:84
#define omFreeBin(addr, bin)
Definition: omAllocDecl.h:259
static Poly * h
Definition: janet.cc:978
int BOOLEAN
Definition: auxiliary.h:85
void Werror(const char *fmt,...)
Definition: reporter.cc:189
int l
Definition: cfEzgcd.cc:94
#define Warn
Definition: emacs.cc:80
#define omStrDup(s)
Definition: omAllocDecl.h:263