1.\" Copyright (c) 1980, 1990, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" Copyright (c) 1976 Board of Trustees of the University of Illinois. 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. All advertising materials mentioning features or use of this software 15.\" must display the following acknowledgement: 16.\" This product includes software developed by the University of 17.\" California, Berkeley and its contributors. 18.\" 4. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.\" @(#)indent.1 8.1 (Berkeley) 7/1/93 35.\" $FreeBSD$ 36.\" 37.Dd June 29, 2004 38.Dt INDENT 1 39.Os 40.Sh NAME 41.Nm indent 42.Nd indent and format C program source 43.Sh SYNOPSIS 44.Nm 45.Op Ar input-file Op Ar output-file 46.Op Fl bad | Fl nbad 47.Op Fl bap | Fl nbap 48.Bk -words 49.Op Fl bbb | Fl nbbb 50.Ek 51.Op Fl \&bc | Fl nbc 52.Op Fl \&bl 53.Op Fl \&br 54.Op Fl c Ns Ar n 55.Op Fl \&cd Ns Ar n 56.Bk -words 57.Op Fl cdb | Fl ncdb 58.Ek 59.Op Fl \&ce | Fl nce 60.Op Fl \&ci Ns Ar n 61.Op Fl cli Ns Ar n 62.Op Fl d Ns Ar n 63.Op Fl \&di Ns Ar n 64.Bk -words 65.Op Fl fbs | Fl nfbs 66.Op Fl fc1 | Fl nfc1 67.Op Fl fcb | Fl nfcb 68.Ek 69.Op Fl i Ns Ar n 70.Op Fl \&ip | Fl nip 71.Op Fl l Ns Ar n 72.Op Fl \&lc Ns Ar n 73.Op Fl \&ldi Ns Ar n 74.Op Fl \&lp | Fl nlp 75.Op Fl npro 76.Op Fl pcs | Fl npcs 77.Op Fl psl | Fl npsl 78.Op Fl \&sc | Fl nsc 79.Bk -words 80.Op Fl sob | Fl nsob 81.Ek 82.Op Fl \&st 83.Op Fl troff 84.Op Fl ut | Fl nut 85.Op Fl v | Fl \&nv 86.Sh DESCRIPTION 87The 88.Nm 89utility is a 90.Em C 91program formatter. 92It reformats the 93.Em C 94program in the 95.Ar input-file 96according to the switches. 97The switches which can be 98specified are described below. 99They may appear before or after the file 100names. 101.Pp 102.Sy NOTE : 103If you only specify an 104.Ar input-file , 105the formatting is 106done `in-place', that is, the formatted file is written back into 107.Ar input-file 108and a backup copy of 109.Ar input-file 110is written in the current directory. 111If 112.Ar input-file 113is named 114.Sq Pa /blah/blah/file , 115the backup file is named 116.Sq Pa file.BAK . 117.Pp 118If 119.Ar output-file 120is specified, 121.Nm 122checks to make sure that it is different from 123.Ar input-file . 124.Pp 125The options listed below control the formatting style imposed by 126.Nm . 127.Bl -tag -width Op 128.It Fl bad , nbad 129If 130.Fl bad 131is specified, a blank line is forced after every block of 132declarations. 133Default: 134.Fl nbad . 135.It Fl bap , nbap 136If 137.Fl bap 138is specified, a blank line is forced after every procedure body. 139Default: 140.Fl nbap . 141.It Fl bbb , nbbb 142If 143.Fl bbb 144is specified, a blank line is forced before every block comment. 145Default: 146.Fl nbbb . 147.It Fl \&bc , nbc 148If 149.Fl \&bc 150is specified, then a newline is forced after each comma in a declaration. 151.Fl nbc 152turns off this option. 153Default: 154.Fl \&nbc . 155.It Fl \&br , \&bl 156Specifying 157.Fl \&bl 158lines-up compound statements like this: 159.Bd -literal -offset indent 160if (...) 161{ 162 code 163} 164.Ed 165.Pp 166Specifying 167.Fl \&br 168(the default) makes them look like this: 169.Bd -literal -offset indent 170if (...) { 171 code 172} 173.Ed 174.Pp 175.It Fl c Ns Ar n 176The column in which comments on code start. 177The default is 33. 178.It Fl cd Ns Ar n 179The column in which comments on declarations start. 180The default 181is for these comments to start in the same column as those on code. 182.It Fl cdb , ncdb 183Enables (disables) the placement of comment delimiters on blank lines. 184With 185this option enabled, comments look like this: 186.Bd -literal -offset indent 187 /* 188 * this is a comment 189 */ 190.Ed 191.Pp 192Rather than like this: 193.Bd -literal -offset indent 194 /* this is a comment */ 195.Ed 196.Pp 197This only affects block comments, not comments to the right of 198code. 199The default is 200.Fl cdb . 201.It Fl ce , nce 202Enables (disables) forcing of `else's to cuddle up to the immediately preceding 203`}'. 204The default is 205.Fl \&ce . 206.It Fl \&ci Ns Ar n 207Sets the continuation indent to be 208.Ar n . 209Continuation 210lines will be indented that far from the beginning of the first line of the 211statement. 212Parenthesized expressions have extra indentation added to 213indicate the nesting, unless 214.Fl \&lp 215is in effect 216or the continuation indent is exactly half of the main indent. 217.Fl \&ci 218defaults to the same value as 219.Fl i . 220.It Fl cli Ns Ar n 221Causes case labels to be indented 222.Ar n 223tab stops to the right of the containing 224.Ic switch 225statement. 226.Fl cli0.5 227causes case labels to be indented half a tab stop. 228The 229default is 230.Fl cli0 . 231.It Fl d Ns Ar n 232Controls the placement of comments which are not to the 233right of code. 234For example, 235.Fl \&d\&1 236means that such comments are placed one indentation level to the 237left of code. 238Specifying the default 239.Fl \&d\&0 240lines-up these comments with the code. 241See the section on comment 242indentation below. 243.It Fl \&di Ns Ar n 244Specifies the indentation, in character positions, 245of global variable names and all struct/union member names 246relative to the beginning of their type declaration. 247The default is 248.Fl di16 . 249.It Fl dj , ndj 250.Fl \&dj 251left justifies declarations. 252.Fl ndj 253indents declarations the same as code. 254The default is 255.Fl ndj . 256.It Fl \&ei , nei 257Enables (disables) special 258.Ic else-if 259processing. 260If it is enabled, an 261.Ic if 262following an 263.Ic else 264will have the same indentation as the preceding 265.Ic \&if 266statement. 267The default is 268.Fl ei . 269.It Fl fbs , nfbs 270Enables (disables) splitting the function declaration and opening brace 271across two lines. 272The default is 273.Fl fbs . 274.It Fl fc1 , nfc1 275Enables (disables) the formatting of comments that start in column 1. 276Often, comments whose leading `/' is in column 1 have been carefully 277hand formatted by the programmer. 278In such cases, 279.Fl nfc1 280should be 281used. 282The default is 283.Fl fc1 . 284.It Fl fcb , nfcb 285Enables (disables) the formatting of block comments (ones that begin 286with `/*\\n'). 287Often, block comments have been not so carefully hand formatted by the 288programmer, but reformatting that would just change the line breaks is not 289wanted. 290In such cases, 291.Fl nfcb 292should be used. 293Block comments are then handled like box comments. 294The default is 295.Fl fcb . 296.It Fl i Ns Ar n 297The number of spaces for one indentation level. 298The default is 8. 299.It Fl \&ip , nip 300Enables (disables) the indentation of parameter declarations from the left 301margin. 302The default is 303.Fl \&ip . 304.It Fl l Ns Ar n 305Maximum length of an output line. 306The default is 78. 307.It Fl \&ldi Ns Ar n 308Specifies the indentation, in character positions, 309of local variable names 310relative to the beginning of their type declaration. 311The default is for local variable names to be indented 312by the same amount as global ones. 313.It Fl \&lp , nlp 314Lines-up code surrounded by parenthesis in continuation lines. 315If a line 316has a left paren which is not closed on that line, then continuation lines 317will be lined up to start at the character position just after the left 318paren. 319For example, here is how a piece of continued code looks with 320.Fl nlp 321in effect: 322.Bd -literal -offset indent 323p1 = first_procedure(second_procedure(p2, p3), 324\ \ third_procedure(p4, p5)); 325.Ed 326.Pp 327With 328.Fl lp 329in effect (the default) the code looks somewhat clearer: 330.Bd -literal -offset indent 331p1\ =\ first_procedure(second_procedure(p2,\ p3), 332\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ third_procedure(p4,\ p5)); 333.Ed 334.Pp 335Inserting two more newlines we get: 336.Bd -literal -offset indent 337p1\ =\ first_procedure(second_procedure(p2, 338\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ p3), 339\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ third_procedure(p4, 340\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ p5)); 341.Ed 342.It Fl npro 343Causes the profile files, 344.Sq Pa ./.indent.pro 345and 346.Sq Pa ~/.indent.pro , 347to be ignored. 348.It Fl pcs , npcs 349If true 350.Pq Fl pcs 351all procedure calls will have a space inserted between 352the name and the `('. 353The default is 354.Fl npcs . 355.It Fl psl , npsl 356If true 357.Pq Fl psl 358the names of procedures being defined are placed in 359column 1 \- their types, if any, will be left on the previous lines. 360The 361default is 362.Fl psl . 363.It Fl \&sc , nsc 364Enables (disables) the placement of asterisks (`*'s) at the left edge of all 365comments. 366The default is 367.Fl sc . 368.It Fl sob , nsob 369If 370.Fl sob 371is specified, indent will swallow optional blank lines. 372You can use this to 373get rid of blank lines after declarations. 374Default: 375.Fl nsob . 376.It Fl \&st 377Causes 378.Nm 379to take its input from stdin and put its output to stdout. 380.It Fl T Ns Ar typename 381Adds 382.Ar typename 383to the list of type keywords. 384Names accumulate: 385.Fl T 386can be specified more than once. 387You need to specify all the typenames that 388appear in your program that are defined by 389.Ic typedef 390\- nothing will be 391harmed if you miss a few, but the program will not be formatted as nicely as 392it should. 393This sounds like a painful thing to have to do, but it is really 394a symptom of a problem in C: 395.Ic typedef 396causes a syntactic change in the 397language and 398.Nm 399cannot find all 400instances of 401.Ic typedef . 402.It Fl troff 403Causes 404.Nm 405to format the program for processing by 406.Xr troff 1 . 407It will produce a fancy 408listing in much the same spirit as 409.Xr vgrind 1 . 410If the output file is not specified, the default is standard output, 411rather than formatting in place. 412.It Fl ut , nut 413Enables (disables) the use of tab characters in the output. 414Tabs are assumed to be aligned on columns divisible by 8. 415The default is 416.Fl ut . 417.It Fl v , \&nv 418.Fl v 419turns on `verbose' mode; 420.Fl \&nv 421turns it off. 422When in verbose mode, 423.Nm 424reports when it splits one line of input into two or more lines of output, 425and gives some size statistics at completion. 426The default is 427.Fl \&nv . 428.El 429.Pp 430You may set up your own `profile' of defaults to 431.Nm 432by creating a file called 433.Pa .indent.pro 434in your login directory and/or the current directory and including 435whatever switches you like. 436A `.indent.pro' in the current directory takes 437precedence over the one in your login directory. 438If 439.Nm 440is run and a profile file exists, then it is read to set up the program's 441defaults. 442Switches on the command line, though, always override profile 443switches. 444The switches should be separated by spaces, tabs or newlines. 445.Pp 446.Ss Comments 447.Sq Em Box 448.Em comments . 449The 450.Nm 451utility 452assumes that any comment with a dash or star immediately after the start of 453comment (that is, `/*\-' or `/**') is a comment surrounded by a box of stars. 454Each line of such a comment is left unchanged, except that its indentation 455may be adjusted to account for the change in indentation of the first line 456of the comment. 457.Pp 458.Em Straight text . 459All other comments are treated as straight text. 460The 461.Nm 462utility fits as many words (separated by blanks, tabs, or newlines) on a 463line as possible. 464Blank lines break paragraphs. 465.Pp 466.Ss Comment indentation 467If a comment is on a line with code it is started in the `comment column', 468which is set by the 469.Fl c Ns Ns Ar n 470command line parameter. 471Otherwise, the comment is started at 472.Ar n 473indentation levels less than where code is currently being placed, where 474.Ar n 475is specified by the 476.Fl d Ns Ns Ar n 477command line parameter. 478If the code on a line extends past the comment 479column, the comment starts further to the right, and the right margin may be 480automatically extended in extreme cases. 481.Pp 482.Ss Preprocessor lines 483In general, 484.Nm 485leaves preprocessor lines alone. 486The only 487reformatting that it will do is to straighten up trailing comments. 488It 489leaves embedded comments alone. 490Conditional compilation 491.Pq Ic #ifdef...#endif 492is recognized and 493.Nm 494attempts to correctly 495compensate for the syntactic peculiarities introduced. 496.Pp 497.Ss C syntax 498The 499.Nm 500utility understands a substantial amount about the syntax of C, but it 501has a `forgiving' parser. 502It attempts to cope with the usual sorts of 503incomplete and misformed syntax. 504In particular, the use of macros like: 505.Pp 506.Dl #define forever for(;;) 507.Pp 508is handled properly. 509.Sh ENVIRONMENT 510The 511.Nm 512utility uses the 513.Ev HOME 514environment variable. 515.Sh FILES 516.Bl -tag -width "./.indent.pro" -compact 517.It Pa ./.indent.pro 518profile file 519.It Pa ~/.indent.pro 520profile file 521.El 522.Sh HISTORY 523The 524.Nm 525command appeared in 526.Bx 4.2 . 527.Sh BUGS 528The 529.Nm 530utility has even more switches than 531.Xr ls 1 . 532.Pp 533A common mistake is to try to indent all the 534.Em C 535programs in a directory by typing: 536.Pp 537.Dl indent *.c 538.Pp 539This is probably a bug, not a feature. 540