style revision 1.1
11.1Scgd/*
21.1Scgd * Style guide for BSD's KNF (Kernel Normal Form).
31.1Scgd *
41.1Scgd *	from: @(#)style	1.10 (Berkeley) 2/11/92
51.1Scgd *	$Id: style,v 1.1 1993/08/06 07:30:52 cgd Exp $
61.1Scgd */
71.1Scgd
81.1Scgd/*
91.1Scgd * VERY important single-line comments look like this.
101.1Scgd */
111.1Scgd
121.1Scgd/* Most single-line comments look like this. */
131.1Scgd
141.1Scgd/*
151.1Scgd * Multi-line comments look like this.  Make them real sentences.  Fill
161.1Scgd * them so they look like real paragraphs.
171.1Scgd */
181.1Scgd
191.1Scgd/* Include files go at the top of the source module. */
201.1Scgd#include <stdio.h>		/* Non-local includes in brackets. */
211.1Scgd
221.1Scgd/*
231.1Scgd * Global pathnames are defined in /usr/include/paths.h.  Pathnames local
241.1Scgd * to the program go in pathnames.h in the local directory.
251.1Scgd */
261.1Scgd#include <paths.h>		/* Non-local includes in brackets. */
271.1Scgd#include "pathnames.h"		/* Local includes in quotes. */		
281.1Scgd
291.1Scgd/*
301.1Scgd * All ANSI function decls go at the top of the source module.  Use the
311.1Scgd * __P macro from include file <sys/cdefs.h>.  Only the kernel has a name
321.1Scgd * associated with the types, i.e. in the kernel use:
331.1Scgd *
341.1Scgd *	void function __P((int a));
351.1Scgd *
361.1Scgd * in user land use:
371.1Scgd *
381.1Scgd *	void function __P((int));
391.1Scgd */
401.1Scgdvoid function __P((int, const char *));
411.1Scgd
421.1Scgd/*
431.1Scgd * Macros are capitalized, parenthesized, and should avoid side-effects.
441.1Scgd * If they are an inline expansion of a function, the function is defined
451.1Scgd * all in lowercase, the macro has the same name all in uppercase. If the
461.1Scgd * macro needs more than a single line, use braces.  Put a space before
471.1Scgd * the backslashes.
481.1Scgd */
491.1Scgd#define	MACRO(x, y) { \
501.1Scgd	variable = (x) + (y); \
511.1Scgd	line two; \
521.1Scgd}
531.1Scgd
541.1Scgd/* Enum types are capitalized. */
551.1Scgdenum enumtype { ONE, TWO } et;
561.1Scgd
571.1Scgd/*
581.1Scgd * When declaring variables in structures, declare them sorted by use, then
591.1Scgd * by size, and then by alphabetical order.  The first category normally
601.1Scgd * doesn't apply, but there are exceptions.  Each one gets its own line.
611.1Scgd * Put a tab after the first word, i.e. use "int^Ix;" and "struct^Ifoo *x;".
621.1Scgd *
631.1Scgd * Major structures should be declared at the top of the file they are
641.1Scgd * used in, or in separate header files, if they are used in multiple
651.1Scgd * source files. Use of the structures should be by separate declarations
661.1Scgd * and should be "extern" if they are declared in a header file.
671.1Scgd */
681.1Scgdstruct foo {
691.1Scgd	struct	foo *next;	/* List of active foo */
701.1Scgd	struct	mumble amumble;	/* Comment for mumble */
711.1Scgd	int	bar;
721.1Scgd};
731.1Scgdstruct foo *foohead;		/* Head of global foo list */
741.1Scgd	
751.1Scgd/*
761.1Scgd * All major routines should have a comment briefly describing what
771.1Scgd * they do. The comment before the "main" routine should describe
781.1Scgd * what the program does.
791.1Scgd */
801.1Scgdmain(argc, argv)
811.1Scgd	int argc;
821.1Scgd	char *argv[];
831.1Scgd{
841.1Scgd	extern char *optarg;
851.1Scgd	extern int optind;
861.1Scgd	long num;
871.1Scgd	int ch;
881.1Scgd	char *ep;
891.1Scgd
901.1Scgd	/*
911.1Scgd	 * For consistency, getopt should be used to parse options.
921.1Scgd	 * Options should be sorted in the getopt call and the switch
931.1Scgd	 * statement, unless they fall through.  Elements in a switch
941.1Scgd	 * statement that fall through should have a FALLTHROUGH comment.
951.1Scgd	 * Numerical arguments should be checked for accuracy.
961.1Scgd	 */
971.1Scgd	while ((ch = getopt(argc, argv, "abn")) != EOF)
981.1Scgd		switch (ch) {		/* Indent the switch. */
991.1Scgd		case 'a':		/* Don't indent the case. */
1001.1Scgd			aflag = 1;
1011.1Scgd			/* FALLTHROUGH */
1021.1Scgd		case 'b':
1031.1Scgd			bflag = 1;
1041.1Scgd			break;
1051.1Scgd		case 'n':
1061.1Scgd			num = strtol(optarg, &ep, 10);
1071.1Scgd                        if (num <= 0 || *ep)
1081.1Scgd                                err("illegal number -- %s", optarg);
1091.1Scgd			break;
1101.1Scgd		case '?':
1111.1Scgd		default:
1121.1Scgd			usage();
1131.1Scgd		}
1141.1Scgd	argc -= optind;
1151.1Scgd	argv += optind;
1161.1Scgd
1171.1Scgd	/*
1181.1Scgd	 * Space after keywords (while, for, return, switch).  No braces are
1191.1Scgd	 * used for single statement block.
1201.1Scgd	 *
1211.1Scgd	 * Forever loops are done with for's, not while's.
1221.1Scgd	 */
1231.1Scgd	for (;;)
1241.1Scgd		stmt;
1251.1Scgd	
1261.1Scgd	/*
1271.1Scgd	 * Parts of a for loop may be left empty.  Avoid declarations in
1281.1Scgd	 * blocks unless the routine is unusually complicated.
1291.1Scgd	 */
1301.1Scgd	for (; cnt < 15; cnt++) {
1311.1Scgd		stmt1;
1321.1Scgd		stmt2;
1331.1Scgd	}
1341.1Scgd
1351.1Scgd	while (cnt < 20) {
1361.1Scgd		stmt1;		/* Second level indents are four spaces. */
1371.1Scgd		z = a + really + long + statment + that + needs + two lines +
1381.1Scgd		    gets + indented + four + spaces + on + the + second +
1391.1Scgd		    and + subsequent + lines.
1401.1Scgd	}
1411.1Scgd
1421.1Scgd	/*
1431.1Scgd	 * Try to put shorter part first.  The closing and opening braces
1441.1Scgd	 * go on the same line as the else.
1451.1Scgd	 */
1461.1Scgd	if (test)
1471.1Scgd		stmt;
1481.1Scgd	else if (bar) {
1491.1Scgd		stmt;
1501.1Scgd		stmt;
1511.1Scgd	} else
1521.1Scgd		stmt;
1531.1Scgd		
1541.1Scgd	/* No space after function names. */
1551.1Scgd	if (error = function(a1, a2))
1561.1Scgd		exit(error);
1571.1Scgd
1581.1Scgd	/*
1591.1Scgd	 * Unary operators do not require spaces, binary operators do.
1601.1Scgd	 * Try not to use too many parenthesis unless the statement is
1611.1Scgd	 * really confusing without them.
1621.1Scgd	 */
1631.1Scgd	a = b->c[0] + ~d == (e || f) || g && h ? i : j >> 1;
1641.1Scgd	k = l & FLAGS;
1651.1Scgd
1661.1Scgd	/*
1671.1Scgd	 * Exits should be 0 on success, and 1 on failure.  Don't denote
1681.1Scgd	 * all the possible exit points, using the integers 1 through 300.
1691.1Scgd	 */
1701.1Scgd	exit(0);    /* Avoid obvious comments such as "Exit 0 on success." */
1711.1Scgd}
1721.1Scgd
1731.1Scgd/*
1741.1Scgd * If a function type is declared, it should be on a line
1751.1Scgd * by itself preceeding the function.
1761.1Scgd */
1771.1Scgdstatic char *
1781.1Scgdfunction(a1, a2, a3, a4)
1791.1Scgd	int a1, a2, a4;	/* Declare ints too. */
1801.1Scgd	float a3;	/* List in order declared, as much as possible. */
1811.1Scgd{
1821.1Scgd	/*
1831.1Scgd	 * When declaring variables in functions declare them sorted by size,
1841.1Scgd	 * then in alphabetical order; multiple ones per line are okay.  Old
1851.1Scgd	 * style function declarations can go on the same line.  ANSI style
1861.1Scgd	 * function declarations should go in the include file "externs.h".
1871.1Scgd	 * If a line overflows reuse the type keyword.
1881.1Scgd	 *
1891.1Scgd	 * Try not to initialize variables in the declarations.
1901.1Scgd	 */
1911.1Scgd	extern u_char one;
1921.1Scgd	extern char two;
1931.1Scgd	struct foo three, *four;
1941.1Scgd	double five;
1951.1Scgd	int *six, seven, eight();
1961.1Scgd	char *nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen;
1971.1Scgd	char *overflow __P((void));
1981.1Scgd	void *mymalloc __P((u_int));
1991.1Scgd
2001.1Scgd	/*
2011.1Scgd	 * Casts and sizeof's are not followed by a space.  NULL is any
2021.1Scgd	 * pointer type, and doesn't need to be cast, so use NULL instead
2031.1Scgd	 * of (struct foo *)0 or (struct foo *)NULL.  Also, test pointers
2041.1Scgd	 * against NULL, i.e. use:
2051.1Scgd	 *
2061.1Scgd	 * 	(p = f()) == NULL
2071.1Scgd	 * not:
2081.1Scgd	 *	!(p = f())
2091.1Scgd 	 *
2101.1Scgd	 * Routines returning void * should not have their return values cast
2111.1Scgd	 * to any pointer type.
2121.1Scgd	 */
2131.1Scgd	if ((four = malloc(sizeof(struct foo))) == NULL)
2141.1Scgd		return (NULL);
2151.1Scgd	if ((six = (int *)overflow()) == NULL)
2161.1Scgd		return (NULL);
2171.1Scgd	return (eight);
2181.1Scgd}
2191.1Scgd
2201.1Scgd/* ANSI function braces look like regular function braces. */
2211.1Scgdfunction(int a1, int a2)
2221.1Scgd{
2231.1Scgd	...
2241.1Scgd}
2251.1Scgd
2261.1Scgdstatic void
2271.1Scgdusage()
2281.1Scgd{	/* Insert an empty line if the function has no local variables. */
2291.1Scgd
2301.1Scgd	/*
2311.1Scgd	 * Use printf(3), not fputs/puts/putchar/whatever, it's faster and
2321.1Scgd	 * usually cleaner, not to mention avoiding stupid bugs.
2331.1Scgd	 *
2341.1Scgd	 * Usage statements should look like the manual pages.  Options w/o
2351.1Scgd	 * operands come first, in alphabetical order inside a single set of
2361.1Scgd	 * braces.  Followed by options with operands, in alphabetical order,
2371.1Scgd	 * each in braces.  Followed by required arguments in the order they
2381.1Scgd	 * are specified, followed by optional arguments in the order they
2391.1Scgd	 * are specified.  A bar ('|') separates either/or options/arguments,
2401.1Scgd	 * and multiple options/arguments which are specified together are
2411.1Scgd	 * placed in a single set of braces.
2421.1Scgd	 *
2431.1Scgd	 * "usage: f [-ade] [-b b_arg] [-m m_arg] req1 req2 [opt1 [opt2]]\n"
2441.1Scgd	 * "usage: f [-a | -b] [-c [-de] [-n number]]\n"
2451.1Scgd	 */
2461.1Scgd	(void)fprintf(stderr, "usage: f [-ab]\n");
2471.1Scgd	exit(1);
2481.1Scgd}
249