2021-03-14 04:44:37 +03:00
|
|
|
/* $NetBSD: io.c,v 1.49 2021/03/14 01:44:37 rillig Exp $ */
|
1997-01-09 23:18:21 +03:00
|
|
|
|
2019-04-04 18:22:13 +03:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-4-Clause
|
2003-08-07 15:13:06 +04:00
|
|
|
*
|
1997-10-18 20:04:21 +04:00
|
|
|
* Copyright (c) 1985 Sun Microsystems, Inc.
|
2019-04-04 18:22:13 +03:00
|
|
|
* Copyright (c) 1980, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-04-09 16:58:42 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-18 20:04:21 +04:00
|
|
|
#if 0
|
2019-04-04 18:22:13 +03:00
|
|
|
#ifndef lint
|
1997-10-18 20:04:21 +04:00
|
|
|
static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93";
|
2019-04-04 18:22:13 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
|
|
|
#if defined(__NetBSD__)
|
2021-03-14 04:44:37 +03:00
|
|
|
__RCSID("$NetBSD: io.c,v 1.49 2021/03/14 01:44:37 rillig Exp $");
|
2019-04-04 18:22:13 +03:00
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
__FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
|
|
|
|
#endif
|
1997-10-18 20:04:21 +04:00
|
|
|
#endif
|
1993-04-09 16:58:42 +04:00
|
|
|
|
|
|
|
#include <ctype.h>
|
1997-10-19 07:17:12 +04:00
|
|
|
#include <err.h>
|
|
|
|
#include <stdio.h>
|
1993-04-09 16:58:42 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2019-10-19 18:44:31 +03:00
|
|
|
#include <stdarg.h>
|
2021-03-07 13:42:48 +03:00
|
|
|
|
2019-04-04 18:22:13 +03:00
|
|
|
#include "indent.h"
|
1993-04-09 16:58:42 +04:00
|
|
|
|
2019-04-04 18:22:13 +03:00
|
|
|
int comment_open;
|
2021-03-13 02:16:00 +03:00
|
|
|
static int paren_indent;
|
1993-04-09 16:58:42 +04:00
|
|
|
|
2021-03-12 22:11:29 +03:00
|
|
|
static void
|
|
|
|
output_char(char ch)
|
|
|
|
{
|
|
|
|
fputc(ch, output);
|
2021-03-13 12:21:57 +03:00
|
|
|
debug_vis_range("output_char '", &ch, &ch + 1, "'\n");
|
2021-03-12 22:11:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
output_range(const char *s, const char *e)
|
|
|
|
{
|
|
|
|
fwrite(s, 1, (size_t)(e - s), output);
|
2021-03-13 12:21:57 +03:00
|
|
|
debug_vis_range("output_range \"", s, e, "\"\n");
|
2021-03-12 22:11:29 +03:00
|
|
|
}
|
|
|
|
|
2021-03-12 22:14:18 +03:00
|
|
|
static inline void
|
2021-03-12 22:11:29 +03:00
|
|
|
output_string(const char *s)
|
|
|
|
{
|
|
|
|
output_range(s, s + strlen(s));
|
|
|
|
}
|
|
|
|
|
2021-03-13 03:26:56 +03:00
|
|
|
static int
|
|
|
|
output_indent(int old_ind, int new_ind)
|
|
|
|
{
|
|
|
|
int ind = old_ind;
|
|
|
|
|
|
|
|
if (opt.use_tabs) {
|
|
|
|
int tabsize = opt.tabsize;
|
|
|
|
int n = new_ind / tabsize - ind / tabsize;
|
|
|
|
if (n > 0)
|
|
|
|
ind -= ind % tabsize;
|
|
|
|
for (int i = 0; i < n; i++) {
|
2021-03-13 12:21:57 +03:00
|
|
|
fputc('\t', output);
|
2021-03-13 03:26:56 +03:00
|
|
|
ind += tabsize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; ind < new_ind; ind++)
|
2021-03-13 12:21:57 +03:00
|
|
|
fputc(' ', output);
|
2021-03-13 03:26:56 +03:00
|
|
|
|
2021-03-13 12:21:57 +03:00
|
|
|
debug_println("output_indent %d", ind);
|
2021-03-13 03:26:56 +03:00
|
|
|
return ind;
|
|
|
|
}
|
|
|
|
|
2021-03-09 01:26:17 +03:00
|
|
|
/*
|
|
|
|
* dump_line is the routine that actually effects the printing of the new
|
|
|
|
* source. It prints the label section, followed by the code section with
|
|
|
|
* the appropriate nesting level, followed by any comments.
|
|
|
|
*/
|
1997-10-19 07:17:12 +04:00
|
|
|
void
|
2002-05-27 02:53:38 +04:00
|
|
|
dump_line(void)
|
2021-03-09 01:26:17 +03:00
|
|
|
{
|
2021-03-13 16:54:01 +03:00
|
|
|
int cur_col;
|
2019-04-04 18:22:13 +03:00
|
|
|
static int not_first_line;
|
|
|
|
|
|
|
|
if (ps.procname[0]) {
|
|
|
|
ps.ind_level = 0;
|
|
|
|
ps.procname[0] = 0;
|
|
|
|
}
|
2021-03-13 02:10:18 +03:00
|
|
|
|
2019-04-04 18:22:13 +03:00
|
|
|
if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
|
|
|
|
if (suppress_blanklines > 0)
|
|
|
|
suppress_blanklines--;
|
|
|
|
else {
|
|
|
|
ps.bl_line = true;
|
|
|
|
n_real_blanklines++;
|
1997-10-19 07:17:12 +04:00
|
|
|
}
|
2021-03-13 02:10:18 +03:00
|
|
|
} else if (!inhibit_formatting) {
|
2019-04-04 18:22:13 +03:00
|
|
|
suppress_blanklines = 0;
|
|
|
|
ps.bl_line = false;
|
|
|
|
if (prefix_blankline_requested && not_first_line) {
|
|
|
|
if (opt.swallow_optional_blanklines) {
|
|
|
|
if (n_real_blanklines == 1)
|
|
|
|
n_real_blanklines = 0;
|
2021-03-13 02:10:18 +03:00
|
|
|
} else {
|
2019-04-04 18:22:13 +03:00
|
|
|
if (n_real_blanklines == 0)
|
|
|
|
n_real_blanklines = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (--n_real_blanklines >= 0)
|
2021-03-12 22:11:29 +03:00
|
|
|
output_char('\n');
|
2019-04-04 18:22:13 +03:00
|
|
|
n_real_blanklines = 0;
|
|
|
|
if (ps.ind_level == 0)
|
|
|
|
ps.ind_stmt = 0; /* this is a class A kludge. dont do
|
|
|
|
* additional statement indentation if we are
|
|
|
|
* at bracket level 0 */
|
|
|
|
|
|
|
|
if (e_lab != s_lab || e_code != s_code)
|
|
|
|
++code_lines; /* keep count of lines with code */
|
|
|
|
|
|
|
|
|
|
|
|
if (e_lab != s_lab) { /* print lab, if any */
|
|
|
|
if (comment_open) {
|
|
|
|
comment_open = 0;
|
2021-03-12 22:11:29 +03:00
|
|
|
output_string(".*/\n");
|
2019-04-04 18:22:13 +03:00
|
|
|
}
|
|
|
|
while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
|
|
|
|
e_lab--;
|
|
|
|
*e_lab = '\0';
|
2021-03-13 12:54:11 +03:00
|
|
|
cur_col = 1 + output_indent(0, compute_label_indent());
|
2019-04-04 18:22:13 +03:00
|
|
|
if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0
|
|
|
|
|| strncmp(s_lab, "#endif", 6) == 0)) {
|
|
|
|
char *s = s_lab;
|
|
|
|
if (e_lab[-1] == '\n') e_lab--;
|
2021-03-09 01:23:58 +03:00
|
|
|
do {
|
2021-03-12 22:11:29 +03:00
|
|
|
output_char(*s++);
|
2021-03-09 01:23:58 +03:00
|
|
|
} while (s < e_lab && 'a' <= *s && *s <= 'z');
|
2019-04-04 18:22:13 +03:00
|
|
|
while ((*s == ' ' || *s == '\t') && s < e_lab)
|
|
|
|
s++;
|
2021-03-12 22:11:29 +03:00
|
|
|
if (s < e_lab) {
|
|
|
|
if (s[0] == '/' && s[1] == '*') {
|
|
|
|
output_char('\t');
|
|
|
|
output_range(s, e_lab);
|
|
|
|
} else {
|
|
|
|
output_string("\t/* ");
|
|
|
|
output_range(s, e_lab);
|
|
|
|
output_string(" */");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
output_range(s_lab, e_lab);
|
2021-03-13 13:32:25 +03:00
|
|
|
cur_col = 1 + indentation_after(cur_col - 1, s_lab);
|
2021-03-13 02:10:18 +03:00
|
|
|
} else
|
2019-04-04 18:22:13 +03:00
|
|
|
cur_col = 1; /* there is no label section */
|
|
|
|
|
|
|
|
ps.pcase = false;
|
|
|
|
|
|
|
|
if (s_code != e_code) { /* print code section, if any */
|
|
|
|
if (comment_open) {
|
|
|
|
comment_open = 0;
|
2021-03-12 22:11:29 +03:00
|
|
|
output_string(".*/\n");
|
2019-04-04 18:22:13 +03:00
|
|
|
}
|
2021-03-13 16:54:01 +03:00
|
|
|
int target_col = 1 + compute_code_indent();
|
2019-04-04 18:22:13 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2021-03-13 12:21:57 +03:00
|
|
|
for (i = 0; i < ps.p_l_follow; i++) {
|
|
|
|
if (ps.paren_indents[i] >= 0) {
|
|
|
|
int ind = ps.paren_indents[i];
|
|
|
|
/*
|
|
|
|
* XXX: this mix of 'indent' and 'column' smells like
|
|
|
|
* an off-by-one error.
|
|
|
|
*/
|
|
|
|
ps.paren_indents[i] = -(ind + target_col);
|
|
|
|
debug_println(
|
|
|
|
"setting pi[%d] from %d to %d for column %d",
|
|
|
|
i, ind, ps.paren_indents[i], target_col);
|
|
|
|
}
|
|
|
|
}
|
2019-04-04 18:22:13 +03:00
|
|
|
}
|
2021-03-13 03:26:56 +03:00
|
|
|
cur_col = 1 + output_indent(cur_col - 1, target_col - 1);
|
2021-03-13 12:06:12 +03:00
|
|
|
output_range(s_code, e_code);
|
2021-03-13 13:32:25 +03:00
|
|
|
cur_col = 1 + indentation_after(cur_col - 1, s_code);
|
2019-04-04 18:22:13 +03:00
|
|
|
}
|
|
|
|
if (s_com != e_com) { /* print comment, if any */
|
2021-03-13 16:55:42 +03:00
|
|
|
int target_col = ps.com_col;
|
2019-04-04 18:22:13 +03:00
|
|
|
char *com_st = s_com;
|
|
|
|
|
2021-03-13 16:55:42 +03:00
|
|
|
target_col += ps.comment_delta;
|
2019-04-04 18:22:13 +03:00
|
|
|
while (*com_st == '\t') /* consider original indentation in
|
2021-03-13 21:24:56 +03:00
|
|
|
* case this is a box comment */
|
2021-03-13 16:55:42 +03:00
|
|
|
com_st++, target_col += opt.tabsize;
|
|
|
|
while (target_col <= 0)
|
2019-04-04 18:22:13 +03:00
|
|
|
if (*com_st == ' ')
|
2021-03-13 16:55:42 +03:00
|
|
|
target_col++, com_st++;
|
2019-04-04 18:22:13 +03:00
|
|
|
else if (*com_st == '\t') {
|
2021-03-13 16:55:42 +03:00
|
|
|
target_col = opt.tabsize * (1 + (target_col - 1) / opt.tabsize) + 1;
|
2019-04-04 18:22:13 +03:00
|
|
|
com_st++;
|
2021-03-13 02:10:18 +03:00
|
|
|
} else
|
2021-03-13 16:55:42 +03:00
|
|
|
target_col = 1;
|
|
|
|
if (cur_col > target_col) { /* if comment can't fit on this line,
|
2021-03-13 21:24:56 +03:00
|
|
|
* put it on next line */
|
2021-03-12 22:11:29 +03:00
|
|
|
output_char('\n');
|
2019-04-04 18:22:13 +03:00
|
|
|
cur_col = 1;
|
|
|
|
++ps.out_lines;
|
|
|
|
}
|
|
|
|
while (e_com > com_st && isspace((unsigned char)e_com[-1]))
|
|
|
|
e_com--;
|
2021-03-13 16:55:42 +03:00
|
|
|
(void)output_indent(cur_col - 1, target_col - 1);
|
2021-03-12 22:11:29 +03:00
|
|
|
output_range(com_st, e_com);
|
2019-04-04 18:22:13 +03:00
|
|
|
ps.comment_delta = ps.n_comment_delta;
|
|
|
|
++ps.com_lines; /* count lines with comments */
|
|
|
|
}
|
|
|
|
if (ps.use_ff)
|
2021-03-12 22:11:29 +03:00
|
|
|
output_char('\014');
|
2019-04-04 18:22:13 +03:00
|
|
|
else
|
2021-03-12 22:11:29 +03:00
|
|
|
output_char('\n');
|
2019-04-04 18:22:13 +03:00
|
|
|
++ps.out_lines;
|
|
|
|
if (ps.just_saw_decl == 1 && opt.blanklines_after_declarations) {
|
|
|
|
prefix_blankline_requested = 1;
|
|
|
|
ps.just_saw_decl = 0;
|
2021-03-13 02:10:18 +03:00
|
|
|
} else
|
2019-04-04 18:22:13 +03:00
|
|
|
prefix_blankline_requested = postfix_blankline_requested;
|
|
|
|
postfix_blankline_requested = 0;
|
|
|
|
}
|
2021-03-08 01:11:01 +03:00
|
|
|
|
|
|
|
/* keep blank lines after '//' comments */
|
|
|
|
if (e_com - s_com > 1 && s_com[1] == '/')
|
2021-03-12 22:11:29 +03:00
|
|
|
output_range(s_token, e_token);
|
2021-03-08 01:11:01 +03:00
|
|
|
|
2021-03-13 21:24:56 +03:00
|
|
|
ps.decl_on_line = ps.in_decl; /* if we are in the middle of a declaration,
|
|
|
|
* remember that fact for proper comment
|
|
|
|
* indentation */
|
|
|
|
ps.ind_stmt = ps.in_stmt & ~ps.in_decl; /* next line should be indented if
|
|
|
|
* we have not completed this stmt and if we
|
|
|
|
* are not in the middle of a declaration */
|
2019-04-04 18:22:13 +03:00
|
|
|
ps.use_ff = false;
|
|
|
|
ps.dumped_decl_indent = 0;
|
|
|
|
*(e_lab = s_lab) = '\0'; /* reset buffers */
|
|
|
|
*(e_code = s_code) = '\0';
|
|
|
|
*(e_com = s_com = combuf + 1) = '\0';
|
|
|
|
ps.ind_level = ps.i_l_follow;
|
|
|
|
ps.paren_level = ps.p_l_follow;
|
2021-03-13 12:21:57 +03:00
|
|
|
if (ps.paren_level > 0) {
|
|
|
|
/* TODO: explain what negative indentation means */
|
2021-03-13 02:16:00 +03:00
|
|
|
paren_indent = -ps.paren_indents[ps.paren_level - 1];
|
2021-03-13 12:21:57 +03:00
|
|
|
debug_println("paren_indent is now %d", paren_indent);
|
|
|
|
}
|
2019-04-04 18:22:13 +03:00
|
|
|
not_first_line = 1;
|
1993-04-09 16:58:42 +04:00
|
|
|
}
|
|
|
|
|
1997-10-19 07:17:12 +04:00
|
|
|
int
|
2021-03-13 13:06:47 +03:00
|
|
|
compute_code_indent(void)
|
1993-04-09 16:58:42 +04:00
|
|
|
{
|
2021-03-13 16:51:08 +03:00
|
|
|
int target_ind = opt.indent_size * ps.ind_level;
|
2019-04-04 18:22:13 +03:00
|
|
|
|
2021-03-13 13:06:47 +03:00
|
|
|
if (ps.paren_level != 0) {
|
2019-04-04 18:22:13 +03:00
|
|
|
if (!opt.lineup_to_parens)
|
2021-03-14 04:44:37 +03:00
|
|
|
if (2 * opt.continuation_indent == opt.indent_size)
|
|
|
|
target_ind += opt.continuation_indent;
|
|
|
|
else
|
|
|
|
target_ind += opt.continuation_indent * ps.paren_level;
|
2019-04-04 18:22:13 +03:00
|
|
|
else if (opt.lineup_to_parens_always)
|
2021-03-13 13:06:47 +03:00
|
|
|
/*
|
|
|
|
* XXX: where does this '- 1' come from? It looks strange but is
|
|
|
|
* nevertheless needed for proper indentation, as demonstrated in
|
|
|
|
* the test opt-lpl.0.
|
|
|
|
*/
|
|
|
|
target_ind = paren_indent - 1;
|
2019-04-04 18:22:13 +03:00
|
|
|
else {
|
|
|
|
int w;
|
2021-03-13 02:16:00 +03:00
|
|
|
int t = paren_indent;
|
2019-04-04 18:22:13 +03:00
|
|
|
|
2021-03-13 14:19:43 +03:00
|
|
|
if ((w = 1 + indentation_after(t - 1, s_code) - opt.max_line_length) > 0
|
|
|
|
&& 1 + indentation_after(target_ind, s_code) <= opt.max_line_length) {
|
2019-04-04 18:22:13 +03:00
|
|
|
t -= w + 1;
|
2021-03-13 13:06:47 +03:00
|
|
|
if (t > target_ind + 1)
|
|
|
|
target_ind = t - 1;
|
2021-03-13 02:10:18 +03:00
|
|
|
} else
|
2021-03-13 13:06:47 +03:00
|
|
|
target_ind = t - 1;
|
2019-04-04 18:22:13 +03:00
|
|
|
}
|
2021-03-13 02:10:18 +03:00
|
|
|
} else if (ps.ind_stmt)
|
2021-03-13 13:06:47 +03:00
|
|
|
target_ind += opt.continuation_indent;
|
|
|
|
return target_ind;
|
1993-04-09 16:58:42 +04:00
|
|
|
}
|
|
|
|
|
1997-10-19 07:17:12 +04:00
|
|
|
int
|
2021-03-13 12:54:11 +03:00
|
|
|
compute_label_indent(void)
|
1993-04-09 16:58:42 +04:00
|
|
|
{
|
2021-03-13 12:54:11 +03:00
|
|
|
if (ps.pcase)
|
2021-03-13 16:51:08 +03:00
|
|
|
return (int) (case_ind * opt.indent_size);
|
2021-03-13 12:54:11 +03:00
|
|
|
if (s_lab[0] == '#')
|
|
|
|
return 0;
|
2021-03-13 16:51:08 +03:00
|
|
|
return opt.indent_size * (ps.ind_level - label_offset);
|
1993-04-09 16:58:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 1976 by the Board of Trustees of the University of Illinois
|
1997-10-19 07:17:12 +04:00
|
|
|
*
|
1993-04-09 16:58:42 +04:00
|
|
|
* All rights reserved
|
1997-10-19 07:17:12 +04:00
|
|
|
*
|
2021-03-13 03:03:29 +03:00
|
|
|
* FUNCTION: Reads one block of input into the input buffer
|
1993-04-09 16:58:42 +04:00
|
|
|
*/
|
1997-10-19 07:17:12 +04:00
|
|
|
void
|
2002-05-27 02:53:38 +04:00
|
|
|
fill_buffer(void)
|
1993-04-09 16:58:42 +04:00
|
|
|
{ /* this routine reads stuff from the input */
|
2019-04-04 18:22:13 +03:00
|
|
|
char *p;
|
|
|
|
int i;
|
|
|
|
FILE *f = input;
|
|
|
|
|
|
|
|
if (bp_save != NULL) { /* there is a partly filled input buffer left */
|
|
|
|
buf_ptr = bp_save; /* do not read anything, just switch buffers */
|
|
|
|
buf_end = be_save;
|
|
|
|
bp_save = be_save = NULL;
|
2021-03-13 21:46:39 +03:00
|
|
|
debug_println("switched buf_ptr back to bp_save");
|
2019-04-04 18:22:13 +03:00
|
|
|
if (buf_ptr < buf_end)
|
|
|
|
return; /* only return if there is really something in
|
1993-04-09 16:58:42 +04:00
|
|
|
* this buffer */
|
2019-04-04 18:22:13 +03:00
|
|
|
}
|
|
|
|
for (p = in_buffer;;) {
|
|
|
|
if (p >= in_buffer_limit) {
|
2021-03-14 03:22:16 +03:00
|
|
|
size_t size = (in_buffer_limit - in_buffer) * 2 + 10;
|
|
|
|
size_t offset = p - in_buffer;
|
2019-04-04 18:22:13 +03:00
|
|
|
in_buffer = realloc(in_buffer, size);
|
|
|
|
if (in_buffer == NULL)
|
|
|
|
errx(1, "input line too long");
|
|
|
|
p = in_buffer + offset;
|
|
|
|
in_buffer_limit = in_buffer + size - 2;
|
1993-04-09 16:58:42 +04:00
|
|
|
}
|
2019-04-04 18:22:13 +03:00
|
|
|
if ((i = getc(f)) == EOF) {
|
2021-03-13 12:48:04 +03:00
|
|
|
*p++ = ' ';
|
|
|
|
*p++ = '\n';
|
|
|
|
had_eof = true;
|
|
|
|
break;
|
1993-04-09 16:58:42 +04:00
|
|
|
}
|
2019-04-04 18:22:13 +03:00
|
|
|
if (i != '\0')
|
|
|
|
*p++ = i;
|
|
|
|
if (i == '\n')
|
2021-03-13 12:48:04 +03:00
|
|
|
break;
|
2019-04-04 18:22:13 +03:00
|
|
|
}
|
|
|
|
buf_ptr = in_buffer;
|
|
|
|
buf_end = p;
|
|
|
|
if (p - in_buffer > 2 && p[-2] == '/' && p[-3] == '*') {
|
|
|
|
if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
|
|
|
|
fill_buffer(); /* flush indent error message */
|
|
|
|
else {
|
2021-03-13 12:48:04 +03:00
|
|
|
int com = 0;
|
2019-04-04 18:22:13 +03:00
|
|
|
|
|
|
|
p = in_buffer;
|
|
|
|
while (*p == ' ' || *p == '\t')
|
|
|
|
p++;
|
|
|
|
if (*p == '/' && p[1] == '*') {
|
|
|
|
p += 2;
|
|
|
|
while (*p == ' ' || *p == '\t')
|
|
|
|
p++;
|
|
|
|
if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E'
|
|
|
|
&& p[4] == 'N' && p[5] == 'T') {
|
|
|
|
p += 6;
|
|
|
|
while (*p == ' ' || *p == '\t')
|
|
|
|
p++;
|
|
|
|
if (*p == '*')
|
|
|
|
com = 1;
|
|
|
|
else if (*p == 'O') {
|
|
|
|
if (*++p == 'N')
|
|
|
|
p++, com = 1;
|
|
|
|
else if (*p == 'F' && *++p == 'F')
|
|
|
|
p++, com = 2;
|
|
|
|
}
|
|
|
|
while (*p == ' ' || *p == '\t')
|
|
|
|
p++;
|
|
|
|
if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) {
|
|
|
|
if (s_com != e_com || s_lab != e_lab || s_code != e_code)
|
|
|
|
dump_line();
|
|
|
|
if (!(inhibit_formatting = com - 1)) {
|
|
|
|
n_real_blanklines = 0;
|
|
|
|
postfix_blankline_requested = 0;
|
|
|
|
prefix_blankline_requested = 0;
|
|
|
|
suppress_blanklines = 1;
|
1993-04-09 16:58:42 +04:00
|
|
|
}
|
2019-04-04 18:22:13 +03:00
|
|
|
}
|
1993-04-09 16:58:42 +04:00
|
|
|
}
|
2019-04-04 18:22:13 +03:00
|
|
|
}
|
1993-04-09 16:58:42 +04:00
|
|
|
}
|
2019-04-04 18:22:13 +03:00
|
|
|
}
|
|
|
|
if (inhibit_formatting) {
|
|
|
|
p = in_buffer;
|
2021-03-09 01:23:58 +03:00
|
|
|
do {
|
2021-03-12 22:11:29 +03:00
|
|
|
output_char(*p);
|
2021-03-09 01:23:58 +03:00
|
|
|
} while (*p++ != '\n');
|
2019-04-04 18:22:13 +03:00
|
|
|
}
|
1993-04-09 16:58:42 +04:00
|
|
|
}
|
2019-04-04 18:22:13 +03:00
|
|
|
|
1993-04-09 16:58:42 +04:00
|
|
|
int
|
2021-03-13 13:20:54 +03:00
|
|
|
indentation_after_range(int ind, const char *start, const char *end)
|
1993-04-09 16:58:42 +04:00
|
|
|
{
|
2021-03-13 13:20:54 +03:00
|
|
|
for (const char *p = start; *p != '\0' && p != end; ++p) {
|
|
|
|
if (*p == '\n' || *p == '\f')
|
|
|
|
ind = 0;
|
|
|
|
else if (*p == '\t')
|
|
|
|
ind = opt.tabsize * (ind / opt.tabsize + 1);
|
|
|
|
else if (*p == '\b')
|
|
|
|
--ind;
|
|
|
|
else
|
|
|
|
++ind;
|
|
|
|
}
|
|
|
|
return ind;
|
|
|
|
}
|
2019-04-04 18:22:13 +03:00
|
|
|
|
2021-03-13 13:20:54 +03:00
|
|
|
int
|
|
|
|
indentation_after(int ind, const char *s)
|
|
|
|
{
|
|
|
|
return indentation_after_range(ind, s, NULL);
|
|
|
|
}
|
2019-04-04 18:22:13 +03:00
|
|
|
|
1997-10-19 07:17:12 +04:00
|
|
|
void
|
2019-10-19 18:44:31 +03:00
|
|
|
diag(int level, const char *msg, ...)
|
1993-04-09 16:58:42 +04:00
|
|
|
{
|
2019-10-19 18:44:31 +03:00
|
|
|
va_list ap;
|
|
|
|
const char *s, *e;
|
1993-04-09 16:58:42 +04:00
|
|
|
|
2019-04-04 18:22:13 +03:00
|
|
|
if (level)
|
|
|
|
found_err = 1;
|
1993-04-09 16:58:42 +04:00
|
|
|
|
2019-04-04 18:22:13 +03:00
|
|
|
if (output == stdout) {
|
2019-10-19 18:44:31 +03:00
|
|
|
s = "/**INDENT** ";
|
|
|
|
e = " */";
|
|
|
|
} else {
|
|
|
|
s = e = "";
|
2019-04-04 18:22:13 +03:00
|
|
|
}
|
2019-10-19 18:44:31 +03:00
|
|
|
|
|
|
|
va_start(ap, msg);
|
|
|
|
fprintf(stderr, "%s%s@%d: ", s, level == 0 ? "Warning" : "Error", line_no);
|
|
|
|
vfprintf(stderr, msg, ap);
|
|
|
|
fprintf(stderr, "%s\n", e);
|
|
|
|
va_end(ap);
|
1993-04-09 16:58:42 +04:00
|
|
|
}
|