2 * $Id: ppd.c,v 1.17 2009-10-14 02:24:05 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", NULL },
30 { "*PSVersion", NULL },
32 { "*FreeVM", "33554432" },
37 { "*PCFileName", NULL },
38 { "*ModelName", NULL },
39 { "*NickName", NULL },
40 { "*ColorDevice", NULL },
41 { "*FaxSupport", NULL },
42 { "*TTRasterizer", NULL },
54 static int ppd_inited;
56 static void ppd_init(void)
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(char *buf, size_t bufsize, FILE *stream)
73 int p; /* uninitialized, OK 310105 */
76 while (count < (bufsize - 1) && EOF != (p=fgetc(stream))) {
79 if ( p == '\r' || p == '\n')
83 if (p == EOF && count == 0)
86 /* translate line endings */
87 if ( buf[count - 1] == '\r')
88 buf[count - 1] = '\n';
94 static struct ppdent *getppdent( FILE *stream)
96 static char buf[ 1024 ];
97 static struct ppdent ppdent;
100 ppdent.pe_main = ppdent.pe_option = ppdent.pe_translation =
101 ppdent.pe_value = NULL;
103 while (( p = my_fgets( buf, sizeof( buf ), stream )) != NULL ) {
104 if ( *p != '*' ) { /* main key word */
107 if ( p[ strlen( p ) - 1 ] != '\n') {
108 LOG(log_error, logtype_papd, "getppdent: line too long" );
113 while ( (*p != ' ') && (*p != '\t') && (*p != ':') && (*p != '\n') ) {
116 if ( (*( q + 1 ) == '%') || (*( q + 1 ) == '?') ) { /* comments & queries */
122 ppdent.pe_option = ppdent.pe_translation = ppdent.pe_value = NULL;
126 if ( *p != ':' ) { /* option key word */
129 while ( (*p == ' ') || (*p == '\t') ) {
134 while ( (*p != ':') && (*p != '/') && (*p != '\n') ) {
142 ppdent.pe_option = q;
143 if ( *p == '/' ) { /* translation string */
146 while ( *p != ':' && *p != '\n' ) {
153 ppdent.pe_translation = q;
155 ppdent.pe_translation = NULL;
160 while ( (*p == ' ') || (*p == '\t') ) {
166 while ( *p != '\n' ) {
178 int read_ppd(char *file, int fcnt)
182 struct ppd_feature *pfe;
183 struct ppd_font *pfo;
186 LOG(log_error, logtype_papd, "read_ppd: %s: Too many files!", file );
190 if (( ppdfile = fopen( file, "r" )) == NULL ) {
191 LOG(log_error, logtype_papd, "read_ppd %s: %s", file, strerror(errno) );
195 while (( pe = getppdent( ppdfile )) != NULL ) {
197 if ( strcmp( pe->pe_main, "*Include" ) == 0 ) {
198 read_ppd( pe->pe_value, fcnt + 1 );
203 if ( strcmp( pe->pe_main, "*Font" ) == 0 && pe->pe_option ) {
204 for ( pfo = ppd_fonts; pfo; pfo = pfo->pd_next ) {
205 if ( strcmp( pfo->pd_font, pe->pe_option ) == 0 ) {
213 if (( pfo = (struct ppd_font *)malloc( sizeof( struct ppd_font )))
215 LOG(log_error, logtype_papd, "malloc: %s", strerror(errno) );
219 (char *)malloc( strlen( pe->pe_option ) + 1 )) == NULL ) {
220 LOG(log_error, logtype_papd, "malloc: %s", strerror(errno) );
223 strcpy( pfo->pd_font, pe->pe_option );
224 pfo->pd_next = ppd_fonts;
231 for ( pfe = ppd_features; pfe->pd_name; pfe++ ) {
232 if ( strcmp( pe->pe_main, pfe->pd_name ) == 0 ) {
236 if ( pfe->pd_name && pe->pe_value ) {
237 if (( pfe->pd_value =
238 (char *)malloc( strlen( pe->pe_value ) + 1 )) == NULL ) {
239 LOG(log_error, logtype_papd, "malloc: %s", strerror(errno) );
243 strcpy( pfe->pd_value, pe->pe_value );
252 struct ppd_font *ppd_font( char *font)
254 struct ppd_font *pfo;
257 if ( ! ppd_inited ) {
262 for ( pfo = ppd_fonts; pfo; pfo = pfo->pd_next ) {
263 if ( strcmp( pfo->pd_font, font ) == 0 ) {
270 struct ppd_feature *ppd_feature( const char *feature, int len)
272 struct ppd_feature *pfe;
273 char ppd_feature_main[ 256 ];
278 if ( ! ppd_inited ) {
283 if (len > sizeof(ppd_feature_main) -1)
286 for ( end = feature + len, p = feature, q = ppd_feature_main;
287 (p <= end) && (*p != '\n') && (*p != '\r'); p++, q++ ) {
295 for ( pfe = ppd_features; pfe->pd_name; pfe++ ) {
296 if ( (strcmp( pfe->pd_name, ppd_feature_main ) == 0) && pfe->pd_value ) {