source: trunk/essentials/sys-devel/autoconf-2.13/standards.info@ 3159

Last change on this file since 3159 was 3157, checked in by bird, 19 years ago

autoconf 2.13

File size: 127.1 KB
Line 
1This is Info file standards.info, produced by Makeinfo version 1.67
2from the input file /home/bje/autoconf-2.13/standards.texi.
3
4START-INFO-DIR-ENTRY
5* Standards: (standards). GNU coding standards.
6END-INFO-DIR-ENTRY
7
8 GNU Coding Standards Copyright (C) 1992, 1993, 1994, 1995, 1996,
91997, 1998 Free Software Foundation, Inc.
10
11 Permission is granted to make and distribute verbatim copies of this
12manual provided the copyright notice and this permission notice are
13preserved on all copies.
14
15 Permission is granted to copy and distribute modified versions of
16this manual under the conditions for verbatim copying, provided that
17the entire resulting derived work is distributed under the terms of a
18permission notice identical to this one.
19
20 Permission is granted to copy and distribute translations of this
21manual into another language, under the above conditions for modified
22versions, except that this permission notice may be stated in a
23translation approved by the Free Software Foundation.
24
25
26File: standards.info, Node: Top, Next: Preface, Prev: (dir), Up: (dir)
27
28Version
29*******
30
31 Last updated August 26, 1998.
32
33* Menu:
34
35* Preface:: About the GNU Coding Standards
36* Intellectual Property:: Keeping Free Software Free
37* Design Advice:: General Program Design
38* Program Behavior:: Program Behavior for All Programs
39* Writing C:: Making The Best Use of C
40* Documentation:: Documenting Programs
41* Managing Releases:: The Release Process
42
43
44File: standards.info, Node: Preface, Next: Intellectual Property, Prev: Top, Up: Top
45
46About the GNU Coding Standards
47******************************
48
49 The GNU Coding Standards were written by Richard Stallman and other
50GNU Project volunteers. Their purpose is to make the GNU system clean,
51consistent, and easy to install. This document can also be read as a
52guide to writing portable, robust and reliable programs. It focuses on
53programs written in C, but many of the rules and principles are useful
54even if you write in another programming language. The rules often
55state reasons for writing in a certain way.
56
57 Corrections or suggestions for this document should be sent to
58<[email protected]>. If you make a suggestion, please include a suggested
59new wording for it; our time is limited. We prefer a context diff to
60the `standards.texi' or `make-stds.texi' files, but if you don't have
61those files, please mail your suggestion anyway.
62
63 This release of the GNU Coding Standards was last updated August 26,
641998.
65
66
67File: standards.info, Node: Intellectual Property, Next: Design Advice, Prev: Preface, Up: Top
68
69Keeping Free Software Free
70**************************
71
72 This node discusses how you can make sure that GNU software remains
73unencumbered.
74
75* Menu:
76
77* Reading Non-Free Code:: Referring to Proprietary Programs
78* Contributions:: Accepting Contributions
79
80
81File: standards.info, Node: Reading Non-Free Code, Next: Contributions, Up: Intellectual Property
82
83Referring to Proprietary Programs
84=================================
85
86 Don't in any circumstances refer to Unix source code for or during
87your work on GNU! (Or to any other proprietary programs.)
88
89 If you have a vague recollection of the internals of a Unix program,
90this does not absolutely mean you can't write an imitation of it, but
91do try to organize the imitation internally along different lines,
92because this is likely to make the details of the Unix version
93irrelevant and dissimilar to your results.
94
95 For example, Unix utilities were generally optimized to minimize
96memory use; if you go for speed instead, your program will be very
97different. You could keep the entire input file in core and scan it
98there instead of using stdio. Use a smarter algorithm discovered more
99recently than the Unix program. Eliminate use of temporary files. Do
100it in one pass instead of two (we did this in the assembler).
101
102 Or, on the contrary, emphasize simplicity instead of speed. For some
103applications, the speed of today's computers makes simpler algorithms
104adequate.
105
106 Or go for generality. For example, Unix programs often have static
107tables or fixed-size strings, which make for arbitrary limits; use
108dynamic allocation instead. Make sure your program handles NULs and
109other funny characters in the input files. Add a programming language
110for extensibility and write part of the program in that language.
111
112 Or turn some parts of the program into independently usable
113libraries. Or use a simple garbage collector instead of tracking
114precisely when to free memory, or use a new GNU facility such as
115obstacks.
116
117
118File: standards.info, Node: Contributions, Prev: Reading Non-Free Code, Up: Intellectual Property
119
120Accepting Contributions
121=======================
122
123 If someone else sends you a piece of code to add to the program you
124are working on, we need legal papers to use it--the same sort of legal
125papers we will need to get from you. *Each* significant contributor to
126a program must sign some sort of legal papers in order for us to have
127clear title to the program. The main author alone is not enough.
128
129 So, before adding in any contributions from other people, please tell
130us, so we can arrange to get the papers. Then wait until we tell you
131that we have received the signed papers, before you actually use the
132contribution.
133
134 This applies both before you release the program and afterward. If
135you receive diffs to fix a bug, and they make significant changes, we
136need legal papers for that change.
137
138 This also applies to comments and documentation files. For copyright
139law, comments and code are just text. Copyright applies to all kinds of
140text, so we need legal papers for all kinds.
141
142 You don't need papers for changes of a few lines here or there, since
143they are not significant for copyright purposes. Also, you don't need
144papers if all you get from the suggestion is some ideas, not actual code
145which you use. For example, if you write a different solution to the
146problem, you don't need to get papers.
147
148 We know this is frustrating; it's frustrating for us as well. But if
149you don't wait, you are going out on a limb--for example, what if the
150contributor's employer won't sign a disclaimer? You might have to take
151that code out again!
152
153 The very worst thing is if you forget to tell us about the other
154contributor. We could be very embarrassed in court some day as a
155result.
156
157 We have more detailed advice for maintainers of programs; if you have
158reached the stage of actually maintaining a program for GNU (whether
159released or not), please ask us for a copy.
160
161
162File: standards.info, Node: Design Advice, Next: Program Behavior, Prev: Intellectual Property, Up: Top
163
164General Program Design
165**********************
166
167 This node discusses some of the issues you should take into account
168when designing your program.
169
170* Menu:
171
172* Compatibility:: Compatibility with other implementations
173* Using Extensions:: Using non-standard features
174* ANSI C:: Using ANSI C features
175* Source Language:: Using languages other than C
176
177
178File: standards.info, Node: Compatibility, Next: Using Extensions, Up: Design Advice
179
180Compatibility with Other Implementations
181========================================
182
183 With occasional exceptions, utility programs and libraries for GNU
184should be upward compatible with those in Berkeley Unix, and upward
185compatible with ANSI C if ANSI C specifies their behavior, and upward
186compatible with POSIX if POSIX specifies their behavior.
187
188 When these standards conflict, it is useful to offer compatibility
189modes for each of them.
190
191 ANSI C and POSIX prohibit many kinds of extensions. Feel free to
192make the extensions anyway, and include a `--ansi', `--posix', or
193`--compatible' option to turn them off. However, if the extension has
194a significant chance of breaking any real programs or scripts, then it
195is not really upward compatible. Try to redesign its interface.
196
197 Many GNU programs suppress extensions that conflict with POSIX if the
198environment variable `POSIXLY_CORRECT' is defined (even if it is
199defined with a null value). Please make your program recognize this
200variable if appropriate.
201
202 When a feature is used only by users (not by programs or command
203files), and it is done poorly in Unix, feel free to replace it
204completely with something totally different and better. (For example,
205`vi' is replaced with Emacs.) But it is nice to offer a compatible
206feature as well. (There is a free `vi' clone, so we offer it.)
207
208 Additional useful features not in Berkeley Unix are welcome.
209
210
211File: standards.info, Node: Using Extensions, Next: ANSI C, Prev: Compatibility, Up: Design Advice
212
213Using Non-standard Features
214===========================
215
216 Many GNU facilities that already exist support a number of convenient
217extensions over the comparable Unix facilities. Whether to use these
218extensions in implementing your program is a difficult question.
219
220 On the one hand, using the extensions can make a cleaner program.
221On the other hand, people will not be able to build the program unless
222the other GNU tools are available. This might cause the program to
223work on fewer kinds of machines.
224
225 With some extensions, it might be easy to provide both alternatives.
226For example, you can define functions with a "keyword" `INLINE' and
227define that as a macro to expand into either `inline' or nothing,
228depending on the compiler.
229
230 In general, perhaps it is best not to use the extensions if you can
231straightforwardly do without them, but to use the extensions if they
232are a big improvement.
233
234 An exception to this rule are the large, established programs (such
235as Emacs) which run on a great variety of systems. Such programs would
236be broken by use of GNU extensions.
237
238 Another exception is for programs that are used as part of
239compilation: anything that must be compiled with other compilers in
240order to bootstrap the GNU compilation facilities. If these require
241the GNU compiler, then no one can compile them without having them
242installed already. That would be no good.
243
244
245File: standards.info, Node: ANSI C, Next: Source Language, Prev: Using Extensions, Up: Design Advice
246
247ANSI C and pre-ANSI C
248=====================
249
250 Do not ever use the "trigraph" feature of ANSI C.
251
252 ANSI C is widespread enough now that it is ok to write new programs
253that use ANSI C features (and therefore will not work in non-ANSI
254compilers). And if a program is already written in ANSI C, there's no
255need to convert it to support non-ANSI compilers.
256
257 However, it is easy to support non-ANSI compilers in most programs,
258so you might still consider doing so when you write a program. Instead
259of writing function definitions in ANSI prototype form,
260
261 int
262 foo (int x, int y)
263 ...
264
265write the definition in pre-ANSI style like this,
266
267 int
268 foo (x, y)
269 int x, y;
270 ...
271
272and use a separate declaration to specify the argument prototype:
273
274 int foo (int, int);
275
276 You need such a declaration anyway, in a header file, to get the
277benefit of ANSI C prototypes in all the files where the function is
278called. And once you have it, you lose nothing by writing the function
279definition in the pre-ANSI style.
280
281 If you don't know non-ANSI C, there's no need to learn it; just
282write in ANSI C.
283
284
285File: standards.info, Node: Source Language, Prev: ANSI C, Up: Design Advice
286
287Using Languages Other Than C
288============================
289
290 Using a language other than C is like using a non-standard feature:
291it will cause trouble for users. Even if GCC supports the other
292language, users may find it inconvenient to have to install the
293compiler for that other language in order to build your program. For
294example, if you write your program in C++, people will have to install
295the C++ compiler in order to compile your program. Thus, it is better
296if you write in C.
297
298 But there are three situations when there is no disadvantage in using
299some other language:
300
301 * It is okay to use another language if your program contains an
302 interpreter for that language.
303
304 For example, if your program links with GUILE, it is ok to write
305 part of the program in Scheme or another language supported by
306 GUILE.
307
308 * It is okay to use another language in a tool specifically intended
309 for use with that language.
310
311 This is okay because the only people who want to build the tool
312 will be those who have installed the other language anyway.
313
314 * If an application is of interest to a narrow community, then
315 perhaps it's not important if the application is inconvenient to
316 install.
317
318 C has one other advantage over C++ and other compiled languages: more
319people know C, so more people will find it easy to read and modify the
320program if it is written in C.
321
322
323File: standards.info, Node: Program Behavior, Next: Writing C, Prev: Design Advice, Up: Top
324
325Program Behavior for All Programs
326*********************************
327
328 This node describes how to write robust software. It also describes
329general standards for error messages, the command line interface, and
330how libraries should behave.
331
332* Menu:
333
334* Semantics:: Writing robust programs
335* Libraries:: Library behavior
336* Errors:: Formatting error messages
337* User Interfaces:: Standards for command line interfaces
338* Option Table:: Table of long options.
339* Memory Usage:: When and how to care about memory needs
340
341
342File: standards.info, Node: Semantics, Next: Libraries, Up: Program Behavior
343
344Writing Robust Programs
345=======================
346
347 Avoid arbitrary limits on the length or number of *any* data
348structure, including file names, lines, files, and symbols, by
349allocating all data structures dynamically. In most Unix utilities,
350"long lines are silently truncated". This is not acceptable in a GNU
351utility.
352
353 Utilities reading files should not drop NUL characters, or any other
354nonprinting characters *including those with codes above 0177*. The
355only sensible exceptions would be utilities specifically intended for
356interface to certain types of printers that can't handle those
357characters.
358
359 Check every system call for an error return, unless you know you
360wish to ignore errors. Include the system error text (from `perror' or
361equivalent) in *every* error message resulting from a failing system
362call, as well as the name of the file if any and the name of the
363utility. Just "cannot open foo.c" or "stat failed" is not sufficient.
364
365 Check every call to `malloc' or `realloc' to see if it returned
366zero. Check `realloc' even if you are making the block smaller; in a
367system that rounds block sizes to a power of 2, `realloc' may get a
368different block if you ask for less space.
369
370 In Unix, `realloc' can destroy the storage block if it returns zero.
371GNU `realloc' does not have this bug: if it fails, the original block
372is unchanged. Feel free to assume the bug is fixed. If you wish to
373run your program on Unix, and wish to avoid lossage in this case, you
374can use the GNU `malloc'.
375
376 You must expect `free' to alter the contents of the block that was
377freed. Anything you want to fetch from the block, you must fetch before
378calling `free'.
379
380 If `malloc' fails in a noninteractive program, make that a fatal
381error. In an interactive program (one that reads commands from the
382user), it is better to abort the command and return to the command
383reader loop. This allows the user to kill other processes to free up
384virtual memory, and then try the command again.
385
386 Use `getopt_long' to decode arguments, unless the argument syntax
387makes this unreasonable.
388
389 When static storage is to be written in during program execution, use
390explicit C code to initialize it. Reserve C initialized declarations
391for data that will not be changed.
392
393 Try to avoid low-level interfaces to obscure Unix data structures
394(such as file directories, utmp, or the layout of kernel memory), since
395these are less likely to work compatibly. If you need to find all the
396files in a directory, use `readdir' or some other high-level interface.
397These will be supported compatibly by GNU.
398
399 The preferred signal handling facilities are the BSD variant of
400`signal', and the POSIX `sigaction' function; the alternative USG
401`signal' interface is an inferior design.
402
403 Nowadays, using the POSIX signal functions may be the easiest way to
404make a program portable. If you use `signal', then on GNU/Linux
405systems running GNU libc version 1, you should include `bsd/signal.h'
406instead of `signal.h', so as to get BSD behavior. It is up to you
407whether to support systems where `signal' has only the USG behavior, or
408give up on them.
409
410 In error checks that detect "impossible" conditions, just abort.
411There is usually no point in printing any message. These checks
412indicate the existence of bugs. Whoever wants to fix the bugs will have
413to read the source code and run a debugger. So explain the problem with
414comments in the source. The relevant data will be in variables, which
415are easy to examine with the debugger, so there is no point moving them
416elsewhere.