style revision 1.10 1 /* $NetBSD: style,v 1.10 1999/01/29 07:24:20 scottr Exp $ */
2
3 /*
4 * Style guide for the 4BSD KNF (Kernel Normal Form).
5 *
6 * from: @(#)style 1.12 (Berkeley) 3/18/94
7 */
8 /*
9 * An indent(1) profile approximating the style outlined in
10 * this document lives in /usr/share/misc/indent.pro. It is a
11 * useful tool to assist in converting code to KNF, but indent(1)
12 * output generated using this profile must not be considered to
13 * be an authoritative reference.
14 */
15
16 /*
17 * VERY important single-line comments look like this.
18 */
19
20 /* Most single-line comments look like this. */
21
22 /*
23 * Multi-line comments look like this. Make them real sentences. Fill
24 * them so they look like real paragraphs.
25 */
26
27 /*
28 * Kernel include files come first; normally, you'll need <sys/types.h>
29 * OR <sys/param.h>, but not both! <sys/types.h> includes <sys/cdefs.h>,
30 * and it's okay to depend on that.
31 */
32 #include <sys/types.h> /* Non-local includes in brackets. */
33
34 /* If it's a network program, put the network include files next. */
35 #include <net/if.h>
36 #include <net/if_dl.h>
37 #include <net/route.h>
38 #include <netinet/in.h>
39 #include <protocols/rwhod.h>
40
41 /*
42 * Then there's a blank line, followed by the /usr include files.
43 * The /usr include files should be sorted!
44 */
45 #include <stdio.h>
46
47 /*
48 * Global pathnames are defined in /usr/include/paths.h. Pathnames local
49 * to the program go in pathnames.h in the local directory.
50 */
51 #include <paths.h>
52
53 /* Then, there's a blank line, and the user include files. */
54 #include "pathnames.h" /* Local includes in double quotes. */
55
56 /*
57 * ANSI function declarations for private functions (i.e. functions not used
58 * elsewhere) go at the top of the source module. Use the __P macro from
59 * the include file <sys/cdefs.h>. Only the kernel has a name associated with
60 * the types, i.e. in the kernel use:
61 *
62 * void function __P((int a));
63 *
64 * in user land use:
65 *
66 * void function __P((int));
67 */
68 static char *function __P((int, const char *));
69 static void usage __P((void));
70
71 /*
72 * Macros are capitalized, parenthesized, and should avoid side-effects.
73 * If they are an inline expansion of a function, the function is defined
74 * all in lowercase, the macro has the same name all in uppercase. If the
75 * macro needs more than a single line, use braces. Right-justify the
76 * backslashes, it makes it easier to read.
77 */
78 #define MACRO(x, y) { \
79 variable = (x) + (y); \
80 (y) += 2; \
81 }
82
83 /* Enum types are capitalized. */
84 enum enumtype { ONE, TWO } et;
85
86 /*
87 * When declaring variables in structures, declare them sorted by use, then
88 * by size, and then by alphabetical order. The first category normally
89 * doesn't apply, but there are exceptions. Each one gets its own line.
90 * Put a tab after the first word, i.e. use "int^Ix;" and "struct^Ifoo *x;".
91 *
92 * Major structures should be declared at the top of the file in which they
93 * are used, or in separate header files, if they are used in multiple
94 * source files. Use of the structures should be by separate declarations
95 * and should be "extern" if they are declared in a header file.
96 */
97 struct foo {
98 struct foo *next; /* List of active foo */
99 struct mumble amumble; /* Comment for mumble */
100 int bar;
101 };
102 struct foo *foohead; /* Head of global foo list */
103
104 /* Make the structure name match the typedef. */
105 typedef struct _bar {
106 int level;
107 } BAR;
108
109 /*
110 * All major routines should have a comment briefly describing what
111 * they do. The comment before the "main" routine should describe
112 * what the program does.
113 */
114 int
115 main(argc, argv)
116 int argc;
117 char *argv[];
118 {
119 extern char *optarg;
120 extern int optind;
121 long num;
122 int ch;
123 char *ep;
124
125 /*
126 * For consistency, getopt should be used to parse options. Options
127 * should be sorted in the getopt call and the switch statement, unless
128 * parts of the switch cascade. Elements in a switch statement that
129 * cascade should have a FALLTHROUGH comment. Numerical arguments
130 * should be checked for accuracy. Code that cannot be reached should
131 * have a NOTREACHED comment.
132 */
133 while ((ch = getopt(argc, argv, "abn")) != -1)
134 switch (ch) { /* Indent the switch. */
135 case 'a': /* Don't indent the case. */
136 aflag = 1;
137 /* FALLTHROUGH */
138 case 'b':
139 bflag = 1;
140 break;
141 case 'n':
142 num = strtol(optarg, &ep, 10);
143 if (num <= 0 || *ep != '\0')
144 err("illegal number -- %s", optarg);
145 break;
146 case '?':
147 default:
148 usage();
149 /* NOTREACHED */
150 }
151 argc -= optind;
152 argv += optind;
153
154 /*
155 * Space after keywords (while, for, return, switch). No braces are
156 * used for control statements with zero or only a single statement.
157 *
158 * Forever loops are done with for's, not while's.
159 */
160 for (p = buf; *p != '\0'; ++p);
161 for (;;)
162 stmt;
163
164 /*
165 * Parts of a for loop may be left empty. Don't put declarations
166 * inside blocks unless the routine is unusually complicated.
167 */
168 for (; cnt < 15; cnt++) {
169 stmt1;
170 stmt2;
171 }
172
173 /* Second level indents are four spaces. */
174 while (cnt < 20)
175 z = a + really + long + statment + that + needs + two lines +
176 gets + indented + four + spaces + on + the + second +
177 and + subsequent + lines;
178
179 /*
180 * Closing and opening braces go on the same line as the else.
181 * Don't add braces that aren't necessary.
182 */
183 if (test)
184 stmt;
185 else if (bar) {
186 stmt;
187 stmt;
188 } else
189 stmt;
190
191 /* No spaces after function names. */
192 if (error = function(a1, a2))
193 exit(error);
194
195 /*
196 * Unary operators don't require spaces, binary operators do. Don't
197 * use parenthesis unless they're required for precedence, or the
198 * statement is really confusing without them, such as:
199 * a = b->c[0] + ~d == (e || f) || g && h ? i : j >> 1;
200 */
201 a = ((b->c[0] + ~d == (e || f)) || (g && h)) ? i : (j >> 1);
202 k = !(l & FLAGS);
203
204 /*
205 * Exits should be 0 on success, and 1 on failure. Don't denote
206 * all the possible exit points, using the integers 1 through 300.
207 */
208 exit(0); /* Avoid obvious comments such as "Exit 0 on success." */
209 }
210
211 /*
212 * The function type must be declared on a line by itself
213 * preceeding the function.
214 */
215 static char *
216 function(a1, a2, fl, a4)
217 int a1, a2, a4; /* Declare ints, too, don't default them. */
218 float fl; /* List in order declared, as much as possible. */
219 {
220 /*
221 * When declaring variables in functions declare them sorted by size,
222 * then in alphabetical order; multiple ones per line are okay. Old
223 * style function declarations can go on the same line. ANSI style
224 * function declarations should go in the include file "extern.h".
225 * If a line overflows reuse the type keyword.
226 *
227 * DO NOT initialize variables in the declarations.
228 */
229 extern u_char one;
230 extern char two;
231 struct foo three, *four;
232 double five;
233 int *six, seven, eight();
234 char *nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen;
235 char *overflow __P((void));
236 void *mymalloc __P((u_int));
237
238 /*
239 * Casts and sizeof's are not followed by a space. NULL is any
240 * pointer type, and doesn't need to be cast, so use NULL instead
241 * of (struct foo *)0 or (struct foo *)NULL. Also, test pointers
242 * against NULL, i.e. use:
243 *
244 * (p = f()) == NULL
245 * not:
246 * !(p = f())
247 *
248 * Don't use '!' for tests unless it's a boolean, e.g. use
249 * "if (*p == '\0')", not "if (!*p)".
250 *
251 * Routines returning void * should not have their return values cast
252 * to any pointer type.
253 *
254 * Use err/warn(3), don't roll your own!
255 */
256 if ((four = malloc(sizeof(struct foo))) == NULL)
257 err(1, NULL);
258 if ((six = (int *)overflow()) == NULL)
259 errx(1, "Number overflowed.");
260 return (eight);
261 }
262
263 /*
264 * Don't use ANSI function declarations unless you absolutely have to,
265 * i.e. you're declaring functions with variable numbers of arguments.
266 *
267 * ANSI function braces look like regular function braces.
268 */
269 void
270 function(int a1, int a2)
271 {
272 ...
273 }
274
275 /* Variable numbers of arguments should look like this. */
276 #if __STDC__
277 #include <stdarg.h>
278 #else
279 #include <varargs.h>
280 #endif
281
282 void
283 #if __STDC__
284 vaf(const char *fmt, ...)
285 #else
286 vaf(fmt, va_alist)
287 char *fmt;
288 va_dcl
289 #endif
290 {
291 va_list ap;
292 #if __STDC__
293 va_start(ap, fmt);
294 #else
295 va_start(ap);
296 #endif
297 STUFF;
298
299 va_end(ap); /* No return needed for void functions. */
300 }
301
302 static void
303 usage()
304 { /* Insert an empty line if the function has no local variables. */
305
306 /*
307 * Use printf(3), not fputs/puts/putchar/whatever, it's faster and
308 * usually cleaner, not to mention avoiding stupid bugs.
309 *
310 * Usage statements should look like the manual pages. Options w/o
311 * operands come first, in alphabetical order inside a single set of
312 * braces. Followed by options with operands, in alphabetical order,
313 * each in braces. Followed by required arguments in the order they
314 * are specified, followed by optional arguments in the order they
315 * are specified. A bar ('|') separates either/or options/arguments,
316 * and multiple options/arguments which are specified together are
317 * placed in a single set of braces.
318 *
319 * "usage: f [-ade] [-b b_arg] [-m m_arg] req1 req2 [opt1 [opt2]]\n"
320 * "usage: f [-a | -b] [-c [-de] [-n number]]\n"
321 */
322 (void)fprintf(stderr, "usage: f [-ab]\n");
323 exit(1);
324 }
325