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