1#!/bin/sh
2
3# ccformat - convert C code to standard format
4
5# @(#) ccformat.sh 1.3 11/5/89 14:39:29
6
7# how to supress newlines in echo
8
9case `echo -n` in
10"") n=-n; c=;;
11 *) n=; c='\c';;
12esac
13
14# initialize
15
16TMPF=/tmp/ccformat.$$
17ERROR=
18TROFF=
19BCK=
20FLAGS="-st -di8 -npsl -bap -bad -bbb -nbc -i4 -d0 -nip -nfc1 -cd41 -c49"
21
22trap 'rm -f .ind.$$ $TMPF; exit 1' 1 2 3 15
23
24# parse command options
25
26while :
27do
28	case $1 in
29	-t) TROFF=-troff;;
30	-b) case $# in
31	    1) ERROR="-b option requires backup argument"; break;;
32	    *) BCK=$2; shift;;
33	    esac;;
34	-T) case $# in
35	    1) ERROR="-T option requires typename argument"; break;;
36	    *) FLAGS="$FLAGS -T$2"; shift;;
37	    esac;;
38	-*) ERROR="invalid option: $1"; break;;
39	 *) break;;
40	esac
41	shift
42done
43
44# check for invalid commands
45
46test -z "$ERROR" || {
47	echo "$0: $ERROR" 1>&2
48	echo "usage: $0 [-b backup] [-t] [-T typename] [file(s)]" 1>&2
49	exit 1; }
50
51# format the files
52
53case $# in 
54 0) indent $TROFF $FLAGS;;
55 *) case "$TROFF" in
56-troff) for i in $*
57	do
58	    indent $TROFF $FLAGS $i
59	done;;
60     *) for i in $*
61	do 
62	    echo $n $i... $c 
63	    test -z "$BCK" || cp $i $i"$BCK" || { echo backup FAILED; exit 1; } 
64	    { # some versions of indent return garbage exit status -- gack!
65	    (indent $FLAGS <$i 2>.ind.$$ >$TMPF || test ! -s .ind.$$) >$TMPF &&
66	    # try a device full check
67	    echo >>$TMPF && (
68		# ignore interrupts while we overwrite the original file
69		trap '' 1 2 3 15; cp $TMPF $i 
70	    ) && echo replaced; } || { echo replacement FAILED; exit 1; }
71	done;;
72    esac;;
73esac
74
75rm -f $TMPF .ind.$$
76
77exit
78
79#++
80# NAME
81#	ccformat 1
82# SUMMARY
83#	convert C source text to standard format
84# PROJECT
85#	sdetools
86# SYNOPSIS
87#	ccformat [-b backup] [-t] [-T typename] [file(s)]
88# DESCRIPTION
89#	The \fIccformat\fR command adjusts the layout of C program text 
90#	such that it approximates the Kernighan and Ritchie coding style.
91#
92#	If no file names are specified, \fIccformat\fR reads 
93#	from standard input and writes the result to standard output. 
94#	This is convenient for source formatting from within a text
95#	editor program.
96#
97#	Otherwise, the named files are overwritten with their 
98#	formatted equivalent. The \fI-b\fR option (see below) provides 
99#	a way to create backup copies of the original files.
100#
101#	Alternatively, the command can be used as a preprocessor for
102#	pretty-printing with the \fInroff\fR or \fItroff\fR commands
103#	(see the -t option below). In this case, output is always written 
104#	to standard output and no change is made to source files.
105#
106#	The following options are recognized:
107# .TP
108# -b backup
109#	Requests that a copy of the original files be saved. The backup
110#	file name is constructed by appending the specified \fIbackup\fR 
111#	string to the original file name. 
112#	This option is ignored when the \fI-t\fR
113#	option is specifid.
114# .TP
115# -t
116#	Makes the program act as a preprocessor
117#	for pretty-printing with \fInroff\fR or \fItroff\fR.
118#	For example, in order to produce a pretty-printed
119#	version on the line printer, use
120#
121	ccformat -t file(s) | nroff -mindent | col | lp
122# .TP
123# -T typename
124#	Adds \fItypename\fR to the list of type keywords.
125#	Names accumulate: -T can be specified more
126#	than once. You need to specify all the
127#	typenames that appear in your program that
128#	are defined by typedefs - nothing will be
129#	harmed if you miss a few, but the program
130#	won't be formatted as nicely as it should.
131# PROGRAM LAYOUT
132# .fi
133# .ad
134#	The following program layout is produced:
135# .TP 
136# comments
137#	Comments starting in the first column are left untouched.
138#	These are often carefully laid out by the programmer.
139# .sp
140#	Comments that appear in-between statements are lined up with 
141#	the surrounding program text, and are adjusted to accomodate 
142#	as many words on a line as possible. 
143#	However, a blank line in the middle of a comment is respected.
144# .sp
145#	Trailing comments after declarations begin at column 41 
146#	(5 tab stops). 
147#	Trailing comments after executable statements start at 
148#	column 49 (6 tab stops). 
149# .TP
150# indentation
151#	Statements are indented by multiples of four columns. 
152#	There is only one statement per line. A control statement
153#	is always placed on a separate line.
154# .TP
155# braces
156#	If an opening brace is preceded by a control statement (\fCif,
157#	else, do, for\fR or \fCswitch\fR), it is placed on the same line 
158#	as the control statement.
159# .sp
160#	A closing brace is placed at the same level of indentation as the 
161#	program text that precedes the corresponding opening brace.
162#	If a closing brace is followed by a control statement (\fCelse\fR
163#	or \fCwhile\fR), that control statement is placed on the same line 
164#	as the closing brace.
165# .sp
166#	In practice, brace placement is as
167#	exemplified by the books on C by B.W. Kernighan and D.M. Ritchie.
168# .TP
169# blanks
170#	Blanks are placed around assignment and arithmetic operators.
171#	Commas in declarations or parameter lists are followed by one blank. 
172# .sp
173#	In the following cases a 
174#	blank line is inserted if it is not already present in the text:
175#	1) in front of a block comment, 2) between local declarations and 
176#	executable statements 3) after each function body. 
177# .TP
178# declarations
179#	In the output, each variable declaration appears on
180#	a separate line.
181# COMMANDS
182#	indent(1)
183# FILES
184#	/tmp/ccformat.*	intermediate files
185# SEE ALSO
186#	indent(1)
187# DIAGNOSTICS
188#	Indent may complain in case of syntax errors. These show
189#	up as comments in the resulting program text.
190# BUGS
191#	The programs seems to beave even when fed ANSI C or even C++
192#	code; this has not been tested thoroughly, however.
193#
194#	Will produce useless files when fed with anything that is
195#	not C program text. This does not imply a judgment about
196#	C programs in general.
197# AUTHOR(S)
198#	W.Z. Venema
199#	Eindhoven University of Technology
200#	Department of Mathematics and Computer Science
201#	Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
202# CREATION DATE
203#	Fri May  6 14:07:04 MET DST 1988
204# STATUS
205#	ccformat.sh 1.3 11/5/89 14:39:29 (draft)
206#--
207