Coverage Report

Created: 2026-04-01 07:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/pcl/pl/pjparsei.c
Line
Count
Source
1
/* Copyright (C) 2001-2023 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
/* pjparsei.c   PJL parser implementation glue file (To pjparse.c) */
18
19
#include "string_.h"
20
#include "gserrors.h"
21
#include "pjtop.h"
22
#include "pjparse.h"
23
#include "plparse.h"
24
#include "plver.h"
25
26
static int
27
pjl_detect_language(const char *s, int len)
28
17.7k
{
29
    /* We will accept a single CRLF before the @PJL
30
     * string. This is a break with the spec, but is
31
     * required to cope with Bug 693269. We believe
32
     * this should be harmless in real world usage. */
33
17.7k
    if (len && *s == '\r')
34
6
        s++, len--;
35
17.7k
    if (len && *s == '\n')
36
41
        s++, len--;
37
17.7k
    if (len < 4)
38
43
        return 0;
39
17.6k
    if (memcmp(s, "@PJL", 4) == 0)
40
4.52k
        return 100;
41
13.1k
    return 0;
42
17.6k
}
43
44
/* Get implementation's characteristics */
45
static const pl_interp_characteristics_t *      /* always returns a descriptor */
46
pjl_impl_characteristics(const pl_interp_implementation_t * impl)        /* implementation of interpreter to alloc */
47
64.8k
{
48
64.8k
    static const pl_interp_characteristics_t pjl_characteristics = {
49
64.8k
        "PJL",
50
64.8k
        pjl_detect_language,
51
64.8k
    };
52
64.8k
    return &pjl_characteristics;
53
64.8k
}
54
55
/* Do per-instance interpreter allocation/init. No device is set yet */
56
static int                      /* ret 0 ok, else -ve error code */
57
pjl_impl_allocate_interp_instance(pl_interp_implementation_t *impl,
58
                                  gs_memory_t * mem     /* allocator to allocate instance from */
59
    )
60
8.09k
{
61
8.09k
    pjl_parser_state *pjls = pjl_process_init(mem);
62
63
8.09k
    impl->interp_client_data = pjls;
64
65
8.09k
    if (pjls == NULL)
66
0
        return gs_error_VMerror;
67
68
    /* Return success */
69
8.09k
    return 0;
70
8.09k
}
71
72
/* Prepare interp instance for the next "job" */
73
static int                      /* ret 0 ok, else -ve error code */
74
pjl_impl_init_job(pl_interp_implementation_t *impl,       /* interp instance to start job in */
75
                  gx_device                  *device)
76
23.7k
{
77
23.7k
    pjl_parser_state *pjls = impl->interp_client_data;
78
23.7k
    if (pjls == NULL)
79
0
        return gs_error_VMerror;
80
    /* copy the default state to the initial state */
81
23.7k
    return pjl_set_init_from_defaults(pjls);
82
23.7k
}
83
84
static int
85
pjl_impl_process_begin(pl_interp_implementation_t *impl       /* interp instance to process data job in */)
86
6.63k
{
87
6.63k
    return 0;
88
6.63k
}
89
90
/* Parse a cursor-full of data */
91
92
/* The parser reads data from the input
93
 * buffer and returns either:
94
 *  >=0 - OK, more input is needed.
95
 *  e_ExitLanguage - Non-PJL was detected.
96
 *  <0 value - an error was detected.
97
 */
98
99
static int
100
pjl_impl_process(pl_interp_implementation_t *impl,       /* interp instance to process data job in */
101
                 stream_cursor_read * cursor    /* data to process */
102
    )
103
6.63k
{
104
6.63k
    pjl_parser_state *pjls = impl->interp_client_data;
105
6.63k
    int code = pjl_process(pjls, NULL, cursor);
106
107
6.63k
    return code == 1 ? e_ExitLanguage : code;
108
6.63k
}
109
110
static int
111
pjl_impl_process_end(pl_interp_implementation_t *impl       /* interp instance to process data job in */)
112
7.62k
{
113
7.62k
    return 0;
114
7.62k
}
115
116
/* Skip to end of job ret 1 if done, 0 ok but EOJ not found, else -ve error code */
117
static int
118
pjl_impl_flush_to_eoj(pl_interp_implementation_t * impl,  /* interp impl to flush for */
119
                      stream_cursor_read * cursor       /* data to process */
120
    )
121
0
{
122
0
    return pjl_skip_to_uel(cursor) ? 1 : 0;
123
0
}
124
125
/* Parser action for end-of-file */
126
static int                      /* ret 0 or +ve if ok, else -ve error code */
127
pjl_impl_process_eof(pl_interp_implementation_t * impl    /* interp impl to process data job in */
128
    )
129
1.12k
{
130
1.12k
    return 0;
131
1.12k
}
132
133
/* Report any errors after running a job */
134
static int                      /* ret 0 ok, else -ve error code */
135
pjl_impl_report_errors(pl_interp_implementation_t * impl, /* interp impl to wrap up job in */
136
                       int code,        /* prev termination status */
137
                       long file_position,      /* file position of error, -1 if unknown */
138
                       bool force_to_cout       /* force errors to cout */
139
    )
140
0
{
141
0
    return 0;
142
0
}
143
144
/* Wrap up interp impl after a "job" */
145
static int                      /* ret 0 ok, else -ve error code */
146
pjl_impl_dnit_job(pl_interp_implementation_t * impl       /* interp impl to wrap up job in */
147
    )
148
23.7k
{
149
23.7k
    return 0;
150
23.7k
}
151
152
/* Deallocate a interpreter impl */
153
static int                      /* ret 0 ok, else -ve error code */
154
pjl_impl_deallocate_interp_instance(pl_interp_implementation_t * impl     /* impl to dealloc */
155
    )
156
8.09k
{
157
8.09k
    pjl_parser_state *pjls = impl->interp_client_data;
158
8.09k
    pjl_process_destroy(pjls);
159
8.09k
    return 0;
160
8.09k
}
161
162
/* Parser implementation descriptor */
163
pl_interp_implementation_t pjl_implementation = {
164
    pjl_impl_characteristics,
165
    pjl_impl_allocate_interp_instance,
166
    NULL,                       /* get_device_memory */
167
    NULL,                       /* set_param */
168
    NULL,                       /* add_path */
169
    NULL,                       /* post_args_init */
170
    pjl_impl_init_job,
171
    NULL,                       /* run_prefix_commands */
172
    NULL,                       /* process_file */
173
    pjl_impl_process_begin,
174
    pjl_impl_process,
175
    pjl_impl_process_end,
176
    pjl_impl_flush_to_eoj,
177
    pjl_impl_process_eof,
178
    pjl_impl_report_errors,
179
    pjl_impl_dnit_job,
180
    pjl_impl_deallocate_interp_instance,
181
    NULL, /* pjl_impl_reset */
182
    NULL, /* interp_client_data */
183
};