xref: /plan9/sys/src/cmd/gs/src/gsiodevs.c (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 2000 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: gsiodevs.c,v 1.6 2004/08/04 19:36:12 stefan Exp $ */
18 /* %stdxxx IODevice implementation for non-PostScript configurations */
19 #include "gx.h"
20 #include "gserrors.h"
21 #include "gxiodev.h"
22 #include "stream.h"
23 #include "strimpl.h"
24 
25 const char iodev_dtype_stdio[] = "Special";
26 #define iodev_stdio(dname, open) {\
27     dname, iodev_dtype_stdio,\
28 	{ iodev_no_init, open, iodev_no_open_file,\
29 	  iodev_no_fopen, iodev_no_fclose,\
30 	  iodev_no_delete_file, iodev_no_rename_file, iodev_no_file_status,\
31 	  iodev_no_enumerate_files, NULL, NULL,\
32 	  iodev_no_get_params, iodev_no_put_params\
33 	}\
34 }
35 
36 #define STDIO_BUF_SIZE 128
37 private int
stdio_close_file(stream * s)38 stdio_close_file(stream *s)
39 {
40     /* Don't close stdio files, but do free the buffer. */
41     gs_memory_t *mem = s->memory;
42 
43     s->file = 0;
44     gs_free_object(mem, s->cbuf, "stdio_close_file(buffer)");
45     return 0;
46 }
47 private int
stdio_open(gx_io_device * iodev,const char * access,stream ** ps,gs_memory_t * mem,char rw,FILE * file,void (* srw_file)(stream *,FILE *,byte *,uint))48 stdio_open(gx_io_device * iodev, const char *access, stream ** ps,
49 	   gs_memory_t * mem, char rw, FILE *file,
50 	   void (*srw_file)(stream *, FILE *, byte *, uint))
51 {
52     stream *s;
53     byte *buf;
54 
55     if (!streq1(access, rw))
56 	return_error(gs_error_invalidfileaccess);
57     s = s_alloc(mem, "stdio_open(stream)");
58     buf = gs_alloc_bytes(mem, STDIO_BUF_SIZE, "stdio_open(buffer)");
59     if (s == 0 || buf == 0) {
60 	gs_free_object(mem, buf, "stdio_open(buffer)");
61 	gs_free_object(mem, s, "stdio_open(stream)");
62 	return_error(gs_error_VMerror);
63     }
64     srw_file(s, file, buf, STDIO_BUF_SIZE);
65     s->procs.close = stdio_close_file;
66     *ps = s;
67     return 0;
68 }
69 
70 private int
stdin_open(gx_io_device * iodev,const char * access,stream ** ps,gs_memory_t * mem)71 stdin_open(gx_io_device * iodev, const char *access, stream ** ps,
72 	   gs_memory_t * mem)
73 {
74     return stdio_open(iodev, access, ps, mem, 'r',
75 		      mem->gs_lib_ctx->fstdin, sread_file);
76 }
77 const gx_io_device gs_iodev_stdin = iodev_stdio("%stdin%", stdin_open);
78 
79 private int
stdout_open(gx_io_device * iodev,const char * access,stream ** ps,gs_memory_t * mem)80 stdout_open(gx_io_device * iodev, const char *access, stream ** ps,
81 	    gs_memory_t * mem)
82 {
83     return stdio_open(iodev, access, ps, mem, 'w',
84 		      mem->gs_lib_ctx->fstdout, swrite_file);
85 }
86 const gx_io_device gs_iodev_stdout = iodev_stdio("%stdout%", stdout_open);
87 
88 private int
stderr_open(gx_io_device * iodev,const char * access,stream ** ps,gs_memory_t * mem)89 stderr_open(gx_io_device * iodev, const char *access, stream ** ps,
90 	    gs_memory_t * mem)
91 {
92     return stdio_open(iodev, access, ps, mem, 'w',
93 		      mem->gs_lib_ctx->fstderr, swrite_file);
94 }
95 const gx_io_device gs_iodev_stderr = iodev_stdio("%stderr%", stderr_open);
96