[0e1846] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
[32df82] | 4 | |
---|
[0e1846] | 5 | /* |
---|
[6ae4f5] | 6 | * ABSTRACT: general interface to links |
---|
[d754b7] | 7 | */ |
---|
[0e1846] | 8 | |
---|
| 9 | #include <stdio.h> |
---|
| 10 | #include <string.h> |
---|
[644227] | 11 | #include <sys/types.h> |
---|
| 12 | #include <sys/stat.h> |
---|
| 13 | #include <unistd.h> |
---|
| 14 | |
---|
[762407] | 15 | #include "config.h" |
---|
[b1dfaf] | 16 | #include <kernel/mod2.h> |
---|
[599326] | 17 | #include <Singular/tok.h> |
---|
[0fb34ba] | 18 | #include <misc/options.h> |
---|
[b1dfaf] | 19 | #include <omalloc/omalloc.h> |
---|
[599326] | 20 | #include <kernel/febase.h> |
---|
| 21 | #include <Singular/subexpr.h> |
---|
| 22 | #include <Singular/ipid.h> |
---|
| 23 | #include <Singular/silink.h> |
---|
| 24 | #include <Singular/ipshell.h> |
---|
[0fb34ba] | 25 | #include <polys/matpol.h> |
---|
| 26 | #include <polys/monomials/ring.h> |
---|
[599326] | 27 | #include <Singular/lists.h> |
---|
| 28 | #include <kernel/ideals.h> |
---|
[0fb34ba] | 29 | #include <coeffs/numbers.h> |
---|
| 30 | #include <misc/intvec.h> |
---|
[47367fd] | 31 | #include <Singular/links/ssiLink.h> |
---|
| 32 | #include <Singular/links/pipeLink.h> |
---|
[286bd57] | 33 | |
---|
[0c7cb8] | 34 | // #ifdef HAVE_DBM |
---|
| 35 | // #ifdef ix86_Win |
---|
| 36 | // #define USE_GDBM |
---|
| 37 | // #endif |
---|
| 38 | // #endif |
---|
[50cbdc] | 39 | |
---|
[c232af] | 40 | omBin s_si_link_extension_bin = omGetSpecBin(sizeof(s_si_link_extension)); |
---|
| 41 | omBin sip_link_bin = omGetSpecBin(sizeof(sip_link)); |
---|
| 42 | omBin ip_link_bin = omGetSpecBin(sizeof(ip_link)); |
---|
| 43 | |
---|
[286bd57] | 44 | /* declarations */ |
---|
| 45 | static BOOLEAN DumpAscii(FILE *fd, idhdl h); |
---|
| 46 | static BOOLEAN DumpAsciiIdhdl(FILE *fd, idhdl h); |
---|
[85e68dd] | 47 | static const char* GetIdString(idhdl h); |
---|
[286bd57] | 48 | static int DumpRhs(FILE *fd, idhdl h); |
---|
[85e68dd] | 49 | static BOOLEAN DumpQring(FILE *fd, idhdl h, const char *type_str); |
---|
[d754b7] | 50 | static BOOLEAN DumpAsciiMaps(FILE *fd, idhdl h, idhdl rhdl); |
---|
[6b32990] | 51 | static si_link_extension slTypeInit(si_link_extension s, const char* type); |
---|
[0e1846] | 52 | |
---|
| 53 | /* ====================================================================== */ |
---|
| 54 | si_link_extension si_link_root=NULL; |
---|
[d754b7] | 55 | |
---|
[0e1846] | 56 | BOOLEAN slInit(si_link l, char *istr) |
---|
| 57 | { |
---|
[d754b7] | 58 | char *type = NULL, *mode = NULL, *name = NULL; |
---|
| 59 | int i = 0, j; |
---|
[c20843] | 60 | |
---|
[d754b7] | 61 | // set mode and type |
---|
| 62 | if (istr != NULL) |
---|
[0e1846] | 63 | { |
---|
[d754b7] | 64 | // find the first colon char in istr |
---|
| 65 | i = 0; |
---|
| 66 | while (istr[i] != ':' && istr[i] != '\0') i++; |
---|
| 67 | if (istr[i] == ':') |
---|
[0e1846] | 68 | { |
---|
[d754b7] | 69 | // if found, set type |
---|
| 70 | if (i > 0) |
---|
[0e1846] | 71 | { |
---|
[d754b7] | 72 | istr[i] = '\0'; |
---|
[c232af] | 73 | type = omStrDup(istr); |
---|
[d754b7] | 74 | istr[i] = ':'; |
---|
[0e1846] | 75 | } |
---|
[d754b7] | 76 | // and check for mode |
---|
| 77 | j = ++i; |
---|
| 78 | while (istr[j] != ' ' && istr[j] != '\0') j++; |
---|
| 79 | if (j > i) |
---|
[0e1846] | 80 | { |
---|
[c232af] | 81 | mode = omStrDup(&(istr[i])); |
---|
[d754b7] | 82 | mode[j - i] = '\0'; |
---|
[0e1846] | 83 | } |
---|
[d754b7] | 84 | // and for the name |
---|
| 85 | while (istr[j] == ' '&& istr[j] != '\0') j++; |
---|
[c232af] | 86 | if (istr[j] != '\0') name = omStrDup(&(istr[j])); |
---|
[d754b7] | 87 | } |
---|
| 88 | else // no colon find -- string is entire name |
---|
| 89 | { |
---|
| 90 | j=0; |
---|
| 91 | while (istr[j] == ' '&& istr[j] != '\0') j++; |
---|
[c232af] | 92 | if (istr[j] != '\0') name = omStrDup(&(istr[j])); |
---|
[0e1846] | 93 | } |
---|
| 94 | } |
---|
[e6969d] | 95 | |
---|
[d754b7] | 96 | // set the link extension |
---|
| 97 | if (type != NULL) |
---|
[0e1846] | 98 | { |
---|
[d754b7] | 99 | si_link_extension s = si_link_root; |
---|
[6b32990] | 100 | si_link_extension prev = s; |
---|
[c20843] | 101 | |
---|
[6b32990] | 102 | while (strcmp(s->type, type) != 0) |
---|
| 103 | { |
---|
[50cbdc] | 104 | if (s->next == NULL) |
---|
[6b32990] | 105 | { |
---|
| 106 | prev = s; |
---|
| 107 | s = NULL; |
---|
| 108 | break; |
---|
| 109 | } |
---|
| 110 | else |
---|
| 111 | { |
---|
| 112 | s = s->next; |
---|
| 113 | } |
---|
| 114 | } |
---|
[d754b7] | 115 | |
---|
| 116 | if (s != NULL) |
---|
| 117 | l->m = s; |
---|
| 118 | else |
---|
[0e1846] | 119 | { |
---|
[6b32990] | 120 | l->m = slTypeInit(prev, type); |
---|
[0e1846] | 121 | } |
---|
[c232af] | 122 | omFree(type); |
---|
[0e1846] | 123 | } |
---|
[d754b7] | 124 | else |
---|
| 125 | l->m = si_link_root; |
---|
| 126 | |
---|
[6b32990] | 127 | if (l->m == NULL) return TRUE; |
---|
[50cbdc] | 128 | |
---|
[c232af] | 129 | l->name = (name != NULL ? name : omStrDup("")); |
---|
| 130 | l->mode = (mode != NULL ? mode : omStrDup("")); |
---|
[d754b7] | 131 | l->ref = 1; |
---|
| 132 | return FALSE; |
---|
[0e1846] | 133 | } |
---|
| 134 | |
---|
[e6969d] | 135 | void slCleanUp(si_link l) |
---|
[0e1846] | 136 | { |
---|
[e6969d] | 137 | (l->ref)--; |
---|
[d754b7] | 138 | if (l->ref == 0) |
---|
[0e1846] | 139 | { |
---|
[80419f] | 140 | if (SI_LINK_OPEN_P(l)) |
---|
| 141 | { |
---|
| 142 | if (l->m->Kill != NULL) l->m->Kill(l); |
---|
| 143 | else if (l->m->Close != NULL) l->m->Close(l); |
---|
| 144 | } |
---|
[c232af] | 145 | omFree((ADDRESS)l->name); |
---|
| 146 | omFree((ADDRESS)l->mode); |
---|
[d754b7] | 147 | memset((void *) l, 0, sizeof(ip_link)); |
---|
[e6969d] | 148 | } |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | void slKill(si_link l) |
---|
| 152 | { |
---|
[d754b7] | 153 | slCleanUp(l); |
---|
[e6969d] | 154 | if (l->ref == 0) |
---|
[c232af] | 155 | omFreeBin((ADDRESS)l, ip_link_bin); |
---|
[0e1846] | 156 | } |
---|
| 157 | |
---|
[9db0567] | 158 | const char* slStatus(si_link l, const char *request) |
---|
[0e1846] | 159 | { |
---|
[d754b7] | 160 | if (l == NULL) return "empty link"; |
---|
| 161 | else if (l->m == NULL) return "unknown link type"; |
---|
| 162 | else if (strcmp(request, "type") == 0) return l->m->type; |
---|
| 163 | else if (strcmp(request, "mode") == 0) return l->mode; |
---|
| 164 | else if (strcmp(request, "name") == 0) return l->name; |
---|
[644227] | 165 | else if (strcmp(request, "exists") ==0) |
---|
| 166 | { |
---|
| 167 | struct stat buf; |
---|
| 168 | if (lstat(l->name,&buf)==0) return "yes"; |
---|
| 169 | else return "no"; |
---|
| 170 | } |
---|
[d754b7] | 171 | else if (strcmp(request, "open") == 0) |
---|
| 172 | { |
---|
| 173 | if (SI_LINK_OPEN_P(l)) return "yes"; |
---|
| 174 | else return "no"; |
---|
| 175 | } |
---|
| 176 | else if (strcmp(request, "openread") == 0) |
---|
| 177 | { |
---|
| 178 | if (SI_LINK_R_OPEN_P(l)) return "yes"; |
---|
| 179 | else return "no"; |
---|
| 180 | } |
---|
| 181 | else if (strcmp(request, "openwrite") == 0) |
---|
| 182 | { |
---|
| 183 | if (SI_LINK_W_OPEN_P(l)) return "yes"; |
---|
| 184 | else return "no"; |
---|
| 185 | } |
---|
| 186 | else if (l->m->Status == NULL) return "unknown status request"; |
---|
| 187 | else return l->m->Status(l, request); |
---|
[0e1846] | 188 | } |
---|
| 189 | |
---|
[d754b7] | 190 | //-------------------------------------------------------------------------- |
---|
[b5f276e] | 191 | BOOLEAN slOpen(si_link l, short flag, leftv h) |
---|
[0e1846] | 192 | { |
---|
[47a616d] | 193 | BOOLEAN res = TRUE; |
---|
| 194 | if (l!=NULL) |
---|
| 195 | { |
---|
[c20843] | 196 | |
---|
[47a616d] | 197 | if (l->m == NULL) slInit(l, ((char*)"")); |
---|
[d754b7] | 198 | |
---|
[47a616d] | 199 | const char *c="_";; |
---|
| 200 | if (h!=NULL) c=h->Name(); |
---|
| 201 | |
---|
| 202 | if (SI_LINK_OPEN_P(l)) |
---|
| 203 | { |
---|
| 204 | Warn("open: link of type: %s, mode: %s, name: %s is already open", |
---|
[d754b7] | 205 | l->m->type, l->mode, l->name); |
---|
[47a616d] | 206 | return FALSE; |
---|
| 207 | } |
---|
| 208 | else if (l->m->Open != NULL) |
---|
| 209 | { |
---|
| 210 | res = l->m->Open(l, flag, h); |
---|
| 211 | if (res) |
---|
| 212 | Werror("open: Error for link %s of type: %s, mode: %s, name: %s", |
---|
| 213 | c, l->m->type, l->mode, l->name); |
---|
| 214 | } |
---|
[d754b7] | 215 | } |
---|
| 216 | return res; |
---|
[0e1846] | 217 | } |
---|
| 218 | |
---|
| 219 | BOOLEAN slClose(si_link l) |
---|
| 220 | { |
---|
[d754b7] | 221 | |
---|
[1fc83c0] | 222 | if(! SI_LINK_OPEN_P(l)) |
---|
| 223 | return FALSE; |
---|
[c20843] | 224 | |
---|
[47a616d] | 225 | BOOLEAN res = TRUE; |
---|
| 226 | if (l->m->Close != NULL) |
---|
| 227 | { |
---|
| 228 | res = l->m->Close(l); |
---|
| 229 | if (res) |
---|
| 230 | Werror("close: Error for link of type: %s, mode: %s, name: %s", |
---|
[d754b7] | 231 | l->m->type, l->mode, l->name); |
---|
[47a616d] | 232 | } |
---|
[d754b7] | 233 | return res; |
---|
[0e1846] | 234 | } |
---|
| 235 | |
---|
[d754b7] | 236 | leftv slRead(si_link l, leftv a) |
---|
[0e1846] | 237 | { |
---|
| 238 | leftv v = NULL; |
---|
| 239 | if( ! SI_LINK_R_OPEN_P(l)) // open r ? |
---|
| 240 | { |
---|
[fcafdb] | 241 | #ifdef HAVE_DBM |
---|
| 242 | #ifdef USE_GDBM |
---|
| 243 | if (! SI_LINK_CLOSE_P(l)) |
---|
| 244 | { |
---|
[50cbdc] | 245 | if (slClose(l)) return NULL; |
---|
[fcafdb] | 246 | } |
---|
| 247 | #endif |
---|
| 248 | #endif |
---|
[b5f276e] | 249 | if (slOpen(l, SI_LINK_READ,NULL)) return NULL; |
---|
[0e1846] | 250 | } |
---|
[d754b7] | 251 | |
---|
| 252 | if (SI_LINK_R_OPEN_P(l)) |
---|
[0e1846] | 253 | { // open r |
---|
| 254 | if (a==NULL) |
---|
| 255 | { |
---|
[d754b7] | 256 | if (l->m->Read != NULL) v = l->m->Read(l); |
---|
[0e1846] | 257 | } |
---|
| 258 | else |
---|
| 259 | { |
---|
[d754b7] | 260 | if (l->m->Read2 != NULL) v = l->m->Read2(l,a); |
---|
[0e1846] | 261 | } |
---|
| 262 | } |
---|
[d754b7] | 263 | else |
---|
[0e1846] | 264 | { |
---|
[d754b7] | 265 | Werror("read: Error to open link of type %s, mode: %s, name: %s for reading", |
---|
| 266 | l->m->type, l->mode, l->name); |
---|
| 267 | return NULL; |
---|
[0e1846] | 268 | } |
---|
[c20843] | 269 | |
---|
[d754b7] | 270 | // here comes the eval: |
---|
[1fc83c0] | 271 | if (v != NULL) |
---|
[42e60f] | 272 | { |
---|
| 273 | if (v->Eval() && !errorreported) |
---|
| 274 | WerrorS("eval: failed"); |
---|
| 275 | } |
---|
[d754b7] | 276 | else |
---|
| 277 | Werror("read: Error for link of type %s, mode: %s, name: %s", |
---|
| 278 | l->m->type, l->mode, l->name); |
---|
[0e1846] | 279 | return v; |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | BOOLEAN slWrite(si_link l, leftv v) |
---|
| 283 | { |
---|
[d754b7] | 284 | BOOLEAN res; |
---|
[c20843] | 285 | |
---|
[0e1846] | 286 | if(! SI_LINK_W_OPEN_P(l)) // open w ? |
---|
| 287 | { |
---|
[fcafdb] | 288 | #ifdef HAVE_DBM |
---|
| 289 | #ifdef USE_GDBM |
---|
| 290 | if (! SI_LINK_CLOSE_P(l)) |
---|
| 291 | { |
---|
[50cbdc] | 292 | if (slClose(l)) return TRUE; |
---|
[fcafdb] | 293 | } |
---|
| 294 | #endif |
---|
| 295 | #endif |
---|
[b5f276e] | 296 | if (slOpen(l, SI_LINK_WRITE,NULL)) return TRUE; |
---|
[0e1846] | 297 | } |
---|
[d754b7] | 298 | |
---|
[fcafdb] | 299 | if (SI_LINK_W_OPEN_P(l)) |
---|
[0e1846] | 300 | { // now open w |
---|
[1fc83c0] | 301 | if (l->m->Write != NULL) |
---|
| 302 | res = l->m->Write(l,v); |
---|
| 303 | else |
---|
| 304 | res = TRUE; |
---|
[d754b7] | 305 | |
---|
| 306 | if (res) |
---|
| 307 | Werror("write: Error for link of type %s, mode: %s, name: %s", |
---|
| 308 | l->m->type, l->mode, l->name); |
---|
| 309 | return res; |
---|
| 310 | } |
---|
| 311 | else |
---|
| 312 | { |
---|
| 313 | Werror("write: Error to open link of type %s, mode: %s, name: %s for writing", |
---|
| 314 | l->m->type, l->mode, l->name); |
---|
| 315 | return TRUE; |
---|
[0e1846] | 316 | } |
---|
| 317 | } |
---|
| 318 | |
---|
[286bd57] | 319 | BOOLEAN slDump(si_link l) |
---|
| 320 | { |
---|
[d754b7] | 321 | BOOLEAN res; |
---|
| 322 | |
---|
[286bd57] | 323 | if(! SI_LINK_W_OPEN_P(l)) // open w ? |
---|
| 324 | { |
---|
[b5f276e] | 325 | if (slOpen(l, SI_LINK_WRITE,NULL)) return TRUE; |
---|
[286bd57] | 326 | } |
---|
[d754b7] | 327 | |
---|
| 328 | if(SI_LINK_W_OPEN_P(l)) |
---|
[286bd57] | 329 | { // now open w |
---|
[1fc83c0] | 330 | if (l->m->Dump != NULL) |
---|
| 331 | res = l->m->Dump(l); |
---|
| 332 | else |
---|
| 333 | res = TRUE; |
---|
[286bd57] | 334 | |
---|
[d754b7] | 335 | if (res) |
---|
| 336 | Werror("dump: Error for link of type %s, mode: %s, name: %s", |
---|
| 337 | l->m->type, l->mode, l->name); |
---|
[09afeb] | 338 | |
---|
[228e0b] | 339 | if (!SI_LINK_R_OPEN_P(l)) slClose(l); // do not close r/w links |
---|
[d754b7] | 340 | return res; |
---|
[286bd57] | 341 | } |
---|
[d754b7] | 342 | else |
---|
| 343 | { |
---|
| 344 | Werror("dump: Error to open link of type %s, mode: %s, name: %s for writing", |
---|
| 345 | l->m->type, l->mode, l->name); |
---|
| 346 | return TRUE; |
---|
[286bd57] | 347 | } |
---|
| 348 | } |
---|
| 349 | |
---|
[d754b7] | 350 | BOOLEAN slGetDump(si_link l) |
---|
[0e1846] | 351 | { |
---|
[d754b7] | 352 | BOOLEAN res; |
---|
| 353 | |
---|
| 354 | if(! SI_LINK_R_OPEN_P(l)) // open r ? |
---|
[0e1846] | 355 | { |
---|
[b5f276e] | 356 | if (slOpen(l, SI_LINK_READ,NULL)) return TRUE; |
---|
[0e1846] | 357 | } |
---|
[d754b7] | 358 | |
---|
| 359 | if(SI_LINK_R_OPEN_P(l)) |
---|
| 360 | { // now open r |
---|
[1fc83c0] | 361 | if (l->m->GetDump != NULL) |
---|
| 362 | res = l->m->GetDump(l); |
---|
| 363 | else |
---|
| 364 | res = TRUE; |
---|
[d754b7] | 365 | |
---|
| 366 | if (res) |
---|
| 367 | Werror("getdump: Error for link of type %s, mode: %s, name: %s", |
---|
| 368 | l->m->type, l->mode, l->name); |
---|
[6d1357] | 369 | //res|=slClose(l); |
---|
[d754b7] | 370 | return res; |
---|
[0e1846] | 371 | } |
---|
[d754b7] | 372 | else |
---|
[0e1846] | 373 | { |
---|
[d754b7] | 374 | Werror("dump: Error open link of type %s, mode: %s, name: %s for reading", |
---|
| 375 | l->m->type, l->mode, l->name); |
---|
| 376 | return TRUE; |
---|
[0e1846] | 377 | } |
---|
| 378 | } |
---|
[d754b7] | 379 | |
---|
| 380 | |
---|
| 381 | /* =============== ASCII ============================================= */ |
---|
[d30a399] | 382 | BOOLEAN slOpenAscii(si_link l, short flag, leftv) |
---|
[0e1846] | 383 | { |
---|
[85e68dd] | 384 | const char *mode; |
---|
[1fc83c0] | 385 | if (flag & SI_LINK_OPEN) |
---|
[0e1846] | 386 | { |
---|
[d754b7] | 387 | if (l->mode[0] != '\0' && (strcmp(l->mode, "r") == 0)) |
---|
| 388 | flag = SI_LINK_READ; |
---|
| 389 | else flag = SI_LINK_WRITE; |
---|
[0e1846] | 390 | } |
---|
[d754b7] | 391 | |
---|
| 392 | if (flag == SI_LINK_READ) mode = "r"; |
---|
| 393 | else if (strcmp(l->mode, "w") == 0) mode = "w"; |
---|
| 394 | else mode = "a"; |
---|
[c20843] | 395 | |
---|
| 396 | |
---|
[d754b7] | 397 | if (l->name[0] == '\0') |
---|
[0e1846] | 398 | { |
---|
[d754b7] | 399 | // stdin or stdout |
---|
| 400 | if (flag == SI_LINK_READ) |
---|
[0e1846] | 401 | { |
---|
[d754b7] | 402 | l->data = (void *) stdin; |
---|
| 403 | mode = "r"; |
---|
[0e1846] | 404 | } |
---|
[d754b7] | 405 | else |
---|
[0e1846] | 406 | { |
---|
[d754b7] | 407 | l->data = (void *) stdout; |
---|
| 408 | mode = "a"; |
---|
[0e1846] | 409 | } |
---|
[d754b7] | 410 | } |
---|
| 411 | else |
---|
| 412 | { |
---|
| 413 | // normal ascii link to a file |
---|
| 414 | FILE *outfile; |
---|
| 415 | char *filename=l->name; |
---|
| 416 | |
---|
| 417 | if(filename[0]=='>') |
---|
[0e1846] | 418 | { |
---|
[d754b7] | 419 | if (filename[1]=='>') |
---|
| 420 | { |
---|
| 421 | filename+=2; |
---|
| 422 | mode = "a"; |
---|
| 423 | } |
---|
| 424 | else |
---|
| 425 | { |
---|
| 426 | filename++; |
---|
| 427 | mode="w"; |
---|
| 428 | } |
---|
[0e1846] | 429 | } |
---|
[64c6d1] | 430 | outfile=myfopen(filename,mode); |
---|
[1fc83c0] | 431 | if (outfile!=NULL) |
---|
| 432 | l->data = (void *) outfile; |
---|
| 433 | else |
---|
| 434 | return TRUE; |
---|
[0e1846] | 435 | } |
---|
[d754b7] | 436 | |
---|
[c232af] | 437 | omFree(l->mode); |
---|
| 438 | l->mode = omStrDup(mode); |
---|
[d754b7] | 439 | SI_LINK_SET_OPEN_P(l, flag); |
---|
[0e1846] | 440 | return FALSE; |
---|
| 441 | } |
---|
[d754b7] | 442 | |
---|
[0e1846] | 443 | BOOLEAN slCloseAscii(si_link l) |
---|
| 444 | { |
---|
| 445 | SI_LINK_SET_CLOSE_P(l); |
---|
[d754b7] | 446 | if (l->name[0] != '\0') |
---|
[0e1846] | 447 | { |
---|
| 448 | return (fclose((FILE *)l->data)!=0); |
---|
| 449 | } |
---|
| 450 | return FALSE; |
---|
| 451 | } |
---|
[d754b7] | 452 | |
---|
[472f39] | 453 | leftv slReadAscii2(si_link l, leftv pr) |
---|
[0e1846] | 454 | { |
---|
| 455 | FILE * fp=(FILE *)l->data; |
---|
| 456 | char * buf=NULL; |
---|
[d754b7] | 457 | if (fp!=NULL && l->name[0] != '\0') |
---|
[0e1846] | 458 | { |
---|
| 459 | fseek(fp,0L,SEEK_END); |
---|
| 460 | long len=ftell(fp); |
---|
| 461 | fseek(fp,0L,SEEK_SET); |
---|
[c232af] | 462 | buf=(char *)omAlloc((int)len+1); |
---|
[1fc83c0] | 463 | if (BVERBOSE(V_READING)) |
---|
[d3a49c] | 464 | Print("//Reading %ld chars\n",len); |
---|
[d5f35ac] | 465 | myfread( buf, len, 1, fp); |
---|
[0e1846] | 466 | buf[len]='\0'; |
---|
| 467 | } |
---|
| 468 | else |
---|
| 469 | { |
---|
[da8702] | 470 | if (pr->Typ()==STRING_CMD) |
---|
[c20843] | 471 | { |
---|
[da8702] | 472 | buf=(char *)omAlloc(80); |
---|
| 473 | fe_fgets_stdin((char *)pr->Data(),buf,80); |
---|
[c20843] | 474 | } |
---|
| 475 | else |
---|
| 476 | { |
---|
[da8702] | 477 | WerrorS("read(<link>,<string>) expected"); |
---|
| 478 | buf=omStrDup(""); |
---|
[c20843] | 479 | } |
---|
[0e1846] | 480 | } |
---|
[c232af] | 481 | leftv v=(leftv)omAlloc0Bin(sleftv_bin); |
---|
[0e1846] | 482 | v->rtyp=STRING_CMD; |
---|
| 483 | v->data=buf; |
---|
| 484 | return v; |
---|
| 485 | } |
---|
[472f39] | 486 | |
---|
| 487 | leftv slReadAscii(si_link l) |
---|
| 488 | { |
---|
| 489 | sleftv tmp; |
---|
| 490 | memset(&tmp,0,sizeof(sleftv)); |
---|
| 491 | tmp.rtyp=STRING_CMD; |
---|
[a70441f] | 492 | tmp.data=(void*) "? "; |
---|
[472f39] | 493 | return slReadAscii2(l,&tmp); |
---|
| 494 | } |
---|
| 495 | |
---|
[0e1846] | 496 | BOOLEAN slWriteAscii(si_link l, leftv v) |
---|
| 497 | { |
---|
| 498 | FILE *outfile=(FILE *)l->data; |
---|
| 499 | BOOLEAN err=FALSE; |
---|
| 500 | char *s; |
---|
| 501 | while (v!=NULL) |
---|
| 502 | { |
---|
| 503 | s = v->String(); |
---|
| 504 | // free v ?? |
---|
| 505 | if (s!=NULL) |
---|
| 506 | { |
---|
| 507 | fprintf(outfile,"%s\n",s); |
---|
[c232af] | 508 | omFree((ADDRESS)s); |
---|
[0e1846] | 509 | } |
---|
| 510 | else |
---|
| 511 | { |
---|
| 512 | Werror("cannot convert to string"); |
---|
| 513 | err=TRUE; |
---|
| 514 | } |
---|
| 515 | v = v->next; |
---|
| 516 | } |
---|
| 517 | fflush(outfile); |
---|
| 518 | return err; |
---|
| 519 | } |
---|
| 520 | |
---|
[9db0567] | 521 | const char* slStatusAscii(si_link l, const char* request) |
---|
[0e1846] | 522 | { |
---|
[d754b7] | 523 | if (strcmp(request, "read") == 0) |
---|
[0e1846] | 524 | { |
---|
[d754b7] | 525 | if (SI_LINK_R_OPEN_P(l)) return "ready"; |
---|
| 526 | else return "not ready"; |
---|
| 527 | } |
---|
| 528 | else if (strcmp(request, "write") == 0) |
---|
| 529 | { |
---|
| 530 | if (SI_LINK_W_OPEN_P(l)) return "ready"; |
---|
| 531 | else return "not ready"; |
---|
[0e1846] | 532 | } |
---|
[d754b7] | 533 | else return "unknown status request"; |
---|
[0e1846] | 534 | } |
---|
| 535 | |
---|
[d754b7] | 536 | /*------------------ Dumping in Ascii format -----------------------*/ |
---|
| 537 | |
---|
[286bd57] | 538 | BOOLEAN slDumpAscii(si_link l) |
---|
| 539 | { |
---|
| 540 | FILE *fd = (FILE *) l->data; |
---|
[46d09b] | 541 | idhdl h = IDROOT, rh = currRingHdl; |
---|
[286bd57] | 542 | BOOLEAN status = DumpAscii(fd, h); |
---|
| 543 | |
---|
[d754b7] | 544 | if (! status ) status = DumpAsciiMaps(fd, h, NULL); |
---|
| 545 | |
---|
[cf42ab1] | 546 | if (currRingHdl != rh) rSetHdl(rh); |
---|
[d30a399] | 547 | fprintf(fd, "option(set, intvec(%d, %d));\n", si_opt_1, si_opt_2); |
---|
[97454d] | 548 | fprintf(fd, "RETURN();\n"); |
---|
[286bd57] | 549 | fflush(fd); |
---|
[c20843] | 550 | |
---|
[286bd57] | 551 | return status; |
---|
| 552 | } |
---|
| 553 | |
---|
[c20843] | 554 | // we do that recursively, to dump ids in the the order in which they |
---|
[286bd57] | 555 | // were actually defined |
---|
| 556 | static BOOLEAN DumpAscii(FILE *fd, idhdl h) |
---|
| 557 | { |
---|
| 558 | if (h == NULL) return FALSE; |
---|
| 559 | |
---|
| 560 | if (DumpAscii(fd, IDNEXT(h))) return TRUE; |
---|
| 561 | |
---|
[d754b7] | 562 | // need to set the ring before writing it, otherwise we get in |
---|
[286bd57] | 563 | // trouble with minpoly |
---|
| 564 | if (IDTYP(h) == RING_CMD || IDTYP(h) == QRING_CMD) |
---|
[cf42ab1] | 565 | rSetHdl(h); |
---|
[286bd57] | 566 | |
---|
| 567 | if (DumpAsciiIdhdl(fd, h)) return TRUE; |
---|
| 568 | |
---|
| 569 | if (IDTYP(h) == RING_CMD || IDTYP(h) == QRING_CMD) |
---|
| 570 | return DumpAscii(fd, IDRING(h)->idroot); |
---|
| 571 | else |
---|
| 572 | return FALSE; |
---|
| 573 | } |
---|
| 574 | |
---|
[d754b7] | 575 | static BOOLEAN DumpAsciiMaps(FILE *fd, idhdl h, idhdl rhdl) |
---|
| 576 | { |
---|
| 577 | if (h == NULL) return FALSE; |
---|
| 578 | if (DumpAsciiMaps(fd, IDNEXT(h), rhdl)) return TRUE; |
---|
| 579 | |
---|
| 580 | if (IDTYP(h) == RING_CMD || IDTYP(h) == QRING_CMD) |
---|
| 581 | return DumpAsciiMaps(fd, IDRING(h)->idroot, h); |
---|
| 582 | else if (IDTYP(h) == MAP_CMD) |
---|
| 583 | { |
---|
| 584 | char *rhs; |
---|
[cf42ab1] | 585 | rSetHdl(rhdl); |
---|
[472f39] | 586 | rhs = h->String(); |
---|
[c20843] | 587 | |
---|
[d754b7] | 588 | if (fprintf(fd, "setring %s;\n", IDID(rhdl)) == EOF) return TRUE; |
---|
| 589 | if (fprintf(fd, "%s %s = %s, %s;\n", Tok2Cmdname(MAP_CMD), IDID(h), |
---|
| 590 | IDMAP(h)->preimage, rhs) == EOF) |
---|
| 591 | { |
---|
[c232af] | 592 | omFree(rhs); |
---|
[d754b7] | 593 | return TRUE; |
---|
| 594 | } |
---|
| 595 | else |
---|
| 596 | { |
---|
[c232af] | 597 | omFree(rhs); |
---|
[d754b7] | 598 | return FALSE; |
---|
| 599 | } |
---|
| 600 | } |
---|
| 601 | else return FALSE; |
---|
| 602 | } |
---|
[c20843] | 603 | |
---|
[286bd57] | 604 | static BOOLEAN DumpAsciiIdhdl(FILE *fd, idhdl h) |
---|
| 605 | { |
---|
[85e68dd] | 606 | const char *type_str = GetIdString(h); |
---|
[e813875] | 607 | int type_id = IDTYP(h); |
---|
[286bd57] | 608 | |
---|
[a3bc95e] | 609 | if ((type_id == PACKAGE_CMD) &&(strcmp(IDID(h), "Top") == 0)) |
---|
| 610 | return FALSE; |
---|
[472f39] | 611 | |
---|
[286bd57] | 612 | // we do not throw an error if a wrong type was attempted to be dumped |
---|
[472f39] | 613 | if (type_str == NULL) |
---|
| 614 | return FALSE; |
---|
[286bd57] | 615 | |
---|
[b7808e] | 616 | // handle qrings separately |
---|
[472f39] | 617 | if (type_id == QRING_CMD) |
---|
| 618 | return DumpQring(fd, h, type_str); |
---|
[c20843] | 619 | |
---|
[1cb879] | 620 | // C-proc not to be dumped |
---|
| 621 | if ((type_id == PROC_CMD) && (IDPROC(h)->language == LANG_C)) |
---|
| 622 | return FALSE; |
---|
| 623 | |
---|
[e1af45] | 624 | // put type and name |
---|
[472f39] | 625 | if (fprintf(fd, "%s %s", type_str, IDID(h)) == EOF) |
---|
| 626 | return TRUE; |
---|
[286bd57] | 627 | // for matricies, append the dimension |
---|
| 628 | if (type_id == MATRIX_CMD) |
---|
| 629 | { |
---|
| 630 | ideal id = IDIDEAL(h); |
---|
| 631 | if (fprintf(fd, "[%d][%d]", id->nrows, id->ncols)== EOF) return TRUE; |
---|
| 632 | } |
---|
[d754b7] | 633 | else if (type_id == INTMAT_CMD) |
---|
[286bd57] | 634 | { |
---|
[d754b7] | 635 | if (fprintf(fd, "[%d][%d]", IDINTVEC(h)->rows(), IDINTVEC(h)->cols()) |
---|
| 636 | == EOF) return TRUE; |
---|
[286bd57] | 637 | } |
---|
[0a3ddd] | 638 | |
---|
[a3bc95e] | 639 | if (type_id == PACKAGE_CMD) |
---|
| 640 | { |
---|
| 641 | return (fprintf(fd, ";\n") == EOF); |
---|
| 642 | } |
---|
[472f39] | 643 | |
---|
[d754b7] | 644 | // write the equal sign |
---|
| 645 | if (fprintf(fd, " = ") == EOF) return TRUE; |
---|
| 646 | |
---|
| 647 | // and the right hand side |
---|
| 648 | if (DumpRhs(fd, h) == EOF) return TRUE; |
---|
[c20843] | 649 | |
---|
[286bd57] | 650 | // semicolon und tschuess |
---|
| 651 | if (fprintf(fd, ";\n") == EOF) return TRUE; |
---|
| 652 | |
---|
| 653 | return FALSE; |
---|
| 654 | } |
---|
| 655 | |
---|
[85e68dd] | 656 | static const char* GetIdString(idhdl h) |
---|
[286bd57] | 657 | { |
---|
[e813875] | 658 | int type = IDTYP(h); |
---|
[c20843] | 659 | |
---|
[286bd57] | 660 | switch(type) |
---|
| 661 | { |
---|
| 662 | case LIST_CMD: |
---|
| 663 | { |
---|
| 664 | lists l = IDLIST(h); |
---|
| 665 | int i, nl = l->nr + 1; |
---|
| 666 | |
---|
| 667 | for (i=0; i<nl; i++) |
---|
| 668 | if (GetIdString((idhdl) &(l->m[i])) == NULL) return NULL; |
---|
| 669 | } |
---|
[0a3ddd] | 670 | case PACKAGE_CMD: |
---|
[286bd57] | 671 | case INT_CMD: |
---|
| 672 | case INTVEC_CMD: |
---|
| 673 | case INTMAT_CMD: |
---|
| 674 | case STRING_CMD: |
---|
| 675 | case RING_CMD: |
---|
[b7808e] | 676 | case QRING_CMD: |
---|
[286bd57] | 677 | case PROC_CMD: |
---|
| 678 | case NUMBER_CMD: |
---|
| 679 | case POLY_CMD: |
---|
| 680 | case IDEAL_CMD: |
---|
| 681 | case VECTOR_CMD: |
---|
| 682 | case MODUL_CMD: |
---|
| 683 | case MATRIX_CMD: |
---|
| 684 | return Tok2Cmdname(type); |
---|
[c20843] | 685 | |
---|
[d754b7] | 686 | case MAP_CMD: |
---|
[286bd57] | 687 | case LINK_CMD: |
---|
| 688 | return NULL; |
---|
| 689 | |
---|
| 690 | default: |
---|
[d754b7] | 691 | Warn("Error dump data of type %s", Tok2Cmdname(IDTYP(h))); |
---|
[286bd57] | 692 | return NULL; |
---|
| 693 | } |
---|
| 694 | } |
---|
| 695 | |
---|
[85e68dd] | 696 | static BOOLEAN DumpQring(FILE *fd, idhdl h, const char *type_str) |
---|
[b7808e] | 697 | { |
---|
[472f39] | 698 | char *ring_str = h->String(); |
---|
[d754b7] | 699 | if (fprintf(fd, "%s temp_ring = %s;\n", Tok2Cmdname(RING_CMD), ring_str) |
---|
| 700 | == EOF) return TRUE; |
---|
| 701 | if (fprintf(fd, "%s temp_ideal = %s;\n", Tok2Cmdname(IDEAL_CMD), |
---|
[7b2b05] | 702 | iiStringMatrix((matrix) IDRING(h)->qideal, 1, currRing, n_GetChar(currRing->cf))) |
---|
[b7808e] | 703 | == EOF) return TRUE; |
---|
| 704 | if (fprintf(fd, "attrib(temp_ideal, \"isSB\", 1);\n") == EOF) return TRUE; |
---|
| 705 | if (fprintf(fd, "%s %s = temp_ideal;\n", type_str, IDID(h)) == EOF) |
---|
| 706 | return TRUE; |
---|
[d754b7] | 707 | if (fprintf(fd, "kill temp_ring;\n") == EOF) return TRUE; |
---|
[b7808e] | 708 | else |
---|
[d754b7] | 709 | { |
---|
[c232af] | 710 | omFree(ring_str); |
---|
[b7808e] | 711 | return FALSE; |
---|
[d754b7] | 712 | } |
---|
[b7808e] | 713 | } |
---|
| 714 | |
---|
[c20843] | 715 | |
---|
[286bd57] | 716 | static int DumpRhs(FILE *fd, idhdl h) |
---|
| 717 | { |
---|
[e813875] | 718 | int type_id = IDTYP(h); |
---|
[286bd57] | 719 | |
---|
| 720 | if (type_id == LIST_CMD) |
---|
| 721 | { |
---|
| 722 | lists l = IDLIST(h); |
---|
| 723 | int i, nl = l->nr; |
---|
| 724 | |
---|
[d754b7] | 725 | fprintf(fd, "list("); |
---|
[c20843] | 726 | |
---|
[286bd57] | 727 | for (i=0; i<nl; i++) |
---|
| 728 | { |
---|
[d754b7] | 729 | if (DumpRhs(fd, (idhdl) &(l->m[i])) == EOF) return EOF; |
---|
| 730 | fprintf(fd, ","); |
---|
[286bd57] | 731 | } |
---|
[d754b7] | 732 | if (nl > 0) |
---|
| 733 | { |
---|
| 734 | if (DumpRhs(fd, (idhdl) &(l->m[nl])) == EOF) return EOF; |
---|
[286bd57] | 735 | } |
---|
[d754b7] | 736 | fprintf(fd, ")"); |
---|
[286bd57] | 737 | } |
---|
[2ba9a6] | 738 | else if (type_id == STRING_CMD) |
---|
[286bd57] | 739 | { |
---|
[d30a399] | 740 | char *pstr = IDSTRING(h); |
---|
[286bd57] | 741 | fputc('"', fd); |
---|
| 742 | while (*pstr != '\0') |
---|
| 743 | { |
---|
[97454d] | 744 | if (*pstr == '"' || *pstr == '\\') fputc('\\', fd); |
---|
[286bd57] | 745 | fputc(*pstr, fd); |
---|
| 746 | pstr++; |
---|
| 747 | } |
---|
| 748 | fputc('"', fd); |
---|
| 749 | } |
---|
[2ba9a6] | 750 | else if (type_id == PROC_CMD) |
---|
[c20843] | 751 | { |
---|
[2ba9a6] | 752 | procinfov pi = IDPROC(h); |
---|
[d30a399] | 753 | if (pi->language == LANG_SINGULAR) |
---|
| 754 | { |
---|
[2ba9a6] | 755 | if( pi->data.s.body==NULL) iiGetLibProcBuffer(pi); |
---|
[d30a399] | 756 | char *pstr = pi->data.s.body; |
---|
[2ba9a6] | 757 | fputc('"', fd); |
---|
[d30a399] | 758 | while (*pstr != '\0') |
---|
| 759 | { |
---|
[97454d] | 760 | if (*pstr == '"' || *pstr == '\\') fputc('\\', fd); |
---|
| 761 | fputc(*pstr, fd); |
---|
| 762 | pstr++; |
---|
[2ba9a6] | 763 | } |
---|
| 764 | fputc('"', fd); |
---|
[d30a399] | 765 | } |
---|
| 766 | else fputs("(null)", fd); |
---|
[2ba9a6] | 767 | } |
---|
[286bd57] | 768 | else |
---|
| 769 | { |
---|
[472f39] | 770 | char *rhs = h->String(); |
---|
[286bd57] | 771 | |
---|
| 772 | if (rhs == NULL) return EOF; |
---|
| 773 | |
---|
[d754b7] | 774 | if (type_id == INTVEC_CMD) fprintf(fd, "intvec("); |
---|
[286bd57] | 775 | |
---|
| 776 | if (fprintf(fd, "%s", rhs) == EOF) return EOF; |
---|
[c232af] | 777 | omFree(rhs); |
---|
[286bd57] | 778 | |
---|
| 779 | if ((type_id == RING_CMD || type_id == QRING_CMD) && |
---|
[7b2b05] | 780 | IDRING(h)->cf->type==n_algExt) |
---|
[286bd57] | 781 | { |
---|
| 782 | StringSetS(""); |
---|
[dd668f] | 783 | p_Write(IDRING(h)->cf->extRing->qideal->m[0],IDRING(h)->cf->extRing); |
---|
[b45d97] | 784 | rhs = StringAppendS(""); |
---|
[286bd57] | 785 | if (fprintf(fd, "; minpoly = %s", rhs) == EOF) return EOF; |
---|
| 786 | } |
---|
[d754b7] | 787 | else if (type_id == INTVEC_CMD) fprintf(fd, ")"); |
---|
[286bd57] | 788 | } |
---|
| 789 | return 1; |
---|
| 790 | } |
---|
[c20843] | 791 | |
---|
[286bd57] | 792 | BOOLEAN slGetDumpAscii(si_link l) |
---|
| 793 | { |
---|
| 794 | if (l->name[0] == '\0') |
---|
| 795 | { |
---|
[d754b7] | 796 | Werror("getdump: Can not get dump from stdin"); |
---|
[286bd57] | 797 | return TRUE; |
---|
| 798 | } |
---|
| 799 | else |
---|
| 800 | { |
---|
[057e93c] | 801 | BOOLEAN status = newFile(l->name); |
---|
| 802 | if (status) |
---|
| 803 | return TRUE; |
---|
[c20843] | 804 | |
---|
[a18fae] | 805 | int old_echo=si_echo; |
---|
| 806 | si_echo=0; |
---|
| 807 | |
---|
[057e93c] | 808 | status=yyparse(); |
---|
[c20843] | 809 | |
---|
[a18fae] | 810 | si_echo=old_echo; |
---|
[286bd57] | 811 | |
---|
| 812 | if (status) |
---|
| 813 | return TRUE; |
---|
| 814 | else |
---|
| 815 | { |
---|
[d754b7] | 816 | // lets reset the file pointer to the end to reflect that |
---|
[286bd57] | 817 | // we are finished with reading |
---|
| 818 | FILE *f = (FILE *) l->data; |
---|
| 819 | fseek(f, 0L, SEEK_END); |
---|
| 820 | return FALSE; |
---|
| 821 | } |
---|
| 822 | } |
---|
| 823 | } |
---|
[c20843] | 824 | |
---|
| 825 | |
---|
[d754b7] | 826 | /*------------Initialization at Start-up time------------------------*/ |
---|
[0e1846] | 827 | |
---|
[599326] | 828 | #include <Singular/slInit.h> |
---|
[0e1846] | 829 | |
---|
[6b32990] | 830 | static si_link_extension slTypeInit(si_link_extension s, const char* type) |
---|
| 831 | { |
---|
| 832 | assume(s != NULL); |
---|
| 833 | s->next = NULL; |
---|
| 834 | si_link_extension ns = (si_link_extension)omAlloc0Bin(s_si_link_extension_bin); |
---|
[50cbdc] | 835 | |
---|
[8ed989] | 836 | if (0) 0; |
---|
[6b32990] | 837 | #ifdef HAVE_DBM |
---|
[8ed989] | 838 | else if (strcmp(type, "DBM") == 0) |
---|
[6b32990] | 839 | s->next = slInitDBMExtension(ns); |
---|
| 840 | #endif |
---|
[8ed989] | 841 | #if 1 |
---|
| 842 | else if (strcmp(type, "ssi") == 0) |
---|
| 843 | s->next = slInitSsiExtension(ns); |
---|
[4082e8] | 844 | #endif |
---|
| 845 | #if 1 |
---|
| 846 | else if (strcmp(type, "|") == 0) |
---|
| 847 | s->next = slInitPipeExtension(ns); |
---|
[c20843] | 848 | #endif |
---|
[8ed989] | 849 | else |
---|
[6b32990] | 850 | { |
---|
| 851 | Warn("Found unknown link type: %s", type); |
---|
| 852 | Warn("Use default link type: %s", si_link_root->type); |
---|
| 853 | omFreeBin(ns, s_si_link_extension_bin); |
---|
| 854 | return si_link_root; |
---|
| 855 | } |
---|
[50cbdc] | 856 | |
---|
[6b32990] | 857 | if (s->next == NULL) |
---|
| 858 | { |
---|
| 859 | Werror("Can not initialize link type %s", type); |
---|
| 860 | omFreeBin(ns, s_si_link_extension_bin); |
---|
| 861 | return NULL; |
---|
| 862 | } |
---|
| 863 | return s->next; |
---|
| 864 | } |
---|
[0e1846] | 865 | |
---|
| 866 | void slStandardInit() |
---|
| 867 | { |
---|
[d754b7] | 868 | si_link_extension s; |
---|
[c232af] | 869 | si_link_root=(si_link_extension)omAlloc0Bin(s_si_link_extension_bin); |
---|
[d754b7] | 870 | si_link_root->Open=slOpenAscii; |
---|
[0e1846] | 871 | si_link_root->Close=slCloseAscii; |
---|
[80419f] | 872 | si_link_root->Kill=slCloseAscii; |
---|
[0e1846] | 873 | si_link_root->Read=slReadAscii; |
---|
[472f39] | 874 | si_link_root->Read2=slReadAscii2; |
---|
[0e1846] | 875 | si_link_root->Write=slWriteAscii; |
---|
[286bd57] | 876 | si_link_root->Dump=slDumpAscii; |
---|
| 877 | si_link_root->GetDump=slGetDumpAscii; |
---|
[d754b7] | 878 | si_link_root->Status=slStatusAscii; |
---|
[fcf5d4] | 879 | si_link_root->type="ASCII"; |
---|
[d754b7] | 880 | s = si_link_root; |
---|
[6b32990] | 881 | s->next = NULL; |
---|
[0e1846] | 882 | } |
---|