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