xref: /csrg-svn/usr.bin/indent/io.c (revision 36972)
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*36972Sbostic static char sccsid[] = "@(#)io.c	5.12 (Berkeley) 03/05/89";
2333767Sbostic #endif /* not lint */
2421969Sdist 
2535503Sbostic #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                 target_col;
4035500Sbostic     static      not_first_line;
418803Smckusick 
4224454Smckusick     if (ps.procname[0]) {
4335500Sbostic 	if (troff) {
4435500Sbostic 	    if (comment_open) {
4535500Sbostic 		comment_open = 0;
4635500Sbostic 		fprintf(output, ".*/\n");
4735500Sbostic 	    }
4824454Smckusick 	    fprintf(output, ".Pr \"%s\"\n", ps.procname);
4935500Sbostic 	}
5024454Smckusick 	ps.ind_level = 0;
5124454Smckusick 	ps.procname[0] = 0;
5224454Smckusick     }
5324454Smckusick     if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
5435500Sbostic 	if (suppress_blanklines > 0)
5535500Sbostic 	    suppress_blanklines--;
5624454Smckusick 	else {
5735500Sbostic 	    ps.bl_line = true;
5835500Sbostic 	    n_real_blanklines++;
5924454Smckusick 	}
6024454Smckusick     }
6124454Smckusick     else if (!inhibit_formatting) {
6224454Smckusick 	suppress_blanklines = 0;
6324454Smckusick 	ps.bl_line = false;
6435500Sbostic 	if (prefix_blankline_requested && not_first_line)
6524454Smckusick 	    if (swallow_optional_blanklines) {
6624454Smckusick 		if (n_real_blanklines == 1)
6724454Smckusick 		    n_real_blanklines = 0;
6824454Smckusick 	    }
6924454Smckusick 	    else {
7024454Smckusick 		if (n_real_blanklines == 0)
7124454Smckusick 		    n_real_blanklines = 1;
7224454Smckusick 	    }
7324454Smckusick 	while (--n_real_blanklines >= 0)
7424454Smckusick 	    putc('\n', output);
7524454Smckusick 	n_real_blanklines = 0;
7624454Smckusick 	if (ps.ind_level == 0)
7724454Smckusick 	    ps.ind_stmt = 0;	/* this is a class A kludge. dont do
7835500Sbostic 				 * additional statement indentation if we are
7935500Sbostic 				 * at bracket level 0 */
808803Smckusick 
8124454Smckusick 	if (e_lab != s_lab || e_code != s_code)
8224454Smckusick 	    ++code_lines;	/* keep count of lines with code */
838803Smckusick 
848803Smckusick 
8524454Smckusick 	if (e_lab != s_lab) {	/* print lab, if any */
8624454Smckusick 	    if (comment_open) {
8724454Smckusick 		comment_open = 0;
8824454Smckusick 		fprintf(output, ".*/\n");
8924454Smckusick 	    }
9024454Smckusick 	    while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
9124454Smckusick 		e_lab--;
9224454Smckusick 	    cur_col = pad_output(1, compute_label_target());
9324454Smckusick 	    fprintf(output, "%.*s", e_lab - s_lab, s_lab);
9424454Smckusick 	    cur_col = count_spaces(cur_col, s_lab);
958803Smckusick 	}
9624454Smckusick 	else
9724454Smckusick 	    cur_col = 1;	/* there is no label section */
988803Smckusick 
9924454Smckusick 	ps.pcase = false;
1008803Smckusick 
10124454Smckusick 	if (s_code != e_code) {	/* print code section, if any */
10224454Smckusick 	    register char *p;
1038803Smckusick 
10424454Smckusick 	    if (comment_open) {
10524454Smckusick 		comment_open = 0;
10624454Smckusick 		fprintf(output, ".*/\n");
10724454Smckusick 	    }
10824454Smckusick 	    target_col = compute_code_target();
10924454Smckusick 	    {
11024454Smckusick 		register    i;
1118803Smckusick 
11224454Smckusick 		for (i = 0; i < ps.p_l_follow; i++)
11324454Smckusick 		    if (ps.paren_indents[i] >= 0)
11424454Smckusick 			ps.paren_indents[i] = -(ps.paren_indents[i] + target_col);
11524454Smckusick 	    }
11624454Smckusick 	    cur_col = pad_output(cur_col, target_col);
11724454Smckusick 	    for (p = s_code; p < e_code; p++)
11835500Sbostic 		if (*p == (char) 0200)
11924454Smckusick 		    fprintf(output, "%d", target_col * 7);
12024454Smckusick 		else
12124454Smckusick 		    putc(*p, output);
12224454Smckusick 	    cur_col = count_spaces(cur_col, s_code);
12324454Smckusick 	}
12424454Smckusick 	if (s_com != e_com)
12524454Smckusick 	    if (troff) {
12635500Sbostic 		int         all_here = 0;
12724454Smckusick 		register char *p;
1288803Smckusick 
12924454Smckusick 		if (e_com[-1] == '/' && e_com[-2] == '*')
13035500Sbostic 		    e_com -= 2, all_here++;
13124454Smckusick 		while (e_com > s_com && e_com[-1] == ' ')
13224454Smckusick 		    e_com--;
13324454Smckusick 		*e_com = 0;
13424454Smckusick 		p = s_com;
13524454Smckusick 		while (*p == ' ')
13624454Smckusick 		    p++;
13724454Smckusick 		if (p[0] == '/' && p[1] == '*')
13835500Sbostic 		    p += 2, all_here++;
13924454Smckusick 		else if (p[0] == '*')
14024454Smckusick 		    p += p[1] == '/' ? 2 : 1;
14124454Smckusick 		while (*p == ' ')
14224454Smckusick 		    p++;
14324454Smckusick 		if (*p == 0)
14424454Smckusick 		    goto inhibit_newline;
14535500Sbostic 		if (comment_open < 2 && ps.box_com) {
14635500Sbostic 		    comment_open = 0;
14735500Sbostic 		    fprintf(output, ".*/\n");
14835500Sbostic 		}
14935500Sbostic 		if (comment_open == 0) {
15024454Smckusick 		    if ('a' <= *p && *p <= 'z')
15124454Smckusick 			*p = *p + 'A' - 'a';
15235500Sbostic 		    if (e_com - p < 50 && all_here == 2) {
15335500Sbostic 			register char *follow = p;
15435500Sbostic 			fprintf(output, "\n.nr C! \\w\1");
15535500Sbostic 			while (follow < e_com) {
15635500Sbostic 			    switch (*follow) {
15735500Sbostic 			    case '\n':
15835500Sbostic 				putc(' ', output);
15935500Sbostic 			    case 1:
16035500Sbostic 				break;
16135500Sbostic 			    case '\\':
16235500Sbostic 				putc('\\', output);
16335500Sbostic 			    default:
16435500Sbostic 				putc(*follow, output);
16535500Sbostic 			    }
16635500Sbostic 			    follow++;
16735500Sbostic 			}
16835500Sbostic 			putc(1, output);
16924454Smckusick 		    }
17035500Sbostic 		    fprintf(output, "\n./* %dp %d %dp\n",
17135500Sbostic 			    ps.com_col * 7,
17235500Sbostic 			    (s_code != e_code || s_lab != e_lab) - ps.box_com,
17335500Sbostic 			    target_col * 7);
17424454Smckusick 		}
17535500Sbostic 		comment_open = 1 + ps.box_com;
17624454Smckusick 		while (*p) {
17724454Smckusick 		    if (*p == BACKSLASH)
17824454Smckusick 			putc(BACKSLASH, output);
17924454Smckusick 		    putc(*p++, output);
18024454Smckusick 		}
18124454Smckusick 	    }
18224454Smckusick 	    else {		/* print comment, if any */
18324454Smckusick 		register    target = ps.com_col;
18424454Smckusick 		register char *com_st = s_com;
1858803Smckusick 
18624454Smckusick 		target += ps.comment_delta;
18735500Sbostic 		while (*com_st == '\t')
18835500Sbostic 		    com_st++, target += 8;	/* ? */
18924454Smckusick 		while (target <= 0)
19035500Sbostic 		    if (*com_st == ' ')
19135500Sbostic 			target++, com_st++;
19235500Sbostic 		    else if (*com_st == '\t')
19335500Sbostic 			target = ((target - 1) & ~7) + 9, com_st++;
19424454Smckusick 		    else
19524454Smckusick 			target = 1;
19635500Sbostic 		if (cur_col > target) {	/* if comment cant fit on this line,
19735500Sbostic 					 * put it on next line */
19824454Smckusick 		    putc('\n', output);
19924454Smckusick 		    cur_col = 1;
20024454Smckusick 		    ++ps.out_lines;
20124454Smckusick 		}
20235500Sbostic 		while (e_com > com_st && isspace(e_com[-1]))
20335500Sbostic 		    e_com--;
20424454Smckusick 		cur_col = pad_output(cur_col, target);
20524454Smckusick 		if (!ps.box_com) {
20635500Sbostic 		    if (star_comment_cont && (com_st[1] != '*' || e_com <= com_st + 1))
20735500Sbostic 			if (com_st[1] == ' ' && com_st[0] == ' ' && e_com > com_st + 1)
20824454Smckusick 			    com_st[1] = '*';
20924454Smckusick 			else
21024454Smckusick 			    fwrite(" * ", com_st[0] == '\t' ? 2 : com_st[0] == '*' ? 1 : 3, 1, output);
21124454Smckusick 		}
21224454Smckusick 		fwrite(com_st, e_com - com_st, 1, output);
21324454Smckusick 		ps.comment_delta = ps.n_comment_delta;
21424454Smckusick 		cur_col = count_spaces(cur_col, com_st);
21524454Smckusick 		++ps.com_lines;	/* count lines with comments */
21624454Smckusick 	    }
21724454Smckusick 	if (ps.use_ff)
21824454Smckusick 	    putc('\014', output);
21924454Smckusick 	else
22024454Smckusick 	    putc('\n', output);
22124454Smckusick inhibit_newline:
22224454Smckusick 	++ps.out_lines;
22324454Smckusick 	if (ps.just_saw_decl == 1 && blanklines_after_declarations) {
22424454Smckusick 	    prefix_blankline_requested = 1;
22524454Smckusick 	    ps.just_saw_decl = 0;
2268803Smckusick 	}
22724454Smckusick 	else
22824454Smckusick 	    prefix_blankline_requested = postfix_blankline_requested;
22924454Smckusick 	postfix_blankline_requested = 0;
2308803Smckusick     }
23124454Smckusick     ps.decl_on_line = ps.in_decl;	/* if we are in the middle of a
23235500Sbostic 					 * declaration, remember that fact for
23335500Sbostic 					 * proper comment indentation */
23424454Smckusick     ps.ind_stmt = ps.in_stmt & ~ps.in_decl;	/* next line should be
23524454Smckusick 						 * indented if we have not
23635500Sbostic 						 * completed this stmt and if
23735500Sbostic 						 * we are not in the middle of
23835500Sbostic 						 * a declaration */
23924454Smckusick     ps.use_ff = false;
24024454Smckusick     ps.dumped_decl_indent = 0;
24124454Smckusick     *(e_lab = s_lab) = '\0';	/* reset buffers */
2428803Smckusick     *(e_code = s_code) = '\0';
2438803Smckusick     *(e_com = s_com) = '\0';
24424454Smckusick     ps.ind_level = ps.i_l_follow;
24524454Smckusick     ps.paren_level = ps.p_l_follow;
24624454Smckusick     paren_target = -ps.paren_indents[ps.paren_level - 1];
24735500Sbostic     not_first_line = 1;
2488803Smckusick     return;
249*36972Sbostic }
2508803Smckusick 
25135500Sbostic compute_code_target()
25235500Sbostic {
25324454Smckusick     register    target_col = ps.ind_size * ps.ind_level + 1;
2548803Smckusick 
25524454Smckusick     if (ps.paren_level)
25624454Smckusick 	if (!lineup_to_parens)
25724454Smckusick 	    target_col += continuation_indent * ps.paren_level;
25824454Smckusick 	else {
25924454Smckusick 	    register    w;
26024454Smckusick 	    register    t = paren_target;
2618803Smckusick 
26224454Smckusick 	    if ((w = count_spaces(t, s_code) - max_col) > 0
26335500Sbostic 		    && count_spaces(target_col, s_code) <= max_col) {
26424454Smckusick 		t -= w + 1;
26524454Smckusick 		if (t > target_col)
26624454Smckusick 		    target_col = t;
26724454Smckusick 	    }
26824454Smckusick 	    else
26924454Smckusick 		target_col = t;
27024454Smckusick 	}
27124454Smckusick     else if (ps.ind_stmt)
27224454Smckusick 	target_col += continuation_indent;
27324454Smckusick     return target_col;
27424454Smckusick }
2758803Smckusick 
27624454Smckusick compute_label_target()
27724454Smckusick {
27824454Smckusick     return
27935500Sbostic 	ps.pcase ? (int) (case_ind * ps.ind_size) + 1
28024454Smckusick 	: *s_lab == '#' ? 1
28135500Sbostic 	: ps.ind_size * (ps.ind_level - label_offset) + 1;
28224454Smckusick }
2838803Smckusick 
2848803Smckusick 
28524454Smckusick /*
28635500Sbostic  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
28735500Sbostic  *
28835500Sbostic  * All rights reserved
28935500Sbostic  *
29035500Sbostic  *
29135500Sbostic  * NAME: fill_buffer
29235500Sbostic  *
29335500Sbostic  * FUNCTION: Reads one block of input into input_buffer
29435500Sbostic  *
29535500Sbostic  * HISTORY: initial coding 	November 1976	D A Willcox of CAC 1/7/77 A
29635500Sbostic  * Willcox of CAC	Added check for switch back to partly full input
29735500Sbostic  * buffer from temporary buffer
29835500Sbostic  *
29924454Smckusick  */
30024454Smckusick int
30124454Smckusick fill_buffer()
30224454Smckusick {				/* this routine reads stuff from the input */
30324454Smckusick     register char *p;
30424454Smckusick     register int i;
30524454Smckusick     register FILE *f = input;
3068803Smckusick 
30735500Sbostic     if (bp_save != 0) {		/* there is a partly filled input buffer left */
30824454Smckusick 	buf_ptr = bp_save;	/* dont read anything, just switch buffers */
3098803Smckusick 	buf_end = be_save;
3108803Smckusick 	bp_save = be_save = 0;
3118803Smckusick 	if (buf_ptr < buf_end)
31235500Sbostic 	    return;		/* only return if there is really something in
31335500Sbostic 				 * this buffer */
3148803Smckusick     }
31535510Sbostic     for (p = buf_ptr = in_buffer;;) {
31635510Sbostic 	if ((i = getc(f)) == EOF) {
31735510Sbostic 		*p++ = ' ';
31835510Sbostic 		*p++ = '\n';
31935510Sbostic 		had_eof = true;
32035510Sbostic 		break;
32135510Sbostic 	}
32235510Sbostic 	*p++ = i;
32335510Sbostic 	if (i == '\n')
32435510Sbostic 		break;
3258803Smckusick     }
32624454Smckusick     buf_end = p;
327*36972Sbostic     if (p[-2] == '/' && p[-3] == '*') {
32824454Smckusick 	if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
32924454Smckusick 	    fill_buffer();	/* flush indent error message */
33024454Smckusick 	else {
33124454Smckusick 	    int         com = 0;
3328803Smckusick 
33324454Smckusick 	    p = in_buffer;
33424454Smckusick 	    while (*p == ' ' || *p == '\t')
33524454Smckusick 		p++;
33624454Smckusick 	    if (*p == '/' && p[1] == '*') {
33724454Smckusick 		p += 2;
33824454Smckusick 		while (*p == ' ' || *p == '\t')
33924454Smckusick 		    p++;
34024454Smckusick 		if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E'
34135500Sbostic 			&& p[4] == 'N' && p[5] == 'T') {
34224454Smckusick 		    p += 6;
34324454Smckusick 		    while (*p == ' ' || *p == '\t')
34424454Smckusick 			p++;
34524454Smckusick 		    if (*p == '*')
34624454Smckusick 			com = 1;
34724454Smckusick 		    else if (*p == 'O')
34824454Smckusick 			if (*++p == 'N')
34924454Smckusick 			    p++, com = 1;
35024454Smckusick 			else if (*p == 'F' && *++p == 'F')
35124454Smckusick 			    p++, com = 2;
35224454Smckusick 		    while (*p == ' ' || *p == '\t')
35324454Smckusick 			p++;
35424454Smckusick 		    if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) {
35524454Smckusick 			if (s_com != e_com || s_lab != e_lab || s_code != e_code)
35624454Smckusick 			    dump_line();
35724454Smckusick 			if (!(inhibit_formatting = com - 1)) {
35824454Smckusick 			    n_real_blanklines = 0;
35924454Smckusick 			    postfix_blankline_requested = 0;
36024454Smckusick 			    prefix_blankline_requested = 0;
36124454Smckusick 			    suppress_blanklines = 1;
36224454Smckusick 			}
36324454Smckusick 		    }
36424454Smckusick 		}
36524454Smckusick 	    }
36624454Smckusick 	}
36724454Smckusick     }
36824454Smckusick     if (inhibit_formatting) {
36924454Smckusick 	p = in_buffer;
37024454Smckusick 	do
37124454Smckusick 	    putc(*p, output);
37224454Smckusick 	while (*p++ != '\n');
37324454Smckusick     }
3748803Smckusick     return;
375*36972Sbostic }
3768803Smckusick 
37724454Smckusick /*
37835500Sbostic  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
37935500Sbostic  *
38035500Sbostic  * All rights reserved
38135500Sbostic  *
38235500Sbostic  *
38335500Sbostic  * NAME: pad_output
38435500Sbostic  *
38535500Sbostic  * FUNCTION: Writes tabs and spaces to move the current column up to the desired
38635500Sbostic  * position.
38735500Sbostic  *
38835500Sbostic  * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf.
38935500Sbostic  *
39024454Smckusick  * PARAMETERS: current		integer		The current column target
39135500Sbostic  * nteger		The desired column
39235500Sbostic  *
39335500Sbostic  * RETURNS: Integer value of the new column.  (If current >= target, no action is
39435500Sbostic  * taken, and current is returned.
39535500Sbostic  *
39635500Sbostic  * GLOBALS: None
39735500Sbostic  *
39835500Sbostic  * CALLS: write (sys)
39935500Sbostic  *
40035500Sbostic  * CALLED BY: dump_line
40135500Sbostic  *
40235500Sbostic  * HISTORY: initial coding 	November 1976	D A Willcox of CAC
40335500Sbostic  *
40424454Smckusick  */
40535500Sbostic pad_output(current, target)	/* writes tabs and blanks (if necessary) to
40635500Sbostic 				 * get the current output position up to the
40735500Sbostic 				 * target column */
40824454Smckusick     int         current;	/* the current column value */
40924454Smckusick     int         target;		/* position we want it at */
4108803Smckusick {
41124454Smckusick     register int curr;		/* internal column pointer */
4128803Smckusick     register int tcur;
4138803Smckusick 
41424454Smckusick     if (troff)
41524454Smckusick 	fprintf(output, "\\h'|%dp'", (target - 1) * 7);
41624454Smckusick     else {
41724454Smckusick 	if (current >= target)
41824454Smckusick 	    return (current);	/* line is already long enough */
41924454Smckusick 	curr = current;
42024454Smckusick 	while ((tcur = ((curr - 1) & tabmask) + tabsize + 1) <= target) {
42124454Smckusick 	    putc('\t', output);
4228803Smckusick 	    curr = tcur;
4238803Smckusick 	}
42424454Smckusick 	while (curr++ < target)
42524454Smckusick 	    putc(' ', output);	/* pad with final blanks */
4268803Smckusick     }
4278803Smckusick     return (target);
428*36972Sbostic }
4298803Smckusick 
43024454Smckusick /*
43135500Sbostic  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
43235500Sbostic  *
43335500Sbostic  * All rights reserved
43435500Sbostic  *
43535500Sbostic  *
43635500Sbostic  * NAME: count_spaces
43735500Sbostic  *
43824454Smckusick  * FUNCTION: Find out where printing of a given string will leave the current
43935500Sbostic  * character position on output.
44035500Sbostic  *
44124454Smckusick  * ALGORITHM: Run thru input string and add appropriate values to current
44235500Sbostic  * position.
44335500Sbostic  *
44435500Sbostic  * RETURNS: Integer value of position after printing "buffer" starting in column
44535500Sbostic  * "current".
44635500Sbostic  *
44735500Sbostic  * HISTORY: initial coding 	November 1976	D A Willcox of CAC
44835500Sbostic  *
44924454Smckusick  */
45024454Smckusick int
45124454Smckusick count_spaces(current, buffer)
45224454Smckusick /*
45324454Smckusick  * this routine figures out where the character position will be after
45435500Sbostic  * printing the text in buffer starting at column "current"
45524454Smckusick  */
45624454Smckusick     int         current;
45724454Smckusick     char       *buffer;
4588803Smckusick {
45924454Smckusick     register char *buf;		/* used to look thru buffer */
46024454Smckusick     register int cur;		/* current character counter */
4618803Smckusick 
4628803Smckusick     cur = current;
4638803Smckusick 
4648803Smckusick     for (buf = buffer; *buf != '\0'; ++buf) {
4658803Smckusick 	switch (*buf) {
4668803Smckusick 
46735500Sbostic 	case '\n':
46835500Sbostic 	case 014:		/* form feed */
46935500Sbostic 	    cur = 1;
47035500Sbostic 	    break;
4718803Smckusick 
47235500Sbostic 	case '\t':
47335500Sbostic 	    cur = ((cur - 1) & tabmask) + tabsize + 1;
47435500Sbostic 	    break;
4758803Smckusick 
476*36972Sbostic 	case 010:		/* backspace */
47735500Sbostic 	    --cur;
47835500Sbostic 	    break;
4798803Smckusick 
48035500Sbostic 	default:
48135500Sbostic 	    ++cur;
48235500Sbostic 	    break;
48324454Smckusick 	}			/* end of switch */
48424454Smckusick     }				/* end of for loop */
4858803Smckusick     return (cur);
486*36972Sbostic }
4878803Smckusick 
48835503Sbostic int	found_err;
489*36972Sbostic /* VARARGS2 */
49024454Smckusick diag(level, msg, a, b)
4918803Smckusick {
49235503Sbostic     if (level)
49335503Sbostic 	found_err = 1;
49424454Smckusick     if (output == stdout) {
49524454Smckusick 	fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
49624454Smckusick 	fprintf(stdout, msg, a, b);
49724454Smckusick 	fprintf(stdout, " */\n");
4988803Smckusick     }
49924454Smckusick     else {
50024454Smckusick 	fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
50124454Smckusick 	fprintf(stderr, msg, a, b);
50224454Smckusick 	fprintf(stderr, "\n");
5038803Smckusick     }
5048803Smckusick }
50535500Sbostic 
50635500Sbostic writefdef(f, nm)
50735500Sbostic     register struct fstate *f;
50835500Sbostic {
50935500Sbostic     fprintf(output, ".ds f%c %s\n.nr s%c %d\n",
51035500Sbostic 	    nm, f->font, nm, f->size);
51135500Sbostic }
51235500Sbostic 
51335500Sbostic char       *
51435500Sbostic chfont(of, nf, s)
51535500Sbostic     register struct fstate *of,
51635500Sbostic                *nf;
51735500Sbostic     char       *s;
51835500Sbostic {
51935500Sbostic     if (of->font[0] != nf->font[0]
52035500Sbostic 	    || of->font[1] != nf->font[1]) {
52135500Sbostic 	*s++ = '\\';
52235500Sbostic 	*s++ = 'f';
52335500Sbostic 	if (nf->font[1]) {
52435500Sbostic 	    *s++ = '(';
52535500Sbostic 	    *s++ = nf->font[0];
52635500Sbostic 	    *s++ = nf->font[1];
52735500Sbostic 	}
52835500Sbostic 	else
52935500Sbostic 	    *s++ = nf->font[0];
53035500Sbostic     }
53135500Sbostic     if (nf->size != of->size) {
53235500Sbostic 	*s++ = '\\';
53335500Sbostic 	*s++ = 's';
53435500Sbostic 	if (nf->size < of->size) {
53535500Sbostic 	    *s++ = '-';
53635500Sbostic 	    *s++ = '0' + of->size - nf->size;
53735500Sbostic 	}
53835500Sbostic 	else {
53935500Sbostic 	    *s++ = '+';
54035500Sbostic 	    *s++ = '0' + nf->size - of->size;
54135500Sbostic 	}
54235500Sbostic     }
54335500Sbostic     return s;
54435500Sbostic }
54535500Sbostic 
54635500Sbostic 
54735500Sbostic parsefont(f, s0)
54835500Sbostic     register struct fstate *f;
54935500Sbostic     char       *s0;
55035500Sbostic {
55135500Sbostic     register char *s = s0;
55235500Sbostic     int         sizedelta = 0;
55335500Sbostic     bzero(f, sizeof *f);
55435500Sbostic     while (*s) {
55535500Sbostic 	if (isdigit(*s))
55635500Sbostic 	    f->size = f->size * 10 + *s - '0';
55735500Sbostic 	else if (isupper(*s))
55835500Sbostic 	    if (f->font[0])
55935500Sbostic 		f->font[1] = *s;
56035500Sbostic 	    else
56135500Sbostic 		f->font[0] = *s;
56235500Sbostic 	else if (*s == 'c')
56335500Sbostic 	    f->allcaps = 1;
56435500Sbostic 	else if (*s == '+')
56535500Sbostic 	    sizedelta++;
56635500Sbostic 	else if (*s == '-')
56735500Sbostic 	    sizedelta--;
56835500Sbostic 	else {
56935500Sbostic 	    fprintf(stderr, "indent: bad font specification: %s\n", s0);
57035500Sbostic 	    exit(1);
57135500Sbostic 	}
57235500Sbostic 	s++;
57335500Sbostic     }
57435500Sbostic     if (f->font[0] == 0)
57535500Sbostic 	f->font[0] = 'R';
57635500Sbostic     if (bodyf.size == 0)
57735500Sbostic 	bodyf.size = 11;
57835500Sbostic     if (f->size == 0)
57935500Sbostic 	f->size = bodyf.size + sizedelta;
58035500Sbostic     else if (sizedelta > 0)
58135500Sbostic 	f->size += bodyf.size;
58235500Sbostic     else
58335500Sbostic 	f->size = bodyf.size - f->size;
58435500Sbostic }
585