Fix getaddrinfo on Windows 2000.
[gnulib.git] / doc / gnulib.texi
1 \input texinfo   @c -*-texinfo-*-
2 @comment $Id: gnulib.texi,v 1.23 2006-06-21 17:22:32 jas Exp $
3 @comment %**start of header
4 @setfilename gnulib.info
5 @settitle GNU Gnulib
6 @syncodeindex fn cp
7 @syncodeindex pg cp
8 @comment %**end of header
9
10 @set UPDATED $Date: 2006-06-21 17:22:32 $
11
12 @copying
13 This manual is for GNU Gnulib (updated @value{UPDATED}),
14 which is a library of common routines intended to be shared at the
15 source level.
16
17 Copyright @copyright{} 2004, 2005, 2006 Free Software Foundation, Inc.
18
19 @quotation
20 Permission is granted to copy, distribute and/or modify this document
21 under the terms of the GNU Free Documentation License, Version 1.1 or
22 any later version published by the Free Software Foundation; with no
23 Invariant Sections, with the Front-Cover Texts being ``A GNU Manual,''
24 and with the Back-Cover Texts as in (a) below.  A copy of the
25 license is included in the section entitled ``GNU Free Documentation
26 License.''
27
28 (a) The FSF's Back-Cover Text is: ``You have freedom to copy and modify
29 this GNU Manual, like GNU software.  Copies published by the Free
30 Software Foundation raise funds for GNU development.''
31 @end quotation
32 @end copying
33
34 @dircategory Software development
35 @direntry
36 * Gnulib: (gnulib).             Source files to share among distributions.
37 @end direntry
38
39 @titlepage
40 @title GNU Gnulib
41 @subtitle updated @value{UPDATED}
42 @author @email{bug-gnulib@@gnu.org}
43 @page
44 @vskip 0pt plus 1filll
45 @insertcopying
46 @end titlepage
47
48 @contents
49
50 @ifnottex
51 @node Top
52 @top GNU Gnulib
53
54 @insertcopying
55 @end ifnottex
56
57 @menu
58 * Gnulib::
59 * Invoking gnulib-tool::
60 * Copying This Manual::
61 * Index::
62 @end menu
63
64
65 @node Gnulib
66 @chapter Gnulib
67
68 This manual contains some bare-bones documentation, but not much more.
69 It's mostly been a place to store notes until someone (you?)@ gets
70 around to writing a coherent manual.
71
72 Getting started:
73
74 @itemize
75 @item Gnulib is hosted at Savannah:
76       @url{http://savannah.gnu.org/projects/gnulib}.  Get the sources
77       through CVS from there.
78 @item The Gnulib home page:
79       @url{http://www.gnu.org/software/gnulib/}.
80 @end itemize
81
82 @menu
83 * Comments::
84 * Header files::
85 * Quoting::
86 * ctime::
87 * inet_ntoa::
88 * gcd::
89 * Out of memory handling::
90 * Library version handling::
91 * Regular expressions::
92 * Windows sockets::
93 @end menu
94
95
96 @node Comments
97 @section Comments
98
99 @cindex comments describing functions
100 @cindex describing functions, locating
101 Where to put comments describing functions: Because of risk of
102 divergence, we prefer to keep most function describing comments in
103 only one place: just above the actual function definition.  Some
104 people prefer to put that documentation in the .h file.  In any case,
105 it should appear in just one place unless you can ensure that the
106 multiple copies will always remain identical.
107
108
109 @node Header files
110 @section Header files
111
112 @cindex double inclusion of header files
113 @cindex header file include protection
114 It is a tradition to use CPP tricks to avoid parsing the same header
115 file more than once, which might cause warnings.  The trick is to wrap
116 the content of the header file (say, @file{foo.h}) in a block, as in:
117
118 @example
119 #ifndef FOO_H
120 # define FOO_H
121 ...
122 body of header file goes here
123 ...
124 #endif /* FOO_H */
125 @end example
126
127 Whether to use @code{FOO_H} or @code{_FOO_H} is a matter of taste and
128 style.  The C89 and C99 standards reserve all identifiers that begin with an
129 underscore and either an uppercase letter or another underscore, for
130 any use.  Thus, in theory, an application might not safely assume that
131 @code{_FOO_H} has not already been defined by a library.  On the other
132 hand, using @code{FOO_H} will likely lead the higher risk of
133 collisions with other symbols (e.g., @code{KEY_H}, @code{XK_H}, @code{BPF_H},
134 which are CPP macro constants, or @code{COFF_LONG_H}, which is a CPP
135 macro function).  Your preference may depend on whether you consider
136 the header file under discussion as part of the application (which has
137 its own namespace for CPP symbols) or a supporting library (that
138 shouldn't interfere with the application's CPP symbol namespace).
139
140 @cindex C++ header files
141 @cindex Header files and C++
142 Adapting C header files for use in C++ applications can use another
143 CPP trick, as in:
144
145 @example
146 # ifdef __cplusplus
147 extern "C"
148 @{
149 # endif
150 ...
151 body of header file goes here
152 ...
153 # ifdef __cplusplus
154 @}
155 # endif
156 @end example
157
158 The idea here is that @code{__cplusplus} is defined only by C++
159 implementations, which will wrap the header file in an @samp{extern "C"}
160 block.  Again, whether to use this trick is a matter of taste and
161 style.  While the above can be seen as harmless, it could be argued
162 that the header file is written in C, and any C++ application using it
163 should explicitly use the @samp{extern "C"} block itself.  Your
164 preference might depend on whether you consider the API exported by
165 your header file as something available for C programs only, or for C
166 and C++ programs alike.
167
168 @subheading Include ordering
169
170 When writing a gnulib module, or even in general, a good way to order
171 the @samp{#include} directives is the following.
172
173 @itemize
174 @item First comes the #include "..." specifying the module being implemented.
175 @item Then come all the #include <...> of system or system-replacement headers,
176 in arbitrary order.
177 @item Then come all the #include "..." of gnulib and private headers, in
178 arbitrary order.
179 @end itemize
180
181
182 @include quote.texi
183
184 @include ctime.texi
185
186 @include gcd.texi
187
188 @include inet_ntoa.texi
189
190
191 @node Out of memory handling
192 @section Out of memory handling
193
194 @cindex Out of Memory handling
195 @cindex Memory allocation failure
196 The GSS API does not have a standard error code for the out of memory
197 error condition.  Instead of adding a non-standard error code, this
198 library has chosen to adopt a different strategy.  Out of memory
199 handling happens in rare situations, but performing the out of memory
200 error handling after almost all API function invocations pollute your
201 source code and might make it harder to spot more serious problems.
202 The strategy chosen improve code readability and robustness.
203
204 @cindex Aborting execution
205 For most applications, aborting the application with an error message
206 when the out of memory situation occur is the best that can be wished
207 for.  This is how the library behaves by default.
208
209 @vindex xalloc_fail_func
210 However, we realize that some applications may not want to have the
211 GSS library abort execution in any situation.  The GSS library support
212 a hook to let the application regain control and perform its own
213 cleanups when an out of memory situation has occured.  The application
214 can define a function (having a @code{void} prototype, i.e., no return
215 value and no parameters) and set the library variable
216 @code{xalloc_fail_func} to that function.  The variable should be
217 declared as follows.
218
219 @example
220 extern void (*xalloc_fail_func) (void);
221 @end example
222
223 The GSS library will invoke this function if an out of memory error
224 occurs.  Note that after this the GSS library is in an undefined
225 state, so you must unload or restart the application to continue call
226 GSS library functions.  The hook is only intended to allow the
227 application to log the situation in a special way.  Of course, care
228 must be taken to not allocate more memory, as that will likely also
229 fail.
230
231
232 @node Library version handling
233 @section Library version handling
234
235 The module @samp{check-version} can be useful when your gnulib
236 application is a system library.  You will typically wrap the call to
237 the @code{check_version} function through a library API, your library
238 header file may contain:
239
240 @example
241 #define STRINGPREP_VERSION "0.5.18"
242 ...
243   extern const char *stringprep_check_version (const char *req_version);
244 @end example
245
246 To avoid ELF symbol collisions with other libraries that use the
247 @samp{check-version} module, add to @file{config.h} through a
248 AC_DEFINE something like:
249
250 @example
251 AC_DEFINE(check_version, stringprep_check_version,
252           [Rename check_version.])
253 @end example
254
255 The @code{stringprep_check_version} function will thus be implemented
256 by the @code{check_version} module.
257
258 There are two uses of the interface.  The first is a way to provide
259 for applications to find out the version number of the library it
260 uses.  The application may contain diagnostic code such as:
261
262 @example
263   printf ("Stringprep version: header %s library %s",
264           STRINGPREP_VERSION,
265           stringprep_check_version (NULL));
266 @end example
267
268 Separating the library and header file version can be useful when
269 searching for version mismatch related problems.
270
271 The second uses is as a rudimentary test of proper library version, by
272 making sure the application get a library version that is the same, or
273 newer, than the header file used when building the application.  This
274 doesn't catch all problems, libraries may change backwards incompatibly
275 in later versions, but enable applications to require a certain
276 minimum version before it may proceed.
277
278 Typical uses look like:
279
280 @example
281        /* Check version of libgcrypt. */
282        if (!gcry_check_version (GCRYPT_VERSION))
283          die ("version mismatch\n");
284 @end example
285
286
287 @node Regular expressions
288 @section Regular expressions
289
290 Gnulib supports many different types of regular expressions; although
291 the underlying features are the same or identical, the syntax used
292 varies.  The descriptions given here for the different types are
293 generated automatically.
294
295 @include regexprops-generic.texi
296
297
298 @node Windows sockets
299 @section Windows sockets
300
301 There are several issues when building applications that should work
302 under Windows.  The most problematic part is for applications that use
303 sockets.
304
305 Hopefully, we can add helpful notes to this section that will help you
306 port your application to Windows using gnulib.
307
308 @subsection Getaddrinfo and WINVER
309
310 This was written for the getaddrinfo module, but may be applicable to
311 other functions too.
312
313 The getaddrinfo function exists in ws2tcpip.h and -lws2_32 on Windows
314 XP.  The function declaration is present if @code{WINVER >= 0x0501}.
315 Windows 2000 does not have getaddrinfo in its @file{WS2_32.dll}.
316
317 Thus, if you want to assume Windows XP or later, you can add
318 AC_DEFINE(WINVER, 0x0501) to avoid compiling to (partial) getaddrinfo
319 implementation.
320
321 If you want to support Windows 2000, don't do anything, but be aware
322 that gnulib will use its own (partial) getaddrinfo implementation even
323 on Windows XP.  Currently the code does not attempt to determine if
324 the getaddrinfo function is available during runtime.
325
326 Todo: Make getaddrinfo.c open the WS2_32.DLL and check for the
327 getaddrinfo symbol and use it if present, otherwise fall back to our
328 own implementation.
329
330 @include gnulib-tool.texi
331
332
333 @node Copying This Manual
334 @appendix Copying This Manual
335
336 @menu
337 * GNU Free Documentation License::  License for copying this manual.
338 @end menu
339
340 @include fdl.texi
341
342
343 @node Index
344 @unnumbered Index
345
346 @printindex cp
347
348 @bye