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