]> arthur.barton.de Git - netatalk.git/blob - etc/papd/magics.c
papd better parsing of postscript requests
[netatalk.git] / etc / papd / magics.c
1 /*
2  * $Id: magics.c,v 1.14 2009-02-02 16:04:33 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 <atalk/logger.h>
13 #include <sys/param.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <stdlib.h>
17
18 #include <netatalk/at.h>
19
20 #include "file.h"
21 #include "comment.h"
22 #include "lp.h"
23
24 static int state=0;
25
26 static void parser_error(outfile)
27     struct papfile      *outfile;
28 {
29                 spoolerror( outfile, "Comments error, Ignoring job." );
30                 outfile->pf_state |= PF_EOF;
31                 lp_close();
32 }
33
34 int ps( infile, outfile, sat )
35     struct papfile      *infile, *outfile;
36     struct sockaddr_at  *sat;
37 {
38     char                        *start;
39     int                         linelength, crlflength;
40     struct papd_comment         *comment;
41
42     for (;;) {
43         if ( infile->pf_state & PF_STW ) {
44                 infile->pf_state &= ~PF_STW;
45                 /* set up spool file */
46                 if ( lp_open( outfile, sat ) < 0 && !state) {
47                     LOG(log_error, logtype_papd, "lp_open failed" );
48                     spoolerror( outfile, "Ignoring job." );
49                 }
50                 state = 1;
51         }       
52         if ( (comment = compeek()) ) {
53             switch( (*comment->c_handler)( infile, outfile, sat )) {
54             case CH_DONE :
55                 continue;
56
57             case CH_MORE :
58                 return( CH_MORE );
59
60             case CH_ERROR :
61                 parser_error(outfile);
62                 return( 0 );
63
64             default :
65                 return( CH_ERROR );
66             }
67         } else {
68             switch ( markline( infile, &start, &linelength, &crlflength )) {
69             case 0 :
70                 /* eof on infile */
71                 outfile->pf_state |= PF_EOF;
72                 lp_close();
73                 return( 0 );
74
75             case -2:
76                 parser_error(outfile);
77                 return( 0 );
78
79             case -1 :
80                 return( 0 );
81             }
82
83             if ( infile->pf_state & PF_BOT ) {
84                 if (( comment = commatch( start, start+linelength, magics )) != NULL ) {
85                     compush( comment );
86                     continue;   /* top of for (;;) */
87                 }
88 #if 0
89                 infile->pf_state &= ~PF_BOT;
90
91                 /* set up spool file */
92                 if ( lp_open( outfile, sat ) < 0 ) {
93                     LOG(log_error, logtype_papd, "lp_open failed" );
94                     spoolerror( outfile, "Ignoring job." );
95                 }
96 #endif
97             }
98
99             /* write to file */
100             lp_write( infile, start, linelength + crlflength );
101             CONSUME( infile, linelength + crlflength );
102         }
103     }
104 }
105
106 int cm_psquery( in, out, sat )
107     struct papfile      *in, *out;
108     struct sockaddr_at  *sat _U_;
109 {
110     struct papd_comment *comment;
111     char                *start;
112     int                 linelength, crlflength;
113
114     for (;;) {
115         switch ( markline( in, &start, &linelength, &crlflength )) {
116         case 0 :
117             /* eof on infile */
118             out->pf_state |= PF_EOF;
119             compop();
120             return( CH_DONE );
121
122         case -1 :
123             return( CH_MORE );
124
125         case -2 :
126             return( CH_ERROR );
127         }
128
129         if ( in->pf_state & PF_BOT ) {
130             in->pf_state &= ~PF_BOT;
131         } else {
132             if (( comment = commatch( start, start+linelength, queries )) != NULL ) {
133                 compush( comment );
134                 return( CH_DONE );
135             }
136         }
137
138         CONSUME( in, linelength + crlflength );
139     }
140 }
141
142 int cm_psadobe( in, out, sat )
143     struct papfile      *in, *out;
144     struct sockaddr_at  *sat _U_;
145 {
146     char                *start;
147     int                 linelength, crlflength;
148     struct papd_comment *comment = compeek();
149
150     for (;;) {
151         switch ( markline( in, &start, &linelength, &crlflength )) {
152         case 0 :
153             /* eof on infile */
154             out->pf_state |= PF_EOF;
155             compop();
156             return( CH_DONE );
157
158         case -1 :
159             return( CH_MORE );
160
161         case -2 :
162             return( CH_ERROR );
163         }
164         if ( in->pf_state & PF_BOT ) {
165             in->pf_state &= ~PF_BOT;
166 #if 0
167             if ( lp_open( out, sat ) < 0 ) {
168                 LOG(log_error, logtype_papd, "lp_open failed" );
169                 spoolerror( out, "Ignoring job." );
170             }
171 #endif
172         } else {
173             if (( comment = commatch( start, start + linelength, headers )) != NULL ) {
174                 compush( comment );
175                 return( CH_DONE );
176             }
177         }
178
179         lp_write( in, start, linelength + crlflength );
180         CONSUME( in, linelength + crlflength );
181     }
182 }
183
184 char    *Query = "Query";
185
186 int cm_psswitch( in, out, sat )
187     struct papfile      *in, *out;
188     struct sockaddr_at  *sat _U_;
189 {
190     char                *start, *stop, *p;
191     int                 linelength, crlflength;
192
193     switch ( markline( in, &start, &linelength, &crlflength )) {
194     case 0 :
195         /* eof on infile */
196         out->pf_state |= PF_EOF;
197         compop();
198         return( 0 );
199
200     case -1 :
201         return( CH_MORE );
202
203     case -2 :
204         return( CH_ERROR );
205     }
206
207     stop = start + linelength;
208     for ( p = start; p < stop; p++ ) {
209         if ( *p == ' ' || *p == '\t' ) {
210             break;
211         }
212     }
213     for ( ; p < stop; p++ ) {
214         if ( *p != ' ' && *p != '\t' ) {
215             break;
216         }
217     }
218
219     if ( (size_t)(stop - p) >= strlen( Query ) &&
220             strncmp( p, Query, strlen( Query )) == 0 ) {
221         if ( comswitch( magics, cm_psquery ) < 0 ) {
222             LOG(log_error, logtype_papd, "cm_psswitch: can't find psquery!" );
223             exit( 1 );
224         }
225     } else {
226         if ( comswitch( magics, cm_psadobe ) < 0 ) {
227             LOG(log_error, logtype_papd, "cm_psswitch: can't find psadobe!" );
228             exit( 1 );
229         }
230     }
231     return( CH_DONE );
232 }
233
234
235 struct papd_comment     magics[] = {
236     { "%!PS-Adobe-3.0 Query",   NULL,                   cm_psquery, C_FULL },
237     { "%!PS-Adobe-3.0",         NULL,                   cm_psadobe, C_FULL },
238     { "%!PS-Adobe-",            NULL,                   cm_psswitch,    0 },
239     { NULL,                     NULL,                   NULL,           0 },
240 };