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