style guide
This commit is contained in:
parent
eac55eb1b3
commit
c9621c279c
|
@ -1,7 +1,7 @@
|
||||||
# from: @(#)Makefile 5.13 (Berkeley) 5/7/91
|
# from: @(#)Makefile 5.13 (Berkeley) 5/7/91
|
||||||
# $Id: Makefile,v 1.6 1994/03/08 07:15:21 cgd Exp $
|
# $Id: Makefile,v 1.7 1994/03/26 03:24:50 cgd Exp $
|
||||||
|
|
||||||
FILES= airport ascii birthtoken eqnchar inter.phone man.template \
|
FILES= airport ascii birthtoken eqnchar getopt inter.phone man.template \
|
||||||
mdoc.template na.phone operator style zipcodes
|
mdoc.template na.phone operator style zipcodes
|
||||||
NOOBJ= noobj
|
NOOBJ= noobj
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
/*
|
||||||
|
* Main/getopt(3) fragment.
|
||||||
|
*
|
||||||
|
* from: @(#)getopt 5.2 (Berkeley) 3/17/94
|
||||||
|
* $Id: getopt,v 1.1 1994/03/26 03:24:53 cgd Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
void usage __P((void));
|
||||||
|
|
||||||
|
int
|
||||||
|
main(argc, argv)
|
||||||
|
int argc;
|
||||||
|
char *argv[];
|
||||||
|
{
|
||||||
|
int ch;
|
||||||
|
|
||||||
|
while ((ch = getopt(argc, argv, "")) != EOF)
|
||||||
|
switch (ch) {
|
||||||
|
case '':
|
||||||
|
break;
|
||||||
|
case '?':
|
||||||
|
default:
|
||||||
|
usage();
|
||||||
|
}
|
||||||
|
argc -= optind;
|
||||||
|
argv += optind;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
usage()
|
||||||
|
{
|
||||||
|
(void)fprintf(stderr, "usage: program:\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
158
share/misc/style
158
share/misc/style
|
@ -1,8 +1,8 @@
|
||||||
/*
|
/*
|
||||||
* Style guide for BSD's KNF (Kernel Normal Form).
|
* Style guide for the 4BSD KNF (Kernel Normal Form).
|
||||||
*
|
*
|
||||||
* from: @(#)style 1.10 (Berkeley) 2/11/92
|
* from: @(#)style 1.12 (Berkeley) 3/18/94
|
||||||
* $Id: style,v 1.1 1993/08/06 07:30:52 cgd Exp $
|
* $Id: style,v 1.2 1994/03/26 03:24:54 cgd Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -16,20 +16,40 @@
|
||||||
* them so they look like real paragraphs.
|
* them so they look like real paragraphs.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* Include files go at the top of the source module. */
|
/*
|
||||||
#include <stdio.h> /* Non-local includes in brackets. */
|
* Kernel include files come first; normally, you'll need <sys/types.h>
|
||||||
|
* OR <sys/param.h>, but not both! <sys/types.h> includes <sys/cdefs.h>,
|
||||||
|
* and it's okay to depend on that.
|
||||||
|
*/
|
||||||
|
#include <sys/types.h> /* Non-local includes in brackets. */
|
||||||
|
|
||||||
|
/* If it's a network program, put the network include files next. */
|
||||||
|
#include <net/if.h>
|
||||||
|
#include <net/if_dl.h>
|
||||||
|
#include <net/route.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <protocols/rwhod.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Then there's a blank line, followed by the /usr include files.
|
||||||
|
* The /usr include files should be sorted!
|
||||||
|
*/
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Global pathnames are defined in /usr/include/paths.h. Pathnames local
|
* Global pathnames are defined in /usr/include/paths.h. Pathnames local
|
||||||
* to the program go in pathnames.h in the local directory.
|
* to the program go in pathnames.h in the local directory.
|
||||||
*/
|
*/
|
||||||
#include <paths.h> /* Non-local includes in brackets. */
|
#include <paths.h>
|
||||||
#include "pathnames.h" /* Local includes in quotes. */
|
|
||||||
|
/* Then, there's a blank line, and the user include files. */
|
||||||
|
#include "pathnames.h" /* Local includes in double quotes. */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* All ANSI function decls go at the top of the source module. Use the
|
* ANSI function declarations for private functions (i.e. functions not used
|
||||||
* __P macro from include file <sys/cdefs.h>. Only the kernel has a name
|
* elsewhere) go at the top of the source module. Use the __P macro from
|
||||||
* associated with the types, i.e. in the kernel use:
|
* the include file <sys/cdefs.h>. Only the kernel has a name associated with
|
||||||
|
* the types, i.e. in the kernel use:
|
||||||
*
|
*
|
||||||
* void function __P((int a));
|
* void function __P((int a));
|
||||||
*
|
*
|
||||||
|
@ -37,18 +57,19 @@
|
||||||
*
|
*
|
||||||
* void function __P((int));
|
* void function __P((int));
|
||||||
*/
|
*/
|
||||||
void function __P((int, const char *));
|
static char *function __P((int, const char *));
|
||||||
|
static void usage __P((void));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Macros are capitalized, parenthesized, and should avoid side-effects.
|
* Macros are capitalized, parenthesized, and should avoid side-effects.
|
||||||
* If they are an inline expansion of a function, the function is defined
|
* If they are an inline expansion of a function, the function is defined
|
||||||
* all in lowercase, the macro has the same name all in uppercase. If the
|
* all in lowercase, the macro has the same name all in uppercase. If the
|
||||||
* macro needs more than a single line, use braces. Put a space before
|
* macro needs more than a single line, use braces. Right-justify the
|
||||||
* the backslashes.
|
* backslashes, it makes it easier to read.
|
||||||
*/
|
*/
|
||||||
#define MACRO(x, y) { \
|
#define MACRO(x, y) { \
|
||||||
variable = (x) + (y); \
|
variable = (x) + (y); \
|
||||||
line two; \
|
(y) += 2; \
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Enum types are capitalized. */
|
/* Enum types are capitalized. */
|
||||||
|
@ -60,9 +81,9 @@ enum enumtype { ONE, TWO } et;
|
||||||
* doesn't apply, but there are exceptions. Each one gets its own line.
|
* doesn't apply, but there are exceptions. Each one gets its own line.
|
||||||
* Put a tab after the first word, i.e. use "int^Ix;" and "struct^Ifoo *x;".
|
* Put a tab after the first word, i.e. use "int^Ix;" and "struct^Ifoo *x;".
|
||||||
*
|
*
|
||||||
* Major structures should be declared at the top of the file they are
|
* Major structures should be declared at the top of the file in which they
|
||||||
* used in, or in separate header files, if they are used in multiple
|
* are used, or in separate header files, if they are used in multiple
|
||||||
* source files. Use of the structures should be by separate declarations
|
* source files. Use of the structures should be by separate declarations
|
||||||
* and should be "extern" if they are declared in a header file.
|
* and should be "extern" if they are declared in a header file.
|
||||||
*/
|
*/
|
||||||
struct foo {
|
struct foo {
|
||||||
|
@ -71,12 +92,18 @@ struct foo {
|
||||||
int bar;
|
int bar;
|
||||||
};
|
};
|
||||||
struct foo *foohead; /* Head of global foo list */
|
struct foo *foohead; /* Head of global foo list */
|
||||||
|
|
||||||
|
/* Make the structure name match the typedef. */
|
||||||
|
typedef struct _bar {
|
||||||
|
int level;
|
||||||
|
} BAR;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* All major routines should have a comment briefly describing what
|
* All major routines should have a comment briefly describing what
|
||||||
* they do. The comment before the "main" routine should describe
|
* they do. The comment before the "main" routine should describe
|
||||||
* what the program does.
|
* what the program does.
|
||||||
*/
|
*/
|
||||||
|
int
|
||||||
main(argc, argv)
|
main(argc, argv)
|
||||||
int argc;
|
int argc;
|
||||||
char *argv[];
|
char *argv[];
|
||||||
|
@ -88,11 +115,12 @@ main(argc, argv)
|
||||||
char *ep;
|
char *ep;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* For consistency, getopt should be used to parse options.
|
* For consistency, getopt should be used to parse options. Options
|
||||||
* Options should be sorted in the getopt call and the switch
|
* should be sorted in the getopt call and the switch statement, unless
|
||||||
* statement, unless they fall through. Elements in a switch
|
* parts of the switch cascade. Elements in a switch statement that
|
||||||
* statement that fall through should have a FALLTHROUGH comment.
|
* cascade should have a FALLTHROUGH comment. Numerical arguments
|
||||||
* Numerical arguments should be checked for accuracy.
|
* should be checked for accuracy. Code that cannot be reached should
|
||||||
|
* have a NOTREACHED comment.
|
||||||
*/
|
*/
|
||||||
while ((ch = getopt(argc, argv, "abn")) != EOF)
|
while ((ch = getopt(argc, argv, "abn")) != EOF)
|
||||||
switch (ch) { /* Indent the switch. */
|
switch (ch) { /* Indent the switch. */
|
||||||
|
@ -104,44 +132,45 @@ main(argc, argv)
|
||||||
break;
|
break;
|
||||||
case 'n':
|
case 'n':
|
||||||
num = strtol(optarg, &ep, 10);
|
num = strtol(optarg, &ep, 10);
|
||||||
if (num <= 0 || *ep)
|
if (num <= 0 || *ep != '\0')
|
||||||
err("illegal number -- %s", optarg);
|
err("illegal number -- %s", optarg);
|
||||||
break;
|
break;
|
||||||
case '?':
|
case '?':
|
||||||
default:
|
default:
|
||||||
usage();
|
usage();
|
||||||
|
/* NOTREACHED */
|
||||||
}
|
}
|
||||||
argc -= optind;
|
argc -= optind;
|
||||||
argv += optind;
|
argv += optind;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Space after keywords (while, for, return, switch). No braces are
|
* Space after keywords (while, for, return, switch). No braces are
|
||||||
* used for single statement block.
|
* used for control statements with zero or only a single statement.
|
||||||
*
|
*
|
||||||
* Forever loops are done with for's, not while's.
|
* Forever loops are done with for's, not while's.
|
||||||
*/
|
*/
|
||||||
|
for (p = buf; *p != '\0'; ++p);
|
||||||
for (;;)
|
for (;;)
|
||||||
stmt;
|
stmt;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Parts of a for loop may be left empty. Avoid declarations in
|
* Parts of a for loop may be left empty. Don't put declarations
|
||||||
* blocks unless the routine is unusually complicated.
|
* inside blocks unless the routine is unusually complicated.
|
||||||
*/
|
*/
|
||||||
for (; cnt < 15; cnt++) {
|
for (; cnt < 15; cnt++) {
|
||||||
stmt1;
|
stmt1;
|
||||||
stmt2;
|
stmt2;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (cnt < 20) {
|
/* Second level indents are four spaces. */
|
||||||
stmt1; /* Second level indents are four spaces. */
|
while (cnt < 20)
|
||||||
z = a + really + long + statment + that + needs + two lines +
|
z = a + really + long + statment + that + needs + two lines +
|
||||||
gets + indented + four + spaces + on + the + second +
|
gets + indented + four + spaces + on + the + second +
|
||||||
and + subsequent + lines.
|
and + subsequent + lines.
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Try to put shorter part first. The closing and opening braces
|
* Closing and opening braces go on the same line as the else.
|
||||||
* go on the same line as the else.
|
* Don't add braces that aren't necessary.
|
||||||
*/
|
*/
|
||||||
if (test)
|
if (test)
|
||||||
stmt;
|
stmt;
|
||||||
|
@ -151,17 +180,17 @@ main(argc, argv)
|
||||||
} else
|
} else
|
||||||
stmt;
|
stmt;
|
||||||
|
|
||||||
/* No space after function names. */
|
/* No spaces after function names. */
|
||||||
if (error = function(a1, a2))
|
if (error = function(a1, a2))
|
||||||
exit(error);
|
exit(error);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Unary operators do not require spaces, binary operators do.
|
* Unary operators don't require spaces, binary operators do. Don't
|
||||||
* Try not to use too many parenthesis unless the statement is
|
* use parenthesis unless they're required for precedence, or the
|
||||||
* really confusing without them.
|
* statement is really confusing without them.
|
||||||
*/
|
*/
|
||||||
a = b->c[0] + ~d == (e || f) || g && h ? i : j >> 1;
|
a = b->c[0] + ~d == (e || f) || g && h ? i : j >> 1;
|
||||||
k = l & FLAGS;
|
k = !(l & FLAGS);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Exits should be 0 on success, and 1 on failure. Don't denote
|
* Exits should be 0 on success, and 1 on failure. Don't denote
|
||||||
|
@ -175,9 +204,9 @@ main(argc, argv)
|
||||||
* by itself preceeding the function.
|
* by itself preceeding the function.
|
||||||
*/
|
*/
|
||||||
static char *
|
static char *
|
||||||
function(a1, a2, a3, a4)
|
function(a1, a2, fl, a4)
|
||||||
int a1, a2, a4; /* Declare ints too. */
|
int a1, a2, a4; /* Declare ints, too, don't default them. */
|
||||||
float a3; /* List in order declared, as much as possible. */
|
float fl; /* List in order declared, as much as possible. */
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* When declaring variables in functions declare them sorted by size,
|
* When declaring variables in functions declare them sorted by size,
|
||||||
|
@ -186,7 +215,7 @@ function(a1, a2, a3, a4)
|
||||||
* function declarations should go in the include file "externs.h".
|
* function declarations should go in the include file "externs.h".
|
||||||
* If a line overflows reuse the type keyword.
|
* If a line overflows reuse the type keyword.
|
||||||
*
|
*
|
||||||
* Try not to initialize variables in the declarations.
|
* DO NOT initialize variables in the declarations.
|
||||||
*/
|
*/
|
||||||
extern u_char one;
|
extern u_char one;
|
||||||
extern char two;
|
extern char two;
|
||||||
|
@ -206,23 +235,60 @@ function(a1, a2, a3, a4)
|
||||||
* (p = f()) == NULL
|
* (p = f()) == NULL
|
||||||
* not:
|
* not:
|
||||||
* !(p = f())
|
* !(p = f())
|
||||||
|
*
|
||||||
|
* Don't use '!' for tests unless it's a boolean, e.g. use
|
||||||
|
* "if (*p == '\0')", not "if (!*p)".
|
||||||
*
|
*
|
||||||
* Routines returning void * should not have their return values cast
|
* Routines returning void * should not have their return values cast
|
||||||
* to any pointer type.
|
* to any pointer type.
|
||||||
|
*
|
||||||
|
* Use err/warn(3), don't roll your own!
|
||||||
*/
|
*/
|
||||||
if ((four = malloc(sizeof(struct foo))) == NULL)
|
if ((four = malloc(sizeof(struct foo))) == NULL)
|
||||||
return (NULL);
|
err(1, NULL);
|
||||||
if ((six = (int *)overflow()) == NULL)
|
if ((six = (int *)overflow()) == NULL)
|
||||||
return (NULL);
|
errx(1, "Number overflowed.");
|
||||||
return (eight);
|
return (eight);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ANSI function braces look like regular function braces. */
|
/*
|
||||||
|
* Don't use ANSI function declarations unless you absolutely have too,
|
||||||
|
* i.e. you're declaring functions with variable numbers of arguments.
|
||||||
|
*
|
||||||
|
* ANSI function braces look like regular function braces.
|
||||||
|
*/
|
||||||
function(int a1, int a2)
|
function(int a1, int a2)
|
||||||
{
|
{
|
||||||
...
|
...
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Variable numbers of arguments should look like this. */
|
||||||
|
#if __STDC__
|
||||||
|
#include <stdarg.h>
|
||||||
|
#else
|
||||||
|
#include <varargs.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void
|
||||||
|
#if __STDC__
|
||||||
|
vaf(const char *fmt, ...)
|
||||||
|
#else
|
||||||
|
vaf(fmt, va_alist)
|
||||||
|
char *fmt;
|
||||||
|
va_dcl
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
va_list ap;
|
||||||
|
#if __STDC__
|
||||||
|
va_start(ap, fmt);
|
||||||
|
#else
|
||||||
|
va_start(ap);
|
||||||
|
#endif
|
||||||
|
STUFF;
|
||||||
|
|
||||||
|
va_end(ap); /* No return needed for void functions. */
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
usage()
|
usage()
|
||||||
{ /* Insert an empty line if the function has no local variables. */
|
{ /* Insert an empty line if the function has no local variables. */
|
||||||
|
|
Loading…
Reference in New Issue