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