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