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