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