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