File: ztoken.c

package info (click to toggle)
ghostscript 8.71~dfsg2-9+squeeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 79,896 kB
  • ctags: 80,654
  • sloc: ansic: 501,432; sh: 25,689; python: 4,853; cpp: 3,633; perl: 3,597; tcl: 1,480; makefile: 1,187; lisp: 407; asm: 284; xml: 263; awk: 66; csh: 17; yacc: 15
file content (382 lines) | stat: -rw-r--r-- 10,387 bytes parent folder | download | duplicates (2)
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* Copyright (C) 2001-2006 Artifex Software, Inc.
   All Rights Reserved.
  
   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied, modified
   or distributed except as expressly authorized under the terms of that
   license.  Refer to licensing information at http://www.artifex.com/
   or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
   San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
*/

/* $Id: ztoken.c 9043 2008-08-28 22:48:19Z giles $ */
/* Token reading operators */
#include "string_.h"
#include "stat_.h" /* get system header early to avoid name clash on Cygwin */
#include "ghost.h"
#include "oper.h"
#include "dstack.h"		/* for dict_find_name */
#include "estack.h"
#include "gsstruct.h"		/* for iscan.h */
#include "gsutil.h"
#include "stream.h"
#include "files.h"
#include "store.h"
#include "strimpl.h"		/* for sfilter.h */
#include "sfilter.h"		/* for iscan.h */
#include "idict.h"
#include "iname.h"
#include "iscan.h"
#include "itoken.h"		/* for prototypes */

/* <file> token <obj> -true- */
/* <string> token <post> <obj> -true- */
/* <string|file> token -false- */
static int ztoken_continue(i_ctx_t *);
static int token_continue(i_ctx_t *, scanner_state *, bool);
int
ztoken(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;

    switch (r_type(op)) {
	default:
	    return_op_typecheck(op);
	case t_file: {
	    stream *s;
	    scanner_state state;

	    check_read_file(s, op);
	    check_ostack(1);
	    scanner_init(&state, op);
	    return token_continue(i_ctx_p, &state, true);
	}
	case t_string: {
	    ref token;
	    /* -1 is to remove the string operand in case of error. */
	    int orig_ostack_depth = ref_stack_count(&o_stack) - 1;
	    int code;

	    /* Don't pop the operand in case of invalidaccess. */
	    if (!r_has_attr(op, a_read))
		return_error(e_invalidaccess);
	    code = scan_string_token(i_ctx_p, op, &token);
	    switch (code) {
	    case scan_EOF:	/* no tokens */
		make_false(op);
		return 0;
	    default:
		if (code < 0) {
		    /*
		     * Clear anything that may have been left on the ostack,
		     * including the string operand.
		     */
	    	    if (orig_ostack_depth < ref_stack_count(&o_stack))
	    		pop(ref_stack_count(&o_stack)- orig_ostack_depth);
		    return code;
		}
	    }
	    push(2);
	    op[-1] = token;
	    make_true(op);
	    return 0;
	}
    }
}
/* Continue reading a token after an interrupt or callout. */
/* *op is the scanner state. */
static int
ztoken_continue(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    scanner_state *pstate;

    check_stype(*op, st_scanner_state);
    pstate = r_ptr(op, scanner_state);
    return token_continue(i_ctx_p, pstate, false);
}
/* Common code for token reading. */
static int
token_continue(i_ctx_t *i_ctx_p, scanner_state * pstate, bool save)
{
    os_ptr op = osp;
    int code;
    ref token;

    /* Note that scan_token may change osp! */
    pop(1);			/* remove the file or scanner state */
again:
    code = scan_token(i_ctx_p, &token, pstate);
    op = osp;
    switch (code) {
	default:		/* error */
	    if (code > 0)	/* comment, not possible */
		code = gs_note_error(e_syntaxerror);
	    scanner_error_object(i_ctx_p, pstate, &i_ctx_p->error_object);
	    break;
	case scan_BOS:
	    code = 0;
	case 0:		/* read a token */
	    push(2);
	    ref_assign(op - 1, &token);
	    make_true(op);
	    break;
	case scan_EOF:		/* no tokens */
	    push(1);
	    make_false(op);
	    code = 0;
	    break;
	case scan_Refill:	/* need more data */
	    code = scan_handle_refill(i_ctx_p, pstate, save,
				      ztoken_continue);
	    switch (code) {
		case 0:	/* state is not copied to the heap */
		    goto again;
		case o_push_estack:
		    return code;
	    }
	    break;		/* error */
    }
    if (code <= 0 && !save) {	/* Deallocate the scanner state record. */
	ifree_object(pstate, "token_continue");
    }
    return code;
}

/* <file> .tokenexec - */
/* Read a token and do what the interpreter would do with it. */
/* This is different from token + exec because literal procedures */
/* are not executed (although binary object sequences ARE executed). */
int ztokenexec_continue(i_ctx_t *);	/* export for interpreter */
static int tokenexec_continue(i_ctx_t *, scanner_state *, bool);
int
ztokenexec(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    stream *s;
    scanner_state state;

    check_read_file(s, op);
    check_estack(1);
    scanner_init(&state, op);
    return tokenexec_continue(i_ctx_p, &state, true);
}
/* Continue reading a token for execution after an interrupt or callout. */
/* *op is the scanner state. */
/* We export this because this is how the interpreter handles a */
/* scan_Refill for an executable file. */
int
ztokenexec_continue(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    scanner_state *pstate;

    check_stype(*op, st_scanner_state);
    pstate = r_ptr(op, scanner_state);
    return tokenexec_continue(i_ctx_p, pstate, false);
}
/* Common code for token reading + execution. */
static int
tokenexec_continue(i_ctx_t *i_ctx_p, scanner_state * pstate, bool save)
{
    os_ptr op;
    int code;
    /* Note that scan_token may change osp! */
    pop(1);
again:
    check_estack(1);
    code = scan_token(i_ctx_p, (ref *) (esp + 1), pstate);
    op = osp;
    switch (code) {
	case 0:
	    if (r_is_proc(esp + 1)) {	/* Treat procedure as a literal. */
		push(1);
		ref_assign(op, esp + 1);
		code = 0;
		break;
	    }
	    /* falls through */
	case scan_BOS:
	    ++esp;
	    code = o_push_estack;
	    break;
	case scan_EOF:		/* no tokens */
	    code = 0;
	    break;
	case scan_Refill:	/* need more data */
	    code = scan_handle_refill(i_ctx_p, pstate, save,
				      ztokenexec_continue);
	    switch (code) {
		case 0:	/* state is not copied to the heap */
		    goto again;
		case o_push_estack:
		    return code;
	    }
	    break;		/* error */
	case scan_Comment:
	case scan_DSC_Comment:
	    return ztoken_handle_comment(i_ctx_p, pstate, esp + 1, code,
					 save, true, ztokenexec_continue);
	default:		/* error */
	    scanner_error_object(i_ctx_p, pstate, &i_ctx_p->error_object);
	    break;
    }
    if (!save) {		/* Deallocate the scanner state record. */
	ifree_object(pstate, "token_continue");
    }
    return code;
}

/*
 * Handle a scan_Comment or scan_DSC_Comment return from scan_token
 * (scan_code) by calling out to %Process[DSC]Comment.  The continuation
 * procedure expects the scanner state on the o-stack.
 */
int
ztoken_handle_comment(i_ctx_t *i_ctx_p, scanner_state *sstate,
		      const ref *ptoken, int scan_code,
		      bool save, bool push_file, op_proc_t cont)
{
    const char *proc_name;
    scanner_state *pstate;
    os_ptr op;
    ref *ppcproc;
    int code;

    switch (scan_code) {
    case scan_Comment:
	proc_name = "%ProcessComment";
	break;
    case scan_DSC_Comment:
	proc_name = "%ProcessDSCComment";
	break;
    default:
	return_error(e_Fatal);	/* can't happen */
    }
    /*
     * We can't use check_ostack here, because it returns on overflow.
     */
    /*check_ostack(2);*/
    if (ostop - osp < 2) {
	code = ref_stack_extend(&o_stack, 2);
	if (code < 0)
	    return code;
    }
    check_estack(3);
    code = name_enter_string(imemory, proc_name, esp + 3);
    if (code < 0)
	return code;
    if (save) {
	pstate = ialloc_struct(scanner_state, &st_scanner_state,
			       "ztoken_handle_comment");
	if (pstate == 0)
	    return_error(e_VMerror);
	*pstate = *sstate;
    } else
	pstate = sstate;
    /* Save the token now -- it might be on the e-stack. */
    if (!pstate->s_pstack)
	osp[2] = *ptoken;
    /*
     * Push the continuation, scanner state, file, and callout procedure
     * on the e-stack.
     */
    make_op_estack(esp + 1, cont);
    make_istruct(esp + 2, 0, pstate);
    ppcproc = dict_find_name(esp + 3);
    if (ppcproc == 0) {
	/*
	 * This can only happen during initialization.
	 * Pop the comment string from the o-stack if needed (see below).
	 */
	if (pstate->s_pstack)
	    --osp;
	esp += 2;		/* do run the continuation */
    } else {
	/*
	 * Push the file and comment string on the o-stack.
	 * If we were inside { }, the comment string is already on the stack.
	 */
	if (pstate->s_pstack) {
	    op = ++osp;
	    *op = op[-1];
	} else {
	    op = osp += 2;
	    /* *op = *ptoken; */	/* saved above */
	}
	op[-1] = pstate->s_file;
	esp[3] = *ppcproc;
	esp += 3;
    }
    return o_push_estack;
}

typedef struct named_scanner_option_s {
    const char *pname;
    int option;
} named_scanner_option_t;
static const named_scanner_option_t named_options[] = {
    {"PDFScanRules", SCAN_PDF_RULES},
    {"ProcessComment", SCAN_PROCESS_COMMENTS},
    {"ProcessDSCComment", SCAN_PROCESS_DSC_COMMENTS},
    {"PDFScanInvNum", SCAN_PDF_INV_NUM},
    {"PDFScanUnsigned", SCAN_PDF_UNSIGNED}
};

/*
 * Update the cached scanner_options in the context state after doing a
 * setuserparams.  (We might move this procedure somewhere else eventually.)
 */
int
ztoken_scanner_options(const ref *upref, int old_options)
{
    int options = old_options;
    int i;

    for (i = 0; i < countof(named_options); ++i) {
	const named_scanner_option_t *pnso = &named_options[i];
	ref *ppcproc;
	int code = dict_find_string(upref, pnso->pname, &ppcproc);

	/* Update the options only if the parameter has changed. */
	if (code >= 0) {
	    if (r_has_type(ppcproc, t_null))
		options &= ~pnso->option;
	    else
		options |= pnso->option;
	}
    }
    return options;
}
/*
 * Get the value for a scanner option.
 * return -1 if no such option, 1/0 for on/off and option's name in *pname as a C string
 */
int
ztoken_get_scanner_option(const ref *psref, int options, const char **pname)
{
    const named_scanner_option_t *pnso;

    for (pnso = named_options + countof(named_options); pnso-- != named_options;) {
	if (!bytes_compare((const byte *)pnso->pname, strlen(pnso->pname),
			psref->value.const_bytes, r_size(psref))) {
	    *pname = pnso->pname;
	    return (options & pnso->option ? 1 : 0);
	}
    }
    return -1;
}

/* ------ Initialization procedure ------ */

const op_def ztoken_op_defs[] =
{
    {"1token", ztoken},
    {"1.tokenexec", ztokenexec},
		/* Internal operators */
    {"2%ztoken_continue", ztoken_continue},
    {"2%ztokenexec_continue", ztokenexec_continue},
    op_def_end(0)
};