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