1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /*************************************************************** |
---|
5 | * File: ssiLink.h |
---|
6 | * Purpose: declaration of sl_link routines for ssi |
---|
7 | * Version: $Id$ |
---|
8 | ***************************************************************/ |
---|
9 | #include <stdio.h> |
---|
10 | #include <fcntl.h> |
---|
11 | #include <errno.h> |
---|
12 | #include <unistd.h> |
---|
13 | #include <sys/types.h> |
---|
14 | #include <signal.h> |
---|
15 | #include <sys/types.h> /* for portability */ |
---|
16 | #include <sys/select.h> |
---|
17 | #include <sys/socket.h> |
---|
18 | #include <netinet/in.h> |
---|
19 | #include <ctype.h> /*for isdigit*/ |
---|
20 | #include <netdb.h> |
---|
21 | #include <sys/wait.h> |
---|
22 | |
---|
23 | |
---|
24 | |
---|
25 | #include <kernel/mod2.h> |
---|
26 | // #include "mod2.h" |
---|
27 | |
---|
28 | #include <Singular/tok.h> |
---|
29 | #include <Singular/ipid.h> |
---|
30 | #include <omalloc/omalloc.h> |
---|
31 | #include <kernel/ring.h> |
---|
32 | #include <kernel/matpol.h> |
---|
33 | #include <kernel/ideals.h> |
---|
34 | #include <kernel/polys.h> |
---|
35 | #include <kernel/longrat.h> |
---|
36 | #include <kernel/ideals.h> |
---|
37 | #include <kernel/intvec.h> |
---|
38 | #include <kernel/options.h> |
---|
39 | #include <kernel/timer.h> |
---|
40 | #include <Singular/subexpr.h> |
---|
41 | #include <Singular/silink.h> |
---|
42 | #include <Singular/cntrlc.h> |
---|
43 | #include <Singular/lists.h> |
---|
44 | #include <Singular/blackbox.h> |
---|
45 | #include <Singular/ssiLink.h> |
---|
46 | |
---|
47 | #ifdef HAVE_MPSR |
---|
48 | #include <Singular/mpsr.h> |
---|
49 | #endif |
---|
50 | |
---|
51 | //#if (_POSIX_C_SOURCE >= 200112L) || (_XOPEN_SOURCE >= 600) |
---|
52 | //#define HAVE_PSELECT |
---|
53 | //#endif |
---|
54 | |
---|
55 | #define SSI_VERSION 2 |
---|
56 | |
---|
57 | typedef struct |
---|
58 | { |
---|
59 | FILE *f_read; |
---|
60 | FILE *f_write; |
---|
61 | ring r; |
---|
62 | pid_t pid; /* only valid for fork/tcp mode*/ |
---|
63 | int fd_read,fd_write; /* only valid for fork/tcp mode*/ |
---|
64 | char level; |
---|
65 | char ungetc_buf; /* status sets to !=0, if ungetc was used, ssiRead* set to 0*/ |
---|
66 | char send_quit_at_exit; |
---|
67 | |
---|
68 | } ssiInfo; |
---|
69 | |
---|
70 | |
---|
71 | link_list ssiToBeClosed=NULL; |
---|
72 | sigset_t ssi_sigmask; |
---|
73 | sigset_t ssi_oldmask; |
---|
74 | #define SSI_BLOCK_CHLD sigprocmask(SIG_SETMASK, &ssi_sigmask, &ssi_oldmask) |
---|
75 | #define SSI_UNBLOCK_CHLD sigprocmask(SIG_SETMASK, &ssi_oldmask, NULL) |
---|
76 | |
---|
77 | // the helper functions: |
---|
78 | void ssiSetCurrRing(const ring r) |
---|
79 | { |
---|
80 | if (!rEqual(r,currRing,1)) |
---|
81 | { |
---|
82 | char name[20]; |
---|
83 | int nr=0; |
---|
84 | do |
---|
85 | { sprintf(name,"ssiRing%d",nr); nr++; } |
---|
86 | while(IDROOT->get(name, 0)!=NULL); |
---|
87 | idhdl h=enterid(omStrDup(name),0,RING_CMD,&IDROOT,FALSE); |
---|
88 | IDRING(h)=r; |
---|
89 | r->ref++; |
---|
90 | rSetHdl(h); |
---|
91 | } |
---|
92 | } |
---|
93 | // the implementation of the functions: |
---|
94 | void ssiWriteInt(ssiInfo *d,const int i) |
---|
95 | { |
---|
96 | SSI_BLOCK_CHLD; |
---|
97 | fprintf(d->f_write,"%d ",i); |
---|
98 | SSI_UNBLOCK_CHLD; |
---|
99 | //if (d->f_debug!=NULL) fprintf(d->f_debug,"int: %d ",i); |
---|
100 | } |
---|
101 | |
---|
102 | void ssiWriteString(ssiInfo *d,const char *s) |
---|
103 | { |
---|
104 | SSI_BLOCK_CHLD; |
---|
105 | fprintf(d->f_write,"%d %s ",strlen(s),s); |
---|
106 | SSI_UNBLOCK_CHLD; |
---|
107 | //if (d->f_debug!=NULL) fprintf(d->f_debug,"stringi: %d \"%s\" ",strlen(s),s); |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | void ssiWriteBigInt(const ssiInfo *d, const number n) |
---|
112 | { |
---|
113 | // syntax is as follows: |
---|
114 | // case 2 Q: 3 4 <int> |
---|
115 | // or 3 3 <mpz_t nominator> |
---|
116 | SSI_BLOCK_CHLD; |
---|
117 | if(SR_HDL(n) & SR_INT) |
---|
118 | { |
---|
119 | fprintf(d->f_write,"4 %ld ",SR_TO_INT(n)); |
---|
120 | //if (d->f_debug!=NULL) fprintf(d->f_debug,"bigint: short \"%ld\" ",SR_TO_INT(n)); |
---|
121 | } |
---|
122 | else if (n->s==3) |
---|
123 | { |
---|
124 | fprintf(d->f_write,"3 "); |
---|
125 | mpz_out_str(d->f_write,10,n->z); |
---|
126 | fprintf(d->f_write," "); |
---|
127 | //gmp_fprintf(d->f_write,"3 %Zd ",n->z); |
---|
128 | //if (d->f_debug!=NULL) gmp_fprintf(d->f_debug,"bigint: gmp \"%Zd\" ",n->z); |
---|
129 | } |
---|
130 | else Werror("illiegal bigint"); |
---|
131 | SSI_UNBLOCK_CHLD; |
---|
132 | } |
---|
133 | |
---|
134 | void ssiWriteNumber(const ssiInfo *d, const number n) |
---|
135 | { |
---|
136 | // syntax is as follows: |
---|
137 | // case 1 Z/p: 3 <int> |
---|
138 | // case 2 Q: 3 4 <int> |
---|
139 | // or 3 0 <mpz_t nominator> <mpz_t denominator> |
---|
140 | // or 3 1 dto. |
---|
141 | // or 3 3 <mpz_t nominator> |
---|
142 | // or 3 5 <mpz_t raw nom.> <mpz_t raw denom.> |
---|
143 | // or 3 6 <mpz_t raw nom.> <mpz_t raw denom.> |
---|
144 | // or 3 7 <mpz_t raw nom.> |
---|
145 | SSI_BLOCK_CHLD; |
---|
146 | if(rField_is_Zp(d->r)) |
---|
147 | { |
---|
148 | fprintf(d->f_write,"%d ",(int)(long)n); |
---|
149 | //if (d->f_debug!=NULL) fprintf(d->f_debug,"number: \"%ld\" ",(int)(long)n); |
---|
150 | } |
---|
151 | else if (rField_is_Q(d->r)) |
---|
152 | { |
---|
153 | if(SR_HDL(n) & SR_INT) |
---|
154 | { |
---|
155 | fprintf(d->f_write,"4 %ld ",SR_TO_INT(n)); |
---|
156 | //if (d->f_debug!=NULL) fprintf(d->f_debug,"number: short \"%ld\" ",SR_TO_INT(n)); |
---|
157 | } |
---|
158 | else if (n->s<2) |
---|
159 | { |
---|
160 | //gmp_fprintf(d->f_write,"%d %Zd %Zd ",n->s,n->z,n->n); |
---|
161 | fprintf(d->f_write,"%d ",n->s+5); |
---|
162 | mpz_out_str (d->f_write,32, n->z); |
---|
163 | fprintf(d->f_write," "); |
---|
164 | mpz_out_str (d->f_write,32, n->n); |
---|
165 | fprintf(d->f_write," "); |
---|
166 | |
---|
167 | //if (d->f_debug!=NULL) gmp_fprintf(d->f_debug,"number: s=%d gmp/gmp \"%Zd %Zd\" ",n->s,n->z,n->n); |
---|
168 | } |
---|
169 | else /*n->s==3*/ |
---|
170 | { |
---|
171 | //gmp_fprintf(d->f_write,"3 %Zd ",n->z); |
---|
172 | fprintf(d->f_write,"8 "); |
---|
173 | mpz_out_str (d->f_write,32, n->z); |
---|
174 | fprintf(d->f_write," "); |
---|
175 | |
---|
176 | //if (d->f_debug!=NULL) gmp_fprintf(d->f_debug,"number: gmp \"%Zd\" ",n->z); |
---|
177 | } |
---|
178 | } |
---|
179 | else WerrorS("coeff field not implemented"); |
---|
180 | SSI_UNBLOCK_CHLD; |
---|
181 | } |
---|
182 | |
---|
183 | void ssiWriteRing(ssiInfo *d,const ring r) |
---|
184 | { |
---|
185 | /* 5 <ch> <N> <l1> <v1> ...<lN> <vN> <number of orderings> <ord1> <block0_1> <block1_1> .... */ |
---|
186 | if (d->r!=NULL) rKill(d->r); |
---|
187 | d->r=r; |
---|
188 | d->r->ref++; |
---|
189 | SSI_BLOCK_CHLD; |
---|
190 | fprintf(d->f_write,"%d %d ",r->ch,r->N); |
---|
191 | |
---|
192 | int i; |
---|
193 | for(i=0;i<r->N;i++) |
---|
194 | { |
---|
195 | fprintf(d->f_write,"%d %s ",strlen(r->names[i]),r->names[i]); |
---|
196 | } |
---|
197 | /* number of orderings:*/ |
---|
198 | i=0; |
---|
199 | while (r->order[i]!=0) i++; |
---|
200 | fprintf(d->f_write,"%d ",i); |
---|
201 | /* each ordering block: */ |
---|
202 | i=0; |
---|
203 | while(r->order[i]!=0) |
---|
204 | { |
---|
205 | fprintf(d->f_write,"%d %d %d ",r->order[i],r->block0[i], r->block1[i]); |
---|
206 | i++; |
---|
207 | } |
---|
208 | SSI_UNBLOCK_CHLD; |
---|
209 | } |
---|
210 | |
---|
211 | void ssiWritePoly(ssiInfo *d, int typ, poly p) |
---|
212 | { |
---|
213 | SSI_BLOCK_CHLD; |
---|
214 | fprintf(d->f_write,"%d ",pLength(p));//number of terms |
---|
215 | SSI_UNBLOCK_CHLD; |
---|
216 | int i; |
---|
217 | |
---|
218 | while(p!=NULL) |
---|
219 | { |
---|
220 | ssiWriteNumber(d,pGetCoeff(p)); |
---|
221 | //nWrite(fich,pGetCoeff(p)); |
---|
222 | SSI_BLOCK_CHLD; |
---|
223 | fprintf(d->f_write,"%ld ",p_GetComp(p,d->r));//component |
---|
224 | |
---|
225 | for(int j=1;j<=rVar(d->r);j++) |
---|
226 | { |
---|
227 | fprintf(d->f_write,"%ld ",p_GetExp(p,j,d->r ));//x^j |
---|
228 | } |
---|
229 | pIter(p); |
---|
230 | SSI_UNBLOCK_CHLD; |
---|
231 | } |
---|
232 | } |
---|
233 | |
---|
234 | void ssiWriteIdeal(ssiInfo *d, int typ,ideal I) |
---|
235 | { |
---|
236 | // syntax: 7 # of elements <poly 1> <poly2>..... |
---|
237 | // syntax: 8 <rows> <cols> <poly 1> <poly2>..... |
---|
238 | matrix M=(matrix)I; |
---|
239 | SSI_BLOCK_CHLD; |
---|
240 | if (typ==MATRIX_CMD) |
---|
241 | fprintf(d->f_write,"%d %d ", MATROWS(M),MATCOLS(M)); |
---|
242 | else |
---|
243 | fprintf(d->f_write,"%d ",IDELEMS(I)); |
---|
244 | SSI_UNBLOCK_CHLD; |
---|
245 | |
---|
246 | int i; |
---|
247 | int tt; |
---|
248 | if (typ==MODUL_CMD) tt=VECTOR_CMD; |
---|
249 | else tt=POLY_CMD; |
---|
250 | |
---|
251 | for(i=0;i<IDELEMS(I);i++) |
---|
252 | { |
---|
253 | ssiWritePoly(d,tt,I->m[i]); |
---|
254 | } |
---|
255 | } |
---|
256 | void ssiWriteCommand(si_link l, command D) |
---|
257 | { |
---|
258 | ssiInfo *d=(ssiInfo*)l->data; |
---|
259 | // syntax: <num ops> <operation> <op1> <op2> .... |
---|
260 | SSI_BLOCK_CHLD; |
---|
261 | fprintf(d->f_write,"%d %d ",D->argc,D->op); |
---|
262 | SSI_UNBLOCK_CHLD; |
---|
263 | if (D->argc >0) ssiWrite(l, &(D->arg1)); |
---|
264 | if (D->argc < 4) |
---|
265 | { |
---|
266 | if (D->argc >1) ssiWrite(l, &(D->arg2)); |
---|
267 | if (D->argc >2) ssiWrite(l, &(D->arg3)); |
---|
268 | } |
---|
269 | } |
---|
270 | |
---|
271 | void ssiWriteProc(ssiInfo *d,procinfov p) |
---|
272 | { |
---|
273 | ssiWriteString(d,p->data.s.body); |
---|
274 | } |
---|
275 | |
---|
276 | void ssiWriteList(si_link l,lists dd) |
---|
277 | { |
---|
278 | ssiInfo *d=(ssiInfo*)l->data; |
---|
279 | SSI_BLOCK_CHLD; |
---|
280 | fprintf(d->f_write,"%d ",dd->nr+1); |
---|
281 | SSI_UNBLOCK_CHLD; |
---|
282 | int i; |
---|
283 | for(i=0;i<=dd->nr;i++) |
---|
284 | { |
---|
285 | ssiWrite(l,&(dd->m[i])); |
---|
286 | } |
---|
287 | } |
---|
288 | void ssiWriteIntvec(ssiInfo *d,intvec * v) |
---|
289 | { |
---|
290 | SSI_BLOCK_CHLD; |
---|
291 | fprintf(d->f_write,"%d ",v->length()); |
---|
292 | int i; |
---|
293 | for(i=0;i<v->length();i++) |
---|
294 | { |
---|
295 | fprintf(d->f_write,"%d ",(*v)[i]); |
---|
296 | } |
---|
297 | SSI_UNBLOCK_CHLD; |
---|
298 | } |
---|
299 | |
---|
300 | char *ssiReadString(ssiInfo *d) |
---|
301 | { |
---|
302 | char *buf; |
---|
303 | int l; |
---|
304 | SSI_BLOCK_CHLD; |
---|
305 | fscanf(d->f_read,"%d ",&l); |
---|
306 | SSI_UNBLOCK_CHLD; |
---|
307 | buf=(char*)omAlloc(l+1); |
---|
308 | SSI_BLOCK_CHLD; |
---|
309 | fread(buf,1,l,d->f_read); |
---|
310 | SSI_UNBLOCK_CHLD; |
---|
311 | buf[l]='\0'; |
---|
312 | return buf; |
---|
313 | } |
---|
314 | |
---|
315 | int ssiReadInt(FILE *fich) |
---|
316 | { |
---|
317 | int d; |
---|
318 | SSI_BLOCK_CHLD; |
---|
319 | fscanf(fich,"%d",&d); |
---|
320 | SSI_UNBLOCK_CHLD; |
---|
321 | return d; |
---|
322 | } |
---|
323 | |
---|
324 | number ssiReadBigInt(ssiInfo *d) |
---|
325 | { |
---|
326 | int sub_type=-1; |
---|
327 | SSI_BLOCK_CHLD; |
---|
328 | fscanf(d->f_read,"%d",&sub_type); |
---|
329 | SSI_UNBLOCK_CHLD; |
---|
330 | switch(sub_type) |
---|
331 | { |
---|
332 | case 3: |
---|
333 | {// read int or mpz_t or mpz_t, mpz_t |
---|
334 | number n=nlRInit(0); |
---|
335 | SSI_BLOCK_CHLD; |
---|
336 | mpz_inp_str(n->z,d->f_read,0); |
---|
337 | SSI_UNBLOCK_CHLD; |
---|
338 | n->s=sub_type; |
---|
339 | return n; |
---|
340 | } |
---|
341 | case 4: |
---|
342 | { |
---|
343 | int dd; |
---|
344 | SSI_BLOCK_CHLD; |
---|
345 | fscanf(d->f_read,"%d",&dd); |
---|
346 | SSI_UNBLOCK_CHLD; |
---|
347 | return INT_TO_SR(dd); |
---|
348 | } |
---|
349 | default: |
---|
350 | Werror("error in reading bigint: invalid subtype %d",sub_type); |
---|
351 | return NULL; |
---|
352 | } |
---|
353 | } |
---|
354 | |
---|
355 | number ssiReadNumber(ssiInfo *d) |
---|
356 | { |
---|
357 | if (rField_is_Q(d->r)) |
---|
358 | { |
---|
359 | int sub_type=-1; |
---|
360 | SSI_BLOCK_CHLD; |
---|
361 | fscanf(d->f_read,"%d",&sub_type); |
---|
362 | SSI_UNBLOCK_CHLD; |
---|
363 | switch(sub_type) |
---|
364 | { |
---|
365 | case 0: |
---|
366 | case 1: |
---|
367 | {// read mpz_t, mpz_t |
---|
368 | number n=nlRInit(0); |
---|
369 | mpz_init(n->n); |
---|
370 | SSI_BLOCK_CHLD; |
---|
371 | gmp_fscanf(d->f_read,"%Zd %Zd",n->z,n->n); |
---|
372 | SSI_UNBLOCK_CHLD; |
---|
373 | n->s=sub_type; |
---|
374 | return n; |
---|
375 | } |
---|
376 | |
---|
377 | case 3: |
---|
378 | {// read mpz_t |
---|
379 | number n=nlRInit(0); |
---|
380 | SSI_BLOCK_CHLD; |
---|
381 | gmp_fscanf(d->f_read,"%Zd",n->z); |
---|
382 | SSI_UNBLOCK_CHLD; |
---|
383 | n->s=3; /*sub_type*/ |
---|
384 | return n; |
---|
385 | } |
---|
386 | case 4: |
---|
387 | { |
---|
388 | int dd; |
---|
389 | SSI_BLOCK_CHLD; |
---|
390 | fscanf(d->f_read,"%d",&dd); |
---|
391 | SSI_UNBLOCK_CHLD; |
---|
392 | return INT_TO_SR(dd); |
---|
393 | } |
---|
394 | case 5: |
---|
395 | case 6: |
---|
396 | {// read raw mpz_t, mpz_t |
---|
397 | number n=nlRInit(0); |
---|
398 | mpz_init(n->n); |
---|
399 | SSI_BLOCK_CHLD; |
---|
400 | mpz_inp_str (n->z, d->f_read, 32); |
---|
401 | mpz_inp_str (n->n, d->f_read, 32); |
---|
402 | SSI_UNBLOCK_CHLD; |
---|
403 | n->s=sub_type-5; |
---|
404 | return n; |
---|
405 | } |
---|
406 | case 8: |
---|
407 | {// read raw mpz_t |
---|
408 | number n=nlRInit(0); |
---|
409 | SSI_BLOCK_CHLD; |
---|
410 | mpz_inp_str (n->z, d->f_read, 32); |
---|
411 | SSI_UNBLOCK_CHLD; |
---|
412 | n->s=sub_type=3; /*subtype-5*/ |
---|
413 | return n; |
---|
414 | } |
---|
415 | |
---|
416 | default: Werror("error in reading number: invalid subtype %d",sub_type); |
---|
417 | return NULL; |
---|
418 | } |
---|
419 | } |
---|
420 | else if (rField_is_Zp(d->r)) |
---|
421 | { |
---|
422 | // read int |
---|
423 | int dd; |
---|
424 | SSI_BLOCK_CHLD; |
---|
425 | fscanf(d->f_read,"%d",&dd); |
---|
426 | SSI_UNBLOCK_CHLD; |
---|
427 | return (number)dd; |
---|
428 | } |
---|
429 | else Werror("coeffs not implemented"); |
---|
430 | return NULL; |
---|
431 | } |
---|
432 | |
---|
433 | ring ssiReadRing(ssiInfo *d) |
---|
434 | { |
---|
435 | /* syntax is <ch> <N> <l1> <v1> ...<lN> <vN> <number of orderings> <ord1> <block0_1> <block1_1> .... */ |
---|
436 | int ch, N,i,l; |
---|
437 | char **names; |
---|
438 | SSI_BLOCK_CHLD; |
---|
439 | fscanf(d->f_read,"%d %d ",&ch,&N); |
---|
440 | SSI_UNBLOCK_CHLD; |
---|
441 | names=(char**)omAlloc(N*sizeof(char*)); |
---|
442 | for(i=0;i<N;i++) |
---|
443 | { |
---|
444 | names[i]=ssiReadString(d); |
---|
445 | } |
---|
446 | // read the orderings: |
---|
447 | int num_ord; // number of orderings |
---|
448 | SSI_BLOCK_CHLD; |
---|
449 | fscanf(d->f_read,"%d",&num_ord); |
---|
450 | SSI_UNBLOCK_CHLD; |
---|
451 | int *ord=(int *)omAlloc0((num_ord+1)*sizeof(int)); |
---|
452 | int *block0=(int *)omAlloc0((num_ord+1)*sizeof(int)); |
---|
453 | int *block1=(int *)omAlloc0((num_ord+1)*sizeof(int)); |
---|
454 | SSI_BLOCK_CHLD; |
---|
455 | for(i=0;i<num_ord;i++) |
---|
456 | { |
---|
457 | fscanf(d->f_read,"%d %d %d",&ord[i],&block0[i],&block1[i]); |
---|
458 | } |
---|
459 | SSI_UNBLOCK_CHLD; |
---|
460 | return rDefault(ch,N,names,num_ord,ord,block0,block1); |
---|
461 | } |
---|
462 | |
---|
463 | poly ssiReadPoly(ssiInfo *D) |
---|
464 | { |
---|
465 | // < # of terms> < term1> < ..... |
---|
466 | int n,i,l; |
---|
467 | n=ssiReadInt(D->f_read); |
---|
468 | //Print("poly: terms:%d\n",n); |
---|
469 | poly p; |
---|
470 | int j; |
---|
471 | j=0; |
---|
472 | poly ret=NULL; |
---|
473 | poly prev=NULL; |
---|
474 | for(l=0;l<n;l++) // read n terms |
---|
475 | { |
---|
476 | // coef,comp.exp1,..exp N |
---|
477 | p=p_Init(D->r); |
---|
478 | pGetCoeff(p)=ssiReadNumber(D); |
---|
479 | int d; |
---|
480 | SSI_BLOCK_CHLD; |
---|
481 | fscanf(D->f_read,"%d",&d); |
---|
482 | SSI_UNBLOCK_CHLD; |
---|
483 | p_SetComp(p,d,D->r); |
---|
484 | for(i=1;i<=rVar(D->r);i++) |
---|
485 | { |
---|
486 | SSI_BLOCK_CHLD; |
---|
487 | fscanf(D->f_read,"%d",&d); |
---|
488 | SSI_UNBLOCK_CHLD; |
---|
489 | p_SetExp(p,i,d,D->r); |
---|
490 | } |
---|
491 | p_Setm(p,D->r); |
---|
492 | p_Test(p,D->r); |
---|
493 | if (ret==NULL) ret=p; |
---|
494 | else pNext(prev)=p; |
---|
495 | prev=p; |
---|
496 | } |
---|
497 | return ret; |
---|
498 | } |
---|
499 | |
---|
500 | ideal ssiReadIdeal(ssiInfo *d) |
---|
501 | { |
---|
502 | int n,i; |
---|
503 | ideal I; |
---|
504 | SSI_BLOCK_CHLD; |
---|
505 | fscanf(d->f_read,"%d",&n); |
---|
506 | SSI_UNBLOCK_CHLD; |
---|
507 | I=idInit(n,1); |
---|
508 | for(i=0;i<IDELEMS(I);i++) // read n terms |
---|
509 | { |
---|
510 | I->m [i]=ssiReadPoly(d); |
---|
511 | } |
---|
512 | return I; |
---|
513 | } |
---|
514 | |
---|
515 | matrix ssiReadMatrix(ssiInfo *d) |
---|
516 | { |
---|
517 | int n,m,i,j; |
---|
518 | SSI_BLOCK_CHLD; |
---|
519 | fscanf(d->f_read,"%d %d",&m,&n); |
---|
520 | SSI_UNBLOCK_CHLD; |
---|
521 | matrix M=mpNew(m,n); |
---|
522 | poly p; |
---|
523 | for(int i=1;i<=MATROWS(M);i++) |
---|
524 | for(int j=1;j<=MATCOLS(M);j++) |
---|
525 | { |
---|
526 | p=ssiReadPoly(d); |
---|
527 | MATELEM(M,i,j)=p; |
---|
528 | } |
---|
529 | return M; |
---|
530 | } |
---|
531 | |
---|
532 | command ssiReadCommand(si_link l) |
---|
533 | { |
---|
534 | ssiInfo *d=(ssiInfo*)l->data; |
---|
535 | // syntax: <num ops> <operation> <op1> <op2> .... |
---|
536 | command D=(command)omAlloc0(sizeof(*D)); |
---|
537 | int argc,op; |
---|
538 | SSI_BLOCK_CHLD; |
---|
539 | fscanf(d->f_read,"%d %d",&argc,&op); |
---|
540 | SSI_UNBLOCK_CHLD; |
---|
541 | D->argc=argc; D->op=op; |
---|
542 | leftv v; |
---|
543 | if (argc >0) |
---|
544 | { |
---|
545 | v=ssiRead1(l); |
---|
546 | memcpy(&(D->arg1),v,sizeof(*v)); |
---|
547 | omFreeBin(v,sleftv_bin); |
---|
548 | } |
---|
549 | if (argc <4) |
---|
550 | { |
---|
551 | if (D->argc >1) |
---|
552 | { |
---|
553 | v=ssiRead1(l); |
---|
554 | memcpy(&(D->arg2),v,sizeof(*v)); |
---|
555 | omFreeBin(v,sleftv_bin); |
---|
556 | } |
---|
557 | if (D->argc >2) |
---|
558 | { |
---|
559 | v=ssiRead1(l); |
---|
560 | memcpy(&(D->arg3),v,sizeof(*v)); |
---|
561 | omFreeBin(v,sleftv_bin); |
---|
562 | } |
---|
563 | } |
---|
564 | else |
---|
565 | { |
---|
566 | leftv prev=&(D->arg1); |
---|
567 | argc--; |
---|
568 | while(argc >0) |
---|
569 | { |
---|
570 | v=ssiRead1(l); |
---|
571 | prev->next=v; |
---|
572 | prev=v; |
---|
573 | argc--; |
---|
574 | } |
---|
575 | } |
---|
576 | return D; |
---|
577 | } |
---|
578 | |
---|
579 | procinfov ssiReadProc(ssiInfo *d) |
---|
580 | { |
---|
581 | char *s=ssiReadString(d); |
---|
582 | procinfov p=(procinfov)omAlloc0Bin(procinfo_bin); |
---|
583 | p->language=LANG_SINGULAR; |
---|
584 | p->libname=omStrDup(""); |
---|
585 | p->procname=omStrDup(""); |
---|
586 | p->data.s.body=s; |
---|
587 | return p; |
---|
588 | } |
---|
589 | lists ssiReadList(si_link l) |
---|
590 | { |
---|
591 | ssiInfo *d=(ssiInfo*)l->data; |
---|
592 | int nr; |
---|
593 | SSI_BLOCK_CHLD; |
---|
594 | fscanf(d->f_read,"%d",&nr); |
---|
595 | SSI_UNBLOCK_CHLD; |
---|
596 | lists L=(lists)omAlloc(sizeof(*L)); |
---|
597 | L->Init(nr); |
---|
598 | |
---|
599 | int i; |
---|
600 | leftv v; |
---|
601 | for(i=0;i<nr;i++) |
---|
602 | { |
---|
603 | v=ssiRead1(l); |
---|
604 | memcpy(&(L->m[i]),v,sizeof(*v)); |
---|
605 | omFreeBin(v,sleftv_bin); |
---|
606 | } |
---|
607 | return L; |
---|
608 | } |
---|
609 | intvec* ssiReadIntvec(ssiInfo *d) |
---|
610 | { |
---|
611 | int nr; |
---|
612 | SSI_BLOCK_CHLD; |
---|
613 | fscanf(d->f_read,"%d",&nr); |
---|
614 | SSI_UNBLOCK_CHLD; |
---|
615 | intvec *v=new intvec(nr); |
---|
616 | SSI_BLOCK_CHLD; |
---|
617 | for(int i=0;i<nr;i++) |
---|
618 | { |
---|
619 | fscanf(d->f_read,"%d",&((*v)[i])); |
---|
620 | } |
---|
621 | SSI_UNBLOCK_CHLD; |
---|
622 | return v; |
---|
623 | } |
---|
624 | |
---|
625 | void ssiReadBlackbox(leftv res, si_link l) |
---|
626 | { |
---|
627 | ssiInfo *d=(ssiInfo*)l->data; |
---|
628 | int throwaway; |
---|
629 | SSI_BLOCK_CHLD; |
---|
630 | fscanf(d->f_read,"%d ",&throwaway); |
---|
631 | SSI_UNBLOCK_CHLD; |
---|
632 | char *name=ssiReadString(d); |
---|
633 | int tok; |
---|
634 | blackboxIsCmd(name,tok); |
---|
635 | if (tok>MAX_TOK) |
---|
636 | { |
---|
637 | blackbox *b=getBlackboxStuff(tok); |
---|
638 | res->rtyp=tok; |
---|
639 | b->blackbox_deserialize(&b,&(res->data),l); |
---|
640 | } |
---|
641 | else |
---|
642 | { |
---|
643 | Werror("blackbox %s not found",name); |
---|
644 | } |
---|
645 | } |
---|
646 | |
---|
647 | //**************************************************************************/ |
---|
648 | |
---|
649 | BOOLEAN ssiOpen(si_link l, short flag, leftv u) |
---|
650 | { |
---|
651 | const char *mode; |
---|
652 | ssiInfo *d=(ssiInfo*)omAlloc0(sizeof(ssiInfo)); |
---|
653 | sigprocmask(SIG_SETMASK, NULL, &ssi_sigmask); |
---|
654 | sigaddset(&ssi_sigmask, SIGCHLD); |
---|
655 | if (flag & SI_LINK_OPEN) |
---|
656 | { |
---|
657 | if (l->mode[0] != '\0' && (strcmp(l->mode, "r") == 0)) |
---|
658 | flag = SI_LINK_READ; |
---|
659 | else flag = SI_LINK_WRITE; |
---|
660 | } |
---|
661 | |
---|
662 | if (flag == SI_LINK_READ) mode = "r"; |
---|
663 | else if (strcmp(l->mode, "w") == 0) mode = "w"; |
---|
664 | else if (strcmp(l->mode, "fork") == 0) mode = "fork"; |
---|
665 | else if (strcmp(l->mode, "tcp") == 0) mode = "tcp"; |
---|
666 | else if (strcmp(l->mode, "connect") == 0) mode = "connect"; |
---|
667 | else mode = "a"; |
---|
668 | |
---|
669 | |
---|
670 | SI_LINK_SET_OPEN_P(l, flag); |
---|
671 | l->data=d; |
---|
672 | omFree(l->mode); |
---|
673 | l->mode = omStrDup(mode); |
---|
674 | |
---|
675 | if (l->name[0] == '\0') |
---|
676 | { |
---|
677 | if (strcmp(mode,"fork")==0) |
---|
678 | { |
---|
679 | link_list n=(link_list)omAlloc(sizeof(link_struct)); |
---|
680 | n->u=u; |
---|
681 | n->l=l; |
---|
682 | n->next=(void *)ssiToBeClosed; |
---|
683 | ssiToBeClosed=n; |
---|
684 | |
---|
685 | int pc[2]; |
---|
686 | int cp[2]; |
---|
687 | pipe(pc); |
---|
688 | pipe(cp); |
---|
689 | pid_t pid=fork(); |
---|
690 | if (pid==0) /*fork: child*/ |
---|
691 | { |
---|
692 | link_list hh=(link_list)ssiToBeClosed->next; |
---|
693 | /* we know: l is the first entry in ssiToBeClosed-list */ |
---|
694 | while(hh!=NULL) |
---|
695 | { |
---|
696 | ssiInfo *dd=(ssiInfo*)hh->l->data; |
---|
697 | fclose(dd->f_read); |
---|
698 | fclose(dd->f_write); |
---|
699 | if (dd->r!=NULL) rKill(dd->r); |
---|
700 | omFreeSize((ADDRESS)dd,(sizeof *dd)); |
---|
701 | hh->l->data=NULL; |
---|
702 | SI_LINK_SET_CLOSE_P(hh->l); |
---|
703 | link_list nn=(link_list)hh->next; |
---|
704 | omFree(hh); |
---|
705 | hh=nn; |
---|
706 | } |
---|
707 | ssiToBeClosed->next=NULL; |
---|
708 | close(pc[1]); close(cp[0]); |
---|
709 | d->f_read=fdopen(pc[0],"r"); |
---|
710 | d->fd_read=pc[0]; |
---|
711 | d->f_write=fdopen(cp[1],"w"); |
---|
712 | d->fd_write=cp[1]; |
---|
713 | l->data=d; |
---|
714 | omFree(l->mode); |
---|
715 | l->mode = omStrDup(mode); |
---|
716 | singular_in_batchmode=TRUE; |
---|
717 | SI_LINK_SET_RW_OPEN_P(l); |
---|
718 | //myynest=0; |
---|
719 | fe_fgets_stdin=fe_fgets_dummy; |
---|
720 | if ((u!=NULL)&&(u->rtyp==IDHDL)) |
---|
721 | { |
---|
722 | idhdl h=(idhdl)u->data; |
---|
723 | h->lev=0; |
---|
724 | } |
---|
725 | loop |
---|
726 | { |
---|
727 | leftv h=ssiRead1(l); /*contains an exit.... */ |
---|
728 | if (feErrors != NULL && *feErrors != '\0') |
---|
729 | { |
---|
730 | // handle errors: |
---|
731 | PrintS(feErrors); /* currently quite simple */ |
---|
732 | *feErrors = '\0'; |
---|
733 | } |
---|
734 | ssiWrite(l,h); |
---|
735 | h->CleanUp(); |
---|
736 | omFreeBin(h, sleftv_bin); |
---|
737 | } |
---|
738 | /* never reached*/ |
---|
739 | } |
---|
740 | else if (pid>0) /*fork: parent*/ |
---|
741 | { |
---|
742 | d->pid=pid; |
---|
743 | close(pc[0]); close(cp[1]); |
---|
744 | d->f_read=fdopen(cp[0],"r"); |
---|
745 | d->fd_read=cp[0]; |
---|
746 | d->f_write=fdopen(pc[1],"w"); |
---|
747 | d->fd_write=pc[1]; |
---|
748 | SI_LINK_SET_RW_OPEN_P(l); |
---|
749 | d->send_quit_at_exit=1; |
---|
750 | } |
---|
751 | else |
---|
752 | { |
---|
753 | Werror("fork failed (%d)",errno); |
---|
754 | l->data=NULL; |
---|
755 | omFree(d); |
---|
756 | return TRUE; |
---|
757 | } |
---|
758 | } |
---|
759 | // --------------------------------------------------------------------- |
---|
760 | else if (strcmp(mode,"tcp")==0) |
---|
761 | { |
---|
762 | int sockfd, newsockfd, portno, clilen; |
---|
763 | struct sockaddr_in serv_addr, cli_addr; |
---|
764 | int n; |
---|
765 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
---|
766 | if(sockfd < 0) |
---|
767 | { |
---|
768 | WerrorS("ERROR opening socket"); |
---|
769 | l->data=NULL; |
---|
770 | omFree(d); |
---|
771 | return TRUE; |
---|
772 | } |
---|
773 | memset((char *) &serv_addr,0, sizeof(serv_addr)); |
---|
774 | portno = 1025; |
---|
775 | serv_addr.sin_family = AF_INET; |
---|
776 | serv_addr.sin_addr.s_addr = INADDR_ANY; |
---|
777 | do |
---|
778 | { |
---|
779 | portno++; |
---|
780 | serv_addr.sin_port = htons(portno); |
---|
781 | if(portno > 50000) |
---|
782 | { |
---|
783 | WerrorS("ERROR on binding (no free port available?)"); |
---|
784 | l->data=NULL; |
---|
785 | omFree(d); |
---|
786 | return TRUE; |
---|
787 | } |
---|
788 | } |
---|
789 | while(bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0); |
---|
790 | Print("waiting on port %d\n", portno);mflush(); |
---|
791 | listen(sockfd,5); |
---|
792 | newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, (socklen_t *)&clilen); |
---|
793 | if(newsockfd < 0) |
---|
794 | { |
---|
795 | WerrorS("ERROR on accept"); |
---|
796 | l->data=NULL; |
---|
797 | omFree(d); |
---|
798 | return TRUE; |
---|
799 | } |
---|
800 | PrintS("client accepted\n"); |
---|
801 | d->fd_read = newsockfd; |
---|
802 | d->fd_write = newsockfd; |
---|
803 | d->f_read = fdopen(newsockfd, "r"); |
---|
804 | d->f_write = fdopen(newsockfd, "w"); |
---|
805 | SI_LINK_SET_RW_OPEN_P(l); |
---|
806 | close(sockfd); |
---|
807 | } |
---|
808 | // no ssi-Link on stdin or stdout |
---|
809 | else |
---|
810 | { |
---|
811 | Werror("invalid mode >>%s<< for ssi",mode); |
---|
812 | l->data=NULL; |
---|
813 | omFree(d); |
---|
814 | return TRUE; |
---|
815 | } |
---|
816 | } |
---|
817 | // ========================================================================= |
---|
818 | else /*l->name=NULL*/ |
---|
819 | { |
---|
820 | // tcp mode |
---|
821 | if(strcmp(mode,"tcp")==0) |
---|
822 | { |
---|
823 | int sockfd, newsockfd, portno, clilen; |
---|
824 | struct sockaddr_in serv_addr, cli_addr; |
---|
825 | int n; |
---|
826 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
---|
827 | if(sockfd < 0) |
---|
828 | { |
---|
829 | WerrorS("ERROR opening socket"); |
---|
830 | l->data=NULL; |
---|
831 | omFree(d); |
---|
832 | return TRUE; |
---|
833 | } |
---|
834 | memset((char *) &serv_addr,0, sizeof(serv_addr)); |
---|
835 | portno = 1025; |
---|
836 | serv_addr.sin_family = AF_INET; |
---|
837 | serv_addr.sin_addr.s_addr = INADDR_ANY; |
---|
838 | do |
---|
839 | { |
---|
840 | portno++; |
---|
841 | serv_addr.sin_port = htons(portno); |
---|
842 | if(portno > 50000) |
---|
843 | { |
---|
844 | WerrorS("ERROR on binding (no free port available?)"); |
---|
845 | l->data=NULL; |
---|
846 | return TRUE; |
---|
847 | } |
---|
848 | } |
---|
849 | while(bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0); |
---|
850 | //Print("waiting on port %d\n", portno);mflush(); |
---|
851 | listen(sockfd,5); |
---|
852 | char* cli_host = (char*)omAlloc(256); |
---|
853 | char* path = (char*)omAlloc(1024); |
---|
854 | int r = sscanf(l->name,"%255[^:]:%s",cli_host,path); |
---|
855 | if(r == 0) |
---|
856 | { |
---|
857 | WerrorS("ERROR: no host specified"); |
---|
858 | l->data=NULL; |
---|
859 | omFree(d); |
---|
860 | omFree(path); |
---|
861 | omFree(cli_host); |
---|
862 | return TRUE; |
---|
863 | } |
---|
864 | else if(r == 1) |
---|
865 | { |
---|
866 | WarnS("program not specified, using /usr/local/bin/Singular"); |
---|
867 | strcpy(path,"/usr/local/bin/Singular"); |
---|
868 | } |
---|
869 | char* ssh_command = (char*)omAlloc(256); |
---|
870 | char* ser_host = (char*)omAlloc(64); |
---|
871 | gethostname(ser_host,64); |
---|
872 | sprintf(ssh_command,"ssh %s %s -q --batch --link=ssi --MPhost=%s --MPport=%d &",cli_host,path,ser_host,portno); |
---|
873 | //Print("client on %s started:%s\n",cli_host,path); |
---|
874 | omFree(path); |
---|
875 | omFree(cli_host); |
---|
876 | if (TEST_OPT_PROT) { Print("running >>%s<<\n",ssh_command); } |
---|
877 | system(ssh_command); |
---|
878 | omFree(ssh_command); |
---|
879 | omFree(ser_host); |
---|
880 | clilen = sizeof(cli_addr); |
---|
881 | newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, (socklen_t *)&clilen); |
---|
882 | if(newsockfd < 0) |
---|
883 | { |
---|
884 | WerrorS("ERROR on accept"); |
---|
885 | l->data=NULL; |
---|
886 | omFree(d); |
---|
887 | return TRUE; |
---|
888 | } |
---|
889 | //PrintS("client accepted\n"); |
---|
890 | d->fd_read = newsockfd; |
---|
891 | d->fd_write = newsockfd; |
---|
892 | d->f_read = fdopen(newsockfd, "r"); |
---|
893 | d->f_write = fdopen(newsockfd, "w"); |
---|
894 | SI_LINK_SET_RW_OPEN_P(l); |
---|
895 | d->send_quit_at_exit=1; |
---|
896 | close(sockfd); |
---|
897 | fprintf(d->f_write,"98 %d %d %u %u\n",SSI_VERSION,MAX_TOK,test,verbose); |
---|
898 | } |
---|
899 | // ---------------------------------------------------------------------- |
---|
900 | else if(strcmp(mode,"connect")==0) |
---|
901 | { |
---|
902 | char* host = (char*)omAlloc(256); |
---|
903 | int sockfd, portno, n; |
---|
904 | struct sockaddr_in serv_addr; |
---|
905 | struct hostent *server; |
---|
906 | |
---|
907 | sscanf(l->name,"%255[^:]:%d",host,&portno); |
---|
908 | //Print("connect to host %s, port %d\n",host,portno);mflush(); |
---|
909 | if (portno!=0) |
---|
910 | { |
---|
911 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
---|
912 | if (sockfd < 0) { WerrorS("ERROR opening socket"); return TRUE; } |
---|
913 | server = gethostbyname(host); |
---|
914 | if (server == NULL) { WerrorS("ERROR, no such host"); return TRUE; } |
---|
915 | memset((char *) &serv_addr, 0, sizeof(serv_addr)); |
---|
916 | serv_addr.sin_family = AF_INET; |
---|
917 | memcpy((char *)&serv_addr.sin_addr.s_addr, |
---|
918 | (char *)server->h_addr, |
---|
919 | server->h_length); |
---|
920 | serv_addr.sin_port = htons(portno); |
---|
921 | if (connect(sockfd,(sockaddr*)&serv_addr,sizeof(serv_addr)) < 0) |
---|
922 | { WerrorS("ERROR connecting"); return TRUE; } |
---|
923 | //PrintS("connected\n");mflush(); |
---|
924 | d->f_read=fdopen(sockfd,"r"); |
---|
925 | d->fd_read=sockfd; |
---|
926 | d->f_write=fdopen(sockfd,"w"); |
---|
927 | d->fd_write=sockfd; |
---|
928 | SI_LINK_SET_RW_OPEN_P(l); |
---|
929 | omFree(host); |
---|
930 | } |
---|
931 | else |
---|
932 | { |
---|
933 | l->data=NULL; |
---|
934 | omFree(d); |
---|
935 | return TRUE; |
---|
936 | } |
---|
937 | } |
---|
938 | // ====================================================================== |
---|
939 | else |
---|
940 | { |
---|
941 | // normal link to a file |
---|
942 | FILE *outfile; |
---|
943 | char *filename=l->name; |
---|
944 | |
---|
945 | if(filename[0]=='>') |
---|
946 | { |
---|
947 | if (filename[1]=='>') |
---|
948 | { |
---|
949 | filename+=2; |
---|
950 | mode = "a"; |
---|
951 | } |
---|
952 | else |
---|
953 | { |
---|
954 | filename++; |
---|
955 | mode="w"; |
---|
956 | } |
---|
957 | } |
---|
958 | outfile=myfopen(filename,mode); |
---|
959 | if (outfile!=NULL) |
---|
960 | { |
---|
961 | if (strcmp(l->mode,"r")==0) d->f_read = outfile; |
---|
962 | else |
---|
963 | { |
---|
964 | d->f_write = outfile; |
---|
965 | fprintf(d->f_write,"98 %d %d %u %u\n",SSI_VERSION,MAX_TOK,test,verbose); |
---|
966 | } |
---|
967 | } |
---|
968 | else |
---|
969 | { |
---|
970 | omFree(d); |
---|
971 | l->data=NULL; |
---|
972 | return TRUE; |
---|
973 | } |
---|
974 | } |
---|
975 | } |
---|
976 | |
---|
977 | return FALSE; |
---|
978 | } |
---|
979 | |
---|
980 | //**************************************************************************/ |
---|
981 | BOOLEAN ssiClose(si_link l) |
---|
982 | { |
---|
983 | ssiInfo *d = (ssiInfo *)l->data; |
---|
984 | if (d!=NULL) |
---|
985 | { |
---|
986 | if (d->r!=NULL) rKill(d->r); |
---|
987 | if (d->f_read!=NULL) fclose(d->f_read); |
---|
988 | if ((strcmp(l->mode,"tcp")==0) |
---|
989 | || (strcmp(l->mode,"fork")==0)) |
---|
990 | { |
---|
991 | link_list hh=ssiToBeClosed; |
---|
992 | if (hh!=NULL) |
---|
993 | { |
---|
994 | if (hh->l==l) |
---|
995 | { |
---|
996 | ssiToBeClosed=(link_list)hh->next; |
---|
997 | omFreeSize(hh,sizeof(link_struct)); |
---|
998 | } |
---|
999 | else while(hh!=NULL) |
---|
1000 | { |
---|
1001 | link_list hhh=(link_list)hh->next; |
---|
1002 | if (hhh->l==l) |
---|
1003 | { |
---|
1004 | hh->next=hhh->next; |
---|
1005 | omFreeSize(hhh,sizeof(link_struct)); |
---|
1006 | break; |
---|
1007 | } |
---|
1008 | hh=(link_list)hh->next; |
---|
1009 | } |
---|
1010 | } |
---|
1011 | if (d->send_quit_at_exit) |
---|
1012 | { |
---|
1013 | fprintf(d->f_write,"99\n");fflush(d->f_write); |
---|
1014 | } |
---|
1015 | } |
---|
1016 | if (d->f_write!=NULL) fclose(d->f_write); |
---|
1017 | if (d->pid!=0) |
---|
1018 | { |
---|
1019 | kill(d->pid,15); |
---|
1020 | waitpid(d->pid,NULL,WNOHANG); |
---|
1021 | kill(d->pid,9); // just to be sure |
---|
1022 | waitpid(d->pid,NULL,0); |
---|
1023 | } |
---|
1024 | omFreeSize((ADDRESS)d,(sizeof *d)); |
---|
1025 | } |
---|
1026 | l->data=NULL; |
---|
1027 | SI_LINK_SET_CLOSE_P(l); |
---|
1028 | return FALSE; |
---|
1029 | } |
---|
1030 | |
---|
1031 | //**************************************************************************/ |
---|
1032 | leftv ssiRead1(si_link l) |
---|
1033 | { |
---|
1034 | ssiInfo *d = (ssiInfo *)l->data; |
---|
1035 | d->ungetc_buf='\0'; |
---|
1036 | leftv res=(leftv)omAlloc0(sizeof(sleftv)); |
---|
1037 | int t=0; |
---|
1038 | SSI_BLOCK_CHLD; |
---|
1039 | fscanf(d->f_read,"%d",&t); |
---|
1040 | SSI_UNBLOCK_CHLD; |
---|
1041 | //Print("got type %d\n",t); |
---|
1042 | switch(t) |
---|
1043 | { |
---|
1044 | case 1:res->rtyp=INT_CMD; |
---|
1045 | res->data=(char *)ssiReadInt(d->f_read); |
---|
1046 | break; |
---|
1047 | case 2:res->rtyp=STRING_CMD; |
---|
1048 | res->data=(char *)ssiReadString(d); |
---|
1049 | break; |
---|
1050 | case 3:res->rtyp=NUMBER_CMD; |
---|
1051 | res->data=(char *)ssiReadNumber(d); |
---|
1052 | break; |
---|
1053 | case 4:res->rtyp=BIGINT_CMD; |
---|
1054 | res->data=(char *)ssiReadBigInt(d); |
---|
1055 | break; |
---|
1056 | case 15: |
---|
1057 | case 5:{ |
---|
1058 | d->r=ssiReadRing(d); |
---|
1059 | d->r->ref++; |
---|
1060 | res->rtyp=RING_CMD; |
---|
1061 | res->data=(char*)d->r; |
---|
1062 | // we are in the top-level, so set the basering to d->r: |
---|
1063 | ssiSetCurrRing(d->r); |
---|
1064 | if (t==15) return ssiRead1(l); |
---|
1065 | } |
---|
1066 | break; |
---|
1067 | case 6:res->rtyp=POLY_CMD; |
---|
1068 | if (d->r==NULL) goto no_ring; |
---|
1069 | res->data=(char*)ssiReadPoly(d); |
---|
1070 | break; |
---|
1071 | case 7:res->rtyp=IDEAL_CMD; |
---|
1072 | if (d->r==NULL) goto no_ring; |
---|
1073 | res->data=(char*)ssiReadIdeal(d); |
---|
1074 | break; |
---|
1075 | case 8:res->rtyp=MATRIX_CMD; |
---|
1076 | if (d->r==NULL) goto no_ring; |
---|
1077 | res->data=(char*)ssiReadMatrix(d); |
---|
1078 | break; |
---|
1079 | case 9:res->rtyp=VECTOR_CMD; |
---|
1080 | if (d->r==NULL) goto no_ring; |
---|
1081 | res->data=(char*)ssiReadPoly(d); |
---|
1082 | break; |
---|
1083 | case 10:res->rtyp=MODUL_CMD; |
---|
1084 | if (d->r==NULL) goto no_ring; |
---|
1085 | res->data=(char*)ssiReadIdeal(d); |
---|
1086 | break; |
---|
1087 | case 11: |
---|
1088 | { |
---|
1089 | res->rtyp=COMMAND; |
---|
1090 | res->data=ssiReadCommand(l); |
---|
1091 | int nok=res->Eval(); |
---|
1092 | if (nok) WerrorS("error in eval"); |
---|
1093 | break; |
---|
1094 | } |
---|
1095 | case 12: /*DEF_CMD*/ |
---|
1096 | { |
---|
1097 | res->rtyp=0; |
---|
1098 | res->name=(char *)ssiReadString(d); |
---|
1099 | int nok=res->Eval(); |
---|
1100 | if (nok) WerrorS("error in name lookup"); |
---|
1101 | break; |
---|
1102 | } |
---|
1103 | case 13: res->rtyp=PROC_CMD; |
---|
1104 | res->data=ssiReadProc(d); |
---|
1105 | break; |
---|
1106 | case 14: res->rtyp=LIST_CMD; |
---|
1107 | res->data=ssiReadList(l); |
---|
1108 | break; |
---|
1109 | case 16: res->rtyp=NONE; res->data=NULL; |
---|
1110 | break; |
---|
1111 | case 17: res->rtyp=INTVEC_CMD; |
---|
1112 | res->data=ssiReadIntvec(d); |
---|
1113 | break; |
---|
1114 | case 20: ssiReadBlackbox(res,l); |
---|
1115 | break; |
---|
1116 | // ------------ |
---|
1117 | case 98: // version |
---|
1118 | { |
---|
1119 | int n98_v,n98_m; |
---|
1120 | BITSET n98_o1,n98_o2; |
---|
1121 | SSI_BLOCK_CHLD; |
---|
1122 | fscanf(d->f_read,"%d %d %u %u\n",&n98_v,&n98_m,&n98_o1,&n98_o2); |
---|
1123 | SSI_UNBLOCK_CHLD; |
---|
1124 | if ((n98_v!=SSI_VERSION) ||(n98_m!=MAX_TOK)) |
---|
1125 | { |
---|
1126 | Print("incompatible versions of ssi: %d/%d vs %d/%d", |
---|
1127 | SSI_VERSION,MAX_TOK,n98_v,n98_m); |
---|
1128 | } |
---|
1129 | #ifndef NDEBUG |
---|
1130 | if (TEST_OPT_DEBUG) |
---|
1131 | Print("// opening ssi-%d, MAX_TOK=%d\n",n98_v,n98_m); |
---|
1132 | #endif |
---|
1133 | test=n98_o1; |
---|
1134 | verbose=n98_o2; |
---|
1135 | return ssiRead1(l); |
---|
1136 | } |
---|
1137 | case 99: ssiClose(l); exit(0); |
---|
1138 | case 0: if (feof(d->f_read)) |
---|
1139 | { |
---|
1140 | ssiClose(l); |
---|
1141 | res->rtyp=DEF_CMD; |
---|
1142 | break; |
---|
1143 | } |
---|
1144 | default: Werror("not implemented (t:%d)",t); |
---|
1145 | omFreeSize(res,sizeof(sleftv)); |
---|
1146 | res=NULL; |
---|
1147 | break; |
---|
1148 | } |
---|
1149 | return res; |
---|
1150 | no_ring: WerrorS("no ring"); |
---|
1151 | omFreeSize(res,sizeof(sleftv)); |
---|
1152 | return NULL; |
---|
1153 | } |
---|
1154 | //**************************************************************************/ |
---|
1155 | BOOLEAN ssiWrite(si_link l, leftv data) |
---|
1156 | { |
---|
1157 | if(SI_LINK_W_OPEN_P(l)==0) slOpen(l,SI_LINK_OPEN|SI_LINK_WRITE,NULL); |
---|
1158 | ssiInfo *d = (ssiInfo *)l->data; |
---|
1159 | d->level++; |
---|
1160 | //FILE *fich=d->f; |
---|
1161 | while (data!=NULL) |
---|
1162 | { |
---|
1163 | int tt=data->Typ(); |
---|
1164 | void *dd=data->Data(); |
---|
1165 | |
---|
1166 | switch(tt /*data->Typ()*/) |
---|
1167 | { |
---|
1168 | case NONE/* nothing*/:fprintf(d->f_write,"16 "); |
---|
1169 | break; |
---|
1170 | case STRING_CMD: fprintf(d->f_write,"2 "); |
---|
1171 | ssiWriteString(d,(char *)dd); |
---|
1172 | break; |
---|
1173 | case INT_CMD: fprintf(d->f_write,"1 "); |
---|
1174 | ssiWriteInt(d,(int)(long)dd); |
---|
1175 | break; |
---|
1176 | case BIGINT_CMD:fprintf(d->f_write,"4 "); |
---|
1177 | ssiWriteBigInt(d,(number)dd); |
---|
1178 | break; |
---|
1179 | case NUMBER_CMD: |
---|
1180 | if (d->r!=currRing) |
---|
1181 | { |
---|
1182 | fprintf(d->f_write,"15 "); |
---|
1183 | ssiWriteRing(d,currRing); |
---|
1184 | if (d->level<=1) fprintf(d->f_write,"\n"); |
---|
1185 | } |
---|
1186 | fprintf(d->f_write,"3 "); |
---|
1187 | ssiWriteNumber(d,(number)dd); |
---|
1188 | break; |
---|
1189 | case RING_CMD:fprintf(d->f_write,"5 "); |
---|
1190 | ssiWriteRing(d,(ring)dd); |
---|
1191 | break; |
---|
1192 | case POLY_CMD: |
---|
1193 | case VECTOR_CMD: |
---|
1194 | if (d->r!=currRing) |
---|
1195 | { |
---|
1196 | fprintf(d->f_write,"15 "); |
---|
1197 | ssiWriteRing(d,currRing); |
---|
1198 | if (d->level<=1) fprintf(d->f_write,"\n"); |
---|
1199 | } |
---|
1200 | if(tt==POLY_CMD) fprintf(d->f_write,"6 "); |
---|
1201 | else fprintf(d->f_write,"9 "); |
---|
1202 | ssiWritePoly(d,tt,(poly)dd); |
---|
1203 | break; |
---|
1204 | case IDEAL_CMD: |
---|
1205 | case MODUL_CMD: |
---|
1206 | case MATRIX_CMD: |
---|
1207 | if (d->r!=currRing) |
---|
1208 | { |
---|
1209 | fprintf(d->f_write,"15 "); |
---|
1210 | ssiWriteRing(d,currRing); |
---|
1211 | if (d->level<=1) fprintf(d->f_write,"\n"); |
---|
1212 | } |
---|
1213 | if(tt==IDEAL_CMD) fprintf(d->f_write,"7 "); |
---|
1214 | else if(tt==MATRIX_CMD) fprintf(d->f_write,"8 "); |
---|
1215 | else fprintf(d->f_write,"10 "); |
---|
1216 | ssiWriteIdeal(d,tt,(ideal)dd); |
---|
1217 | break; |
---|
1218 | case COMMAND: |
---|
1219 | fprintf(d->f_write,"11 "); |
---|
1220 | ssiWriteCommand(l,(command)dd); |
---|
1221 | break; |
---|
1222 | case DEF_CMD: /* not evaluated stuff in quotes */ |
---|
1223 | fprintf(d->f_write,"12 "); |
---|
1224 | ssiWriteString(d,data->Name()); |
---|
1225 | break; |
---|
1226 | case PROC_CMD: |
---|
1227 | fprintf(d->f_write,"13 "); |
---|
1228 | ssiWriteProc(d,(procinfov)dd); |
---|
1229 | break; |
---|
1230 | case LIST_CMD: |
---|
1231 | fprintf(d->f_write,"14 "); |
---|
1232 | ssiWriteList(l,(lists)dd); |
---|
1233 | break; |
---|
1234 | case INTVEC_CMD: |
---|
1235 | fprintf(d->f_write,"17 "); |
---|
1236 | ssiWriteIntvec(d,(intvec *)dd); |
---|
1237 | break; |
---|
1238 | default: |
---|
1239 | if (tt>MAX_TOK) |
---|
1240 | { |
---|
1241 | blackbox *b=getBlackboxStuff(tt); |
---|
1242 | fprintf(d->f_write,"20 "); |
---|
1243 | b->blackbox_serialize(b,dd,l); |
---|
1244 | } |
---|
1245 | else |
---|
1246 | { |
---|
1247 | Werror("not implemented (t:%d, rtyp:%d)",tt, data->rtyp); |
---|
1248 | d->level=0; |
---|
1249 | return TRUE; |
---|
1250 | } |
---|
1251 | break; |
---|
1252 | } |
---|
1253 | if (d->level<=1) { fprintf(d->f_write,"\n"); fflush(d->f_write); } |
---|
1254 | data=data->next; |
---|
1255 | } |
---|
1256 | d->level--; |
---|
1257 | return FALSE; |
---|
1258 | } |
---|
1259 | |
---|
1260 | si_link_extension slInitSsiExtension(si_link_extension s) |
---|
1261 | { |
---|
1262 | s->Open=ssiOpen; |
---|
1263 | s->Close=ssiClose; |
---|
1264 | s->Kill=ssiClose; |
---|
1265 | s->Read=ssiRead1; |
---|
1266 | s->Read2=(slRead2Proc)NULL; |
---|
1267 | s->Write=ssiWrite; |
---|
1268 | |
---|
1269 | s->Status=slStatusSsi; |
---|
1270 | s->type="ssi"; |
---|
1271 | return s; |
---|
1272 | } |
---|
1273 | |
---|
1274 | const char* slStatusSsi(si_link l, const char* request) |
---|
1275 | { |
---|
1276 | ssiInfo *d=(ssiInfo*)l->data; |
---|
1277 | if (d==NULL) return "not open"; |
---|
1278 | if (((strcmp(l->mode,"fork")==0)||(strcmp(l->mode,"tcp")==0)) |
---|
1279 | && (strcmp(request, "read") == 0)) |
---|
1280 | { |
---|
1281 | fd_set mask, fdmask; |
---|
1282 | struct timeval wt; |
---|
1283 | if (d->ungetc_buf) return "ready"; |
---|
1284 | loop |
---|
1285 | { |
---|
1286 | /* Don't block. Return socket status immediately. */ |
---|
1287 | wt.tv_sec = 0; |
---|
1288 | wt.tv_usec = 0; |
---|
1289 | |
---|
1290 | FD_ZERO(&mask); |
---|
1291 | FD_SET(d->fd_read, &mask); |
---|
1292 | //Print("test fd %d\n",d->fd_read); |
---|
1293 | /* check with select: chars waiting: no -> not ready */ |
---|
1294 | switch (select(d->fd_read+1, &mask, NULL, NULL, &wt)) |
---|
1295 | { |
---|
1296 | case 0: /* not ready */ return "not ready"; |
---|
1297 | case -1: /*error*/ return "error"; |
---|
1298 | case 1: /*ready ? */ break; |
---|
1299 | } |
---|
1300 | /* yes: read 1 char*/ |
---|
1301 | /* if \n, check again with select else ungetc(c), ready*/ |
---|
1302 | int c=fgetc(d->f_read); |
---|
1303 | //Print("try c=%d\n",c); |
---|
1304 | if (c== -1) return "eof"; /* eof or error */ |
---|
1305 | else if (isdigit(c)) |
---|
1306 | { ungetc(c,d->f_read); d->ungetc_buf='\1'; return "ready"; } |
---|
1307 | else if (c>' ') |
---|
1308 | { |
---|
1309 | Werror("unknown char in ssiLink(%d)",c); |
---|
1310 | return "error"; |
---|
1311 | } |
---|
1312 | /* else: next char */ |
---|
1313 | } |
---|
1314 | } |
---|
1315 | else if (strcmp(request, "read") == 0) |
---|
1316 | { |
---|
1317 | if (SI_LINK_R_OPEN_P(l) && (!feof(d->f_read))) return "ready"; |
---|
1318 | else return "not ready"; |
---|
1319 | } |
---|
1320 | else if (strcmp(request, "write") == 0) |
---|
1321 | { |
---|
1322 | if (SI_LINK_W_OPEN_P(l)) return "ready"; |
---|
1323 | else return "not ready"; |
---|
1324 | } |
---|
1325 | else return "unknown status request"; |
---|
1326 | } |
---|
1327 | |
---|
1328 | int slStatusSsiL(lists L, int timeout) |
---|
1329 | { |
---|
1330 | // input: L: a list with links of type |
---|
1331 | // ssi-fork, ssi-tcp, MPtcp-fork or MPtcp-launch. |
---|
1332 | // Note: Not every entry in L must be set. |
---|
1333 | // timeout: timeout for select in micro-seconds |
---|
1334 | // or -1 for infinity |
---|
1335 | // or 0 for polling |
---|
1336 | // returns: ERROR (via Werror): L has wrong elements or link not open |
---|
1337 | // -2: select returns an error |
---|
1338 | // -1: the read state of all links is eof |
---|
1339 | // 0: timeout (or polling): none ready, |
---|
1340 | // i>0: (at least) L[i] is ready |
---|
1341 | si_link l; |
---|
1342 | ssiInfo *d; |
---|
1343 | #ifdef HAVE_MPSR |
---|
1344 | MP_Link_pt dd; |
---|
1345 | #endif |
---|
1346 | int d_fd; |
---|
1347 | fd_set mask, fdmask; |
---|
1348 | FD_ZERO(&fdmask); |
---|
1349 | FD_ZERO(&mask); |
---|
1350 | int max_fd=0; /* 1 + max fd in fd_set */ |
---|
1351 | |
---|
1352 | /* timeout */ |
---|
1353 | #ifdef HAVE_PSELECT |
---|
1354 | struct timespec wt; |
---|
1355 | struct timespec *wt_ptr=&wt; |
---|
1356 | #else |
---|
1357 | struct timeval wt; |
---|
1358 | struct timeval *wt_ptr=&wt; |
---|
1359 | #endif |
---|
1360 | int startingtime = getRTimer()/TIMER_RESOLUTION; // in seconds |
---|
1361 | if (timeout== -1) |
---|
1362 | { |
---|
1363 | wt_ptr=NULL; |
---|
1364 | } |
---|
1365 | else |
---|
1366 | { |
---|
1367 | wt.tv_sec = timeout / 1000000; |
---|
1368 | #ifdef HAVE_PSELECT |
---|
1369 | wt.tv_nsec = 1000 * (timeout % 1000000); |
---|
1370 | #else |
---|
1371 | wt.tv_usec = timeout % 1000000; |
---|
1372 | #endif |
---|
1373 | } |
---|
1374 | |
---|
1375 | /* signal mask for pselect() */ |
---|
1376 | sigset_t sigmask; |
---|
1377 | if(sigprocmask(SIG_SETMASK, NULL, &sigmask) < 0) |
---|
1378 | { |
---|
1379 | WerrorS("error in sigprocmask()"); |
---|
1380 | return -2; |
---|
1381 | } |
---|
1382 | if(sigaddset(&sigmask, SIGCHLD) < 0) |
---|
1383 | { |
---|
1384 | WerrorS("error in sigaddset()"); |
---|
1385 | return -2; |
---|
1386 | } |
---|
1387 | |
---|
1388 | /* auxiliary variables */ |
---|
1389 | int i; |
---|
1390 | int j; |
---|
1391 | int k; |
---|
1392 | int s; |
---|
1393 | char fdmaskempty; |
---|
1394 | |
---|
1395 | /* check the links and fill in fdmask */ |
---|
1396 | for(i=L->nr; i>=0; i--) |
---|
1397 | { |
---|
1398 | if (L->m[i].Typ()!=DEF_CMD) |
---|
1399 | { |
---|
1400 | if (L->m[i].Typ()!=LINK_CMD) |
---|
1401 | { WerrorS("all elements must be of type link"); return -2;} |
---|
1402 | l=(si_link)L->m[i].Data(); |
---|
1403 | if(SI_LINK_OPEN_P(l)==0) |
---|
1404 | { WerrorS("all links must be open"); return -2;} |
---|
1405 | if (((strcmp(l->m->type,"ssi")!=0) && (strcmp(l->m->type,"MPtcp")!=0)) |
---|
1406 | || ((strcmp(l->mode,"fork")!=0) && (strcmp(l->mode,"tcp")!=0) |
---|
1407 | && (strcmp(l->mode,"launch")!=0))) |
---|
1408 | { |
---|
1409 | WerrorS("all links must be of type ssi:fork, ssi:tcp, MPtcp:fork\n"); |
---|
1410 | WerrorS("or MPtcp:launch"); |
---|
1411 | return -2; |
---|
1412 | } |
---|
1413 | #ifdef HAVE_MPSR |
---|
1414 | if (strcmp(l->m->type,"ssi")==0) |
---|
1415 | { |
---|
1416 | d=(ssiInfo*)l->data; |
---|
1417 | d_fd=d->fd_read; |
---|
1418 | } |
---|
1419 | else |
---|
1420 | { |
---|
1421 | dd=(MP_Link_pt)l->data; |
---|
1422 | d_fd=((MP_TCP_t *)dd->transp.private1)->sock; |
---|
1423 | } |
---|
1424 | #else |
---|
1425 | d=(ssiInfo*)l->data; |
---|
1426 | d_fd=d->fd_read; |
---|
1427 | #endif |
---|
1428 | FD_SET(d_fd, &fdmask); |
---|
1429 | if (d_fd > max_fd) max_fd=d_fd; |
---|
1430 | } |
---|
1431 | } |
---|
1432 | max_fd++; |
---|
1433 | |
---|
1434 | do_select: |
---|
1435 | /* copy fdmask to mask */ |
---|
1436 | FD_ZERO(&mask); |
---|
1437 | for(k = 0; k < max_fd; k++) |
---|
1438 | { |
---|
1439 | if(FD_ISSET(k, &fdmask)) |
---|
1440 | { |
---|
1441 | FD_SET(k, &mask); |
---|
1442 | } |
---|
1443 | } |
---|
1444 | |
---|
1445 | /* check with select: chars waiting: no -> not ready */ |
---|
1446 | #ifdef HAVE_PSELECT |
---|
1447 | s = pselect(max_fd, &mask, NULL, NULL, wt_ptr, &sigmask); |
---|
1448 | #else |
---|
1449 | SSI_BLOCK_CHLD; |
---|
1450 | s = select(max_fd, &mask, NULL, NULL, wt_ptr); |
---|
1451 | SSI_UNBLOCK_CHLD; |
---|
1452 | #endif |
---|
1453 | |
---|
1454 | if (s==-1) |
---|
1455 | { |
---|
1456 | WerrorS("error in select call"); |
---|
1457 | return -2; /*error*/ |
---|
1458 | } |
---|
1459 | if (s==0) |
---|
1460 | { |
---|
1461 | return 0; /*poll: not ready */ |
---|
1462 | } |
---|
1463 | else /* s>0, at least one ready (the number of fd which are ready is s)*/ |
---|
1464 | { |
---|
1465 | j=0; |
---|
1466 | while (j<=max_fd) { if (FD_ISSET(j,&mask)) break; j++; } |
---|
1467 | for(i=L->nr; i>=0; i--) |
---|
1468 | { |
---|
1469 | if (L->m[i].rtyp==LINK_CMD) |
---|
1470 | { |
---|
1471 | l=(si_link)L->m[i].Data(); |
---|
1472 | #ifdef HAVE_MPSR |
---|
1473 | if (strcmp(l->m->type,"ssi")!=0) |
---|
1474 | { |
---|
1475 | // for MP links, return here: |
---|
1476 | dd=(MP_Link_pt)l->data; |
---|
1477 | d_fd=((MP_TCP_t *)dd->transp.private1)->sock; |
---|
1478 | if(j==d_fd) return i+1; |
---|
1479 | } |
---|
1480 | else |
---|
1481 | { |
---|
1482 | d=(ssiInfo*)l->data; |
---|
1483 | d_fd=d->fd_read; |
---|
1484 | if(j==d_fd) break; |
---|
1485 | } |
---|
1486 | #else |
---|
1487 | d=(ssiInfo*)l->data; |
---|
1488 | d_fd=d->fd_read; |
---|
1489 | if(j==d_fd) break; |
---|
1490 | #endif |
---|
1491 | } |
---|
1492 | } |
---|
1493 | // only ssi links: |
---|
1494 | if (d->ungetc_buf) return i+1; |
---|
1495 | loop |
---|
1496 | { |
---|
1497 | /* yes: read 1 char*/ |
---|
1498 | /* if \n, check again with select else ungetc(c), ready*/ |
---|
1499 | /* setting: d: current ssiInfo, j current fd, i current entry in L*/ |
---|
1500 | int c=fgetc(d->f_read); |
---|
1501 | //Print("try c=%d\n",c); |
---|
1502 | if (c== -1) /* eof */ |
---|
1503 | { |
---|
1504 | FD_CLR(j,&fdmask); |
---|
1505 | fdmaskempty = 1; |
---|
1506 | for(k = 0; k < max_fd; k++) |
---|
1507 | { |
---|
1508 | if(FD_ISSET(k, &fdmask)) |
---|
1509 | { |
---|
1510 | fdmaskempty = 0; |
---|
1511 | break; |
---|
1512 | } |
---|
1513 | } |
---|
1514 | if(fdmaskempty) |
---|
1515 | { |
---|
1516 | return -1; |
---|
1517 | } |
---|
1518 | if(timeout != -1) |
---|
1519 | { |
---|
1520 | timeout = si_max(0, |
---|
1521 | timeout - 1000000*(getRTimer()/TIMER_RESOLUTION - startingtime)); |
---|
1522 | wt.tv_sec = timeout / 1000000; |
---|
1523 | #ifdef HAVE_PSELECT |
---|
1524 | wt.tv_nsec = 1000 * (timeout % 1000000); |
---|
1525 | #else |
---|
1526 | wt.tv_usec = (timeout % 1000000); |
---|
1527 | #endif |
---|
1528 | } |
---|
1529 | goto do_select; |
---|
1530 | } |
---|
1531 | |
---|
1532 | else if (isdigit(c)) |
---|
1533 | { ungetc(c,d->f_read); d->ungetc_buf='\1'; return i+1; } |
---|
1534 | else if (c>' ') |
---|
1535 | { |
---|
1536 | Werror("unknown char in ssiLink(%d)",c); |
---|
1537 | return -2; |
---|
1538 | } |
---|
1539 | /* else: next char */ |
---|
1540 | } |
---|
1541 | } |
---|
1542 | } |
---|
1543 | |
---|
1544 | int ssiBatch(const char *host, const char * port) |
---|
1545 | /* return 0 on success, >0 else*/ |
---|
1546 | { |
---|
1547 | si_link l=(si_link) omAlloc0Bin(sip_link_bin); |
---|
1548 | char *buf=(char*)omAlloc(256); |
---|
1549 | sprintf(buf,"ssi:connect %s:%s",host,port); |
---|
1550 | slInit(l, buf); |
---|
1551 | slOpen(l,SI_LINK_OPEN,NULL); |
---|
1552 | SI_LINK_SET_RW_OPEN_P(l); |
---|
1553 | |
---|
1554 | idhdl id = enterid(omStrDup("link_ll"), 0, LINK_CMD, &IDROOT, FALSE); |
---|
1555 | IDLINK(id) = l; |
---|
1556 | |
---|
1557 | loop |
---|
1558 | { |
---|
1559 | leftv h=ssiRead1(l); /*contains an exit.... */ |
---|
1560 | if (feErrors != NULL && *feErrors != '\0') |
---|
1561 | { |
---|
1562 | // handle errors: |
---|
1563 | PrintS(feErrors); /* currently quite simple */ |
---|
1564 | *feErrors = '\0'; |
---|
1565 | } |
---|
1566 | ssiWrite(l,h); |
---|
1567 | h->CleanUp(); |
---|
1568 | omFreeBin(h, sleftv_bin); |
---|
1569 | } |
---|
1570 | /* never reached*/ |
---|
1571 | exit(0); |
---|
1572 | } |
---|
1573 | |
---|
1574 | // ---------------------------------------------------------------- |
---|
1575 | // format |
---|
1576 | // 1 int %d |
---|
1577 | // 2 string <len> %s |
---|
1578 | // 3 number |
---|
1579 | // 4 bigint 4 %d or 3 <mpz_t> |
---|
1580 | // 5 ring |
---|
1581 | // 6 poly |
---|
1582 | // 7 ideal |
---|
1583 | // 8 matrix |
---|
1584 | // 9 vector |
---|
1585 | // 10 module |
---|
1586 | // 11 command |
---|
1587 | // 12 def <len> %s |
---|
1588 | // 13 proc <len> %s |
---|
1589 | // 14 list %d <elem1> .... |
---|
1590 | // 15 setring ....... |
---|
1591 | // 16 nothing |
---|
1592 | // 17 intvec <len> ... |
---|
1593 | // |
---|
1594 | // 20 blackbox <name> ... |
---|
1595 | // |
---|
1596 | // 98: verify version: <ssi-version> <MAX_TOK> <OPT1> <OPT2> |
---|
1597 | // 99: quit Singular |
---|