]> arthur.barton.de Git - netatalk.git/blobdiff - etc/papd/ppd.c
Big configure.in cleanup
[netatalk.git] / etc / papd / ppd.c
index 783719feaf4eaecc1ef6d103a02532875cbc83e0..f16469289f9a4af9af93cba62fad839025affefe 100644 (file)
@@ -1,12 +1,19 @@
 /*
+ * $Id: ppd.c,v 1.17 2009-10-14 02:24:05 didg Exp $
+ *
  * Copyright (c) 1995 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif /* HAVE_CONFIG_H */
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/syslog.h>
+#include <errno.h>
+#include <atalk/logger.h>
 #include <sys/types.h>
 #include <sys/param.h>
 #include <sys/time.h>
 struct ppd_font                *ppd_fonts = NULL;
 
 struct ppd_feature     ppd_features[] = {
-    { "*LanguageLevel",        0 },
-    { "*PSVersion",    0 },
-    { "*FreeVM",       0 },
-    { "*Product",      0 },
-    { "*PCFileName",   0 },
-    { "*ModelName",    0 },
-    { "*NickName",     0 },
-    { "*ColorDevice",  0 },
-    { "*FaxSupport",   0 },
-    { "*TTRasterizer", 0 },
-    { 0, 0 },
+    { "*LanguageLevel",        NULL },
+    { "*PSVersion",    NULL },
+#ifdef HAVE_CUPS
+    { "*FreeVM",       "33554432" },
+#else
+    { "*FreeVM",       NULL },
+#endif
+    { "*Product",      NULL },
+    { "*PCFileName",   NULL },
+    { "*ModelName",    NULL },
+    { "*NickName",     NULL },
+    { "*ColorDevice",  NULL },
+    { "*FaxSupport",   NULL },
+    { "*TTRasterizer", NULL },
+    { NULL, NULL },
 };
 
 struct ppdent {
@@ -39,46 +50,48 @@ struct ppdent {
     char       *pe_value;
 };
 
-#ifdef notdef
-main( ac, av )
-    int                ac;
-    char       **av;
-{
-    struct ppd_feature *pfe;
-    struct ppd_font    *pfo;
+#ifndef SHOWPPD
+static int ppd_inited;
 
-    if ( ac != 2 ) {
-       fprintf( stderr, "Usage:\t%s ppdfile\n", av[ 0 ] );
-       exit( 1 );
-    }
+static void ppd_init(void)
+{
+    if (ppd_inited)
+        return;
 
-    read_ppd( av[ 1 ], 0 );
-    for ( pfo = ppd_fonts; pfo; pfo = pfo->pd_next ) {
-       printf( "Font: %s\n", pfo->pd_font );
-    }
-    for ( pfe = ppd_features; pfe->pd_name; pfe++ ) {
-       printf( "Feature: %s %s\n", pfe->pd_name, pfe->pd_value );
-    }
+    ppd_inited++;
 
-    exit( 0 );
+    if (printer->p_ppdfile)
+        read_ppd( printer->p_ppdfile, 0 );
 }
-#endif notdef
+#endif /* SHOWPPD */
 
-int ppd_inited = 0;
 
-ppd_init()
+/* quick and ugly hack to be able to read
+   ppd files with Mac line ending */
+static char* my_fgets(char *buf, size_t bufsize, FILE *stream)
 {
-    if ( ppd_inited ) {
-       return( -1 );
+    int p;           /* uninitialized, OK 310105 */
+    size_t count = 0;
+    while (count < (bufsize - 1) && EOF != (p=fgetc(stream))) {
+        buf[count] = p;
+        count++;
+        if ( p == '\r' || p == '\n')
+           break;
     }
-    ppd_inited++;
 
-    read_ppd( printer->p_ppdfile, 0 );
+    if (p == EOF && count == 0)
+        return NULL;
+
+    /* translate line endings */
+    if ( buf[count - 1] == '\r')
+        buf[count - 1] = '\n';
+
+    buf[count] = 0;
+    return buf;
 }
 
-    struct ppdent *
-getppdent( stream )
-    FILE       *stream;
+static struct ppdent *getppdent( FILE *stream)
 {
     static char                        buf[ 1024 ];
     static struct ppdent       ppdent;
@@ -87,20 +100,20 @@ getppdent( stream )
     ppdent.pe_main = ppdent.pe_option = ppdent.pe_translation =
            ppdent.pe_value = NULL;
 
-    while (( p = fgets( buf, sizeof( buf ), stream )) != NULL ) {
+    while (( p = my_fgets( buf, sizeof( buf ), stream )) != NULL ) {
        if ( *p != '*' ) {      /* main key word */
            continue;
        }
-       if ( p[ strlen( p ) - 1 ] != '\n' ) {
-           syslog( LOG_ERR, "getppdent: line too long" );
+       if ( p[ strlen( p ) - 1 ] != '\n') {
+           LOG(log_error, logtype_papd, "getppdent: line too long" );
            continue;
        }
 
        q = p;
-       while ( *p != ' ' && *p != '\t' && *p != ':' && *p != '\n' ) {
+       while ( (*p != ' ') && (*p != '\t') && (*p != ':') && (*p != '\n') ) {
            p++;
        }
-       if ( *( q + 1 ) == '%' || *( q + 1 ) == '?' ) { /* comments & queries */
+       if ( (*( q + 1 ) == '%') || (*( q + 1 ) == '?') ) {     /* comments & queries */
            continue;
        }
        ppdent.pe_main = q;
@@ -113,12 +126,12 @@ getppdent( stream )
        if ( *p != ':' ) {      /* option key word */
            *p++ = '\0';
 
-           while ( *p == ' ' || *p == '\t' ) {
+           while ( (*p == ' ') || (*p == '\t') ) {
                p++;
            }
 
            q = p;
-           while ( *p != ':' && *p != '/' && *p != '\n' ) {
+           while ( (*p != ':') && (*p != '/') && (*p != '\n') ) {
                p++;
            }
 
@@ -144,41 +157,25 @@ getppdent( stream )
        }
        *p++ = '\0';
 
-       while ( *p == ' ' || *p == '\t' ) {
+       while ( (*p == ' ') || (*p == '\t') ) {
            p++;
        }
 
        /* value */
-       if ( *p == '"' ) {
+       q = p;
+       while ( *p != '\n' ) {
            p++;
-           q = p;
-
-           while ( *p != '"' && *p != '\n' ) {
-               p++;
-           }
-
-           if ( *p == '\n' ) {
-               continue;
-           }
-           *p = '\0';
-           ppdent.pe_value = q;
-       } else {
-           q = p;
-           while ( *p != '\n' ) {
-               p++;
-           }
-           *p = '\0';
-           ppdent.pe_value = q;
        }
+       *p = '\0';
+       ppdent.pe_value = q;
+
        return( &ppdent );
     }
 
     return( NULL );
 }
 
-read_ppd( file, fcnt )
-    char       *file;
-    int                fcnt;
+int read_ppd(char *file, int fcnt)
 {
     FILE               *ppdfile;
     struct ppdent      *pe;
@@ -186,12 +183,12 @@ read_ppd( file, fcnt )
     struct ppd_font    *pfo;
 
     if ( fcnt > 20 ) {
-       syslog( LOG_ERR, "read_ppd: %s: Too many files!", file );
+       LOG(log_error, logtype_papd, "read_ppd: %s: Too many files!", file );
        return( -1 );
     }
 
     if (( ppdfile = fopen( file, "r" )) == NULL ) {
-       syslog( LOG_ERR, "read_ppd %s: %m", file );
+       LOG(log_error, logtype_papd, "read_ppd %s: %s", file, strerror(errno) );
        return( -1 );
     }
 
@@ -203,7 +200,7 @@ read_ppd( file, fcnt )
        }
 
        /* *Font */
-       if ( strcmp( pe->pe_main, "*Font" ) == 0 ) {
+       if ( strcmp( pe->pe_main, "*Font" ) == 0 && pe->pe_option ) {
            for ( pfo = ppd_fonts; pfo; pfo = pfo->pd_next ) {
                if ( strcmp( pfo->pd_font, pe->pe_option ) == 0 ) {
                    break;
@@ -215,12 +212,12 @@ read_ppd( file, fcnt )
 
            if (( pfo = (struct ppd_font *)malloc( sizeof( struct ppd_font )))
                    == NULL ) {
-               syslog( LOG_ERR, "malloc: %m" );
+               LOG(log_error, logtype_papd, "malloc: %s", strerror(errno) );
                exit( 1 );
            }
            if (( pfo->pd_font =
                    (char *)malloc( strlen( pe->pe_option ) + 1 )) == NULL ) {
-               syslog( LOG_ERR, "malloc: %m" );
+               LOG(log_error, logtype_papd, "malloc: %s", strerror(errno) );
                exit( 1 );
            }
            strcpy( pfo->pd_font, pe->pe_option );
@@ -236,10 +233,10 @@ read_ppd( file, fcnt )
                break;
            }
        }
-       if ( pfe->pd_name && pfe->pd_value == NULL ) {
+       if ( pfe->pd_name && pe->pe_value ) { 
            if (( pfe->pd_value =
                    (char *)malloc( strlen( pe->pe_value ) + 1 )) == NULL ) {
-               syslog( LOG_ERR, "malloc: %m" );
+               LOG(log_error, logtype_papd, "malloc: %s", strerror(errno) );
                exit( 1 );
            }
 
@@ -252,15 +249,15 @@ read_ppd( file, fcnt )
     return( 0 );
 }
 
-    struct ppd_font *
-ppd_font( font )
-    char       *font;
+struct ppd_font *ppd_font( char *font)
 {
     struct ppd_font    *pfo;
 
+#ifndef SHOWPPD
     if ( ! ppd_inited ) {
        ppd_init();
     }
+#endif /* SHOWPPD */
 
     for ( pfo = ppd_fonts; pfo; pfo = pfo->pd_next ) {
        if ( strcmp( pfo->pd_font, font ) == 0 ) {
@@ -270,21 +267,24 @@ ppd_font( font )
     return( NULL );
 }
 
-    struct ppd_feature *
-ppd_feature( feature, len )
-    char       *feature;
-    int                len;
+struct ppd_feature *ppd_feature( const char *feature, int len)
 {
     struct ppd_feature *pfe;
-    char               main[ 256 ];
-    char               *end, *p, *q;
+    char               ppd_feature_main[ 256 ];
+    const char         *end, *p;
+    char               *q;
 
+#ifndef SHOWPPD
     if ( ! ppd_inited ) {
        ppd_init();
     }
+#endif /* SHOWPPD */
 
-    for ( end = feature + len, p = feature, q = main;
-           p <= end && *p != '\n' && *p != '\r'; p++, q++ ) {
+    if (len > sizeof(ppd_feature_main) -1)
+        return( NULL );
+        
+    for ( end = feature + len, p = feature, q = ppd_feature_main;
+           (p <= end) && (*p != '\n') && (*p != '\r'); p++, q++ ) {
        *q = *p;
     }
     if ( p > end ) {
@@ -293,7 +293,7 @@ ppd_feature( feature, len )
     *q = '\0';
 
     for ( pfe = ppd_features; pfe->pd_name; pfe++ ) {
-       if ( strcmp( pfe->pd_name, main ) == 0 && pfe->pd_value ) {
+       if ( (strcmp( pfe->pd_name, ppd_feature_main ) == 0) && pfe->pd_value ) {
            return( pfe );
        }
     }