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