11590Srgrimes/*
21590Srgrimes * Copyright (c) 1985 Sun Microsystems, Inc.
31590Srgrimes * Copyright (c) 1980, 1993
41590Srgrimes *	The Regents of the University of California.  All rights reserved.
51590Srgrimes * All rights reserved.
61590Srgrimes *
71590Srgrimes * Redistribution and use in source and binary forms, with or without
81590Srgrimes * modification, are permitted provided that the following conditions
91590Srgrimes * are met:
101590Srgrimes * 1. Redistributions of source code must retain the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer.
121590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
131590Srgrimes *    notice, this list of conditions and the following disclaimer in the
141590Srgrimes *    documentation and/or other materials provided with the distribution.
151590Srgrimes * 3. All advertising materials mentioning features or use of this software
161590Srgrimes *    must display the following acknowledgement:
171590Srgrimes *	This product includes software developed by the University of
181590Srgrimes *	California, Berkeley and its contributors.
191590Srgrimes * 4. Neither the name of the University nor the names of its contributors
201590Srgrimes *    may be used to endorse or promote products derived from this software
211590Srgrimes *    without specific prior written permission.
221590Srgrimes *
231590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
241590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
251590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
261590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
271590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
281590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
291590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
301590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
311590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
321590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
331590Srgrimes * SUCH DAMAGE.
341590Srgrimes */
351590Srgrimes
36116390Scharnier#if 0
371590Srgrimes#ifndef lint
381590Srgrimesstatic char sccsid[] = "@(#)io.c	8.1 (Berkeley) 6/6/93";
39116390Scharnier#endif /* not lint */
4027419Scharnier#endif
41116390Scharnier
4299112Sobrien#include <sys/cdefs.h>
4399112Sobrien__FBSDID("$FreeBSD$");
441590Srgrimes
4527419Scharnier#include <ctype.h>
4627419Scharnier#include <err.h>
471590Srgrimes#include <stdio.h>
481590Srgrimes#include <stdlib.h>
491590Srgrimes#include <string.h>
501590Srgrimes#include "indent_globs.h"
5185632Sschweikh#include "indent.h"
521590Srgrimes
531590Srgrimesint         comment_open;
5485632Sschweikhstatic int  paren_target;
5585632Sschweikhstatic int pad_output(int current, int target);
561590Srgrimes
5785632Sschweikhvoid
5885632Sschweikhdump_line(void)
591590Srgrimes{				/* dump_line is the routine that actually
601590Srgrimes				 * effects the printing of the new source. It
611590Srgrimes				 * prints the label section, followed by the
621590Srgrimes				 * code section with the appropriate nesting
631590Srgrimes				 * level, followed by any comments */
6498771Sjmallett    int cur_col,
6585632Sschweikh                target_col = 1;
6685632Sschweikh    static int  not_first_line;
671590Srgrimes
681590Srgrimes    if (ps.procname[0]) {
691590Srgrimes	if (troff) {
701590Srgrimes	    if (comment_open) {
711590Srgrimes		comment_open = 0;
721590Srgrimes		fprintf(output, ".*/\n");
731590Srgrimes	    }
741590Srgrimes	    fprintf(output, ".Pr \"%s\"\n", ps.procname);
751590Srgrimes	}
761590Srgrimes	ps.ind_level = 0;
771590Srgrimes	ps.procname[0] = 0;
781590Srgrimes    }
791590Srgrimes    if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
801590Srgrimes	if (suppress_blanklines > 0)
811590Srgrimes	    suppress_blanklines--;
821590Srgrimes	else {
831590Srgrimes	    ps.bl_line = true;
841590Srgrimes	    n_real_blanklines++;
851590Srgrimes	}
861590Srgrimes    }
871590Srgrimes    else if (!inhibit_formatting) {
881590Srgrimes	suppress_blanklines = 0;
891590Srgrimes	ps.bl_line = false;
9048566Sbillf	if (prefix_blankline_requested && not_first_line) {
911590Srgrimes	    if (swallow_optional_blanklines) {
921590Srgrimes		if (n_real_blanklines == 1)
931590Srgrimes		    n_real_blanklines = 0;
941590Srgrimes	    }
951590Srgrimes	    else {
961590Srgrimes		if (n_real_blanklines == 0)
971590Srgrimes		    n_real_blanklines = 1;
981590Srgrimes	    }
9948566Sbillf	}
1001590Srgrimes	while (--n_real_blanklines >= 0)
1011590Srgrimes	    putc('\n', output);
1021590Srgrimes	n_real_blanklines = 0;
1031590Srgrimes	if (ps.ind_level == 0)
1041590Srgrimes	    ps.ind_stmt = 0;	/* this is a class A kludge. dont do
1051590Srgrimes				 * additional statement indentation if we are
1061590Srgrimes				 * at bracket level 0 */
1071590Srgrimes
1081590Srgrimes	if (e_lab != s_lab || e_code != s_code)
1091590Srgrimes	    ++code_lines;	/* keep count of lines with code */
1101590Srgrimes
1111590Srgrimes
1121590Srgrimes	if (e_lab != s_lab) {	/* print lab, if any */
1131590Srgrimes	    if (comment_open) {
1141590Srgrimes		comment_open = 0;
1151590Srgrimes		fprintf(output, ".*/\n");
1161590Srgrimes	    }
1171590Srgrimes	    while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
1181590Srgrimes		e_lab--;
1191590Srgrimes	    cur_col = pad_output(1, compute_label_target());
1201590Srgrimes	    if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0
1211590Srgrimes				    || strncmp(s_lab, "#endif", 6) == 0)) {
12298771Sjmallett		char *s = s_lab;
1231590Srgrimes		if (e_lab[-1] == '\n') e_lab--;
1241590Srgrimes		do putc(*s++, output);
1251590Srgrimes		while (s < e_lab && 'a' <= *s && *s<='z');
1261590Srgrimes		while ((*s == ' ' || *s == '\t') && s < e_lab)
1271590Srgrimes		    s++;
1281590Srgrimes		if (s < e_lab)
1291590Srgrimes		    fprintf(output, s[0]=='/' && s[1]=='*' ? "\t%.*s" : "\t/* %.*s */",
13093440Sdwmalone			    (int)(e_lab - s), s);
1311590Srgrimes	    }
13293440Sdwmalone	    else fprintf(output, "%.*s", (int)(e_lab - s_lab), s_lab);
1331590Srgrimes	    cur_col = count_spaces(cur_col, s_lab);
1341590Srgrimes	}
1351590Srgrimes	else
1361590Srgrimes	    cur_col = 1;	/* there is no label section */
1371590Srgrimes
1381590Srgrimes	ps.pcase = false;
1391590Srgrimes
1401590Srgrimes	if (s_code != e_code) {	/* print code section, if any */
14198771Sjmallett	    char *p;
1421590Srgrimes
1431590Srgrimes	    if (comment_open) {
1441590Srgrimes		comment_open = 0;
1451590Srgrimes		fprintf(output, ".*/\n");
1461590Srgrimes	    }
1471590Srgrimes	    target_col = compute_code_target();
1481590Srgrimes	    {
14998771Sjmallett		int i;
1501590Srgrimes
1511590Srgrimes		for (i = 0; i < ps.p_l_follow; i++)
1521590Srgrimes		    if (ps.paren_indents[i] >= 0)
1531590Srgrimes			ps.paren_indents[i] = -(ps.paren_indents[i] + target_col);
1541590Srgrimes	    }
1551590Srgrimes	    cur_col = pad_output(cur_col, target_col);
1561590Srgrimes	    for (p = s_code; p < e_code; p++)
1571590Srgrimes		if (*p == (char) 0200)
1581590Srgrimes		    fprintf(output, "%d", target_col * 7);
1591590Srgrimes		else
1601590Srgrimes		    putc(*p, output);
1611590Srgrimes	    cur_col = count_spaces(cur_col, s_code);
1621590Srgrimes	}
16385632Sschweikh	if (s_com != e_com) {
1641590Srgrimes	    if (troff) {
1651590Srgrimes		int         all_here = 0;
16698771Sjmallett		char *p;
1671590Srgrimes
1681590Srgrimes		if (e_com[-1] == '/' && e_com[-2] == '*')
1691590Srgrimes		    e_com -= 2, all_here++;
1701590Srgrimes		while (e_com > s_com && e_com[-1] == ' ')
1711590Srgrimes		    e_com--;
1721590Srgrimes		*e_com = 0;
1731590Srgrimes		p = s_com;
1741590Srgrimes		while (*p == ' ')
1751590Srgrimes		    p++;
1761590Srgrimes		if (p[0] == '/' && p[1] == '*')
1771590Srgrimes		    p += 2, all_here++;
1781590Srgrimes		else if (p[0] == '*')
1791590Srgrimes		    p += p[1] == '/' ? 2 : 1;
1801590Srgrimes		while (*p == ' ')
1811590Srgrimes		    p++;
1821590Srgrimes		if (*p == 0)
1831590Srgrimes		    goto inhibit_newline;
1841590Srgrimes		if (comment_open < 2 && ps.box_com) {
1851590Srgrimes		    comment_open = 0;
1861590Srgrimes		    fprintf(output, ".*/\n");
1871590Srgrimes		}
1881590Srgrimes		if (comment_open == 0) {
1891590Srgrimes		    if ('a' <= *p && *p <= 'z')
1901590Srgrimes			*p = *p + 'A' - 'a';
1911590Srgrimes		    if (e_com - p < 50 && all_here == 2) {
19298771Sjmallett			char *follow = p;
1931590Srgrimes			fprintf(output, "\n.nr C! \\w\1");
1941590Srgrimes			while (follow < e_com) {
1951590Srgrimes			    switch (*follow) {
1961590Srgrimes			    case '\n':
1971590Srgrimes				putc(' ', output);
1981590Srgrimes			    case 1:
1991590Srgrimes				break;
2001590Srgrimes			    case '\\':
2011590Srgrimes				putc('\\', output);
2021590Srgrimes			    default:
2031590Srgrimes				putc(*follow, output);
2041590Srgrimes			    }
2051590Srgrimes			    follow++;
2061590Srgrimes			}
2071590Srgrimes			putc(1, output);
2081590Srgrimes		    }
2091590Srgrimes		    fprintf(output, "\n./* %dp %d %dp\n",
2101590Srgrimes			    ps.com_col * 7,
2111590Srgrimes			    (s_code != e_code || s_lab != e_lab) - ps.box_com,
2121590Srgrimes			    target_col * 7);
2131590Srgrimes		}
2141590Srgrimes		comment_open = 1 + ps.box_com;
2151590Srgrimes		while (*p) {
2161590Srgrimes		    if (*p == BACKSLASH)
2171590Srgrimes			putc(BACKSLASH, output);
2181590Srgrimes		    putc(*p++, output);
2191590Srgrimes		}
2201590Srgrimes	    }
2211590Srgrimes	    else {		/* print comment, if any */
22298771Sjmallett		int target = ps.com_col;
22398771Sjmallett		char *com_st = s_com;
2241590Srgrimes
2251590Srgrimes		target += ps.comment_delta;
2261590Srgrimes		while (*com_st == '\t')
2271590Srgrimes		    com_st++, target += 8;	/* ? */
2281590Srgrimes		while (target <= 0)
2291590Srgrimes		    if (*com_st == ' ')
2301590Srgrimes			target++, com_st++;
2311590Srgrimes		    else if (*com_st == '\t')
2321590Srgrimes			target = ((target - 1) & ~7) + 9, com_st++;
2331590Srgrimes		    else
2341590Srgrimes			target = 1;
235228992Suqs		if (cur_col > target) {	/* if comment can't fit on this line,
2361590Srgrimes					 * put it on next line */
2371590Srgrimes		    putc('\n', output);
2381590Srgrimes		    cur_col = 1;
2391590Srgrimes		    ++ps.out_lines;
2401590Srgrimes		}
2411590Srgrimes		while (e_com > com_st && isspace(e_com[-1]))
2421590Srgrimes		    e_com--;
2431590Srgrimes		cur_col = pad_output(cur_col, target);
2441590Srgrimes		if (!ps.box_com) {
24548566Sbillf		    if (star_comment_cont && (com_st[1] != '*' || e_com <= com_st + 1)) {
2461590Srgrimes			if (com_st[1] == ' ' && com_st[0] == ' ' && e_com > com_st + 1)
2471590Srgrimes			    com_st[1] = '*';
2481590Srgrimes			else
2491590Srgrimes			    fwrite(" * ", com_st[0] == '\t' ? 2 : com_st[0] == '*' ? 1 : 3, 1, output);
25048566Sbillf		    }
2511590Srgrimes		}
2521590Srgrimes		fwrite(com_st, e_com - com_st, 1, output);
2531590Srgrimes		ps.comment_delta = ps.n_comment_delta;
2541590Srgrimes		cur_col = count_spaces(cur_col, com_st);
2551590Srgrimes		++ps.com_lines;	/* count lines with comments */
2561590Srgrimes	    }
25785632Sschweikh	}
2581590Srgrimes	if (ps.use_ff)
2591590Srgrimes	    putc('\014', output);
2601590Srgrimes	else
2611590Srgrimes	    putc('\n', output);
2621590Srgrimesinhibit_newline:
2631590Srgrimes	++ps.out_lines;
2641590Srgrimes	if (ps.just_saw_decl == 1 && blanklines_after_declarations) {
2651590Srgrimes	    prefix_blankline_requested = 1;
2661590Srgrimes	    ps.just_saw_decl = 0;
2671590Srgrimes	}
2681590Srgrimes	else
2691590Srgrimes	    prefix_blankline_requested = postfix_blankline_requested;
2701590Srgrimes	postfix_blankline_requested = 0;
2711590Srgrimes    }
2721590Srgrimes    ps.decl_on_line = ps.in_decl;	/* if we are in the middle of a
2731590Srgrimes					 * declaration, remember that fact for
2741590Srgrimes					 * proper comment indentation */
2751590Srgrimes    ps.ind_stmt = ps.in_stmt & ~ps.in_decl;	/* next line should be
2761590Srgrimes						 * indented if we have not
2771590Srgrimes						 * completed this stmt and if
2781590Srgrimes						 * we are not in the middle of
2791590Srgrimes						 * a declaration */
2801590Srgrimes    ps.use_ff = false;
2811590Srgrimes    ps.dumped_decl_indent = 0;
2821590Srgrimes    *(e_lab = s_lab) = '\0';	/* reset buffers */
2831590Srgrimes    *(e_code = s_code) = '\0';
2841590Srgrimes    *(e_com = s_com) = '\0';
2851590Srgrimes    ps.ind_level = ps.i_l_follow;
2861590Srgrimes    ps.paren_level = ps.p_l_follow;
2871590Srgrimes    paren_target = -ps.paren_indents[ps.paren_level - 1];
2881590Srgrimes    not_first_line = 1;
2891590Srgrimes}
2901590Srgrimes
29185632Sschweikhint
29285632Sschweikhcompute_code_target(void)
2931590Srgrimes{
29498771Sjmallett    int target_col = ps.ind_size * ps.ind_level + 1;
2951590Srgrimes
2961590Srgrimes    if (ps.paren_level)
2971590Srgrimes	if (!lineup_to_parens)
298125736Sbde	    target_col += continuation_indent
299125736Sbde		* (2 * continuation_indent == ps.ind_size ? 1 : ps.paren_level);
3001590Srgrimes	else {
30198771Sjmallett	    int w;
30298771Sjmallett	    int t = paren_target;
3031590Srgrimes
3041590Srgrimes	    if ((w = count_spaces(t, s_code) - max_col) > 0
3051590Srgrimes		    && count_spaces(target_col, s_code) <= max_col) {
3061590Srgrimes		t -= w + 1;
3071590Srgrimes		if (t > target_col)
3081590Srgrimes		    target_col = t;
3091590Srgrimes	    }
3101590Srgrimes	    else
3111590Srgrimes		target_col = t;
3121590Srgrimes	}
3131590Srgrimes    else if (ps.ind_stmt)
3141590Srgrimes	target_col += continuation_indent;
3151590Srgrimes    return target_col;
3161590Srgrimes}
3171590Srgrimes
31885632Sschweikhint
31985632Sschweikhcompute_label_target(void)
3201590Srgrimes{
3211590Srgrimes    return
3221590Srgrimes	ps.pcase ? (int) (case_ind * ps.ind_size) + 1
3231590Srgrimes	: *s_lab == '#' ? 1
3241590Srgrimes	: ps.ind_size * (ps.ind_level - label_offset) + 1;
3251590Srgrimes}
3261590Srgrimes
3271590Srgrimes
3281590Srgrimes/*
3291590Srgrimes * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
3308874Srgrimes *
3311590Srgrimes * All rights reserved
3328874Srgrimes *
3338874Srgrimes *
3341590Srgrimes * NAME: fill_buffer
3358874Srgrimes *
3361590Srgrimes * FUNCTION: Reads one block of input into input_buffer
3378874Srgrimes *
3381590Srgrimes * HISTORY: initial coding 	November 1976	D A Willcox of CAC 1/7/77 A
3391590Srgrimes * Willcox of CAC	Added check for switch back to partly full input
3401590Srgrimes * buffer from temporary buffer
3418874Srgrimes *
3421590Srgrimes */
34385632Sschweikhvoid
34485632Sschweikhfill_buffer(void)
3451590Srgrimes{				/* this routine reads stuff from the input */
34698771Sjmallett    char *p;
34798771Sjmallett    int i;
34898771Sjmallett    FILE *f = input;
3491590Srgrimes
3501590Srgrimes    if (bp_save != 0) {		/* there is a partly filled input buffer left */
3511590Srgrimes	buf_ptr = bp_save;	/* dont read anything, just switch buffers */
3521590Srgrimes	buf_end = be_save;
3531590Srgrimes	bp_save = be_save = 0;
3541590Srgrimes	if (buf_ptr < buf_end)
3551590Srgrimes	    return;		/* only return if there is really something in
3561590Srgrimes				 * this buffer */
3571590Srgrimes    }
3581590Srgrimes    for (p = in_buffer;;) {
3591590Srgrimes	if (p >= in_buffer_limit) {
36098771Sjmallett	    int size = (in_buffer_limit - in_buffer) * 2 + 10;
36198771Sjmallett	    int offset = p - in_buffer;
36285632Sschweikh	    in_buffer = realloc(in_buffer, size);
363116390Scharnier	    if (in_buffer == NULL)
364116390Scharnier		errx(1, "input line too long");
3651590Srgrimes	    p = in_buffer + offset;
3661590Srgrimes	    in_buffer_limit = in_buffer + size - 2;
3671590Srgrimes	}
3681590Srgrimes	if ((i = getc(f)) == EOF) {
3691590Srgrimes		*p++ = ' ';
3701590Srgrimes		*p++ = '\n';
3711590Srgrimes		had_eof = true;
3721590Srgrimes		break;
3731590Srgrimes	}
3741590Srgrimes	*p++ = i;
3751590Srgrimes	if (i == '\n')
3761590Srgrimes		break;
3771590Srgrimes    }
3781590Srgrimes    buf_ptr = in_buffer;
3791590Srgrimes    buf_end = p;
3801590Srgrimes    if (p[-2] == '/' && p[-3] == '*') {
3811590Srgrimes	if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
3821590Srgrimes	    fill_buffer();	/* flush indent error message */
3831590Srgrimes	else {
3841590Srgrimes	    int         com = 0;
3851590Srgrimes
3861590Srgrimes	    p = in_buffer;
3871590Srgrimes	    while (*p == ' ' || *p == '\t')
3881590Srgrimes		p++;
3891590Srgrimes	    if (*p == '/' && p[1] == '*') {
3901590Srgrimes		p += 2;
3911590Srgrimes		while (*p == ' ' || *p == '\t')
3921590Srgrimes		    p++;
3931590Srgrimes		if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E'
3941590Srgrimes			&& p[4] == 'N' && p[5] == 'T') {
3951590Srgrimes		    p += 6;
3961590Srgrimes		    while (*p == ' ' || *p == '\t')
3971590Srgrimes			p++;
3981590Srgrimes		    if (*p == '*')
3991590Srgrimes			com = 1;
40085632Sschweikh		    else if (*p == 'O') {
4011590Srgrimes			if (*++p == 'N')
4021590Srgrimes			    p++, com = 1;
4031590Srgrimes			else if (*p == 'F' && *++p == 'F')
4041590Srgrimes			    p++, com = 2;
40585632Sschweikh		    }
4061590Srgrimes		    while (*p == ' ' || *p == '\t')
4071590Srgrimes			p++;
4081590Srgrimes		    if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) {
4091590Srgrimes			if (s_com != e_com || s_lab != e_lab || s_code != e_code)
4101590Srgrimes			    dump_line();
4111590Srgrimes			if (!(inhibit_formatting = com - 1)) {
4121590Srgrimes			    n_real_blanklines = 0;
4131590Srgrimes			    postfix_blankline_requested = 0;
4141590Srgrimes			    prefix_blankline_requested = 0;
4151590Srgrimes			    suppress_blanklines = 1;
4161590Srgrimes			}
4171590Srgrimes		    }
4181590Srgrimes		}
4191590Srgrimes	    }
4201590Srgrimes	}
4211590Srgrimes    }
4221590Srgrimes    if (inhibit_formatting) {
4231590Srgrimes	p = in_buffer;
4241590Srgrimes	do
4251590Srgrimes	    putc(*p, output);
4261590Srgrimes	while (*p++ != '\n');
4271590Srgrimes    }
4281590Srgrimes}
4291590Srgrimes
4301590Srgrimes/*
4311590Srgrimes * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
4328874Srgrimes *
4331590Srgrimes * All rights reserved
4348874Srgrimes *
4358874Srgrimes *
4361590Srgrimes * NAME: pad_output
4378874Srgrimes *
4381590Srgrimes * FUNCTION: Writes tabs and spaces to move the current column up to the desired
4391590Srgrimes * position.
4408874Srgrimes *
4411590Srgrimes * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf.
4428874Srgrimes *
4431590Srgrimes * PARAMETERS: current		integer		The current column target
4441590Srgrimes * nteger		The desired column
4458874Srgrimes *
4461590Srgrimes * RETURNS: Integer value of the new column.  (If current >= target, no action is
4471590Srgrimes * taken, and current is returned.
4488874Srgrimes *
4491590Srgrimes * GLOBALS: None
4508874Srgrimes *
4511590Srgrimes * CALLS: write (sys)
4528874Srgrimes *
4531590Srgrimes * CALLED BY: dump_line
4548874Srgrimes *
4551590Srgrimes * HISTORY: initial coding 	November 1976	D A Willcox of CAC
4568874Srgrimes *
4571590Srgrimes */
45885632Sschweikhstatic int
45985632Sschweikhpad_output(int current, int target)
46085632Sschweikh			        /* writes tabs and blanks (if necessary) to
4611590Srgrimes				 * get the current output position up to the
4621590Srgrimes				 * target column */
46385632Sschweikh    /* current: the current column value */
46485632Sschweikh    /* target: position we want it at */
4651590Srgrimes{
46698771Sjmallett    int curr;		/* internal column pointer */
46798771Sjmallett    int tcur;
4681590Srgrimes
4691590Srgrimes    if (troff)
4701590Srgrimes	fprintf(output, "\\h'|%dp'", (target - 1) * 7);
4711590Srgrimes    else {
4721590Srgrimes	if (current >= target)
4731590Srgrimes	    return (current);	/* line is already long enough */
4741590Srgrimes	curr = current;
475131184Sschweikh        if (use_tabs) {
476131184Sschweikh            while ((tcur = ((curr - 1) & tabmask) + tabsize + 1) <= target) {
477131184Sschweikh                putc('\t', output);
478131184Sschweikh                curr = tcur;
479131184Sschweikh            }
480131184Sschweikh        }
481131184Sschweikh        while (curr++ < target)
4821590Srgrimes	    putc(' ', output);	/* pad with final blanks */
4831590Srgrimes    }
4841590Srgrimes    return (target);
4851590Srgrimes}
4861590Srgrimes
4871590Srgrimes/*
4881590Srgrimes * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
4898874Srgrimes *
4901590Srgrimes * All rights reserved
4918874Srgrimes *
4928874Srgrimes *
4931590Srgrimes * NAME: count_spaces
4948874Srgrimes *
4951590Srgrimes * FUNCTION: Find out where printing of a given string will leave the current
4961590Srgrimes * character position on output.
4978874Srgrimes *
4981590Srgrimes * ALGORITHM: Run thru input string and add appropriate values to current
4991590Srgrimes * position.
5008874Srgrimes *
5011590Srgrimes * RETURNS: Integer value of position after printing "buffer" starting in column
5021590Srgrimes * "current".
5038874Srgrimes *
5041590Srgrimes * HISTORY: initial coding 	November 1976	D A Willcox of CAC
5058874Srgrimes *
5061590Srgrimes */
5071590Srgrimesint
50885632Sschweikhcount_spaces(int current, char *buffer)
5091590Srgrimes/*
5101590Srgrimes * this routine figures out where the character position will be after
5111590Srgrimes * printing the text in buffer starting at column "current"
5121590Srgrimes */
5131590Srgrimes{
51498771Sjmallett    char *buf;		/* used to look thru buffer */
51598771Sjmallett    int cur;		/* current character counter */
5161590Srgrimes
5171590Srgrimes    cur = current;
5181590Srgrimes
5191590Srgrimes    for (buf = buffer; *buf != '\0'; ++buf) {
5201590Srgrimes	switch (*buf) {
5211590Srgrimes
5221590Srgrimes	case '\n':
5231590Srgrimes	case 014:		/* form feed */
5241590Srgrimes	    cur = 1;
5251590Srgrimes	    break;
5261590Srgrimes
5271590Srgrimes	case '\t':
5281590Srgrimes	    cur = ((cur - 1) & tabmask) + tabsize + 1;
5291590Srgrimes	    break;
5301590Srgrimes
5311590Srgrimes	case 010:		/* backspace */
5321590Srgrimes	    --cur;
5331590Srgrimes	    break;
5341590Srgrimes
5351590Srgrimes	default:
5361590Srgrimes	    ++cur;
5371590Srgrimes	    break;
5381590Srgrimes	}			/* end of switch */
5391590Srgrimes    }				/* end of for loop */
5401590Srgrimes    return (cur);
5411590Srgrimes}
5421590Srgrimes
54385632Sschweikhvoid
54493440Sdwmalonediag4(int level, const char *msg, int a, int b)
5451590Srgrimes{
5461590Srgrimes    if (level)
5471590Srgrimes	found_err = 1;
5481590Srgrimes    if (output == stdout) {
5491590Srgrimes	fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
5501590Srgrimes	fprintf(stdout, msg, a, b);
5511590Srgrimes	fprintf(stdout, " */\n");
5521590Srgrimes    }
5531590Srgrimes    else {
5541590Srgrimes	fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
5551590Srgrimes	fprintf(stderr, msg, a, b);
5561590Srgrimes	fprintf(stderr, "\n");
5571590Srgrimes    }
5581590Srgrimes}
5591590Srgrimes
56085632Sschweikhvoid
56193440Sdwmalonediag3(int level, const char *msg, int a)
5621590Srgrimes{
56385632Sschweikh    if (level)
56485632Sschweikh	found_err = 1;
56585632Sschweikh    if (output == stdout) {
56685632Sschweikh	fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
56785632Sschweikh	fprintf(stdout, msg, a);
56885632Sschweikh	fprintf(stdout, " */\n");
56985632Sschweikh    }
57085632Sschweikh    else {
57185632Sschweikh	fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
57285632Sschweikh	fprintf(stderr, msg, a);
57385632Sschweikh	fprintf(stderr, "\n");
57485632Sschweikh    }
57585632Sschweikh}
57685632Sschweikh
57785632Sschweikhvoid
57893440Sdwmalonediag2(int level, const char *msg)
57985632Sschweikh{
58085632Sschweikh    if (level)
58185632Sschweikh	found_err = 1;
58285632Sschweikh    if (output == stdout) {
58385632Sschweikh	fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
584228639Sdim	fprintf(stdout, "%s", msg);
58585632Sschweikh	fprintf(stdout, " */\n");
58685632Sschweikh    }
58785632Sschweikh    else {
58885632Sschweikh	fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
589228639Sdim	fprintf(stderr, "%s", msg);
59085632Sschweikh	fprintf(stderr, "\n");
59185632Sschweikh    }
59285632Sschweikh}
59385632Sschweikh
59485632Sschweikhvoid
59585632Sschweikhwritefdef(struct fstate *f, int nm)
59685632Sschweikh{
5971590Srgrimes    fprintf(output, ".ds f%c %s\n.nr s%c %d\n",
5981590Srgrimes	    nm, f->font, nm, f->size);
5991590Srgrimes}
6001590Srgrimes
60185632Sschweikhchar *
60285632Sschweikhchfont(struct fstate *of, struct fstate *nf, char *s)
6031590Srgrimes{
6041590Srgrimes    if (of->font[0] != nf->font[0]
6051590Srgrimes	    || of->font[1] != nf->font[1]) {
6061590Srgrimes	*s++ = '\\';
6071590Srgrimes	*s++ = 'f';
6081590Srgrimes	if (nf->font[1]) {
6091590Srgrimes	    *s++ = '(';
6101590Srgrimes	    *s++ = nf->font[0];
6111590Srgrimes	    *s++ = nf->font[1];
6121590Srgrimes	}
6131590Srgrimes	else
6141590Srgrimes	    *s++ = nf->font[0];
6151590Srgrimes    }
6161590Srgrimes    if (nf->size != of->size) {
6171590Srgrimes	*s++ = '\\';
6181590Srgrimes	*s++ = 's';
6191590Srgrimes	if (nf->size < of->size) {
6201590Srgrimes	    *s++ = '-';
6211590Srgrimes	    *s++ = '0' + of->size - nf->size;
6221590Srgrimes	}
6231590Srgrimes	else {
6241590Srgrimes	    *s++ = '+';
6251590Srgrimes	    *s++ = '0' + nf->size - of->size;
6261590Srgrimes	}
6271590Srgrimes    }
6281590Srgrimes    return s;
6291590Srgrimes}
6301590Srgrimes
63185632Sschweikhvoid
63293440Sdwmaloneparsefont(struct fstate *f, const char *s0)
6331590Srgrimes{
63493440Sdwmalone    const char *s = s0;
6351590Srgrimes    int         sizedelta = 0;
63685632Sschweikh
6371590Srgrimes    bzero(f, sizeof *f);
6381590Srgrimes    while (*s) {
6391590Srgrimes	if (isdigit(*s))
6401590Srgrimes	    f->size = f->size * 10 + *s - '0';
6411590Srgrimes	else if (isupper(*s))
6421590Srgrimes	    if (f->font[0])
6431590Srgrimes		f->font[1] = *s;
6441590Srgrimes	    else
6451590Srgrimes		f->font[0] = *s;
6461590Srgrimes	else if (*s == 'c')
6471590Srgrimes	    f->allcaps = 1;
6481590Srgrimes	else if (*s == '+')
6491590Srgrimes	    sizedelta++;
6501590Srgrimes	else if (*s == '-')
6511590Srgrimes	    sizedelta--;
6521590Srgrimes	else {
65327419Scharnier	    errx(1, "bad font specification: %s", s0);
6541590Srgrimes	}
6551590Srgrimes	s++;
6561590Srgrimes    }
6571590Srgrimes    if (f->font[0] == 0)
6581590Srgrimes	f->font[0] = 'R';
6591590Srgrimes    if (bodyf.size == 0)
6601590Srgrimes	bodyf.size = 11;
6611590Srgrimes    if (f->size == 0)
6621590Srgrimes	f->size = bodyf.size + sizedelta;
6631590Srgrimes    else if (sizedelta > 0)
6641590Srgrimes	f->size += bodyf.size;
6651590Srgrimes    else
6661590Srgrimes	f->size = bodyf.size - f->size;
6671590Srgrimes}
668