Debian-installer style guide
============================

(This guide is ripped off from multiple sources, most notably Busybox's
style guide)

This document describes the coding style conventions used in
debian-installer. If you add a new file to any of the subprojects in
debian-installer or are editing an existing file, please format your code
according to this style. If you are the maintainer of a file that does not
follow these guidelines, please -- at your own convenience -- modify the
file(s) you maintain to bring them into conformance with this style guide.
Please note that this is a low priority task.

Formatting
----------

Line length
~~~~~~~~~~~

Keep line length within 80 characters.
(allow printing on old printers, it means that
inclusion as verbatim text in webpages and other documents is possible)

Tabs vs spaces
~~~~~~~~~~~~~~

d-i does not currently have a well-defined policy on tabs vs. spaces for C
code. Please respect the indentation method used in the file; two are
common:

  1. Indent lines with spaces.  Each indent is four spaces.
  2. Indent blocks with tabs. Use spaces for indentation that is not a code
     block (presentational indentation).

Since shell scripts that are part of the installer should be as small as
possibly to minimise its runtime footprint, and since indenting with spaces
actually makes files larger than indenting with tabs, we do have a
well-defined policy for shell scripts in d-i:

  Indent blocks with tabs. Use spaces (rarely) for presentational indentation.

One situation where use of spaces makes script code more readable by limiting
total indentation and in most cases saves space as well, is in case statements:
	case X in
	    foo)
		# code block for foo
		;;
	    bar)
		# code block for bar
		;;
	esac
So, the selection lines are indented with 4 spaces and the code for the
selections with an extra tab (note: not 8 spaces or 4 spaces + tab!).

Any file that contains a combination of two or more indentation types
can be reformatted by anyone who wasn't responsible for it getting into
that messed up state in the first place, using whatever indentation method
they prefer.

Operator Spacing
~~~~~~~~~~~~~~~~

Put spaces between terms and operators. Example:

Don't do this:

      for(i=0;i<num_items;i++){

Do this instead:

      for (i = 0; i < num_items; i++) {

While it extends the line a bit longer, the spaced version is more
readable. An allowable exception to this rule is the situation where
excluding the spacing makes it more obvious that we are dealing with a
single term (even if it is a compound term) such as:

      if (str[idx] == '/' && str[idx-1] != '\\')

or

      if ((argc-1) - (optind+1) > 0)

Bracket Spacing
~~~~~~~~~~~~~~~

If an opening bracket starts a function, it should be on the
next line with no spacing before it. However, if a bracket follows an opening
control block, it should be on the same line with a single space (not a tab)
between it and the opening control block statement. Examples:

Don't do this:

      while (!done)
      {

      do
      {

Don't do this either:

      while (!done){

      do{

And for heaven's sake, don't do this:

      while (!done)
        {

      do
        {

Do this instead:

      while (!done) {

      do {


Spacing around Parentheses
~~~~~~~~~~~~~~~~~~~~~~~~~~

Put a space between C keywords and left parens, but not between function names
and the left paren that starts its parameter list (whether it is being
declared or called). Examples:

Don't do this:

      while(foo) {
      for(i = 0; i < n; i++) {

Do this instead:

      while (foo) {
      for (i = 0; i < n; i++) {

But do functions like this:

      static int my_func(int foo, char bar)
      ...
      baz = my_func(1, 2);

Also, don't put a space between the left paren and the first term, nor between
the last arg and the right paren.

Don't do this:

      if ( x < 1 )
      strcmp( thisstr, thatstr )

Do this instead:

      if (x < 1)
      strcmp (thisstr, thatstr)

Cuddled Elses
~~~~~~~~~~~~~

Also, please "cuddle" your else statements by putting the else keyword on the
same line after the right bracket that closes an 'if' statement.

Don't do this:

      if (foo) {
          stmt;
      }
      else {
          stmt;
      }

Do this instead:

     if (foo) {
         stmt;
     } else {
         stmt;
     }

The exception to this rule is if you want to include a comment before the else
block. Example:

     if (foo) {
         stmts...
     }
     /* otherwise, we're just kidding ourselves, so re-frob the input */
     else {
         other_stmts...
     }

Variable and Function Names
---------------------------

Use the K&R style with names in all lower-case and underscores occasionally
used to separate words (e.g., "variable_name" and "numchars" are both
acceptable). Using underscores makes variable and function names more readable
because it looks like whitespace; using lower-case is easy on the eyes.

        Frowned upon:

                hitList
                TotalChars
                szFileName
                pf_Nfol_TriState

        Preferred:

                hit_list
                total_chars
                file_name
                sensible_name

Exceptions:

 - Enums, macros, and constant variables are occasionally written in all
   upper-case with words optionally separated by underscores (i.e. FIFOTYPE,
   ISBLKDEV()).

