]> arthur.barton.de Git - netatalk.git/blob - etc/papd/ppd.c
implemented config.h
[netatalk.git] / etc / papd / ppd.c
1 /*
2  * Copyright (c) 1995 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 <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/syslog.h>
14 #include <sys/types.h>
15 #include <sys/param.h>
16 #include <sys/time.h>
17 #include <netatalk/at.h>
18 #include <atalk/atp.h>
19
20 #include "printer.h"
21 #include "ppd.h"
22
23 struct ppd_font         *ppd_fonts = NULL;
24
25 struct ppd_feature      ppd_features[] = {
26     { "*LanguageLevel", 0 },
27     { "*PSVersion",     0 },
28     { "*FreeVM",        0 },
29     { "*Product",       0 },
30     { "*PCFileName",    0 },
31     { "*ModelName",     0 },
32     { "*NickName",      0 },
33     { "*ColorDevice",   0 },
34     { "*FaxSupport",    0 },
35     { "*TTRasterizer",  0 },
36     { 0, 0 },
37 };
38
39 struct ppdent {
40     char        *pe_main;
41     char        *pe_option;
42     char        *pe_translation;
43     char        *pe_value;
44 };
45
46 #ifndef SHOWPPD
47 int ppd_inited = 0;
48
49 ppd_init()
50 {
51     if ( ppd_inited ) {
52         return( -1 );
53     }
54     ppd_inited++;
55
56     read_ppd( printer->p_ppdfile, 0 );
57 }
58 #endif SHOWPPD
59
60     struct ppdent *
61 getppdent( stream )
62     FILE        *stream;
63 {
64     static char                 buf[ 1024 ];
65     static struct ppdent        ppdent;
66     char                        *p, *q;
67
68     ppdent.pe_main = ppdent.pe_option = ppdent.pe_translation =
69             ppdent.pe_value = NULL;
70
71     while (( p = fgets( buf, sizeof( buf ), stream )) != NULL ) {
72         if ( *p != '*' ) {      /* main key word */
73             continue;
74         }
75         if ( p[ strlen( p ) - 1 ] != '\n' ) {
76             syslog( LOG_ERR, "getppdent: line too long" );
77             continue;
78         }
79
80         q = p;
81         while ( *p != ' ' && *p != '\t' && *p != ':' && *p != '\n' ) {
82             p++;
83         }
84         if ( *( q + 1 ) == '%' || *( q + 1 ) == '?' ) { /* comments & queries */
85             continue;
86         }
87         ppdent.pe_main = q;
88         if ( *p == '\n' ) {
89             *p = '\0';
90             ppdent.pe_option = ppdent.pe_translation = ppdent.pe_value = NULL;
91             return( &ppdent );
92         }
93
94         if ( *p != ':' ) {      /* option key word */
95             *p++ = '\0';
96
97             while ( *p == ' ' || *p == '\t' ) {
98                 p++;
99             }
100
101             q = p;
102             while ( *p != ':' && *p != '/' && *p != '\n' ) {
103                 p++;
104             }
105
106             if ( *p == '\n' ) {
107                 continue;
108             }
109
110             ppdent.pe_option = q;
111             if ( *p == '/' ) {  /* translation string */
112                 *p++ = '\0';
113                 q = p;
114                 while ( *p != ':' && *p != '\n' ) {
115                     p++;
116                 }
117                 if ( *p != ':' ) {
118                     continue;
119                 }
120
121                 ppdent.pe_translation = q;
122             } else {
123                 ppdent.pe_translation = NULL;
124             }
125         }
126         *p++ = '\0';
127
128         while ( *p == ' ' || *p == '\t' ) {
129             p++;
130         }
131
132         /* value */
133         q = p;
134         while ( *p != '\n' ) {
135             p++;
136         }
137         *p = '\0';
138         ppdent.pe_value = q;
139
140         return( &ppdent );
141     }
142
143     return( NULL );
144 }
145
146 read_ppd( file, fcnt )
147     char        *file;
148     int         fcnt;
149 {
150     FILE                *ppdfile;
151     struct ppdent       *pe;
152     struct ppd_feature  *pfe;
153     struct ppd_font     *pfo;
154
155     if ( fcnt > 20 ) {
156         syslog( LOG_ERR, "read_ppd: %s: Too many files!", file );
157         return( -1 );
158     }
159
160     if (( ppdfile = fopen( file, "r" )) == NULL ) {
161         syslog( LOG_ERR, "read_ppd %s: %m", file );
162         return( -1 );
163     }
164
165     while (( pe = getppdent( ppdfile )) != NULL ) {
166         /* *Include files */
167         if ( strcmp( pe->pe_main, "*Include" ) == 0 ) {
168             read_ppd( pe->pe_value, fcnt + 1 );
169             continue;
170         }
171
172         /* *Font */
173         if ( strcmp( pe->pe_main, "*Font" ) == 0 ) {
174             for ( pfo = ppd_fonts; pfo; pfo = pfo->pd_next ) {
175                 if ( strcmp( pfo->pd_font, pe->pe_option ) == 0 ) {
176                     break;
177                 }
178             }
179             if ( pfo ) {
180                 continue;
181             }
182
183             if (( pfo = (struct ppd_font *)malloc( sizeof( struct ppd_font )))
184                     == NULL ) {
185                 syslog( LOG_ERR, "malloc: %m" );
186                 exit( 1 );
187             }
188             if (( pfo->pd_font =
189                     (char *)malloc( strlen( pe->pe_option ) + 1 )) == NULL ) {
190                 syslog( LOG_ERR, "malloc: %m" );
191                 exit( 1 );
192             }
193             strcpy( pfo->pd_font, pe->pe_option );
194             pfo->pd_next = ppd_fonts;
195             ppd_fonts = pfo;
196             continue;
197         }
198
199
200         /* Features */
201         for ( pfe = ppd_features; pfe->pd_name; pfe++ ) {
202             if ( strcmp( pe->pe_main, pfe->pd_name ) == 0 ) {
203                 break;
204             }
205         }
206         if ( pfe->pd_name && pfe->pd_value == NULL ) {
207             if (( pfe->pd_value =
208                     (char *)malloc( strlen( pe->pe_value ) + 1 )) == NULL ) {
209                 syslog( LOG_ERR, "malloc: %m" );
210                 exit( 1 );
211             }
212
213             strcpy( pfe->pd_value, pe->pe_value );
214             continue;
215         }
216     }
217
218     fclose( ppdfile );
219     return( 0 );
220 }
221
222     struct ppd_font *
223 ppd_font( font )
224     char        *font;
225 {
226     struct ppd_font     *pfo;
227
228 #ifndef SHOWPPD
229     if ( ! ppd_inited ) {
230         ppd_init();
231     }
232 #endif SHOWPPD
233
234     for ( pfo = ppd_fonts; pfo; pfo = pfo->pd_next ) {
235         if ( strcmp( pfo->pd_font, font ) == 0 ) {
236             return( pfo );
237         }
238     }
239     return( NULL );
240 }
241
242     struct ppd_feature *
243 ppd_feature( feature, len )
244     char        *feature;
245     int         len;
246 {
247     struct ppd_feature  *pfe;
248     char                main[ 256 ];
249     char                *end, *p, *q;
250
251 #ifndef SHOWPPD
252     if ( ! ppd_inited ) {
253         ppd_init();
254     }
255 #endif SHOWPPD
256
257     for ( end = feature + len, p = feature, q = main;
258             p <= end && *p != '\n' && *p != '\r'; p++, q++ ) {
259         *q = *p;
260     }
261     if ( p > end ) {
262         return( NULL );
263     }
264     *q = '\0';
265
266     for ( pfe = ppd_features; pfe->pd_name; pfe++ ) {
267         if ( strcmp( pfe->pd_name, main ) == 0 && pfe->pd_value ) {
268             return( pfe );
269         }
270     }
271
272     return( NULL );
273 }