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