143083Scael.\" 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*50518Scael.\" %sccs.include.redist.roff% 719718Smckusick.\" 8*50518Scael.\" @(#)indent.1 6.10 (Berkeley) 07/24/91 943083Scael.\" 1043083Scael.Dd 1143083Scael.Dt INDENT 1 1243083Scael.Sh NAME 1343083Scael.Nm indent 1443083Scael.Nd indent and format C program source 1543083Scael.Sh SYNOPSIS 1643083Scael.Nm indent 1743083Scael.Op Ar input-file Op Ar output-file 18*50518Scael.Op Fl bad | Fl nbad 19*50518Scael.Op Fl bap | Fl nbap 20*50518Scael.Bk -words 21*50518Scael.Op Fl bbb | Fl nbbb 22*50518Scael.Ek 23*50518Scael.Op Fl \&bc | Fl nbc 24*50518Scael.Op Fl \&bl 25*50518Scael.Op Fl \&br 26*50518Scael.Op Fl c Ns Ar n 27*50518Scael.Op Fl \&cd Ns Ar n 28*50518Scael.Bk -words 29*50518Scael.Op Fl cdb | Fl ncdb 30*50518Scael.Ek 31*50518Scael.Op Fl \&ce | Fl nce 32*50518Scael.Op Fl \&ci Ns Ar n 33*50518Scael.Op Fl cli Ns Ar n 34*50518Scael.Op Fl d Ns Ar n 35*50518Scael.Op Fl \&di Ns Ar n 36*50518Scael.Bk -words 37*50518Scael.Op Fl fc1 | Fl nfc1 38*50518Scael.Ek 39*50518Scael.Op Fl i Ns Ar n 40*50518Scael.Op Fl \&ip | Fl nip 41*50518Scael.Op Fl l Ns Ar n 42*50518Scael.Op Fl \&lc Ns Ar n 43*50518Scael.Op Fl \&lp | Fl nlp 4443083Scael.Op Fl npro 45*50518Scael.Op Fl pcs | Fl npcs 46*50518Scael.Op Fl psl | Fl npsl 47*50518Scael.Op Fl \&sc | Fl nsc 48*50518Scael.Bk -words 49*50518Scael.Op Fl sob | Fl nsob 50*50518Scael.Ek 51*50518Scael.Op Fl \&st 5243083Scael.Op Fl troff 53*50518Scael.Op Fl v | Fl \&nv 5443083Scael.Sh DESCRIPTION 5543083Scael.Nm Indent 5643083Scaelis a 5743083Scael.Ar C 5843083Scaelprogram formatter. It reformats the 5943083Scael.Ar C 6043083Scaelprogram in the 6143083Scael.Ar input-file 6243083Scaelaccording to the switches. The switches which can be 6324459Smckusickspecified are described below. They may appear before or after the file 6424459Smckusicknames. 6543083Scael.Pp 6643083Scael.Sy NOTE : 6743083ScaelIf you only specify an 6843083Scael.Ar input-file , 6943083Scaelthe formatting is 7024459Smckusickdone `in-place', that is, the formatted file is written back into 7143083Scael.Ar input-file 7224459Smckusickand a backup copy of 7343083Scael.Ar input-file 7424459Smckusickis written in the current directory. If 7543083Scael.Ar input-file 7643083Scaelis named 7743083Scael.Sq Pa /blah/blah/file , 7843083Scaelthe backup file is named 7943083Scael.Pa file.BAK . 8043083Scael.Pp 8119718SmckusickIf 8243083Scael.Ar output-file 8319718Smckusickis specified, 8443083Scael.Nm indent 8519718Smckusickchecks to make sure it is different from 8643083Scael.Ar input-file . 8743083Scael.Pp 8824459SmckusickThe options listed below control the formatting style imposed by 8943083Scael.Nm indent . 90*50518Scael.Bl -tag -width Op 91*50518Scael.It Fl bad , nbad 9236973SbosticIf 9343083Scael.Fl bad 9436973Sbosticis specified, a blank line is forced after every block of 9543083Scaeldeclarations. Default: 9643083Scael.Fl nbad . 97*50518Scael.It Fl bap , nbap 9835500SbosticIf 9943083Scael.Fl bap 10035500Sbosticis specified, a blank line is forced after every procedure body. Default: 10143083Scael.Fl nbap . 102*50518Scael.It Fl bbb , nbbb 10324459SmckusickIf 10443083Scael.Fl bbb 10524459Smckusickis specified, a blank line is forced before every block comment. Default: 10643083Scael.Fl nbbb . 107*50518Scael.It Fl \&bc , nbc 10819718SmckusickIf 109*50518Scael.Fl \&bc 11043083Scaelis specified, then a newline is forced after each comma in a declaration. 11143083Scael.Fl nbc 11224459Smckusickturns off this option. The default is 113*50518Scael.Fl \&bc . 114*50518Scael.It Fl \&br , \&bl 11519718SmckusickSpecifying 116*50518Scael.Fl \&bl 11724459Smckusicklines up compound statements like this: 11819718Smckusick.ne 4 119*50518Scael.Bd -literal -offset indent 12043083Scaelif (...) 12143083Scael{ 12243083Scael code 12343083Scael} 124*50518Scael.Ed 125*50518Scael.Pp 12619718SmckusickSpecifying 127*50518Scael.Fl \&br 12824459Smckusick(the default) makes them look like this: 12919718Smckusick.ne 3 130*50518Scael.Bd -literal -offset indent 13143083Scaelif (...) { 13243083Scael code 13343083Scael} 134*50518Scael.Ed 13543083Scael.Pp 136*50518Scael.It Fl c n 13743083ScaelThe column in which comments on code start. The default is 33. 138*50518Scael.It Fl cd n 13924459SmckusickThe column in which comments on declarations start. The default 14024459Smckusickis for these comments to start in the same column as those on code. 141*50518Scael.It Fl cdb , ncdb 14224459SmckusickEnables (disables) the placement of comment delimiters on blank lines. With 14324459Smckusickthis option enabled, comments look like this: 144*50518Scael.Bd -literal -offset indent 14524459Smckusick.ne 3 14624459Smckusick /* 14743083Scael * this is a comment 14843083Scael */ 149*50518Scael.Ed 150*50518Scael.Pp 15124459SmckusickRather than like this: 152*50518Scael.Bd -literal -offset indent 15324459Smckusick /* this is a comment */ 154*50518Scael.Ed 155*50518Scael.Pp 15636973SbosticThis only affects block comments, not comments to the right of 15736973Sbosticcode. The default is 15843083Scael.Fl cdb . 159*50518Scael.It Fl ce , nce 16036973SbosticEnables (disables) forcing `else's to cuddle up to the immediately preceding 16124459Smckusick`}'. The default is 162*50518Scael.Fl \&ce . 163*50518Scael.It Fl \&ci Ns Ar n 16443083ScaelSets the continuation indent to be 16543083Scael.Ar n . 16643083ScaelContinuation 16724459Smckusicklines will be indented that far from the beginning of the first line of the 16824459Smckusickstatement. Parenthesized expressions have extra indentation added to 16943083Scaelindicate the nesting, unless 170*50518Scael.Fl \&lp 17143083Scaelis in effect. 172*50518Scael.Fl \&ci 17343083Scaeldefaults to the same value as 17443083Scael.Fl i . 175*50518Scael.It Fl cli Ns Ar n 17624459SmckusickCauses case labels to be indented 17743083Scael.Ar n 17843083Scaeltab stops to the right of the containing 17943083Scael.Ic switch 18043083Scaelstatement. 18143083Scael.Fl cli0 .5 18243083Scaelcauses case labels to be indented half a tab stop. The 18324459Smckusickdefault is 18443083Scael.Fl cli0 . 185*50518Scael.It Fl d Ns Ar n 18624459SmckusickControls the placement of comments which are not to the 18735500Sbosticright of code. The default 188*50518Scael.Fl \&d\&1 18924459Smckusickmeans that such comments are placed one indentation level to the 19035500Sbosticleft of code. Specifying 191*50518Scael.Fl \&d\&0 19224459Smckusicklines up these comments with the code. See the section on comment 19324459Smckusickindentation below. 194*50518Scael.It Fl \&di Ns Ar n 19524459SmckusickSpecifies the indentation, in character positions, from a declaration keyword 19624459Smckusickto the following identifier. The default is 19743083Scael.Fl di16 . 198*50518Scael.It Fl dj , ndj 199*50518Scael.Fl \&dj 20024459Smckusickleft justifies declarations. 20143083Scael.Fl ndj 20224459Smckusickindents declarations the same as code. The default is 20343083Scael.Fl ndj . 204*50518Scael.It Fl \&ei , nei 20524459SmckusickEnables (disables) special 20643083Scael.Ic else-if 20743083Scaelprocessing. If it's enabled, an 20843083Scael.Ic if 20943083Scaelfollowing an 21043083Scael.Ic else 21136973Sbosticwill have the same indentation as the preceding 212*50518Scael.Ic \&if 21343083Scaelstatement. 214*50518Scael.It Fl fc1 , nfc1 21524459SmckusickEnables (disables) the formatting of comments that start in column 1. 21624459SmckusickOften, comments whose leading `/' is in column 1 have been carefully 21743083Scaelhand formatted by the programmer. In such cases, 21843083Scael.Fl nfc1 21943083Scaelshould be 22043083Scaelused. The default is 22143083Scael.Fl fc1 . 222*50518Scael.It Fl i Ns Ar n 22335500SbosticThe number of spaces for one indentation level. The default is 4. 224*50518Scael.It Fl \&ip , nip 22524459SmckusickEnables (disables) the indentation of parameter declarations from the left 22624459Smckusickmargin. The default is 227*50518Scael.Fl \&ip . 228*50518Scael.It Fl l Ns Ar n 22935500SbosticMaximum length of an output line. The default is 75. 230*50518Scael.It Fl \&lp , nlp 23124459SmckusickLines up code surrounded by parenthesis in continuation lines. If a line 23224459Smckusickhas a left paren which is not closed on that line, then continuation lines 23324459Smckusickwill be lined up to start at the character position just after the left 23436973Sbosticparen. For example, here is how a piece of continued code looks with 23543083Scael.Fl nlp 23643083Scaelin effect: 23724459Smckusick.ne 2 238*50518Scael.Bd -literal -offset indent 23943083Scael.Li p1 = first_procedure(second_procedure(p2, p3), 24043083Scael.Li \ \ third_procedure(p4,p5)); 241*50518Scael.Ed 242*50518Scael.Pp 24324459Smckusick.ne 5 24443083ScaelWith 24543083Scael.Fl lp 24643083Scaelin effect (the default) the code looks somewhat clearer: 247*50518Scael.Bd -literal -offset indent 24843083Scael.Li p1\ =\ first_procedure(second_procedure(p2,\ p3), 24943083Scael.Li \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ third_procedure(p4,p5)); 250*50518Scael.Ed 251*50518Scael.Pp 25224459Smckusick.ne 5 25336973SbosticInserting two more newlines we get: 254*50518Scael.Bd -literal -offset indent 25543083Scael.Li p1\ =\ first_procedure(second_procedure(p2, 25643083Scael.Li \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ p3), 25743083Scael.Li \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ third_procedure(p4 25843083Scael.Li \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ p5)); 259*50518Scael.Ed 260*50518Scael.It Fl npro 26143083ScaelCauses the profile files, 26243083Scael.Sq Pa ./.indent.pro 26343083Scaeland 26443083Scael.Sq Pa ~/.indent.pro , 26543083Scaelto be ignored. 266*50518Scael.It Fl pcs , npcs 26743083ScaelIf true 26843083Scael.Pq Fl pcs 26943083Scaelall procedure calls will have a space inserted between 27043083Scaelthe name and the `('. The default is 27143083Scael.Fl npcs . 272*50518Scael.It Fl psl , npsl 27343083ScaelIf true 27443083Scael.Pq Fl psl 27543083Scaelthe names of procedures being defined are placed in 27624459Smckusickcolumn 1 \- their types, if any, will be left on the previous lines. The 27743083Scaeldefault is 27843083Scael.Fl psl . 279*50518Scael.It Fl \&sc , nsc 28024459SmckusickEnables (disables) the placement of asterisks (`*'s) at the left edge of all 28135500Sbosticcomments. 282*50518Scael.It Fl sob , nsob 28319718SmckusickIf 28443083Scael.Fl sob 28524459Smckusickis specified, indent will swallow optional blank lines. You can use this to 28624459Smckusickget rid of blank lines after declarations. Default: 28743083Scael.Fl nsob . 288*50518Scael.It Fl \&st 28924459SmckusickCauses 29043083Scael.Nm indent 29124459Smckusickto take its input from stdin, and put its output to stdout. 292*50518Scael.It Fl T Ns Ar typename 29324459SmckusickAdds 29443083Scael.Ar typename 29524459Smckusickto the list of type keywords. Names accumulate: 29643083Scael.Fl T 29724459Smckusickcan be specified more than once. You need to specify all the typenames that 29843083Scaelappear in your program that are defined by 29943083Scael.Ic typedef 30043083Scael\- nothing will be 30124459Smckusickharmed if you miss a few, but the program won't be formatted as nicely as 30224459Smckusickit should. This sounds like a painful thing to have to do, but it's really 30343083Scaela symptom of a problem in C: 30443083Scael.Ic typedef 30543083Scaelcauses a syntactic change in the 30643083Scaellanguage and 30743083Scael.Nm indent 30843083Scaelcan't find all 30943083Scaelinstances of 31043083Scael.Ic typedef . 311*50518Scael.It Fl troff 31224459SmckusickCauses 31343083Scael.Nm indent 314*50518Scaelto format the program for processing by 315*50518Scael.Xr troff 1 . 316*50518ScaelIt will produce a fancy 31724459Smckusicklisting in much the same spirit as 31843083Scael.Xr vgrind 1 . 31924459SmckusickIf the output file is not specified, the default is standard output, 32024459Smckusickrather than formatting in place. 321*50518Scael.It Fl v , \&nv 32243083Scael.Fl v 32336973Sbosticturns on `verbose' mode; 324*50518Scael.Fl \&nv 32524459Smckusickturns it off. When in verbose mode, 32643083Scael.Nm indent 32724459Smckusickreports when it splits one line of input into two or more lines of output, 32824459Smckusickand gives some size statistics at completion. The default is 329*50518Scael.Fl \&nv . 330*50518Scael.El 33143083Scael.Pp 33224459SmckusickYou may set up your own `profile' of defaults to 33343083Scael.Nm indent 33424459Smckusickby creating a file called 33543083Scael.Pa .indent.pro 33636973Sbosticin your login directory and/or the current directory and including 33735500Sbosticwhatever switches you like. A `.indent.pro' in the current directory takes 33835500Sbosticprecedence over the one in your login directory. If 33943083Scael.Nm indent 34024459Smckusickis run and a profile file exists, then it is read to set up the program's 34135500Sbosticdefaults. Switches on the command line, though, always override profile 34235500Sbosticswitches. The switches should be separated by spaces, tabs or newlines. 34343083Scael.Pp 34443083Scael.Ss Comments 34543083Scael.Sq Em Box 34643083Scael.Em comments . 34743083Scael.Nm Indent 34824459Smckusickassumes that any comment with a dash or star immediately after the start of 34924459Smckusickcomment (that is, `/*\-' or `/**') is a comment surrounded by a box of stars. 35024459SmckusickEach line of such a comment is left unchanged, except that its indentation 35124459Smckusickmay be adjusted to account for the change in indentation of the first line 35224459Smckusickof the comment. 35343083Scael.Pp 35443083Scael.Em Straight text . 35524459SmckusickAll other comments are treated as straight text. 35643083Scael.Nm Indent 35724459Smckusickfits as many words (separated by blanks, tabs, or newlines) on a 35824459Smckusickline as possible. Blank lines break paragraphs. 35943083Scael.Pp 36043083Scael.Ss Comment indentation 36124459SmckusickIf a comment is on a line with code it is started in the `comment column', 36224459Smckusickwhich is set by the 363*50518Scael.Fl c Ns Ns Ar n 36424459Smckusickcommand line parameter. Otherwise, the comment is started at 36543083Scael.Ar n 36624459Smckusickindentation levels less than where code is currently being placed, where 36743083Scael.Ar n 36819718Smckusickis specified by the 369*50518Scael.Fl d Ns Ns Ar n 37024459Smckusickcommand line parameter. If the code on a line extends past the comment 37124459Smckusickcolumn, the comment starts further to the right, and the right margin may be 37224459Smckusickautomatically extended in extreme cases. 37343083Scael.Pp 37443083Scael.Ss Preprocessor lines 37543083ScaelIn general, 37643083Scael.Nm indent 37743083Scaelleaves preprocessor lines alone. The only 37836973Sbosticreformatting that it will do is to straighten up trailing comments. It 37936973Sbosticleaves embedded comments alone. Conditional compilation 38043083Scael.Pq Ic #ifdef...#endif 38143083Scaelis recognized and 38243083Scael.Nm indent 38343083Scaelattempts to correctly 38436973Sbosticcompensate for the syntactic peculiarities introduced. 38543083Scael.Pp 38643083Scael.Ss C syntax 38743083Scael.Nm Indent 38843083Scaelunderstands a substantial amount about the syntax of C, but it 38924459Smckusickhas a `forgiving' parser. It attempts to cope with the usual sorts of 39024459Smckusickincomplete and misformed syntax. In particular, the use of macros like: 391*50518Scael.Pp 39243083Scael.Dl #define forever for(;;) 393*50518Scael.Pp 39424459Smckusickis handled properly. 39543083Scael.Sh ENVIRONMENT 39643083Scael.Nm Indent 39743083Scaeluses the 39843083Scael.Ev HOME 39943083Scaelenvironment variable. 40043083Scael.Sh FILES 401*50518Scael.Bl -tag -width "./.indent.pro" -compact 402*50518Scael.It Pa ./.indent.pro 40343083Scaelprofile file 404*50518Scael.It Pa ~/.indent.pro 40543083Scaelprofile file 406*50518Scael.El 40743083Scael.Sh HISTORY 408*50518ScaelThe 409*50518Scael.Nm indent 410*50518Scaelcommand appeared in 411*50518Scael.Bx 4.2 . 41243083Scael.Sh BUGS 41343083Scael.Nm Indent 41443083Scaelhas even more switches than 41543083Scael.Xr ls 1 . 41643083Scael.Pp 41724459Smckusick.ne 5 41824459SmckusickA common mistake that often causes grief is typing: 419*50518Scael.Pp 42043083Scael.Dl indent *.c 421*50518Scael.Pp 42243083Scaelto the shell in an attempt to indent all the 42343083Scael.Nm C 42443083Scaelprograms in a directory. 42524459SmckusickThis is probably a bug, not a feature. 426