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