]>
Commit | Line | Data |
---|---|---|
b42b3782 RP |
1 | \input texinfo @c -*-texinfo-*- |
2 | @c %**start of header | |
ee7bc291 | 3 | @setfilename standards.info |
b42b3782 RP |
4 | @settitle GNU Coding Standards |
5 | @c %**end of header | |
6 | ||
01b8752d DZ |
7 | @ifinfo |
8 | @format | |
9 | START-INFO-DIR-ENTRY | |
10 | * standards: (standards). GNU Project Coding Standards | |
11 | END-INFO-DIR-ENTRY | |
12 | @end format | |
13 | @end ifinfo | |
14 | ||
15 | ||
b42b3782 RP |
16 | @setchapternewpage off |
17 | ||
18 | @ifinfo | |
19 | Copyright (C) 1992 Free Software Foundation | |
20 | Permission is granted to make and distribute verbatim copies of | |
21 | this manual provided the copyright notice and this permission notice | |
22 | are preserved on all copies. | |
23 | ||
24 | @ignore | |
25 | Permission is granted to process this file through TeX and print the | |
26 | results, provided the printed document carries copying permission | |
27 | notice identical to this one except for the removal of this paragraph | |
28 | (this paragraph not being relevant to the printed manual). | |
29 | @end ignore | |
30 | ||
31 | Permission is granted to copy and distribute modified versions of this | |
32 | manual under the conditions for verbatim copying, provided that the entire | |
33 | resulting derived work is distributed under the terms of a permission | |
34 | notice identical to this one. | |
35 | ||
36 | Permission is granted to copy and distribute translations of this manual | |
37 | into another language, under the above conditions for modified versions, | |
38 | except that this permission notice may be stated in a translation approved | |
39 | by the Free Software Foundation. | |
40 | @end ifinfo | |
41 | ||
42 | @titlepage | |
43 | @sp 10 | |
44 | @titlefont{GNU Coding Standards} | |
45 | @author{Richard Stallman} | |
ee7bc291 | 46 | @author{last updated 16 Jul 1992} |
b42b3782 RP |
47 | @c Note date also appears below. |
48 | @page | |
49 | ||
50 | @vskip 0pt plus 1filll | |
51 | Copyright @copyright{} 1992 Free Software Foundation | |
52 | ||
53 | Permission is granted to make and distribute verbatim copies of | |
54 | this manual provided the copyright notice and this permission notice | |
55 | are preserved on all copies. | |
56 | ||
57 | Permission is granted to copy and distribute modified versions of this | |
58 | manual under the conditions for verbatim copying, provided that the entire | |
59 | resulting derived work is distributed under the terms of a permission | |
60 | notice identical to this one. | |
61 | ||
62 | Permission is granted to copy and distribute translations of this manual | |
63 | into another language, under the above conditions for modified versions, | |
64 | except that this permission notice may be stated in a translation approved | |
65 | by Free Software Foundation. | |
66 | @end titlepage | |
67 | ||
68 | @ifinfo | |
69 | @node Top, Reading Non-Free Code, (dir), (dir) | |
70 | @top Version | |
71 | ||
ee7bc291 | 72 | Last updated 16 Jul 1992. |
b42b3782 RP |
73 | @c Note date also appears above. |
74 | @end ifinfo | |
75 | ||
76 | @menu | |
77 | * Reading Non-Free Code:: Referring to Proprietary Programs | |
78 | * Contributions:: Accepting Contributions | |
79 | * Change Logs:: Recording Changes | |
3e12f39a | 80 | * Compatibility:: Compatibility with Other Implementations |
b42b3782 RP |
81 | * Makefiles:: Makefile Conventions |
82 | * Configuration:: How Configuration Should Work | |
83 | * Source Language:: Using Languages Other Than C | |
84 | * Formatting:: Formatting Your Source Code | |
85 | * Comments:: Commenting Your Work | |
86 | * Syntactic Conventions:: Clean Use of C Constructs | |
87 | * Names:: Naming Variables and Functions | |
88 | * Using Extensions:: Using Non-standard Features | |
89 | * Semantics:: Program Behaviour for All Programs | |
90 | * Errors:: Formatting Error Messages | |
91 | * Libraries:: Library Behaviour | |
92 | * Portability:: Portability As It Applies to GNU | |
93 | * User Interfaces:: Standards for Command Line Interfaces | |
94 | * Documentation:: Documenting Programs | |
95 | * Releases:: Making Releases | |
96 | @end menu | |
97 | ||
98 | @node Reading Non-Free Code | |
99 | @chapter Referring to Proprietary Programs | |
100 | ||
101 | Don't in any circumstances refer to Unix source code for or during | |
102 | your work on GNU! (Or to any other proprietary programs.) | |
103 | ||
104 | If you have a vague recollection of the internals of a Unix program, | |
105 | this does not absolutely mean you can't write an imitation of it, but | |
106 | do try to organize the imitation internally along different lines, | |
107 | because this is likely to make the details of the Unix version | |
108 | irrelevant and dissimilar to your results. | |
109 | ||
110 | For example, Unix utilities were generally optimized to minimize | |
111 | memory use; if you go for speed instead, your program will be very | |
112 | different. You could keep the entire input file in core and scan it | |
113 | there instead of using stdio. Use a smarter algorithm discovered more | |
114 | recently than the Unix program. Eliminate use of temporary files. Do | |
115 | it in one pass instead of two (we did this in the assembler). | |
116 | ||
117 | Or, on the contrary, emphasize simplicity instead of speed. For some | |
118 | applications, the speed of today's computers makes simpler algorithms | |
119 | adequate. | |
120 | ||
121 | Or go for generality. For example, Unix programs often have static | |
122 | tables or fixed-size strings, which make for arbitrary limits; use | |
123 | dynamic allocation instead. Make sure your program handles NULs and | |
124 | other funny characters in the input files. Add a programming language | |
125 | for extensibility and write part of the program in that language. | |
126 | ||
127 | Or turn some parts of the program into independently usable libraries. | |
128 | Or use a simple garbage collector instead of tracking precisely when | |
129 | to free memory, or use a new GNU facility such as obstacks. | |
130 | ||
131 | ||
132 | @node Contributions | |
133 | @chapter Accepting Contributions | |
134 | ||
135 | If someone else sends you a piece of code to add to the program you are | |
136 | working on, we need legal papers to use it---the same sort of legal | |
137 | papers we will need to get from you. @emph{Each} significant | |
138 | contributor to a program must sign some sort of legal papers in order | |
139 | for us to have clear title to the program. The main author alone is not | |
140 | enough. | |
141 | ||
142 | So, before adding in any contributions from other people, tell us | |
143 | so we can arrange to get the papers. Then wait until we tell you | |
144 | that we have received the signed papers, before you actually use the | |
145 | contribution. | |
146 | ||
147 | This applies both before you release the program and afterward. If | |
148 | you receive diffs to fix a bug, and they make significant change, we | |
149 | need legal papers for it. | |
150 | ||
151 | You don't need papers for changes of a few lines here or there, since | |
152 | they are not significant for copyright purposes. Also, you don't need | |
153 | papers if all you get from the suggestion is some ideas, not actual code | |
154 | which you use. For example, if you write a different solution to the | |
155 | problem, you don't need to get papers. | |
156 | ||
157 | I know this is frustrating; it's frustrating for us as well. But if | |
158 | you don't wait, you are going out on a limb---for example, what if the | |
159 | contributor's employer won't sign a disclaimer? You might have to take | |
160 | that code out again! | |
161 | ||
162 | The very worst thing is if you forget to tell us about the other | |
163 | contributor. We could be very embarrassed in court some day as a | |
164 | result. | |
165 | ||
166 | @node Change Logs | |
167 | @chapter Change Logs | |
168 | ||
169 | Keep a change log for each directory, describing the changes made to | |
170 | source files in that directory. The purpose of this is so that people | |
171 | investigating bugs in the future will know about the changes that | |
172 | might have introduced the bug. Often a new bug can be found by | |
173 | looking at what was recently changed. More importantly, change logs | |
174 | can help eliminate conceptual inconsistencies between different parts | |
175 | of a program; they can give you a history of how the conflicting | |
176 | concepts arose. | |
177 | ||
178 | Use the Emacs command @kbd{M-x add-change} to start a new entry in the | |
179 | change log. An entry should have an asterisk, the name of the changed | |
180 | file, and then in parentheses the name of the changed functions, | |
181 | variables or whatever, followed by a colon. Then describe the changes | |
182 | you made to that function or variable. | |
183 | ||
184 | Separate unrelated entries with blank lines. When two entries | |
185 | represent parts of the same change, so that they work together, then | |
186 | don't put blank lines between them. Then you can omit the file name | |
187 | and the asterisk when successive entries are in the same file. | |
188 | ||
189 | Here are some examples: | |
190 | ||
191 | @example | |
192 | * register.el (insert-register): Return nil. | |
193 | (jump-to-register): Likewise. | |
194 | ||
195 | * sort.el (sort-subr): Return nil. | |
196 | ||
197 | * tex-mode.el (tex-bibtex-file, tex-file, tex-region): | |
198 | Restart the tex shell if process is gone or stopped. | |
199 | (tex-shell-running): New function. | |
200 | ||
201 | * expr.c (store_one_arg): Round size up for move_block_to_reg. | |
202 | (expand_call): Round up when emitting USE insns. | |
203 | * stmt.c (assign_parms): Round size up for move_block_from_reg. | |
204 | @end example | |
205 | ||
206 | There's no need to describe here the full purpose of the changes or how | |
207 | they work together. It is better to put this explanation in comments in | |
208 | the code. That's why just ``New function'' is enough; there is a | |
209 | comment with the function in the source to explain what it does. | |
210 | ||
211 | However, sometimes it is useful to write one line to describe the | |
212 | overall purpose of a large batch of changes. | |
213 | ||
ee7bc291 RP |
214 | You can think of the change log as a conceptual ``undo list'' which |
215 | explains how earlier versions were different from the current version. | |
216 | People can see the current version; they don't need the change log | |
217 | to tell them what is in it. What they want from a change log is a | |
218 | clear explanation of how the earlier version differed. | |
219 | ||
b42b3782 RP |
220 | When you change the calling sequence of a function in a simple |
221 | fashion, and you change all the callers of the function, there is no | |
222 | need to make individual entries for all the callers. Just write in | |
223 | the entry for the function being called, ``All callers changed.'' | |
224 | ||
225 | When you change just comments or doc strings, it is enough to write an | |
226 | entry for the file, without mentioning the functions. Write just, | |
227 | ``Doc fix.'' There's no need to keep a change log for documentation | |
228 | files. This is because documentation is not susceptible to bugs that | |
229 | are hard to fix. Documentation does not consist of parts that must | |
230 | interact in a precisely engineered fashion; to correct an error, you | |
231 | need not know the history of the erroneous passage. | |
232 | ||
233 | ||
234 | @node Compatibility | |
3e12f39a | 235 | @chapter Compatibility with Other Implementations |
b42b3782 RP |
236 | |
237 | With certain exceptions, utility programs and libraries for GNU should | |
238 | be upward compatible with those in Berkeley Unix, and upward compatible | |
239 | with @sc{ANSI} C if @sc{ANSI} C specifies their behavior, and upward | |
240 | compatible with @sc{POSIX} if @sc{POSIX} specifies their behavior. | |
241 | ||
242 | When these standards conflict, it is useful to offer compatibility | |
243 | modes for each of them. | |
244 | ||
245 | @sc{ANSI} C and @sc{POSIX} prohibit many kinds of extensions. Feel | |
246 | free to make the extensions anyway, and include a @samp{--ansi} or | |
247 | @samp{--compatible} option to turn them off. However, if the extension | |
248 | has a significant chance of breaking any real programs or scripts, | |
249 | then it is not really upward compatible. Try to redesign its | |
250 | interface. | |
251 | ||
252 | When a feature is used only by users (not by programs or command | |
253 | files), and it is done poorly in Unix, feel free to replace it | |
254 | completely with something totally different and better. (For example, | |
255 | vi is replaced with Emacs.) But it is nice to offer a compatible | |
256 | feature as well. (There is a free vi clone, so we offer it.) | |
257 | ||
258 | Additional useful features not in Berkeley Unix are welcome. | |
259 | Additional programs with no counterpart in Unix may be useful, | |
260 | but our first priority is usually to duplicate what Unix already | |
261 | has. | |
262 | ||
263 | ||
264 | @node Makefiles | |
265 | @chapter Makefile Conventions | |
266 | ||
267 | This chapter describes conventions for writing Makefiles. | |
268 | ||
269 | @menu | |
270 | * Makefile Basics:: | |
271 | * Standard Targets:: | |
272 | * Command Variables:: | |
273 | * Directory Variables:: | |
274 | @end menu | |
275 | ||
276 | @node Makefile Basics | |
277 | @section General Conventions for Makefiles | |
278 | ||
279 | Every Makefile should contain this line: | |
280 | ||
281 | @example | |
282 | SHELL = /bin/sh | |
283 | @end example | |
284 | ||
285 | @noindent | |
286 | to avoid trouble on systems where the @code{SHELL} variable might be | |
287 | inherited from the environment. | |
288 | ||
289 | Don't assume that @file{.} is in the path for command execution. When | |
92f66b26 DZ |
290 | you need to run programs that are a part of your package during the |
291 | make, please make sure that it uses @file{./} if the program is built as | |
292 | part of the make or @file{$(srcdir)/} if the file is an unchanging part | |
293 | of the source code. Without one of these prefixes, the current search | |
294 | path is used. | |
295 | ||
296 | The distinction between @file{./} and @file{$(srcdir)/} is important | |
297 | when using the @samp{--srcdir} option to @file{configure}. A rule of | |
298 | the form: | |
299 | ||
300 | @example | |
301 | foo.1 : foo.man sedscript | |
302 | sed -e sedscript foo.man > foo.1 | |
303 | @end example | |
304 | ||
305 | @noindent | |
306 | will fail when the current directory is not the source directory, | |
307 | because @file{foo.man} and @file{sedscript} are not in the current | |
308 | directory. | |
309 | ||
310 | Relying on @samp{VPATH} to find the source file will work in the case | |
311 | where there is a single dependency file, since the @file{make} automatic | |
312 | variable @samp{$<} will represent the source file wherever it is. A | |
313 | makefile target like | |
314 | ||
315 | @example | |
316 | foo.o : bar.c | |
317 | $(CC) $(CFLAGS) -I. -I$(srcdir) -c bar.c -o foo.o | |
318 | @end example | |
319 | ||
320 | @noindent | |
321 | should instead be written as | |
322 | ||
323 | @example | |
324 | foo.o : bar.c | |
325 | $(CC) $(CFLAGS) $< -o $@ | |
326 | @end example | |
3e12f39a | 327 | |
92f66b26 DZ |
328 | @noindent |
329 | in order to allow @samp{VPATH} to work correctly. When the target has | |
330 | multiple dependencies, using an explicit @samp{$(srcdir)} is the easiest | |
331 | way to make the rule work well. For example, the target above for | |
332 | @file{foo.1} is best written as: | |
333 | ||
334 | @example | |
335 | foo.1 : foo.man sedscript | |
336 | sed -s $(srcdir)/sedscript $(srcdir)/foo.man > foo.1 | |
337 | @end example | |
338 | ||
b42b3782 RP |
339 | @node Standard Targets |
340 | @section Standard Targets for Users | |
341 | ||
342 | All GNU programs should have the following targets in their Makefiles: | |
343 | ||
344 | @table @samp | |
345 | @item all | |
346 | Compile the entire program. | |
347 | ||
348 | @item install | |
349 | Compile the program and copy the executables, libraries, and so on to | |
350 | the file names where they should reside for actual use. If there is a | |
351 | simple test to verify that a program is properly installed then run that | |
352 | test. | |
353 | ||
9dec5417 DZ |
354 | Use @samp{-} before any command for installing a man page, so that |
355 | @code{make} will ignore any errors. This is in case there are systems | |
356 | that don't have the Unix man page documentation system installed. | |
357 | ||
b42b3782 RP |
358 | @item clean |
359 | Delete all files from the current directory that are normally created by | |
360 | building the program. Don't delete the files that record the | |
361 | configuration. Also preserve files that could be made by building, but | |
362 | normally aren't because the distribution comes with them. | |
363 | ||
9dec5417 DZ |
364 | Delete @file{.dvi} files here if they are not part of the distribution. |
365 | ||
b42b3782 RP |
366 | @item distclean |
367 | Delete all files from the current directory that are created by | |
85e44e95 RP |
368 | configuring or building the program. If you have unpacked the source |
369 | and built the program without creating any other files, @samp{make | |
370 | distclean} should leave only the files that were in the distribution. | |
b42b3782 RP |
371 | |
372 | @item mostlyclean | |
373 | Like @samp{clean}, but may refrain from deleting a few files that people | |
374 | normally don't want to recompile. For example, the @samp{mostlyclean} | |
375 | target for GCC does not delete @file{libgcc.a}, because recompiling it | |
376 | is rarely necessary and takes a lot of time. | |
377 | ||
378 | @item realclean | |
379 | Delete everything from the current directory that can be reconstructed | |
380 | with this Makefile. This typically includes everything deleted by | |
381 | distclean, plus more: C source files produced by Bison, tags tables, | |
382 | info files, and so on. | |
383 | ||
384 | @item TAGS | |
385 | Update a tags table for this program. | |
386 | ||
387 | @item dist | |
388 | Create a distribution tar file for this program. The tar file should be | |
389 | set up so that the file names in the tar file start with a subdirectory | |
390 | name which is the name of the package it is a distribution for. This | |
391 | name can include the version number. | |
392 | ||
393 | For example, the distribution tar file of GCC version 1.40 unpacks into | |
394 | a subdirectory named @file{gcc-1.40}. | |
395 | ||
396 | The easiest way to do this is to create a subdirectory appropriately | |
397 | named, use @code{ln} or @code{cp} to install the proper files in it, and | |
398 | then @code{tar} that subdirectory. | |
399 | ||
400 | The @code{dist} target should explicitly depend on all non-source files | |
401 | that are in the distribution, to make sure they are up to date in the | |
402 | distribution. @xref{Releases}. | |
403 | ||
404 | @item check | |
405 | Perform self-tests (if any). The user must build the program before | |
406 | running the tests, but need not install the program; you should write | |
407 | the self-tests so that they work when the program is built but not | |
408 | installed. | |
409 | @end table | |
410 | ||
411 | @node Command Variables | |
412 | @section Variables for Specifying Commands | |
413 | ||
414 | Makefiles should provide variables for overriding certain commands, options, | |
415 | and so on. | |
416 | ||
417 | In particular, you should run most utility programs via variables. | |
418 | Thus, if you use Bison, have a variable named @code{BISON} whose default | |
419 | value is set with @samp{BISON = bison}, and refer to it with | |
420 | @code{$(BISON)} whenever you need to use Bison. | |
421 | ||
3e12f39a RP |
422 | File management utilities such as @code{ln}, @code{rm}, @code{mv}, and |
423 | so on, need not be referred to through variables in this way, since users | |
424 | don't need to replace them with other programs. | |
425 | ||
b42b3782 RP |
426 | Each program-name variable should come with an options variable that is |
427 | used to supply options to the program. Append @samp{FLAGS} to the | |
428 | program-name variable name to get the options variable name---for | |
429 | example, @code{BISONFLAGS}. (The name @code{CFLAGS} is an exception to | |
3e12f39a RP |
430 | this rule, but we keep it because it is standard.) Use @code{CPPFLAGS} |
431 | in any compilation command that runs the preprocessor, and use | |
432 | @code{LDFLAGS} in any compilation command that does linking as well as | |
433 | in any direct use of @code{ld}. | |
434 | ||
435 | If there are C compiler options that @emph{must} be used for proper | |
436 | compilation of certain files, do not include them in @code{CFLAGS}. | |
437 | Users expect to be able to specify @code{CFLAGS} freely themselves. | |
438 | Instead, arrange to pass the necessary options to the C compiler | |
439 | independently of @code{CFLAGS}, by writing them explicitly in the | |
440 | compilation commands or by defining an implicit rule, like this: | |
b42b3782 | 441 | |
3e12f39a RP |
442 | @example |
443 | CFLAGS = -g | |
444 | ALL_CFLAGS = $(CFLAGS) -I. | |
445 | .c.o: | |
446 | $(CC) -c $(ALL_CFLAGS) $(CPPFLAGS) $< | |
447 | @end example | |
448 | ||
449 | Do include the @samp{-g} option in @code{CFLAGS}, because that is not | |
450 | @emph{required} for proper compilation. You can consider it a default | |
451 | that is only recommended. If the package is set up so that it is | |
452 | compiled with GCC by default, then you might as well include @samp{-O} | |
453 | in the default value of @code{CFLAGS} as well. | |
b42b3782 RP |
454 | |
455 | Every Makefile should define the variable @code{INSTALL}, which is the | |
456 | basic command for installing a file into the system. | |
457 | ||
458 | Every Makefile should also define variables @code{INSTALL_PROGRAM} and | |
459 | @code{INSTALL_DATA}. (The default for each of these should be | |
460 | @code{$(INSTALL)}.) Then it should use those variables as the commands | |
461 | for actual installation, for executables and nonexecutables | |
462 | respectively. Use these variables as follows: | |
463 | ||
464 | @example | |
3e12f39a RP |
465 | $(INSTALL_PROGRAM) foo $(bindir)/foo |
466 | $(INSTALL_DATA) libfoo.a $(libdir)/libfoo.a | |
b42b3782 RP |
467 | @end example |
468 | ||
469 | @noindent | |
3e12f39a RP |
470 | Always use a file name, not a directory name, as the second argument of |
471 | the installation commands. Use a separate command for each file to be | |
472 | installed. | |
b42b3782 RP |
473 | |
474 | @node Directory Variables | |
475 | @section Variables for Installation Directories | |
476 | ||
477 | Installation directories should always be named by variables, so it is | |
478 | easy to install in a nonstandard place. The standard names for these | |
479 | variables are: | |
480 | ||
481 | @table @samp | |
a60ff512 RP |
482 | @item prefix |
483 | A prefix used in constructing the default values of the variables listed | |
484 | below. The default value of @code{prefix} should be @file{/usr/local} | |
485 | (at least for now). | |
486 | ||
487 | @item exec_prefix | |
488 | A prefix used in constructing the default values of the some of the | |
489 | variables listed below. The default value of @code{exec_prefix} should | |
490 | be @code{$(prefix)}. | |
491 | ||
492 | Generally, @code{$(exec_prefix)} is used for directories that contain | |
493 | machine-specific files (such as executables and subroutine libraries), | |
494 | while @code{$(prefix)} is used directly for other directories. | |
495 | ||
b42b3782 RP |
496 | @item bindir |
497 | The directory for installing executable programs that users can run. | |
a60ff512 RP |
498 | This should normally be @file{/usr/local/bin}, but it should be written |
499 | as @file{$(exec_prefix)/bin}. | |
500 | ||
501 | @item libdir | |
502 | The directory for installing executable files to be run by the program | |
503 | rather than by users. Object files and libraries of object code should | |
504 | also go in this directory. The idea is that this directory is used for | |
505 | files that pertain to a specific machine architecture, but need not be | |
506 | in the path for commands. The value of @code{libdir} should normally be | |
507 | @file{/usr/local/lib}, but it should be written as | |
508 | @file{$(exec_prefix)/lib}. | |
b42b3782 RP |
509 | |
510 | @item datadir | |
511 | The directory for installing read-only data files which the programs | |
512 | refer to while they run. This directory is used for files which are | |
513 | independent of the type of machine being used. This should normally be | |
a60ff512 RP |
514 | @file{/usr/local/lib}, but it should be written as |
515 | @file{$(prefix)/lib}. | |
b42b3782 RP |
516 | |
517 | @item statedir | |
518 | The directory for installing data files which the programs modify while | |
519 | they run. These files should be independent of the type of machine | |
520 | being used, and it should be possible to share them among machines at a | |
521 | network installation. This should normally be @file{/usr/local/lib}, | |
a60ff512 | 522 | but it should be written as @file{$(prefix)/lib}. |
b42b3782 RP |
523 | |
524 | @item includedir | |
525 | The directory for installing @samp{#include} header files to be included | |
526 | by user programs. This should normally be @file{/usr/local/include}, | |
a60ff512 | 527 | but it should be written as @file{$(prefix)/include}. |
b42b3782 RP |
528 | |
529 | Most compilers other than GCC do not look for header files in | |
530 | @file{/usr/local/include}. So installing the header files this way is | |
531 | only useful with GCC. Sometimes this is not a problem because some | |
532 | libraries are only really intended to work with GCC. But some libraries | |
533 | are intended to work with other compilers. They should install their | |
3e12f39a RP |
534 | header files in two places, one specified by @code{includedir} and one |
535 | specified by @code{oldincludedir}. | |
b42b3782 RP |
536 | |
537 | @item oldincludedir | |
538 | The directory for installing @samp{#include} header files for use with | |
539 | compilers other than GCC. This should normally be @file{/usr/include}. | |
540 | ||
541 | The Makefile commands should check whether the value of | |
542 | @code{oldincludedir} is empty. If it is, they should not try to use | |
543 | it; they should cancel the second installation of the header files. | |
544 | ||
545 | @item mandir | |
546 | The directory for installing the man pages (if any) for this package. | |
547 | It should include the suffix for the proper section of the | |
548 | manual---usually @samp{1} for a utility. | |
549 | ||
550 | @item man1dir | |
551 | The directory for installing section 1 man pages. | |
552 | @item man2dir | |
553 | The directory for installing section 2 man pages. | |
554 | @item @dots{} | |
555 | Use these names instead of @samp{mandir} if the package needs to install man | |
556 | pages in more than one section of the manual. | |
557 | ||
558 | @strong{Don't make the primary documentation for any GNU software be a | |
559 | man page. Write a manual in Texinfo instead. Man pages are just for | |
560 | the sake of people running GNU software on Unix, which is a secondary | |
561 | application only.} | |
562 | ||
563 | @item manext | |
564 | The file name extension for the installed man page. This should contain | |
565 | a period followed by the appropriate digit. | |
566 | ||
567 | @item infodir | |
568 | The directory for installing the info files for this package. By | |
a60ff512 RP |
569 | default, it should be @file{/usr/local/info}, but it should be written |
570 | as @file{$(prefix)/info}. | |
b42b3782 RP |
571 | |
572 | @item srcdir | |
573 | The directory for the sources being compiled. The value of this | |
574 | variable is normally inserted by the @code{configure} shell script. | |
b42b3782 RP |
575 | @end table |
576 | ||
577 | For example: | |
578 | ||
579 | @example | |
580 | # Common prefix for installation directories. | |
581 | # NOTE: This directory must exist when you start installation. | |
582 | prefix = /usr/local | |
a60ff512 | 583 | exec_prefix = $(prefix) |
b42b3782 | 584 | # Directory in which to put the executable for the command `gcc' |
a60ff512 | 585 | bindir = $(exec_prefix)/bin |
b42b3782 | 586 | # Directory in which to put the directories used by the compiler. |
a60ff512 RP |
587 | libdir = $(exec_prefix)/lib |
588 | # Directory in which to put the Info files. | |
589 | infodir = $(prefix)/info | |
b42b3782 RP |
590 | @end example |
591 | ||
3e12f39a RP |
592 | If your program installs a large number of files into one of the |
593 | standard user-specified directories, it might be useful to group them | |
594 | into a subdirectory particular to that program. If you do this, you | |
595 | should write the @code{install} rule to create these subdirectories. | |
596 | ||
597 | Do not expect the user to include the subdirectory name in the value of | |
598 | any of the variables listed above. The idea of having a uniform set of | |
599 | variable names for installation directories is to enable the user to | |
600 | specify the exact same values for several different GNU packages. In | |
601 | order for this to be useful, all the packages must be designed so that | |
602 | they will work sensibly when the user does so. | |
603 | ||
b42b3782 RP |
604 | @node Configuration |
605 | @chapter How Configuration Should Work | |
606 | ||
607 | Each GNU distribution should come with a shell script named | |
608 | @code{configure}. This script is given arguments which describe the | |
609 | kind of machine and system you want to compile the program for. | |
610 | ||
611 | The @code{configure} script must record the configuration options so | |
612 | that they affect compilation. | |
613 | ||
614 | One way to do this is to make a link from a standard name such as | |
615 | @file{config.h} to the proper configuration file for the chosen system. | |
616 | If you use this technique, the distribution should @emph{not} contain a | |
617 | file named @file{config.h}. This is so that people won't be able to | |
618 | build the program without configuring it first. | |
619 | ||
620 | Another thing that @code{configure} can do is to edit the Makefile. If | |
621 | you do this, the distribution should @emph{not} contain a file named | |
622 | @file{Makefile}. Instead, include a file @file{Makefile.in} which | |
623 | contains the input used for editing. Once again, this is so that people | |
624 | won't be able to build the program without configuring it first. | |
625 | ||
626 | If @code{configure} does write the @file{Makefile}, then @file{Makefile} | |
627 | should have a target named @file{Makefile} which causes @code{configure} | |
628 | to be rerun, setting up the same configuration that was set up last | |
629 | time. The files that @code{configure} reads should be listed as | |
630 | dependencies of @file{Makefile}. | |
631 | ||
632 | All the files which are output from the @code{configure} script should | |
633 | have comments at the beginning explaining that they were generated | |
634 | automatically using @code{configure}. This is so that users won't think | |
635 | of trying to edit them by hand. | |
636 | ||
637 | The @code{configure} script should write a file named @file{config.status} | |
638 | which describes which configuration options were specified when the | |
639 | program was last configured. This file should be a shell script which, | |
640 | if run, will recreate the same configuration. | |
641 | ||
642 | The @code{configure} script should accept an option of the form | |
643 | @samp{--srcdir=@var{dirname}} to specify the directory where sources are found | |
644 | (if it is not the current directory). This makes it possible to build | |
645 | the program in a separate directory, so that the actual source directory | |
646 | is not modified. | |
647 | ||
648 | If the user does not specify @samp{--srcdir}, then @code{configure} should | |
649 | check both @file{.} and @file{..} to see if it can find the sources. If | |
650 | it finds the sources in one of these places, it should use them from | |
651 | there. Otherwise, it should report that it cannot find the sources, and | |
652 | should exit with nonzero status. | |
653 | ||
654 | Usually the easy way to support @samp{--srcdir} is by editing a | |
655 | definition of @code{VPATH} into the Makefile. Some rules may need to | |
656 | refer explicitly to the specified source directory. To make this | |
657 | possible, @code{configure} can add to the Makefile a variable named | |
658 | @code{srcdir} whose value is precisely the specified directory. | |
659 | ||
660 | The @code{configure} script should also take an argument which specifies the | |
661 | type of system to build the program for. This argument should look like | |
662 | this: | |
663 | ||
664 | @example | |
665 | @var{cpu}-@var{company}-@var{system} | |
666 | @end example | |
667 | ||
668 | For example, a Sun 3 might be @samp{m68k-sun-sunos4.1}. | |
669 | ||
670 | The @code{configure} script needs to be able to decode all plausible | |
671 | alternatives for how to describe a machine. Thus, @samp{sun3-sunos4.1} | |
85e44e95 | 672 | would be a valid alias. So would @samp{sun3-bsd4.2}, since SunOS is |
b42b3782 RP |
673 | basically @sc{BSD} and no other @sc{BSD} system is used on a Sun. For many |
674 | programs, @samp{vax-dec-ultrix} would be an alias for | |
675 | @samp{vax-dec-bsd}, simply because the differences between Ultrix and | |
676 | @sc{BSD} are rarely noticeable, but a few programs might need to distinguish | |
677 | them. | |
678 | ||
679 | There is a shell script called @file{config.sub} that you can use | |
680 | as a subroutine to validate system types and canonicalize aliases. | |
681 | ||
682 | Other options are permitted to specify in more detail the software | |
683 | or hardware are present on the machine: | |
684 | ||
685 | @table @samp | |
686 | @item --with-@var{package} | |
687 | The package @var{package} will be installed, so configure this package | |
688 | to work with @var{package}. | |
689 | ||
690 | Possible values of @var{package} include @samp{x}, @samp{gnu-as} (or | |
691 | @samp{gas}), @samp{gnu-ld}, @samp{gnu-libc}, and @samp{gdb}. | |
692 | ||
693 | @item --nfp | |
694 | The target machine has no floating point processor. | |
695 | ||
696 | @item --gas | |
697 | The target machine assembler is GAS, the GNU assembler. | |
698 | This is obsolete; use @samp{--with-gnu-as} instead. | |
699 | ||
700 | @item --x | |
701 | The target machine has the X Window system installed. | |
702 | This is obsolete; use @samp{--with-x} instead. | |
703 | @end table | |
704 | ||
705 | All @code{configure} scripts should accept all of these ``detail'' | |
706 | options, whether or not they make any difference to the particular | |
707 | package at hand. In particular, they should accept any option that | |
708 | starts with @samp{--with-}. This is so users will be able to configure | |
709 | an entire GNU source tree at once with a single set of options. | |
710 | ||
711 | Packages that perform part of compilation may support cross-compilation. | |
712 | In such a case, the host and target machines for the program may be | |
713 | different. The @code{configure} script should normally treat the | |
714 | specified type of system as both the host and the target, thus producing | |
715 | a program which works for the same type of machine that it runs on. | |
716 | ||
717 | The way to build a cross-compiler, cross-assembler, or what have you, is | |
718 | to specify the option @samp{--host=@var{hosttype}} when running | |
719 | @code{configure}. This specifies the host system without changing the | |
720 | type of target system. The syntax for @var{hosttype} is the same as | |
721 | described above. | |
722 | ||
723 | Programs for which cross-operation is not meaningful need not accept the | |
724 | @samp{--host} option, because configuring an entire operating system for | |
725 | cross-operation is not a meaningful thing. | |
726 | ||
727 | Some programs have ways of configuring themselves automatically. If | |
728 | your program is set up to do this, your @code{configure} script can simply | |
729 | ignore most of its arguments. | |
730 | ||
731 | ||
732 | @node Source Language | |
733 | @chapter Using Languages Other Than C | |
734 | ||
735 | Using a language other than C is like using a non-standard feature: it | |
736 | will cause trouble for users. Even if GCC supports the other language, | |
737 | users may find it inconvenient to have to install the compiler for that | |
738 | other language in order to build your program. So please write in C. | |
739 | ||
740 | There are three exceptions for this rule: | |
741 | ||
742 | @itemize @bullet | |
743 | @item | |
744 | It is okay to use a special language if the same program contains an | |
745 | interpreter for that language. | |
746 | ||
747 | Thus, it is not a problem that GNU Emacs contains code written in Emacs | |
748 | Lisp, because it comes with a Lisp interpreter. | |
749 | ||
750 | @item | |
751 | It is okay to use another language in a tool specifically intended for | |
752 | use with that language. | |
753 | ||
754 | This is okay because the only people who want to build the tool will be | |
755 | those who have installed the other language anyway. | |
756 | ||
757 | @item | |
758 | If an application is not of extremely widespread interest, then perhaps | |
759 | it's not important if the application is inconvenient to install. | |
760 | @end itemize | |
761 | ||
762 | @node Formatting | |
763 | @chapter Formatting Your Source Code | |
764 | ||
765 | It is important to put the open-brace that starts the body of a C | |
766 | function in column zero, and avoid putting any other open-brace or | |
767 | open-parenthesis or open-bracket in column zero. Several tools look | |
768 | for open-braces in column zero to find the beginnings of C functions. | |
769 | These tools will not work on code not formatted that way. | |
770 | ||
771 | It is also important for function definitions to start the name of the | |
772 | function in column zero. This helps people to search for function | |
773 | definitions, and may also help certain tools recognize them. Thus, | |
774 | the proper format is this: | |
775 | ||
776 | @example | |
777 | static char * | |
778 | concat (s1, s2) /* Name starts in column zero here */ | |
779 | char *s1, *s2; | |
780 | @{ /* Open brace in column zero here */ | |
781 | @dots{} | |
782 | @} | |
783 | @end example | |
784 | ||
785 | @noindent | |
786 | or, if you want to use @sc{ANSI} C, format the definition like this: | |
787 | ||
788 | @example | |
789 | static char * | |
790 | concat (char *s1, char *s2) | |
791 | @{ | |
792 | @dots{} | |
793 | @} | |
794 | @end example | |
795 | ||
796 | In @sc{ANSI} C, if the arguments don't fit nicely on one line, | |
797 | split it like this: | |
798 | ||
799 | @example | |
800 | int | |
801 | lots_of_args (int an_integer, long a_long, short a_short, | |
802 | double a_double, float a_float) | |
803 | @dots{} | |
804 | @end example | |
805 | ||
806 | For the body of the function, we prefer code formatted like this: | |
807 | ||
808 | @example | |
809 | if (x < foo (y, z)) | |
810 | haha = bar[4] + 5; | |
811 | else | |
812 | @{ | |
813 | while (z) | |
814 | @{ | |
815 | haha += foo (z, z); | |
816 | z--; | |
817 | @} | |
818 | return ++x + bar (); | |
819 | @} | |
820 | @end example | |
821 | ||
822 | We find it easier to read a program when it has spaces before the | |
823 | open-parentheses and after the commas. Especially after the commas. | |
824 | ||
825 | When you split an expression into multiple lines, split it | |
826 | before an operator, not after one. Here is the right way: | |
827 | ||
828 | @example | |
829 | if (foo_this_is_long && bar > win (x, y, z) | |
830 | && remaining_condition) | |
831 | @end example | |
832 | ||
833 | Try to avoid having two operators of different precedence at the same | |
834 | level of indentation. For example, don't write this: | |
835 | ||
836 | @example | |
837 | mode = (inmode[j] == VOIDmode | |
838 | || GET_MODE_SIZE (outmode[j]) > GET_MODE_SIZE (inmode[j]) | |
839 | ? outmode[j] : inmode[j]); | |
840 | @end example | |
841 | ||
842 | Instead, use extra parentheses so that the indentation shows the nesting: | |
843 | ||
844 | @example | |
845 | mode = ((inmode[j] == VOIDmode | |
846 | || (GET_MODE_SIZE (outmode[j]) > GET_MODE_SIZE (inmode[j]))) | |
847 | ? outmode[j] : inmode[j]); | |
848 | @end example | |
849 | ||
850 | Insert extra parentheses so that Emacs will indent the code properly. | |
851 | For example, the following indentation looks nice if you do it by hand, | |
852 | but Emacs would mess it up: | |
853 | ||
854 | @example | |
855 | v = rup->ru_utime.tv_sec*1000 + rup->ru_utime.tv_usec/1000 | |
856 | + rup->ru_stime.tv_sec*1000 + rup->ru_stime.tv_usec/1000; | |
857 | @end example | |
858 | ||
859 | But adding a set of parentheses solves the problem: | |
860 | ||
861 | @example | |
862 | v = (rup->ru_utime.tv_sec*1000 + rup->ru_utime.tv_usec/1000 | |
863 | + rup->ru_stime.tv_sec*1000 + rup->ru_stime.tv_usec/1000); | |
864 | @end example | |
865 | ||
866 | Format do-while statements like this: | |
867 | ||
868 | @example | |
869 | do | |
870 | @{ | |
871 | a = foo (a); | |
872 | @} | |
873 | while (a > 0); | |
874 | @end example | |
875 | ||
876 | Please use formfeed characters (control-L) to divide the program into | |
877 | pages at logical places (but not within a function). It does not matter | |
878 | just how long the pages are, since they do not have to fit on a printed | |
879 | page. The formfeeds should appear alone on lines by themselves. | |
880 | ||
881 | ||
882 | @node Comments | |
883 | @chapter Commenting Your Work | |
884 | ||
885 | Every program should start with a comment saying briefly what it is for. | |
886 | Example: @samp{fmt - filter for simple filling of text}. | |
887 | ||
888 | Please put a comment on each function saying what the function does, | |
889 | what sorts of arguments it gets, and what the possible values of | |
890 | arguments mean and are used for. It is not necessary to duplicate in | |
891 | words the meaning of the C argument declarations, if a C type is being | |
892 | used in its customary fashion. If there is anything nonstandard about | |
893 | its use (such as an argument of type @code{char *} which is really the | |
894 | address of the second character of a string, not the first), or any | |
895 | possible values that would not work the way one would expect (such as, | |
896 | that strings containing newlines are not guaranteed to work), be sure | |
897 | to say so. | |
898 | ||
899 | Also explain the significance of the return value, if there is one. | |
900 | ||
901 | Please put two spaces after the end of a sentence in your comments, so | |
902 | that the Emacs sentence commands will work. Also, please write | |
903 | complete sentences and capitalize the first word. If a lower-case | |
904 | identifer comes at the beginning of a sentence, don't capitalize it! | |
905 | Changing the spelling makes it a different identifier. If you don't | |
906 | like starting a sentence with a lower case letter, write the sentence | |
907 | differently (e.g. ``The identifier lower-case is @dots{}''). | |
908 | ||
909 | The comment on a function is much clearer if you use the argument | |
910 | names to speak about the argument values. The variable name itself | |
911 | should be lower case, but write it in upper case when you are speaking | |
912 | about the value rather than the variable itself. Thus, ``the inode | |
913 | number @var{node_num}'' rather than ``an inode''. | |
914 | ||
915 | There is usually no purpose in restating the name of the function in | |
916 | the comment before it, because the reader can see that for himself. | |
917 | There might be an exception when the comment is so long that the function | |
918 | itself would be off the bottom of the screen. | |
919 | ||
920 | There should be a comment on each static variable as well, like this: | |
921 | ||
922 | @example | |
923 | /* Nonzero means truncate lines in the display; | |
924 | zero means continue them. */ | |
925 | ||
926 | int truncate_lines; | |
927 | @end example | |
928 | ||
929 | Every @samp{#endif} should have a comment, except in the case of short | |
930 | conditionals (just a few lines) that are not nested. The comment should | |
931 | state the condition of the conditional that is ending, @emph{including | |
932 | its sense}. @samp{#else} should have a comment describing the condition | |
933 | @emph{and sense} of the code that follows. For example: | |
934 | ||
935 | @example | |
936 | #ifdef foo | |
937 | @dots{} | |
938 | #else /* not foo */ | |
939 | @dots{} | |
940 | #endif /* not foo */ | |
941 | @end example | |
942 | ||
943 | @noindent | |
944 | but, by contrast, write the comments this way for a @samp{#ifndef}: | |
945 | ||
946 | @example | |
947 | #ifndef foo | |
948 | @dots{} | |
949 | #else /* foo */ | |
950 | @dots{} | |
951 | #endif /* foo */ | |
952 | @end example | |
953 | ||
954 | ||
955 | @node Syntactic Conventions | |
956 | @chapter Clean Use of C Constructs | |
957 | ||
958 | Please explicitly declare all arguments to functions. | |
959 | Don't omit them just because they are ints. | |
960 | ||
961 | Declarations of external functions and functions to appear later | |
962 | in the source file should all go in one place near the beginning of | |
963 | the file (somewhere before the first function definition in the file), | |
964 | or else should go in a header file. Don't put extern declarations | |
965 | inside functions. | |
966 | ||
967 | Don't declare multiple variables in one declaration that spans lines. | |
968 | Start a new declaration on each line, instead. For example, instead | |
969 | of this: | |
970 | ||
971 | @example | |
972 | int foo, | |
973 | bar; | |
974 | @end example | |
975 | ||
976 | @noindent | |
977 | write either this: | |
978 | ||
979 | @example | |
980 | int foo, bar; | |
981 | @end example | |
982 | ||
983 | @noindent | |
984 | or this: | |
985 | ||
986 | @example | |
987 | int foo; | |
988 | int bar; | |
989 | @end example | |
990 | ||
991 | @noindent | |
992 | (If they are global variables, each should have a comment preceding it | |
993 | anyway.) | |
994 | ||
995 | When you have an if-else statement nested in another if statement, | |
996 | always put braces around the if-else. Thus, never write like this: | |
997 | ||
998 | @example | |
999 | if (foo) | |
1000 | if (bar) | |
1001 | win (); | |
1002 | else | |
1003 | lose (); | |
1004 | @end example | |
1005 | ||
1006 | @noindent | |
1007 | always like this: | |
1008 | ||
1009 | @example | |
1010 | if (foo) | |
1011 | @{ | |
1012 | if (bar) | |
1013 | win (); | |
1014 | else | |
1015 | lose (); | |
1016 | @} | |
1017 | @end example | |
1018 | ||
1019 | If you have an if statement nested inside of an else statement, | |
1020 | either write @code{else if} on one line, like this, | |
1021 | ||
1022 | @example | |
1023 | if (foo) | |
1024 | @dots{} | |
1025 | else if (bar) | |
1026 | @dots{} | |
1027 | @end example | |
1028 | ||
1029 | @noindent | |
1030 | with its then-part indented like the preceding then-part, or write the | |
1031 | nested if within braces like this: | |
1032 | ||
1033 | @example | |
1034 | if (foo) | |
1035 | @dots{} | |
1036 | else | |
1037 | @{ | |
1038 | if (bar) | |
1039 | @dots{} | |
1040 | @} | |
1041 | @end example | |
1042 | ||
1043 | Don't declare both a structure tag and variables or typedefs in the | |
1044 | same declaration. Instead, declare the structure tag separately | |
1045 | and then use it to declare the variables or typedefs. | |
1046 | ||
1047 | Try to avoid assignments inside if-conditions. For example, don't | |
1048 | write this: | |
1049 | ||
1050 | @example | |
1051 | if ((foo = (char *) malloc (sizeof *foo)) == 0) | |
1052 | fatal ("virtual memory exhausted"); | |
1053 | @end example | |
1054 | ||
1055 | @noindent | |
1056 | instead, write this: | |
1057 | ||
1058 | @example | |
1059 | foo = (char *) malloc (sizeof *foo); | |
1060 | if (foo == 0) | |
1061 | fatal ("virtual memory exhausted"); | |
1062 | @end example | |
1063 | ||
1064 | Don't make the program ugly to placate lint. Please don't insert any | |
1065 | casts to void. Zero without a cast is perfectly fine as a null | |
1066 | pointer constant. | |
1067 | ||
1068 | ||
1069 | @node Names | |
1070 | @chapter Naming Variables and Functions | |
1071 | ||
1072 | Please use underscores to separate words in a name, so that the Emacs | |
1073 | word commands can be useful within them. Stick to lower case; reserve | |
1074 | upper case for macros and enum constants, and for name-prefixes that | |
1075 | follow a uniform convention. | |
1076 | ||
1077 | For example, you should use names like @code{ignore_space_change_flag}; | |
1078 | don't use names like @code{iCantReadThis}. | |
1079 | ||
1080 | Variables that indicate whether command-line options have been | |
1081 | specified should be named after the meaning of the option, not after | |
1082 | the option-letter. A comment should state both the exact meaning of | |
1083 | the option and its letter. For example, | |
1084 | ||
1085 | @example | |
1086 | /* Ignore changes in horizontal whitespace (-b). */ | |
1087 | int ignore_space_change_flag; | |
1088 | @end example | |
1089 | ||
1090 | When you want to define names with constant integer values, use | |
1091 | @code{enum} rather than @samp{#define}. GDB knows about enumeration | |
1092 | constants. | |
1093 | ||
1094 | Use file names of 14 characters or less, to avoid creating gratuitous | |
1095 | problems on System V. | |
1096 | ||
1097 | ||
1098 | @node Using Extensions | |
1099 | @chapter Using Non-standard Features | |
1100 | ||
1101 | Many GNU facilities that already exist support a number of convenient | |
1102 | extensions over the comparable Unix facilities. Whether to use these | |
1103 | extensions in implementing your program is a difficult question. | |
1104 | ||
1105 | On the one hand, using the extensions can make a cleaner program. | |
1106 | On the other hand, people will not be able to build the program | |
3e12f39a | 1107 | unless the other GNU tools are available. This might cause the |
b42b3782 RP |
1108 | program to work on fewer kinds of machines. |
1109 | ||
1110 | With some extensions, it might be easy to provide both alternatives. | |
1111 | For example, you can define functions with a ``keyword'' @code{INLINE} | |
1112 | and define that as a macro to expand into either @code{inline} or | |
1113 | nothing, depending on the compiler. | |
1114 | ||
1115 | In general, perhaps it is best not to use the extensions if you can | |
1116 | straightforwardly do without them, but to use the extensions if they | |
1117 | are a big improvement. | |
1118 | ||
1119 | An exception to this rule are the large, established programs (such as | |
1120 | Emacs) which run on a great variety of systems. Such programs would | |
1121 | be broken by use of GNU extensions. | |
1122 | ||
1123 | Another exception is for programs that are used as part of | |
1124 | compilation: anything that must be compiled with other compilers in | |
1125 | order to bootstrap the GNU compilation facilities. If these require | |
1126 | the GNU compiler, then no one can compile them without having them | |
1127 | installed already. That would be no good. | |
1128 | ||
1129 | Since most computer systems do not yet implement @sc{ANSI} C, using the | |
1130 | @sc{ANSI} C features is effectively using a GNU extension, so the | |
1131 | same considerations apply. (Except for @sc{ANSI} features that we | |
1132 | discourage, such as trigraphs---don't ever use them.) | |
1133 | ||
1134 | @node Semantics | |
1135 | @chapter Program Behaviour for All Programs | |
1136 | ||
1137 | Avoid arbitrary limits on the length or number of @emph{any} data | |
1138 | structure, including filenames, lines, files, and symbols, by allocating | |
1139 | all data structures dynamically. In most Unix utilities, ``long lines | |
1140 | are silently truncated''. This is not acceptable in a GNU utility. | |
1141 | ||
1142 | Utilities reading files should not drop NUL characters, or any other | |
1143 | nonprinting characters @emph{including those with codes above 0177}. The | |
1144 | only sensible exceptions would be utilities specifically intended for | |
1145 | interface to certain types of printers that can't handle those characters. | |
1146 | ||
1147 | Check every system call for an error return, unless you know you wish to | |
1148 | ignore errors. Include the system error text (from @code{perror} or | |
1149 | equivalent) in @emph{every} error message resulting from a failing | |
1150 | system call, as well as the name of the file if any and the name of the | |
1151 | utility. Just ``cannot open foo.c'' or ``stat failed'' is not | |
1152 | sufficient. | |
1153 | ||
1154 | Check every call to @code{malloc} or @code{realloc} to see if it | |
1155 | returned zero. Check @code{realloc} even if you are making the block | |
1156 | smaller; in a system that rounds block sizes to a power of 2, | |
1157 | @code{realloc} may get a different block if you ask for less space. | |
1158 | ||
1159 | In Unix, @code{realloc} can destroy the storage block if it returns | |
1160 | zero. GNU @code{realloc} does not have this bug: if it fails, the | |
1161 | original block is unchanged. Feel free to assume the bug is fixed. If | |
1162 | you wish to run your program on Unix, and wish to avoid lossage in this | |
1163 | case, you can use the GNU @code{malloc}. | |
1164 | ||
1165 | You must expect @code{free} to alter the contents of the block that was | |
1166 | freed. Anything you want to fetch from the block, you must fetch before | |
1167 | calling @code{free}. | |
1168 | ||
1169 | Use @code{getopt_long} to decode arguments, unless the argument syntax | |
1170 | makes this unreasonable. | |
1171 | ||
1172 | When static storage is to be written in during program execution, use | |
1173 | explicit C code to initialize it. Reserve C initialized declarations | |
1174 | for data that will not be changed. | |
1175 | ||
1176 | Try to avoid low-level interfaces to obscure Unix data structures (such | |
1177 | as file directories, utmp, or the layout of kernel memory), since these | |
1178 | are less likely to work compatibly. If you need to find all the files | |
85e44e95 RP |
1179 | in a directory, use @code{readdir} or some other high-level interface. |
1180 | These will be supported compatibly by GNU. | |
b42b3782 | 1181 | |
85e44e95 RP |
1182 | By default, the GNU system will provide the signal handling functions of |
1183 | @sc{BSD} and of @sc{POSIX}. So GNU software should be written to use | |
1184 | these. | |
b42b3782 RP |
1185 | |
1186 | In error checks that detect ``impossible'' conditions, just abort. | |
1187 | There is usually no point in printing any message. These checks | |
1188 | indicate the existence of bugs. Whoever wants to fix the bugs will have | |
1189 | to read the source code and run a debugger. So explain the problem with | |
1190 | comments in the source. The relevant data will be in variables, which | |
1191 | are easy to examine with the debugger, so there is no point moving them | |
1192 | elsewhere. | |
1193 | ||
1194 | ||
1195 | @node Errors | |
1196 | @chapter Formatting Error Messages | |
1197 | ||
1198 | Error messages from compilers should look like this: | |
1199 | ||
1200 | @example | |
1201 | @var{source-file-name}:@var{lineno}: @var{message} | |
1202 | @end example | |
1203 | ||
1204 | Error messages from other noninteractive programs should look like this: | |
1205 | ||
1206 | @example | |
1207 | @var{program}:@var{source-file-name}:@var{lineno}: @var{message} | |
1208 | @end example | |
1209 | ||
1210 | @noindent | |
1211 | when there is an appropriate source file, or like this: | |
1212 | ||
1213 | @example | |
1214 | @var{program}: @var{message} | |
1215 | @end example | |
1216 | ||
1217 | @noindent | |
1218 | when there is no relevant source file. | |
1219 | ||
1220 | In an interactive program (one that is reading commands from a | |
1221 | terminal), it is better not to include the program name in an error | |
1222 | message. The place to indicate which program is running is in the | |
1223 | prompt or with the screen layout. (When the same program runs with | |
1224 | input from a source other than a terminal, it is not interactive and | |
1225 | would do best to print error messages using the noninteractive style.) | |
1226 | ||
1227 | The string @var{message} should not begin with a capital letter when | |
1228 | it follows a program name and/or filename. Also, it should not end | |
1229 | with a period. | |
1230 | ||
1231 | Error messages from interactive programs, and other messages such as | |
1232 | usage messages, should start with a capital letter. But they should not | |
1233 | end with a period. | |
1234 | ||
1235 | ||
1236 | @node Libraries | |
1237 | @chapter Library Behaviour | |
1238 | ||
1239 | Try to make library functions reentrant. If they need to do dynamic | |
1240 | storage allocation, at least try to avoid any nonreentrancy aside from | |
1241 | that of @code{malloc} itself. | |
1242 | ||
1243 | Here are certain name conventions for libraries, to avoid name | |
1244 | conflicts. | |
1245 | ||
1246 | Choose a name prefix for the library, more than two characters long. | |
1247 | All external function and variable names should start with this | |
1248 | prefix. In addition, there should only be one of these in any given | |
1249 | library member. This usually means putting each one in a separate | |
1250 | source file. | |
1251 | ||
1252 | An exception can be made when two external symbols are always used | |
1253 | together, so that no reasonable program could use one without the | |
1254 | other; then they can both go in the same file. | |
1255 | ||
1256 | External symbols that are not documented entry points for the user | |
1257 | should have names beginning with @samp{_}. They should also contain | |
1258 | the chosen name prefix for the library, to prevent collisions with | |
3e12f39a | 1259 | other libraries. These can go in the same files with user entry |
b42b3782 RP |
1260 | points if you like. |
1261 | ||
1262 | Static functions and variables can be used as you like and need not | |
1263 | fit any naming convention. | |
1264 | ||
1265 | ||
1266 | @node Portability | |
1267 | @chapter Portability As It Applies to GNU | |
1268 | ||
1269 | Much of what is called ``portability'' in the Unix world refers to | |
ee7bc291 RP |
1270 | porting to different Unix versions. This is a secondary consideration |
1271 | for GNU software, because its primary purpose is to run on top of one | |
1272 | and only one kernel, the GNU kernel, compiled with one and only one C | |
1273 | compiler, the GNU C compiler. The amount and kinds of variation among | |
1274 | GNU systems on different cpu's will be like the variation among Berkeley | |
1275 | 4.3 systems on different cpu's. | |
1276 | ||
1277 | All users today run GNU software on non-GNU systems. So supporting a | |
1278 | variety of non-GNU systems is desirable; simply not paramount. | |
1279 | The easiest way to achieve portability to a reasonable range of systems | |
1280 | is to use Autoconf. It's unlikely that your program needs to know more | |
1281 | information about the host machine than Autoconf can provide, simply | |
1282 | because most of the programs that need such knowledge have already been | |
1283 | written. | |
b42b3782 RP |
1284 | |
1285 | It is difficult to be sure exactly what facilities the GNU kernel | |
1286 | will provide, since it isn't finished yet. Therefore, assume you can | |
1287 | use anything in 4.3; just avoid using the format of semi-internal data | |
1288 | bases (e.g., directories) when there is a higher-level alternative | |
1289 | (readdir). | |
1290 | ||
1291 | You can freely assume any reasonably standard facilities in the C | |
1292 | language, libraries or kernel, because we will find it necessary to | |
1293 | support these facilities in the full GNU system, whether or not we | |
1294 | have already done so. The fact that there may exist kernels or C | |
1295 | compilers that lack these facilities is irrelevant as long as the GNU | |
1296 | kernel and C compiler support them. | |
1297 | ||
1298 | It remains necessary to worry about differences among cpu types, such | |
1299 | as the difference in byte ordering and alignment restrictions. It's | |
1300 | unlikely that 16-bit machines will ever be supported by GNU, so there | |
1301 | is no point in spending any time to consider the possibility that an | |
1302 | int will be less than 32 bits. | |
1303 | ||
1304 | You can assume that all pointers have the same format, regardless | |
1305 | of the type they point to, and that this is really an integer. | |
1306 | There are some weird machines where this isn't true, but they aren't | |
1307 | important; don't waste time catering to them. Besides, eventually | |
1308 | we will put function prototypes into all GNU programs, and that will | |
1309 | probably make your program work even on weird machines. | |
1310 | ||
1311 | Since some important machines (including the 68000) are big-endian, | |
1312 | it is important not to assume that the address of an int object | |
1313 | is also the address of its least-significant byte. Thus, don't | |
1314 | make the following mistake: | |
1315 | ||
1316 | @example | |
1317 | int c; | |
1318 | @dots{} | |
1319 | while ((c = getchar()) != EOF) | |
1320 | write(file_descriptor, &c, 1); | |
1321 | @end example | |
1322 | ||
1323 | You can assume that it is reasonable to use a meg of memory. Don't | |
1324 | strain to reduce memory usage unless it can get to that level. If | |
1325 | your program creates complicated data structures, just make them in | |
1326 | core and give a fatal error if malloc returns zero. | |
1327 | ||
1328 | If a program works by lines and could be applied to arbitrary | |
1329 | user-supplied input files, it should keep only a line in memory, because | |
1330 | this is not very hard and users will want to be able to operate on input | |
1331 | files that are bigger than will fit in core all at once. | |
1332 | ||
1333 | ||
1334 | @node User Interfaces | |
1335 | @chapter Standards for Command Line Interfaces | |
1336 | ||
1337 | Please don't make the behavior of a utility depend on the name used | |
1338 | to invoke it. It is useful sometimes to make a link to a utility | |
1339 | with a different name, and that should not change what it does. | |
1340 | ||
1341 | Instead, use a run time option or a compilation switch or both | |
1342 | to select among the alternate behaviors. | |
1343 | ||
1344 | It is a good idea to follow the @sc{POSIX} guidelines for the | |
1345 | command-line options of a program. The easiest way to do this is to use | |
1346 | @code{getopt} to parse them. Note that the GNU version of @code{getopt} | |
1347 | will normally permit options anywhere among the arguments unless the | |
1348 | special argument @samp{--} is used. This is not what @sc{POSIX} | |
1349 | specifies; it is a GNU extension. | |
1350 | ||
1351 | Please define long-named options that are equivalent to the | |
1352 | single-letter Unix-style options. We hope to make GNU more user | |
1353 | friendly this way. This is easy to do with the GNU function | |
1354 | @code{getopt_long}. | |
1355 | ||
1356 | It is usually a good idea for file names given as ordinary arguments | |
1357 | to be input files only; any output files would be specified using | |
1358 | options (preferably @samp{-o}). Even if you allow an output file name | |
1359 | as an ordinary argument for compatibility, try to provide a suitable | |
1360 | option as well. This will lead to more consistency among GNU | |
1361 | utilities, so that there are fewer idiosyncracies for users to | |
1362 | remember. | |
1363 | ||
1364 | Programs should support an option @samp{--version} which prints the | |
1365 | program's version number, and an option @samp{--help} which prints | |
1366 | option usage information. | |
1367 | ||
1368 | ||
1369 | @node Documentation | |
1370 | @chapter Documenting Programs | |
1371 | ||
1372 | Please use Texinfo for documenting GNU programs. See the Texinfo | |
1373 | manual, either the hardcopy or the version in the GNU Emacs Info | |
ee7bc291 RP |
1374 | subsystem (@kbd{C-h i}). See existing GNU Texinfo files (e.g. those |
1375 | under the @file{man/} directory in the GNU Emacs Distribution) for | |
1376 | examples. | |
b42b3782 RP |
1377 | |
1378 | The title page of the manual should state the version of the program | |
1379 | which the manual applies to. The Top node of the manual should also | |
1380 | contain this information. If the manual is changing more frequently | |
1381 | than or independent of the program, also state a version number for | |
1382 | the manual in both of these places. | |
1383 | ||
1384 | The manual should document all command-line arguments and all | |
1385 | commands. It should give examples of their use. But don't organize | |
1386 | the manual as a list of features. Instead, organize it by the | |
1387 | concepts a user will have before reaching that point in the manual. | |
1388 | Address the goals that a user will have in mind, and explain how to | |
1389 | accomplish them. | |
1390 | ||
ee7bc291 RP |
1391 | In addition to its manual, the package should have a file named |
1392 | @file{NEWS} which contains a list of user-visible changes worth | |
1393 | mentioning. In each new release, add items to the front of the file, | |
1394 | and identify the version they pertain to. Don't discard old items. | |
1395 | This way, a user upgrading from any previous version can see what | |
1396 | is new. | |
b42b3782 RP |
1397 | |
1398 | @node Releases | |
1399 | @chapter Making Releases | |
1400 | ||
1401 | Package the distribution of Foo version 69.96 in a tar file named | |
1402 | @file{foo-69.96.tar}. It should unpack into a subdirectory named | |
1403 | @file{foo-69.96}. | |
1404 | ||
1405 | Building and installing the program should never modify any of the files | |
1406 | contained in the distribution. This means that all the files that form | |
1407 | part of the program in any way must be classified into @dfn{source | |
1408 | files} and @dfn{non-source files}. Source files are written by humans | |
1409 | and never changed automatically; non-source files are produced from | |
1410 | source files by programs under the control of the Makefile. | |
1411 | ||
1412 | Naturally, all the source files must be in the distribution. It is okay | |
1413 | to include non-source files in the distribution, provided they are | |
1414 | up-to-date and machine-independent, so that building the distribution | |
1415 | normally will never modify them. We commonly included non-source files | |
1416 | produced by Bison, Lex, @TeX{}, and Makeinfo; this helps avoid | |
1417 | unnecessary dependencies between our distributions, so that users can | |
1418 | install whichever packages they want to install. | |
1419 | ||
1420 | Non-source files that might actually be modified by building and | |
1421 | installing the program should @strong{never} be included in the | |
1422 | distribution. So if you do distribute non-source files, always make | |
1423 | sure they are up to date when you make a new distribution. | |
1424 | ||
1425 | Make sure that no file name in the distribution is no more than 14 | |
1426 | characters long. Nowadays, there are systems that adhere to a foolish | |
1427 | interpretation of the POSIX standard which holds that they should refuse | |
1428 | to open a longer name, rather than truncating as they did in the past. | |
1429 | ||
1430 | Try to make sure that all the file names will be unique on MS-DOG. A | |
1431 | name on MS-DOG consists of up to 8 characters, optionally followed by a | |
1432 | period and up to three characters. MS-DOG will truncate extra | |
1433 | characters both before and after the period. Thus, | |
1434 | @file{foobarhacker.c} and @file{foobarhacker.o} are not ambiguous; they | |
91633020 | 1435 | are truncated to @file{foobarha.c} and @file{foobarha.o}, which are |
b42b3782 RP |
1436 | distinct. |
1437 | ||
1438 | Include in your distribution a copy of the @file{texinfo.tex} you used | |
1439 | to test print any @file{*.texinfo} files. | |
1440 | ||
a60ff512 RP |
1441 | Likewise, if your program uses small GNU software packages like regex, |
1442 | getopt, obstack, or termcap, include them in the distribution file. | |
1443 | Leaving them out would make the distribution file a little smaller at | |
1444 | the expense of possible inconvenience to a user who doesn't know what | |
1445 | other files to get. | |
b42b3782 | 1446 | @bye |