C implementation of clear command

technical

Following is the code to clear the screen on terminal.

main() {write(1, "\33[H\33[2J", 7);}

Above program does nothing but writes an escape sequence on screen. Escape sequences are used to control the display properties like graphics,control cursor movements and key reassignment. Above one is specific to clear the screen. \33[H sends the cursor to home location and \33[2J clears the text on screen. It can also be used interchangeably i.e first clear the screen and then send the cursor to home location since printing of terminal info (like root@localhost] ) takes place after executing the complete process though if you write the separate programs for both escape sequences individually and then execute them sequentially, output may differ based on order of execution.

Wikipedia reference for escape sequences.

A complete list of escape sequences.

Little more explanation:

\33[ means an Escape sequence (of characters) follows just as \ means an escape character follows. Surprisingly not mentioned in the wiki or the other link! A common macro definition is:

#define     ANSI_ESCAPE_SEQUENCE(EscapeSeq)   "\33[" EscapeSeq

Write Linux device drivers

driversSome good tutorials on how to write Linux device drivers (some are old but good for concept-building nonetheless):

The Free Electrons LXR (Linux Cross Reference) for referring the kernel source. However, I prefer ctags and cscope for lengthy source-code browsing sessions.

The GNU build process and cross-compilation

gnuIf you are in the Embedded Linux world it’s very important to be well-versed on the makefiles, configure files, build process and cross-compilation. The link below covers this topic:
The GNU configure and build system

Cross-compilation basics:
Introduction to cross-compiling for Linux

Here is a more specific guide on how to cross-compile for the ARM architecture:
ARM cross-compiling howto

How to build a cross-compiler:

Which programming language to start with?

hacker_compThis is an eternal question dug over and over during engineering graduation days. IMHO, it’s best to start with C. It is a very rich language and is more powerful than any other non-obscure 😉 programming language ever written. Many other programming languages are written based on C. It is not very difficult to learn new programming languages when you already are well-versed in one. In my personal experience, I studied for 15 days and scored 98% in SCJP then conducted by SUN Microsystems. I haven’t worked regularly in JAVA since 5 years but even today it doesn’t block me from looking into a piece of JAVA code or reviewing them. The truth is – learning a language doesn’t matter if you know only the syntax and features of the language, a complete understanding of how to convert any logic into that language in the best possible way is required. It takes minimum 6 years for a good programmer to claim that he knows C (or any other language) well. One more point to remember here is – if you are interested in systems programming it’s better to marry C ;); C++, JAVA etc. are engineered more towards customer requirement oriented rapid application development. I mean business, not elegance.

ctags & cscope: the fastest IDE

vim_compIf you are a Linux developer, there is no alternative to the deadly combination of ctags and cscope when it comes to source code browsing and editing. I remember years back, out of mere curiosity I started learning them one evening and ended up in practicing throughout the night to get accustomed. I am a fan of ctags and cscope since then. They are much much lighter and faster than any other IDE I have laid my hands on.

ctags and cscope are the solutions to all your code browsing needs

Installation

To install on Ubuntu:

$ sudo apt-get install exuberant-ctags cscope

Usage

I added the following in my .bashrc to generate the tags and cscope file data:

function ta ()
{
    #clean older info
    rm -rf tags
    rm -rf cscope.files
    rm -rf cscope.out
    # generate new info
    find $PWD | egrep -i "\.(c|h|cpp)$" > cscope.files
    ctags -R . *.c *.h *.cpp --tag-relative=yes ./
}

To generate tags and cscope file information, navigate to the root directory of your project and run

$ ta

Troubleshooting on SuSE

I got the following error repeatedly on SuSE while trying to open tag search results (though it worked well on Ubuntu):

E429: File "/path/to/file.c" does not exist

Here are the steps I followed to fix it:

  1. Generate cscope.files with absolute path
    $ find /path/to/project/files | egrep -i "\.(c|h|cpp)$" > cscope.files
  2. Generate the tags file
    $ ctags -R . *.c *.h *.cpp --tag-relative=yes ./

A list of excellent tutorials, tips etc. to learn ctags & cscope:

The only plug-ins I use:

  • MiniBufExplorer
    NOTE: Add ‘set hidden‘ in your ~/.vimrc not to lose syntax highlighting when you close a buffer. This hides the buffer instead of closing them. vim has a bug which causes loss of syntax highlighting on a buffer quit.
  • Taglist
  • CScope maps
  • a.vim

Some pointers:

  • search word under cursor: <Shift-#>
  • find file with pattern in cscope: cs f fe file //check cscope_maps.vim for other switches
  • to list all possibilities: type :ts IPPR and then press <Ctrl-d>
  • <Ctrl-]> jumps to definition while <Ctrl-o> returns to previous location (double <'> does the same too). <Ctrl-i> jumps forward.

Some handy .vimrc entries to enjoy your mug of vi:

set ai //leads to scattered code when pasting in remote terminal (like PuTTY) on Windows,
//run set noai first to avoid.
set cindent //c like indentation
set ic //ignore case while searching
set incsearch //incremental search during typing
set hlsearch //highlight search matches
set ts=4 //tab length
set sw=4 //shift width, amount on shift you want in new lines
set nu //show line numbers

Happy coding! 🙂