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 case 0: if (s_iseof(d->f_read))
1662 {
1663 ssiClose(l);
1664 }
1665 res->rtyp=DEF_CMD;
1666 break;
1667 default: Werror("not implemented (t:%d)",t);
1669 res=NULL;
1670 break;
1671 }
1672 // if currRing is required for the result, but lost
1673 // define "ssiRing%d" as currRing:
1674 if ((d->r!=NULL)
1675 && (currRing!=d->r)
1676 && (res->RingDependend()))
1677 {
1678 if(ssiSetCurrRing(d->r)) { d->r=currRing; }
1679 }
1680 return res;
1681no_ring: WerrorS("no ring");
1683 return NULL;
1684}
1685//**************************************************************************/
1687{
1688 if(SI_LINK_W_OPEN_P(l)==0)
1690 ssiInfo *d = (ssiInfo *)l->data;
1691 if (d->r!=r)
1692 {
1693 if (send)
1694 {
1695 fputs("15 ",d->f_write);
1696 ssiWriteRing(d,r);
1697 }
1698 d->r=r;
1699 }
1700 if (currRing!=r) rChangeCurrRing(r);
1701 return FALSE;
1702}
1703//**************************************************************************/
1704
1706{
1707 if(SI_LINK_W_OPEN_P(l)==0)
1709 ssiInfo *d = (ssiInfo *)l->data;
1710 d->level++;
1711 //FILE *fich=d->f;
1712 while (data!=NULL)
1713 {
1714 int tt=data->Typ();
1715 void *dd=data->Data();
1716 attr *aa=data->Attribute();
1717 if ((aa!=NULL) && ((*aa)!=NULL)) // n user attributes
1718 {
1719 attr a=*aa;
1720 int n=0;
1721 while(a!=NULL) { n++; a=a->next;}
1722 fprintf(d->f_write,"21 %d %d ",data->flag,n);
1723 }
1724 else if (data->flag!=0) // only "flag" attributes
1725 {
1726 fprintf(d->f_write,"21 %d 0 ",data->flag);
1727 }
1728 if ((dd==NULL) && (data->name!=NULL) && (tt==0)) tt=DEF_CMD;
1729 // return pure undefined names as def
1730
1731 switch(tt /*data->Typ()*/)
1732 {
1733 case 0: /*error*/
1734 case NONE/* nothing*/:fputs("16 ",d->f_write);
1735 break;
1736 case STRING_CMD: fputs("2 ",d->f_write);
1737 ssiWriteString(d,(char *)dd);
1738 break;
1739 case INT_CMD: fputs("1 ",d->f_write);
1740 ssiWriteInt(d,(int)(long)dd);
1741 break;
1742 case BIGINT_CMD:fputs("4 ",d->f_write);
1744 break;
1745 case NUMBER_CMD:
1746 if (d->r!=currRing)
1747 {
1748 fputs("15 ",d->f_write);
1750 if (d->level<=1) fputc('\n',d->f_write);
1751 }
1752 fputs("3 ",d->f_write);
1754 break;
1755 case RING_CMD:fputs("5 ",d->f_write);
1756 ssiWriteRing(d,(ring)dd);
1757 break;
1758 case BUCKET_CMD:
1759 {
1761 if (d->r!=sBucketGetRing(b))
1762 {
1763 fputs("15 ",d->f_write);
1765 if (d->level<=1) fputc('\n',d->f_write);
1766 }
1767 fputs("6 ",d->f_write);
1769 break;
1770 }
1771 case POLY_CMD:
1772 case VECTOR_CMD:
1773 if (d->r!=currRing)
1774 {
1775 fputs("15 ",d->f_write);
1777 if (d->level<=1) fputc('\n',d->f_write);
1778 }
1779 if(tt==POLY_CMD) fputs("6 ",d->f_write);
1780 else fputs("9 ",d->f_write);
1781 ssiWritePoly(d,tt,(poly)dd);
1782 break;
1783 case IDEAL_CMD:
1784 case MODUL_CMD:
1785 case MATRIX_CMD:
1786 case SMATRIX_CMD:
1787 if (d->r!=currRing)
1788 {
1789 fputs("15 ",d->f_write);
1791 if (d->level<=1) fputc('\n',d->f_write);
1792 }
1793 if(tt==IDEAL_CMD) fputs("7 ",d->f_write);
1794 else if(tt==MATRIX_CMD) fputs("8 ",d->f_write);
1795 else if(tt==SMATRIX_CMD) fputs("22 ",d->f_write);
1796 else /* tt==MODUL_CMD*/
1797 {
1798 ideal M=(ideal)dd;
1799 fprintf(d->f_write,"10 %d ",(int)M->rank);
1800 }
1802 break;
1803 case COMMAND:
1804 fputs("11 ",d->f_write);
1806 break;
1807 case DEF_CMD: /* not evaluated stuff in quotes */
1808 fputs("12 ",d->f_write);
1809 ssiWriteString(d,data->Name());
1810 break;
1811 case PROC_CMD:
1812 fputs("13 ",d->f_write);
1814 break;
1815 case LIST_CMD:
1816 fputs("14 ",d->f_write);
1818 break;
1819 case INTVEC_CMD:
1820 fputs("17 ",d->f_write);
1821 ssiWriteIntvec(d,(intvec *)dd);
1822 break;
1823 case INTMAT_CMD:
1824 fputs("18 ",d->f_write);
1825 ssiWriteIntmat(d,(intvec *)dd);
1826 break;
1827 case BIGINTMAT_CMD:
1828 fputs("19 ",d->f_write);
1830 break;
1831 case BIGINTVEC_CMD:
1832 fputs("24 ",d->f_write);
1834 break;
1835 default:
1836 if (tt>MAX_TOK)
1837 {
1839 fputs("20 ",d->f_write);
1840 b->blackbox_serialize(b,dd,l);
1841 }
1842 else
1843 {
1844 Werror("not implemented (t:%d, rtyp:%d)",tt, data->rtyp);
1845 d->level=0;
1846 return TRUE;
1847 }
1848 break;
1849 }
1850 if (d->level<=1) { fputc('\n',d->f_write); fflush(d->f_write); }
1851 data=data->next;
1852 }
1853 d->level--;
1854 return FALSE;
1855}
1856
1859
1861{
1862 s->Open=ssiOpen;
1863 s->Close=ssiClose;
1864 s->Kill=ssiClose;
1865 s->Read=ssiRead1;
1866 s->Read2=(slRead2Proc)NULL;
1867 s->Write=ssiWrite;
1868 s->Dump=ssiDump;
1869 s->GetDump=ssiGetDump;
1870
1871 s->Status=slStatusSsi;
1872 s->SetRing=ssiSetRing;
1873 s->type="ssi";
1874 return s;
1875}
1876
1877const char* slStatusSsi(si_link l, const char* request)
1878{
1879 ssiInfo *d=(ssiInfo*)l->data;
1880 if (d==NULL) return "not open";
1881 if (((strcmp(l->mode,"fork")==0)
1882 ||(strcmp(l->mode,"tcp")==0)
1883 ||(strcmp(l->mode,"connect")==0))
1884 && (strcmp(request, "read") == 0))
1885 {
1886 fd_set mask;
1887 struct timeval wt;
1888 if (s_isready(d->f_read)) return "ready";
1889 if (FD_SETSIZE<=d->fd_read)
1890 {
1891 Werror("file descriptor number too high (%d)",d->fd_read);
1892 return "error";
1893 }
1894
1895 loop
1896 {
1897 /* Don't block. Return socket status immediately. */
1898 wt.tv_sec = 0;
1899 wt.tv_usec = 0;
1900
1901 FD_ZERO(&mask);
1902 FD_SET(d->fd_read, &mask);
1903 //Print("test fd %d\n",d->fd_read);
1904 /* check with select: chars waiting: no -> not ready */
1905 switch (si_select(d->fd_read+1, &mask, NULL, NULL, &wt))
1906 {
1907 case 0: /* not ready */ return "not ready";
1908 case -1: /*error*/ return "error";
1909 case 1: /*ready ? */ break;
1910 }
1911 /* yes: read 1 char*/
1912 /* if \n, check again with select else ungetc(c), ready*/
1913 int c=s_getc(d->f_read);
1914 //Print("try c=%d\n",c);
1915 if (c== -1) return "eof"; /* eof or error */
1916 else if (isdigit(c))
1917 { s_ungetc(c,d->f_read); return "ready"; }
1918 else if (c>' ')
1919 {
1920 Werror("unknown char in ssiLink(%d)",c);
1921 return "error";
1922 }
1923 /* else: next char */
1924 }
1925 }
1926 else if (strcmp(request, "read") == 0)
1927 {
1928 if (SI_LINK_R_OPEN_P(l) && (!s_iseof(d->f_read)) && (s_isready(d->f_read))) return "ready";
1929 else return "not ready";
1930 }
1931 else if (strcmp(request, "write") == 0)
1932 {
1933 if (SI_LINK_W_OPEN_P(l)) return "ready";
1934 else return "not ready";
1935 }
1936 else return "unknown status request";
1937}
1938
1940{
1941// input: L: a list with links of type
1942// ssi-connect, ssi-fork, ssi-tcp, MPtcp-fork or MPtcp-launch.
1943// Note: Not every entry in L must be set.
1944// timeout: timeout for select in micro-seconds
1945// or -1 for infinity
1946// or 0 for polling
1947// returns: ERROR (via Werror): L has wrong elements or link not open
1948// -2: select returns an error
1949// -1: the read state of all links is eof
1950// 0: timeout (or polling): none ready,
1951// i>0: (at least) L[i] is ready
1952 si_link l;
1953 ssiInfo *d=NULL;
1954 int d_fd;
1956 FD_ZERO(&fdmask);
1957 FD_ZERO(&mask);
1958 int max_fd=0; /* 1 + max fd in fd_set */
1959
1960 /* timeout */
1961 struct timeval wt;
1962 struct timeval *wt_ptr=&wt;
1963 int startingtime = getRTimer()/TIMER_RESOLUTION; // in seconds
1964 if (timeout== -1)
1965 {
1966 wt_ptr=NULL;
1967 }
1968 else
1969 {
1970 wt.tv_sec = timeout / 1000000;
1971 wt.tv_usec = timeout % 1000000;
1972 }
1973
1974 /* auxiliary variables */
1975 int i;
1976 int j;
1977 int k;
1978 int s;
1979 char fdmaskempty;
1980
1981 /* check the links and fill in fdmask */
1982 /* check ssi links for ungetc_buf */
1983 for(i=L->nr; i>=0; i--)
1984 {
1985 if (L->m[i].Typ()!=DEF_CMD)
1986 {
1987 if (L->m[i].Typ()!=LINK_CMD)
1988 { WerrorS("all elements must be of type link"); return -2;}
1989 l=(si_link)L->m[i].Data();
1990 if(SI_LINK_OPEN_P(l)==0)
1991 { WerrorS("all links must be open"); return -2;}
1992 if (((strcmp(l->m->type,"ssi")!=0) && (strcmp(l->m->type,"MPtcp")!=0))
1993 || ((strcmp(l->mode,"fork")!=0) && (strcmp(l->mode,"tcp")!=0)
1994 && (strcmp(l->mode,"launch")!=0) && (strcmp(l->mode,"connect")!=0)))
1995 {
1996 WerrorS("all links must be of type ssi:fork, ssi:tcp, ssi:connect");
1997 return -2;
1998 }
1999 if (strcmp(l->m->type,"ssi")==0)
2000 {
2001 d=(ssiInfo*)l->data;
2002 d_fd=d->fd_read;
2003 if (!s_isready(d->f_read))
2004 {
2005 FD_SET(d_fd, &fdmask);
2006 if (d_fd > max_fd) max_fd=d_fd;
2007 }
2008 else
2009 return i+1;
2010 }
2011 else
2012 {
2013 Werror("wrong link type >>%s<<",l->m->type);
2014 return -2;
2015 }
2016 }
2017 }
2018 max_fd++;
2019 if (FD_SETSIZE<=max_fd)
2020 {
2021 Werror("file descriptor number too high (%d)",max_fd);
2022 return -2;
2023 }
2024
2025do_select:
2026 /* copy fdmask to mask */
2027 FD_ZERO(&mask);
2028 for(k = 0; k < max_fd; k++)
2029 {
2030 if(FD_ISSET(k, &fdmask))
2031 {
2032 FD_SET(k, &mask);
2033 }
2034 }
2035
2036 /* check with select: chars waiting: no -> not ready */
2038 if (s==-1)
2039 {
2040 WerrorS("error in select call");
2041 return -2; /*error*/
2042 }
2043 if (s==0)
2044 {
2045 return 0; /*poll: not ready */
2046 }
2047 else /* s>0, at least one ready (the number of fd which are ready is s)*/
2048 {
2049 j=0;
2050 while (j<=max_fd) { if (FD_ISSET(j,&mask)) break; j++; }
2051 for(i=L->nr; i>=0; i--)
2052 {
2053 if (L->m[i].rtyp==LINK_CMD)
2054 {
2055 l=(si_link)L->m[i].Data();
2056 if (strcmp(l->m->type,"ssi")==0)
2057 {
2058 d=(ssiInfo*)l->data;
2059 d_fd=d->fd_read;
2060 if(j==d_fd) break;
2061 }
2062 else
2063 {
2064 Werror("wrong link type >>%s<<",l->m->type);
2065 return -2;
2066 }
2067 }
2068 }
2069 // only ssi links:
2070 loop
2071 {
2072 /* yes: read 1 char*/
2073 /* if \n, check again with select else ungetc(c), ready*/
2074 /* setting: d: current ssiInfo, j current fd, i current entry in L*/
2075 int c=s_getc(d->f_read);
2076 //Print("try c=%d\n",c);
2077 if (c== -1) /* eof */
2078 {
2079 FD_CLR(j,&fdmask);
2080 fdmaskempty = 1;
2081 for(k = 0; k < max_fd; k++)
2082 {
2083 if(FD_ISSET(k, &fdmask))
2084 {
2085 fdmaskempty = 0;
2086 break;
2087 }
2088 }
2089 if(fdmaskempty)
2090 {
2091 return -1;
2092 }
2093 if(timeout != -1)
2094 {
2095 timeout = si_max(0,
2097 wt.tv_sec = timeout / 1000000;
2098 wt.tv_usec = (timeout % 1000000);
2099 }
2100 goto do_select;
2101 }
2102
2103 else if (isdigit(c))
2104 { s_ungetc(c,d->f_read); return i+1; }
2105 else if (c>' ')
2106 {
2107 Werror("unknown char in ssiLink(%d)",c);
2108 return -2;
2109 }
2110 /* else: next char */
2111 goto do_select;
2112 }
2113 }
2114}
2115
2116int ssiBatch(const char *host, const char * port)
2117/* return 0 on success, >0 else*/
2118{
2120 char *buf=(char*)omAlloc(256);
2121 snprintf(buf,256,"ssi:connect %s:%s",host,port);
2122 slInit(l, buf);
2123 omFreeSize(buf,256);
2124 if (slOpen(l,SI_LINK_OPEN,NULL)) return 1;
2126
2127 idhdl id = enterid("link_ll", 0, LINK_CMD, &IDROOT, FALSE);
2128 IDLINK(id) = l;
2129
2130 loop
2131 {
2132 leftv h=ssiRead1(l); /*contains an exit.... */
2133 if (feErrors != NULL && *feErrors != '\0')
2134 {
2135 // handle errors:
2136 PrintS(feErrors); /* currently quite simple */
2137 *feErrors = '\0';
2138 }
2139 ssiWrite(l,h);
2140 h->CleanUp();
2142 }
2143 /* never reached*/
2144 exit(0);
2145}
2146
2152{
2153 if (ssiReserved_P!=0)
2154 {
2155 WerrorS("ERROR already a reserved port requested");
2156 return 0;
2157 }
2158 int portno;
2160 if(ssiReserved_sockfd < 0)
2161 {
2162 WerrorS("ERROR opening socket");
2163 return 0;
2164 }
2165 memset((char *) &ssiResverd_serv_addr,0, sizeof(ssiResverd_serv_addr));
2166 portno = 1025;
2167 ssiResverd_serv_addr.sin_family = AF_INET;
2168 ssiResverd_serv_addr.sin_addr.s_addr = INADDR_ANY;
2169 do
2170 {
2171 portno++;
2172 ssiResverd_serv_addr.sin_port = htons(portno);
2173 if(portno > 50000)
2174 {
2175 WerrorS("ERROR on binding (no free port available?)");
2176 return 0;
2177 }
2178 }
2183 return portno;
2184}
2185
2188{
2189 if (ssiReserved_P==0)
2190 {
2191 WerrorS("ERROR no reserved port requested");
2192 return NULL;
2193 }
2194 struct sockaddr_in cli_addr;
2195 int clilen = sizeof(cli_addr);
2197 if(newsockfd < 0)
2198 {
2199 Werror("ERROR on accept (errno=%d)",errno);
2200 return NULL;
2201 }
2204 si_link_extension prev = s;
2205 while (strcmp(s->type, "ssi") != 0)
2206 {
2207 if (s->next == NULL)
2208 {
2209 prev = s;
2210 s = NULL;
2211 break;
2212 }
2213 else
2214 {
2215 s = s->next;
2216 }
2217 }
2218 if (s != NULL)
2219 l->m = s;
2220 else
2221 {
2223 prev->next=slInitSsiExtension(ns);
2224 l->m = prev->next;
2225 }
2226 l->name=omStrDup("");
2227 l->mode=omStrDup("tcp");
2228 l->ref=1;
2229 ssiInfo *d=(ssiInfo*)omAlloc0(sizeof(ssiInfo));
2230 l->data=d;
2231 d->fd_read = newsockfd;
2232 d->fd_write = newsockfd;
2233 d->f_read = s_open(newsockfd);
2234 d->f_write = fdopen(newsockfd, "w");
2237 if (ssiReserved_Clients<=0)
2238 {
2239 ssiReserved_P=0;
2241 }
2242 return l;
2243}
2244/*---------------------------------------------------------------------*/
2245/**
2246 * @brief additional default signal handler
2247
2248 // some newer Linux version cannot have SIG_IGN for SIGCHLD,
2249 // so use this nice routine here:
2250 // SuSe 9.x reports -1 always
2251 // Redhat 9.x/FC x reports sometimes -1
2252 // see also: hpux_system
2253 // also needed by getrusage (timer etc.)
2254
2255 @param[in] sig
2256**/
2257/*---------------------------------------------------------------------*/
2259{
2260 pid_t kidpid;
2261 int status;
2262
2263 loop
2264 {
2266 if (kidpid==-1)
2267 {
2268 /* continue on interruption (EINTR): */
2269 if (errno == EINTR) continue;
2270 /* break on anything else (EINVAL or ECHILD according to manpage): */
2271 break;
2272 }
2273 else if (kidpid==0) break; /* no more children to process, so break */
2274
2275 //printf("Child %ld terminated\n", kidpid);
2277 while((hh!=NULL)&&(ssiToBeClosed_inactive))
2278 {
2279 if((hh->l!=NULL) && (hh->l->m->Open==ssiOpen))
2280 {
2281 ssiInfo *d = (ssiInfo *)hh->l->data;
2282 if(d->pid==kidpid)
2283 {
2285 {
2287 slClose(hh->l);
2289 break;
2290 }
2291 else break;
2292 }
2293 else hh=(link_list)hh->next;
2294 }
2295 else hh=(link_list)hh->next;
2296 }
2297 }
2298}
2299
2301{
2302 int type_id = IDTYP(h);
2303
2304 // C-proc not to be dumped, also LIB-proc not
2305 if (type_id == PROC_CMD)
2306 {
2307 if (IDPROC(h)->language == LANG_C) return FALSE;
2308 if (IDPROC(h)->libname != NULL) return FALSE;
2309 }
2310 // do not dump links
2311 if (type_id == LINK_CMD) return FALSE;
2312
2313 // do not dump ssi internal rings: ssiRing*
2314 if ((type_id == RING_CMD) && (strncmp(IDID(h),"ssiRing",7)==0))
2315 return FALSE;
2316
2317 // do not dump default cring:
2318 if (type_id == CRING_CMD)
2319 {
2320 if (strcmp(IDID(h),"ZZ")==0) return FALSE;
2321 if (strcmp(IDID(h),"QQ")==0) return FALSE;
2322 #ifdef SINGULAR_4_2
2323 if (strcmp(IDID(h),"AE")==0) return FALSE;
2324 if (strcmp(IDID(h),"QAE")==0) return FALSE;
2325 #endif
2326 }
2327
2328 command D=(command)omAlloc0(sizeof(*D));
2329 sleftv tmp;
2330 memset(&tmp,0,sizeof(tmp));
2331 tmp.rtyp=COMMAND;
2332 tmp.data=D;
2333
2334 if (type_id == PACKAGE_CMD)
2335 {
2336 // do not dump Top, Standard
2337 if ((strcmp(IDID(h), "Top") == 0)
2338 || (strcmp(IDID(h), "Standard") == 0))
2339 {
2340 omFreeSize(D,sizeof(*D));
2341 return FALSE;
2342 }
2343 package p=(package)IDDATA(h);
2344 // dump Singular-packages as LIB("...");
2345 if (p->language==LANG_SINGULAR)
2346 {
2347 D->op=LOAD_CMD;
2348 D->argc=2;
2349 D->arg1.rtyp=STRING_CMD;
2350 D->arg1.data=p->libname;
2351 D->arg2.rtyp=STRING_CMD;
2352 D->arg2.data=(char*)"with";
2353 ssiWrite(l,&tmp);
2354 omFreeSize(D,sizeof(*D));
2355 return FALSE;
2356 }
2357 // dump Singular-packages as load("...");
2358 else if (p->language==LANG_C)
2359 {
2360 D->op=LOAD_CMD;
2361 D->argc=1;
2362 D->arg1.rtyp=STRING_CMD;
2363 D->arg1.data=p->libname;
2364 ssiWrite(l,&tmp);
2365 omFreeSize(D,sizeof(*D));
2366 return FALSE;
2367 }
2368 }
2369
2370 // put type and name
2371 //Print("generic dump:%s,%s\n",IDID(h),Tok2Cmdname(IDTYP(h)));
2372 D->op='=';
2373 D->argc=2;
2374 D->arg1.rtyp=DEF_CMD;
2375 D->arg1.name=IDID(h);
2376 D->arg2.rtyp=IDTYP(h);
2377 D->arg2.data=IDDATA(h);
2378 ssiWrite(l,&tmp);
2379 omFreeSize(D,sizeof(*D));
2380 return FALSE;
2381}
2383{
2384 if (h == NULL) return FALSE;
2385
2386 if (ssiDumpIter(l, IDNEXT(h))) return TRUE;
2387
2388 // need to set the ring before writing it, otherwise we get in
2389 // trouble with minpoly
2390 if (IDTYP(h) == RING_CMD)
2391 rSetHdl(h);
2392
2393 if (DumpSsiIdhdl(l, h)) return TRUE;
2394
2395 // do not dump ssi internal rings: ssiRing*
2396 // but dump objects of all other rings
2397 if ((IDTYP(h) == RING_CMD)
2398 && (strncmp(IDID(h),"ssiRing",7)!=0))
2399 return ssiDumpIter(l, IDRING(h)->idroot);
2400 else
2401 return FALSE;
2402}
2404{
2407
2408 //if (! status ) status = DumpAsciiMaps(fd, h, NULL);
2409
2410 if (currRingHdl != rh) rSetHdl(rh);
2411 //fprintf(fd, "option(set, intvec(%d, %d));\n", si_opt_1, si_opt_2);
2412
2413 return status;
2414}
2416{
2417 ssiInfo *d=(ssiInfo*)l->data;
2418 loop
2419 {
2420 if (!SI_LINK_OPEN_P(l)) break;
2421 if (s_iseof(d->f_read)) break;
2422 leftv h=ssiRead1(l); /*contains an exit.... */
2423 if (feErrors != NULL && *feErrors != '\0')
2424 {
2425 // handle errors:
2426 PrintS(feErrors); /* currently quite simple */
2427 return TRUE;
2428 *feErrors = '\0';
2429 }
2430 h->CleanUp();
2432 }
2433 return FALSE;
2434}
2435// ----------------------------------------------------------------
2436// format
2437// 1 int %d
2438// 2 string <len> %s
2439// 3 number
2440// 4 bigint 4 %d or 3 <mpz_t>
2441// 5 ring
2442// 6 poly
2443// 7 ideal
2444// 8 matrix
2445// 9 vector
2446// 10 module
2447// 11 command
2448// 12 def <len> %s
2449// 13 proc <len> %s
2450// 14 list %d <elem1> ....
2451// 15 setring .......
2452// 16 nothing
2453// 17 intvec <len> ...
2454// 18 intmat
2455// 19 bigintmat <r> <c> ...
2456// 20 blackbox <name> 1 <len> ...
2457// 21 attrib <bit-attrib> <len> <a-name1> <val1>... <data>
2458// 22 smatrix
2459// 23 0 <log(bitmask)> ring properties: max.exp.
2460// 23 1 <log(bitmask)> <r->IsLPRing> ring properties:LPRing
2461// 23 2 <matrix C> <matrix D> ring properties: PLuralRing
2462// 24 bigintvec <c>
2463//
2464// 98: verify version: <ssi-version> <MAX_TOK> <OPT1> <OPT2>
2465// 99: quit Singular
static int si_max(const int a, const int b)
Definition auxiliary.h:124
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:1049
const char * name
Definition subexpr.h:87
int rtyp
Definition subexpr.h:91
void * Data()
Definition subexpr.cc:1193
leftv next
Definition subexpr.h:86
const char * Name()
Definition subexpr.h:120
void * data
Definition subexpr.h:88
attr * Attribute()
Definition subexpr.cc:1506
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:978
static FORCE_INLINE void n_WriteFd(number a, const ssiInfo *f, const coeffs r)
io via ssi:
Definition coeffs.h:974
@ 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:419
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:967
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:542
#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:131
#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:6189
idhdl rFindHdl(ring r, idhdl n)
Definition ipshell.cc:1702
void rSetHdl(idhdl h)
Definition ipshell.cc:5135
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:152
coeffs nFindCoeffByName(char *cf_name)
find an existing coeff by its "CoeffName"
Definition numbers.cc:651
#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:51
int status int void * buf
Definition si_signals.h:59
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