Mercurial > hg > xemacs-beta
comparison man/standards.texi @ 462:0784d089fdc9 r21-2-46
Import from CVS: tag r21-2-46
author | cvs |
---|---|
date | Mon, 13 Aug 2007 11:44:37 +0200 |
parents | 3ecd8885ac67 |
children | bac3173b2665 |
comparison
equal
deleted
inserted
replaced
461:120ed4009e51 | 462:0784d089fdc9 |
---|---|
1 \input texinfo @c -*-texinfo-*- | 1 \input texinfo @c -*-texinfo-*- |
2 @c %**start of header | 2 @c %**start of header |
3 @setfilename ../info/standards.info | 3 @setfilename ../info/standards.info |
4 @settitle GNU Coding Standards | 4 @settitle GNU Coding Standards |
5 @c This date is automagically updated when you save this file: | 5 @c This date is automagically updated when you save this file: |
6 @set lastupdate June 24, 1999 | 6 @set lastupdate February 21, 2001 |
7 @c %**end of header | 7 @c %**end of header |
8 | 8 |
9 @ifinfo | 9 @ifinfo |
10 @format | 10 @format |
11 START-INFO-DIR-ENTRY | 11 START-INFO-DIR-ENTRY |
14 @end format | 14 @end format |
15 @end ifinfo | 15 @end ifinfo |
16 | 16 |
17 @c @setchapternewpage odd | 17 @c @setchapternewpage odd |
18 @setchapternewpage off | 18 @setchapternewpage off |
19 | |
20 @c Put everything in one index (arbitrarily chosen to be the concept index). | |
21 @syncodeindex fn cp | |
22 @syncodeindex ky cp | |
23 @syncodeindex pg cp | |
24 @syncodeindex vr cp | |
19 | 25 |
20 @c This is used by a cross ref in make-stds.texi | 26 @c This is used by a cross ref in make-stds.texi |
21 @set CODESTD 1 | 27 @set CODESTD 1 |
22 @iftex | 28 @iftex |
23 @set CHAPTER chapter | 29 @set CHAPTER chapter |
26 @set CHAPTER node | 32 @set CHAPTER node |
27 @end ifinfo | 33 @end ifinfo |
28 | 34 |
29 @ifinfo | 35 @ifinfo |
30 GNU Coding Standards | 36 GNU Coding Standards |
31 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc. | 37 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc. |
32 | 38 |
33 Permission is granted to make and distribute verbatim copies of | 39 Permission is granted to make and distribute verbatim copies of |
34 this manual provided the copyright notice and this permission notice | 40 this manual provided the copyright notice and this permission notice |
35 are preserved on all copies. | 41 are preserved on all copies. |
36 | 42 |
57 @author Richard Stallman | 63 @author Richard Stallman |
58 @author last updated @value{lastupdate} | 64 @author last updated @value{lastupdate} |
59 @page | 65 @page |
60 | 66 |
61 @vskip 0pt plus 1filll | 67 @vskip 0pt plus 1filll |
62 Copyright @copyright{} 1992, 1993, 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc. | 68 Copyright @copyright{} 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc. |
63 | 69 |
64 Permission is granted to make and distribute verbatim copies of | 70 Permission is granted to make and distribute verbatim copies of |
65 this manual provided the copyright notice and this permission notice | 71 this manual provided the copyright notice and this permission notice |
66 are preserved on all copies. | 72 are preserved on all copies. |
67 | 73 |
90 * Program Behavior:: Program Behavior for All Programs | 96 * Program Behavior:: Program Behavior for All Programs |
91 * Writing C:: Making The Best Use of C | 97 * Writing C:: Making The Best Use of C |
92 * Documentation:: Documenting Programs | 98 * Documentation:: Documenting Programs |
93 * Managing Releases:: The Release Process | 99 * Managing Releases:: The Release Process |
94 * References:: References to Non-Free Software or Documentation | 100 * References:: References to Non-Free Software or Documentation |
101 * Index:: | |
95 @end menu | 102 @end menu |
96 | 103 |
97 @node Preface | 104 @node Preface |
98 @chapter About the GNU Coding Standards | 105 @chapter About the GNU Coding Standards |
99 | 106 |
104 programs written in C, but many of the rules and principles are useful | 111 programs written in C, but many of the rules and principles are useful |
105 even if you write in another programming language. The rules often | 112 even if you write in another programming language. The rules often |
106 state reasons for writing in a certain way. | 113 state reasons for writing in a certain way. |
107 | 114 |
108 Corrections or suggestions for this document should be sent to | 115 Corrections or suggestions for this document should be sent to |
109 @email{gnu@@gnu.org}. If you make a suggestion, please include a | 116 @email{bug-standards@@gnu.org}. If you make a suggestion, please include a |
110 suggested new wording for it; our time is limited. We prefer a context | 117 suggested new wording for it; our time is limited. We prefer a context |
111 diff to the @file{standards.texi} or @file{make-stds.texi} files, but if | 118 diff to the @file{standards.texi} or @file{make-stds.texi} files, but if |
112 you don't have those files, please mail your suggestion anyway. | 119 you don't have those files, please mail your suggestion anyway. |
113 | 120 |
114 This release of the GNU Coding Standards was last updated | 121 This release of the GNU Coding Standards was last updated |
115 @value{lastupdate}. | 122 @value{lastupdate}. |
116 | 123 |
124 @cindex where to obtain @code{standards.texi} | |
125 @cindex downloading this manual | |
126 If you did not obtain this file directly from the GNU project and | |
127 recently, please check for a newer version. You can ftp the GNU Coding | |
128 Standards from any GNU FTP host in the directory | |
129 @file{/pub/gnu/standards/}. The GNU Coding Standards are available | |
130 there in several different formats: @file{standards.text}, | |
131 @file{standards.texi}, @file{standards.info}, and @file{standards.dvi}. | |
132 The GNU Coding Standards are also available on the GNU World Wide Web | |
133 server: @uref{http://www.gnu.org/prep/standards_toc.html}. | |
134 | |
117 @node Legal Issues | 135 @node Legal Issues |
118 @chapter Keeping Free Software Free | 136 @chapter Keeping Free Software Free |
137 @cindex legal aspects | |
119 | 138 |
120 This @value{CHAPTER} discusses how you can make sure that GNU software | 139 This @value{CHAPTER} discusses how you can make sure that GNU software |
121 remains unencumbered. | 140 avoids legal difficulties, and other related issues. |
122 | 141 |
123 @menu | 142 @menu |
124 * Reading Non-Free Code:: Referring to Proprietary Programs | 143 * Reading Non-Free Code:: Referring to Proprietary Programs |
125 * Contributions:: Accepting Contributions | 144 * Contributions:: Accepting Contributions |
145 * Trademarks:: How We Deal with Trademark Issues | |
126 @end menu | 146 @end menu |
127 | 147 |
128 @node Reading Non-Free Code | 148 @node Reading Non-Free Code |
129 @section Referring to Proprietary Programs | 149 @section Referring to Proprietary Programs |
150 @cindex proprietary programs | |
151 @cindex avoiding proprietary code | |
130 | 152 |
131 Don't in any circumstances refer to Unix source code for or during | 153 Don't in any circumstances refer to Unix source code for or during |
132 your work on GNU! (Or to any other proprietary programs.) | 154 your work on GNU! (Or to any other proprietary programs.) |
133 | 155 |
134 If you have a vague recollection of the internals of a Unix program, | 156 If you have a vague recollection of the internals of a Unix program, |
156 | 178 |
157 Or turn some parts of the program into independently usable libraries. | 179 Or turn some parts of the program into independently usable libraries. |
158 Or use a simple garbage collector instead of tracking precisely when | 180 Or use a simple garbage collector instead of tracking precisely when |
159 to free memory, or use a new GNU facility such as obstacks. | 181 to free memory, or use a new GNU facility such as obstacks. |
160 | 182 |
161 | |
162 @node Contributions | 183 @node Contributions |
163 @section Accepting Contributions | 184 @section Accepting Contributions |
185 @cindex legal papers | |
186 @cindex accepting contributions | |
164 | 187 |
165 If the program you are working on is copyrighted by the Free Software | 188 If the program you are working on is copyrighted by the Free Software |
166 Foundation, then when someone else sends you a piece of code to add to | 189 Foundation, then when someone else sends you a piece of code to add to |
167 the program, we need legal papers to use it---just as we asked you to | 190 the program, we need legal papers to use it---just as we asked you to |
168 sign papers initially. @emph{Each} person who makes a nontrivial | 191 sign papers initially. @emph{Each} person who makes a nontrivial |
201 | 224 |
202 We have more detailed advice for maintainers of programs; if you have | 225 We have more detailed advice for maintainers of programs; if you have |
203 reached the stage of actually maintaining a program for GNU (whether | 226 reached the stage of actually maintaining a program for GNU (whether |
204 released or not), please ask us for a copy. | 227 released or not), please ask us for a copy. |
205 | 228 |
229 @node Trademarks | |
230 @section Trademarks | |
231 @cindex trademarks | |
232 | |
233 Please do not include any trademark acknowledgements in GNU software | |
234 packages or documentation. | |
235 | |
236 Trademark acknowledgements are the statements that such-and-such is a | |
237 trademark of so-and-so. The GNU Project has no objection to the basic | |
238 idea of trademarks, but these acknowledgements feel like kowtowing, so | |
239 we don't use them. There is no legal requirement for them. | |
240 | |
241 What is legally required, as regards other people's trademarks, is to | |
242 avoid using them in ways which a reader might read as naming or labeling | |
243 our own programs or activities. For example, since ``Objective C'' is | |
244 (or at least was) a trademark, we made sure to say that we provide a | |
245 ``compiler for the Objective C language'' rather than an ``Objective C | |
246 compiler''. The latter is meant to be short for the former, but it does | |
247 not explicitly state the relationship, so it could be misinterpreted as | |
248 using ``Objective C'' as a label for the compiler rather than for the | |
249 language. | |
250 | |
206 @node Design Advice | 251 @node Design Advice |
207 @chapter General Program Design | 252 @chapter General Program Design |
253 @cindex program design | |
208 | 254 |
209 This @value{CHAPTER} discusses some of the issues you should take into | 255 This @value{CHAPTER} discusses some of the issues you should take into |
210 account when designing your program. | 256 account when designing your program. |
211 | 257 |
258 @c Standard or ANSI C | |
259 @c | |
260 @c In 1989 the American National Standards Institute (ANSI) standardized | |
261 @c C as standard X3.159-1989. In December of that year the | |
262 @c International Standards Organization ISO adopted the ANSI C standard | |
263 @c making minor changes. In 1990 ANSI then re-adopted ISO standard | |
264 @c C. This version of C is known as either ANSI C or Standard C. | |
265 | |
266 @c A major revision of the C Standard appeared in 1999. | |
267 | |
212 @menu | 268 @menu |
269 * Source Language:: Which languges to use. | |
213 * Compatibility:: Compatibility with other implementations | 270 * Compatibility:: Compatibility with other implementations |
214 * Using Extensions:: Using non-standard features | 271 * Using Extensions:: Using non-standard features |
215 * ANSI C:: Using ANSI C features | 272 * Standard C:: Using Standard C features |
216 * Source Language:: Using languages other than C | |
217 @end menu | 273 @end menu |
274 | |
275 @node Source Language | |
276 @section Which Languages to Use | |
277 @cindex programming languges | |
278 | |
279 When you want to use a language that gets compiled and runs at high | |
280 speed, the best language to use is C. Using another language is like | |
281 using a non-standard feature: it will cause trouble for users. Even if | |
282 GCC supports the other language, users may find it inconvenient to have | |
283 to install the compiler for that other language in order to build your | |
284 program. For example, if you write your program in C++, people will | |
285 have to install the GNU C++ compiler in order to compile your program. | |
286 | |
287 C has one other advantage over C++ and other compiled languages: more | |
288 people know C, so more people will find it easy to read and modify the | |
289 program if it is written in C. | |
290 | |
291 So in general it is much better to use C, rather than the | |
292 comparable alternatives. | |
293 | |
294 But there are two exceptions to that conclusion: | |
295 | |
296 @itemize @bullet | |
297 @item | |
298 It is no problem to use another language to write a tool specifically | |
299 intended for use with that language. That is because the only people | |
300 who want to build the tool will be those who have installed the other | |
301 language anyway. | |
302 | |
303 @item | |
304 If an application is of interest only to a narrow part of the community, | |
305 then the question of which language it is written in has less effect on | |
306 other people, so you may as well please yourself. | |
307 @end itemize | |
308 | |
309 Many programs are designed to be extensible: they include an interpreter | |
310 for a language that is higher level than C. Often much of the program | |
311 is written in that language, too. The Emacs editor pioneered this | |
312 technique. | |
313 | |
314 @cindex GUILE | |
315 The standard extensibility interpreter for GNU software is GUILE, which | |
316 implements the language Scheme (an especially clean and simple dialect | |
317 of Lisp). @uref{http://www.gnu.org/software/guile/}. We don't reject | |
318 programs written in other ``scripting languages'' such as Perl and | |
319 Python, but using GUILE is very important for the overall consistency of | |
320 the GNU system. | |
218 | 321 |
219 @node Compatibility | 322 @node Compatibility |
220 @section Compatibility with Other Implementations | 323 @section Compatibility with Other Implementations |
324 @cindex compatibility with C and @sc{posix} standards | |
325 @cindex @sc{posix} compatibility | |
221 | 326 |
222 With occasional exceptions, utility programs and libraries for GNU | 327 With occasional exceptions, utility programs and libraries for GNU |
223 should be upward compatible with those in Berkeley Unix, and upward | 328 should be upward compatible with those in Berkeley Unix, and upward |
224 compatible with @sc{ansi} C if @sc{ansi} C specifies their behavior, and | 329 compatible with Standard C if Standard C specifies their |
225 upward compatible with @sc{posix} if @sc{posix} specifies their | 330 behavior, and upward compatible with @sc{posix} if @sc{posix} specifies |
226 behavior. | 331 their behavior. |
227 | 332 |
228 When these standards conflict, it is useful to offer compatibility | 333 When these standards conflict, it is useful to offer compatibility |
229 modes for each of them. | 334 modes for each of them. |
230 | 335 |
231 @sc{ansi} C and @sc{posix} prohibit many kinds of extensions. Feel free | 336 @cindex options for compatibility |
232 to make the extensions anyway, and include a @samp{--ansi}, | 337 Standard C and @sc{posix} prohibit many kinds of extensions. Feel |
338 free to make the extensions anyway, and include a @samp{--ansi}, | |
233 @samp{--posix}, or @samp{--compatible} option to turn them off. | 339 @samp{--posix}, or @samp{--compatible} option to turn them off. |
234 However, if the extension has a significant chance of breaking any real | 340 However, if the extension has a significant chance of breaking any real |
235 programs or scripts, then it is not really upward compatible. Try to | 341 programs or scripts, then it is not really upward compatible. So you |
236 redesign its interface. | 342 should try to redesign its interface to make it upward compatible. |
237 | 343 |
344 @cindex @code{POSIXLY_CORRECT}, environment variable | |
238 Many GNU programs suppress extensions that conflict with @sc{posix} if the | 345 Many GNU programs suppress extensions that conflict with @sc{posix} if the |
239 environment variable @code{POSIXLY_CORRECT} is defined (even if it is | 346 environment variable @code{POSIXLY_CORRECT} is defined (even if it is |
240 defined with a null value). Please make your program recognize this | 347 defined with a null value). Please make your program recognize this |
241 variable if appropriate. | 348 variable if appropriate. |
242 | 349 |
244 files), and it is done poorly in Unix, feel free to replace it | 351 files), and it is done poorly in Unix, feel free to replace it |
245 completely with something totally different and better. (For example, | 352 completely with something totally different and better. (For example, |
246 @code{vi} is replaced with Emacs.) But it is nice to offer a compatible | 353 @code{vi} is replaced with Emacs.) But it is nice to offer a compatible |
247 feature as well. (There is a free @code{vi} clone, so we offer it.) | 354 feature as well. (There is a free @code{vi} clone, so we offer it.) |
248 | 355 |
249 Additional useful features not in Berkeley Unix are welcome. | 356 Additional useful features are welcome regardless of whether |
357 there is any precedent for them. | |
250 | 358 |
251 @node Using Extensions | 359 @node Using Extensions |
252 @section Using Non-standard Features | 360 @section Using Non-standard Features |
361 @cindex non-standard extensions | |
253 | 362 |
254 Many GNU facilities that already exist support a number of convenient | 363 Many GNU facilities that already exist support a number of convenient |
255 extensions over the comparable Unix facilities. Whether to use these | 364 extensions over the comparable Unix facilities. Whether to use these |
256 extensions in implementing your program is a difficult question. | 365 extensions in implementing your program is a difficult question. |
257 | 366 |
268 In general, perhaps it is best not to use the extensions if you can | 377 In general, perhaps it is best not to use the extensions if you can |
269 straightforwardly do without them, but to use the extensions if they | 378 straightforwardly do without them, but to use the extensions if they |
270 are a big improvement. | 379 are a big improvement. |
271 | 380 |
272 An exception to this rule are the large, established programs (such as | 381 An exception to this rule are the large, established programs (such as |
273 Emacs) which run on a great variety of systems. Such programs would | 382 Emacs) which run on a great variety of systems. Using GNU extensions in |
274 be broken by use of GNU extensions. | 383 such programs would make many users unhappy, so we don't do that. |
275 | 384 |
276 Another exception is for programs that are used as part of | 385 Another exception is for programs that are used as part of compilation: |
277 compilation: anything that must be compiled with other compilers in | 386 anything that must be compiled with other compilers in order to |
278 order to bootstrap the GNU compilation facilities. If these require | 387 bootstrap the GNU compilation facilities. If these require the GNU |
279 the GNU compiler, then no one can compile them without having them | 388 compiler, then no one can compile them without having them installed |
280 installed already. That would be no good. | 389 already. That would be extremely troublesome in certain cases. |
281 | 390 |
282 @node ANSI C | 391 @node Standard C |
283 @section @sc{ansi} C and pre-@sc{ansi} C | 392 @section Standard C and Pre-Standard C |
284 | 393 @cindex @sc{ansi} C standard |
285 Do not ever use the ``trigraph'' feature of @sc{ansi} C. | 394 |
286 | 395 1989 Standard C is widespread enough now that it is ok to use its |
287 @sc{ansi} C is widespread enough now that it is ok to write new programs | 396 features in new programs. There is one exception: do not ever use the |
288 that use @sc{ansi} C features (and therefore will not work in | 397 ``trigraph'' feature of Standard C. |
289 non-@sc{ansi} compilers). And if a program is already written in | 398 |
290 @sc{ansi} C, there's no need to convert it to support non-@sc{ansi} | 399 1999 Standard C is not widespread yet, so please do not require its |
291 compilers. | 400 features in programs. It is ok to use its features if they are present. |
292 | 401 |
293 If you don't know non-@sc{ansi} C, there's no need to learn it; just | 402 However, it is easy to support pre-standard compilers in most programs, |
294 write in @sc{ansi} C. | 403 so if you know how to do that, feel free. If a program you are |
295 | 404 maintaining has such support, you should try to keep it working. |
296 However, it is easy to support non-@sc{ansi} compilers in most programs, | 405 |
297 so you might still consider doing so when you write a program. And if a | 406 @cindex function prototypes |
298 program you are maintaining has such support, you should try to keep it | 407 To support pre-standard C, instead of writing function definitions in |
299 working. | 408 standard prototype form, |
300 | |
301 To support pre-@sc{ansi} C, instead of writing function definitions in | |
302 @sc{ansi} prototype form, | |
303 | 409 |
304 @example | 410 @example |
305 int | 411 int |
306 foo (int x, int y) | 412 foo (int x, int y) |
307 @dots{} | 413 @dots{} |
308 @end example | 414 @end example |
309 | 415 |
310 @noindent | 416 @noindent |
311 write the definition in pre-@sc{ansi} style like this, | 417 write the definition in pre-standard style like this, |
312 | 418 |
313 @example | 419 @example |
314 int | 420 int |
315 foo (x, y) | 421 foo (x, y) |
316 int x, y; | 422 int x, y; |
323 @example | 429 @example |
324 int foo (int, int); | 430 int foo (int, int); |
325 @end example | 431 @end example |
326 | 432 |
327 You need such a declaration anyway, in a header file, to get the benefit | 433 You need such a declaration anyway, in a header file, to get the benefit |
328 of @sc{ansi} C prototypes in all the files where the function is called. | 434 of prototypes in all the files where the function is called. And once |
329 And once you have the declaration, you normally lose nothing by writing | 435 you have the declaration, you normally lose nothing by writing the |
330 the function definition in the pre-@sc{ansi} style. | 436 function definition in the pre-standard style. |
331 | 437 |
332 This technique does not work for integer types narrower than @code{int}. | 438 This technique does not work for integer types narrower than @code{int}. |
333 If you think of an argument as being of a type narrower than @code{int}, | 439 If you think of an argument as being of a type narrower than @code{int}, |
334 declare it as @code{int} instead. | 440 declare it as @code{int} instead. |
335 | 441 |
336 There are a few special cases where this technique is hard to use. For | 442 There are a few special cases where this technique is hard to use. For |
337 example, if a function argument needs to hold the system type | 443 example, if a function argument needs to hold the system type |
338 @code{dev_t}, you run into trouble, because @code{dev_t} is shorter than | 444 @code{dev_t}, you run into trouble, because @code{dev_t} is shorter than |
339 @code{int} on some machines; but you cannot use @code{int} instead, | 445 @code{int} on some machines; but you cannot use @code{int} instead, |
340 because @code{dev_t} is wider than @code{int} on some machines. There | 446 because @code{dev_t} is wider than @code{int} on some machines. There |
341 is no type you can safely use on all machines in a non-@sc{ansi} | 447 is no type you can safely use on all machines in a non-standard |
342 definition. The only way to support non-@sc{ansi} C and pass such an | 448 definition. The only way to support non-standard C and pass such an |
343 argument is to check the width of @code{dev_t} using Autoconf and choose | 449 argument is to check the width of @code{dev_t} using Autoconf and choose |
344 the argument type accordingly. This may not be worth the trouble. | 450 the argument type accordingly. This may not be worth the trouble. |
345 | 451 |
346 @node Source Language | 452 In order to support pre-standard compilers that do not recognize |
347 @section Using Languages Other Than C | 453 prototypes, you may want to use a preprocessor macro like this: |
348 | 454 |
349 Using a language other than C is like using a non-standard feature: it | 455 @example |
350 will cause trouble for users. Even if GCC supports the other language, | 456 /* Declare the prototype for a general external function. */ |
351 users may find it inconvenient to have to install the compiler for that | 457 #if defined (__STDC__) || defined (WINDOWSNT) |
352 other language in order to build your program. For example, if you | 458 #define P_(proto) proto |
353 write your program in C++, people will have to install the C++ compiler | 459 #else |
354 in order to compile your program. Thus, it is better if you write in C. | 460 #define P_(proto) () |
355 | 461 #endif |
356 But there are three situations when there is no disadvantage in using | 462 @end example |
357 some other language: | |
358 | |
359 @itemize @bullet | |
360 @item | |
361 It is okay to use another language if your program contains an | |
362 interpreter for that language. | |
363 | |
364 For example, if your program links with GUILE, it is ok to write part of | |
365 the program in Scheme or another language supported by GUILE. | |
366 | |
367 @item | |
368 It is okay to use another language in a tool specifically intended for | |
369 use with that language. | |
370 | |
371 This is okay because the only people who want to build the tool will be | |
372 those who have installed the other language anyway. | |
373 | |
374 @item | |
375 If an application is of interest to a narrow community, then perhaps | |
376 it's not important if the application is inconvenient to install. | |
377 @end itemize | |
378 | |
379 C has one other advantage over C++ and other compiled languages: more | |
380 people know C, so more people will find it easy to read and modify the | |
381 program if it is written in C. | |
382 | 463 |
383 @node Program Behavior | 464 @node Program Behavior |
384 @chapter Program Behavior for All Programs | 465 @chapter Program Behavior for All Programs |
385 | 466 |
386 This @value{CHAPTER} describes how to write robust software. It also | 467 This @value{CHAPTER} describes conventions for writing robust |
387 describes general standards for error messages, the command line interface, | 468 software. It also describes general standards for error messages, the |
388 and how libraries should behave. | 469 command line interface, and how libraries should behave. |
389 | 470 |
390 @menu | 471 @menu |
391 * Semantics:: Writing robust programs | 472 * Semantics:: Writing robust programs |
392 * Libraries:: Library behavior | 473 * Libraries:: Library behavior |
393 * Errors:: Formatting error messages | 474 * Errors:: Formatting error messages |
394 * User Interfaces:: Standards for command line interfaces | 475 * User Interfaces:: Standards about interfaces generally |
395 * Option Table:: Table of long options. | 476 * Graphical Interfaces:: Standards for graphical interfaces |
477 * Command-Line Interfaces:: Standards for command line interfaces | |
478 * Option Table:: Table of long options | |
396 * Memory Usage:: When and how to care about memory needs | 479 * Memory Usage:: When and how to care about memory needs |
480 * File Usage:: Which files to use, and where | |
397 @end menu | 481 @end menu |
398 | 482 |
399 @node Semantics | 483 @node Semantics |
400 @section Writing Robust Programs | 484 @section Writing Robust Programs |
401 | 485 |
486 @cindex arbitrary limits on data | |
402 Avoid arbitrary limits on the length or number of @emph{any} data | 487 Avoid arbitrary limits on the length or number of @emph{any} data |
403 structure, including file names, lines, files, and symbols, by allocating | 488 structure, including file names, lines, files, and symbols, by allocating |
404 all data structures dynamically. In most Unix utilities, ``long lines | 489 all data structures dynamically. In most Unix utilities, ``long lines |
405 are silently truncated''. This is not acceptable in a GNU utility. | 490 are silently truncated''. This is not acceptable in a GNU utility. |
406 | 491 |
492 @cindex @code{NUL} characters | |
407 Utilities reading files should not drop NUL characters, or any other | 493 Utilities reading files should not drop NUL characters, or any other |
408 nonprinting characters @emph{including those with codes above 0177}. | 494 nonprinting characters @emph{including those with codes above 0177}. |
409 The only sensible exceptions would be utilities specifically intended | 495 The only sensible exceptions would be utilities specifically intended |
410 for interface to certain types of terminals or printers | 496 for interface to certain types of terminals or printers |
411 that can't handle those characters. | 497 that can't handle those characters. |
412 Whenever possible, try to make programs work properly with | 498 Whenever possible, try to make programs work properly with |
413 sequences of bytes that represent multibyte characters, using encodings | 499 sequences of bytes that represent multibyte characters, using encodings |
414 such as UTF-8 and others. | 500 such as UTF-8 and others. |
415 | 501 |
502 @cindex error messages | |
416 Check every system call for an error return, unless you know you wish to | 503 Check every system call for an error return, unless you know you wish to |
417 ignore errors. Include the system error text (from @code{perror} or | 504 ignore errors. Include the system error text (from @code{perror} or |
418 equivalent) in @emph{every} error message resulting from a failing | 505 equivalent) in @emph{every} error message resulting from a failing |
419 system call, as well as the name of the file if any and the name of the | 506 system call, as well as the name of the file if any and the name of the |
420 utility. Just ``cannot open foo.c'' or ``stat failed'' is not | 507 utility. Just ``cannot open foo.c'' or ``stat failed'' is not |
421 sufficient. | 508 sufficient. |
422 | 509 |
510 @cindex @code{malloc} return value | |
511 @cindex memory allocation failure | |
423 Check every call to @code{malloc} or @code{realloc} to see if it | 512 Check every call to @code{malloc} or @code{realloc} to see if it |
424 returned zero. Check @code{realloc} even if you are making the block | 513 returned zero. Check @code{realloc} even if you are making the block |
425 smaller; in a system that rounds block sizes to a power of 2, | 514 smaller; in a system that rounds block sizes to a power of 2, |
426 @code{realloc} may get a different block if you ask for less space. | 515 @code{realloc} may get a different block if you ask for less space. |
427 | 516 |
439 error. In an interactive program (one that reads commands from the | 528 error. In an interactive program (one that reads commands from the |
440 user), it is better to abort the command and return to the command | 529 user), it is better to abort the command and return to the command |
441 reader loop. This allows the user to kill other processes to free up | 530 reader loop. This allows the user to kill other processes to free up |
442 virtual memory, and then try the command again. | 531 virtual memory, and then try the command again. |
443 | 532 |
533 @cindex command-line arguments, decoding | |
444 Use @code{getopt_long} to decode arguments, unless the argument syntax | 534 Use @code{getopt_long} to decode arguments, unless the argument syntax |
445 makes this unreasonable. | 535 makes this unreasonable. |
446 | 536 |
447 When static storage is to be written in during program execution, use | 537 When static storage is to be written in during program execution, use |
448 explicit C code to initialize it. Reserve C initialized declarations | 538 explicit C code to initialize it. Reserve C initialized declarations |
453 as file directories, utmp, or the layout of kernel memory), since these | 543 as file directories, utmp, or the layout of kernel memory), since these |
454 are less likely to work compatibly. If you need to find all the files | 544 are less likely to work compatibly. If you need to find all the files |
455 in a directory, use @code{readdir} or some other high-level interface. | 545 in a directory, use @code{readdir} or some other high-level interface. |
456 These are supported compatibly by GNU. | 546 These are supported compatibly by GNU. |
457 | 547 |
548 @cindex signal handling | |
458 The preferred signal handling facilities are the BSD variant of | 549 The preferred signal handling facilities are the BSD variant of |
459 @code{signal}, and the @sc{posix} @code{sigaction} function; the | 550 @code{signal}, and the @sc{posix} @code{sigaction} function; the |
460 alternative USG @code{signal} interface is an inferior design. | 551 alternative USG @code{signal} interface is an inferior design. |
461 | 552 |
462 Nowadays, using the @sc{posix} signal functions may be the easiest way | 553 Nowadays, using the @sc{posix} signal functions may be the easiest way |
464 systems running GNU libc version 1, you should include | 555 systems running GNU libc version 1, you should include |
465 @file{bsd/signal.h} instead of @file{signal.h}, so as to get BSD | 556 @file{bsd/signal.h} instead of @file{signal.h}, so as to get BSD |
466 behavior. It is up to you whether to support systems where | 557 behavior. It is up to you whether to support systems where |
467 @code{signal} has only the USG behavior, or give up on them. | 558 @code{signal} has only the USG behavior, or give up on them. |
468 | 559 |
560 @cindex impossible conditions | |
469 In error checks that detect ``impossible'' conditions, just abort. | 561 In error checks that detect ``impossible'' conditions, just abort. |
470 There is usually no point in printing any message. These checks | 562 There is usually no point in printing any message. These checks |
471 indicate the existence of bugs. Whoever wants to fix the bugs will have | 563 indicate the existence of bugs. Whoever wants to fix the bugs will have |
472 to read the source code and run a debugger. So explain the problem with | 564 to read the source code and run a debugger. So explain the problem with |
473 comments in the source. The relevant data will be in variables, which | 565 comments in the source. The relevant data will be in variables, which |
478 @emph{That does not work}, because exit status values are limited to 8 | 570 @emph{That does not work}, because exit status values are limited to 8 |
479 bits (0 through 255). A single run of the program might have 256 | 571 bits (0 through 255). A single run of the program might have 256 |
480 errors; if you try to return 256 as the exit status, the parent process | 572 errors; if you try to return 256 as the exit status, the parent process |
481 will see 0 as the status, and it will appear that the program succeeded. | 573 will see 0 as the status, and it will appear that the program succeeded. |
482 | 574 |
575 @cindex temporary files | |
576 @cindex @code{TMPDIR} environment variable | |
483 If you make temporary files, check the @code{TMPDIR} environment | 577 If you make temporary files, check the @code{TMPDIR} environment |
484 variable; if that variable is defined, use the specified directory | 578 variable; if that variable is defined, use the specified directory |
485 instead of @file{/tmp}. | 579 instead of @file{/tmp}. |
486 | 580 |
581 In addition, be aware that there is a possible security problem when | |
582 creating temporary files in world-writable directories. In C, you can | |
583 avoid this problem by creating temporary files in this manner: | |
584 | |
585 @example | |
586 fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0600); | |
587 @end example | |
588 | |
589 @noindent | |
590 or by using the @code{mkstemps} function from libiberty. | |
591 | |
592 In bash, use @code{set -C} to avoid this problem. | |
593 | |
487 @node Libraries | 594 @node Libraries |
488 @section Library Behavior | 595 @section Library Behavior |
596 @cindex libraries | |
489 | 597 |
490 Try to make library functions reentrant. If they need to do dynamic | 598 Try to make library functions reentrant. If they need to do dynamic |
491 storage allocation, at least try to avoid any nonreentrancy aside from | 599 storage allocation, at least try to avoid any nonreentrancy aside from |
492 that of @code{malloc} itself. | 600 that of @code{malloc} itself. |
493 | 601 |
503 An exception can be made when two external symbols are always used | 611 An exception can be made when two external symbols are always used |
504 together, so that no reasonable program could use one without the | 612 together, so that no reasonable program could use one without the |
505 other; then they can both go in the same file. | 613 other; then they can both go in the same file. |
506 | 614 |
507 External symbols that are not documented entry points for the user | 615 External symbols that are not documented entry points for the user |
508 should have names beginning with @samp{_}. They should also contain | 616 should have names beginning with @samp{_}. The @samp{_} should be |
509 the chosen name prefix for the library, to prevent collisions with | 617 followed by the chosen name prefix for the library, to prevent |
510 other libraries. These can go in the same files with user entry | 618 collisions with other libraries. These can go in the same files with |
511 points if you like. | 619 user entry points if you like. |
512 | 620 |
513 Static functions and variables can be used as you like and need not | 621 Static functions and variables can be used as you like and need not |
514 fit any naming convention. | 622 fit any naming convention. |
515 | 623 |
516 @node Errors | 624 @node Errors |
517 @section Formatting Error Messages | 625 @section Formatting Error Messages |
626 @cindex formatting error messages | |
627 @cindex error messages, formatting | |
518 | 628 |
519 Error messages from compilers should look like this: | 629 Error messages from compilers should look like this: |
520 | 630 |
521 @example | 631 @example |
522 @var{source-file-name}:@var{lineno}: @var{message} | 632 @var{source-file-name}:@var{lineno}: @var{message} |
572 Error messages from interactive programs, and other messages such as | 682 Error messages from interactive programs, and other messages such as |
573 usage messages, should start with a capital letter. But they should not | 683 usage messages, should start with a capital letter. But they should not |
574 end with a period. | 684 end with a period. |
575 | 685 |
576 @node User Interfaces | 686 @node User Interfaces |
577 @section Standards for Command Line Interfaces | 687 @section Standards for Interfaces Generally |
578 | 688 |
689 @cindex program name and its behavior | |
690 @cindex behavior, dependent on program's name | |
579 Please don't make the behavior of a utility depend on the name used | 691 Please don't make the behavior of a utility depend on the name used |
580 to invoke it. It is useful sometimes to make a link to a utility | 692 to invoke it. It is useful sometimes to make a link to a utility |
581 with a different name, and that should not change what it does. | 693 with a different name, and that should not change what it does. |
582 | 694 |
583 Instead, use a run time option or a compilation switch or both | 695 Instead, use a run time option or a compilation switch or both |
584 to select among the alternate behaviors. | 696 to select among the alternate behaviors. |
585 | 697 |
698 @cindex output device and program's behavior | |
586 Likewise, please don't make the behavior of the program depend on the | 699 Likewise, please don't make the behavior of the program depend on the |
587 type of output device it is used with. Device independence is an | 700 type of output device it is used with. Device independence is an |
588 important principle of the system's design; do not compromise it merely | 701 important principle of the system's design; do not compromise it merely |
589 to save someone from typing an option now and then. (Variation in error | 702 to save someone from typing an option now and then. (Variation in error |
590 message syntax when using a terminal is ok, because that is a side issue | 703 message syntax when using a terminal is ok, because that is a side issue |
602 program with a preferred alternate version that does not depend on the | 715 program with a preferred alternate version that does not depend on the |
603 output device type. For example, we provide a @code{dir} program much | 716 output device type. For example, we provide a @code{dir} program much |
604 like @code{ls} except that its default output format is always | 717 like @code{ls} except that its default output format is always |
605 multi-column format. | 718 multi-column format. |
606 | 719 |
720 @node Graphical Interfaces | |
721 @section Standards for Graphical Interfaces | |
722 @cindex graphical user interface | |
723 | |
724 @cindex gtk | |
725 When you write a program that provides a graphical user interface, | |
726 please make it work with X Windows and the GTK toolkit unless the | |
727 functionality specifically requires some alternative (for example, | |
728 ``displaying jpeg images while in console mode''). | |
729 | |
730 In addition, please provide a command-line interface to control the | |
731 functionality. (In many cases, the graphical user interface can be a | |
732 separate program which invokes the command-line program.) This is | |
733 so that the same jobs can be done from scripts. | |
734 | |
735 @cindex corba | |
736 @cindex gnome | |
737 Please also consider providing a CORBA interface (for use from GNOME), a | |
738 library interface (for use from C), and perhaps a keyboard-driven | |
739 console interface (for use by users from console mode). Once you are | |
740 doing the work to provide the functionality and the graphical interface, | |
741 these won't be much extra work. | |
742 | |
743 @node Command-Line Interfaces | |
744 @section Standards for Command Line Interfaces | |
745 @cindex command-line interface | |
746 | |
747 @findex getopt | |
607 It is a good idea to follow the @sc{posix} guidelines for the | 748 It is a good idea to follow the @sc{posix} guidelines for the |
608 command-line options of a program. The easiest way to do this is to use | 749 command-line options of a program. The easiest way to do this is to use |
609 @code{getopt} to parse them. Note that the GNU version of @code{getopt} | 750 @code{getopt} to parse them. Note that the GNU version of @code{getopt} |
610 will normally permit options anywhere among the arguments unless the | 751 will normally permit options anywhere among the arguments unless the |
611 special argument @samp{--} is used. This is not what @sc{posix} | 752 special argument @samp{--} is used. This is not what @sc{posix} |
612 specifies; it is a GNU extension. | 753 specifies; it is a GNU extension. |
613 | 754 |
755 @cindex long-named options | |
614 Please define long-named options that are equivalent to the | 756 Please define long-named options that are equivalent to the |
615 single-letter Unix-style options. We hope to make GNU more user | 757 single-letter Unix-style options. We hope to make GNU more user |
616 friendly this way. This is easy to do with the GNU function | 758 friendly this way. This is easy to do with the GNU function |
617 @code{getopt_long}. | 759 @code{getopt_long}. |
618 | 760 |
628 (preferably @samp{-o} or @samp{--output}). Even if you allow an output | 770 (preferably @samp{-o} or @samp{--output}). Even if you allow an output |
629 file name as an ordinary argument for compatibility, try to provide an | 771 file name as an ordinary argument for compatibility, try to provide an |
630 option as another way to specify it. This will lead to more consistency | 772 option as another way to specify it. This will lead to more consistency |
631 among GNU utilities, and fewer idiosyncracies for users to remember. | 773 among GNU utilities, and fewer idiosyncracies for users to remember. |
632 | 774 |
775 @cindex standard command-line options | |
633 All programs should support two standard options: @samp{--version} | 776 All programs should support two standard options: @samp{--version} |
634 and @samp{--help}. | 777 and @samp{--help}. |
635 | 778 |
636 @table @code | 779 @table @code |
780 @cindex @samp{--version} option | |
637 @item --version | 781 @item --version |
638 This option should direct the program to print information about its name, | 782 This option should direct the program to print information about its name, |
639 version, origin and legal status, all on standard output, and then exit | 783 version, origin and legal status, all on standard output, and then exit |
640 successfully. Other options and arguments should be ignored once this | 784 successfully. Other options and arguments should be ignored once this |
641 is seen, and the program should not perform its normal function. | 785 is seen, and the program should not perform its normal function. |
642 | 786 |
787 @cindex canonical name of a program | |
788 @cindex program's canonical name | |
643 The first line is meant to be easy for a program to parse; the version | 789 The first line is meant to be easy for a program to parse; the version |
644 number proper starts after the last space. In addition, it contains | 790 number proper starts after the last space. In addition, it contains |
645 the canonical name for this program, in this format: | 791 the canonical name for this program, in this format: |
646 | 792 |
647 @example | 793 @example |
710 which changes were made---there's no need to list the years for previous | 856 which changes were made---there's no need to list the years for previous |
711 versions' changes. You don't have to mention the name of the program in | 857 versions' changes. You don't have to mention the name of the program in |
712 these notices, if that is inconvenient, since it appeared in the first | 858 these notices, if that is inconvenient, since it appeared in the first |
713 line. | 859 line. |
714 | 860 |
861 @cindex @samp{--help} option | |
715 @item --help | 862 @item --help |
716 This option should output brief documentation for how to invoke the | 863 This option should output brief documentation for how to invoke the |
717 program, on standard output, then exit successfully. Other options and | 864 program, on standard output, then exit successfully. Other options and |
718 arguments should be ignored once this is seen, and the program should | 865 arguments should be ignored once this is seen, and the program should |
719 not perform its normal function. | 866 not perform its normal function. |
720 | 867 |
868 @cindex address for bug reports | |
869 @cindex bug reports | |
721 Near the end of the @samp{--help} option's output there should be a line | 870 Near the end of the @samp{--help} option's output there should be a line |
722 that says where to mail bug reports. It should have this format: | 871 that says where to mail bug reports. It should have this format: |
723 | 872 |
724 @example | 873 @example |
725 Report bugs to @var{mailing-address}. | 874 Report bugs to @var{mailing-address}. |
726 @end example | 875 @end example |
727 @end table | 876 @end table |
728 | 877 |
729 @node Option Table | 878 @node Option Table |
730 @section Table of Long Options | 879 @section Table of Long Options |
880 @cindex long option names | |
881 @cindex table of long options | |
731 | 882 |
732 Here is a table of long options used by GNU programs. It is surely | 883 Here is a table of long options used by GNU programs. It is surely |
733 incomplete, but we aim to list all the options that a new program might | 884 incomplete, but we aim to list all the options that a new program might |
734 want to be compatible with. If you use names not already in the table, | 885 want to be compatible with. If you use names not already in the table, |
735 please send @email{gnu@@gnu.org} a list of them, with their | 886 please send @email{bug-standards@@gnu.org} a list of them, with their |
736 meanings, so we can update the table. | 887 meanings, so we can update the table. |
737 | 888 |
738 @c Please leave newlines between items in this table; it's much easier | 889 @c Please leave newlines between items in this table; it's much easier |
739 @c to update when it isn't completely squashed together and unreadable. | 890 @c to update when it isn't completely squashed together and unreadable. |
740 @c When there is more than one short option for a long option name, put | 891 @c When there is more than one short option for a long option name, put |
1102 @samp{-d} in @code{shar}. | 1253 @samp{-d} in @code{shar}. |
1103 | 1254 |
1104 @item hide-control-chars | 1255 @item hide-control-chars |
1105 @samp{-q} in @code{ls}. | 1256 @samp{-q} in @code{ls}. |
1106 | 1257 |
1258 @item html | |
1259 In @code{makeinfo}, output HTML. | |
1260 | |
1107 @item idle | 1261 @item idle |
1108 @samp{-u} in @code{who}. | 1262 @samp{-u} in @code{who}. |
1109 | 1263 |
1110 @item ifdef | 1264 @item ifdef |
1111 @samp{-D} in @code{diff}. | 1265 @samp{-D} in @code{diff}. |
1161 @item incremental | 1315 @item incremental |
1162 @samp{-G} in @code{tar}. | 1316 @samp{-G} in @code{tar}. |
1163 | 1317 |
1164 @item info | 1318 @item info |
1165 @samp{-i}, @samp{-l}, and @samp{-m} in Finger. | 1319 @samp{-i}, @samp{-l}, and @samp{-m} in Finger. |
1320 | |
1321 @item init-file | |
1322 In some programs, specify the name of the file to read as the user's | |
1323 init file. | |
1166 | 1324 |
1167 @item initial | 1325 @item initial |
1168 @samp{-i} in @code{expand}. | 1326 @samp{-i} in @code{expand}. |
1169 | 1327 |
1170 @item initial-tab | 1328 @item initial-tab |
1180 @samp{-w} in @code{tar}. | 1338 @samp{-w} in @code{tar}. |
1181 | 1339 |
1182 @item intermix-type | 1340 @item intermix-type |
1183 @samp{-p} in @code{shar}. | 1341 @samp{-p} in @code{shar}. |
1184 | 1342 |
1343 @item iso-8601 | |
1344 Used in @code{date} | |
1345 | |
1185 @item jobs | 1346 @item jobs |
1186 @samp{-j} in Make. | 1347 @samp{-j} in Make. |
1187 | 1348 |
1188 @item just-print | 1349 @item just-print |
1189 @samp{-n} in Make. | 1350 @samp{-n} in Make. |
1833 @samp{-z} in @code{gprof}. | 1994 @samp{-z} in @code{gprof}. |
1834 @end table | 1995 @end table |
1835 | 1996 |
1836 @node Memory Usage | 1997 @node Memory Usage |
1837 @section Memory Usage | 1998 @section Memory Usage |
1838 | 1999 @cindex memory usage |
1839 If it typically uses just a few meg of memory, don't bother making any | 2000 |
2001 If a program typically uses just a few meg of memory, don't bother making any | |
1840 effort to reduce memory usage. For example, if it is impractical for | 2002 effort to reduce memory usage. For example, if it is impractical for |
1841 other reasons to operate on files more than a few meg long, it is | 2003 other reasons to operate on files more than a few meg long, it is |
1842 reasonable to read entire input files into core to operate on them. | 2004 reasonable to read entire input files into core to operate on them. |
1843 | 2005 |
1844 However, for programs such as @code{cat} or @code{tail}, that can | 2006 However, for programs such as @code{cat} or @code{tail}, that can |
1849 this is not very hard and users will want to be able to operate on input | 2011 this is not very hard and users will want to be able to operate on input |
1850 files that are bigger than will fit in core all at once. | 2012 files that are bigger than will fit in core all at once. |
1851 | 2013 |
1852 If your program creates complicated data structures, just make them in | 2014 If your program creates complicated data structures, just make them in |
1853 core and give a fatal error if @code{malloc} returns zero. | 2015 core and give a fatal error if @code{malloc} returns zero. |
2016 | |
2017 @node File Usage | |
2018 @section File Usage | |
2019 @cindex file usage | |
2020 | |
2021 Programs should be prepared to operate when @file{/usr} and @file{/etc} | |
2022 are read-only file systems. Thus, if the program manages log files, | |
2023 lock files, backup files, score files, or any other files which are | |
2024 modified for internal purposes, these files should not be stored in | |
2025 @file{/usr} or @file{/etc}. | |
2026 | |
2027 There are two exceptions. @file{/etc} is used to store system | |
2028 configuration information; it is reasonable for a program to modify | |
2029 files in @file{/etc} when its job is to update the system configuration. | |
2030 Also, if the user explicitly asks to modify one file in a directory, it | |
2031 is reasonable for the program to store other files in the same | |
2032 directory. | |
1854 | 2033 |
1855 @node Writing C | 2034 @node Writing C |
1856 @chapter Making The Best Use of C | 2035 @chapter Making The Best Use of C |
1857 | 2036 |
1858 This @value{CHAPTER} provides advice on how best to use the C language | 2037 This @value{CHAPTER} provides advice on how best to use the C language |
1870 * Mmap:: How you can safely use @code{mmap}. | 2049 * Mmap:: How you can safely use @code{mmap}. |
1871 @end menu | 2050 @end menu |
1872 | 2051 |
1873 @node Formatting | 2052 @node Formatting |
1874 @section Formatting Your Source Code | 2053 @section Formatting Your Source Code |
1875 | 2054 @cindex formatting source code |
2055 | |
2056 @cindex open brace | |
2057 @cindex braces, in C source | |
1876 It is important to put the open-brace that starts the body of a C | 2058 It is important to put the open-brace that starts the body of a C |
1877 function in column zero, and avoid putting any other open-brace or | 2059 function in column zero, and avoid putting any other open-brace or |
1878 open-parenthesis or open-bracket in column zero. Several tools look | 2060 open-parenthesis or open-bracket in column zero. Several tools look |
1879 for open-braces in column zero to find the beginnings of C functions. | 2061 for open-braces in column zero to find the beginnings of C functions. |
1880 These tools will not work on code not formatted that way. | 2062 These tools will not work on code not formatted that way. |
1892 @dots{} | 2074 @dots{} |
1893 @} | 2075 @} |
1894 @end example | 2076 @end example |
1895 | 2077 |
1896 @noindent | 2078 @noindent |
1897 or, if you want to use @sc{ansi} C, format the definition like this: | 2079 or, if you want to use Standard C syntax, format the definition like |
2080 this: | |
1898 | 2081 |
1899 @example | 2082 @example |
1900 static char * | 2083 static char * |
1901 concat (char *s1, char *s2) | 2084 concat (char *s1, char *s2) |
1902 @{ | 2085 @{ |
1903 @dots{} | 2086 @dots{} |
1904 @} | 2087 @} |
1905 @end example | 2088 @end example |
1906 | 2089 |
1907 In @sc{ansi} C, if the arguments don't fit nicely on one line, | 2090 In Standard C, if the arguments don't fit nicely on one line, |
1908 split it like this: | 2091 split it like this: |
1909 | 2092 |
1910 @example | 2093 @example |
1911 int | 2094 int |
1912 lots_of_args (int an_integer, long a_long, short a_short, | 2095 lots_of_args (int an_integer, long a_long, short a_short, |
1913 double a_double, float a_float) | 2096 double a_double, float a_float) |
1914 @dots{} | 2097 @dots{} |
1915 @end example | 2098 @end example |
1916 | 2099 |
1917 For the body of the function, we prefer code formatted like this: | 2100 The rest of this section gives our recommendations for other aspects of |
2101 C formatting style, which is also the default style of the @code{indent} | |
2102 program in version 1.2 and newer. It corresponds to the options | |
2103 | |
2104 @smallexample | |
2105 -nbad -bap -nbc -bbo -bl -bli2 -bls -ncdb -nce -cp1 -cs -di2 | |
2106 -ndj -nfc1 -nfca -hnl -i2 -ip5 -lp -pcs -psl -nsc -nsob | |
2107 @end smallexample | |
2108 | |
2109 We don't think of these recommendations as requirements, because it | |
2110 causes no problems for users if two different programs have different | |
2111 formatting styles. | |
2112 | |
2113 But whatever style you use, please use it consistently, since a mixture | |
2114 of styles within one program tends to look ugly. If you are | |
2115 contributing changes to an existing program, please follow the style of | |
2116 that program. | |
2117 | |
2118 For the body of the function, our recommended style looks like this: | |
1918 | 2119 |
1919 @example | 2120 @example |
1920 if (x < foo (y, z)) | 2121 if (x < foo (y, z)) |
1921 haha = bar[4] + 5; | 2122 haha = bar[4] + 5; |
1922 else | 2123 else |
1928 @} | 2129 @} |
1929 return ++x + bar (); | 2130 return ++x + bar (); |
1930 @} | 2131 @} |
1931 @end example | 2132 @end example |
1932 | 2133 |
2134 @cindex spaces before open-paren | |
1933 We find it easier to read a program when it has spaces before the | 2135 We find it easier to read a program when it has spaces before the |
1934 open-parentheses and after the commas. Especially after the commas. | 2136 open-parentheses and after the commas. Especially after the commas. |
1935 | 2137 |
1936 When you split an expression into multiple lines, split it | 2138 When you split an expression into multiple lines, split it |
1937 before an operator, not after one. Here is the right way: | 2139 before an operator, not after one. Here is the right way: |
1938 | 2140 |
2141 @cindex expressions, splitting | |
1939 @example | 2142 @example |
1940 if (foo_this_is_long && bar > win (x, y, z) | 2143 if (foo_this_is_long && bar > win (x, y, z) |
1941 && remaining_condition) | 2144 && remaining_condition) |
1942 @end example | 2145 @end example |
1943 | 2146 |
1958 ? outmode[j] : inmode[j]); | 2161 ? outmode[j] : inmode[j]); |
1959 @end example | 2162 @end example |
1960 | 2163 |
1961 Insert extra parentheses so that Emacs will indent the code properly. | 2164 Insert extra parentheses so that Emacs will indent the code properly. |
1962 For example, the following indentation looks nice if you do it by hand, | 2165 For example, the following indentation looks nice if you do it by hand, |
1963 but Emacs would mess it up: | |
1964 | 2166 |
1965 @example | 2167 @example |
1966 v = rup->ru_utime.tv_sec*1000 + rup->ru_utime.tv_usec/1000 | 2168 v = rup->ru_utime.tv_sec*1000 + rup->ru_utime.tv_usec/1000 |
1967 + rup->ru_stime.tv_sec*1000 + rup->ru_stime.tv_usec/1000; | 2169 + rup->ru_stime.tv_sec*1000 + rup->ru_stime.tv_usec/1000; |
1968 @end example | 2170 @end example |
1969 | 2171 |
1970 But adding a set of parentheses solves the problem: | 2172 @noindent |
2173 but Emacs would alter it. Adding a set of parentheses produces | |
2174 something that looks equally nice, and which Emacs will preserve: | |
1971 | 2175 |
1972 @example | 2176 @example |
1973 v = (rup->ru_utime.tv_sec*1000 + rup->ru_utime.tv_usec/1000 | 2177 v = (rup->ru_utime.tv_sec*1000 + rup->ru_utime.tv_usec/1000 |
1974 + rup->ru_stime.tv_sec*1000 + rup->ru_stime.tv_usec/1000); | 2178 + rup->ru_stime.tv_sec*1000 + rup->ru_stime.tv_usec/1000); |
1975 @end example | 2179 @end example |
1982 a = foo (a); | 2186 a = foo (a); |
1983 @} | 2187 @} |
1984 while (a > 0); | 2188 while (a > 0); |
1985 @end example | 2189 @end example |
1986 | 2190 |
2191 @cindex formfeed | |
2192 @cindex control-L | |
1987 Please use formfeed characters (control-L) to divide the program into | 2193 Please use formfeed characters (control-L) to divide the program into |
1988 pages at logical places (but not within a function). It does not matter | 2194 pages at logical places (but not within a function). It does not matter |
1989 just how long the pages are, since they do not have to fit on a printed | 2195 just how long the pages are, since they do not have to fit on a printed |
1990 page. The formfeeds should appear alone on lines by themselves. | 2196 page. The formfeeds should appear alone on lines by themselves. |
1991 | 2197 |
1992 | |
1993 @node Comments | 2198 @node Comments |
1994 @section Commenting Your Work | 2199 @section Commenting Your Work |
2200 @cindex commenting | |
1995 | 2201 |
1996 Every program should start with a comment saying briefly what it is for. | 2202 Every program should start with a comment saying briefly what it is for. |
1997 Example: @samp{fmt - filter for simple filling of text}. | 2203 Example: @samp{fmt - filter for simple filling of text}. |
1998 | 2204 |
1999 Please write the comments in a GNU program in English, because English | 2205 Please write the comments in a GNU program in English, because English |
2041 /* Nonzero means truncate lines in the display; | 2247 /* Nonzero means truncate lines in the display; |
2042 zero means continue them. */ | 2248 zero means continue them. */ |
2043 int truncate_lines; | 2249 int truncate_lines; |
2044 @end example | 2250 @end example |
2045 | 2251 |
2252 @cindex conditionals, comments for | |
2253 @cindex @code{#endif}, commenting | |
2046 Every @samp{#endif} should have a comment, except in the case of short | 2254 Every @samp{#endif} should have a comment, except in the case of short |
2047 conditionals (just a few lines) that are not nested. The comment should | 2255 conditionals (just a few lines) that are not nested. The comment should |
2048 state the condition of the conditional that is ending, @emph{including | 2256 state the condition of the conditional that is ending, @emph{including |
2049 its sense}. @samp{#else} should have a comment describing the condition | 2257 its sense}. @samp{#else} should have a comment describing the condition |
2050 @emph{and sense} of the code that follows. For example: | 2258 @emph{and sense} of the code that follows. For example: |
2082 @end group | 2290 @end group |
2083 @end example | 2291 @end example |
2084 | 2292 |
2085 @node Syntactic Conventions | 2293 @node Syntactic Conventions |
2086 @section Clean Use of C Constructs | 2294 @section Clean Use of C Constructs |
2087 | 2295 @cindex syntactic conventions |
2088 Please explicitly declare all arguments to functions. | 2296 |
2089 Don't omit them just because they are @code{int}s. | 2297 @cindex implicit @code{int} |
2298 @cindex function argument, declaring | |
2299 Please explicitly declare the types of all objects. For example, you | |
2300 should explicitly declare all arguments to functions, and you should | |
2301 declare functions to return @code{int} rather than omitting the | |
2302 @code{int}. | |
2303 | |
2304 @cindex compiler warnings | |
2305 @cindex @samp{-Wall} compiler option | |
2306 Some programmers like to use the GCC @samp{-Wall} option, and change the | |
2307 code whenever it issues a warning. If you want to do this, then do. | |
2308 Other programmers prefer not to use @samp{-Wall}, because it gives | |
2309 warnings for valid and legitimate code which they do not want to change. | |
2310 If you want to do this, then do. The compiler should be your servant, | |
2311 not your master. | |
2090 | 2312 |
2091 Declarations of external functions and functions to appear later in the | 2313 Declarations of external functions and functions to appear later in the |
2092 source file should all go in one place near the beginning of the file | 2314 source file should all go in one place near the beginning of the file |
2093 (somewhere before the first function definition in the file), or else | 2315 (somewhere before the first function definition in the file), or else |
2094 should go in a header file. Don't put @code{extern} declarations inside | 2316 should go in a header file. Don't put @code{extern} declarations inside |
2095 functions. | 2317 functions. |
2096 | 2318 |
2319 @cindex temporary variables | |
2097 It used to be common practice to use the same local variables (with | 2320 It used to be common practice to use the same local variables (with |
2098 names like @code{tem}) over and over for different values within one | 2321 names like @code{tem}) over and over for different values within one |
2099 function. Instead of doing this, it is better declare a separate local | 2322 function. Instead of doing this, it is better declare a separate local |
2100 variable for each distinct purpose, and give it a name which is | 2323 variable for each distinct purpose, and give it a name which is |
2101 meaningful. This not only makes programs easier to understand, it also | 2324 meaningful. This not only makes programs easier to understand, it also |
2103 declaration of each local variable into the smallest scope that includes | 2326 declaration of each local variable into the smallest scope that includes |
2104 all its uses. This makes the program even cleaner. | 2327 all its uses. This makes the program even cleaner. |
2105 | 2328 |
2106 Don't use local variables or parameters that shadow global identifiers. | 2329 Don't use local variables or parameters that shadow global identifiers. |
2107 | 2330 |
2331 @cindex multiple variables in a line | |
2108 Don't declare multiple variables in one declaration that spans lines. | 2332 Don't declare multiple variables in one declaration that spans lines. |
2109 Start a new declaration on each line, instead. For example, instead | 2333 Start a new declaration on each line, instead. For example, instead |
2110 of this: | 2334 of this: |
2111 | 2335 |
2112 @example | 2336 @example |
2203 foo = (char *) malloc (sizeof *foo); | 2427 foo = (char *) malloc (sizeof *foo); |
2204 if (foo == 0) | 2428 if (foo == 0) |
2205 fatal ("virtual memory exhausted"); | 2429 fatal ("virtual memory exhausted"); |
2206 @end example | 2430 @end example |
2207 | 2431 |
2432 @pindex lint | |
2208 Don't make the program ugly to placate @code{lint}. Please don't insert any | 2433 Don't make the program ugly to placate @code{lint}. Please don't insert any |
2209 casts to @code{void}. Zero without a cast is perfectly fine as a null | 2434 casts to @code{void}. Zero without a cast is perfectly fine as a null |
2210 pointer constant, except when calling a varargs function. | 2435 pointer constant, except when calling a varargs function. |
2211 | 2436 |
2212 @node Names | 2437 @node Names |
2213 @section Naming Variables and Functions | 2438 @section Naming Variables and Functions |
2214 | 2439 |
2440 @cindex names of variables and functions | |
2215 The names of global variables and functions in a program serve as | 2441 The names of global variables and functions in a program serve as |
2216 comments of a sort. So don't choose terse names---instead, look for | 2442 comments of a sort. So don't choose terse names---instead, look for |
2217 names that give useful information about the meaning of the variable or | 2443 names that give useful information about the meaning of the variable or |
2218 function. In a GNU program, names should be English, like other | 2444 function. In a GNU program, names should be English, like other |
2219 comments. | 2445 comments. |
2247 | 2473 |
2248 When you want to define names with constant integer values, use | 2474 When you want to define names with constant integer values, use |
2249 @code{enum} rather than @samp{#define}. GDB knows about enumeration | 2475 @code{enum} rather than @samp{#define}. GDB knows about enumeration |
2250 constants. | 2476 constants. |
2251 | 2477 |
2252 Use file names of 14 characters or less, to avoid creating gratuitous | 2478 @cindex file-name limitations |
2253 problems on older System V systems. You can use the program | 2479 @pindex doschk |
2254 @code{doschk} to test for this. @code{doschk} also tests for potential | 2480 You might want to make sure that none of the file names would conflict |
2255 name conflicts if the files were loaded onto an MS-DOS file | 2481 the files were loaded onto an MS-DOS file system which shortens the |
2256 system---something you may or may not care about. | 2482 names. You can use the program @code{doschk} to test for this. |
2483 | |
2484 Some GNU programs were designed to limit themselves to file names of 14 | |
2485 characters or less, to avoid file name conflicts if they are read into | |
2486 older System V systems. Please preserve this feature in the existing | |
2487 GNU programs that have it, but there is no need to do this in new GNU | |
2488 programs. @code{doschk} also reports file names longer than 14 | |
2489 characters. | |
2257 | 2490 |
2258 @node System Portability | 2491 @node System Portability |
2259 @section Portability between System Types | 2492 @section Portability between System Types |
2493 @cindex portability, between system types | |
2260 | 2494 |
2261 In the Unix world, ``portability'' refers to porting to different Unix | 2495 In the Unix world, ``portability'' refers to porting to different Unix |
2262 versions. For a GNU program, this kind of portability is desirable, but | 2496 versions. For a GNU program, this kind of portability is desirable, but |
2263 not paramount. | 2497 not paramount. |
2264 | 2498 |
2265 The primary purpose of GNU software is to run on top of the GNU kernel, | 2499 The primary purpose of GNU software is to run on top of the GNU kernel, |
2266 compiled with the GNU C compiler, on various types of @sc{cpu}. The | 2500 compiled with the GNU C compiler, on various types of @sc{cpu}. So the |
2267 amount and kinds of variation among GNU systems on different @sc{cpu}s | 2501 kinds of portability that are absolutely necessary are quite limited. |
2268 will be comparable to the variation among Linux-based GNU systems or | 2502 But it is important to support Linux-based GNU systems, since they |
2269 among BSD systems today. So the kinds of portability that are absolutely | 2503 are the form of GNU that is popular. |
2270 necessary are quite limited. | 2504 |
2271 | 2505 Beyond that, it is good to support the other free operating systems |
2272 But many users do run GNU software on non-GNU Unix or Unix-like systems. | 2506 (*BSD), and it is nice to support other Unix-like systems if you want |
2273 So supporting a variety of Unix-like systems is desirable, although not | 2507 to. Supporting a variety of Unix-like systems is desirable, although |
2274 paramount. | 2508 not paramount. It is usually not too hard, so you may as well do it. |
2275 | 2509 But you don't have to consider it an obligation, if it does turn out to |
2510 be hard. | |
2511 | |
2512 @pindex autoconf | |
2276 The easiest way to achieve portability to most Unix-like systems is to | 2513 The easiest way to achieve portability to most Unix-like systems is to |
2277 use Autoconf. It's unlikely that your program needs to know more | 2514 use Autoconf. It's unlikely that your program needs to know more |
2278 information about the host platform than Autoconf can provide, simply | 2515 information about the host platform than Autoconf can provide, simply |
2279 because most of the programs that need such knowledge have already been | 2516 because most of the programs that need such knowledge have already been |
2280 written. | 2517 written. |
2281 | 2518 |
2282 Avoid using the format of semi-internal data bases (e.g., directories) | 2519 Avoid using the format of semi-internal data bases (e.g., directories) |
2283 when there is a higher-level alternative (@code{readdir}). | 2520 when there is a higher-level alternative (@code{readdir}). |
2284 | 2521 |
2522 @cindex non-@sc{posix} systems, and portability | |
2285 As for systems that are not like Unix, such as MSDOS, Windows, the | 2523 As for systems that are not like Unix, such as MSDOS, Windows, the |
2286 Macintosh, VMS, and MVS, supporting them is often a lot of work. When | 2524 Macintosh, VMS, and MVS, supporting them is often a lot of work. When |
2287 that is the case, it is better to spend your time adding features that | 2525 that is the case, it is better to spend your time adding features that |
2288 will be useful on GNU and GNU/Linux, rather than on supporting other | 2526 will be useful on GNU and GNU/Linux, rather than on supporting other |
2289 incompatible systems. | 2527 incompatible systems. |
2290 | 2528 |
2529 It is a good idea to define the ``feature test macro'' | |
2530 @code{_GNU_SOURCE} when compiling your C files. When you compile on GNU | |
2531 or GNU/Linux, this will enable the declarations of GNU library extension | |
2532 functions, and that will usually give you a compiler error message if | |
2533 you define the same function names in some other way in your program. | |
2534 (You don't have to actually @emph{use} these functions, if you prefer | |
2535 to make the program more portable to other systems.) | |
2536 | |
2537 But whether or not you use these GNU extensions, you should avoid | |
2538 using their names for any other meanings. Doing so would make it hard | |
2539 to move your code into other GNU programs. | |
2540 | |
2291 @node CPU Portability | 2541 @node CPU Portability |
2292 @section Portability between @sc{cpu}s | 2542 @section Portability between @sc{cpu}s |
2293 | 2543 |
2544 @cindex data types, and portability | |
2545 @cindex portability, and data types | |
2294 Even GNU systems will differ because of differences among @sc{cpu} | 2546 Even GNU systems will differ because of differences among @sc{cpu} |
2295 types---for example, difference in byte ordering and alignment | 2547 types---for example, difference in byte ordering and alignment |
2296 requirements. It is absolutely essential to handle these differences. | 2548 requirements. It is absolutely essential to handle these differences. |
2297 However, don't make any effort to cater to the possibility that an | 2549 However, don't make any effort to cater to the possibility that an |
2298 @code{int} will be less than 32 bits. We don't support 16-bit machines | 2550 @code{int} will be less than 32 bits. We don't support 16-bit machines |
2299 in GNU. | 2551 in GNU. |
2300 | 2552 |
2553 Similarly, don't make any effort to cater to the possibility that | |
2554 @code{long} will be smaller than predefined types like @code{size_t}. | |
2555 For example, the following code is ok: | |
2556 | |
2557 @example | |
2558 printf ("size = %lu\n", (unsigned long) sizeof array); | |
2559 printf ("diff = %ld\n", (long) (pointer2 - pointer1)); | |
2560 @end example | |
2561 | |
2562 1989 Standard C requires this to work, and we know of only one | |
2563 counterexample: 64-bit programs on Microsoft Windows IA-64. We will | |
2564 leave it to those who want to port GNU programs to that environment | |
2565 to figure out how to do it. | |
2566 | |
2567 Predefined file-size types like @code{off_t} are an exception: they are | |
2568 longer than @code{long} on many platforms, so code like the above won't | |
2569 work with them. One way to print an @code{off_t} value portably is to | |
2570 print its digits yourself, one by one. | |
2571 | |
2301 Don't assume that the address of an @code{int} object is also the | 2572 Don't assume that the address of an @code{int} object is also the |
2302 address of its least-significant byte. This is false on big-endian | 2573 address of its least-significant byte. This is false on big-endian |
2303 machines. Thus, don't make the following mistake: | 2574 machines. Thus, don't make the following mistake: |
2304 | 2575 |
2305 @example | 2576 @example |
2310 @end example | 2581 @end example |
2311 | 2582 |
2312 When calling functions, you need not worry about the difference between | 2583 When calling functions, you need not worry about the difference between |
2313 pointers of various types, or between pointers and integers. On most | 2584 pointers of various types, or between pointers and integers. On most |
2314 machines, there's no difference anyway. As for the few machines where | 2585 machines, there's no difference anyway. As for the few machines where |
2315 there is a difference, all of them support @sc{ansi} C, so you can use | 2586 there is a difference, all of them support Standard C prototypes, so you can |
2316 prototypes (conditionalized to be active only in @sc{ansi} C) to make | 2587 use prototypes (perhaps conditionalized to be active only in Standard C) |
2317 the code work on those systems. | 2588 to make the code work on those systems. |
2318 | 2589 |
2319 In certain cases, it is ok to pass integer and pointer arguments | 2590 In certain cases, it is ok to pass integer and pointer arguments |
2320 indiscriminately to the same function, and use no prototype on any | 2591 indiscriminately to the same function, and use no prototype on any |
2321 system. For example, many GNU programs have error-reporting functions | 2592 system. For example, many GNU programs have error-reporting functions |
2322 that pass their arguments along to @code{printf} and friends: | 2593 that pass their arguments along to @code{printf} and friends: |
2331 @} | 2602 @} |
2332 @end example | 2603 @end example |
2333 | 2604 |
2334 @noindent | 2605 @noindent |
2335 In practice, this works on all machines, since a pointer is generally | 2606 In practice, this works on all machines, since a pointer is generally |
2336 the widest possible kind of argument, and it is much simpler than any | 2607 the widest possible kind of argument; it is much simpler than any |
2337 ``correct'' alternative. Be sure @emph{not} to use a prototype for such | 2608 ``correct'' alternative. Be sure @emph{not} to use a prototype for such |
2338 functions. | 2609 functions. |
2339 | 2610 |
2340 However, avoid casting pointers to integers unless you really need to. | 2611 If you have decided to use Standard C, then you can instead define |
2341 Outside of special situations, such casts greatly reduce portability, | 2612 @code{error} using @file{stdarg.h}, and pass the arguments along to |
2342 and in most programs they are easy to avoid. In the cases where casting | 2613 @code{vfprintf}. |
2343 pointers to integers is essential---such as, a Lisp interpreter which | 2614 |
2344 stores type information as well as an address in one word---it is ok to | 2615 @cindex casting pointers to integers |
2345 do it, but you'll have to make explicit provisions to handle different | 2616 Avoid casting pointers to integers if you can. Such casts greatly |
2346 word sizes. | 2617 reduce portability, and in most programs they are easy to avoid. In the |
2618 cases where casting pointers to integers is essential---such as, a Lisp | |
2619 interpreter which stores type information as well as an address in one | |
2620 word---you'll have to make explicit provisions to handle different word | |
2621 sizes. You will also need to make provision for systems in which the | |
2622 normal range of addresses you can get from @code{malloc} starts far away | |
2623 from zero. | |
2347 | 2624 |
2348 @node System Functions | 2625 @node System Functions |
2349 @section Calling System Functions | 2626 @section Calling System Functions |
2350 | 2627 @cindex library functions, and portability |
2351 C implementations differ substantially. @sc{ansi} C reduces but does not | 2628 @cindex portability, and library functions |
2352 eliminate the incompatibilities; meanwhile, many users wish to compile | 2629 |
2353 GNU software with pre-@sc{ansi} compilers. This chapter gives | 2630 C implementations differ substantially. Standard C reduces but does |
2354 recommendations for how to use the more or less standard C library | 2631 not eliminate the incompatibilities; meanwhile, many GNU packages still |
2355 functions to avoid unnecessary loss of portability. | 2632 support pre-standard compilers because this is not hard to do. This |
2633 chapter gives recommendations for how to use the more-or-less standard C | |
2634 library functions to avoid unnecessary loss of portability. | |
2356 | 2635 |
2357 @itemize @bullet | 2636 @itemize @bullet |
2358 @item | 2637 @item |
2359 Don't use the value of @code{sprintf}. It returns the number of | 2638 Don't use the return value of @code{sprintf}. It returns the number of |
2360 characters written on some systems, but not on all systems. | 2639 characters written on some systems, but not on all systems. |
2640 | |
2641 @item | |
2642 Be aware that @code{vfprintf} is not always available. | |
2361 | 2643 |
2362 @item | 2644 @item |
2363 @code{main} should be declared to return type @code{int}. It should | 2645 @code{main} should be declared to return type @code{int}. It should |
2364 terminate either by calling @code{exit} or by returning the integer | 2646 terminate either by calling @code{exit} or by returning the integer |
2365 status code; make sure it cannot ever return an undefined value. | 2647 status code; make sure it cannot ever return an undefined value. |
2366 | 2648 |
2649 @cindex declaration for system functions | |
2367 @item | 2650 @item |
2368 Don't declare system functions explicitly. | 2651 Don't declare system functions explicitly. |
2369 | 2652 |
2370 Almost any declaration for a system function is wrong on some system. | 2653 Almost any declaration for a system function is wrong on some system. |
2371 To minimize conflicts, leave it to the system header files to declare | 2654 To minimize conflicts, leave it to the system header files to declare |
2378 theoretical. By contrast, actual declarations have frequently caused | 2661 theoretical. By contrast, actual declarations have frequently caused |
2379 actual conflicts. | 2662 actual conflicts. |
2380 | 2663 |
2381 @item | 2664 @item |
2382 If you must declare a system function, don't specify the argument types. | 2665 If you must declare a system function, don't specify the argument types. |
2383 Use an old-style declaration, not an @sc{ansi} prototype. The more you | 2666 Use an old-style declaration, not a Standard C prototype. The more you |
2384 specify about the function, the more likely a conflict. | 2667 specify about the function, the more likely a conflict. |
2385 | 2668 |
2386 @item | 2669 @item |
2387 In particular, don't unconditionally declare @code{malloc} or | 2670 In particular, don't unconditionally declare @code{malloc} or |
2388 @code{realloc}. | 2671 @code{realloc}. |
2400 exceptional systems (mostly 64-bit machines), you can use | 2683 exceptional systems (mostly 64-bit machines), you can use |
2401 @strong{conditionalized} declarations of @code{malloc} and | 2684 @strong{conditionalized} declarations of @code{malloc} and |
2402 @code{realloc}---or put these declarations in configuration files | 2685 @code{realloc}---or put these declarations in configuration files |
2403 specific to those systems. | 2686 specific to those systems. |
2404 | 2687 |
2688 @cindex string library functions | |
2405 @item | 2689 @item |
2406 The string functions require special treatment. Some Unix systems have | 2690 The string functions require special treatment. Some Unix systems have |
2407 a header file @file{string.h}; others have @file{strings.h}. Neither | 2691 a header file @file{string.h}; others have @file{strings.h}. Neither |
2408 file name is portable. There are two things you can do: use Autoconf to | 2692 file name is portable. There are two things you can do: use Autoconf to |
2409 figure out which file to include, or don't include either file. | 2693 figure out which file to include, or don't include either file. |
2410 | 2694 |
2411 @item | 2695 @item |
2412 If you don't include either strings file, you can't get declarations for | 2696 If you don't include either strings file, you can't get declarations for |
2413 the string functions from the header file in the usual way. | 2697 the string functions from the header file in the usual way. |
2414 | 2698 |
2415 That causes less of a problem than you might think. The newer @sc{ansi} | 2699 That causes less of a problem than you might think. The newer standard |
2416 string functions should be avoided anyway because many systems still | 2700 string functions should be avoided anyway because many systems still |
2417 don't support them. The string functions you can use are these: | 2701 don't support them. The string functions you can use are these: |
2418 | 2702 |
2419 @example | 2703 @example |
2420 strcpy strncpy strcat strncat | 2704 strcpy strncpy strcat strncat |
2440 @code{strchr} and @code{strrchr}. Some systems support both pairs of | 2724 @code{strchr} and @code{strrchr}. Some systems support both pairs of |
2441 names, but neither pair works on all systems. | 2725 names, but neither pair works on all systems. |
2442 | 2726 |
2443 You should pick a single pair of names and use it throughout your | 2727 You should pick a single pair of names and use it throughout your |
2444 program. (Nowadays, it is better to choose @code{strchr} and | 2728 program. (Nowadays, it is better to choose @code{strchr} and |
2445 @code{strrchr} for new programs, since those are the standard @sc{ansi} | 2729 @code{strrchr} for new programs, since those are the standard |
2446 names.) Declare both of those names as functions returning @code{char | 2730 names.) Declare both of those names as functions returning @code{char |
2447 *}. On systems which don't support those names, define them as macros | 2731 *}. On systems which don't support those names, define them as macros |
2448 in terms of the other pair. For example, here is what to put at the | 2732 in terms of the other pair. For example, here is what to put at the |
2449 beginning of your file (or in a header) if you want to use the names | 2733 beginning of your file (or in a header) if you want to use the names |
2450 @code{strchr} and @code{strrchr} throughout: | 2734 @code{strchr} and @code{strrchr} throughout: |
2466 macros defined in systems where the corresponding functions exist. | 2750 macros defined in systems where the corresponding functions exist. |
2467 One way to get them properly defined is to use Autoconf. | 2751 One way to get them properly defined is to use Autoconf. |
2468 | 2752 |
2469 @node Internationalization | 2753 @node Internationalization |
2470 @section Internationalization | 2754 @section Internationalization |
2471 | 2755 @cindex internationalization |
2756 | |
2757 @pindex gettext | |
2472 GNU has a library called GNU gettext that makes it easy to translate the | 2758 GNU has a library called GNU gettext that makes it easy to translate the |
2473 messages in a program into various languages. You should use this | 2759 messages in a program into various languages. You should use this |
2474 library in every program. Use English for the messages as they appear | 2760 library in every program. Use English for the messages as they appear |
2475 in the program, and let gettext provide the way to translate them into | 2761 in the program, and let gettext provide the way to translate them into |
2476 other languages. | 2762 other languages. |
2493 name} for the package. The text domain name is used to separate the | 2779 name} for the package. The text domain name is used to separate the |
2494 translations for this package from the translations for other packages. | 2780 translations for this package from the translations for other packages. |
2495 Normally, the text domain name should be the same as the name of the | 2781 Normally, the text domain name should be the same as the name of the |
2496 package---for example, @samp{fileutils} for the GNU file utilities. | 2782 package---for example, @samp{fileutils} for the GNU file utilities. |
2497 | 2783 |
2784 @cindex message text, and internationalization | |
2498 To enable gettext to work well, avoid writing code that makes | 2785 To enable gettext to work well, avoid writing code that makes |
2499 assumptions about the structure of words or sentences. When you want | 2786 assumptions about the structure of words or sentences. When you want |
2500 the precise text of a sentence to vary depending on the data, use two or | 2787 the precise text of a sentence to vary depending on the data, use two or |
2501 more alternative string constants each containing a complete sentences, | 2788 more alternative string constants each containing a complete sentences, |
2502 rather than inserting conditionalized words or phrases into a single | 2789 rather than inserting conditionalized words or phrases into a single |
2564 : "# Implicit rule search has not been done.\n"); | 2851 : "# Implicit rule search has not been done.\n"); |
2565 @end example | 2852 @end example |
2566 | 2853 |
2567 @node Mmap | 2854 @node Mmap |
2568 @section Mmap | 2855 @section Mmap |
2856 @findex mmap | |
2569 | 2857 |
2570 Don't assume that @code{mmap} either works on all files or fails | 2858 Don't assume that @code{mmap} either works on all files or fails |
2571 for all files. It may work on some files and fail on others. | 2859 for all files. It may work on some files and fail on others. |
2572 | 2860 |
2573 The proper way to use @code{mmap} is to try it on the specific file for | 2861 The proper way to use @code{mmap} is to try it on the specific file for |
2580 @code{mmap}, but some do not. It is important to make programs handle | 2868 @code{mmap}, but some do not. It is important to make programs handle |
2581 all these kinds of files. | 2869 all these kinds of files. |
2582 | 2870 |
2583 @node Documentation | 2871 @node Documentation |
2584 @chapter Documenting Programs | 2872 @chapter Documenting Programs |
2873 @cindex documentation | |
2874 | |
2875 A GNU program should ideally come with full free documentation, adequate | |
2876 for both reference and tutorial purposes. If the package can be | |
2877 programmed or extended, the documentation should cover programming or | |
2878 extending it, as well as just using it. | |
2585 | 2879 |
2586 @menu | 2880 @menu |
2587 * GNU Manuals:: Writing proper manuals. | 2881 * GNU Manuals:: Writing proper manuals. |
2882 * Doc Strings and Manuals:: Compiling doc strings doesn't make a manual. | |
2588 * Manual Structure Details:: Specific structure conventions. | 2883 * Manual Structure Details:: Specific structure conventions. |
2589 * License for Manuals:: Writing the distribution terms for a manual. | 2884 * License for Manuals:: Writing the distribution terms for a manual. |
2885 * Manual Credits:: Giving credit to documentation contributors. | |
2886 * Printed Manuals:: Mentioning the printed manual. | |
2590 * NEWS File:: NEWS files supplement manuals. | 2887 * NEWS File:: NEWS files supplement manuals. |
2591 * Change Logs:: Recording Changes | 2888 * Change Logs:: Recording Changes |
2592 * Man Pages:: Man pages are secondary. | 2889 * Man Pages:: Man pages are secondary. |
2593 * Reading other Manuals:: How far you can go in learning | 2890 * Reading other Manuals:: How far you can go in learning |
2594 from other manuals. | 2891 from other manuals. |
2595 @end menu | 2892 @end menu |
2596 | 2893 |
2597 @node GNU Manuals | 2894 @node GNU Manuals |
2598 @section GNU Manuals | 2895 @section GNU Manuals |
2599 | 2896 |
2600 The preferred way to document part of the GNU system is to write a | 2897 The preferred document format for the GNU system is the Texinfo |
2601 manual in the Texinfo formatting language. This makes it possible to | 2898 formatting language. Every GNU package should (ideally) have |
2602 produce a good quality formatted book, using @TeX{}, and to generate an | 2899 documentation in Texinfo both for reference and for learners. Texinfo |
2603 Info file. It is also possible to generate HTML output from Texinfo | 2900 makes it possible to produce a good quality formatted book, using |
2604 source. See the Texinfo manual, either the hardcopy, or the on-line | 2901 @TeX{}, and to generate an Info file. It is also possible to generate |
2605 version available through @code{info} or the Emacs Info subsystem | 2902 HTML output from Texinfo source. See the Texinfo manual, either the |
2606 (@kbd{C-h i}). | 2903 hardcopy, or the on-line version available through @code{info} or the |
2904 Emacs Info subsystem (@kbd{C-h i}). | |
2905 | |
2906 Nowadays some other formats such as Docbook and Sgmltexi can be | |
2907 converted automatically into Texinfo. It is ok to produce the Texinfo | |
2908 documentation by conversion this way, as long as it gives good results. | |
2607 | 2909 |
2608 Programmers often find it most natural to structure the documentation | 2910 Programmers often find it most natural to structure the documentation |
2609 following the structure of the implementation, which they know. But | 2911 following the structure of the implementation, which they know. But |
2610 this structure is not necessarily good for explaining how to use the | 2912 this structure is not necessarily good for explaining how to use the |
2611 program; it may be irrelevant and confusing for a user. | 2913 program; it may be irrelevant and confusing for a user. |
2630 instead of a manual for @code{diff} and a manual for @code{diff3}, we | 2932 instead of a manual for @code{diff} and a manual for @code{diff3}, we |
2631 have one manual for ``comparison of files'' which covers both of those | 2933 have one manual for ``comparison of files'' which covers both of those |
2632 programs, as well as @code{cmp}. By documenting these programs | 2934 programs, as well as @code{cmp}. By documenting these programs |
2633 together, we can make the whole subject clearer. | 2935 together, we can make the whole subject clearer. |
2634 | 2936 |
2635 The manual which discusses a program should document all of the | 2937 The manual which discusses a program should certainly document all of |
2636 program's command-line options and all of its commands. It should give | 2938 the program's command-line options and all of its commands. It should |
2637 examples of their use. But don't organize the manual as a list of | 2939 give examples of their use. But don't organize the manual as a list of |
2638 features. Instead, organize it logically, by subtopics. Address the | 2940 features. Instead, organize it logically, by subtopics. Address the |
2639 questions that a user will ask when thinking about the job that the | 2941 questions that a user will ask when thinking about the job that the |
2640 program does. | 2942 program does. |
2641 | 2943 |
2642 In general, a GNU manual should serve both as tutorial and reference. | 2944 In general, a GNU manual should serve both as tutorial and reference. |
2657 If necessary, add extra chapters at the beginning of the manual which | 2959 If necessary, add extra chapters at the beginning of the manual which |
2658 are purely tutorial and cover the basics of the subject. These provide | 2960 are purely tutorial and cover the basics of the subject. These provide |
2659 the framework for a beginner to understand the rest of the manual. The | 2961 the framework for a beginner to understand the rest of the manual. The |
2660 Bison manual provides a good example of how to do this. | 2962 Bison manual provides a good example of how to do this. |
2661 | 2963 |
2964 To serve as a reference, a manual should have an Index that list all the | |
2965 functions, variables, options, and important concepts that are part of | |
2966 the program. One combined Index should do for a short manual, but | |
2967 sometimes for a complex package it is better to use multiple indices. | |
2968 The Texinfo manual includes advice on preparing good index entries, see | |
2969 @ref{Index Entries, , Making Index Entries, texinfo, The GNU Texinfo | |
2970 Manual}, and see @ref{Indexing Commands, , Defining the Entries of an | |
2971 Index, texinfo, The GNU Texinfo manual}. | |
2972 | |
2662 Don't use Unix man pages as a model for how to write GNU documentation; | 2973 Don't use Unix man pages as a model for how to write GNU documentation; |
2663 most of them are terse, badly structured, and give inadequate | 2974 most of them are terse, badly structured, and give inadequate |
2664 explanation of the underlying concepts. (There are, of course | 2975 explanation of the underlying concepts. (There are, of course, some |
2665 exceptions.) Also Unix man pages use a particular format which is | 2976 exceptions.) Also, Unix man pages use a particular format which is |
2666 different from what we use in GNU manuals. | 2977 different from what we use in GNU manuals. |
2667 | 2978 |
2668 Please include an email address in the manual for where to report | 2979 Please include an email address in the manual for where to report |
2669 bugs @emph{in the manual}. | 2980 bugs @emph{in the manual}. |
2670 | 2981 |
2672 documentation; use ``file name'' (two words) instead. We use the term | 2983 documentation; use ``file name'' (two words) instead. We use the term |
2673 ``path'' only for search paths, which are lists of directory names. | 2984 ``path'' only for search paths, which are lists of directory names. |
2674 | 2985 |
2675 Please do not use the term ``illegal'' to refer to erroneous input to a | 2986 Please do not use the term ``illegal'' to refer to erroneous input to a |
2676 computer program. Please use ``invalid'' for this, and reserve the term | 2987 computer program. Please use ``invalid'' for this, and reserve the term |
2677 ``illegal'' for violations of law. | 2988 ``illegal'' for activities punishable by law. |
2989 | |
2990 @node Doc Strings and Manuals | |
2991 @section Doc Strings and Manuals | |
2992 | |
2993 Some programming systems, such as Emacs, provide a documentation string | |
2994 for each function, command or variable. You may be tempted to write a | |
2995 reference manual by compiling the documentation strings and writing a | |
2996 little additional text to go around them---but you must not do it. That | |
2997 approach is a fundamental mistake. The text of well-written | |
2998 documentation strings will be entirely wrong for a manual. | |
2999 | |
3000 A documentation string needs to stand alone---when it appears on the | |
3001 screen, there will be no other text to introduce or explain it. | |
3002 Meanwhile, it can be rather informal in style. | |
3003 | |
3004 The text describing a function or variable in a manual must not stand | |
3005 alone; it appears in the context of a section or subsection. Other text | |
3006 at the beginning of the section should explain some of the concepts, and | |
3007 should often make some general points that apply to several functions or | |
3008 variables. The previous descriptions of functions and variables in the | |
3009 section will also have given information about the topic. A description | |
3010 written to stand alone would repeat some of that information; this | |
3011 redundance looks bad. Meanwhile, the informality that is acceptable in | |
3012 a documentation string is totally unacceptable in a manual. | |
3013 | |
3014 The only good way to use documentation strings in writing a good manual | |
3015 is to use them as a source of information for writing good text. | |
2678 | 3016 |
2679 @node Manual Structure Details | 3017 @node Manual Structure Details |
2680 @section Manual Structure Details | 3018 @section Manual Structure Details |
3019 @cindex manual structure | |
2681 | 3020 |
2682 The title page of the manual should state the version of the programs or | 3021 The title page of the manual should state the version of the programs or |
2683 packages documented in the manual. The Top node of the manual should | 3022 packages documented in the manual. The Top node of the manual should |
2684 also contain this information. If the manual is changing more | 3023 also contain this information. If the manual is changing more |
2685 frequently than or independent of the program, also state a version | 3024 frequently than or independent of the program, also state a version |
2695 | 3034 |
2696 Alternatively, put a menu item in some menu whose item name fits one of | 3035 Alternatively, put a menu item in some menu whose item name fits one of |
2697 the above patterns. This identifies the node which that item points to | 3036 the above patterns. This identifies the node which that item points to |
2698 as the node for this purpose, regardless of the node's actual name. | 3037 as the node for this purpose, regardless of the node's actual name. |
2699 | 3038 |
2700 There will be automatic features for specifying a program name and | 3039 The @samp{--usage} feature of the Info reader looks for such a node |
2701 quickly reading just this part of its manual. | 3040 or menu item in order to find the relevant text, so it is essential |
3041 for every Texinfo file to have one. | |
2702 | 3042 |
2703 If one manual describes several programs, it should have such a node for | 3043 If one manual describes several programs, it should have such a node for |
2704 each program described. | 3044 each program described in the manual. |
2705 | 3045 |
2706 @node License for Manuals | 3046 @node License for Manuals |
2707 @section License for Manuals | 3047 @section License for Manuals |
2708 | 3048 @cindex license for manuals |
2709 If the manual contains a copy of the GNU GPL or GNU LGPL, or if it | 3049 |
2710 contains chapters that make political or personal statements, please | 3050 Please use the GNU Free Documentation License for all GNU manuals that |
2711 copy the distribution terms of the GNU Emacs Manual, and adapt it by | 3051 are more than a few pages long. Likewise for a collection of short |
2712 modifying appropriately the list of special chapters that may not be | 3052 documents---you only need one copy of the GNU FDL for the whole |
2713 modified or deleted. | 3053 collection. For a single short document, you can use a very permissive |
2714 | 3054 non-copyleft license, to avoid taking up space with a long license. |
2715 If the manual does not contain any such chapters, then imitate the | 3055 |
2716 simpler distribution terms of the Texinfo manual. | 3056 See @uref{http://www.gnu.org/copyleft/fdl-howto.html} for more explanation |
3057 of how to employ the GFDL. | |
3058 | |
3059 Note that it is not obligatory to include a copy of the GNU GPL or GNU | |
3060 LGPL in a manual whose license is neither the GPL nor the LGPL. It can | |
3061 be a good idea to include the program's license in a large manual; in a | |
3062 short manual, whose size would be increased considerably by including | |
3063 the program's license, it is probably better not to include it. | |
3064 | |
3065 @node Manual Credits | |
3066 @section Manual Credits | |
3067 @cindex credits for manuals | |
3068 | |
3069 Please credit the principal human writers of the manual as the authors, | |
3070 on the title page of the manual. If a company sponsored the work, thank | |
3071 the company in a suitable place in the manual, but do not cite the | |
3072 company as an author. | |
3073 | |
3074 @node Printed Manuals | |
3075 @section Printed Manuals | |
3076 | |
3077 The FSF publishes some GNU manuals in printed form. To encourage sales | |
3078 of these manuals, the on-line versions of the manual should mention at | |
3079 the very start that the printed manual is available and should point at | |
3080 information for getting it---for instance, with a link to the page | |
3081 @url{http://www.gnu.org/order/order.html}. This should not be included | |
3082 in the printed manual, though, because there it is redundant. | |
3083 | |
3084 It is also useful to explain in the on-line forms of the manual how the | |
3085 user can print out the manual from the sources. | |
2717 | 3086 |
2718 @node NEWS File | 3087 @node NEWS File |
2719 @section The NEWS File | 3088 @section The NEWS File |
3089 @cindex @file{NEWS} file | |
2720 | 3090 |
2721 In addition to its manual, the package should have a file named | 3091 In addition to its manual, the package should have a file named |
2722 @file{NEWS} which contains a list of user-visible changes worth | 3092 @file{NEWS} which contains a list of user-visible changes worth |
2723 mentioning. In each new release, add items to the front of the file and | 3093 mentioning. In each new release, add items to the front of the file and |
2724 identify the version they pertain to. Don't discard old items; leave | 3094 identify the version they pertain to. Don't discard old items; leave |
2729 into a file named @file{ONEWS} and put a note at the end referring the | 3099 into a file named @file{ONEWS} and put a note at the end referring the |
2730 user to that file. | 3100 user to that file. |
2731 | 3101 |
2732 @node Change Logs | 3102 @node Change Logs |
2733 @section Change Logs | 3103 @section Change Logs |
3104 @cindex change logs | |
2734 | 3105 |
2735 Keep a change log to describe all the changes made to program source | 3106 Keep a change log to describe all the changes made to program source |
2736 files. The purpose of this is so that people investigating bugs in the | 3107 files. The purpose of this is so that people investigating bugs in the |
2737 future will know about the changes that might have introduced the bug. | 3108 future will know about the changes that might have introduced the bug. |
2738 Often a new bug can be found by looking at what was recently changed. | 3109 Often a new bug can be found by looking at what was recently changed. |
2743 @menu | 3114 @menu |
2744 * Change Log Concepts:: | 3115 * Change Log Concepts:: |
2745 * Style of Change Logs:: | 3116 * Style of Change Logs:: |
2746 * Simple Changes:: | 3117 * Simple Changes:: |
2747 * Conditional Changes:: | 3118 * Conditional Changes:: |
3119 * Indicating the Part Changed:: | |
2748 @end menu | 3120 @end menu |
2749 | 3121 |
2750 @node Change Log Concepts | 3122 @node Change Log Concepts |
2751 @subsection Change Log Concepts | 3123 @subsection Change Log Concepts |
2752 | 3124 |
2783 of the changed functions, variables or whatever, followed by a colon. | 3155 of the changed functions, variables or whatever, followed by a colon. |
2784 Then describe the changes you made to that function or variable. | 3156 Then describe the changes you made to that function or variable. |
2785 | 3157 |
2786 @node Style of Change Logs | 3158 @node Style of Change Logs |
2787 @subsection Style of Change Logs | 3159 @subsection Style of Change Logs |
2788 | 3160 @cindex change logs, style |
2789 Here are some examples of change log entries: | 3161 |
2790 | 3162 Here are some simple examples of change log entries, starting with the |
2791 @example | 3163 header line that says who made the change and when, followed by |
3164 descriptions of specific changes. (These examples are drawn from Emacs | |
3165 and GCC.) | |
3166 | |
3167 @example | |
3168 1998-08-17 Richard Stallman <rms@@gnu.org> | |
3169 | |
2792 * register.el (insert-register): Return nil. | 3170 * register.el (insert-register): Return nil. |
2793 (jump-to-register): Likewise. | 3171 (jump-to-register): Likewise. |
2794 | 3172 |
2795 * sort.el (sort-subr): Return nil. | 3173 * sort.el (sort-subr): Return nil. |
2796 | 3174 |
2817 Separate unrelated change log entries with blank lines. When two | 3195 Separate unrelated change log entries with blank lines. When two |
2818 entries represent parts of the same change, so that they work together, | 3196 entries represent parts of the same change, so that they work together, |
2819 then don't put blank lines between them. Then you can omit the file | 3197 then don't put blank lines between them. Then you can omit the file |
2820 name and the asterisk when successive entries are in the same file. | 3198 name and the asterisk when successive entries are in the same file. |
2821 | 3199 |
3200 Break long lists of function names by closing continued lines with | |
3201 @samp{)}, rather than @samp{,}, and opening the continuation with | |
3202 @samp{(} as in this example: | |
3203 | |
3204 @example | |
3205 * keyboard.c (menu_bar_items, tool_bar_items) | |
3206 (Fexecute_extended_command): Deal with `keymap' property. | |
3207 @end example | |
3208 | |
2822 @node Simple Changes | 3209 @node Simple Changes |
2823 @subsection Simple Changes | 3210 @subsection Simple Changes |
2824 | 3211 |
2825 Certain simple kinds of changes don't need much detail in the change | 3212 Certain simple kinds of changes don't need much detail in the change |
2826 log. | 3213 log. |
2827 | 3214 |
2828 When you change the calling sequence of a function in a simple fashion, | 3215 When you change the calling sequence of a function in a simple fashion, |
2829 and you change all the callers of the function, there is no need to make | 3216 and you change all the callers of the function to use the new calling |
2830 individual entries for all the callers that you changed. Just write in | 3217 sequence, there is no need to make individual entries for all the |
2831 the entry for the function being called, ``All callers changed.'' | 3218 callers that you changed. Just write in the entry for the function |
3219 being called, ``All callers changed''---like this: | |
2832 | 3220 |
2833 @example | 3221 @example |
2834 * keyboard.c (Fcommand_execute): New arg SPECIAL. | 3222 * keyboard.c (Fcommand_execute): New arg SPECIAL. |
2835 All callers changed. | 3223 All callers changed. |
2836 @end example | 3224 @end example |
2846 the history of the erroneous passage; it is enough to compare what the | 3234 the history of the erroneous passage; it is enough to compare what the |
2847 documentation says with the way the program actually works. | 3235 documentation says with the way the program actually works. |
2848 | 3236 |
2849 @node Conditional Changes | 3237 @node Conditional Changes |
2850 @subsection Conditional Changes | 3238 @subsection Conditional Changes |
3239 @cindex conditional changes, and change logs | |
3240 @cindex change logs, conditional changes | |
2851 | 3241 |
2852 C programs often contain compile-time @code{#if} conditionals. Many | 3242 C programs often contain compile-time @code{#if} conditionals. Many |
2853 changes are conditional; sometimes you add a new definition which is | 3243 changes are conditional; sometimes you add a new definition which is |
2854 entirely contained in a conditional. It is very useful to indicate in | 3244 entirely contained in a conditional. It is very useful to indicate in |
2855 the change log the conditions for which the change applies. | 3245 the change log the conditions for which the change applies. |
2885 | 3275 |
2886 @example | 3276 @example |
2887 (gethostname) [!HAVE_SOCKETS]: Replace with winsock version. | 3277 (gethostname) [!HAVE_SOCKETS]: Replace with winsock version. |
2888 @end example | 3278 @end example |
2889 | 3279 |
3280 @node Indicating the Part Changed | |
3281 @subsection Indicating the Part Changed | |
3282 | |
3283 Indicate the part of a function which changed by using angle brackets | |
3284 enclosing an indication of what the changed part does. Here is an entry | |
3285 for a change in the part of the function @code{sh-while-getopts} that | |
3286 deals with @code{sh} commands: | |
3287 | |
3288 @example | |
3289 * progmodes/sh-script.el (sh-while-getopts) <sh>: Handle case that | |
3290 user-specified option string is empty. | |
3291 @end example | |
3292 | |
3293 | |
2890 @node Man Pages | 3294 @node Man Pages |
2891 @section Man Pages | 3295 @section Man Pages |
3296 @cindex man pages | |
2892 | 3297 |
2893 In the GNU project, man pages are secondary. It is not necessary or | 3298 In the GNU project, man pages are secondary. It is not necessary or |
2894 expected for every GNU program to have a man page, but some of them do. | 3299 expected for every GNU program to have a man page, but some of them do. |
2895 It's your choice whether to include a man page in your program. | 3300 It's your choice whether to include a man page in your program. |
2896 | 3301 |
2933 documentation. Copying from free documentation may be ok; please check | 3338 documentation. Copying from free documentation may be ok; please check |
2934 with the FSF about the individual case. | 3339 with the FSF about the individual case. |
2935 | 3340 |
2936 @node Managing Releases | 3341 @node Managing Releases |
2937 @chapter The Release Process | 3342 @chapter The Release Process |
3343 @cindex releasing | |
2938 | 3344 |
2939 Making a release is more than just bundling up your source files in a | 3345 Making a release is more than just bundling up your source files in a |
2940 tar file and putting it up for FTP. You should set up your software so | 3346 tar file and putting it up for FTP. You should set up your software so |
2941 that it can be configured to run on a variety of systems. Your Makefile | 3347 that it can be configured to run on a variety of systems. Your Makefile |
2942 should conform to the GNU standards described below, and your directory | 3348 should conform to the GNU standards described below, and your directory |
2950 * Releases:: Making Releases | 3356 * Releases:: Making Releases |
2951 @end menu | 3357 @end menu |
2952 | 3358 |
2953 @node Configuration | 3359 @node Configuration |
2954 @section How Configuration Should Work | 3360 @section How Configuration Should Work |
2955 | 3361 @cindex program configuration |
3362 | |
3363 @pindex configure | |
2956 Each GNU distribution should come with a shell script named | 3364 Each GNU distribution should come with a shell script named |
2957 @code{configure}. This script is given arguments which describe the | 3365 @code{configure}. This script is given arguments which describe the |
2958 kind of machine and system you want to compile the program for. | 3366 kind of machine and system you want to compile the program for. |
2959 | 3367 |
2960 The @code{configure} script must record the configuration options so | 3368 The @code{configure} script must record the configuration options so |
3018 | 3426 |
3019 The @code{configure} script needs to be able to decode all plausible | 3427 The @code{configure} script needs to be able to decode all plausible |
3020 alternatives for how to describe a machine. Thus, @samp{sun3-sunos4.1} | 3428 alternatives for how to describe a machine. Thus, @samp{sun3-sunos4.1} |
3021 would be a valid alias. For many programs, @samp{vax-dec-ultrix} would | 3429 would be a valid alias. For many programs, @samp{vax-dec-ultrix} would |
3022 be an alias for @samp{vax-dec-bsd}, simply because the differences | 3430 be an alias for @samp{vax-dec-bsd}, simply because the differences |
3023 between Ultrix and BSD are rarely noticeable, but a few programs | 3431 between Ultrix and @sc{bsd} are rarely noticeable, but a few programs |
3024 might need to distinguish them. | 3432 might need to distinguish them. |
3025 @c Real 4.4BSD now runs on some Suns. | 3433 @c Real 4.4BSD now runs on some Suns. |
3026 | 3434 |
3027 There is a shell script called @file{config.sub} that you can use | 3435 There is a shell script called @file{config.sub} that you can use |
3028 as a subroutine to validate system types and canonicalize aliases. | 3436 as a subroutine to validate system types and canonicalize aliases. |
3029 | 3437 |
3438 @cindex optional features, configure-time | |
3030 Other options are permitted to specify in more detail the software | 3439 Other options are permitted to specify in more detail the software |
3031 or hardware present on the machine, and include or exclude optional | 3440 or hardware present on the machine, and include or exclude optional |
3032 parts of the package: | 3441 parts of the package: |
3033 | 3442 |
3034 @table @samp | 3443 @table @samp |
3060 @samp{x-toolkit}. | 3469 @samp{x-toolkit}. |
3061 | 3470 |
3062 Do not use a @samp{--with} option to specify the file name to use to | 3471 Do not use a @samp{--with} option to specify the file name to use to |
3063 find certain files. That is outside the scope of what @samp{--with} | 3472 find certain files. That is outside the scope of what @samp{--with} |
3064 options are for. | 3473 options are for. |
3065 | |
3066 @item --nfp | |
3067 The target machine has no floating point processor. | |
3068 | |
3069 @item --gas | |
3070 The target machine assembler is GAS, the GNU assembler. | |
3071 This is obsolete; users should use @samp{--with-gnu-as} instead. | |
3072 | |
3073 @item --x | |
3074 The target machine has the X Window System installed. | |
3075 This is obsolete; users should use @samp{--with-x} instead. | |
3076 @end table | 3474 @end table |
3077 | 3475 |
3078 All @code{configure} scripts should accept all of these ``detail'' | 3476 All @code{configure} scripts should accept all of these ``detail'' |
3079 options, whether or not they make any difference to the particular | 3477 options, whether or not they make any difference to the particular |
3080 package at hand. In particular, they should accept any option that | 3478 package at hand. In particular, they should accept any option that |
3086 are narrow: they @strong{do not} provide a place for any sort of option | 3484 are narrow: they @strong{do not} provide a place for any sort of option |
3087 you might think of. That is deliberate. We want to limit the possible | 3485 you might think of. That is deliberate. We want to limit the possible |
3088 configuration options in GNU software. We do not want GNU programs to | 3486 configuration options in GNU software. We do not want GNU programs to |
3089 have idiosyncratic configuration options. | 3487 have idiosyncratic configuration options. |
3090 | 3488 |
3091 Packages that perform part of the compilation process may support cross-compilation. | 3489 Packages that perform part of the compilation process may support |
3092 In such a case, the host and target machines for the program may be | 3490 cross-compilation. In such a case, the host and target machines for the |
3093 different. The @code{configure} script should normally treat the | 3491 program may be different. |
3094 specified type of system as both the host and the target, thus producing | 3492 |
3095 a program which works for the same type of machine that it runs on. | 3493 The @code{configure} script should normally treat the specified type of |
3096 | 3494 system as both the host and the target, thus producing a program which |
3097 The way to build a cross-compiler, cross-assembler, or what have you, is | 3495 works for the same type of machine that it runs on. |
3098 to specify the option @samp{--host=@var{hosttype}} when running | 3496 |
3099 @code{configure}. This specifies the host system without changing the | 3497 To configure a cross-compiler, cross-assembler, or what have you, you |
3100 type of target system. The syntax for @var{hosttype} is the same as | 3498 should specify a target different from the host, using the configure |
3101 described above. | 3499 option @samp{--target=@var{targettype}}. The syntax for |
3500 @var{targettype} is the same as for the host type. So the command would | |
3501 look like this: | |
3502 | |
3503 @example | |
3504 ./configure @var{hosttype} --target=@var{targettype} | |
3505 @end example | |
3506 | |
3507 Programs for which cross-operation is not meaningful need not accept the | |
3508 @samp{--target} option, because configuring an entire operating system for | |
3509 cross-operation is not a meaningful operation. | |
3102 | 3510 |
3103 Bootstrapping a cross-compiler requires compiling it on a machine other | 3511 Bootstrapping a cross-compiler requires compiling it on a machine other |
3104 than the host it will run on. Compilation packages accept a | 3512 than the host it will run on. Compilation packages accept a |
3105 configuration option @samp{--build=@var{hosttype}} for specifying the | 3513 configuration option @samp{--build=@var{buildtype}} for specifying the |
3106 configuration on which you will compile them, in case that is different | 3514 configuration on which you will compile them, but the configure script |
3107 from the host. | 3515 should normally guess the build machine type (using |
3108 | 3516 @file{config.guess}), so this option is probably not necessary. The |
3109 Programs for which cross-operation is not meaningful need not accept the | 3517 host and target types normally default from the build type, so in |
3110 @samp{--host} option, because configuring an entire operating system for | 3518 bootstrapping a cross-compiler you must specify them both explicitly. |
3111 cross-operation is not a meaningful thing. | |
3112 | 3519 |
3113 Some programs have ways of configuring themselves automatically. If | 3520 Some programs have ways of configuring themselves automatically. If |
3114 your program is set up to do this, your @code{configure} script can simply | 3521 your program is set up to do this, your @code{configure} script can simply |
3115 ignore most of its arguments. | 3522 ignore most of its arguments. |
3116 | 3523 |
3121 @include make-stds.texi | 3528 @include make-stds.texi |
3122 @raisesections | 3529 @raisesections |
3123 | 3530 |
3124 @node Releases | 3531 @node Releases |
3125 @section Making Releases | 3532 @section Making Releases |
3533 @cindex packaging | |
3126 | 3534 |
3127 Package the distribution of @code{Foo version 69.96} up in a gzipped tar | 3535 Package the distribution of @code{Foo version 69.96} up in a gzipped tar |
3128 file with the name @file{foo-69.96.tar.gz}. It should unpack into a | 3536 file with the name @file{foo-69.96.tar.gz}. It should unpack into a |
3129 subdirectory named @file{foo-69.96}. | 3537 subdirectory named @file{foo-69.96}. |
3130 | 3538 |
3133 part of the program in any way must be classified into @dfn{source | 3541 part of the program in any way must be classified into @dfn{source |
3134 files} and @dfn{non-source files}. Source files are written by humans | 3542 files} and @dfn{non-source files}. Source files are written by humans |
3135 and never changed automatically; non-source files are produced from | 3543 and never changed automatically; non-source files are produced from |
3136 source files by programs under the control of the Makefile. | 3544 source files by programs under the control of the Makefile. |
3137 | 3545 |
3546 @cindex @file{README} file | |
3138 The distribution should contain a file named @file{README} which gives | 3547 The distribution should contain a file named @file{README} which gives |
3139 the name of the package, and a general description of what it does. It | 3548 the name of the package, and a general description of what it does. It |
3140 is also good to explain the purpose of each of the first-level | 3549 is also good to explain the purpose of each of the first-level |
3141 subdirectories in the package, if there are any. The @file{README} file | 3550 subdirectories in the package, if there are any. The @file{README} file |
3142 should either state the version number of the package, or refer to where | 3551 should either state the version number of the package, or refer to where |
3191 characters both before and after the period. Thus, | 3600 characters both before and after the period. Thus, |
3192 @file{foobarhacker.c} and @file{foobarhacker.o} are not ambiguous; they | 3601 @file{foobarhacker.c} and @file{foobarhacker.o} are not ambiguous; they |
3193 are truncated to @file{foobarha.c} and @file{foobarha.o}, which are | 3602 are truncated to @file{foobarha.c} and @file{foobarha.o}, which are |
3194 distinct. | 3603 distinct. |
3195 | 3604 |
3605 @cindex @file{texinfo.tex}, in a distribution | |
3196 Include in your distribution a copy of the @file{texinfo.tex} you used | 3606 Include in your distribution a copy of the @file{texinfo.tex} you used |
3197 to test print any @file{*.texinfo} or @file{*.texi} files. | 3607 to test print any @file{*.texinfo} or @file{*.texi} files. |
3198 | 3608 |
3199 Likewise, if your program uses small GNU software packages like regex, | 3609 Likewise, if your program uses small GNU software packages like regex, |
3200 getopt, obstack, or termcap, include them in the distribution file. | 3610 getopt, obstack, or termcap, include them in the distribution file. |
3202 the expense of possible inconvenience to a user who doesn't know what | 3612 the expense of possible inconvenience to a user who doesn't know what |
3203 other files to get. | 3613 other files to get. |
3204 | 3614 |
3205 @node References | 3615 @node References |
3206 @chapter References to Non-Free Software and Documentation | 3616 @chapter References to Non-Free Software and Documentation |
3617 @cindex references to non-free material | |
3207 | 3618 |
3208 A GNU program should not recommend use of any non-free program. We | 3619 A GNU program should not recommend use of any non-free program. We |
3209 can't stop some people from writing proprietary programs, or stop other | 3620 can't stop some people from writing proprietary programs, or stop other |
3210 people from using them. But we can and should avoid helping to | 3621 people from using them. But we can and should avoid helping to |
3211 advertise them to new customers. | 3622 advertise them to new customers. |
3225 with free software is now a major focus of the GNU project; to show that | 3636 with free software is now a major focus of the GNU project; to show that |
3226 we are serious about the need for free documentation, we must not | 3637 we are serious about the need for free documentation, we must not |
3227 undermine our position by recommending use of documentation that isn't | 3638 undermine our position by recommending use of documentation that isn't |
3228 free. | 3639 free. |
3229 | 3640 |
3641 @node Index | |
3642 @unnumbered Index | |
3643 @printindex cp | |
3644 | |
3230 @contents | 3645 @contents |
3231 | 3646 |
3232 @bye | 3647 @bye |
3648 Local variables: | |
3649 update-date-leading-regexp: "@c This date is automagically updated when you save this file:\n@set lastupdate " | |
3650 update-date-trailing-regexp: "" | |
3651 eval: (load "/gd/gnuorg/update-date.el") | |
3652 eval: (add-hook 'write-file-hooks 'update-date) | |
3653 compile-command: "make just-standards" | |
3654 End: |