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