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