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