summaryrefslogtreecommitdiff
path: root/doc/gnulib.texi
blob: 23638c774699b376b33638c71d73b5c7d5337426 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
\input texinfo   @c -*-texinfo-*-
@comment $Id: gnulib.texi,v 1.26 2006-07-19 21:48:14 dprice Exp $
@comment %**start of header
@setfilename gnulib.info
@settitle GNU Gnulib
@syncodeindex fn cp
@syncodeindex pg cp
@comment %**end of header

@set UPDATED $Date: 2006-07-19 21:48:14 $

@copying
This manual is for GNU Gnulib (updated @value{UPDATED}),
which is a library of common routines intended to be shared at the
source level.

Copyright @copyright{} 2004, 2005, 2006 Free Software Foundation, Inc.

@quotation
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.1 or
any later version published by the Free Software Foundation; with no
Invariant Sections, with the Front-Cover Texts being ``A GNU Manual,''
and with the Back-Cover Texts as in (a) below.  A copy of the
license is included in the section entitled ``GNU Free Documentation
License.''

(a) The FSF's Back-Cover Text is: ``You have freedom to copy and modify
this GNU Manual, like GNU software.  Copies published by the Free
Software Foundation raise funds for GNU development.''
@end quotation
@end copying

@dircategory Software development
@direntry
* Gnulib: (gnulib).             Source files to share among distributions.
@end direntry

@titlepage
@title GNU Gnulib
@subtitle updated @value{UPDATED}
@author @email{bug-gnulib@@gnu.org}
@page
@vskip 0pt plus 1filll
@insertcopying
@end titlepage

@contents

@ifnottex
@node Top
@top GNU Gnulib

@insertcopying
@end ifnottex

@menu
* Gnulib::
* Invoking gnulib-tool::
* Copying This Manual::
* Index::
@end menu


@node Gnulib
@chapter Gnulib

This manual contains some bare-bones documentation, but not much more.
It's mostly been a place to store notes until someone (you?)@ gets
around to writing a coherent manual.

Getting started:

@itemize
@item Gnulib is hosted at Savannah:
      @url{http://savannah.gnu.org/projects/gnulib}.  Get the sources
      through CVS from there.
@item The Gnulib home page:
      @url{http://www.gnu.org/software/gnulib/}.
@end itemize

@menu
* Comments::
* Header files::
* Quoting::
* ctime::
* inet_ntoa::
* gcd::
* Out of memory handling::
* Library version handling::
* Regular expressions::
* Windows sockets::
* Libtool and Windows::
@end menu


@node Comments
@section Comments

@cindex comments describing functions
@cindex describing functions, locating
Where to put comments describing functions: Because of risk of
divergence, we prefer to keep most function describing comments in
only one place: just above the actual function definition.  Some
people prefer to put that documentation in the .h file.  In any case,
it should appear in just one place unless you can ensure that the
multiple copies will always remain identical.


@node Header files
@section Header files

@cindex double inclusion of header files
@cindex header file include protection
It is a tradition to use CPP tricks to avoid parsing the same header
file more than once, which might cause warnings.  The trick is to wrap
the content of the header file (say, @file{foo.h}) in a block, as in:

@example
#ifndef FOO_H
# define FOO_H
...
body of header file goes here
...
#endif /* FOO_H */
@end example

Whether to use @code{FOO_H} or @code{_FOO_H} is a matter of taste and
style.  The C89 and C99 standards reserve all identifiers that begin with an
underscore and either an uppercase letter or another underscore, for
any use.  Thus, in theory, an application might not safely assume that
@code{_FOO_H} has not already been defined by a library.  On the other
hand, using @code{FOO_H} will likely lead the higher risk of
collisions with other symbols (e.g., @code{KEY_H}, @code{XK_H}, @code{BPF_H},
which are CPP macro constants, or @code{COFF_LONG_H}, which is a CPP
macro function).  Your preference may depend on whether you consider
the header file under discussion as part of the application (which has
its own namespace for CPP symbols) or a supporting library (that
shouldn't interfere with the application's CPP symbol namespace).

@cindex C++ header files
@cindex Header files and C++
Adapting C header files for use in C++ applications can use another
CPP trick, as in:

@example
# ifdef __cplusplus
extern "C"
@{
# endif
...
body of header file goes here
...
# ifdef __cplusplus
@}
# endif
@end example

The idea here is that @code{__cplusplus} is defined only by C++
implementations, which will wrap the header file in an @samp{extern "C"}
block.  Again, whether to use this trick is a matter of taste and
style.  While the above can be seen as harmless, it could be argued
that the header file is written in C, and any C++ application using it
should explicitly use the @samp{extern "C"} block itself.  Your
preference might depend on whether you consider the API exported by
your header file as something available for C programs only, or for C
and C++ programs alike.

@subheading Include ordering

When writing a gnulib module, or even in general, a good way to order
the @samp{#include} directives is the following.

@itemize
@item First comes the #include "..." specifying the module being implemented.
@item Then come all the #include <...> of system or system-replacement headers,
in arbitrary order.
@item Then come all the #include "..." of gnulib and private headers, in
arbitrary order.
@end itemize


@include quote.texi

@include ctime.texi

@include gcd.texi

@include inet_ntoa.texi


@node Out of memory handling
@section Out of memory handling

@cindex Out of Memory handling
@cindex Memory allocation failure
The GSS API does not have a standard error code for the out of memory
error condition.  Instead of adding a non-standard error code, this
library has chosen to adopt a different strategy.  Out of memory
handling happens in rare situations, but performing the out of memory
error handling after almost all API function invocations pollute your
source code and might make it harder to spot more serious problems.
The strategy chosen improve code readability and robustness.

@cindex Aborting execution
For most applications, aborting the application with an error message
when the out of memory situation occur is the best that can be wished
for.  This is how the library behaves by default.

@vindex xalloc_fail_func
However, we realize that some applications may not want to have the
GSS library abort execution in any situation.  The GSS library support
a hook to let the application regain control and perform its own
cleanups when an out of memory situation has occurred.  The application
can define a function (having a @code{void} prototype, i.e., no return
value and no parameters) and set the library variable
@code{xalloc_fail_func} to that function.  The variable should be
declared as follows.

@example
extern void (*xalloc_fail_func) (void);
@end example

The GSS library will invoke this function if an out of memory error
occurs.  Note that after this the GSS library is in an undefined
state, so you must unload or restart the application to continue call
GSS library functions.  The hook is only intended to allow the
application to log the situation in a special way.  Of course, care
must be taken to not allocate more memory, as that will likely also
fail.


@node Library version handling
@section Library version handling

The module @samp{check-version} can be useful when your gnulib
application is a system library.  You will typically wrap the call to
the @code{check_version} function through a library API, your library
header file may contain:

@example
#define STRINGPREP_VERSION "0.5.18"
...
  extern const char *stringprep_check_version (const char *req_version);
@end example

To avoid ELF symbol collisions with other libraries that use the
@samp{check-version} module, add to @file{config.h} through a
AC_DEFINE something like:

@example
AC_DEFINE(check_version, stringprep_check_version,
          [Rename check_version.])
@end example

The @code{stringprep_check_version} function will thus be implemented
by the @code{check_version} module.

There are two uses of the interface.  The first is a way to provide
for applications to find out the version number of the library it
uses.  The application may contain diagnostic code such as:

@example
  printf ("Stringprep version: header %s library %s",
          STRINGPREP_VERSION,
          stringprep_check_version (NULL));
@end example

Separating the library and header file version can be useful when
searching for version mismatch related problems.

The second uses is as a rudimentary test of proper library version, by
making sure the application get a library version that is the same, or
newer, than the header file used when building the application.  This
doesn't catch all problems, libraries may change backwards incompatibly
in later versions, but enable applications to require a certain
minimum version before it may proceed.

Typical uses look like:

@example
       /* Check version of libgcrypt. */
       if (!gcry_check_version (GCRYPT_VERSION))
         die ("version mismatch\n");
@end example


@node Regular expressions
@section Regular expressions

Gnulib supports many different types of regular expressions; although
the underlying features are the same or identical, the syntax used
varies.  The descriptions given here for the different types are
generated automatically.

@include regexprops-generic.texi


@node Windows sockets
@section Windows sockets

There are several issues when building applications that should work
under Windows.  The most problematic part is for applications that use
sockets.

Hopefully, we can add helpful notes to this section that will help you
port your application to Windows using gnulib.

@subsection Getaddrinfo and WINVER

This was written for the getaddrinfo module, but may be applicable to
other functions too.

The getaddrinfo function exists in ws2tcpip.h and -lws2_32 on Windows
XP.  The function declaration is present if @code{WINVER >= 0x0501}.
Windows 2000 does not have getaddrinfo in its @file{WS2_32.dll}.

Thus, if you want to assume Windows XP or later, you can add
AC_DEFINE(WINVER, 0x0501) to avoid compiling to (partial) getaddrinfo
implementation.

If you want to support Windows 2000, don't do anything, but be aware
that gnulib will use its own (partial) getaddrinfo implementation even
on Windows XP.  Currently the code does not attempt to determine if
the getaddrinfo function is available during runtime.

Todo: Make getaddrinfo.c open the WS2_32.DLL and check for the
getaddrinfo symbol and use it if present, otherwise fall back to our
own implementation.

@node Libtool and Windows
@section Libtool and Windows

If you want it to be possible to cross-compile your program to MinGW
and you use Libtool, you need to put:

@example
AC_LIBTOOL_WIN32_DLL
@end example

in your @file{configure.ac}.  This sets the correct names for the
@code{OBJDUMP}, @code{DLLTOOL}, and @code{AS} tools for the build.

If you are building a library, you will also need to pass
@code{-no-undefined} to make sure Libtool produces a DLL for your
library.  From a @file{Makefile.am}:

@example
libgsasl_la_LDFLAGS += -no-undefined
@end example

@include gnulib-tool.texi


@node Copying This Manual
@appendix Copying This Manual

@menu
* GNU Free Documentation License::  License for copying this manual.
@end menu

@include fdl.texi


@node Index
@unnumbered Index

@printindex cp

@bye