xref: /plan9/sys/src/cmd/gs/src/ziodev2.c (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1993, 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: ziodev2.c,v 1.6 2002/03/13 20:27:39 ray Exp $ */
18 /* (Level 2) IODevice operators */
19 #include "string_.h"
20 #include "ghost.h"
21 #include "gp.h"
22 #include "oper.h"
23 #include "stream.h"
24 #include "gxiodev.h"
25 #include "dstack.h"		/* for systemdict */
26 #include "files.h"		/* for file_open_stream */
27 #include "iparam.h"
28 #include "iutil2.h"
29 #include "store.h"
30 
31 /* ------ %null% ------ */
32 
33 /* This represents the null output file. */
34 private iodev_proc_open_device(null_open);
35 const gx_io_device gs_iodev_null = {
36     "%null%", "Special",
37     {
38 	iodev_no_init, null_open, iodev_no_open_file,
39 	iodev_os_fopen, iodev_os_fclose,
40 	iodev_no_delete_file, iodev_no_rename_file, iodev_no_file_status,
41 	iodev_no_enumerate_files, NULL, NULL,
42 	iodev_no_get_params, iodev_no_put_params
43     }
44 };
45 
46 private int
null_open(gx_io_device * iodev,const char * access,stream ** ps,gs_memory_t * mem)47 null_open(gx_io_device * iodev, const char *access, stream ** ps,
48 	  gs_memory_t * mem)
49 {
50     if (!streq1(access, 'w'))
51 	return_error(e_invalidfileaccess);
52     return file_open_stream(gp_null_file_name,
53 			    strlen(gp_null_file_name),
54 			    access, 256 /* arbitrary */ , ps,
55 			    iodev, iodev->procs.fopen, mem);
56 }
57 
58 /* ------ Operators ------ */
59 
60 /* <iodevice> .getdevparams <mark> <name> <value> ... */
61 private int
zgetdevparams(i_ctx_t * i_ctx_p)62 zgetdevparams(i_ctx_t *i_ctx_p)
63 {
64     os_ptr op = osp;
65     gx_io_device *iodev;
66     stack_param_list list;
67     gs_param_list *const plist = (gs_param_list *) & list;
68     int code;
69     ref *pmark;
70 
71     check_read_type(*op, t_string);
72     iodev = gs_findiodevice(op->value.bytes, r_size(op));
73     if (iodev == 0)
74 	return_error(e_undefinedfilename);
75     stack_param_list_write(&list, &o_stack, NULL, iimemory);
76     if ((code = gs_getdevparams(iodev, plist)) < 0) {
77 	ref_stack_pop(&o_stack, list.count * 2);
78 	return code;
79     }
80     pmark = ref_stack_index(&o_stack, list.count * 2);
81     make_mark(pmark);
82     return 0;
83 }
84 
85 /* <mark> <name> <value> ... <iodevice> .putdevparams */
86 private int
zputdevparams(i_ctx_t * i_ctx_p)87 zputdevparams(i_ctx_t *i_ctx_p)
88 {
89     os_ptr op = osp;
90     gx_io_device *iodev;
91     stack_param_list list;
92     gs_param_list *const plist = (gs_param_list *) & list;
93     int code;
94     password system_params_password;
95 
96     check_read_type(*op, t_string);
97     iodev = gs_findiodevice(op->value.bytes, r_size(op));
98     if (iodev == 0)
99 	return_error(e_undefinedfilename);
100     code = stack_param_list_read(&list, &o_stack, 1, NULL, false, iimemory);
101     if (code < 0)
102 	return code;
103     code = dict_read_password(&system_params_password, systemdict,
104 			      "SystemParamsPassword");
105     if (code < 0)
106 	return code;
107     code = param_check_password(plist, &system_params_password);
108     if (code != 0) {
109 	iparam_list_release(&list);
110 	return_error(code < 0 ? code : e_invalidaccess);
111     }
112     code = gs_putdevparams(iodev, plist);
113     iparam_list_release(&list);
114     if (code < 0)
115 	return code;
116     ref_stack_pop(&o_stack, list.count * 2 + 2);
117     return 0;
118 }
119 
120 /* ------ Initialization procedure ------ */
121 
122 const op_def ziodev2_l2_op_defs[] =
123 {
124     op_def_begin_level2(),
125     {"1.getdevparams", zgetdevparams},
126     {"2.putdevparams", zputdevparams},
127     op_def_end(0)
128 };
129