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
|
/*
* Copyright © 1988-2004 Keith Packard and Bart Massey.
* All Rights Reserved. See the file COPYING in this directory
* for licensing information.
*/
/*
* file.c
*
* provide builtin functions for the File namespace
*/
#include <ctype.h>
#include <strings.h>
#include <time.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "builtin.h"
NamespacePtr FileNamespace;
void
import_File_namespace(void)
{
ENTER ();
static const struct fbuiltin_0 funcs_0[] = {
{ do_File_string_write, "string_write", "f", "", "\n"
" file string_write ()\n"
"\n"
" Returns a writable file which can be converted to\n"
" a string with string_string.\n"
},
{ do_File_mkpipe, "mkpipe", "A*f", "", "\n"
" file[*] mkpipe ()\n"
"\n"
" Returns an array of two file objects representing\n"
" the endpoints of a pipe.\n"
" Data written to [1] will be available for reading on [0].\n" },
{ 0 }
};
static const struct fbuiltin_1 funcs_1[] = {
{ do_File_clear_error, "clear_error", "v", "f", "\n"
" void clear_error (file f)\n"
"\n"
" Clear any error or end-of-file condition on 'f'.\n" },
{ do_File_close, "close", "v", "f", "\n"
" void close (file f)\n"
"\n"
" Close 'f'. Note that files will automatically be closed\n"
" at some point after they become unreachable. This function\n"
" simply provides a mechanism for causing it to happen at\n"
" a specific point in time.\n" },
{ do_File_end, "end", "b", "f", "\n"
" bool end (file f)\n"
"\n"
" Returns true if 'f' is at the end of file.\n"
" This often entails attempting to read a character from 'f'\n"
" if the end has not already been discovered.\n" },
{ do_File_error, "error", "b", "f", "\n"
" bool error (file f)\n"
"\n"
" Returns true if 'f' has an error condition set.\n" },
{ do_File_flush, "flush", "v", "f", "\n"
" void flush (file f)\n"
"\n"
" Force any pending output to be delivered to the OS.\n" },
{ do_File_getb, "getb", "i", "f", "\n"
" int getb (file f)\n"
"\n"
" Return the next byte of data from 'f'.\n" },
{ do_File_getc, "getc", "i", "f", "\n"
" int getc (file f)\n"
"\n"
" Return the next character from 'f'.\n" },
{ do_File_string_read, "string_read", "f", "s", "\n"
" file string_read (string s)\n"
"\n"
" Create a file which when read will return successive"
" characters from 's'.\n" },
{ do_File_string_string, "string_string", "s", "f", "\n"
" string string_string (file f)\n"
"\n"
" Returns the contents 'f', which must be a file that was\n"
" created with string_write.\n" },
{ do_File_isatty, "isatty", "b", "f", "\n"
" bool isatty (file f)\n"
"\n"
" Return whether 'f' is associated with an interactive\n"
" terminal device\n" },
{ do_File_unlink, "unlink", "v", "s", "\n"
" void unlink (string name)\n"
"\n"
" Delete the filename 'name'\n" },
{ do_File_rmdir, "rmdir", "v", "s", "\n"
" void rmdir (string name)\n"
"\n"
" Delete the directory 'name'\n" },
{ 0 }
};
static const struct fbuiltin_2 funcs_2[] = {
{ do_File_open, "open", "f", "ss", "\n"
" file open (string name, string mode)\n"
"\n"
" Open file 'name' where 'mode' is one of:\n"
" \"r\": read only\n"
" \"r+\": read-write\n"
" \"w\": write only (created or truncated)\n"
" \"w+\": read-write (created or truncated)\n"
" \"a\": write-only for appending (created if needed)\n"
" \"a+\": read-write for appending (created if needed)\n"
" Raises open_error if the file cannot be opened.\n" },
{ do_File_putb, "putb", "i", "if", "\n"
" int putb (int b, file f)\n"
"\n"
" Write the byte 'b' to 'f'.\n" },
{ do_File_putc, "putc", "i", "if", "\n"
" int putc (int c, file f)\n"
"\n"
" Write the character 'c' to 'f'.\n" },
{ do_File_setbuf, "setbuffer", "i", "fi", "\n"
" int setbuffer (file f, int mode)\n"
"\n"
" Change buffering of 'f' to 'mode', which is one of:\n"
" 0: normal block buffering.\n"
" 1: line buffering.\n"
" 2: unbuffered.\n"
" Returns 'mode'.\n" },
{ do_File_ungetb, "ungetb", "i", "if", "\n"
" int ungetb (int b, file f)\n"
"\n"
" Pushes the byte 'b' back on file 'f'.\n" },
{ do_File_ungetc, "ungetc", "i", "if", "\n"
" int ungetc (int c, file f)\n"
"\n"
" Pushes the character 'c' back on file 'f'.\n" },
{ do_File_rename, "rename", "v", "ss", "\n"
" void rename (string oldname, string newname)\n"
"\n"
" Renames a file\n" },
{ do_File_mkdir, "mkdir", "v", "si", "\n"
" void mkdir (string name, int mode)\n"
"\n"
" Create the new directory 'name'\n" },
{ 0 }
};
static const struct fbuiltin_3 funcs_3[] = {
{ do_File_filter, "filter", "i", "sA*sA*f", "\n"
" int filter (string program, string[*] argv, file[3] f)\n"
"\n"
" Fork and execute 'program' using 'argv' for arguments and\n"
" f as stdin, stdout and stderr (respectively).\n" },
{ do_File_reopen, "reopen", "f", "ssf", "\n"
" file reopen (string name, string mode, file f)\n"
"\n"
" Change which file 'f' is associated with to 'name',\n"
" opening it with 'mode' as in the open function.\n" },
{ 0 }
};
static const struct fbuiltin_7 funcs_7[] = {
{ do_File_print, "print", "v", "fpsiiis", "\n"
" void print (file f, poly v, string format, int base,\n"
" int width, int precision, string fill)\n"
"\n"
" Print 'v' to 'f'.\n"
" 'format' is one of:\n"
" \"v\": Reparsable representation.\n"
" \"g\": Human readable representation.\n"
" \"d\": Integer portion of a number.\n"
" \"f\": iii.fff format floating point.\n"
" \"e\": i.fff'e'ee format floating point.\n"
" \"c\": Integer printed as a character.\n"
" \"s\": String printed without quotes.\n"
" Any number will be represented using 'base' numerals.\n"
" The output will be at most 'width' characters unless it\n"
" won't fit in that size.\n"
" Any decimal part will be limited to 'precision' characters.\n"
" If 'precision' is -1, precision will be set to the default (10)\n"
" If 'precision' is -2, precision will set to whatever is needed \n"
" to precisely represent the number.\n"
" Any extra characters will be filled with 'fill'.\n" },
{ 0 }
};
static const struct ebuiltin excepts[] = {
{"open_error", exception_open_error, "sEs", "\n"
" open_error (string message, error_type error, string name)\n"
"\n"
" Raised when an open attempt fails.\n"
" 'message' is a printable error string.\n"
" 'error' is a symbolic error code.\n"
" 'name' is the filename which failed.\n" },
{"io_error", exception_io_error, "sEf", "\n"
" io_error (string message, error_type error, file f)\n"
"\n"
" Raised when an i/o error occurs.\n" },
{"name_error", exception_name_error, "sEs", "\n"
" name_error (string message, error_type error, string name)\n"
"\n"
" Raised when an operation involving a filename fails.\n"
" 'message' is a printable error string.\n"
" 'error' is a symbolic error code.\n"
" 'name' is the filename which failed.\n" },
{"io_eof", exception_io_eof, "f", "\n"
" io_eof (file f)\n"
"\n"
" Raised when reading at end-of-file.\n"
" 'file' is the file at eof.\n" },
{0, 0, 0, 0 },
};
const struct ebuiltin *e;
SymbolPtr s;
FileNamespace = BuiltinNamespace (/*parent*/ 0, "File")->namespace.namespace;
BuiltinFuncs0 (&FileNamespace, funcs_0);
BuiltinFuncs1 (&FileNamespace, funcs_1);
BuiltinFuncs2 (&FileNamespace, funcs_2);
BuiltinFuncs3 (&FileNamespace, funcs_3);
BuiltinFuncs7 (&FileNamespace, funcs_7);
for (e = excepts; e->name; e++)
BuiltinAddException (&FileNamespace, e->exception, e->name, e->args, e->doc);
s = typeFileError->name.name;
NamespaceAddName (FileNamespace, s, publish_public);
s = NewSymbolType (AtomId("errorType"), typeFileError);
NamespaceAddName (FileNamespace, s, publish_public);
EXIT ();
}
Value
do_File_print (Value file, Value value, Value format,
Value base, Value width, Value prec, Value fill)
{
int ibase, iwidth, iprec;
ibase = IntPart (base, "Illegal base");
if (aborting)
return Void;
if (ibase < 0 || ibase == 1)
{
RaiseStandardException (exception_invalid_argument, 3,
NewStrString ("Illegal base"),
NewInt (0), base);
return Void;
}
iwidth = IntPart (width, "Illegal width");
if (aborting)
return Void;
iprec = IntPart (prec, "Illegal precision");
if (aborting)
return Void;
if (file->file.flags & FileOutputBlocked)
ThreadSleep (running, file, PriorityIo);
else
{
Print (file, value,
StringGet (StringChars (&format->string),
format->string.length, 0),
ibase, iwidth,
iprec,
StringGet (StringChars (&fill->string),
fill->string.length, 0));
if (file->file.flags & FileOutputError)
{
RaiseStandardException (exception_io_error, 3,
FileGetErrorMessage (file->file.output_errno),
FileGetError (file->file.output_errno),
file);
}
}
return Void;
}
Value
do_File_open (Value name, Value mode)
{
ENTER ();
char *n, *m;
Value ret;
int err;
n = StrzPart (name, "invalid file name");
if (!n)
RETURN (Void);
m = StrzPart (mode, "invalid file mode");
if (!m)
RETURN (Void);
if (aborting)
RETURN (Void);
ret = FileFopen (n, m, &err);
if (!ret)
{
RaiseStandardException (exception_open_error, 3,
FileGetErrorMessage (err),
FileGetError (err),
name);
RETURN (Void);
}
complete = True;
RETURN (ret);
}
Value
do_File_flush (Value f)
{
switch (FileFlush (f, False)) {
case FileBlocked:
ThreadSleep (running, f, PriorityIo);
break;
case FileError:
RaiseStandardException (exception_io_error, 3,
FileGetErrorMessage (f->file.output_errno),
FileGetError (f->file.output_errno), f);
break;
}
return Void;
}
Value
do_File_close (Value f)
{
if (aborting)
return Void;
switch (FileFlush (f, False)) {
case FileBlocked:
ThreadSleep (running, f, PriorityIo);
break;
case FileError:
RaiseStandardException (exception_io_error, 3,
FileGetErrorMessage (f->file.output_errno),
FileGetError (f->file.output_errno), f);
break;
default:
if (FileClose (f) == FileError)
{
RaiseStandardException (exception_io_error, 3,
FileGetErrorMessage (f->file.output_errno),
FileGetError (f->file.output_errno), f);
}
else
complete = True;
}
return Void;
}
Value
do_File_filter (Value path, Value argv, Value filev)
{
ENTER ();
char *p = StrzPart (path, "invalid program path");
char **args;
int argc;
Value arg;
Value ret;
int err;
if (!p)
RETURN (Void);
/* set up arguments */
args = AllocateTemp ((ArrayLimits(&argv->array)[0] + 1) * sizeof (char *));
for (argc = 0; argc < ArrayLimits(&argv->array)[0]; argc++)
{
arg = ArrayValue (&argv->array, argc);
args[argc] = StrzPart (arg, "invalid argument");
if (!args[argc])
RETURN (Void);
}
args[argc] = 0;
/* run the filter */
if (aborting)
RETURN(Void);
ret = FileFilter (p, args, filev, &err);
if (!ret)
{
RaiseStandardException (exception_open_error, 3,
FileGetErrorMessage (err),
FileGetError (err), path);
ret = Void;
}
complete = True;
RETURN (ret);
}
Value do_File_mkpipe (void) {
ENTER ();
int err;
Value ret;
if (aborting)
RETURN (Void);
ret = FileMakePipe (&err);
if (!ret)
{
RaiseStandardException (exception_open_error, 3,
FileGetErrorMessage (err),
FileGetError (err), Void);
RETURN (Void);
}
RETURN (ret);
}
Value
do_File_reopen (Value name, Value mode, Value file)
{
ENTER ();
char *n, *m;
Value ret;
int err;
n = StrzPart (name, "invalid file name");
if (!n)
RETURN (Void);
m = StrzPart (mode, "invalid file mode");
if (!m)
RETURN (Void);
if (aborting)
RETURN (Void);
ret = FileReopen (n, m, file, &err);
if (!ret)
{
RaiseStandardException (exception_open_error, 3,
FileGetErrorMessage (err),
FileGetError (err), name);
RETURN (Void);
}
complete = True;
RETURN (ret);
}
Value
do_File_string_read (Value s)
{
ENTER ();
RETURN (FileStringRead (StringChars (&s->string), s->string.length));
}
Value
do_File_string_write (void)
{
ENTER ();
RETURN (FileStringWrite ());
}
Value
do_File_string_string (Value f)
{
ENTER ();
RETURN (FileStringString (f));
}
Value
do_File_isatty (Value file)
{
ENTER ();
if (file->file.flags & FileString)
return FalseVal;
RETURN(isatty (file->file.fd) ? TrueVal : FalseVal);
}
Value
do_File_getb (Value f)
{
ENTER ();
int c;
if (!aborting)
{
c = FileInput (f);
switch (c) {
case FileBlocked:
ThreadSleep (running, f, PriorityIo);
RETURN (Void);
case FileError:
RaiseStandardException (exception_io_error, 3,
FileGetErrorMessage (f->file.input_errno),
FileGetError (f->file.input_errno), f);
RETURN (Void);
default:
complete = True;
RETURN (NewInt (c));
}
}
RETURN (Void);
}
Value
do_File_getc (Value f)
{
ENTER ();
int c;
if (!aborting)
{
c = FileInchar (f);
switch (c) {
case FileBlocked:
ThreadSleep (running, f, PriorityIo);
RETURN (Void);
case FileError:
RaiseStandardException (exception_io_error, 3,
FileGetErrorMessage (f->file.input_errno),
FileGetError (f->file.input_errno), f);
RETURN (Void);
case FileEOF:
RaiseStandardException (exception_io_eof, 1, f);
RETURN (Void);
default:
complete = True;
RETURN (NewInt (c));
}
}
RETURN (Void);
}
Value
do_File_end (Value f)
{
ENTER ();
if (f->file.flags & FileEnd)
RETURN (TrueVal);
else
{
Value b = do_File_getb(f);
if (b == Void)
RETURN(Void);
do_File_ungetb (b, f);
if (f->file.flags & FileEnd)
RETURN (TrueVal);
RETURN (FalseVal);
}
}
Value
do_File_error (Value f)
{
ENTER ();
if (f->file.flags & (FileInputError|FileOutputError))
RETURN (TrueVal);
else
RETURN (FalseVal);
}
Value
do_File_clear_error (Value f)
{
ENTER ();
f->file.flags &= ~(FileInputError|FileOutputError|FileEnd);
RETURN (Void);
}
Value
do_File_putb (Value v, Value f)
{
ENTER ();
if (f->file.flags & FileOutputBlocked)
ThreadSleep (running, f, PriorityIo);
else
{
if (!aborting)
{
if (FileOutput (f, IntPart (v, "putb non integer")) == FileError)
{
RaiseStandardException (exception_io_error, 3,
FileGetErrorMessage (f->file.output_errno),
FileGetError (f->file.output_errno),
f);
}
else
complete = True;
}
}
RETURN (v);
}
Value
do_File_putc (Value v, Value f)
{
ENTER ();
if (f->file.flags & FileOutputBlocked)
ThreadSleep (running, f, PriorityIo);
else
{
if (!aborting)
{
if (FileOutchar (f, IntPart (v, "putc non integer")) == FileError)
{
RaiseStandardException (exception_io_error, 3,
FileGetErrorMessage (f->file.output_errno),
FileGetError (f->file.output_errno),
f);
}
else
complete = True;
}
}
RETURN (v);
}
Value
do_File_ungetb (Value v, Value f)
{
ENTER ();
if (!aborting)
{
complete = True;
FileUnput (f, IntPart (v, "ungetb: non integer"));
}
RETURN (v);
}
Value
do_File_ungetc (Value v, Value f)
{
ENTER ();
if (f->file.flags & FileOutputBlocked)
ThreadSleep (running, f, PriorityIo);
else
{
if (!aborting)
{
complete = True;
FileUnchar (f, IntPart (v, "ungetc: non integer"));
}
}
RETURN (v);
}
Value
do_File_setbuf (Value f, Value v)
{
ENTER ();
int i;
i = IntPart (v, "setbuffer non integer");
if (!aborting)
FileSetBuffer (f, i);
RETURN (v);
}
Value
do_File_unlink (Value name)
{
ENTER ();
char *n;
int ret;
n = StrzPart (name, "invalid file name");
if (!n)
RETURN (Void);
ret = unlink (n);
if (ret < 0) {
int err = errno;
RaiseStandardException (exception_name_error, 3,
FileGetErrorMessage (err),
FileGetError (err), name);
RETURN (Void);
}
RETURN (Void);
}
Value
do_File_rename (Value old, Value new)
{
ENTER ();
char *o, *n;
int ret;
o = StrzPart (old, "invalid file name");
if (!o)
RETURN (Void);
n = StrzPart (new, "invalid file name");
if (!n)
RETURN (Void);
ret = rename (o, n);
if (ret < 0) {
int err = errno;
RaiseStandardException (exception_name_error, 3,
FileGetErrorMessage (err),
FileGetError (err), new);
RETURN (Void);
}
RETURN (Void);
}
Value
do_File_mkdir (Value name, Value mode)
{
ENTER ();
char *n;
int m;
int ret;
n = StrzPart (name, "invalid file name");
if (!n)
RETURN (Void);
m = IntPart (mode, "invalid file mode");
if (aborting)
RETURN (Void);
ret = mkdir (n, m);
if (ret < 0) {
int err = errno;
RaiseStandardException (exception_name_error, 3,
FileGetErrorMessage (err),
FileGetError (err), name);
RETURN (Void);
}
RETURN (Void);
}
Value
do_File_rmdir (Value name)
{
ENTER ();
char *n;
int ret;
n = StrzPart (name, "invalid file name");
if (!n)
RETURN (Void);
ret = rmdir (n);
if (ret < 0) {
int err = errno;
RaiseStandardException (exception_name_error, 3,
FileGetErrorMessage (err),
FileGetError (err), name);
RETURN (Void);
}
RETURN (Void);
}
|