[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); |
---|
[09afeb] | 340 | |
---|
| 341 | slClose(l); |
---|
[d754b7] | 342 | return res; |
---|
[286bd57] | 343 | } |
---|
[d754b7] | 344 | else |
---|
| 345 | { |
---|
| 346 | Werror("dump: Error to open link of type %s, mode: %s, name: %s for writing", |
---|
| 347 | l->m->type, l->mode, l->name); |
---|
| 348 | return TRUE; |
---|
[286bd57] | 349 | } |
---|
| 350 | } |
---|
| 351 | |
---|
[d754b7] | 352 | BOOLEAN slGetDump(si_link l) |
---|
[0e1846] | 353 | { |
---|
[d754b7] | 354 | BOOLEAN res; |
---|
| 355 | |
---|
| 356 | if(! SI_LINK_R_OPEN_P(l)) // open r ? |
---|
[0e1846] | 357 | { |
---|
[b5f276e] | 358 | if (slOpen(l, SI_LINK_READ,NULL)) return TRUE; |
---|
[0e1846] | 359 | } |
---|
[d754b7] | 360 | |
---|
| 361 | if(SI_LINK_R_OPEN_P(l)) |
---|
| 362 | { // now open r |
---|
[1fc83c0] | 363 | if (l->m->GetDump != NULL) |
---|
| 364 | res = l->m->GetDump(l); |
---|
| 365 | else |
---|
| 366 | res = TRUE; |
---|
[d754b7] | 367 | |
---|
| 368 | if (res) |
---|
| 369 | Werror("getdump: Error for link of type %s, mode: %s, name: %s", |
---|
| 370 | l->m->type, l->mode, l->name); |
---|
[6d1357] | 371 | //res|=slClose(l); |
---|
[d754b7] | 372 | return res; |
---|
[0e1846] | 373 | } |
---|
[d754b7] | 374 | else |
---|
[0e1846] | 375 | { |
---|
[d754b7] | 376 | Werror("dump: Error open link of type %s, mode: %s, name: %s for reading", |
---|
| 377 | l->m->type, l->mode, l->name); |
---|
| 378 | return TRUE; |
---|
[0e1846] | 379 | } |
---|
| 380 | } |
---|
[d754b7] | 381 | |
---|
| 382 | |
---|
| 383 | /* =============== ASCII ============================================= */ |
---|
[b5f276e] | 384 | BOOLEAN slOpenAscii(si_link l, short flag, leftv h) |
---|
[0e1846] | 385 | { |
---|
[85e68dd] | 386 | const char *mode; |
---|
[1fc83c0] | 387 | if (flag & SI_LINK_OPEN) |
---|
[0e1846] | 388 | { |
---|
[d754b7] | 389 | if (l->mode[0] != '\0' && (strcmp(l->mode, "r") == 0)) |
---|
| 390 | flag = SI_LINK_READ; |
---|
| 391 | else flag = SI_LINK_WRITE; |
---|
[0e1846] | 392 | } |
---|
[d754b7] | 393 | |
---|
| 394 | if (flag == SI_LINK_READ) mode = "r"; |
---|
| 395 | else if (strcmp(l->mode, "w") == 0) mode = "w"; |
---|
| 396 | else mode = "a"; |
---|
[c20843] | 397 | |
---|
| 398 | |
---|
[d754b7] | 399 | if (l->name[0] == '\0') |
---|
[0e1846] | 400 | { |
---|
[d754b7] | 401 | // stdin or stdout |
---|
| 402 | if (flag == SI_LINK_READ) |
---|
[0e1846] | 403 | { |
---|
[d754b7] | 404 | l->data = (void *) stdin; |
---|
| 405 | mode = "r"; |
---|
[0e1846] | 406 | } |
---|
[d754b7] | 407 | else |
---|
[0e1846] | 408 | { |
---|
[d754b7] | 409 | l->data = (void *) stdout; |
---|
| 410 | mode = "a"; |
---|
[0e1846] | 411 | } |
---|
[d754b7] | 412 | } |
---|
| 413 | else |
---|
| 414 | { |
---|
| 415 | // normal ascii link to a file |
---|
| 416 | FILE *outfile; |
---|
| 417 | char *filename=l->name; |
---|
| 418 | |
---|
| 419 | if(filename[0]=='>') |
---|
[0e1846] | 420 | { |
---|
[d754b7] | 421 | if (filename[1]=='>') |
---|
| 422 | { |
---|
| 423 | filename+=2; |
---|
| 424 | mode = "a"; |
---|
| 425 | } |
---|
| 426 | else |
---|
| 427 | { |
---|
| 428 | filename++; |
---|
| 429 | mode="w"; |
---|
| 430 | } |
---|
[0e1846] | 431 | } |
---|
[64c6d1] | 432 | outfile=myfopen(filename,mode); |
---|
[1fc83c0] | 433 | if (outfile!=NULL) |
---|
| 434 | l->data = (void *) outfile; |
---|
| 435 | else |
---|
| 436 | return TRUE; |
---|
[0e1846] | 437 | } |
---|
[d754b7] | 438 | |
---|
[c232af] | 439 | omFree(l->mode); |
---|
| 440 | l->mode = omStrDup(mode); |
---|
[d754b7] | 441 | SI_LINK_SET_OPEN_P(l, flag); |
---|
[0e1846] | 442 | return FALSE; |
---|
| 443 | } |
---|
[d754b7] | 444 | |
---|
[0e1846] | 445 | BOOLEAN slCloseAscii(si_link l) |
---|
| 446 | { |
---|
| 447 | SI_LINK_SET_CLOSE_P(l); |
---|
[d754b7] | 448 | if (l->name[0] != '\0') |
---|
[0e1846] | 449 | { |
---|
| 450 | return (fclose((FILE *)l->data)!=0); |
---|
| 451 | } |
---|
| 452 | return FALSE; |
---|
| 453 | } |
---|
[d754b7] | 454 | |
---|
[472f39] | 455 | leftv slReadAscii2(si_link l, leftv pr) |
---|
[0e1846] | 456 | { |
---|
| 457 | FILE * fp=(FILE *)l->data; |
---|
| 458 | char * buf=NULL; |
---|
[d754b7] | 459 | if (fp!=NULL && l->name[0] != '\0') |
---|
[0e1846] | 460 | { |
---|
| 461 | fseek(fp,0L,SEEK_END); |
---|
| 462 | long len=ftell(fp); |
---|
| 463 | fseek(fp,0L,SEEK_SET); |
---|
[c232af] | 464 | buf=(char *)omAlloc((int)len+1); |
---|
[1fc83c0] | 465 | if (BVERBOSE(V_READING)) |
---|
[d3a49c] | 466 | Print("//Reading %ld chars\n",len); |
---|
[d5f35ac] | 467 | myfread( buf, len, 1, fp); |
---|
[0e1846] | 468 | buf[len]='\0'; |
---|
| 469 | } |
---|
| 470 | else |
---|
| 471 | { |
---|
[da8702] | 472 | if (pr->Typ()==STRING_CMD) |
---|
[c20843] | 473 | { |
---|
[da8702] | 474 | buf=(char *)omAlloc(80); |
---|
| 475 | fe_fgets_stdin((char *)pr->Data(),buf,80); |
---|
[c20843] | 476 | } |
---|
| 477 | else |
---|
| 478 | { |
---|
[da8702] | 479 | WerrorS("read(<link>,<string>) expected"); |
---|
| 480 | buf=omStrDup(""); |
---|
[c20843] | 481 | } |
---|
[0e1846] | 482 | } |
---|
[c232af] | 483 | leftv v=(leftv)omAlloc0Bin(sleftv_bin); |
---|
[0e1846] | 484 | v->rtyp=STRING_CMD; |
---|
| 485 | v->data=buf; |
---|
| 486 | return v; |
---|
| 487 | } |
---|
[472f39] | 488 | |
---|
| 489 | leftv slReadAscii(si_link l) |
---|
| 490 | { |
---|
| 491 | sleftv tmp; |
---|
| 492 | memset(&tmp,0,sizeof(sleftv)); |
---|
| 493 | tmp.rtyp=STRING_CMD; |
---|
[a70441f] | 494 | tmp.data=(void*) "? "; |
---|
[472f39] | 495 | return slReadAscii2(l,&tmp); |
---|
| 496 | } |
---|
| 497 | |
---|
[0e1846] | 498 | BOOLEAN slWriteAscii(si_link l, leftv v) |
---|
| 499 | { |
---|
| 500 | FILE *outfile=(FILE *)l->data; |
---|
| 501 | BOOLEAN err=FALSE; |
---|
| 502 | char *s; |
---|
| 503 | while (v!=NULL) |
---|
| 504 | { |
---|
| 505 | s = v->String(); |
---|
| 506 | // free v ?? |
---|
| 507 | if (s!=NULL) |
---|
| 508 | { |
---|
| 509 | fprintf(outfile,"%s\n",s); |
---|
[c232af] | 510 | omFree((ADDRESS)s); |
---|
[0e1846] | 511 | } |
---|
| 512 | else |
---|
| 513 | { |
---|
| 514 | Werror("cannot convert to string"); |
---|
| 515 | err=TRUE; |
---|
| 516 | } |
---|
| 517 | v = v->next; |
---|
| 518 | } |
---|
| 519 | fflush(outfile); |
---|
| 520 | return err; |
---|
| 521 | } |
---|
| 522 | |
---|
[9db0567] | 523 | const char* slStatusAscii(si_link l, const char* request) |
---|
[0e1846] | 524 | { |
---|
[d754b7] | 525 | if (strcmp(request, "read") == 0) |
---|
[0e1846] | 526 | { |
---|
[d754b7] | 527 | if (SI_LINK_R_OPEN_P(l)) return "ready"; |
---|
| 528 | else return "not ready"; |
---|
| 529 | } |
---|
| 530 | else if (strcmp(request, "write") == 0) |
---|
| 531 | { |
---|
| 532 | if (SI_LINK_W_OPEN_P(l)) return "ready"; |
---|
| 533 | else return "not ready"; |
---|
[0e1846] | 534 | } |
---|
[d754b7] | 535 | else return "unknown status request"; |
---|
[0e1846] | 536 | } |
---|
| 537 | |
---|
[d754b7] | 538 | /*------------------ Dumping in Ascii format -----------------------*/ |
---|
| 539 | |
---|
[286bd57] | 540 | BOOLEAN slDumpAscii(si_link l) |
---|
| 541 | { |
---|
| 542 | FILE *fd = (FILE *) l->data; |
---|
[46d09b] | 543 | idhdl h = IDROOT, rh = currRingHdl; |
---|
[286bd57] | 544 | BOOLEAN status = DumpAscii(fd, h); |
---|
| 545 | |
---|
[d754b7] | 546 | if (! status ) status = DumpAsciiMaps(fd, h, NULL); |
---|
| 547 | |
---|
[cf42ab1] | 548 | if (currRingHdl != rh) rSetHdl(rh); |
---|
[a6904c] | 549 | fprintf(fd, "option(set, intvec(%d, %d));\n", test, verbose); |
---|
[97454d] | 550 | fprintf(fd, "RETURN();\n"); |
---|
[286bd57] | 551 | fflush(fd); |
---|
[c20843] | 552 | |
---|
[286bd57] | 553 | return status; |
---|
| 554 | } |
---|
| 555 | |
---|
[c20843] | 556 | // we do that recursively, to dump ids in the the order in which they |
---|
[286bd57] | 557 | // were actually defined |
---|
| 558 | static BOOLEAN DumpAscii(FILE *fd, idhdl h) |
---|
| 559 | { |
---|
| 560 | if (h == NULL) return FALSE; |
---|
| 561 | |
---|
| 562 | if (DumpAscii(fd, IDNEXT(h))) return TRUE; |
---|
| 563 | |
---|
[d754b7] | 564 | // need to set the ring before writing it, otherwise we get in |
---|
[286bd57] | 565 | // trouble with minpoly |
---|
| 566 | if (IDTYP(h) == RING_CMD || IDTYP(h) == QRING_CMD) |
---|
[cf42ab1] | 567 | rSetHdl(h); |
---|
[286bd57] | 568 | |
---|
| 569 | if (DumpAsciiIdhdl(fd, h)) return TRUE; |
---|
| 570 | |
---|
| 571 | if (IDTYP(h) == RING_CMD || IDTYP(h) == QRING_CMD) |
---|
| 572 | return DumpAscii(fd, IDRING(h)->idroot); |
---|
| 573 | else |
---|
| 574 | return FALSE; |
---|
| 575 | } |
---|
| 576 | |
---|
[d754b7] | 577 | static BOOLEAN DumpAsciiMaps(FILE *fd, idhdl h, idhdl rhdl) |
---|
| 578 | { |
---|
| 579 | if (h == NULL) return FALSE; |
---|
| 580 | if (DumpAsciiMaps(fd, IDNEXT(h), rhdl)) return TRUE; |
---|
| 581 | |
---|
| 582 | if (IDTYP(h) == RING_CMD || IDTYP(h) == QRING_CMD) |
---|
| 583 | return DumpAsciiMaps(fd, IDRING(h)->idroot, h); |
---|
| 584 | else if (IDTYP(h) == MAP_CMD) |
---|
| 585 | { |
---|
| 586 | char *rhs; |
---|
[cf42ab1] | 587 | rSetHdl(rhdl); |
---|
[472f39] | 588 | rhs = h->String(); |
---|
[c20843] | 589 | |
---|
[d754b7] | 590 | if (fprintf(fd, "setring %s;\n", IDID(rhdl)) == EOF) return TRUE; |
---|
| 591 | if (fprintf(fd, "%s %s = %s, %s;\n", Tok2Cmdname(MAP_CMD), IDID(h), |
---|
| 592 | IDMAP(h)->preimage, rhs) == EOF) |
---|
| 593 | { |
---|
[c232af] | 594 | omFree(rhs); |
---|
[d754b7] | 595 | return TRUE; |
---|
| 596 | } |
---|
| 597 | else |
---|
| 598 | { |
---|
[c232af] | 599 | omFree(rhs); |
---|
[d754b7] | 600 | return FALSE; |
---|
| 601 | } |
---|
| 602 | } |
---|
| 603 | else return FALSE; |
---|
| 604 | } |
---|
[c20843] | 605 | |
---|
[286bd57] | 606 | static BOOLEAN DumpAsciiIdhdl(FILE *fd, idhdl h) |
---|
| 607 | { |
---|
[85e68dd] | 608 | const char *type_str = GetIdString(h); |
---|
[e813875] | 609 | int type_id = IDTYP(h); |
---|
[286bd57] | 610 | |
---|
[a3bc95e] | 611 | if ((type_id == PACKAGE_CMD) &&(strcmp(IDID(h), "Top") == 0)) |
---|
| 612 | return FALSE; |
---|
[472f39] | 613 | |
---|
[286bd57] | 614 | // we do not throw an error if a wrong type was attempted to be dumped |
---|
[472f39] | 615 | if (type_str == NULL) |
---|
| 616 | return FALSE; |
---|
[286bd57] | 617 | |
---|
[b7808e] | 618 | // handle qrings separately |
---|
[472f39] | 619 | if (type_id == QRING_CMD) |
---|
| 620 | return DumpQring(fd, h, type_str); |
---|
[c20843] | 621 | |
---|
[1cb879] | 622 | // C-proc not to be dumped |
---|
| 623 | if ((type_id == PROC_CMD) && (IDPROC(h)->language == LANG_C)) |
---|
| 624 | return FALSE; |
---|
| 625 | |
---|
[e1af45] | 626 | // put type and name |
---|
[472f39] | 627 | if (fprintf(fd, "%s %s", type_str, IDID(h)) == EOF) |
---|
| 628 | return TRUE; |
---|
[286bd57] | 629 | // for matricies, append the dimension |
---|
| 630 | if (type_id == MATRIX_CMD) |
---|
| 631 | { |
---|
| 632 | ideal id = IDIDEAL(h); |
---|
| 633 | if (fprintf(fd, "[%d][%d]", id->nrows, id->ncols)== EOF) return TRUE; |
---|
| 634 | } |
---|
[d754b7] | 635 | else if (type_id == INTMAT_CMD) |
---|
[286bd57] | 636 | { |
---|
[d754b7] | 637 | if (fprintf(fd, "[%d][%d]", IDINTVEC(h)->rows(), IDINTVEC(h)->cols()) |
---|
| 638 | == EOF) return TRUE; |
---|
[286bd57] | 639 | } |
---|
[0a3ddd] | 640 | |
---|
[a3bc95e] | 641 | if (type_id == PACKAGE_CMD) |
---|
| 642 | { |
---|
| 643 | return (fprintf(fd, ";\n") == EOF); |
---|
| 644 | } |
---|
[472f39] | 645 | |
---|
[d754b7] | 646 | // write the equal sign |
---|
| 647 | if (fprintf(fd, " = ") == EOF) return TRUE; |
---|
| 648 | |
---|
| 649 | // and the right hand side |
---|
| 650 | if (DumpRhs(fd, h) == EOF) return TRUE; |
---|
[c20843] | 651 | |
---|
[286bd57] | 652 | // semicolon und tschuess |
---|
| 653 | if (fprintf(fd, ";\n") == EOF) return TRUE; |
---|
| 654 | |
---|
| 655 | return FALSE; |
---|
| 656 | } |
---|
| 657 | |
---|
[85e68dd] | 658 | static const char* GetIdString(idhdl h) |
---|
[286bd57] | 659 | { |
---|
[e813875] | 660 | int type = IDTYP(h); |
---|
[c20843] | 661 | |
---|
[286bd57] | 662 | switch(type) |
---|
| 663 | { |
---|
| 664 | case LIST_CMD: |
---|
| 665 | { |
---|
| 666 | lists l = IDLIST(h); |
---|
| 667 | int i, nl = l->nr + 1; |
---|
| 668 | char *name; |
---|
| 669 | |
---|
| 670 | for (i=0; i<nl; i++) |
---|
| 671 | if (GetIdString((idhdl) &(l->m[i])) == NULL) return NULL; |
---|
| 672 | } |
---|
[0a3ddd] | 673 | case PACKAGE_CMD: |
---|
[286bd57] | 674 | case INT_CMD: |
---|
| 675 | case INTVEC_CMD: |
---|
| 676 | case INTMAT_CMD: |
---|
| 677 | case STRING_CMD: |
---|
| 678 | case RING_CMD: |
---|
[b7808e] | 679 | case QRING_CMD: |
---|
[286bd57] | 680 | case PROC_CMD: |
---|
| 681 | case NUMBER_CMD: |
---|
| 682 | case POLY_CMD: |
---|
| 683 | case IDEAL_CMD: |
---|
| 684 | case VECTOR_CMD: |
---|
| 685 | case MODUL_CMD: |
---|
| 686 | case MATRIX_CMD: |
---|
| 687 | return Tok2Cmdname(type); |
---|
[c20843] | 688 | |
---|
[d754b7] | 689 | case MAP_CMD: |
---|
[286bd57] | 690 | case LINK_CMD: |
---|
| 691 | return NULL; |
---|
| 692 | |
---|
| 693 | default: |
---|
[d754b7] | 694 | Warn("Error dump data of type %s", Tok2Cmdname(IDTYP(h))); |
---|
[286bd57] | 695 | return NULL; |
---|
| 696 | } |
---|
| 697 | } |
---|
| 698 | |
---|
[85e68dd] | 699 | static BOOLEAN DumpQring(FILE *fd, idhdl h, const char *type_str) |
---|
[b7808e] | 700 | { |
---|
[472f39] | 701 | char *ring_str = h->String(); |
---|
[d754b7] | 702 | if (fprintf(fd, "%s temp_ring = %s;\n", Tok2Cmdname(RING_CMD), ring_str) |
---|
| 703 | == EOF) return TRUE; |
---|
| 704 | if (fprintf(fd, "%s temp_ideal = %s;\n", Tok2Cmdname(IDEAL_CMD), |
---|
[7b2b05] | 705 | iiStringMatrix((matrix) IDRING(h)->qideal, 1, currRing, n_GetChar(currRing->cf))) |
---|
[b7808e] | 706 | == EOF) return TRUE; |
---|
| 707 | if (fprintf(fd, "attrib(temp_ideal, \"isSB\", 1);\n") == EOF) return TRUE; |
---|
| 708 | if (fprintf(fd, "%s %s = temp_ideal;\n", type_str, IDID(h)) == EOF) |
---|
| 709 | return TRUE; |
---|
[d754b7] | 710 | if (fprintf(fd, "kill temp_ring;\n") == EOF) return TRUE; |
---|
[b7808e] | 711 | else |
---|
[d754b7] | 712 | { |
---|
[c232af] | 713 | omFree(ring_str); |
---|
[b7808e] | 714 | return FALSE; |
---|
[d754b7] | 715 | } |
---|
[b7808e] | 716 | } |
---|
| 717 | |
---|
[c20843] | 718 | |
---|
[286bd57] | 719 | static int DumpRhs(FILE *fd, idhdl h) |
---|
| 720 | { |
---|
[e813875] | 721 | int type_id = IDTYP(h); |
---|
[286bd57] | 722 | |
---|
| 723 | if (type_id == LIST_CMD) |
---|
| 724 | { |
---|
| 725 | lists l = IDLIST(h); |
---|
| 726 | int i, nl = l->nr; |
---|
| 727 | |
---|
[d754b7] | 728 | fprintf(fd, "list("); |
---|
[c20843] | 729 | |
---|
[286bd57] | 730 | for (i=0; i<nl; i++) |
---|
| 731 | { |
---|
[d754b7] | 732 | if (DumpRhs(fd, (idhdl) &(l->m[i])) == EOF) return EOF; |
---|
| 733 | fprintf(fd, ","); |
---|
[286bd57] | 734 | } |
---|
[d754b7] | 735 | if (nl > 0) |
---|
| 736 | { |
---|
| 737 | if (DumpRhs(fd, (idhdl) &(l->m[nl])) == EOF) return EOF; |
---|
[286bd57] | 738 | } |
---|
[d754b7] | 739 | fprintf(fd, ")"); |
---|
[286bd57] | 740 | } |
---|
[2ba9a6] | 741 | else if (type_id == STRING_CMD) |
---|
[286bd57] | 742 | { |
---|
| 743 | char *pstr = IDSTRING(h), c; |
---|
| 744 | fputc('"', fd); |
---|
| 745 | while (*pstr != '\0') |
---|
| 746 | { |
---|
[97454d] | 747 | if (*pstr == '"' || *pstr == '\\') fputc('\\', fd); |
---|
[286bd57] | 748 | fputc(*pstr, fd); |
---|
| 749 | pstr++; |
---|
| 750 | } |
---|
| 751 | fputc('"', fd); |
---|
| 752 | } |
---|
[2ba9a6] | 753 | else if (type_id == PROC_CMD) |
---|
[c20843] | 754 | { |
---|
[2ba9a6] | 755 | procinfov pi = IDPROC(h); |
---|
| 756 | if (pi->language == LANG_SINGULAR) { |
---|
| 757 | if( pi->data.s.body==NULL) iiGetLibProcBuffer(pi); |
---|
| 758 | char *pstr = pi->data.s.body, c; |
---|
| 759 | fputc('"', fd); |
---|
| 760 | while (*pstr != '\0') { |
---|
[97454d] | 761 | if (*pstr == '"' || *pstr == '\\') fputc('\\', fd); |
---|
| 762 | fputc(*pstr, fd); |
---|
| 763 | pstr++; |
---|
[2ba9a6] | 764 | } |
---|
| 765 | fputc('"', fd); |
---|
| 766 | } else fputs("(null)", fd); |
---|
| 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; |
---|
[0e1846] | 837 | #ifdef HAVE_MPSR |
---|
[8ed989] | 838 | else if (strcmp(type, "MPfile") == 0) |
---|
[6b32990] | 839 | s->next = slInitMPFileExtension(ns); |
---|
| 840 | else if (strcmp(type, "MPtcp") == 0) |
---|
| 841 | s->next = slInitMPTcpExtension(ns); |
---|
| 842 | #endif |
---|
| 843 | #ifdef HAVE_DBM |
---|
[8ed989] | 844 | else if (strcmp(type, "DBM") == 0) |
---|
[6b32990] | 845 | s->next = slInitDBMExtension(ns); |
---|
| 846 | #endif |
---|
[8ed989] | 847 | #if 1 |
---|
| 848 | else if (strcmp(type, "ssi") == 0) |
---|
| 849 | s->next = slInitSsiExtension(ns); |
---|
[4082e8] | 850 | #endif |
---|
| 851 | #if 1 |
---|
| 852 | else if (strcmp(type, "|") == 0) |
---|
| 853 | s->next = slInitPipeExtension(ns); |
---|
[c20843] | 854 | #endif |
---|
[8ed989] | 855 | else |
---|
[6b32990] | 856 | { |
---|
| 857 | Warn("Found unknown link type: %s", type); |
---|
| 858 | Warn("Use default link type: %s", si_link_root->type); |
---|
| 859 | omFreeBin(ns, s_si_link_extension_bin); |
---|
| 860 | return si_link_root; |
---|
| 861 | } |
---|
[50cbdc] | 862 | |
---|
[6b32990] | 863 | if (s->next == NULL) |
---|
| 864 | { |
---|
| 865 | Werror("Can not initialize link type %s", type); |
---|
| 866 | omFreeBin(ns, s_si_link_extension_bin); |
---|
| 867 | return NULL; |
---|
| 868 | } |
---|
| 869 | return s->next; |
---|
| 870 | } |
---|
[0e1846] | 871 | |
---|
| 872 | void slStandardInit() |
---|
| 873 | { |
---|
[d754b7] | 874 | si_link_extension s; |
---|
[c232af] | 875 | si_link_root=(si_link_extension)omAlloc0Bin(s_si_link_extension_bin); |
---|
[d754b7] | 876 | si_link_root->Open=slOpenAscii; |
---|
[0e1846] | 877 | si_link_root->Close=slCloseAscii; |
---|
[80419f] | 878 | si_link_root->Kill=slCloseAscii; |
---|
[0e1846] | 879 | si_link_root->Read=slReadAscii; |
---|
[472f39] | 880 | si_link_root->Read2=slReadAscii2; |
---|
[0e1846] | 881 | si_link_root->Write=slWriteAscii; |
---|
[286bd57] | 882 | si_link_root->Dump=slDumpAscii; |
---|
| 883 | si_link_root->GetDump=slGetDumpAscii; |
---|
[d754b7] | 884 | si_link_root->Status=slStatusAscii; |
---|
[fcf5d4] | 885 | si_link_root->type="ASCII"; |
---|
[d754b7] | 886 | s = si_link_root; |
---|
[6b32990] | 887 | s->next = NULL; |
---|
[0e1846] | 888 | } |
---|