Blame README.CodingStyle

Packit 6c0a39
Coding conventions in the libssh tree
Packit 6c0a39
======================================
Packit 6c0a39
Packit 6c0a39
===========
Packit 6c0a39
Quick Start
Packit 6c0a39
===========
Packit 6c0a39
Packit 6c0a39
Coding style guidelines are about reducing the number of unnecessary
Packit 6c0a39
reformatting patches and making things easier for developers to work together.
Packit 6c0a39
Packit 6c0a39
You don't have to like them or even agree with them, but once put in place we
Packit 6c0a39
all have to abide by them (or vote to change them).  However, coding style
Packit 6c0a39
should never outweigh coding itself and so the guidelines described here are
Packit 6c0a39
hopefully easy enough to follow as they are very common and supported by tools
Packit 6c0a39
and editors.
Packit 6c0a39
Packit 6c0a39
The basic style for C code, is the Linux kernel coding style (See
Packit 6c0a39
Documentation/CodingStyle in the kernel source tree). This closely matches what
Packit 6c0a39
libssh developers use already anyways, with a few exceptions as mentioned
Packit 6c0a39
below.
Packit 6c0a39
Packit 6c0a39
But to save you the trouble of reading the Linux kernel style guide, here
Packit 6c0a39
are the highlights.
Packit 6c0a39
Packit 6c0a39
* Maximum Line Width is 80 Characters
Packit 6c0a39
  The reason is not about people with low-res screens but rather sticking
Packit 6c0a39
  to 80 columns prevents you from easily nesting more than one level of
Packit 6c0a39
  if statements or other code blocks.
Packit 6c0a39
Packit 6c0a39
* Use 4 Spaces to Indent
Packit 6c0a39
Packit 6c0a39
* No Trailing Whitespace
Packit 6c0a39
  Clean up your files before committing.
Packit 6c0a39
Packit 6c0a39
* Follow the K&R guidelines.  We won't go through all of them here. Do you
Packit 6c0a39
  have a copy of "The C Programming Language" anyways right?
Packit 6c0a39
Packit 6c0a39
Packit 6c0a39
=============
Packit 6c0a39
Editor Hints
Packit 6c0a39
=============
Packit 6c0a39
Packit 6c0a39
Emacs
Packit 6c0a39
------
Packit 6c0a39
Add the follow to your $HOME/.emacs file:
Packit 6c0a39
Packit 6c0a39
  (add-hook 'c-mode-hook
Packit 6c0a39
    (lambda ()
Packit 6c0a39
        (c-set-style "linux")
Packit 6c0a39
        (c-toggle-auto-state)))
Packit 6c0a39
Packit 6c0a39
Packit 6c0a39
Vim
Packit 6c0a39
----
Packit 6c0a39
Packit 6c0a39
For the basic vi editor included with all variants of \*nix, add the
Packit 6c0a39
following to $HOME/.vimrc:
Packit 6c0a39
Packit 6c0a39
    set ts=4 sw=4 et cindent
Packit 6c0a39
Packit 6c0a39
You can use the Vim gitmodline plugin to store this in the git config:
Packit 6c0a39
Packit 6c0a39
    http://git.cryptomilk.org/projects/vim-gitmodeline.git/
Packit 6c0a39
Packit 6c0a39
For Vim, the following settings in $HOME/.vimrc will also deal with
Packit 6c0a39
displaying trailing whitespace:
Packit 6c0a39
Packit 6c0a39
    if has("syntax") && (&t_Co > 2 || has("gui_running"))
Packit 6c0a39
        syntax on
Packit 6c0a39
        function! ActivateInvisibleCharIndicator()
Packit 6c0a39
            syntax match TrailingSpace "[ \t]\+$" display containedin=ALL
Packit 6c0a39
            highlight TrailingSpace ctermbg=Red
Packit 6c0a39
        endf
Packit 6c0a39
        autocmd BufNewFile,BufRead * call ActivateInvisibleCharIndicator()
Packit 6c0a39
    endif
Packit 6c0a39
    " Show tabs, trailing whitespace, and continued lines visually
Packit 6c0a39
    set list listchars=tab:»·,trail:·,extends:…
Packit 6c0a39
Packit 6c0a39
    " highlight overly long lines same as TODOs.
Packit 6c0a39
    set textwidth=80
Packit 6c0a39
    autocmd BufNewFile,BufRead *.c,*.h exec 'match Todo /\%>' . &textwidth . 'v.\+/'
Packit 6c0a39
Packit 6c0a39
Packit 6c0a39
==========================
Packit 6c0a39
FAQ & Statement Reference
Packit 6c0a39
==========================
Packit 6c0a39
Packit 6c0a39
Comments
Packit 6c0a39
---------
Packit 6c0a39
Packit 6c0a39
Comments should always use the standard C syntax.  C++ style comments are not
Packit 6c0a39
currently allowed.
Packit 6c0a39
Packit 6c0a39
The lines before a comment should be empty. If the comment directly belongs to
Packit 6c0a39
the following code, there should be no empty line after the comment, except if
Packit 6c0a39
the comment contains a summary of multiple following code blocks.
Packit 6c0a39
Packit 6c0a39
This is good:
Packit 6c0a39
Packit 6c0a39
    ...
Packit 6c0a39
    int i;
Packit 6c0a39
Packit 6c0a39
    /*
Packit 6c0a39
     * This is a multi line comment,
Packit 6c0a39
     * which explains the logical steps we have to do:
Packit 6c0a39
     *
Packit 6c0a39
     * 1. We need to set i=5, because...
Packit 6c0a39
     * 2. We need to call complex_fn1
Packit 6c0a39
     */
Packit 6c0a39
Packit 6c0a39
    /* This is a one line comment about i = 5. */
Packit 6c0a39
    i = 5;
Packit 6c0a39
Packit 6c0a39
    /*
Packit 6c0a39
     * This is a multi line comment,
Packit 6c0a39
     * explaining the call to complex_fn1()
Packit 6c0a39
     */
Packit 6c0a39
    ret = complex_fn1();
Packit 6c0a39
    if (ret != 0) {
Packit 6c0a39
    ...
Packit 6c0a39
Packit 6c0a39
    /**
Packit 6c0a39
     * @brief This is a doxygen comment.
Packit 6c0a39
     *
Packit 6c0a39
     * This is a more detailed explanation of
Packit 6c0a39
     * this simple function.
Packit 6c0a39
     *
Packit 6c0a39
     * @param[in]   param1     The parameter value of the function.
Packit 6c0a39
     *
Packit 6c0a39
     * @param[out]  result1    The result value of the function.
Packit 6c0a39
     *
Packit 6c0a39
     * @return              0 on success and -1 on error.
Packit 6c0a39
     */
Packit 6c0a39
    int example(int param1, int *result1);
Packit 6c0a39
Packit 6c0a39
This is bad:
Packit 6c0a39
Packit 6c0a39
    ...
Packit 6c0a39
    int i;
Packit 6c0a39
    /*
Packit 6c0a39
     * This is a multi line comment,
Packit 6c0a39
     * which explains the logical steps we have to do:
Packit 6c0a39
     *
Packit 6c0a39
     * 1. We need to set i=5, because...
Packit 6c0a39
     * 2. We need to call complex_fn1
Packit 6c0a39
     */
Packit 6c0a39
    /* This is a one line comment about i = 5. */
Packit 6c0a39
    i = 5;
Packit 6c0a39
    /*
Packit 6c0a39
     * This is a multi line comment,
Packit 6c0a39
     * explaining the call to complex_fn1()
Packit 6c0a39
     */
Packit 6c0a39
    ret = complex_fn1();
Packit 6c0a39
    if (ret != 0) {
Packit 6c0a39
    ...
Packit 6c0a39
Packit 6c0a39
    /*This is a one line comment.*/
Packit 6c0a39
Packit 6c0a39
    /* This is a multi line comment,
Packit 6c0a39
       with some more words...*/
Packit 6c0a39
Packit 6c0a39
    /*
Packit 6c0a39
     * This is a multi line comment,
Packit 6c0a39
     * with some more words...*/
Packit 6c0a39
Packit 6c0a39
Indention & Whitespace & 80 columns
Packit 6c0a39
------------------------------------
Packit 6c0a39
Packit 6c0a39
To avoid confusion, indentations have to be 4 spaces. Do not use tabs!.  When
Packit 6c0a39
wrapping parameters for function calls, align the parameter list with the first
Packit 6c0a39
parameter on the previous line.  For example,
Packit 6c0a39
Packit 6c0a39
    var1 = foo(arg1,
Packit 6c0a39
               arg2,
Packit 6c0a39
               arg3);
Packit 6c0a39
Packit 6c0a39
The previous example is intended to illustrate alignment of function
Packit 6c0a39
parameters across lines and not as encourage for gratuitous line
Packit 6c0a39
splitting.  Never split a line before columns 70 - 79 unless you
Packit 6c0a39
have a really good reason.  Be smart about formatting.
Packit 6c0a39
Packit 6c0a39
Packit 6c0a39
If, switch, & Code blocks
Packit 6c0a39
--------------------------
Packit 6c0a39
Packit 6c0a39
Always follow an 'if' keyword with a space but don't include additional
Packit 6c0a39
spaces following or preceding the parentheses in the conditional.
Packit 6c0a39
This is good:
Packit 6c0a39
Packit 6c0a39
    if (x == 1)
Packit 6c0a39
Packit 6c0a39
This is bad:
Packit 6c0a39
Packit 6c0a39
    if ( x == 1 )
Packit 6c0a39
Packit 6c0a39
or
Packit 6c0a39
Packit 6c0a39
    if (x==1)
Packit 6c0a39
Packit 6c0a39
Yes we have a lot of code that uses the second and third form and we are trying
Packit 6c0a39
to clean it up without being overly intrusive.
Packit 6c0a39
Packit 6c0a39
Note that this is a rule about parentheses following keywords and not
Packit 6c0a39
functions.  Don't insert a space between the name and left parentheses when
Packit 6c0a39
invoking functions.
Packit 6c0a39
Packit 6c0a39
Braces for code blocks used by for, if, switch, while, do..while, etc.  should
Packit 6c0a39
begin on the same line as the statement keyword and end on a line of their own.
Packit 6c0a39
You should always include braces, even if the block only contains one
Packit 6c0a39
statement.  NOTE: Functions are different and the beginning left brace should
Packit 6c0a39
be located in the first column on the next line.
Packit 6c0a39
Packit 6c0a39
If the beginning statement has to be broken across lines due to length, the
Packit 6c0a39
beginning brace should be on a line of its own.
Packit 6c0a39
Packit 6c0a39
The exception to the ending rule is when the closing brace is followed by
Packit 6c0a39
another language keyword such as else or the closing while in a do..while loop.
Packit 6c0a39
Packit 6c0a39
Good examples:
Packit 6c0a39
Packit 6c0a39
    if (x == 1) {
Packit 6c0a39
        printf("good\n");
Packit 6c0a39
    }
Packit 6c0a39
Packit 6c0a39
    for (x = 1; x < 10; x++) {
Packit 6c0a39
        print("%d\n", x);
Packit 6c0a39
    }
Packit 6c0a39
Packit 6c0a39
    for (really_really_really_really_long_var_name = 0;
Packit 6c0a39
         really_really_really_really_long_var_name < 10;
Packit 6c0a39
         really_really_really_really_long_var_name++)
Packit 6c0a39
    {
Packit 6c0a39
        print("%d\n", really_really_really_really_long_var_name);
Packit 6c0a39
    }
Packit 6c0a39
Packit 6c0a39
    do {
Packit 6c0a39
        printf("also good\n");
Packit 6c0a39
    } while (1);
Packit 6c0a39
Packit 6c0a39
Bad examples:
Packit 6c0a39
Packit 6c0a39
    while (1)
Packit 6c0a39
    {
Packit 6c0a39
        print("I'm in a loop!\n"); }
Packit 6c0a39
Packit 6c0a39
    for (x=1;
Packit 6c0a39
         x<10;
Packit 6c0a39
         x++)
Packit 6c0a39
    {
Packit 6c0a39
        print("no good\n");
Packit 6c0a39
    }
Packit 6c0a39
Packit 6c0a39
    if (i < 10)
Packit 6c0a39
        print("I should be in braces.\n");
Packit 6c0a39
Packit 6c0a39
Packit 6c0a39
Goto
Packit 6c0a39
-----
Packit 6c0a39
Packit 6c0a39
While many people have been academically taught that "goto"s are fundamentally
Packit 6c0a39
evil, they can greatly enhance readability and reduce memory leaks when used as
Packit 6c0a39
the single exit point from a function. But in no libssh world what so ever is a
Packit 6c0a39
goto outside of a function or block of code a good idea.
Packit 6c0a39
Packit 6c0a39
Good Examples:
Packit 6c0a39
Packit 6c0a39
    int function foo(int y)
Packit 6c0a39
    {
Packit 6c0a39
        int *z = NULL;
Packit 6c0a39
        int rc = 0;
Packit 6c0a39
Packit 6c0a39
        if (y < 10) {
Packit 6c0a39
            z = malloc(sizeof(int)*y);
Packit 6c0a39
            if (z == NULL) {
Packit 6c0a39
                rc = 1;
Packit 6c0a39
                goto done;
Packit 6c0a39
            }
Packit 6c0a39
        }
Packit 6c0a39
Packit 6c0a39
        print("Allocated %d elements.\n", y);
Packit 6c0a39
Packit 6c0a39
    done:
Packit 6c0a39
        if (z != NULL) {
Packit 6c0a39
            free(z);
Packit 6c0a39
        }
Packit 6c0a39
Packit 6c0a39
        return rc;
Packit 6c0a39
    }
Packit 6c0a39
Packit 6c0a39
Initialize pointers
Packit 6c0a39
-------------------
Packit 6c0a39
Packit 6c0a39
All pointer variables MUST be initialized to NULL. History has
Packit 6c0a39
demonstrated that uninitialized pointer variables have lead to various
Packit 6c0a39
bugs and security issues.
Packit 6c0a39
Packit 6c0a39
Pointers MUST be initialized even if the assignment directly follows
Packit 6c0a39
the declaration, like pointer2 in the example below, because the
Packit 6c0a39
instructions sequence may change over time.
Packit 6c0a39
Packit 6c0a39
Good Example:
Packit 6c0a39
Packit 6c0a39
    char *pointer1 = NULL;
Packit 6c0a39
    char *pointer2 = NULL;
Packit 6c0a39
Packit 6c0a39
    pointer2 = some_func2();
Packit 6c0a39
Packit 6c0a39
    ...
Packit 6c0a39
Packit 6c0a39
    pointer1 = some_func1();
Packit 6c0a39
Packit 6c0a39
Typedefs
Packit 6c0a39
---------
Packit 6c0a39
Packit 6c0a39
libssh tries to avoid "typedef struct { .. } x_t;" so we do always try to use
Packit 6c0a39
"struct x { .. };". We know there are still such typedefs in the code, but for
Packit 6c0a39
new code, please don't do that anymore.
Packit 6c0a39
Packit 6c0a39
Make use of helper variables
Packit 6c0a39
-----------------------------
Packit 6c0a39
Packit 6c0a39
Please try to avoid passing function calls as function parameters in new code.
Packit 6c0a39
This makes the code much easier to read and it's also easier to use the "step"
Packit 6c0a39
command within gdb.
Packit 6c0a39
Packit 6c0a39
Good Example:
Packit 6c0a39
Packit 6c0a39
    char *name;
Packit 6c0a39
Packit 6c0a39
    name = get_some_name();
Packit 6c0a39
    if (name == NULL) {
Packit 6c0a39
        ...
Packit 6c0a39
    }
Packit 6c0a39
Packit 6c0a39
    rc = some_function_my_name(name);
Packit 6c0a39
    ...
Packit 6c0a39
Packit 6c0a39
Packit 6c0a39
Bad Example:
Packit 6c0a39
Packit 6c0a39
    rc = some_function_my_name(get_some_name());
Packit 6c0a39
    ...
Packit 6c0a39
Packit 6c0a39
Please try to avoid passing function return values to if- or while-conditions.
Packit 6c0a39
The reason for this is better handling of code under a debugger.
Packit 6c0a39
Packit 6c0a39
Good example:
Packit 6c0a39
Packit 6c0a39
    x = malloc(sizeof(short) * 10);
Packit 6c0a39
    if (x == NULL) {
Packit 6c0a39
        fprintf(stderr, "Unable to alloc memory!\n");
Packit 6c0a39
    }
Packit 6c0a39
Packit 6c0a39
Bad example:
Packit 6c0a39
Packit 6c0a39
    if ((x = malloc(sizeof(short)*10)) == NULL ) {
Packit 6c0a39
        fprintf(stderr, "Unable to alloc memory!\n");
Packit 6c0a39
    }
Packit 6c0a39
Packit 6c0a39
There are exceptions to this rule. One example is walking a data structure in
Packit 6c0a39
an iterator style:
Packit 6c0a39
Packit 6c0a39
    while ((opt = poptGetNextOpt(pc)) != -1) {
Packit 6c0a39
        ... do something with opt ...
Packit 6c0a39
    }
Packit 6c0a39
Packit 6c0a39
But in general, please try to avoid this pattern.
Packit 6c0a39
Packit 6c0a39
Packit 6c0a39
Control-Flow changing macros
Packit 6c0a39
-----------------------------
Packit 6c0a39
Packit 6c0a39
Macros like STATUS_NOT_OK_RETURN that change control flow (return/goto/etc)
Packit 6c0a39
from within the macro are considered bad, because they look like function calls
Packit 6c0a39
that never change control flow. Please do not introduce them.