style revision 1.2
11.1Scgd/*
21.2Scgd * Style guide for the 4BSD KNF (Kernel Normal Form).
31.1Scgd *
41.2Scgd *	from: @(#)style	1.12 (Berkeley) 3/18/94
51.1Scgd *	$Id: style,v 1.2 1994/03/26 03:24:54 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.2Scgd/*
201.2Scgd * Kernel include files come first; normally, you'll need <sys/types.h>
211.2Scgd * OR <sys/param.h>, but not both!  <sys/types.h> includes <sys/cdefs.h>,
221.2Scgd * and it's okay to depend on that.
231.2Scgd */
241.2Scgd#include <sys/types.h>		/* Non-local includes in brackets. */
251.2Scgd
261.2Scgd/* If it's a network program, put the network include files next. */
271.2Scgd#include <net/if.h>
281.2Scgd#include <net/if_dl.h>
291.2Scgd#include <net/route.h>
301.2Scgd#include <netinet/in.h>
311.2Scgd#include <protocols/rwhod.h>
321.2Scgd
331.2Scgd/*
341.2Scgd * Then there's a blank line, followed by the /usr include files.
351.2Scgd * The /usr include files should be sorted!
361.2Scgd */
371.2Scgd#include <stdio.h>
381.1Scgd
391.1Scgd/*
401.1Scgd * Global pathnames are defined in /usr/include/paths.h.  Pathnames local
411.1Scgd * to the program go in pathnames.h in the local directory.
421.1Scgd */
431.2Scgd#include <paths.h>
441.2Scgd
451.2Scgd/* Then, there's a blank line, and the user include files. */
461.2Scgd#include "pathnames.h"		/* Local includes in double quotes. */		
471.1Scgd
481.1Scgd/*
491.2Scgd * ANSI function declarations for private functions (i.e. functions not used
501.2Scgd * elsewhere) go at the top of the source module.  Use the __P macro from
511.2Scgd * the include file <sys/cdefs.h>.  Only the kernel has a name associated with
521.2Scgd * the types, i.e. in the kernel use:
531.1Scgd *
541.1Scgd *	void function __P((int a));
551.1Scgd *
561.1Scgd * in user land use:
571.1Scgd *
581.1Scgd *	void function __P((int));
591.1Scgd */
601.2Scgdstatic char	*function __P((int, const char *));
611.2Scgdstatic void	 usage __P((void));
621.1Scgd
631.1Scgd/*
641.1Scgd * Macros are capitalized, parenthesized, and should avoid side-effects.
651.1Scgd * If they are an inline expansion of a function, the function is defined
661.1Scgd * all in lowercase, the macro has the same name all in uppercase. If the
671.2Scgd * macro needs more than a single line, use braces.  Right-justify the
681.2Scgd * backslashes, it makes it easier to read.
691.1Scgd */
701.2Scgd#define	MACRO(x, y) {							\
711.2Scgd	variable = (x) + (y);						\
721.2Scgd	(y) += 2;							\
731.1Scgd}
741.1Scgd
751.1Scgd/* Enum types are capitalized. */
761.1Scgdenum enumtype { ONE, TWO } et;
771.1Scgd
781.1Scgd/*
791.1Scgd * When declaring variables in structures, declare them sorted by use, then
801.1Scgd * by size, and then by alphabetical order.  The first category normally
811.1Scgd * doesn't apply, but there are exceptions.  Each one gets its own line.
821.1Scgd * Put a tab after the first word, i.e. use "int^Ix;" and "struct^Ifoo *x;".
831.1Scgd *
841.2Scgd * Major structures should be declared at the top of the file in which they
851.2Scgd * are used, or in separate header files, if they are used in multiple
861.2Scgd * source files.  Use of the structures should be by separate declarations
871.1Scgd * and should be "extern" if they are declared in a header file.
881.1Scgd */
891.1Scgdstruct foo {
901.1Scgd	struct	foo *next;	/* List of active foo */
911.1Scgd	struct	mumble amumble;	/* Comment for mumble */
921.1Scgd	int	bar;
931.1Scgd};
941.1Scgdstruct foo *foohead;		/* Head of global foo list */
951.2Scgd
961.2Scgd/* Make the structure name match the typedef. */
971.2Scgdtypedef struct _bar {
981.2Scgd	int	level;
991.2Scgd} BAR;
1001.1Scgd	
1011.1Scgd/*
1021.1Scgd * All major routines should have a comment briefly describing what
1031.2Scgd * they do.  The comment before the "main" routine should describe
1041.1Scgd * what the program does.
1051.1Scgd */
1061.2Scgdint
1071.1Scgdmain(argc, argv)
1081.1Scgd	int argc;
1091.1Scgd	char *argv[];
1101.1Scgd{
1111.1Scgd	extern char *optarg;
1121.1Scgd	extern int optind;
1131.1Scgd	long num;
1141.1Scgd	int ch;
1151.1Scgd	char *ep;
1161.1Scgd
1171.1Scgd	/*
1181.2Scgd	 * For consistency, getopt should be used to parse options.  Options
1191.2Scgd	 * should be sorted in the getopt call and the switch statement, unless
1201.2Scgd	 * parts of the switch cascade.  Elements in a switch statement that
1211.2Scgd	 * cascade should have a FALLTHROUGH comment.  Numerical arguments
1221.2Scgd	 * should be checked for accuracy.  Code that cannot be reached should
1231.2Scgd	 * have a NOTREACHED comment.
1241.1Scgd	 */
1251.1Scgd	while ((ch = getopt(argc, argv, "abn")) != EOF)
1261.1Scgd		switch (ch) {		/* Indent the switch. */
1271.1Scgd		case 'a':		/* Don't indent the case. */
1281.1Scgd			aflag = 1;
1291.1Scgd			/* FALLTHROUGH */
1301.1Scgd		case 'b':
1311.1Scgd			bflag = 1;
1321.1Scgd			break;
1331.1Scgd		case 'n':
1341.1Scgd			num = strtol(optarg, &ep, 10);
1351.2Scgd                        if (num <= 0 || *ep != '\0')
1361.1Scgd                                err("illegal number -- %s", optarg);
1371.1Scgd			break;
1381.1Scgd		case '?':
1391.1Scgd		default:
1401.1Scgd			usage();
1411.2Scgd			/* NOTREACHED */
1421.1Scgd		}
1431.1Scgd	argc -= optind;
1441.1Scgd	argv += optind;
1451.1Scgd
1461.1Scgd	/*
1471.1Scgd	 * Space after keywords (while, for, return, switch).  No braces are
1481.2Scgd	 * used for control statements with zero or only a single statement.
1491.1Scgd	 *
1501.1Scgd	 * Forever loops are done with for's, not while's.
1511.1Scgd	 */
1521.2Scgd	for (p = buf; *p != '\0'; ++p);
1531.1Scgd	for (;;)
1541.1Scgd		stmt;
1551.1Scgd	
1561.1Scgd	/*
1571.2Scgd	 * Parts of a for loop may be left empty.  Don't put declarations
1581.2Scgd	 * inside blocks unless the routine is unusually complicated.
1591.1Scgd	 */
1601.1Scgd	for (; cnt < 15; cnt++) {
1611.1Scgd		stmt1;
1621.1Scgd		stmt2;
1631.1Scgd	}
1641.1Scgd
1651.2Scgd	/* Second level indents are four spaces. */
1661.2Scgd	while (cnt < 20)
1671.1Scgd		z = a + really + long + statment + that + needs + two lines +
1681.1Scgd		    gets + indented + four + spaces + on + the + second +
1691.1Scgd		    and + subsequent + lines.
1701.1Scgd
1711.1Scgd	/*
1721.2Scgd	 * Closing and opening braces go on the same line as the else.
1731.2Scgd	 * Don't add braces that aren't necessary.
1741.1Scgd	 */
1751.1Scgd	if (test)
1761.1Scgd		stmt;
1771.1Scgd	else if (bar) {
1781.1Scgd		stmt;
1791.1Scgd		stmt;
1801.1Scgd	} else
1811.1Scgd		stmt;
1821.1Scgd		
1831.2Scgd	/* No spaces after function names. */
1841.1Scgd	if (error = function(a1, a2))
1851.1Scgd		exit(error);
1861.1Scgd
1871.1Scgd	/*
1881.2Scgd	 * Unary operators don't require spaces, binary operators do. Don't
1891.2Scgd	 * use parenthesis unless they're required for precedence, or the
1901.2Scgd	 * statement is really confusing without them.
1911.1Scgd	 */
1921.1Scgd	a = b->c[0] + ~d == (e || f) || g && h ? i : j >> 1;
1931.2Scgd	k = !(l & FLAGS);
1941.1Scgd
1951.1Scgd	/*
1961.1Scgd	 * Exits should be 0 on success, and 1 on failure.  Don't denote
1971.1Scgd	 * all the possible exit points, using the integers 1 through 300.
1981.1Scgd	 */
1991.1Scgd	exit(0);    /* Avoid obvious comments such as "Exit 0 on success." */
2001.1Scgd}
2011.1Scgd
2021.1Scgd/*
2031.1Scgd * If a function type is declared, it should be on a line
2041.1Scgd * by itself preceeding the function.
2051.1Scgd */
2061.1Scgdstatic char *
2071.2Scgdfunction(a1, a2, fl, a4)
2081.2Scgd	int a1, a2, a4;	/* Declare ints, too, don't default them. */
2091.2Scgd	float fl;	/* List in order declared, as much as possible. */
2101.1Scgd{
2111.1Scgd	/*
2121.1Scgd	 * When declaring variables in functions declare them sorted by size,
2131.1Scgd	 * then in alphabetical order; multiple ones per line are okay.  Old
2141.1Scgd	 * style function declarations can go on the same line.  ANSI style
2151.1Scgd	 * function declarations should go in the include file "externs.h".
2161.1Scgd	 * If a line overflows reuse the type keyword.
2171.1Scgd	 *
2181.2Scgd	 * DO NOT initialize variables in the declarations.
2191.1Scgd	 */
2201.1Scgd	extern u_char one;
2211.1Scgd	extern char two;
2221.1Scgd	struct foo three, *four;
2231.1Scgd	double five;
2241.1Scgd	int *six, seven, eight();
2251.1Scgd	char *nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen;
2261.1Scgd	char *overflow __P((void));
2271.1Scgd	void *mymalloc __P((u_int));
2281.1Scgd
2291.1Scgd	/*
2301.1Scgd	 * Casts and sizeof's are not followed by a space.  NULL is any
2311.1Scgd	 * pointer type, and doesn't need to be cast, so use NULL instead
2321.1Scgd	 * of (struct foo *)0 or (struct foo *)NULL.  Also, test pointers
2331.1Scgd	 * against NULL, i.e. use:
2341.1Scgd	 *
2351.1Scgd	 * 	(p = f()) == NULL
2361.1Scgd	 * not:
2371.1Scgd	 *	!(p = f())
2381.2Scgd	 *
2391.2Scgd	 * Don't use '!' for tests unless it's a boolean, e.g. use
2401.2Scgd	 * "if (*p == '\0')", not "if (!*p)".
2411.1Scgd 	 *
2421.1Scgd	 * Routines returning void * should not have their return values cast
2431.1Scgd	 * to any pointer type.
2441.2Scgd	 *
2451.2Scgd	 * Use err/warn(3), don't roll your own!
2461.1Scgd	 */
2471.1Scgd	if ((four = malloc(sizeof(struct foo))) == NULL)
2481.2Scgd		err(1, NULL);
2491.1Scgd	if ((six = (int *)overflow()) == NULL)
2501.2Scgd		errx(1, "Number overflowed.");
2511.1Scgd	return (eight);
2521.1Scgd}
2531.1Scgd
2541.2Scgd/*
2551.2Scgd * Don't use ANSI function declarations unless you absolutely have too,
2561.2Scgd * i.e. you're declaring functions with variable numbers of arguments.
2571.2Scgd *
2581.2Scgd * ANSI function braces look like regular function braces.
2591.2Scgd */
2601.1Scgdfunction(int a1, int a2)
2611.1Scgd{
2621.1Scgd	...
2631.2Scgd}
2641.2Scgd
2651.2Scgd/* Variable numbers of arguments should look like this. */
2661.2Scgd#if __STDC__
2671.2Scgd#include <stdarg.h>
2681.2Scgd#else
2691.2Scgd#include <varargs.h>
2701.2Scgd#endif
2711.2Scgd
2721.2Scgdvoid
2731.2Scgd#if __STDC__
2741.2Scgdvaf(const char *fmt, ...)
2751.2Scgd#else
2761.2Scgdvaf(fmt, va_alist)
2771.2Scgd	char *fmt;
2781.2Scgd	va_dcl
2791.2Scgd#endif
2801.2Scgd{
2811.2Scgd	va_list ap;
2821.2Scgd#if __STDC__
2831.2Scgd	va_start(ap, fmt);
2841.2Scgd#else
2851.2Scgd	va_start(ap);
2861.2Scgd#endif
2871.2Scgd	STUFF;
2881.2Scgd
2891.2Scgd	va_end(ap);		/* No return needed for void functions. */
2901.1Scgd}
2911.1Scgd
2921.1Scgdstatic void
2931.1Scgdusage()
2941.1Scgd{	/* Insert an empty line if the function has no local variables. */
2951.1Scgd
2961.1Scgd	/*
2971.1Scgd	 * Use printf(3), not fputs/puts/putchar/whatever, it's faster and
2981.1Scgd	 * usually cleaner, not to mention avoiding stupid bugs.
2991.1Scgd	 *
3001.1Scgd	 * Usage statements should look like the manual pages.  Options w/o
3011.1Scgd	 * operands come first, in alphabetical order inside a single set of
3021.1Scgd	 * braces.  Followed by options with operands, in alphabetical order,
3031.1Scgd	 * each in braces.  Followed by required arguments in the order they
3041.1Scgd	 * are specified, followed by optional arguments in the order they
3051.1Scgd	 * are specified.  A bar ('|') separates either/or options/arguments,
3061.1Scgd	 * and multiple options/arguments which are specified together are
3071.1Scgd	 * placed in a single set of braces.
3081.1Scgd	 *
3091.1Scgd	 * "usage: f [-ade] [-b b_arg] [-m m_arg] req1 req2 [opt1 [opt2]]\n"
3101.1Scgd	 * "usage: f [-a | -b] [-c [-de] [-n number]]\n"
3111.1Scgd	 */
3121.1Scgd	(void)fprintf(stderr, "usage: f [-ab]\n");
3131.1Scgd	exit(1);
3141.1Scgd}
315