2 * $Id: ppd.c,v 1.14 2009-01-21 04:04:20 didg Exp $
4 * Copyright (c) 1995 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
16 #include <atalk/logger.h>
17 #include <sys/types.h>
18 #include <sys/param.h>
20 #include <netatalk/at.h>
21 #include <atalk/atp.h>
26 struct ppd_font *ppd_fonts = NULL;
28 struct ppd_feature ppd_features[] = {
29 { "*LanguageLevel", 0 },
32 { "*FreeVM", "33554432" },
40 { "*ColorDevice", 0 },
42 { "*TTRasterizer", 0 },
54 static int ppd_inited;
56 static void ppd_init()
63 if (printer->p_ppdfile)
64 read_ppd( printer->p_ppdfile, 0 );
69 /* quick and ugly hack to be able to read
70 ppd files with Mac line ending */
71 static char* my_fgets(buf, bufsize, stream)
76 int p; /* uninitialized, OK 310105 */
79 while (count < (bufsize - 1) && EOF != (p=fgetc(stream))) {
82 if ( p == '\r' || p == '\n')
86 if (p == EOF && count == 0)
89 /* translate line endings */
90 if ( buf[count - 1] == '\r')
91 buf[count - 1] = '\n';
97 static struct ppdent *getppdent( stream )
100 static char buf[ 1024 ];
101 static struct ppdent ppdent;
104 ppdent.pe_main = ppdent.pe_option = ppdent.pe_translation =
105 ppdent.pe_value = NULL;
107 while (( p = my_fgets( buf, sizeof( buf ), stream )) != NULL ) {
108 if ( *p != '*' ) { /* main key word */
111 if ( p[ strlen( p ) - 1 ] != '\n') {
112 LOG(log_error, logtype_papd, "getppdent: line too long" );
117 while ( (*p != ' ') && (*p != '\t') && (*p != ':') && (*p != '\n') ) {
120 if ( (*( q + 1 ) == '%') || (*( q + 1 ) == '?') ) { /* comments & queries */
126 ppdent.pe_option = ppdent.pe_translation = ppdent.pe_value = NULL;
130 if ( *p != ':' ) { /* option key word */
133 while ( (*p == ' ') || (*p == '\t') ) {
138 while ( (*p != ':') && (*p != '/') && (*p != '\n') ) {
146 ppdent.pe_option = q;
147 if ( *p == '/' ) { /* translation string */
150 while ( *p != ':' && *p != '\n' ) {
157 ppdent.pe_translation = q;
159 ppdent.pe_translation = NULL;
164 while ( (*p == ' ') || (*p == '\t') ) {
170 while ( *p != '\n' ) {
182 int read_ppd( file, fcnt )
188 struct ppd_feature *pfe;
189 struct ppd_font *pfo;
192 LOG(log_error, logtype_papd, "read_ppd: %s: Too many files!", file );
196 if (( ppdfile = fopen( file, "r" )) == NULL ) {
197 LOG(log_error, logtype_papd, "read_ppd %s: %s", file, strerror(errno) );
201 while (( pe = getppdent( ppdfile )) != NULL ) {
203 if ( strcmp( pe->pe_main, "*Include" ) == 0 ) {
204 read_ppd( pe->pe_value, fcnt + 1 );
209 if ( strcmp( pe->pe_main, "*Font" ) == 0 && pe->pe_option ) {
210 for ( pfo = ppd_fonts; pfo; pfo = pfo->pd_next ) {
211 if ( strcmp( pfo->pd_font, pe->pe_option ) == 0 ) {
219 if (( pfo = (struct ppd_font *)malloc( sizeof( struct ppd_font )))
221 LOG(log_error, logtype_papd, "malloc: %s", strerror(errno) );
225 (char *)malloc( strlen( pe->pe_option ) + 1 )) == NULL ) {
226 LOG(log_error, logtype_papd, "malloc: %s", strerror(errno) );
229 strcpy( pfo->pd_font, pe->pe_option );
230 pfo->pd_next = ppd_fonts;
237 for ( pfe = ppd_features; pfe->pd_name; pfe++ ) {
238 if ( strcmp( pe->pe_main, pfe->pd_name ) == 0 ) {
242 if ( pfe->pd_name && pe->pe_value ) {
243 if (( pfe->pd_value =
244 (char *)malloc( strlen( pe->pe_value ) + 1 )) == NULL ) {
245 LOG(log_error, logtype_papd, "malloc: %s", strerror(errno) );
249 strcpy( pfe->pd_value, pe->pe_value );
258 struct ppd_font *ppd_font( font )
261 struct ppd_font *pfo;
264 if ( ! ppd_inited ) {
269 for ( pfo = ppd_fonts; pfo; pfo = pfo->pd_next ) {
270 if ( strcmp( pfo->pd_font, font ) == 0 ) {
277 struct ppd_feature *ppd_feature( feature, len )
281 struct ppd_feature *pfe;
282 char ppd_feature_main[ 256 ];
287 if ( ! ppd_inited ) {
292 for ( end = feature + len, p = feature, q = ppd_feature_main;
293 (p <= end) && (*p != '\n') && (*p != '\r'); p++, q++ ) {
301 for ( pfe = ppd_features; pfe->pd_name; pfe++ ) {
302 if ( (strcmp( pfe->pd_name, ppd_feature_main ) == 0) && pfe->pd_value ) {