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