Home | History | Annotate | Line # | Download | only in indent
io.c revision 1.26
      1  1.26    rillig /*	$NetBSD: io.c,v 1.26 2021/03/08 22:26:17 rillig Exp $	*/
      2   1.3       tls 
      3  1.19     kamil /*-
      4  1.19     kamil  * SPDX-License-Identifier: BSD-4-Clause
      5  1.19     kamil  *
      6  1.19     kamil  * Copyright (c) 1985 Sun Microsystems, Inc.
      7   1.4       mrg  * Copyright (c) 1980, 1993
      8   1.4       mrg  *	The Regents of the University of California.  All rights reserved.
      9   1.1       cgd  * All rights reserved.
     10   1.1       cgd  *
     11   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     12   1.1       cgd  * modification, are permitted provided that the following conditions
     13   1.1       cgd  * are met:
     14   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     15   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     16   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     18   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     19   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     20   1.1       cgd  *    must display the following acknowledgement:
     21   1.1       cgd  *	This product includes software developed by the University of
     22   1.1       cgd  *	California, Berkeley and its contributors.
     23   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     24   1.1       cgd  *    may be used to endorse or promote products derived from this software
     25   1.1       cgd  *    without specific prior written permission.
     26   1.1       cgd  *
     27   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37   1.1       cgd  * SUCH DAMAGE.
     38   1.1       cgd  */
     39   1.1       cgd 
     40  1.19     kamil #if 0
     41  1.19     kamil #ifndef lint
     42  1.19     kamil static char sccsid[] = "@(#)io.c	8.1 (Berkeley) 6/6/93";
     43  1.19     kamil #endif /* not lint */
     44  1.19     kamil #endif
     45  1.19     kamil 
     46   1.5     lukem #include <sys/cdefs.h>
     47   1.1       cgd #ifndef lint
     48  1.19     kamil #if defined(__NetBSD__)
     49  1.26    rillig __RCSID("$NetBSD: io.c,v 1.26 2021/03/08 22:26:17 rillig Exp $");
     50  1.19     kamil #elif defined(__FreeBSD__)
     51  1.19     kamil __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
     52  1.19     kamil #endif
     53   1.4       mrg #endif
     54   1.1       cgd 
     55   1.5     lukem #include <ctype.h>
     56   1.5     lukem #include <err.h>
     57   1.1       cgd #include <stdio.h>
     58   1.1       cgd #include <stdlib.h>
     59   1.1       cgd #include <string.h>
     60  1.20  christos #include <stdarg.h>
     61  1.22    rillig 
     62  1.19     kamil #include "indent.h"
     63   1.1       cgd 
     64  1.19     kamil int         comment_open;
     65  1.19     kamil static int  paren_target;
     66  1.19     kamil static int pad_output(int current, int target);
     67   1.1       cgd 
     68  1.26    rillig /*
     69  1.26    rillig  * dump_line is the routine that actually effects the printing of the new
     70  1.26    rillig  * source. It prints the label section, followed by the code section with
     71  1.26    rillig  * the appropriate nesting level, followed by any comments.
     72  1.26    rillig  */
     73   1.5     lukem void
     74  1.11       wiz dump_line(void)
     75  1.26    rillig {
     76  1.19     kamil     int cur_col,
     77  1.19     kamil                 target_col = 1;
     78  1.19     kamil     static int  not_first_line;
     79  1.19     kamil 
     80  1.19     kamil     if (ps.procname[0]) {
     81  1.19     kamil 	ps.ind_level = 0;
     82  1.19     kamil 	ps.procname[0] = 0;
     83  1.19     kamil     }
     84  1.19     kamil     if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
     85  1.19     kamil 	if (suppress_blanklines > 0)
     86  1.19     kamil 	    suppress_blanklines--;
     87  1.19     kamil 	else {
     88  1.19     kamil 	    ps.bl_line = true;
     89  1.19     kamil 	    n_real_blanklines++;
     90  1.19     kamil 	}
     91  1.19     kamil     }
     92  1.19     kamil     else if (!inhibit_formatting) {
     93  1.19     kamil 	suppress_blanklines = 0;
     94  1.19     kamil 	ps.bl_line = false;
     95  1.19     kamil 	if (prefix_blankline_requested && not_first_line) {
     96  1.19     kamil 	    if (opt.swallow_optional_blanklines) {
     97  1.19     kamil 		if (n_real_blanklines == 1)
     98  1.19     kamil 		    n_real_blanklines = 0;
     99  1.19     kamil 	    }
    100  1.19     kamil 	    else {
    101  1.19     kamil 		if (n_real_blanklines == 0)
    102  1.19     kamil 		    n_real_blanklines = 1;
    103  1.19     kamil 	    }
    104  1.19     kamil 	}
    105  1.19     kamil 	while (--n_real_blanklines >= 0)
    106  1.19     kamil 	    putc('\n', output);
    107  1.19     kamil 	n_real_blanklines = 0;
    108  1.19     kamil 	if (ps.ind_level == 0)
    109  1.19     kamil 	    ps.ind_stmt = 0;	/* this is a class A kludge. dont do
    110  1.19     kamil 				 * additional statement indentation if we are
    111  1.19     kamil 				 * at bracket level 0 */
    112  1.19     kamil 
    113  1.19     kamil 	if (e_lab != s_lab || e_code != s_code)
    114  1.19     kamil 	    ++code_lines;	/* keep count of lines with code */
    115  1.19     kamil 
    116  1.19     kamil 
    117  1.19     kamil 	if (e_lab != s_lab) {	/* print lab, if any */
    118  1.19     kamil 	    if (comment_open) {
    119  1.19     kamil 		comment_open = 0;
    120  1.19     kamil 		fprintf(output, ".*/\n");
    121  1.19     kamil 	    }
    122  1.19     kamil 	    while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
    123  1.19     kamil 		e_lab--;
    124  1.19     kamil 	    *e_lab = '\0';
    125  1.19     kamil 	    cur_col = pad_output(1, compute_label_target());
    126  1.19     kamil 	    if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0
    127  1.19     kamil 				    || strncmp(s_lab, "#endif", 6) == 0)) {
    128  1.19     kamil 		char *s = s_lab;
    129  1.19     kamil 		if (e_lab[-1] == '\n') e_lab--;
    130  1.25    rillig 		do {
    131  1.25    rillig 		    putc(*s++, output);
    132  1.25    rillig 		} while (s < e_lab && 'a' <= *s && *s <= 'z');
    133  1.19     kamil 		while ((*s == ' ' || *s == '\t') && s < e_lab)
    134  1.19     kamil 		    s++;
    135  1.19     kamil 		if (s < e_lab)
    136  1.19     kamil 		    fprintf(output, s[0]=='/' && s[1]=='*' ? "\t%.*s" : "\t/* %.*s */",
    137  1.19     kamil 			    (int)(e_lab - s), s);
    138  1.19     kamil 	    }
    139  1.19     kamil 	    else fprintf(output, "%.*s", (int)(e_lab - s_lab), s_lab);
    140  1.19     kamil 	    cur_col = count_spaces(cur_col, s_lab);
    141  1.19     kamil 	}
    142  1.19     kamil 	else
    143  1.19     kamil 	    cur_col = 1;	/* there is no label section */
    144  1.19     kamil 
    145  1.19     kamil 	ps.pcase = false;
    146  1.19     kamil 
    147  1.19     kamil 	if (s_code != e_code) {	/* print code section, if any */
    148  1.19     kamil 	    char *p;
    149   1.1       cgd 
    150  1.19     kamil 	    if (comment_open) {
    151  1.19     kamil 		comment_open = 0;
    152  1.19     kamil 		fprintf(output, ".*/\n");
    153  1.19     kamil 	    }
    154  1.19     kamil 	    target_col = compute_code_target();
    155  1.19     kamil 	    {
    156  1.19     kamil 		int i;
    157  1.19     kamil 
    158  1.19     kamil 		for (i = 0; i < ps.p_l_follow; i++)
    159  1.19     kamil 		    if (ps.paren_indents[i] >= 0)
    160  1.19     kamil 			ps.paren_indents[i] = -(ps.paren_indents[i] + target_col);
    161  1.19     kamil 	    }
    162  1.19     kamil 	    cur_col = pad_output(cur_col, target_col);
    163  1.19     kamil 	    for (p = s_code; p < e_code; p++)
    164  1.19     kamil 		if (*p == (char) 0200)
    165  1.19     kamil 		    fprintf(output, "%d", target_col * 7);
    166  1.19     kamil 		else
    167  1.19     kamil 		    putc(*p, output);
    168  1.19     kamil 	    cur_col = count_spaces(cur_col, s_code);
    169  1.19     kamil 	}
    170  1.19     kamil 	if (s_com != e_com) {		/* print comment, if any */
    171  1.19     kamil 	    int target = ps.com_col;
    172  1.19     kamil 	    char *com_st = s_com;
    173  1.19     kamil 
    174  1.19     kamil 	    target += ps.comment_delta;
    175  1.19     kamil 	    while (*com_st == '\t')	/* consider original indentation in
    176  1.19     kamil 				     * case this is a box comment */
    177  1.19     kamil 		com_st++, target += opt.tabsize;
    178  1.19     kamil 	    while (target <= 0)
    179  1.19     kamil 		if (*com_st == ' ')
    180  1.19     kamil 		    target++, com_st++;
    181  1.19     kamil 		else if (*com_st == '\t') {
    182  1.19     kamil 		    target = opt.tabsize * (1 + (target - 1) / opt.tabsize) + 1;
    183  1.19     kamil 		    com_st++;
    184   1.5     lukem 		}
    185  1.19     kamil 		else
    186  1.19     kamil 		    target = 1;
    187  1.19     kamil 	    if (cur_col > target) {	/* if comment can't fit on this line,
    188  1.19     kamil 				     * put it on next line */
    189  1.19     kamil 		putc('\n', output);
    190  1.19     kamil 		cur_col = 1;
    191  1.19     kamil 		++ps.out_lines;
    192  1.19     kamil 	    }
    193  1.19     kamil 	    while (e_com > com_st && isspace((unsigned char)e_com[-1]))
    194  1.19     kamil 		e_com--;
    195  1.19     kamil 	    (void)pad_output(cur_col, target);
    196  1.19     kamil 	    fwrite(com_st, e_com - com_st, 1, output);
    197  1.19     kamil 	    ps.comment_delta = ps.n_comment_delta;
    198  1.19     kamil 	    ++ps.com_lines;	/* count lines with comments */
    199  1.19     kamil 	}
    200  1.19     kamil 	if (ps.use_ff)
    201  1.19     kamil 	    putc('\014', output);
    202  1.19     kamil 	else
    203  1.19     kamil 	    putc('\n', output);
    204  1.19     kamil 	++ps.out_lines;
    205  1.19     kamil 	if (ps.just_saw_decl == 1 && opt.blanklines_after_declarations) {
    206  1.19     kamil 	    prefix_blankline_requested = 1;
    207  1.19     kamil 	    ps.just_saw_decl = 0;
    208   1.1       cgd 	}
    209  1.19     kamil 	else
    210  1.19     kamil 	    prefix_blankline_requested = postfix_blankline_requested;
    211  1.19     kamil 	postfix_blankline_requested = 0;
    212  1.19     kamil     }
    213  1.24    rillig 
    214  1.24    rillig     /* keep blank lines after '//' comments */
    215  1.24    rillig     if (e_com - s_com > 1 && s_com[1] == '/')
    216  1.24    rillig 	fprintf(output, "%.*s", (int)(e_token - s_token), s_token);
    217  1.24    rillig 
    218  1.19     kamil     ps.decl_on_line = ps.in_decl;	/* if we are in the middle of a
    219   1.1       cgd 					 * declaration, remember that fact for
    220   1.1       cgd 					 * proper comment indentation */
    221  1.19     kamil     ps.ind_stmt = ps.in_stmt & ~ps.in_decl;	/* next line should be
    222   1.1       cgd 						 * indented if we have not
    223   1.1       cgd 						 * completed this stmt and if
    224   1.1       cgd 						 * we are not in the middle of
    225   1.1       cgd 						 * a declaration */
    226  1.19     kamil     ps.use_ff = false;
    227  1.19     kamil     ps.dumped_decl_indent = 0;
    228  1.19     kamil     *(e_lab = s_lab) = '\0';	/* reset buffers */
    229  1.19     kamil     *(e_code = s_code) = '\0';
    230  1.19     kamil     *(e_com = s_com = combuf + 1) = '\0';
    231  1.19     kamil     ps.ind_level = ps.i_l_follow;
    232  1.19     kamil     ps.paren_level = ps.p_l_follow;
    233  1.19     kamil     if (ps.paren_level > 0)
    234   1.5     lukem 	paren_target = -ps.paren_indents[ps.paren_level - 1];
    235  1.19     kamil     not_first_line = 1;
    236   1.1       cgd }
    237   1.1       cgd 
    238   1.5     lukem int
    239  1.11       wiz compute_code_target(void)
    240   1.1       cgd {
    241  1.19     kamil     int target_col = opt.ind_size * ps.ind_level + 1;
    242  1.19     kamil 
    243  1.19     kamil     if (ps.paren_level)
    244  1.19     kamil 	if (!opt.lineup_to_parens)
    245  1.19     kamil 	    target_col += opt.continuation_indent *
    246  1.19     kamil 		(2 * opt.continuation_indent == opt.ind_size ? 1 : ps.paren_level);
    247  1.19     kamil 	else if (opt.lineup_to_parens_always)
    248  1.19     kamil 	    target_col = paren_target;
    249  1.19     kamil 	else {
    250  1.19     kamil 	    int w;
    251  1.19     kamil 	    int t = paren_target;
    252   1.1       cgd 
    253  1.19     kamil 	    if ((w = count_spaces(t, s_code) - opt.max_col) > 0
    254  1.19     kamil 		    && count_spaces(target_col, s_code) <= opt.max_col) {
    255  1.19     kamil 		t -= w + 1;
    256  1.19     kamil 		if (t > target_col)
    257  1.19     kamil 		    target_col = t;
    258  1.19     kamil 	    }
    259  1.19     kamil 	    else
    260  1.19     kamil 		target_col = t;
    261  1.19     kamil 	}
    262  1.19     kamil     else if (ps.ind_stmt)
    263  1.19     kamil 	target_col += opt.continuation_indent;
    264  1.19     kamil     return target_col;
    265   1.1       cgd }
    266   1.1       cgd 
    267   1.5     lukem int
    268  1.11       wiz compute_label_target(void)
    269   1.1       cgd {
    270  1.19     kamil     return
    271  1.19     kamil 	ps.pcase ? (int) (case_ind * opt.ind_size) + 1
    272   1.1       cgd 	: *s_lab == '#' ? 1
    273  1.19     kamil 	: opt.ind_size * (ps.ind_level - label_offset) + 1;
    274   1.1       cgd }
    275   1.1       cgd 
    276   1.1       cgd 
    277   1.1       cgd /*
    278   1.1       cgd  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
    279   1.5     lukem  *
    280   1.1       cgd  * All rights reserved
    281   1.5     lukem  *
    282   1.5     lukem  *
    283   1.1       cgd  * NAME: fill_buffer
    284   1.5     lukem  *
    285   1.1       cgd  * FUNCTION: Reads one block of input into input_buffer
    286   1.5     lukem  *
    287  1.21    rillig  * HISTORY: initial coding	November 1976	D A Willcox of CAC 1/7/77 A
    288   1.1       cgd  * Willcox of CAC	Added check for switch back to partly full input
    289   1.1       cgd  * buffer from temporary buffer
    290   1.5     lukem  *
    291   1.1       cgd  */
    292   1.5     lukem void
    293  1.11       wiz fill_buffer(void)
    294   1.1       cgd {				/* this routine reads stuff from the input */
    295  1.19     kamil     char *p;
    296  1.19     kamil     int i;
    297  1.19     kamil     FILE *f = input;
    298  1.19     kamil 
    299  1.19     kamil     if (bp_save != NULL) {	/* there is a partly filled input buffer left */
    300  1.19     kamil 	buf_ptr = bp_save;	/* do not read anything, just switch buffers */
    301  1.19     kamil 	buf_end = be_save;
    302  1.19     kamil 	bp_save = be_save = NULL;
    303  1.19     kamil 	if (buf_ptr < buf_end)
    304  1.19     kamil 	    return;		/* only return if there is really something in
    305   1.1       cgd 				 * this buffer */
    306  1.19     kamil     }
    307  1.19     kamil     for (p = in_buffer;;) {
    308  1.19     kamil 	if (p >= in_buffer_limit) {
    309  1.19     kamil 	    int size = (in_buffer_limit - in_buffer) * 2 + 10;
    310  1.19     kamil 	    int offset = p - in_buffer;
    311  1.19     kamil 	    in_buffer = realloc(in_buffer, size);
    312  1.19     kamil 	    if (in_buffer == NULL)
    313  1.19     kamil 		errx(1, "input line too long");
    314  1.19     kamil 	    p = in_buffer + offset;
    315  1.19     kamil 	    in_buffer_limit = in_buffer + size - 2;
    316  1.19     kamil 	}
    317  1.19     kamil 	if ((i = getc(f)) == EOF) {
    318  1.19     kamil 		*p++ = ' ';
    319  1.19     kamil 		*p++ = '\n';
    320  1.19     kamil 		had_eof = true;
    321  1.19     kamil 		break;
    322   1.1       cgd 	}
    323  1.19     kamil 	if (i != '\0')
    324  1.19     kamil 	    *p++ = i;
    325  1.19     kamil 	if (i == '\n')
    326  1.19     kamil 		break;
    327  1.19     kamil     }
    328  1.19     kamil     buf_ptr = in_buffer;
    329  1.19     kamil     buf_end = p;
    330  1.19     kamil     if (p - in_buffer > 2 && p[-2] == '/' && p[-3] == '*') {
    331  1.19     kamil 	if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
    332  1.19     kamil 	    fill_buffer();	/* flush indent error message */
    333  1.19     kamil 	else {
    334  1.19     kamil 	    int         com = 0;
    335  1.19     kamil 
    336  1.19     kamil 	    p = in_buffer;
    337  1.19     kamil 	    while (*p == ' ' || *p == '\t')
    338  1.19     kamil 		p++;
    339  1.19     kamil 	    if (*p == '/' && p[1] == '*') {
    340  1.19     kamil 		p += 2;
    341  1.19     kamil 		while (*p == ' ' || *p == '\t')
    342  1.19     kamil 		    p++;
    343  1.19     kamil 		if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E'
    344  1.19     kamil 			&& p[4] == 'N' && p[5] == 'T') {
    345  1.19     kamil 		    p += 6;
    346  1.19     kamil 		    while (*p == ' ' || *p == '\t')
    347  1.19     kamil 			p++;
    348  1.19     kamil 		    if (*p == '*')
    349  1.19     kamil 			com = 1;
    350  1.19     kamil 		    else if (*p == 'O') {
    351  1.19     kamil 			if (*++p == 'N')
    352  1.19     kamil 			    p++, com = 1;
    353  1.19     kamil 			else if (*p == 'F' && *++p == 'F')
    354  1.19     kamil 			    p++, com = 2;
    355  1.19     kamil 		    }
    356  1.19     kamil 		    while (*p == ' ' || *p == '\t')
    357  1.19     kamil 			p++;
    358  1.19     kamil 		    if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) {
    359  1.19     kamil 			if (s_com != e_com || s_lab != e_lab || s_code != e_code)
    360  1.19     kamil 			    dump_line();
    361  1.19     kamil 			if (!(inhibit_formatting = com - 1)) {
    362  1.19     kamil 			    n_real_blanklines = 0;
    363  1.19     kamil 			    postfix_blankline_requested = 0;
    364  1.19     kamil 			    prefix_blankline_requested = 0;
    365  1.19     kamil 			    suppress_blanklines = 1;
    366   1.1       cgd 			}
    367  1.19     kamil 		    }
    368   1.1       cgd 		}
    369  1.19     kamil 	    }
    370   1.1       cgd 	}
    371  1.19     kamil     }
    372  1.19     kamil     if (inhibit_formatting) {
    373  1.19     kamil 	p = in_buffer;
    374  1.25    rillig 	do {
    375  1.19     kamil 	    putc(*p, output);
    376  1.25    rillig 	} while (*p++ != '\n');
    377  1.19     kamil     }
    378   1.1       cgd }
    379  1.19     kamil 
    380   1.1       cgd /*
    381   1.1       cgd  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
    382   1.5     lukem  *
    383   1.1       cgd  * All rights reserved
    384   1.5     lukem  *
    385   1.5     lukem  *
    386   1.1       cgd  * NAME: pad_output
    387   1.5     lukem  *
    388   1.1       cgd  * FUNCTION: Writes tabs and spaces to move the current column up to the desired
    389   1.1       cgd  * position.
    390   1.5     lukem  *
    391   1.1       cgd  * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf.
    392   1.5     lukem  *
    393   1.1       cgd  * PARAMETERS: current		integer		The current column target
    394  1.19     kamil  * nteger		The desired column
    395   1.5     lukem  *
    396   1.1       cgd  * RETURNS: Integer value of the new column.  (If current >= target, no action is
    397   1.1       cgd  * taken, and current is returned.
    398   1.5     lukem  *
    399   1.1       cgd  * GLOBALS: None
    400   1.5     lukem  *
    401   1.1       cgd  * CALLS: write (sys)
    402   1.5     lukem  *
    403   1.1       cgd  * CALLED BY: dump_line
    404   1.5     lukem  *
    405  1.21    rillig  * HISTORY: initial coding	November 1976	D A Willcox of CAC
    406   1.5     lukem  *
    407   1.1       cgd  */
    408  1.19     kamil static int
    409  1.11       wiz pad_output(int current, int target)
    410  1.19     kamil 			        /* writes tabs and blanks (if necessary) to
    411  1.19     kamil 				 * get the current output position up to the
    412  1.19     kamil 				 * target column */
    413  1.19     kamil     /* current: the current column value */
    414  1.19     kamil     /* target: position we want it at */
    415   1.1       cgd {
    416  1.19     kamil     int curr;			/* internal column pointer */
    417   1.1       cgd 
    418  1.19     kamil     if (current >= target)
    419  1.23    rillig 	return current;		/* line is already long enough */
    420  1.19     kamil     curr = current;
    421  1.19     kamil     if (opt.use_tabs) {
    422  1.19     kamil 	int tcur;
    423  1.19     kamil 
    424  1.19     kamil 	while ((tcur = opt.tabsize * (1 + (curr - 1) / opt.tabsize) + 1) <= target) {
    425  1.19     kamil 	    putc('\t', output);
    426  1.19     kamil 	    curr = tcur;
    427   1.1       cgd 	}
    428  1.19     kamil     }
    429  1.19     kamil     while (curr++ < target)
    430  1.19     kamil 	putc(' ', output);	/* pad with final blanks */
    431  1.19     kamil 
    432  1.23    rillig     return target;
    433   1.1       cgd }
    434  1.19     kamil 
    435   1.1       cgd /*
    436   1.1       cgd  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
    437   1.5     lukem  *
    438   1.1       cgd  * All rights reserved
    439   1.5     lukem  *
    440   1.5     lukem  *
    441   1.1       cgd  * NAME: count_spaces
    442   1.5     lukem  *
    443   1.1       cgd  * FUNCTION: Find out where printing of a given string will leave the current
    444   1.1       cgd  * character position on output.
    445   1.5     lukem  *
    446   1.1       cgd  * ALGORITHM: Run thru input string and add appropriate values to current
    447   1.1       cgd  * position.
    448   1.5     lukem  *
    449   1.1       cgd  * RETURNS: Integer value of position after printing "buffer" starting in column
    450   1.1       cgd  * "current".
    451   1.5     lukem  *
    452  1.21    rillig  * HISTORY: initial coding	November 1976	D A Willcox of CAC
    453   1.5     lukem  *
    454   1.1       cgd  */
    455   1.1       cgd int
    456  1.19     kamil count_spaces_until(int cur, char *buffer, char *end)
    457   1.1       cgd /*
    458   1.1       cgd  * this routine figures out where the character position will be after
    459   1.1       cgd  * printing the text in buffer starting at column "current"
    460   1.1       cgd  */
    461   1.1       cgd {
    462  1.19     kamil     char *buf;		/* used to look thru buffer */
    463   1.1       cgd 
    464  1.19     kamil     for (buf = buffer; *buf != '\0' && buf != end; ++buf) {
    465  1.19     kamil 	switch (*buf) {
    466   1.1       cgd 
    467  1.19     kamil 	case '\n':
    468  1.19     kamil 	case 014:		/* form feed */
    469  1.19     kamil 	    cur = 1;
    470  1.19     kamil 	    break;
    471  1.19     kamil 
    472  1.19     kamil 	case '\t':
    473  1.19     kamil 	    cur = opt.tabsize * (1 + (cur - 1) / opt.tabsize) + 1;
    474  1.19     kamil 	    break;
    475  1.19     kamil 
    476  1.19     kamil 	case 010:		/* backspace */
    477  1.19     kamil 	    --cur;
    478  1.19     kamil 	    break;
    479  1.19     kamil 
    480  1.19     kamil 	default:
    481  1.19     kamil 	    ++cur;
    482  1.19     kamil 	    break;
    483  1.19     kamil 	}			/* end of switch */
    484  1.19     kamil     }				/* end of for loop */
    485  1.23    rillig     return cur;
    486   1.1       cgd }
    487   1.1       cgd 
    488  1.19     kamil int
    489  1.19     kamil count_spaces(int cur, char *buffer)
    490   1.1       cgd {
    491  1.23    rillig     return count_spaces_until(cur, buffer, NULL);
    492   1.1       cgd }
    493   1.1       cgd 
    494   1.5     lukem void
    495  1.20  christos diag(int level, const char *msg, ...)
    496   1.1       cgd {
    497  1.20  christos     va_list ap;
    498  1.20  christos     const char *s, *e;
    499  1.20  christos 
    500  1.19     kamil     if (level)
    501  1.19     kamil 	found_err = 1;
    502   1.1       cgd 
    503  1.19     kamil     if (output == stdout) {
    504  1.20  christos 	s = "/**INDENT** ";
    505  1.20  christos 	e = " */";
    506  1.20  christos     } else {
    507  1.20  christos 	s = e = "";
    508  1.19     kamil     }
    509   1.1       cgd 
    510  1.20  christos     va_start(ap, msg);
    511  1.20  christos     fprintf(stderr, "%s%s@%d: ", s, level == 0 ? "Warning" : "Error", line_no);
    512  1.20  christos     vfprintf(stderr, msg, ap);
    513  1.20  christos     fprintf(stderr, "%s\n", e);
    514  1.20  christos     va_end(ap);
    515   1.1       cgd }
    516