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