]> arthur.barton.de Git - netatalk.git/blob - etc/papd/file.c
Big configure.in cleanup
[netatalk.git] / etc / papd / file.c
1 /*
2  * $Id: file.c,v 1.12 2009-10-14 02:24:05 didg Exp $
3  *
4  * Copyright (c) 1990,1994 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 <atalk/logger.h>
13 #include <sys/param.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17
18 #include "file.h"
19
20 /* 
21 */
22 int markline( struct papfile *pf, char **start, int *linelength, int *crlflength )
23 {
24     char                *p;
25
26     if ( pf->pf_datalen == 0 && ( pf->pf_state & PF_EOF )) {
27         return( 0 );
28     }
29
30     *start = pf->pf_data;
31
32     /* get a line */
33     for ( *linelength=0; *linelength < pf->pf_datalen; (*linelength)++) {
34         if (pf->pf_data[*linelength] == '\n' ||
35             pf->pf_data[*linelength] == '\r') {
36             break;
37         }
38     }
39
40     if ( *linelength >= pf->pf_datalen ) {
41         if ( pf->pf_state & PF_EOF ) {
42             append( pf, "\n", 1 );
43         } else if (*linelength < 1024) {
44             return( -1 );
45         }
46     }
47
48     p = pf->pf_data + *linelength;
49
50     *crlflength=0;
51     while(*crlflength < pf->pf_datalen-*linelength && 
52     (p[*crlflength]=='\r' || p[*crlflength]=='\n')) {
53         (*crlflength)++;
54     }
55     
56     if (!*crlflength) {
57         /* line is way too long, something fishy is going on, give up */
58         LOG(log_error, logtype_papd, "markline: no crlf in comment, give up" );
59         return( -2 );
60     }
61
62     /* success, return 1 */
63     return( 1 );
64 }
65
66 void morespace(struct papfile *pf, const char *data, int len)
67 {
68     char                *nbuf;
69     int                 nsize;
70
71     if ( pf->pf_data != pf->pf_buf ) {                  /* pull up */
72         bcopy( pf->pf_data, pf->pf_buf, pf->pf_datalen);
73         pf->pf_data = pf->pf_buf;
74     }
75
76     if ( pf->pf_datalen + len > pf->pf_bufsize ) {      /* make more space */
77         nsize = (( pf->pf_bufsize + len ) / PF_MORESPACE +
78                 (( pf->pf_bufsize + len ) % PF_MORESPACE != 0 )) * PF_MORESPACE;
79         if ( pf->pf_buf ) {
80             if (( nbuf = (char *)realloc( pf->pf_buf, nsize )) == NULL ) {
81                 exit( 1 );
82             }
83         } else {
84             if (( nbuf = (char *)malloc( nsize )) == NULL ) {
85                 exit( 1 );
86             }
87         }
88         pf->pf_bufsize = nsize;
89         pf->pf_data = nbuf + ( pf->pf_data - pf->pf_buf );
90         pf->pf_buf = nbuf;
91     }
92
93     bcopy( data, pf->pf_data + pf->pf_datalen, len );
94     pf->pf_datalen += len;
95 }
96
97
98 void append(struct papfile *pf, const char *data, int len)
99 {
100     if ((pf->pf_data + pf->pf_datalen + len) >
101         (pf->pf_buf + pf->pf_bufsize)) {
102                 morespace(pf, data, len);
103     } else {
104         memcpy(pf->pf_data + pf->pf_datalen, data, len);
105         pf->pf_datalen += len;
106     }
107 }
108
109
110 void spoolerror(struct papfile *out, char *str)
111 {
112     char        *pserr1 = "%%[ Error: ";
113     char        *pserr2 = " ]%%\n";
114
115     if ( str == NULL ) {
116         str = "Spooler error.";
117     }
118
119     append( out, pserr1, strlen( pserr1 ));
120     append( out, str, strlen( str ));
121     append( out, pserr2, strlen( pserr2 ));
122 }