summaryrefslogtreecommitdiff
path: root/contrib/pcl3/src/pclscan.c
blob: 461023cd2f2156e82206aa737b600264a2eb0d2c (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
/******************************************************************************
  File:     $Id: pclscan.c,v 1.8 2000-10-22 11:05:34+02 Martin Rel $
  Contents: PCL scanner
  Author:   Martin Lottermoser, Greifswaldstrasse 28, 38124 Braunschweig,
            Germany, e-mail: Martin.Lottermoser@t-online.de

*******************************************************************************
*									      *
*	Copyright (C) 1999, 2000 by Martin Lottermoser			      *
*	All rights reserved						      *
*									      *
******************************************************************************/

/*****************************************************************************/

#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE	500
#endif
#ifndef _XOPEN_SOURCE_EXTENDED
#define _XOPEN_SOURCE_EXTENDED	1
#endif

/* Standard headers */
#include <errno.h>
#include <nl_types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Special headers */
#include "pclscan.h"

/*****************************************************************************/

static nl_catd catd = (nl_catd)-1;	/* NLS catalogue descriptor */

static void message(int id, const char *s)
{
  if (catd != (nl_catd)-1) s = catgets(catd, 1, id, s);
   /* The check might not be necessary (X/Open requires catgets() to return 's'
      if the call is unsuccessful for *any* reason and the possible errors
      include EBADF for an invalid 'catd'), but one never knows. */

  fputs("? pclscan: ", stderr);
  fprintf(stderr, s);
  fputc('\n', stderr);

  return;
}

/*****************************************************************************/

static const pcl_Octet ESC = '\x1B';

/******************************************************************************

  Function: pcl_is_control_code

  This function returns a non-zero value iff 'c', taken to be an unsigned char
  converted to 'int', is a PCL control code.

******************************************************************************/

int pcl_is_control_code(int c)
{
  return
    c <= ' ' && (
      c == '\0' ||
      /* TRG500 does not mention NUL as a control code. */
      c == '\b' || c == '\t' || c == '\n' || c == '\f' || c == '\r' ||
      c == '\x0E' /* Shift Out (SO) */ ||
      c == '\x0F' /* Shift In (SI) */ ||
      c == '\x11' /* Device Control 1 (DC1) */ ||
      c == '\x13' /* Device Control 3 (DC3) */ ||
      c == ESC ||
      c == ' ');
}

/*****************************************************************************/

static int cmp_strings(const void *a, const void *b)
{
  return strncmp((const char *)a, (const char *)b, 3);
}

static int default_interpreter(FILE *in, const pcl_Command *cmd)
{
  /* Skip over arguments for those commands which are known to have them */
  if (cmd->kind >= 3) {
    if (strncmp((const char *)cmd->chars, "%-X", 3) == 0 && cmd->i == 12345) {
      /* Universal Exit Language (UEL)/Start of PJL */
      int c;

      do {
        c = fgetc(in);
        if (c != '@') break;
        do c = fgetc(in); while (c != EOF && c != '\n');
      } while (c != EOF);
      if (c != EOF) ungetc(c, in);
    }
    else if (cmd->i > 0) {
      static const char with_args[][4] = {
        /* Must be sorted with respect to cmp_strings() */
        "&bW",
        "&iW",
        "&pX",	/* Transparent Print Mode */
        "(sW",	/* Download Character */
        ")sW",	/* Create Font */
        "*bV",	/* Transfer Raster Graphics Data by Plane */
        "*bW",	/* Transfer Raster Graphics Data by Row */
        "*dW",	/* Palette Configuration */
        "*gW",	/* Configure Raster Data */
        "*oW"};

      if (bsearch(cmd->chars, with_args,
          sizeof(with_args)/sizeof(with_args[0]), sizeof(with_args[0]),
          &cmp_strings) != NULL) {
        int j;

        j = cmd->i;
        while (j > 0 && fgetc(in) != EOF) j--;
        if (j > 0) {
          message(1, "Premature EOF on input.");
          return -1;
        }
      }
    }
  }
  else if (cmd->kind == 2 && cmd->chars[0] == 'Y') {
    /* Display Functions Mode ON */
    int c;

    /* Read until EOF or Display Functions Mode OFF */
    do {
      do c = fgetc(in); while (c != EOF && c != ESC);
      if (c != EOF) c = fgetc(in);
    } while (c != EOF && c != 'Z');

    if (c != 'Z') {
      message(1, "Premature EOF on input.");
      return -1;
    }
  }

  return 0;
}

/*****************************************************************************/

static int default_handler(FILE *in)
{
  int c;

  do {
    c = fgetc(in);
  } while (c != EOF && !pcl_is_control_code(c));
  if (c != EOF) ungetc(c, in);

  return 0;
}

/******************************************************************************

  Function: pcl_scan

  This function scans a PCL file, separating it into printer commands and
  unknown data, and calling the functions '(*interpreter)()' and '(*handler)()'
  with these parts, respectively.

  'in' must have been opened as a binary file and for reading.
  'interpreter' or 'handler' may be null in which case default routines are
  used.
  If a non-NULL function pointer is provided for 'interpreter' or 'handler',
  the function will be passed the value of 'idata' or 'hdata', respectively.

  This function returns zero on success and a negative value otherwise.
  If one of the interpreter or data handler functions returns a negative value,
  processing will stop and the value will be returned as the return value of
  pcl_scan().

******************************************************************************/

int pcl_scan(FILE *in, pcl_CommandInterpreter interpreter, void *idata,
  pcl_UnknownDataHandler handler, void *hdata)
{
  int
    c,
    rc = 0;

  /* Open catalogue descriptor */
  catd = catopen("pcltools", 0);
  if (catd == (nl_catd)(-1) && errno != ENOENT)
     /* A system returning ENOENT if no catalogue is available is for example
        Sun Solaris 2.5. */
    fprintf(stderr,
      "?-W pclscan: Error trying to open message catalogue: %s.\n",
      strerror(errno));

  /* Loop over input */
  while (rc >= 0 && (c = fgetc(in)) != EOF) {
    if (c == ESC) {
      pcl_Command command;

      if ((c = fgetc(in)) == EOF) {
        rc = -1;
        message(1, "Premature EOF on input.");
        break;
      }
      command.chars[0] = c;
      if (48 <= c && c <= 126) {
        /* Two-character escape sequence */
        command.kind = 2;
        if (interpreter == NULL ||
            (rc = (*interpreter)(in, &command, idata)) > 0)
          rc = default_interpreter(in, &command);
      }
      else {
        int continued;

        /* Parameterized escape sequence or garbage. The character we've just
           read is the "parameterized character" and it should be in the range
           33-47. */

        /* Now the group character (should be in the range 96-126, but HP uses
           sometimes at least '-', which has the value 45) */
        if ((c = fgetc(in)) == EOF) {
          rc = -1;
          message(1, "Premature EOF on input.");
          break;
        }
        command.chars[1] = c;

        continued = 0;
        do {
          /* Now for the value */
          if ((c = fgetc(in)) == EOF) {
            rc = -1;
            message(1, "Premature EOF on input.");
            break;
          }
          if (c == '+' || c == '-' || '0' <= c && c <= '9') {
            if (c == '+' || c == '-') command.prefix = c;
            else command.prefix = ' ';
            ungetc(c, in);
            if (fscanf(in, "%d", &command.i) != 1) {
              rc = -1;
              message(2, "Syntax error in value field.");
              break;
            }

            /* Decimal point? */
            if ((c = fgetc(in)) == EOF) {
              rc = -1;
              message(1, "Premature EOF on input.");
              break;
            }
            command.scale = 0;
            command.fraction = 0;
            if (c == '.') {
              command.scale = 1;
              while ((c = fgetc(in)) != EOF && '0' <= c && c <= '9') {
                command.fraction = command.fraction*10 + (c - '0');
                command.scale *= 10;
              }
              if (c == EOF) {
                rc = -1;
                message(1, "Premature EOF on input.");
                break;
              }
              if (command.prefix == '-') command.fraction = -command.fraction;
            }
          }
          else {
            command.prefix = '\0'; /* no value given */
            command.i = 0;
          }

          /* Final character */
          if (96 <= c && c <= 126) {
            /* Parameter character */
            command.chars[2] = c - ('a' - 'A');
            command.kind = (continued? 6: 4);
            continued = 1;
          }
          else {
            /* Termination character (should be in the range 64-94) */
            command.chars[2] = c;
            command.kind = (continued? 5: 3);
            continued = 0;
          }

          if (interpreter == NULL ||
              (rc = (*interpreter)(in, &command, idata)) > 0)
            rc = default_interpreter(in, &command);
        } while (rc == 0 && continued);
      }
    }
    else if (pcl_is_control_code(c)) {
      pcl_Command command;

      command.chars[0] = c;
      command.kind = 1;
      command.i = 1;	/* number of occurrences */

      while ((c = fgetc(in)) != EOF && c == command.chars[0]) command.i++;
      if (c != EOF) ungetc(c, in);

      if (interpreter == NULL ||
          (rc = (*interpreter)(in, &command, idata)) > 0)
        rc = default_interpreter(in, &command);
    }
    else {
      ungetc(c, in);
      if (handler == NULL || (rc = (*handler)(in, hdata)) > 0)
        rc = default_handler(in);
    }
  }

  /* Close catalogue descriptor */
  if (catd != (nl_catd)-1) catclose(catd);

  return rc;
}