1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
/* Copyright (C) 1989, 1995, 1997 Aladdin Enterprises. All rights reserved.
This file is part of Aladdin Ghostscript.
Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author
or distributor accepts any responsibility for the consequences of using it,
or for whether it serves any particular purpose or works at all, unless he
or she says so in writing. Refer to the Aladdin Ghostscript Free Public
License (the "License") for full details.
Every copy of Aladdin Ghostscript must include a copy of the License,
normally in a plain ASCII text file named PUBLIC. The License grants you
the right to copy, modify and redistribute Aladdin Ghostscript, but only
under certain conditions described in the License. Among other things, the
License requires that the copyright notice and this notice be preserved on
all copies.
*/
/*$Id$ */
/* Miscellaneous operators */
#include "errno_.h"
#include "memory_.h"
#include "string_.h"
#include "ghost.h"
#include "gscdefs.h" /* for gs_serialnumber */
#include "gp.h"
#include "oper.h"
#include "ialloc.h"
#include "idict.h"
#include "dstack.h" /* for name lookup in bind */
#include "iname.h"
#include "ipacked.h"
#include "ivmspace.h"
#include "store.h"
/* <proc> bind <proc> */
private int
zbind(os_ptr op)
{
uint depth = 1;
ref defn;
register os_ptr bsp;
switch (r_type(op)) {
case t_array:
case t_mixedarray:
case t_shortarray:
defn = *op;
break;
case t_oparray:
defn = *op->value.const_refs;
break;
default:
return_op_typecheck(op);
}
push(1);
*op = defn;
bsp = op;
/*
* We must not make the top-level procedure read-only,
* but we must bind it even if it is read-only already.
*
* Here are the invariants for the following loop:
* `depth' elements have been pushed on the ostack;
* For i < depth, p = ref_stack_index(&o_stack, i):
* *p is an array (or packedarray) ref. */
#define r_is_ex_oper(rp)\
((r_btype(rp) == t_operator || r_type(rp) == t_oparray) &&\
r_has_attr(rp, a_executable))
while (depth) {
while (r_size(bsp)) {
ref *tp = bsp->value.refs;
r_dec_size(bsp, 1);
if (r_is_packed(tp)) {
/* Check for a packed executable name */
ushort elt = *(ushort *) tp;
if (r_packed_is_exec_name(&elt)) {
ref nref;
ref *pvalue;
name_index_ref(packed_name_index(&elt),
&nref);
if ((pvalue = dict_find_name(&nref)) != 0 &&
r_is_ex_oper(pvalue)
)
/* Note: can't undo this by restore! */
*(ushort *) tp =
pt_tag(pt_executable_operator) +
op_index(pvalue);
}
bsp->value.refs = (ref *) ((ref_packed *) tp + 1);
} else
switch (bsp->value.refs++, r_type(tp)) {
case t_name: /* bind the name if an operator */
if (r_has_attr(tp, a_executable)) {
ref *pvalue;
if ((pvalue = dict_find_name(tp)) != 0 &&
r_is_ex_oper(pvalue)
)
ref_assign_old(bsp, tp, pvalue, "bind");
}
break;
case t_array: /* push into array if writable */
if (!r_has_attr(tp, a_write))
break;
case t_mixedarray:
case t_shortarray:
if (r_has_attr(tp, a_executable)) {
/* Make reference read-only */
r_clear_attrs(tp, a_write);
if (bsp >= ostop) {
/* Push a new stack block. */
ref temp;
int code;
temp = *tp;
osp = bsp;
code = ref_stack_push(&o_stack, 1);
if (code < 0) {
ref_stack_pop(&o_stack, depth);
return_error(code);
}
bsp = osp;
*bsp = temp;
} else
*++bsp = *tp;
depth++;
}
}
}
bsp--;
depth--;
if (bsp < osbot) { /* Pop back to the previous stack block. */
osp = bsp;
ref_stack_pop_block(&o_stack);
bsp = osp;
}
}
osp = bsp;
return 0;
}
/* - serialnumber <int> */
private int
zserialnumber(register os_ptr op)
{
push(1);
make_int(op, gs_serialnumber);
return 0;
}
/* - realtime <int> */
private int
zrealtime(register os_ptr op)
{
long secs_ns[2];
gp_get_realtime(secs_ns);
push(1);
make_int(op, secs_ns[0] * 1000 + secs_ns[1] / 1000000);
return 0;
}
/* - usertime <int> */
private int
zusertime(register os_ptr op)
{
long secs_ns[2];
gp_get_usertime(secs_ns);
push(1);
make_int(op, secs_ns[0] * 1000 + secs_ns[1] / 1000000);
return 0;
}
/* ---------------- Non-standard operators ---------------- */
/* <string> getenv <value_string> true */
/* <string> getenv false */
private int
zgetenv(register os_ptr op)
{
char *str;
byte *value;
int code;
int len = 0;
check_read_type(*op, t_string);
str = ref_to_string(op, imemory, "getenv key");
if (str == 0)
return_error(e_VMerror);
if (gp_getenv(str, (char *)0, &len) > 0) { /* key missing */
ifree_string((byte *) str, r_size(op) + 1, "getenv key");
make_false(op);
return 0;
}
value = ialloc_string(len, "getenv value");
if (value == 0) {
ifree_string((byte *) str, r_size(op) + 1, "getenv key");
return_error(e_VMerror);
}
code = gp_getenv(str, (char *)value, &len); /* can't fail */
ifree_string((byte *) str, r_size(op) + 1, "getenv key");
/* Delete the stupid C string terminator. */
value = iresize_string(value, len, len - 1,
"getenv value"); /* can't fail */
push(1);
make_string(op - 1, a_all | icurrent_space, len - 1, value);
make_true(op);
return 0;
}
/* <name> <proc> .makeoperator <oper> */
private int
zmakeoperator(register os_ptr op)
{
op_array_table *opt;
uint count;
ref *tab;
check_type(op[-1], t_name);
check_proc(*op);
switch (r_space(op)) {
case avm_global:
opt = &op_array_table_global;
break;
case avm_local:
opt = &op_array_table_local;
break;
default:
return_error(e_invalidaccess);
}
count = opt->count;
tab = opt->table.value.refs;
/*
* restore doesn't reset op_array_table.count, but it does
* remove entries from op_array_table.table. Since we fill
* the table in order, we can detect that a restore has occurred
* by checking whether what should be the most recent entry
* is occupied. If not, we scan backwards over the vacated entries
* to find the true end of the table.
*/
while (count > 0 && r_has_type(&tab[count - 1], t_null))
--count;
if (count == r_size(&opt->table))
return_error(e_limitcheck);
ref_assign_old(&opt->table, &tab[count], op, "makeoperator");
opt->nx_table[count] = name_index(op - 1);
op_index_ref(opt->base_index + count, op - 1);
opt->count = count + 1;
pop(1);
return 0;
}
/* - .oserrno <int> */
private int
zoserrno(register os_ptr op)
{
push(1);
make_int(op, errno);
return 0;
}
/* <int> .setoserrno - */
private int
zsetoserrno(register os_ptr op)
{
check_type(*op, t_integer);
errno = op->value.intval;
pop(1);
return 0;
}
/* <int> .oserrorstring <string> true */
/* <int> .oserrorstring false */
private int
zoserrorstring(register os_ptr op)
{
const char *str;
int code;
uint len;
byte ch;
check_type(*op, t_integer);
str = gp_strerror((int)op->value.intval);
if (str == 0 || (len = strlen(str)) == 0) {
make_false(op);
return 0;
}
check_ostack(1);
code = string_to_ref(str, op, iimemory, ".oserrorstring");
if (code < 0)
return code;
/* Strip trailing end-of-line characters. */
while ((len = r_size(op)) != 0 &&
((ch = op->value.bytes[--len]) == '\r' || ch == '\n')
)
r_dec_size(op, 1);
push(1);
make_true(op);
return 0;
}
/* <string> <bool> .setdebug - */
private int
zsetdebug(register os_ptr op)
{
check_read_type(op[-1], t_string);
check_type(*op, t_boolean);
{
int i;
for (i = 0; i < r_size(op - 1); i++)
gs_debug[op[-1].value.bytes[i] & 127] =
op->value.boolval;
}
pop(2);
return 0;
}
/* ------ Initialization procedure ------ */
const op_def zmisc_op_defs[] =
{
{"1bind", zbind},
{"1getenv", zgetenv},
{"2.makeoperator", zmakeoperator},
{"0.oserrno", zoserrno},
{"1.oserrorstring", zoserrorstring},
{"0realtime", zrealtime},
{"1serialnumber", zserialnumber},
{"2.setdebug", zsetdebug},
{"1.setoserrno", zsetoserrno},
{"0usertime", zusertime},
op_def_end(0)
};
|