xref: /csrg-svn/usr.bin/indent/io.c (revision 38011)
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*38011Sbostic static char sccsid[] = "@(#)io.c	5.13 (Berkeley) 05/15/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());
93*38011Sbostic 	    if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0
94*38011Sbostic 				    || strncmp(s_lab, "#endif", 6) == 0)) {
95*38011Sbostic 		register char *s = s_lab;
96*38011Sbostic 		if (e_lab[-1] == '\n') e_lab--;
97*38011Sbostic 		do putc(*s++, output);
98*38011Sbostic 		while (s < e_lab && 'a' <= *s && *s<='z');
99*38011Sbostic 		while ((*s == ' ' || *s == '\t') && s < e_lab)
100*38011Sbostic 		    s++;
101*38011Sbostic 		if (s < e_lab)
102*38011Sbostic 		    fprintf(output, s[0]=='/' && s[1]=='*' ? "\t%.*s" : "\t/* %.*s */",
103*38011Sbostic 			    e_lab - s, s);
104*38011Sbostic 	    }
105*38011Sbostic 	    else fprintf(output, "%.*s", e_lab - s_lab, s_lab);
10624454Smckusick 	    cur_col = count_spaces(cur_col, s_lab);
1078803Smckusick 	}
10824454Smckusick 	else
10924454Smckusick 	    cur_col = 1;	/* there is no label section */
1108803Smckusick 
11124454Smckusick 	ps.pcase = false;
1128803Smckusick 
11324454Smckusick 	if (s_code != e_code) {	/* print code section, if any */
11424454Smckusick 	    register char *p;
1158803Smckusick 
11624454Smckusick 	    if (comment_open) {
11724454Smckusick 		comment_open = 0;
11824454Smckusick 		fprintf(output, ".*/\n");
11924454Smckusick 	    }
12024454Smckusick 	    target_col = compute_code_target();
12124454Smckusick 	    {
12224454Smckusick 		register    i;
1238803Smckusick 
12424454Smckusick 		for (i = 0; i < ps.p_l_follow; i++)
12524454Smckusick 		    if (ps.paren_indents[i] >= 0)
12624454Smckusick 			ps.paren_indents[i] = -(ps.paren_indents[i] + target_col);
12724454Smckusick 	    }
12824454Smckusick 	    cur_col = pad_output(cur_col, target_col);
12924454Smckusick 	    for (p = s_code; p < e_code; p++)
13035500Sbostic 		if (*p == (char) 0200)
13124454Smckusick 		    fprintf(output, "%d", target_col * 7);
13224454Smckusick 		else
13324454Smckusick 		    putc(*p, output);
13424454Smckusick 	    cur_col = count_spaces(cur_col, s_code);
13524454Smckusick 	}
13624454Smckusick 	if (s_com != e_com)
13724454Smckusick 	    if (troff) {
13835500Sbostic 		int         all_here = 0;
13924454Smckusick 		register char *p;
1408803Smckusick 
14124454Smckusick 		if (e_com[-1] == '/' && e_com[-2] == '*')
14235500Sbostic 		    e_com -= 2, all_here++;
14324454Smckusick 		while (e_com > s_com && e_com[-1] == ' ')
14424454Smckusick 		    e_com--;
14524454Smckusick 		*e_com = 0;
14624454Smckusick 		p = s_com;
14724454Smckusick 		while (*p == ' ')
14824454Smckusick 		    p++;
14924454Smckusick 		if (p[0] == '/' && p[1] == '*')
15035500Sbostic 		    p += 2, all_here++;
15124454Smckusick 		else if (p[0] == '*')
15224454Smckusick 		    p += p[1] == '/' ? 2 : 1;
15324454Smckusick 		while (*p == ' ')
15424454Smckusick 		    p++;
15524454Smckusick 		if (*p == 0)
15624454Smckusick 		    goto inhibit_newline;
15735500Sbostic 		if (comment_open < 2 && ps.box_com) {
15835500Sbostic 		    comment_open = 0;
15935500Sbostic 		    fprintf(output, ".*/\n");
16035500Sbostic 		}
16135500Sbostic 		if (comment_open == 0) {
16224454Smckusick 		    if ('a' <= *p && *p <= 'z')
16324454Smckusick 			*p = *p + 'A' - 'a';
16435500Sbostic 		    if (e_com - p < 50 && all_here == 2) {
16535500Sbostic 			register char *follow = p;
16635500Sbostic 			fprintf(output, "\n.nr C! \\w\1");
16735500Sbostic 			while (follow < e_com) {
16835500Sbostic 			    switch (*follow) {
16935500Sbostic 			    case '\n':
17035500Sbostic 				putc(' ', output);
17135500Sbostic 			    case 1:
17235500Sbostic 				break;
17335500Sbostic 			    case '\\':
17435500Sbostic 				putc('\\', output);
17535500Sbostic 			    default:
17635500Sbostic 				putc(*follow, output);
17735500Sbostic 			    }
17835500Sbostic 			    follow++;
17935500Sbostic 			}
18035500Sbostic 			putc(1, output);
18124454Smckusick 		    }
18235500Sbostic 		    fprintf(output, "\n./* %dp %d %dp\n",
18335500Sbostic 			    ps.com_col * 7,
18435500Sbostic 			    (s_code != e_code || s_lab != e_lab) - ps.box_com,
18535500Sbostic 			    target_col * 7);
18624454Smckusick 		}
18735500Sbostic 		comment_open = 1 + ps.box_com;
18824454Smckusick 		while (*p) {
18924454Smckusick 		    if (*p == BACKSLASH)
19024454Smckusick 			putc(BACKSLASH, output);
19124454Smckusick 		    putc(*p++, output);
19224454Smckusick 		}
19324454Smckusick 	    }
19424454Smckusick 	    else {		/* print comment, if any */
19524454Smckusick 		register    target = ps.com_col;
19624454Smckusick 		register char *com_st = s_com;
1978803Smckusick 
19824454Smckusick 		target += ps.comment_delta;
19935500Sbostic 		while (*com_st == '\t')
20035500Sbostic 		    com_st++, target += 8;	/* ? */
20124454Smckusick 		while (target <= 0)
20235500Sbostic 		    if (*com_st == ' ')
20335500Sbostic 			target++, com_st++;
20435500Sbostic 		    else if (*com_st == '\t')
20535500Sbostic 			target = ((target - 1) & ~7) + 9, com_st++;
20624454Smckusick 		    else
20724454Smckusick 			target = 1;
20835500Sbostic 		if (cur_col > target) {	/* if comment cant fit on this line,
20935500Sbostic 					 * put it on next line */
21024454Smckusick 		    putc('\n', output);
21124454Smckusick 		    cur_col = 1;
21224454Smckusick 		    ++ps.out_lines;
21324454Smckusick 		}
21435500Sbostic 		while (e_com > com_st && isspace(e_com[-1]))
21535500Sbostic 		    e_com--;
21624454Smckusick 		cur_col = pad_output(cur_col, target);
21724454Smckusick 		if (!ps.box_com) {
21835500Sbostic 		    if (star_comment_cont && (com_st[1] != '*' || e_com <= com_st + 1))
21935500Sbostic 			if (com_st[1] == ' ' && com_st[0] == ' ' && e_com > com_st + 1)
22024454Smckusick 			    com_st[1] = '*';
22124454Smckusick 			else
22224454Smckusick 			    fwrite(" * ", com_st[0] == '\t' ? 2 : com_st[0] == '*' ? 1 : 3, 1, output);
22324454Smckusick 		}
22424454Smckusick 		fwrite(com_st, e_com - com_st, 1, output);
22524454Smckusick 		ps.comment_delta = ps.n_comment_delta;
22624454Smckusick 		cur_col = count_spaces(cur_col, com_st);
22724454Smckusick 		++ps.com_lines;	/* count lines with comments */
22824454Smckusick 	    }
22924454Smckusick 	if (ps.use_ff)
23024454Smckusick 	    putc('\014', output);
23124454Smckusick 	else
23224454Smckusick 	    putc('\n', output);
23324454Smckusick inhibit_newline:
23424454Smckusick 	++ps.out_lines;
23524454Smckusick 	if (ps.just_saw_decl == 1 && blanklines_after_declarations) {
23624454Smckusick 	    prefix_blankline_requested = 1;
23724454Smckusick 	    ps.just_saw_decl = 0;
2388803Smckusick 	}
23924454Smckusick 	else
24024454Smckusick 	    prefix_blankline_requested = postfix_blankline_requested;
24124454Smckusick 	postfix_blankline_requested = 0;
2428803Smckusick     }
24324454Smckusick     ps.decl_on_line = ps.in_decl;	/* if we are in the middle of a
24435500Sbostic 					 * declaration, remember that fact for
24535500Sbostic 					 * proper comment indentation */
24624454Smckusick     ps.ind_stmt = ps.in_stmt & ~ps.in_decl;	/* next line should be
24724454Smckusick 						 * indented if we have not
24835500Sbostic 						 * completed this stmt and if
24935500Sbostic 						 * we are not in the middle of
25035500Sbostic 						 * a declaration */
25124454Smckusick     ps.use_ff = false;
25224454Smckusick     ps.dumped_decl_indent = 0;
25324454Smckusick     *(e_lab = s_lab) = '\0';	/* reset buffers */
2548803Smckusick     *(e_code = s_code) = '\0';
2558803Smckusick     *(e_com = s_com) = '\0';
25624454Smckusick     ps.ind_level = ps.i_l_follow;
25724454Smckusick     ps.paren_level = ps.p_l_follow;
25824454Smckusick     paren_target = -ps.paren_indents[ps.paren_level - 1];
25935500Sbostic     not_first_line = 1;
2608803Smckusick     return;
26136972Sbostic }
2628803Smckusick 
26335500Sbostic compute_code_target()
26435500Sbostic {
26524454Smckusick     register    target_col = ps.ind_size * ps.ind_level + 1;
2668803Smckusick 
26724454Smckusick     if (ps.paren_level)
26824454Smckusick 	if (!lineup_to_parens)
26924454Smckusick 	    target_col += continuation_indent * ps.paren_level;
27024454Smckusick 	else {
27124454Smckusick 	    register    w;
27224454Smckusick 	    register    t = paren_target;
2738803Smckusick 
27424454Smckusick 	    if ((w = count_spaces(t, s_code) - max_col) > 0
27535500Sbostic 		    && count_spaces(target_col, s_code) <= max_col) {
27624454Smckusick 		t -= w + 1;
27724454Smckusick 		if (t > target_col)
27824454Smckusick 		    target_col = t;
27924454Smckusick 	    }
28024454Smckusick 	    else
28124454Smckusick 		target_col = t;
28224454Smckusick 	}
28324454Smckusick     else if (ps.ind_stmt)
28424454Smckusick 	target_col += continuation_indent;
28524454Smckusick     return target_col;
28624454Smckusick }
2878803Smckusick 
28824454Smckusick compute_label_target()
28924454Smckusick {
29024454Smckusick     return
29135500Sbostic 	ps.pcase ? (int) (case_ind * ps.ind_size) + 1
29224454Smckusick 	: *s_lab == '#' ? 1
29335500Sbostic 	: ps.ind_size * (ps.ind_level - label_offset) + 1;
29424454Smckusick }
2958803Smckusick 
2968803Smckusick 
29724454Smckusick /*
29835500Sbostic  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
29935500Sbostic  *
30035500Sbostic  * All rights reserved
30135500Sbostic  *
30235500Sbostic  *
30335500Sbostic  * NAME: fill_buffer
30435500Sbostic  *
30535500Sbostic  * FUNCTION: Reads one block of input into input_buffer
30635500Sbostic  *
30735500Sbostic  * HISTORY: initial coding 	November 1976	D A Willcox of CAC 1/7/77 A
30835500Sbostic  * Willcox of CAC	Added check for switch back to partly full input
30935500Sbostic  * buffer from temporary buffer
31035500Sbostic  *
311*38011Sbostic  */
31224454Smckusick int
31324454Smckusick fill_buffer()
31424454Smckusick {				/* this routine reads stuff from the input */
31524454Smckusick     register char *p;
31624454Smckusick     register int i;
31724454Smckusick     register FILE *f = input;
3188803Smckusick 
31935500Sbostic     if (bp_save != 0) {		/* there is a partly filled input buffer left */
32024454Smckusick 	buf_ptr = bp_save;	/* dont read anything, just switch buffers */
3218803Smckusick 	buf_end = be_save;
3228803Smckusick 	bp_save = be_save = 0;
3238803Smckusick 	if (buf_ptr < buf_end)
32435500Sbostic 	    return;		/* only return if there is really something in
32535500Sbostic 				 * this buffer */
3268803Smckusick     }
327*38011Sbostic     for (p = in_buffer;;) {
328*38011Sbostic 	if (p >= in_buffer_limit) {
329*38011Sbostic 	    register size = (in_buffer_limit - in_buffer) * 2 + 10;
330*38011Sbostic 	    register offset = p - in_buffer;
331*38011Sbostic 	    in_buffer = (char *) realloc(in_buffer, size);
332*38011Sbostic 	    if (in_buffer == 0)
333*38011Sbostic 		err("input line too long");
334*38011Sbostic 	    p = in_buffer + offset;
335*38011Sbostic 	    in_buffer_limit = in_buffer + size - 2;
336*38011Sbostic 	}
33735510Sbostic 	if ((i = getc(f)) == EOF) {
33835510Sbostic 		*p++ = ' ';
33935510Sbostic 		*p++ = '\n';
34035510Sbostic 		had_eof = true;
34135510Sbostic 		break;
34235510Sbostic 	}
34335510Sbostic 	*p++ = i;
34435510Sbostic 	if (i == '\n')
34535510Sbostic 		break;
3468803Smckusick     }
347*38011Sbostic     buf_ptr = in_buffer;
34824454Smckusick     buf_end = p;
34936972Sbostic     if (p[-2] == '/' && p[-3] == '*') {
35024454Smckusick 	if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
35124454Smckusick 	    fill_buffer();	/* flush indent error message */
35224454Smckusick 	else {
35324454Smckusick 	    int         com = 0;
3548803Smckusick 
35524454Smckusick 	    p = in_buffer;
35624454Smckusick 	    while (*p == ' ' || *p == '\t')
35724454Smckusick 		p++;
35824454Smckusick 	    if (*p == '/' && p[1] == '*') {
35924454Smckusick 		p += 2;
36024454Smckusick 		while (*p == ' ' || *p == '\t')
36124454Smckusick 		    p++;
36224454Smckusick 		if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E'
36335500Sbostic 			&& p[4] == 'N' && p[5] == 'T') {
36424454Smckusick 		    p += 6;
36524454Smckusick 		    while (*p == ' ' || *p == '\t')
36624454Smckusick 			p++;
36724454Smckusick 		    if (*p == '*')
36824454Smckusick 			com = 1;
36924454Smckusick 		    else if (*p == 'O')
37024454Smckusick 			if (*++p == 'N')
37124454Smckusick 			    p++, com = 1;
37224454Smckusick 			else if (*p == 'F' && *++p == 'F')
37324454Smckusick 			    p++, com = 2;
37424454Smckusick 		    while (*p == ' ' || *p == '\t')
37524454Smckusick 			p++;
37624454Smckusick 		    if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) {
37724454Smckusick 			if (s_com != e_com || s_lab != e_lab || s_code != e_code)
37824454Smckusick 			    dump_line();
37924454Smckusick 			if (!(inhibit_formatting = com - 1)) {
38024454Smckusick 			    n_real_blanklines = 0;
38124454Smckusick 			    postfix_blankline_requested = 0;
38224454Smckusick 			    prefix_blankline_requested = 0;
38324454Smckusick 			    suppress_blanklines = 1;
38424454Smckusick 			}
38524454Smckusick 		    }
38624454Smckusick 		}
38724454Smckusick 	    }
38824454Smckusick 	}
38924454Smckusick     }
39024454Smckusick     if (inhibit_formatting) {
39124454Smckusick 	p = in_buffer;
39224454Smckusick 	do
39324454Smckusick 	    putc(*p, output);
39424454Smckusick 	while (*p++ != '\n');
39524454Smckusick     }
3968803Smckusick     return;
39736972Sbostic }
3988803Smckusick 
39924454Smckusick /*
40035500Sbostic  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
40135500Sbostic  *
40235500Sbostic  * All rights reserved
40335500Sbostic  *
40435500Sbostic  *
40535500Sbostic  * NAME: pad_output
40635500Sbostic  *
40735500Sbostic  * FUNCTION: Writes tabs and spaces to move the current column up to the desired
40835500Sbostic  * position.
40935500Sbostic  *
41035500Sbostic  * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf.
41135500Sbostic  *
41224454Smckusick  * PARAMETERS: current		integer		The current column target
41335500Sbostic  * nteger		The desired column
41435500Sbostic  *
41535500Sbostic  * RETURNS: Integer value of the new column.  (If current >= target, no action is
41635500Sbostic  * taken, and current is returned.
41735500Sbostic  *
41835500Sbostic  * GLOBALS: None
41935500Sbostic  *
42035500Sbostic  * CALLS: write (sys)
42135500Sbostic  *
42235500Sbostic  * CALLED BY: dump_line
42335500Sbostic  *
42435500Sbostic  * HISTORY: initial coding 	November 1976	D A Willcox of CAC
42535500Sbostic  *
426*38011Sbostic  */
42735500Sbostic pad_output(current, target)	/* writes tabs and blanks (if necessary) to
42835500Sbostic 				 * get the current output position up to the
42935500Sbostic 				 * target column */
43024454Smckusick     int         current;	/* the current column value */
43124454Smckusick     int         target;		/* position we want it at */
4328803Smckusick {
43324454Smckusick     register int curr;		/* internal column pointer */
4348803Smckusick     register int tcur;
4358803Smckusick 
43624454Smckusick     if (troff)
43724454Smckusick 	fprintf(output, "\\h'|%dp'", (target - 1) * 7);
43824454Smckusick     else {
43924454Smckusick 	if (current >= target)
44024454Smckusick 	    return (current);	/* line is already long enough */
44124454Smckusick 	curr = current;
44224454Smckusick 	while ((tcur = ((curr - 1) & tabmask) + tabsize + 1) <= target) {
44324454Smckusick 	    putc('\t', output);
4448803Smckusick 	    curr = tcur;
4458803Smckusick 	}
44624454Smckusick 	while (curr++ < target)
44724454Smckusick 	    putc(' ', output);	/* pad with final blanks */
4488803Smckusick     }
4498803Smckusick     return (target);
45036972Sbostic }
4518803Smckusick 
45224454Smckusick /*
45335500Sbostic  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
45435500Sbostic  *
45535500Sbostic  * All rights reserved
45635500Sbostic  *
45735500Sbostic  *
45835500Sbostic  * NAME: count_spaces
45935500Sbostic  *
46024454Smckusick  * FUNCTION: Find out where printing of a given string will leave the current
46135500Sbostic  * character position on output.
46235500Sbostic  *
46324454Smckusick  * ALGORITHM: Run thru input string and add appropriate values to current
46435500Sbostic  * position.
46535500Sbostic  *
46635500Sbostic  * RETURNS: Integer value of position after printing "buffer" starting in column
46735500Sbostic  * "current".
46835500Sbostic  *
46935500Sbostic  * HISTORY: initial coding 	November 1976	D A Willcox of CAC
47035500Sbostic  *
471*38011Sbostic  */
47224454Smckusick int
47324454Smckusick count_spaces(current, buffer)
47424454Smckusick /*
47524454Smckusick  * this routine figures out where the character position will be after
47635500Sbostic  * printing the text in buffer starting at column "current"
47724454Smckusick  */
47824454Smckusick     int         current;
47924454Smckusick     char       *buffer;
4808803Smckusick {
48124454Smckusick     register char *buf;		/* used to look thru buffer */
48224454Smckusick     register int cur;		/* current character counter */
4838803Smckusick 
4848803Smckusick     cur = current;
4858803Smckusick 
4868803Smckusick     for (buf = buffer; *buf != '\0'; ++buf) {
4878803Smckusick 	switch (*buf) {
4888803Smckusick 
48935500Sbostic 	case '\n':
49035500Sbostic 	case 014:		/* form feed */
49135500Sbostic 	    cur = 1;
49235500Sbostic 	    break;
4938803Smckusick 
49435500Sbostic 	case '\t':
49535500Sbostic 	    cur = ((cur - 1) & tabmask) + tabsize + 1;
49635500Sbostic 	    break;
4978803Smckusick 
49836972Sbostic 	case 010:		/* backspace */
49935500Sbostic 	    --cur;
50035500Sbostic 	    break;
5018803Smckusick 
50235500Sbostic 	default:
50335500Sbostic 	    ++cur;
50435500Sbostic 	    break;
50524454Smckusick 	}			/* end of switch */
50624454Smckusick     }				/* end of for loop */
5078803Smckusick     return (cur);
50836972Sbostic }
5098803Smckusick 
51035503Sbostic int	found_err;
51136972Sbostic /* VARARGS2 */
51224454Smckusick diag(level, msg, a, b)
5138803Smckusick {
51435503Sbostic     if (level)
51535503Sbostic 	found_err = 1;
51624454Smckusick     if (output == stdout) {
51724454Smckusick 	fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
51824454Smckusick 	fprintf(stdout, msg, a, b);
51924454Smckusick 	fprintf(stdout, " */\n");
5208803Smckusick     }
52124454Smckusick     else {
52224454Smckusick 	fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
52324454Smckusick 	fprintf(stderr, msg, a, b);
52424454Smckusick 	fprintf(stderr, "\n");
5258803Smckusick     }
5268803Smckusick }
52735500Sbostic 
52835500Sbostic writefdef(f, nm)
52935500Sbostic     register struct fstate *f;
53035500Sbostic {
53135500Sbostic     fprintf(output, ".ds f%c %s\n.nr s%c %d\n",
53235500Sbostic 	    nm, f->font, nm, f->size);
53335500Sbostic }
53435500Sbostic 
53535500Sbostic char       *
53635500Sbostic chfont(of, nf, s)
53735500Sbostic     register struct fstate *of,
53835500Sbostic                *nf;
53935500Sbostic     char       *s;
54035500Sbostic {
54135500Sbostic     if (of->font[0] != nf->font[0]
54235500Sbostic 	    || of->font[1] != nf->font[1]) {
54335500Sbostic 	*s++ = '\\';
54435500Sbostic 	*s++ = 'f';
54535500Sbostic 	if (nf->font[1]) {
54635500Sbostic 	    *s++ = '(';
54735500Sbostic 	    *s++ = nf->font[0];
54835500Sbostic 	    *s++ = nf->font[1];
54935500Sbostic 	}
55035500Sbostic 	else
55135500Sbostic 	    *s++ = nf->font[0];
55235500Sbostic     }
55335500Sbostic     if (nf->size != of->size) {
55435500Sbostic 	*s++ = '\\';
55535500Sbostic 	*s++ = 's';
55635500Sbostic 	if (nf->size < of->size) {
55735500Sbostic 	    *s++ = '-';
55835500Sbostic 	    *s++ = '0' + of->size - nf->size;
55935500Sbostic 	}
56035500Sbostic 	else {
56135500Sbostic 	    *s++ = '+';
56235500Sbostic 	    *s++ = '0' + nf->size - of->size;
56335500Sbostic 	}
56435500Sbostic     }
56535500Sbostic     return s;
56635500Sbostic }
56735500Sbostic 
56835500Sbostic 
56935500Sbostic parsefont(f, s0)
57035500Sbostic     register struct fstate *f;
57135500Sbostic     char       *s0;
57235500Sbostic {
57335500Sbostic     register char *s = s0;
57435500Sbostic     int         sizedelta = 0;
57535500Sbostic     bzero(f, sizeof *f);
57635500Sbostic     while (*s) {
57735500Sbostic 	if (isdigit(*s))
57835500Sbostic 	    f->size = f->size * 10 + *s - '0';
57935500Sbostic 	else if (isupper(*s))
58035500Sbostic 	    if (f->font[0])
58135500Sbostic 		f->font[1] = *s;
58235500Sbostic 	    else
58335500Sbostic 		f->font[0] = *s;
58435500Sbostic 	else if (*s == 'c')
58535500Sbostic 	    f->allcaps = 1;
58635500Sbostic 	else if (*s == '+')
58735500Sbostic 	    sizedelta++;
58835500Sbostic 	else if (*s == '-')
58935500Sbostic 	    sizedelta--;
59035500Sbostic 	else {
59135500Sbostic 	    fprintf(stderr, "indent: bad font specification: %s\n", s0);
59235500Sbostic 	    exit(1);
59335500Sbostic 	}
59435500Sbostic 	s++;
59535500Sbostic     }
59635500Sbostic     if (f->font[0] == 0)
59735500Sbostic 	f->font[0] = 'R';
59835500Sbostic     if (bodyf.size == 0)
59935500Sbostic 	bodyf.size = 11;
60035500Sbostic     if (f->size == 0)
60135500Sbostic 	f->size = bodyf.size + sizedelta;
60235500Sbostic     else if (sizedelta > 0)
60335500Sbostic 	f->size += bodyf.size;
60435500Sbostic     else
60535500Sbostic 	f->size = bodyf.size - f->size;
60635500Sbostic }
607