Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / afsweb / apache_includes / 1.3.1 / ap.h
1 /* ====================================================================
2 * Copyright (c) 1998 The Apache Group. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the Apache Group
19 * for use in the Apache HTTP server project (http://www.apache.org/)."
20 *
21 * 4. The names "Apache Server" and "Apache Group" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * apache@apache.org.
25 *
26 * 5. Products derived from this software may not be called "Apache"
27 * nor may "Apache" appear in their names without prior written
28 * permission of the Apache Group.
29 *
30 * 6. Redistributions of any form whatsoever must retain the following
31 * acknowledgment:
32 * "This product includes software developed by the Apache Group
33 * for use in the Apache HTTP server project (http://www.apache.org/)."
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
48 *
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Group and was originally based
51 * on public domain software written at the National Center for
52 * Supercomputing Applications, University of Illinois, Urbana-Champaign.
53 * For more information on the Apache Group and the Apache HTTP server
54 * project, please see <http://www.apache.org/>.
55 *
56 * The ap_vsnprintf/ap_snprintf functions are based on, and used with the
57 * permission of, the SIO stdio-replacement strx_* functions by Panos
58 * Tsirigotis <panos@alumni.cs.colorado.edu> for xinetd.
59 */
60
61 #ifndef APACHE_AP_H
62 #define APACHE_AP_H
63
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67
68 API_EXPORT(char *) ap_cpystrn(char *, const char *, size_t);
69 int ap_slack(int, int);
70 API_EXPORT(int) ap_snprintf(char *, size_t, const char *, ...);
71 API_EXPORT(int) ap_vsnprintf(char *, size_t, const char *, va_list ap);
72 int ap_execle(const char *, const char *, ...);
73 int ap_execve(const char *, const char *argv[], const char *envp[]);
74
75 /* small utility macros to make things easier to read */
76
77 #ifdef WIN32
78 #define ap_killpg(x, y)
79 #else
80 #ifdef NO_KILLPG
81 #define ap_killpg(x, y) (kill (-(x), (y)))
82 #else
83 #define ap_killpg(x, y) (killpg ((x), (y)))
84 #endif
85 #endif /* WIN32 */
86
87 /* ap_vformatter() is a generic printf-style formatting routine
88 * with some extensions. The extensions are:
89 *
90 * %pA takes a struct in_addr *, and prints it as a.b.c.d
91 * %pI takes a struct sockaddr_in * and prints it as a.b.c.d:port
92 * %pp takes a void * and outputs it in hex
93 *
94 * The %p hacks are to force gcc's printf warning code to skip
95 * over a pointer argument without complaining. This does
96 * mean that the ANSI-style %p (output a void * in hex format) won't
97 * work as expected at all, but that seems to be a fair trade-off
98 * for the increased robustness of having printf-warnings work.
99 *
100 * Additionally, ap_vformatter allows for arbitrary output methods
101 * using the ap_vformatter_buff and flush_func.
102 *
103 * The ap_vformatter_buff has two elements curpos and endpos.
104 * curpos is where ap_vformatter will write the next byte of output.
105 * It proceeds writing output to curpos, and updating curpos, until
106 * either the end of output is reached, or curpos == endpos (i.e. the
107 * buffer is full).
108 *
109 * If the end of output is reached, ap_vformatter returns the
110 * number of bytes written.
111 *
112 * When the buffer is full, the flush_func is called. The flush_func
113 * can return -1 to indicate that no further output should be attempted,
114 * and ap_vformatter will return immediately with -1. Otherwise
115 * the flush_func should flush the buffer in whatever manner is
116 * appropriate, re-initialize curpos and endpos, and return 0.
117 *
118 * Note that flush_func is only invoked as a result of attempting to
119 * write another byte at curpos when curpos >= endpos. So for
120 * example, it's possible when the output exactly matches the buffer
121 * space available that curpos == endpos will be true when
122 * ap_vformatter returns.
123 *
124 * ap_vformatter does not call out to any other code, it is entirely
125 * self-contained. This allows the callers to do things which are
126 * otherwise "unsafe". For example, ap_psprintf uses the "scratch"
127 * space at the unallocated end of a block, and doesn't actually
128 * complete the allocation until ap_vformatter returns. ap_psprintf
129 * would be completely broken if ap_vformatter were to call anything
130 * that used a pool. Similarly http_bprintf() uses the "scratch"
131 * space at the end of its output buffer, and doesn't actually note
132 * that the space is in use until it either has to flush the buffer
133 * or until ap_vformatter returns.
134 */
135
136 typedef struct {
137 char *curpos;
138 char *endpos;
139 } ap_vformatter_buff;
140
141 API_EXPORT(int) ap_vformatter(int (*flush_func) (ap_vformatter_buff *),
142 ap_vformatter_buff *, const char *fmt,
143 va_list ap);
144
145 /* These are snprintf implementations based on ap_vformatter().
146 *
147 * Note that various standards and implementations disagree on the return
148 * value of snprintf, and side-effects due to %n in the formatting string.
149 * ap_snprintf behaves as follows:
150 *
151 * Process the format string until the entire string is exhausted, or
152 * the buffer fills. If the buffer fills then stop processing immediately
153 * (so no further %n arguments are processed), and return the buffer
154 * length. In all cases the buffer is NUL terminated.
155 *
156 * In no event does ap_snprintf return a negative number. It's not possible
157 * to distinguish between an output which was truncated, and an output which
158 * exactly filled the buffer.
159 */
160 API_EXPORT(int) ap_snprintf(char *buf, size_t len, const char *format,
161 ...)
162 __attribute__ ((format(printf, 3, 4)));
163 API_EXPORT(int) ap_vsnprintf(char *buf, size_t len, const char *format,
164 va_list ap);
165
166 #ifdef __cplusplus
167 }
168 #endif
169 #endif /* !APACHE_AP_H */