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