]> arthur.barton.de Git - netatalk.git/blob - etc/papd/magics.c
800c1854271097d95de6be8e03969d3f2a8766a8
[netatalk.git] / etc / papd / magics.c
1 /*
2  * $Id: magics.c,v 1.11 2003-02-17 01:34:35 srittau 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 int ps( infile, outfile, sat )
25     struct papfile      *infile, *outfile;
26     struct sockaddr_at  *sat;
27 {
28     char                        *start;
29     int                         linelength, crlflength;
30     struct papd_comment         *comment;
31
32     for (;;) {
33         if ( (comment = compeek()) ) {
34             switch( (*comment->c_handler)( infile, outfile, sat )) {
35             case CH_DONE :
36                 continue;
37
38             case CH_MORE :
39                 return( CH_MORE );
40
41             default :
42                 return( CH_ERROR );
43             }
44
45         } else {
46             switch ( markline( infile, &start, &linelength, &crlflength )) {
47             case 0 :
48                 /* eof on infile */
49                 outfile->pf_state |= PF_EOF;
50                 lp_close();
51                 return( 0 );
52
53             case -1 :
54                 return( 0 );
55             }
56
57             if ( infile->pf_state & PF_BOT ) {
58                 if (( comment = commatch( start, start+linelength, magics )) != NULL ) {
59                     compush( comment );
60                     continue;   /* top of for (;;) */
61                 }
62                 infile->pf_state &= ~PF_BOT;
63
64                 /* set up spool file */
65                 if ( lp_open( outfile, sat ) < 0 ) {
66                     LOG(log_error, logtype_papd, "lp_open failed" );
67                     spoolerror( outfile, "Ignoring job." );
68                 }
69             }
70
71             /* write to file */
72             lp_write( start, linelength + crlflength );
73             CONSUME( infile, linelength + crlflength );
74         }
75     }
76 }
77
78 int cm_psquery( in, out, sat )
79     struct papfile      *in, *out;
80     struct sockaddr_at  *sat;
81 {
82     struct papd_comment *comment;
83     char                *start;
84     int                 linelength, crlflength;
85
86     for (;;) {
87         switch ( markline( in, &start, &linelength, &crlflength )) {
88         case 0 :
89             /* eof on infile */
90             out->pf_state |= PF_EOF;
91             compop();
92             return( CH_DONE );
93
94         case -1 :
95             return( CH_MORE );
96         }
97
98         if ( in->pf_state & PF_BOT ) {
99             in->pf_state &= ~PF_BOT;
100         } else {
101             if (( comment = commatch( start, start+linelength, queries )) != NULL ) {
102                 compush( comment );
103                 return( CH_DONE );
104             }
105         }
106
107         CONSUME( in, linelength + crlflength );
108     }
109 }
110
111 int cm_psadobe( in, out, sat )
112     struct papfile      *in, *out;
113     struct sockaddr_at  *sat;
114 {
115     char                *start;
116     int                 linelength, crlflength;
117     struct papd_comment *comment = compeek();
118
119     for (;;) {
120         switch ( markline( in, &start, &linelength, &crlflength )) {
121         case 0 :
122             /* eof on infile */
123             out->pf_state |= PF_EOF;
124             compop();
125             return( CH_DONE );
126
127         case -1 :
128             return( CH_MORE );
129         }
130
131         if ( in->pf_state & PF_BOT ) {
132             in->pf_state &= ~PF_BOT;
133             if ( lp_open( out, sat ) < 0 ) {
134                 LOG(log_error, logtype_papd, "lp_open failed" );
135                 spoolerror( out, "Ignoring job." );
136             }
137         } else {
138             if (( comment = commatch( start, start + linelength, headers )) != NULL ) {
139                 compush( comment );
140                 return( CH_DONE );
141             }
142         }
143
144         lp_write( start, linelength + crlflength );
145         CONSUME( in, linelength + crlflength );
146     }
147 }
148
149 char    *Query = "Query";
150
151 int cm_psswitch( in, out, sat )
152     struct papfile      *in, *out;
153     struct sockaddr_at  *sat;
154 {
155     char                *start, *stop, *p;
156     int                 linelength, crlflength;
157
158     switch ( markline( in, &start, &linelength, &crlflength )) {
159     case 0 :
160         /* eof on infile */
161         out->pf_state |= PF_EOF;
162         compop();
163         return( 0 );
164
165     case -1 :
166         return( CH_MORE );
167     }
168
169     stop = start + linelength;
170     for ( p = start; p < stop; p++ ) {
171         if ( *p == ' ' || *p == '\t' ) {
172             break;
173         }
174     }
175     for ( ; p < stop; p++ ) {
176         if ( *p != ' ' && *p != '\t' ) {
177             break;
178         }
179     }
180
181     if ( stop - p >= strlen( Query ) &&
182             strncmp( p, Query, strlen( Query )) == 0 ) {
183         if ( comswitch( magics, cm_psquery ) < 0 ) {
184             LOG(log_error, logtype_papd, "cm_psswitch: can't find psquery!" );
185             exit( 1 );
186         }
187     } else {
188         if ( comswitch( magics, cm_psadobe ) < 0 ) {
189             LOG(log_error, logtype_papd, "cm_psswitch: can't find psadobe!" );
190             exit( 1 );
191         }
192     }
193     return( CH_DONE );
194 }
195
196 struct papd_comment     magics[] = {
197     { "%!PS-Adobe-3.0 Query",   0,                      cm_psquery, C_FULL },
198     { "%!PS-Adobe-3.0",         0,                      cm_psadobe, C_FULL },
199     { "%!PS-Adobe-",            0,                      cm_psswitch,    0 },
200     { 0 },
201 };