[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 | |
---|
[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> |
---|
[8ed989] | 31 | #include <Singular/ssiLink.h> |
---|
[4082e8] | 32 | #include <Singular/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 | { |
---|
[fcafdb] | 238 | char *mode; |
---|
[0e1846] | 239 | leftv v = NULL; |
---|
| 240 | if( ! SI_LINK_R_OPEN_P(l)) // open r ? |
---|
| 241 | { |
---|
[fcafdb] | 242 | #ifdef HAVE_DBM |
---|
| 243 | #ifdef USE_GDBM |
---|
| 244 | if (! SI_LINK_CLOSE_P(l)) |
---|
| 245 | { |
---|
[50cbdc] | 246 | if (slClose(l)) return NULL; |
---|
[fcafdb] | 247 | } |
---|
| 248 | #endif |
---|
| 249 | #endif |
---|
[b5f276e] | 250 | if (slOpen(l, SI_LINK_READ,NULL)) return NULL; |
---|
[0e1846] | 251 | } |
---|
[d754b7] | 252 | |
---|
| 253 | if (SI_LINK_R_OPEN_P(l)) |
---|
[0e1846] | 254 | { // open r |
---|
| 255 | if (a==NULL) |
---|
| 256 | { |
---|
[d754b7] | 257 | if (l->m->Read != NULL) v = l->m->Read(l); |
---|
[0e1846] | 258 | } |
---|
| 259 | else |
---|
| 260 | { |
---|
[d754b7] | 261 | if (l->m->Read2 != NULL) v = l->m->Read2(l,a); |
---|
[0e1846] | 262 | } |
---|
| 263 | } |
---|
[d754b7] | 264 | else |
---|
[0e1846] | 265 | { |
---|
[d754b7] | 266 | Werror("read: Error to open link of type %s, mode: %s, name: %s for reading", |
---|
| 267 | l->m->type, l->mode, l->name); |
---|
| 268 | return NULL; |
---|
[0e1846] | 269 | } |
---|
[c20843] | 270 | |
---|
[d754b7] | 271 | // here comes the eval: |
---|
[1fc83c0] | 272 | if (v != NULL) |
---|
[42e60f] | 273 | { |
---|
| 274 | if (v->Eval() && !errorreported) |
---|
| 275 | WerrorS("eval: failed"); |
---|
| 276 | } |
---|
[d754b7] | 277 | else |
---|
| 278 | Werror("read: Error for link of type %s, mode: %s, name: %s", |
---|
| 279 | l->m->type, l->mode, l->name); |
---|
[0e1846] | 280 | return v; |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | BOOLEAN slWrite(si_link l, leftv v) |
---|
| 284 | { |
---|
[d754b7] | 285 | BOOLEAN res; |
---|
[c20843] | 286 | |
---|
[0e1846] | 287 | if(! SI_LINK_W_OPEN_P(l)) // open w ? |
---|
| 288 | { |
---|
[fcafdb] | 289 | #ifdef HAVE_DBM |
---|
| 290 | #ifdef USE_GDBM |
---|
| 291 | if (! SI_LINK_CLOSE_P(l)) |
---|
| 292 | { |
---|
[50cbdc] | 293 | if (slClose(l)) return TRUE; |
---|
[fcafdb] | 294 | } |
---|
| 295 | #endif |
---|
| 296 | #endif |
---|
[b5f276e] | 297 | if (slOpen(l, SI_LINK_WRITE,NULL)) return TRUE; |
---|
[0e1846] | 298 | } |
---|
[d754b7] | 299 | |
---|
[fcafdb] | 300 | if (SI_LINK_W_OPEN_P(l)) |
---|
[0e1846] | 301 | { // now open w |
---|
[1fc83c0] | 302 | if (l->m->Write != NULL) |
---|
| 303 | res = l->m->Write(l,v); |
---|
| 304 | else |
---|
| 305 | res = TRUE; |
---|
[d754b7] | 306 | |
---|
| 307 | if (res) |
---|
| 308 | Werror("write: Error for link of type %s, mode: %s, name: %s", |
---|
| 309 | l->m->type, l->mode, l->name); |
---|
| 310 | return res; |
---|
| 311 | } |
---|
| 312 | else |
---|
| 313 | { |
---|
| 314 | Werror("write: Error to open link of type %s, mode: %s, name: %s for writing", |
---|
| 315 | l->m->type, l->mode, l->name); |
---|
| 316 | return TRUE; |
---|
[0e1846] | 317 | } |
---|
| 318 | } |
---|
| 319 | |
---|
[286bd57] | 320 | BOOLEAN slDump(si_link l) |
---|
| 321 | { |
---|
[d754b7] | 322 | BOOLEAN res; |
---|
| 323 | |
---|
[286bd57] | 324 | if(! SI_LINK_W_OPEN_P(l)) // open w ? |
---|
| 325 | { |
---|
[b5f276e] | 326 | if (slOpen(l, SI_LINK_WRITE,NULL)) return TRUE; |
---|
[286bd57] | 327 | } |
---|
[d754b7] | 328 | |
---|
| 329 | if(SI_LINK_W_OPEN_P(l)) |
---|
[286bd57] | 330 | { // now open w |
---|
[1fc83c0] | 331 | if (l->m->Dump != NULL) |
---|
| 332 | res = l->m->Dump(l); |
---|
| 333 | else |
---|
| 334 | res = TRUE; |
---|
[286bd57] | 335 | |
---|
[d754b7] | 336 | if (res) |
---|
| 337 | Werror("dump: Error for link of type %s, mode: %s, name: %s", |
---|
| 338 | l->m->type, l->mode, l->name); |
---|
| 339 | return res; |
---|
[286bd57] | 340 | } |
---|
[d754b7] | 341 | else |
---|
| 342 | { |
---|
| 343 | Werror("dump: Error to open link of type %s, mode: %s, name: %s for writing", |
---|
| 344 | l->m->type, l->mode, l->name); |
---|
| 345 | return TRUE; |
---|
[286bd57] | 346 | } |
---|
| 347 | } |
---|
| 348 | |
---|
[d754b7] | 349 | BOOLEAN slGetDump(si_link l) |
---|
[0e1846] | 350 | { |
---|
[d754b7] | 351 | BOOLEAN res; |
---|
| 352 | |
---|
| 353 | if(! SI_LINK_R_OPEN_P(l)) // open r ? |
---|
[0e1846] | 354 | { |
---|
[b5f276e] | 355 | if (slOpen(l, SI_LINK_READ,NULL)) return TRUE; |
---|
[0e1846] | 356 | } |
---|
[d754b7] | 357 | |
---|
| 358 | if(SI_LINK_R_OPEN_P(l)) |
---|
| 359 | { // now open r |
---|
[1fc83c0] | 360 | if (l->m->GetDump != NULL) |
---|
| 361 | res = l->m->GetDump(l); |
---|
| 362 | else |
---|
| 363 | res = TRUE; |
---|
[d754b7] | 364 | |
---|
| 365 | if (res) |
---|
| 366 | Werror("getdump: Error for link of type %s, mode: %s, name: %s", |
---|
| 367 | l->m->type, l->mode, l->name); |
---|
[6d1357] | 368 | //res|=slClose(l); |
---|
[d754b7] | 369 | return res; |
---|
[0e1846] | 370 | } |
---|
[d754b7] | 371 | else |
---|
[0e1846] | 372 | { |
---|
[d754b7] | 373 | Werror("dump: Error open link of type %s, mode: %s, name: %s for reading", |
---|
| 374 | l->m->type, l->mode, l->name); |
---|
| 375 | return TRUE; |
---|
[0e1846] | 376 | } |
---|
| 377 | } |
---|
[d754b7] | 378 | |
---|
| 379 | |
---|
| 380 | /* =============== ASCII ============================================= */ |
---|
[b5f276e] | 381 | BOOLEAN slOpenAscii(si_link l, short flag, leftv h) |
---|
[0e1846] | 382 | { |
---|
[85e68dd] | 383 | const char *mode; |
---|
[1fc83c0] | 384 | if (flag & SI_LINK_OPEN) |
---|
[0e1846] | 385 | { |
---|
[d754b7] | 386 | if (l->mode[0] != '\0' && (strcmp(l->mode, "r") == 0)) |
---|
| 387 | flag = SI_LINK_READ; |
---|
| 388 | else flag = SI_LINK_WRITE; |
---|
[0e1846] | 389 | } |
---|
[d754b7] | 390 | |
---|
| 391 | if (flag == SI_LINK_READ) mode = "r"; |
---|
| 392 | else if (strcmp(l->mode, "w") == 0) mode = "w"; |
---|
| 393 | else mode = "a"; |
---|
[c20843] | 394 | |
---|
| 395 | |
---|
[d754b7] | 396 | if (l->name[0] == '\0') |
---|
[0e1846] | 397 | { |
---|
[d754b7] | 398 | // stdin or stdout |
---|
| 399 | if (flag == SI_LINK_READ) |
---|
[0e1846] | 400 | { |
---|
[d754b7] | 401 | l->data = (void *) stdin; |
---|
| 402 | mode = "r"; |
---|
[0e1846] | 403 | } |
---|
[d754b7] | 404 | else |
---|
[0e1846] | 405 | { |
---|
[d754b7] | 406 | l->data = (void *) stdout; |
---|
| 407 | mode = "a"; |
---|
[0e1846] | 408 | } |
---|
[d754b7] | 409 | } |
---|
| 410 | else |
---|
| 411 | { |
---|
| 412 | // normal ascii link to a file |
---|
| 413 | FILE *outfile; |
---|
| 414 | char *filename=l->name; |
---|
| 415 | |
---|
| 416 | if(filename[0]=='>') |
---|
[0e1846] | 417 | { |
---|
[d754b7] | 418 | if (filename[1]=='>') |
---|
| 419 | { |
---|
| 420 | filename+=2; |
---|
| 421 | mode = "a"; |
---|
| 422 | } |
---|
| 423 | else |
---|
| 424 | { |
---|
| 425 | filename++; |
---|
| 426 | mode="w"; |
---|
| 427 | } |
---|
[0e1846] | 428 | } |
---|
[64c6d1] | 429 | outfile=myfopen(filename,mode); |
---|
[1fc83c0] | 430 | if (outfile!=NULL) |
---|
| 431 | l->data = (void *) outfile; |
---|
| 432 | else |
---|
| 433 | return TRUE; |
---|
[0e1846] | 434 | } |
---|
[d754b7] | 435 | |
---|
[c232af] | 436 | omFree(l->mode); |
---|
| 437 | l->mode = omStrDup(mode); |
---|
[d754b7] | 438 | SI_LINK_SET_OPEN_P(l, flag); |
---|
[0e1846] | 439 | return FALSE; |
---|
| 440 | } |
---|
[d754b7] | 441 | |
---|
[0e1846] | 442 | BOOLEAN slCloseAscii(si_link l) |
---|
| 443 | { |
---|
| 444 | SI_LINK_SET_CLOSE_P(l); |
---|
[d754b7] | 445 | if (l->name[0] != '\0') |
---|
[0e1846] | 446 | { |
---|
| 447 | return (fclose((FILE *)l->data)!=0); |
---|
| 448 | } |
---|
| 449 | return FALSE; |
---|
| 450 | } |
---|
[d754b7] | 451 | |
---|
[472f39] | 452 | leftv slReadAscii2(si_link l, leftv pr) |
---|
[0e1846] | 453 | { |
---|
| 454 | FILE * fp=(FILE *)l->data; |
---|
| 455 | char * buf=NULL; |
---|
[d754b7] | 456 | if (fp!=NULL && l->name[0] != '\0') |
---|
[0e1846] | 457 | { |
---|
| 458 | fseek(fp,0L,SEEK_END); |
---|
| 459 | long len=ftell(fp); |
---|
| 460 | fseek(fp,0L,SEEK_SET); |
---|
[c232af] | 461 | buf=(char *)omAlloc((int)len+1); |
---|
[1fc83c0] | 462 | if (BVERBOSE(V_READING)) |
---|
[d3a49c] | 463 | Print("//Reading %ld chars\n",len); |
---|
[d5f35ac] | 464 | myfread( buf, len, 1, fp); |
---|
[0e1846] | 465 | buf[len]='\0'; |
---|
| 466 | } |
---|
| 467 | else |
---|
| 468 | { |
---|
[da8702] | 469 | if (pr->Typ()==STRING_CMD) |
---|
[c20843] | 470 | { |
---|
[da8702] | 471 | buf=(char *)omAlloc(80); |
---|
| 472 | fe_fgets_stdin((char *)pr->Data(),buf,80); |
---|
[c20843] | 473 | } |
---|
| 474 | else |
---|
| 475 | { |
---|
[da8702] | 476 | WerrorS("read(<link>,<string>) expected"); |
---|
| 477 | buf=omStrDup(""); |
---|
[c20843] | 478 | } |
---|
[0e1846] | 479 | } |
---|
[c232af] | 480 | leftv v=(leftv)omAlloc0Bin(sleftv_bin); |
---|
[0e1846] | 481 | v->rtyp=STRING_CMD; |
---|
| 482 | v->data=buf; |
---|
| 483 | return v; |
---|
| 484 | } |
---|
[472f39] | 485 | |
---|
| 486 | leftv slReadAscii(si_link l) |
---|
| 487 | { |
---|
| 488 | sleftv tmp; |
---|
| 489 | memset(&tmp,0,sizeof(sleftv)); |
---|
| 490 | tmp.rtyp=STRING_CMD; |
---|
[a70441f] | 491 | tmp.data=(void*) "? "; |
---|
[472f39] | 492 | return slReadAscii2(l,&tmp); |
---|
| 493 | } |
---|
| 494 | |
---|
[0e1846] | 495 | BOOLEAN slWriteAscii(si_link l, leftv v) |
---|
| 496 | { |
---|
| 497 | FILE *outfile=(FILE *)l->data; |
---|
| 498 | BOOLEAN err=FALSE; |
---|
| 499 | char *s; |
---|
| 500 | while (v!=NULL) |
---|
| 501 | { |
---|
| 502 | s = v->String(); |
---|
| 503 | // free v ?? |
---|
| 504 | if (s!=NULL) |
---|
| 505 | { |
---|
| 506 | fprintf(outfile,"%s\n",s); |
---|
[c232af] | 507 | omFree((ADDRESS)s); |
---|
[0e1846] | 508 | } |
---|
| 509 | else |
---|
| 510 | { |
---|
| 511 | Werror("cannot convert to string"); |
---|
| 512 | err=TRUE; |
---|
| 513 | } |
---|
| 514 | v = v->next; |
---|
| 515 | } |
---|
| 516 | fflush(outfile); |
---|
| 517 | return err; |
---|
| 518 | } |
---|
| 519 | |
---|
[9db0567] | 520 | const char* slStatusAscii(si_link l, const char* request) |
---|
[0e1846] | 521 | { |
---|
[d754b7] | 522 | if (strcmp(request, "read") == 0) |
---|
[0e1846] | 523 | { |
---|
[d754b7] | 524 | if (SI_LINK_R_OPEN_P(l)) return "ready"; |
---|
| 525 | else return "not ready"; |
---|
| 526 | } |
---|
| 527 | else if (strcmp(request, "write") == 0) |
---|
| 528 | { |
---|
| 529 | if (SI_LINK_W_OPEN_P(l)) return "ready"; |
---|
| 530 | else return "not ready"; |
---|
[0e1846] | 531 | } |
---|
[d754b7] | 532 | else return "unknown status request"; |
---|
[0e1846] | 533 | } |
---|
| 534 | |
---|
[d754b7] | 535 | /*------------------ Dumping in Ascii format -----------------------*/ |
---|
| 536 | |
---|
[286bd57] | 537 | BOOLEAN slDumpAscii(si_link l) |
---|
| 538 | { |
---|
| 539 | FILE *fd = (FILE *) l->data; |
---|
[46d09b] | 540 | idhdl h = IDROOT, rh = currRingHdl; |
---|
[286bd57] | 541 | BOOLEAN status = DumpAscii(fd, h); |
---|
| 542 | |
---|
[d754b7] | 543 | if (! status ) status = DumpAsciiMaps(fd, h, NULL); |
---|
| 544 | |
---|
[cf42ab1] | 545 | if (currRingHdl != rh) rSetHdl(rh); |
---|
[a6904c] | 546 | fprintf(fd, "option(set, intvec(%d, %d));\n", test, verbose); |
---|
[97454d] | 547 | fprintf(fd, "RETURN();\n"); |
---|
[286bd57] | 548 | fflush(fd); |
---|
[c20843] | 549 | |
---|
[286bd57] | 550 | return status; |
---|
| 551 | } |
---|
| 552 | |
---|
[c20843] | 553 | // we do that recursively, to dump ids in the the order in which they |
---|
[286bd57] | 554 | // were actually defined |
---|
| 555 | static BOOLEAN DumpAscii(FILE *fd, idhdl h) |
---|
| 556 | { |
---|
| 557 | if (h == NULL) return FALSE; |
---|
| 558 | |
---|
| 559 | if (DumpAscii(fd, IDNEXT(h))) return TRUE; |
---|
| 560 | |
---|
[d754b7] | 561 | // need to set the ring before writing it, otherwise we get in |
---|
[286bd57] | 562 | // trouble with minpoly |
---|
| 563 | if (IDTYP(h) == RING_CMD || IDTYP(h) == QRING_CMD) |
---|
[cf42ab1] | 564 | rSetHdl(h); |
---|
[286bd57] | 565 | |
---|
| 566 | if (DumpAsciiIdhdl(fd, h)) return TRUE; |
---|
| 567 | |
---|
| 568 | if (IDTYP(h) == RING_CMD || IDTYP(h) == QRING_CMD) |
---|
| 569 | return DumpAscii(fd, IDRING(h)->idroot); |
---|
| 570 | else |
---|
| 571 | return FALSE; |
---|
| 572 | } |
---|
| 573 | |
---|
[d754b7] | 574 | static BOOLEAN DumpAsciiMaps(FILE *fd, idhdl h, idhdl rhdl) |
---|
| 575 | { |
---|
| 576 | if (h == NULL) return FALSE; |
---|
| 577 | if (DumpAsciiMaps(fd, IDNEXT(h), rhdl)) return TRUE; |
---|
| 578 | |
---|
| 579 | if (IDTYP(h) == RING_CMD || IDTYP(h) == QRING_CMD) |
---|
| 580 | return DumpAsciiMaps(fd, IDRING(h)->idroot, h); |
---|
| 581 | else if (IDTYP(h) == MAP_CMD) |
---|
| 582 | { |
---|
| 583 | char *rhs; |
---|
[cf42ab1] | 584 | rSetHdl(rhdl); |
---|
[472f39] | 585 | rhs = h->String(); |
---|
[c20843] | 586 | |
---|
[d754b7] | 587 | if (fprintf(fd, "setring %s;\n", IDID(rhdl)) == EOF) return TRUE; |
---|
| 588 | if (fprintf(fd, "%s %s = %s, %s;\n", Tok2Cmdname(MAP_CMD), IDID(h), |
---|
| 589 | IDMAP(h)->preimage, rhs) == EOF) |
---|
| 590 | { |
---|
[c232af] | 591 | omFree(rhs); |
---|
[d754b7] | 592 | return TRUE; |
---|
| 593 | } |
---|
| 594 | else |
---|
| 595 | { |
---|
[c232af] | 596 | omFree(rhs); |
---|
[d754b7] | 597 | return FALSE; |
---|
| 598 | } |
---|
| 599 | } |
---|
| 600 | else return FALSE; |
---|
| 601 | } |
---|
[c20843] | 602 | |
---|
[286bd57] | 603 | static BOOLEAN DumpAsciiIdhdl(FILE *fd, idhdl h) |
---|
| 604 | { |
---|
[85e68dd] | 605 | const char *type_str = GetIdString(h); |
---|
[e813875] | 606 | int type_id = IDTYP(h); |
---|
[286bd57] | 607 | |
---|
[a3bc95e] | 608 | if ((type_id == PACKAGE_CMD) &&(strcmp(IDID(h), "Top") == 0)) |
---|
| 609 | return FALSE; |
---|
[472f39] | 610 | |
---|
[286bd57] | 611 | // we do not throw an error if a wrong type was attempted to be dumped |
---|
[472f39] | 612 | if (type_str == NULL) |
---|
| 613 | return FALSE; |
---|
[286bd57] | 614 | |
---|
[b7808e] | 615 | // handle qrings separately |
---|
[472f39] | 616 | if (type_id == QRING_CMD) |
---|
| 617 | return DumpQring(fd, h, type_str); |
---|
[c20843] | 618 | |
---|
[1cb879] | 619 | // C-proc not to be dumped |
---|
| 620 | if ((type_id == PROC_CMD) && (IDPROC(h)->language == LANG_C)) |
---|
| 621 | return FALSE; |
---|
| 622 | |
---|
[e1af45] | 623 | // put type and name |
---|
[472f39] | 624 | if (fprintf(fd, "%s %s", type_str, IDID(h)) == EOF) |
---|
| 625 | return TRUE; |
---|
[286bd57] | 626 | // for matricies, append the dimension |
---|
| 627 | if (type_id == MATRIX_CMD) |
---|
| 628 | { |
---|
| 629 | ideal id = IDIDEAL(h); |
---|
| 630 | if (fprintf(fd, "[%d][%d]", id->nrows, id->ncols)== EOF) return TRUE; |
---|
| 631 | } |
---|
[d754b7] | 632 | else if (type_id == INTMAT_CMD) |
---|
[286bd57] | 633 | { |
---|
[d754b7] | 634 | if (fprintf(fd, "[%d][%d]", IDINTVEC(h)->rows(), IDINTVEC(h)->cols()) |
---|
| 635 | == EOF) return TRUE; |
---|
[286bd57] | 636 | } |
---|
[0a3ddd] | 637 | |
---|
[a3bc95e] | 638 | if (type_id == PACKAGE_CMD) |
---|
| 639 | { |
---|
| 640 | return (fprintf(fd, ";\n") == EOF); |
---|
| 641 | } |
---|
[472f39] | 642 | |
---|
[d754b7] | 643 | // write the equal sign |
---|
| 644 | if (fprintf(fd, " = ") == EOF) return TRUE; |
---|
| 645 | |
---|
| 646 | // and the right hand side |
---|
| 647 | if (DumpRhs(fd, h) == EOF) return TRUE; |
---|
[c20843] | 648 | |
---|
[286bd57] | 649 | // semicolon und tschuess |
---|
| 650 | if (fprintf(fd, ";\n") == EOF) return TRUE; |
---|
| 651 | |
---|
| 652 | return FALSE; |
---|
| 653 | } |
---|
| 654 | |
---|
[85e68dd] | 655 | static const char* GetIdString(idhdl h) |
---|
[286bd57] | 656 | { |
---|
[e813875] | 657 | int type = IDTYP(h); |
---|
[c20843] | 658 | |
---|
[286bd57] | 659 | switch(type) |
---|
| 660 | { |
---|
| 661 | case LIST_CMD: |
---|
| 662 | { |
---|
| 663 | lists l = IDLIST(h); |
---|
| 664 | int i, nl = l->nr + 1; |
---|
| 665 | char *name; |
---|
| 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 | { |
---|
| 740 | char *pstr = IDSTRING(h), c; |
---|
| 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); |
---|
| 753 | if (pi->language == LANG_SINGULAR) { |
---|
| 754 | if( pi->data.s.body==NULL) iiGetLibProcBuffer(pi); |
---|
| 755 | char *pstr = pi->data.s.body, c; |
---|
| 756 | fputc('"', fd); |
---|
| 757 | while (*pstr != '\0') { |
---|
[97454d] | 758 | if (*pstr == '"' || *pstr == '\\') fputc('\\', fd); |
---|
| 759 | fputc(*pstr, fd); |
---|
| 760 | pstr++; |
---|
[2ba9a6] | 761 | } |
---|
| 762 | fputc('"', fd); |
---|
| 763 | } else fputs("(null)", fd); |
---|
| 764 | } |
---|
[286bd57] | 765 | else |
---|
| 766 | { |
---|
[472f39] | 767 | char *rhs = h->String(); |
---|
[286bd57] | 768 | |
---|
| 769 | if (rhs == NULL) return EOF; |
---|
| 770 | |
---|
[d754b7] | 771 | if (type_id == INTVEC_CMD) fprintf(fd, "intvec("); |
---|
[286bd57] | 772 | |
---|
| 773 | if (fprintf(fd, "%s", rhs) == EOF) return EOF; |
---|
[c232af] | 774 | omFree(rhs); |
---|
[286bd57] | 775 | |
---|
| 776 | if ((type_id == RING_CMD || type_id == QRING_CMD) && |
---|
[7b2b05] | 777 | IDRING(h)->cf->type==n_algExt) |
---|
[286bd57] | 778 | { |
---|
| 779 | StringSetS(""); |
---|
[5ad2f8] | 780 | p_Write(IDRING(h)->cf->extRing->minideal->m[0],IDRING(h)->cf->extRing); |
---|
[b45d97] | 781 | rhs = StringAppendS(""); |
---|
[286bd57] | 782 | if (fprintf(fd, "; minpoly = %s", rhs) == EOF) return EOF; |
---|
| 783 | } |
---|
[d754b7] | 784 | else if (type_id == INTVEC_CMD) fprintf(fd, ")"); |
---|
[286bd57] | 785 | } |
---|
| 786 | return 1; |
---|
| 787 | } |
---|
[c20843] | 788 | |
---|
[286bd57] | 789 | BOOLEAN slGetDumpAscii(si_link l) |
---|
| 790 | { |
---|
| 791 | if (l->name[0] == '\0') |
---|
| 792 | { |
---|
[d754b7] | 793 | Werror("getdump: Can not get dump from stdin"); |
---|
[286bd57] | 794 | return TRUE; |
---|
| 795 | } |
---|
| 796 | else |
---|
| 797 | { |
---|
[057e93c] | 798 | BOOLEAN status = newFile(l->name); |
---|
| 799 | if (status) |
---|
| 800 | return TRUE; |
---|
[c20843] | 801 | |
---|
[a18fae] | 802 | int old_echo=si_echo; |
---|
| 803 | si_echo=0; |
---|
| 804 | |
---|
[057e93c] | 805 | status=yyparse(); |
---|
[c20843] | 806 | |
---|
[a18fae] | 807 | si_echo=old_echo; |
---|
[286bd57] | 808 | |
---|
| 809 | if (status) |
---|
| 810 | return TRUE; |
---|
| 811 | else |
---|
| 812 | { |
---|
[d754b7] | 813 | // lets reset the file pointer to the end to reflect that |
---|
[286bd57] | 814 | // we are finished with reading |
---|
| 815 | FILE *f = (FILE *) l->data; |
---|
| 816 | fseek(f, 0L, SEEK_END); |
---|
| 817 | return FALSE; |
---|
| 818 | } |
---|
| 819 | } |
---|
| 820 | } |
---|
[c20843] | 821 | |
---|
| 822 | |
---|
[d754b7] | 823 | /*------------Initialization at Start-up time------------------------*/ |
---|
[0e1846] | 824 | |
---|
[599326] | 825 | #include <Singular/slInit.h> |
---|
[0e1846] | 826 | |
---|
[6b32990] | 827 | static si_link_extension slTypeInit(si_link_extension s, const char* type) |
---|
| 828 | { |
---|
| 829 | assume(s != NULL); |
---|
| 830 | s->next = NULL; |
---|
| 831 | si_link_extension ns = (si_link_extension)omAlloc0Bin(s_si_link_extension_bin); |
---|
[50cbdc] | 832 | |
---|
[8ed989] | 833 | if (0) 0; |
---|
[0e1846] | 834 | #ifdef HAVE_MPSR |
---|
[8ed989] | 835 | else if (strcmp(type, "MPfile") == 0) |
---|
[6b32990] | 836 | s->next = slInitMPFileExtension(ns); |
---|
| 837 | else if (strcmp(type, "MPtcp") == 0) |
---|
| 838 | s->next = slInitMPTcpExtension(ns); |
---|
| 839 | #endif |
---|
| 840 | #ifdef HAVE_DBM |
---|
[8ed989] | 841 | else if (strcmp(type, "DBM") == 0) |
---|
[6b32990] | 842 | s->next = slInitDBMExtension(ns); |
---|
| 843 | #endif |
---|
[8ed989] | 844 | #if 1 |
---|
| 845 | else if (strcmp(type, "ssi") == 0) |
---|
| 846 | s->next = slInitSsiExtension(ns); |
---|
[4082e8] | 847 | #endif |
---|
| 848 | #if 1 |
---|
| 849 | else if (strcmp(type, "|") == 0) |
---|
| 850 | s->next = slInitPipeExtension(ns); |
---|
[c20843] | 851 | #endif |
---|
[8ed989] | 852 | else |
---|
[6b32990] | 853 | { |
---|
| 854 | Warn("Found unknown link type: %s", type); |
---|
| 855 | Warn("Use default link type: %s", si_link_root->type); |
---|
| 856 | omFreeBin(ns, s_si_link_extension_bin); |
---|
| 857 | return si_link_root; |
---|
| 858 | } |
---|
[50cbdc] | 859 | |
---|
[6b32990] | 860 | if (s->next == NULL) |
---|
| 861 | { |
---|
| 862 | Werror("Can not initialize link type %s", type); |
---|
| 863 | omFreeBin(ns, s_si_link_extension_bin); |
---|
| 864 | return NULL; |
---|
| 865 | } |
---|
| 866 | return s->next; |
---|
| 867 | } |
---|
[0e1846] | 868 | |
---|
| 869 | void slStandardInit() |
---|
| 870 | { |
---|
[d754b7] | 871 | si_link_extension s; |
---|
[c232af] | 872 | si_link_root=(si_link_extension)omAlloc0Bin(s_si_link_extension_bin); |
---|
[d754b7] | 873 | si_link_root->Open=slOpenAscii; |
---|
[0e1846] | 874 | si_link_root->Close=slCloseAscii; |
---|
[80419f] | 875 | si_link_root->Kill=slCloseAscii; |
---|
[0e1846] | 876 | si_link_root->Read=slReadAscii; |
---|
[472f39] | 877 | si_link_root->Read2=slReadAscii2; |
---|
[0e1846] | 878 | si_link_root->Write=slWriteAscii; |
---|
[286bd57] | 879 | si_link_root->Dump=slDumpAscii; |
---|
| 880 | si_link_root->GetDump=slGetDumpAscii; |
---|
[d754b7] | 881 | si_link_root->Status=slStatusAscii; |
---|
[fcf5d4] | 882 | si_link_root->type="ASCII"; |
---|
[d754b7] | 883 | s = si_link_root; |
---|
[6b32990] | 884 | s->next = NULL; |
---|
[0e1846] | 885 | } |
---|