home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 5
/
FreshFish_July-August1994.bin
/
bbs
/
gnu
/
gs-2.6.1.4-src.lha
/
src
/
amiga
/
gs-2.6.1.4
/
zfile.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-01-27
|
22KB
|
757 lines
/* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises. All rights reserved.
This file is part of Ghostscript.
Ghostscript is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing. Refer
to the Ghostscript General Public License for full details.
Everyone is granted permission to copy, modify and redistribute
Ghostscript, but only under the conditions described in the Ghostscript
General Public License. A copy of this license is supposed to have been
given to you along with Ghostscript so you can know your rights and
responsibilities. It should be in a file named COPYING. Among other
things, the copyright notice and this notice must be preserved on all
copies. */
/* zfile.c */
/* Non-I/O file operators for Ghostscript */
#include "memory_.h"
#include "string_.h"
#include "ghost.h"
#include "gp.h"
#include "errors.h"
#include "oper.h"
#include "alloc.h"
#include "estack.h" /* for filenameforall, file_close */
#include "ilevel.h" /* %names only work in Level 2 */
#include "iutil.h"
#include "save.h" /* for restore */
#include "stream.h"
#include "filedev.h" /* must come after stream.h */
#include "files.h" /* ditto */
#include "store.h"
/* Import the file device table from zfiledev.c. */
extern file_device *file_device_table[];
#define fdev_default file_device_table[0]
/* Forward references: file name parsing. */
/* Parsed file name type. Note that the file name may be either a */
/* Ghostscript string (no terminator) or a C string (null terminator). */
typedef struct parsed_file_name_s {
file_device *fdev;
const char *fname;
uint len;
} parsed_file_name;
private int parse_file_name(P2(os_ptr, parsed_file_name *));
private int parse_real_file_name(P3(os_ptr, parsed_file_name *,
const char *));
private void free_real_file_name(P2(parsed_file_name *, const char *));
/* Forward references: file opening. */
int file_open(P6(const byte *, uint, const char *, uint, ref *, stream **));
stream *file_alloc_stream(P0());
int file_open_stream(P6(const byte *, uint, const char *,
uint, stream **, fdev_proc_fopen_t));
private void make_stream_file(P3(ref *, stream *, const char *));
/* Forward references: other. */
private int file_close_file(P1(stream *));
/* Imported from gs.c */
extern char **gs_lib_paths; /* search path list, */
/* terminated by a null pointer */
/*
* Since there can be many file objects referring to the same file/stream,
* we can't simply free a stream when we close it. On the other hand,
* we don't want freed streams to clutter up memory needlessly.
* Our solution is to retain the freed streams, and reuse them.
* To prevent an old file object from being able to access a reused stream,
* we keep a serial number in each stream, and check it against a serial
* number stored in the file object (as the "size"); when we close a file,
* we increment its serial number. If the serial number ever overflows,
* we leave it at zero, and do not reuse the stream.
* (This will never happen.)
*
* Storage management for this scheme is a little tricky.
* We maintain an invariant that says that a stream opened at a given
* save level always uses a stream structure allocated at that level.
* By doing this, we don't need to keep track separately of streams open
* at a level vs. streams allocated at a level; this simplifies things.
*/
/*
* The chain of all streams allocated at the current level.
* We need this so that we can do the right thing for restore.
* Note that this chain includes both open and closed files.
*/
private ref file_list_ref; /* t_file */
#define file_list file_list_ref.value.pfile
/* File buffer sizes. For real files, this is arbitrary, */
/* since the C library does its own buffering in addition. */
/* stdout and stderr use smaller buffers, */
/* on the assumption that they are usually not real files. */
/* The buffer size for type 1 encrypted files is NOT arbitrary: */
/* it must be at most 512. */
#define default_buffer_size 512
const uint file_default_buffer_size = default_buffer_size;
/* An invalid file object */
stream *invalid_file_entry;
/* Initialize the file table */
private void
zfile_init(void)
{
invalid_file_entry =
(stream *)gs_malloc(1, sizeof(stream), "zfile_init");
s_disable(invalid_file_entry);
s_init_no_id(invalid_file_entry);
/* Initialize the bookkeeping lists. */
make_file(&file_list_ref, 0, 0, 0);
}
/* <name_string> <access_string> file <file> */
int
zfile(register os_ptr op)
{ char file_access[3];
parsed_file_name pname;
const byte *astr;
int code;
stream *s;
check_read_type(*op, t_string);
astr = op->value.const_bytes;
switch ( r_size(op) )
{
case 2:
if ( astr[1] != '+' )
return_error(e_invalidfileaccess);
file_access[1] = '+';
file_access[2] = 0;
break;
case 1:
file_access[1] = 0;
break;
default:
return_error(e_invalidfileaccess);
}
switch ( astr[0] )
{
case 'r': case 'w': case 'a':
break;
default:
return_error(e_invalidfileaccess);
}
file_access[0] = astr[0];
code = parse_file_name(op - 1, &pname);
if ( code < 0 )
return code;
if ( pname.fdev == NULL )
pname.fdev = fdev_default;
if ( pname.fname == NULL ) /* just a device */
code = (*pname.fdev->procs.open_device)(pname.fdev,
file_access, &s);
else /* file */
code = (*pname.fdev->procs.open_file)(pname.fdev,
pname.fname, pname.len,
file_access, &s);
if ( code < 0 )
return code;
make_stream_file(op - 1, s, file_access);
pop(1);
return code;
}
/* <file> closefile - */
int
zclosefile(register os_ptr op)
{ stream *s;
check_file(s, op);
switch ( sclose(s) )
{
case 0:
pop(1);
return 0;
default:
return_error(e_ioerror);
}
}
/* ------ Level 2 extensions ------ */
/* <string> deletefile - */
int
zdeletefile(register os_ptr op)
{ parsed_file_name pname;
int code = parse_real_file_name(op, &pname, "deletefile");
if ( code < 0 )
return code;
code = (*pname.fdev->procs.delete_file)(pname.fdev, pname.fname);
free_real_file_name(&pname, "deletefile");
if ( code < 0 )
return code;
pop(1);
return 0;
}
/* <template> <proc> <scratch> filenameforall - */
/****** NOT CONVERTED FOR FILE DEVICES YET ******/
private int file_continue(P1(os_ptr));
private int file_cleanup(P1(os_ptr));
int
zfilenameforall(register os_ptr op)
{ file_enum *pfen;
int code;
check_write_type(*op, t_string);
check_proc(op[-1]);
check_read_type(op[-2], t_string);
/* Push a mark, the pattern, the scratch string, the enumerator, */
/* and the procedure, and invoke the continuation. */
check_estack(7);
pfen = gp_enumerate_files_init((char *)op[-2].value.bytes, r_size(op - 2), &alloc_memory_procs);
if ( pfen == 0 )
return_error(e_VMerror);
mark_estack(es_for, file_cleanup);
*++esp = op[-2];
*++esp = *op;
++esp;
make_tasv(esp, t_string, a_read+a_execute+a_executable, 0,
bytes, (byte *)pfen);
*++esp = op[-1];
pop(3); op -= 3;
code = file_continue(op);
return (code == o_pop_estack ? o_push_estack : code);
}
/* Continuation operator for enumerating files */
private int
file_continue(register os_ptr op)
{ es_ptr pscratch = esp - 2;
file_enum *pfen = (file_enum *)esp[-1].value.bytes;
uint len = r_size(pscratch);
uint code = gp_enumerate_files_next(pfen, (char *)pscratch->value.bytes, len);
if ( code == ~(uint)0 ) /* all done */
{ esp -= 4; /* pop proc, pfen, scratch, mark */
return o_pop_estack;
}
else if ( code > len ) /* overran string */
return_error(e_rangecheck);
else
{ push(1);
ref_assign(op, pscratch);
r_set_size(op, code);
push_op_estack(file_continue); /* come again */
*++esp = pscratch[2]; /* proc */
return o_push_estack;
}
}
/* Cleanup procedure for enumerating files */
private int
file_cleanup(os_ptr op)
{ gp_enumerate_files_close((file_enum *)esp[4].value.bytes);
return 0;
}
/* <string1> <string2> renamefile - */
int
zrenamefile(register os_ptr op)
{ parsed_file_name pname1, pname2;
int code = parse_real_file_name(op - 1, &pname1, "renamefil