1.\" $OpenBSD: indent.1,v 1.13 2003/06/12 01:07:27 deraadt 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 July 1, 1993 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.Op Ar input-file Op Ar output-file 44.Op Fl bad | Fl nbad 45.Op Fl bap | Fl nbap 46.Bk -words 47.Op Fl bbb | Fl nbbb 48.Ek 49.Op Fl \&bc | Fl nbc 50.Op Fl \&bl 51.Op Fl \&br 52.Op Fl c Ns Ar n 53.Op Fl \&cd Ns Ar n 54.Bk -words 55.Op Fl cdb | Fl ncdb 56.Ek 57.Op Fl \&ce | Fl nce 58.Op Fl \&ci Ns Ar n 59.Op Fl cli Ns Ar n 60.Op Fl d Ns Ar n 61.Op Fl \&di Ns Ar n 62.Bk -words 63.Op Fl fc1 | Fl nfc1 64.Ek 65.Op Fl i Ns Ar n 66.Op Fl \&ip | Fl nip 67.Op Fl l Ns Ar n 68.Op Fl \&lc Ns Ar n 69.Op Fl \&lp | Fl nlp 70.Op Fl npro 71.Op Fl pcs | Fl npcs 72.Op Fl psl | Fl npsl 73.Op Fl \&sc | Fl nsc 74.Bk -words 75.Op Fl sob | Fl nsob 76.Ek 77.Op Fl \&st 78.Op Fl troff 79.Op Fl v | Fl \&nv 80.Sh DESCRIPTION 81.Nm 82is a 83.Ar C 84program formatter. 85It reformats the 86.Ar C 87program in the 88.Ar input-file 89according to the switches. 90The switches which can be specified are described below. 91They may appear before or after the file names. 92.Pp 93.Sy NOTE : 94If you only specify an 95.Ar input-file , 96the formatting is 97done 98.Dq in-place , 99that is, the formatted file is written back into 100.Ar input-file 101and a backup copy of 102.Ar input-file 103is written in the current directory. 104If 105.Ar input-file 106is named 107.Pa /blah/blah/file , 108the backup file is named 109.Pa file.BAK . 110If 111.Pa file.BAK 112exists, it is overwritten. 113.Pp 114If 115.Ar output-file 116is specified, 117.Nm 118checks to make sure it is different from 119.Ar input-file . 120.Pp 121The options listed below control the formatting style imposed by 122.Nm indent . 123.Bl -tag -width Op 124.It Fl bad , nbad 125If 126.Fl bad 127is specified, a blank line is forced after every block of 128declarations. 129Default: 130.Fl nbad . 131.It Fl bap , nbap 132If 133.Fl bap 134is specified, a blank line is forced after every procedure body. 135Default: 136.Fl nbap . 137.It Fl bbb , nbbb 138If 139.Fl bbb 140is specified, a blank line is forced before every block comment. 141Default: 142.Fl nbbb . 143.It Fl \&bc , nbc 144If 145.Fl \&bc 146is specified, then a newline is forced after each comma in a declaration. 147.Fl nbc 148turns off this option. 149The default is 150.Fl \&nbc . 151.It Fl \&br , \&bl 152Specifying 153.Fl \&bl 154lines up compound statements like this: 155.ne 4 156.Bd -literal -offset indent 157if (...) 158{ 159 code 160} 161.Ed 162.Pp 163Specifying 164.Fl \&br 165(the default) makes them look like this: 166.ne 3 167.Bd -literal -offset indent 168if (...) { 169 code 170} 171.Ed 172.Pp 173.It Fl c Ns Ar n 174The column in which comments on code start. 175The default is 33. 176.It Fl cd Ns Ar n 177The column in which comments on declarations start. 178The default 179is for these comments to start in the same column as those on code. 180.It Fl cdb , ncdb 181Enables (disables) the placement of comment delimiters on blank lines. 182With this option enabled, comments look like this: 183.Bd -literal -offset indent 184.ne 3 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. 232The default 233.Fl \&d\&1 234means that such comments are placed one indentation level to the 235left of code. 236Specifying 237.Fl \&d\&0 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. 263.It Fl fc1 , nfc1 264Enables (disables) the formatting of comments that start in column 1. 265Often, comments whose leading 266.Ql / 267is in column 1 have been carefully have formatted by the programmer. 268In such cases, 269.Fl nfc1 270should be 271used. 272The default is 273.Fl fc1 . 274.It Fl i Ns Ar n 275The number of spaces for one indentation level. 276The default is 8. 277.It Fl \&ip , nip 278Enables (disables) the indentation of parameter declarations from the left 279margin. 280The default is 281.Fl \&ip . 282.It Fl l Ns Ar n 283Maximum length of an output line. 284The default is 75. 285.It Fl \&lp , nlp 286Lines up code surrounded by parenthesis in continuation lines. 287If a line 288has a left paren which is not closed on that line, then continuation lines 289will be lined up to start at the character position just after the left 290paren. 291For example, here is how a piece of continued code looks with 292.Fl nlp 293in effect: 294.ne 2 295.Bd -literal -offset indent 296p1 = first_procedure(second_procedure(p2, p3), 297\ \ third_procedure(p4,p5)); 298.Ed 299.Pp 300.ne 5 301With 302.Fl lp 303in effect (the default) the code looks somewhat clearer: 304.Bd -literal -offset indent 305p1\ =\ first_procedure(second_procedure(p2,\ p3), 306\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ third_procedure(p4,p5)); 307.Ed 308.Pp 309.ne 5 310Inserting two more newlines we get: 311.Bd -literal -offset indent 312p1\ =\ first_procedure(second_procedure(p2, 313\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ p3), 314\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ third_procedure(p4, 315\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ p5)); 316.Ed 317.It Fl npro 318Causes the profile files, 319.Pa ./.indent.pro 320and 321.Pa ~/.indent.pro , 322to be ignored. 323.It Fl pcs , npcs 324If true 325.Pq Fl pcs 326all procedure calls will have a space inserted between 327the name and the 328.Ql ( . 329The default is 330.Fl npcs . 331.It Fl psl , npsl 332If true 333.Pq Fl psl 334the names of procedures being defined are placed in 335column 1 \- their types, if any, will be left on the previous lines. 336The default is 337.Fl psl . 338.It Fl \&sc , nsc 339Enables (disables) the placement of asterisks 340.Pq Ql * 341at the left edge of all comments. 342.It Fl sob , nsob 343If 344.Fl sob 345is specified, indent will swallow optional blank lines. 346You can use this to get rid of blank lines after declarations. 347Default: 348.Fl nsob . 349.It Fl \&st 350Causes 351.Nm 352to take its input from stdin, and put its output to stdout. 353.It Fl T Ns Ar typename 354Adds 355.Ar typename 356to the list of type keywords. 357Names accumulate: 358.Fl T 359can be specified more than once. 360You need to specify all the typenames that 361appear in your program that are defined by 362.Ic typedef 363\- nothing will be 364harmed if you miss a few, but the program won't be formatted as nicely as 365it should. 366This sounds like a painful thing to have to do, but it's really 367a symptom of a problem in C: 368.Ic typedef 369causes a syntactic change in the 370language and 371.Nm 372can't find all 373instances of 374.Ic typedef . 375.It Fl troff 376Causes 377.Nm 378to format the program for processing by 379.Xr troff 1 . 380It will produce a fancy 381listing in much the same spirit as 382.Xr vgrind 1 . 383If the output file is not specified, the default is standard output, 384rather than formatting in place. 385.It Fl v , \&nv 386.Fl v 387turns on 388.Dq verbose 389mode; 390.Fl \&nv 391turns it off. 392When in verbose mode, 393.Nm 394reports when it splits one line of input into two or more lines of output, 395and gives some size statistics at completion. 396The default is 397.Fl \&nv . 398.El 399.Pp 400You may set up your own 401.Dq profile 402of defaults to 403.Nm 404by creating a file called 405.Pa .indent.pro 406in your login directory and/or the current directory and including 407whatever switches you like. 408An 409.Pa \&.indent.pro 410file in the current directory takes 411precedence over the one in your login directory. 412If 413.Nm 414is run and a profile file exists, then it is read to set up the program's 415defaults. 416Switches on the command line, though, always override profile 417switches. 418The switches should be separated by spaces, tabs or newlines. 419.Ss Comments 420.Sq Em Box 421.Em comments . 422.Nm 423assumes that any comment with a dash, star, or newline immediately after 424the start of comment (that is, 425.Ql /*\- , 426.Ql /** , 427or 428.Ql /* 429followed immediately by a newline character) is a comment surrounded 430by a box of stars. 431Each line of such a comment is left unchanged, except 432that its indentation may be adjusted to account for the change in indentation 433of the first line 434of the comment. 435.Pp 436.Em Straight text . 437All other comments are treated as straight text. 438.Nm 439fits as many words (separated by blanks, tabs, or newlines) on a 440line as possible. 441Blank lines break paragraphs. 442.Ss Comment indentation 443If a comment is on a line with code it is started in the 444.Dq comment column , 445which is set by the 446.Fl c Ns Ns Ar n 447command line parameter. 448Otherwise, the comment is started at 449.Ar n 450indentation levels less than where code is currently being placed, where 451.Ar n 452is specified by the 453.Fl d Ns Ns Ar n 454command line parameter. 455If the code on a line extends past the comment 456column, the comment starts further to the right, and the right margin may be 457automatically extended in extreme cases. 458.Ss Preprocessor lines 459In general, 460.Nm 461leaves preprocessor lines alone. 462The only 463reformatting that it will do is to straighten up trailing comments. 464It leaves embedded comments alone. 465Conditional compilation 466.Pq Ic #ifdef...#endif 467is recognized and 468.Nm 469attempts to correctly 470compensate for the syntactic peculiarities introduced. 471.Ss C syntax 472.Nm 473understands a substantial amount about the syntax of C, but it 474has a 475.Dq forgiving 476parser. 477It attempts to cope with the usual sorts of 478incomplete and misformed syntax. 479In particular, the use of macros like: 480.Pp 481.Dl #define forever for(;;) 482.Pp 483is handled properly. 484.Sh ENVIRONMENT 485.Bl -tag -width Ds 486.It Ev HOME 487Used to locate the full path to 488.Pa ~/.indent.pro . 489.El 490.Sh FILES 491.Bl -tag -width "./.indent.pro" -compact 492.It Pa ./.indent.pro 493profile file 494.It Pa ~/.indent.pro 495profile file 496.El 497.Sh HISTORY 498The 499.Nm 500command appeared in 501.Bx 4.2 . 502.Sh BUGS 503.Nm 504has even more switches than 505.Xr ls 1 . 506.Pp 507.ne 5 508A common mistake that often causes grief is typing: 509.Pp 510.Dl indent *.c 511.Pp 512to the shell in an attempt to indent all the 513.Nm C 514programs in a directory. 515This is probably a bug, not a feature. 516