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 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
|
/*
ASCIO.C
Define standard Yorick built-in functions for ASCII I/O
See std.i for documentation on the interface functions defined here.
$Id: ascio.c,v 1.1 1993/08/27 18:32:09 munro Exp munro $
*/
/* Copyright (c) 1994. The Regents of the University of California.
All rights reserved. */
#include "ydata.h"
#include "yio.h"
#include "defstr.h"
#include "defmem.h"
extern BuiltIn Y_open, Y_close, Y_read, Y_write, Y_sread, Y_swrite;
extern BuiltIn Y_rdline, Y_bookmark, Y_backup, Y_popen, Y_fflush;
extern char *MakeErrorLine(long lineNumber, const char *filename);
extern void YErrorIO(const char *msg);
/*--------------------------------------------------------------------------*/
static char *CheckBuf(long len);
static char *CrackScan(char *format);
static char *CrackPrint(char *format);
static int CrackFormat(char *format, char *(*Cracker)(char *));
static void AddFormat(char *format, int type, int danger);
static void FreeFormats(void);
static void CheckOps(int nArgs);
static char *NextInLine(Operand *op, long n);
static void ReadWorker(Operand *sourceOp, Symbol *stack, char *format);
static void WriteWorker(Operand *sinkOp, Symbol *stack, char *format);
static char *CheckOut(long len);
static char *fmtBuf= 0;
static long fmtLen= 0;
static int fmtType;
#define IO_NONE 0
#define IO_STRING 1
#define IO_LONG 2
#define IO_DOUBLE 3
#define IO_POINTER 4
#define IO_CHAR 5
struct FormatList { char *format; int type; int typeID; int danger; };
static struct FormatList *fmtList= 0;
static long fmtMax= 0;
static long fmtNow= 0;
static long fmtAssigns;
static long fmtWidth, fmtTotal;
static int fmtDanger;
static Operand *ioOps= 0;
static long maxIOops= 0;
static int typeMatch[]= { IO_LONG, IO_LONG, IO_LONG, IO_LONG,
IO_DOUBLE, IO_DOUBLE, IO_NONE, IO_STRING, IO_POINTER };
static char *scanDefaults[]= { 0, "%s%n", "%ld%n", "%le%n" };
static char *printDefaults[]= { 0, " %s", " %8ld", " %#14.6g", " %8p" };
typedef int ScanFunc(Operand *op, char *format, char **text);
static ScanFunc CScanner, SScanner, IScanner, LScanner,
FScanner, DScanner, QScanner, NilScanner;
static ScanFunc *Scanner[]= { &CScanner, &SScanner, &IScanner, &LScanner,
&FScanner, &DScanner, 0, &QScanner, &NilScanner };
static YgetsLine inputBuffer= { 0, 0, 0 };
static Dimension *pDims= 0;
static char *outBuf= 0;
static long outLen= 0;
static long lineSize;
typedef void PrtFunc(Operand *op, char *format, char *text);
static PrtFunc CPrinter, SPrinter, IPrinter, LPrinter,
FPrinter, DPrinter, QPrinter, PPrinter;
static PrtFunc *Printer[]= { &CPrinter, &SPrinter, &IPrinter, &LPrinter,
&FPrinter, &DPrinter, 0, &QPrinter, &PPrinter };
static PrtFunc CPrintC, SPrintC, IPrintC, LPrintC;
static PrtFunc *PrintC[]= { &CPrintC, &SPrintC, &IPrintC, &LPrintC };
static PrtFunc FPrintD, DPrintD;
static PrtFunc *PrintD[]= { &FPrintD, &DPrintD };
/*--------------------------------------------------------------------------*/
/* Two data types which are "foreign" to Yorick are defined in this
file: the TextStream and the Bookmark. */
extern PromoteOp PromXX;
extern UnaryOp ToAnyX, NegateX, ComplementX, NotX, TrueX;
extern BinaryOp AddX, SubtractX, MultiplyX, DivideX, ModuloX, PowerX;
extern BinaryOp EqualX, NotEqualX, GreaterX, GreaterEQX;
extern BinaryOp ShiftLX, ShiftRX, OrX, AndX, XorX;
extern BinaryOp AssignX, MatMultX;
extern UnaryOp EvalX, SetupX, PrintX;
extern MemberOp GetMemberX;
static UnaryOp PrintBM, PrintTX;
/* Implement text streams as a foreign Yorick data type. */
struct TextStream {
int references; /* reference counter */
Operations *ops; /* virtual function table */
FILE *stream; /* 0 indicates file has been closed */
char *fullname; /* filename after YExpandName */
int permissions; /* +1 read permission, +2 write permission
+4 append mode, +8 binary mode,
+16 not seekable, +32 pipe */
/* ------ begin specific text stream part ------- */
long lastLineRead; /* 1-origin line number of last line read */
long readPosition; /* file position (ftell) after lastLineRead */
long lastPosition; /* file position (ftell) before lastLineRead --
after backup, lastPosition==readPosition,
and lastPosition is not valid */
int readWrite; /* 0 initially, 1 after read, 2 after write */
long fileID; /* unique number used to recognize this file */
};
extern TextStream *NewTextStream(char *fullname,
void *stream, int permissions,
long line, long pos);
extern void FreeTextStream(void *ts); /* ******* Use Unref(ts) ******* */
Operations textOps = {
&FreeTextStream, T_OPAQUE, 0, T_STRING, "text_stream",
{&PromXX, &PromXX, &PromXX, &PromXX, &PromXX, &PromXX, &PromXX, &PromXX},
&ToAnyX, &ToAnyX, &ToAnyX, &ToAnyX, &ToAnyX, &ToAnyX, &ToAnyX,
&NegateX, &ComplementX, &NotX, &TrueX,
&AddX, &SubtractX, &MultiplyX, &DivideX, &ModuloX, &PowerX,
&EqualX, &NotEqualX, &GreaterX, &GreaterEQX,
&ShiftLX, &ShiftRX, &OrX, &AndX, &XorX,
&AssignX, &EvalX, &SetupX, &GetMemberX, &MatMultX, &PrintTX
};
/* Implement bookmarks as a foreign Yorick data type. */
typedef struct Bookmark Bookmark;
struct Bookmark {
int references; /* reference counter */
Operations *ops; /* virtual function table */
long lastLineRead;
long lastPosition, readPosition;
long fileID;
};
extern Bookmark *NewBookmark(long line, long last, long next, long id);
extern void FreeBookmark(void *bm); /* ******* Use Unref(bm) ******* */
Operations bookOps = {
&FreeBookmark, T_OPAQUE, 0, T_STRING, "bookmark",
{&PromXX, &PromXX, &PromXX, &PromXX, &PromXX, &PromXX, &PromXX, &PromXX},
&ToAnyX, &ToAnyX, &ToAnyX, &ToAnyX, &ToAnyX, &ToAnyX, &ToAnyX,
&NegateX, &ComplementX, &NotX, &TrueX,
&AddX, &SubtractX, &MultiplyX, &DivideX, &ModuloX, &PowerX,
&EqualX, &NotEqualX, &GreaterX, &GreaterEQX,
&ShiftLX, &ShiftRX, &OrX, &AndX, &XorX,
&AssignX, &EvalX, &SetupX, &GetMemberX, &MatMultX, &PrintBM
};
/*--------------------------------------------------------------------------*/
void Y_open(int nArgs)
{
Symbol *stack= sp-nArgs+1;
char *filename, *filemode, *fullname;
int errmode, permissions;
FILE *file;
if (nArgs<1 || nArgs>3) YError("bad argument list to open function");
filename= YGetString(stack);
if (nArgs<2) filemode= 0;
else filemode= YGetString(stack+1);
if (nArgs<3) errmode= 0;
else errmode= YGetInteger(stack+2);
if (!filemode || !filemode[0])
filemode= "r";
else if (filemode[0]!='r' && filemode[0]!='w' && filemode[0]!='a')
YError("2nd argument to open must begin with r, w, or a");
fullname= YExpandName(filename);
file= fopen(fullname, filemode);
if (file) {
/* set permission bits and push result IOStream */
if (filemode[0]=='r') permissions= 1;
else if (filemode[0]=='w') permissions= 2;
else permissions= 6;
if (filemode[1]=='+') {
if (filemode[2]=='b') permissions|= 11;
else permissions|= 3;
} else if (filemode[1]=='b') {
if (filemode[2]=='+') permissions|= 11;
else permissions|= 8;
}
if (permissions&8) {
IOStream *ios= NewIOStream(fullname, file, permissions);
PushDataBlock(ios);
if (permissions&2) CLupdate(ios);
} else {
PushDataBlock(NewTextStream(fullname, file, permissions, 0L, 0L));
}
} else if (errmode) {
/* fail silently if optional errmode flag is set */
StrFree(fullname);
permissions= 0;
PushDataBlock(Ref(&nilDB));
} else {
/* blow up if optional errmode flag is not set */
char *dots= strlen(filename)>40? "..." : "";
char message[80];
StrFree(fullname);
sprintf(message, "cannot open file %.40s%s (mode %.6s)",
filename, dots, filemode);
YErrorIO(message);
return;
}
PopTo(sp-nArgs-1);
Drop(nArgs);
return;
}
void Y_close(int nArgs)
{
DataBlock *db;
IOStream *binary= 0;
if (nArgs!=1) YError("close function takes exactly one argument");
/* If argument is a simple variable reference, nil the variable. */
if (sp->ops==&referenceSym) {
Symbol *s= &globTab[sp->index];
ReplaceRef(sp);
if (s->ops==&dataBlockSym &&
(s->value.db->ops==&textOps || s->value.db->ops==&streamOps)) {
s->ops= &intScalar;
Unref(s->value.db);
s->value.db= Ref(&nilDB);
s->ops= &dataBlockSym;
}
}
db= sp->value.db;
if (db->ops==&textOps) {
TextStream *text= (TextStream *)db;
if (text->stream) {
#ifndef NO_POPEN
if (text->permissions&32)
pclose(text->stream);
else
#endif
fclose(text->stream);
}
text->stream= 0;
} else if (db->ops==&streamOps) {
/* Make sure that the binary->stream gets closed, even if the IOStream
itself is not freed by the Drop() below. */
if (db->references) binary= (IOStream *)db;
} else if (db->ops!=&voidOps) {
YError("bad argument type to close function");
}
if (db->references && db->ops!=&voidOps) {
char message[80];
sprintf(message, "%d outstanding references to closed file",
db->references);
YWarning(message);
}
Drop(1);
if (binary) {
if (binary->stream) binary->ioOps->Close(binary);
binary->stream= 0;
}
}
/*--------------------------------------------------------------------------*/
void Y_read(int nArgs)
{
Symbol *stack, *s;
char *format, *keyNames[3];
Symbol *keySymbols[2];
Operand sourceOp;
keyNames[0]= "format";
keyNames[1]= "prompt";
keyNames[2]= 0;
stack= YGetKeywords(sp-nArgs+1, nArgs, keyNames, keySymbols);
/* Get 1st argument if it is an IOStream or nil, otherwise will use
nil stream (keyboard). */
if (stack>sp) YError("read function takes at least one argument");
/* treat references gently -- don't want to suck reference to
scalar int, long, or double onto the stack */
if (stack->ops==&referenceSym) s= &globTab[stack->index];
else s= stack;
if (s->ops==&dataBlockSym &&
(s->value.db->ops==&textOps || s->value.db->ops==&voidOps)) {
stack->ops->FormOperand(stack, &sourceOp);
stack++;
if (sourceOp.ops==&voidOps) sourceOp.ops= 0;
else {
TextStream *ts= sourceOp.value;
FILE *file= ts->stream;
if (!file)
YErrorIO("attempt to read from closed I/O stream");
else if (!(ts->permissions & 1))
YErrorIO("attempt to read from file opened in w or a mode");
if (ts->readWrite&2 && fseek(file, ts->readPosition, SEEK_SET)) {
clearerr(file); /* don't prejudice future I/O attempts */
YErrorIO("fseek failed to find current position in ASCII read");
}
ts->readWrite= 1;
}
} else {
sourceOp.ops= 0; /* special form recognized by NextInLine */
}
if (!sourceOp.ops) {
/* set prompt string for NextInLine. */
sourceOp.value= keySymbols[1]? YGetString(keySymbols[1]) : "read> ";
if (!sourceOp.value) sourceOp.value= "";
}
/* get format keyword, if any */
format= keySymbols[0]? YGetString(keySymbols[0]) : 0;
ReadWorker(&sourceOp, stack, format);
}
void Y_sread(int nArgs)
{
Symbol *stack;
char *format, *keyNames[2];
Symbol *keySymbols[1];
Operand sourceOp;
keyNames[0]= "format";
keyNames[1]= 0;
stack= YGetKeywords(sp-nArgs+1, nArgs, keyNames, keySymbols);
/* Get 1st argument if it is an IOStream or nil, otherwise will use
nil stream (keyboard). */
if (stack>sp) YError("sread function takes at least one argument");
stack->ops->FormOperand(stack, &sourceOp);
if (sourceOp.ops!=&stringOps)
YError("1st argument to sread must be source string or string array");
stack++;
/* get format keyword, if any */
format= keySymbols[0]? YGetString(keySymbols[0]) : 0;
ReadWorker(&sourceOp, stack, format);
}
static void ReadWorker(Operand *sourceOp, Symbol *stack, char *format)
{
Symbol *s;
char *text;
Dimension *dims= 0;
Operand *op;
long i, number, lineCount;
int j, nConversions, nArgs, typeID;
/* crack format string */
nConversions= CrackFormat(format, &CrackScan);
/* First pass through arguments counts them, checks data types and
conformability, and matches them to conversions in the format list. */
CheckOps((int)(sp-stack+1));
nArgs= 0;
for ( ; stack<=sp ; stack++) {
if (!stack->ops) { /* skip keywords */
stack++;
continue;
}
if (stack->ops==&referenceSym &&
globTab[stack->index].ops!=&dataBlockSym) s= &globTab[stack->index];
else s= stack;
op= &ioOps[nArgs];
s->ops->FormOperand(s, op);
typeID= op->ops->typeID;
if (typeID>T_DOUBLE && typeID!=T_STRING)
YError("read cannot handle non-array, complex, pointer, or structure");
if (nArgs<nConversions) {
if (typeMatch[typeID]!=fmtList[nArgs].type)
YError("read format/read output data type mismatch");
} else if (!nArgs && !nConversions && fmtNow==1) {
format= fmtList[0].format;
fmtList[0].format= StrCat(format, scanDefaults[typeMatch[typeID]]);
StrFree(format);
} else {
AddFormat(scanDefaults[typeMatch[typeID]], typeMatch[typeID], 0);
}
fmtList[nArgs].typeID= typeID;
if (nArgs) {
Dimension *tmp= op->type.dims;
while (tmp && dims && tmp->number==dims->number) {
tmp= tmp->next;
dims= dims->next;
}
if (tmp || dims)
YError("all outputs from formatted read must have same dimensions");
}
dims= op->type.dims;
nArgs++;
}
number= TotalNumber(dims);
if (nArgs<fmtNow) {
/* must scan for matching junk after all arguments read */
fmtList[nArgs].typeID= 8; /* NilScanner */
i= strlen(fmtList[nArgs].format);
while (i && (fmtList[nArgs].format[i-1]=='\012' ||
fmtList[nArgs].format[i-1]=='\015' ||
fmtList[nArgs].format[i-1]==' '))
fmtList[nArgs].format[--i]= '\0'; /* strip off trailing newlines */
if (i) nArgs++;
}
/* outer loop is on input array elements */
lineCount= 0;
text= NextInLine(sourceOp, lineCount++);
fmtAssigns= 0;
for (i=0 ; i<number ; i++) {
/* inner loop is on arguments to be read */
for (j=0 ; j<nArgs ; j++) {
do {
while (text && !text[0]) text= NextInLine(sourceOp, lineCount++);
/* If input exhausted, NextInLine returns text==0.
If matching failure, Scanner returns text==0. */
/* Scanner returns zero if object was found, non-zero if not
found. The text pointer is advanced past the number of
characters scanned, or set to zero if a matching failure
occurred. */
} while (Scanner[fmtList[j].typeID](&ioOps[j], fmtList[j].format,
&text));
}
}
/* release excessive temporary space */
FreeFormats();
/* return total number of objects actually assigned */
PushLongValue(fmtAssigns);
}
void Y_rdline(int nArgs)
{
Symbol *stack;
Operand op;
long i, nLines= 0;
Array *result;
Dimension *dims;
char *keyNames[2];
Symbol *keySymbols[1];
keyNames[0]= "prompt";
keyNames[1]= 0;
stack= YGetKeywords(sp-nArgs+1, nArgs, keyNames, keySymbols);
for (nArgs=0 ; stack<=sp ; stack++) {
if (!stack->ops) {
stack++;
continue;
}
if (nArgs==1) nLines= YGetInteger(stack);
else if (nArgs==0) {
stack->ops->FormOperand(stack, &op);
if (op.ops!=&textOps && op.ops!=&voidOps)
YError("1st argument to rdline function not a text stream or nil");
} else {
YError("rdline function takes exactly one or two arguments");
}
nArgs++;
}
if (!nArgs || op.ops==&voidOps) op.ops= 0;
else {
TextStream *ts= op.value;
FILE *file= ts->stream;
if (!file)
YErrorIO("attempt to read from closed I/O stream");
else if (!(ts->permissions & 1))
YErrorIO("attempt to read from file opened in w or a mode");
if (ts->readWrite&2 && fseek(file, ts->readPosition, SEEK_SET)) {
clearerr(file); /* don't prejudice future I/O attempts */
YErrorIO("fseek failed to find current position in ASCII read");
}
ts->readWrite= 1;
}
if (!op.ops) {
/* set prompt string for NextInLine. */
op.value= keySymbols[0]? YGetString(keySymbols[0]) : "read> ";
if (!op.value) op.value= "";
}
dims= tmpDims;
tmpDims= 0;
FreeDimension(dims);
if (nLines>0) tmpDims= NewDimension(nLines, 1L, tmpDims);
else nLines= 1;
result= PushDataBlock(NewArray(&stringStruct, tmpDims));
for (i=0 ; i<nLines ; i++)
result->value.q[i]= StrCpy(NextInLine(&op, i));
}
/*--------------------------------------------------------------------------*/
void Y_write(int nArgs)
{
Symbol *stack;
char *format, *keyNames[3];
Symbol *keySymbols[2];
Operand sinkOp;
keyNames[0]= "format";
keyNames[1]= "linesize";
keyNames[2]= 0;
stack= YGetKeywords(sp-nArgs+1, nArgs, keyNames, keySymbols);
/* Get 1st argument if it is an IOStream or nil, otherwise will use
nil stream (keyboard). */
if (stack>sp) YError("write function takes at least one argument");
stack->ops->FormOperand(stack, &sinkOp);
if (sinkOp.ops==&textOps) {
TextStream *ts= sinkOp.value;
FILE *file= ts->stream;
stack++;
if (!file)
YErrorIO("attempt to write to closed I/O stream");
else if (!(ts->permissions & 2))
YErrorIO("attempt to write to file opened in r mode");
if (ts->readWrite&1 && fseek(file, 0L, SEEK_END)) {
clearerr(file); /* don't prejudice future I/O attempts */
YErrorIO("fseek failed to find current position in ASCII write");
}
ts->readWrite= 2;
} else {
if (sinkOp.ops==&voidOps) stack++;
sinkOp.ops= 0;
}
/* get format keyword, if any */
format= keySymbols[0]? YGetString(keySymbols[0]) : 0;
/* get linesize keyword, if any */
lineSize= keySymbols[1]? YGetInteger(keySymbols[1]) : 80;
WriteWorker(&sinkOp, stack, format);
}
void Y_swrite(int nArgs)
{
Symbol *stack;
char *format, *keyNames[2];
Symbol *keySymbols[1];
keyNames[0]= "format";
keyNames[1]= 0;
stack= YGetKeywords(sp-nArgs+1, nArgs, keyNames, keySymbols);
/* get format keyword, if any */
format= keySymbols[0]? YGetString(keySymbols[0]) : 0;
WriteWorker((Operand *)0, stack, format);
}
static void WriteWorker(Operand *sinkOp, Symbol *stack, char *format)
{
#ifdef FAKE_INTERRUPTS
extern int check_except(void);
extern short check_brk_flag;
#endif
Dimension *dims;
Operand *op;
long i, number;
int j, nConversions, nArgs, typeID, nChars, nLine;
char *text;
Array *result; /* for swrite only (sinkOp==0) */
FILE *file= (sinkOp && sinkOp->ops)? ((TextStream *)sinkOp->value)->stream :
(FILE *)0;
/* crack format string, set fmtTotal */
nConversions= CrackFormat(format, &CrackPrint);
dims= pDims;
pDims= 0;
FreeDimension(dims);
/* First pass through arguments counts them, checks data types and
conformability, and matches them to conversions in the format list. */
CheckOps((int)(sp-stack+1));
nArgs= 0;
for ( ; stack<=sp ; stack++) {
if (!stack->ops) { /* skip keywords */
stack++;
continue;
}
op= &ioOps[nArgs];
stack->ops->FormOperand(stack, op);
typeID= op->ops->typeID;
if (typeID>T_DOUBLE && typeID!=T_STRING && typeID!=T_POINTER)
YError("write cannot handle non-array, complex, or structure");
if (nArgs<nConversions) {
int ftype= fmtList[nArgs].type;
if (ftype==IO_CHAR) ftype= IO_LONG;
if (typeMatch[typeID]!=ftype)
YError("write format/write input data type mismatch");
} else if (!nArgs && !nConversions && fmtNow==1) {
format= fmtList[0].format;
fmtList[0].format= StrCat(format, printDefaults[typeMatch[typeID]]);
StrFree(format);
} else {
AddFormat(printDefaults[typeMatch[typeID]], typeMatch[typeID], 0);
}
fmtList[nArgs].typeID= typeID;
if (typeID==T_STRING) {
/* A string field might expand by as much as the length
of the longest string to be printed. */
char **q= op->value;
long len;
fmtWidth= 0;
number= op->type.number;
for (i=0 ; i<number ; i++)
if (q[i] && (len= strlen(q[i]))>fmtWidth) fmtWidth= len;
fmtTotal+= fmtWidth;
} else {
fmtTotal+= 25; /* difficult to imagine longer numeric field */
}
if (nArgs) {
if (Conform(pDims, op->type.dims) & 4)
YError("all inputs to formatted write must be conformable");
dims= pDims;
pDims= Ref(tmpDims);
FreeDimension(dims);
} else {
pDims= Ref(op->type.dims);
}
nArgs++;
}
number= TotalNumber(pDims);
/* second pass broadcasts arguments to same size */
for (j=0 ; j<nArgs ; j++) RightConform(pDims, &ioOps[j]);
/* Make sure the outBuf has at least fmtTotal characters, which should
be a very conservative estimate of the maximum number of characters
required by the Printer output routines. */
CheckOut(fmtTotal>80? fmtTotal : 80);
outBuf[0]= '\0';
if (!sinkOp)
/* this is swrite call -- it's time to create the result array */
result= PushDataBlock(NewArray(&stringStruct, pDims));
else
result= 0;
/* outer loop is on output array elements */
nLine= 0;
fmtTotal= 0;
for (i=0 ; i<number ; i++) {
#ifdef FAKE_INTERRUPTS
if (check_brk_flag) check_except();
#endif
/* inner loop is on arguments to be written */
text= outBuf;
text[0]= '\0';
nChars= 0;
for (j=0 ; j<nArgs ; j++) {
if (fmtList[j].type == IO_CHAR)
PrintC[fmtList[j].typeID](&ioOps[j], fmtList[j].format, text);
else if (fmtList[j].danger)
PrintD[fmtList[j].typeID-T_FLOAT](&ioOps[j], fmtList[j].format, text);
else
Printer[fmtList[j].typeID](&ioOps[j], fmtList[j].format, text);
nChars= (int)strlen(text); /* can't rely on sprintf to return this */
text+= nChars;
fmtTotal+= nChars;
nLine+= nChars;
}
/* one result printed/stored per array element */
if (!sinkOp) {
result->value.q[i]= StrCpy(outBuf);
} else {
/* YPrompt does NOT append \n, as desired (YputsOut does) */
if (i && ((nArgs>nConversions && nArgs>1) || nLine>lineSize)) {
if (sinkOp->ops) fputs("\n", file);
else YPrompt("\n");
fmtTotal++;
nLine= strlen(outBuf);
}
if (sinkOp->ops) fputs(outBuf, file);
else YPrompt(outBuf);
if (nChars && text[-1]=='\n') nLine= 0;
}
}
/* add trailing newline if it seems reasonable to do so */
if (sinkOp && nArgs>nConversions) {
if (sinkOp->ops) fputs("\n", file);
else YPrompt("\n");
fmtTotal++;
}
/* release excessive temporary space */
FreeFormats();
CheckOut(0L);
/* if this is write (not swrite), result is character count */
if (!result) PushLongValue(fmtTotal);
}
/*--------------------------------------------------------------------------*/
static long aFileID= 0; /* unique file ID number for ASCII files */
IOFileLink *yTextFiles= 0;
/* Set up a block allocator which grabs space for 16 TextStream objects
at a time. Since TextStream contains several pointers, the alignment
of an TextStream must be at least as strict as a void*. */
static MemoryBlock txtsBlock= {0, 0, sizeof(TextStream),
16*sizeof(TextStream)};
TextStream *NewTextStream(char *fullname, void *stream, int permissions,
long line, long pos)
{
TextStream *ios= NextUnit(&txtsBlock);
FILE *file= stream;
ios->references= 0;
ios->ops= &textOps;
ios->stream= file;
ios->fullname= fullname;
ios->permissions= permissions;
ios->lastLineRead= line;
ios->readPosition= ios->lastPosition= pos;
if (file && !(permissions&16) &&
fseek(file, pos, SEEK_SET)) ios->permissions|= 16;
ios->readWrite= 0;
ios->fileID= aFileID++;
AddIOLink(&yTextFiles, ios);
return ios;
}
void FreeTextStream(void *ios)
{
TextStream *io= ios;
FILE *stream= io->stream;
if (stream) fclose(stream);
StrFree(io->fullname);
RemoveIOLink(yTextFiles, io);
FreeUnit(&txtsBlock, io);
}
static char *txStatus[]=
{ "<illegal>", "read-only", "write-only", "read-write" };
static void PrintTX(Operand *op)
{
TextStream *ts= op->value;
long line= ts->lastLineRead+1;
ForceNewline();
if (ts->stream) {
char text[32];
sprintf(text, "%s text stream at:", txStatus[ts->permissions&3]);
PrintFunc(text);
} else {
PrintFunc("text stream <closed> was:");
line= 0;
}
ForceNewline();
PrintFunc(MakeErrorLine(line, ts->fullname));
ForceNewline();
}
/* Set up a block allocator which grabs space for 16 bookmark objects
at a time. Since Bookmark contains an ops pointer, the alignment
of a Bookmark must be at least as strict as a void*. */
static MemoryBlock bookBlock= {0, 0, sizeof(Bookmark),
16*sizeof(Bookmark)};
Bookmark *NewBookmark(long line, long last, long next, long id)
{
Bookmark *bookmark= NextUnit(&bookBlock);
bookmark->references= 0;
bookmark->ops= &bookOps;
bookmark->lastLineRead= line;
bookmark->lastPosition= last;
bookmark->readPosition= next;
bookmark->fileID= id;
return bookmark;
}
void FreeBookmark(void *bm) /* ******* Use Unref(bm) ******* */
{
FreeUnit(&bookBlock , bm);
}
static void PrintBM(Operand *op)
{
Bookmark *bm= op->value;
IOFileLink *iofl;
for (iofl=yTextFiles ; iofl ; iofl=iofl->next)
if (((TextStream *)iofl->ios)->fileID == bm->fileID) break;
if (iofl) {
TextStream *ts= iofl->ios;
ForceNewline();
PrintFunc("bookmark at:");
ForceNewline();
PrintFunc(MakeErrorLine(bm->lastLineRead+1, ts->fullname));
ForceNewline();
} else {
PrintFunc("<lost bookmark>");
}
}
void Y_bookmark(int nArgs)
{
Operand op;
TextStream *ios;
Bookmark *bm;
if (nArgs!=1) YError("bookmark function takes exactly one argument");
sp->ops->FormOperand(sp, &op);
ios= op.value;
if (op.ops!=&textOps)
YError("argument to bookmark function not a text stream");
if (ios->permissions&16) YError("can't place a bookmark in a pipe");
bm= PushDataBlock(NewBookmark(ios->lastLineRead, ios->lastPosition,
ios->readPosition, ios->fileID));
}
void Y_backup(int nArgs)
{
Operand op;
TextStream *ios;
Bookmark *bm= 0;
FILE *file;
if (nArgs!=1 && nArgs!=2)
YError("backup function takes exactly one or two arguments");
if (nArgs==2) {
sp->ops->FormOperand(sp, &op);
if (op.ops==&bookOps)
bm= op.value;
else if (op.ops!=&voidOps)
YError("2nd argument to backup function is not nil or bookmark");
Drop(1);
}
sp->ops->FormOperand(sp, &op);
ios= op.value;
if (op.ops!=&textOps)
YError("1st argument to backup function not a text stream");
if (ios->permissions&16) YError("can't backup a pipe");
file= ios->stream;
/* don't try to detect no-op, as side effect of this routine is
to ensure that fseek is called to make a read operation
legal (previous operation may have been a write) */
if (bm) {
/* reset state to bookmark */
struct IOFileLink *iofl;
for (iofl=yTextFiles ; iofl ; iofl=iofl->next)
if (((TextStream *)iofl->ios)->fileID == bm->fileID) break;
if (!iofl) YError("no file for bookmark passed to backup function");
if (iofl->ios!=ios)
YError("wrong file for bookmark passed to backup function");
if (fseek(file, bm->readPosition, SEEK_SET))
YErrorIO("fseek failed in backup function");
ios->lastLineRead= bm->lastLineRead;
ios->lastPosition= bm->lastPosition;
ios->readPosition= bm->readPosition;
} else {
/* back up to previous line */
if (fseek(file, ios->lastPosition, SEEK_SET))
YErrorIO("fseek failed in backup function");
ios->readPosition= ios->lastPosition;
ios->lastLineRead--;
}
ios->readWrite= 1; /* fseek equivalent to read here */
}
/*--------------------------------------------------------------------------*/
static char *NextInLine(Operand *op, long n)
{
char *text;
if (!op->ops) {
char *prompt= op->value;
YPrompt(prompt);
/* This will not block other input sources if no keyboard input
is available. */
text= Ygets(&inputBuffer, (FILE *)0);
} else if (op->ops==&textOps) {
TextStream *stream= op->value;
FILE *file= stream->stream;
text= Ygets(&inputBuffer, file);
if (!text) {
int hadError= Yferror(file);
int hadEOF= Yfeof(file);
clearerr(file); /* don't prejudice later I/O attempts */
if (hadError)
YErrorIO("****ABORTING READ**** error reading input file");
if (!hadEOF)
YErrorIO("****ABORTING READ**** input file not ASCII text");
}
stream->lastLineRead++;
stream->lastPosition= stream->readPosition;
if (!(stream->permissions&16)) stream->readPosition= ftell(file);
} else if (op->ops==&stringOps) {
char **q= op->value;
if (n<op->type.number) text= q[n];
else text= 0;
} else {
YError("(BUG) impossible operand to NextInLine");
text= 0;
}
return text;
}
/*--------------------------------------------------------------------------*/
#undef OPERATION
#define OPERATION(opname, type1, type2) \
static int opname(Operand *op, char *format, char **text) \
{ \
type1 *x= op->value; \
type2 v; int i, n; \
if (*text) { \
i= sscanf(*text, format, &v, &n); \
if (i==1) { *x= v; fmtAssigns++; *text+= n; i= 0; \
} else if (i==0) { *text= 0; i= 1; \
} else { *text+= strlen(*text); i= 1; \
} \
} else { \
*x= 0; i= 0; \
} \
if (!i) op->value= x+1; \
return i; \
}
OPERATION(CScanner, unsigned char, long)
OPERATION(SScanner, short, long)
OPERATION(IScanner, int, long)
OPERATION(LScanner, long, long)
/* modify floating point read operations to cope with Fortran
"D" exponent format */
static int retry_sscanf(char *text, char *format, double *pv, int *pn);
#undef OPERATION
#define OPERATION(opname, type1, type2) \
static int opname(Operand *op, char *format, char **text) \
{ \
type1 *x= op->value; \
type2 v; int i, n; \
if (*text) { \
i= retry_sscanf(*text, format, &v, &n); \
if (i==1) { *x= v; fmtAssigns++; *text+= n; i= 0; \
} else if (i==0) { *text= 0; i= 1; \
} else { *text+= strlen(*text); i= 1; \
} \
} else { \
*x= 0; i= 0; \
} \
if (!i) op->value= x+1; \
return i; \
}
OPERATION(FScanner, float, double)
OPERATION(DScanner, double, double)
static int QScanner(Operand *op, char *format, char **text)
{
char **x= op->value;
char *v; int i, n;
if (*x) { StrFree(*x); *x= 0; }
if (*text) {
long len= strlen(*text);
v= CheckBuf(len); /* allow enough space for worst case */
i= sscanf(*text, format, v, &n);
if (i==1) { *x= StrCpy(v); fmtAssigns++; *text+= n; i= 0;
} else if (i==0) { *text= 0; i= 1;
} else { *text+= len; i= 1;
}
} else {
i= 0;
}
if (!i) op->value= x+1;
return i;
}
/* ARGSUSED */
static int NilScanner(Operand *op, char *format, char **text)
{
if (!*text) return 0;
if (strcmp(format,*text)) {
*text= 0;
return 1;
} else {
*text+= strlen(*text);
return 0;
}
}
/* Try to recognize things like 1.234d-21 or 1.234D-21 that
Fortran emits.
The only reasonably cheap recourse is also a bit dangerous:
We temporarily modify the text buffer and attempt a
rescan. Of course, if we're interrupted before we restore
the modified text buffer, we may have modified the caller's
text illegally.
Hopefully, a significant performance penalty accrues only if
the text you are reading contains things like "1.234e-21dumb".
However, a file full of "1.234D-21" style numbers will take
twice as long to read as one with ANSI C acceptable formats. */
static int retry_sscanf(char *text, char *format, double *pv, int *pn)
{
int i= sscanf(text, format, pv, pn);
int n= i? *pn : 0;
char c= text[n];
if (i==1 && (c=='D' || c=='d')) {
/* this may represent only limited success if the scan
stopped at a "d" or "D" character */
text[n]= 'e';
i= sscanf(text, format, pv, pn);
text[n]= c;
}
return i;
}
/*--------------------------------------------------------------------------*/
#undef OPERATION
#define OPERATION(opname, type1, type2) \
static void opname(Operand *op, char *format, char *text) \
{ \
type1 *x= op->value; \
type2 v= *x; op->value= x+1; \
sprintf(text, format, v); \
}
OPERATION(CPrinter, unsigned char, long)
OPERATION(SPrinter, short, long)
OPERATION(IPrinter, int, long)
OPERATION(LPrinter, long, long)
OPERATION(FPrinter, float, double)
OPERATION(DPrinter, double, double)
OPERATION(PPrinter, void *, void *)
static void QPrinter(Operand *op, char *format, char *text)
{
char **x= op->value;
char *v= *x; op->value= x+1;
sprintf(text, format, v? v : "");
}
#undef OPERATION
#define OPERATION(opname, type1) \
static void opname(Operand *op, char *format, char *text) \
{ \
type1 *x= op->value; \
double v= *x; op->value= x+1; \
if (v>1.e15) v= 1.e15; else if (v<-1.e15) v= -1.e15; \
sprintf(text, format, v); \
}
OPERATION(FPrintD, float)
OPERATION(DPrintD, double)
#undef OPERATION
#define OPERATION(opname, type1) \
static void opname(Operand *op, char *format, char *text) \
{ \
type1 *x= op->value; \
int v= *x; op->value= x+1; \
sprintf(text, format, v); \
}
OPERATION(CPrintC, unsigned char)
OPERATION(SPrintC, short)
OPERATION(IPrintC, int)
OPERATION(LPrintC, long)
/*--------------------------------------------------------------------------*/
static void CheckOps(int nArgs)
{
if (nArgs >= maxIOops) {
long newMax= maxIOops+16;
while (nArgs >= newMax) newMax+= 16;
ioOps= Yrealloc(ioOps, sizeof(Operand)*newMax);
maxIOops= newMax;
}
}
static void AddFormat(char *format, int type, int danger)
{
if (fmtNow >= fmtMax) {
fmtList= Yrealloc(fmtList, sizeof(struct FormatList)*(fmtMax+16));
fmtMax+= 16;
}
fmtList[fmtNow].format= StrCpy(format);
fmtList[fmtNow].type= type;
fmtList[fmtNow].typeID= -1;
fmtList[fmtNow++].danger= danger;
}
static void FreeFormats(void)
{
while (fmtNow) StrFree(fmtList[--fmtNow].format);
if (fmtMax>32) {
fmtList= Yrealloc(fmtList, sizeof(struct FormatList)*32);
fmtMax= 32;
}
if (maxIOops>32) {
ioOps= Yrealloc(ioOps, sizeof(Operand)*32);
maxIOops= 32;
}
}
static int CrackFormat(char *format, char *(*Cracker)(char *))
{
int nConversions= 0;
fmtTotal= 0;
/* free fmtList left over from last time, if any */
while (fmtNow) StrFree(fmtList[--fmtNow].format);
/* Use either CrackScan or CrackPrint to split the format into pieces
containing a single conversion specification corresponding to one
item in the read or write argument list. After this, either
fmtNow==nConversions, or possibly fmtNow==1 and nConversions==0. */
if (format) {
while (format[0]) {
format= Cracker(format);
if (fmtType!=IO_NONE) nConversions++;
AddFormat(fmtBuf, fmtType, fmtDanger);
fmtTotal+= fmtWidth;
}
}
return nConversions;
}
static char *CheckBuf(long len)
{
if (len+4 > fmtLen) {
long newSize= 80*(1 + (len+4)/80);
fmtBuf= Yrealloc(fmtBuf, newSize);
fmtLen= newSize;
} else if (fmtLen>80 && len+4<=80) {
fmtBuf= Yrealloc(fmtBuf, 80L);
fmtLen= 80L;
}
return fmtBuf; /* used by CrackScan and CrackPrint for format strings */
}
static char *CheckOut(long len)
{
if (len > outLen) {
long newSize= 256*(1 + (len-1)/256);
outBuf= Yrealloc(outBuf, newSize);
outLen= newSize;
} else if (outLen>512 && len<=512) {
outBuf= Yrealloc(outBuf, 512L);
outLen= 512L;
}
return outBuf; /* used by Printer routines to hold sprintf results */
}
static char *CrackScan(char *format)
{
int got_one;
long i, n;
char *part= CheckBuf(strlen(format));
part[0]= '\0';
fmtType= IO_NONE;
got_one= 0;
fmtWidth= 0;
fmtDanger= 0;
while (!got_one) { /* loop on conversion specifiers which do not assign */
/* copy format until first conversion specifier */
i= strcspn(format, "%");
strncat(part, format, i);
part+= i;
format+= i;
if (got_one || !format[0]) break;
*part++= '%';
*part= '\0';
format++;
/* find conversion type character */
i= strcspn(format, "diouxXfeEgGs[cpn%");
if (!format[i]) {
strncat(part, format, i);
break;
}
got_one= (format[i]!='%' && format[0]!='*');
switch (format[i]) {
case '%':
i++;
strncat(part, format, i);
part+= i;
format+= i;
break;
case '[':
if (format[i+1]==']') i+= 2;
else if (format[i+1]=='^' && format[i+2]==']') i+= 3;
i+= strcspn(&format[i], "]");
case 's': /* actually can use case drop through here... */
i++;
strncat(part, format, i);
part+= i;
format+= i;
fmtType= IO_STRING;
break;
case 'd': case 'i': case 'o': case 'u': case 'x': case 'X':
/* all integers are handled as longs */
if (format[i-1]=='h' || format[i-1]=='l') n= i-1;
else n= i;
strncat(part, format, n);
part+= n;
*part++= 'l';
*part++= format[i];
*part= '\0';
format+= i+1;
fmtType= IO_LONG;
break;
case 'e': case 'E': case 'f': case 'g': case 'G':
/* all reals are handled as doubles */
if (format[i-1]=='h' || format[i-1]=='l' ||
format[i-1]=='L') n= i-1;
else n= i;
strncat(part, format, n);
part+= n;
*part++= 'l';
*part++= format[i];
*part= '\0';
format+= i+1;
fmtType= IO_DOUBLE;
break;
case 'p':
YError("Yorick read cannot handle %p format, use %i");
break;
case 'c':
YError("Yorick read cannot handle %c format, use %1s or %1[...]");
break;
case 'n':
YError("Yorick read cannot handle %n format");
break;
}
}
/* append final character count to be able to advance input pointer */
if (got_one) strcat(part, "%n");
return format;
}
static char *CrackPrint(char *format)
{
int got_one;
long i, n;
char *part= CheckBuf(strlen(format));
part[0]= '\0';
fmtType= IO_NONE;
got_one= 0;
fmtWidth= 0;
fmtDanger= 0;
for (;;) { /* loop on conversion specifiers which do not eat arguments */
/* copy format until first conversion specifier */
i= strcspn(format, "%");
while (format[i]=='%' && format[i+1]=='%')
i+= 2+strcspn(format+i+2, "%"); /* skip %% immediately */
strncat(part, format, i);
part+= i;
format+= i;
if (got_one || !format[0]) break;
*part++= '%';
*part= '\0';
format++;
/* find conversion type character */
i= strcspn(format, "diouxXfeEgGscpn");
if (!format[i]) break;
for (n=0 ; n<i ; n++) {
if (format[n] == '*')
YError("Yorick write cannot handle %*.* format, compute format");
if (!fmtWidth && format[n]>='1' && format[n]<='9') {
/* get minimum field width, if specified */
fmtWidth= format[n]-'0';
for (n++ ; n<i && format[n]>='0' && format[n]<='9' ; n++)
fmtWidth= 10*fmtWidth + format[n]-'0';
}
}
got_one= 1;
switch (format[i]) {
case 's':
i++;
strncat(part, format, i);
part+= i;
format+= i;
fmtType= IO_STRING;
break;
case 'd': case 'i': case 'o': case 'u': case 'x': case 'X':
/* all integers are handled as longs */
if (format[i-1]=='h' || format[i-1]=='l') n= i-1;
else n= i;
strncat(part, format, n);
part+= n;
*part++= 'l';
*part++= format[i];
*part= '\0';
format+= i+1;
fmtType= IO_LONG;
break;
case 'f':
fmtDanger= 1;
case 'e': case 'E': case 'g': case 'G':
/* all reals are handled as doubles */
if (format[i-1]=='L') n= i-1;
else n= i;
strncat(part, format, n);
part+= n;
*part++= format[i];
*part= '\0';
format+= i+1;
fmtType= IO_DOUBLE;
break;
case 'c':
i++;
strncat(part, format, i);
part+= i;
format+= i;
fmtType= IO_CHAR;
break;
case 'p':
i++;
strncat(part, format, i);
part+= i;
format+= i;
fmtType= IO_POINTER;
break;
case 'n':
YError("Yorick write cannot handle %n format, use strlen(swrite())");
break;
}
}
fmtWidth+= strlen(fmtBuf);
return format;
}
/*--------------------------------------------------------------------------*/
void YErrorIO(const char *msg)
{
extern int y_catch_category;
y_catch_category= 0x02;
YError(msg);
}
/*--------------------------------------------------------------------------*/
void Y_popen(int nArgs)
{
char *command;
int mode;
TextStream *stream;
FILE *file;
if (nArgs!=2) YError("popen needs exactly two arguments");
#ifndef NO_POPEN
command= YGetString(sp-1);
mode= (int)YGetInteger(sp);
Drop(1);
file= popen(command, mode?"w":"r");
if (!file) YError("system popen function failed");
PushDataBlock(NewTextStream(StrCpy(command), file, mode?50:49, 0L, 0L));
#else
YError("popen function not implemented on this platform");
#endif
}
void Y_fflush(int nArgs)
{
TextStream *ts;
Operand op;
if (nArgs!=1) YError("fflush needs exactly one argument");
sp->ops->FormOperand(sp, &op);
if (sp->value.db->ops!=&textOps)
YError("fflush only works for text files");
ts= (TextStream *)sp->value.db;
if (ts->stream) fflush(ts->stream);
}
/*--------------------------------------------------------------------------*/
|