6 |
7u83 |
1 |
<?xml version="1.0" standalone="no"?>
|
|
|
2 |
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
|
|
|
3 |
"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd">
|
|
|
4 |
|
|
|
5 |
<!--
|
|
|
6 |
$Id$
|
|
|
7 |
-->
|
|
|
8 |
|
|
|
9 |
<article>
|
|
|
10 |
<articleinfo>
|
|
|
11 |
<title>Style Guide</title>
|
|
|
12 |
|
|
|
13 |
<corpauthor>The TenDRA Project</corpauthor>
|
|
|
14 |
|
|
|
15 |
<author>
|
|
|
16 |
<firstname>Jeroen</firstname>
|
|
|
17 |
<surname>Ruigrok van der Werven</surname>
|
|
|
18 |
</author>
|
|
|
19 |
<authorinitials>JRvdW</authorinitials>
|
|
|
20 |
<pubdate>2005</pubdate>
|
|
|
21 |
|
|
|
22 |
<copyright>
|
|
|
23 |
<year>2005</year>
|
|
|
24 |
|
|
|
25 |
<holder>The TenDRA Project</holder>
|
|
|
26 |
</copyright>
|
|
|
27 |
</articleinfo>
|
|
|
28 |
|
|
|
29 |
<sect1 id="introduction">
|
|
|
30 |
<title>Introduction</title>
|
|
|
31 |
|
|
|
32 |
</sect1>
|
|
|
33 |
|
|
|
34 |
<sect1 id="coding-style">
|
|
|
35 |
<title>Coding Style</title>
|
|
|
36 |
|
|
|
37 |
<programlisting>
|
|
|
38 |
/* Most single-line comments look like this. */
|
|
|
39 |
</programlisting>
|
|
|
40 |
|
|
|
41 |
<programlisting>
|
|
|
42 |
/*
|
|
|
43 |
* VERY important single-line comments look like this.
|
|
|
44 |
*/
|
|
|
45 |
</programlisting>
|
|
|
46 |
|
|
|
47 |
<programlisting>
|
|
|
48 |
/*
|
|
|
49 |
* Multi-line comments look like this. Make them real sentences. Fill
|
|
|
50 |
* them so they look like real paragraphs.
|
|
|
51 |
*/
|
|
|
52 |
</programlisting>
|
|
|
53 |
|
|
|
54 |
<para>The copyright header should be a multi-line comment:</para>
|
|
|
55 |
|
|
|
56 |
<programlisting>
|
|
|
57 |
/*
|
|
|
58 |
* Copyright (c) 1984-2025 John Q. Public. All Rights Reserved.
|
|
|
59 |
*
|
|
|
60 |
* Long, boring license goes here, but redacted for brevity
|
|
|
61 |
*/
|
|
|
62 |
</programlisting>
|
|
|
63 |
|
|
|
64 |
<para>After any copyright header, there is a blank line, and the rcsid for
|
|
|
65 |
source files. Version control system ID tags should only exist once in
|
|
|
66 |
a file (unlike in this one). Non-C/C++ source files follow the example
|
|
|
67 |
above, while C/C++ source files follow the one below. All VCS (version
|
|
|
68 |
control system) revision identification in files obtained from elsewhere
|
|
|
69 |
should be maintained, including, where applicable, multiple IDs showing
|
|
|
70 |
a file's history. In general, do not edit foreign IDs or their
|
|
|
71 |
infrastructure. Unless otherwise wrapped (such as #if
|
|
|
72 |
defined(LIBC_SCCS)), enclose both in #if 0 ... #endif to hide any
|
|
|
73 |
uncompilable bits and to keep the IDs out of object files. Only add
|
|
|
74 |
From: in front of foreign VCS IDs if the file is renamed.</para>
|
|
|
75 |
|
|
|
76 |
<programlisting>
|
|
|
77 |
#if 0
|
|
|
78 |
#ifndef lint
|
|
|
79 |
static char sccsid[] = "@(#)style 1.14 (Berkeley) 4/28/95";
|
|
|
80 |
#endif /* not lint */
|
|
|
81 |
#endif
|
|
|
82 |
|
|
|
83 |
#include <sys/cdefs.h>
|
|
|
84 |
__FBSDID("$FreeBSD: src/share/man/man9/style.9,v 1.110.2.1 2005/03/01 12:44:49 brueffer Exp $");
|
|
|
85 |
</programlisting>
|
|
|
86 |
|
|
|
87 |
<para>Leave another blank line before the header files.</para>
|
|
|
88 |
|
|
|
89 |
<para>Kernel include files (i.e. sys/*.h) come first; normally, include
|
|
|
90 |
<sys/types.h> OR <sys/param.h> but not both.
|
|
|
91 |
<sys/types.h> includes <sys/cdefs.h> and it is okay to
|
|
|
92 |
depend on that.</para>
|
|
|
93 |
|
|
|
94 |
<programlisting>
|
|
|
95 |
#include <sys/types.h> /* Non-local includes in angle brackets. */
|
|
|
96 |
</programlisting>
|
|
|
97 |
|
|
|
98 |
<para>Leave a blank line before the next group, the /usr/include files,
|
|
|
99 |
which should be sorted alphabetically by name.</para>
|
|
|
100 |
|
|
|
101 |
<programlisting>
|
|
|
102 |
#include <stdio.h>
|
|
|
103 |
</programlisting>
|
|
|
104 |
|
|
|
105 |
<para>Global pathnames are defined in <paths.h>. Pathnames local to
|
|
|
106 |
the program go in "pathnames.h" in the local directory.</para>
|
|
|
107 |
|
|
|
108 |
<programlisting>
|
|
|
109 |
#include <paths.h>
|
|
|
110 |
</programlisting>
|
|
|
111 |
|
|
|
112 |
<para>Leave another blank line before the user include files.</para>
|
|
|
113 |
|
|
|
114 |
#include "pathnames.h" /* Local includes in double quotes. */
|
|
|
115 |
|
|
|
116 |
<para>Do not #define or declare names in the implementation namespace
|
|
|
117 |
except for implementing application interfaces.</para>
|
|
|
118 |
|
|
|
119 |
<para>The names of unsafe macros (ones that have side effects), and the
|
|
|
120 |
names of macros for manifest constants, are all in uppercase. The
|
|
|
121 |
expansions of expression-like macros are either a single token or have
|
|
|
122 |
outer parentheses. Put a single tab character between the #define and
|
|
|
123 |
the macro name. If a macro is an inline expansion of a function, the
|
|
|
124 |
function name is all in lowercase and the macro has the same name all in
|
|
|
125 |
uppercase. Right-justify the backslashes; it makes it easier to read.
|
|
|
126 |
If the macro encapsulates a compound statement, enclose it in a do loop,
|
|
|
127 |
so that it can safely be used in if statements. Any final
|
|
|
128 |
statement-terminating semicolon should be supplied by the macro
|
|
|
129 |
invocation rather than the macro, to make parsing easier for
|
|
|
130 |
pretty-printers and editors.</para>
|
|
|
131 |
|
|
|
132 |
<programlisting>
|
|
|
133 |
#define MACRO(x, y) do { \
|
|
|
134 |
variable = (x) + (y); \
|
|
|
135 |
(y) += 2; \
|
|
|
136 |
} while (0)
|
|
|
137 |
</programlisting>
|
|
|
138 |
|
|
|
139 |
<para>When code is conditionally compiled using #ifdef or #if, a comment
|
|
|
140 |
may be added following the matching #endif or #else to permit the reader
|
|
|
141 |
to easily discern where conditionally compiled code regions end. This
|
|
|
142 |
comment should be used only for (subjectively) long regions, regions
|
|
|
143 |
greater than 20 lines, or where a series of nested #ifdefs may be
|
|
|
144 |
confusing to the reader. Exceptions may be made for cases where code is
|
|
|
145 |
conditionally not compiled for the purposes of lint(1), even though the
|
|
|
146 |
uncompiled region may be small. The comment should be separated from
|
|
|
147 |
the #endif or #else by a single space. For short conditionally compiled
|
|
|
148 |
regions, a closing comment should not be used.</para>
|
|
|
149 |
|
|
|
150 |
<para>The comment for #endif should match the expression used in the
|
|
|
151 |
corresponding #if or #ifdef. The comment for #else and #elif should
|
|
|
152 |
match the inverse of the expression(s) used in the preceding #if and/or
|
|
|
153 |
#elif statements. In the comments, the subexpression defined(FOO) is
|
|
|
154 |
abbreviated as FOO. For the purposes of comments, #ifndef FOO is
|
|
|
155 |
treated as #if !defined(FOO).</para>
|
|
|
156 |
|
|
|
157 |
<programlisting>
|
|
|
158 |
#ifdef KTRACE
|
|
|
159 |
#include <sys/ktrace.h>
|
|
|
160 |
#endif
|
|
|
161 |
|
|
|
162 |
#ifdef COMPAT_43
|
|
|
163 |
/* A large region here, or other conditional code. */
|
|
|
164 |
#else /* !COMPAT_43 */
|
|
|
165 |
/* Or here. */
|
|
|
166 |
#endif /* COMPAT_43 */
|
|
|
167 |
|
|
|
168 |
#ifndef COMPAT_43
|
|
|
169 |
/* Yet another large region here, or other conditional code. */
|
|
|
170 |
#else /* COMPAT_43 */
|
|
|
171 |
/* Or here. */
|
|
|
172 |
#endif /* !COMPAT_43 */
|
|
|
173 |
</programlisting>
|
|
|
174 |
|
|
|
175 |
<para>The project is slowly moving to use the ISO/IEC 9899:1999 (ISO
|
|
|
176 |
C99) unsigned integer identifiers of the form uintXX_t in preference to
|
|
|
177 |
the older BSD style integer identifiers of the form u_intXX_t. New code
|
|
|
178 |
should use the former, and old code should be converted to the new form
|
|
|
179 |
if other major work is being done in that area and there is no
|
|
|
180 |
overriding reason to prefer the older BSD style. Like white space
|
|
|
181 |
commits, care should be taken in making uintXX_t only commits.</para>
|
|
|
182 |
|
|
|
183 |
<para>Enumeration values are all uppercase.</para>
|
|
|
184 |
|
|
|
185 |
<programlisting>
|
|
|
186 |
enum enumtype { ONE, TWO } et;
|
|
|
187 |
</programlisting>
|
|
|
188 |
|
|
|
189 |
<para>In declarations, do not put any whitespace between asterisks and
|
|
|
190 |
adjacent tokens, except for tokens that are identifiers related to
|
|
|
191 |
types. (These identifiers are the names of basic types, type
|
|
|
192 |
qualifiers, and typedef names other than the one being declared.)
|
|
|
193 |
Separate these identifiers from asterisks using a single space.</para>
|
|
|
194 |
|
|
|
195 |
<para>When declaring variables in structures, declare them sorted by use,
|
|
|
196 |
then by size (largest to smallest), and then in alphabetical order. The
|
|
|
197 |
first category normally does not apply, but there are exceptions. Each
|
|
|
198 |
one gets its own line. Try to make the structure readable by aligning
|
|
|
199 |
the member names using either one or two tabs depending upon your
|
|
|
200 |
judgment. You should use one tab only if it suffices to align at least
|
|
|
201 |
90% of the member names. Names following extremely long types should be
|
|
|
202 |
separated by a single space.</para>
|
|
|
203 |
|
|
|
204 |
<para>Major structures should be declared at the top of the file in which
|
|
|
205 |
they are used, or in separate header files if they are used in multiple
|
|
|
206 |
source files. Use of the structures should be by separate declarations
|
|
|
207 |
and should be extern if they are declared in a header file.</para>
|
|
|
208 |
|
|
|
209 |
<programlisting>
|
|
|
210 |
struct foo {
|
|
|
211 |
struct foo *next; /* List of active foo. */
|
|
|
212 |
struct mumble amumble; /* Comment for mumble. */
|
|
|
213 |
int bar; /* Try to align the comments. */
|
|
|
214 |
struct verylongtypename *baz; /* Won't fit in 2 tabs. */
|
|
|
215 |
};
|
|
|
216 |
struct foo *foohead; /* Head of global foo list. */
|
|
|
217 |
</programlisting>
|
|
|
218 |
|
|
|
219 |
<para>Use queue(3) macros rather than rolling your own lists, whenever
|
|
|
220 |
possible. Thus, the previous example would be better written:</para>
|
|
|
221 |
|
|
|
222 |
<programlisting>
|
|
|
223 |
#include <sys/queue.h>
|
|
|
224 |
|
|
|
225 |
struct foo {
|
|
|
226 |
LIST_ENTRY(foo) link; /* Use queue macros for foo lists. */
|
|
|
227 |
struct mumble amumble; /* Comment for mumble. */
|
|
|
228 |
int bar; /* Try to align the comments. */
|
|
|
229 |
struct verylongtypename *baz; /* Won't fit in 2 tabs. */
|
|
|
230 |
};
|
|
|
231 |
LIST_HEAD(, foo) foohead; /* Head of global foo list. */
|
|
|
232 |
</programlisting>
|
|
|
233 |
|
|
|
234 |
<para>Avoid using typedefs for structure types. Typedefs are problematic
|
|
|
235 |
because they do not properly hide their underlying type; for example you
|
|
|
236 |
need to know if the typedef is the structure itself or a pointer to the
|
|
|
237 |
structure. In addition they must be declared exactly once, whereas an
|
|
|
238 |
incomplete structure type can be mentioned as many times as necessary.
|
|
|
239 |
Typedefs are difficult to use in stand-alone header files: the header
|
|
|
240 |
that defines the typedef must be included before the header that uses
|
|
|
241 |
it, or by the header that uses it (which causes namespace pollution), or
|
|
|
242 |
there must be a back-door mechanism for obtaining the typedef.</para>
|
|
|
243 |
|
|
|
244 |
<para>When convention requires a typedef, make its name match the struct
|
|
|
245 |
tag. Avoid typedefs ending in _t, except as specified in Standard C
|
|
|
246 |
or by POSIX.</para>
|
|
|
247 |
|
|
|
248 |
<programlisting>
|
|
|
249 |
/* Make the structure name match the typedef. */
|
|
|
250 |
typedef struct bar {
|
|
|
251 |
int level;
|
|
|
252 |
} BAR;
|
|
|
253 |
typedef int foo; /* This is foo. */
|
|
|
254 |
typedef const long baz; /* This is baz. */
|
|
|
255 |
</programlisting>
|
|
|
256 |
|
|
|
257 |
<para>All functions are prototyped somewhere.</para>
|
|
|
258 |
|
|
|
259 |
<para>Function prototypes for private functions (i.e., functions not used
|
|
|
260 |
elsewhere) go at the top of the first source module. Functions local
|
|
|
261 |
to one source module should be declared static.</para>
|
|
|
262 |
|
|
|
263 |
<para>Function prototypes should be listed in a logical order, preferably
|
|
|
264 |
alphabetical unless there is a compelling reason to use a different
|
|
|
265 |
ordering.</para>
|
|
|
266 |
|
|
|
267 |
<para>Functions that are used locally in more than one module go into a
|
|
|
268 |
separate header file, e.g. "extern.h".</para>
|
|
|
269 |
|
|
|
270 |
<para>Do not use the __P macro.</para>
|
|
|
271 |
|
|
|
272 |
<para>In general code can be considered new code when it makes up about
|
|
|
273 |
50% or more of the file(s) involved. This is enough to break precedents
|
|
|
274 |
in the existing code and use the current style guidelines.</para>
|
|
|
275 |
|
|
|
276 |
<para>In header files visible to userland applications, prototypes that
|
|
|
277 |
are visible must use either protected names (ones beginning with an
|
|
|
278 |
underscore) or no names with the types. It is preferable to use
|
|
|
279 |
protected names. E.g., use:</para>
|
|
|
280 |
|
|
|
281 |
<programlisting>
|
|
|
282 |
void function(int);
|
|
|
283 |
</programlisting>
|
|
|
284 |
|
|
|
285 |
<para>or:</para>
|
|
|
286 |
|
|
|
287 |
<programlisting>
|
|
|
288 |
void function(int _fd);
|
|
|
289 |
</programlisting>
|
|
|
290 |
|
|
|
291 |
<para>Prototypes may have an extra space after a tab to enable function
|
|
|
292 |
names to line up:</para>
|
|
|
293 |
|
|
|
294 |
<programlisting>
|
|
|
295 |
static char *function(int _arg, const char *_arg2, struct foo *_arg3,
|
|
|
296 |
struct bar *_arg4);
|
|
|
297 |
static void usage(void);
|
|
|
298 |
|
|
|
299 |
/*
|
|
|
300 |
* All major routines should have a comment briefly describing what
|
|
|
301 |
* they do. The comment before the "main" routine should describe
|
|
|
302 |
* what the program does.
|
|
|
303 |
*/
|
|
|
304 |
int
|
|
|
305 |
main(int argc, char *argv[])
|
|
|
306 |
{
|
|
|
307 |
char *ep;
|
|
|
308 |
long num;
|
|
|
309 |
int ch;
|
|
|
310 |
</programlisting>
|
|
|
311 |
|
|
|
312 |
<para>For consistency, getopt(3) should be used to parse options. Options
|
|
|
313 |
should be sorted in the getopt(3) call and the switch statement, unless
|
|
|
314 |
parts of the switch cascade. Elements in a switch statement that
|
|
|
315 |
cascade should have a FALLTHROUGH comment. Numerical arguments should
|
|
|
316 |
be checked for accuracy. Code that cannot be reached should have a
|
|
|
317 |
NOTREACHED comment.</para>
|
|
|
318 |
|
|
|
319 |
<programlisting>
|
|
|
320 |
while ((ch = getopt(argc, argv, "abNn:")) != 1)
|
|
|
321 |
switch (ch) { /* Indent the switch. */
|
|
|
322 |
case a: /* Don't indent the case. */
|
|
|
323 |
aflag = 1;
|
|
|
324 |
/* FALLTHROUGH */
|
|
|
325 |
case b:
|
|
|
326 |
bflag = 1;
|
|
|
327 |
break;
|
|
|
328 |
case N:
|
|
|
329 |
Nflag = 1;
|
|
|
330 |
break;
|
|
|
331 |
case n:
|
|
|
332 |
num = strtol(optarg, &ep, 10);
|
|
|
333 |
if (num <= 0 || *ep != '\0') {
|
|
|
334 |
warnx("illegal number, -n argument -- %s",
|
|
|
335 |
optarg);
|
|
|
336 |
usage();
|
|
|
337 |
}
|
|
|
338 |
break;
|
|
|
339 |
case '?':
|
|
|
340 |
default:
|
|
|
341 |
usage();
|
|
|
342 |
/* NOTREACHED */
|
|
|
343 |
}
|
|
|
344 |
argc -= optind;
|
|
|
345 |
argv += optind;
|
|
|
346 |
</programlisting>
|
|
|
347 |
|
|
|
348 |
<para>Space after keywords (if, while, for, return, switch). No braces
|
|
|
349 |
({ and }) are used for control statements with zero or only a single
|
|
|
350 |
statement unless that statement is more than a single line in which case
|
|
|
351 |
they are permitted. Forever loops are done with for's, not
|
|
|
352 |
while's.</para>
|
|
|
353 |
|
|
|
354 |
<programlisting>
|
|
|
355 |
for (p = buf; *p != '\0'; ++p)
|
|
|
356 |
; /* nothing */
|
|
|
357 |
for (;;)
|
|
|
358 |
stmt;
|
|
|
359 |
for (;;) {
|
|
|
360 |
z = a + really + long + statement + that + needs +
|
|
|
361 |
two + lines + gets + indented + four + spaces +
|
|
|
362 |
on + the + second + and + subsequent + lines;
|
|
|
363 |
}
|
|
|
364 |
for (;;) {
|
|
|
365 |
if (cond)
|
|
|
366 |
stmt;
|
|
|
367 |
}
|
|
|
368 |
if (val != NULL)
|
|
|
369 |
val = realloc(val, newsize);
|
|
|
370 |
</programlisting>
|
|
|
371 |
|
|
|
372 |
<para>Parts of a for loop may be left empty. Do not put declarations
|
|
|
373 |
inside blocks unless the routine is unusually complicated.</para>
|
|
|
374 |
|
|
|
375 |
<programlisting>
|
|
|
376 |
for (; cnt < 15; cnt++) {
|
|
|
377 |
stmt1;
|
|
|
378 |
stmt2;
|
|
|
379 |
}
|
|
|
380 |
</programlisting>
|
|
|
381 |
|
|
|
382 |
<para>Indentation is an 8 character tab. Second level indents are four
|
|
|
383 |
spaces. If you have to wrap a long statement, put the operator at the
|
|
|
384 |
end of the line.</para>
|
|
|
385 |
|
|
|
386 |
<programlisting>
|
|
|
387 |
while (cnt < 20 && this_variable_name_is_too_long &&
|
|
|
388 |
ep != NULL)
|
|
|
389 |
z = a + really + long + statement + that + needs +
|
|
|
390 |
two + lines + gets + indented + four + spaces +
|
|
|
391 |
on + the + second + and + subsequent + lines;
|
|
|
392 |
</programlisting>
|
|
|
393 |
|
|
|
394 |
<para>Do not add whitespace at the end of a line, and only use tabs
|
|
|
395 |
followed by spaces to form the indentation. Do not use more spaces than
|
|
|
396 |
a tab will produce and do not use spaces in front of tabs.</para>
|
|
|
397 |
|
|
|
398 |
<para>Closing and opening braces go on the same line as the else. Braces
|
|
|
399 |
that are not necessary may be left out.</para>
|
|
|
400 |
|
|
|
401 |
<programlisting>
|
|
|
402 |
if (test)
|
|
|
403 |
stmt;
|
|
|
404 |
else if (bar) {
|
|
|
405 |
stmt;
|
|
|
406 |
stmt;
|
|
|
407 |
} else
|
|
|
408 |
stmt;
|
|
|
409 |
</programlisting>
|
|
|
410 |
|
|
|
411 |
<para>No spaces after function names. Commas have a space after them. No
|
|
|
412 |
spaces after ( or [ or preceding ] or ) characters.</para>
|
|
|
413 |
|
|
|
414 |
<programlisting>
|
|
|
415 |
error = function(a1, a2);
|
|
|
416 |
if (error != 0)
|
|
|
417 |
exit(error);
|
|
|
418 |
</programlisting>
|
|
|
419 |
|
|
|
420 |
<para>Unary operators do not require spaces, binary operators do. Do not
|
|
|
421 |
use parentheses unless they are required for precedence or unless the
|
|
|
422 |
statement is confusing without them. Remember that other people may
|
|
|
423 |
confuse easier than you. Do YOU understand the following?</para>
|
|
|
424 |
|
|
|
425 |
<programlisting>
|
|
|
426 |
a = b->c[0] + ~d == (e || f) || g && h ? i : j >> 1;
|
|
|
427 |
k = !(l & FLAGS);
|
|
|
428 |
</programlisting>
|
|
|
429 |
|
|
|
430 |
<para>Exits should be 0 on success, or according to the predefined values
|
|
|
431 |
in sysexits(3).</para>
|
|
|
432 |
|
|
|
433 |
<programlisting>
|
|
|
434 |
exit(EX_OK); /*
|
|
|
435 |
* Avoid obvious comments such as
|
|
|
436 |
* "Exit 0 on success."
|
|
|
437 |
*/
|
|
|
438 |
}
|
|
|
439 |
</programlisting>
|
|
|
440 |
|
|
|
441 |
<para>The function type should be on a line by itself preceding the
|
|
|
442 |
function. The opening brace of the function body should be on a line by
|
|
|
443 |
itself.</para>
|
|
|
444 |
|
|
|
445 |
<programlisting>
|
|
|
446 |
static char *
|
|
|
447 |
function(int a1, int a2, float fl, int a4)
|
|
|
448 |
{
|
|
|
449 |
</programlisting>
|
|
|
450 |
|
|
|
451 |
<para>When declaring variables in functions declare them sorted by size,
|
|
|
452 |
then in alphabetical order; multiple ones per line are okay. If a line
|
|
|
453 |
overflows reuse the type keyword.</para>
|
|
|
454 |
|
|
|
455 |
<para>Be careful to not obfuscate the code by initializing variables in
|
|
|
456 |
the declarations. Use this feature only thoughtfully. DO NOT use
|
|
|
457 |
function calls in initializers.</para>
|
|
|
458 |
|
|
|
459 |
<programlisting>
|
|
|
460 |
struct foo one, *two;
|
|
|
461 |
double three;
|
|
|
462 |
int *four, five;
|
|
|
463 |
char *six, seven, eight, nine, ten, eleven, twelve;
|
|
|
464 |
|
|
|
465 |
four = myfunction();
|
|
|
466 |
</programlisting>
|
|
|
467 |
|
|
|
468 |
<para>Do not declare functions inside other functions; ANSI C says that
|
|
|
469 |
such declarations have file scope regardless of the nesting of the
|
|
|
470 |
declaration. Hiding file declarations in what appears to be a local
|
|
|
471 |
scope is undesirable and will elicit complaints from a good
|
|
|
472 |
compiler.</para>
|
|
|
473 |
|
|
|
474 |
<para>Casts and sizeof's are not followed by a space. Note that indent(1)
|
|
|
475 |
does not understand this rule. sizeof's are written with parenthesis
|
|
|
476 |
always. The redundant parenthesis rules do not apply to sizeof(var)
|
|
|
477 |
instances.</para>
|
|
|
478 |
|
|
|
479 |
<para>NULL is the preferred null pointer constant. Use NULL instead of
|
|
|
480 |
(type *)0 or (type *)NULL in contexts where the compiler knows the
|
|
|
481 |
type, e.g., in assignments. Use (type *)NULL in other contexts, in
|
|
|
482 |
particular for all function args. (Casting is essential for variadic
|
|
|
483 |
args and is necessary for other args if the function prototype might
|
|
|
484 |
not be in scope.) Test pointers against NULL, e.g., use:</para>
|
|
|
485 |
|
|
|
486 |
<programlisting>
|
|
|
487 |
(p = f()) == NULL
|
|
|
488 |
</programlisting>
|
|
|
489 |
|
|
|
490 |
<para>not:</para>
|
|
|
491 |
|
|
|
492 |
<programlisting>
|
|
|
493 |
!(p = f())
|
|
|
494 |
</programlisting>
|
|
|
495 |
|
|
|
496 |
<para>Do not use !! for tests unless it is a boolean, e.g. use:</para>
|
|
|
497 |
|
|
|
498 |
<programlisting>
|
|
|
499 |
if (*p == '\0')
|
|
|
500 |
</programlisting>
|
|
|
501 |
|
|
|
502 |
<para>not:</para>
|
|
|
503 |
|
|
|
504 |
<programlisting>
|
|
|
505 |
if (!*p)
|
|
|
506 |
</programlisting>
|
|
|
507 |
|
|
|
508 |
<para>Routines returning void * should not have their return values cast
|
|
|
509 |
to any pointer type.</para>
|
|
|
510 |
|
|
|
511 |
<para>Values in rreettuurrnn statements should be enclosed in
|
|
|
512 |
parentheses.</para>
|
|
|
513 |
|
|
|
514 |
<para>Use err(3) or warn(3), do not roll your own.</para>
|
|
|
515 |
|
|
|
516 |
<programlisting>
|
|
|
517 |
if ((four = malloc(sizeof(struct foo))) == NULL)
|
|
|
518 |
err(1, (char *)NULL);
|
|
|
519 |
if ((six = (int *)overflow()) == NULL)
|
|
|
520 |
errx(1, "number overflowed");
|
|
|
521 |
return (eight);
|
|
|
522 |
}
|
|
|
523 |
</programlisting>
|
|
|
524 |
|
|
|
525 |
<para>Old-style function declarations look like this:</para>
|
|
|
526 |
|
|
|
527 |
<programlisting>
|
|
|
528 |
static char *
|
|
|
529 |
function(a1, a2, fl, a4)
|
|
|
530 |
int a1, a2; /* Declare ints, too, don't default them. */
|
|
|
531 |
float fl; /* Beware double vs. float prototype differences. */
|
|
|
532 |
int a4; /* List in order declared. */
|
|
|
533 |
{
|
|
|
534 |
</programlisting>
|
|
|
535 |
|
|
|
536 |
<para>Use ANSI function declarations unless you explicitly need K&R
|
|
|
537 |
compatibility. Long parameter lists are wrapped with a normal four
|
|
|
538 |
space indent.</para>
|
|
|
539 |
|
|
|
540 |
<para>Variable numbers of arguments should look like this:</para>
|
|
|
541 |
|
|
|
542 |
<programlisting>
|
|
|
543 |
#include <stdarg.h>
|
|
|
544 |
|
|
|
545 |
void
|
|
|
546 |
vaf(const char *fmt, ...)
|
|
|
547 |
{
|
|
|
548 |
va_list ap;
|
|
|
549 |
|
|
|
550 |
va_start(ap, fmt);
|
|
|
551 |
STUFF;
|
|
|
552 |
va_end(ap);
|
|
|
553 |
/* No return needed for void functions. */
|
|
|
554 |
}
|
|
|
555 |
|
|
|
556 |
static void
|
|
|
557 |
usage()
|
|
|
558 |
{
|
|
|
559 |
/* Insert an empty line if the function has no local variables. */
|
|
|
560 |
</programlisting>
|
|
|
561 |
|
|
|
562 |
<para>Use printf(3), not fputs(3), puts(3), putchar(3), whatever; it is
|
|
|
563 |
faster and usually cleaner, not to mention avoiding stupid bugs.</para>
|
|
|
564 |
|
|
|
565 |
<para>Usage statements should look like the manual pages SYNOPSIS. The
|
|
|
566 |
usage statement should be structured in the following order:</para>
|
|
|
567 |
|
|
|
568 |
<itemizedlist>
|
|
|
569 |
<listitem>Options without operands come first, in alphabetical order,
|
|
|
570 |
inside a single set of brackets ([ and ]).</listitem>
|
|
|
571 |
|
|
|
572 |
<listitem>Options with operands come next, also in alphabetical order,
|
|
|
573 |
with each option and its argument inside its own pair of
|
|
|
574 |
brackets.</listitem>
|
|
|
575 |
|
|
|
576 |
<listitem>Required arguments (if any) are next, listed in the order they
|
|
|
577 |
should be specified on the command line.</listitem>
|
|
|
578 |
|
|
|
579 |
<listitem>Finally, any optional arguments should be listed, listed in
|
|
|
580 |
the order they should be specified, and all inside
|
|
|
581 |
brackets.</listitem>
|
|
|
582 |
</itemizedlist>
|
|
|
583 |
|
|
|
584 |
<para>A bar (|) separates either - or options/arguments, and multiple
|
|
|
585 |
options/arguments which are specified together are placed in a single
|
|
|
586 |
set of brackets.</para>
|
|
|
587 |
|
|
|
588 |
<programlisting>
|
|
|
589 |
"usage: f [-aDde] [-b b_arg] [-m m_arg] req1 req2 [opt1 [opt2]]\n"
|
|
|
590 |
"usage: f [-a | -b] [-c [-dEe] [-n number]]\n"
|
|
|
591 |
|
|
|
592 |
(void)fprintf(stderr, "usage: f [-ab]\n");
|
|
|
593 |
exit(EX_USAGE);
|
|
|
594 |
}
|
|
|
595 |
</programlisting>
|
|
|
596 |
|
|
|
597 |
<para>Note that the manual page options description should list the
|
|
|
598 |
options in pure alphabetical order. That is, without regard to whether
|
|
|
599 |
an option takes arguments or not. The alphabetical ordering should take
|
|
|
600 |
into account the case ordering shown above.</para>
|
|
|
601 |
|
|
|
602 |
<para>Stylistic changes (including whitespace changes) are hard on the
|
|
|
603 |
source repository and are to be avoided without good reason. Code that
|
|
|
604 |
is approximately FreeBSD KNF style compliant in the repository must not
|
|
|
605 |
diverge from compliance.</para>
|
|
|
606 |
|
|
|
607 |
<para>Whenever possible, code should be run through a code checker (e.g.,
|
|
|
608 |
lint(1) or gcc Wall) and produce minimal warnings.</para>
|
|
|
609 |
|
|
|
610 |
<para>All makefiles should have an SCM ID at the start of the file,
|
|
|
611 |
followed by a blank line.</para>
|
|
|
612 |
|
|
|
613 |
<programlisting>
|
|
|
614 |
# $FreeBSD$
|
|
|
615 |
</programlisting>
|
|
|
616 |
|
|
|
617 |
<para>.PATH: comes next if needed, and is spelled .PATH: , with a single
|
|
|
618 |
ASCII space after a colon. Do not use the VPATH variable.</para>
|
|
|
619 |
|
|
|
620 |
<para>Special variables (i.e., LIB, SRCS, MLINKS, etc.) are listed in
|
|
|
621 |
order of product, then building and installing a binary. Special
|
|
|
622 |
variables may also be listed in build order: i.e., ones for the primary
|
|
|
623 |
program (or library) first. The general product order is:
|
|
|
624 |
PROG/[SH]LIB/SCRIPTS FILES LINKS [NO]MAN MLINKS INCS SRCS WARNS CFLAGS
|
|
|
625 |
DPADD LDADD. The general build order is: PROG/[SH]LIB/SCRIPTS SRCS
|
|
|
626 |
WARNS CFLAGS DPADD LDADD INCS FILES LINKS [NO]MAN MLINKS.</para>
|
|
|
627 |
|
|
|
628 |
<para>Omit SRCS when using <bsd.prog.mk> and there is a single
|
|
|
629 |
source file named the same as the PROG.</para>
|
|
|
630 |
|
|
|
631 |
|
|
|
632 |
<para>Omit MAN when using <bsd.prog.mk> and the manual page is
|
|
|
633 |
named the same as the PROG, and is in section 1.</para>
|
|
|
634 |
|
|
|
635 |
|
|
|
636 |
<para>All variable assignments are spelled VAR==, i.e., no space between
|
|
|
637 |
the variable name and the ==. Keep values sorted alphabetically, if
|
|
|
638 |
possible.</para>
|
|
|
639 |
|
|
|
640 |
<para>Do not use ++== to set variables that are only set once (or to set
|
|
|
641 |
variables for the first time).</para>
|
|
|
642 |
|
|
|
643 |
<para>Do not use vertical whitespace in simple makefiles, but do use it to
|
|
|
644 |
group locally related things in more complex/longer ones.</para>
|
|
|
645 |
|
|
|
646 |
<para>WARNS comes before CFLAGS, as it is basically a CFLAGS modifier. It
|
|
|
647 |
comes before CFLAGS rather than after CFLAGS so it does not get lost in
|
|
|
648 |
a sea of CFLAGS statements as WARNS is an important thing. The usage of
|
|
|
649 |
WARNS is spelled WARNS?= , so that it may be overridden on the command
|
|
|
650 |
line or in /etc/make.conf.</para>
|
|
|
651 |
|
|
|
652 |
<para>NO_WERROR= yes should not be used, it defeats the purpose of WARNS.
|
|
|
653 |
It should only be used on the command line and in special circum
|
|
|
654 |
stances.</para>
|
|
|
655 |
|
|
|
656 |
<para>CFLAGS is spelled CFLAGS+= .</para>
|
|
|
657 |
|
|
|
658 |
<para>Listing D's before I's in CFLAGS is preferred for alphabetical
|
|
|
659 |
ordering and to make D's easier to see. The D's often affect
|
|
|
660 |
conditional compilation, and I's tend to be quite long. Split long
|
|
|
661 |
CFLAGS settings between the D's and I's.</para>
|
|
|
662 |
|
|
|
663 |
<para>Do not use GCCisms (such as g and Wall) in CFLAGS.</para>
|
|
|
664 |
|
|
|
665 |
<para>Typically, there is one ASCII tab between VAR== and the value in
|
|
|
666 |
order to start the value in column 9. An ASCII space is allowed for
|
|
|
667 |
variable names that extend beyond column 9. A lack of whitespace is
|
|
|
668 |
also allowed for very long variable names.</para>
|
|
|
669 |
|
|
|
670 |
<para>.include <bsd.*.mk> goes last.</para>
|
|
|
671 |
|
|
|
672 |
<para>Do not use anachronisms like $< and $@. Instead use ${.IMPSRC}
|
|
|
673 |
or ${.ALLSRC} and ${.TARGET}.</para>
|
|
|
674 |
|
|
|
675 |
<para>The desire to express a logical grouping often means not obeying
|
|
|
676 |
some of the above.</para>
|
|
|
677 |
|
|
|
678 |
<para>The simplest program Makefile is:</para>
|
|
|
679 |
|
|
|
680 |
<programlisting>
|
|
|
681 |
# $FreeBSD$
|
|
|
682 |
|
|
|
683 |
PROG= foo
|
|
|
684 |
|
|
|
685 |
.include <bsd.prog.mk>
|
|
|
686 |
</programlisting>
|
|
|
687 |
|
|
|
688 |
<para>The simplest library Makefile is:</para>
|
|
|
689 |
|
|
|
690 |
<programlisting>
|
|
|
691 |
# $FreeBSD$
|
|
|
692 |
|
|
|
693 |
LIB= foo
|
|
|
694 |
SHLIB_MAJOR= 1
|
|
|
695 |
MAN= libfoo.3
|
|
|
696 |
SRCS= foo.c
|
|
|
697 |
|
|
|
698 |
.include <bsd.lib.mk>
|
|
|
699 |
</programlisting>
|
|
|
700 |
</sect1>
|
|
|
701 |
|
|
|
702 |
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
|
|
|
703 |
href="../common/colophon.xml"/>
|
|
|
704 |
</article>
|