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