1.\" $NetBSD: indent.1,v 1.20 2011/01/12 06:17:52 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.Bd -literal -offset indent 214if (...) 215{ 216 code 217} 218.Ed 219.Pp 220Specifying 221.Fl \&br 222(the default) makes them look like this: 223.Bd -literal -offset indent 224if (...) { 225 code 226} 227.Ed 228.Pp 229.It Fl bs , Fl nbs 230If 231.Fl bs 232is specified, a blank is forced after 233.Ic sizeof . 234Default: 235.Fl nbs . 236.It Fl c Ns Ar n 237The column in which comments on code start. 238Default: 239.Fl c33 . 240.It Fl cd Ns Ar n 241The column in which comments on declarations start. 242The default 243is for these comments to start in the same column as those on code. 244.It Fl cdb , ncdb 245Enables (disables) the placement of comment delimiters on blank lines. 246With this option enabled, comments look like this: 247.Bd -literal -offset indent 248 /* 249 * this is a comment 250 */ 251.Ed 252.Pp 253Rather than like this: 254.Bd -literal -offset indent 255 /* this is a comment */ 256.Ed 257.Pp 258This only affects block comments, not comments to the right of 259code. 260Default: 261.Fl cdb . 262.It Fl ce , nce 263Enables (disables) forcing `else's to cuddle up to the immediately preceding 264`}'. 265Default: 266.Fl \&ce . 267.It Fl \&ci Ns Ar n 268Sets the continuation indent to be 269.Ar n . 270Continuation 271lines will be indented that far from the beginning of the first line of the 272statement. 273Parenthesized expressions have extra indentation added to 274indicate the nesting, unless 275.Fl \&lp 276is in effect. 277.Fl \&ci 278defaults to the same value as 279.Fl i . 280.It Fl cli Ns Ar n 281Causes case labels to be indented 282.Ar n 283tab stops to the right of the containing 284.Ic switch 285statement. 286.Fl cli0.5 287causes case labels to be indented half a tab stop. 288Default: 289.Fl cli0 . 290.It Fl d Ns Ar n 291Controls the placement of comments which are not to the right of code. 292For example, 293.Fl \&d\&1 294means that such comments are placed one indentation level to the left of code. 295Specifying the default 296.Fl \&d\&0 297lines up these comments with the code. 298See the section on comment 299indentation below. 300.It Fl \&di Ns Ar n 301Specifies the indentation, in character positions, from a declaration keyword 302to the following identifier. 303Default: 304.Fl di16 . 305.It Fl dj , ndj 306.Fl \&dj 307left justifies declarations. 308.Fl ndj 309indents declarations the same as code. 310Default: 311.Fl ndj . 312.It Fl \&ei , nei 313Enables (disables) special 314.Ic else-if 315processing. 316If it's enabled, an 317.Ic if 318following an 319.Ic else 320will have the same indentation as the preceding 321.Ic \&if 322statement. 323Default: 324.Fl ei . 325.It Fl eei , neei 326Enables (disables) extra indentation on continuation lines of 327the expression part of 328.Ic if 329and 330.Ic while 331statements. 332These continuation lines will be indented one extra level. 333Default: 334.Fl neei . 335.It Fl fc1 , nfc1 336Enables (disables) the formatting of comments that start in column 1. 337Often, comments whose leading `/' is in column 1 have been carefully 338hand formatted by the programmer. 339In such cases, 340.Fl nfc1 341should be used. 342Default: 343.Fl fc1 . 344.It Fl i Ns Ar n 345The number of spaces for one indentation level. 346Default: 347.Fl i8 . 348.It Fl \&ip , nip 349Enables (disables) the indentation of parameter declarations from the left 350margin. 351Default: 352.Fl \&ip . 353.It Fl l Ns Ar n 354Maximum length of an output line. 355Default: 356.Fl l78 . 357.It Fl \&lp , nlp 358Lines up code surrounded by parenthesis in continuation lines. 359If a line has a left parenthesis which is not closed on that line, then 360continuation lines will be lined up to start at the character 361position just after the left parenthesis. 362For example, here is how a piece of continued code looks with 363.Fl nlp 364in effect: 365.Bd -literal -offset indent 366p1 = first_procedure(second_procedure(p2, p3), 367\ \ third_procedure(p4,p5)); 368.Ed 369.Pp 370With 371.Fl lp 372in effect (the default) the code looks somewhat clearer: 373.Bd -literal -offset indent 374p1\ =\ first_procedure(second_procedure(p2,\ p3), 375\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ third_procedure(p4,p5)); 376.Ed 377.Pp 378Inserting two more newlines we get: 379.Bd -literal -offset indent 380p1\ =\ first_procedure(second_procedure(p2, 381\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ p3), 382\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ third_procedure(p4 383\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ p5)); 384.Ed 385.It Fl npro 386Causes the profile files, 387.Sq Pa ./.indent.pro 388and 389.Sq Pa ~/.indent.pro , 390to be ignored. 391.It Fl pcs , npcs 392If true 393.Pq Fl pcs 394all procedure calls will have a space inserted between 395the name and the `('. 396Default: 397.Fl npcs . 398.It Fl psl , npsl 399If true 400.Pq Fl psl 401the names of procedures being defined are placed in 402column 1 \- their types, if any, will be left on the previous lines. 403Default: 404.Fl psl . 405.It Fl \&sc , nsc 406Enables (disables) the placement of asterisks (`*'s) at the left edge of all 407comments. 408Default: 409.Fl sc . 410.It Fl sob , nsob 411If 412.Fl sob 413is specified, indent will swallow optional blank lines. 414You can use this to get rid of blank lines after declarations. 415Default: 416.Fl nsob . 417.It Fl \&st 418Causes 419.Nm 420to take its input from stdin, and put its output to stdout. 421.It Fl T Ns Ar typename 422Adds 423.Ar typename 424to the list of type keywords. 425Names accumulate: 426.Fl T 427can be specified more than once. 428You need to specify all the typenames that 429appear in your program that are defined by 430.Ic typedef 431\- nothing will be 432harmed if you miss a few, but the program won't be formatted as nicely as 433it should. 434This sounds like a painful thing to have to do, but it's really 435a symptom of a problem in C: 436.Ic typedef 437causes a syntactic change in the 438language and 439.Nm 440can't find all 441instances of 442.Ic typedef . 443.It Fl troff 444Causes 445.Nm 446to format the program for processing by 447.Xr troff 1 . 448It will produce a fancy 449listing in much the same spirit as 450.Xr vgrind 1 . 451If the output file is not specified, the default is standard output, 452rather than formatting in place. 453.It Fl v , \&nv 454.Fl v 455turns on `verbose' mode; 456.Fl \&nv 457turns it off. 458When in verbose mode, 459.Nm 460reports when it splits one line of input into two or more lines of output, 461and gives some size statistics at completion. 462Default: 463.Fl \&nv . 464.El 465.Pp 466You may set up your own `profile' of defaults to 467.Nm 468by creating a file called 469.Pa .indent.pro 470in your login directory and/or the current directory and including 471whatever switches you like. 472A `.indent.pro' in the current directory takes 473precedence over the one in your login directory. 474If 475.Nm 476is run and a profile file exists, then it is read to set up the program's 477defaults. 478Switches on the command line, though, always override profile switches. 479The switches should be separated by spaces, tabs or newlines. 480.Ss Comments 481.Sq Em Box 482.Em comments . 483.Nm 484assumes that any comment with a dash or star immediately after the start of 485comment (that is, `/*\-' or `/**') is a comment surrounded by a box of stars. 486Each line of such a comment is left unchanged, except that its indentation 487may be adjusted to account for the change in indentation of the first line 488of the comment. 489.Pp 490.Em Straight text . 491All other comments are treated as straight text. 492.Nm 493fits as many words (separated by blanks, tabs, or newlines) on a 494line as possible. 495Blank lines break paragraphs. 496.Ss Comment indentation 497If a comment is on a line with code it is started in the `comment column', 498which is set by the 499.Fl c Ns Ns Ar n 500command line parameter. 501Otherwise, the comment is started at 502.Ar n 503indentation levels less than where code is currently being placed, where 504.Ar n 505is specified by the 506.Fl d Ns Ns Ar n 507command line parameter. 508If the code on a line extends past the comment 509column, the comment starts further to the right, and the right margin may be 510automatically extended in extreme cases. 511.Ss Preprocessor lines 512In general, 513.Nm 514leaves preprocessor lines alone. 515The only reformatting that it will do is to straighten up trailing comments. 516It leaves embedded comments alone. 517Conditional compilation 518.Pq Ic #ifdef...#endif 519is recognized and 520.Nm 521attempts to correctly 522compensate for the syntactic peculiarities introduced. 523.Ss C syntax 524.Nm 525understands a substantial amount about the syntax of C, but it 526has a `forgiving' parser. 527It attempts to cope with the usual sorts of incomplete and misformed syntax. 528In particular, the use of macros like: 529.Pp 530.Dl #define forever for(;;) 531.Pp 532is handled properly. 533.Sh ENVIRONMENT 534.Nm 535uses the 536.Ev HOME 537environment variable. 538.Sh FILES 539.Bl -tag -width "./.indent.pro" -compact 540.It Pa ./.indent.pro 541profile file 542.It Pa ~/.indent.pro 543profile file 544.El 545.Sh HISTORY 546The 547.Nm 548command appeared in 549.Bx 4.2 . 550.Sh BUGS 551.Nm 552has even more switches than 553.Xr ls 1 . 554.Pp 555A common mistake that often causes grief is typing: 556.Pp 557.Dl indent *.c 558.Pp 559to the shell in an attempt to indent all the 560.Tn C 561programs in a directory. 562This is probably a bug, not a feature. 563