1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
|
/*
* $Id: yio.c,v 1.4 2010-02-28 21:32:23 dhmunro Exp $
* Implement Yorick I/O functions.
*/
/* Copyright (c) 2005, The Regents of the University of California.
* All rights reserved.
* This file is part of yorick (http://yorick.sourceforge.net).
* Read the accompanying LICENSE file for details.
*/
#include "yio.h"
#include "defmem.h"
#include "ydata.h"
#include "pstdlib.h"
#include "play.h"
#include <string.h>
/* Print action defined here */
extern VMaction Print;
extern UnaryOp EvalFN, EvalBI, eval_auto; /* required by Print */
extern BuiltIn Y_print, Y_print_format;
/* required by print() called as function */
extern Array *GrowArray(Array *array, long extra);
extern char *MakeErrorLine(long lineNumber, const char *filename);
/* Non-blocking input tester defined in sysdep.c */
extern int YstdinNB(int noWait);
/*--------------------------------------------------------------------------*/
extern char *GetRFName(RangeFunc *rfTarget);
extern long ReopenSource(long index, int notExtern, long isrc);
extern int YpParse(void *func);
/*--------------------------------------------------------------------------*/
/* Yorick wrappers for fgets, feof, and ferror. */
char *Yfgets(char *s, int n, p_file *stream)
{
if (stream) return p_fgets(stream, s, n);
YError("(BUG) Yfgets with stream==0"); return 0;
}
int Yfeof(p_file *stream)
{
if (stream) return p_feof(stream);
YError("(BUG) Yfeof with stream==0"); return 0;
}
int Yferror(p_file *stream)
{
if (stream) return p_ferror(stream);
YError("(BUG) Yferror with stream==0"); return 0;
}
int YDPrompt(char *s)
{
p_stdout(s);
return 0;
}
int YDputsOut(char *s)
{
p_stdout(s);
p_stdout("\n");
return 0;
}
int YDputsErr(char *s)
{
p_stderr(s);
p_stderr("\n");
return 0;
}
int (*YPrompt)(char *s)= &YDPrompt;
int (*YputsOut)(char *s)= &YDputsOut;
int (*YputsErr)(char *s)= &YDputsErr;
/*--------------------------------------------------------------------------*/
/* Yorick interface to gets() allows for virtually unlimited length lines.
The price is the necessity of passing a struct* instead of a simple
char*... The case of file==0 is special -- file==0 means "stdin",
but Yorick allows for the possibility of more complex arrangements
for "stdin", including the possibility that keyboard input stream
is not coming from a standard C library input stream at all (it could
come, e.g., from some sort of X window message). */
#define MIN_LINE 256
#define BIG_LINE 1024
#define MAX_LINE 16384
#define INC_LINE 256
char *
Ygets(YgetsLine *getsLine, p_file *file)
{
char *line = getsLine->line;
int ateof, n, maxChars = getsLine->max;
if (!file) YError("(BUG) file==0 to Ygets no longer legal");
if (maxChars<MIN_LINE || maxChars>BIG_LINE) {
p_free(line);
getsLine->line = line = p_malloc(MIN_LINE+1);
getsLine->max = maxChars = MIN_LINE;
line[maxChars] = '\0'; /* just in case... */
}
/* Take care not to try to read a stream that has already returned EOF. */
if (Yfeof(file) || !Yfgets(line, maxChars+1, file)) goto abort;
ateof = Yfeof(file);
if (!ateof && Yferror(file)) goto abort;
n = strlen(line);
while (line[n-1]!='\n' && !ateof) {
if (maxChars>=MAX_LINE) goto abort;
if (n >= maxChars-8) {
/* on any reasonable file system, n == maxChars here
* one some massively parallel file systems, however, fgets can
* return before end-of-line even though not at end-of-file
* - hence fgets should always be called in a loop until either
* \n or EOF
*/
char *prev = line;
getsLine->line = line = p_malloc(maxChars+INC_LINE+1);
strcpy(line, prev);
p_free(prev);
getsLine->max = (maxChars += INC_LINE);
line[maxChars] = '\0'; /* just in case... */
}
/* EOF is possible, in which case Yfgets returns 0 (ignored here)
and leaves line unmodified. The following strlen is guaranteed
to return 0, and the loop terminates on the ateof test above. */
Yfgets(&line[n], maxChars+1-n, file);
ateof = Yfeof(file);
if (!ateof && Yferror(file)) goto abort;
n += strlen(&line[n]); /* faster than n= strlen(line) */
}
if (line[n-1]=='\n') line[--n] = '\0';
getsLine->n = n;
return line;
abort:
/* If neither Yferror(file) nor Yfeof(file), then line-too-long abort. */
if (maxChars>MIN_LINE) {
p_free(line);
getsLine->line = line = p_malloc(MIN_LINE+1);
getsLine->max = maxChars = MIN_LINE;
}
line[0] = '\0';
getsLine->n = 0;
return 0;
}
/*--------------------------------------------------------------------------*/
/* Scan for C-style escape sequences in quoted strings (e.g.- \n, \t),
returning the (single character) value of the escape sequence, and,
if the 2nd parameter is non-0, the character which stopped the scan.
Thus, if s=="tXYZ", then YpEscapeSeq returns 9 (ASCII tab), and
endp=="XYZ"; the same results would obtain if s=="009XYZ". */
int YpEscapeSeq(const char *s, char **endp)
{
unsigned char c= *s++;
int val;
/* 1, 2, or 3 octal digits */
if (c<='7' && c>='0') {
int i= 2;
val= c-'0';
while ((c=*s)<='7' && c>='0' && i--) {
val<<= 3;
val|= c-'0';
s++;
}
/* any number of hex digits (according to appendix A2.5.2 2nd ed K+R) */
} else if (c=='x') {
int isD, isU= 0;
val= 0;
while ((isD= ((c= *s)<='9' && c>='0')) ||
(isU= (c<='F' && c>='A')) ||
(c<='f' && c>='a')) {
val<<= 4;
if (isD) val|= c-'0';
else if (isU) val|= c-('A'-10);
else val|= c-('a'-10);
s++;
}
/* symbolic escapes in rough order of frequency I use them */
} else if (c=='n') val= '\n';
else if (c=='t') val= '\t';
else if (c=='a') val= '\a';
else if (c=='f') val= '\f';
else if (c=='r') val= '\r';
else if (c=='v') val= '\v';
else if (c=='b') val= '\b';
/* Note that \ ? ' and " are just self-insertion like anything else. */
else val= c;
if (endp) *endp= (char *)s; /* sigh */
return val;
}
/*--------------------------------------------------------------------------*/
extern UnaryOp PrintC, PrintS, PrintI, PrintL, PrintF, PrintD, PrintZ,
PrintQ, PrintP, PrintSI, PrintR, PrintVD, PrintSD, PrintFN, PrintBI,
PrintIO;
int printLength= 79; /* maximum number of characters to put on a line */
long maxPrintLines= 5000;
static char nBuffer[120]; /* buffer to hold numbers from sprintf */
static int (*RawPrinter)(char *s);
extern int PutsAsArray(char *s);
static void (*PrintRaw)(Operand *at);
static void PrintRawC(Operand *at);
static void PrintRawS(Operand *at);
static void PrintRawI(Operand *at);
static void PrintRawL(Operand *at);
static void PrintRawF(Operand *at);
static void PrintRawD(Operand *at);
static void PrintRawZ(Operand *at);
static void PrintRawQ(Operand *at);
static void PrintRawP(Operand *at);
static void PrintRawSI(Operand *at);
static void PrintArray(Operand *at);
static char *GetTempBuffer(long len);
static void ClearTempBuffer(int force);
static void PrintDims(Dimension *dims);
static int PDRecurse(Dimension *dims, long *length, int last);
void y_setup_func_hack(Operand *op)
{
YError("bad data type for array index");
}
/* Print calls functions with no arguments, or prints anything else using
YputsOut. */
void Print(void)
{
Operand op;
sp->ops->FormOperand(sp, &op);
if (op.ops->Setup == &y_setup_func_hack) {
/* put operand into bogus form expected by Eval (see Eval in ops3.c) */
op.references= 0; /* intentionally misused */
op.ops->Eval(&op);
} else {
PrintInit(YputsOut);
op.ops->Print(&op);
/* Drop(1);
Print will be followed by DropTop so that CalledAsSubroutine always
works. See parse.c. */
ForceNewline();
}
}
/* Y_print is a built in function which prints each of its arguments
in turn in the manner of Print if called as a function, or returns a
string vector if called as a function. */
void Y_print(int nArgs)
{
Operand op;
Symbol *stack= sp-nArgs+1;
if (CalledAsSubroutine()) {
PrintInit(YputsOut);
PushDataBlock(RefNC(&nilDB));
} else {
PrintInit(&PutsAsArray);
PushDataBlock(NewArray(&stringStruct, (Dimension *)0));
}
while (nArgs--) {
if (!stack->ops) YError("print accepts no keywords (do you mean write?)");
stack->ops->FormOperand(stack, &op);
op.ops->Print(&op);
if (nArgs) PermitNewline(2);
stack++;
}
ForceNewline();
}
/* PutsAsArray appends string to string array on top of stack --
intended as a plug-in replacement for YputsOut */
int PutsAsArray(char *s)
{
Array *array= (Array *)sp->value.db;
Dimension *dims= array->type.dims;
long number;
if (dims) {
number= dims->number;
sp->value.db= (DataBlock *)GrowArray(array, 1L);
Unref(array);
array= (Array *)sp->value.db;
} else {
array->type.dims= NewDimension(1L, 1L, (Dimension *)0);
number= 0;
}
array->value.q[number]= p_strcpy(s);
return 0;
}
static char *printBuf= 0;
static int lenPrintBuf= 0;
static int printNow, permitNow;
static long printLines;
void PrintInit(int (*puts_fun)(char *))
{
RawPrinter= puts_fun;
if (lenPrintBuf<printLength || (printLength<=79 && lenPrintBuf>79)) {
char *p= printBuf;
printBuf= 0;
p_free(p);
if (printLength<39) printLength= 39;
else if (printLength>256) printLength= 256;
printBuf= p_malloc(printLength+2); /* allow for newline, \0 */
lenPrintBuf= printLength;
}
printNow= permitNow= 0;
printLines= 0;
printBuf[0]= '\0';
}
void PrintFunc(const char *s)
{
long len= strlen(s);
while (printNow+len > printLength) {
if (p_signalling) p_abort();
if (permitNow) {
char savec[2];
int i= permitNow, j= 1;
savec[0]= printBuf[i];
printBuf[i++]= '\0';
if (printLines++ < maxPrintLines) RawPrinter(printBuf);
printBuf[0]= savec[0];
while (i<=printNow) printBuf[j++]= printBuf[i++];
printNow-= permitNow;
permitNow= 0;
} else {
long nhere= printLength-printNow-1;
char movec= '\0';
if (nhere>0) {
strncpy(&printBuf[printNow], s, nhere);
s+= nhere;
len-= nhere;
} else if (nhere<0) { /* only -1 is possible */
movec= printBuf[printLength-1];
}
strcpy(&printBuf[printLength-1], "\\");
if (printLines++ < maxPrintLines) RawPrinter(printBuf);
if (nhere >= 0) {
printNow= 0;
printBuf[0]= '\0';
} else {
printNow= 1;
printBuf[0]= movec;
printBuf[1]= '\0';
}
}
}
strcpy(&printBuf[printNow], s);
printNow+= len;
}
void PermitNewline(int nSpaces)
{
if (printNow+nSpaces > printLength) ForceNewline();
else while (nSpaces--) printBuf[printNow++]= ' ';
printBuf[printNow]= '\0';
permitNow= printNow;
}
void ForceNewline(void)
{
if (p_signalling) p_abort();
if (printNow) {
if (printLines++ < maxPrintLines) RawPrinter(printBuf);
printNow= permitNow= 0;
printBuf[0]= '\0';
}
}
void PrintFN(Operand *op)
{
Function *f= op->value;
Instruction *pc= f->code;
long posList;
int n;
PrintFunc("func ");
if (pc->index < 0) PrintFunc("<nameless>");
else PrintFunc(globalTable.names[pc->index]);
pc++;
PrintFunc("(");
PermitNewline(0);
n= f->nPos;
posList= f->hasPosList;
while (n--) {
posList>>= 1;
if (posList&1) PrintFunc("&");
PrintFunc(globalTable.names[(pc++)->index]);
if (n || (f->hasPosList&1) || f->nKey)
{ PrintFunc(","); PermitNewline(0); }
}
n= f->nKey;
if (f->hasPosList&1) {
pc++; /* skip *va* parameter */
PrintFunc("..");
if (n) { PrintFunc(","); PermitNewline(0); }
}
while (n--) {
PrintFunc(globalTable.names[(pc++)->index]);
if (n) { PrintFunc("=,"); PermitNewline(0); }
else PrintFunc("=");
}
PrintFunc(")");
}
void PrintBI(Operand *op)
{
BIFunction *bif= op->value;
char *name;
long len;
name= bif->index>=0? globalTable.names[bif->index] : "<nameless>";
len= strlen(name);
strcpy(nBuffer, "builtin ");
strncat(nBuffer+8, name, 64);
if (len>64) strcpy(nBuffer+72, "...");
strcat(nBuffer, "()");
PrintFunc(nBuffer);
}
void PrintX(Operand *op)
{
DataBlock *db= op->value;
char *name= db? db->ops->typeName : "<unknown>";
long len;
len= strlen(name);
strcpy(nBuffer, "Object of type: ");
strncat(nBuffer+16, name, 64);
if (len>56) strcpy(nBuffer+72, "...");
PrintFunc(nBuffer);
}
static void PrintArray(Operand *at)
{
Dimension *dims= at->type.dims;
if (!dims) {
PrintRaw(at);
} else {
void (*OrigPrintRaw)(Operand *)= PrintRaw;
long n= dims->number;
at->type.dims= dims->next;
PrintFunc("[");
for (;;) {
PrintArray(at);
PrintRaw= OrigPrintRaw; /* PrintRawSI changes this */
if (!(--n)) break;
PrintFunc(",");
PermitNewline(0);
}
PrintFunc("]");
at->type.dims= dims;
}
}
void PrintC(Operand *op) { PrintRaw= PrintRawC; PrintArray(op); }
void PrintS(Operand *op) { PrintRaw= PrintRawS; PrintArray(op); }
void PrintI(Operand *op) { PrintRaw= PrintRawI; PrintArray(op); }
void PrintL(Operand *op) { PrintRaw= PrintRawL; PrintArray(op); }
void PrintF(Operand *op) { PrintRaw= PrintRawF; PrintArray(op); }
void PrintD(Operand *op) { PrintRaw= PrintRawD; PrintArray(op); }
void PrintZ(Operand *op) { PrintRaw= PrintRawZ; PrintArray(op); }
void PrintQ(Operand *op) { PrintRaw= PrintRawQ; PrintArray(op); }
void PrintP(Operand *op) { PrintRaw= PrintRawP; PrintArray(op); }
void PrintSI(Operand *op) { PrintRaw= PrintRawSI; PrintArray(op); }
#define DEFAULT_CHAR "0x%02x"
#define DEFAULT_SHORT "%d"
#define DEFAULT_INT "%d"
#define DEFAULT_LONG "%ld"
#define DEFAULT_FLOAT "%g"
#define DEFAULT_DOUBLE "%g"
#define DEFAULT_COMPLEX "%g%+gi"
#define DEFAULT_POINTER "%p"
static char *typeDefault[8]= {
DEFAULT_CHAR, DEFAULT_SHORT, DEFAULT_INT, DEFAULT_LONG,
DEFAULT_FLOAT, DEFAULT_DOUBLE, DEFAULT_COMPLEX, DEFAULT_POINTER };
void DefaultPrintFormat(int type)
{
if (type & (1<<T_CHAR)) yCharFormat= typeDefault[0];
if (type & (1<<T_SHORT)) yShortFormat= typeDefault[1];
if (type & (1<<T_INT)) yIntFormat= typeDefault[2];
if (type & (1<<T_LONG)) yLongFormat= typeDefault[3];
if (type & (1<<T_FLOAT)) yFloatFormat= typeDefault[4];
if (type & (1<<T_DOUBLE)) yDoubleFormat= typeDefault[5];
if (type & (1<<T_COMPLEX)) yComplexFormat= typeDefault[6];
if (type & (1<<T_POINTER)) yPointerFormat= typeDefault[7];
}
char *yCharFormat= DEFAULT_CHAR;
static void PrintRawC(Operand *at)
{ unsigned char *pv= at->value; sprintf(nBuffer, yCharFormat, (int)*pv);
PrintFunc(nBuffer); at->value= pv+1; }
char *yShortFormat= DEFAULT_SHORT;
static void PrintRawS(Operand *at)
{ short *pv= at->value; sprintf(nBuffer, yShortFormat, (int)*pv);
PrintFunc(nBuffer); at->value= pv+1; }
char *yIntFormat= DEFAULT_INT;
static void PrintRawI(Operand *at)
{ int *pv= at->value; sprintf(nBuffer, yIntFormat, *pv);
PrintFunc(nBuffer); at->value= pv+1; }
char *yLongFormat= DEFAULT_LONG;
static void PrintRawL(Operand *at)
{ long *pv= at->value; sprintf(nBuffer, yLongFormat, *pv);
PrintFunc(nBuffer); at->value= pv+1; }
char *yFloatFormat= DEFAULT_FLOAT;
static void PrintRawF(Operand *at)
{ float *pv= at->value; sprintf(nBuffer, yFloatFormat, (double)*pv);
PrintFunc(nBuffer); at->value= pv+1; }
char *yDoubleFormat= DEFAULT_DOUBLE;
static void PrintRawD(Operand *at)
{ double *pv= at->value; sprintf(nBuffer, yDoubleFormat, *pv);
PrintFunc(nBuffer); at->value= pv+1; }
char *yComplexFormat= DEFAULT_COMPLEX;
static void PrintRawZ(Operand *at)
{ double *pv= at->value; sprintf(nBuffer, yComplexFormat, pv[0], pv[1]);
PrintFunc(nBuffer); at->value= pv+2; }
char *yPointerFormat= DEFAULT_POINTER;
static void PrintRawP(Operand *at)
{ void **pv= at->value; sprintf(nBuffer, yPointerFormat, *pv);
PrintFunc(nBuffer); at->value= pv+1; }
static long typeIndex[8]= { -1, -1, -1, -1, -1, -1, -1, -1 };
static char **typeFormat[8]= {
&yCharFormat, &yShortFormat, &yIntFormat, &yLongFormat,
&yFloatFormat, &yDoubleFormat, &yComplexFormat, &yPointerFormat };
void Y_print_format(int nArgs)
{
Symbol *arg = sp-nArgs+1;
int npos = 0;
if (typeIndex[0]<0) {
typeIndex[0]= Globalize("char", 0L);
typeIndex[1]= Globalize("short", 0L);
typeIndex[2]= Globalize("int", 0L);
typeIndex[3]= Globalize("long", 0L);
typeIndex[4]= Globalize("float", 0L);
typeIndex[5]= Globalize("double", 0L);
typeIndex[6]= Globalize("complex", 0L);
typeIndex[7]= Globalize("pointer", 0L);
}
if (nArgs < 1) {
DefaultPrintFormat(~0);
printLength= 79;
maxPrintLines= 5000;
} else do {
if (arg->ops) {
if (++npos > 2) YError("print_format expects at most two positional arguments");
if (YNotNil(arg)) {
long i = YGetInteger(arg);
if (npos == 1) {
if (i>256) printLength = 256;
else if (i>39) printLength = i;
else if (i>0) printLength = 39;
else printLength = 79;
} else {
if (i < 10) maxPrintLines = (i<=0)? 5000 : 10;
else maxPrintLines = i;
}
}
arg++;
} else {
long index= (arg++)->index;
int i;
char *string;
for (i=0 ; i<8 ; i++) if (typeIndex[i]==index) break;
if (i>=8) YError("unrecognized keyword in print_format");
string= *typeFormat[i];
*typeFormat[i]= typeDefault[i];
if (string!=typeDefault[i]) p_free(string);
string= YGetString(arg++);
nArgs--;
if (string && strlen(string)) *typeFormat[i]= p_strcpy(string);
}
} while (--nArgs);
}
static char *sPart= 0;
static long sPartLen= 0;
static char *GetTempBuffer(long len)
{
if (len>=sPartLen) {
long newlen= ((len-1)/80 + 1)*80;
ClearTempBuffer(1);
sPart= p_malloc(newlen+1);
sPartLen= newlen;
}
return sPart;
}
static void ClearTempBuffer(int force)
{
if (force || sPartLen>80) {
char *part= sPart;
sPartLen= 0;
sPart= 0;
p_free(part);
}
}
char *ScanForEscape(char *s)
{
char c;
if (!s) return 0;
while ((c= *s) && c>=' ' && c<'\177' && c!='\\' && c!='\"') s++;
return s;
}
int AddEscapeSeq(char *s, int esc)
{
int n= 2;
if (esc=='\\') strcpy(s, "\\\\");
else if (esc=='\"') strcpy(s, "\\\"");
else if (esc=='\n') strcpy(s, "\\n");
else if (esc=='\t') strcpy(s, "\\t");
else if (esc=='\a') strcpy(s, "\\a");
else if (esc=='\f') strcpy(s, "\\f");
else if (esc=='\r') strcpy(s, "\\r");
else if (esc=='\v') strcpy(s, "\\v");
else if (esc=='\b') strcpy(s, "\\b");
else { sprintf(s, "\\%03o", esc&0xff); n= 4; }
return n;
}
static void PrintRawQ(Operand *at)
{
char **pv= at->value;
char *s= *pv++;
char *esc;
if (!s) {
PrintRawP(at);
} else {
PrintFunc("\"");
if (*(esc= ScanForEscape(s))) {
char *part;
do {
part= GetTempBuffer(esc-s + 4);
strncpy(part, s, esc-s);
AddEscapeSeq(part + (esc-s), (int)(*esc));
PrintFunc(part);
s= esc+1;
} while (*(esc= ScanForEscape(s)));
ClearTempBuffer(0);
}
if (*s) PrintFunc(s);
PrintFunc("\"");
}
at->value= pv;
}
static void PrintRawSI(Operand *at)
{
char *pv= at->value;
StructDef *base= at->type.base;
Operand subOp;
long n= base->table.nItems;
char **name= base->table.names;
Member *member= base->members;
long *offset= base->offsets;
if (base->file) YError("(BUG?) can't print an instance of a disk struct");
PrintFunc(StructName(base));
PrintFunc("(");
PermitNewline(0);
for (;;) {
PrintFunc(*name);
PrintFunc("=");
PermitNewline(0);
subOp.type.base= member->base;
subOp.type.dims= member->dims;
subOp.value= pv + (*offset);
subOp.type.base->dataOps->Print(&subOp);
if (!(--n)) break;
PrintFunc(",");
PermitNewline(0);
name++;
member++;
offset++;
}
PrintFunc(")");
at->value= pv + base->size;
}
/* range functions */
extern RangeFunc RFmin, RFmax, RFptp, RFsum, RFavg, RFrms, RFmnx, RFmxx,
RFpsum, RFdif, RFzcen, RFpcen, RFuncp, RFcum;
static char *rfNames[]= { "avg:","cum:","dif:","max:","min:","mnx:","mxx:",
"pcen:","psum:","ptp:","rms:","sum:","uncp:","zcen:","??:" };
static RangeFunc *RFs[]= { &RFavg,&RFcum,&RFdif,&RFmax,&RFmin,&RFmnx,&RFmxx,
&RFpcen,&RFpsum,&RFptp,&RFrms,&RFsum,&RFuncp,&RFzcen,0 };
char *GetRFName(RangeFunc *rfTarget)
{
RangeFunc **rf= RFs;
while (*rf && *rf!=rfTarget) rf++;
return rfNames[rf-RFs];
}
void PrintR(Operand *op)
{
Range *range= op->value;
int flags= range->nilFlags;
long len;
if (range->rf) {
strcpy(nBuffer, GetRFName(range->rf));
len= strlen(nBuffer);
} else if (flags&R_MARKED) {
strcpy(nBuffer, "+:");
len= 2;
} else if (flags&R_PSEUDO) {
strcpy(nBuffer, "-:");
len= 2;
} else if (flags&R_RUBBER) {
PrintFunc("<..>:");
return;
} else if (flags&R_NULLER) {
PrintFunc("<nuller>:");
return;
} else {
nBuffer[0]= '\0';
len= 0;
}
if (flags&R_MINNIL) {
if (flags&R_MAXNIL) {
strcpy(nBuffer+len, ":");
} else {
sprintf(nBuffer+len, ":%ld", range->max);
}
} else if (flags&R_MAXNIL) {
sprintf(nBuffer+len, "%ld:", range->min);
} else {
sprintf(nBuffer+len, "%ld:%ld", range->min, range->max);
}
if (range->inc!=1) {
len= strlen(nBuffer);
sprintf(nBuffer+len, ":%ld", range->inc);
}
PrintFunc(nBuffer);
}
/* ARGSUSED */
void PrintVD(Operand *op)
{
PrintFunc("[]");
}
static int PDRecurse(Dimension *dims, long *length, int last)
{
long len;
int count;
if (!dims) return 0;
count= PDRecurse(dims->next, length, 0);
if (count<0) return count-1;
len= *length;
if (dims->origin!=1L) {
if (len>45) return -1;
sprintf(nBuffer+len, "%ld:%ld",
dims->origin, dims->origin+dims->number-1);
} else {
if (len>60) return -1;
sprintf(nBuffer+len, "%ld", dims->number);
}
len+= strlen(nBuffer+len);
if (!last) { strcpy(nBuffer+len, ","); len++; }
*length= len;
return count+1;
}
static void PrintDims(Dimension *dims)
{
if (dims) {
long len= 1;
strcpy(nBuffer, "(");
if (PDRecurse(dims, &len, 1)<0) {
strcpy(nBuffer+len, "...");
len+= 3;
}
strcpy(nBuffer+len, ")");
PrintFunc(nBuffer);
}
}
void PrintSD(Operand *at)
{
StructDef *base= at->value;
long n= base->table.nItems;
Member *member= base->members;
char **name= base->table.names;
ForceNewline();
PrintFunc("struct ");
PrintFunc(StructName(base));
PrintFunc(" {");
ForceNewline();
while (n--) {
PrintFunc(" ");
PrintFunc(StructName(member->base));
PrintFunc(" ");
PrintFunc(*name);
PrintDims(member->dims);
PrintFunc(";");
ForceNewline();
member++;
name++;
}
PrintFunc("}");
ForceNewline();
}
/*--------------------------------------------------------------------------*/
static char *printText= 0, *tmpText= 0;
static char *ioStatus[]=
{ "<illegal>", "read-only", "write-only", "read-write" };
static void SafeFree(char **s);
static void SafeFree(char **s)
{
char *t= *s;
*s= 0;
p_free(t);
}
void PrintIO(Operand *op)
{
IOStream *file= op->value;
char text[80];
SafeFree(&printText);
SafeFree(&tmpText);
/* read-write binary stream: <tail of fullname>
In directory: <head of fullname>
Current record is number 53 of 53
File of current record: <tail of child's name>
Time, cycle of current record: 1.234567e+00, 9876
*/
ForceNewline();
if (file->stream) {
sprintf(text, "%s binary stream: ", ioStatus[file->permissions&3]);
tmpText= YNameTail(file->fullname);
} else {
strcpy(text, "binary stream <closed>: ");
}
printText= p_strncat(text, tmpText, 0);
SafeFree(&tmpText);
PrintFunc(printText);
SafeFree(&printText);
ForceNewline();
tmpText= YNameHead(file->fullname);
printText= p_strncat(" In directory: ", tmpText, 0);
SafeFree(&tmpText);
PrintFunc(printText);
SafeFree(&printText);
ForceNewline();
if (file->history) {
HistoryInfo *history= file->history;
if (history->recNumber>=0) {
long i= history->recNumber;
sprintf(text, " Current record is number %ld of %ld",
i+1, history->nRecords);
PrintFunc(text);
ForceNewline();
tmpText= YNameTail(history->child->fullname);
printText= p_strncat(" File of current record: ", tmpText, 0);
SafeFree(&tmpText);
PrintFunc(printText);
SafeFree(&printText);
ForceNewline();
if (history->time) {
if (history->ncyc) {
sprintf(text, " Time, cycle of current record: %.6e, %ld",
history->time[i], history->ncyc[i]);
} else {
sprintf(text, " Time of current record: %.6e",
history->time[i]);
}
} else {
if (history->ncyc) {
sprintf(text, " Cycle of current record: %ld",
history->ncyc[i]);
} else {
strcpy(text, " <No time or cycle available>");
}
}
PrintFunc(text);
ForceNewline();
} else {
PrintFunc(" <No current record>");
ForceNewline();
}
}
}
/*--------------------------------------------------------------------------*/
/* Set up a block allocator which grabs space for 16 IOFileLink objects
at a time. Since IOFileLink contains several pointers, the alignment
of an IOFileLink must be at least as strict as a void*. */
static MemryBlock ioflBlock= {0, 0, sizeof(IOFileLink),
16*sizeof(IOFileLink)};
void AddIOLink(IOFileLink** list, void *ios)
{
IOFileLink *first= *list;
IOFileLink *iofl= NextUnit(&ioflBlock);
iofl->next= first;
iofl->prev= list;
iofl->ios= ios;
*list= iofl;
if (first) first->prev= &iofl->next;
}
void RemoveIOLink(IOFileLink* iofl, void *ios)
{
for ( ; iofl ; iofl=iofl->next) if (iofl->ios==ios) break;
if (iofl) {
*iofl->prev= iofl->next;
if (iofl->next) iofl->next->prev= iofl->prev;
FreeUnit(&ioflBlock, iofl);
}
}
/*--------------------------------------------------------------------------*/
char *YpReparse(void *function)
{
Function *func= function;
long index= func? func->code[0].index : -1;
long position= ReopenSource(index, 1, (func?func->isrc:-1));
char *msg;
if (position>=0) {
YpParse(func); /* This had better not generate any new tasks... */
msg= MakeErrorLine(ypBeginLine,
nYpIncludes? ypIncludes[nYpIncludes-1].filename : 0);
if (nYpIncludes && ypIncludes[nYpIncludes-1].file) {
p_fclose(ypIncludes[nYpIncludes-1].file);
ypIncludes[nYpIncludes-1].file= 0;
}
} else if (position<-3) {
msg= MakeErrorLine(-4,
"*****fseek failed scanning source file for function");
} else if (position<-2) {
msg= MakeErrorLine(-3,
"*****source file changed, no longer contains function");
} else if (position<-1) {
msg= MakeErrorLine(-2,
"*****source file for function no longer exists");
} else {
msg= MakeErrorLine(-1,
"*****source file for function unknown");
}
return msg;
}
/*--------------------------------------------------------------------------*/
|