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