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