My Project
Loading...
Searching...
No Matches
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"
18
19#include "kernel/ideals.h"
20
21#include "Singular/lists.h"
22#include "Singular/cntrlc.h"
25#include "Singular/tok.h"
26#include "Singular/subexpr.h"
27#include "Singular/ipid.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
41VAR omBin ip_link_bin = omGetSpecBin(sizeof(ip_link));
43
44/* ====================================================================== */
45static si_link_extension slTypeInit(si_link_extension s, const char* type);
46VAR si_link_extension si_link_root=NULL;
47
48BOOLEAN slInit(si_link l, char *istr)
49{
50 char *type = NULL, *mode = NULL, *name = NULL;
51 int i = 0, j;
52
53 // set mode and type
54 if (istr != NULL)
55 {
56 // find the first colon char in istr
57 i = 0;
58 while (istr[i] != ':' && istr[i] != '\0') i++;
59 if (istr[i] == ':')
60 {
61 // if found, set type
62 if (i > 0)
63 {
64 istr[i] = '\0';
65 type = omStrDup(istr);
66 istr[i] = ':';
67 }
68 // and check for mode
69 j = ++i;
70 while (istr[j] != ' ' && istr[j] != '\0') j++;
71 if (j > i)
72 {
73 mode = omStrDup(&(istr[i]));
74 mode[j - i] = '\0';
75 }
76 // and for the name
77 while (istr[j] == ' '&& istr[j] != '\0') j++;
78 if (istr[j] != '\0') name = omStrDup(&(istr[j]));
79 }
80 else // no colon find -- string is entire name
81 {
82 j=0;
83 while (istr[j] == ' '&& istr[j] != '\0') j++;
84 if (istr[j] != '\0') name = omStrDup(&(istr[j]));
85 }
86 }
87
88 // set the link extension
89 if (type != NULL)
90 {
91 si_link_extension s = si_link_root;
92 si_link_extension prev = s;
93
94 while (strcmp(s->type, type) != 0)
95 {
96 if (s->next == NULL)
97 {
98 prev = s;
99 s = NULL;
100 break;
101 }
102 else
103 {
104 s = s->next;
105 }
106 }
107
108 if (s != NULL)
109 l->m = s;
110 else
111 {
112 l->m = slTypeInit(prev, type);
113 }
114 omFree(type);
115 }
116 else
117 l->m = si_link_root;
118
119 if (l->m == NULL) return TRUE;
120
121 l->name = (name != NULL ? name : omStrDup(""));
122 l->mode = (mode != NULL ? mode : omStrDup(""));
123 l->ref = 1;
124 return FALSE;
125}
126
128{
130 (l->ref)--;
131 if (l->ref == 0)
132 {
133 if (SI_LINK_OPEN_P(l))
134 {
135 if (l->m->Close != NULL) l->m->Close(l);
136 }
137 if ((l->data != NULL) && (l->m->Kill != NULL)) l->m->Kill(l);
138 omFree((ADDRESS)l->name);
139 omFree((ADDRESS)l->mode);
140 memset((void *) l, 0, sizeof(ip_link));
141 }
144}
145
147{
149 slCleanUp(l);
150 if ((l!=NULL) &&(l->ref == 0))
154}
155
156const char* slStatus(si_link l, const char *request)
157{
158 if (l == NULL) return "empty link";
159 else if (l->m == NULL) return "unknown link type";
160 else if (strcmp(request, "type") == 0) return l->m->type;
161 else if (strcmp(request, "mode") == 0) return l->mode;
162 else if (strcmp(request, "name") == 0) return l->name;
163 else if (strcmp(request, "exists") ==0)
164 {
165 struct stat buf;
166 if (si_lstat(l->name,&buf)==0) return "yes";
167 else return "no";
168 }
169 else if (strcmp(request, "open") == 0)
170 {
171 if (SI_LINK_OPEN_P(l)) return "yes";
172 else return "no";
173 }
174 else if (strcmp(request, "openread") == 0)
175 {
176 if (SI_LINK_R_OPEN_P(l)) return "yes";
177 else return "no";
178 }
179 else if (strcmp(request, "openwrite") == 0)
180 {
181 if (SI_LINK_W_OPEN_P(l)) return "yes";
182 else return "no";
183 }
184 else if (l->m->Status == NULL) return "unknown status request";
185 else return l->m->Status(l, request);
186}
187
188//--------------------------------------------------------------------------
190{
191 if (currRing!=r) rChangeCurrRing(r);
192 return FALSE;
193}
195{
196 BOOLEAN res = TRUE;
197 if (l!=NULL)
198 {
199
200 if (l->m == NULL) slInit(l, ((char*)""));
201
202 const char *c="_";;
203 if (h!=NULL) c=h->Name();
204
205 if (SI_LINK_OPEN_P(l))
206 {
207 Warn("open: link of type: %s, mode: %s, name: %s is already open",
208 l->m->type, l->mode, l->name);
209 return FALSE;
210 }
211 else if (l->m->Open != NULL)
212 {
213 res = l->m->Open(l, flag, h);
214 if (res)
215 Werror("open: Error for link %s of type: %s, mode: %s, name: %s",
216 c, l->m->type, l->mode, l->name);
217 }
218 if (l->m->SetRing==NULL) l->m->SetRing=slSetRingDummy;
219 }
220 return res;
221}
222
224{
225
226 if(! SI_LINK_OPEN_P(l))
227 return FALSE;
228
229 BOOLEAN res = TRUE;
230 if (l->m->PrepClose != NULL)
231 {
232 res = l->m->PrepClose(l);
233 if (res)
234 Werror("close: Error for link of type: %s, mode: %s, name: %s",
235 l->m->type, l->mode, l->name);
236 }
237 return res;
238}
239
241{
242
243 if(! SI_LINK_OPEN_P(l))
244 return FALSE;
245
247 BOOLEAN res = TRUE;
248 if (l->m->Close != NULL)
249 {
250 res = l->m->Close(l);
251 if (res)
252 Werror("close: Error for link of type: %s, mode: %s, name: %s",
253 l->m->type, l->mode, l->name);
254 }
258 return res;
259}
260
262{
263 leftv v = NULL;
264 if( ! SI_LINK_R_OPEN_P(l)) // open r ?
265 {
266#ifdef HAVE_DBM
267#ifdef USE_GDBM
268 if (! SI_LINK_CLOSE_P(l))
269 {
270 if (slClose(l)) return NULL;
271 }
272#endif
273#endif
274 if (slOpen(l, SI_LINK_READ,NULL)) return NULL;
275 }
276
277 if (SI_LINK_R_OPEN_P(l))
278 { // open r
279 if (a==NULL)
280 {
281 if (l->m->Read != NULL) v = l->m->Read(l);
282 }
283 else
284 {
285 if (l->m->Read2 != NULL) v = l->m->Read2(l,a);
286 }
287 }
288 else
289 {
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;
293 }
294
295 // here comes the eval:
296 if (v != NULL)
297 {
298 if (v->Eval() && !errorreported)
299 WerrorS("eval: failed");
300 }
301 else
302 Werror("read: Error for link of type %s, mode: %s, name: %s",
303 l->m->type, l->mode, l->name);
304 return v;
305}
306
308{
309 BOOLEAN res;
310
311 if(! SI_LINK_W_OPEN_P(l)) // open w ?
312 {
313#ifdef HAVE_DBM
314#ifdef USE_GDBM
315 if (! SI_LINK_CLOSE_P(l))
316 {
317 if (slClose(l)) return TRUE;
318 }
319#endif
320#endif
321 if (slOpen(l, SI_LINK_WRITE,NULL)) return TRUE;
322 }
323
324 if (SI_LINK_W_OPEN_P(l))
325 { // now open w
326 if (l->m->Write != NULL)
327 res = l->m->Write(l,v);
328 else
329 res = TRUE;
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;
341 }
342}
343
345{
346 BOOLEAN res;
347
348 if(! SI_LINK_W_OPEN_P(l)) // open w ?
349 {
350 if (slOpen(l, SI_LINK_WRITE,NULL)) return TRUE;
351 }
352
354 { // now open w
355 if (l->m->Dump != NULL)
356 res = l->m->Dump(l);
357 else
358 res = TRUE;
359
360 if (res)
361 Werror("dump: Error for link of type %s, mode: %s, name: %s",
362 l->m->type, l->mode, l->name);
363 if (!SI_LINK_R_OPEN_P(l)) slClose(l); // do not close r/w links
364 return res;
365 }
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;
371 }
372}
373
375{
376 BOOLEAN res;
377
378 if(! SI_LINK_R_OPEN_P(l)) // open r ?
379 {
380 if (slOpen(l, SI_LINK_READ,NULL)) return TRUE;
381 }
382
384 { // now open r
385 if (l->m->GetDump != NULL)
386 res = l->m->GetDump(l);
387 else
388 res = TRUE;
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);
393 //res|=slClose(l);
394 return res;
395 }
396 else
397 {
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;
401 }
402}
403
404/*------------Initialization at Start-up time------------------------*/
405
406
407static si_link_extension slTypeInit(si_link_extension s, const char* type)
408{
409 assume(s != NULL);
410 s->next = NULL;
411 si_link_extension ns = (si_link_extension)omAlloc0Bin(s_si_link_extension_bin);
412
413 if (0)
414 ; // dummy
415#ifdef HAVE_DBM
416 else if (strcmp(type, "DBM") == 0)
417 s->next = slInitDBMExtension(ns);
418#endif
419#if 1
420 else if (strcmp(type, "ssi") == 0)
421 s->next = slInitSsiExtension(ns);
422#endif
423#if 1
424 else if (strcmp(type, "|") == 0)
425 s->next = slInitPipeExtension(ns);
426#endif
427 else
428 {
429 Warn("Found unknown link type: %s", type);
430 Warn("Use default link type: %s", si_link_root->type);
432 return si_link_root;
433 }
434
435 if (s->next == NULL)
436 {
437 Werror("Can not initialize link type %s", type);
439 return NULL;
440 }
441 return s->next;
442}
443
int BOOLEAN
Definition auxiliary.h:88
#define TRUE
Definition auxiliary.h:101
#define FALSE
Definition auxiliary.h:97
void * ADDRESS
Definition auxiliary.h:120
int l
Definition cfEzgcd.cc:100
int i
Definition cfEzgcd.cc:132
VAR volatile BOOLEAN do_shutdown
Definition cntrlc.cc:74
VAR volatile int defer_shutdown
Definition cntrlc.cc:75
#define Warn
Definition emacs.cc:77
EXTERN_VAR BOOLEAN FE_OPT_NO_SHELL_FLAG
Definition extra.cc:170
const CanonicalForm int s
Definition facAbsFact.cc:51
CanonicalForm res
Definition facAbsFact.cc:60
const Variable & v
< [in] a sqrfree bivariate poly
Definition facBivar.h:39
int j
Definition facHensel.cc:110
VAR short errorreported
Definition feFopen.cc:23
void WerrorS(const char *s)
Definition feFopen.cc:24
#define GLOBAL_VAR
Definition globaldefs.h:11
#define VAR
Definition globaldefs.h:5
STATIC_VAR Poly * h
Definition janet.cc:971
#define assume(x)
Definition mod2.h:389
void m2_end(int i)
Definition misc_ip.cc:1102
#define omStrDup(s)
#define omAlloc0Bin(bin)
#define omFree(addr)
#define omFreeBin(addr, bin)
#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:16
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:69
si_link_extension slInitDBMExtension(si_link_extension s)
sleftv * leftv
Definition structs.h:53
int name
New type name for int.