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" |
---|
24 | #include "Singular/links/pipeLink.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 | |
---|
39 | VAR omBin s_si_link_extension_bin = omGetSpecBin(sizeof(s_si_link_extension)); |
---|
40 | VAR omBin sip_link_bin = omGetSpecBin(sizeof(sip_link)); |
---|
41 | VAR omBin ip_link_bin = omGetSpecBin(sizeof(ip_link)); |
---|
42 | GLOBAL_VAR BOOLEAN FE_OPT_NO_SHELL_FLAG; |
---|
43 | |
---|
44 | /* ====================================================================== */ |
---|
45 | static si_link_extension slTypeInit(si_link_extension s, const char* type); |
---|
46 | VAR si_link_extension si_link_root=NULL; |
---|
47 | |
---|
48 | BOOLEAN 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 | |
---|
127 | void slCleanUp(si_link l) |
---|
128 | { |
---|
129 | defer_shutdown++; |
---|
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 | } |
---|
142 | defer_shutdown--; |
---|
143 | if (!defer_shutdown && do_shutdown) m2_end(1); |
---|
144 | } |
---|
145 | |
---|
146 | void slKill(si_link l) |
---|
147 | { |
---|
148 | defer_shutdown++; |
---|
149 | slCleanUp(l); |
---|
150 | if ((l!=NULL) &&(l->ref == 0)) |
---|
151 | omFreeBin((ADDRESS)l, ip_link_bin); |
---|
152 | defer_shutdown--; |
---|
153 | if (!defer_shutdown && do_shutdown) m2_end(1); |
---|
154 | } |
---|
155 | |
---|
156 | const 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 | //-------------------------------------------------------------------------- |
---|
189 | BOOLEAN slSetRingDummy(si_link, ring r, BOOLEAN) |
---|
190 | { |
---|
191 | if (currRing!=r) rChangeCurrRing(r); |
---|
192 | return FALSE; |
---|
193 | } |
---|
194 | BOOLEAN slOpen(si_link l, short flag, leftv h) |
---|
195 | { |
---|
196 | BOOLEAN res = TRUE; |
---|
197 | if (l!=NULL) |
---|
198 | { |
---|
199 | |
---|
200 | if (l->m == NULL) slInit(l, ((char*)"")); |
---|
201 | |
---|
202 | if (FE_OPT_NO_SHELL_FLAG) {WerrorS("no links allowed");return TRUE;} |
---|
203 | |
---|
204 | const char *c="_";; |
---|
205 | if (h!=NULL) c=h->Name(); |
---|
206 | |
---|
207 | if (SI_LINK_OPEN_P(l)) |
---|
208 | { |
---|
209 | Warn("open: link of type: %s, mode: %s, name: %s is already open", |
---|
210 | l->m->type, l->mode, l->name); |
---|
211 | return FALSE; |
---|
212 | } |
---|
213 | else if (l->m->Open != NULL) |
---|
214 | { |
---|
215 | res = l->m->Open(l, flag, h); |
---|
216 | if (res) |
---|
217 | Werror("open: Error for link %s of type: %s, mode: %s, name: %s", |
---|
218 | c, l->m->type, l->mode, l->name); |
---|
219 | } |
---|
220 | if (l->m->SetRing==NULL) l->m->SetRing=slSetRingDummy; |
---|
221 | } |
---|
222 | return res; |
---|
223 | } |
---|
224 | |
---|
225 | BOOLEAN slPrepClose(si_link l) |
---|
226 | { |
---|
227 | |
---|
228 | if(! SI_LINK_OPEN_P(l)) |
---|
229 | return FALSE; |
---|
230 | |
---|
231 | BOOLEAN res = TRUE; |
---|
232 | if (l->m->PrepClose != NULL) |
---|
233 | { |
---|
234 | res = l->m->PrepClose(l); |
---|
235 | if (res) |
---|
236 | Werror("close: Error for link of type: %s, mode: %s, name: %s", |
---|
237 | l->m->type, l->mode, l->name); |
---|
238 | } |
---|
239 | return res; |
---|
240 | } |
---|
241 | |
---|
242 | BOOLEAN slClose(si_link l) |
---|
243 | { |
---|
244 | |
---|
245 | if(! SI_LINK_OPEN_P(l)) |
---|
246 | return FALSE; |
---|
247 | |
---|
248 | defer_shutdown++; |
---|
249 | BOOLEAN res = TRUE; |
---|
250 | if (l->m->Close != NULL) |
---|
251 | { |
---|
252 | res = l->m->Close(l); |
---|
253 | if (res) |
---|
254 | Werror("close: Error for link of type: %s, mode: %s, name: %s", |
---|
255 | l->m->type, l->mode, l->name); |
---|
256 | } |
---|
257 | defer_shutdown--; |
---|
258 | if (!defer_shutdown && do_shutdown) m2_end(1); |
---|
259 | SI_LINK_SET_CLOSE_P(l); |
---|
260 | return res; |
---|
261 | } |
---|
262 | |
---|
263 | leftv slRead(si_link l, leftv a) |
---|
264 | { |
---|
265 | leftv v = NULL; |
---|
266 | if( ! SI_LINK_R_OPEN_P(l)) // open r ? |
---|
267 | { |
---|
268 | #ifdef HAVE_DBM |
---|
269 | #ifdef USE_GDBM |
---|
270 | if (! SI_LINK_CLOSE_P(l)) |
---|
271 | { |
---|
272 | if (slClose(l)) return NULL; |
---|
273 | } |
---|
274 | #endif |
---|
275 | #endif |
---|
276 | if (slOpen(l, SI_LINK_READ,NULL)) return NULL; |
---|
277 | } |
---|
278 | |
---|
279 | if (SI_LINK_R_OPEN_P(l)) |
---|
280 | { // open r |
---|
281 | if (a==NULL) |
---|
282 | { |
---|
283 | if (l->m->Read != NULL) v = l->m->Read(l); |
---|
284 | } |
---|
285 | else |
---|
286 | { |
---|
287 | if (l->m->Read2 != NULL) v = l->m->Read2(l,a); |
---|
288 | } |
---|
289 | } |
---|
290 | else |
---|
291 | { |
---|
292 | Werror("read: Error to open link of type %s, mode: %s, name: %s for reading", |
---|
293 | l->m->type, l->mode, l->name); |
---|
294 | return NULL; |
---|
295 | } |
---|
296 | |
---|
297 | // here comes the eval: |
---|
298 | if (v != NULL) |
---|
299 | { |
---|
300 | if (v->Eval() && !errorreported) |
---|
301 | WerrorS("eval: failed"); |
---|
302 | } |
---|
303 | else |
---|
304 | Werror("read: Error for link of type %s, mode: %s, name: %s", |
---|
305 | l->m->type, l->mode, l->name); |
---|
306 | return v; |
---|
307 | } |
---|
308 | |
---|
309 | BOOLEAN slWrite(si_link l, leftv v) |
---|
310 | { |
---|
311 | BOOLEAN res; |
---|
312 | |
---|
313 | if(! SI_LINK_W_OPEN_P(l)) // open w ? |
---|
314 | { |
---|
315 | #ifdef HAVE_DBM |
---|
316 | #ifdef USE_GDBM |
---|
317 | if (! SI_LINK_CLOSE_P(l)) |
---|
318 | { |
---|
319 | if (slClose(l)) return TRUE; |
---|
320 | } |
---|
321 | #endif |
---|
322 | #endif |
---|
323 | if (slOpen(l, SI_LINK_WRITE,NULL)) return TRUE; |
---|
324 | } |
---|
325 | |
---|
326 | if (SI_LINK_W_OPEN_P(l)) |
---|
327 | { // now open w |
---|
328 | if (l->m->Write != NULL) |
---|
329 | res = l->m->Write(l,v); |
---|
330 | else |
---|
331 | res = TRUE; |
---|
332 | |
---|
333 | if (res) |
---|
334 | Werror("write: Error for link of type %s, mode: %s, name: %s", |
---|
335 | l->m->type, l->mode, l->name); |
---|
336 | return res; |
---|
337 | } |
---|
338 | else |
---|
339 | { |
---|
340 | Werror("write: Error to open link of type %s, mode: %s, name: %s for writing", |
---|
341 | l->m->type, l->mode, l->name); |
---|
342 | return TRUE; |
---|
343 | } |
---|
344 | } |
---|
345 | |
---|
346 | BOOLEAN slDump(si_link l) |
---|
347 | { |
---|
348 | BOOLEAN res; |
---|
349 | |
---|
350 | if(! SI_LINK_W_OPEN_P(l)) // open w ? |
---|
351 | { |
---|
352 | if (slOpen(l, SI_LINK_WRITE,NULL)) return TRUE; |
---|
353 | } |
---|
354 | |
---|
355 | if(SI_LINK_W_OPEN_P(l)) |
---|
356 | { // now open w |
---|
357 | if (l->m->Dump != NULL) |
---|
358 | res = l->m->Dump(l); |
---|
359 | else |
---|
360 | res = TRUE; |
---|
361 | |
---|
362 | if (res) |
---|
363 | Werror("dump: Error for link of type %s, mode: %s, name: %s", |
---|
364 | l->m->type, l->mode, l->name); |
---|
365 | if (!SI_LINK_R_OPEN_P(l)) slClose(l); // do not close r/w links |
---|
366 | return res; |
---|
367 | } |
---|
368 | else |
---|
369 | { |
---|
370 | Werror("dump: Error to open link of type %s, mode: %s, name: %s for writing", |
---|
371 | l->m->type, l->mode, l->name); |
---|
372 | return TRUE; |
---|
373 | } |
---|
374 | } |
---|
375 | |
---|
376 | BOOLEAN slGetDump(si_link l) |
---|
377 | { |
---|
378 | BOOLEAN res; |
---|
379 | |
---|
380 | if(! SI_LINK_R_OPEN_P(l)) // open r ? |
---|
381 | { |
---|
382 | if (slOpen(l, SI_LINK_READ,NULL)) return TRUE; |
---|
383 | } |
---|
384 | |
---|
385 | if(SI_LINK_R_OPEN_P(l)) |
---|
386 | { // now open r |
---|
387 | if (l->m->GetDump != NULL) |
---|
388 | res = l->m->GetDump(l); |
---|
389 | else |
---|
390 | res = TRUE; |
---|
391 | |
---|
392 | if (res) |
---|
393 | Werror("getdump: Error for link of type %s, mode: %s, name: %s", |
---|
394 | l->m->type, l->mode, l->name); |
---|
395 | //res|=slClose(l); |
---|
396 | return res; |
---|
397 | } |
---|
398 | else |
---|
399 | { |
---|
400 | Werror("dump: Error open link of type %s, mode: %s, name: %s for reading", |
---|
401 | l->m->type, l->mode, l->name); |
---|
402 | return TRUE; |
---|
403 | } |
---|
404 | } |
---|
405 | |
---|
406 | /*------------Initialization at Start-up time------------------------*/ |
---|
407 | |
---|
408 | |
---|
409 | static si_link_extension slTypeInit(si_link_extension s, const char* type) |
---|
410 | { |
---|
411 | assume(s != NULL); |
---|
412 | s->next = NULL; |
---|
413 | si_link_extension ns = (si_link_extension)omAlloc0Bin(s_si_link_extension_bin); |
---|
414 | |
---|
415 | if (0) |
---|
416 | ; // dummy |
---|
417 | #ifdef HAVE_DBM |
---|
418 | else if (strcmp(type, "DBM") == 0) |
---|
419 | s->next = slInitDBMExtension(ns); |
---|
420 | #endif |
---|
421 | #if 1 |
---|
422 | else if (strcmp(type, "ssi") == 0) |
---|
423 | s->next = slInitSsiExtension(ns); |
---|
424 | #endif |
---|
425 | #if 1 |
---|
426 | else if (strcmp(type, "|") == 0) |
---|
427 | s->next = slInitPipeExtension(ns); |
---|
428 | #endif |
---|
429 | else |
---|
430 | { |
---|
431 | Warn("Found unknown link type: %s", type); |
---|
432 | Warn("Use default link type: %s", si_link_root->type); |
---|
433 | omFreeBin(ns, s_si_link_extension_bin); |
---|
434 | return si_link_root; |
---|
435 | } |
---|
436 | |
---|
437 | if (s->next == NULL) |
---|
438 | { |
---|
439 | Werror("Can not initialize link type %s", type); |
---|
440 | omFreeBin(ns, s_si_link_extension_bin); |
---|
441 | return NULL; |
---|
442 | } |
---|
443 | return s->next; |
---|
444 | } |
---|
445 | |
---|