xref: /plan9/sys/src/cmd/gs/src/iscan.h (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1992, 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: iscan.h,v 1.9 2004/03/19 08:30:16 ray Exp $ */
18 /* Token scanner state and interface */
19 /* Requires gsstruct.h, ostack.h, stream.h */
20 
21 #ifndef iscan_INCLUDED
22 #  define iscan_INCLUDED
23 
24 #include "sa85x.h"
25 #include "sstring.h"
26 
27 /*
28  * Define the state of the scanner.  Before calling scan_token initially,
29  * the caller must initialize the state by calling scanner_state_init.
30  * Most of the state is only used if scanning is suspended because of
31  * an interrupt or a callout.
32  *
33  * We expose the entire state definition to the caller so that
34  * the state can normally be allocated on the stack.
35  */
36 #ifndef scanner_state_DEFINED
37 #  define scanner_state_DEFINED
38 typedef struct scanner_state_s scanner_state;
39 #endif
40 
41 /*
42  * Define a structure for dynamically growable strings.
43  * If is_dynamic is true, base/next/limit point to a string on the heap;
44  * if is_dynamic is false, base/next/limit point either to the local buffer
45  * or (only while control is inside scan_token) into the source stream buffer.
46  */
47 #define max_comment_line 255	/* max size of an externally processable comment */
48 #define max_dsc_line max_comment_line	/* backward compatibility */
49 #define da_buf_size (max_comment_line + 2)
50 typedef struct dynamic_area_s {
51     byte *base;
52     byte *next;
53     byte *limit;
54     bool is_dynamic;
55     byte buf[da_buf_size];	/* initial buffer */
56     gs_memory_t *memory;
57 } dynamic_area;
58 
59 #define da_size(pda) ((uint)((pda)->limit - (pda)->base))
60 typedef dynamic_area *da_ptr;
61 
62 /* Define state specific to binary tokens and binary object sequences. */
63 typedef struct scan_binary_state_s {
64     int num_format;
65     int (*cont)(i_ctx_t *, stream *, ref *, scanner_state *);
66     ref bin_array;
67     uint index;
68     uint max_array_index;	/* largest legal index in objects */
69     uint min_string_index;	/* smallest legal index in strings */
70     uint top_size;
71     uint size;
72 } scan_binary_state;
73 
74 /* Define the scanner state. */
75 struct scanner_state_s {
76     uint s_pstack;		/* stack depth when starting current */
77 				/* procedure, after pushing old pstack */
78     uint s_pdepth;		/* pstack for very first { encountered, */
79 				/* for error recovery */
80     int s_options;
81     enum {
82 	scanning_none,
83 	scanning_binary,
84 	scanning_comment,
85 	scanning_name,
86 	scanning_string
87     } s_scan_type;
88     dynamic_area s_da;
89     union sss_ {		/* scanning sub-state */
90 	scan_binary_state binary;	/* binary */
91 	struct sns_ {		/* name */
92 	    int s_name_type;	/* number of /'s preceding a name */
93 	    bool s_try_number;	/* true if should try scanning name */
94 	    /* as number */
95 	} s_name;
96 	stream_state st;	/* string */
97 	stream_A85D_state a85d;	/* string */
98 	stream_AXD_state axd;	/* string */
99 	stream_PSSD_state pssd;	/* string */
100     } s_ss;
101 };
102 
103 /* The type descriptor is public only for checking. */
104 extern_st(st_scanner_state);
105 #define public_st_scanner_state()	/* in iscan.c */\
106   gs_public_st_complex_only(st_scanner_state, scanner_state, "scanner state",\
107     scanner_clear_marks, scanner_enum_ptrs, scanner_reloc_ptrs, 0)
108 
109 /* Initialize a scanner with a given set of options. */
110 #define SCAN_FROM_STRING 1	/* true if string is source of data */
111 				/* (for Level 1 `\' handling) */
112 #define SCAN_CHECK_ONLY 2	/* true if just checking for syntax errors */
113 				/* and complete statements (no value) */
114 #define SCAN_PROCESS_COMMENTS 4	/* return scan_Comment for comments */
115 				/* (all comments or only non-DSC) */
116 #define SCAN_PROCESS_DSC_COMMENTS 8  /* return scan_DSC_Comment */
117 #define SCAN_PDF_RULES 16	/* PDF scanning rules (for names) */
118 #define SCAN_PDF_INV_NUM 32	/* Adobe ignores invalid numbers */
119 				/* This is for compatibility with Adobe */
120 				/* Acrobat Reader			*/
121 void scanner_state_init_options(scanner_state *sstate, int options);
122 #define scanner_state_init_check(pstate, from_string, check_only)\
123   scanner_state_init_options(pstate,\
124 			     (from_string ? SCAN_FROM_STRING : 0) |\
125 			     (check_only ? SCAN_CHECK_ONLY : 0))
126 #define scanner_state_init(pstate, from_string)\
127   scanner_state_init_check(pstate, from_string, false)
128 
129 /*
130  * Read a token from a stream.  As usual, 0 is a normal return,
131  * <0 is an error.  There are also some special return codes:
132  */
133 #define scan_BOS 1		/* binary object sequence */
134 #define scan_EOF 2		/* end of stream */
135 #define scan_Refill 3		/* get more input data, then call again */
136 #define scan_Comment 4		/* comment, non-DSC if processing DSC */
137 #define scan_DSC_Comment 5	/* DSC comment */
138 int scan_token(i_ctx_t *i_ctx_p, stream * s, ref * pref,
139 	       scanner_state * pstate);
140 
141 /*
142  * Read a token from a string.  Return like scan_token, but also
143  * update the string to move past the token (if no error).
144  */
145 int scan_string_token_options(i_ctx_t *i_ctx_p, ref * pstr, ref * pref,
146 			      int options);
147 #define scan_string_token(i_ctx_p, pstr, pref)\
148   scan_string_token_options(i_ctx_p, pstr, pref, 0)
149 
150 /*
151  * Handle a scan_Refill return from scan_token.
152  * This may return o_push_estack, 0 (meaning just call scan_token again),
153  * or an error code.
154  */
155 int scan_handle_refill(i_ctx_t *i_ctx_p, const ref * fop,
156 		       scanner_state * pstate, bool save, bool push_file,
157 		       op_proc_t cont);
158 
159 /*
160  * Define the procedure "hook" for parsing DSC comments.  If not NULL,
161  * this procedure is called for every DSC comment seen by the scanner.
162  */
163 extern int (*scan_dsc_proc) (const byte *, uint);
164 
165 /*
166  * Define the procedure "hook" for parsing general comments.  If not NULL,
167  * this procedure is called for every comment seen by the scanner.
168  * If both scan_dsc_proc and scan_comment_proc are set,
169  * scan_comment_proc is called only for non-DSC comments.
170  */
171 extern int (*scan_comment_proc) (const byte *, uint);
172 
173 #endif /* iscan_INCLUDED */
174