summaryrefslogtreecommitdiff
path: root/pl/pjparsei.c
blob: 67c98ea35cfbb286c7e639febf76221f3ccf2132 (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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/* Portions Copyright (C) 2001 artofcode LLC.
   Portions Copyright (C) 1996, 2001 Artifex Software Inc.
   Portions Copyright (C) 1988, 2000 Aladdin Enterprises.
   This software is based in part on the work of the Independent JPEG Group.
   All Rights Reserved.

   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., 101 Lucas Valley Road #110,
   San Rafael, CA  94903, (415)492-9861, for further information. */
/*$Id$ */

/* pjparsei.c   PJL parser implementation glue file (To pjparse.c) */

#include "string_.h"
#include "gserrors.h"
#include "pjtop.h"
#include "pjparse.h"
#include "plparse.h"
#include "plver.h"

/*
 * PJL interpeter: derived from pl_interp_t
 */
typedef struct pjl_interp_s {
  pl_interp_t              pl;               /* common part: must be first */
  gs_memory_t              *memory;          /* memory allocator to use */
} pjl_interp_t;

/*
 * PJL interpreter instance: derived from pl_interp_instance_t
 */
typedef struct pjl_interp_instance_s {
  pl_interp_instance_t     pl;               /* common part: must be first */
  gs_memory_t              *memory;          /* memory allocator to use */
  pjl_parser_state         *state;           /* parser's state */
} pjl_interp_instance_t;

/* Get implemtation's characteristics */
static const pl_interp_characteristics_t * /* always returns a descriptor */
pjl_impl_characteristics(
  const pl_interp_implementation_t *impl     /* implementation of interpereter to alloc */
)
{
  static const pl_interp_characteristics_t pjl_characteristics = {
    "PJL",
    "",
    "Artifex",
    PJLVERSION,
    PJLBUILDDATE,
    17    /* sizeof min buffer == sizeof UEL */
  };
  return &pjl_characteristics;
}

/* Allocate a PJL interp */
static int   /* ret 0 ok, else -ve error code */
pjl_impl_allocate_interp(
  pl_interp_t        **interp,       /* RETURNS abstract interpreter struct */
  const pl_interp_implementation_t *impl,     /* implementation of interpereter to alloc */
  gs_memory_t        *mem            /* allocator to allocate interp from */
)
{
        /* Allocate an interpreter */
        pjl_interp_t *pjl  /****** SHOULD HAVE A STRUCT DESCRIPTOR ******/
         = (pjl_interp_t *)gs_alloc_bytes( mem,
                                           sizeof(pjl_interp_t),
                                           "pjl_impl_allocate_interp(pjl_interp_t)"
                                          );
        if (pjl == 0)
          return gs_error_VMerror;
        pjl->memory = mem;
        *interp = (pl_interp_t *)pjl;
        return 0;   /* success */
}

/* Do per-instance interpreter allocation/init. No device is set yet */
static int   /* ret 0 ok, else -ve error code */
pjl_impl_allocate_interp_instance(
  pl_interp_instance_t   **instance,     /* RETURNS instance struct */
  pl_interp_t            *interp,        /* dummy interpreter */
  gs_memory_t            *mem            /* allocator to allocate instance from */
)
{
        /* Allocate everything up front */
        pjl_interp_instance_t *pjli  /****** SHOULD HAVE A STRUCT DESCRIPTOR ******/
         = (pjl_interp_instance_t *)gs_alloc_bytes( mem,
                                                    sizeof(pjl_interp_instance_t),
                                                    "pjl_impl_allocate_interp_instance(pjl_interp_instance_t)"
                                              );
        pjl_parser_state *pjls = pjl_process_init(mem);

        /* If any allocation error simply return */
        if (!pjli || !pjls)
          return gs_error_VMerror;

        /* Setup pointers to allocated mem within instance */
        pjli->state = pjls;
        pjli->memory = mem;

        /* Return success */
        *instance = (pl_interp_instance_t *)pjli;
        return 0;
}

/* Set a client language into an interperter instance */
static int   /* ret 0 ok, else -ve error code */
pjl_impl_set_client_instance(
  pl_interp_instance_t   *instance,     /* interp instance to use */
  pl_interp_instance_t   *client,       /* client to set */
  pl_interp_instance_clients_t which_client
)
{
        return 0;
}

/* Set a device into an interperter instance */
static int   /* ret 0 ok, else -ve error code */
pjl_impl_set_device(
  pl_interp_instance_t   *instance,     /* interp instance to use */
  gx_device              *device        /* device to set (open or closed) */
)
{
        return gs_error_undefined;   /* this operation is undefined for PJL */
}

/* Set an interpreter instance's pre-page action */
static int   /* ret 0 ok, else -ve err */
pjl_impl_set_pre_page_action(
  pl_interp_instance_t   *instance,     /* interp instance to use */
  pl_page_action_t       action,        /* action to execute */
  void                   *closure       /* closure to call action with */
)
{
        return gs_error_undefined;   /* this operation is undefined for PJL */
}

/* Set an interpreter instance's post-page action */
static int   /* ret 0 ok, else -ve err */
pjl_impl_set_post_page_action(
  pl_interp_instance_t   *instance,     /* interp instance to use */
  pl_page_action_t       action,        /* action to execute */
  void                   *closure       /* closure to call action with */
)
{
        return gs_error_undefined;   /* this operation is undefined for PJL */
}

static int
pjl_impl_get_device_memory(
  pl_interp_instance_t   *instance,     /* interp instance to use */
  gs_memory_t **pmem)
{
    return 0;
}

/* Prepare interp instance for the next "job" */
static int	/* ret 0 ok, else -ve error code */
pjl_impl_init_job(
        pl_interp_instance_t *instance         /* interp instance to start job in */
)
{
        int code = 0;
        pjl_interp_instance_t *pjli = (pjl_interp_instance_t *)instance;
        if (pjli->state == 0)
          code = gs_error_VMerror;
        /* copy the default state to the initial state */
        pjl_set_init_from_defaults(pjli->state);
        return code;
}

/* Parse a cursor-full of data */

/* The parser reads data from the input
 * buffer and returns either:
 *	>=0 - OK, more input is needed.
 *	e_ExitLanguage - Non-PJL was detected.
 *	<0 value - an error was detected.
 */

static int	
pjl_impl_process(
        pl_interp_instance_t *instance,        /* interp instance to process data job in */
        stream_cursor_read    *cursor           /* data to process */
)
{
        pjl_interp_instance_t *pjli = (pjl_interp_instance_t *)instance;
        int code = pjl_process(pjli->state, NULL, cursor);
        return code == 1 ? e_ExitLanguage : code;
}

/* Skip to end of job ret 1 if done, 0 ok but EOJ not found, else -ve error code */
static int
pjl_impl_flush_to_eoj(
        pl_interp_instance_t *instance,        /* interp instance to flush for */
        stream_cursor_read   *cursor           /* data to process */
)
{
        return pjl_skip_to_uel(cursor) ? 1 : 0;
}

/* Parser action for end-of-file */
static int	/* ret 0 or +ve if ok, else -ve error code */
pjl_impl_process_eof(
        pl_interp_instance_t *instance        /* interp instance to process data job in */
)
{
        return 0;
}

/* Report any errors after running a job */
static int   /* ret 0 ok, else -ve error code */
pjl_impl_report_errors(
        pl_interp_instance_t *instance,         /* interp instance to wrap up job in */
   int                  code,              /* prev termination status */
   long                 file_position,     /* file position of error, -1 if unknown */
        bool                 force_to_cout     /* force errors to cout */
)
{
        return 0;
}

/* Wrap up interp instance after a "job" */
static int	/* ret 0 ok, else -ve error code */
pjl_impl_dnit_job(
        pl_interp_instance_t *instance         /* interp instance to wrap up job in */
)
{
        int code = 0;
        return code;
}

/* Remove a device from an interperter instance */
static int   /* ret 0 ok, else -ve error code */
pjl_impl_remove_device(
  pl_interp_instance_t   *instance     /* interp instance to use */
)
{
        return gs_error_undefined;   /* this operation is undefined for PJL */
}

/* Deallocate a interpreter instance */
static int   /* ret 0 ok, else -ve error code */
pjl_impl_deallocate_interp_instance(
  pl_interp_instance_t   *instance     /* instance to dealloc */
)
{
        pjl_interp_instance_t *pjli = (pjl_interp_instance_t *)instance;
        gs_memory_t *mem = pjli->memory;

        pjl_process_destroy(pjli->state, mem);
        gs_free_object(mem, pjli, "pjl_impl_deallocate_interp_instance(pjl_interp_instance_t)");

        return 0;
}

/* Do static deinit of PJL interpreter */
static int   /* ret 0 ok, else -ve error code */
pjl_impl_deallocate_interp(
  pl_interp_t        *interp       /* interpreter to deallocate */
)
{
        pjl_interp_t *pi = (pjl_interp_t *)interp;
        gs_memory_t *mem = pi->memory;
        gs_free_object(mem, pi, "pjl_impl_deallocte_interp(pjl_interp_t)");

        return 0;
}

/* return the current setting of a pjl environment variable. */
static pjl_envvar_t *
pjl_impl_get_envvar(
  pl_interp_instance_t   *pli,
  const char             *pjl_var
)
{
        pjl_interp_instance_t *pjli = (pjl_interp_instance_t *)pli;
        return pjl_get_envvar(pjli->state, pjl_var);
}

/* compare a pjl environment variable to a string values. */
static int
pjl_impl_compare(
  pl_interp_instance_t   *pli,
  const pjl_envvar_t     *s1,
  const char             *s2
)
{
        return pjl_compare(s1, s2);
}

/* map a pjl symbol set name to a pcl integer */
static int
pjl_impl_map_pjl_sym_to_pcl_sym(
  pl_interp_instance_t   *pli,
  const pjl_envvar_t     *symname
)
{
        return pjl_map_pjl_sym_to_pcl_sym(symname);
}

/* pjl environment variable to integer. */
static int
pjl_impl_vartoi(
  pl_interp_instance_t   *pli,
  const pjl_envvar_t     *s
)
{
        return pjl_vartoi(s);
}

/* pjl envioronment variable to float. */
static floatp
pjl_impl_vartof(
  pl_interp_instance_t   *pli,
  const pjl_envvar_t     *s
)
{
        return pjl_vartof(s);
}

/* convert a pjl designated fontsource to a subdirectory pathname. */
static char *
pjl_impl_fontsource_to_path(
  pl_interp_instance_t   *pli,
  const pjl_envvar_t     *fontsource
)
{
        pjl_interp_instance_t *pjli = (pjl_interp_instance_t *)pli;
        return pjl_fontsource_to_path(pjli->state, fontsource);
}

/* Change to next highest priority font source. */
static void
pjl_impl_set_next_fontsource(
  pl_interp_instance_t   *pli
)
{
        pjl_interp_instance_t *pjli = (pjl_interp_instance_t *)pli;
        pjl_set_next_fontsource(pjli->state);
}

/* tell pjl that a soft font is being deleted. */
static int
pjl_impl_register_permanent_soft_font_deletion(
  pl_interp_instance_t   *pli,
  int                     font_number
)
{
        pjl_interp_instance_t *pjli = (pjl_interp_instance_t *)pli;
        return pjl_register_permanent_soft_font_deletion(pjli->state, font_number);
}

/* request that pjl add a soft font and return a pjl font number for the font. */
static int
pjl_impl_register_permanent_soft_font_addition(
  pl_interp_instance_t   *pli
)
{
        pjl_interp_instance_t *pjli = (pjl_interp_instance_t *)pli;
        return pjl_register_permanent_soft_font_addition(pjli->state);
}

static long int
pjl_impl_get_named_resource_size(pl_interp_instance_t *pli, char *name)
{
    pjl_interp_instance_t *pjli = (pjl_interp_instance_t *)pli;
    return pjl_get_named_resource_size(pjli->state, name);
}

static int
pjl_impl_get_named_resource(pl_interp_instance_t *pli, char *name, unsigned char *data)
{
    pjl_interp_instance_t *pjli = (pjl_interp_instance_t *)pli;
    return pjl_get_named_resource(pjli->state, name, data);
}

/* Parser implementation descriptor */
pjl_implementation_t pjl_implementation = {
  /* Generic language parser portion */
  { pjl_impl_characteristics,
    pjl_impl_allocate_interp,
    pjl_impl_allocate_interp_instance,
    pjl_impl_set_client_instance,
    pjl_impl_set_pre_page_action,
    pjl_impl_set_post_page_action,
    pjl_impl_set_device,
    pjl_impl_init_job,
    NULL, /* process_file */
    pjl_impl_process,
    pjl_impl_flush_to_eoj,
    pjl_impl_process_eof,
    pjl_impl_report_errors,
    pjl_impl_dnit_job,
    pjl_impl_remove_device,
    pjl_impl_deallocate_interp_instance,
    pjl_impl_deallocate_interp,
    pjl_impl_get_device_memory,
  },
  /* PJL-specific portion */
  pjl_impl_get_envvar,
  pjl_impl_compare,
  pjl_impl_map_pjl_sym_to_pcl_sym,
  pjl_impl_vartoi,
  pjl_impl_vartof,
  pjl_impl_fontsource_to_path,
  pjl_impl_set_next_fontsource,
  pjl_impl_register_permanent_soft_font_deletion,
  pjl_impl_register_permanent_soft_font_addition,
  pjl_impl_get_named_resource_size,
  pjl_impl_get_named_resource,
  pjl_impl_process
};