aboutsummaryrefslogtreecommitdiff
path: root/usr.bin/indent
Commit message (Collapse)AuthorAgeFilesLines
* indent(1): add regression test casesPiotr Pawel Stefaniak2017-02-1033-0/+312
| | | | | | | | | | | | | | | | | | | | | | | | These examples show expected behavior of indent(1). They are meant to be used together with a regression test mechanism, either Kyua, a Makefile or perhaps something else. The mechanism should in essence do this: indent -P${test}.pro < ${test}.0 > ${test}.0.run and compare ${test}.0.stdout to ${test}.0.run. If the files differ or the exit status isn't 0, the test failed. * ${test}.pro is an indent(1) profile: a list of options passed through a file. The program doesn't complain if the file doesn't exist. * ${test}.0 is a C source file which acts as input for indent(1). It doesn't have to have any particular formatting, since it's the output that matters. * ${test}.0.stdout contains expected output. It doesn't have to be formatted in Kernel Normal Form as the point of the tests is to check for regressions in the program and not to check that it always produces KNF. Reviewed by: ngie Approved by: pfg (mentor) Differential Revision: https://reviews.freebsd.org/D9007 Notes: svn path=/head/; revision=313544
* indent(1): add a piece missed in r311138.Piotr Pawel Stefaniak2017-01-021-0/+1
| | | | Notes: svn path=/head/; revision=311141
* indent(1): add option -P for loading user-provided files as profilesPiotr Pawel Stefaniak2017-01-024-5/+15
| | | | | | | | | | Without this change, indent(1) would only look to load options from ~/.indent.pro if it's there and -npro wasn't used on the command line. This option lets the user set their own path to the file. Approved by: pfg (mentor) Differential Revision: https://reviews.freebsd.org/D9010 Notes: svn path=/head/; revision=311138
* indent(1): Avoid out of bounds access of array ps.paren_indentsPiotr Pawel Stefaniak2016-12-301-1/+6
| | | | | | | | | ps.p_l_follow can't be allowed to grow beyond maximum index of paren_indents. Approved by: pfg (mentor) Notes: svn path=/head/; revision=310863
* indent(1): remove unneeded comma.Pedro F. Giffuni2016-12-041-1/+1
| | | | | | | | | | It is not a good practice to start a newline with a comma anyways. Discussed with: bjk Differential Revision: https://reviews.freebsd.org/D8690 Notes: svn path=/head/; revision=309524
* Document undocumented indent(1) options badp/nbadp, bs/nbs, and lc.Pedro F. Giffuni2016-12-031-1/+19
| | | | | | | | Submitted by: Piotr Stefaniak Differential Revision: https://reviews.freebsd.org/D8690 Notes: svn path=/head/; revision=309451
* indent(1): Avoid out-of-bound accesses of array ps.p_stack.Pedro F. Giffuni2016-12-021-1/+1
| | | | | | | Submitted by: Piotr Stefaniak Notes: svn path=/head/; revision=309419
* indent(1): Avoid out-of-bound accesses of arrays.Pedro F. Giffuni2016-12-021-2/+3
| | | | | | | | | | | | | | | ps.paren_indents: When ps.paren_level was 0, this was accessing paren_indents[-1]. in_buffer: This fragment checks if "*/" was read, but there's no guarantee that there is more than one byte in the array (actually, this happens frequently for the "{" in things like "int main(void) {"). Submitted by: Piotr Stefaniak Notes: svn path=/head/; revision=309418
* indent(1): Remove an extra newline added in a previous commit.Pedro F. Giffuni2016-12-021-1/+0
| | | | | | | It doesn't math the remaining of the case statement. Notes: svn path=/head/; revision=309417
* indent(1): Optimize parser stack usage.Pedro F. Giffuni2016-12-021-1/+7
| | | | | | | | | | | | | | When special else-if processing is enabled (-ei), we can assume "else if" and "if" to be equivalent for indentation purposes. This reduction saves a lot of stack space in case of a long "if-else-if ... else-if" sequence; with this change, Postgres/src/bin/psql/tab-complete.c as of 9.6beta3 requires minimum of the stack length to be 31 instead of 444. Submitted by: Piotr Sephaniak Notes: svn path=/head/; revision=309415
* indent(1): Do not define opchar unless it will be used.Pedro F. Giffuni2016-12-021-0/+2
| | | | | | | | | | "opchar" is only used once depending on "undef" macro being defined. Conditionalize it in the same way. Submitted by: Piotr Sephaniak Notes: svn path=/head/; revision=309382
* indent(1): Fix indent's confusion about custom FreeBSD macros.Pedro F. Giffuni2016-12-023-7/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Teach indent(1) about storage-class specifiers. Don't assume "in_parameter_declaration" state if "in_decl" hasn't been set. Don't set "in_decl" for storage-class specifiers. That set of changes helps with recognizing the difference between file scope declarations like this: static LIST_HEAD(, alq) ald_active; static int ald_shuttingdown = 0; struct thread *ald_thread; and old style function declarators like this: static int do_execve(td, args, mac_p) struct thread *td; struct image_args *args; struct mac *mac_p; { Unfortunately, at the same time this change makes indent(1) require explicit int in declarations like "static a;", in order to understand that it's part of a declaration. On the other hand, declarations like in the first example are no longer indented as if ald_shuttingdown and ald_thread were parameters of a function named LIST_HEAD. Submitted by: Piotr Stefaniak Notes: svn path=/head/; revision=309380
* indent(1): Don't unnecessarily add a blank before a comment ends.Pedro F. Giffuni2016-12-011-1/+1
| | | | | | | | | | pr_comment() did avoid adding surplus space character when a comment contained it at the end. Now it's also paying attention to tabs. Taken from: Piotr Stefaniak Notes: svn path=/head/; revision=309343
* indent(1): Don't ignore newlines after comments that follow braces.Pedro F. Giffuni2016-12-011-2/+4
| | | | | | | | | | | | indent.c has a special loop that stores tokens from between an if () and the next statement into a buffer. The loop ignored all newlines, but that resulted in not calling dump_line() when it was needed to produce the final line of the buffered up comment. Taken from: Piotr Stefaniak Notes: svn path=/head/; revision=309342
* indent(1): Avoid out of bound access of array in_bufferPedro F. Giffuni2016-12-012-5/+13
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Work-around a somewhat complex interaction within the code. From Piotr's commit [1]: When pr_comment() calls dump_line() for the first line of a multiline comment, it doesn't include any indentation - it starts with the "/*". This is consistent for both boxed and not boxed comments. Where the logic diverges is in how it treats the rest of the lines of the comment. For box comments indent assumes that it must not change anything, so lines are dumped as they were, including the indentation where it exists. For the rest of comments, it will first remove the indentation to store plain text of the comment and then add it again where indent thinks it's appropriate -- this is part of comment re-indenting process. For continuations of multi-line comments, the code that handles comments in dump_line() will use pad_output() to create indentation from the beginning of the line (what indent calls the first column) and then write string pointed by s_com afterwards. But if it's a box comment, the string will include original indentation, unless it's the first line of the comment. This is why tab characters from s_com have to be considered when calculating how much padding is needed and the "while (*com_st == '\t') com_st++, target += 8;" does that. In dump_line(), /target/ is initially set to ps.com_col, so it always assumes that indentation needs to be produced in this function, regardless of which line of a box comment it is. But for the first line of a box comment it is not true, so pr_comment() signals it by setting ps.n_comment_delta, the negative comment delta, to a negative number which is then added to /target/ in dump_line() on all lines except the first one, so that the function produces adequate indentation in this special case. The bug was in how that negative offset was calculated: pr_comment() used count_spaces() on in_buffer, which pr_comment() expected to contain non-null terminated sequence of characters, originating from whatever originally was on the left side of the comment. Understanding that count_spaces() requires a string, pr_comment() temporarily set buf_ptr[-2] to 0 in hope that it would nul-terminate the right thing in in_buffer and calling count_spaces() would be safe and do the expected thing. This was false whenever buf_ptr would point into save_com, an entirely different char array than in_buffer. The short-term fix is to recognize whether buf_ptr points into in_buffer or save_com. Reference: [1] https://github.com/pstef/freebsd_indent/commit/ea486a2aa3b056b146bdfbb8e94843159750f200 Taken from: Piotr Stefaniak Notes: svn path=/head/; revision=309341
* indent(1): Properly handle the wide string literal and wide char constant L.Pedro F. Giffuni2016-11-273-0/+16
| | | | | | | | | | | indent(1) treated the "L" in "L'a'" as if it were an identifier and forced a space character after it, breaking valid code. PR: 143090 MFC after: 2 weeks Notes: svn path=/head/; revision=309220
* indent(1): fix regression introduced in r303596.Pedro F. Giffuni2016-11-271-8/+12
| | | | | | | | | | | Multi-line comments are always block comments in KNF. Restore properly, handling the case when a long one-liner gets wrapped and becomes a multi-line comment. Obtained from: Piotr Stefaniak Notes: svn path=/head/; revision=309219
* indent(1): minor off-by-one error.Pedro F. Giffuni2016-11-271-1/+1
| | | | | | | | | This was introduced in r303571. Obtianed from: Piotr Stefaniak Notes: svn path=/head/; revision=309217
* Move sys/capsicum.h includes after types.h or param.hConrad Meyer2016-09-191-1/+1
| | | | | | | | | | | This is not actually documented or even implied in style(9). Make the change to match convention. Someone should document this convention in style(9). Reported by: jhb Sponsored by: EMC Dell Isilon Notes: svn path=/head/; revision=305998
* indent(1): CapsicumifyConrad Meyer2016-09-191-0/+14
| | | | | | | | | | | | This is a nice and trivial program for sandboxing. One input file, one output file. Reviewed by: pfg Sponsored by: Dell EMC Isilon Differential Revision: https://reviews.freebsd.org/D7920 Notes: svn path=/head/; revision=305983
* indent(1): have the memset invocation somewhat more canonical.Pedro F. Giffuni2016-08-231-1/+1
| | | | | | | | | While correct, the previous invocation was somewhat more error prone. Pointed out by: delphij, bde Notes: svn path=/head/; revision=304686
* indent(1): remove dead assignments.Pedro F. Giffuni2016-08-232-2/+1
| | | | | | | Taken from: Piotr Sephaniak Notes: svn path=/head/; revision=304684
* indent(1): Fix off-by-one in control flow leading dead code.Pedro F. Giffuni2016-08-231-1/+1
| | | | | | | | | | | | | | | | | Coverity correctly reported that it's impossible for /comparison/ to be 0 here, because the only way for the for loop to end is by /comparison/ being < 0. Fortunately the consequences of this bug weren't severe; for duplicated entries in the typedef names file it would unnecessarily duplicate strings with strdup(), but pointers to those would replace existing ones. So this was a memory leak at worst. CID: 1361477 Obtained from: Piotr Stephaniak Notes: svn path=/head/; revision=304653
* indent(1): add some comments to quiet down Coverity.Pedro F. Giffuni2016-08-232-1/+3
| | | | | | | | | | | Hopefully adding comments should help explain the code to both static checkers and humans. CID: 976543, 976544, 976545 Obtained from: Piotr Stephaniak Notes: svn path=/head/; revision=304651
* indent(1): Fix memory leaks pointed out by clang-analyzer.Pedro F. Giffuni2016-08-232-15/+11
| | | | | | | | | | | | Shift the responsibility of allocating memory for the string duplicate from the caller (set_option, add_typedefs_from_file) to the callee (add_typename) as it has more knowledge about when the duplication actually needs to occur. Taken from: Piotr Stefaniak Notes: svn path=/head/; revision=304650
* indent(1): Use bsearch() for looking up type keywords.Pedro F. Giffuni2016-08-044-77/+99
| | | | | | | | | | | Reference: https://github.com/pstef/freebsd_indent/commit/f3b8e6e57fd47364b2360b44011a2fad5d11adc7 Submitted by: Piotr Stefaniak Differential Revision: https://reviews.freebsd.org/D6966 Notes: svn path=/head/; revision=303746
* indent(1): add new -sac and -U options.Pedro F. Giffuni2016-08-035-6/+50
| | | | | | | | | | | | | | | | | | | | | | Add -sac (space after cast) and -nsac options. These control whether space character is put after a cast operator or not. Default is -nsac. Add -U option for providing a file containing list of types. This is needed for properly deciding which asterisks denote unary operation and which denote binary. These come from PostgreSQL. Reference: https://github.com/pstef/freebsd_indent/commit/84b00e3d46dfd6d955b2f481a1f3b275de9ad6d1 https://github.com/pstef/freebsd_indent/commit/49c52cf383fa2a246a1a22c6640a5a21b0f1fd90 Differential Revision: https://reviews.freebsd.org/D6966 (Partial) Submitted by: Piotr Stefaniak Notes: svn path=/head/; revision=303735
* indent(1): accept offsetof(3) as a keyword.Pedro F. Giffuni2016-08-033-32/+34
| | | | | | | | | | | Reference: https://github.com/pstef/freebsd_indent/commit/c470e5e2c974aa38450ca4762b93829f7a7bfa4d Differential Revision: https://reviews.freebsd.org/D6966 (Partial) Submitted by: Piotr Stefaniak Notes: svn path=/head/; revision=303718
* indent: Avoid using values of pointers that refer to deallocated space.Pedro F. Giffuni2016-08-011-5/+10
| | | | | | | | | | | | | | | For now maintain the local style in this file. Reviewed by: jilles Reference: https://github.com/pstef/freebsd_indent/commit/9099a9f17bc5f579514a4c11111f5cf3df6624c6 Differential Revision: https://reviews.freebsd.org/D6966 (Partial) Submitted by: Piotr Stefaniak Notes: svn path=/head/; revision=303629
* indent(1): Use a dash in the license headers.Pedro F. Giffuni2016-08-019-9/+9
| | | | | | | | Use of the canonical dash avoids indent(1) from reformatting the license headers. Notes: svn path=/head/; revision=303625
* indent(1): Rearrange option parsing code to squelch clang's static analyzer.Pedro F. Giffuni2016-07-311-8/+6
| | | | | | | | | | | | | | | | | clang-analyzer complained that eqin() sets file-scoped pointer param_start to point into char buffer defined in scan_profile(), and once scan_profile() exits, param_start is a "dangling reference". param_start was never used afterwards, but it's cleaner to move it to set_option() which is the only branch where param_start is needed. Reference: https://github.com/pstef/freebsd_indent/commit/ab0e44e5da3ff0fa4b62e451e4bbc3ea1ec7f365 Differential Revision: https://reviews.freebsd.org/D6966 (Partial) Submitted by: Piotr Stefaniak Notes: svn path=/head/; revision=303601
* indent(1): replace function call to bzero with memset.Pedro F. Giffuni2016-07-311-1/+1
| | | | | | | | | | | Reference: https://github.com/pstef/freebsd_indent/commit/7422f42f80099c69d34833d7106035dc09230235 Differential Revision: https://reviews.freebsd.org/D6966 (Partial) Submitted by: Piotr Stefaniak Notes: svn path=/head/; revision=303600
* indent(1): Don't newline on cpp lines like #endif unless -bacc is on.Pedro F. Giffuni2016-07-311-23/+32
| | | | | | | | | | | Reference: https://github.com/pstef/freebsd_indent/commit/01f36f4141c71754b3a73a91886fb425bab0df3e Differential Revision: https://reviews.freebsd.org/D6966 (Partial) Submitted by: Piotr Stefaniak Notes: svn path=/head/; revision=303599
* indent(1): Untangle the connection between pr_comment.c and io.c.Pedro F. Giffuni2016-07-312-59/+37
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | It's pr_comment.c that should decide whether to put a "star comment continuation" or not. This duplicates code a bit, but it simplifies pr_comment() at the same time since pr_comment() no longer has to "signal" whether a star continuation is needed or not. This change requires indent(1) to not wrap comment lines that lack a blank character, but I think it's for the better if you look at cases when that happens (mostly long URIs and file system paths, which arguably shouldn't be wrapped). It also fixes two bugs: 1. Cases where asterisk is a part of the comment's content (like in "*we* are the champions") and happens to appear at the beginning of the line, misleading dump_line() into thinking that this is part of the star comment continuation, leading to misalignment. 2. Cases where blank starred lines had three too many characters on the line when wrapped. Reference: https://github.com/pstef/freebsd_indent/commit/3b41ee78aafafc7c3e662b794835e3253218dbb3 Differential Revision: https://reviews.freebsd.org/D6966 (Partial) Submitted by: Piotr Stefaniak Notes: svn path=/head/; revision=303598
* indent(1): Fix wrapping of some lines in comments.Pedro F. Giffuni2016-07-311-0/+1
| | | | | | | | | | | | | | | | After a blank line was printed (to separate paragraphs in comments), the next line was sometimes wrapped to the column at which the previous non-empty line ended. The fix is to reset the last blank pointer (last_bl) on newline. References: https://github.com/pstef/freebsd_indent/commit/345663c07af0758fd10433bde14722dfd900f85c Differential Revision: https://reviews.freebsd.org/D6966 (Partial) Submitted by: Piotr Stefaniak Notes: svn path=/head/; revision=303597
* indent(1): Simplify pr_comment().Pedro F. Giffuni2016-07-313-91/+55
| | | | | | | | | | | | | | | | | | | | | | | | | | | Modify count_spaces() to take a third parameter "end" that will make the function return when the end is reached. This lets the caller pass a pointer to non nul-terminated sequence of characters. Rename count_spaces() to count_spaces_until() and reinstate count_spaces(), this time based on count_spaces_until(). Use count_spaces_until() to recalculate current column when going through a comment just before the fragment which decides if current line of the comment should be wrapped. This move simplifies this code by eliminating the need for keeping the column counter up to date every time e_com is advanced and also reduces spread of code that has to know how many columns a tab will produce. Deduplicate code that decided if a comment needs a blank line at the top. References: https://github.com/pstef/freebsd_indent/commit/d9fa3b481532a448095f8ddd14fd0797ce59230c https://github.com/pstef/freebsd_indent/commit/27185b4b336b0e2108a3d96aee6df80cced94192 Differential Revision: https://reviews.freebsd.org/D6966 (Partial) Submitted by: Piotr Stefaniak Notes: svn path=/head/; revision=303596
* indent(1): Remove dead code relating to unix-style comments.Pedro F. Giffuni2016-07-311-43/+1
| | | | | | | | | | | | | | | | | | | The original indent(1) described unix-style comments as similar to box comments, except the first non-blank character on each line is lined up with the '*' of the "/*" which appears on a line by itself. The code has been turned off for ages and -sc/-nsc make it even less relevant. Reference: https://github.com/pstef/freebsd_indent/commit/89c5fe2c56742d96975bb3ea6b99f28baf9d82f6 Differential Revision: https://reviews.freebsd.org/D6966 (Partial) Submitted by: Piotr Stefaniak Notes: svn path=/head/; revision=303588
* indent(1): Bail out if there's no more space on the parser stack.Pedro F. Giffuni2016-07-312-1/+5
| | | | | | | | | | | | Also increase the stack size still keeping a conservative value of 256. This is based on a similar changes done for PostgreSQL which instead uses a stack size of 1000. Differential Revision: https://reviews.freebsd.org/D6966 (Partial) Submitted by: Piotr Stefaniak (with changes) Notes: svn path=/head/; revision=303571
* indent(1): Consistently indent declarations.Pedro F. Giffuni2016-07-311-80/+79
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This fixes a very visible issue that may be hidden by some indent.pro settings as in the example from FreeBSD's /usr/share. From Piotr's log: ____ To prevent losing tabs from indentation in declarations, FreeBSD indent's r125624 added code for the most common case when it's an identifier that is indented, but didn't do anything with the original code that did the same for any other cases. The other cases are: lparens (function pointer declaration), asterisks (pointer declaration), stray semicolons, and commas leading identifiers instead of trailing them. Use the code added in r125624 (and improved in later commits) to write a new function indent_declaration() and use it in all places that meant to indent declarations. In order to indent only once per line, reuse existing ps.dumped_decl_indent variable that was only used when formatting for troff (-troff) until now. ____ Reference: https://github.com/pstef/freebsd_indent/commit/ddd263db2a59978f43468989eff65299cf3ce7e1 Differential Revision: https://reviews.freebsd.org/D6966 (Partial) Submitted by: Piotr Stefaniak Notes: svn path=/head/; revision=303570
* Use nitems() from sys/param.h.Marcelo Araujo2016-07-301-1/+1
| | | | | | | | MFC after: 2 weeks. Sponsored by: gandi.net (BSD Day Taiwan) Notes: svn path=/head/; revision=303547
* indent(1): Yet more style issues.Pedro F. Giffuni2016-07-303-6/+6
| | | | | | | | | | | strchr(3) returns a pointer not a boolean. Attempt to make the style somewhat more ocnsistent with what indent had before recent changes. Pointed out by: bde Notes: svn path=/head/; revision=303525
* indent(1): Attempt to preserve some consistent style.Pedro F. Giffuni2016-07-291-4/+3
| | | | | | | | | | | Remove the excessive braces from r303485 and align the comments to the right as done in the rest of the code. This is not nice but there is no clear way to make it nice (and KNF). Pointed out by: bde Notes: svn path=/head/; revision=303523
* indent(1): Use NULL instead of zero for pointers.Pedro F. Giffuni2016-07-295-23/+23
| | | | Notes: svn path=/head/; revision=303502
* indent(1): Support "f" and "F" floating constant suffixes.Pedro F. Giffuni2016-07-291-4/+2
| | | | | | | | | | Actually this just brings back r303487 with the correct commit log. Differential Revision: https://reviews.freebsd.org/D6966 (Partial) Obtained from: Piotr Stefaniak Notes: svn path=/head/; revision=303499
* Revert r303487: Wrong commit log.Pedro F. Giffuni2016-07-291-2/+4
| | | | | | | Will be brought back with the correct log. Notes: svn path=/head/; revision=303498
* indent(1): Removed whitespace shouldn't be considered in column calculations.Pedro F. Giffuni2016-07-291-0/+1
| | | | | | | | | | | | | | | | | This piece of code removed tabs and space characters from after colons that follow labels by decrementing the e_lab (end of label) "pointer" which is later used to calculate the width of the string that fprintf() puts into "output". But pad_output() gets the length from the actual string, so it miscalculated what the current column is. Fixed by putting a string terminator at the e_lab "pointer". Differential Revision: https://reviews.freebsd.org/D6966 (Partial) Obtained from: Piotr Stefaniak Notes: svn path=/head/; revision=303489
* indent(1): fix struct termination detection.Pedro F. Giffuni2016-07-291-4/+2
| | | | | | | | | | Small style cleanup while here. Differential Revision: https://reviews.freebsd.org/D6966 (Partial) Obtained from: Piotr Stefaniak Notes: svn path=/head/; revision=303487
* indent(1): fix struct termination detection.Pedro F. Giffuni2016-07-291-2/+4
| | | | | | | | | | Semicolons inside struct declarations don't end the declarations. Differential Revision: https://reviews.freebsd.org/D6966 (Partial) Obtained from: Piotr Stefaniak Notes: svn path=/head/; revision=303485
* indent(1): Fix breakage caused by single comment following "else".Pedro F. Giffuni2016-07-291-0/+2
| | | | | | | | | | | | indent(1) simply wasn't taught that "else" may be followed by a comment without any opening brace anywhere on the line, so it was very confused in such cases. Differential Revision: https://reviews.freebsd.org/D6966 (Partial) Obtained from: Piotr Stefaniak Notes: svn path=/head/; revision=303484
* indent(1): Avoid potential use-after-free.Pedro F. Giffuni2016-07-291-0/+1
| | | | | | | | | | | | | last_bl is a char pointer that tracks the last blank character in a comment, which is used for wrapping long comment lines. Since the underlying array may be reallocated, make sure last_bl is up to date when that happens. Differential Revision: https://reviews.freebsd.org/D6966 (Partial) Obtained from: Piotr Stefaniak Notes: svn path=/head/; revision=303483