xref: /csrg-svn/usr.bin/indent/io.c (revision 35503)
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  *
733767Sbostic  * Redistribution and use in source and binary forms are permitted
834885Sbostic  * provided that the above copyright notice and this paragraph are
934885Sbostic  * duplicated in all such forms and that any documentation,
1034885Sbostic  * advertising materials, and other materials related to such
1134885Sbostic  * distribution and use acknowledge that the software was developed
1235500Sbostic  * by the University of California, Berkeley, the University of Illinois,
1335500Sbostic  * Urbana, and Sun Microsystems, Inc.  The name of either University
1435500Sbostic  * or Sun Microsystems may not be used to endorse or promote products
1535500Sbostic  * derived from this software without specific prior written permission.
1634885Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1734885Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1834885Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1921969Sdist  */
208803Smckusick 
2121969Sdist #ifndef lint
22*35503Sbostic static char sccsid[] = "@(#)io.c	5.9 (Berkeley) 09/15/88";
2333767Sbostic #endif /* not lint */
2421969Sdist 
25*35503Sbostic #include "indent_globs.h"
2635500Sbostic #include <ctype.h>
278803Smckusick 
288803Smckusick 
2924454Smckusick int         comment_open;
3024454Smckusick static      paren_target;
318803Smckusick 
3224454Smckusick dump_line()
3324454Smckusick {				/* dump_line is the routine that actually
3435500Sbostic 				 * effects the printing of the new source. It
3535500Sbostic 				 * prints the label section, followed by the
3635500Sbostic 				 * code section with the appropriate nesting
3735500Sbostic 				 * level, followed by any comments */
3824454Smckusick     register int cur_col,
3924454Smckusick                 temp_col,
4024454Smckusick                 target_col;
4135500Sbostic     static      not_first_line;
428803Smckusick 
4324454Smckusick     if (ps.procname[0]) {
4435500Sbostic 	if (troff) {
4535500Sbostic 	    if (comment_open) {
4635500Sbostic 		comment_open = 0;
4735500Sbostic 		fprintf(output, ".*/\n");
4835500Sbostic 	    }
4924454Smckusick 	    fprintf(output, ".Pr \"%s\"\n", ps.procname);
5035500Sbostic 	}
5124454Smckusick 	ps.ind_level = 0;
5224454Smckusick 	ps.procname[0] = 0;
5324454Smckusick     }
5424454Smckusick     if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
5535500Sbostic 	if (suppress_blanklines > 0)
5635500Sbostic 	    suppress_blanklines--;
5724454Smckusick 	else {
5835500Sbostic 	    ps.bl_line = true;
5935500Sbostic 	    n_real_blanklines++;
6024454Smckusick 	}
6124454Smckusick     }
6224454Smckusick     else if (!inhibit_formatting) {
6324454Smckusick 	suppress_blanklines = 0;
6424454Smckusick 	ps.bl_line = false;
6535500Sbostic 	if (prefix_blankline_requested && not_first_line)
6624454Smckusick 	    if (swallow_optional_blanklines) {
6724454Smckusick 		if (n_real_blanklines == 1)
6824454Smckusick 		    n_real_blanklines = 0;
6924454Smckusick 	    }
7024454Smckusick 	    else {
7124454Smckusick 		if (n_real_blanklines == 0)
7224454Smckusick 		    n_real_blanklines = 1;
7324454Smckusick 	    }
7424454Smckusick 	while (--n_real_blanklines >= 0)
7524454Smckusick 	    putc('\n', output);
7624454Smckusick 	n_real_blanklines = 0;
7724454Smckusick 	if (ps.ind_level == 0)
7824454Smckusick 	    ps.ind_stmt = 0;	/* this is a class A kludge. dont do
7935500Sbostic 				 * additional statement indentation if we are
8035500Sbostic 				 * at bracket level 0 */
818803Smckusick 
8224454Smckusick 	if (e_lab != s_lab || e_code != s_code)
8324454Smckusick 	    ++code_lines;	/* keep count of lines with code */
848803Smckusick 
858803Smckusick 
8624454Smckusick 	if (e_lab != s_lab) {	/* print lab, if any */
8724454Smckusick 	    if (comment_open) {
8824454Smckusick 		comment_open = 0;
8924454Smckusick 		fprintf(output, ".*/\n");
9024454Smckusick 	    }
9124454Smckusick 	    while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
9224454Smckusick 		e_lab--;
9324454Smckusick 	    cur_col = pad_output(1, compute_label_target());
9424454Smckusick 	    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;
2508803Smckusick };
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  *
30024454Smckusick  */
30124454Smckusick int
30224454Smckusick fill_buffer()
30324454Smckusick {				/* this routine reads stuff from the input */
30424454Smckusick     int         count;
30524454Smckusick     register char *p;
30624454Smckusick     register int i;
30724454Smckusick     register FILE *f = input;
3088803Smckusick 
30935500Sbostic     if (bp_save != 0) {		/* there is a partly filled input buffer left */
31024454Smckusick 	buf_ptr = bp_save;	/* dont read anything, just switch buffers */
3118803Smckusick 	buf_end = be_save;
3128803Smckusick 	bp_save = be_save = 0;
3138803Smckusick 	if (buf_ptr < buf_end)
31435500Sbostic 	    return;		/* only return if there is really something in
31535500Sbostic 				 * this buffer */
3168803Smckusick     }
31724454Smckusick     p = in_buffer;
31824454Smckusick     buf_ptr = p;
31924454Smckusick     while ((*p++ = i = getc(f)) != EOF && i != '\n');
32024454Smckusick     if (i == EOF) {
32124454Smckusick 	p[-1] = ' ';
32224454Smckusick 	*p++ = '\n';
3238803Smckusick 	had_eof = true;
3248803Smckusick     }
32524454Smckusick     buf_end = p;
32624454Smckusick     if (p[-2] == '/' && p[-3] == '*') {
32724454Smckusick 	if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
32824454Smckusick 	    fill_buffer();	/* flush indent error message */
32924454Smckusick 	else {
33024454Smckusick 	    int         com = 0;
3318803Smckusick 
33224454Smckusick 	    p = in_buffer;
33324454Smckusick 	    while (*p == ' ' || *p == '\t')
33424454Smckusick 		p++;
33524454Smckusick 	    if (*p == '/' && p[1] == '*') {
33624454Smckusick 		p += 2;
33724454Smckusick 		while (*p == ' ' || *p == '\t')
33824454Smckusick 		    p++;
33924454Smckusick 		if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E'
34035500Sbostic 			&& p[4] == 'N' && p[5] == 'T') {
34124454Smckusick 		    p += 6;
34224454Smckusick 		    while (*p == ' ' || *p == '\t')
34324454Smckusick 			p++;
34424454Smckusick 		    if (*p == '*')
34524454Smckusick 			com = 1;
34624454Smckusick 		    else if (*p == 'O')
34724454Smckusick 			if (*++p == 'N')
34824454Smckusick 			    p++, com = 1;
34924454Smckusick 			else if (*p == 'F' && *++p == 'F')
35024454Smckusick 			    p++, com = 2;
35124454Smckusick 		    while (*p == ' ' || *p == '\t')
35224454Smckusick 			p++;
35324454Smckusick 		    if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) {
35424454Smckusick 			if (s_com != e_com || s_lab != e_lab || s_code != e_code)
35524454Smckusick 			    dump_line();
35624454Smckusick 			if (!(inhibit_formatting = com - 1)) {
35724454Smckusick 			    n_real_blanklines = 0;
35824454Smckusick 			    postfix_blankline_requested = 0;
35924454Smckusick 			    prefix_blankline_requested = 0;
36024454Smckusick 			    suppress_blanklines = 1;
36124454Smckusick 			}
36224454Smckusick 		    }
36324454Smckusick 		}
36424454Smckusick 	    }
36524454Smckusick 	}
36624454Smckusick     }
36724454Smckusick     if (inhibit_formatting) {
36824454Smckusick 	p = in_buffer;
36924454Smckusick 	do
37024454Smckusick 	    putc(*p, output);
37124454Smckusick 	while (*p++ != '\n');
37224454Smckusick     }
3738803Smckusick     return;
3748803Smckusick };
3758803Smckusick 
37624454Smckusick /*
37735500Sbostic  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
37835500Sbostic  *
37935500Sbostic  * All rights reserved
38035500Sbostic  *
38135500Sbostic  *
38235500Sbostic  * NAME: pad_output
38335500Sbostic  *
38435500Sbostic  * FUNCTION: Writes tabs and spaces to move the current column up to the desired
38535500Sbostic  * position.
38635500Sbostic  *
38735500Sbostic  * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf.
38835500Sbostic  *
38924454Smckusick  * PARAMETERS: current		integer		The current column target
39035500Sbostic  * nteger		The desired column
39135500Sbostic  *
39235500Sbostic  * RETURNS: Integer value of the new column.  (If current >= target, no action is
39335500Sbostic  * taken, and current is returned.
39435500Sbostic  *
39535500Sbostic  * GLOBALS: None
39635500Sbostic  *
39735500Sbostic  * CALLS: write (sys)
39835500Sbostic  *
39935500Sbostic  * CALLED BY: dump_line
40035500Sbostic  *
40135500Sbostic  * HISTORY: initial coding 	November 1976	D A Willcox of CAC
40235500Sbostic  *
40324454Smckusick  */
40435500Sbostic pad_output(current, target)	/* writes tabs and blanks (if necessary) to
40535500Sbostic 				 * get the current output position up to the
40635500Sbostic 				 * target column */
40724454Smckusick     int         current;	/* the current column value */
40824454Smckusick     int         target;		/* position we want it at */
4098803Smckusick {
41024454Smckusick     register int curr;		/* internal column pointer */
4118803Smckusick     register int tcur;
4128803Smckusick 
41324454Smckusick     if (troff)
41424454Smckusick 	fprintf(output, "\\h'|%dp'", (target - 1) * 7);
41524454Smckusick     else {
41624454Smckusick 	if (current >= target)
41724454Smckusick 	    return (current);	/* line is already long enough */
41824454Smckusick 	curr = current;
41924454Smckusick 	while ((tcur = ((curr - 1) & tabmask) + tabsize + 1) <= target) {
42024454Smckusick 	    putc('\t', output);
4218803Smckusick 	    curr = tcur;
4228803Smckusick 	}
42324454Smckusick 	while (curr++ < target)
42424454Smckusick 	    putc(' ', output);	/* pad with final blanks */
4258803Smckusick     }
4268803Smckusick     return (target);
4278803Smckusick };
4288803Smckusick 
42924454Smckusick /*
43035500Sbostic  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
43135500Sbostic  *
43235500Sbostic  * All rights reserved
43335500Sbostic  *
43435500Sbostic  *
43535500Sbostic  * NAME: count_spaces
43635500Sbostic  *
43724454Smckusick  * FUNCTION: Find out where printing of a given string will leave the current
43835500Sbostic  * character position on output.
43935500Sbostic  *
44024454Smckusick  * ALGORITHM: Run thru input string and add appropriate values to current
44135500Sbostic  * position.
44235500Sbostic  *
44335500Sbostic  * RETURNS: Integer value of position after printing "buffer" starting in column
44435500Sbostic  * "current".
44535500Sbostic  *
44635500Sbostic  * HISTORY: initial coding 	November 1976	D A Willcox of CAC
44735500Sbostic  *
44824454Smckusick  */
44924454Smckusick int
45024454Smckusick count_spaces(current, buffer)
45124454Smckusick /*
45224454Smckusick  * this routine figures out where the character position will be after
45335500Sbostic  * printing the text in buffer starting at column "current"
45424454Smckusick  */
45524454Smckusick     int         current;
45624454Smckusick     char       *buffer;
4578803Smckusick {
45824454Smckusick     register char *buf;		/* used to look thru buffer */
45924454Smckusick     register int cur;		/* current character counter */
4608803Smckusick 
4618803Smckusick     cur = current;
4628803Smckusick 
4638803Smckusick     for (buf = buffer; *buf != '\0'; ++buf) {
4648803Smckusick 	switch (*buf) {
4658803Smckusick 
46635500Sbostic 	case '\n':
46735500Sbostic 	case 014:		/* form feed */
46835500Sbostic 	    cur = 1;
46935500Sbostic 	    break;
4708803Smckusick 
47135500Sbostic 	case '\t':
47235500Sbostic 	    cur = ((cur - 1) & tabmask) + tabsize + 1;
47335500Sbostic 	    break;
4748803Smckusick 
47535500Sbostic 	case '':		/* this is a backspace */
47635500Sbostic 	    --cur;
47735500Sbostic 	    break;
4788803Smckusick 
47935500Sbostic 	default:
48035500Sbostic 	    ++cur;
48135500Sbostic 	    break;
48224454Smckusick 	}			/* end of switch */
48324454Smckusick     }				/* end of for loop */
4848803Smckusick     return (cur);
4858803Smckusick };
4868803Smckusick 
487*35503Sbostic int	found_err;
48824454Smckusick diag(level, msg, a, b)
4898803Smckusick {
490*35503Sbostic     if (level)
491*35503Sbostic 	found_err = 1;
49224454Smckusick     if (output == stdout) {
49324454Smckusick 	fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
49424454Smckusick 	fprintf(stdout, msg, a, b);
49524454Smckusick 	fprintf(stdout, " */\n");
4968803Smckusick     }
49724454Smckusick     else {
49824454Smckusick 	fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
49924454Smckusick 	fprintf(stderr, msg, a, b);
50024454Smckusick 	fprintf(stderr, "\n");
5018803Smckusick     }
5028803Smckusick }
50335500Sbostic 
50435500Sbostic writefdef(f, nm)
50535500Sbostic     register struct fstate *f;
50635500Sbostic {
50735500Sbostic     fprintf(output, ".ds f%c %s\n.nr s%c %d\n",
50835500Sbostic 	    nm, f->font, nm, f->size);
50935500Sbostic }
51035500Sbostic 
51135500Sbostic char       *
51235500Sbostic chfont(of, nf, s)
51335500Sbostic     register struct fstate *of,
51435500Sbostic                *nf;
51535500Sbostic     char       *s;
51635500Sbostic {
51735500Sbostic     if (of->font[0] != nf->font[0]
51835500Sbostic 	    || of->font[1] != nf->font[1]) {
51935500Sbostic 	*s++ = '\\';
52035500Sbostic 	*s++ = 'f';
52135500Sbostic 	if (nf->font[1]) {
52235500Sbostic 	    *s++ = '(';
52335500Sbostic 	    *s++ = nf->font[0];
52435500Sbostic 	    *s++ = nf->font[1];
52535500Sbostic 	}
52635500Sbostic 	else
52735500Sbostic 	    *s++ = nf->font[0];
52835500Sbostic     }
52935500Sbostic     if (nf->size != of->size) {
53035500Sbostic 	*s++ = '\\';
53135500Sbostic 	*s++ = 's';
53235500Sbostic 	if (nf->size < of->size) {
53335500Sbostic 	    *s++ = '-';
53435500Sbostic 	    *s++ = '0' + of->size - nf->size;
53535500Sbostic 	}
53635500Sbostic 	else {
53735500Sbostic 	    *s++ = '+';
53835500Sbostic 	    *s++ = '0' + nf->size - of->size;
53935500Sbostic 	}
54035500Sbostic     }
54135500Sbostic     return s;
54235500Sbostic }
54335500Sbostic 
54435500Sbostic 
54535500Sbostic setfont(f)
54635500Sbostic     register struct fstate *f;
54735500Sbostic {
54835500Sbostic     if (f->font[0] != ps.cfont.font[0]
54935500Sbostic 	    || f->font[1] != ps.cfont.font[1])
55035500Sbostic 	fprintf(output, f->font[1] ? "\\f(%s" : "\\f%s", f->font);
55135500Sbostic     if (f->size != ps.cfont.size)
55235500Sbostic 	fprintf(output, "\\s%d", f->size);
55335500Sbostic     ps.cfont = *f;
55435500Sbostic }
55535500Sbostic 
55635500Sbostic parsefont(f, s0)
55735500Sbostic     register struct fstate *f;
55835500Sbostic     char       *s0;
55935500Sbostic {
56035500Sbostic     register char *s = s0;
56135500Sbostic     int         sizedelta = 0;
56235500Sbostic     bzero(f, sizeof *f);
56335500Sbostic     while (*s) {
56435500Sbostic 	if (isdigit(*s))
56535500Sbostic 	    f->size = f->size * 10 + *s - '0';
56635500Sbostic 	else if (isupper(*s))
56735500Sbostic 	    if (f->font[0])
56835500Sbostic 		f->font[1] = *s;
56935500Sbostic 	    else
57035500Sbostic 		f->font[0] = *s;
57135500Sbostic 	else if (*s == 'c')
57235500Sbostic 	    f->allcaps = 1;
57335500Sbostic 	else if (*s == '+')
57435500Sbostic 	    sizedelta++;
57535500Sbostic 	else if (*s == '-')
57635500Sbostic 	    sizedelta--;
57735500Sbostic 	else {
57835500Sbostic 	    fprintf(stderr, "indent: bad font specification: %s\n", s0);
57935500Sbostic 	    exit(1);
58035500Sbostic 	}
58135500Sbostic 	s++;
58235500Sbostic     }
58335500Sbostic     if (f->font[0] == 0)
58435500Sbostic 	f->font[0] = 'R';
58535500Sbostic     if (bodyf.size == 0)
58635500Sbostic 	bodyf.size = 11;
58735500Sbostic     if (f->size == 0)
58835500Sbostic 	f->size = bodyf.size + sizedelta;
58935500Sbostic     else if (sizedelta > 0)
59035500Sbostic 	f->size += bodyf.size;
59135500Sbostic     else
59235500Sbostic 	f->size = bodyf.size - f->size;
59335500Sbostic }
594