]> arthur.barton.de Git - netatalk.git/blobdiff - etc/papd/headers.c
Remove bdb env on exit
[netatalk.git] / etc / papd / headers.c
index 2aa3d9ea5acbf556c4ec6e777e287fccacfc56b7..dd315396c16cf226077ccaadb42de66b54f0b90d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: headers.c,v 1.10 2005-04-28 20:49:49 bfernhomberg Exp $
+ * $Id: headers.c,v 1.14 2009-10-29 13:38:15 didg Exp $
  *
  * Copyright (c) 1990,1994 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
@@ -12,6 +12,7 @@
 #include <sys/param.h>
 #include <string.h>
 #include <stdio.h>
+#include <stdlib.h>
 
 #include <netatalk/at.h>
 #include <atalk/logger.h>
 int ch_title( struct papfile *, struct papfile * );
 int ch_for( struct papfile *, struct papfile * );
 
-
-int ch_for( in, out )
-       struct papfile  *in, *out _U_;
+static char *get_text(char *start, int linelength)
 {
-    char                *start, *stop, *p, *q, c;
-    int                 linelength, crlflength;
+    char *p, *q;
+    char *t, *ret;
+    char *stop;
+    
+    /* 1023 is arbitrary 255 max for comment but some may be escape \xxx and space and keyword */
 
-    switch ( markline( in, &start, &linelength, &crlflength )) {
-    case 0 :
-        return( 0 );
+    if (linelength > 1023)
+        return NULL;
 
-    case -1 :
-        return( CH_MORE );
-    }
+    t = ret = calloc(1, linelength +1);
+
+    if (!ret)
+        return NULL;
 
     stop = start + linelength;
     for ( p = start; p < stop; p++ ) {
         if ( *p == ':' ) {
+            p++;
             break;
         }
     }
-
+    
     for ( ; p < stop; p++ ) {
-        if ( *p == '(' ) {
+        if (*p != ' ' && *p != '\t') {
             break;
         }
     }
 
-    for ( q = p; q < stop; q++ ) {
-        if ( *q == ')' ) {
-            break;
+    if ( p < stop && *p == '(' ) {
+        int count;
+        /* start with ( then it's a <text> */ 
+        p++;
+        for ( q = p, count = 1; q < stop; q++, t++ ) {
+            if (*q == '(') {
+              count++;
+            }
+            else if ( *q == ')' ) {
+                count--;
+                if (!count) {
+                    break;
+                }
+            }
+            *t = *q;
         }
     }
+    else {
+        /* it's a textline */
+        for ( q = p; q < stop; q++, t++ ) {
+            *t = *q;
+        }
+    }
+    return ret;
+}
 
-    if ( q < stop && p < stop ) {
-        p++;
-        c = *q;
-        *q = '\0';
-       lp_for ( p );
-        *q = c;
+int ch_for( struct papfile *in, struct papfile *out _U_)
+{
+    char                *start, *cmt;
+    int                 linelength, crlflength;
+
+    switch ( markline( in, &start, &linelength, &crlflength )) {
+    case 0 :
+        return( 0 );
+
+    case -1 :
+        return( CH_MORE );
+
+    case -2 :
+        return( CH_ERROR );
+    }
+
+    cmt = get_text(start, linelength);
+
+    if ( cmt ) {
+       lp_for ( cmt );
+       free(cmt);
     }
 
     in->pf_state |= PF_TRANSLATE;
@@ -73,10 +111,9 @@ int ch_for( in, out )
     return( CH_DONE );
 }
 
-int ch_title( in, out )
-    struct papfile     *in, *out _U_;
+int ch_title( struct papfile *in, struct papfile *out _U_)
 {
-    char               *start, *stop, *p, *q, c;
+    char               *start, *cmt;
     int                        linelength, crlflength;
 
     switch ( markline( in, &start, &linelength, &crlflength )) {
@@ -85,37 +122,20 @@ int ch_title( in, out )
 
     case -1 :
        return( CH_MORE );
+
+    case -2 :
+        return( CH_ERROR );
     }
 
 #ifdef DEBUG
-    LOG(log_debug, logtype_papd, "Parsing %%Title");
+    LOG(log_debug9, logtype_papd, "Parsing %%Title");
 #endif
 
-    stop = start + linelength;
-    for ( p = start; p < stop; p++ ) {
-       if ( *p == ':' ) {
-           break;
-       }
-    }
-
-    for ( ; p < stop; p++ ) {
-       if ( *p == '(' ) {
-           break;
-       }
-    }
-
-    for ( q = p; q < stop; q++ ) {
-       if ( *q == ')' ) {
-           break;
-       }
-    }
+    cmt = get_text(start, linelength);
 
-    if ( q < stop && p < stop ) {
-       p++;
-       c = *q;
-       *q = '\0';
-       lp_job( p );
-       *q = c;
+    if ( cmt ) {
+       lp_job( cmt );
+       free(cmt);
     }
 
     in->pf_state |= PF_TRANSLATE;
@@ -137,10 +157,9 @@ static int guess_creator ( char *creator )
 }
 
 
-int ch_creator( in, out )
-    struct papfile     *in, *out _U_;
+int ch_creator( struct papfile *in, struct papfile *out _U_)
 {
-    char               *start, *stop, *p, *q, c;
+    char               *start, *cmt;
     int                        linelength, crlflength;
 
     switch ( markline( in, &start, &linelength, &crlflength )) {
@@ -149,34 +168,17 @@ int ch_creator( in, out )
 
     case -1 :
        return( CH_MORE );
-    }
-
-    stop = start + linelength;
-    for ( p = start; p < stop; p++ ) {
-       if ( *p == ':' ) {
-           break;
-       }
-    }
 
-    for ( ; p < stop; p++ ) {
-       if ( *p == '(' ) {
-           break;
-       }
+    case -2 :
+        return( CH_ERROR );
     }
 
-    for ( q = p; q < stop; q++ ) {
-       if ( *q == ')' ) {
-           break;
-       }
-    }
+    cmt = get_text(start, linelength);
 
-    if ( q < stop && p < stop ) {
-       p++;
-       c = *q;
-       *q = '\0';
-       in->origin = guess_creator ( p );
+    if ( cmt ) {
+       in->origin = guess_creator ( cmt );
+       free(cmt);
        lp_origin(in->origin);
-       *q = c;
     }
 
     in->pf_state |= PF_TRANSLATE;
@@ -187,14 +189,13 @@ int ch_creator( in, out )
     return( CH_DONE );
 }
 
-int ch_endcomm( in, out )
-    struct papfile     *in, *out _U_;
+int ch_endcomm( struct papfile *in, struct papfile *out _U_)
 {
     char                *start;
     int                 linelength, crlflength;
 
 #ifdef DEBUG
-    LOG(log_debug, logtype_papd, "End Comment");
+    LOG(log_debug9, logtype_papd, "End Comment");
 #endif
     in->pf_state |= PF_STW;
 
@@ -204,6 +205,9 @@ int ch_endcomm( in, out )
 
     case -1 :
        return( CH_MORE );
+
+    case -2 :
+        return( CH_ERROR );
     }
 
     in->pf_state |= PF_TRANSLATE;
@@ -214,14 +218,13 @@ int ch_endcomm( in, out )
     return ( CH_DONE);
 }
 
-int ch_starttranslate(in,out)
-    struct papfile      *in, *out _U_;
+int ch_starttranslate( struct papfile *in, struct papfile *out _U_)
 {
     char                *start;
     int                 linelength, crlflength;
 
 #ifdef DEBUG
-    LOG(log_debug, logtype_papd, "Start translate");
+    LOG(log_debug9, logtype_papd, "Start translate");
 #endif
 
     switch ( markline( in, &start, &linelength, &crlflength )) {
@@ -230,6 +233,9 @@ int ch_starttranslate(in,out)
 
     case -1 :
         return( CH_MORE );
+
+    case -2 :
+        return( CH_ERROR );
     }
 
     in->pf_state |= PF_TRANSLATE;
@@ -239,14 +245,13 @@ int ch_starttranslate(in,out)
     return ( CH_DONE);
 }
 
-int ch_endtranslate(in,out)
-    struct papfile      *in, *out _U_;
+int ch_endtranslate(struct papfile *in, struct papfile *out _U_)
 {
     char                *start;
     int                 linelength, crlflength;
 
 #ifdef DEBUG
-    LOG(log_debug, logtype_papd, "EndTranslate");
+    LOG(log_debug9, logtype_papd, "EndTranslate");
 #endif
 
     switch ( markline( in, &start, &linelength, &crlflength )) {
@@ -255,6 +260,9 @@ int ch_endtranslate(in,out)
 
     case -1 :
         return( CH_MORE );
+
+    case -2 :
+        return( CH_ERROR );
     }
 
     lp_write( in, start, linelength + crlflength );
@@ -264,14 +272,13 @@ int ch_endtranslate(in,out)
     return ( CH_DONE);
 }
 
-int ch_translateone(in,out)
-    struct papfile      *in, *out _U_;
+int ch_translateone( struct papfile *in, struct papfile *out _U_)
 {
     char                *start;
     int                 linelength, crlflength;
 
 #ifdef DEBUG
-    LOG(log_debug, logtype_papd, "TranslateOne");
+    LOG(log_debug9, logtype_papd, "TranslateOne");
 #endif
 
     switch ( markline( in, &start, &linelength, &crlflength )) {
@@ -280,6 +287,9 @@ int ch_translateone(in,out)
 
     case -1 :
         return( CH_MORE );
+
+    case -2 :
+        return( CH_ERROR );
     }
 
     in->pf_state |= PF_TRANSLATE;