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