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 14 /* $Id: gxiodev.h 9718 2009-05-02 00:03:15Z ray $ */ 15 /* Structure and default implementation of IODvices */ 16 /* Requires gsmemory.h */ 17 18 #ifndef gxiodev_INCLUDED 19 # define gxiodev_INCLUDED 20 21 #include "stat_.h" 22 23 /* 24 * Note that IODevices are not the same as Ghostscript output devices. 25 * See section 3.8.2 of the PostScript Language Reference Manual, 26 * Second and Third Edition, for more information. 27 */ 28 29 #ifndef gx_io_device_DEFINED 30 # define gx_io_device_DEFINED 31 typedef struct gx_io_device_s gx_io_device; 32 #endif 33 typedef struct gx_io_device_procs_s gx_io_device_procs; /* defined here */ 34 35 /* The IODevice table is defined in gconf.c; its extern is in gscdefs.h. */ 36 37 #ifndef file_enum_DEFINED /* also defined in gp.h */ 38 # define file_enum_DEFINED 39 struct file_enum_s; /* opaque to client, defined by implementors */ 40 typedef struct file_enum_s file_enum; 41 #endif 42 43 /* Define an opaque type for parameter lists. */ 44 #ifndef gs_param_list_DEFINED 45 # define gs_param_list_DEFINED 46 typedef struct gs_param_list_s gs_param_list; 47 #endif 48 49 /* Define an opaque type for streams. */ 50 #ifndef stream_DEFINED 51 # define stream_DEFINED 52 typedef struct stream_s stream; 53 #endif 54 55 /* 56 * Define the IODevice procedures. Note that file names for fopen, delete, 57 * rename, and status are C strings, not pointer + length. 58 * 59 * open_device is called when opening a file whose name consists only of 60 * the IODevice name, e.g., '%lineedit'. open_file is called when opening 61 * a file whose name includes both an (optional) IODevice and a further 62 * name, e.g., '%os%xyz' or just 'xyz'. 63 * 64 * The open_device and open_file procedures return streams. The default 65 * implementation of open_device returns an error; the default 66 * implementation of open_file in the PostScript interpreter, 67 * iodev_os_open_file, uses the IODevice's fopen procedure to open a stream 68 * based on an OS FILE *. However, IODevices are free to implement 69 * open_file (and, if desired, open_device) in any way they want, returning 70 * a stream that need not have any relationship to the OS's file system. 71 * In this case there is no need to implement fopen or fclose. 72 */ 73 /* Note also that "streams" are a higher-level concept; */ 74 /* the open_device and open_file procedures are normally NULL. */ 75 76 struct gx_io_device_procs_s { 77 78 #define iodev_proc_init(proc)\ 79 int proc(gx_io_device *iodev, gs_memory_t *mem) 80 iodev_proc_init((*init)); /* one-time initialization */ 81 82 #define iodev_proc_open_device(proc)\ 83 int proc(gx_io_device *iodev, const char *access, stream **ps,\ 84 gs_memory_t *mem) 85 iodev_proc_open_device((*open_device)); 86 87 #define iodev_proc_open_file(proc)\ 88 int proc(gx_io_device *iodev, const char *fname, uint namelen,\ 89 const char *access, stream **ps, gs_memory_t *mem) 90 iodev_proc_open_file((*open_file)); 91 92 /* fopen was changed in release 2.9.6, */ 93 /* and again in 3.20 to return the real fname separately */ 94 95 #define iodev_proc_fopen(proc)\ 96 int proc(gx_io_device *iodev, const char *fname, const char *access,\ 97 FILE **pfile, char *rfname, uint rnamelen) 98 iodev_proc_fopen((*fopen)); 99 100 #define iodev_proc_fclose(proc)\ 101 int proc(gx_io_device *iodev, FILE *file) 102 iodev_proc_fclose((*fclose)); 103 104 #define iodev_proc_delete_file(proc)\ 105 int proc(gx_io_device *iodev, const char *fname) 106 iodev_proc_delete_file((*delete_file)); 107 108 #define iodev_proc_rename_file(proc)\ 109 int proc(gx_io_device *iodev, const char *from, const char *to) 110 iodev_proc_rename_file((*rename_file)); 111 112 #define iodev_proc_file_status(proc)\ 113 int proc(gx_io_device *iodev, const char *fname, struct stat *pstat) 114 iodev_proc_file_status((*file_status)); 115 116 #define iodev_proc_enumerate_files(proc)\ 117 file_enum *proc(gx_io_device *iodev, const char *pat, uint patlen,\ 118 gs_memory_t *mem) 119 iodev_proc_enumerate_files((*enumerate_files)); 120 121 #define iodev_proc_enumerate_next(proc)\ 122 uint proc(file_enum *pfen, char *ptr, uint maxlen) 123 iodev_proc_enumerate_next((*enumerate_next)); 124 125 #define iodev_proc_enumerate_close(proc)\ 126 void proc(file_enum *pfen) 127 iodev_proc_enumerate_close((*enumerate_close)); 128 129 /* Added in release 2.9 */ 130 131 #define iodev_proc_get_params(proc)\ 132 int proc(gx_io_device *iodev, gs_param_list *plist) 133 iodev_proc_get_params((*get_params)); 134 135 #define iodev_proc_put_params(proc)\ 136 int proc(gx_io_device *iodev, gs_param_list *plist) 137 iodev_proc_put_params((*put_params)); 138 139 }; 140 141 /* The following typedef is needed because ansi2knr can't handle */ 142 /* iodev_proc_fopen((*procname)) in a formal argument list. */ 143 typedef iodev_proc_fopen((*iodev_proc_fopen_t)); 144 145 /* Default implementations of procedures */ 146 iodev_proc_init(iodev_no_init); 147 iodev_proc_open_device(iodev_no_open_device); 148 iodev_proc_open_file(iodev_no_open_file); 149 iodev_proc_fopen(iodev_no_fopen); 150 iodev_proc_fclose(iodev_no_fclose); 151 iodev_proc_delete_file(iodev_no_delete_file); 152 iodev_proc_rename_file(iodev_no_rename_file); 153 iodev_proc_file_status(iodev_no_file_status); 154 iodev_proc_enumerate_files(iodev_no_enumerate_files); 155 iodev_proc_get_params(iodev_no_get_params); 156 iodev_proc_put_params(iodev_no_put_params); 157 /* The %os% implemention of fopen and fclose. */ 158 /* These are exported for pipes and for %null. */ 159 iodev_proc_fopen(iodev_os_fopen); 160 iodev_proc_fclose(iodev_os_fclose); 161 162 /* Get the N'th IODevice. */ 163 gx_io_device *gs_getiodevice(int); 164 165 #define iodev_default (gs_getiodevice(0)) 166 167 /* Look up an IODevice name. */ 168 gx_io_device *gs_findiodevice(const byte *, uint); 169 170 /* Get and put IODevice parameters. */ 171 int gs_getdevparams(gx_io_device *, gs_param_list *); 172 int gs_putdevparams(gx_io_device *, gs_param_list *); 173 174 /* Convert an OS error number to a PostScript error */ 175 /* if opening a file fails. */ 176 int gs_fopen_errno_to_code(int); 177 178 /* Interface functions for clients that want iodev independent access to */ 179 /* the gp_enumerate functions */ 180 file_enum *gs_enumerate_files_init(const char *pat, uint patlen, gs_memory_t * mem); 181 uint gs_enumerate_files_next(file_enum * pfen, char *ptr, uint maxlen); 182 void gs_enumerate_files_close(file_enum * pfen); 183 184 /* Test whether a string is equal to a character. */ 185 /* (This is used for access testing in file_open procedures.) */ 186 #define streq1(str, chr)\ 187 ((str)[0] == (chr) && (str)[1] == 0) 188 189 /* Finally, the IODevice structure itself. */ 190 struct gx_io_device_s { 191 const char *dname; /* the IODevice name */ 192 const char *dtype; /* the type returned by currentdevparams */ 193 gx_io_device_procs procs; 194 void *state; /* (if the IODevice has state) */ 195 }; 196 197 #define private_st_io_device() /* in gsiodev.c */\ 198 gs_private_st_ptrs1(st_io_device, gx_io_device, "gx_io_device",\ 199 io_device_enum_ptrs, io_device_reloc_ptrs, state) 200 201 #endif /* gxiodev_INCLUDED */ 202