xref: /csrg-svn/usr.bin/indent/io.c (revision 42688)
121969Sdist /*
235500Sbostic  * Copyright (c) 1985 Sun Microsystems, Inc.
335500Sbostic  * Copyright (c) 1980 The Regents of the University of California.
433767Sbostic  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
533767Sbostic  * All rights reserved.
633767Sbostic  *
7*42688Sbostic  * %sccs.include.redist.c%
821969Sdist  */
98803Smckusick 
1021969Sdist #ifndef lint
11*42688Sbostic static char sccsid[] = "@(#)io.c	5.14 (Berkeley) 06/01/90";
1233767Sbostic #endif /* not lint */
1321969Sdist 
1435503Sbostic #include "indent_globs.h"
1535500Sbostic #include <ctype.h>
168803Smckusick 
178803Smckusick 
1824454Smckusick int         comment_open;
1924454Smckusick static      paren_target;
208803Smckusick 
2124454Smckusick dump_line()
2224454Smckusick {				/* dump_line is the routine that actually
2335500Sbostic 				 * effects the printing of the new source. It
2435500Sbostic 				 * prints the label section, followed by the
2535500Sbostic 				 * code section with the appropriate nesting
2635500Sbostic 				 * level, followed by any comments */
2724454Smckusick     register int cur_col,
2824454Smckusick                 target_col;
2935500Sbostic     static      not_first_line;
308803Smckusick 
3124454Smckusick     if (ps.procname[0]) {
3235500Sbostic 	if (troff) {
3335500Sbostic 	    if (comment_open) {
3435500Sbostic 		comment_open = 0;
3535500Sbostic 		fprintf(output, ".*/\n");
3635500Sbostic 	    }
3724454Smckusick 	    fprintf(output, ".Pr \"%s\"\n", ps.procname);
3835500Sbostic 	}
3924454Smckusick 	ps.ind_level = 0;
4024454Smckusick 	ps.procname[0] = 0;
4124454Smckusick     }
4224454Smckusick     if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
4335500Sbostic 	if (suppress_blanklines > 0)
4435500Sbostic 	    suppress_blanklines--;
4524454Smckusick 	else {
4635500Sbostic 	    ps.bl_line = true;
4735500Sbostic 	    n_real_blanklines++;
4824454Smckusick 	}
4924454Smckusick     }
5024454Smckusick     else if (!inhibit_formatting) {
5124454Smckusick 	suppress_blanklines = 0;
5224454Smckusick 	ps.bl_line = false;
5335500Sbostic 	if (prefix_blankline_requested && not_first_line)
5424454Smckusick 	    if (swallow_optional_blanklines) {
5524454Smckusick 		if (n_real_blanklines == 1)
5624454Smckusick 		    n_real_blanklines = 0;
5724454Smckusick 	    }
5824454Smckusick 	    else {
5924454Smckusick 		if (n_real_blanklines == 0)
6024454Smckusick 		    n_real_blanklines = 1;
6124454Smckusick 	    }
6224454Smckusick 	while (--n_real_blanklines >= 0)
6324454Smckusick 	    putc('\n', output);
6424454Smckusick 	n_real_blanklines = 0;
6524454Smckusick 	if (ps.ind_level == 0)
6624454Smckusick 	    ps.ind_stmt = 0;	/* this is a class A kludge. dont do
6735500Sbostic 				 * additional statement indentation if we are
6835500Sbostic 				 * at bracket level 0 */
698803Smckusick 
7024454Smckusick 	if (e_lab != s_lab || e_code != s_code)
7124454Smckusick 	    ++code_lines;	/* keep count of lines with code */
728803Smckusick 
738803Smckusick 
7424454Smckusick 	if (e_lab != s_lab) {	/* print lab, if any */
7524454Smckusick 	    if (comment_open) {
7624454Smckusick 		comment_open = 0;
7724454Smckusick 		fprintf(output, ".*/\n");
7824454Smckusick 	    }
7924454Smckusick 	    while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
8024454Smckusick 		e_lab--;
8124454Smckusick 	    cur_col = pad_output(1, compute_label_target());
8238011Sbostic 	    if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0
8338011Sbostic 				    || strncmp(s_lab, "#endif", 6) == 0)) {
8438011Sbostic 		register char *s = s_lab;
8538011Sbostic 		if (e_lab[-1] == '\n') e_lab--;
8638011Sbostic 		do putc(*s++, output);
8738011Sbostic 		while (s < e_lab && 'a' <= *s && *s<='z');
8838011Sbostic 		while ((*s == ' ' || *s == '\t') && s < e_lab)
8938011Sbostic 		    s++;
9038011Sbostic 		if (s < e_lab)
9138011Sbostic 		    fprintf(output, s[0]=='/' && s[1]=='*' ? "\t%.*s" : "\t/* %.*s */",
9238011Sbostic 			    e_lab - s, s);
9338011Sbostic 	    }
9438011Sbostic 	    else fprintf(output, "%.*s", e_lab - s_lab, s_lab);
9524454Smckusick 	    cur_col = count_spaces(cur_col, s_lab);
968803Smckusick 	}
9724454Smckusick 	else
9824454Smckusick 	    cur_col = 1;	/* there is no label section */
998803Smckusick 
10024454Smckusick 	ps.pcase = false;
1018803Smckusick 
10224454Smckusick 	if (s_code != e_code) {	/* print code section, if any */
10324454Smckusick 	    register char *p;
1048803Smckusick 
10524454Smckusick 	    if (comment_open) {
10624454Smckusick 		comment_open = 0;
10724454Smckusick 		fprintf(output, ".*/\n");
10824454Smckusick 	    }
10924454Smckusick 	    target_col = compute_code_target();
11024454Smckusick 	    {
11124454Smckusick 		register    i;
1128803Smckusick 
11324454Smckusick 		for (i = 0; i < ps.p_l_follow; i++)
11424454Smckusick 		    if (ps.paren_indents[i] >= 0)
11524454Smckusick 			ps.paren_indents[i] = -(ps.paren_indents[i] + target_col);
11624454Smckusick 	    }
11724454Smckusick 	    cur_col = pad_output(cur_col, target_col);
11824454Smckusick 	    for (p = s_code; p < e_code; p++)
11935500Sbostic 		if (*p == (char) 0200)
12024454Smckusick 		    fprintf(output, "%d", target_col * 7);
12124454Smckusick 		else
12224454Smckusick 		    putc(*p, output);
12324454Smckusick 	    cur_col = count_spaces(cur_col, s_code);
12424454Smckusick 	}
12524454Smckusick 	if (s_com != e_com)
12624454Smckusick 	    if (troff) {
12735500Sbostic 		int         all_here = 0;
12824454Smckusick 		register char *p;
1298803Smckusick 
13024454Smckusick 		if (e_com[-1] == '/' && e_com[-2] == '*')
13135500Sbostic 		    e_com -= 2, all_here++;
13224454Smckusick 		while (e_com > s_com && e_com[-1] == ' ')
13324454Smckusick 		    e_com--;
13424454Smckusick 		*e_com = 0;
13524454Smckusick 		p = s_com;
13624454Smckusick 		while (*p == ' ')
13724454Smckusick 		    p++;
13824454Smckusick 		if (p[0] == '/' && p[1] == '*')
13935500Sbostic 		    p += 2, all_here++;
14024454Smckusick 		else if (p[0] == '*')
14124454Smckusick 		    p += p[1] == '/' ? 2 : 1;
14224454Smckusick 		while (*p == ' ')
14324454Smckusick 		    p++;
14424454Smckusick 		if (*p == 0)
14524454Smckusick 		    goto inhibit_newline;
14635500Sbostic 		if (comment_open < 2 && ps.box_com) {
14735500Sbostic 		    comment_open = 0;
14835500Sbostic 		    fprintf(output, ".*/\n");
14935500Sbostic 		}
15035500Sbostic 		if (comment_open == 0) {
15124454Smckusick 		    if ('a' <= *p && *p <= 'z')
15224454Smckusick 			*p = *p + 'A' - 'a';
15335500Sbostic 		    if (e_com - p < 50 && all_here == 2) {
15435500Sbostic 			register char *follow = p;
15535500Sbostic 			fprintf(output, "\n.nr C! \\w\1");
15635500Sbostic 			while (follow < e_com) {
15735500Sbostic 			    switch (*follow) {
15835500Sbostic 			    case '\n':
15935500Sbostic 				putc(' ', output);
16035500Sbostic 			    case 1:
16135500Sbostic 				break;
16235500Sbostic 			    case '\\':
16335500Sbostic 				putc('\\', output);
16435500Sbostic 			    default:
16535500Sbostic 				putc(*follow, output);
16635500Sbostic 			    }
16735500Sbostic 			    follow++;
16835500Sbostic 			}
16935500Sbostic 			putc(1, output);
17024454Smckusick 		    }
17135500Sbostic 		    fprintf(output, "\n./* %dp %d %dp\n",
17235500Sbostic 			    ps.com_col * 7,
17335500Sbostic 			    (s_code != e_code || s_lab != e_lab) - ps.box_com,
17435500Sbostic 			    target_col * 7);
17524454Smckusick 		}
17635500Sbostic 		comment_open = 1 + ps.box_com;
17724454Smckusick 		while (*p) {
17824454Smckusick 		    if (*p == BACKSLASH)
17924454Smckusick 			putc(BACKSLASH, output);
18024454Smckusick 		    putc(*p++, output);
18124454Smckusick 		}
18224454Smckusick 	    }
18324454Smckusick 	    else {		/* print comment, if any */
18424454Smckusick 		register    target = ps.com_col;
18524454Smckusick 		register char *com_st = s_com;
1868803Smckusick 
18724454Smckusick 		target += ps.comment_delta;
18835500Sbostic 		while (*com_st == '\t')
18935500Sbostic 		    com_st++, target += 8;	/* ? */
19024454Smckusick 		while (target <= 0)
19135500Sbostic 		    if (*com_st == ' ')
19235500Sbostic 			target++, com_st++;
19335500Sbostic 		    else if (*com_st == '\t')
19435500Sbostic 			target = ((target - 1) & ~7) + 9, com_st++;
19524454Smckusick 		    else
19624454Smckusick 			target = 1;
19735500Sbostic 		if (cur_col > target) {	/* if comment cant fit on this line,
19835500Sbostic 					 * put it on next line */
19924454Smckusick 		    putc('\n', output);
20024454Smckusick 		    cur_col = 1;
20124454Smckusick 		    ++ps.out_lines;
20224454Smckusick 		}
20335500Sbostic 		while (e_com > com_st && isspace(e_com[-1]))
20435500Sbostic 		    e_com--;
20524454Smckusick 		cur_col = pad_output(cur_col, target);
20624454Smckusick 		if (!ps.box_com) {
20735500Sbostic 		    if (star_comment_cont && (com_st[1] != '*' || e_com <= com_st + 1))
20835500Sbostic 			if (com_st[1] == ' ' && com_st[0] == ' ' && e_com > com_st + 1)
20924454Smckusick 			    com_st[1] = '*';
21024454Smckusick 			else
21124454Smckusick 			    fwrite(" * ", com_st[0] == '\t' ? 2 : com_st[0] == '*' ? 1 : 3, 1, output);
21224454Smckusick 		}
21324454Smckusick 		fwrite(com_st, e_com - com_st, 1, output);
21424454Smckusick 		ps.comment_delta = ps.n_comment_delta;
21524454Smckusick 		cur_col = count_spaces(cur_col, com_st);
21624454Smckusick 		++ps.com_lines;	/* count lines with comments */
21724454Smckusick 	    }
21824454Smckusick 	if (ps.use_ff)
21924454Smckusick 	    putc('\014', output);
22024454Smckusick 	else
22124454Smckusick 	    putc('\n', output);
22224454Smckusick inhibit_newline:
22324454Smckusick 	++ps.out_lines;
22424454Smckusick 	if (ps.just_saw_decl == 1 && blanklines_after_declarations) {
22524454Smckusick 	    prefix_blankline_requested = 1;
22624454Smckusick 	    ps.just_saw_decl = 0;
2278803Smckusick 	}
22824454Smckusick 	else
22924454Smckusick 	    prefix_blankline_requested = postfix_blankline_requested;
23024454Smckusick 	postfix_blankline_requested = 0;
2318803Smckusick     }
23224454Smckusick     ps.decl_on_line = ps.in_decl;	/* if we are in the middle of a
23335500Sbostic 					 * declaration, remember that fact for
23435500Sbostic 					 * proper comment indentation */
23524454Smckusick     ps.ind_stmt = ps.in_stmt & ~ps.in_decl;	/* next line should be
23624454Smckusick 						 * indented if we have not
23735500Sbostic 						 * completed this stmt and if
23835500Sbostic 						 * we are not in the middle of
23935500Sbostic 						 * a declaration */
24024454Smckusick     ps.use_ff = false;
24124454Smckusick     ps.dumped_decl_indent = 0;
24224454Smckusick     *(e_lab = s_lab) = '\0';	/* reset buffers */
2438803Smckusick     *(e_code = s_code) = '\0';
2448803Smckusick     *(e_com = s_com) = '\0';
24524454Smckusick     ps.ind_level = ps.i_l_follow;
24624454Smckusick     ps.paren_level = ps.p_l_follow;
24724454Smckusick     paren_target = -ps.paren_indents[ps.paren_level - 1];
24835500Sbostic     not_first_line = 1;
2498803Smckusick     return;
25036972Sbostic }
2518803Smckusick 
25235500Sbostic compute_code_target()
25335500Sbostic {
25424454Smckusick     register    target_col = ps.ind_size * ps.ind_level + 1;
2558803Smckusick 
25624454Smckusick     if (ps.paren_level)
25724454Smckusick 	if (!lineup_to_parens)
25824454Smckusick 	    target_col += continuation_indent * ps.paren_level;
25924454Smckusick 	else {
26024454Smckusick 	    register    w;
26124454Smckusick 	    register    t = paren_target;
2628803Smckusick 
26324454Smckusick 	    if ((w = count_spaces(t, s_code) - max_col) > 0
26435500Sbostic 		    && count_spaces(target_col, s_code) <= max_col) {
26524454Smckusick 		t -= w + 1;
26624454Smckusick 		if (t > target_col)
26724454Smckusick 		    target_col = t;
26824454Smckusick 	    }
26924454Smckusick 	    else
27024454Smckusick 		target_col = t;
27124454Smckusick 	}
27224454Smckusick     else if (ps.ind_stmt)
27324454Smckusick 	target_col += continuation_indent;
27424454Smckusick     return target_col;
27524454Smckusick }
2768803Smckusick 
27724454Smckusick compute_label_target()
27824454Smckusick {
27924454Smckusick     return
28035500Sbostic 	ps.pcase ? (int) (case_ind * ps.ind_size) + 1
28124454Smckusick 	: *s_lab == '#' ? 1
28235500Sbostic 	: ps.ind_size * (ps.ind_level - label_offset) + 1;
28324454Smckusick }
2848803Smckusick 
2858803Smckusick 
28624454Smckusick /*
28735500Sbostic  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
28835500Sbostic  *
28935500Sbostic  * All rights reserved
29035500Sbostic  *
29135500Sbostic  *
29235500Sbostic  * NAME: fill_buffer
29335500Sbostic  *
29435500Sbostic  * FUNCTION: Reads one block of input into input_buffer
29535500Sbostic  *
29635500Sbostic  * HISTORY: initial coding 	November 1976	D A Willcox of CAC 1/7/77 A
29735500Sbostic  * Willcox of CAC	Added check for switch back to partly full input
29835500Sbostic  * buffer from temporary buffer
29935500Sbostic  *
30038011Sbostic  */
30124454Smckusick int
30224454Smckusick fill_buffer()
30324454Smckusick {				/* this routine reads stuff from the input */
30424454Smckusick     register char *p;
30524454Smckusick     register int i;
30624454Smckusick     register FILE *f = input;
3078803Smckusick 
30835500Sbostic     if (bp_save != 0) {		/* there is a partly filled input buffer left */
30924454Smckusick 	buf_ptr = bp_save;	/* dont read anything, just switch buffers */
3108803Smckusick 	buf_end = be_save;
3118803Smckusick 	bp_save = be_save = 0;
3128803Smckusick 	if (buf_ptr < buf_end)
31335500Sbostic 	    return;		/* only return if there is really something in
31435500Sbostic 				 * this buffer */
3158803Smckusick     }
31638011Sbostic     for (p = in_buffer;;) {
31738011Sbostic 	if (p >= in_buffer_limit) {
31838011Sbostic 	    register size = (in_buffer_limit - in_buffer) * 2 + 10;
31938011Sbostic 	    register offset = p - in_buffer;
32038011Sbostic 	    in_buffer = (char *) realloc(in_buffer, size);
32138011Sbostic 	    if (in_buffer == 0)
32238011Sbostic 		err("input line too long");
32338011Sbostic 	    p = in_buffer + offset;
32438011Sbostic 	    in_buffer_limit = in_buffer + size - 2;
32538011Sbostic 	}
32635510Sbostic 	if ((i = getc(f)) == EOF) {
32735510Sbostic 		*p++ = ' ';
32835510Sbostic 		*p++ = '\n';
32935510Sbostic 		had_eof = true;
33035510Sbostic 		break;
33135510Sbostic 	}
33235510Sbostic 	*p++ = i;
33335510Sbostic 	if (i == '\n')
33435510Sbostic 		break;
3358803Smckusick     }
33638011Sbostic     buf_ptr = in_buffer;
33724454Smckusick     buf_end = p;
33836972Sbostic     if (p[-2] == '/' && p[-3] == '*') {
33924454Smckusick 	if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
34024454Smckusick 	    fill_buffer();	/* flush indent error message */
34124454Smckusick 	else {
34224454Smckusick 	    int         com = 0;
3438803Smckusick 
34424454Smckusick 	    p = in_buffer;
34524454Smckusick 	    while (*p == ' ' || *p == '\t')
34624454Smckusick 		p++;
34724454Smckusick 	    if (*p == '/' && p[1] == '*') {
34824454Smckusick 		p += 2;
34924454Smckusick 		while (*p == ' ' || *p == '\t')
35024454Smckusick 		    p++;
35124454Smckusick 		if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E'
35235500Sbostic 			&& p[4] == 'N' && p[5] == 'T') {
35324454Smckusick 		    p += 6;
35424454Smckusick 		    while (*p == ' ' || *p == '\t')
35524454Smckusick 			p++;
35624454Smckusick 		    if (*p == '*')
35724454Smckusick 			com = 1;
35824454Smckusick 		    else if (*p == 'O')
35924454Smckusick 			if (*++p == 'N')
36024454Smckusick 			    p++, com = 1;
36124454Smckusick 			else if (*p == 'F' && *++p == 'F')
36224454Smckusick 			    p++, com = 2;
36324454Smckusick 		    while (*p == ' ' || *p == '\t')
36424454Smckusick 			p++;
36524454Smckusick 		    if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) {
36624454Smckusick 			if (s_com != e_com || s_lab != e_lab || s_code != e_code)
36724454Smckusick 			    dump_line();
36824454Smckusick 			if (!(inhibit_formatting = com - 1)) {
36924454Smckusick 			    n_real_blanklines = 0;
37024454Smckusick 			    postfix_blankline_requested = 0;
37124454Smckusick 			    prefix_blankline_requested = 0;
37224454Smckusick 			    suppress_blanklines = 1;
37324454Smckusick 			}
37424454Smckusick 		    }
37524454Smckusick 		}
37624454Smckusick 	    }
37724454Smckusick 	}
37824454Smckusick     }
37924454Smckusick     if (inhibit_formatting) {
38024454Smckusick 	p = in_buffer;
38124454Smckusick 	do
38224454Smckusick 	    putc(*p, output);
38324454Smckusick 	while (*p++ != '\n');
38424454Smckusick     }
3858803Smckusick     return;
38636972Sbostic }
3878803Smckusick 
38824454Smckusick /*
38935500Sbostic  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
39035500Sbostic  *
39135500Sbostic  * All rights reserved
39235500Sbostic  *
39335500Sbostic  *
39435500Sbostic  * NAME: pad_output
39535500Sbostic  *
39635500Sbostic  * FUNCTION: Writes tabs and spaces to move the current column up to the desired
39735500Sbostic  * position.
39835500Sbostic  *
39935500Sbostic  * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf.
40035500Sbostic  *
40124454Smckusick  * PARAMETERS: current		integer		The current column target
40235500Sbostic  * nteger		The desired column
40335500Sbostic  *
40435500Sbostic  * RETURNS: Integer value of the new column.  (If current >= target, no action is
40535500Sbostic  * taken, and current is returned.
40635500Sbostic  *
40735500Sbostic  * GLOBALS: None
40835500Sbostic  *
40935500Sbostic  * CALLS: write (sys)
41035500Sbostic  *
41135500Sbostic  * CALLED BY: dump_line
41235500Sbostic  *
41335500Sbostic  * HISTORY: initial coding 	November 1976	D A Willcox of CAC
41435500Sbostic  *
41538011Sbostic  */
41635500Sbostic pad_output(current, target)	/* writes tabs and blanks (if necessary) to
41735500Sbostic 				 * get the current output position up to the
41835500Sbostic 				 * target column */
41924454Smckusick     int         current;	/* the current column value */
42024454Smckusick     int         target;		/* position we want it at */
4218803Smckusick {
42224454Smckusick     register int curr;		/* internal column pointer */
4238803Smckusick     register int tcur;
4248803Smckusick 
42524454Smckusick     if (troff)
42624454Smckusick 	fprintf(output, "\\h'|%dp'", (target - 1) * 7);
42724454Smckusick     else {
42824454Smckusick 	if (current >= target)
42924454Smckusick 	    return (current);	/* line is already long enough */
43024454Smckusick 	curr = current;
43124454Smckusick 	while ((tcur = ((curr - 1) & tabmask) + tabsize + 1) <= target) {
43224454Smckusick 	    putc('\t', output);
4338803Smckusick 	    curr = tcur;
4348803Smckusick 	}
43524454Smckusick 	while (curr++ < target)
43624454Smckusick 	    putc(' ', output);	/* pad with final blanks */
4378803Smckusick     }
4388803Smckusick     return (target);
43936972Sbostic }
4408803Smckusick 
44124454Smckusick /*
44235500Sbostic  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
44335500Sbostic  *
44435500Sbostic  * All rights reserved
44535500Sbostic  *
44635500Sbostic  *
44735500Sbostic  * NAME: count_spaces
44835500Sbostic  *
44924454Smckusick  * FUNCTION: Find out where printing of a given string will leave the current
45035500Sbostic  * character position on output.
45135500Sbostic  *
45224454Smckusick  * ALGORITHM: Run thru input string and add appropriate values to current
45335500Sbostic  * position.
45435500Sbostic  *
45535500Sbostic  * RETURNS: Integer value of position after printing "buffer" starting in column
45635500Sbostic  * "current".
45735500Sbostic  *
45835500Sbostic  * HISTORY: initial coding 	November 1976	D A Willcox of CAC
45935500Sbostic  *
46038011Sbostic  */
46124454Smckusick int
46224454Smckusick count_spaces(current, buffer)
46324454Smckusick /*
46424454Smckusick  * this routine figures out where the character position will be after
46535500Sbostic  * printing the text in buffer starting at column "current"
46624454Smckusick  */
46724454Smckusick     int         current;
46824454Smckusick     char       *buffer;
4698803Smckusick {
47024454Smckusick     register char *buf;		/* used to look thru buffer */
47124454Smckusick     register int cur;		/* current character counter */
4728803Smckusick 
4738803Smckusick     cur = current;
4748803Smckusick 
4758803Smckusick     for (buf = buffer; *buf != '\0'; ++buf) {
4768803Smckusick 	switch (*buf) {
4778803Smckusick 
47835500Sbostic 	case '\n':
47935500Sbostic 	case 014:		/* form feed */
48035500Sbostic 	    cur = 1;
48135500Sbostic 	    break;
4828803Smckusick 
48335500Sbostic 	case '\t':
48435500Sbostic 	    cur = ((cur - 1) & tabmask) + tabsize + 1;
48535500Sbostic 	    break;
4868803Smckusick 
48736972Sbostic 	case 010:		/* backspace */
48835500Sbostic 	    --cur;
48935500Sbostic 	    break;
4908803Smckusick 
49135500Sbostic 	default:
49235500Sbostic 	    ++cur;
49335500Sbostic 	    break;
49424454Smckusick 	}			/* end of switch */
49524454Smckusick     }				/* end of for loop */
4968803Smckusick     return (cur);
49736972Sbostic }
4988803Smckusick 
49935503Sbostic int	found_err;
50036972Sbostic /* VARARGS2 */
50124454Smckusick diag(level, msg, a, b)
5028803Smckusick {
50335503Sbostic     if (level)
50435503Sbostic 	found_err = 1;
50524454Smckusick     if (output == stdout) {
50624454Smckusick 	fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
50724454Smckusick 	fprintf(stdout, msg, a, b);
50824454Smckusick 	fprintf(stdout, " */\n");
5098803Smckusick     }
51024454Smckusick     else {
51124454Smckusick 	fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
51224454Smckusick 	fprintf(stderr, msg, a, b);
51324454Smckusick 	fprintf(stderr, "\n");
5148803Smckusick     }
5158803Smckusick }
51635500Sbostic 
51735500Sbostic writefdef(f, nm)
51835500Sbostic     register struct fstate *f;
51935500Sbostic {
52035500Sbostic     fprintf(output, ".ds f%c %s\n.nr s%c %d\n",
52135500Sbostic 	    nm, f->font, nm, f->size);
52235500Sbostic }
52335500Sbostic 
52435500Sbostic char       *
52535500Sbostic chfont(of, nf, s)
52635500Sbostic     register struct fstate *of,
52735500Sbostic                *nf;
52835500Sbostic     char       *s;
52935500Sbostic {
53035500Sbostic     if (of->font[0] != nf->font[0]
53135500Sbostic 	    || of->font[1] != nf->font[1]) {
53235500Sbostic 	*s++ = '\\';
53335500Sbostic 	*s++ = 'f';
53435500Sbostic 	if (nf->font[1]) {
53535500Sbostic 	    *s++ = '(';
53635500Sbostic 	    *s++ = nf->font[0];
53735500Sbostic 	    *s++ = nf->font[1];
53835500Sbostic 	}
53935500Sbostic 	else
54035500Sbostic 	    *s++ = nf->font[0];
54135500Sbostic     }
54235500Sbostic     if (nf->size != of->size) {
54335500Sbostic 	*s++ = '\\';
54435500Sbostic 	*s++ = 's';
54535500Sbostic 	if (nf->size < of->size) {
54635500Sbostic 	    *s++ = '-';
54735500Sbostic 	    *s++ = '0' + of->size - nf->size;
54835500Sbostic 	}
54935500Sbostic 	else {
55035500Sbostic 	    *s++ = '+';
55135500Sbostic 	    *s++ = '0' + nf->size - of->size;
55235500Sbostic 	}
55335500Sbostic     }
55435500Sbostic     return s;
55535500Sbostic }
55635500Sbostic 
55735500Sbostic 
55835500Sbostic parsefont(f, s0)
55935500Sbostic     register struct fstate *f;
56035500Sbostic     char       *s0;
56135500Sbostic {
56235500Sbostic     register char *s = s0;
56335500Sbostic     int         sizedelta = 0;
56435500Sbostic     bzero(f, sizeof *f);
56535500Sbostic     while (*s) {
56635500Sbostic 	if (isdigit(*s))
56735500Sbostic 	    f->size = f->size * 10 + *s - '0';
56835500Sbostic 	else if (isupper(*s))
56935500Sbostic 	    if (f->font[0])
57035500Sbostic 		f->font[1] = *s;
57135500Sbostic 	    else
57235500Sbostic 		f->font[0] = *s;
57335500Sbostic 	else if (*s == 'c')
57435500Sbostic 	    f->allcaps = 1;
57535500Sbostic 	else if (*s == '+')
57635500Sbostic 	    sizedelta++;
57735500Sbostic 	else if (*s == '-')
57835500Sbostic 	    sizedelta--;
57935500Sbostic 	else {
58035500Sbostic 	    fprintf(stderr, "indent: bad font specification: %s\n", s0);
58135500Sbostic 	    exit(1);
58235500Sbostic 	}
58335500Sbostic 	s++;
58435500Sbostic     }
58535500Sbostic     if (f->font[0] == 0)
58635500Sbostic 	f->font[0] = 'R';
58735500Sbostic     if (bodyf.size == 0)
58835500Sbostic 	bodyf.size = 11;
58935500Sbostic     if (f->size == 0)
59035500Sbostic 	f->size = bodyf.size + sizedelta;
59135500Sbostic     else if (sizedelta > 0)
59235500Sbostic 	f->size += bodyf.size;
59335500Sbostic     else
59435500Sbostic 	f->size = bodyf.size - f->size;
59535500Sbostic }
596