Monday, February 23, 2009

I am an avid vim user.  I cannot compare it to emacs, as I have rarely used emacs because of its werird keyboard sequence.  Anyway its important to carry around your vimenv with you.  I am posting below my .vimrc file.  In addition, I have had to add the following extensions to make it do my bidding:

1. vimshell : Vim  has had buffers for a few versions. But it does not support shell access in any of its buffers. Enter vimshell. Its extremely convenient when debugging your code. Thing is you have to patch the vim code and recompile it. But its totally worth it.
2. matchit.vim plugin: google it. Its lets you to match on html/xml tags as well Super userful

-------------------------vimrc----------------------------------------------
"type :help option to get help on options below
set tags=tags;/
set nocompatible
set backspace=2
set confirm
set dictionary=/usr/share/dict/words
set formatoptions=tcq2
set incsearch
set listchars=tab:»·,trail:·
set report=1
set shortmess=fnrxotTI
set smarttab
set textwidth=78
set title
set whichwrap=
set wildmenu
set tabstop=2
set shiftwidth=2
set expandtab
set paste
set ruler
set incsearch  " do incremental searching
set history=50
set hlsearch
"set viminfo='20,\"50  " read/write a .viminfo file, don't store more
      " than 50 lines of registers
set viminfo='10,\"100,:20,%,n~/.viminfo
au BufReadPost * if line("'\"") > 0|if line("'\"") <= line("$")|exe("norm '\"")|else|exe "norm $"|endif|endif

filetype plugin on

syntax on

function CodePrefs()
    set cindent
    set cinoptions=(0,u0
    set expandtab
    set makeprg=gmake
    set shiftwidth=4
    set textwidth=0
endfun
if has("autocmd")
    autocmd FileType c,cpp,html,make,perl,php,php3,php4,inc,sh,dc call CodePrefs()
    autocmd FileType make set noexpandtab nosmarttab
    autocmd FileType mail set textwidth=72 titleold=mutt
    autocmd FileType html set shiftwidth=2
endif

" Transparent editing of gpg encrypted files.
" By Wouter Hanegraaff
augroup encrypted
    au!

    " First make sure nothing is written to ~/.viminfo while editing
    " an encrypted file.
    autocmd BufReadPre,FileReadPre      *.gpg set viminfo=
    " We don't want a swap file, as it writes unencrypted data to disk
    autocmd BufReadPre,FileReadPre      *.gpg set noswapfile
    " Switch to binary mode to read the encrypted file
    autocmd BufReadPre,FileReadPre      *.gpg set bin
    autocmd BufReadPre,FileReadPre      *.gpg let ch_save = &ch|set ch=2
    autocmd BufReadPost,FileReadPost    *.gpg '[,']!gpg --decrypt -q -a 2>/dev/null
    " Switch to normal mode for editing
    autocmd BufReadPost,FileReadPost    *.gpg set nobin
    autocmd BufReadPost,FileReadPost    *.gpg let &ch = ch_save|unlet ch_save
    autocmd BufReadPost,FileReadPost    *.gpg execute ":doautocmd BufReadPost " . expand("%:r")

    " Convert all text to encrypted text before writing
    autocmd BufWritePre,FileWritePre    *.gpg   '[,']!gpg --encrypt --default-recipient "Punit Rathore " -q -a 2>/dev/null
    " Undo the encryption so we are back in the normal text, directly
    " after the file has been written.
    autocmd BufWritePost,FileWritePost    *.gpg   u
augroup END


Friday, February 6, 2009

Automate tedious tasks using Autoit

One of my collegues was looking for a script that would let him automate his daily ritual of opening up 4-5 putty sessions to his linux host. I had used autoit before and suggested it to him. I hadn't used it in a while and was curious as to how it worked. After downloading it took me all of 45 minutes to come up with the following script. It asks for your password, then uses it to log on to 5 different putty sessions!!
---------------------------------------------------
; this script logs you on to 5 different putty sessions

$answer =  InputBox ("enter password", "please enter your password","","*",150,80)

If $answer = "" Then
MsgBox(0, "AutoIt Example", "Enter valid password")
Exit
EndIf

Opt("WinTitleMatchMode", 2)

For $count = 1 To 5
Run("C:\Program Files\PuTTY\putty.exe -ssh username@hostname")
WinWaitActive("PuTTY")
Sleep(1500) 
;MsgBox(0,"Sending password" ,$answer)
Send($answer)
Send("{ENTER}")
Next
--------------------------------------------------------------

Thursday, February 5, 2009

Traps in printf

I was debugging this segmentation fault that we were seeing at a customer. It wasn't caught because in never hit the code path in dev/QA. It would happen when logging was enabled. GNU debugger seemed to indicate that the logging library was using the vnsprintf call which uses va_list (variable argument list) as input. I spent close to 2 hrs trying to debug this before realizing the error. So lets see if you can try to figure it out. The same problem exists in printf as well:

printf(" some randmon long %l and string %s \n", 4, "hello");

compile the above and it will throw segv.

Thing is, its easy to assume that %l is a specifier for long. Its not. Its a length specifier not type specifier. You have use %ld. In absense of that printf will assumen that the first arguement ( 4) is a string and throw segv.

Lesson: variable arugement is very powerful. but be aware of the traps.

Monday, February 2, 2009

The case of missing core dumps

Have you had situations when your program seg faulted and yet did not create a core file. Well run the following commad :
> ulimit -a
if you see "core file size  0" then there's your answer.
To enable run the follwoing :
> ulimit -c unlimited

viola.