📄 standards.texi
字号:
int ignore_space_change_flag;@end exampleWhen you want to define names with constant integer values, use@code{enum} rather than @samp{#define}. GDB knows about enumerationconstants.Use file names of 14 characters or less, to avoid creating gratuitousproblems on System V.@node Using Extensions@chapter Using Non-standard FeaturesMany GNU facilities that already exist support a number of convenientextensions over the comparable Unix facilities. Whether to use theseextensions in implementing your program is a difficult question.On the one hand, using the extensions can make a cleaner program.On the other hand, people will not be able to build the programunless the other GNU tools are available. This might cause theprogram to work on fewer kinds of machines.With some extensions, it might be easy to provide both alternatives.For example, you can define functions with a ``keyword'' @code{INLINE}and define that as a macro to expand into either @code{inline} ornothing, depending on the compiler.In general, perhaps it is best not to use the extensions if you canstraightforwardly do without them, but to use the extensions if theyare a big improvement.An exception to this rule are the large, established programs (such asEmacs) which run on a great variety of systems. Such programs wouldbe broken by use of GNU extensions.Another exception is for programs that are used as part ofcompilation: anything that must be compiled with other compilers inorder to bootstrap the GNU compilation facilities. If these requirethe GNU compiler, then no one can compile them without having theminstalled already. That would be no good.Since most computer systems do not yet implement @sc{ANSI} C, using the@sc{ANSI} C features is effectively using a GNU extension, so thesame considerations apply. (Except for @sc{ANSI} features that wediscourage, such as trigraphs---don't ever use them.)@node Semantics@chapter Program Behaviour for All ProgramsAvoid arbitrary limits on the length or number of @emph{any} datastructure, including filenames, lines, files, and symbols, by allocatingall data structures dynamically. In most Unix utilities, ``long linesare silently truncated''. This is not acceptable in a GNU utility.Utilities reading files should not drop NUL characters, or any othernonprinting characters @emph{including those with codes above 0177}. Theonly sensible exceptions would be utilities specifically intended forinterface to certain types of printers that can't handle those characters.Check every system call for an error return, unless you know you wish toignore errors. Include the system error text (from @code{perror} orequivalent) in @emph{every} error message resulting from a failingsystem call, as well as the name of the file if any and the name of theutility. Just ``cannot open foo.c'' or ``stat failed'' is notsufficient.Check every call to @code{malloc} or @code{realloc} to see if itreturned zero. Check @code{realloc} even if you are making the blocksmaller; in a system that rounds block sizes to a power of 2,@code{realloc} may get a different block if you ask for less space.In Unix, @code{realloc} can destroy the storage block if it returnszero. GNU @code{realloc} does not have this bug: if it fails, theoriginal block is unchanged. Feel free to assume the bug is fixed. Ifyou wish to run your program on Unix, and wish to avoid lossage in thiscase, you can use the GNU @code{malloc}.You must expect @code{free} to alter the contents of the block that wasfreed. Anything you want to fetch from the block, you must fetch beforecalling @code{free}.Use @code{getopt_long} to decode arguments, unless the argument syntaxmakes this unreasonable.When static storage is to be written in during program execution, useexplicit C code to initialize it. Reserve C initialized declarationsfor data that will not be changed.Try to avoid low-level interfaces to obscure Unix data structures (suchas file directories, utmp, or the layout of kernel memory), since theseare less likely to work compatibly. If you need to find all the filesin a directory, use @code{readdir} or some other high-level interface.These will be supported compatibly by GNU.By default, the GNU system will provide the signal handling functions of@sc{BSD} and of @sc{POSIX}. So GNU software should be written to usethese.In error checks that detect ``impossible'' conditions, just abort.There is usually no point in printing any message. These checksindicate the existence of bugs. Whoever wants to fix the bugs will haveto read the source code and run a debugger. So explain the problem withcomments in the source. The relevant data will be in variables, whichare easy to examine with the debugger, so there is no point moving themelsewhere.@node Errors@chapter Formatting Error MessagesError messages from compilers should look like this:@example@var{source-file-name}:@var{lineno}: @var{message}@end exampleError messages from other noninteractive programs should look like this:@example@var{program}:@var{source-file-name}:@var{lineno}: @var{message}@end example@noindentwhen there is an appropriate source file, or like this:@example@var{program}: @var{message}@end example@noindentwhen there is no relevant source file.In an interactive program (one that is reading commands from aterminal), it is better not to include the program name in an errormessage. The place to indicate which program is running is in theprompt or with the screen layout. (When the same program runs withinput from a source other than a terminal, it is not interactive andwould do best to print error messages using the noninteractive style.)The string @var{message} should not begin with a capital letter whenit follows a program name and/or filename. Also, it should not endwith a period.Error messages from interactive programs, and other messages such asusage messages, should start with a capital letter. But they should notend with a period.@node Libraries@chapter Library BehaviourTry to make library functions reentrant. If they need to do dynamicstorage allocation, at least try to avoid any nonreentrancy aside fromthat of @code{malloc} itself.Here are certain name conventions for libraries, to avoid nameconflicts.Choose a name prefix for the library, more than two characters long.All external function and variable names should start with thisprefix. In addition, there should only be one of these in any givenlibrary member. This usually means putting each one in a separatesource file.An exception can be made when two external symbols are always usedtogether, so that no reasonable program could use one without theother; then they can both go in the same file.External symbols that are not documented entry points for the usershould have names beginning with @samp{_}. They should also containthe chosen name prefix for the library, to prevent collisions withother libraries. These can go in the same files with user entrypoints if you like.Static functions and variables can be used as you like and need notfit any naming convention.@node Portability@chapter Portability As It Applies to GNUMuch of what is called ``portability'' in the Unix world refers toporting to different Unix versions. This is a secondary considerationfor GNU software, because its primary purpose is to run on top of oneand only one kernel, the GNU kernel, compiled with one and only one Ccompiler, the GNU C compiler. The amount and kinds of variation amongGNU systems on different cpu's will be like the variation among Berkeley4.3 systems on different cpu's.All users today run GNU software on non-GNU systems. So supporting avariety of non-GNU systems is desirable; simply not paramount.The easiest way to achieve portability to a reasonable range of systemsis to use Autoconf. It's unlikely that your program needs to know moreinformation about the host machine than Autoconf can provide, simplybecause most of the programs that need such knowledge have already beenwritten.It is difficult to be sure exactly what facilities the GNU kernelwill provide, since it isn't finished yet. Therefore, assume you canuse anything in 4.3; just avoid using the format of semi-internal databases (e.g., directories) when there is a higher-level alternative(readdir).You can freely assume any reasonably standard facilities in the Clanguage, libraries or kernel, because we will find it necessary tosupport these facilities in the full GNU system, whether or not wehave already done so. The fact that there may exist kernels or Ccompilers that lack these facilities is irrelevant as long as the GNUkernel and C compiler support them.It remains necessary to worry about differences among cpu types, suchas the difference in byte ordering and alignment restrictions. It'sunlikely that 16-bit machines will ever be supported by GNU, so thereis no point in spending any time to consider the possibility that anint will be less than 32 bits.You can assume that all pointers have the same format, regardlessof the type they point to, and that this is really an integer.There are some weird machines where this isn't true, but they aren'timportant; don't waste time catering to them. Besides, eventuallywe will put function prototypes into all GNU programs, and that willprobably make your program work even on weird machines.Since some important machines (including the 68000) are big-endian,it is important not to assume that the address of an int objectis also the address of its least-significant byte. Thus, don'tmake the following mistake:@exampleint c;@dots{}while ((c = getchar()) != EOF) write(file_descriptor, &c, 1);@end exampleYou can assume that it is reasonable to use a meg of memory. Don'tstrain to reduce memory usage unless it can get to that level. Ifyour program creates complicated data structures, just make them incore and give a fatal error if malloc returns zero.If a program works by lines and could be applied to arbitraryuser-supplied input files, it should keep only a line in memory, becausethis is not very hard and users will want to be able to operate on inputfiles that are bigger than will fit in core all at once.@node User Interfaces@chapter Standards for Command Line InterfacesPlease don't make the behavior of a utility depend on the name usedto invoke it. It is useful sometimes to make a link to a utilitywith a different name, and that should not change what it does.Instead, use a run time option or a compilation switch or bothto select among the alternate behaviors.It is a good idea to follow the @sc{POSIX} guidelines for thecommand-line options of a program. The easiest way to do this is to use@code{getopt} to parse them. Note that the GNU version of @code{getopt}will normally permit options anywhere among the arguments unless thespecial argument @samp{--} is used. This is not what @sc{POSIX}specifies; it is a GNU extension.Please define long-named options that are equivalent to thesingle-letter Unix-style options. We hope to make GNU more userfriendly this way. This is easy to do with the GNU function@code{getopt_long}.It is usually a good idea for file names given as ordinary argumentsto be input files only; any output files would be specified usingoptions (preferably @samp{-o}). Even if you allow an output file nameas an ordinary argument for compatibility, try to provide a suitableoption as well. This will lead to more consistency among GNUutilities, so that there are fewer idiosyncracies for users toremember.Programs should support an option @samp{--version} which prints theprogram's version number, and an option @samp{--help} which printsoption usage information.@node Documentation@chapter Documenting ProgramsPlease use Texinfo for documenting GNU programs. See the Texinfomanual, either the hardcopy or the version in the GNU Emacs Infosubsystem (@kbd{C-h i}). See existing GNU Texinfo files (e.g. thoseunder the @file{man/} directory in the GNU Emacs Distribution) forexamples.The title page of the manual should state the version of the programwhich the manual applies to. The Top node of the manual should alsocontain this information. If the manual is changing more frequentlythan or independent of the program, also state a version number forthe manual in both of these places.The manual should document all command-line arguments and allcommands. It should give examples of their use. But don't organizethe manual as a list of features. Instead, organize it by theconcepts a user will have before reaching that point in the manual.Address the goals that a user will have in mind, and explain how toaccomplish them.In addition to its manual, the package should have a file named@file{NEWS} which contains a list of user-visible changes worthmentioning. In each new release, add items to the front of the file,and identify the version they pertain to. Don't discard old items.This way, a user upgrading from any previous version can see whatis new.@node Releases@chapter Making ReleasesPackage the distribution of Foo version 69.96 in a tar file named@file{foo-69.96.tar}. It should unpack into a subdirectory named@file{foo-69.96}.Building and installing the program should never modify any of the filescontained in the distribution. This means that all the files that formpart of the program in any way must be classified into @dfn{sourcefiles} and @dfn{non-source files}. Source files are written by humansand never changed automatically; non-source files are produced fromsource files by programs under the control of the Makefile.Naturally, all the source files must be in the distribution. It is okayto include non-source files in the distribution, provided they areup-to-date and machine-independent, so that building the distributionnormally will never modify them. We commonly included non-source filesproduced by Bison, Lex, @TeX{}, and Makeinfo; this helps avoidunnecessary dependencies between our distributions, so that users caninstall whichever packages they want to install.Non-source files that might actually be modified by building andinstalling the program should @strong{never} be included in thedistribution. So if you do distribute non-source files, always makesure they are up to date when you make a new distribution.Make sure that no file name in the distribution is no more than 14characters long. Nowadays, there are systems that adhere to a foolishinterpretation of the POSIX standard which holds that they should refuseto open a longer name, rather than truncating as they did in the past.Try to make sure that all the file names will be unique on MS-DOG. Aname on MS-DOG consists of up to 8 characters, optionally followed by aperiod and up to three characters. MS-DOG will truncate extracharacters both before and after the period. Thus,@file{foobarhacker.c} and @file{foobarhacker.o} are not ambiguous; theyare truncated to @file{foobarha.c} and @file{foobarha.o}, which aredistinct.Include in your distribution a copy of the @file{texinfo.tex} you usedto test print any @file{*.texinfo} files.Likewise, if your program uses small GNU software packages like regex,getopt, obstack, or termcap, include them in the distribution file.Leaving them out would make the distribution file a little smaller atthe expense of possible inconvenience to a user who doesn't know whatother files to get.@bye
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -