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