1 /* Copyright (C) 1989, 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises. All rights reserved. 2 3 This software is provided AS-IS with no warranty, either express or 4 implied. 5 6 This software is distributed under license and may not be copied, 7 modified or distributed except as expressly authorized under the terms 8 of the license contained in the file LICENSE in this distribution. 9 10 For more information about licensing, please refer to 11 http://www.ghostscript.com/licensing/. For information on 12 commercial licensing, go to http://www.artifex.com/licensing/ or 13 contact Artifex Software, Inc., 101 Lucas Valley Road #110, 14 San Rafael, CA 94903, U.S.A., +1(415)492-9861. 15 */ 16 17 /* $Id: files.h,v 1.10 2004/08/04 19:36:12 stefan Exp $ */ 18 /* Definitions for interpreter support for file objects */ 19 /* Requires stream.h */ 20 21 #ifndef files_INCLUDED 22 # define files_INCLUDED 23 24 /* 25 * File objects store a pointer to a stream in value.pfile. 26 * A file object is valid if its "size" matches the read_id or write_id 27 * (as appropriate) in the stream it points to. This arrangement 28 * allows us to detect closed files reliably, while allowing us to 29 * reuse closed streams for new files. 30 */ 31 #define fptr(pref) (pref)->value.pfile 32 #define make_file(pref,a,id,s)\ 33 make_tasv(pref,t_file,a,id,pfile,s) 34 35 /* The stdxxx files. We have to access them through procedures, */ 36 /* because they might have to be opened when referenced. */ 37 int zget_stdin(i_ctx_t *, stream **); 38 int zget_stdout(i_ctx_t *, stream **); 39 int zget_stderr(i_ctx_t *, stream **); 40 /* Test whether a stream is stdin. */ 41 bool zis_stdin(const stream *); 42 43 /* Define access to the stdio refs for operators. */ 44 #define ref_stdio (i_ctx_p->stdio) 45 #define ref_stdin ref_stdio[0] 46 #define ref_stdout ref_stdio[1] 47 #define ref_stderr ref_stdio[2] 48 /* An invalid (closed) file. */ 49 #define avm_invalid_file_entry avm_foreign 50 extern stream *const invalid_file_entry; 51 /* Make an invalid file object. */ 52 void make_invalid_file(ref *); 53 54 /* 55 * Macros for checking file validity. 56 * NOTE: in order to work around a bug in the Borland 5.0 compiler, 57 * you must use file_is_invalid rather than !file_is_valid. 58 */ 59 #define file_is_valid(svar,op)\ 60 (svar = fptr(op), (svar->read_id | svar->write_id) == r_size(op)) 61 #define file_is_invalid(svar,op)\ 62 (svar = fptr(op), (svar->read_id | svar->write_id) != r_size(op)) 63 #define check_file(svar,op)\ 64 BEGIN\ 65 check_type(*(op), t_file);\ 66 if ( file_is_invalid(svar, op) ) return_error(e_invalidaccess);\ 67 END 68 69 /* 70 * If a file is open for both reading and writing, its read_id, write_id, 71 * and stream procedures and modes reflect the current mode of use; 72 * an id check failure will switch it to the other mode. 73 */ 74 int file_switch_to_read(const ref *); 75 76 #define check_read_file(svar,op)\ 77 BEGIN\ 78 check_read_type(*(op), t_file);\ 79 check_read_known_file(svar, op, return);\ 80 END 81 #define check_read_known_file(svar,op,error_return)\ 82 check_read_known_file_else(svar, op, error_return, svar = invalid_file_entry) 83 #define check_read_known_file_else(svar,op,error_return,invalid_action)\ 84 BEGIN\ 85 svar = fptr(op);\ 86 if (svar->read_id != r_size(op)) {\ 87 if (svar->read_id == 0 && svar->write_id == r_size(op)) {\ 88 int fcode = file_switch_to_read(op);\ 89 \ 90 if (fcode < 0)\ 91 error_return(fcode);\ 92 } else {\ 93 invalid_action; /* closed or reopened file */\ 94 }\ 95 }\ 96 END 97 int file_switch_to_write(const ref *); 98 99 #define check_write_file(svar,op)\ 100 BEGIN\ 101 check_write_type(*(op), t_file);\ 102 check_write_known_file(svar, op, return);\ 103 END 104 #define check_write_known_file(svar,op,error_return)\ 105 BEGIN\ 106 svar = fptr(op);\ 107 if ( svar->write_id != r_size(op) )\ 108 { int fcode = file_switch_to_write(op);\ 109 if ( fcode < 0 ) error_return(fcode);\ 110 }\ 111 END 112 113 /* Data exported by zfile.c. */ 114 /* for zfilter.c and ziodev.c */ 115 extern const uint file_default_buffer_size; 116 117 #ifndef gs_file_path_ptr_DEFINED 118 # define gs_file_path_ptr_DEFINED 119 typedef struct gs_file_path_s *gs_file_path_ptr; 120 #endif 121 122 /* Procedures exported by zfile.c. */ 123 /* for imainarg.c */ 124 FILE *lib_fopen(const gs_file_path_ptr pfpath, const gs_memory_t *mem, const char *); 125 126 /* for imain.c */ 127 int lib_file_open(const gs_file_path_ptr pfpath, i_ctx_t *, const char *, uint, byte *, uint, 128 uint *, ref *, gs_memory_t *); 129 130 /* for imain.c */ 131 #ifndef gs_ref_memory_DEFINED 132 # define gs_ref_memory_DEFINED 133 typedef struct gs_ref_memory_s gs_ref_memory_t; 134 #endif 135 int file_read_string(const byte *, uint, ref *, gs_ref_memory_t *); 136 137 /* for os_open in ziodev.c */ 138 #ifdef iodev_proc_fopen /* in gxiodev.h */ 139 int file_open_stream(const char *, uint, const char *, uint, stream **, 140 gx_io_device *, iodev_proc_fopen_t, gs_memory_t *); 141 #endif 142 143 /* for zfilter.c */ 144 int filter_open(const char *, uint, ref *, const stream_procs *, 145 const stream_template *, const stream_state *, 146 gs_memory_t *); 147 148 /* for zfileio.c */ 149 void make_stream_file(ref *, stream *, const char *); 150 151 /* for ziodev.c */ 152 int file_close_finish(stream *); 153 int file_close_disable(stream *); 154 int file_close_file(stream *); 155 156 /* for gsmain.c, interp.c */ 157 int file_close(ref *); 158 159 /* for zfproc.c, ziodev.c */ 160 stream *file_alloc_stream(gs_memory_t *, client_name_t); 161 162 /* Procedures exported by zfileio.c. */ 163 /* for ziodev.c */ 164 int zreadline_from(stream *s, gs_string *buf, gs_memory_t *bufmem, 165 uint *pcount, bool *pin_eol); 166 167 /* Procedures exported by zfileio.c. */ 168 /* for zfile.c */ 169 int zfilelineedit(i_ctx_t *i_ctx_p); 170 171 /* for zfproc.c */ 172 int zneedstdin(i_ctx_t *i_ctx_p); 173 int zneedstdout(i_ctx_t *i_ctx_p); 174 int zneedstderr(i_ctx_t *i_ctx_p); 175 #endif /* files_INCLUDED */ 176