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