xref: /plan9/sys/src/cmd/gs/src/gsiodevs.c (revision ff8c3af2f44d95267f67219afa20ba82ff6cf7e4)
1 /* Copyright (C) 2000 Aladdin Enterprises.  All rights reserved.
2 
3   This file is part of AFPL Ghostscript.
4 
5   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
6   distributor accepts any responsibility for the consequences of using it, or
7   for whether it serves any particular purpose or works at all, unless he or
8   she says so in writing.  Refer to the Aladdin Free Public License (the
9   "License") for full details.
10 
11   Every copy of AFPL Ghostscript must include a copy of the License, normally
12   in a plain ASCII text file named PUBLIC.  The License grants you the right
13   to copy, modify and redistribute AFPL Ghostscript, but only under certain
14   conditions described in the License.  Among other things, the License
15   requires that the copyright notice and this notice be preserved on all
16   copies.
17 */
18 
19 /*$Id: gsiodevs.c,v 1.2 2000/09/19 19:00:29 lpd Exp $ */
20 /* %stdxxx IODevice implementation for non-PostScript configurations */
21 #include "gx.h"
22 #include "gserrors.h"
23 #include "gxiodev.h"
24 #include "stream.h"
25 #include "strimpl.h"
26 
27 const char iodev_dtype_stdio[] = "Special";
28 #define iodev_stdio(dname, open) {\
29     dname, iodev_dtype_stdio,\
30 	{ iodev_no_init, open, iodev_no_open_file,\
31 	  iodev_no_fopen, iodev_no_fclose,\
32 	  iodev_no_delete_file, iodev_no_rename_file, iodev_no_file_status,\
33 	  iodev_no_enumerate_files, NULL, NULL,\
34 	  iodev_no_get_params, iodev_no_put_params\
35 	}\
36 }
37 
38 #define STDIO_BUF_SIZE 128
39 private int
40 stdio_close_file(stream *s)
41 {
42     /* Don't close stdio files, but do free the buffer. */
43     gs_memory_t *mem = s->memory;
44 
45     s->file = 0;
46     gs_free_object(mem, s->cbuf, "stdio_close_file(buffer)");
47     return 0;
48 }
49 private int
50 stdio_open(gx_io_device * iodev, const char *access, stream ** ps,
51 	   gs_memory_t * mem, char rw, FILE *file,
52 	   void (*srw_file)(P4(stream *, FILE *, byte *, uint)))
53 {
54     stream *s;
55     byte *buf;
56 
57     if (!streq1(access, rw))
58 	return_error(gs_error_invalidfileaccess);
59     s = s_alloc(mem, "stdio_open(stream)");
60     buf = gs_alloc_bytes(mem, STDIO_BUF_SIZE, "stdio_open(buffer)");
61     if (s == 0 || buf == 0) {
62 	gs_free_object(mem, buf, "stdio_open(buffer)");
63 	gs_free_object(mem, s, "stdio_open(stream)");
64 	return_error(gs_error_VMerror);
65     }
66     srw_file(s, file, buf, STDIO_BUF_SIZE);
67     s->procs.close = stdio_close_file;
68     *ps = s;
69     return 0;
70 }
71 
72 private int
73 stdin_open(gx_io_device * iodev, const char *access, stream ** ps,
74 	   gs_memory_t * mem)
75 {
76     return stdio_open(iodev, access, ps, mem, 'r', gs_stdin, sread_file);
77 }
78 const gx_io_device gs_iodev_stdin = iodev_stdio("%stdin%", stdin_open);
79 
80 private int
81 stdout_open(gx_io_device * iodev, const char *access, stream ** ps,
82 	    gs_memory_t * mem)
83 {
84     return stdio_open(iodev, access, ps, mem, 'w', gs_stdout, swrite_file);
85 }
86 const gx_io_device gs_iodev_stdout = iodev_stdio("%stdout%", stdout_open);
87 
88 private int
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', gs_stderr, swrite_file);
93 }
94 const gx_io_device gs_iodev_stderr = iodev_stdio("%stderr%", stderr_open);
95