204Chapter7 • Miscellaneous Tools
7.1Using indent Utility
One major objective of every software development project is that the code be well organized,
structured and easy to understand for other members of the team. Proper indentation plays an
important role in achieving these objectives. Most of the editors used in software development
can do automatic indentation if they are configured to do so. The Emacs editor is the best exam-
ple as it understands different programming languages and does indentation according to lan-
guage rules and style. However you can come across code written by others that is poorly
indented and you may like to re-indent it according to your own requirements or preferences. In
large software development projects, you may try to enforce a particular coding style, but be
assured that not everyone will follow it. The
indent
program is your tool to handle the situa-
tion and reformat all files in the project according to the style adopted for the project. You can do
it by creating a script that goes into each directory of the project and reformats all files. In this
section we shall explore some of its features and how to utilize it.
By default, the
indent
program uses the GNU style of coding and applies these rules to
input files. It creates a backup file with original contents of the file. The reformatted file is cre-
ated with the same name. The backup file ends with a tilde character ~. Note that the same
scheme of creating backup files is used by the Emacs editor.
Let us see how indent works. We will take a poorly indented file and apply the
indent
program to it. Following is a listing of the poorly indented file
hello.c
.
1 /**************************************
2 * hello.c
3 *
4 * This file is used to demonstrate use
5 * of indent utility
6 *************************************/
7 #include
8
9 main () {
10 char string[25] ;
11 printf ("Enter a string of characters : ") ;
12 scanf ("%s", string);
13 printf ("The entered string is : %s\n ", string);
14 }
Line numbers are added to the listing using the “
cat –n hello.c
” command to
explain changes. They are not part of the
hello.c
file. To properly indent the file
hello.c
,
the following command is executed:
indent hello.c
The resulting
hello.c
file is shown below. Note how the
indent
program has modi-
fied the file. Lines 10 to 14 are indented to two characters (depending upon default settings).
Next Page >>
<< Previous Page
Back to the Table of Contents