[0e1846] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
[356dc3d] | 4 | /* $Id: febase.cc,v 1.31 1998-04-23 18:52:48 Singular Exp $ */ |
---|
[0e1846] | 5 | /* |
---|
[d931f8] | 6 | * ABSTRACT: i/o system |
---|
[0e1846] | 7 | */ |
---|
| 8 | |
---|
| 9 | #include <stdlib.h> |
---|
| 10 | #include <stdio.h> |
---|
| 11 | #include <limits.h> |
---|
| 12 | #include <stdarg.h> |
---|
| 13 | #ifndef macintosh |
---|
| 14 | #include <unistd.h> |
---|
| 15 | #endif |
---|
| 16 | #ifdef NeXT |
---|
| 17 | #include <sys/file.h> |
---|
| 18 | #endif |
---|
| 19 | #include "mod2.h" |
---|
| 20 | #include "tok.h" |
---|
| 21 | #include "febase.h" |
---|
| 22 | #include "mmemory.h" |
---|
| 23 | #include "subexpr.h" |
---|
| 24 | #include "ipshell.h" |
---|
| 25 | |
---|
| 26 | #define fePutChar(c) fputc((uchar)(c),stdout) |
---|
| 27 | /*0 implementation */ |
---|
| 28 | |
---|
| 29 | char fe_promptstr[] |
---|
| 30 | #ifdef macintosh |
---|
| 31 | =" \n"; |
---|
| 32 | #else |
---|
| 33 | =" "; |
---|
| 34 | #endif |
---|
| 35 | |
---|
| 36 | #define INITIAL_PRINT_BUFFER 24*1024 |
---|
| 37 | static int feBufferLength=INITIAL_PRINT_BUFFER; |
---|
[f3d535] | 38 | static char * feBuffer=(char *)Alloc(INITIAL_PRINT_BUFFER); |
---|
[0e1846] | 39 | |
---|
| 40 | int si_echo = 0; |
---|
| 41 | int printlevel = 0; |
---|
| 42 | #ifndef macintosh |
---|
| 43 | int pagelength = 24; |
---|
| 44 | #else |
---|
| 45 | int pagelength = -1; |
---|
| 46 | #endif |
---|
| 47 | int colmax = 80; |
---|
| 48 | char prompt_char = '>'; /*1 either '>' or '.'*/ |
---|
| 49 | BITSET verbose = 1 |
---|
| 50 | | Sy_bit(V_REDEFINE) |
---|
| 51 | | Sy_bit(V_LOAD_LIB) |
---|
| 52 | | Sy_bit(V_SHOW_USE) |
---|
| 53 | | Sy_bit(V_PROMPT) |
---|
| 54 | // | Sy_bit(V_DEBUG_MEM) |
---|
| 55 | ; |
---|
| 56 | BOOLEAN errorreported = FALSE; |
---|
| 57 | BOOLEAN feBatch; |
---|
| 58 | char * feErrors=NULL; |
---|
| 59 | int feErrorsLen=0; |
---|
| 60 | |
---|
[0f678f] | 61 | const char feNotImplemented[]="not implemented"; |
---|
| 62 | |
---|
[0e1846] | 63 | BOOLEAN feProt = FALSE; |
---|
| 64 | FILE* feProtFile; |
---|
| 65 | BOOLEAN tclmode=FALSE; |
---|
| 66 | /* TCL-Protocoll (Singular -x): <char type>:<int length>:<string> \n |
---|
| 67 | * E:l:s error - not implemented yet (use N) |
---|
| 68 | * W:l:s warning |
---|
| 69 | * N:l:s stdout |
---|
| 70 | * Q:0: quit |
---|
| 71 | * P:0: prompt > |
---|
| 72 | * P:1: prompt . |
---|
| 73 | * R:l:<ring-name> ring change |
---|
| 74 | * plan: |
---|
| 75 | * O:l:<option/no-option> option change (option) |
---|
| 76 | * V:l:<option/no-option> option change (verbose) |
---|
| 77 | */ |
---|
| 78 | |
---|
[d931f8] | 79 | #include "febase.inc" |
---|
| 80 | |
---|
[30637f] | 81 | /*2 |
---|
| 82 | * fopen, but use 'SINGULARPATH' from environment and SINGULARDATADIR |
---|
[f7b5a1] | 83 | * as set by configure |
---|
[30637f] | 84 | */ |
---|
[0e1846] | 85 | #ifdef macintosh |
---|
| 86 | # define FS_SEP ',' |
---|
| 87 | # define DIR_SEP ':' |
---|
| 88 | # define DIR_SEPP ":" |
---|
| 89 | #else |
---|
| 90 | #ifdef MSDOS |
---|
| 91 | # define FS_SEP ';' |
---|
| 92 | # define DIR_SEP '\\' |
---|
| 93 | # define DIR_SEPP "\\" |
---|
| 94 | #else |
---|
| 95 | #ifdef atarist |
---|
| 96 | # define FS_SEP ';' |
---|
| 97 | # define DIR_SEP '\\' |
---|
| 98 | # define DIR_SEPP "\\" |
---|
| 99 | #else /* unix */ |
---|
| 100 | # define FS_SEP ':' |
---|
| 101 | # define DIR_SEP '/' |
---|
| 102 | # define DIR_SEPP "/" |
---|
| 103 | #endif /* atarist */ |
---|
| 104 | #endif /* MSDOS */ |
---|
| 105 | #endif /* macintosh */ |
---|
| 106 | |
---|
[97454d] | 107 | extern "C" char* find_executable(const char* argv0); |
---|
[1caa72] | 108 | |
---|
| 109 | #define SINGULAR_RELATIVE_DATA_DIR "LIB" |
---|
| 110 | |
---|
| 111 | static char* SearchPath = NULL; |
---|
[97454d] | 112 | static char* ExpandedExecutable = NULL; |
---|
| 113 | |
---|
| 114 | char* feGetExpandedExecutable(const char* argv0) |
---|
| 115 | { |
---|
| 116 | if (ExpandedExecutable == NULL) |
---|
| 117 | { |
---|
| 118 | if (argv0 != NULL) |
---|
| 119 | ExpandedExecutable = find_executable(argv0); |
---|
| 120 | } |
---|
| 121 | return ExpandedExecutable; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | static char* feRemovePathnameHead(const char* ef) |
---|
| 125 | { |
---|
| 126 | if (ef != NULL) |
---|
| 127 | { |
---|
| 128 | char *temp, *temp2, *temp3; |
---|
| 129 | |
---|
| 130 | temp2 = mstrdup(ef); |
---|
| 131 | temp3 = temp2; |
---|
| 132 | temp = NULL; |
---|
| 133 | while (*temp3 != '\0') |
---|
| 134 | { |
---|
| 135 | if (*temp3 == DIR_SEP) temp = temp3; |
---|
| 136 | temp3++; |
---|
| 137 | } |
---|
| 138 | if (temp != NULL) |
---|
| 139 | { |
---|
| 140 | *temp = '\0'; |
---|
| 141 | } |
---|
| 142 | return temp2; |
---|
| 143 | } |
---|
| 144 | else |
---|
| 145 | return NULL; |
---|
| 146 | } |
---|
| 147 | |
---|
[1caa72] | 148 | |
---|
| 149 | // Return the file search path for singular w.r.t. the following priorities: |
---|
| 150 | // Env-variables + Relative Data Dir + Burned-in data dir |
---|
| 151 | char* feGetSearchPath(const char* argv0) |
---|
| 152 | { |
---|
| 153 | if (SearchPath == NULL) |
---|
| 154 | { |
---|
| 155 | char *env = NULL, *sibbling = NULL, *path; |
---|
| 156 | int plength = 0, tmp; |
---|
| 157 | |
---|
| 158 | #ifdef MSDOS |
---|
| 159 | env=getenv("SPATH"); |
---|
| 160 | #else |
---|
| 161 | env=getenv("SINGULARPATH"); |
---|
| 162 | #endif |
---|
| 163 | |
---|
| 164 | if (argv0 != NULL) |
---|
[97454d] | 165 | sibbling = feRemovePathnameHead(feGetExpandedExecutable(argv0)); |
---|
[1caa72] | 166 | |
---|
| 167 | if (env != NULL) |
---|
| 168 | plength = strlen(env) + 1; |
---|
| 169 | |
---|
| 170 | if (sibbling != NULL) |
---|
| 171 | plength += strlen(sibbling) + strlen(SINGULAR_RELATIVE_DATA_DIR) + 2; |
---|
| 172 | |
---|
| 173 | plength += strlen(SINGULAR_DATADIR) + 2; |
---|
| 174 | |
---|
| 175 | path = (char*) AllocL(plength*sizeof(char)); |
---|
| 176 | SearchPath = path; |
---|
| 177 | |
---|
| 178 | if (env != NULL) |
---|
| 179 | { |
---|
| 180 | tmp = strlen(env); |
---|
| 181 | memcpy(path, env, tmp); |
---|
| 182 | path = &(path[tmp]); |
---|
| 183 | *path = FS_SEP; |
---|
| 184 | path++; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | if (sibbling != NULL) |
---|
| 188 | { |
---|
| 189 | tmp = strlen(sibbling); |
---|
| 190 | memcpy(path, sibbling, tmp); |
---|
| 191 | path = &(path[tmp]); |
---|
| 192 | *path = DIR_SEP; |
---|
| 193 | path++; |
---|
| 194 | tmp = strlen(SINGULAR_RELATIVE_DATA_DIR); |
---|
| 195 | memcpy(path, SINGULAR_RELATIVE_DATA_DIR, tmp); |
---|
| 196 | path = &(path[tmp]); |
---|
| 197 | *path = FS_SEP; |
---|
| 198 | path++; |
---|
| 199 | FreeL(sibbling); |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | tmp = strlen(SINGULAR_DATADIR); |
---|
| 203 | memcpy(path,SINGULAR_DATADIR, tmp); |
---|
| 204 | path = &(path[tmp]); |
---|
| 205 | *path = '\0'; |
---|
| 206 | } |
---|
| 207 | return SearchPath; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | |
---|
[0e1846] | 211 | FILE * feFopen(char *path, char *mode, char *where,int useWerror) |
---|
| 212 | { |
---|
[30637f] | 213 | FILE * f=fopen(path,mode); |
---|
[82a884b] | 214 | #ifdef macintosh |
---|
| 215 | if (f!=NULL) |
---|
| 216 | { |
---|
| 217 | if (where!=NULL) strcpy(where,path); |
---|
| 218 | return f; |
---|
| 219 | } |
---|
| 220 | char *res; |
---|
| 221 | int idat=strlen(SINGULAR_DATADIR),ipath=strlen(path); |
---|
| 222 | char *env=getenv("SINGULARPATH"); |
---|
[c9ea817] | 223 | int ienv=0, ii=0; |
---|
| 224 | if (env!=NULL) |
---|
| 225 | { |
---|
| 226 | ienv=strlen(env); |
---|
| 227 | ii=ienv; |
---|
| 228 | } |
---|
[82a884b] | 229 | if (ii<idat) ii = idat; |
---|
| 230 | if (ii==0) |
---|
| 231 | { |
---|
| 232 | if (useWerror) |
---|
| 233 | Werror("cannot open `%s`",path); |
---|
| 234 | return f; |
---|
| 235 | } |
---|
| 236 | res=(char*) AllocL(ii+ipath+1); |
---|
| 237 | if (ienv!=0) |
---|
| 238 | { |
---|
| 239 | memcpy(res,env,ienv); |
---|
| 240 | memcpy(res+ienv,path,ipath); |
---|
| 241 | res[ienv+ipath]='\0'; |
---|
| 242 | f=fopen(res,mode); |
---|
| 243 | } |
---|
| 244 | if ((f==NULL)&&(idat!=0)) |
---|
| 245 | { |
---|
| 246 | memcpy(res,SINGULAR_DATADIR,idat); |
---|
| 247 | memcpy(res+idat,path,ipath); |
---|
| 248 | res[idat+ipath]='\0'; |
---|
| 249 | f=fopen(res,mode); |
---|
| 250 | } |
---|
| 251 | if (f==NULL) |
---|
| 252 | { |
---|
| 253 | if (useWerror) |
---|
| 254 | Werror("cannot open `%s`",res); |
---|
| 255 | } |
---|
| 256 | else if (where!=NULL) |
---|
| 257 | strcpy(where,res); |
---|
| 258 | FreeL(res); |
---|
| 259 | #else |
---|
[667247] | 260 | if (where!=NULL) strcpy(where,path); |
---|
| 261 | if ((*mode=='r') && (path[0]!=DIR_SEP)&&(path[0]!='.') |
---|
| 262 | &&(f==NULL)) |
---|
[0e1846] | 263 | { |
---|
[667247] | 264 | char found = 0; |
---|
[1caa72] | 265 | char* spath = feGetSearchPath(); |
---|
[667247] | 266 | char *s; |
---|
[1caa72] | 267 | |
---|
[667247] | 268 | if (where==NULL) s=(char *)AllocL(250); |
---|
| 269 | else s=where; |
---|
[1caa72] | 270 | |
---|
| 271 | if (spath!=NULL) |
---|
[0e1846] | 272 | { |
---|
[667247] | 273 | char *p,*q; |
---|
[1caa72] | 274 | p = spath; |
---|
[667247] | 275 | while( (q=strchr(p, FS_SEP)) != NULL) |
---|
| 276 | { |
---|
| 277 | *q = '\0'; |
---|
| 278 | strcpy(s,p); |
---|
| 279 | *q = FS_SEP; |
---|
| 280 | strcat(s, DIR_SEPP); |
---|
| 281 | strcat(s, path); |
---|
| 282 | #ifndef macintosh |
---|
| 283 | if(!access(s, R_OK)) { found++; break; } |
---|
| 284 | #else |
---|
| 285 | f=fopen(s,mode); |
---|
| 286 | if (f!=NULL) { found++; fclose(f); break; } |
---|
| 287 | #endif |
---|
| 288 | p = q+1; |
---|
| 289 | } |
---|
| 290 | if(!found) |
---|
| 291 | { |
---|
| 292 | strcpy(s,p); |
---|
| 293 | strcat(s, DIR_SEPP); |
---|
| 294 | strcat(s, path); |
---|
| 295 | } |
---|
[0e1846] | 296 | f=fopen(s,mode); |
---|
[667247] | 297 | if (f!=NULL) |
---|
| 298 | { |
---|
| 299 | if (where==NULL) FreeL((ADDRESS)s); |
---|
| 300 | return f; |
---|
| 301 | } |
---|
[40edb03] | 302 | } |
---|
| 303 | else |
---|
| 304 | { |
---|
[667247] | 305 | if (where!=NULL) strcpy(s/*where*/,path); |
---|
| 306 | f=fopen(path,mode); |
---|
[40edb03] | 307 | } |
---|
[667247] | 308 | if (where==NULL) FreeL((ADDRESS)s); |
---|
[0e1846] | 309 | } |
---|
| 310 | if ((f==NULL)&&(useWerror)) |
---|
| 311 | Werror("cannot open `%s`",path); |
---|
[82a884b] | 312 | #endif |
---|
[0e1846] | 313 | return f; |
---|
| 314 | } |
---|
| 315 | |
---|
[f3d535] | 316 | static char * feBufferStart; |
---|
[0e1846] | 317 | /* only used in StringSet(S)/StringAppend(S)*/ |
---|
| 318 | char * StringAppend(char *fmt, ...) |
---|
| 319 | { |
---|
| 320 | va_list ap; |
---|
| 321 | char *s = feBufferStart; /*feBuffer + strlen(feBuffer);*/ |
---|
| 322 | int more; |
---|
| 323 | va_start(ap, fmt); |
---|
| 324 | if ((more=feBufferStart-feBuffer+strlen(fmt)+100)>feBufferLength) |
---|
| 325 | { |
---|
| 326 | more = ((more + (4*1024-1))/(4*1024))*(4*1024); |
---|
| 327 | int l=s-feBuffer; |
---|
| 328 | feBuffer=(char *)ReAlloc((ADDRESS)feBuffer,feBufferLength, |
---|
| 329 | more); |
---|
| 330 | feBufferLength=more; |
---|
| 331 | s=feBuffer+l; |
---|
| 332 | #ifndef BSD_SPRINTF |
---|
| 333 | feBufferStart=s; |
---|
| 334 | #endif |
---|
| 335 | } |
---|
| 336 | #ifdef BSD_SPRINTF |
---|
| 337 | vsprintf(s, fmt, ap); |
---|
| 338 | while (*s!='\0') s++; |
---|
| 339 | feBufferStart =s; |
---|
| 340 | #else |
---|
| 341 | feBufferStart += vsprintf(s, fmt, ap); |
---|
| 342 | #endif |
---|
| 343 | va_end(ap); |
---|
| 344 | return feBuffer; |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | char * StringAppendS(char *st) |
---|
| 348 | { |
---|
| 349 | /* feBufferStart is feBuffer + strlen(feBuffer);*/ |
---|
| 350 | int more,l; |
---|
| 351 | int ll=feBufferStart-feBuffer; |
---|
[b97e94] | 352 | if ((more=ll+2+(l=strlen(st)))>feBufferLength) |
---|
[0e1846] | 353 | { |
---|
| 354 | more = ((more + (4*1024-1))/(4*1024))*(4*1024); |
---|
| 355 | feBuffer=(char *)ReAlloc((ADDRESS)feBuffer,feBufferLength, |
---|
| 356 | more); |
---|
| 357 | feBufferLength=more; |
---|
| 358 | feBufferStart=feBuffer+ll; |
---|
| 359 | } |
---|
| 360 | strcat(feBufferStart, st); |
---|
| 361 | feBufferStart +=l; |
---|
| 362 | return feBuffer; |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | char * StringSet(char *fmt, ...) |
---|
| 366 | { |
---|
| 367 | va_list ap; |
---|
| 368 | char *s = feBuffer; |
---|
| 369 | va_start(ap, fmt); |
---|
| 370 | #ifdef BSD_SPRINTF |
---|
| 371 | vsprintf(s, fmt, ap); |
---|
| 372 | while (*s!='\0') s++; |
---|
| 373 | feBufferStart = s; |
---|
| 374 | #else |
---|
| 375 | feBufferStart = feBuffer + vsprintf(s, fmt, ap); |
---|
| 376 | #endif |
---|
| 377 | va_end(ap); |
---|
| 378 | return feBuffer; |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | char * StringSetS(char *st) |
---|
| 382 | { |
---|
| 383 | int more,l; |
---|
| 384 | if ((l=strlen(st))>feBufferLength) |
---|
| 385 | { |
---|
| 386 | more = ((l + (4*1024-1))/(4*1024))*(4*1024); |
---|
| 387 | feBuffer=(char *)ReAlloc((ADDRESS)feBuffer,feBufferLength, |
---|
| 388 | more); |
---|
| 389 | feBufferLength=more; |
---|
| 390 | } |
---|
| 391 | strcpy(feBuffer,st); |
---|
| 392 | feBufferStart=feBuffer+l; |
---|
| 393 | return feBuffer; |
---|
| 394 | } |
---|
| 395 | |
---|
| 396 | void PrintTCLS(char c, char *s) |
---|
| 397 | { |
---|
| 398 | #ifndef macintosh |
---|
| 399 | int l=strlen(s); |
---|
| 400 | if (l>0) PrintTCL(c,l,s); |
---|
| 401 | #endif |
---|
| 402 | } |
---|
| 403 | |
---|
[0da09b5] | 404 | extern "C" { |
---|
[4b5c87] | 405 | void WerrorS(const char *s) |
---|
[0e1846] | 406 | { |
---|
| 407 | #ifdef HAVE_MPSR |
---|
| 408 | if (feBatch) |
---|
| 409 | { |
---|
| 410 | if (feErrors==NULL) |
---|
| 411 | { |
---|
| 412 | feErrors=(char *)Alloc(256); |
---|
| 413 | feErrorsLen=256; |
---|
[4b5c87] | 414 | strcpy(feErrors,(char *)s); |
---|
[0e1846] | 415 | } |
---|
| 416 | else |
---|
| 417 | { |
---|
[4b5c87] | 418 | if (((int)(strlen((char *)s)+strlen(feErrors)))>=feErrorsLen) |
---|
[0e1846] | 419 | { |
---|
| 420 | feErrors=(char *)ReAlloc(feErrors,feErrorsLen,feErrorsLen+256); |
---|
| 421 | feErrorsLen+=256; |
---|
| 422 | } |
---|
[4b5c87] | 423 | strcat(feErrors,(char *)s); |
---|
[0e1846] | 424 | } |
---|
| 425 | strcat(feErrors,"\n"); |
---|
| 426 | } |
---|
| 427 | else |
---|
| 428 | #endif |
---|
| 429 | { |
---|
| 430 | #ifdef HAVE_TCL |
---|
| 431 | if (tclmode) |
---|
| 432 | { |
---|
[4b5c87] | 433 | //PrintTCLS('E',(char *)s); |
---|
[0e1846] | 434 | //PrintTCLS('E',"\n"); |
---|
[4b5c87] | 435 | PrintTCLS('N',(char *)s); |
---|
[0e1846] | 436 | PrintTCLS('N',"\n"); |
---|
| 437 | } |
---|
| 438 | else |
---|
| 439 | #endif |
---|
| 440 | { |
---|
| 441 | fwrite(" ? ",1,5,stderr); |
---|
[4b5c87] | 442 | fwrite((char *)s,1,strlen((char *)s),stderr); |
---|
[0e1846] | 443 | fwrite("\n",1,1,stderr); |
---|
| 444 | fflush(stderr); |
---|
| 445 | if (feProt&PROT_O) |
---|
| 446 | { |
---|
| 447 | fwrite(" ? ",1,5,feProtFile); |
---|
[4b5c87] | 448 | fwrite((char *)s,1,strlen((char *)s),feProtFile); |
---|
[0e1846] | 449 | fwrite("\n",1,1,feProtFile); |
---|
| 450 | } |
---|
| 451 | } |
---|
| 452 | } |
---|
| 453 | errorreported = TRUE; |
---|
| 454 | } |
---|
| 455 | |
---|
| 456 | void Werror(char *fmt, ...) |
---|
| 457 | { |
---|
| 458 | va_list ap; |
---|
| 459 | va_start(ap, fmt); |
---|
| 460 | char *s=(char *)Alloc(256); |
---|
| 461 | vsprintf(s, fmt, ap); |
---|
| 462 | WerrorS(s); |
---|
| 463 | Free(s,256); |
---|
| 464 | va_end(ap); |
---|
| 465 | } |
---|
| 466 | } |
---|
| 467 | |
---|
[356dc3d] | 468 | void WarnS(const char *s) |
---|
[0e1846] | 469 | { |
---|
| 470 | #ifdef HAVE_TCL |
---|
| 471 | if (tclmode) |
---|
| 472 | { |
---|
| 473 | PrintTCLS('W',s); |
---|
| 474 | } |
---|
| 475 | else |
---|
| 476 | #endif |
---|
| 477 | { |
---|
| 478 | fwrite("// ** ",1,6,stdout); |
---|
| 479 | fwrite(s,1,strlen(s),stdout); |
---|
| 480 | fwrite("\n",1,1,stdout); |
---|
| 481 | fflush(stdout); |
---|
| 482 | if (feProt&PROT_O) |
---|
| 483 | { |
---|
| 484 | fwrite("// ** ",1,6,feProtFile); |
---|
| 485 | fwrite(s,1,strlen(s),feProtFile); |
---|
| 486 | fwrite("\n",1,1,feProtFile); |
---|
| 487 | } |
---|
| 488 | } |
---|
| 489 | } |
---|
| 490 | |
---|
[356dc3d] | 491 | void Warn(const char *fmt, ...) |
---|
[0e1846] | 492 | { |
---|
| 493 | va_list ap; |
---|
| 494 | va_start(ap, fmt); |
---|
| 495 | char *s=(char *)Alloc(256); |
---|
| 496 | vsprintf(s, fmt, ap); |
---|
| 497 | WarnS(s); |
---|
| 498 | Free(s,256); |
---|
| 499 | va_end(ap); |
---|
| 500 | } |
---|
| 501 | |
---|
| 502 | #ifdef macintosh |
---|
| 503 | static int lines = 0; |
---|
| 504 | static int cols = 0; |
---|
| 505 | void mwrite(uchar c) |
---|
| 506 | { |
---|
| 507 | if (c == '\n') |
---|
| 508 | { |
---|
| 509 | cols = 0; |
---|
| 510 | if (lines == pagelength) |
---|
| 511 | { |
---|
| 512 | lines = 0; |
---|
| 513 | fePause(); |
---|
| 514 | } |
---|
| 515 | else |
---|
| 516 | { |
---|
| 517 | lines++; |
---|
| 518 | fePutChar(c); |
---|
| 519 | } |
---|
| 520 | } |
---|
| 521 | else |
---|
| 522 | { |
---|
| 523 | fePutChar(c); |
---|
| 524 | cols++; |
---|
| 525 | if (cols == colmax) |
---|
| 526 | { |
---|
| 527 | // cols = 0; //will be done by mwrite('\n'); |
---|
| 528 | mwrite('\n'); |
---|
| 529 | } |
---|
| 530 | } |
---|
| 531 | } |
---|
| 532 | #endif |
---|
| 533 | |
---|
| 534 | void PrintS(char *s) |
---|
| 535 | { |
---|
| 536 | #ifdef macintosh |
---|
| 537 | char c; |
---|
| 538 | while ('\0' != (c = *s++)) |
---|
| 539 | { |
---|
| 540 | mwrite(c); |
---|
| 541 | } |
---|
| 542 | #else |
---|
| 543 | #ifdef HAVE_TCL |
---|
| 544 | if (tclmode) |
---|
| 545 | { |
---|
| 546 | PrintTCLS('N',s); |
---|
| 547 | } |
---|
| 548 | else |
---|
| 549 | #endif |
---|
| 550 | { |
---|
| 551 | fwrite(s,1,strlen(s),stdout); |
---|
| 552 | fflush(stdout); |
---|
| 553 | if (feProt&PROT_O) |
---|
| 554 | { |
---|
| 555 | fwrite(s,1,strlen(s),feProtFile); |
---|
| 556 | } |
---|
| 557 | } |
---|
| 558 | #endif |
---|
| 559 | } |
---|
| 560 | |
---|
[057e93c] | 561 | void PrintLn() |
---|
| 562 | { |
---|
| 563 | PrintS("\n"); |
---|
| 564 | } |
---|
| 565 | |
---|
[0e1846] | 566 | void Print(char *fmt, ...) |
---|
| 567 | { |
---|
| 568 | va_list ap; |
---|
| 569 | va_start(ap, fmt); |
---|
| 570 | #ifdef HAVE_TCL |
---|
| 571 | if(tclmode) |
---|
| 572 | #endif |
---|
| 573 | #if (defined(HAVE_TCL) || defined(macintosh)) |
---|
| 574 | { |
---|
| 575 | char *s=(char *)Alloc(strlen(fmt)+256); |
---|
| 576 | vsprintf(s,fmt, ap); |
---|
| 577 | #ifdef HAVE_TCL |
---|
| 578 | PrintTCLS('N',s); |
---|
| 579 | #endif |
---|
| 580 | #ifdef macintosh |
---|
| 581 | char c; |
---|
| 582 | while ('\0' != (c = *s++)) |
---|
| 583 | { |
---|
| 584 | mwrite(c); |
---|
| 585 | } |
---|
| 586 | if (feProt&PROT_O) |
---|
| 587 | { |
---|
[d8bb0f] | 588 | vfprintf(feProtFile,fmt,ap); |
---|
[0e1846] | 589 | } |
---|
| 590 | #endif |
---|
| 591 | } |
---|
| 592 | #endif |
---|
| 593 | #if !defined(macintosh) || defined(HAVE_TCL) |
---|
| 594 | #ifdef HAVE_TCL |
---|
| 595 | else |
---|
| 596 | #endif |
---|
| 597 | { |
---|
| 598 | vfprintf(stdout, fmt, ap); |
---|
| 599 | fflush(stdout); |
---|
| 600 | if (feProt&PROT_O) |
---|
| 601 | { |
---|
| 602 | vfprintf(feProtFile,fmt,ap); |
---|
| 603 | } |
---|
| 604 | } |
---|
| 605 | #endif |
---|
| 606 | va_end(ap); |
---|
| 607 | } |
---|
| 608 | |
---|
| 609 | void fePause() |
---|
| 610 | { |
---|
| 611 | uchar c; |
---|
| 612 | mflush(); |
---|
| 613 | #ifndef macintosh |
---|
| 614 | fputs("pause>",stderr); |
---|
| 615 | #else |
---|
| 616 | fputs("pause>\n",stderr); |
---|
| 617 | #endif |
---|
| 618 | c = fgetc(stdin); |
---|
| 619 | if (((c == '\003') || (c == 'C')) || (c == 'c')) |
---|
| 620 | { |
---|
| 621 | m2_end(1); |
---|
| 622 | } |
---|
| 623 | } |
---|
| 624 | |
---|
| 625 | void monitor(char* s, int mode) |
---|
| 626 | { |
---|
| 627 | if (feProt) |
---|
| 628 | { |
---|
| 629 | fclose(feProtFile); |
---|
| 630 | } |
---|
| 631 | if ((s!=NULL) && (*s!='\0')) |
---|
| 632 | { |
---|
| 633 | feProtFile = fopen(s,"w"); |
---|
| 634 | if (feProtFile==NULL) |
---|
| 635 | { |
---|
| 636 | Werror("cannot open %s",s); |
---|
| 637 | } |
---|
| 638 | else |
---|
| 639 | feProt = (BOOLEAN)mode; |
---|
| 640 | } |
---|
| 641 | } |
---|
| 642 | |
---|
| 643 | |
---|
| 644 | char* eati(char *s, int *i) |
---|
| 645 | { |
---|
| 646 | int l=0; |
---|
| 647 | |
---|
| 648 | if (*s >= '0' && *s <= '9') |
---|
| 649 | { |
---|
| 650 | *i = 0; |
---|
| 651 | while (*s >= '0' && *s <= '9') |
---|
| 652 | { |
---|
| 653 | *i *= 10; |
---|
| 654 | *i += *s++ - '0'; |
---|
| 655 | l++; |
---|
| 656 | if ((l>MAX_INT_LEN)||((*i) <0)) |
---|
| 657 | { |
---|
| 658 | s-=l; |
---|
| 659 | Werror("`%s` greater than %d(max. integer representation)", |
---|
| 660 | s,INT_MAX); |
---|
| 661 | return s; |
---|
| 662 | } |
---|
| 663 | } |
---|
| 664 | } |
---|
| 665 | else *i = 1; |
---|
| 666 | return s; |
---|
| 667 | } |
---|
| 668 | |
---|