[0e1846] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
[341696] | 4 | /* $Id$ */ |
---|
[32df82] | 5 | |
---|
[0e1846] | 6 | /* |
---|
[6ae4f5] | 7 | * ABSTRACT: general interface to links |
---|
[d754b7] | 8 | */ |
---|
[0e1846] | 9 | |
---|
| 10 | #include <stdio.h> |
---|
| 11 | #include <string.h> |
---|
[644227] | 12 | #include <sys/types.h> |
---|
| 13 | #include <sys/stat.h> |
---|
| 14 | #include <unistd.h> |
---|
| 15 | |
---|
[762407] | 16 | #include "config.h" |
---|
[b1dfaf] | 17 | #include <kernel/mod2.h> |
---|
[599326] | 18 | #include <Singular/tok.h> |
---|
[0fb34ba] | 19 | #include <misc/options.h> |
---|
[b1dfaf] | 20 | #include <omalloc/omalloc.h> |
---|
[599326] | 21 | #include <kernel/febase.h> |
---|
| 22 | #include <Singular/subexpr.h> |
---|
| 23 | #include <Singular/ipid.h> |
---|
| 24 | #include <Singular/silink.h> |
---|
| 25 | #include <Singular/ipshell.h> |
---|
[0fb34ba] | 26 | #include <polys/matpol.h> |
---|
| 27 | #include <polys/monomials/ring.h> |
---|
[599326] | 28 | #include <Singular/lists.h> |
---|
| 29 | #include <kernel/ideals.h> |
---|
[0fb34ba] | 30 | #include <coeffs/numbers.h> |
---|
| 31 | #include <misc/intvec.h> |
---|
[8ed989] | 32 | #include <Singular/ssiLink.h> |
---|
[4082e8] | 33 | #include <Singular/pipeLink.h> |
---|
[286bd57] | 34 | |
---|
[0c7cb8] | 35 | // #ifdef HAVE_DBM |
---|
| 36 | // #ifdef ix86_Win |
---|
| 37 | // #define USE_GDBM |
---|
| 38 | // #endif |
---|
| 39 | // #endif |
---|
[50cbdc] | 40 | |
---|
[c232af] | 41 | omBin s_si_link_extension_bin = omGetSpecBin(sizeof(s_si_link_extension)); |
---|
| 42 | omBin sip_link_bin = omGetSpecBin(sizeof(sip_link)); |
---|
| 43 | omBin ip_link_bin = omGetSpecBin(sizeof(ip_link)); |
---|
| 44 | |
---|
[286bd57] | 45 | /* declarations */ |
---|
| 46 | static BOOLEAN DumpAscii(FILE *fd, idhdl h); |
---|
| 47 | static BOOLEAN DumpAsciiIdhdl(FILE *fd, idhdl h); |
---|
[85e68dd] | 48 | static const char* GetIdString(idhdl h); |
---|
[286bd57] | 49 | static int DumpRhs(FILE *fd, idhdl h); |
---|
[85e68dd] | 50 | static BOOLEAN DumpQring(FILE *fd, idhdl h, const char *type_str); |
---|
[d754b7] | 51 | static BOOLEAN DumpAsciiMaps(FILE *fd, idhdl h, idhdl rhdl); |
---|
[6b32990] | 52 | static si_link_extension slTypeInit(si_link_extension s, const char* type); |
---|
[0e1846] | 53 | |
---|
| 54 | /* ====================================================================== */ |
---|
| 55 | si_link_extension si_link_root=NULL; |
---|
[d754b7] | 56 | |
---|
[0e1846] | 57 | BOOLEAN slInit(si_link l, char *istr) |
---|
| 58 | { |
---|
[d754b7] | 59 | char *type = NULL, *mode = NULL, *name = NULL; |
---|
| 60 | int i = 0, j; |
---|
[c20843] | 61 | |
---|
[d754b7] | 62 | // set mode and type |
---|
| 63 | if (istr != NULL) |
---|
[0e1846] | 64 | { |
---|
[d754b7] | 65 | // find the first colon char in istr |
---|
| 66 | i = 0; |
---|
| 67 | while (istr[i] != ':' && istr[i] != '\0') i++; |
---|
| 68 | if (istr[i] == ':') |
---|
[0e1846] | 69 | { |
---|
[d754b7] | 70 | // if found, set type |
---|
| 71 | if (i > 0) |
---|
[0e1846] | 72 | { |
---|
[d754b7] | 73 | istr[i] = '\0'; |
---|
[c232af] | 74 | type = omStrDup(istr); |
---|
[d754b7] | 75 | istr[i] = ':'; |
---|
[0e1846] | 76 | } |
---|
[d754b7] | 77 | // and check for mode |
---|
| 78 | j = ++i; |
---|
| 79 | while (istr[j] != ' ' && istr[j] != '\0') j++; |
---|
| 80 | if (j > i) |
---|
[0e1846] | 81 | { |
---|
[c232af] | 82 | mode = omStrDup(&(istr[i])); |
---|
[d754b7] | 83 | mode[j - i] = '\0'; |
---|
[0e1846] | 84 | } |
---|
[d754b7] | 85 | // and for the name |
---|
| 86 | while (istr[j] == ' '&& istr[j] != '\0') j++; |
---|
[c232af] | 87 | if (istr[j] != '\0') name = omStrDup(&(istr[j])); |
---|
[d754b7] | 88 | } |
---|
| 89 | else // no colon find -- string is entire name |
---|
| 90 | { |
---|
| 91 | j=0; |
---|
| 92 | while (istr[j] == ' '&& istr[j] != '\0') j++; |
---|
[c232af] | 93 | if (istr[j] != '\0') name = omStrDup(&(istr[j])); |
---|
[0e1846] | 94 | } |
---|
| 95 | } |
---|
[e6969d] | 96 | |
---|
[d754b7] | 97 | // set the link extension |
---|
| 98 | if (type != NULL) |
---|
[0e1846] | 99 | { |
---|
[d754b7] | 100 | si_link_extension s = si_link_root; |
---|
[6b32990] | 101 | si_link_extension prev = s; |
---|
[c20843] | 102 | |
---|
[6b32990] | 103 | while (strcmp(s->type, type) != 0) |
---|
| 104 | { |
---|
[50cbdc] | 105 | if (s->next == NULL) |
---|
[6b32990] | 106 | { |
---|
| 107 | prev = s; |
---|
| 108 | s = NULL; |
---|
| 109 | break; |
---|
| 110 | } |
---|
| 111 | else |
---|
| 112 | { |
---|
| 113 | s = s->next; |
---|
| 114 | } |
---|
| 115 | } |
---|
[d754b7] | 116 | |
---|
| 117 | if (s != NULL) |
---|
| 118 | l->m = s; |
---|
| 119 | else |
---|
[0e1846] | 120 | { |
---|
[6b32990] | 121 | l->m = slTypeInit(prev, type); |
---|
[0e1846] | 122 | } |
---|
[c232af] | 123 | omFree(type); |
---|
[0e1846] | 124 | } |
---|
[d754b7] | 125 | else |
---|
| 126 | l->m = si_link_root; |
---|
| 127 | |
---|
[6b32990] | 128 | if (l->m == NULL) return TRUE; |
---|
[50cbdc] | 129 | |
---|
[c232af] | 130 | l->name = (name != NULL ? name : omStrDup("")); |
---|
| 131 | l->mode = (mode != NULL ? mode : omStrDup("")); |
---|
[d754b7] | 132 | l->ref = 1; |
---|
| 133 | return FALSE; |
---|
[0e1846] | 134 | } |
---|
| 135 | |
---|
[e6969d] | 136 | void slCleanUp(si_link l) |
---|
[0e1846] | 137 | { |
---|
[e6969d] | 138 | (l->ref)--; |
---|
[d754b7] | 139 | if (l->ref == 0) |
---|
[0e1846] | 140 | { |
---|
[80419f] | 141 | if (SI_LINK_OPEN_P(l)) |
---|
| 142 | { |
---|
| 143 | if (l->m->Kill != NULL) l->m->Kill(l); |
---|
| 144 | else if (l->m->Close != NULL) l->m->Close(l); |
---|
| 145 | } |
---|
[c232af] | 146 | omFree((ADDRESS)l->name); |
---|
| 147 | omFree((ADDRESS)l->mode); |
---|
[d754b7] | 148 | memset((void *) l, 0, sizeof(ip_link)); |
---|
[e6969d] | 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | void slKill(si_link l) |
---|
| 153 | { |
---|
[d754b7] | 154 | slCleanUp(l); |
---|
[e6969d] | 155 | if (l->ref == 0) |
---|
[c232af] | 156 | omFreeBin((ADDRESS)l, ip_link_bin); |
---|
[0e1846] | 157 | } |
---|
| 158 | |
---|
[9db0567] | 159 | const char* slStatus(si_link l, const char *request) |
---|
[0e1846] | 160 | { |
---|
[d754b7] | 161 | if (l == NULL) return "empty link"; |
---|
| 162 | else if (l->m == NULL) return "unknown link type"; |
---|
| 163 | else if (strcmp(request, "type") == 0) return l->m->type; |
---|
| 164 | else if (strcmp(request, "mode") == 0) return l->mode; |
---|
| 165 | else if (strcmp(request, "name") == 0) return l->name; |
---|
[644227] | 166 | else if (strcmp(request, "exists") ==0) |
---|
| 167 | { |
---|
| 168 | struct stat buf; |
---|
| 169 | if (lstat(l->name,&buf)==0) return "yes"; |
---|
| 170 | else return "no"; |
---|
| 171 | } |
---|
[d754b7] | 172 | else if (strcmp(request, "open") == 0) |
---|
| 173 | { |
---|
| 174 | if (SI_LINK_OPEN_P(l)) return "yes"; |
---|
| 175 | else return "no"; |
---|
| 176 | } |
---|
| 177 | else if (strcmp(request, "openread") == 0) |
---|
| 178 | { |
---|
| 179 | if (SI_LINK_R_OPEN_P(l)) return "yes"; |
---|
| 180 | else return "no"; |
---|
| 181 | } |
---|
| 182 | else if (strcmp(request, "openwrite") == 0) |
---|
| 183 | { |
---|
| 184 | if (SI_LINK_W_OPEN_P(l)) return "yes"; |
---|
| 185 | else return "no"; |
---|
| 186 | } |
---|
| 187 | else if (l->m->Status == NULL) return "unknown status request"; |
---|
| 188 | else return l->m->Status(l, request); |
---|
[0e1846] | 189 | } |
---|
| 190 | |
---|
[d754b7] | 191 | //-------------------------------------------------------------------------- |
---|
[b5f276e] | 192 | BOOLEAN slOpen(si_link l, short flag, leftv h) |
---|
[0e1846] | 193 | { |
---|
[47a616d] | 194 | BOOLEAN res = TRUE; |
---|
| 195 | if (l!=NULL) |
---|
| 196 | { |
---|
[c20843] | 197 | |
---|
[47a616d] | 198 | if (l->m == NULL) slInit(l, ((char*)"")); |
---|
[d754b7] | 199 | |
---|
[47a616d] | 200 | const char *c="_";; |
---|
| 201 | if (h!=NULL) c=h->Name(); |
---|
| 202 | |
---|
| 203 | if (SI_LINK_OPEN_P(l)) |
---|
| 204 | { |
---|
| 205 | Warn("open: link of type: %s, mode: %s, name: %s is already open", |
---|
[d754b7] | 206 | l->m->type, l->mode, l->name); |
---|
[47a616d] | 207 | return FALSE; |
---|
| 208 | } |
---|
| 209 | else if (l->m->Open != NULL) |
---|
| 210 | { |
---|
| 211 | res = l->m->Open(l, flag, h); |
---|
| 212 | if (res) |
---|
| 213 | Werror("open: Error for link %s of type: %s, mode: %s, name: %s", |
---|
| 214 | c, l->m->type, l->mode, l->name); |
---|
| 215 | } |
---|
[d754b7] | 216 | } |
---|
| 217 | return res; |
---|
[0e1846] | 218 | } |
---|
| 219 | |
---|
| 220 | BOOLEAN slClose(si_link l) |
---|
| 221 | { |
---|
[d754b7] | 222 | |
---|
[1fc83c0] | 223 | if(! SI_LINK_OPEN_P(l)) |
---|
| 224 | return FALSE; |
---|
[c20843] | 225 | |
---|
[47a616d] | 226 | BOOLEAN res = TRUE; |
---|
| 227 | if (l->m->Close != NULL) |
---|
| 228 | { |
---|
| 229 | res = l->m->Close(l); |
---|
| 230 | if (res) |
---|
| 231 | Werror("close: Error for link of type: %s, mode: %s, name: %s", |
---|
[d754b7] | 232 | l->m->type, l->mode, l->name); |
---|
[47a616d] | 233 | } |
---|
[d754b7] | 234 | return res; |
---|
[0e1846] | 235 | } |
---|
| 236 | |
---|
[d754b7] | 237 | leftv slRead(si_link l, leftv a) |
---|
[0e1846] | 238 | { |
---|
[fcafdb] | 239 | char *mode; |
---|
[0e1846] | 240 | leftv v = NULL; |
---|
| 241 | if( ! SI_LINK_R_OPEN_P(l)) // open r ? |
---|
| 242 | { |
---|
[fcafdb] | 243 | #ifdef HAVE_DBM |
---|
| 244 | #ifdef USE_GDBM |
---|
| 245 | if (! SI_LINK_CLOSE_P(l)) |
---|
| 246 | { |
---|
[50cbdc] | 247 | if (slClose(l)) return NULL; |
---|
[fcafdb] | 248 | } |
---|
| 249 | #endif |
---|
| 250 | #endif |
---|
[b5f276e] | 251 | if (slOpen(l, SI_LINK_READ,NULL)) return NULL; |
---|
[0e1846] | 252 | } |
---|
[d754b7] | 253 | |
---|
| 254 | if (SI_LINK_R_OPEN_P(l)) |
---|
[0e1846] | 255 | { // open r |
---|
| 256 | if (a==NULL) |
---|
| 257 | { |
---|
[d754b7] | 258 | if (l->m->Read != NULL) v = l->m->Read(l); |
---|
[0e1846] | 259 | } |
---|
| 260 | else |
---|
| 261 | { |
---|
[d754b7] | 262 | if (l->m->Read2 != NULL) v = l->m->Read2(l,a); |
---|
[0e1846] | 263 | } |
---|
| 264 | } |
---|
[d754b7] | 265 | else |
---|
[0e1846] | 266 | { |
---|
[d754b7] | 267 | Werror("read: Error to open link of type %s, mode: %s, name: %s for reading", |
---|
| 268 | l->m->type, l->mode, l->name); |
---|
| 269 | return NULL; |
---|
[0e1846] | 270 | } |
---|
[c20843] | 271 | |
---|
[d754b7] | 272 | // here comes the eval: |
---|
[1fc83c0] | 273 | if (v != NULL) |
---|
[42e60f] | 274 | { |
---|
| 275 | if (v->Eval() && !errorreported) |
---|
| 276 | WerrorS("eval: failed"); |
---|
| 277 | } |
---|
[d754b7] | 278 | else |
---|
| 279 | Werror("read: Error for link of type %s, mode: %s, name: %s", |
---|
| 280 | l->m->type, l->mode, l->name); |
---|
[0e1846] | 281 | return v; |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | BOOLEAN slWrite(si_link l, leftv v) |
---|
| 285 | { |
---|
[d754b7] | 286 | BOOLEAN res; |
---|
[c20843] | 287 | |
---|
[0e1846] | 288 | if(! SI_LINK_W_OPEN_P(l)) // open w ? |
---|
| 289 | { |
---|
[fcafdb] | 290 | #ifdef HAVE_DBM |
---|
| 291 | #ifdef USE_GDBM |
---|
| 292 | if (! SI_LINK_CLOSE_P(l)) |
---|
| 293 | { |
---|
[50cbdc] | 294 | if (slClose(l)) return TRUE; |
---|
[fcafdb] | 295 | } |
---|
| 296 | #endif |
---|
| 297 | #endif |
---|
[b5f276e] | 298 | if (slOpen(l, SI_LINK_WRITE,NULL)) return TRUE; |
---|
[0e1846] | 299 | } |
---|
[d754b7] | 300 | |
---|
[fcafdb] | 301 | if (SI_LINK_W_OPEN_P(l)) |
---|
[0e1846] | 302 | { // now open w |
---|
[1fc83c0] | 303 | if (l->m->Write != NULL) |
---|
| 304 | res = l->m->Write(l,v); |
---|
| 305 | else |
---|
| 306 | res = TRUE; |
---|
[d754b7] | 307 | |
---|
| 308 | if (res) |
---|
| 309 | Werror("write: Error for link of type %s, mode: %s, name: %s", |
---|
| 310 | l->m->type, l->mode, l->name); |
---|
| 311 | return res; |
---|
| 312 | } |
---|
| 313 | else |
---|
| 314 | { |
---|
| 315 | Werror("write: Error to open link of type %s, mode: %s, name: %s for writing", |
---|
| 316 | l->m->type, l->mode, l->name); |
---|
| 317 | return TRUE; |
---|
[0e1846] | 318 | } |
---|
| 319 | } |
---|
| 320 | |
---|
[286bd57] | 321 | BOOLEAN slDump(si_link l) |
---|
| 322 | { |
---|
[d754b7] | 323 | BOOLEAN res; |
---|
| 324 | |
---|
[286bd57] | 325 | if(! SI_LINK_W_OPEN_P(l)) // open w ? |
---|
| 326 | { |
---|
[b5f276e] | 327 | if (slOpen(l, SI_LINK_WRITE,NULL)) return TRUE; |
---|
[286bd57] | 328 | } |
---|
[d754b7] | 329 | |
---|
| 330 | if(SI_LINK_W_OPEN_P(l)) |
---|
[286bd57] | 331 | { // now open w |
---|
[1fc83c0] | 332 | if (l->m->Dump != NULL) |
---|
| 333 | res = l->m->Dump(l); |
---|
| 334 | else |
---|
| 335 | res = TRUE; |
---|
[286bd57] | 336 | |
---|
[d754b7] | 337 | if (res) |
---|
| 338 | Werror("dump: Error for link of type %s, mode: %s, name: %s", |
---|
| 339 | l->m->type, l->mode, l->name); |
---|
| 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 ============================================= */ |
---|
[b5f276e] | 382 | BOOLEAN slOpenAscii(si_link l, short flag, leftv h) |
---|
[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); |
---|
[a6904c] | 547 | fprintf(fd, "option(set, intvec(%d, %d));\n", test, verbose); |
---|
[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 | char *name; |
---|
| 667 | |
---|
| 668 | for (i=0; i<nl; i++) |
---|
| 669 | if (GetIdString((idhdl) &(l->m[i])) == NULL) return NULL; |
---|
| 670 | } |
---|
[0a3ddd] | 671 | case PACKAGE_CMD: |
---|
[286bd57] | 672 | case INT_CMD: |
---|
| 673 | case INTVEC_CMD: |
---|
| 674 | case INTMAT_CMD: |
---|
| 675 | case STRING_CMD: |
---|
| 676 | case RING_CMD: |
---|
[b7808e] | 677 | case QRING_CMD: |
---|
[286bd57] | 678 | case PROC_CMD: |
---|
| 679 | case NUMBER_CMD: |
---|
| 680 | case POLY_CMD: |
---|
| 681 | case IDEAL_CMD: |
---|
| 682 | case VECTOR_CMD: |
---|
| 683 | case MODUL_CMD: |
---|
| 684 | case MATRIX_CMD: |
---|
| 685 | return Tok2Cmdname(type); |
---|
[c20843] | 686 | |
---|
[d754b7] | 687 | case MAP_CMD: |
---|
[286bd57] | 688 | case LINK_CMD: |
---|
| 689 | return NULL; |
---|
| 690 | |
---|
| 691 | default: |
---|
[d754b7] | 692 | Warn("Error dump data of type %s", Tok2Cmdname(IDTYP(h))); |
---|
[286bd57] | 693 | return NULL; |
---|
| 694 | } |
---|
| 695 | } |
---|
| 696 | |
---|
[85e68dd] | 697 | static BOOLEAN DumpQring(FILE *fd, idhdl h, const char *type_str) |
---|
[b7808e] | 698 | { |
---|
[472f39] | 699 | char *ring_str = h->String(); |
---|
[d754b7] | 700 | if (fprintf(fd, "%s temp_ring = %s;\n", Tok2Cmdname(RING_CMD), ring_str) |
---|
| 701 | == EOF) return TRUE; |
---|
| 702 | if (fprintf(fd, "%s temp_ideal = %s;\n", Tok2Cmdname(IDEAL_CMD), |
---|
[7b2b05] | 703 | iiStringMatrix((matrix) IDRING(h)->qideal, 1, currRing, n_GetChar(currRing->cf))) |
---|
[b7808e] | 704 | == EOF) return TRUE; |
---|
| 705 | if (fprintf(fd, "attrib(temp_ideal, \"isSB\", 1);\n") == EOF) return TRUE; |
---|
| 706 | if (fprintf(fd, "%s %s = temp_ideal;\n", type_str, IDID(h)) == EOF) |
---|
| 707 | return TRUE; |
---|
[d754b7] | 708 | if (fprintf(fd, "kill temp_ring;\n") == EOF) return TRUE; |
---|
[b7808e] | 709 | else |
---|
[d754b7] | 710 | { |
---|
[c232af] | 711 | omFree(ring_str); |
---|
[b7808e] | 712 | return FALSE; |
---|
[d754b7] | 713 | } |
---|
[b7808e] | 714 | } |
---|
| 715 | |
---|
[c20843] | 716 | |
---|
[286bd57] | 717 | static int DumpRhs(FILE *fd, idhdl h) |
---|
| 718 | { |
---|
[e813875] | 719 | int type_id = IDTYP(h); |
---|
[286bd57] | 720 | |
---|
| 721 | if (type_id == LIST_CMD) |
---|
| 722 | { |
---|
| 723 | lists l = IDLIST(h); |
---|
| 724 | int i, nl = l->nr; |
---|
| 725 | |
---|
[d754b7] | 726 | fprintf(fd, "list("); |
---|
[c20843] | 727 | |
---|
[286bd57] | 728 | for (i=0; i<nl; i++) |
---|
| 729 | { |
---|
[d754b7] | 730 | if (DumpRhs(fd, (idhdl) &(l->m[i])) == EOF) return EOF; |
---|
| 731 | fprintf(fd, ","); |
---|
[286bd57] | 732 | } |
---|
[d754b7] | 733 | if (nl > 0) |
---|
| 734 | { |
---|
| 735 | if (DumpRhs(fd, (idhdl) &(l->m[nl])) == EOF) return EOF; |
---|
[286bd57] | 736 | } |
---|
[d754b7] | 737 | fprintf(fd, ")"); |
---|
[286bd57] | 738 | } |
---|
[2ba9a6] | 739 | else if (type_id == STRING_CMD) |
---|
[286bd57] | 740 | { |
---|
| 741 | char *pstr = IDSTRING(h), c; |
---|
| 742 | fputc('"', fd); |
---|
| 743 | while (*pstr != '\0') |
---|
| 744 | { |
---|
[97454d] | 745 | if (*pstr == '"' || *pstr == '\\') fputc('\\', fd); |
---|
[286bd57] | 746 | fputc(*pstr, fd); |
---|
| 747 | pstr++; |
---|
| 748 | } |
---|
| 749 | fputc('"', fd); |
---|
| 750 | } |
---|
[2ba9a6] | 751 | else if (type_id == PROC_CMD) |
---|
[c20843] | 752 | { |
---|
[2ba9a6] | 753 | procinfov pi = IDPROC(h); |
---|
| 754 | if (pi->language == LANG_SINGULAR) { |
---|
| 755 | if( pi->data.s.body==NULL) iiGetLibProcBuffer(pi); |
---|
| 756 | char *pstr = pi->data.s.body, c; |
---|
| 757 | fputc('"', fd); |
---|
| 758 | while (*pstr != '\0') { |
---|
[97454d] | 759 | if (*pstr == '"' || *pstr == '\\') fputc('\\', fd); |
---|
| 760 | fputc(*pstr, fd); |
---|
| 761 | pstr++; |
---|
[2ba9a6] | 762 | } |
---|
| 763 | fputc('"', fd); |
---|
| 764 | } else fputs("(null)", fd); |
---|
| 765 | } |
---|
[286bd57] | 766 | else |
---|
| 767 | { |
---|
[472f39] | 768 | char *rhs = h->String(); |
---|
[286bd57] | 769 | |
---|
| 770 | if (rhs == NULL) return EOF; |
---|
| 771 | |
---|
[d754b7] | 772 | if (type_id == INTVEC_CMD) fprintf(fd, "intvec("); |
---|
[286bd57] | 773 | |
---|
| 774 | if (fprintf(fd, "%s", rhs) == EOF) return EOF; |
---|
[c232af] | 775 | omFree(rhs); |
---|
[286bd57] | 776 | |
---|
| 777 | if ((type_id == RING_CMD || type_id == QRING_CMD) && |
---|
[7b2b05] | 778 | IDRING(h)->cf->type==n_algExt) |
---|
[286bd57] | 779 | { |
---|
| 780 | StringSetS(""); |
---|
[dd668f] | 781 | p_Write(IDRING(h)->cf->extRing->qideal->m[0],IDRING(h)->cf->extRing); |
---|
[b45d97] | 782 | rhs = StringAppendS(""); |
---|
[286bd57] | 783 | if (fprintf(fd, "; minpoly = %s", rhs) == EOF) return EOF; |
---|
| 784 | } |
---|
[d754b7] | 785 | else if (type_id == INTVEC_CMD) fprintf(fd, ")"); |
---|
[286bd57] | 786 | } |
---|
| 787 | return 1; |
---|
| 788 | } |
---|
[c20843] | 789 | |
---|
[286bd57] | 790 | BOOLEAN slGetDumpAscii(si_link l) |
---|
| 791 | { |
---|
| 792 | if (l->name[0] == '\0') |
---|
| 793 | { |
---|
[d754b7] | 794 | Werror("getdump: Can not get dump from stdin"); |
---|
[286bd57] | 795 | return TRUE; |
---|
| 796 | } |
---|
| 797 | else |
---|
| 798 | { |
---|
[057e93c] | 799 | BOOLEAN status = newFile(l->name); |
---|
| 800 | if (status) |
---|
| 801 | return TRUE; |
---|
[c20843] | 802 | |
---|
[a18fae] | 803 | int old_echo=si_echo; |
---|
| 804 | si_echo=0; |
---|
| 805 | |
---|
[057e93c] | 806 | status=yyparse(); |
---|
[c20843] | 807 | |
---|
[a18fae] | 808 | si_echo=old_echo; |
---|
[286bd57] | 809 | |
---|
| 810 | if (status) |
---|
| 811 | return TRUE; |
---|
| 812 | else |
---|
| 813 | { |
---|
[d754b7] | 814 | // lets reset the file pointer to the end to reflect that |
---|
[286bd57] | 815 | // we are finished with reading |
---|
| 816 | FILE *f = (FILE *) l->data; |
---|
| 817 | fseek(f, 0L, SEEK_END); |
---|
| 818 | return FALSE; |
---|
| 819 | } |
---|
| 820 | } |
---|
| 821 | } |
---|
[c20843] | 822 | |
---|
| 823 | |
---|
[d754b7] | 824 | /*------------Initialization at Start-up time------------------------*/ |
---|
[0e1846] | 825 | |
---|
[599326] | 826 | #include <Singular/slInit.h> |
---|
[0e1846] | 827 | |
---|
[6b32990] | 828 | static si_link_extension slTypeInit(si_link_extension s, const char* type) |
---|
| 829 | { |
---|
| 830 | assume(s != NULL); |
---|
| 831 | s->next = NULL; |
---|
| 832 | si_link_extension ns = (si_link_extension)omAlloc0Bin(s_si_link_extension_bin); |
---|
[50cbdc] | 833 | |
---|
[8ed989] | 834 | if (0) 0; |
---|
[0e1846] | 835 | #ifdef HAVE_MPSR |
---|
[8ed989] | 836 | else if (strcmp(type, "MPfile") == 0) |
---|
[6b32990] | 837 | s->next = slInitMPFileExtension(ns); |
---|
| 838 | else if (strcmp(type, "MPtcp") == 0) |
---|
| 839 | s->next = slInitMPTcpExtension(ns); |
---|
| 840 | #endif |
---|
| 841 | #ifdef HAVE_DBM |
---|
[8ed989] | 842 | else if (strcmp(type, "DBM") == 0) |
---|
[6b32990] | 843 | s->next = slInitDBMExtension(ns); |
---|
| 844 | #endif |
---|
[8ed989] | 845 | #if 1 |
---|
| 846 | else if (strcmp(type, "ssi") == 0) |
---|
| 847 | s->next = slInitSsiExtension(ns); |
---|
[4082e8] | 848 | #endif |
---|
| 849 | #if 1 |
---|
| 850 | else if (strcmp(type, "|") == 0) |
---|
| 851 | s->next = slInitPipeExtension(ns); |
---|
[c20843] | 852 | #endif |
---|
[8ed989] | 853 | else |
---|
[6b32990] | 854 | { |
---|
| 855 | Warn("Found unknown link type: %s", type); |
---|
| 856 | Warn("Use default link type: %s", si_link_root->type); |
---|
| 857 | omFreeBin(ns, s_si_link_extension_bin); |
---|
| 858 | return si_link_root; |
---|
| 859 | } |
---|
[50cbdc] | 860 | |
---|
[6b32990] | 861 | if (s->next == NULL) |
---|
| 862 | { |
---|
| 863 | Werror("Can not initialize link type %s", type); |
---|
| 864 | omFreeBin(ns, s_si_link_extension_bin); |
---|
| 865 | return NULL; |
---|
| 866 | } |
---|
| 867 | return s->next; |
---|
| 868 | } |
---|
[0e1846] | 869 | |
---|
| 870 | void slStandardInit() |
---|
| 871 | { |
---|
[d754b7] | 872 | si_link_extension s; |
---|
[c232af] | 873 | si_link_root=(si_link_extension)omAlloc0Bin(s_si_link_extension_bin); |
---|
[d754b7] | 874 | si_link_root->Open=slOpenAscii; |
---|
[0e1846] | 875 | si_link_root->Close=slCloseAscii; |
---|
[80419f] | 876 | si_link_root->Kill=slCloseAscii; |
---|
[0e1846] | 877 | si_link_root->Read=slReadAscii; |
---|
[472f39] | 878 | si_link_root->Read2=slReadAscii2; |
---|
[0e1846] | 879 | si_link_root->Write=slWriteAscii; |
---|
[286bd57] | 880 | si_link_root->Dump=slDumpAscii; |
---|
| 881 | si_link_root->GetDump=slGetDumpAscii; |
---|
[d754b7] | 882 | si_link_root->Status=slStatusAscii; |
---|
[fcf5d4] | 883 | si_link_root->type="ASCII"; |
---|
[d754b7] | 884 | s = si_link_root; |
---|
[6b32990] | 885 | s->next = NULL; |
---|
[0e1846] | 886 | } |
---|