LDP/LDP/howto/linuxdoc/C-C++Beautifier-HOWTO.sgml

449 lines
15 KiB
Plaintext

<!doctype linuxdoc system>
<!--
************************** begin comment *****************************
The following is the HOW-TO for setting up C-C++Beautifier editor.
This document is in the SGML format. You must use sgml package to
process this document
************************* end of comment *****************************
-->
<!--
************************** SGML USER GUIDE *****************************
The SGML user guide on linux is located at /usr/doc/sgml-tools
Read the example.sgml and guide.html documents.
Usage:
HTML sgml2html foo (Do not give extension .sgml here!!)
Text sgml2txt foo.sgml
Latex sgml2latex foo.sgml
Postscript sgml2latex -language=english -o ps foo.sgml
DVI sgml2latex -d foo.sgml
Lyx sgml2lyx foo.sgml
Richtext sgml2rtf foo.sgml
gnuinfo sgml2info foo.sgml
man sgml2txt -man foo.sgml
SGML sgmlcheck foo.sgml
************************* end of comment *****************************
-->
<article>
<!-- Title information -->
<title>C-C++ Beautifier HOW-TO
<!-- chapt change
C-C++ Beautifier HOW-TO
-->
<author>Al Dev (Alavoor Vasudevan)
<htmlurl url="mailto:alavoor@yahoo.com"
name="alavoor@yahoo.com">
<date>v3.0, 21 April 2000
<abstract>
This document will help you to format (beautify) the C/C++ programs so
that it is more readable and confirms to your site C/C++ coding standards.
The information in this document applies to all the operating sytems
that is - Linux, MS DOS, Windows 95/NT, OS/2, IBM OSes, all flavors of
Unix like Solaris, HPUX, AIX, SCO, Sinix, BSD, SCO, etc.. and to
all other operating systems which support "C" compiler (it means almost all the
operating systems on this planet!).
</abstract>
<!-- Table of contents -->
<toc>
<!-- Begin the document -->
<!--
*******************************************
************ End of Section ***************
*******************************************
<chapt>Introduction
-->
<sect>Introduction
<p>
Coding standards for C/C++ or any language is required in order to
make the programs more readable/understandable by programmers. There are
C/C++ beautifiers (formating tools) to accomplish this goal.
Formatted (beautified) code improves
the <bf>productivity</bf> of programmers by <bf>2 times</bf>!!
On Linux/Unixes there is a command called <bf>"indent"</bf> and <bf>"cb"</bf>
. Refer to 'man indent' and 'man cb'. Note that <bf>indent</bf> and
<bf>cb</bf> work for only "C" programs. For "C++" programs use
<bf>"bcpp"</bf>. Download the beautifier program from one of the following
<itemize>
<item>C++ : BCPP site is at <url url="http://www.clark.net/pub/dickey/bcpp/bcpp.html">
<item>C++ : <url url="http://www.consultix-inc.com/www.consultix-inc.com/c++b.html">
<item>C : <url url="http://www.chips.navy.mil/oasys/c/">
</itemize>
I used BCPP to format the C++ programs and it worked fine for me. You
may want to check other tools and use the one which you may like the most.
<!--
*******************************************
************ End of Section ***************
*******************************************
<chapt> How can I trust Beautifier programs??!!
-->
<sect> How can I trust Beautifier programs??!!
<p>
For 100% assurance you need a <bf>SCIENTIFIC</bf> way to
validate and trust a beautifier program. The method described
in this section will enable the beautifier program to be accepted
as "trust-worthy" and reliable.
In order to verify that beautifier programs like
<bf>bcpp</bf>, <bf>indent</bf> or <bf>cb</bf> is not damaging or
changing the input source-code after formatting, you can use the
shell script <ref id="verification" name="verification program">
or use the following technique -
Generate the object code from the original input
source code using the compiler -
<code>
g++ -c myprogram.cpp
</code>
Here g++ is GNU C++ compiler. This will create object
output myprogram.o
Save this file -
<code>
mv myprogram.o myprogram_orig.o
</code>
Now run bcpp -
<code>
bcpp myprogram.cpp
</code>
This will create the formatted output program file myprogram.cpp and
move the original file to myprogram.cpp.orig. Compile the
new file with -
<code>
g++ -c myprogram.cpp
</code>
Now use the unix 'diff' command to compare the two object files -
<code>
diff myprogram.o myprogram_orig.o
</code>
Both these files <bf>MUST BE IDENTICAL</bf>. This verifies that
bcpp is working perfectly. On DOS or Windows 95 you may want to use
the free <url name="Cygnus" url="http://www.cygnus.com"> Cygwin
'diff' or 'MKS' utilities.
Also you can use the assembler output instead of
object output in compiler. Like -
<code>
g++ -S myprogram.cpp
</code>
This creates myprogram.s. Verify with -
<code>
diff myprogram.s myprogram_orig.s
</code>
This step gives 100% guarantee that your valuable
source code is intact and bcpp is JUST doing ONLY
formatting and is NOT changing or damaging your code in any way.
This method gives you 100% quality assurance and life term or long term
<bf>WARRANTY</bf> on beautifier programs like 'bcpp',
'cb' or 'indent'.
It is strongly recommended that you do these two steps
every time you run beautifier programs like <bf>bcpp</bf>,
<bf>indent</bf> or <bf>cb</bf>.
<!--
*******************************************
************ End of Section ***************
*******************************************
<chapt> Tools for other Languages
-->
<sect> Beautifiers for other Languages
<p>
Visit the following sites to get beautifiers for other languages like
HTML, SQL, Java, Perl, Fortran.
<itemize>
<item> HTML : <url url="http://www.digital-mines.com/htb/">
<item> HTML : <url url="http://www.datacomm.ch/mwoog/software/perl/beautifier.html">
<item> HTML : <url url="http://www.watson-net.com/free/perl/s_fhtml.asp">
<item> SQL : <url url="http://www.netbula.com/products/sqlb">
<item> Java : <url url="http://home.wtal.de/software-solutions/jindent/frameset.html">
<item> Perl : <url url="http://www.consultix-inc.com/www.consultix-inc.com/talk.htm">
<item> Perl : <url url="http://www.consultix-inc.com/www.consultix-inc.com/perl_beautifier.html">
<item> Fortran beautifier : <url url="http://www.aeem.iastate.edu/Fortran/tools.html">
<item>C++ : BCPP site is at <url url="http://www.clark.net/pub/dickey/bcpp/bcpp.html">
<item>C++ : <url url="http://www.consultix-inc.com/www.consultix-inc.com/c++b.html">
<item>C : <url url="http://www.chips.navy.mil/oasys/c/">
</itemize>
<itemize>
<item>White paper on beautifier : <url url="http://www.consultix-inc.com/www.consultix-inc.com/talk.htm">
</itemize>
To create presentation of codes to display using HTML -
<itemize>
<item>Presentation (C,C++,Java) to html : <url url="http://www.perlstudio.de/cbindex.html">
</itemize>
Also search the search engines like <url url="http://www.yahoo.com">
or <url url="http://www.lycos.com"> and search for keyword "beautfier".
<!--
*******************************************
************ End of Section ***************
*******************************************
<chapt> Verification Script <label id="verification">
-->
<sect> Verification Script <label id="verification">
<p>
This is a Korn shell script to verify beautifier program. Requires "pdksh*.rpm" from
Linux 'contrib' cdrom. Save this file as 'text' file and chmod a+rx on it. You can
re-write this shell script in PERL so that
you can use it on Window 95/NT or MSDOS. Uncomment the PRGM variable to
point to
<bf>bcpp</bf>,
<bf>cb</bf>
or <bf>indent</bf>
<code>
#!/bin/ksh
# Verification program to check C++ Beautifiers 'bcpp', 'indent' or cb
############################################################
# Copyright
# The copyright policy is GNU/GPL.
# Author: Al Dev (Alavoor Vasudevan) alavoor@yahoo.com
############################################################
check_beautify_now()
{
# Remove all the temp files....
\rm -f ${TMP_FILE}
\rm -f ${TMP_CPPFILE}*.*
FNAME=$1
if [ ! -f ${FNAME} ]; then
print "\nError: The file ${FNAME} does not exist!!. Aborting now ...."
exit
fi
\cp -f ${FNAME} ${TMP_CPPFILE}.cpp
${COMPILER} -c ${TMP_CPPFILE}.cpp
if [ ! -f ${TMP_CPPFILE}.o ]; then
print "Fatal Error: Failed to compile ${FNAME}. Aborting now... "
exit
fi
\mv -f ${TMP_CPPFILE}.o ${TMP_CPPFILE}_orig.o
aa=`basename $PRGM`
print "\nRunning, verifying $aa on ${FNAME}"
${PRGM} ${TMP_CPPFILE}.cpp
${COMPILER} -c ${TMP_CPPFILE}.cpp
\rm -f $TMP_FILE
diff ${TMP_CPPFILE}.o ${TMP_CPPFILE}_orig.o 1> $TMP_FILE 2>> $TMP_FILE
result=""
result=`wc -c $TMP_FILE | awk '{print $1}' `
if [ "$result" = "0" ]; then
print "Success!! Beautifier $aa is working properly!!\n"
else
print "Fatal Error: Something wrong!! Beautifier is not working!!"
exit
fi
# ${COMPILER} -S ${TMP_CPPFILE}.cpp
# diff ${TMP_CPPFILE}.s ${TMP_CPPFILE}_orig.s
# Remove all the temp files....
\rm -f ${TMP_FILE}
\rm -f ${TMP_CPPFILE}*.*
}
########## Main of program begins here ##################3
#PRGM=/usr/bin/bcpp
#PRGM=/usr/bin/cb
PRGM=/usr/bin/indent
COMPILER=/usr/bin/g++
TMP_FILE=beautify.tmp
TMP_CPPFILE=beautify-tmp_cppfile
print -n "Enter the C++ file name <default is *.cpp> : "
read ans
if [ "$ans" = "" -o "$ans" = " " ]; then
ans="ALL"
else
FILENAME=$ans
fi
# Remove all the temp files....
\rm -f ${TMP_FILE}
\rm -f ${TMP_CPPFILE}*.*
if [ "$ans" != "ALL" ]; then
check_beautify_now ${FILENAME}
else
ls *.cpp |
while read FILENAME
do
check_beautify_now ${FILENAME}
done
fi
</code>
<!--
*******************************************
************ End of Section ***************
*******************************************
<chapt change> Related URLs
-->
<sect> Related URLs
<p>
Visit following locators which are related to C, C++ -
<itemize>
<item> <url url="http://metalab.unc.edu/LDP/HOWTO/Vim-HOWTO.html" name="Vim color text editor for C++, C">
<item> <url url="http://metalab.unc.edu/LDP/HOWTO/C++Programming-HOWTO.html" name="C++ Programming HOWTO">
<item> <url url="http://metalab.unc.edu/LDP/HOWTO/CVS-HOWTO.html" name="CVS HOWTO for C++ programs">
<item> Linux goodies main site <url url="http://www.aldev.8m.com">
<item> Linux goodies mirror site <url url="http://www.aldev.webjump.com">
</itemize>
<!--
*******************************************
************ End of Section ***************
*******************************************
<chapt change> Other Formats of this Document
-->
<sect> Other Formats of this Document
<p>
This document is published in 11 different formats namely - DVI, Postscript,
Latex, Adobe Acrobat PDF,
LyX, GNU-info, HTML, RTF(Rich Text Format), Plain-text, Unix man pages and SGML.
<itemize>
<item>
You can get this HOWTO document as a single file tar ball in HTML, DVI,
Postscript or SGML formats from -
<url url="ftp://metalab.unc.edu/pub/Linux/docs/HOWTO/other-formats/">
or
<url url="ftp://metalab.unc.edu/pub/Linux/docs/HOWTO/other-formats/">
<item>Plain text format is in: <url url="ftp://metalab.unc.edu/pub/Linux/docs/HOWTO">
or
<url url="ftp://metalab.unc.edu/pub/Linux/docs/HOWTO">
<item>Translations to other languages like French, German, Spanish,
Chinese, Japanese are in
<url url="ftp://metalab.unc.edu/pub/Linux/docs/HOWTO">
or <url url="ftp://metalab.unc.edu/pub/Linux/docs/HOWTO">
Any help from you to translate to other languages is welcome.
</itemize>
The document is written using a tool called "SGML tool" which can be got from -
<url url="http://www.xs4all.nl/~cg/sgmltools/">
Compiling the source you will get the following commands like
<itemize>
<item>sgml2html C-C++Beautifier-howto.sgml (to generate html file)
<item>sgml2rtf C-C++Beautifier-howto.sgml (to generate RTF file)
<item>sgml2latex C-C++Beautifier-howto.sgml (to generate latex file)
</itemize>
This document is located at -
<itemize>
<item> <url url="http://metalab.unc.edu/LDP/HOWTO/C-C++Beautifier-HOWTO.html">
</itemize>
Also you can find this document at the following mirrors sites -
<itemize>
<item> <url url="http://www.caldera.com/LDP/HOWTO/C-C++Beautifier-HOWTO.html">
<item> <url url="http://www.WGS.com/LDP/HOWTO/C-C++Beautifier-HOWTO.html">
<item> <url url="http://www.cc.gatech.edu/linux/LDP/HOWTO/C-C++Beautifier-HOWTO.html">
<item> <url url="http://www.redhat.com/linux-info/ldp/HOWTO/C-C++Beautifier-HOWTO.html">
<item> Other mirror sites near you (network-address-wise) can be found at
<url url="http://metalab.unc.edu/LDP/hmirrors.html">
select a site and go to directory /LDP/HOWTO/C-C++Beautifier-HOWTO.html
</itemize>
In order to view the document in dvi format, use the xdvi program. The xdvi
program is located in tetex-xdvi*.rpm package in Redhat Linux which can be
located through ControlPanel | Applications | Publishing | TeX menu buttons.
<tscreen><verb>
To read dvi document give the command -
xdvi -geometry 80x90 howto.dvi
And resize the window with mouse. See man page on xdvi.
To navigate use Arrow keys, Page Up, Page Down keys, also
you can use 'f', 'd', 'u', 'c', 'l', 'r', 'p', 'n' letter
keys to move up, down, center, next page, previous page etc.
To turn off expert menu press 'x'.
</verb></tscreen>
You can read postscript file using the program 'gv' (ghostview) or
'ghostscript'.
The ghostscript program is in ghostscript*.rpm package and gv
program is in gv*.rpm package in Redhat Linux
which can be located through ControlPanel | Applications | Graphics menu
buttons. The gv program is much more user friendly than ghostscript.
Ghostscript and gv are also available on other platforms like OS/2,
Windows 95 and NT.
<tscreen><verb>
To read postscript document give the command -
gv howto.ps
To use ghostscript give -
ghostscript howto.ps
</verb></tscreen>
<itemize>
<item>Get ghostscript for Windows95, OS/2 and all OSes from <url url="http://www.cs.wisc.edu/~ghost">
</itemize>
You can read HTML format document using Netscape Navigator, Microsoft Internet
explorer, Redhat Baron Web browser or any other web browsers.
You can read the latex, LyX output using LyX a "X-Windows" front end to latex.
<!--
*******************************************
************ End of Section ***************
*******************************************
<chapt> Copyright
-->
<sect> Copyright
<p>
Copyright policy is GNU/GPL as per LDP (Linux Documentation project).
LDP is a GNU/GPL project.
Additional restrictions are - you must retain the author's name, email address
and this copyright notice on all the copies. If you make any changes
or additions to this document then you should
intimate all the authors of this document.
<!--
*******************************************
************ End of Section ***************
*******************************************
-->
</article>