summaryrefslogtreecommitdiff
path: root/pcl/pcmacros.c
blob: c68cac5623579da894449a9fab06c610541a8986 (plain)
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
/* Copyright (C) 1996, 1997, 1998 Aladdin Enterprises.  All rights reserved.
   Unauthorized use, copying, and/or distribution prohibited.
 */

/* pcmacros.c */
/* PCL5 macro commands */
#include "stdio_.h"			/* std.h + NULL */
#include "pcommand.h"
#include "pcstate.h"
#include "pcparse.h"

/* ---------------- Macro execution ---------------- */

/* Execute a macro.  We're willing to use C recursion for this because */
/* the PCL5 specification only allows 2 levels of call. */
private int
do_copies(pcl_state_t *psaved, pcl_state_t *pcs,
  pcl_copy_operation_t copy)
{	const pcl_init_t **init = pcl_init_table;
	int code = 0;

	for ( ; *init && code >= 0; ++init )
	  if ( (*init)->do_copy )
	    code = (*(*init)->do_copy)(psaved, pcs, copy);
	return code;
}
int
pcl_execute_macro(const pcl_macro_t *pmac, pcl_state_t *pcs,
  pcl_copy_operation_t before, pcl_reset_type_t reset,
  pcl_copy_operation_t after)
{	pcl_parser_state_t state;
	pcl_state_t saved;
	stream_cursor_read r;
	int code;

	if ( before )
	  { memcpy(&saved, pcs, sizeof(*pcs));
	    do_copies(&saved, pcs, before);
	    pcs->saved = &saved;
	  }
	if ( reset )
	  pcl_do_resets(pcs, reset);
	pcl_process_init(&state);
	r.ptr = (const byte *)(pmac + 1) - 1;
	r.limit =
	  (const byte *)pmac + (gs_object_size(pcs->memory, pmac) - 1);
	pcs->macro_level++;
	code = pcl_process(&state, pcs, &r);
	pcs->macro_level--;
	if ( after )
	  { do_copies(&saved, pcs, after);
	    memcpy(pcs, &saved, sizeof(*pcs));
	  }
	return code;
}

/* ---------------- Commands ---------------- */

/* Define the macro control operations. */
enum {
  macro_end_definition = 1,
  macro_execute = 2,
  macro_call = 3,
  macro_delete_temporary = 7
};

private int /* ESC & f <mc_enum> X */
pcl_macro_control(pcl_args_t *pargs, pcl_state_t *pcs)
{	int i = int_arg(pargs);
	gs_const_string key;
	void *value;
	pl_dict_enum_t denum;

	if ( i == macro_end_definition )
	  { if ( pcs->defining_macro )
	      { /* The parser has included everything through this command */
		/* in the macro_definition, so we can finish things up. */
		int code;
		code = pl_dict_put(&pcs->macros, current_macro_id,
				   current_macro_id_size, pcs->macro_definition);
		pcs->defining_macro = false;
		pcs->macro_definition = 0;
	        return code;
	      }
	  }
	else if ( pcs->defining_macro )
	  return 0;		/* don't execute other macro operations */
	else if ( i == macro_execute || i == macro_call )
	  { /*
	     * TRM 12-9 says that "two levels of nesting are allowed",
	     * which means 3 levels of macros (1 non-nested, 2 nested).
	     */
	    if ( pcs->macro_level > 2 )
	      return 0;
	  }
	else if ( pcs->macro_level )
	  return e_Range;	/* macro operations not allowed inside macro */
	switch ( i )
	  {
	  case 0:
	    { /* Start defining <macro_id>. */
	      pcl_macro_t *pmac;

	      pl_dict_undef_purge_synonyms(&pcs->macros, current_macro_id, current_macro_id_size);
	      pmac = (pcl_macro_t *)
		gs_alloc_bytes(pcs->memory, sizeof(pcl_macro_t),
			       "begin macro definition");
	      if ( pmac == 0 )
		return_error(e_Memory);
	      pmac->storage = pcds_temporary;
	      pcs->macro_definition = (byte *)pmac;
	      pcs->defining_macro = true;
	      return 0;
	    }
	  case macro_end_definition: /* 1 */
	    { /* Stop defining macro.  Outside a macro, this is an error. */
	      return e_Range;
	    }
	  case macro_execute:	/* 2 */
	    { /* Execute <macro_id>. */
	      void *value;
	      if ( !pl_dict_find(&pcs->macros, current_macro_id, current_macro_id_size,
				 &value)
		 )
		return 0;
	      return pcl_execute_macro((const pcl_macro_t *)value, pcs,
			       pcl_copy_none, pcl_reset_none, pcl_copy_none);
	    }
	  case macro_call:	/* 3 */
	    { /* Call <macro_id>, saving and restoring environment. */
	      void *value;
	      if ( !pl_dict_find(&pcs->macros, current_macro_id, current_macro_id_size,
				 &value)
		 )
		return 0;
	      return pcl_execute_macro((const pcl_macro_t *)value, pcs,
				       pcl_copy_before_call, pcl_reset_none,
				       pcl_copy_after_call);
	    }
	  case 4:
	    { /* Define <macro_id> as automatic overlay. */
	      pcs->overlay_macro_id = pcs->macro_id;
	      pcs->overlay_enabled = true;
	      return 0;
	    }
	  case 5:
	    { /* Cancel automatic overlay. */
	      pcs->overlay_enabled = false;
	      return 0;
	    }
	  case 6:
	    { /* Delete all macros. */
	      pl_dict_release(&pcs->macros);
	      return 0;
	    }
	  case macro_delete_temporary: /* 7 */
	    { /* Delete temporary macros. */
	      pl_dict_enum_stack_begin(&pcs->macros, &denum, false);
	      while ( pl_dict_enum_next(&denum, &key, &value) )
		if ( ((pcl_macro_t *)value)->storage == pcds_temporary )
		  pl_dict_undef_purge_synonyms(&pcs->macros, key.data, key.size);
	      return 0;
	    }
	  case 8:
	    { /* Delete <macro_id>. */
	      pl_dict_undef_purge_synonyms(&pcs->macros, current_macro_id, current_macro_id_size);
	      return 0;
	    }
	  case 9:
	    { /* Make <macro_id> temporary. */
	      if ( pl_dict_find(&pcs->macros, current_macro_id, current_macro_id_size, &value) )
		((pcl_macro_t *)value)->storage = pcds_temporary;
	      return 0;
	    }
	  case 10:
	    { /* Make <macro_id> permanent. */
	      if ( pl_dict_find(&pcs->macros, current_macro_id, current_macro_id_size, &value) )
		((pcl_macro_t *)value)->storage = pcds_permanent;
	      return 0;
	    }
	  default:
	    return e_Range;
	  }
}

private int /* ESC & f <id> Y */
pcl_assign_macro_id(pcl_args_t *pargs, pcl_state_t *pcs)
{	uint id = uint_arg(pargs);
	id_set_value(pcs->macro_id, id);
	pcs->macro_id_type = numeric_id;
	return 0;
}

/* Initialization */
private int
pcmacros_do_init(gs_memory_t *mem)
{		/* Register commands */
	DEFINE_CLASS('&')
	  {'f', 'X',
	     PCL_COMMAND("Macro Control", pcl_macro_control,
			 pca_neg_error|pca_big_error|pca_in_macro)},
	  {'f', 'Y',
	     PCL_COMMAND("Assign Macro ID", pcl_assign_macro_id,
			 pca_neg_error|pca_big_error)},
	END_CLASS
	return 0;
}
private void
pcmacros_do_reset(pcl_state_t *pcs, pcl_reset_type_t type)
{	if ( type & (pcl_reset_initial | pcl_reset_printer) )
	  { pcs->overlay_enabled = false;
	    pcs->macro_level = 0;
	    pcs->defining_macro = false;
	    pcs->saved = 0;
	    pcs->macro_definition = 0;

	    if ( type & pcl_reset_initial )
		pl_dict_init(&pcs->macros, pcs->memory, NULL);
	    else
	      { pcl_args_t args;
	        arg_set_uint(&args, macro_delete_temporary);
		pcl_macro_control(&args, pcs);
		if ( pcs->alpha_macro_id.id != 0 )
		  gs_free_object(pcs->memory,
				 pcs->alpha_macro_id.id,
				 "pcmacros_do_reset");
	      }
	  }

	if ( type & (pcl_reset_initial | pcl_reset_printer | pcl_reset_overlay) )
          {
	    pcs->alpha_macro_id.size = 0;
	    pcs->macro_id_type = numeric_id;
	    id_set_value(pcs->macro_id, 0);
	    pcs->alpha_macro_id.id = 0;
          }
}
private int
pcmacros_do_copy(pcl_state_t *psaved, const pcl_state_t *pcs,
  pcl_copy_operation_t operation)
{	if ( operation & pcl_copy_after )
	  { /* Don't restore the macro dictionary. */
	    psaved->macros = pcs->macros;
	  }
	return 0;
}
const pcl_init_t pcmacros_init = {
  pcmacros_do_init, pcmacros_do_reset, pcmacros_do_copy
};