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