1/*
2 * $Id: headers.c,v 1.14 2009-10-29 13:38:15 didg Exp $
3 *
4 * Copyright (c) 1990,1994 Regents of The University of Michigan.
5 * All Rights Reserved.  See COPYRIGHT.
6 */
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif /* HAVE_CONFIG_H */
11
12#include <sys/param.h>
13#include <string.h>
14#include <stdio.h>
15#include <stdlib.h>
16
17#include <netatalk/at.h>
18#include <atalk/logger.h>
19
20#include "file.h"
21#include "comment.h"
22#include "lp.h"
23
24int ch_title( struct papfile *, struct papfile * );
25int ch_for( struct papfile *, struct papfile * );
26
27static char *get_text(char *start, int linelength)
28{
29    char *p, *q;
30    char *t, *ret;
31    char *stop;
32
33    /* 1023 is arbitrary 255 max for comment but some may be escape \xxx and space and keyword */
34
35    if (linelength > 1023)
36        return NULL;
37
38    t = ret = calloc(1, linelength +1);
39
40    if (!ret)
41        return NULL;
42
43    stop = start + linelength;
44    for ( p = start; p < stop; p++ ) {
45        if ( *p == ':' ) {
46            p++;
47            break;
48        }
49    }
50
51    for ( ; p < stop; p++ ) {
52        if (*p != ' ' && *p != '\t') {
53            break;
54        }
55    }
56
57    if ( p < stop && *p == '(' ) {
58        int count;
59        /* start with ( then it's a <text> */
60        p++;
61        for ( q = p, count = 1; q < stop; q++, t++ ) {
62            if (*q == '(') {
63              count++;
64            }
65            else if ( *q == ')' ) {
66                count--;
67                if (!count) {
68                    break;
69                }
70            }
71            *t = *q;
72        }
73    }
74    else {
75        /* it's a textline */
76        for ( q = p; q < stop; q++, t++ ) {
77            *t = *q;
78        }
79    }
80    return ret;
81}
82
83int ch_for( struct papfile *in, struct papfile *out _U_)
84{
85    char                *start, *cmt;
86    int                 linelength, crlflength;
87
88    switch ( markline( in, &start, &linelength, &crlflength )) {
89    case 0 :
90        return( 0 );
91
92    case -1 :
93        return( CH_MORE );
94
95    case -2 :
96        return( CH_ERROR );
97    }
98
99    cmt = get_text(start, linelength);
100
101    if ( cmt ) {
102	lp_for ( cmt );
103	free(cmt);
104    }
105
106    in->pf_state |= PF_TRANSLATE;
107    lp_write( in, start, linelength + crlflength );
108    in->pf_state &= ~PF_TRANSLATE;
109    compop();
110    CONSUME( in, linelength + crlflength );
111    return( CH_DONE );
112}
113
114int ch_title( struct papfile *in, struct papfile *out _U_)
115{
116    char		*start, *cmt;
117    int			linelength, crlflength;
118
119    switch ( markline( in, &start, &linelength, &crlflength )) {
120    case 0 :
121	return( 0 );
122
123    case -1 :
124	return( CH_MORE );
125
126    case -2 :
127        return( CH_ERROR );
128    }
129
130#ifdef DEBUG
131    LOG(log_debug9, logtype_papd, "Parsing %%Title");
132#endif
133
134    cmt = get_text(start, linelength);
135
136    if ( cmt ) {
137	lp_job( cmt );
138	free(cmt);
139    }
140
141    in->pf_state |= PF_TRANSLATE;
142    lp_write( in, start, linelength + crlflength );
143    in->pf_state &= ~PF_TRANSLATE;
144    compop();
145    CONSUME( in, linelength + crlflength );
146    return( CH_DONE );
147}
148
149static int guess_creator ( char *creator )
150{
151	if (strstr(creator, "LaserWriter"))
152		return 1;
153	if (strstr(creator, "cgpdftops"))
154		return 2;
155
156	return 0;
157}
158
159
160int ch_creator( struct papfile *in, struct papfile *out _U_)
161{
162    char		*start, *cmt;
163    int			linelength, crlflength;
164
165    switch ( markline( in, &start, &linelength, &crlflength )) {
166    case 0 :
167	return( 0 );
168
169    case -1 :
170	return( CH_MORE );
171
172    case -2 :
173        return( CH_ERROR );
174    }
175
176    cmt = get_text(start, linelength);
177
178    if ( cmt ) {
179	in->origin = guess_creator ( cmt );
180	free(cmt);
181	lp_origin(in->origin);
182    }
183
184    in->pf_state |= PF_TRANSLATE;
185    lp_write( in, start, linelength + crlflength );
186    in->pf_state &= ~PF_TRANSLATE;
187    compop();
188    CONSUME( in, linelength + crlflength );
189    return( CH_DONE );
190}
191
192int ch_endcomm( struct papfile *in, struct papfile *out _U_)
193{
194    char                *start;
195    int                 linelength, crlflength;
196
197#ifdef DEBUG
198    LOG(log_debug9, logtype_papd, "End Comment");
199#endif
200    in->pf_state |= PF_STW;
201
202    switch ( markline( in, &start, &linelength, &crlflength )) {
203    case 0 :
204	return( 0 );
205
206    case -1 :
207	return( CH_MORE );
208
209    case -2 :
210        return( CH_ERROR );
211    }
212
213    in->pf_state |= PF_TRANSLATE;
214    lp_write( in, start, linelength + crlflength );
215    in->pf_state &= ~PF_TRANSLATE;
216    compop();
217    CONSUME( in, linelength + crlflength );
218    return ( CH_DONE);
219}
220
221int ch_starttranslate( struct papfile *in, struct papfile *out _U_)
222{
223    char                *start;
224    int                 linelength, crlflength;
225
226#ifdef DEBUG
227    LOG(log_debug9, logtype_papd, "Start translate");
228#endif
229
230    switch ( markline( in, &start, &linelength, &crlflength )) {
231    case 0 :
232        return( 0 );
233
234    case -1 :
235        return( CH_MORE );
236
237    case -2 :
238        return( CH_ERROR );
239    }
240
241    in->pf_state |= PF_TRANSLATE;
242    lp_write( in, start, linelength + crlflength );
243    compop();
244    CONSUME( in, linelength + crlflength );
245    return ( CH_DONE);
246}
247
248int ch_endtranslate(struct papfile *in, struct papfile *out _U_)
249{
250    char                *start;
251    int                 linelength, crlflength;
252
253#ifdef DEBUG
254    LOG(log_debug9, logtype_papd, "EndTranslate");
255#endif
256
257    switch ( markline( in, &start, &linelength, &crlflength )) {
258    case 0 :
259        return( 0 );
260
261    case -1 :
262        return( CH_MORE );
263
264    case -2 :
265        return( CH_ERROR );
266    }
267
268    lp_write( in, start, linelength + crlflength );
269    in->pf_state &= ~PF_TRANSLATE;
270    compop();
271    CONSUME( in, linelength + crlflength );
272    return ( CH_DONE);
273}
274
275int ch_translateone( struct papfile *in, struct papfile *out _U_)
276{
277    char                *start;
278    int                 linelength, crlflength;
279
280#ifdef DEBUG
281    LOG(log_debug9, logtype_papd, "TranslateOne");
282#endif
283
284    switch ( markline( in, &start, &linelength, &crlflength )) {
285    case 0 :
286        return( 0 );
287
288    case -1 :
289        return( CH_MORE );
290
291    case -2 :
292        return( CH_ERROR );
293    }
294
295    in->pf_state |= PF_TRANSLATE;
296    lp_write( in, start, linelength + crlflength );
297    in->pf_state &= ~PF_TRANSLATE;
298    compop();
299    CONSUME( in, linelength + crlflength );
300    return ( CH_DONE);
301}
302
303
304
305
306/*
307 * "Header" comments.
308 */
309struct papd_comment	headers[] = {
310    { "%%Title:",			NULL,		ch_title,	0 },
311    { "%%For:",				NULL,		ch_for,		0 },
312    { "%%Creator:",			NULL,		ch_creator,	0 },
313    { "%%EndComments",			NULL,		ch_endcomm,	0 },
314    { "%%BeginFeature",			NULL,		ch_starttranslate,  0 },
315    { "%%EndFeature",			NULL,		ch_endtranslate,  0 },
316    { "%%BeginPageSetup",		NULL,		ch_starttranslate, 0 },
317    { "%%EndPageSetup",			NULL,		ch_endtranslate, 0 },
318#if 0
319    { "%%BeginSetup",			NULL,		ch_translateone,  0 },
320    { "%%EndSetup",			NULL,		ch_translateone,  0 },
321    { "%%BeginProlog",			NULL,		ch_translateone,  0 },
322    { "%%EndProlog",			NULL,		ch_translateone,  0 },
323    { "%%Page:",			NULL,		ch_translateone, 0 },
324    { "%%PageTrailer",			NULL,		ch_translateone, 0 },
325    { "%%Trailer",			NULL,		ch_translateone, 0 },
326    { "%%EOF",				NULL,		ch_translateone, 0 },
327#endif
328    { "%%",				NULL,		ch_translateone, 0 },
329    { NULL,				NULL,		NULL,		0 },
330};
331