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