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