/src/ghostpdl/pcl/pcl/pcmacros.c
Line | Count | Source |
1 | | /* Copyright (C) 2001-2026 Artifex Software, Inc. |
2 | | All Rights Reserved. |
3 | | |
4 | | This software is provided AS-IS with no warranty, either express or |
5 | | implied. |
6 | | |
7 | | This software is distributed under license and may not be copied, |
8 | | modified or distributed except as expressly authorized under the terms |
9 | | of the license contained in the file LICENSE in this distribution. |
10 | | |
11 | | Refer to licensing information at http://www.artifex.com or contact |
12 | | Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, |
13 | | CA 94129, USA, for further information. |
14 | | */ |
15 | | |
16 | | |
17 | | /* pcmacros.c */ |
18 | | /* PCL5 macro commands */ |
19 | | #include "stdio_.h" /* std.h + NULL */ |
20 | | #include "pcommand.h" |
21 | | #include "pgmand.h" |
22 | | #include "pcstate.h" |
23 | | #include "pcparse.h" |
24 | | |
25 | | /* ---------------- Macro execution ---------------- */ |
26 | | |
27 | | #ifdef DEBUG |
28 | | static void |
29 | | pcl_dump_current_macro(pcl_state_t * pcs, const char *msg) |
30 | | { |
31 | | int i; |
32 | | |
33 | | dmputs(pcs->memory, msg); |
34 | | dmputs(pcs->memory, " "); |
35 | | for (i = 0; i < CURRENT_MACRO_ID_SIZE; i++) |
36 | | dmprintf1(pcs->memory, "%02x", CURRENT_MACRO_ID[i]); |
37 | | dmputs(pcs->memory, "\n"); |
38 | | return; |
39 | | } |
40 | | #endif |
41 | | |
42 | | /* Execute a macro. We're willing to use C recursion for this because */ |
43 | | /* the PCL5 specification only allows 2 levels of call. */ |
44 | | static int |
45 | | do_copies(pcl_state_t * psaved, pcl_state_t * pcs, pcl_copy_operation_t copy) |
46 | 0 | { |
47 | 0 | const pcl_init_t **init = pcl_init_table; |
48 | 0 | int code = 0; |
49 | |
|
50 | 0 | for (; *init && code >= 0; ++init) |
51 | 0 | if ((*init)->do_copy) |
52 | 0 | code = (*(*init)->do_copy) (psaved, pcs, copy); |
53 | 0 | return code; |
54 | 0 | } |
55 | | int |
56 | | pcl_execute_macro(const pcl_macro_t * pmac, pcl_state_t * pcs, |
57 | | pcl_copy_operation_t before, pcl_reset_type_t reset, |
58 | | pcl_copy_operation_t after) |
59 | 0 | { |
60 | 0 | pcl_parser_state_t state; |
61 | 0 | hpgl_parser_state_t gstate; |
62 | 0 | pcl_state_t saved; |
63 | 0 | stream_cursor_read r; |
64 | 0 | int code; |
65 | |
|
66 | | #ifdef DEBUG |
67 | | if (gs_debug_c('i')) { |
68 | | pcl_dump_current_macro(pcs, "Starting macro execution"); |
69 | | } |
70 | | #endif |
71 | |
|
72 | 0 | if (before) { |
73 | 0 | memcpy(&saved, pcs, sizeof(*pcs)); |
74 | 0 | code = do_copies(&saved, pcs, before); |
75 | 0 | if (code < 0) |
76 | 0 | return code; |
77 | 0 | pcs->saved = &saved; |
78 | 0 | } |
79 | 0 | if (reset) { |
80 | 0 | code = pcl_do_resets(pcs, reset); |
81 | 0 | if (code < 0) |
82 | 0 | goto check_after; |
83 | 0 | } |
84 | 0 | state.definitions = pcs->pcl_commands; |
85 | 0 | state.hpgl_parser_state = &gstate; |
86 | 0 | code = pcl_process_init(&state, pcs); |
87 | 0 | if (code < 0) |
88 | 0 | return code; |
89 | 0 | r.ptr = (const byte *)(pmac + 1) - 1; |
90 | 0 | r.limit = (const byte *)pmac + (gs_object_size(pcs->memory, pmac) - 1); |
91 | 0 | pcs->macro_level++; |
92 | 0 | code = pcl_process(&state, pcs, &r); |
93 | 0 | pcs->macro_level--; |
94 | 0 | check_after: |
95 | 0 | if (after) { |
96 | 0 | int errcode = do_copies(&saved, pcs, after); |
97 | 0 | if (errcode < 0) |
98 | 0 | return errcode; |
99 | 0 | memcpy(pcs, &saved, sizeof(*pcs)); |
100 | 0 | } |
101 | 0 | if (code < 0) |
102 | 0 | return code; |
103 | | #ifdef DEBUG |
104 | | if (gs_debug_c('i')) { |
105 | | pcl_dump_current_macro(pcs, "Finished macro execution"); |
106 | | } |
107 | | #endif |
108 | | |
109 | 0 | return code; |
110 | 0 | } |
111 | | |
112 | | /* ---------------- Commands ---------------- */ |
113 | | |
114 | | /* Define the macro control operations. */ |
115 | | enum |
116 | | { |
117 | | macro_end_definition = 1, |
118 | | macro_execute = 2, |
119 | | macro_call = 3, |
120 | | macro_delete_temporary = 7 |
121 | | }; |
122 | | |
123 | | static int /* ESC & f <mc_enum> X */ |
124 | | pcl_macro_control(pcl_args_t * pargs, pcl_state_t * pcs) |
125 | 44.6k | { |
126 | 44.6k | int i = int_arg(pargs); |
127 | 44.6k | gs_const_string key; |
128 | 44.6k | void *value; |
129 | 44.6k | pl_dict_enum_t denum; |
130 | | |
131 | 44.6k | if (i == macro_end_definition) { |
132 | 48 | if (pcs->defining_macro) { /* The parser has included everything through this command */ |
133 | | /* in the macro_definition, so we can finish things up. */ |
134 | 1 | int code; |
135 | | |
136 | 1 | code = pl_dict_put(&pcs->macros, CURRENT_MACRO_ID, |
137 | 1 | CURRENT_MACRO_ID_SIZE, pcs->macro_definition); |
138 | 1 | pcs->defining_macro = false; |
139 | 1 | pcs->macro_definition = 0; |
140 | | #ifdef DEBUG |
141 | | if (gs_debug_c('i')) { |
142 | | pcl_dump_current_macro(pcs, "[i] Ending Macro"); |
143 | | } |
144 | | #endif |
145 | 1 | return code; |
146 | 1 | } |
147 | 44.6k | } else if (pcs->defining_macro) |
148 | 14 | return 0; /* don't execute other macro operations */ |
149 | 44.6k | else if (i == macro_execute || i == macro_call) { /* |
150 | | * TRM 12-9 says that "two levels of nesting are allowed", |
151 | | * which means 3 levels of macros (1 non-nested, 2 nested). |
152 | | */ |
153 | 277 | if (pcs->macro_level > 2) |
154 | 0 | return 0; |
155 | 44.3k | } else if (pcs->macro_level) |
156 | 0 | return e_Range; /* macro operations not allowed inside macro */ |
157 | 44.6k | switch (i) { |
158 | 45 | case 0: |
159 | 45 | { /* Start defining <macro_id>. */ |
160 | 45 | pcl_macro_t *pmac; |
161 | | |
162 | 45 | pmac = (pcl_macro_t *) |
163 | 45 | gs_alloc_bytes(pcs->memory, sizeof(pcl_macro_t), |
164 | 45 | "begin macro definition"); |
165 | 45 | if (pmac == 0) |
166 | 0 | return_error(e_Memory); |
167 | 45 | pmac->storage = pcds_temporary; |
168 | 45 | pcs->macro_definition = (byte *) pmac; |
169 | 45 | pcs->defining_macro = true; |
170 | | #ifdef DEBUG |
171 | | if (gs_debug_c('i')) { |
172 | | pcl_dump_current_macro(pcs, "[i] Starting Macro"); |
173 | | } |
174 | | #endif |
175 | 45 | return 0; |
176 | 45 | } |
177 | 47 | case macro_end_definition: /* 1 */ |
178 | 47 | { /* Stop defining macro. Outside a macro, this is an error. */ |
179 | 47 | return e_Range; |
180 | 45 | } |
181 | 274 | case macro_execute: /* 2 */ |
182 | 274 | { /* Execute <macro_id>. */ |
183 | 274 | void *value; |
184 | | |
185 | 274 | if (!pl_dict_find |
186 | 274 | (&pcs->macros, CURRENT_MACRO_ID, CURRENT_MACRO_ID_SIZE, |
187 | 274 | &value) |
188 | 274 | ) |
189 | 274 | return 0; |
190 | 0 | return pcl_execute_macro((const pcl_macro_t *)value, pcs, |
191 | 0 | pcl_copy_none, pcl_reset_none, |
192 | 0 | pcl_copy_none); |
193 | 274 | } |
194 | 3 | case macro_call: /* 3 */ |
195 | 3 | { /* Call <macro_id>, saving and restoring environment. */ |
196 | 3 | void *value; |
197 | | |
198 | 3 | if (!pl_dict_find |
199 | 3 | (&pcs->macros, CURRENT_MACRO_ID, CURRENT_MACRO_ID_SIZE, |
200 | 3 | &value) |
201 | 3 | ) |
202 | 3 | return 0; |
203 | 0 | return pcl_execute_macro((const pcl_macro_t *)value, pcs, |
204 | 0 | pcl_copy_before_call, pcl_reset_none, |
205 | 0 | pcl_copy_after_call); |
206 | 3 | } |
207 | 0 | case 4: |
208 | 0 | { /* Define <macro_id> as automatic overlay. */ |
209 | 0 | pcs->overlay_macro_id = pcs->macro_id; |
210 | 0 | pcs->overlay_enabled = true; |
211 | 0 | return 0; |
212 | 3 | } |
213 | 0 | case 5: |
214 | 0 | { /* Cancel automatic overlay. */ |
215 | 0 | pcs->overlay_enabled = false; |
216 | 0 | return 0; |
217 | 3 | } |
218 | 319 | case 6: |
219 | 319 | { /* Delete all macros. */ |
220 | 319 | pl_dict_release(&pcs->macros); |
221 | 319 | return 0; |
222 | 3 | } |
223 | 43.9k | case macro_delete_temporary: /* 7 */ |
224 | 43.9k | { /* Delete temporary macros. */ |
225 | 43.9k | pl_dict_enum_stack_begin(&pcs->macros, &denum, false); |
226 | 43.9k | while (pl_dict_enum_next(&denum, &key, &value)) |
227 | 1 | if (((pcl_macro_t *) value)->storage == pcds_temporary) { |
228 | 1 | pl_dict_undef_purge_synonyms(&pcs->macros, key.data, |
229 | 1 | key.size); |
230 | | /* The undef may have changed the dictionary under the feet |
231 | | of the enumerator, so restart. |
232 | | Called rarely enough, and dictionaries generally small enough |
233 | | not to worry about the overhead. |
234 | | */ |
235 | 1 | pl_dict_enum_stack_begin(&pcs->macros, &denum, false); |
236 | 1 | } |
237 | 43.9k | return 0; |
238 | 3 | } |
239 | 0 | case 8: |
240 | 0 | { /* Delete <macro_id>. */ |
241 | 0 | pl_dict_undef_purge_synonyms(&pcs->macros, CURRENT_MACRO_ID, |
242 | 0 | CURRENT_MACRO_ID_SIZE); |
243 | 0 | return 0; |
244 | 3 | } |
245 | 0 | case 9: |
246 | 0 | { /* Make <macro_id> temporary. */ |
247 | 0 | if (pl_dict_find |
248 | 0 | (&pcs->macros, CURRENT_MACRO_ID, CURRENT_MACRO_ID_SIZE, |
249 | 0 | &value)) |
250 | 0 | ((pcl_macro_t *) value)->storage = pcds_temporary; |
251 | 0 | return 0; |
252 | 3 | } |
253 | 0 | case 10: |
254 | 0 | { /* Make <macro_id> permanent. */ |
255 | 0 | if (pl_dict_find |
256 | 0 | (&pcs->macros, CURRENT_MACRO_ID, CURRENT_MACRO_ID_SIZE, |
257 | 0 | &value)) |
258 | 0 | ((pcl_macro_t *) value)->storage = pcds_permanent; |
259 | 0 | return 0; |
260 | 3 | } |
261 | 15 | default: |
262 | 15 | return e_Range; |
263 | 44.6k | } |
264 | 44.6k | } |
265 | | |
266 | | static int /* ESC & f <id> Y */ |
267 | | pcl_assign_macro_id(pcl_args_t * pargs, pcl_state_t * pcs) |
268 | 426 | { |
269 | 426 | uint id = uint_arg(pargs); |
270 | | |
271 | 426 | id_set_value(pcs->macro_id, id); |
272 | 426 | pcs->macro_id_type = numeric_id; |
273 | 426 | return 0; |
274 | 426 | } |
275 | | |
276 | | /* Initialization */ |
277 | | static int |
278 | | pcmacros_do_registration(pcl_parser_state_t * pcl_parser_state, |
279 | | gs_memory_t * mem) |
280 | 18.4k | { /* Register commands */ |
281 | 18.4k | DEFINE_CLASS('&') { |
282 | 18.4k | 'f', 'X', |
283 | 18.4k | PCL_COMMAND("Macro Control", pcl_macro_control, |
284 | 18.4k | pca_neg_error | pca_big_error | pca_in_macro)}, { |
285 | 18.4k | 'f', 'Y', |
286 | 18.4k | PCL_COMMAND("Assign Macro ID", pcl_assign_macro_id, |
287 | 18.4k | pca_neg_error | pca_big_error)}, |
288 | 18.4k | END_CLASS return 0; |
289 | 18.4k | } |
290 | | static int |
291 | | pcmacros_do_reset(pcl_state_t * pcs, pcl_reset_type_t type) |
292 | 62.4k | { |
293 | 62.4k | if (type & (pcl_reset_initial | pcl_reset_printer)) { |
294 | 62.4k | int code = 0; |
295 | | |
296 | 62.4k | pcs->overlay_enabled = false; |
297 | 62.4k | pcs->macro_level = 0; |
298 | 62.4k | pcs->defining_macro = false; |
299 | 62.4k | pcs->saved = 0; |
300 | 62.4k | pcs->macro_definition = 0; |
301 | | |
302 | 62.4k | if (type & pcl_reset_initial) |
303 | 18.4k | pl_dict_init(&pcs->macros, pcs->memory, NULL); |
304 | 43.9k | else { |
305 | 43.9k | pcl_args_t args; |
306 | | |
307 | 43.9k | arg_set_uint(&args, macro_delete_temporary); |
308 | 43.9k | code = pcl_macro_control(&args, pcs); |
309 | 43.9k | if (pcs->alpha_macro_id.id != 0) |
310 | 0 | gs_free_object(pcs->memory, |
311 | 43.9k | pcs->alpha_macro_id.id, "pcmacros_do_reset"); |
312 | 43.9k | if (code < 0) |
313 | 0 | return code; |
314 | 43.9k | } |
315 | 62.4k | } |
316 | | |
317 | 62.4k | if (type & |
318 | 62.4k | (pcl_reset_initial | pcl_reset_printer | pcl_reset_overlay | |
319 | 62.4k | pcl_reset_permanent)) { |
320 | 62.4k | pcs->alpha_macro_id.size = 0; |
321 | 62.4k | pcs->macro_id_type = numeric_id; |
322 | 62.4k | id_set_value(pcs->macro_id, 0); |
323 | 62.4k | pcs->alpha_macro_id.id = 0; |
324 | 62.4k | } |
325 | 62.4k | if (type & pcl_reset_permanent) { |
326 | 0 | gs_free_object(pcs->memory, pcs->macro_definition, "begin macro definition"); |
327 | 0 | pl_dict_release(&pcs->macros); |
328 | 0 | } |
329 | | |
330 | 62.4k | return 0; |
331 | 62.4k | } |
332 | | static int |
333 | | pcmacros_do_copy(pcl_state_t * psaved, const pcl_state_t * pcs, |
334 | | pcl_copy_operation_t operation) |
335 | 0 | { |
336 | 0 | if (operation & pcl_copy_after) { /* Don't restore the macro dictionary. */ |
337 | 0 | psaved->macros = pcs->macros; |
338 | 0 | } |
339 | 0 | return 0; |
340 | 0 | } |
341 | | const pcl_init_t pcmacros_init = { |
342 | | pcmacros_do_registration, pcmacros_do_reset, pcmacros_do_copy |
343 | | }; |