]> arthur.barton.de Git - netatalk.git/blob - etc/papd/headers.c
markline: return an error if the buffer is too big and there's no end of line
[netatalk.git] / etc / papd / headers.c
1 /*
2  * $Id: headers.c,v 1.12 2009-02-02 10:31:32 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
24 int ch_title( struct papfile *, struct papfile * );
25 int ch_for( struct papfile *, struct papfile * );
26
27 static 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
83 int ch_for( in, out )
84         struct papfile  *in, *out _U_;
85 {
86     char                *start, *cmt;
87     int                 linelength, crlflength;
88
89     switch ( markline( in, &start, &linelength, &crlflength )) {
90     case 0 :
91         return( 0 );
92
93     case -1 :
94         return( CH_MORE );
95
96     case -2 :
97         return( CH_ERROR );
98     }
99
100     cmt = get_text(start, linelength);
101
102     if ( cmt ) {
103         lp_for ( cmt );
104         free(cmt);
105     }
106
107     in->pf_state |= PF_TRANSLATE;
108     lp_write( in, start, linelength + crlflength );
109     in->pf_state &= ~PF_TRANSLATE;
110     compop();
111     CONSUME( in, linelength + crlflength );
112     return( CH_DONE );
113 }
114
115 int ch_title( in, out )
116     struct papfile      *in, *out _U_;
117 {
118     char                *start, *cmt;
119     int                 linelength, crlflength;
120
121     switch ( markline( in, &start, &linelength, &crlflength )) {
122     case 0 :
123         return( 0 );
124
125     case -1 :
126         return( CH_MORE );
127
128     case -2 :
129         return( CH_ERROR );
130     }
131
132 #ifdef DEBUG
133     LOG(log_debug, logtype_papd, "Parsing %%Title");
134 #endif
135
136     cmt = get_text(start, linelength);
137
138     if ( cmt ) {
139         lp_job( cmt );
140         free(cmt);
141     }
142
143     in->pf_state |= PF_TRANSLATE;
144     lp_write( in, start, linelength + crlflength );
145     in->pf_state &= ~PF_TRANSLATE;
146     compop();
147     CONSUME( in, linelength + crlflength );
148     return( CH_DONE );
149 }
150
151 static int guess_creator ( char *creator )
152 {
153         if (strstr(creator, "LaserWriter"))
154                 return 1;
155         if (strstr(creator, "cgpdftops"))
156                 return 2;
157
158         return 0;
159 }
160
161
162 int ch_creator( in, out )
163     struct papfile      *in, *out _U_;
164 {
165     char                *start, *cmt;
166     int                 linelength, crlflength;
167
168     switch ( markline( in, &start, &linelength, &crlflength )) {
169     case 0 :
170         return( 0 );
171
172     case -1 :
173         return( CH_MORE );
174
175     case -2 :
176         return( CH_ERROR );
177     }
178
179     cmt = get_text(start, linelength);
180
181     if ( cmt ) {
182         in->origin = guess_creator ( cmt );
183         free(cmt);
184         lp_origin(in->origin);
185     }
186
187     in->pf_state |= PF_TRANSLATE;
188     lp_write( in, start, linelength + crlflength );
189     in->pf_state &= ~PF_TRANSLATE;
190     compop();
191     CONSUME( in, linelength + crlflength );
192     return( CH_DONE );
193 }
194
195 int ch_endcomm( in, out )
196     struct papfile      *in, *out _U_;
197 {
198     char                *start;
199     int                 linelength, crlflength;
200
201 #ifdef DEBUG
202     LOG(log_debug, logtype_papd, "End Comment");
203 #endif
204     in->pf_state |= PF_STW;
205
206     switch ( markline( in, &start, &linelength, &crlflength )) {
207     case 0 :
208         return( 0 );
209
210     case -1 :
211         return( CH_MORE );
212
213     case -2 :
214         return( CH_ERROR );
215     }
216
217     in->pf_state |= PF_TRANSLATE;
218     lp_write( in, start, linelength + crlflength );
219     in->pf_state &= ~PF_TRANSLATE;
220     compop();
221     CONSUME( in, linelength + crlflength );
222     return ( CH_DONE);
223 }
224
225 int ch_starttranslate(in,out)
226     struct papfile      *in, *out _U_;
227 {
228     char                *start;
229     int                 linelength, crlflength;
230
231 #ifdef DEBUG
232     LOG(log_debug, logtype_papd, "Start translate");
233 #endif
234
235     switch ( markline( in, &start, &linelength, &crlflength )) {
236     case 0 :
237         return( 0 );
238
239     case -1 :
240         return( CH_MORE );
241
242     case -2 :
243         return( CH_ERROR );
244     }
245
246     in->pf_state |= PF_TRANSLATE;
247     lp_write( in, start, linelength + crlflength );
248     compop();
249     CONSUME( in, linelength + crlflength );
250     return ( CH_DONE);
251 }
252
253 int ch_endtranslate(in,out)
254     struct papfile      *in, *out _U_;
255 {
256     char                *start;
257     int                 linelength, crlflength;
258
259 #ifdef DEBUG
260     LOG(log_debug, logtype_papd, "EndTranslate");
261 #endif
262
263     switch ( markline( in, &start, &linelength, &crlflength )) {
264     case 0 :
265         return( 0 );
266
267     case -1 :
268         return( CH_MORE );
269
270     case -2 :
271         return( CH_ERROR );
272     }
273
274     lp_write( in, start, linelength + crlflength );
275     in->pf_state &= ~PF_TRANSLATE;
276     compop();
277     CONSUME( in, linelength + crlflength );
278     return ( CH_DONE);
279 }
280
281 int ch_translateone(in,out)
282     struct papfile      *in, *out _U_;
283 {
284     char                *start;
285     int                 linelength, crlflength;
286
287 #ifdef DEBUG
288     LOG(log_debug, logtype_papd, "TranslateOne");
289 #endif
290
291     switch ( markline( in, &start, &linelength, &crlflength )) {
292     case 0 :
293         return( 0 );
294
295     case -1 :
296         return( CH_MORE );
297
298     case -2 :
299         return( CH_ERROR );
300     }
301
302     in->pf_state |= PF_TRANSLATE;
303     lp_write( in, start, linelength + crlflength );
304     in->pf_state &= ~PF_TRANSLATE;
305     compop();
306     CONSUME( in, linelength + crlflength );
307     return ( CH_DONE);
308 }
309
310
311
312
313 /*
314  * "Header" comments.
315  */
316 struct papd_comment     headers[] = {
317     { "%%Title:",                       NULL,           ch_title,       0 },
318     { "%%For:",                         NULL,           ch_for,         0 },
319     { "%%Creator:",                     NULL,           ch_creator,     0 },
320     { "%%EndComments",                  NULL,           ch_endcomm,     0 },
321     { "%%BeginFeature",                 NULL,           ch_starttranslate,  0 },
322     { "%%EndFeature",                   NULL,           ch_endtranslate,  0 },
323     { "%%BeginPageSetup",               NULL,           ch_starttranslate, 0 },
324     { "%%EndPageSetup",                 NULL,           ch_endtranslate, 0 },
325 #if 0
326     { "%%BeginSetup",                   NULL,           ch_translateone,  0 },
327     { "%%EndSetup",                     NULL,           ch_translateone,  0 },
328     { "%%BeginProlog",                  NULL,           ch_translateone,  0 },
329     { "%%EndProlog",                    NULL,           ch_translateone,  0 },
330     { "%%Page:",                        NULL,           ch_translateone, 0 },
331     { "%%PageTrailer",                  NULL,           ch_translateone, 0 },
332     { "%%Trailer",                      NULL,           ch_translateone, 0 },
333     { "%%EOF",                          NULL,           ch_translateone, 0 },
334 #endif
335     { "%%",                             NULL,           ch_translateone, 0 },
336     { NULL,                             NULL,           NULL,           0 },
337 };