1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /*************************************************************** |
---|
5 | * File: ssiLink.h |
---|
6 | * Purpose: declaration of sl_link routines for ssi |
---|
7 | ***************************************************************/ |
---|
8 | #define TRANSEXT_PRIVATES 1 /* allow access to transext internals */ |
---|
9 | |
---|
10 | #include "kernel/mod2.h" |
---|
11 | |
---|
12 | #include "misc/intvec.h" |
---|
13 | #include "misc/options.h" |
---|
14 | |
---|
15 | #include "reporter/si_signals.h" |
---|
16 | #include "reporter/s_buff.h" |
---|
17 | |
---|
18 | #include "coeffs/bigintmat.h" |
---|
19 | #include "coeffs/longrat.h" |
---|
20 | |
---|
21 | #include "polys/monomials/ring.h" |
---|
22 | #include "polys/monomials/p_polys.h" |
---|
23 | #include "polys/ext_fields/transext.h" |
---|
24 | #include "polys/simpleideals.h" |
---|
25 | #include "polys/matpol.h" |
---|
26 | |
---|
27 | #include "kernel/oswrapper/timer.h" |
---|
28 | #include "kernel/oswrapper/feread.h" |
---|
29 | #include "kernel/oswrapper/rlimit.h" |
---|
30 | |
---|
31 | #include "Singular/tok.h" |
---|
32 | #include "Singular/ipid.h" |
---|
33 | #include "Singular/ipshell.h" |
---|
34 | #include "Singular/subexpr.h" |
---|
35 | #include "Singular/links/silink.h" |
---|
36 | #include "Singular/cntrlc.h" |
---|
37 | #include "Singular/lists.h" |
---|
38 | #include "Singular/blackbox.h" |
---|
39 | #include "Singular/links/ssiLink.h" |
---|
40 | |
---|
41 | #ifdef HAVE_SIMPLEIPC |
---|
42 | #include "Singular/links/simpleipc.h" |
---|
43 | #endif |
---|
44 | |
---|
45 | #include <errno.h> |
---|
46 | #include <sys/types.h> /* for portability */ |
---|
47 | #include <ctype.h> /*for isdigit*/ |
---|
48 | #include <netdb.h> |
---|
49 | #include <netinet/in.h> /* for htons etc.*/ |
---|
50 | |
---|
51 | #define SSI_VERSION 13 |
---|
52 | // 5->6: changed newstruct representation |
---|
53 | // 6->7: attributes |
---|
54 | // 7->8: qring |
---|
55 | // 8->9: module: added rank |
---|
56 | // 9->10: tokens in grammar.h/tok.h reorganized |
---|
57 | // 10->11: extended ring descr. for named coeffs (not in used until 4.1) |
---|
58 | // 11->12: add rank to ideal/module, add smatrix |
---|
59 | // 12->13: NC rings |
---|
60 | |
---|
61 | VAR link_list ssiToBeClosed=NULL; |
---|
62 | VAR volatile BOOLEAN ssiToBeClosed_inactive=TRUE; |
---|
63 | |
---|
64 | // forward declarations: |
---|
65 | void ssiWritePoly_R(const ssiInfo *d, int typ, poly p, const ring r); |
---|
66 | void ssiWriteIdeal_R(const ssiInfo *d, int typ,const ideal I, const ring r); |
---|
67 | poly ssiReadPoly_R(const ssiInfo *D, const ring r); |
---|
68 | ideal ssiReadIdeal_R(const ssiInfo *d,const ring r); |
---|
69 | |
---|
70 | // the helper functions: |
---|
71 | BOOLEAN ssiSetCurrRing(const ring r) /* returned: not accepted */ |
---|
72 | { |
---|
73 | // if (currRing!=NULL) |
---|
74 | // Print("need to change the ring, currRing:%s, switch to: ssiRing%d\n",IDID(currRingHdl),nr); |
---|
75 | // else |
---|
76 | // Print("no ring, switch to ssiRing%d\n",nr); |
---|
77 | if (!rEqual(r,currRing,1)) |
---|
78 | { |
---|
79 | char name[20]; |
---|
80 | int nr=0; |
---|
81 | idhdl h=NULL; |
---|
82 | loop |
---|
83 | { |
---|
84 | sprintf(name,"ssiRing%d",nr); nr++; |
---|
85 | h=IDROOT->get(name, 0); |
---|
86 | if (h==NULL) |
---|
87 | { |
---|
88 | h=enterid(name,0,RING_CMD,&IDROOT,FALSE); |
---|
89 | IDRING(h)=r; |
---|
90 | r->ref++; |
---|
91 | break; |
---|
92 | } |
---|
93 | else if ((IDTYP(h)==RING_CMD) |
---|
94 | && (rEqual(r,IDRING(h),1))) |
---|
95 | break; |
---|
96 | } |
---|
97 | rSetHdl(h); |
---|
98 | return FALSE; |
---|
99 | } |
---|
100 | else |
---|
101 | { |
---|
102 | rKill(r); |
---|
103 | return TRUE; |
---|
104 | } |
---|
105 | } |
---|
106 | // the implementation of the functions: |
---|
107 | void ssiWriteInt(const ssiInfo *d,const int i) |
---|
108 | { |
---|
109 | fprintf(d->f_write,"%d ",i); |
---|
110 | //if (d->f_debug!=NULL) fprintf(d->f_debug,"int: %d ",i); |
---|
111 | } |
---|
112 | |
---|
113 | void ssiWriteString(const ssiInfo *d,const char *s) |
---|
114 | { |
---|
115 | fprintf(d->f_write,"%d %s ",(int)strlen(s),s); |
---|
116 | //if (d->f_debug!=NULL) fprintf(d->f_debug,"stringi: %d \"%s\" ",strlen(s),s); |
---|
117 | } |
---|
118 | |
---|
119 | void ssiWriteBigInt(const ssiInfo *d, const number n) |
---|
120 | { |
---|
121 | n_WriteFd(n,d,coeffs_BIGINT); |
---|
122 | } |
---|
123 | |
---|
124 | void ssiWriteNumber_CF(const ssiInfo *d, const number n, const coeffs cf) |
---|
125 | { |
---|
126 | // syntax is as follows: |
---|
127 | // case 1 Z/p: 3 <int> |
---|
128 | // case 2 Q: 3 4 <int> |
---|
129 | // or 3 0 <mpz_t nominator> <mpz_t denominator> |
---|
130 | // or 3 1 dto. |
---|
131 | // or 3 3 <mpz_t nominator> |
---|
132 | // or 3 5 <mpz_t raw nom.> <mpz_t raw denom.> |
---|
133 | // or 3 6 <mpz_t raw nom.> <mpz_t raw denom.> |
---|
134 | // or 3 8 <mpz_t raw nom.> |
---|
135 | if (getCoeffType(cf)==n_transExt) |
---|
136 | { |
---|
137 | fraction f=(fraction)n; |
---|
138 | ssiWritePoly_R(d,POLY_CMD,NUM(f),cf->extRing); |
---|
139 | ssiWritePoly_R(d,POLY_CMD,DEN(f),cf->extRing); |
---|
140 | } |
---|
141 | else if (getCoeffType(cf)==n_algExt) |
---|
142 | { |
---|
143 | ssiWritePoly_R(d,POLY_CMD,(poly)n,cf->extRing); |
---|
144 | } |
---|
145 | else if (cf->cfWriteFd!=NULL) |
---|
146 | { |
---|
147 | n_WriteFd(n,d,cf); |
---|
148 | } |
---|
149 | else WerrorS("coeff field not implemented"); |
---|
150 | } |
---|
151 | |
---|
152 | void ssiWriteNumber(const ssiInfo *d, const number n) |
---|
153 | { |
---|
154 | ssiWriteNumber_CF(d,n,d->r->cf); |
---|
155 | } |
---|
156 | |
---|
157 | void ssiWriteRing_R(ssiInfo *d,const ring r) |
---|
158 | { |
---|
159 | /* 5 <ch> <N> <l1> <v1> ...<lN> <vN> <number of orderings> <ord1> <block0_1> <block1_1> .... <extRing> <Q-ideal> */ |
---|
160 | /* ch=-1: transext, coeff ring follows */ |
---|
161 | /* ch=-2: algext, coeff ring and minpoly follows */ |
---|
162 | /* ch=-3: cf name follows */ |
---|
163 | if (r!=NULL) |
---|
164 | { |
---|
165 | if (rField_is_Q(r) || rField_is_Zp(r)) |
---|
166 | fprintf(d->f_write,"%d %d ",n_GetChar(r->cf),r->N); |
---|
167 | else if (rFieldType(r)==n_transExt) |
---|
168 | fprintf(d->f_write,"-1 %d ",r->N); |
---|
169 | else if (rFieldType(r)==n_algExt) |
---|
170 | fprintf(d->f_write,"-2 %d ",r->N); |
---|
171 | else /*dummy*/ |
---|
172 | { |
---|
173 | fprintf(d->f_write,"-3 %d ",r->N); |
---|
174 | ssiWriteString(d,nCoeffName(r->cf)); |
---|
175 | } |
---|
176 | |
---|
177 | int i; |
---|
178 | for(i=0;i<r->N;i++) |
---|
179 | { |
---|
180 | fprintf(d->f_write,"%d %s ",(int)strlen(r->names[i]),r->names[i]); |
---|
181 | } |
---|
182 | /* number of orderings:*/ |
---|
183 | i=0; |
---|
184 | // remember dummy ring: everything 0: |
---|
185 | if (r->order!=NULL) while (r->order[i]!=0) i++; |
---|
186 | fprintf(d->f_write,"%d ",i); |
---|
187 | /* each ordering block: */ |
---|
188 | i=0; |
---|
189 | if (r->order!=NULL) while(r->order[i]!=0) |
---|
190 | { |
---|
191 | fprintf(d->f_write,"%d %d %d ",r->order[i],r->block0[i], r->block1[i]); |
---|
192 | switch(r->order[i]) |
---|
193 | { |
---|
194 | case ringorder_a: |
---|
195 | case ringorder_wp: |
---|
196 | case ringorder_Wp: |
---|
197 | case ringorder_ws: |
---|
198 | case ringorder_Ws: |
---|
199 | case ringorder_aa: |
---|
200 | { |
---|
201 | int ii; |
---|
202 | for(ii=r->block0[i];ii<=r->block1[i];ii++) |
---|
203 | fprintf(d->f_write,"%d ",r->wvhdl[i][ii-r->block0[i]]); |
---|
204 | } |
---|
205 | break; |
---|
206 | |
---|
207 | case ringorder_a64: |
---|
208 | case ringorder_M: |
---|
209 | case ringorder_L: |
---|
210 | case ringorder_IS: |
---|
211 | Werror("ring oder not implemented for ssi:%d",r->order[i]); |
---|
212 | break; |
---|
213 | |
---|
214 | default: break; |
---|
215 | } |
---|
216 | i++; |
---|
217 | } |
---|
218 | if ((rFieldType(r)==n_transExt) |
---|
219 | || (rFieldType(r)==n_algExt)) |
---|
220 | { |
---|
221 | ssiWriteRing_R(d,r->cf->extRing); /* includes alg.ext if rFieldType(r)==n_algExt */ |
---|
222 | } |
---|
223 | /* Q-ideal :*/ |
---|
224 | if (r->qideal!=NULL) |
---|
225 | { |
---|
226 | ssiWriteIdeal_R(d,IDEAL_CMD,r->qideal,r); |
---|
227 | } |
---|
228 | else |
---|
229 | { |
---|
230 | fputs("0 ",d->f_write/*ideal with 0 entries */); |
---|
231 | } |
---|
232 | } |
---|
233 | else /* dummy ring r==NULL*/ |
---|
234 | { |
---|
235 | fputs("0 0 0 0 "/*,r->ch,r->N, blocks, q-ideal*/,d->f_write); |
---|
236 | } |
---|
237 | if (rIsLPRing(r)) // cannot be combined with 23 2 |
---|
238 | { |
---|
239 | fprintf(d->f_write,"23 1 %d %d ",SI_LOG2(r->bitmask),r->isLPring); |
---|
240 | } |
---|
241 | else |
---|
242 | { |
---|
243 | unsigned long bm=0; |
---|
244 | int b=0; |
---|
245 | bm=rGetExpSize(bm,b,r->N); |
---|
246 | if (r->bitmask!=bm) |
---|
247 | { |
---|
248 | fprintf(d->f_write,"23 0 %d ",SI_LOG2(r->bitmask)); |
---|
249 | } |
---|
250 | if (rIsPluralRing(r)) |
---|
251 | { |
---|
252 | fputs("23 2 ",d->f_write); |
---|
253 | void ssiWriteIdeal(const ssiInfo *d, int typ,const ideal I); |
---|
254 | ssiWriteIdeal(d,MATRIX_CMD,(ideal)r->GetNC()->C); |
---|
255 | ssiWriteIdeal(d,MATRIX_CMD,(ideal)r->GetNC()->D); |
---|
256 | } |
---|
257 | } |
---|
258 | } |
---|
259 | |
---|
260 | void ssiWriteRing(ssiInfo *d,const ring r) |
---|
261 | { |
---|
262 | /* 5 <ch> <N> <l1> <v1> ...<lN> <vN> <number of orderings> <ord1> <block0_1> <block1_1> .... <extRing> <Q-ideal> */ |
---|
263 | /* ch=-1: transext, coeff ring follows */ |
---|
264 | /* ch=-2: algext, coeff ring and minpoly follows */ |
---|
265 | /* ch=-3: cf name follows */ |
---|
266 | if ((r==NULL)||(r->cf==NULL)) |
---|
267 | { |
---|
268 | WerrorS("undefined ring"); |
---|
269 | return; |
---|
270 | } |
---|
271 | if (r==currRing) // see recursive calls for transExt/algExt |
---|
272 | { |
---|
273 | if (d->r!=NULL) rKill(d->r); |
---|
274 | d->r=r; |
---|
275 | } |
---|
276 | if (r!=NULL) |
---|
277 | { |
---|
278 | /*d->*/r->ref++; |
---|
279 | } |
---|
280 | ssiWriteRing_R(d,r); |
---|
281 | } |
---|
282 | void ssiWritePoly_R(const ssiInfo *d, int /*typ*/, poly p, const ring r) |
---|
283 | { |
---|
284 | fprintf(d->f_write,"%d ",pLength(p));//number of terms |
---|
285 | |
---|
286 | while(p!=NULL) |
---|
287 | { |
---|
288 | ssiWriteNumber_CF(d,pGetCoeff(p),r->cf); |
---|
289 | //nWrite(fich,pGetCoeff(p)); |
---|
290 | fprintf(d->f_write,"%ld ",p_GetComp(p,r));//component |
---|
291 | |
---|
292 | for(int j=1;j<=rVar(r);j++) |
---|
293 | { |
---|
294 | fprintf(d->f_write,"%ld ",p_GetExp(p,j,r ));//x^j |
---|
295 | } |
---|
296 | pIter(p); |
---|
297 | } |
---|
298 | } |
---|
299 | |
---|
300 | void ssiWritePoly(const ssiInfo *d, int typ, poly p) |
---|
301 | { |
---|
302 | ssiWritePoly_R(d,typ,p,d->r); |
---|
303 | } |
---|
304 | |
---|
305 | void ssiWriteIdeal_R(const ssiInfo *d, int typ,const ideal I, const ring R) |
---|
306 | { |
---|
307 | // syntax: 7 # of elements <poly 1> <poly2>.....(ideal,module,smatrix) |
---|
308 | // syntax: 8 <rows> <cols> <poly 1> <poly2>.....(matrix) |
---|
309 | // syntax |
---|
310 | matrix M=(matrix)I; |
---|
311 | int mn; |
---|
312 | if (typ==MATRIX_CMD) |
---|
313 | { |
---|
314 | mn=MATROWS(M)*MATCOLS(M); |
---|
315 | fprintf(d->f_write,"%d %d ", MATROWS(M),MATCOLS(M)); |
---|
316 | } |
---|
317 | else |
---|
318 | { |
---|
319 | mn=IDELEMS(I); |
---|
320 | fprintf(d->f_write,"%d ",IDELEMS(I)); |
---|
321 | } |
---|
322 | |
---|
323 | int i; |
---|
324 | int tt; |
---|
325 | if ((typ==MODUL_CMD)||(typ==SMATRIX_CMD)) |
---|
326 | tt=VECTOR_CMD; |
---|
327 | else |
---|
328 | tt=POLY_CMD; |
---|
329 | |
---|
330 | for(i=0;i<mn;i++) |
---|
331 | { |
---|
332 | ssiWritePoly_R(d,tt,I->m[i],R); |
---|
333 | } |
---|
334 | } |
---|
335 | void ssiWriteIdeal(const ssiInfo *d, int typ,const ideal I) |
---|
336 | { |
---|
337 | ssiWriteIdeal_R(d,typ,I,d->r); |
---|
338 | } |
---|
339 | |
---|
340 | void ssiWriteCommand(si_link l, command D) |
---|
341 | { |
---|
342 | ssiInfo *d=(ssiInfo*)l->data; |
---|
343 | // syntax: <num ops> <operation> <op1> <op2> .... |
---|
344 | fprintf(d->f_write,"%d %d ",D->argc,D->op); |
---|
345 | if (D->argc >0) ssiWrite(l, &(D->arg1)); |
---|
346 | if (D->argc < 4) |
---|
347 | { |
---|
348 | if (D->argc >1) ssiWrite(l, &(D->arg2)); |
---|
349 | if (D->argc >2) ssiWrite(l, &(D->arg3)); |
---|
350 | } |
---|
351 | } |
---|
352 | |
---|
353 | void ssiWriteProc(const ssiInfo *d,procinfov p) |
---|
354 | { |
---|
355 | if (p->data.s.body==NULL) |
---|
356 | iiGetLibProcBuffer(p); |
---|
357 | if (p->data.s.body!=NULL) |
---|
358 | ssiWriteString(d,p->data.s.body); |
---|
359 | else |
---|
360 | ssiWriteString(d,""); |
---|
361 | } |
---|
362 | |
---|
363 | void ssiWriteList(si_link l,lists dd) |
---|
364 | { |
---|
365 | ssiInfo *d=(ssiInfo*)l->data; |
---|
366 | int Ll=dd->nr; |
---|
367 | fprintf(d->f_write,"%d ",Ll+1); |
---|
368 | int i; |
---|
369 | for(i=0;i<=Ll;i++) |
---|
370 | { |
---|
371 | ssiWrite(l,&(dd->m[i])); |
---|
372 | } |
---|
373 | } |
---|
374 | void ssiWriteIntvec(const ssiInfo *d,intvec * v) |
---|
375 | { |
---|
376 | fprintf(d->f_write,"%d ",v->length()); |
---|
377 | int i; |
---|
378 | for(i=0;i<v->length();i++) |
---|
379 | { |
---|
380 | fprintf(d->f_write,"%d ",(*v)[i]); |
---|
381 | } |
---|
382 | } |
---|
383 | void ssiWriteIntmat(const ssiInfo *d,intvec * v) |
---|
384 | { |
---|
385 | fprintf(d->f_write,"%d %d ",v->rows(),v->cols()); |
---|
386 | int i; |
---|
387 | for(i=0;i<v->length();i++) |
---|
388 | { |
---|
389 | fprintf(d->f_write,"%d ",(*v)[i]); |
---|
390 | } |
---|
391 | } |
---|
392 | |
---|
393 | void ssiWriteBigintmat(const ssiInfo *d,bigintmat * v) |
---|
394 | { |
---|
395 | fprintf(d->f_write,"%d %d ",v->rows(),v->cols()); |
---|
396 | int i; |
---|
397 | for(i=0;i<v->length();i++) |
---|
398 | { |
---|
399 | ssiWriteBigInt(d,(*v)[i]); |
---|
400 | } |
---|
401 | } |
---|
402 | |
---|
403 | char *ssiReadString(const ssiInfo *d) |
---|
404 | { |
---|
405 | char *buf; |
---|
406 | int l; |
---|
407 | l=s_readint(d->f_read); |
---|
408 | buf=(char*)omAlloc0(l+1); |
---|
409 | int throwaway =s_getc(d->f_read); /* skip ' '*/ |
---|
410 | throwaway=s_readbytes(buf,l,d->f_read); |
---|
411 | //if (throwaway!=l) printf("want %d, got %d bytes\n",l,throwaway); |
---|
412 | buf[l]='\0'; |
---|
413 | return buf; |
---|
414 | } |
---|
415 | |
---|
416 | int ssiReadInt(s_buff fich) |
---|
417 | { |
---|
418 | return s_readint(fich); |
---|
419 | } |
---|
420 | |
---|
421 | number ssiReadNumber_CF(const ssiInfo *d, const coeffs cf) |
---|
422 | { |
---|
423 | if (cf->cfReadFd!=NULL) |
---|
424 | { |
---|
425 | return n_ReadFd(d,cf); |
---|
426 | } |
---|
427 | else if (getCoeffType(cf) == n_transExt) |
---|
428 | { |
---|
429 | // poly poly |
---|
430 | fraction f=(fraction)n_Init(1,cf); |
---|
431 | p_Delete(&NUM(f),cf->extRing); |
---|
432 | NUM(f)=ssiReadPoly_R(d,cf->extRing); |
---|
433 | DEN(f)=ssiReadPoly_R(d,cf->extRing); |
---|
434 | return (number)f; |
---|
435 | } |
---|
436 | else if (getCoeffType(cf) == n_algExt) |
---|
437 | { |
---|
438 | // poly |
---|
439 | return (number)ssiReadPoly_R(d,cf->extRing); |
---|
440 | } |
---|
441 | else WerrorS("coeffs not implemented in ssiReadNumber"); |
---|
442 | return NULL; |
---|
443 | } |
---|
444 | |
---|
445 | number ssiReadBigInt(const ssiInfo *d) |
---|
446 | { |
---|
447 | number n=ssiReadNumber_CF(d,coeffs_BIGINT); |
---|
448 | if ((SR_HDL(n) & SR_INT)==0) |
---|
449 | { |
---|
450 | if (n->s!=3) Werror("invalid sub type in bigint:%d",n->s); |
---|
451 | } |
---|
452 | return n; |
---|
453 | } |
---|
454 | |
---|
455 | number ssiReadNumber(const ssiInfo *d) |
---|
456 | { |
---|
457 | if (currRing==NULL) ssiSetCurrRing(d->r); |
---|
458 | return ssiReadNumber_CF(d,d->r->cf); |
---|
459 | } |
---|
460 | |
---|
461 | ring ssiReadRing(const ssiInfo *d) |
---|
462 | { |
---|
463 | /* syntax is <ch> <N> <l1> <v1> ...<lN> <vN> <number of orderings> <ord1> <block0_1> <block1_1> .... <Q-ideal> */ |
---|
464 | int ch, N,i; |
---|
465 | char **names; |
---|
466 | ch=s_readint(d->f_read); |
---|
467 | N=s_readint(d->f_read); |
---|
468 | coeffs cf=NULL; |
---|
469 | if (ch==-3) |
---|
470 | { |
---|
471 | char *cf_name=ssiReadString(d); |
---|
472 | cf=nFindCoeffByName(cf_name); |
---|
473 | if (cf==NULL) |
---|
474 | { |
---|
475 | Werror("cannot find cf:%s",cf_name); |
---|
476 | omFree(cf_name); |
---|
477 | return NULL; |
---|
478 | } |
---|
479 | } |
---|
480 | if (N!=0) |
---|
481 | { |
---|
482 | names=(char**)omAlloc(N*sizeof(char*)); |
---|
483 | for(i=0;i<N;i++) |
---|
484 | { |
---|
485 | names[i]=ssiReadString(d); |
---|
486 | } |
---|
487 | } |
---|
488 | // read the orderings: |
---|
489 | int num_ord; // number of orderings |
---|
490 | num_ord=s_readint(d->f_read); |
---|
491 | rRingOrder_t *ord=(rRingOrder_t *)omAlloc0((num_ord+1)*sizeof(rRingOrder_t)); |
---|
492 | int *block0=(int *)omAlloc0((num_ord+1)*sizeof(int)); |
---|
493 | int *block1=(int *)omAlloc0((num_ord+1)*sizeof(int)); |
---|
494 | int **wvhdl=(int**)omAlloc0((num_ord+1)*sizeof(int*)); |
---|
495 | for(i=0;i<num_ord;i++) |
---|
496 | { |
---|
497 | ord[i]=(rRingOrder_t)s_readint(d->f_read); |
---|
498 | block0[i]=s_readint(d->f_read); |
---|
499 | block1[i]=s_readint(d->f_read); |
---|
500 | switch(ord[i]) |
---|
501 | { |
---|
502 | case ringorder_a: |
---|
503 | case ringorder_wp: |
---|
504 | case ringorder_Wp: |
---|
505 | case ringorder_ws: |
---|
506 | case ringorder_Ws: |
---|
507 | case ringorder_aa: |
---|
508 | { |
---|
509 | wvhdl[i]=(int*)omAlloc((block1[i]-block0[i]+1)*sizeof(int)); |
---|
510 | int ii; |
---|
511 | for(ii=block0[i];ii<=block1[i];ii++) |
---|
512 | wvhdl[i][ii-block0[i]]=s_readint(d->f_read); |
---|
513 | } |
---|
514 | break; |
---|
515 | |
---|
516 | case ringorder_a64: |
---|
517 | case ringorder_M: |
---|
518 | case ringorder_L: |
---|
519 | case ringorder_IS: |
---|
520 | Werror("ring oder not implemented for ssi:%d",ord[i]); |
---|
521 | break; |
---|
522 | |
---|
523 | default: break; |
---|
524 | } |
---|
525 | } |
---|
526 | if (N==0) |
---|
527 | { |
---|
528 | omFree(ord); |
---|
529 | omFree(block0); |
---|
530 | omFree(block1); |
---|
531 | omFree(wvhdl); |
---|
532 | return NULL; |
---|
533 | } |
---|
534 | else |
---|
535 | { |
---|
536 | ring r=NULL; |
---|
537 | if (ch>=0) /* Q, Z/p */ |
---|
538 | r=rDefault(ch,N,names,num_ord,ord,block0,block1,wvhdl); |
---|
539 | else if (ch==-1) /* trans ext. */ |
---|
540 | { |
---|
541 | TransExtInfo T; |
---|
542 | T.r=ssiReadRing(d); |
---|
543 | if (T.r==NULL) return NULL; |
---|
544 | cf=nInitChar(n_transExt,&T); |
---|
545 | r=rDefault(cf,N,names,num_ord,ord,block0,block1,wvhdl); |
---|
546 | } |
---|
547 | else if (ch==-2) /* alg ext. */ |
---|
548 | { |
---|
549 | TransExtInfo T; |
---|
550 | T.r=ssiReadRing(d); /* includes qideal */ |
---|
551 | if (T.r==NULL) return NULL; |
---|
552 | cf=nInitChar(n_algExt,&T); |
---|
553 | r=rDefault(cf,N,names,num_ord,ord,block0,block1,wvhdl); |
---|
554 | } |
---|
555 | else if (ch==-3) |
---|
556 | { |
---|
557 | r=rDefault(cf,N,names,num_ord,ord,block0,block1,wvhdl); |
---|
558 | } |
---|
559 | else |
---|
560 | { |
---|
561 | Werror("ssi: read unknown coeffs type (%d)",ch); |
---|
562 | for(i=0;i<N;i++) |
---|
563 | { |
---|
564 | omFree(names[i]); |
---|
565 | } |
---|
566 | omFreeSize(names,N*sizeof(char*)); |
---|
567 | return NULL; |
---|
568 | } |
---|
569 | ideal q=ssiReadIdeal_R(d,r); |
---|
570 | if (IDELEMS(q)==0) omFreeBin(q,sip_sideal_bin); |
---|
571 | else r->qideal=q; |
---|
572 | for(i=0;i<N;i++) |
---|
573 | { |
---|
574 | omFree(names[i]); |
---|
575 | } |
---|
576 | omFreeSize(names,N*sizeof(char*)); |
---|
577 | return r; |
---|
578 | } |
---|
579 | } |
---|
580 | |
---|
581 | poly ssiReadPoly_R(const ssiInfo *D, const ring r) |
---|
582 | { |
---|
583 | // < # of terms> < term1> < ..... |
---|
584 | int n,i,l; |
---|
585 | n=ssiReadInt(D->f_read); // # of terms |
---|
586 | //Print("poly: terms:%d\n",n); |
---|
587 | poly p; |
---|
588 | poly ret=NULL; |
---|
589 | poly prev=NULL; |
---|
590 | for(l=0;l<n;l++) // read n terms |
---|
591 | { |
---|
592 | // coef,comp.exp1,..exp N |
---|
593 | p=p_Init(r,r->PolyBin); |
---|
594 | pSetCoeff0(p,ssiReadNumber_CF(D,r->cf)); |
---|
595 | int d; |
---|
596 | d=s_readint(D->f_read); |
---|
597 | p_SetComp(p,d,r); |
---|
598 | for(i=1;i<=rVar(r);i++) |
---|
599 | { |
---|
600 | d=s_readint(D->f_read); |
---|
601 | p_SetExp(p,i,d,r); |
---|
602 | } |
---|
603 | p_Setm(p,r); |
---|
604 | p_Test(p,r); |
---|
605 | if (ret==NULL) ret=p; |
---|
606 | else pNext(prev)=p; |
---|
607 | prev=p; |
---|
608 | } |
---|
609 | return ret; |
---|
610 | } |
---|
611 | |
---|
612 | poly ssiReadPoly(const ssiInfo *D) |
---|
613 | { |
---|
614 | if (currRing==NULL) ssiSetCurrRing(D->r); |
---|
615 | return ssiReadPoly_R(D,D->r); |
---|
616 | } |
---|
617 | |
---|
618 | ideal ssiReadIdeal_R(const ssiInfo *d,const ring r) |
---|
619 | { |
---|
620 | // < # of terms> < term1> < ..... |
---|
621 | int n,i; |
---|
622 | ideal I; |
---|
623 | n=s_readint(d->f_read); |
---|
624 | I=idInit(n,1); // will be fixed later for module/smatrix |
---|
625 | for(i=0;i<IDELEMS(I);i++) // read n terms |
---|
626 | { |
---|
627 | I->m [i]=ssiReadPoly_R(d,r); |
---|
628 | } |
---|
629 | return I; |
---|
630 | } |
---|
631 | |
---|
632 | ideal ssiReadIdeal(const ssiInfo *d) |
---|
633 | { |
---|
634 | if (currRing==NULL) ssiSetCurrRing(d->r); |
---|
635 | return ssiReadIdeal_R(d,d->r); |
---|
636 | } |
---|
637 | |
---|
638 | matrix ssiReadMatrix(const ssiInfo *d) |
---|
639 | { |
---|
640 | int n,m; |
---|
641 | m=s_readint(d->f_read); |
---|
642 | n=s_readint(d->f_read); |
---|
643 | matrix M=mpNew(m,n); |
---|
644 | poly p; |
---|
645 | for(int i=1;i<=MATROWS(M);i++) |
---|
646 | for(int j=1;j<=MATCOLS(M);j++) |
---|
647 | { |
---|
648 | p=ssiReadPoly(d); |
---|
649 | MATELEM(M,i,j)=p; |
---|
650 | } |
---|
651 | return M; |
---|
652 | } |
---|
653 | |
---|
654 | command ssiReadCommand(si_link l) |
---|
655 | { |
---|
656 | ssiInfo *d=(ssiInfo*)l->data; |
---|
657 | // syntax: <num ops> <operation> <op1> <op2> .... |
---|
658 | command D=(command)omAlloc0(sizeof(*D)); |
---|
659 | int argc,op; |
---|
660 | argc=s_readint(d->f_read); |
---|
661 | op=s_readint(d->f_read); |
---|
662 | D->argc=argc; D->op=op; |
---|
663 | leftv v; |
---|
664 | if (argc >0) |
---|
665 | { |
---|
666 | v=ssiRead1(l); |
---|
667 | memcpy(&(D->arg1),v,sizeof(*v)); |
---|
668 | omFreeBin(v,sleftv_bin); |
---|
669 | } |
---|
670 | if (argc <4) |
---|
671 | { |
---|
672 | if (D->argc >1) |
---|
673 | { |
---|
674 | v=ssiRead1(l); |
---|
675 | memcpy(&(D->arg2),v,sizeof(*v)); |
---|
676 | omFreeBin(v,sleftv_bin); |
---|
677 | } |
---|
678 | if (D->argc >2) |
---|
679 | { |
---|
680 | v=ssiRead1(l); |
---|
681 | memcpy(&(D->arg3),v,sizeof(*v)); |
---|
682 | omFreeBin(v,sleftv_bin); |
---|
683 | } |
---|
684 | } |
---|
685 | else |
---|
686 | { |
---|
687 | leftv prev=&(D->arg1); |
---|
688 | argc--; |
---|
689 | while(argc >0) |
---|
690 | { |
---|
691 | v=ssiRead1(l); |
---|
692 | prev->next=v; |
---|
693 | prev=v; |
---|
694 | argc--; |
---|
695 | } |
---|
696 | } |
---|
697 | return D; |
---|
698 | } |
---|
699 | |
---|
700 | procinfov ssiReadProc(const ssiInfo *d) |
---|
701 | { |
---|
702 | char *s=ssiReadString(d); |
---|
703 | procinfov p=(procinfov)omAlloc0Bin(procinfo_bin); |
---|
704 | p->language=LANG_SINGULAR; |
---|
705 | p->libname=omStrDup(""); |
---|
706 | p->procname=omStrDup(""); |
---|
707 | p->data.s.body=s; |
---|
708 | return p; |
---|
709 | } |
---|
710 | lists ssiReadList(si_link l) |
---|
711 | { |
---|
712 | ssiInfo *d=(ssiInfo*)l->data; |
---|
713 | int nr; |
---|
714 | nr=s_readint(d->f_read); |
---|
715 | lists L=(lists)omAlloc0Bin(slists_bin); |
---|
716 | L->Init(nr); |
---|
717 | |
---|
718 | int i; |
---|
719 | leftv v; |
---|
720 | for(i=0;i<=L->nr;i++) |
---|
721 | { |
---|
722 | v=ssiRead1(l); |
---|
723 | memcpy(&(L->m[i]),v,sizeof(*v)); |
---|
724 | omFreeBin(v,sleftv_bin); |
---|
725 | } |
---|
726 | return L; |
---|
727 | } |
---|
728 | intvec* ssiReadIntvec(const ssiInfo *d) |
---|
729 | { |
---|
730 | int nr; |
---|
731 | nr=s_readint(d->f_read); |
---|
732 | intvec *v=new intvec(nr); |
---|
733 | for(int i=0;i<nr;i++) |
---|
734 | { |
---|
735 | (*v)[i]=s_readint(d->f_read); |
---|
736 | } |
---|
737 | return v; |
---|
738 | } |
---|
739 | intvec* ssiReadIntmat(const ssiInfo *d) |
---|
740 | { |
---|
741 | int r,c; |
---|
742 | r=s_readint(d->f_read); |
---|
743 | c=s_readint(d->f_read); |
---|
744 | intvec *v=new intvec(r,c,0); |
---|
745 | for(int i=0;i<r*c;i++) |
---|
746 | { |
---|
747 | (*v)[i]=s_readint(d->f_read); |
---|
748 | } |
---|
749 | return v; |
---|
750 | } |
---|
751 | bigintmat* ssiReadBigintmat(const ssiInfo *d) |
---|
752 | { |
---|
753 | int r,c; |
---|
754 | r=s_readint(d->f_read); |
---|
755 | c=s_readint(d->f_read); |
---|
756 | bigintmat *v=new bigintmat(r,c,coeffs_BIGINT); |
---|
757 | for(int i=0;i<r*c;i++) |
---|
758 | { |
---|
759 | (*v)[i]=ssiReadBigInt(d); |
---|
760 | } |
---|
761 | return v; |
---|
762 | } |
---|
763 | |
---|
764 | void ssiReadBlackbox(leftv res, si_link l) |
---|
765 | { |
---|
766 | ssiInfo *d=(ssiInfo*)l->data; |
---|
767 | int throwaway=s_readint(d->f_read); |
---|
768 | char *name=ssiReadString(d); |
---|
769 | int tok; |
---|
770 | blackboxIsCmd(name,tok); |
---|
771 | if (tok>MAX_TOK) |
---|
772 | { |
---|
773 | blackbox *b=getBlackboxStuff(tok); |
---|
774 | res->rtyp=tok; |
---|
775 | b->blackbox_deserialize(&b,&(res->data),l); |
---|
776 | } |
---|
777 | else |
---|
778 | { |
---|
779 | Werror("blackbox %s not found",name); |
---|
780 | } |
---|
781 | omFree(name); |
---|
782 | } |
---|
783 | |
---|
784 | void ssiReadAttrib(leftv res, si_link l) |
---|
785 | { |
---|
786 | ssiInfo *d=(ssiInfo*)l->data; |
---|
787 | BITSET fl=(BITSET)s_readint(d->f_read); |
---|
788 | int nr_of_attr=s_readint(d->f_read); |
---|
789 | if (nr_of_attr>0) |
---|
790 | { |
---|
791 | for(int i=1;i<nr_of_attr;i++) |
---|
792 | { |
---|
793 | } |
---|
794 | } |
---|
795 | leftv tmp=ssiRead1(l); |
---|
796 | memcpy(res,tmp,sizeof(sleftv)); |
---|
797 | memset(tmp,0,sizeof(sleftv)); |
---|
798 | omFreeBin(tmp,sleftv_bin); |
---|
799 | if (nr_of_attr>0) |
---|
800 | { |
---|
801 | } |
---|
802 | res->flag=fl; |
---|
803 | } |
---|
804 | void ssiReadRingProperties(si_link l) |
---|
805 | { |
---|
806 | ssiInfo *d=(ssiInfo*)l->data; |
---|
807 | int what=s_readint(d->f_read); |
---|
808 | switch(what) |
---|
809 | { |
---|
810 | case 0: // bitmask |
---|
811 | { |
---|
812 | int lb=s_readint(d->f_read); |
---|
813 | unsigned long bm=~0L; |
---|
814 | bm=bm<<lb; |
---|
815 | bm=~bm; |
---|
816 | rUnComplete(d->r); |
---|
817 | d->r->bitmask=bm; |
---|
818 | rComplete(d->r); |
---|
819 | break; |
---|
820 | } |
---|
821 | case 1: // LPRing |
---|
822 | { |
---|
823 | int lb=s_readint(d->f_read); |
---|
824 | int isLPring=s_readint(d->f_read); |
---|
825 | unsigned long bm=~0L; |
---|
826 | bm=bm<<lb; |
---|
827 | bm=~bm; |
---|
828 | rUnComplete(d->r); |
---|
829 | d->r->bitmask=bm; |
---|
830 | d->r->isLPring=isLPring; |
---|
831 | rComplete(d->r); |
---|
832 | break; |
---|
833 | } |
---|
834 | case 2: // Plural rings |
---|
835 | { |
---|
836 | matrix C=ssiReadMatrix(d); |
---|
837 | matrix D=ssiReadMatrix(d); |
---|
838 | nc_CallPlural(C,D,NULL,NULL,d->r,true,true,false,d->r,false); |
---|
839 | break; |
---|
840 | } |
---|
841 | } |
---|
842 | } |
---|
843 | //**************************************************************************/ |
---|
844 | |
---|
845 | BOOLEAN ssiOpen(si_link l, short flag, leftv u) |
---|
846 | { |
---|
847 | if (l!=NULL) |
---|
848 | { |
---|
849 | const char *mode; |
---|
850 | ssiInfo *d=(ssiInfo*)omAlloc0(sizeof(ssiInfo)); |
---|
851 | if (flag & SI_LINK_OPEN) |
---|
852 | { |
---|
853 | if (l->mode[0] != '\0' && (strcmp(l->mode, "r") == 0)) |
---|
854 | flag = SI_LINK_READ; |
---|
855 | else flag = SI_LINK_WRITE; |
---|
856 | } |
---|
857 | |
---|
858 | if (flag == SI_LINK_READ) mode = "r"; |
---|
859 | else if (strcmp(l->mode, "w") == 0) mode = "w"; |
---|
860 | else if (strcmp(l->mode, "fork") == 0) mode = "fork"; |
---|
861 | else if (strcmp(l->mode, "tcp") == 0) mode = "tcp"; |
---|
862 | else if (strcmp(l->mode, "connect") == 0) mode = "connect"; |
---|
863 | else mode = "a"; |
---|
864 | |
---|
865 | |
---|
866 | SI_LINK_SET_OPEN_P(l, flag); |
---|
867 | if(l->data!=NULL) omFreeSize(l->data,sizeof(ssiInfo)); |
---|
868 | l->data=d; |
---|
869 | omFree(l->mode); |
---|
870 | l->mode = omStrDup(mode); |
---|
871 | |
---|
872 | if (l->name[0] == '\0') |
---|
873 | { |
---|
874 | if (strcmp(mode,"fork")==0) |
---|
875 | { |
---|
876 | link_list n=(link_list)omAlloc(sizeof(link_struct)); |
---|
877 | n->u=u; |
---|
878 | n->l=l; |
---|
879 | n->next=(void *)ssiToBeClosed; |
---|
880 | ssiToBeClosed=n; |
---|
881 | |
---|
882 | int pc[2]; |
---|
883 | int cp[2]; |
---|
884 | pipe(pc); |
---|
885 | pipe(cp); |
---|
886 | pid_t pid = fork(); |
---|
887 | if (pid == -1 && errno == EAGAIN) // RLIMIT_NPROC too low? |
---|
888 | { |
---|
889 | raise_rlimit_nproc(); |
---|
890 | pid = fork(); |
---|
891 | } |
---|
892 | if (pid == -1) |
---|
893 | { |
---|
894 | WerrorS("could not fork"); |
---|
895 | } |
---|
896 | if (pid==0) /*fork: child*/ |
---|
897 | { |
---|
898 | /* block SIGINT */ |
---|
899 | sigset_t sigint; |
---|
900 | sigemptyset(&sigint); |
---|
901 | sigaddset(&sigint, SIGINT); |
---|
902 | sigprocmask(SIG_BLOCK, &sigint, NULL); |
---|
903 | |
---|
904 | link_list hh=(link_list)ssiToBeClosed->next; |
---|
905 | /* we know: l is the first entry in ssiToBeClosed-list */ |
---|
906 | while(hh!=NULL) |
---|
907 | { |
---|
908 | SI_LINK_SET_CLOSE_P(hh->l); |
---|
909 | ssiInfo *dd=(ssiInfo*)hh->l->data; |
---|
910 | s_close(dd->f_read); |
---|
911 | fclose(dd->f_write); |
---|
912 | if (dd->r!=NULL) rKill(dd->r); |
---|
913 | omFreeSize((ADDRESS)dd,(sizeof *dd)); |
---|
914 | hh->l->data=NULL; |
---|
915 | link_list nn=(link_list)hh->next; |
---|
916 | omFree(hh); |
---|
917 | hh=nn; |
---|
918 | } |
---|
919 | ssiToBeClosed->next=NULL; |
---|
920 | #ifdef HAVE_SIMPLEIPC |
---|
921 | memset(sem_acquired, 0, SIPC_MAX_SEMAPHORES*sizeof(sem_acquired[0])); |
---|
922 | #endif // HAVE_SIMPLEIPC |
---|
923 | si_close(pc[1]); si_close(cp[0]); |
---|
924 | d->f_write=fdopen(cp[1],"w"); |
---|
925 | d->f_read=s_open(pc[0]); |
---|
926 | d->fd_read=pc[0]; |
---|
927 | d->fd_write=cp[1]; |
---|
928 | //d->r=currRing; |
---|
929 | //if (d->r!=NULL) d->r->ref++; |
---|
930 | l->data=d; |
---|
931 | omFree(l->mode); |
---|
932 | l->mode = omStrDup(mode); |
---|
933 | singular_in_batchmode=TRUE; |
---|
934 | SI_LINK_SET_RW_OPEN_P(l); |
---|
935 | //myynest=0; |
---|
936 | fe_fgets_stdin=fe_fgets_dummy; |
---|
937 | if ((u!=NULL)&&(u->rtyp==IDHDL)) |
---|
938 | { |
---|
939 | idhdl h=(idhdl)u->data; |
---|
940 | h->lev=0; |
---|
941 | } |
---|
942 | loop |
---|
943 | { |
---|
944 | if (!SI_LINK_OPEN_P(l)) m2_end(0); |
---|
945 | if(d->f_read->is_eof) m2_end(0); |
---|
946 | leftv h=ssiRead1(l); /*contains an exit.... */ |
---|
947 | if (feErrors != NULL && *feErrors != '\0') |
---|
948 | { |
---|
949 | // handle errors: |
---|
950 | PrintS(feErrors); /* currently quite simple */ |
---|
951 | *feErrors = '\0'; |
---|
952 | } |
---|
953 | ssiWrite(l,h); |
---|
954 | h->CleanUp(); |
---|
955 | omFreeBin(h, sleftv_bin); |
---|
956 | } |
---|
957 | /* never reached*/ |
---|
958 | } |
---|
959 | else if (pid>0) /*fork: parent*/ |
---|
960 | { |
---|
961 | d->pid=pid; |
---|
962 | si_close(pc[0]); si_close(cp[1]); |
---|
963 | d->f_write=fdopen(pc[1],"w"); |
---|
964 | d->f_read=s_open(cp[0]); |
---|
965 | d->fd_read=cp[0]; |
---|
966 | d->fd_write=pc[1]; |
---|
967 | SI_LINK_SET_RW_OPEN_P(l); |
---|
968 | d->send_quit_at_exit=1; |
---|
969 | //d->r=currRing; |
---|
970 | //if (d->r!=NULL) d->r->ref++; |
---|
971 | } |
---|
972 | else |
---|
973 | { |
---|
974 | Werror("fork failed (%d)",errno); |
---|
975 | l->data=NULL; |
---|
976 | omFree(d); |
---|
977 | return TRUE; |
---|
978 | } |
---|
979 | } |
---|
980 | // --------------------------------------------------------------------- |
---|
981 | else if (strcmp(mode,"tcp")==0) |
---|
982 | { |
---|
983 | int sockfd, newsockfd, portno, clilen; |
---|
984 | struct sockaddr_in serv_addr, cli_addr; |
---|
985 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
---|
986 | if(sockfd < 0) |
---|
987 | { |
---|
988 | WerrorS("ERROR opening socket"); |
---|
989 | l->data=NULL; |
---|
990 | omFree(d); |
---|
991 | return TRUE; |
---|
992 | } |
---|
993 | memset((char *) &serv_addr,0, sizeof(serv_addr)); |
---|
994 | portno = 1025; |
---|
995 | serv_addr.sin_family = AF_INET; |
---|
996 | serv_addr.sin_addr.s_addr = INADDR_ANY; |
---|
997 | do |
---|
998 | { |
---|
999 | portno++; |
---|
1000 | serv_addr.sin_port = htons(portno); |
---|
1001 | if(portno > 50000) |
---|
1002 | { |
---|
1003 | WerrorS("ERROR on binding (no free port available?)"); |
---|
1004 | l->data=NULL; |
---|
1005 | omFree(d); |
---|
1006 | return TRUE; |
---|
1007 | } |
---|
1008 | } |
---|
1009 | while(bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0); |
---|
1010 | Print("waiting on port %d\n", portno);mflush(); |
---|
1011 | listen(sockfd,1); |
---|
1012 | newsockfd = si_accept(sockfd, (struct sockaddr *) &cli_addr, (socklen_t *)&clilen); |
---|
1013 | if(newsockfd < 0) |
---|
1014 | { |
---|
1015 | WerrorS("ERROR on accept"); |
---|
1016 | l->data=NULL; |
---|
1017 | omFree(d); |
---|
1018 | return TRUE; |
---|
1019 | } |
---|
1020 | PrintS("client accepted\n"); |
---|
1021 | d->fd_read = newsockfd; |
---|
1022 | d->fd_write = newsockfd; |
---|
1023 | d->f_read = s_open(newsockfd); |
---|
1024 | d->f_write = fdopen(newsockfd, "w"); |
---|
1025 | SI_LINK_SET_RW_OPEN_P(l); |
---|
1026 | si_close(sockfd); |
---|
1027 | } |
---|
1028 | // no ssi-Link on stdin or stdout |
---|
1029 | else |
---|
1030 | { |
---|
1031 | Werror("invalid mode >>%s<< for ssi",mode); |
---|
1032 | l->data=NULL; |
---|
1033 | omFree(d); |
---|
1034 | return TRUE; |
---|
1035 | } |
---|
1036 | } |
---|
1037 | // ========================================================================= |
---|
1038 | else /*l->name=NULL*/ |
---|
1039 | { |
---|
1040 | // tcp mode |
---|
1041 | if(strcmp(mode,"tcp")==0) |
---|
1042 | { |
---|
1043 | int sockfd, newsockfd, portno, clilen; |
---|
1044 | struct sockaddr_in serv_addr, cli_addr; |
---|
1045 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
---|
1046 | if(sockfd < 0) |
---|
1047 | { |
---|
1048 | WerrorS("ERROR opening socket"); |
---|
1049 | l->data=NULL; |
---|
1050 | omFree(d); |
---|
1051 | return TRUE; |
---|
1052 | } |
---|
1053 | memset((char *) &serv_addr,0, sizeof(serv_addr)); |
---|
1054 | portno = 1025; |
---|
1055 | serv_addr.sin_family = AF_INET; |
---|
1056 | serv_addr.sin_addr.s_addr = INADDR_ANY; |
---|
1057 | do |
---|
1058 | { |
---|
1059 | portno++; |
---|
1060 | serv_addr.sin_port = htons(portno); |
---|
1061 | if(portno > 50000) |
---|
1062 | { |
---|
1063 | WerrorS("ERROR on binding (no free port available?)"); |
---|
1064 | l->data=NULL; |
---|
1065 | return TRUE; |
---|
1066 | } |
---|
1067 | } |
---|
1068 | while(bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0); |
---|
1069 | //Print("waiting on port %d\n", portno);mflush(); |
---|
1070 | listen(sockfd,1); |
---|
1071 | char* cli_host = (char*)omAlloc(256); |
---|
1072 | char* path = (char*)omAlloc(1024); |
---|
1073 | int r = si_sscanf(l->name,"%255[^:]:%s",cli_host,path); |
---|
1074 | if(r == 0) |
---|
1075 | { |
---|
1076 | WerrorS("ERROR: no host specified"); |
---|
1077 | l->data=NULL; |
---|
1078 | omFree(d); |
---|
1079 | omFree(path); |
---|
1080 | omFree(cli_host); |
---|
1081 | return TRUE; |
---|
1082 | } |
---|
1083 | else if(r == 1) |
---|
1084 | { |
---|
1085 | WarnS("program not specified, using /usr/local/bin/Singular"); |
---|
1086 | Warn("in line >>%s<<",my_yylinebuf); |
---|
1087 | strcpy(path,"/usr/local/bin/Singular"); |
---|
1088 | } |
---|
1089 | char* ssh_command = (char*)omAlloc(256); |
---|
1090 | char* ser_host = (char*)omAlloc(64); |
---|
1091 | gethostname(ser_host,64); |
---|
1092 | sprintf(ssh_command,"ssh %s %s -q --batch --link=ssi --MPhost=%s --MPport=%d &",cli_host,path,ser_host,portno); |
---|
1093 | //Print("client on %s started:%s\n",cli_host,path); |
---|
1094 | omFree(path); |
---|
1095 | omFree(cli_host); |
---|
1096 | if (TEST_OPT_PROT) { Print("running >>%s<<\n",ssh_command); } |
---|
1097 | system(ssh_command); |
---|
1098 | omFree(ssh_command); |
---|
1099 | omFree(ser_host); |
---|
1100 | clilen = sizeof(cli_addr); |
---|
1101 | newsockfd = si_accept(sockfd, (struct sockaddr *) &cli_addr, (socklen_t *)&clilen); |
---|
1102 | if(newsockfd < 0) |
---|
1103 | { |
---|
1104 | WerrorS("ERROR on accept"); |
---|
1105 | l->data=NULL; |
---|
1106 | omFree(d); |
---|
1107 | return TRUE; |
---|
1108 | } |
---|
1109 | //PrintS("client accepted\n"); |
---|
1110 | d->fd_read = newsockfd; |
---|
1111 | d->fd_write = newsockfd; |
---|
1112 | d->f_read = s_open(newsockfd); |
---|
1113 | d->f_write = fdopen(newsockfd, "w"); |
---|
1114 | si_close(sockfd); |
---|
1115 | SI_LINK_SET_RW_OPEN_P(l); |
---|
1116 | d->send_quit_at_exit=1; |
---|
1117 | link_list newlink=(link_list)omAlloc(sizeof(link_struct)); |
---|
1118 | newlink->u=u; |
---|
1119 | newlink->l=l; |
---|
1120 | newlink->next=(void *)ssiToBeClosed; |
---|
1121 | ssiToBeClosed=newlink; |
---|
1122 | fprintf(d->f_write,"98 %d %d %u %u\n",SSI_VERSION,MAX_TOK,si_opt_1,si_opt_2); |
---|
1123 | } |
---|
1124 | // ---------------------------------------------------------------------- |
---|
1125 | else if(strcmp(mode,"connect")==0) |
---|
1126 | { |
---|
1127 | char* host = (char*)omAlloc(256); |
---|
1128 | int sockfd, portno; |
---|
1129 | struct sockaddr_in serv_addr; |
---|
1130 | struct hostent *server; |
---|
1131 | |
---|
1132 | si_sscanf(l->name,"%255[^:]:%d",host,&portno); |
---|
1133 | //Print("connect to host %s, port %d\n",host,portno);mflush(); |
---|
1134 | if (portno!=0) |
---|
1135 | { |
---|
1136 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
---|
1137 | if (sockfd < 0) { WerrorS("ERROR opening socket"); return TRUE; } |
---|
1138 | server = gethostbyname(host); |
---|
1139 | if (server == NULL) { WerrorS("ERROR, no such host"); return TRUE; } |
---|
1140 | memset((char *) &serv_addr, 0, sizeof(serv_addr)); |
---|
1141 | serv_addr.sin_family = AF_INET; |
---|
1142 | memcpy((char *)&serv_addr.sin_addr.s_addr, |
---|
1143 | (char *)server->h_addr, |
---|
1144 | server->h_length); |
---|
1145 | serv_addr.sin_port = htons(portno); |
---|
1146 | if (si_connect(sockfd,(sockaddr*)&serv_addr,sizeof(serv_addr)) < 0) |
---|
1147 | { Werror("ERROR connecting(errno=%d)",errno); return TRUE; } |
---|
1148 | //PrintS("connected\n");mflush(); |
---|
1149 | d->f_read=s_open(sockfd); |
---|
1150 | d->fd_read=sockfd; |
---|
1151 | d->f_write=fdopen(sockfd,"w"); |
---|
1152 | d->fd_write=sockfd; |
---|
1153 | SI_LINK_SET_RW_OPEN_P(l); |
---|
1154 | omFree(host); |
---|
1155 | } |
---|
1156 | else |
---|
1157 | { |
---|
1158 | l->data=NULL; |
---|
1159 | omFree(d); |
---|
1160 | return TRUE; |
---|
1161 | } |
---|
1162 | } |
---|
1163 | // ====================================================================== |
---|
1164 | else |
---|
1165 | { |
---|
1166 | // normal link to a file |
---|
1167 | FILE *outfile; |
---|
1168 | char *filename=l->name; |
---|
1169 | |
---|
1170 | if(filename[0]=='>') |
---|
1171 | { |
---|
1172 | if (filename[1]=='>') |
---|
1173 | { |
---|
1174 | filename+=2; |
---|
1175 | mode = "a"; |
---|
1176 | } |
---|
1177 | else |
---|
1178 | { |
---|
1179 | filename++; |
---|
1180 | mode="w"; |
---|
1181 | } |
---|
1182 | } |
---|
1183 | outfile=myfopen(filename,mode); |
---|
1184 | if (outfile!=NULL) |
---|
1185 | { |
---|
1186 | if (strcmp(l->mode,"r")==0) |
---|
1187 | { |
---|
1188 | fclose(outfile); |
---|
1189 | d->f_read=s_open_by_name(filename); |
---|
1190 | } |
---|
1191 | else |
---|
1192 | { |
---|
1193 | d->f_write = outfile; |
---|
1194 | fprintf(d->f_write,"98 %d %d %u %u\n",SSI_VERSION,MAX_TOK,si_opt_1,si_opt_2); |
---|
1195 | } |
---|
1196 | } |
---|
1197 | else |
---|
1198 | { |
---|
1199 | omFree(d); |
---|
1200 | l->data=NULL; |
---|
1201 | return TRUE; |
---|
1202 | } |
---|
1203 | } |
---|
1204 | } |
---|
1205 | } |
---|
1206 | |
---|
1207 | return FALSE; |
---|
1208 | } |
---|
1209 | |
---|
1210 | //**************************************************************************/ |
---|
1211 | BOOLEAN ssiPrepClose(si_link l) |
---|
1212 | { |
---|
1213 | if (l!=NULL) |
---|
1214 | { |
---|
1215 | SI_LINK_SET_CLOSE_P(l); |
---|
1216 | ssiInfo *d = (ssiInfo *)l->data; |
---|
1217 | if (d!=NULL) |
---|
1218 | { |
---|
1219 | if (d->send_quit_at_exit) |
---|
1220 | { |
---|
1221 | fputs("99\n",d->f_write); |
---|
1222 | fflush(d->f_write); |
---|
1223 | } |
---|
1224 | d->quit_sent=1; |
---|
1225 | } |
---|
1226 | } |
---|
1227 | return FALSE; |
---|
1228 | } |
---|
1229 | |
---|
1230 | BOOLEAN ssiClose(si_link l) |
---|
1231 | { |
---|
1232 | if (l!=NULL) |
---|
1233 | { |
---|
1234 | SI_LINK_SET_CLOSE_P(l); |
---|
1235 | ssiInfo *d = (ssiInfo *)l->data; |
---|
1236 | if (d!=NULL) |
---|
1237 | { |
---|
1238 | // send quit signal |
---|
1239 | if ((d->send_quit_at_exit) |
---|
1240 | && (d->quit_sent==0)) |
---|
1241 | { |
---|
1242 | fputs("99\n",d->f_write); |
---|
1243 | fflush(d->f_write); |
---|
1244 | } |
---|
1245 | // clean ring |
---|
1246 | if (d->r!=NULL) rKill(d->r); |
---|
1247 | // did the child to stop ? |
---|
1248 | si_waitpid(d->pid,NULL,WNOHANG); |
---|
1249 | if ((d->pid!=0) |
---|
1250 | && (kill(d->pid,0)==0)) // child is still running |
---|
1251 | { |
---|
1252 | struct timespec t; |
---|
1253 | t.tv_sec=0; |
---|
1254 | t.tv_nsec=100000000; // <=100 ms |
---|
1255 | struct timespec rem; |
---|
1256 | int r; |
---|
1257 | loop |
---|
1258 | { |
---|
1259 | // wait till signal or time rem: |
---|
1260 | r = nanosleep(&t, &rem); |
---|
1261 | t = rem; |
---|
1262 | // child finished: |
---|
1263 | if (si_waitpid(d->pid,NULL,WNOHANG) != 0) break; |
---|
1264 | // other signal, waited s>= 100 ms: |
---|
1265 | if ((r==0) || (errno != EINTR)) break; |
---|
1266 | } |
---|
1267 | if (kill(d->pid,0) == 0) // pid still exists |
---|
1268 | { |
---|
1269 | kill(d->pid,15); |
---|
1270 | t.tv_sec=5; // <=5s |
---|
1271 | t.tv_nsec=0; |
---|
1272 | loop |
---|
1273 | { |
---|
1274 | // wait till signal or time rem: |
---|
1275 | r = nanosleep(&t, &rem); |
---|
1276 | t = rem; |
---|
1277 | // child finished: |
---|
1278 | if (si_waitpid(d->pid,NULL,WNOHANG) != 0) break; |
---|
1279 | // other signal, waited s>= 5 s: |
---|
1280 | if ((r==0) || (errno != EINTR)) break; |
---|
1281 | } |
---|
1282 | if (kill(d->pid,0) == 0) |
---|
1283 | { |
---|
1284 | kill(d->pid,9); // just to be sure |
---|
1285 | si_waitpid(d->pid,NULL,0); |
---|
1286 | } |
---|
1287 | } |
---|
1288 | } |
---|
1289 | if (d->f_read!=NULL) { s_close(d->f_read);d->f_read=NULL;} |
---|
1290 | if (d->f_write!=NULL) { fclose(d->f_write); d->f_write=NULL; } |
---|
1291 | if ((strcmp(l->mode,"tcp")==0) |
---|
1292 | || (strcmp(l->mode,"fork")==0)) |
---|
1293 | { |
---|
1294 | link_list hh=ssiToBeClosed; |
---|
1295 | if (hh!=NULL) |
---|
1296 | { |
---|
1297 | if (hh->l==l) |
---|
1298 | { |
---|
1299 | ssiToBeClosed=(link_list)hh->next; |
---|
1300 | omFreeSize(hh,sizeof(link_struct)); |
---|
1301 | } |
---|
1302 | else while(hh->next!=NULL) |
---|
1303 | { |
---|
1304 | link_list hhh=(link_list)hh->next; |
---|
1305 | if (hhh->l==l) |
---|
1306 | { |
---|
1307 | hh->next=hhh->next; |
---|
1308 | omFreeSize(hhh,sizeof(link_struct)); |
---|
1309 | break; |
---|
1310 | } |
---|
1311 | else |
---|
1312 | hh=(link_list)hh->next; |
---|
1313 | } |
---|
1314 | } |
---|
1315 | } |
---|
1316 | omFreeSize((ADDRESS)d,(sizeof *d)); |
---|
1317 | } |
---|
1318 | l->data=NULL; |
---|
1319 | } |
---|
1320 | return FALSE; |
---|
1321 | } |
---|
1322 | |
---|
1323 | //**************************************************************************/ |
---|
1324 | leftv ssiRead1(si_link l) |
---|
1325 | { |
---|
1326 | ssiInfo *d = (ssiInfo *)l->data; |
---|
1327 | leftv res=(leftv)omAlloc0Bin(sleftv_bin); |
---|
1328 | int t=0; |
---|
1329 | t=s_readint(d->f_read); |
---|
1330 | //Print("got type %d\n",t); |
---|
1331 | switch(t) |
---|
1332 | { |
---|
1333 | case 1:res->rtyp=INT_CMD; |
---|
1334 | res->data=(char *)(long)ssiReadInt(d->f_read); |
---|
1335 | break; |
---|
1336 | case 2:res->rtyp=STRING_CMD; |
---|
1337 | res->data=(char *)ssiReadString(d); |
---|
1338 | break; |
---|
1339 | case 3:res->rtyp=NUMBER_CMD; |
---|
1340 | res->data=(char *)ssiReadNumber(d); |
---|
1341 | break; |
---|
1342 | case 4:res->rtyp=BIGINT_CMD; |
---|
1343 | res->data=(char *)ssiReadBigInt(d); |
---|
1344 | break; |
---|
1345 | case 15: |
---|
1346 | case 5:{ |
---|
1347 | d->r=ssiReadRing(d); |
---|
1348 | if (d->r==NULL) return NULL; |
---|
1349 | res->data=(char*)d->r; |
---|
1350 | res->rtyp=RING_CMD; |
---|
1351 | if (t==15) // setring |
---|
1352 | { |
---|
1353 | if(ssiSetCurrRing(d->r)) { d->r=currRing; d->r->ref++; } |
---|
1354 | omFreeBin(res,sleftv_bin); |
---|
1355 | return ssiRead1(l); |
---|
1356 | } |
---|
1357 | } |
---|
1358 | break; |
---|
1359 | case 6:res->rtyp=POLY_CMD; |
---|
1360 | if (d->r==NULL) goto no_ring; |
---|
1361 | res->data=(char*)ssiReadPoly(d); |
---|
1362 | break; |
---|
1363 | case 7:res->rtyp=IDEAL_CMD; |
---|
1364 | if (d->r==NULL) goto no_ring; |
---|
1365 | res->data=(char*)ssiReadIdeal(d); |
---|
1366 | break; |
---|
1367 | case 8:res->rtyp=MATRIX_CMD; |
---|
1368 | if (d->r==NULL) goto no_ring; |
---|
1369 | res->data=(char*)ssiReadMatrix(d); |
---|
1370 | break; |
---|
1371 | case 9:res->rtyp=VECTOR_CMD; |
---|
1372 | if (d->r==NULL) goto no_ring; |
---|
1373 | res->data=(char*)ssiReadPoly(d); |
---|
1374 | break; |
---|
1375 | case 10: |
---|
1376 | case 22:if (t==22) res->rtyp=SMATRIX_CMD; |
---|
1377 | else res->rtyp=MODUL_CMD; |
---|
1378 | if (d->r==NULL) goto no_ring; |
---|
1379 | { |
---|
1380 | int rk=s_readint(d->f_read); |
---|
1381 | ideal M=ssiReadIdeal(d); |
---|
1382 | M->rank=rk; |
---|
1383 | res->data=(char*)M; |
---|
1384 | } |
---|
1385 | break; |
---|
1386 | case 11: |
---|
1387 | { |
---|
1388 | res->rtyp=COMMAND; |
---|
1389 | res->data=ssiReadCommand(l); |
---|
1390 | int nok=res->Eval(); |
---|
1391 | if (nok) WerrorS("error in eval"); |
---|
1392 | break; |
---|
1393 | } |
---|
1394 | case 12: /*DEF_CMD*/ |
---|
1395 | { |
---|
1396 | res->rtyp=0; |
---|
1397 | res->name=(char *)ssiReadString(d); |
---|
1398 | int nok=res->Eval(); |
---|
1399 | if (nok) WerrorS("error in name lookup"); |
---|
1400 | break; |
---|
1401 | } |
---|
1402 | case 13: res->rtyp=PROC_CMD; |
---|
1403 | res->data=ssiReadProc(d); |
---|
1404 | break; |
---|
1405 | case 14: res->rtyp=LIST_CMD; |
---|
1406 | res->data=ssiReadList(l); |
---|
1407 | break; |
---|
1408 | case 16: res->rtyp=NONE; res->data=NULL; |
---|
1409 | break; |
---|
1410 | case 17: res->rtyp=INTVEC_CMD; |
---|
1411 | res->data=ssiReadIntvec(d); |
---|
1412 | break; |
---|
1413 | case 18: res->rtyp=INTMAT_CMD; |
---|
1414 | res->data=ssiReadIntmat(d); |
---|
1415 | break; |
---|
1416 | case 19: res->rtyp=BIGINTMAT_CMD; |
---|
1417 | res->data=ssiReadBigintmat(d); |
---|
1418 | break; |
---|
1419 | case 20: ssiReadBlackbox(res,l); |
---|
1420 | break; |
---|
1421 | case 21: ssiReadAttrib(res,l); |
---|
1422 | break; |
---|
1423 | case 23: ssiReadRingProperties(l); |
---|
1424 | return ssiRead1(l); |
---|
1425 | break; |
---|
1426 | // ------------ |
---|
1427 | case 98: // version |
---|
1428 | { |
---|
1429 | int n98_v,n98_m; |
---|
1430 | BITSET n98_o1,n98_o2; |
---|
1431 | n98_v=s_readint(d->f_read); |
---|
1432 | n98_m=s_readint(d->f_read); |
---|
1433 | n98_o1=s_readint(d->f_read); |
---|
1434 | n98_o2=s_readint(d->f_read); |
---|
1435 | if ((n98_v!=SSI_VERSION) ||(n98_m!=MAX_TOK)) |
---|
1436 | { |
---|
1437 | Print("incompatible versions of ssi: %d/%d vs %d/%d\n", |
---|
1438 | SSI_VERSION,MAX_TOK,n98_v,n98_m); |
---|
1439 | } |
---|
1440 | #ifndef SING_NDEBUG |
---|
1441 | if (TEST_OPT_DEBUG) |
---|
1442 | Print("// opening ssi-%d, MAX_TOK=%d\n",n98_v,n98_m); |
---|
1443 | #endif |
---|
1444 | si_opt_1=n98_o1; |
---|
1445 | si_opt_2=n98_o2; |
---|
1446 | omFreeBin(res,sleftv_bin); |
---|
1447 | return ssiRead1(l); |
---|
1448 | } |
---|
1449 | case 99: omFreeBin(res,sleftv_bin); ssiClose(l); m2_end(0); |
---|
1450 | case 0: if (s_iseof(d->f_read)) |
---|
1451 | { |
---|
1452 | ssiClose(l); |
---|
1453 | } |
---|
1454 | res->rtyp=DEF_CMD; |
---|
1455 | break; |
---|
1456 | default: Werror("not implemented (t:%d)",t); |
---|
1457 | omFreeBin(res,sleftv_bin); |
---|
1458 | res=NULL; |
---|
1459 | break; |
---|
1460 | } |
---|
1461 | // if currRing is required for the result, but lost |
---|
1462 | // define "ssiRing%d" as currRing: |
---|
1463 | if ((d->r!=NULL) |
---|
1464 | && (currRing!=d->r) |
---|
1465 | && (res->RingDependend())) |
---|
1466 | { |
---|
1467 | if(ssiSetCurrRing(d->r)) { d->r=currRing; d->r->ref++; } |
---|
1468 | } |
---|
1469 | return res; |
---|
1470 | no_ring: WerrorS("no ring"); |
---|
1471 | omFreeBin(res,sleftv_bin); |
---|
1472 | return NULL; |
---|
1473 | } |
---|
1474 | //**************************************************************************/ |
---|
1475 | BOOLEAN ssiSetRing(si_link l, ring r, BOOLEAN send) |
---|
1476 | { |
---|
1477 | if(SI_LINK_W_OPEN_P(l)==0) |
---|
1478 | if (slOpen(l,SI_LINK_OPEN|SI_LINK_WRITE,NULL)) return TRUE; |
---|
1479 | ssiInfo *d = (ssiInfo *)l->data; |
---|
1480 | if (d->r!=r) |
---|
1481 | { |
---|
1482 | if (send) |
---|
1483 | { |
---|
1484 | fputs("15 ",d->f_write); |
---|
1485 | ssiWriteRing(d,r); |
---|
1486 | } |
---|
1487 | d->r=r; |
---|
1488 | } |
---|
1489 | if (currRing!=r) rChangeCurrRing(r); |
---|
1490 | return FALSE; |
---|
1491 | } |
---|
1492 | //**************************************************************************/ |
---|
1493 | |
---|
1494 | BOOLEAN ssiWrite(si_link l, leftv data) |
---|
1495 | { |
---|
1496 | if(SI_LINK_W_OPEN_P(l)==0) |
---|
1497 | if (slOpen(l,SI_LINK_OPEN|SI_LINK_WRITE,NULL)) return TRUE; |
---|
1498 | ssiInfo *d = (ssiInfo *)l->data; |
---|
1499 | d->level++; |
---|
1500 | //FILE *fich=d->f; |
---|
1501 | while (data!=NULL) |
---|
1502 | { |
---|
1503 | int tt=data->Typ(); |
---|
1504 | void *dd=data->Data(); |
---|
1505 | attr *aa=data->Attribute(); |
---|
1506 | BOOLEAN with_attr=FALSE; |
---|
1507 | if ((aa!=NULL) && ((*aa)!=NULL)) |
---|
1508 | { |
---|
1509 | attr a=*aa; |
---|
1510 | int n=0; |
---|
1511 | while(a!=NULL) { n++; a=a->next;} |
---|
1512 | fprintf(d->f_write,"21 %d %d ",data->flag,n); |
---|
1513 | } |
---|
1514 | else if (data->flag!=0) |
---|
1515 | { |
---|
1516 | fprintf(d->f_write,"21 %d 0 ",data->flag); |
---|
1517 | } |
---|
1518 | if ((dd==NULL) && (data->name!=NULL) && (tt==0)) tt=DEF_CMD; |
---|
1519 | // return pure undefined names as def |
---|
1520 | |
---|
1521 | switch(tt /*data->Typ()*/) |
---|
1522 | { |
---|
1523 | case 0: /*error*/ |
---|
1524 | case NONE/* nothing*/:fputs("16 ",d->f_write); |
---|
1525 | break; |
---|
1526 | case STRING_CMD: fputs("2 ",d->f_write); |
---|
1527 | ssiWriteString(d,(char *)dd); |
---|
1528 | break; |
---|
1529 | case INT_CMD: fputs("1 ",d->f_write); |
---|
1530 | ssiWriteInt(d,(int)(long)dd); |
---|
1531 | break; |
---|
1532 | case BIGINT_CMD:fputs("4 ",d->f_write); |
---|
1533 | ssiWriteBigInt(d,(number)dd); |
---|
1534 | break; |
---|
1535 | case NUMBER_CMD: |
---|
1536 | if (d->r!=currRing) |
---|
1537 | { |
---|
1538 | fputs("15 ",d->f_write); |
---|
1539 | ssiWriteRing(d,currRing); |
---|
1540 | if (d->level<=1) fputc('\n',d->f_write); |
---|
1541 | } |
---|
1542 | fputs("3 ",d->f_write); |
---|
1543 | ssiWriteNumber(d,(number)dd); |
---|
1544 | break; |
---|
1545 | case RING_CMD:fputs("5 ",d->f_write); |
---|
1546 | ssiWriteRing(d,(ring)dd); |
---|
1547 | break; |
---|
1548 | case BUCKET_CMD: |
---|
1549 | { |
---|
1550 | sBucket_pt b=(sBucket_pt)dd; |
---|
1551 | if (d->r!=sBucketGetRing(b)) |
---|
1552 | { |
---|
1553 | fputs("15 ",d->f_write); |
---|
1554 | ssiWriteRing(d,sBucketGetRing(b)); |
---|
1555 | if (d->level<=1) fputc('\n',d->f_write); |
---|
1556 | } |
---|
1557 | fputs("6 ",d->f_write); |
---|
1558 | ssiWritePoly(d,tt,sBucketPeek(b)); |
---|
1559 | break; |
---|
1560 | } |
---|
1561 | case POLY_CMD: |
---|
1562 | case VECTOR_CMD: |
---|
1563 | if (d->r!=currRing) |
---|
1564 | { |
---|
1565 | fputs("15 ",d->f_write); |
---|
1566 | ssiWriteRing(d,currRing); |
---|
1567 | if (d->level<=1) fputc('\n',d->f_write); |
---|
1568 | } |
---|
1569 | if(tt==POLY_CMD) fputs("6 ",d->f_write); |
---|
1570 | else fputs("9 ",d->f_write); |
---|
1571 | ssiWritePoly(d,tt,(poly)dd); |
---|
1572 | break; |
---|
1573 | case IDEAL_CMD: |
---|
1574 | case MODUL_CMD: |
---|
1575 | case MATRIX_CMD: |
---|
1576 | case SMATRIX_CMD: |
---|
1577 | if (d->r!=currRing) |
---|
1578 | { |
---|
1579 | fputs("15 ",d->f_write); |
---|
1580 | ssiWriteRing(d,currRing); |
---|
1581 | if (d->level<=1) fputc('\n',d->f_write); |
---|
1582 | } |
---|
1583 | if(tt==IDEAL_CMD) fputs("7 ",d->f_write); |
---|
1584 | else if(tt==MATRIX_CMD) fputs("8 ",d->f_write); |
---|
1585 | else if(tt==SMATRIX_CMD) fputs("22 ",d->f_write); |
---|
1586 | else /* tt==MODUL_CMD*/ |
---|
1587 | { |
---|
1588 | ideal M=(ideal)dd; |
---|
1589 | fprintf(d->f_write,"10 %d ",(int)M->rank); |
---|
1590 | } |
---|
1591 | ssiWriteIdeal(d,tt,(ideal)dd); |
---|
1592 | break; |
---|
1593 | case COMMAND: |
---|
1594 | fputs("11 ",d->f_write); |
---|
1595 | ssiWriteCommand(l,(command)dd); |
---|
1596 | break; |
---|
1597 | case DEF_CMD: /* not evaluated stuff in quotes */ |
---|
1598 | fputs("12 ",d->f_write); |
---|
1599 | ssiWriteString(d,data->Name()); |
---|
1600 | break; |
---|
1601 | case PROC_CMD: |
---|
1602 | fputs("13 ",d->f_write); |
---|
1603 | ssiWriteProc(d,(procinfov)dd); |
---|
1604 | break; |
---|
1605 | case LIST_CMD: |
---|
1606 | fputs("14 ",d->f_write); |
---|
1607 | ssiWriteList(l,(lists)dd); |
---|
1608 | break; |
---|
1609 | case INTVEC_CMD: |
---|
1610 | fputs("17 ",d->f_write); |
---|
1611 | ssiWriteIntvec(d,(intvec *)dd); |
---|
1612 | break; |
---|
1613 | case INTMAT_CMD: |
---|
1614 | fputs("18 ",d->f_write); |
---|
1615 | ssiWriteIntmat(d,(intvec *)dd); |
---|
1616 | break; |
---|
1617 | case BIGINTMAT_CMD: |
---|
1618 | fputs("19 ",d->f_write); |
---|
1619 | ssiWriteBigintmat(d,(bigintmat *)dd); |
---|
1620 | break; |
---|
1621 | default: |
---|
1622 | if (tt>MAX_TOK) |
---|
1623 | { |
---|
1624 | blackbox *b=getBlackboxStuff(tt); |
---|
1625 | fputs("20 ",d->f_write); |
---|
1626 | b->blackbox_serialize(b,dd,l); |
---|
1627 | } |
---|
1628 | else |
---|
1629 | { |
---|
1630 | Werror("not implemented (t:%d, rtyp:%d)",tt, data->rtyp); |
---|
1631 | d->level=0; |
---|
1632 | return TRUE; |
---|
1633 | } |
---|
1634 | break; |
---|
1635 | } |
---|
1636 | if (d->level<=1) { fputc('\n',d->f_write); fflush(d->f_write); } |
---|
1637 | data=data->next; |
---|
1638 | } |
---|
1639 | d->level--; |
---|
1640 | return FALSE; |
---|
1641 | } |
---|
1642 | |
---|
1643 | BOOLEAN ssiGetDump(si_link l); |
---|
1644 | BOOLEAN ssiDump(si_link l); |
---|
1645 | |
---|
1646 | si_link_extension slInitSsiExtension(si_link_extension s) |
---|
1647 | { |
---|
1648 | s->Open=ssiOpen; |
---|
1649 | s->Close=ssiClose; |
---|
1650 | s->Kill=ssiClose; |
---|
1651 | s->Read=ssiRead1; |
---|
1652 | s->Read2=(slRead2Proc)NULL; |
---|
1653 | s->Write=ssiWrite; |
---|
1654 | s->Dump=ssiDump; |
---|
1655 | s->GetDump=ssiGetDump; |
---|
1656 | |
---|
1657 | s->Status=slStatusSsi; |
---|
1658 | s->SetRing=ssiSetRing; |
---|
1659 | s->type="ssi"; |
---|
1660 | return s; |
---|
1661 | } |
---|
1662 | |
---|
1663 | const char* slStatusSsi(si_link l, const char* request) |
---|
1664 | { |
---|
1665 | ssiInfo *d=(ssiInfo*)l->data; |
---|
1666 | if (d==NULL) return "not open"; |
---|
1667 | if (((strcmp(l->mode,"fork")==0) |
---|
1668 | ||(strcmp(l->mode,"tcp")==0) |
---|
1669 | ||(strcmp(l->mode,"connect")==0)) |
---|
1670 | && (strcmp(request, "read") == 0)) |
---|
1671 | { |
---|
1672 | fd_set mask; |
---|
1673 | struct timeval wt; |
---|
1674 | if (s_isready(d->f_read)) return "ready"; |
---|
1675 | loop |
---|
1676 | { |
---|
1677 | /* Don't block. Return socket status immediately. */ |
---|
1678 | wt.tv_sec = 0; |
---|
1679 | wt.tv_usec = 0; |
---|
1680 | |
---|
1681 | FD_ZERO(&mask); |
---|
1682 | FD_SET(d->fd_read, &mask); |
---|
1683 | //Print("test fd %d\n",d->fd_read); |
---|
1684 | /* check with select: chars waiting: no -> not ready */ |
---|
1685 | switch (si_select(d->fd_read+1, &mask, NULL, NULL, &wt)) |
---|
1686 | { |
---|
1687 | case 0: /* not ready */ return "not ready"; |
---|
1688 | case -1: /*error*/ return "error"; |
---|
1689 | case 1: /*ready ? */ break; |
---|
1690 | } |
---|
1691 | /* yes: read 1 char*/ |
---|
1692 | /* if \n, check again with select else ungetc(c), ready*/ |
---|
1693 | int c=s_getc(d->f_read); |
---|
1694 | //Print("try c=%d\n",c); |
---|
1695 | if (c== -1) return "eof"; /* eof or error */ |
---|
1696 | else if (isdigit(c)) |
---|
1697 | { s_ungetc(c,d->f_read); return "ready"; } |
---|
1698 | else if (c>' ') |
---|
1699 | { |
---|
1700 | Werror("unknown char in ssiLink(%d)",c); |
---|
1701 | return "error"; |
---|
1702 | } |
---|
1703 | /* else: next char */ |
---|
1704 | } |
---|
1705 | } |
---|
1706 | else if (strcmp(request, "read") == 0) |
---|
1707 | { |
---|
1708 | if (SI_LINK_R_OPEN_P(l) && (!s_iseof(d->f_read)) && (s_isready(d->f_read))) return "ready"; |
---|
1709 | else return "not ready"; |
---|
1710 | } |
---|
1711 | else if (strcmp(request, "write") == 0) |
---|
1712 | { |
---|
1713 | if (SI_LINK_W_OPEN_P(l)) return "ready"; |
---|
1714 | else return "not ready"; |
---|
1715 | } |
---|
1716 | else return "unknown status request"; |
---|
1717 | } |
---|
1718 | |
---|
1719 | int slStatusSsiL(lists L, int timeout) |
---|
1720 | { |
---|
1721 | // input: L: a list with links of type |
---|
1722 | // ssi-connect, ssi-fork, ssi-tcp, MPtcp-fork or MPtcp-launch. |
---|
1723 | // Note: Not every entry in L must be set. |
---|
1724 | // timeout: timeout for select in micro-seconds |
---|
1725 | // or -1 for infinity |
---|
1726 | // or 0 for polling |
---|
1727 | // returns: ERROR (via Werror): L has wrong elements or link not open |
---|
1728 | // -2: select returns an error |
---|
1729 | // -1: the read state of all links is eof |
---|
1730 | // 0: timeout (or polling): none ready, |
---|
1731 | // i>0: (at least) L[i] is ready |
---|
1732 | si_link l; |
---|
1733 | ssiInfo *d; |
---|
1734 | int d_fd; |
---|
1735 | fd_set mask, fdmask; |
---|
1736 | FD_ZERO(&fdmask); |
---|
1737 | FD_ZERO(&mask); |
---|
1738 | int max_fd=0; /* 1 + max fd in fd_set */ |
---|
1739 | |
---|
1740 | /* timeout */ |
---|
1741 | struct timeval wt; |
---|
1742 | struct timeval *wt_ptr=&wt; |
---|
1743 | int startingtime = getRTimer()/TIMER_RESOLUTION; // in seconds |
---|
1744 | if (timeout== -1) |
---|
1745 | { |
---|
1746 | wt_ptr=NULL; |
---|
1747 | } |
---|
1748 | else |
---|
1749 | { |
---|
1750 | wt.tv_sec = timeout / 1000000; |
---|
1751 | wt.tv_usec = timeout % 1000000; |
---|
1752 | } |
---|
1753 | |
---|
1754 | /* auxiliary variables */ |
---|
1755 | int i; |
---|
1756 | int j; |
---|
1757 | int k; |
---|
1758 | int s; |
---|
1759 | char fdmaskempty; |
---|
1760 | |
---|
1761 | /* check the links and fill in fdmask */ |
---|
1762 | /* check ssi links for ungetc_buf */ |
---|
1763 | for(i=L->nr; i>=0; i--) |
---|
1764 | { |
---|
1765 | if (L->m[i].Typ()!=DEF_CMD) |
---|
1766 | { |
---|
1767 | if (L->m[i].Typ()!=LINK_CMD) |
---|
1768 | { WerrorS("all elements must be of type link"); return -2;} |
---|
1769 | l=(si_link)L->m[i].Data(); |
---|
1770 | if(SI_LINK_OPEN_P(l)==0) |
---|
1771 | { WerrorS("all links must be open"); return -2;} |
---|
1772 | if (((strcmp(l->m->type,"ssi")!=0) && (strcmp(l->m->type,"MPtcp")!=0)) |
---|
1773 | || ((strcmp(l->mode,"fork")!=0) && (strcmp(l->mode,"tcp")!=0) |
---|
1774 | && (strcmp(l->mode,"launch")!=0) && (strcmp(l->mode,"connect")!=0))) |
---|
1775 | { |
---|
1776 | WerrorS("all links must be of type ssi:fork, ssi:tcp, ssi:connect"); |
---|
1777 | return -2; |
---|
1778 | } |
---|
1779 | if (strcmp(l->m->type,"ssi")==0) |
---|
1780 | { |
---|
1781 | d=(ssiInfo*)l->data; |
---|
1782 | d_fd=d->fd_read; |
---|
1783 | if (!s_isready(d->f_read)) |
---|
1784 | { |
---|
1785 | FD_SET(d_fd, &fdmask); |
---|
1786 | if (d_fd > max_fd) max_fd=d_fd; |
---|
1787 | } |
---|
1788 | else |
---|
1789 | return i+1; |
---|
1790 | } |
---|
1791 | else |
---|
1792 | { |
---|
1793 | Werror("wrong link type >>%s<<",l->m->type); |
---|
1794 | return -2; |
---|
1795 | } |
---|
1796 | } |
---|
1797 | } |
---|
1798 | max_fd++; |
---|
1799 | |
---|
1800 | do_select: |
---|
1801 | /* copy fdmask to mask */ |
---|
1802 | FD_ZERO(&mask); |
---|
1803 | for(k = 0; k < max_fd; k++) |
---|
1804 | { |
---|
1805 | if(FD_ISSET(k, &fdmask)) |
---|
1806 | { |
---|
1807 | FD_SET(k, &mask); |
---|
1808 | } |
---|
1809 | } |
---|
1810 | |
---|
1811 | /* check with select: chars waiting: no -> not ready */ |
---|
1812 | s = si_select(max_fd, &mask, NULL, NULL, wt_ptr); |
---|
1813 | if (s==-1) |
---|
1814 | { |
---|
1815 | WerrorS("error in select call"); |
---|
1816 | return -2; /*error*/ |
---|
1817 | } |
---|
1818 | if (s==0) |
---|
1819 | { |
---|
1820 | return 0; /*poll: not ready */ |
---|
1821 | } |
---|
1822 | else /* s>0, at least one ready (the number of fd which are ready is s)*/ |
---|
1823 | { |
---|
1824 | j=0; |
---|
1825 | while (j<=max_fd) { if (FD_ISSET(j,&mask)) break; j++; } |
---|
1826 | for(i=L->nr; i>=0; i--) |
---|
1827 | { |
---|
1828 | if (L->m[i].rtyp==LINK_CMD) |
---|
1829 | { |
---|
1830 | l=(si_link)L->m[i].Data(); |
---|
1831 | if (strcmp(l->m->type,"ssi")==0) |
---|
1832 | { |
---|
1833 | d=(ssiInfo*)l->data; |
---|
1834 | d_fd=d->fd_read; |
---|
1835 | if(j==d_fd) break; |
---|
1836 | } |
---|
1837 | else |
---|
1838 | { |
---|
1839 | Werror("wrong link type >>%s<<",l->m->type); |
---|
1840 | return -2; |
---|
1841 | } |
---|
1842 | } |
---|
1843 | } |
---|
1844 | // only ssi links: |
---|
1845 | loop |
---|
1846 | { |
---|
1847 | /* yes: read 1 char*/ |
---|
1848 | /* if \n, check again with select else ungetc(c), ready*/ |
---|
1849 | /* setting: d: current ssiInfo, j current fd, i current entry in L*/ |
---|
1850 | int c=s_getc(d->f_read); |
---|
1851 | //Print("try c=%d\n",c); |
---|
1852 | if (c== -1) /* eof */ |
---|
1853 | { |
---|
1854 | FD_CLR(j,&fdmask); |
---|
1855 | fdmaskempty = 1; |
---|
1856 | for(k = 0; k < max_fd; k++) |
---|
1857 | { |
---|
1858 | if(FD_ISSET(k, &fdmask)) |
---|
1859 | { |
---|
1860 | fdmaskempty = 0; |
---|
1861 | break; |
---|
1862 | } |
---|
1863 | } |
---|
1864 | if(fdmaskempty) |
---|
1865 | { |
---|
1866 | return -1; |
---|
1867 | } |
---|
1868 | if(timeout != -1) |
---|
1869 | { |
---|
1870 | timeout = si_max(0, |
---|
1871 | timeout - 1000000*(getRTimer()/TIMER_RESOLUTION - startingtime)); |
---|
1872 | wt.tv_sec = timeout / 1000000; |
---|
1873 | wt.tv_usec = (timeout % 1000000); |
---|
1874 | } |
---|
1875 | goto do_select; |
---|
1876 | } |
---|
1877 | |
---|
1878 | else if (isdigit(c)) |
---|
1879 | { s_ungetc(c,d->f_read); return i+1; } |
---|
1880 | else if (c>' ') |
---|
1881 | { |
---|
1882 | Werror("unknown char in ssiLink(%d)",c); |
---|
1883 | return -2; |
---|
1884 | } |
---|
1885 | /* else: next char */ |
---|
1886 | goto do_select; |
---|
1887 | } |
---|
1888 | } |
---|
1889 | } |
---|
1890 | |
---|
1891 | int ssiBatch(const char *host, const char * port) |
---|
1892 | /* return 0 on success, >0 else*/ |
---|
1893 | { |
---|
1894 | si_link l=(si_link) omAlloc0Bin(sip_link_bin); |
---|
1895 | char *buf=(char*)omAlloc(256); |
---|
1896 | sprintf(buf,"ssi:connect %s:%s",host,port); |
---|
1897 | slInit(l, buf); |
---|
1898 | omFreeSize(buf,256); |
---|
1899 | if (slOpen(l,SI_LINK_OPEN,NULL)) return 1; |
---|
1900 | SI_LINK_SET_RW_OPEN_P(l); |
---|
1901 | |
---|
1902 | idhdl id = enterid("link_ll", 0, LINK_CMD, &IDROOT, FALSE); |
---|
1903 | IDLINK(id) = l; |
---|
1904 | |
---|
1905 | loop |
---|
1906 | { |
---|
1907 | leftv h=ssiRead1(l); /*contains an exit.... */ |
---|
1908 | if (feErrors != NULL && *feErrors != '\0') |
---|
1909 | { |
---|
1910 | // handle errors: |
---|
1911 | PrintS(feErrors); /* currently quite simple */ |
---|
1912 | *feErrors = '\0'; |
---|
1913 | } |
---|
1914 | ssiWrite(l,h); |
---|
1915 | h->CleanUp(); |
---|
1916 | omFreeBin(h, sleftv_bin); |
---|
1917 | } |
---|
1918 | /* never reached*/ |
---|
1919 | exit(0); |
---|
1920 | } |
---|
1921 | |
---|
1922 | STATIC_VAR int ssiReserved_P=0; |
---|
1923 | STATIC_VAR int ssiReserved_sockfd; |
---|
1924 | STATIC_VAR struct sockaddr_in ssiResverd_serv_addr; |
---|
1925 | STATIC_VAR int ssiReserved_Clients; |
---|
1926 | int ssiReservePort(int clients) |
---|
1927 | { |
---|
1928 | if (ssiReserved_P!=0) |
---|
1929 | { |
---|
1930 | WerrorS("ERROR already a reverved port requested"); |
---|
1931 | return 0; |
---|
1932 | } |
---|
1933 | int portno; |
---|
1934 | ssiReserved_sockfd = socket(AF_INET, SOCK_STREAM, 0); |
---|
1935 | if(ssiReserved_sockfd < 0) |
---|
1936 | { |
---|
1937 | WerrorS("ERROR opening socket"); |
---|
1938 | return 0; |
---|
1939 | } |
---|
1940 | memset((char *) &ssiResverd_serv_addr,0, sizeof(ssiResverd_serv_addr)); |
---|
1941 | portno = 1025; |
---|
1942 | ssiResverd_serv_addr.sin_family = AF_INET; |
---|
1943 | ssiResverd_serv_addr.sin_addr.s_addr = INADDR_ANY; |
---|
1944 | do |
---|
1945 | { |
---|
1946 | portno++; |
---|
1947 | ssiResverd_serv_addr.sin_port = htons(portno); |
---|
1948 | if(portno > 50000) |
---|
1949 | { |
---|
1950 | WerrorS("ERROR on binding (no free port available?)"); |
---|
1951 | return 0; |
---|
1952 | } |
---|
1953 | } |
---|
1954 | while(bind(ssiReserved_sockfd, (struct sockaddr *) &ssiResverd_serv_addr, sizeof(ssiResverd_serv_addr)) < 0); |
---|
1955 | ssiReserved_P=portno; |
---|
1956 | listen(ssiReserved_sockfd,clients); |
---|
1957 | ssiReserved_Clients=clients; |
---|
1958 | return portno; |
---|
1959 | } |
---|
1960 | |
---|
1961 | EXTERN_VAR si_link_extension si_link_root; |
---|
1962 | si_link ssiCommandLink() |
---|
1963 | { |
---|
1964 | if (ssiReserved_P==0) |
---|
1965 | { |
---|
1966 | WerrorS("ERROR no reverved port requested"); |
---|
1967 | return NULL; |
---|
1968 | } |
---|
1969 | struct sockaddr_in cli_addr; |
---|
1970 | int clilen = sizeof(cli_addr); |
---|
1971 | int newsockfd = si_accept(ssiReserved_sockfd, (struct sockaddr *) &cli_addr, (socklen_t *)&clilen); |
---|
1972 | if(newsockfd < 0) |
---|
1973 | { |
---|
1974 | Werror("ERROR on accept (errno=%d)",errno); |
---|
1975 | return NULL; |
---|
1976 | } |
---|
1977 | si_link l=(si_link) omAlloc0Bin(sip_link_bin); |
---|
1978 | si_link_extension s = si_link_root; |
---|
1979 | si_link_extension prev = s; |
---|
1980 | while (strcmp(s->type, "ssi") != 0) |
---|
1981 | { |
---|
1982 | if (s->next == NULL) |
---|
1983 | { |
---|
1984 | prev = s; |
---|
1985 | s = NULL; |
---|
1986 | break; |
---|
1987 | } |
---|
1988 | else |
---|
1989 | { |
---|
1990 | s = s->next; |
---|
1991 | } |
---|
1992 | } |
---|
1993 | if (s != NULL) |
---|
1994 | l->m = s; |
---|
1995 | else |
---|
1996 | { |
---|
1997 | si_link_extension ns = (si_link_extension)omAlloc0Bin(s_si_link_extension_bin); |
---|
1998 | prev->next=slInitSsiExtension(ns); |
---|
1999 | l->m = prev->next; |
---|
2000 | } |
---|
2001 | l->name=omStrDup(""); |
---|
2002 | l->mode=omStrDup("tcp"); |
---|
2003 | l->ref=1; |
---|
2004 | ssiInfo *d=(ssiInfo*)omAlloc0(sizeof(ssiInfo)); |
---|
2005 | l->data=d; |
---|
2006 | d->fd_read = newsockfd; |
---|
2007 | d->fd_write = newsockfd; |
---|
2008 | d->f_read = s_open(newsockfd); |
---|
2009 | d->f_write = fdopen(newsockfd, "w"); |
---|
2010 | SI_LINK_SET_RW_OPEN_P(l); |
---|
2011 | ssiReserved_Clients--; |
---|
2012 | if (ssiReserved_Clients<=0) |
---|
2013 | { |
---|
2014 | ssiReserved_P=0; |
---|
2015 | si_close(ssiReserved_sockfd); |
---|
2016 | } |
---|
2017 | return l; |
---|
2018 | } |
---|
2019 | /*---------------------------------------------------------------------*/ |
---|
2020 | /** |
---|
2021 | * @brief additional default signal handler |
---|
2022 | |
---|
2023 | // some newer Linux version cannot have SIG_IGN for SIGCHLD, |
---|
2024 | // so use this nice routine here: |
---|
2025 | // SuSe 9.x reports -1 always |
---|
2026 | // Redhat 9.x/FC x reports sometimes -1 |
---|
2027 | // see also: hpux_system |
---|
2028 | // also needed by getrusage (timer etc.) |
---|
2029 | |
---|
2030 | @param[in] sig |
---|
2031 | **/ |
---|
2032 | /*---------------------------------------------------------------------*/ |
---|
2033 | void sig_chld_hdl(int) |
---|
2034 | { |
---|
2035 | pid_t kidpid; |
---|
2036 | int status; |
---|
2037 | |
---|
2038 | loop |
---|
2039 | { |
---|
2040 | kidpid = si_waitpid(-1, &status, WNOHANG); |
---|
2041 | if (kidpid==-1) |
---|
2042 | { |
---|
2043 | /* continue on interruption (EINTR): */ |
---|
2044 | if (errno == EINTR) continue; |
---|
2045 | /* break on anything else (EINVAL or ECHILD according to manpage): */ |
---|
2046 | break; |
---|
2047 | } |
---|
2048 | else if (kidpid==0) break; /* no more children to process, so break */ |
---|
2049 | |
---|
2050 | //printf("Child %ld terminated\n", kidpid); |
---|
2051 | link_list hh=ssiToBeClosed; |
---|
2052 | while((hh!=NULL)&&(ssiToBeClosed_inactive)) |
---|
2053 | { |
---|
2054 | if((hh->l!=NULL) && (hh->l->m->Open==ssiOpen)) |
---|
2055 | { |
---|
2056 | ssiInfo *d = (ssiInfo *)hh->l->data; |
---|
2057 | if(d->pid==kidpid) |
---|
2058 | { |
---|
2059 | if(ssiToBeClosed_inactive) |
---|
2060 | { |
---|
2061 | ssiToBeClosed_inactive=FALSE; |
---|
2062 | slClose(hh->l); |
---|
2063 | ssiToBeClosed_inactive=TRUE; |
---|
2064 | break; |
---|
2065 | } |
---|
2066 | else break; |
---|
2067 | } |
---|
2068 | else hh=(link_list)hh->next; |
---|
2069 | } |
---|
2070 | else hh=(link_list)hh->next; |
---|
2071 | } |
---|
2072 | } |
---|
2073 | } |
---|
2074 | |
---|
2075 | static BOOLEAN DumpSsiIdhdl(si_link l, idhdl h) |
---|
2076 | { |
---|
2077 | int type_id = IDTYP(h); |
---|
2078 | |
---|
2079 | // C-proc not to be dumped, also LIB-proc not |
---|
2080 | if (type_id == PROC_CMD) |
---|
2081 | { |
---|
2082 | if (IDPROC(h)->language == LANG_C) return FALSE; |
---|
2083 | if (IDPROC(h)->libname != NULL) return FALSE; |
---|
2084 | } |
---|
2085 | // do not dump links |
---|
2086 | if (type_id == LINK_CMD) return FALSE; |
---|
2087 | |
---|
2088 | // do not dump ssi internal rings: ssiRing* |
---|
2089 | if ((type_id == RING_CMD) && (strncmp(IDID(h),"ssiRing",7)==0)) |
---|
2090 | return FALSE; |
---|
2091 | |
---|
2092 | // do not dump default cring: |
---|
2093 | if (type_id == CRING_CMD) |
---|
2094 | { |
---|
2095 | if (strcmp(IDID(h),"ZZ")==0) return FALSE; |
---|
2096 | if (strcmp(IDID(h),"QQ")==0) return FALSE; |
---|
2097 | #ifdef SINGULAR_4_2 |
---|
2098 | if (strcmp(IDID(h),"AE")==0) return FALSE; |
---|
2099 | if (strcmp(IDID(h),"QAE")==0) return FALSE; |
---|
2100 | #endif |
---|
2101 | } |
---|
2102 | |
---|
2103 | command D=(command)omAlloc0(sizeof(*D)); |
---|
2104 | sleftv tmp; |
---|
2105 | memset(&tmp,0,sizeof(tmp)); |
---|
2106 | tmp.rtyp=COMMAND; |
---|
2107 | tmp.data=D; |
---|
2108 | |
---|
2109 | if (type_id == PACKAGE_CMD) |
---|
2110 | { |
---|
2111 | // do not dump Top, Standard |
---|
2112 | if ((strcmp(IDID(h), "Top") == 0) |
---|
2113 | || (strcmp(IDID(h), "Standard") == 0)) |
---|
2114 | { |
---|
2115 | omFreeSize(D,sizeof(*D)); |
---|
2116 | return FALSE; |
---|
2117 | } |
---|
2118 | package p=(package)IDDATA(h); |
---|
2119 | // dump Singular-packages as LIB("..."); |
---|
2120 | if (p->language==LANG_SINGULAR) |
---|
2121 | { |
---|
2122 | D->op=LOAD_CMD; |
---|
2123 | D->argc=2; |
---|
2124 | D->arg1.rtyp=STRING_CMD; |
---|
2125 | D->arg1.data=p->libname; |
---|
2126 | D->arg2.rtyp=STRING_CMD; |
---|
2127 | D->arg2.data=(char*)"with"; |
---|
2128 | ssiWrite(l,&tmp); |
---|
2129 | omFreeSize(D,sizeof(*D)); |
---|
2130 | return FALSE; |
---|
2131 | } |
---|
2132 | // dump Singular-packages as load("..."); |
---|
2133 | else if (p->language==LANG_C) |
---|
2134 | { |
---|
2135 | D->op=LOAD_CMD; |
---|
2136 | D->argc=1; |
---|
2137 | D->arg1.rtyp=STRING_CMD; |
---|
2138 | D->arg1.data=p->libname; |
---|
2139 | ssiWrite(l,&tmp); |
---|
2140 | omFreeSize(D,sizeof(*D)); |
---|
2141 | return FALSE; |
---|
2142 | } |
---|
2143 | } |
---|
2144 | |
---|
2145 | // put type and name |
---|
2146 | //Print("generic dump:%s,%s\n",IDID(h),Tok2Cmdname(IDTYP(h))); |
---|
2147 | D->op='='; |
---|
2148 | D->argc=2; |
---|
2149 | D->arg1.rtyp=DEF_CMD; |
---|
2150 | D->arg1.name=IDID(h); |
---|
2151 | D->arg2.rtyp=IDTYP(h); |
---|
2152 | D->arg2.data=IDDATA(h); |
---|
2153 | ssiWrite(l,&tmp); |
---|
2154 | omFreeSize(D,sizeof(*D)); |
---|
2155 | return FALSE; |
---|
2156 | } |
---|
2157 | static BOOLEAN ssiDumpIter(si_link l, idhdl h) |
---|
2158 | { |
---|
2159 | if (h == NULL) return FALSE; |
---|
2160 | |
---|
2161 | if (ssiDumpIter(l, IDNEXT(h))) return TRUE; |
---|
2162 | |
---|
2163 | // need to set the ring before writing it, otherwise we get in |
---|
2164 | // trouble with minpoly |
---|
2165 | if (IDTYP(h) == RING_CMD) |
---|
2166 | rSetHdl(h); |
---|
2167 | |
---|
2168 | if (DumpSsiIdhdl(l, h)) return TRUE; |
---|
2169 | |
---|
2170 | // do not dump ssi internal rings: ssiRing* |
---|
2171 | // but dump objects of all other rings |
---|
2172 | if ((IDTYP(h) == RING_CMD) |
---|
2173 | && (strncmp(IDID(h),"ssiRing",7)!=0)) |
---|
2174 | return ssiDumpIter(l, IDRING(h)->idroot); |
---|
2175 | else |
---|
2176 | return FALSE; |
---|
2177 | } |
---|
2178 | BOOLEAN ssiDump(si_link l) |
---|
2179 | { |
---|
2180 | idhdl h = IDROOT, rh = currRingHdl; |
---|
2181 | BOOLEAN status = ssiDumpIter(l, h); |
---|
2182 | |
---|
2183 | //if (! status ) status = DumpAsciiMaps(fd, h, NULL); |
---|
2184 | |
---|
2185 | if (currRingHdl != rh) rSetHdl(rh); |
---|
2186 | //fprintf(fd, "option(set, intvec(%d, %d));\n", si_opt_1, si_opt_2); |
---|
2187 | |
---|
2188 | return status; |
---|
2189 | } |
---|
2190 | BOOLEAN ssiGetDump(si_link l) |
---|
2191 | { |
---|
2192 | ssiInfo *d=(ssiInfo*)l->data; |
---|
2193 | loop |
---|
2194 | { |
---|
2195 | if (!SI_LINK_OPEN_P(l)) break; |
---|
2196 | if (s_iseof(d->f_read)) break; |
---|
2197 | leftv h=ssiRead1(l); /*contains an exit.... */ |
---|
2198 | if (feErrors != NULL && *feErrors != '\0') |
---|
2199 | { |
---|
2200 | // handle errors: |
---|
2201 | PrintS(feErrors); /* currently quite simple */ |
---|
2202 | return TRUE; |
---|
2203 | *feErrors = '\0'; |
---|
2204 | } |
---|
2205 | h->CleanUp(); |
---|
2206 | omFreeBin(h, sleftv_bin); |
---|
2207 | } |
---|
2208 | return FALSE; |
---|
2209 | } |
---|
2210 | // ---------------------------------------------------------------- |
---|
2211 | // format |
---|
2212 | // 1 int %d |
---|
2213 | // 2 string <len> %s |
---|
2214 | // 3 number |
---|
2215 | // 4 bigint 4 %d or 3 <mpz_t> |
---|
2216 | // 5 ring |
---|
2217 | // 6 poly |
---|
2218 | // 7 ideal |
---|
2219 | // 8 matrix |
---|
2220 | // 9 vector |
---|
2221 | // 10 module |
---|
2222 | // 11 command |
---|
2223 | // 12 def <len> %s |
---|
2224 | // 13 proc <len> %s |
---|
2225 | // 14 list %d <elem1> .... |
---|
2226 | // 15 setring ....... |
---|
2227 | // 16 nothing |
---|
2228 | // 17 intvec <len> ... |
---|
2229 | // 18 intmat |
---|
2230 | // 19 bigintmat <r> <c> ... |
---|
2231 | // 20 blackbox <name> 1 <len> ... |
---|
2232 | // 21 attrib <bit-attrib> <len> <a-name1> <val1>... <data> |
---|
2233 | // 22 smatrix |
---|
2234 | // 23 0 <log(bitmask)> ring properties: max.exp. |
---|
2235 | // 23 1 <log(bitmask)> <r->IsLPRing> ring properties:LPRing |
---|
2236 | // 23 2 <matrix C> <matrix D> ring properties: PLuralRing |
---|
2237 | // |
---|
2238 | // 98: verify version: <ssi-version> <MAX_TOK> <OPT1> <OPT2> |
---|
2239 | // 99: quit Singular |
---|