Saturday, 21 April 2012

How to set EMACS C STYLE to hack KAMAILIO code

Kamailio (former OpenSER) is a SIP proxy server, call router, and user agent registration server used in Voice over Internet Protocol and instant messaging applications. Kamailio is free software (GPL licensed).

In this post we are going to learn how to configure emacs to follow kamailio coding style guidelines.


From Kamailio documentation:

As a resume:
  • Use tabs for indentation instead of spaces.
  • Set tab stops to 4 spaces.
  • Wrap lines that are longer than 78 characters (80 characters - two characters for window decorations).
  • Avoid C++ style comments (//)
  • Do not declare variables inside blocks.
  • Use the following style for function declarations
  • int func(int x)
        /* body */


We are going to create an emacs C coding style in order to comply with most of those rules.

We could add in our .emacs configuration file:
;; Kamailio settings for .emacs file. 
(setq-default indent-tabs-mode t) ;; Use tabs instead of spaces.
(setq-default tab-width 4)
;; (setq-default c-basic-offset 4) # Not needed, it comes from stroustrup base settings.
(c-add-style "kamailio"  '("stroustrup"
    (case-label . +))))
c-add-style function sets that style by default.


If we want to manualy set the C coding style, when in C mode we execute:

;; M-x c-set-style

C-c . RET kamailio RET ;; sets kamailio coding style.


C-c . RET TAB ;; To show a available style list.

C Stroustrup style

Kamailio style is based on stroustrup one:

  (c-basic-offset . 4)
  (c-comment-only-line-offset . 0)
   (statement-block-intro . +)
   (substatement-open . 0)
   (substatement-label . 0)
   (label . 0)
   (statement-cont . +)))

We could read more info using these commands:

C-h v c-style-alist ;; to see current default styles.

C-h v c-offsets-alist ;; help about offset settings.

Whitespace mode

Whitespace is an useful emacs mode that shows space and tabulators and displays using different colour where current buffer code does not match current coding style.

M-x whitespace-mode

Inserting TAB character

If we simply want to insert TAB character instead of calling indenting function.

C-q TAB ;; C-q stands for quote.

Disabling syntactic indentation

Previous settings use syntactic indentation to follow close kamailio guidelines.
Another way is to disable C syntactic indentation and let the user type the right indentation himself.

To accomplish that those settings are needed in .emacs file:
(setq-default indent-tabs-mode nil) ;                                                                                                
(setq-default tab-width 4) ; set tab width to 4 for all buffers                                                                      
(setq-default c-syntactic-indentation nil) ;                                                                                         
(setq-default c-basic-offset 4) ; 

When C syntactic indentation disabled, next line indents to previous one, and TAB moves cursor one tabulator distance (that is c-basic-offset).


SIP Router Project - Wiki Site

c-offsets-alist variable help:

C-h v c-offsets-alist ;; help about offset settings.

string                 -- Inside multi-line string.
 c                      -- Inside a multi-line C style block comment.
 defun-open             -- Brace that opens a function definition.
 defun-close            -- Brace that closes a function definition.
 defun-block-intro      -- The first line in a top-level defun.
 class-open             -- Brace that opens a class definition.
 class-close            -- Brace that closes a class definition.
 inline-open            -- Brace that opens an in-class inline method.
 inline-close           -- Brace that closes an in-class inline method.
 func-decl-cont         -- The region between a function definition's
                           argument list and the function opening brace
                           (excluding K&R argument declarations).  In C, you
                           cannot put anything but whitespace and comments
                           between them; in C++ and Java, throws declarations
                           and other things can appear in this context.
 knr-argdecl-intro      -- First line of a K&R C argument declaration.
 knr-argdecl            -- Subsequent lines in a K&R C argument declaration.
 topmost-intro          -- The first line in a topmost construct definition.
 topmost-intro-cont     -- Topmost definition continuation lines.
 member-init-intro      -- First line in a member initialization list.
 member-init-cont       -- Subsequent member initialization list lines.
 inher-intro            -- First line of a multiple inheritance list.
 inher-cont             -- Subsequent multiple inheritance lines.
 block-open             -- Statement block open brace.
 block-close            -- Statement block close brace.
 brace-list-open        -- Open brace of an enum or static array list.
 brace-list-close       -- Close brace of an enum or static array list.
 brace-list-intro       -- First line in an enum or static array list.
 brace-list-entry       -- Subsequent lines in an enum or static array list.
 brace-entry-open       -- Subsequent lines in an enum or static array
                           list that start with an open brace.
 statement              -- A C (or like) statement.
 statement-cont         -- A continuation of a C (or like) statement.
 statement-block-intro  -- The first line in a new statement block.
 statement-case-intro   -- The first line in a case "block".
 statement-case-open    -- The first line in a case block starting with brace.
 substatement           -- The first line after an if/while/for/do/else.
 substatement-open      -- The brace that opens a substatement block.
 substatement-label     -- Labelled line after an if/while/for/do/else.
 case-label             -- A "case" or "default" label.
 access-label           -- C++ private/protected/public access label.
 label                  -- Any ordinary label.
 do-while-closure       -- The "while" that ends a do/while construct.
 else-clause            -- The "else" of an if/else construct.
 catch-clause           -- The "catch" or "finally" of a try/catch construct.
 comment-intro          -- A line containing only a comment introduction.
 arglist-intro          -- The first line in an argument list.
 arglist-cont           -- Subsequent argument list lines when no
                           arguments follow on the same line as the
                           arglist opening paren.
 arglist-cont-nonempty  -- Subsequent argument list lines when at
                           least one argument follows on the same
                           line as the arglist opening paren.
 arglist-close          -- The solo close paren of an argument list.
 stream-op              -- Lines continuing a stream operator construct.
 inclass                -- The construct is nested inside a class definition.
                           Used together with e.g. `topmost-intro'.
 cpp-macro              -- The start of a C preprocessor macro definition.
 cpp-macro-cont         -- Inside a multi-line C preprocessor macro definition.
 friend                 -- A C++ friend declaration.
 objc-method-intro      -- The first line of an Objective-C method definition.
 objc-method-args-cont  -- Lines continuing an Objective-C method definition.
 objc-method-call-cont  -- Lines continuing an Objective-C method call.
 extern-lang-open       -- Brace that opens an "extern" block.
 extern-lang-close      -- Brace that closes an "extern" block.
 inextern-lang          -- Analogous to the `inclass' syntactic symbol,
 inextern-lang          -- Analogous to the `inclass' syntactic symbol,
                           but used inside "extern" blocks.
 namespace-open, namespace-close, innamespace
                        -- Similar to the three `extern-lang' symbols, but for
                           C++ "namespace" blocks.
 module-open, module-close, inmodule
                        -- Similar to the three `extern-lang' symbols, but for
                           CORBA IDL "module" blocks.
 composition-open, composition-close, incomposition
                        -- Similar to the three `extern-lang' symbols, but for
                           CORBA CIDL "composition" blocks.
 template-args-cont     -- C++ template argument list continuations.
 inlambda               -- In the header or body of a lambda function.
 lambda-intro-cont      -- Continuation of the header of a lambda function.
 inexpr-statement       -- The statement is inside an expression.
 inexpr-class           -- The class is inside an expression.  Used e.g. for
                           Java anonymous classes.

0 comentarios: