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