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