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