xref: /csrg-svn/usr.bin/indent/io.c (revision 62038)
121969Sdist /*
235500Sbostic  * Copyright (c) 1985 Sun Microsystems, Inc.
3*62038Sbostic  * Copyright (c) 1980, 1993
4*62038Sbostic  *	The Regents of the University of California.  All rights reserved.
533767Sbostic  * All rights reserved.
633767Sbostic  *
742688Sbostic  * %sccs.include.redist.c%
821969Sdist  */
98803Smckusick 
1021969Sdist #ifndef lint
11*62038Sbostic static char sccsid[] = "@(#)io.c	8.1 (Berkeley) 06/06/93";
1233767Sbostic #endif /* not lint */
1321969Sdist 
1446695Sbostic #include <stdio.h>
1546695Sbostic #include <ctype.h>
1646695Sbostic #include <stdlib.h>
1746695Sbostic #include <string.h>
1835503Sbostic #include "indent_globs.h"
198803Smckusick 
208803Smckusick 
2124454Smckusick int         comment_open;
2224454Smckusick static      paren_target;
238803Smckusick 
dump_line()2424454Smckusick dump_line()
2524454Smckusick {				/* dump_line is the routine that actually
2635500Sbostic 				 * effects the printing of the new source. It
2735500Sbostic 				 * prints the label section, followed by the
2835500Sbostic 				 * code section with the appropriate nesting
2935500Sbostic 				 * level, followed by any comments */
3024454Smckusick     register int cur_col,
3124454Smckusick                 target_col;
3235500Sbostic     static      not_first_line;
338803Smckusick 
3424454Smckusick     if (ps.procname[0]) {
3535500Sbostic 	if (troff) {
3635500Sbostic 	    if (comment_open) {
3735500Sbostic 		comment_open = 0;
3835500Sbostic 		fprintf(output, ".*/\n");
3935500Sbostic 	    }
4024454Smckusick 	    fprintf(output, ".Pr \"%s\"\n", ps.procname);
4135500Sbostic 	}
4224454Smckusick 	ps.ind_level = 0;
4324454Smckusick 	ps.procname[0] = 0;
4424454Smckusick     }
4524454Smckusick     if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
4635500Sbostic 	if (suppress_blanklines > 0)
4735500Sbostic 	    suppress_blanklines--;
4824454Smckusick 	else {
4935500Sbostic 	    ps.bl_line = true;
5035500Sbostic 	    n_real_blanklines++;
5124454Smckusick 	}
5224454Smckusick     }
5324454Smckusick     else if (!inhibit_formatting) {
5424454Smckusick 	suppress_blanklines = 0;
5524454Smckusick 	ps.bl_line = false;
5635500Sbostic 	if (prefix_blankline_requested && not_first_line)
5724454Smckusick 	    if (swallow_optional_blanklines) {
5824454Smckusick 		if (n_real_blanklines == 1)
5924454Smckusick 		    n_real_blanklines = 0;
6024454Smckusick 	    }
6124454Smckusick 	    else {
6224454Smckusick 		if (n_real_blanklines == 0)
6324454Smckusick 		    n_real_blanklines = 1;
6424454Smckusick 	    }
6524454Smckusick 	while (--n_real_blanklines >= 0)
6624454Smckusick 	    putc('\n', output);
6724454Smckusick 	n_real_blanklines = 0;
6824454Smckusick 	if (ps.ind_level == 0)
6924454Smckusick 	    ps.ind_stmt = 0;	/* this is a class A kludge. dont do
7035500Sbostic 				 * additional statement indentation if we are
7135500Sbostic 				 * at bracket level 0 */
728803Smckusick 
7324454Smckusick 	if (e_lab != s_lab || e_code != s_code)
7424454Smckusick 	    ++code_lines;	/* keep count of lines with code */
758803Smckusick 
768803Smckusick 
7724454Smckusick 	if (e_lab != s_lab) {	/* print lab, if any */
7824454Smckusick 	    if (comment_open) {
7924454Smckusick 		comment_open = 0;
8024454Smckusick 		fprintf(output, ".*/\n");
8124454Smckusick 	    }
8224454Smckusick 	    while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
8324454Smckusick 		e_lab--;
8424454Smckusick 	    cur_col = pad_output(1, compute_label_target());
8538011Sbostic 	    if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0
8638011Sbostic 				    || strncmp(s_lab, "#endif", 6) == 0)) {
8738011Sbostic 		register char *s = s_lab;
8838011Sbostic 		if (e_lab[-1] == '\n') e_lab--;
8938011Sbostic 		do putc(*s++, output);
9038011Sbostic 		while (s < e_lab && 'a' <= *s && *s<='z');
9138011Sbostic 		while ((*s == ' ' || *s == '\t') && s < e_lab)
9238011Sbostic 		    s++;
9338011Sbostic 		if (s < e_lab)
9438011Sbostic 		    fprintf(output, s[0]=='/' && s[1]=='*' ? "\t%.*s" : "\t/* %.*s */",
9538011Sbostic 			    e_lab - s, s);
9638011Sbostic 	    }
9738011Sbostic 	    else fprintf(output, "%.*s", e_lab - s_lab, s_lab);
9824454Smckusick 	    cur_col = count_spaces(cur_col, s_lab);
998803Smckusick 	}
10024454Smckusick 	else
10124454Smckusick 	    cur_col = 1;	/* there is no label section */
1028803Smckusick 
10324454Smckusick 	ps.pcase = false;
1048803Smckusick 
10524454Smckusick 	if (s_code != e_code) {	/* print code section, if any */
10624454Smckusick 	    register char *p;
1078803Smckusick 
10824454Smckusick 	    if (comment_open) {
10924454Smckusick 		comment_open = 0;
11024454Smckusick 		fprintf(output, ".*/\n");
11124454Smckusick 	    }
11224454Smckusick 	    target_col = compute_code_target();
11324454Smckusick 	    {
11424454Smckusick 		register    i;
1158803Smckusick 
11624454Smckusick 		for (i = 0; i < ps.p_l_follow; i++)
11724454Smckusick 		    if (ps.paren_indents[i] >= 0)
11824454Smckusick 			ps.paren_indents[i] = -(ps.paren_indents[i] + target_col);
11924454Smckusick 	    }
12024454Smckusick 	    cur_col = pad_output(cur_col, target_col);
12124454Smckusick 	    for (p = s_code; p < e_code; p++)
12235500Sbostic 		if (*p == (char) 0200)
12324454Smckusick 		    fprintf(output, "%d", target_col * 7);
12424454Smckusick 		else
12524454Smckusick 		    putc(*p, output);
12624454Smckusick 	    cur_col = count_spaces(cur_col, s_code);
12724454Smckusick 	}
12824454Smckusick 	if (s_com != e_com)
12924454Smckusick 	    if (troff) {
13035500Sbostic 		int         all_here = 0;
13124454Smckusick 		register char *p;
1328803Smckusick 
13324454Smckusick 		if (e_com[-1] == '/' && e_com[-2] == '*')
13435500Sbostic 		    e_com -= 2, all_here++;
13524454Smckusick 		while (e_com > s_com && e_com[-1] == ' ')
13624454Smckusick 		    e_com--;
13724454Smckusick 		*e_com = 0;
13824454Smckusick 		p = s_com;
13924454Smckusick 		while (*p == ' ')
14024454Smckusick 		    p++;
14124454Smckusick 		if (p[0] == '/' && p[1] == '*')
14235500Sbostic 		    p += 2, all_here++;
14324454Smckusick 		else if (p[0] == '*')
14424454Smckusick 		    p += p[1] == '/' ? 2 : 1;
14524454Smckusick 		while (*p == ' ')
14624454Smckusick 		    p++;
14724454Smckusick 		if (*p == 0)
14824454Smckusick 		    goto inhibit_newline;
14935500Sbostic 		if (comment_open < 2 && ps.box_com) {
15035500Sbostic 		    comment_open = 0;
15135500Sbostic 		    fprintf(output, ".*/\n");
15235500Sbostic 		}
15335500Sbostic 		if (comment_open == 0) {
15424454Smckusick 		    if ('a' <= *p && *p <= 'z')
15524454Smckusick 			*p = *p + 'A' - 'a';
15635500Sbostic 		    if (e_com - p < 50 && all_here == 2) {
15735500Sbostic 			register char *follow = p;
15835500Sbostic 			fprintf(output, "\n.nr C! \\w\1");
15935500Sbostic 			while (follow < e_com) {
16035500Sbostic 			    switch (*follow) {
16135500Sbostic 			    case '\n':
16235500Sbostic 				putc(' ', output);
16335500Sbostic 			    case 1:
16435500Sbostic 				break;
16535500Sbostic 			    case '\\':
16635500Sbostic 				putc('\\', output);
16735500Sbostic 			    default:
16835500Sbostic 				putc(*follow, output);
16935500Sbostic 			    }
17035500Sbostic 			    follow++;
17135500Sbostic 			}
17235500Sbostic 			putc(1, output);
17324454Smckusick 		    }
17435500Sbostic 		    fprintf(output, "\n./* %dp %d %dp\n",
17535500Sbostic 			    ps.com_col * 7,
17635500Sbostic 			    (s_code != e_code || s_lab != e_lab) - ps.box_com,
17735500Sbostic 			    target_col * 7);
17824454Smckusick 		}
17935500Sbostic 		comment_open = 1 + ps.box_com;
18024454Smckusick 		while (*p) {
18124454Smckusick 		    if (*p == BACKSLASH)
18224454Smckusick 			putc(BACKSLASH, output);
18324454Smckusick 		    putc(*p++, output);
18424454Smckusick 		}
18524454Smckusick 	    }
18624454Smckusick 	    else {		/* print comment, if any */
18724454Smckusick 		register    target = ps.com_col;
18824454Smckusick 		register char *com_st = s_com;
1898803Smckusick 
19024454Smckusick 		target += ps.comment_delta;
19135500Sbostic 		while (*com_st == '\t')
19235500Sbostic 		    com_st++, target += 8;	/* ? */
19324454Smckusick 		while (target <= 0)
19435500Sbostic 		    if (*com_st == ' ')
19535500Sbostic 			target++, com_st++;
19635500Sbostic 		    else if (*com_st == '\t')
19735500Sbostic 			target = ((target - 1) & ~7) + 9, com_st++;
19824454Smckusick 		    else
19924454Smckusick 			target = 1;
20035500Sbostic 		if (cur_col > target) {	/* if comment cant fit on this line,
20135500Sbostic 					 * put it on next line */
20224454Smckusick 		    putc('\n', output);
20324454Smckusick 		    cur_col = 1;
20424454Smckusick 		    ++ps.out_lines;
20524454Smckusick 		}
20635500Sbostic 		while (e_com > com_st && isspace(e_com[-1]))
20735500Sbostic 		    e_com--;
20824454Smckusick 		cur_col = pad_output(cur_col, target);
20924454Smckusick 		if (!ps.box_com) {
21035500Sbostic 		    if (star_comment_cont && (com_st[1] != '*' || e_com <= com_st + 1))
21135500Sbostic 			if (com_st[1] == ' ' && com_st[0] == ' ' && e_com > com_st + 1)
21224454Smckusick 			    com_st[1] = '*';
21324454Smckusick 			else
21424454Smckusick 			    fwrite(" * ", com_st[0] == '\t' ? 2 : com_st[0] == '*' ? 1 : 3, 1, output);
21524454Smckusick 		}
21624454Smckusick 		fwrite(com_st, e_com - com_st, 1, output);
21724454Smckusick 		ps.comment_delta = ps.n_comment_delta;
21824454Smckusick 		cur_col = count_spaces(cur_col, com_st);
21924454Smckusick 		++ps.com_lines;	/* count lines with comments */
22024454Smckusick 	    }
22124454Smckusick 	if (ps.use_ff)
22224454Smckusick 	    putc('\014', output);
22324454Smckusick 	else
22424454Smckusick 	    putc('\n', output);
22524454Smckusick inhibit_newline:
22624454Smckusick 	++ps.out_lines;
22724454Smckusick 	if (ps.just_saw_decl == 1 && blanklines_after_declarations) {
22824454Smckusick 	    prefix_blankline_requested = 1;
22924454Smckusick 	    ps.just_saw_decl = 0;
2308803Smckusick 	}
23124454Smckusick 	else
23224454Smckusick 	    prefix_blankline_requested = postfix_blankline_requested;
23324454Smckusick 	postfix_blankline_requested = 0;
2348803Smckusick     }
23524454Smckusick     ps.decl_on_line = ps.in_decl;	/* if we are in the middle of a
23635500Sbostic 					 * declaration, remember that fact for
23735500Sbostic 					 * proper comment indentation */
23824454Smckusick     ps.ind_stmt = ps.in_stmt & ~ps.in_decl;	/* next line should be
23924454Smckusick 						 * indented if we have not
24035500Sbostic 						 * completed this stmt and if
24135500Sbostic 						 * we are not in the middle of
24235500Sbostic 						 * a declaration */
24324454Smckusick     ps.use_ff = false;
24424454Smckusick     ps.dumped_decl_indent = 0;
24524454Smckusick     *(e_lab = s_lab) = '\0';	/* reset buffers */
2468803Smckusick     *(e_code = s_code) = '\0';
2478803Smckusick     *(e_com = s_com) = '\0';
24824454Smckusick     ps.ind_level = ps.i_l_follow;
24924454Smckusick     ps.paren_level = ps.p_l_follow;
25024454Smckusick     paren_target = -ps.paren_indents[ps.paren_level - 1];
25135500Sbostic     not_first_line = 1;
2528803Smckusick     return;
25336972Sbostic }
2548803Smckusick 
compute_code_target()25535500Sbostic compute_code_target()
25635500Sbostic {
25724454Smckusick     register    target_col = ps.ind_size * ps.ind_level + 1;
2588803Smckusick 
25924454Smckusick     if (ps.paren_level)
26024454Smckusick 	if (!lineup_to_parens)
26124454Smckusick 	    target_col += continuation_indent * ps.paren_level;
26224454Smckusick 	else {
26324454Smckusick 	    register    w;
26424454Smckusick 	    register    t = paren_target;
2658803Smckusick 
26624454Smckusick 	    if ((w = count_spaces(t, s_code) - max_col) > 0
26735500Sbostic 		    && count_spaces(target_col, s_code) <= max_col) {
26824454Smckusick 		t -= w + 1;
26924454Smckusick 		if (t > target_col)
27024454Smckusick 		    target_col = t;
27124454Smckusick 	    }
27224454Smckusick 	    else
27324454Smckusick 		target_col = t;
27424454Smckusick 	}
27524454Smckusick     else if (ps.ind_stmt)
27624454Smckusick 	target_col += continuation_indent;
27724454Smckusick     return target_col;
27824454Smckusick }
2798803Smckusick 
compute_label_target()28024454Smckusick compute_label_target()
28124454Smckusick {
28224454Smckusick     return
28335500Sbostic 	ps.pcase ? (int) (case_ind * ps.ind_size) + 1
28424454Smckusick 	: *s_lab == '#' ? 1
28535500Sbostic 	: ps.ind_size * (ps.ind_level - label_offset) + 1;
28624454Smckusick }
2878803Smckusick 
2888803Smckusick 
28924454Smckusick /*
29035500Sbostic  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
29135500Sbostic  *
29235500Sbostic  * All rights reserved
29335500Sbostic  *
29435500Sbostic  *
29535500Sbostic  * NAME: fill_buffer
29635500Sbostic  *
29735500Sbostic  * FUNCTION: Reads one block of input into input_buffer
29835500Sbostic  *
29935500Sbostic  * HISTORY: initial coding 	November 1976	D A Willcox of CAC 1/7/77 A
30035500Sbostic  * Willcox of CAC	Added check for switch back to partly full input
30135500Sbostic  * buffer from temporary buffer
30235500Sbostic  *
30338011Sbostic  */
30424454Smckusick int
fill_buffer()30524454Smckusick fill_buffer()
30624454Smckusick {				/* this routine reads stuff from the input */
30724454Smckusick     register char *p;
30824454Smckusick     register int i;
30924454Smckusick     register FILE *f = input;
3108803Smckusick 
31135500Sbostic     if (bp_save != 0) {		/* there is a partly filled input buffer left */
31224454Smckusick 	buf_ptr = bp_save;	/* dont read anything, just switch buffers */
3138803Smckusick 	buf_end = be_save;
3148803Smckusick 	bp_save = be_save = 0;
3158803Smckusick 	if (buf_ptr < buf_end)
31635500Sbostic 	    return;		/* only return if there is really something in
31735500Sbostic 				 * this buffer */
3188803Smckusick     }
31938011Sbostic     for (p = in_buffer;;) {
32038011Sbostic 	if (p >= in_buffer_limit) {
32138011Sbostic 	    register size = (in_buffer_limit - in_buffer) * 2 + 10;
32238011Sbostic 	    register offset = p - in_buffer;
32338011Sbostic 	    in_buffer = (char *) realloc(in_buffer, size);
32438011Sbostic 	    if (in_buffer == 0)
32538011Sbostic 		err("input line too long");
32638011Sbostic 	    p = in_buffer + offset;
32738011Sbostic 	    in_buffer_limit = in_buffer + size - 2;
32838011Sbostic 	}
32935510Sbostic 	if ((i = getc(f)) == EOF) {
33035510Sbostic 		*p++ = ' ';
33135510Sbostic 		*p++ = '\n';
33235510Sbostic 		had_eof = true;
33335510Sbostic 		break;
33435510Sbostic 	}
33535510Sbostic 	*p++ = i;
33635510Sbostic 	if (i == '\n')
33735510Sbostic 		break;
3388803Smckusick     }
33938011Sbostic     buf_ptr = in_buffer;
34024454Smckusick     buf_end = p;
34136972Sbostic     if (p[-2] == '/' && p[-3] == '*') {
34224454Smckusick 	if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
34324454Smckusick 	    fill_buffer();	/* flush indent error message */
34424454Smckusick 	else {
34524454Smckusick 	    int         com = 0;
3468803Smckusick 
34724454Smckusick 	    p = in_buffer;
34824454Smckusick 	    while (*p == ' ' || *p == '\t')
34924454Smckusick 		p++;
35024454Smckusick 	    if (*p == '/' && p[1] == '*') {
35124454Smckusick 		p += 2;
35224454Smckusick 		while (*p == ' ' || *p == '\t')
35324454Smckusick 		    p++;
35424454Smckusick 		if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E'
35535500Sbostic 			&& p[4] == 'N' && p[5] == 'T') {
35624454Smckusick 		    p += 6;
35724454Smckusick 		    while (*p == ' ' || *p == '\t')
35824454Smckusick 			p++;
35924454Smckusick 		    if (*p == '*')
36024454Smckusick 			com = 1;
36124454Smckusick 		    else if (*p == 'O')
36224454Smckusick 			if (*++p == 'N')
36324454Smckusick 			    p++, com = 1;
36424454Smckusick 			else if (*p == 'F' && *++p == 'F')
36524454Smckusick 			    p++, com = 2;
36624454Smckusick 		    while (*p == ' ' || *p == '\t')
36724454Smckusick 			p++;
36824454Smckusick 		    if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) {
36924454Smckusick 			if (s_com != e_com || s_lab != e_lab || s_code != e_code)
37024454Smckusick 			    dump_line();
37124454Smckusick 			if (!(inhibit_formatting = com - 1)) {
37224454Smckusick 			    n_real_blanklines = 0;
37324454Smckusick 			    postfix_blankline_requested = 0;
37424454Smckusick 			    prefix_blankline_requested = 0;
37524454Smckusick 			    suppress_blanklines = 1;
37624454Smckusick 			}
37724454Smckusick 		    }
37824454Smckusick 		}
37924454Smckusick 	    }
38024454Smckusick 	}
38124454Smckusick     }
38224454Smckusick     if (inhibit_formatting) {
38324454Smckusick 	p = in_buffer;
38424454Smckusick 	do
38524454Smckusick 	    putc(*p, output);
38624454Smckusick 	while (*p++ != '\n');
38724454Smckusick     }
3888803Smckusick     return;
38936972Sbostic }
3908803Smckusick 
39124454Smckusick /*
39235500Sbostic  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
39335500Sbostic  *
39435500Sbostic  * All rights reserved
39535500Sbostic  *
39635500Sbostic  *
39735500Sbostic  * NAME: pad_output
39835500Sbostic  *
39935500Sbostic  * FUNCTION: Writes tabs and spaces to move the current column up to the desired
40035500Sbostic  * position.
40135500Sbostic  *
40235500Sbostic  * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf.
40335500Sbostic  *
40424454Smckusick  * PARAMETERS: current		integer		The current column target
40535500Sbostic  * nteger		The desired column
40635500Sbostic  *
40735500Sbostic  * RETURNS: Integer value of the new column.  (If current >= target, no action is
40835500Sbostic  * taken, and current is returned.
40935500Sbostic  *
41035500Sbostic  * GLOBALS: None
41135500Sbostic  *
41235500Sbostic  * CALLS: write (sys)
41335500Sbostic  *
41435500Sbostic  * CALLED BY: dump_line
41535500Sbostic  *
41635500Sbostic  * HISTORY: initial coding 	November 1976	D A Willcox of CAC
41735500Sbostic  *
41838011Sbostic  */
pad_output(current,target)41935500Sbostic pad_output(current, target)	/* writes tabs and blanks (if necessary) to
42035500Sbostic 				 * get the current output position up to the
42135500Sbostic 				 * target column */
42224454Smckusick     int         current;	/* the current column value */
42324454Smckusick     int         target;		/* position we want it at */
4248803Smckusick {
42524454Smckusick     register int curr;		/* internal column pointer */
4268803Smckusick     register int tcur;
4278803Smckusick 
42824454Smckusick     if (troff)
42924454Smckusick 	fprintf(output, "\\h'|%dp'", (target - 1) * 7);
43024454Smckusick     else {
43124454Smckusick 	if (current >= target)
43224454Smckusick 	    return (current);	/* line is already long enough */
43324454Smckusick 	curr = current;
43424454Smckusick 	while ((tcur = ((curr - 1) & tabmask) + tabsize + 1) <= target) {
43524454Smckusick 	    putc('\t', output);
4368803Smckusick 	    curr = tcur;
4378803Smckusick 	}
43824454Smckusick 	while (curr++ < target)
43924454Smckusick 	    putc(' ', output);	/* pad with final blanks */
4408803Smckusick     }
4418803Smckusick     return (target);
44236972Sbostic }
4438803Smckusick 
44424454Smckusick /*
44535500Sbostic  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
44635500Sbostic  *
44735500Sbostic  * All rights reserved
44835500Sbostic  *
44935500Sbostic  *
45035500Sbostic  * NAME: count_spaces
45135500Sbostic  *
45224454Smckusick  * FUNCTION: Find out where printing of a given string will leave the current
45335500Sbostic  * character position on output.
45435500Sbostic  *
45524454Smckusick  * ALGORITHM: Run thru input string and add appropriate values to current
45635500Sbostic  * position.
45735500Sbostic  *
45835500Sbostic  * RETURNS: Integer value of position after printing "buffer" starting in column
45935500Sbostic  * "current".
46035500Sbostic  *
46135500Sbostic  * HISTORY: initial coding 	November 1976	D A Willcox of CAC
46235500Sbostic  *
46338011Sbostic  */
46424454Smckusick int
count_spaces(current,buffer)46524454Smckusick count_spaces(current, buffer)
46624454Smckusick /*
46724454Smckusick  * this routine figures out where the character position will be after
46835500Sbostic  * printing the text in buffer starting at column "current"
46924454Smckusick  */
47024454Smckusick     int         current;
47124454Smckusick     char       *buffer;
4728803Smckusick {
47324454Smckusick     register char *buf;		/* used to look thru buffer */
47424454Smckusick     register int cur;		/* current character counter */
4758803Smckusick 
4768803Smckusick     cur = current;
4778803Smckusick 
4788803Smckusick     for (buf = buffer; *buf != '\0'; ++buf) {
4798803Smckusick 	switch (*buf) {
4808803Smckusick 
48135500Sbostic 	case '\n':
48235500Sbostic 	case 014:		/* form feed */
48335500Sbostic 	    cur = 1;
48435500Sbostic 	    break;
4858803Smckusick 
48635500Sbostic 	case '\t':
48735500Sbostic 	    cur = ((cur - 1) & tabmask) + tabsize + 1;
48835500Sbostic 	    break;
4898803Smckusick 
49036972Sbostic 	case 010:		/* backspace */
49135500Sbostic 	    --cur;
49235500Sbostic 	    break;
4938803Smckusick 
49435500Sbostic 	default:
49535500Sbostic 	    ++cur;
49635500Sbostic 	    break;
49724454Smckusick 	}			/* end of switch */
49824454Smckusick     }				/* end of for loop */
4998803Smckusick     return (cur);
50036972Sbostic }
5018803Smckusick 
50235503Sbostic int	found_err;
50336972Sbostic /* VARARGS2 */
diag(level,msg,a,b)50424454Smckusick diag(level, msg, a, b)
50546695Sbostic 	char *msg;
5068803Smckusick {
50735503Sbostic     if (level)
50835503Sbostic 	found_err = 1;
50924454Smckusick     if (output == stdout) {
51024454Smckusick 	fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
51124454Smckusick 	fprintf(stdout, msg, a, b);
51224454Smckusick 	fprintf(stdout, " */\n");
5138803Smckusick     }
51424454Smckusick     else {
51524454Smckusick 	fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
51624454Smckusick 	fprintf(stderr, msg, a, b);
51724454Smckusick 	fprintf(stderr, "\n");
5188803Smckusick     }
5198803Smckusick }
52035500Sbostic 
writefdef(f,nm)52135500Sbostic writefdef(f, nm)
52235500Sbostic     register struct fstate *f;
52335500Sbostic {
52435500Sbostic     fprintf(output, ".ds f%c %s\n.nr s%c %d\n",
52535500Sbostic 	    nm, f->font, nm, f->size);
52635500Sbostic }
52735500Sbostic 
52835500Sbostic char       *
chfont(of,nf,s)52935500Sbostic chfont(of, nf, s)
53035500Sbostic     register struct fstate *of,
53135500Sbostic                *nf;
53235500Sbostic     char       *s;
53335500Sbostic {
53435500Sbostic     if (of->font[0] != nf->font[0]
53535500Sbostic 	    || of->font[1] != nf->font[1]) {
53635500Sbostic 	*s++ = '\\';
53735500Sbostic 	*s++ = 'f';
53835500Sbostic 	if (nf->font[1]) {
53935500Sbostic 	    *s++ = '(';
54035500Sbostic 	    *s++ = nf->font[0];
54135500Sbostic 	    *s++ = nf->font[1];
54235500Sbostic 	}
54335500Sbostic 	else
54435500Sbostic 	    *s++ = nf->font[0];
54535500Sbostic     }
54635500Sbostic     if (nf->size != of->size) {
54735500Sbostic 	*s++ = '\\';
54835500Sbostic 	*s++ = 's';
54935500Sbostic 	if (nf->size < of->size) {
55035500Sbostic 	    *s++ = '-';
55135500Sbostic 	    *s++ = '0' + of->size - nf->size;
55235500Sbostic 	}
55335500Sbostic 	else {
55435500Sbostic 	    *s++ = '+';
55535500Sbostic 	    *s++ = '0' + nf->size - of->size;
55635500Sbostic 	}
55735500Sbostic     }
55835500Sbostic     return s;
55935500Sbostic }
56035500Sbostic 
56135500Sbostic 
parsefont(f,s0)56235500Sbostic parsefont(f, s0)
56335500Sbostic     register struct fstate *f;
56435500Sbostic     char       *s0;
56535500Sbostic {
56635500Sbostic     register char *s = s0;
56735500Sbostic     int         sizedelta = 0;
56835500Sbostic     bzero(f, sizeof *f);
56935500Sbostic     while (*s) {
57035500Sbostic 	if (isdigit(*s))
57135500Sbostic 	    f->size = f->size * 10 + *s - '0';
57235500Sbostic 	else if (isupper(*s))
57335500Sbostic 	    if (f->font[0])
57435500Sbostic 		f->font[1] = *s;
57535500Sbostic 	    else
57635500Sbostic 		f->font[0] = *s;
57735500Sbostic 	else if (*s == 'c')
57835500Sbostic 	    f->allcaps = 1;
57935500Sbostic 	else if (*s == '+')
58035500Sbostic 	    sizedelta++;
58135500Sbostic 	else if (*s == '-')
58235500Sbostic 	    sizedelta--;
58335500Sbostic 	else {
58435500Sbostic 	    fprintf(stderr, "indent: bad font specification: %s\n", s0);
58535500Sbostic 	    exit(1);
58635500Sbostic 	}
58735500Sbostic 	s++;
58835500Sbostic     }
58935500Sbostic     if (f->font[0] == 0)
59035500Sbostic 	f->font[0] = 'R';
59135500Sbostic     if (bodyf.size == 0)
59235500Sbostic 	bodyf.size = 11;
59335500Sbostic     if (f->size == 0)
59435500Sbostic 	f->size = bodyf.size + sizedelta;
59535500Sbostic     else if (sizedelta > 0)
59635500Sbostic 	f->size += bodyf.size;
59735500Sbostic     else
59835500Sbostic 	f->size = bodyf.size - f->size;
59935500Sbostic }
600