Home | History | Annotate | Line # | Download | only in indent
indent.c revision 1.394
      1 /*	$NetBSD: indent.c,v 1.394 2025/01/04 21:20:59 rillig Exp $	*/
      2 
      3 /*-
      4  * SPDX-License-Identifier: BSD-4-Clause
      5  *
      6  * Copyright (c) 1985 Sun Microsystems, Inc.
      7  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
      8  * Copyright (c) 1980, 1993
      9  *	The Regents of the University of California.  All rights reserved.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the University of
     22  *	California, Berkeley and its contributors.
     23  * 4. Neither the name of the University nor the names of its contributors
     24  *    may be used to endorse or promote products derived from this software
     25  *    without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  * SUCH DAMAGE.
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 __RCSID("$NetBSD: indent.c,v 1.394 2025/01/04 21:20:59 rillig Exp $");
     42 
     43 #include <sys/param.h>
     44 #include <err.h>
     45 #include <stdarg.h>
     46 #include <stdio.h>
     47 #include <stdlib.h>
     48 #include <string.h>
     49 
     50 #include "indent.h"
     51 
     52 struct options opt = {
     53 	.brace_same_line = true,
     54 	.comment_delimiter_on_blank_line = true,
     55 	.cuddle_else = true,
     56 	.comment_column = 33,
     57 	.decl_indent = 16,
     58 	.else_if_in_same_line = true,
     59 	.function_brace_split = true,
     60 	.format_col1_comments = true,
     61 	.format_block_comments = true,
     62 	.indent_parameters = true,
     63 	.indent_size = 8,
     64 	.local_decl_indent = -1,
     65 	.lineup_to_parens = true,
     66 	.procnames_start_line = true,
     67 	.star_comment_cont = true,
     68 	.tabsize = 8,
     69 	.max_line_length = 78,
     70 	.use_tabs = true,
     71 };
     72 
     73 struct parser_state ps;
     74 
     75 struct buffer token;
     76 
     77 struct buffer lab;
     78 struct buffer code;
     79 struct buffer com;
     80 
     81 bool found_err;
     82 bool had_eof;
     83 
     84 static struct {
     85 	struct parser_state *item;
     86 	size_t len;
     87 	size_t cap;
     88 } ifdef;
     89 
     90 FILE *output;
     91 
     92 static const char *in_name = "Standard Input";
     93 static char backup_name[PATH_MAX];
     94 static const char *backup_suffix = ".BAK";
     95 
     96 
     97 void *
     98 nonnull(void *p)
     99 {
    100 	if (p == NULL)
    101 		err(EXIT_FAILURE, NULL);
    102 	return p;
    103 }
    104 
    105 static void
    106 buf_expand(struct buffer *buf, size_t add_size)
    107 {
    108 	buf->cap = buf->cap + add_size + 400;
    109 	buf->s = nonnull(realloc(buf->s, buf->cap));
    110 }
    111 
    112 #ifdef debug
    113 void
    114 buf_terminate(struct buffer *buf)
    115 {
    116 	if (buf->len == buf->cap)
    117 		buf_expand(buf, 1);
    118 	buf->s[buf->len] = '\0';
    119 }
    120 #endif
    121 
    122 void
    123 buf_add_char(struct buffer *buf, char ch)
    124 {
    125 	if (buf->len == buf->cap)
    126 		buf_expand(buf, 1);
    127 	buf->s[buf->len++] = ch;
    128 	buf_terminate(buf);
    129 }
    130 
    131 void
    132 buf_add_chars(struct buffer *buf, const char *s, size_t len)
    133 {
    134 	if (len == 0)
    135 		return;
    136 	if (len > buf->cap - buf->len)
    137 		buf_expand(buf, len);
    138 	memcpy(buf->s + buf->len, s, len);
    139 	buf->len += len;
    140 	buf_terminate(buf);
    141 }
    142 
    143 static void
    144 buf_add_buf(struct buffer *buf, const struct buffer *add)
    145 {
    146 	buf_add_chars(buf, add->s, add->len);
    147 }
    148 
    149 void
    150 diag(int level, const char *msg, ...)
    151 {
    152 	va_list ap;
    153 
    154 	if (level != 0)
    155 		found_err = true;
    156 
    157 	va_start(ap, msg);
    158 	(void)fprintf(stderr, "%s: %s:%d: ",
    159 	    level == 0 ? "warning" : "error", in_name, in.token_start_line);
    160 	(void)vfprintf(stderr, msg, ap);
    161 	(void)fprintf(stderr, "\n");
    162 	va_end(ap);
    163 }
    164 
    165 /*
    166  * Compute the indentation from starting at 'ind' and adding the text starting
    167  * at 's'.
    168  */
    169 int
    170 ind_add(int ind, const char *s, size_t len)
    171 {
    172 	for (const char *p = s; len > 0; p++, len--) {
    173 		if (*p == '\n')
    174 			ind = 0;
    175 		else if (*p == '\t')
    176 			ind = next_tab(ind);
    177 		else
    178 			ind++;
    179 	}
    180 	return ind;
    181 }
    182 
    183 static void
    184 init_globals(void)
    185 {
    186 	ps_push(psym_stmt, false);	/* as a stop symbol */
    187 	ps.prev_lsym = lsym_semicolon;
    188 	ps.lbrace_kind = psym_lbrace_block;
    189 
    190 	const char *suffix = getenv("SIMPLE_BACKUP_SUFFIX");
    191 	if (suffix != NULL)
    192 		backup_suffix = suffix;
    193 }
    194 
    195 static void
    196 load_profiles(int argc, char **argv)
    197 {
    198 	const char *profile_name = NULL;
    199 
    200 	for (int i = 1; i < argc; i++) {
    201 		const char *arg = argv[i];
    202 
    203 		if (strcmp(arg, "-npro") == 0)
    204 			return;
    205 		if (arg[0] == '-' && arg[1] == 'P' && arg[2] != '\0')
    206 			profile_name = arg + 2;
    207 	}
    208 
    209 	load_profile_files(profile_name);
    210 }
    211 
    212 /*
    213  * Copy the input file to the backup file, then make the backup file the input
    214  * and the original input file the output.
    215  */
    216 static void
    217 copy_to_bak_file(void)
    218 {
    219 	size_t n;
    220 	char buff[BUFSIZ];
    221 
    222 	const char *last_slash = strrchr(in_name, '/');
    223 	const char *base = last_slash != NULL ? last_slash + 1 : in_name;
    224 	snprintf(backup_name, sizeof(backup_name), "%s%s", base, backup_suffix);
    225 
    226 	/* copy the input file to the backup file */
    227 	FILE *bak = fopen(backup_name, "w");
    228 	if (bak == NULL)
    229 		err(1, "%s", backup_name);
    230 
    231 	while ((n = fread(buff, 1, sizeof(buff), in.f)) > 0)
    232 		if (fwrite(buff, 1, n, bak) != n)
    233 			err(1, "%s", backup_name);
    234 	if (fclose(in.f) != 0)
    235 		err(1, "%s", in_name);
    236 	if (fclose(bak) != 0)
    237 		err(1, "%s", backup_name);
    238 
    239 	/* re-open the backup file as the input file */
    240 	in.f = fopen(backup_name, "r");
    241 	if (in.f == NULL)
    242 		err(1, "%s", backup_name);
    243 	/* now the original input file will be the output */
    244 	output = fopen(in_name, "w");
    245 	if (output == NULL) {
    246 		(void)remove(backup_name);
    247 		err(1, "%s", in_name);
    248 	}
    249 }
    250 
    251 static void
    252 parse_command_line(int argc, char **argv)
    253 {
    254 	for (int i = 1; i < argc; i++) {
    255 		const char *arg = argv[i];
    256 
    257 		if (arg[0] == '-') {
    258 			set_option(arg, "Command line");
    259 
    260 		} else if (in.f == NULL) {
    261 			in_name = arg;
    262 			if ((in.f = fopen(in_name, "r")) == NULL)
    263 				err(1, "%s", in_name);
    264 
    265 		} else if (output == NULL) {
    266 			if (strcmp(arg, in_name) == 0)
    267 				errx(1, "input and output files "
    268 				    "must be different");
    269 			if ((output = fopen(arg, "w")) == NULL)
    270 				err(1, "%s", arg);
    271 
    272 		} else
    273 			errx(1, "too many arguments: %s", arg);
    274 	}
    275 
    276 	if (in.f == NULL) {
    277 		in.f = stdin;
    278 		output = stdout;
    279 	} else if (output == NULL)
    280 		copy_to_bak_file();
    281 
    282 	if (opt.comment_column <= 1)
    283 		opt.comment_column = 2;	/* don't put normal comments in column
    284 					 * 1, see opt.format_col1_comments */
    285 	if (opt.block_comment_max_line_length <= 0)
    286 		opt.block_comment_max_line_length = opt.max_line_length;
    287 	if (opt.local_decl_indent < 0)
    288 		opt.local_decl_indent = opt.decl_indent;
    289 	if (opt.decl_comment_column <= 0)
    290 		opt.decl_comment_column = opt.left_justify_decl
    291 		    ? (opt.comment_column <= 10 ? 2 : opt.comment_column - 8)
    292 		    : opt.comment_column;
    293 	if (opt.continuation_indent == 0)
    294 		opt.continuation_indent = opt.indent_size;
    295 }
    296 
    297 static void
    298 set_initial_indentation(void)
    299 {
    300 	inp_read_line();
    301 
    302 	int ind = 0;
    303 	for (const char *p = in.p;; p++) {
    304 		if (*p == ' ')
    305 			ind++;
    306 		else if (*p == '\t')
    307 			ind = next_tab(ind);
    308 		else
    309 			break;
    310 	}
    311 
    312 	ps.ind_level = ps.ind_level_follow = ind / opt.indent_size;
    313 }
    314 
    315 static bool
    316 should_break_line(lexer_symbol lsym)
    317 {
    318 	if (lsym == lsym_if && ps.prev_lsym == lsym_else
    319 	    && opt.else_if_in_same_line)
    320 		ps.newline = nl_no;
    321 	if (ps.newline == nl_unless_lbrace && lsym != lsym_lbrace)
    322 		ps.newline = nl_yes;
    323 	if (ps.newline == nl_unless_semicolon && lsym != lsym_semicolon)
    324 		ps.newline = nl_yes;
    325 	if (ps.newline == nl_unless_if && lsym != lsym_if)
    326 		ps.newline = nl_yes;
    327 	if (ps.newline != nl_yes)
    328 		return false;
    329 	if (lsym == lsym_semicolon && ps.prev_lsym == lsym_rbrace)
    330 		return false;
    331 	if (ps.prev_lsym == lsym_lbrace || ps.prev_lsym == lsym_semicolon)
    332 		return true;
    333 	if (lsym == lsym_lbrace && opt.brace_same_line)
    334 		return false;
    335 	return true;
    336 }
    337 
    338 static void
    339 move_com_to_code(lexer_symbol lsym)
    340 {
    341 	if (ps.want_blank)
    342 		buf_add_char(&code, ' ');
    343 	buf_add_buf(&code, &com);
    344 	buf_clear(&com);
    345 	ps.want_blank = lsym != lsym_rparen && lsym != lsym_rbracket;
    346 }
    347 
    348 static void
    349 update_ps_lbrace_kind(lexer_symbol lsym)
    350 {
    351 	if (lsym == lsym_tag) {
    352 		ps.lbrace_kind = token.s[0] == 's' ? psym_lbrace_struct :
    353 		    token.s[0] == 'u' ? psym_lbrace_union :
    354 		    psym_lbrace_enum;
    355 	} else if ((lsym == lsym_type && ps.paren.len == 0)
    356 	    || lsym == lsym_word
    357 	    || lsym == lsym_lbrace) {
    358 		/* Keep the current '{' kind. */
    359 	} else
    360 		ps.lbrace_kind = psym_lbrace_block;
    361 }
    362 
    363 static void
    364 update_ps_badp(lexer_symbol lsym)
    365 {
    366 	if (lsym == lsym_lbrace && ps.lbrace_kind == psym_lbrace_block
    367 	    && ps.psyms.len == 3)
    368 		ps.badp = badp_seen_lbrace;
    369 	if (lsym == lsym_rbrace && !ps.in_decl)
    370 		ps.badp = badp_none;
    371 	if (lsym == lsym_type && ps.paren.len == 0
    372 	    && (ps.badp == badp_seen_lbrace || ps.badp == badp_yes))
    373 		ps.badp = badp_decl;
    374 	if (lsym == lsym_semicolon && ps.badp == badp_decl
    375 	    && ps.decl_level == 0)
    376 		ps.badp = badp_seen_decl;
    377 }
    378 
    379 static void
    380 indent_declarator(int decl_ind, bool tabs_to_var)
    381 {
    382 	int base = ps.ind_level * opt.indent_size;
    383 	int ind = ind_add(base, code.s, code.len);
    384 	int target = base + decl_ind;
    385 	size_t orig_code_len = code.len;
    386 
    387 	if (tabs_to_var)
    388 		for (int next; (next = next_tab(ind)) <= target; ind = next)
    389 			buf_add_char(&code, '\t');
    390 	for (; ind < target; ind++)
    391 		buf_add_char(&code, ' ');
    392 	if (code.len == orig_code_len && ps.want_blank)
    393 		buf_add_char(&code, ' ');
    394 
    395 	ps.want_blank = false;
    396 	ps.decl_indent_done = true;
    397 }
    398 
    399 static bool
    400 is_function_pointer_declaration(void)
    401 {
    402 	return ps.in_decl
    403 	    && !ps.in_typedef_decl
    404 	    && !ps.in_init
    405 	    && !ps.decl_indent_done
    406 	    && !ps.line_has_func_def
    407 	    && ps.ind_paren_level == 0;
    408 }
    409 
    410 static int
    411 process_eof(void)
    412 {
    413 	finish_output();
    414 
    415 	if (ps.psyms.len > 2)	/* check for balanced braces */
    416 		diag(1, "Stuff missing from end of file");
    417 
    418 	return found_err ? EXIT_FAILURE : EXIT_SUCCESS;
    419 }
    420 
    421 /* move the whole line to the 'label' buffer */
    422 static void
    423 read_preprocessing_line(void)
    424 {
    425 	enum {
    426 		PLAIN, STR, CHR, COMM
    427 	} state = PLAIN;
    428 
    429 	buf_add_char(&lab, '#');
    430 
    431 	while (in.p[0] != '\n' || (state == COMM && !had_eof)) {
    432 		buf_add_char(&lab, inp_next());
    433 		switch (lab.s[lab.len - 1]) {
    434 		case '\\':
    435 			if (state != COMM)
    436 				buf_add_char(&lab, inp_next());
    437 			break;
    438 		case '/':
    439 			if (in.p[0] == '*' && state == PLAIN) {
    440 				state = COMM;
    441 				buf_add_char(&lab, *in.p++);
    442 			}
    443 			break;
    444 		case '"':
    445 			if (state == STR)
    446 				state = PLAIN;
    447 			else if (state == PLAIN)
    448 				state = STR;
    449 			break;
    450 		case '\'':
    451 			if (state == CHR)
    452 				state = PLAIN;
    453 			else if (state == PLAIN)
    454 				state = CHR;
    455 			break;
    456 		case '*':
    457 			if (in.p[0] == '/' && state == COMM) {
    458 				state = PLAIN;
    459 				buf_add_char(&lab, *in.p++);
    460 			}
    461 			break;
    462 		}
    463 	}
    464 
    465 	while (lab.len > 0 && ch_isblank(lab.s[lab.len - 1]))
    466 		lab.len--;
    467 	buf_terminate(&lab);
    468 }
    469 
    470 static void
    471 paren_stack_push(struct paren_stack *s, int indent, enum paren_level_cast cast)
    472 {
    473 	if (s->len == s->cap) {
    474 		s->cap = 10 + s->cap;
    475 		s->item = nonnull(realloc(s->item,
    476 			sizeof(s->item[0]) * s->cap));
    477 	}
    478 	s->item[s->len++] = (struct paren_level){indent, cast};
    479 }
    480 
    481 static void *
    482 dup_mem(const void *src, size_t size)
    483 {
    484 	return memcpy(nonnull(malloc(size)), src, size);
    485 }
    486 
    487 #define dup_array(src, len) \
    488     dup_mem((src), sizeof((src)[0]) * (len))
    489 #define copy_array(dst, src, len) \
    490     memcpy((dst), (src), sizeof((dst)[0]) * (len))
    491 
    492 static_unless_debug void
    493 parser_state_back_up(struct parser_state *dst)
    494 {
    495 	*dst = ps;
    496 
    497 	dst->paren.item = dup_array(ps.paren.item, ps.paren.len);
    498 	dst->psyms.sym = dup_array(ps.psyms.sym, ps.psyms.len);
    499 	dst->psyms.ind_level = dup_array(ps.psyms.ind_level, ps.psyms.len);
    500 }
    501 
    502 static void
    503 parser_state_restore(const struct parser_state *src)
    504 {
    505 	struct paren_level *ps_paren_item = ps.paren.item;
    506 	size_t ps_paren_cap = ps.paren.cap;
    507 	parser_symbol *ps_psyms_sym = ps.psyms.sym;
    508 	int *ps_psyms_ind_level = ps.psyms.ind_level;
    509 	size_t ps_psyms_cap = ps.psyms.cap;
    510 
    511 	ps = *src;
    512 
    513 	ps.paren.item = ps_paren_item;
    514 	ps.paren.cap = ps_paren_cap;
    515 	ps.psyms.sym = ps_psyms_sym;
    516 	ps.psyms.ind_level = ps_psyms_ind_level;
    517 	ps.psyms.cap = ps_psyms_cap;
    518 
    519 	copy_array(ps.paren.item, src->paren.item, src->paren.len);
    520 	copy_array(ps.psyms.sym, src->psyms.sym, src->psyms.len);
    521 	copy_array(ps.psyms.ind_level, src->psyms.ind_level, src->psyms.len);
    522 }
    523 
    524 static_unless_debug void
    525 parser_state_free(struct parser_state *pst)
    526 {
    527 	free(pst->paren.item);
    528 	free(pst->psyms.sym);
    529 	free(pst->psyms.ind_level);
    530 }
    531 
    532 static void
    533 process_preprocessing(void)
    534 {
    535 	if (lab.len > 0 || code.len > 0 || com.len > 0)
    536 		output_line();
    537 
    538 	read_preprocessing_line();
    539 
    540 	const char *dir = lab.s + 1, *line_end = lab.s + lab.len;
    541 	while (dir < line_end && ch_isblank(*dir))
    542 		dir++;
    543 	size_t dir_len = 0;
    544 	while (dir + dir_len < line_end && ch_isalpha(dir[dir_len]))
    545 		dir_len++;
    546 
    547 	if (dir_len >= 2 && memcmp(dir, "if", 2) == 0) {
    548 		if (ifdef.len >= ifdef.cap) {
    549 			ifdef.cap += 5;
    550 			ifdef.item = nonnull(realloc(ifdef.item,
    551 				sizeof(ifdef.item[0]) * ifdef.cap));
    552 		}
    553 		parser_state_back_up(ifdef.item + ifdef.len++);
    554 		out.line_kind = lk_pre_if;
    555 
    556 	} else if (dir_len >= 2 && memcmp(dir, "el", 2) == 0) {
    557 		if (ifdef.len == 0)
    558 			diag(1, "Unmatched #%.*s", (int)dir_len, dir);
    559 		else
    560 			parser_state_restore(ifdef.item + ifdef.len - 1);
    561 		out.line_kind = lk_pre_other;
    562 
    563 	} else if (dir_len == 5 && memcmp(dir, "endif", 5) == 0) {
    564 		if (ifdef.len == 0)
    565 			diag(1, "Unmatched #endif");
    566 		else
    567 			parser_state_free(ifdef.item + --ifdef.len);
    568 		out.line_kind = lk_pre_endif;
    569 	} else
    570 		out.line_kind = lk_pre_other;
    571 }
    572 
    573 static void
    574 process_newline(void)
    575 {
    576 	if (ps.prev_lsym == lsym_comma
    577 	    && ps.paren.len == 0 && !ps.in_init
    578 	    && !opt.break_after_comma && ps.break_after_comma
    579 	    && lab.len == 0	/* for preprocessing lines */
    580 	    && com.len == 0)
    581 		goto stay_in_line;
    582 	if (ps.psyms.sym[ps.psyms.len - 1] == psym_switch_expr
    583 	    && opt.brace_same_line
    584 	    && com.len == 0) {
    585 		ps.newline = nl_unless_lbrace;
    586 		goto stay_in_line;
    587 	}
    588 	if (ps.psyms.sym[ps.psyms.len - 1] == psym_if_expr_stmt_else
    589 	    && opt.else_if_in_same_line) {
    590 		ps.newline = nl_unless_if;
    591 		goto stay_in_line;
    592 	}
    593 
    594 	output_line();
    595 
    596 stay_in_line:
    597 	in.token_end_line++;
    598 }
    599 
    600 static bool
    601 want_blank_before_lparen(void)
    602 {
    603 	if (opt.proc_calls_space)
    604 		return true;
    605 	if (ps.prev_lsym == lsym_sizeof)
    606 		return opt.blank_after_sizeof;
    607 	if (ps.prev_lsym == lsym_rparen
    608 	    || ps.prev_lsym == lsym_rbracket
    609 	    || ps.prev_lsym == lsym_postfix_op
    610 	    || ps.prev_lsym == lsym_offsetof
    611 	    || ps.prev_lsym == lsym_word
    612 	    || ps.prev_lsym == lsym_funcname)
    613 		return false;
    614 	return true;
    615 }
    616 
    617 static void
    618 process_lparen(void)
    619 {
    620 
    621 	if (is_function_pointer_declaration())
    622 		indent_declarator(ps.decl_ind, ps.tabs_to_var);
    623 	else if (ps.want_blank && want_blank_before_lparen())
    624 		buf_add_char(&code, ' ');
    625 	ps.want_blank = false;
    626 	buf_add_buf(&code, &token);
    627 
    628 	if (opt.extra_expr_indent && ps.spaced_expr_psym != psym_0)
    629 		ps.extra_expr_indent = eei_maybe;
    630 
    631 	if (ps.in_var_decl && ps.psyms.len <= 3 && !ps.in_init) {
    632 		parse(psym_stmt);	/* prepare for function definition */
    633 		ps.in_var_decl = false;
    634 	}
    635 
    636 	enum paren_level_cast cast = cast_unknown;
    637 	if (ps.prev_lsym == lsym_offsetof
    638 	    || ps.prev_lsym == lsym_sizeof
    639 	    || ps.prev_lsym == lsym_for
    640 	    || ps.prev_lsym == lsym_if
    641 	    || ps.prev_lsym == lsym_switch
    642 	    || ps.prev_lsym == lsym_while
    643 	    || ps.line_has_func_def)
    644 		cast = cast_no;
    645 
    646 	paren_stack_push(&ps.paren, ind_add(0, code.s, code.len), cast);
    647 }
    648 
    649 static bool
    650 rparen_is_cast(bool paren_cast)
    651 {
    652 	if (ps.in_func_def_params)
    653 		return false;
    654 	if (ps.line_has_decl && !ps.in_init)
    655 		return false;
    656 	if (ps.prev_lsym == lsym_unary_op)
    657 		return true;
    658 	if (ps.spaced_expr_psym != psym_0 && ps.paren.len == 0)
    659 		return false;
    660 	return paren_cast || ch_isalpha(in.p[0]) || in.p[0] == '{';
    661 }
    662 
    663 static void
    664 process_rparen(void)
    665 {
    666 	if (ps.paren.len == 0)
    667 		diag(0, "Extra '%c'", *token.s);
    668 
    669 	bool paren_cast = ps.paren.len > 0
    670 	    && ps.paren.item[--ps.paren.len].cast == cast_maybe;
    671 	ps.prev_paren_was_cast = rparen_is_cast(paren_cast);
    672 	if (ps.prev_paren_was_cast) {
    673 		ps.next_unary = true;
    674 		ps.want_blank = opt.space_after_cast;
    675 	} else
    676 		ps.want_blank = true;
    677 
    678 	if (code.len == 0)
    679 		ps.ind_paren_level = (int)ps.paren.len;
    680 
    681 	buf_add_buf(&code, &token);
    682 
    683 	if (ps.spaced_expr_psym != psym_0 && ps.paren.len == 0) {
    684 		bool is_do_while = ps.spaced_expr_psym == psym_while_expr
    685 		    && ps.psyms.sym[ps.psyms.len - 1] == psym_do_stmt;
    686 		parse(ps.spaced_expr_psym);
    687 		ps.spaced_expr_psym = psym_0;
    688 
    689 		ps.newline = is_do_while
    690 		    ? nl_unless_semicolon : nl_unless_lbrace;
    691 		ps.next_unary = true;
    692 		ps.in_stmt_or_decl = false;
    693 		ps.want_blank = true;
    694 		out.line_kind = lk_stmt_head;
    695 		if (ps.extra_expr_indent == eei_maybe)
    696 			ps.extra_expr_indent = eei_last;
    697 	}
    698 }
    699 
    700 static void
    701 process_lbracket(void)
    702 {
    703 	if (code.len > 0
    704 	    && (ps.prev_lsym == lsym_comma || ps.prev_lsym == lsym_binary_op))
    705 		buf_add_char(&code, ' ');
    706 	buf_add_buf(&code, &token);
    707 	ps.want_blank = false;
    708 
    709 	paren_stack_push(&ps.paren, ind_add(0, code.s, code.len), cast_no);
    710 }
    711 
    712 static void
    713 process_rbracket(void)
    714 {
    715 	if (ps.paren.len == 0)
    716 		diag(0, "Extra '%c'", *token.s);
    717 	if (ps.paren.len > 0)
    718 		ps.paren.len--;
    719 
    720 	if (code.len == 0)
    721 		ps.ind_paren_level = (int)ps.paren.len;
    722 
    723 	buf_add_buf(&code, &token);
    724 	ps.want_blank = true;
    725 }
    726 
    727 static void
    728 process_lbrace(void)
    729 {
    730 	if (ps.prev_lsym == lsym_rparen && ps.prev_paren_was_cast) {
    731 		ps.in_var_decl = true;	// XXX: not really
    732 		ps.in_init = true;
    733 	}
    734 
    735 	if (out.line_kind == lk_stmt_head)
    736 		out.line_kind = lk_other;
    737 
    738 	ps.in_stmt_or_decl = false;	/* don't indent the {} */
    739 
    740 	if (ps.in_init)
    741 		ps.init_level++;
    742 	else
    743 		ps.newline = nl_yes;
    744 
    745 	if (code.len > 0 && !ps.in_init) {
    746 		if (!opt.brace_same_line ||
    747 		    (code.len > 0 && code.s[code.len - 1] == '}'))
    748 			output_line();
    749 		else if (ps.in_func_def_params && !ps.in_var_decl) {
    750 			ps.ind_level_follow = 0;
    751 			if (opt.function_brace_split)
    752 				output_line();
    753 			else
    754 				ps.want_blank = true;
    755 		}
    756 	}
    757 
    758 	if (ps.paren.len > 0 && ps.init_level == 0) {
    759 		diag(1, "Unbalanced parentheses");
    760 		ps.paren.len = 0;
    761 		if (ps.spaced_expr_psym != psym_0) {
    762 			parse(ps.spaced_expr_psym);
    763 			ps.spaced_expr_psym = psym_0;
    764 			ps.ind_level = ps.ind_level_follow;
    765 		}
    766 	}
    767 
    768 	if (code.len == 0)
    769 		ps.line_is_stmt_cont = false;
    770 	if (ps.in_decl && ps.in_var_decl) {
    771 		ps.di_stack[ps.decl_level] = ps.decl_ind;
    772 		if (++ps.decl_level == (int)array_length(ps.di_stack)) {
    773 			diag(0, "Reached internal limit of %zu struct levels",
    774 			    array_length(ps.di_stack));
    775 			ps.decl_level--;
    776 		}
    777 	} else {
    778 		ps.line_has_decl = false;	/* don't do special indentation
    779 						 * of comments */
    780 		ps.in_func_def_params = false;
    781 		ps.in_decl = false;
    782 	}
    783 
    784 	ps.decl_ind = 0;
    785 	parse(ps.lbrace_kind);
    786 	if (ps.want_blank)
    787 		buf_add_char(&code, ' ');
    788 	ps.want_blank = false;
    789 	buf_add_char(&code, '{');
    790 	ps.declaration = decl_no;
    791 }
    792 
    793 static void
    794 process_rbrace(void)
    795 {
    796 	if (ps.paren.len > 0 && ps.init_level == 0) {
    797 		diag(1, "Unbalanced parentheses");
    798 		ps.paren.len = 0;
    799 		ps.spaced_expr_psym = psym_0;
    800 	}
    801 
    802 	ps.declaration = decl_no;
    803 	if (ps.decl_level == 0)
    804 		ps.blank_line_after_decl = false;
    805 	if (ps.init_level > 0)
    806 		ps.init_level--;
    807 
    808 	if (code.len > 0 && !ps.in_init)
    809 		output_line();
    810 
    811 	buf_add_char(&code, '}');
    812 	ps.want_blank = true;
    813 	ps.in_stmt_or_decl = false;	// XXX: Initializers don't end a stmt
    814 	ps.line_is_stmt_cont = false;
    815 
    816 	if (ps.decl_level > 0) {	/* multi-level structure declaration */
    817 		ps.decl_ind = ps.di_stack[--ps.decl_level];
    818 		if (ps.decl_level == 0 && !ps.in_func_def_params) {
    819 			ps.declaration = decl_begin;
    820 			ps.decl_ind = ps.ind_level == 0
    821 			    ? opt.decl_indent : opt.local_decl_indent;
    822 		}
    823 		ps.in_decl = true;
    824 	}
    825 
    826 	if (ps.psyms.len == 3)
    827 		out.line_kind = lk_func_end;
    828 
    829 	parse(psym_rbrace);
    830 
    831 	if (!ps.in_var_decl
    832 	    && ps.psyms.sym[ps.psyms.len - 1] != psym_do_stmt
    833 	    && ps.psyms.sym[ps.psyms.len - 1] != psym_if_expr_stmt)
    834 		ps.newline = nl_yes;
    835 }
    836 
    837 static void
    838 process_period(void)
    839 {
    840 	if (code.len > 0 && code.s[code.len - 1] == ',')
    841 		buf_add_char(&code, ' ');
    842 	buf_add_char(&code, '.');
    843 	ps.want_blank = false;
    844 }
    845 
    846 static void
    847 process_unary_op(void)
    848 {
    849 	if (is_function_pointer_declaration()) {
    850 		int ind = ps.decl_ind - (int)token.len;
    851 		indent_declarator(ind, ps.tabs_to_var);
    852 	} else if ((token.s[0] == '+' || token.s[0] == '-')
    853 	    && code.len > 0 && code.s[code.len - 1] == token.s[0])
    854 		ps.want_blank = true;
    855 
    856 	if (ps.want_blank)
    857 		buf_add_char(&code, ' ');
    858 	buf_add_buf(&code, &token);
    859 	ps.want_blank = false;
    860 }
    861 
    862 static void
    863 process_postfix_op(void)
    864 {
    865 	buf_add_buf(&code, &token);
    866 	ps.want_blank = true;
    867 }
    868 
    869 static void
    870 process_comma(void)
    871 {
    872 	ps.want_blank = code.len > 0;	/* only put blank after comma if comma
    873 					 * does not start the line */
    874 
    875 	if (ps.in_decl && ps.ind_paren_level == 0
    876 	    && !ps.line_has_func_def && !ps.in_init && !ps.decl_indent_done) {
    877 		/* indent leading commas and not the actual identifiers */
    878 		indent_declarator(ps.decl_ind - 1, ps.tabs_to_var);
    879 	}
    880 
    881 	buf_add_char(&code, ',');
    882 
    883 	if (ps.paren.len == 0) {
    884 		if (ps.init_level == 0)
    885 			ps.in_init = false;
    886 		int typical_varname_length = 8;
    887 		if (ps.break_after_comma && (opt.break_after_comma ||
    888 			ind_add(compute_code_indent(), code.s, code.len)
    889 			>= opt.max_line_length - typical_varname_length))
    890 			ps.newline = nl_yes;
    891 	}
    892 }
    893 
    894 static void
    895 process_label_colon(void)
    896 {
    897 	buf_add_buf(&lab, &code);
    898 	buf_add_char(&lab, ':');
    899 	buf_clear(&code);
    900 
    901 	if (ps.seen_case)
    902 		out.line_kind = lk_case_or_default;
    903 	ps.in_stmt_or_decl = false;
    904 	ps.newline = ps.seen_case ? nl_unless_semicolon : nl_no;
    905 	ps.seen_case = false;
    906 	ps.want_blank = false;
    907 }
    908 
    909 static void
    910 process_other_colon(void)
    911 {
    912 	buf_add_char(&code, ':');
    913 	ps.want_blank = ps.decl_level == 0;
    914 }
    915 
    916 static void
    917 process_semicolon(void)
    918 {
    919 	if (out.line_kind == lk_stmt_head)
    920 		out.line_kind = lk_other;
    921 	if (ps.decl_level == 0) {
    922 		ps.in_var_decl = false;
    923 		ps.in_typedef_decl = false;
    924 	}
    925 	ps.seen_case = false;	/* only needs to be reset on error */
    926 	ps.quest_level = 0;	/* only needs to be reset on error */
    927 	if (ps.prev_lsym == lsym_rparen)
    928 		ps.in_func_def_params = false;
    929 	ps.in_init = false;
    930 	ps.init_level = 0;
    931 	ps.declaration = ps.declaration == decl_begin ? decl_end : decl_no;
    932 
    933 	if (ps.in_decl && code.len == 0 && !ps.in_init &&
    934 	    !ps.decl_indent_done && ps.ind_paren_level == 0) {
    935 		/* indent stray semicolons in declarations */
    936 		indent_declarator(ps.decl_ind - 1, ps.tabs_to_var);
    937 	}
    938 
    939 	ps.in_decl = ps.decl_level > 0;	/* if we were in a first level
    940 					 * structure declaration before, we
    941 					 * aren't anymore */
    942 
    943 	if (ps.paren.len > 0 && ps.spaced_expr_psym != psym_for_exprs) {
    944 		diag(1, "Unbalanced parentheses");
    945 		ps.paren.len = 0;
    946 		if (ps.spaced_expr_psym != psym_0) {
    947 			parse(ps.spaced_expr_psym);
    948 			ps.spaced_expr_psym = psym_0;
    949 		}
    950 	}
    951 	buf_add_char(&code, ';');
    952 	ps.want_blank = true;
    953 	ps.in_stmt_or_decl = ps.paren.len > 0;
    954 	ps.decl_ind = 0;
    955 
    956 	if (ps.spaced_expr_psym == psym_0) {
    957 		parse(psym_stmt);
    958 		ps.newline = nl_yes;
    959 	}
    960 }
    961 
    962 static void
    963 process_type_outside_parentheses(void)
    964 {
    965 	parse(psym_decl);	/* let the parser worry about indentation */
    966 
    967 	if (ps.prev_lsym == lsym_rparen && ps.psyms.len <= 2 && code.len > 0)
    968 		output_line();
    969 
    970 	if (ps.in_func_def_params && opt.indent_parameters &&
    971 	    ps.decl_level == 0) {
    972 		ps.ind_level = ps.ind_level_follow = 1;
    973 		ps.line_is_stmt_cont = false;
    974 	}
    975 
    976 	ps.in_var_decl = /* maybe */ true;
    977 	ps.in_decl = true;
    978 	ps.line_has_decl = ps.in_decl;
    979 	if (ps.decl_level == 0)
    980 		ps.declaration = decl_begin;
    981 
    982 	int ind = ps.ind_level > 0 && ps.decl_level == 0
    983 	    ? opt.local_decl_indent	/* local variable */
    984 	    : opt.decl_indent;	/* global variable, or member */
    985 	if (ind == 0) {
    986 		int ind0 = code.len > 0 ? ind_add(0, code.s, code.len) + 1 : 0;
    987 		ps.decl_ind = ind_add(ind0, token.s, token.len) + 1;
    988 	} else
    989 		ps.decl_ind = ind;
    990 	ps.tabs_to_var = opt.use_tabs && ind > 0;
    991 }
    992 
    993 static void
    994 process_word(lexer_symbol lsym)
    995 {
    996 	if (lsym == lsym_type	/* in parentheses */
    997 	    && ps.paren.item[ps.paren.len - 1].cast == cast_unknown)
    998 		ps.paren.item[ps.paren.len - 1].cast = cast_maybe;
    999 
   1000 	if (ps.in_decl) {
   1001 		if (lsym == lsym_funcname) {
   1002 			ps.in_decl = false;
   1003 			if (opt.procnames_start_line
   1004 			    && code.len > (*in.p == ')' ? 1 : 0))
   1005 				output_line();
   1006 			else if (ps.want_blank)
   1007 				buf_add_char(&code, ' ');
   1008 			ps.want_blank = false;
   1009 		} else if (ps.in_typedef_decl && ps.decl_level == 0) {
   1010 			/* Do not indent typedef declarators. */
   1011 		} else if (!ps.in_init && !ps.decl_indent_done &&
   1012 		    ps.ind_paren_level == 0) {
   1013 			if (opt.decl_indent == 0
   1014 			    && code.len > 0 && code.s[code.len - 1] == '}')
   1015 				ps.decl_ind = ind_add(0, code.s, code.len) + 1;
   1016 			indent_declarator(ps.decl_ind, ps.tabs_to_var);
   1017 		}
   1018 
   1019 	} else if (ps.spaced_expr_psym != psym_0 && ps.paren.len == 0) {
   1020 		parse(ps.spaced_expr_psym);
   1021 		ps.spaced_expr_psym = psym_0;
   1022 		ps.newline = nl_unless_lbrace;
   1023 		ps.in_stmt_or_decl = false;
   1024 		ps.next_unary = true;
   1025 	}
   1026 }
   1027 
   1028 static void
   1029 process_do(void)
   1030 {
   1031 	ps.in_stmt_or_decl = false;
   1032 	ps.in_decl = false;
   1033 
   1034 	if (code.len > 0)
   1035 		output_line();
   1036 
   1037 	parse(psym_do);
   1038 	ps.newline = nl_unless_lbrace;
   1039 }
   1040 
   1041 static void
   1042 process_else(void)
   1043 {
   1044 	ps.in_stmt_or_decl = false;
   1045 	ps.in_decl = false;
   1046 
   1047 	if (code.len > 0
   1048 	    && !(opt.cuddle_else && code.s[code.len - 1] == '}'))
   1049 		output_line();
   1050 
   1051 	parse(psym_else);
   1052 	ps.newline = opt.else_if_in_same_line ? nl_unless_if : nl_yes;
   1053 }
   1054 
   1055 static void
   1056 process_lsym(lexer_symbol lsym)
   1057 {
   1058 	switch (lsym) {
   1059 		/* INDENT OFF */
   1060 	case lsym_preprocessing: process_preprocessing(); break;
   1061 	case lsym_newline:	process_newline();	break;
   1062 	case lsym_comment:	process_comment();	break;
   1063 	case lsym_lparen:	process_lparen();	break;
   1064 	case lsym_lbracket:	process_lbracket();	break;
   1065 	case lsym_rparen:	process_rparen();	break;
   1066 	case lsym_rbracket:	process_rbracket();	break;
   1067 	case lsym_lbrace:	process_lbrace();	break;
   1068 	case lsym_rbrace:	process_rbrace();	break;
   1069 	case lsym_period:	process_period();	break;
   1070 	case lsym_unary_op:	process_unary_op();	break;
   1071 	case lsym_postfix_op:	process_postfix_op();	break;
   1072 	case lsym_binary_op:				goto copy_token;
   1073 	case lsym_question:	ps.quest_level++;	goto copy_token;
   1074 	case lsym_question_colon:			goto copy_token;
   1075 	case lsym_label_colon:	process_label_colon();	break;
   1076 	case lsym_other_colon:	process_other_colon();	break;
   1077 	case lsym_comma:	process_comma();	break;
   1078 	case lsym_semicolon:	process_semicolon();	break;
   1079 	case lsym_typedef:	ps.in_typedef_decl = true; goto copy_token;
   1080 	case lsym_modifier:				goto copy_token;
   1081 	case lsym_case:		ps.seen_case = true;	goto copy_token;
   1082 	case lsym_default:	ps.seen_case = true;	goto copy_token;
   1083 	case lsym_do:		process_do();		goto copy_token;
   1084 	case lsym_else:		process_else();		goto copy_token;
   1085 	case lsym_for:		ps.spaced_expr_psym = psym_for_exprs; goto copy_token;
   1086 	case lsym_if:		ps.spaced_expr_psym = psym_if_expr; goto copy_token;
   1087 	case lsym_switch:	ps.spaced_expr_psym = psym_switch_expr; goto copy_token;
   1088 	case lsym_while:	ps.spaced_expr_psym = psym_while_expr; goto copy_token;
   1089 		/* INDENT ON */
   1090 
   1091 	case lsym_tag:
   1092 		if (ps.paren.len > 0)
   1093 			goto copy_token;
   1094 		/* FALLTHROUGH */
   1095 	case lsym_type:
   1096 		if (ps.paren.len == 0) {
   1097 			process_type_outside_parentheses();
   1098 			goto copy_token;
   1099 		}
   1100 		/* FALLTHROUGH */
   1101 	case lsym_sizeof:
   1102 	case lsym_offsetof:
   1103 	case lsym_word:
   1104 	case lsym_funcname:
   1105 	case lsym_return:
   1106 		process_word(lsym);
   1107 copy_token:
   1108 		if (ps.want_blank)
   1109 			buf_add_char(&code, ' ');
   1110 		buf_add_buf(&code, &token);
   1111 		if (lsym != lsym_funcname)
   1112 			ps.want_blank = true;
   1113 		break;
   1114 
   1115 	default:
   1116 		break;
   1117 	}
   1118 }
   1119 
   1120 static int
   1121 indent(void)
   1122 {
   1123 	debug_parser_state();
   1124 
   1125 	for (;;) {
   1126 		lexer_symbol lsym = lexi();
   1127 
   1128 		debug_blank_line();
   1129 		debug_printf("line %s:%d: next token is %s",
   1130 		    in_name, in.token_start_line, lsym_name[lsym]);
   1131 		debug_print_buf("with text", &token);
   1132 		debug_println("");
   1133 		if (lab.len > 0 || code.len > 0 || com.len > 0)
   1134 			debug_buffers("the buffers contain");
   1135 		debug_blank_line();
   1136 
   1137 		if (lsym == lsym_eof)
   1138 			return process_eof();
   1139 
   1140 		if (lsym == lsym_preprocessing || lsym == lsym_newline)
   1141 			ps.newline = nl_no;
   1142 		else if (lsym == lsym_comment) {
   1143 			/* no special processing */
   1144 		} else {
   1145 			if (should_break_line(lsym)) {
   1146 				ps.newline = nl_no;
   1147 				output_line();
   1148 			}
   1149 			ps.in_stmt_or_decl = true;
   1150 			if (com.len > 0)
   1151 				move_com_to_code(lsym);
   1152 			update_ps_lbrace_kind(lsym);
   1153 		}
   1154 
   1155 		process_lsym(lsym);
   1156 
   1157 		if (opt.blank_line_after_decl_at_top)
   1158 			update_ps_badp(lsym);
   1159 		if (lsym != lsym_preprocessing
   1160 		    && lsym != lsym_newline
   1161 		    && lsym != lsym_comment)
   1162 			ps.prev_lsym = lsym;
   1163 
   1164 		debug_parser_state();
   1165 	}
   1166 }
   1167 
   1168 int
   1169 main(int argc, char **argv)
   1170 {
   1171 	init_globals();
   1172 	load_profiles(argc, argv);
   1173 	parse_command_line(argc, argv);
   1174 	set_initial_indentation();
   1175 	return indent();
   1176 }
   1177