xref: /csrg-svn/usr.bin/indent/indent.1 (revision 43083)
1*43083Scael.\" Copyright (c) 1980, 1990 The Regents of the University of California.
235500Sbostic.\" Copyright (c) 1985 Sun Microsystems, Inc.
335500Sbostic.\" Copyright (c) 1976 Board of Trustees of the University of Illinois.
435500Sbostic.\" All rights reserved.
519718Smckusick.\"
6*43083Scael.\" %sccs.include.redist.man%
719718Smckusick.\"
8*43083Scael.\"     @(#)indent.1	6.7 (Berkeley) 06/11/90
9*43083Scael.\"
10*43083Scael.Dd
11*43083Scael.Dt INDENT 1
12*43083Scael.Sh NAME
13*43083Scael.Nm indent
14*43083Scael.Nd indent and format C program source
15*43083Scael.Sh SYNOPSIS
16*43083Scael.Nm indent
17*43083Scael.Op Ar input-file Op Ar output-file
18*43083Scael.Cx \&[
19*43083Scael.Fl bad
20*43083Scael.Cx \&\ |\ \&
21*43083Scael.Fl nbad
22*43083Scael.Cx \&]
23*43083Scael.Cx
24*43083Scael.Cx \&[
25*43083Scael.Fl bap
26*43083Scael.Cx \&\ |\ \&
27*43083Scael.Fl nbap
28*43083Scael.Cx \&]
29*43083Scael.Cx
30*43083Scael.Cx \&[
31*43083Scael.Fl bbb
32*43083Scael.Cx \&\ |\ \&
33*43083Scael.Fl nbbb
34*43083Scael.Cx \&]
35*43083Scael.Cx
36*43083Scael.Cx \&[
37*43083Scael.Fl bc
38*43083Scael.Cx \&\ |\ \&
39*43083Scael.Fl nbc
40*43083Scael.Cx \&]
41*43083Scael.Cx
42*43083Scael.Op Fl bl
43*43083Scael.Op Fl br
44*43083Scael.Oo
45*43083Scael.Op Fl c Ar n
46*43083Scael.Op Fl cd Ar n
47*43083Scael.Oo
48*43083Scael.Cx \&[
49*43083Scael.Fl cdb
50*43083Scael.Cx \&\ |\ \&
51*43083Scael.Fl ncdb
52*43083Scael.Cx \&]
53*43083Scael.Cx
54*43083Scael.Cx \&[
55*43083Scael.Fl ce
56*43083Scael.Cx \&\ |\ \&
57*43083Scael.Fl nce
58*43083Scael.Cx \&]
59*43083Scael.Cx
60*43083Scael.Oo
61*43083Scael.Op Fl ci Ar n
62*43083Scael.Op Fl cli Ar n
63*43083Scael.Op Fl d Ar n
64*43083Scael.Op Fl di Ar n
65*43083Scael.Oo
66*43083Scael.Cx \&[
67*43083Scael.Fl fc1
68*43083Scael.Cx \&\ |\ \&
69*43083Scael.Fl nfc1
70*43083Scael.Cx \&]
71*43083Scael.Cx
72*43083Scael.Oo
73*43083Scael.Op Fl i Ar n
74*43083Scael.Oo
75*43083Scael.Cx \&[
76*43083Scael.Fl ip
77*43083Scael.Cx \&\ |\ \&
78*43083Scael.Fl nip
79*43083Scael.Cx \&]
80*43083Scael.Cx
81*43083Scael.Oo
82*43083Scael.Op Fl l Ar n
83*43083Scael.Op Fl lc Ar n
84*43083Scael.Oo
85*43083Scael.Cx \&[
86*43083Scael.Fl lp
87*43083Scael.Cx \&\ |\ \&
88*43083Scael.Fl nlp
89*43083Scael.Cx \&]
90*43083Scael.Cx
91*43083Scael.Op Fl npro
92*43083Scael.Cx \&[
93*43083Scael.Fl pcs
94*43083Scael.Cx \&\ |\ \&
95*43083Scael.Fl npcs
96*43083Scael.Cx \&]
97*43083Scael.Cx
98*43083Scael.Cx \&[
99*43083Scael.Fl psl
100*43083Scael.Cx \&\ |\ \&
101*43083Scael.Fl npsl
102*43083Scael.Cx \&]
103*43083Scael.Cx
104*43083Scael.Cx \&[
105*43083Scael.Fl sc
106*43083Scael.Cx \&\ |\ \&
107*43083Scael.Fl nsc
108*43083Scael.Cx \&]
109*43083Scael.Cx
110*43083Scael.Cx \&[
111*43083Scael.Fl sob
112*43083Scael.Cx \&\ |\ \&
113*43083Scael.Fl nsob
114*43083Scael.Cx \&]
115*43083Scael.Cx
116*43083Scael.Op Fl st
117*43083Scael.Op Fl troff
118*43083Scael.Cx \&[
119*43083Scael.Fl v
120*43083Scael.Cx \&\ |\ \&
121*43083Scael.Fl nv
122*43083Scael.Cx \&]
123*43083Scael.Cx
124*43083Scael.Sh DESCRIPTION
125*43083Scael.Nm Indent
126*43083Scaelis a
127*43083Scael.Ar C
128*43083Scaelprogram formatter.  It reformats the
129*43083Scael.Ar C
130*43083Scaelprogram in the
131*43083Scael.Ar input-file
132*43083Scaelaccording to the switches.  The switches which can be
13324459Smckusickspecified are described below. They may appear before or after the file
13424459Smckusicknames.
135*43083Scael.Pp
136*43083Scael.Sy NOTE  :
137*43083ScaelIf you only specify an
138*43083Scael.Ar input-file  ,
139*43083Scaelthe formatting is
14024459Smckusickdone `in-place', that is, the formatted file is written back into
141*43083Scael.Ar input-file
14224459Smckusickand a backup copy of
143*43083Scael.Ar input-file
14424459Smckusickis written in the current directory.  If
145*43083Scael.Ar input-file
146*43083Scaelis named
147*43083Scael.Sq Pa /blah/blah/file ,
148*43083Scaelthe backup file is named
149*43083Scael.Pa file.BAK .
150*43083Scael.Pp
15119718SmckusickIf
152*43083Scael.Ar output-file
15319718Smckusickis specified,
154*43083Scael.Nm indent
15519718Smckusickchecks to make sure it is different from
156*43083Scael.Ar input-file  .
157*43083Scael.Pp
15824459SmckusickThe options listed below control the formatting style imposed by
159*43083Scael.Nm indent  .
160*43083Scael.Tw Op
161*43083Scael.Tp Fl bad , nbad
16236973SbosticIf
163*43083Scael.Fl bad
16436973Sbosticis specified, a blank line is forced after every block of
165*43083Scaeldeclarations.  Default:
166*43083Scael.Fl nbad  .
167*43083Scael.Tp Fl bap , nbap
16835500SbosticIf
169*43083Scael.Fl bap
17035500Sbosticis specified, a blank line is forced after every procedure body.  Default:
171*43083Scael.Fl nbap .
172*43083Scael.Tp Fl bbb , nbbb
17324459SmckusickIf
174*43083Scael.Fl bbb
17524459Smckusickis specified, a blank line is forced before every block comment.  Default:
176*43083Scael.Fl nbbb .
177*43083Scael.Tp Fl bc , nbc
17819718SmckusickIf
179*43083Scael.Fl bc
180*43083Scaelis specified, then a newline is forced after each comma in a declaration.
181*43083Scael.Fl nbc
18224459Smckusickturns off this option.  The default is
183*43083Scael.Fl bc  .
184*43083Scael.Tp Fl br , bl
18519718SmckusickSpecifying
186*43083Scael.Fl bl
18724459Smckusicklines up compound statements like this:
18819718Smckusick.ne 4
189*43083Scael.Ds I
190*43083Scaelif (...)
191*43083Scael{
192*43083Scael  code
193*43083Scael}
194*43083Scael.De
19519718SmckusickSpecifying
196*43083Scael.Fl br
19724459Smckusick(the default) makes them look like this:
19819718Smckusick.ne 3
199*43083Scael.Ds I
200*43083Scaelif (...) {
201*43083Scael  code
202*43083Scael}
203*43083Scael.De
204*43083Scael.Pp
205*43083Scael.Tp Fl c n
206*43083ScaelThe column in which comments on code start.  The default is 33.
207*43083Scael.Tp Fl cd n
20824459SmckusickThe column in which comments on declarations start.  The default
20924459Smckusickis for these comments to start in the same column as those on code.
210*43083Scael.Tp Fl cdb , ncdb
21124459SmckusickEnables (disables) the placement of comment delimiters on blank lines.  With
21224459Smckusickthis option enabled, comments look like this:
213*43083Scael.Ds I
21424459Smckusick.ne 3
21524459Smckusick	/*
216*43083Scael	* this is a comment
217*43083Scael	*/
218*43083Scael.De
21924459SmckusickRather than like this:
220*43083Scael.Ds I
22124459Smckusick	/* this is a comment */
222*43083Scael.De
22336973SbosticThis only affects block comments, not comments to the right of
22436973Sbosticcode.  The default is
225*43083Scael.Fl cdb  .
226*43083Scael.Tp Fl ce , nce
22736973SbosticEnables (disables) forcing `else's to cuddle up to the immediately preceding
22824459Smckusick`}'.  The default is
229*43083Scael.Fl ce  .
230*43083Scael.Tp Cx Fl ci
231*43083Scael.Ar n
232*43083Scael.Cx
233*43083ScaelSets the continuation indent to be
234*43083Scael.Ar n  .
235*43083ScaelContinuation
23624459Smckusicklines will be indented that far from the beginning of the first line of the
23724459Smckusickstatement.  Parenthesized expressions have extra indentation added to
238*43083Scaelindicate the nesting, unless
239*43083Scael.Fl lp
240*43083Scaelis in effect.
241*43083Scael.Fl ci
242*43083Scaeldefaults to the same value as
243*43083Scael.Fl i  .
244*43083Scael.Tp Cx Fl cli
245*43083Scael.Ar n
246*43083Scael.Cx
24724459SmckusickCauses case labels to be indented
248*43083Scael.Ar n
249*43083Scaeltab stops to the right of the containing
250*43083Scael.Ic switch
251*43083Scaelstatement.
252*43083Scael.Fl cli0 .5
253*43083Scaelcauses case labels to be indented half a tab stop.  The
25424459Smckusickdefault is
255*43083Scael.Fl cli0  .
256*43083Scael.Tp Cx Fl d
257*43083Scael.Ar n
258*43083Scael.Cx
25924459SmckusickControls the placement of comments which are not to the
26035500Sbosticright of code.  The default
261*43083Scael.Fl d1
26224459Smckusickmeans that such comments are placed one indentation level to the
26335500Sbosticleft of code.  Specifying
264*43083Scael.Fl d0
26524459Smckusicklines up these comments with the code.  See the section on comment
26624459Smckusickindentation below.
267*43083Scael.Tp Cx Fl di
268*43083Scael.Ar n
269*43083Scael.Cx
27024459SmckusickSpecifies the indentation, in character positions, from a declaration keyword
27124459Smckusickto the following identifier.  The default is
272*43083Scael.Fl di16  .
273*43083Scael.Tp Fl dj , ndj
274*43083Scael.Fl dj
27524459Smckusickleft justifies declarations.
276*43083Scael.Fl ndj
27724459Smckusickindents declarations the same as code.  The default is
278*43083Scael.Fl ndj  .
279*43083Scael.Tp Fl ei , nei
28024459SmckusickEnables (disables) special
281*43083Scael.Ic else-if
282*43083Scaelprocessing.  If it's enabled, an
283*43083Scael.Ic if
284*43083Scaelfollowing an
285*43083Scael.Ic else
28636973Sbosticwill have the same indentation as the preceding
287*43083Scael.Ic if
288*43083Scaelstatement.
289*43083Scael.Tp Fl fc1 , nfc1
29024459SmckusickEnables (disables) the formatting of comments that start in column 1.
29124459SmckusickOften, comments whose leading `/' is in column 1 have been carefully
292*43083Scaelhand formatted by the programmer.  In such cases,
293*43083Scael.Fl nfc1
294*43083Scaelshould be
295*43083Scaelused.  The default is
296*43083Scael.Fl fc1  .
297*43083Scael.Tp Cx Fl i
298*43083Scael.Ar n
299*43083Scael.Cx
30035500SbosticThe number of spaces for one indentation level.  The default is 4.
301*43083Scael.Tp Fl ip , nip
30224459SmckusickEnables (disables) the indentation of parameter declarations from the left
30324459Smckusickmargin.  The default is
304*43083Scael.Fl ip  .
305*43083Scael.Tp Cx Fl l
306*43083Scael.Ar n
307*43083Scael.Cx
30835500SbosticMaximum length of an output line.  The default is 75.
309*43083Scael.Tp Fl lp , nlp
31024459SmckusickLines up code surrounded by parenthesis in continuation lines.  If a line
31124459Smckusickhas a left paren which is not closed on that line, then continuation lines
31224459Smckusickwill be lined up to start at the character position just after the left
31336973Sbosticparen.  For example, here is how a piece of continued code looks with
314*43083Scael.Fl nlp
315*43083Scaelin effect:
31624459Smckusick.ne 2
317*43083Scael.Ds I
318*43083Scael.Li p1 = first_procedure(second_procedure(p2, p3),
319*43083Scael.Li \ \ third_procedure(p4,p5));
320*43083Scael.De
32124459Smckusick.ne 5
322*43083ScaelWith
323*43083Scael.Fl lp
324*43083Scaelin effect (the default) the code looks somewhat clearer:
325*43083Scael.Ds I
326*43083Scael.Li p1\ =\ first_procedure(second_procedure(p2,\ p3),
327*43083Scael.Li \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ third_procedure(p4,p5));
328*43083Scael.De
32924459Smckusick.ne 5
33036973SbosticInserting two more newlines we get:
331*43083Scael.Ds I
332*43083Scael.Li p1\ =\ first_procedure(second_procedure(p2,
333*43083Scael.Li \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ p3),
334*43083Scael.Li \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ third_procedure(p4
335*43083Scael.Li \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ p5));
336*43083Scael.De
337*43083Scael.Pp
338*43083Scael.Tp Fl npro
339*43083ScaelCauses the profile files,
340*43083Scael.Sq Pa ./.indent.pro
341*43083Scaeland
342*43083Scael.Sq Pa ~/.indent.pro ,
343*43083Scaelto be ignored.
344*43083Scael.Tp Fl pcs , npcs
345*43083ScaelIf true
346*43083Scael.Pq Fl pcs
347*43083Scaelall procedure calls will have a space inserted between
348*43083Scaelthe name and the `('.  The default is
349*43083Scael.Fl npcs  .
350*43083Scael.Tp Fl psl , npsl
351*43083ScaelIf true
352*43083Scael.Pq Fl psl
353*43083Scaelthe names of procedures being defined are placed in
35424459Smckusickcolumn 1 \- their types, if any, will be left on the previous lines.  The
355*43083Scaeldefault is
356*43083Scael.Fl psl  .
357*43083Scael.Tp Fl sc , nsc
35824459SmckusickEnables (disables) the placement of asterisks (`*'s) at the left edge of all
35935500Sbosticcomments.
360*43083Scael.Tp Fl sob , nsob
36119718SmckusickIf
362*43083Scael.Fl sob
36324459Smckusickis specified, indent will swallow optional blank lines.  You can use this to
36424459Smckusickget rid of blank lines after declarations.  Default:
365*43083Scael.Fl nsob  .
366*43083Scael.Tp Fl st
36724459SmckusickCauses
368*43083Scael.Nm indent
36924459Smckusickto take its input from stdin, and put its output to stdout.
370*43083Scael.Tp Cx Fl T
371*43083Scael.Ar typename
372*43083Scael.Cx
37324459SmckusickAdds
374*43083Scael.Ar typename
37524459Smckusickto the list of type keywords.  Names accumulate:
376*43083Scael.Fl T
37724459Smckusickcan be specified more than once.  You need to specify all the typenames that
378*43083Scaelappear in your program that are defined by
379*43083Scael.Ic typedef
380*43083Scael\- nothing will be
38124459Smckusickharmed if you miss a few, but the program won't be formatted as nicely as
38224459Smckusickit should.  This sounds like a painful thing to have to do, but it's really
383*43083Scaela symptom of a problem in C:
384*43083Scael.Ic typedef
385*43083Scaelcauses a syntactic change in the
386*43083Scaellanguage and
387*43083Scael.Nm indent
388*43083Scaelcan't find all
389*43083Scaelinstances of
390*43083Scael.Ic typedef .
391*43083Scael.Tp Fl troff
39224459SmckusickCauses
393*43083Scael.Nm indent
39424459Smckusickto format the program for processing by troff.  It will produce a fancy
39524459Smckusicklisting in much the same spirit as
396*43083Scael.Xr vgrind 1 .
39724459SmckusickIf the output file is not specified, the default is standard output,
39824459Smckusickrather than formatting in place.
399*43083Scael.Tp Fl v , nv
400*43083Scael.Fl v
40136973Sbosticturns on `verbose' mode;
402*43083Scael.Fl nv
40324459Smckusickturns it off.  When in verbose mode,
404*43083Scael.Nm indent
40524459Smckusickreports when it splits one line of input into two or more lines of output,
40624459Smckusickand gives some size statistics at completion. The default is
407*43083Scael.Fl nv  .
408*43083Scael.Tp
409*43083Scael.Pp
41024459SmckusickYou may set up your own `profile' of defaults to
411*43083Scael.Nm indent
41224459Smckusickby creating a file called
413*43083Scael.Pa .indent.pro
41436973Sbosticin your login directory and/or the current directory and including
41535500Sbosticwhatever switches you like.  A `.indent.pro' in the current directory takes
41635500Sbosticprecedence over the one in your login directory.  If
417*43083Scael.Nm indent
41824459Smckusickis run and a profile file exists, then it is read to set up the program's
41935500Sbosticdefaults.  Switches on the command line, though, always override profile
42035500Sbosticswitches.  The switches should be separated by spaces, tabs or newlines.
421*43083Scael.Pp
422*43083Scael.Ss Comments
423*43083Scael.Sq Em Box
424*43083Scael.Em comments .
425*43083Scael.Nm Indent
42624459Smckusickassumes that any comment with a dash or star immediately after the start of
42724459Smckusickcomment (that is, `/*\-' or `/**') is a comment surrounded by a box of stars.
42824459SmckusickEach line of such a comment is left unchanged, except that its indentation
42924459Smckusickmay be adjusted to account for the change in indentation of the first line
43024459Smckusickof the comment.
431*43083Scael.Pp
432*43083Scael.Em Straight text .
43324459SmckusickAll other comments are treated as straight text.
434*43083Scael.Nm Indent
43524459Smckusickfits as many words (separated by blanks, tabs, or newlines) on a
43624459Smckusickline as possible.  Blank lines break paragraphs.
437*43083Scael.Pp
438*43083Scael.Ss Comment indentation
43924459SmckusickIf a comment is on a line with code it is started in the `comment column',
44024459Smckusickwhich is set by the
441*43083Scael.Cx Fl c
442*43083Scael.Ar n
443*43083Scael.Cx
44424459Smckusickcommand line parameter.  Otherwise, the comment is started at
445*43083Scael.Ar n
44624459Smckusickindentation levels less than where code is currently being placed, where
447*43083Scael.Ar n
44819718Smckusickis specified by the
449*43083Scael.Cx Fl d
450*43083Scael.Ar n
451*43083Scael.Cx
45224459Smckusickcommand line parameter.  If the code on a line extends past the comment
45324459Smckusickcolumn, the comment starts further to the right, and the right margin may be
45424459Smckusickautomatically extended in extreme cases.
455*43083Scael.Pp
456*43083Scael.Ss Preprocessor lines
457*43083ScaelIn general,
458*43083Scael.Nm indent
459*43083Scaelleaves preprocessor lines alone.  The only
46036973Sbosticreformatting that it will do is to straighten up trailing comments.  It
46136973Sbosticleaves embedded comments alone.  Conditional compilation
462*43083Scael.Pq Ic #ifdef...#endif
463*43083Scaelis recognized and
464*43083Scael.Nm indent
465*43083Scaelattempts to correctly
46636973Sbosticcompensate for the syntactic peculiarities introduced.
467*43083Scael.Pp
468*43083Scael.Ss C syntax
469*43083Scael.Nm Indent
470*43083Scaelunderstands a substantial amount about the syntax of C, but it
47124459Smckusickhas a `forgiving' parser.  It attempts to cope with the usual sorts of
47224459Smckusickincomplete and misformed syntax.  In particular, the use of macros like:
473*43083Scael.Dl #define forever for(;;)
47424459Smckusickis handled properly.
475*43083Scael.Sh ENVIRONMENT
476*43083Scael.Nm Indent
477*43083Scaeluses the
478*43083Scael.Ev HOME
479*43083Scaelenvironment variable.
480*43083Scael.Sh FILES
481*43083Scael.Dw \&./.indent.pro
482*43083Scael.Di L
483*43083Scael.Dp \&./.indent.pro
484*43083Scaelprofile file
485*43083Scael.Dp ~/.indent.pro
486*43083Scaelprofile file
487*43083Scael.Dp
488*43083Scael.Sh HISTORY
489*43083Scael.Nm Indent
490*43083Scaelappeared in 4.2 BSD.
491*43083Scael.Sh BUGS
492*43083Scael.Nm Indent
493*43083Scaelhas even more switches than
494*43083Scael.Xr ls 1 .
495*43083Scael.Pp
49624459Smckusick.ne 5
49724459SmckusickA common mistake that often causes grief is typing:
498*43083Scael.Dl indent *.c
499*43083Scaelto the shell in an attempt to indent all the
500*43083Scael.Nm C
501*43083Scaelprograms in a directory.
50224459SmckusickThis is probably a bug, not a feature.
503