xref: /freebsd/usr.bin/indent/io.c (revision 8c7e7698527463cd6c24b4ab5aa86a44edb85572)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1985 Sun Microsystems, Inc.
39b50d902SRodney W. Grimes  * Copyright (c) 1980, 1993
49b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
59b50d902SRodney W. Grimes  * All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes  * are met:
109b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
159b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
169b50d902SRodney W. Grimes  *    must display the following acknowledgement:
179b50d902SRodney W. Grimes  *	This product includes software developed by the University of
189b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
199b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
209b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
219b50d902SRodney W. Grimes  *    without specific prior written permission.
229b50d902SRodney W. Grimes  *
239b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
249b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
259b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
269b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
279b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
289b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
299b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
309b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
319b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
329b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
339b50d902SRodney W. Grimes  * SUCH DAMAGE.
349b50d902SRodney W. Grimes  */
359b50d902SRodney W. Grimes 
369b50d902SRodney W. Grimes #ifndef lint
37958d7c9fSPhilippe Charnier #if 0
389b50d902SRodney W. Grimes static char sccsid[] = "@(#)io.c	8.1 (Berkeley) 6/6/93";
39958d7c9fSPhilippe Charnier #endif
40958d7c9fSPhilippe Charnier static const char rcsid[] =
41175f26d6SDavid E. O'Brien   "$FreeBSD$";
429b50d902SRodney W. Grimes #endif /* not lint */
439b50d902SRodney W. Grimes 
449b50d902SRodney W. Grimes #include <ctype.h>
45958d7c9fSPhilippe Charnier #include <err.h>
46958d7c9fSPhilippe Charnier #include <stdio.h>
479b50d902SRodney W. Grimes #include <stdlib.h>
489b50d902SRodney W. Grimes #include <string.h>
499b50d902SRodney W. Grimes #include "indent_globs.h"
507916863dSJens Schweikhardt #include "indent.h"
519b50d902SRodney W. Grimes 
529b50d902SRodney W. Grimes int         comment_open;
537916863dSJens Schweikhardt static int  paren_target;
547916863dSJens Schweikhardt static int pad_output(int current, int target);
559b50d902SRodney W. Grimes 
567916863dSJens Schweikhardt void
577916863dSJens Schweikhardt dump_line(void)
589b50d902SRodney W. Grimes {				/* dump_line is the routine that actually
599b50d902SRodney W. Grimes 				 * effects the printing of the new source. It
609b50d902SRodney W. Grimes 				 * prints the label section, followed by the
619b50d902SRodney W. Grimes 				 * code section with the appropriate nesting
629b50d902SRodney W. Grimes 				 * level, followed by any comments */
639b50d902SRodney W. Grimes     register int cur_col,
647916863dSJens Schweikhardt                 target_col = 1;
657916863dSJens Schweikhardt     static int  not_first_line;
669b50d902SRodney W. Grimes 
679b50d902SRodney W. Grimes     if (ps.procname[0]) {
689b50d902SRodney W. Grimes 	if (troff) {
699b50d902SRodney W. Grimes 	    if (comment_open) {
709b50d902SRodney W. Grimes 		comment_open = 0;
719b50d902SRodney W. Grimes 		fprintf(output, ".*/\n");
729b50d902SRodney W. Grimes 	    }
739b50d902SRodney W. Grimes 	    fprintf(output, ".Pr \"%s\"\n", ps.procname);
749b50d902SRodney W. Grimes 	}
759b50d902SRodney W. Grimes 	ps.ind_level = 0;
769b50d902SRodney W. Grimes 	ps.procname[0] = 0;
779b50d902SRodney W. Grimes     }
789b50d902SRodney W. Grimes     if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
799b50d902SRodney W. Grimes 	if (suppress_blanklines > 0)
809b50d902SRodney W. Grimes 	    suppress_blanklines--;
819b50d902SRodney W. Grimes 	else {
829b50d902SRodney W. Grimes 	    ps.bl_line = true;
839b50d902SRodney W. Grimes 	    n_real_blanklines++;
849b50d902SRodney W. Grimes 	}
859b50d902SRodney W. Grimes     }
869b50d902SRodney W. Grimes     else if (!inhibit_formatting) {
879b50d902SRodney W. Grimes 	suppress_blanklines = 0;
889b50d902SRodney W. Grimes 	ps.bl_line = false;
899ef5c48bSBill Fumerola 	if (prefix_blankline_requested && not_first_line) {
909b50d902SRodney W. Grimes 	    if (swallow_optional_blanklines) {
919b50d902SRodney W. Grimes 		if (n_real_blanklines == 1)
929b50d902SRodney W. Grimes 		    n_real_blanklines = 0;
939b50d902SRodney W. Grimes 	    }
949b50d902SRodney W. Grimes 	    else {
959b50d902SRodney W. Grimes 		if (n_real_blanklines == 0)
969b50d902SRodney W. Grimes 		    n_real_blanklines = 1;
979b50d902SRodney W. Grimes 	    }
989ef5c48bSBill Fumerola 	}
999b50d902SRodney W. Grimes 	while (--n_real_blanklines >= 0)
1009b50d902SRodney W. Grimes 	    putc('\n', output);
1019b50d902SRodney W. Grimes 	n_real_blanklines = 0;
1029b50d902SRodney W. Grimes 	if (ps.ind_level == 0)
1039b50d902SRodney W. Grimes 	    ps.ind_stmt = 0;	/* this is a class A kludge. dont do
1049b50d902SRodney W. Grimes 				 * additional statement indentation if we are
1059b50d902SRodney W. Grimes 				 * at bracket level 0 */
1069b50d902SRodney W. Grimes 
1079b50d902SRodney W. Grimes 	if (e_lab != s_lab || e_code != s_code)
1089b50d902SRodney W. Grimes 	    ++code_lines;	/* keep count of lines with code */
1099b50d902SRodney W. Grimes 
1109b50d902SRodney W. Grimes 
1119b50d902SRodney W. Grimes 	if (e_lab != s_lab) {	/* print lab, if any */
1129b50d902SRodney W. Grimes 	    if (comment_open) {
1139b50d902SRodney W. Grimes 		comment_open = 0;
1149b50d902SRodney W. Grimes 		fprintf(output, ".*/\n");
1159b50d902SRodney W. Grimes 	    }
1169b50d902SRodney W. Grimes 	    while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
1179b50d902SRodney W. Grimes 		e_lab--;
1189b50d902SRodney W. Grimes 	    cur_col = pad_output(1, compute_label_target());
1199b50d902SRodney W. Grimes 	    if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0
1209b50d902SRodney W. Grimes 				    || strncmp(s_lab, "#endif", 6) == 0)) {
1219b50d902SRodney W. Grimes 		register char *s = s_lab;
1229b50d902SRodney W. Grimes 		if (e_lab[-1] == '\n') e_lab--;
1239b50d902SRodney W. Grimes 		do putc(*s++, output);
1249b50d902SRodney W. Grimes 		while (s < e_lab && 'a' <= *s && *s<='z');
1259b50d902SRodney W. Grimes 		while ((*s == ' ' || *s == '\t') && s < e_lab)
1269b50d902SRodney W. Grimes 		    s++;
1279b50d902SRodney W. Grimes 		if (s < e_lab)
1289b50d902SRodney W. Grimes 		    fprintf(output, s[0]=='/' && s[1]=='*' ? "\t%.*s" : "\t/* %.*s */",
1298c7e7698SDavid Malone 			    (int)(e_lab - s), s);
1309b50d902SRodney W. Grimes 	    }
1318c7e7698SDavid Malone 	    else fprintf(output, "%.*s", (int)(e_lab - s_lab), s_lab);
1329b50d902SRodney W. Grimes 	    cur_col = count_spaces(cur_col, s_lab);
1339b50d902SRodney W. Grimes 	}
1349b50d902SRodney W. Grimes 	else
1359b50d902SRodney W. Grimes 	    cur_col = 1;	/* there is no label section */
1369b50d902SRodney W. Grimes 
1379b50d902SRodney W. Grimes 	ps.pcase = false;
1389b50d902SRodney W. Grimes 
1399b50d902SRodney W. Grimes 	if (s_code != e_code) {	/* print code section, if any */
1409b50d902SRodney W. Grimes 	    register char *p;
1419b50d902SRodney W. Grimes 
1429b50d902SRodney W. Grimes 	    if (comment_open) {
1439b50d902SRodney W. Grimes 		comment_open = 0;
1449b50d902SRodney W. Grimes 		fprintf(output, ".*/\n");
1459b50d902SRodney W. Grimes 	    }
1469b50d902SRodney W. Grimes 	    target_col = compute_code_target();
1479b50d902SRodney W. Grimes 	    {
1487916863dSJens Schweikhardt 		register    int i;
1499b50d902SRodney W. Grimes 
1509b50d902SRodney W. Grimes 		for (i = 0; i < ps.p_l_follow; i++)
1519b50d902SRodney W. Grimes 		    if (ps.paren_indents[i] >= 0)
1529b50d902SRodney W. Grimes 			ps.paren_indents[i] = -(ps.paren_indents[i] + target_col);
1539b50d902SRodney W. Grimes 	    }
1549b50d902SRodney W. Grimes 	    cur_col = pad_output(cur_col, target_col);
1559b50d902SRodney W. Grimes 	    for (p = s_code; p < e_code; p++)
1569b50d902SRodney W. Grimes 		if (*p == (char) 0200)
1579b50d902SRodney W. Grimes 		    fprintf(output, "%d", target_col * 7);
1589b50d902SRodney W. Grimes 		else
1599b50d902SRodney W. Grimes 		    putc(*p, output);
1609b50d902SRodney W. Grimes 	    cur_col = count_spaces(cur_col, s_code);
1619b50d902SRodney W. Grimes 	}
1627916863dSJens Schweikhardt 	if (s_com != e_com) {
1639b50d902SRodney W. Grimes 	    if (troff) {
1649b50d902SRodney W. Grimes 		int         all_here = 0;
1659b50d902SRodney W. Grimes 		register char *p;
1669b50d902SRodney W. Grimes 
1679b50d902SRodney W. Grimes 		if (e_com[-1] == '/' && e_com[-2] == '*')
1689b50d902SRodney W. Grimes 		    e_com -= 2, all_here++;
1699b50d902SRodney W. Grimes 		while (e_com > s_com && e_com[-1] == ' ')
1709b50d902SRodney W. Grimes 		    e_com--;
1719b50d902SRodney W. Grimes 		*e_com = 0;
1729b50d902SRodney W. Grimes 		p = s_com;
1739b50d902SRodney W. Grimes 		while (*p == ' ')
1749b50d902SRodney W. Grimes 		    p++;
1759b50d902SRodney W. Grimes 		if (p[0] == '/' && p[1] == '*')
1769b50d902SRodney W. Grimes 		    p += 2, all_here++;
1779b50d902SRodney W. Grimes 		else if (p[0] == '*')
1789b50d902SRodney W. Grimes 		    p += p[1] == '/' ? 2 : 1;
1799b50d902SRodney W. Grimes 		while (*p == ' ')
1809b50d902SRodney W. Grimes 		    p++;
1819b50d902SRodney W. Grimes 		if (*p == 0)
1829b50d902SRodney W. Grimes 		    goto inhibit_newline;
1839b50d902SRodney W. Grimes 		if (comment_open < 2 && ps.box_com) {
1849b50d902SRodney W. Grimes 		    comment_open = 0;
1859b50d902SRodney W. Grimes 		    fprintf(output, ".*/\n");
1869b50d902SRodney W. Grimes 		}
1879b50d902SRodney W. Grimes 		if (comment_open == 0) {
1889b50d902SRodney W. Grimes 		    if ('a' <= *p && *p <= 'z')
1899b50d902SRodney W. Grimes 			*p = *p + 'A' - 'a';
1909b50d902SRodney W. Grimes 		    if (e_com - p < 50 && all_here == 2) {
1919b50d902SRodney W. Grimes 			register char *follow = p;
1929b50d902SRodney W. Grimes 			fprintf(output, "\n.nr C! \\w\1");
1939b50d902SRodney W. Grimes 			while (follow < e_com) {
1949b50d902SRodney W. Grimes 			    switch (*follow) {
1959b50d902SRodney W. Grimes 			    case '\n':
1969b50d902SRodney W. Grimes 				putc(' ', output);
1979b50d902SRodney W. Grimes 			    case 1:
1989b50d902SRodney W. Grimes 				break;
1999b50d902SRodney W. Grimes 			    case '\\':
2009b50d902SRodney W. Grimes 				putc('\\', output);
2019b50d902SRodney W. Grimes 			    default:
2029b50d902SRodney W. Grimes 				putc(*follow, output);
2039b50d902SRodney W. Grimes 			    }
2049b50d902SRodney W. Grimes 			    follow++;
2059b50d902SRodney W. Grimes 			}
2069b50d902SRodney W. Grimes 			putc(1, output);
2079b50d902SRodney W. Grimes 		    }
2089b50d902SRodney W. Grimes 		    fprintf(output, "\n./* %dp %d %dp\n",
2099b50d902SRodney W. Grimes 			    ps.com_col * 7,
2109b50d902SRodney W. Grimes 			    (s_code != e_code || s_lab != e_lab) - ps.box_com,
2119b50d902SRodney W. Grimes 			    target_col * 7);
2129b50d902SRodney W. Grimes 		}
2139b50d902SRodney W. Grimes 		comment_open = 1 + ps.box_com;
2149b50d902SRodney W. Grimes 		while (*p) {
2159b50d902SRodney W. Grimes 		    if (*p == BACKSLASH)
2169b50d902SRodney W. Grimes 			putc(BACKSLASH, output);
2179b50d902SRodney W. Grimes 		    putc(*p++, output);
2189b50d902SRodney W. Grimes 		}
2199b50d902SRodney W. Grimes 	    }
2209b50d902SRodney W. Grimes 	    else {		/* print comment, if any */
2217916863dSJens Schweikhardt 		register    int target = ps.com_col;
2229b50d902SRodney W. Grimes 		register char *com_st = s_com;
2239b50d902SRodney W. Grimes 
2249b50d902SRodney W. Grimes 		target += ps.comment_delta;
2259b50d902SRodney W. Grimes 		while (*com_st == '\t')
2269b50d902SRodney W. Grimes 		    com_st++, target += 8;	/* ? */
2279b50d902SRodney W. Grimes 		while (target <= 0)
2289b50d902SRodney W. Grimes 		    if (*com_st == ' ')
2299b50d902SRodney W. Grimes 			target++, com_st++;
2309b50d902SRodney W. Grimes 		    else if (*com_st == '\t')
2319b50d902SRodney W. Grimes 			target = ((target - 1) & ~7) + 9, com_st++;
2329b50d902SRodney W. Grimes 		    else
2339b50d902SRodney W. Grimes 			target = 1;
2349b50d902SRodney W. Grimes 		if (cur_col > target) {	/* if comment cant fit on this line,
2359b50d902SRodney W. Grimes 					 * put it on next line */
2369b50d902SRodney W. Grimes 		    putc('\n', output);
2379b50d902SRodney W. Grimes 		    cur_col = 1;
2389b50d902SRodney W. Grimes 		    ++ps.out_lines;
2399b50d902SRodney W. Grimes 		}
2409b50d902SRodney W. Grimes 		while (e_com > com_st && isspace(e_com[-1]))
2419b50d902SRodney W. Grimes 		    e_com--;
2429b50d902SRodney W. Grimes 		cur_col = pad_output(cur_col, target);
2439b50d902SRodney W. Grimes 		if (!ps.box_com) {
2449ef5c48bSBill Fumerola 		    if (star_comment_cont && (com_st[1] != '*' || e_com <= com_st + 1)) {
2459b50d902SRodney W. Grimes 			if (com_st[1] == ' ' && com_st[0] == ' ' && e_com > com_st + 1)
2469b50d902SRodney W. Grimes 			    com_st[1] = '*';
2479b50d902SRodney W. Grimes 			else
2489b50d902SRodney W. Grimes 			    fwrite(" * ", com_st[0] == '\t' ? 2 : com_st[0] == '*' ? 1 : 3, 1, output);
2499b50d902SRodney W. Grimes 		    }
2509ef5c48bSBill Fumerola 		}
2519b50d902SRodney W. Grimes 		fwrite(com_st, e_com - com_st, 1, output);
2529b50d902SRodney W. Grimes 		ps.comment_delta = ps.n_comment_delta;
2539b50d902SRodney W. Grimes 		cur_col = count_spaces(cur_col, com_st);
2549b50d902SRodney W. Grimes 		++ps.com_lines;	/* count lines with comments */
2559b50d902SRodney W. Grimes 	    }
2567916863dSJens Schweikhardt 	}
2579b50d902SRodney W. Grimes 	if (ps.use_ff)
2589b50d902SRodney W. Grimes 	    putc('\014', output);
2599b50d902SRodney W. Grimes 	else
2609b50d902SRodney W. Grimes 	    putc('\n', output);
2619b50d902SRodney W. Grimes inhibit_newline:
2629b50d902SRodney W. Grimes 	++ps.out_lines;
2639b50d902SRodney W. Grimes 	if (ps.just_saw_decl == 1 && blanklines_after_declarations) {
2649b50d902SRodney W. Grimes 	    prefix_blankline_requested = 1;
2659b50d902SRodney W. Grimes 	    ps.just_saw_decl = 0;
2669b50d902SRodney W. Grimes 	}
2679b50d902SRodney W. Grimes 	else
2689b50d902SRodney W. Grimes 	    prefix_blankline_requested = postfix_blankline_requested;
2699b50d902SRodney W. Grimes 	postfix_blankline_requested = 0;
2709b50d902SRodney W. Grimes     }
2719b50d902SRodney W. Grimes     ps.decl_on_line = ps.in_decl;	/* if we are in the middle of a
2729b50d902SRodney W. Grimes 					 * declaration, remember that fact for
2739b50d902SRodney W. Grimes 					 * proper comment indentation */
2749b50d902SRodney W. Grimes     ps.ind_stmt = ps.in_stmt & ~ps.in_decl;	/* next line should be
2759b50d902SRodney W. Grimes 						 * indented if we have not
2769b50d902SRodney W. Grimes 						 * completed this stmt and if
2779b50d902SRodney W. Grimes 						 * we are not in the middle of
2789b50d902SRodney W. Grimes 						 * a declaration */
2799b50d902SRodney W. Grimes     ps.use_ff = false;
2809b50d902SRodney W. Grimes     ps.dumped_decl_indent = 0;
2819b50d902SRodney W. Grimes     *(e_lab = s_lab) = '\0';	/* reset buffers */
2829b50d902SRodney W. Grimes     *(e_code = s_code) = '\0';
2839b50d902SRodney W. Grimes     *(e_com = s_com) = '\0';
2849b50d902SRodney W. Grimes     ps.ind_level = ps.i_l_follow;
2859b50d902SRodney W. Grimes     ps.paren_level = ps.p_l_follow;
2869b50d902SRodney W. Grimes     paren_target = -ps.paren_indents[ps.paren_level - 1];
2879b50d902SRodney W. Grimes     not_first_line = 1;
2889b50d902SRodney W. Grimes }
2899b50d902SRodney W. Grimes 
2907916863dSJens Schweikhardt int
2917916863dSJens Schweikhardt compute_code_target(void)
2929b50d902SRodney W. Grimes {
2937916863dSJens Schweikhardt     register int target_col = ps.ind_size * ps.ind_level + 1;
2949b50d902SRodney W. Grimes 
2959b50d902SRodney W. Grimes     if (ps.paren_level)
2969b50d902SRodney W. Grimes 	if (!lineup_to_parens)
2979b50d902SRodney W. Grimes 	    target_col += continuation_indent * ps.paren_level;
2989b50d902SRodney W. Grimes 	else {
2997916863dSJens Schweikhardt 	    register int w;
3007916863dSJens Schweikhardt 	    register int t = paren_target;
3019b50d902SRodney W. Grimes 
3029b50d902SRodney W. Grimes 	    if ((w = count_spaces(t, s_code) - max_col) > 0
3039b50d902SRodney W. Grimes 		    && count_spaces(target_col, s_code) <= max_col) {
3049b50d902SRodney W. Grimes 		t -= w + 1;
3059b50d902SRodney W. Grimes 		if (t > target_col)
3069b50d902SRodney W. Grimes 		    target_col = t;
3079b50d902SRodney W. Grimes 	    }
3089b50d902SRodney W. Grimes 	    else
3099b50d902SRodney W. Grimes 		target_col = t;
3109b50d902SRodney W. Grimes 	}
3119b50d902SRodney W. Grimes     else if (ps.ind_stmt)
3129b50d902SRodney W. Grimes 	target_col += continuation_indent;
3139b50d902SRodney W. Grimes     return target_col;
3149b50d902SRodney W. Grimes }
3159b50d902SRodney W. Grimes 
3167916863dSJens Schweikhardt int
3177916863dSJens Schweikhardt compute_label_target(void)
3189b50d902SRodney W. Grimes {
3199b50d902SRodney W. Grimes     return
3209b50d902SRodney W. Grimes 	ps.pcase ? (int) (case_ind * ps.ind_size) + 1
3219b50d902SRodney W. Grimes 	: *s_lab == '#' ? 1
3229b50d902SRodney W. Grimes 	: ps.ind_size * (ps.ind_level - label_offset) + 1;
3239b50d902SRodney W. Grimes }
3249b50d902SRodney W. Grimes 
3259b50d902SRodney W. Grimes 
3269b50d902SRodney W. Grimes /*
3279b50d902SRodney W. Grimes  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
3289b50d902SRodney W. Grimes  *
3299b50d902SRodney W. Grimes  * All rights reserved
3309b50d902SRodney W. Grimes  *
3319b50d902SRodney W. Grimes  *
3329b50d902SRodney W. Grimes  * NAME: fill_buffer
3339b50d902SRodney W. Grimes  *
3349b50d902SRodney W. Grimes  * FUNCTION: Reads one block of input into input_buffer
3359b50d902SRodney W. Grimes  *
3369b50d902SRodney W. Grimes  * HISTORY: initial coding 	November 1976	D A Willcox of CAC 1/7/77 A
3379b50d902SRodney W. Grimes  * Willcox of CAC	Added check for switch back to partly full input
3389b50d902SRodney W. Grimes  * buffer from temporary buffer
3399b50d902SRodney W. Grimes  *
3409b50d902SRodney W. Grimes  */
3417916863dSJens Schweikhardt void
3427916863dSJens Schweikhardt fill_buffer(void)
3439b50d902SRodney W. Grimes {				/* this routine reads stuff from the input */
3449b50d902SRodney W. Grimes     register char *p;
3459b50d902SRodney W. Grimes     register int i;
3469b50d902SRodney W. Grimes     register FILE *f = input;
3479b50d902SRodney W. Grimes 
3489b50d902SRodney W. Grimes     if (bp_save != 0) {		/* there is a partly filled input buffer left */
3499b50d902SRodney W. Grimes 	buf_ptr = bp_save;	/* dont read anything, just switch buffers */
3509b50d902SRodney W. Grimes 	buf_end = be_save;
3519b50d902SRodney W. Grimes 	bp_save = be_save = 0;
3529b50d902SRodney W. Grimes 	if (buf_ptr < buf_end)
3539b50d902SRodney W. Grimes 	    return;		/* only return if there is really something in
3549b50d902SRodney W. Grimes 				 * this buffer */
3559b50d902SRodney W. Grimes     }
3569b50d902SRodney W. Grimes     for (p = in_buffer;;) {
3579b50d902SRodney W. Grimes 	if (p >= in_buffer_limit) {
3587916863dSJens Schweikhardt 	    register int size = (in_buffer_limit - in_buffer) * 2 + 10;
3597916863dSJens Schweikhardt 	    register int offset = p - in_buffer;
3607916863dSJens Schweikhardt 	    in_buffer = realloc(in_buffer, size);
3619b50d902SRodney W. Grimes 	    if (in_buffer == 0)
362958d7c9fSPhilippe Charnier 		err(1, "input line too long");
3639b50d902SRodney W. Grimes 	    p = in_buffer + offset;
3649b50d902SRodney W. Grimes 	    in_buffer_limit = in_buffer + size - 2;
3659b50d902SRodney W. Grimes 	}
3669b50d902SRodney W. Grimes 	if ((i = getc(f)) == EOF) {
3679b50d902SRodney W. Grimes 		*p++ = ' ';
3689b50d902SRodney W. Grimes 		*p++ = '\n';
3699b50d902SRodney W. Grimes 		had_eof = true;
3709b50d902SRodney W. Grimes 		break;
3719b50d902SRodney W. Grimes 	}
3729b50d902SRodney W. Grimes 	*p++ = i;
3739b50d902SRodney W. Grimes 	if (i == '\n')
3749b50d902SRodney W. Grimes 		break;
3759b50d902SRodney W. Grimes     }
3769b50d902SRodney W. Grimes     buf_ptr = in_buffer;
3779b50d902SRodney W. Grimes     buf_end = p;
3789b50d902SRodney W. Grimes     if (p[-2] == '/' && p[-3] == '*') {
3799b50d902SRodney W. Grimes 	if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
3809b50d902SRodney W. Grimes 	    fill_buffer();	/* flush indent error message */
3819b50d902SRodney W. Grimes 	else {
3829b50d902SRodney W. Grimes 	    int         com = 0;
3839b50d902SRodney W. Grimes 
3849b50d902SRodney W. Grimes 	    p = in_buffer;
3859b50d902SRodney W. Grimes 	    while (*p == ' ' || *p == '\t')
3869b50d902SRodney W. Grimes 		p++;
3879b50d902SRodney W. Grimes 	    if (*p == '/' && p[1] == '*') {
3889b50d902SRodney W. Grimes 		p += 2;
3899b50d902SRodney W. Grimes 		while (*p == ' ' || *p == '\t')
3909b50d902SRodney W. Grimes 		    p++;
3919b50d902SRodney W. Grimes 		if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E'
3929b50d902SRodney W. Grimes 			&& p[4] == 'N' && p[5] == 'T') {
3939b50d902SRodney W. Grimes 		    p += 6;
3949b50d902SRodney W. Grimes 		    while (*p == ' ' || *p == '\t')
3959b50d902SRodney W. Grimes 			p++;
3969b50d902SRodney W. Grimes 		    if (*p == '*')
3979b50d902SRodney W. Grimes 			com = 1;
3987916863dSJens Schweikhardt 		    else if (*p == 'O') {
3999b50d902SRodney W. Grimes 			if (*++p == 'N')
4009b50d902SRodney W. Grimes 			    p++, com = 1;
4019b50d902SRodney W. Grimes 			else if (*p == 'F' && *++p == 'F')
4029b50d902SRodney W. Grimes 			    p++, com = 2;
4037916863dSJens Schweikhardt 		    }
4049b50d902SRodney W. Grimes 		    while (*p == ' ' || *p == '\t')
4059b50d902SRodney W. Grimes 			p++;
4069b50d902SRodney W. Grimes 		    if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) {
4079b50d902SRodney W. Grimes 			if (s_com != e_com || s_lab != e_lab || s_code != e_code)
4089b50d902SRodney W. Grimes 			    dump_line();
4099b50d902SRodney W. Grimes 			if (!(inhibit_formatting = com - 1)) {
4109b50d902SRodney W. Grimes 			    n_real_blanklines = 0;
4119b50d902SRodney W. Grimes 			    postfix_blankline_requested = 0;
4129b50d902SRodney W. Grimes 			    prefix_blankline_requested = 0;
4139b50d902SRodney W. Grimes 			    suppress_blanklines = 1;
4149b50d902SRodney W. Grimes 			}
4159b50d902SRodney W. Grimes 		    }
4169b50d902SRodney W. Grimes 		}
4179b50d902SRodney W. Grimes 	    }
4189b50d902SRodney W. Grimes 	}
4199b50d902SRodney W. Grimes     }
4209b50d902SRodney W. Grimes     if (inhibit_formatting) {
4219b50d902SRodney W. Grimes 	p = in_buffer;
4229b50d902SRodney W. Grimes 	do
4239b50d902SRodney W. Grimes 	    putc(*p, output);
4249b50d902SRodney W. Grimes 	while (*p++ != '\n');
4259b50d902SRodney W. Grimes     }
4269b50d902SRodney W. Grimes }
4279b50d902SRodney W. Grimes 
4289b50d902SRodney W. Grimes /*
4299b50d902SRodney W. Grimes  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
4309b50d902SRodney W. Grimes  *
4319b50d902SRodney W. Grimes  * All rights reserved
4329b50d902SRodney W. Grimes  *
4339b50d902SRodney W. Grimes  *
4349b50d902SRodney W. Grimes  * NAME: pad_output
4359b50d902SRodney W. Grimes  *
4369b50d902SRodney W. Grimes  * FUNCTION: Writes tabs and spaces to move the current column up to the desired
4379b50d902SRodney W. Grimes  * position.
4389b50d902SRodney W. Grimes  *
4399b50d902SRodney W. Grimes  * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf.
4409b50d902SRodney W. Grimes  *
4419b50d902SRodney W. Grimes  * PARAMETERS: current		integer		The current column target
4429b50d902SRodney W. Grimes  * nteger		The desired column
4439b50d902SRodney W. Grimes  *
4449b50d902SRodney W. Grimes  * RETURNS: Integer value of the new column.  (If current >= target, no action is
4459b50d902SRodney W. Grimes  * taken, and current is returned.
4469b50d902SRodney W. Grimes  *
4479b50d902SRodney W. Grimes  * GLOBALS: None
4489b50d902SRodney W. Grimes  *
4499b50d902SRodney W. Grimes  * CALLS: write (sys)
4509b50d902SRodney W. Grimes  *
4519b50d902SRodney W. Grimes  * CALLED BY: dump_line
4529b50d902SRodney W. Grimes  *
4539b50d902SRodney W. Grimes  * HISTORY: initial coding 	November 1976	D A Willcox of CAC
4549b50d902SRodney W. Grimes  *
4559b50d902SRodney W. Grimes  */
4567916863dSJens Schweikhardt static int
4577916863dSJens Schweikhardt pad_output(int current, int target)
4587916863dSJens Schweikhardt 			        /* writes tabs and blanks (if necessary) to
4599b50d902SRodney W. Grimes 				 * get the current output position up to the
4609b50d902SRodney W. Grimes 				 * target column */
4617916863dSJens Schweikhardt     /* current: the current column value */
4627916863dSJens Schweikhardt     /* target: position we want it at */
4639b50d902SRodney W. Grimes {
4649b50d902SRodney W. Grimes     register int curr;		/* internal column pointer */
4659b50d902SRodney W. Grimes     register int tcur;
4669b50d902SRodney W. Grimes 
4679b50d902SRodney W. Grimes     if (troff)
4689b50d902SRodney W. Grimes 	fprintf(output, "\\h'|%dp'", (target - 1) * 7);
4699b50d902SRodney W. Grimes     else {
4709b50d902SRodney W. Grimes 	if (current >= target)
4719b50d902SRodney W. Grimes 	    return (current);	/* line is already long enough */
4729b50d902SRodney W. Grimes 	curr = current;
4739b50d902SRodney W. Grimes 	while ((tcur = ((curr - 1) & tabmask) + tabsize + 1) <= target) {
4749b50d902SRodney W. Grimes 	    putc('\t', output);
4759b50d902SRodney W. Grimes 	    curr = tcur;
4769b50d902SRodney W. Grimes 	}
4779b50d902SRodney W. Grimes 	while (curr++ < target)
4789b50d902SRodney W. Grimes 	    putc(' ', output);	/* pad with final blanks */
4799b50d902SRodney W. Grimes     }
4809b50d902SRodney W. Grimes     return (target);
4819b50d902SRodney W. Grimes }
4829b50d902SRodney W. Grimes 
4839b50d902SRodney W. Grimes /*
4849b50d902SRodney W. Grimes  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
4859b50d902SRodney W. Grimes  *
4869b50d902SRodney W. Grimes  * All rights reserved
4879b50d902SRodney W. Grimes  *
4889b50d902SRodney W. Grimes  *
4899b50d902SRodney W. Grimes  * NAME: count_spaces
4909b50d902SRodney W. Grimes  *
4919b50d902SRodney W. Grimes  * FUNCTION: Find out where printing of a given string will leave the current
4929b50d902SRodney W. Grimes  * character position on output.
4939b50d902SRodney W. Grimes  *
4949b50d902SRodney W. Grimes  * ALGORITHM: Run thru input string and add appropriate values to current
4959b50d902SRodney W. Grimes  * position.
4969b50d902SRodney W. Grimes  *
4979b50d902SRodney W. Grimes  * RETURNS: Integer value of position after printing "buffer" starting in column
4989b50d902SRodney W. Grimes  * "current".
4999b50d902SRodney W. Grimes  *
5009b50d902SRodney W. Grimes  * HISTORY: initial coding 	November 1976	D A Willcox of CAC
5019b50d902SRodney W. Grimes  *
5029b50d902SRodney W. Grimes  */
5039b50d902SRodney W. Grimes int
5047916863dSJens Schweikhardt count_spaces(int current, char *buffer)
5059b50d902SRodney W. Grimes /*
5069b50d902SRodney W. Grimes  * this routine figures out where the character position will be after
5079b50d902SRodney W. Grimes  * printing the text in buffer starting at column "current"
5089b50d902SRodney W. Grimes  */
5099b50d902SRodney W. Grimes {
5109b50d902SRodney W. Grimes     register char *buf;		/* used to look thru buffer */
5119b50d902SRodney W. Grimes     register int cur;		/* current character counter */
5129b50d902SRodney W. Grimes 
5139b50d902SRodney W. Grimes     cur = current;
5149b50d902SRodney W. Grimes 
5159b50d902SRodney W. Grimes     for (buf = buffer; *buf != '\0'; ++buf) {
5169b50d902SRodney W. Grimes 	switch (*buf) {
5179b50d902SRodney W. Grimes 
5189b50d902SRodney W. Grimes 	case '\n':
5199b50d902SRodney W. Grimes 	case 014:		/* form feed */
5209b50d902SRodney W. Grimes 	    cur = 1;
5219b50d902SRodney W. Grimes 	    break;
5229b50d902SRodney W. Grimes 
5239b50d902SRodney W. Grimes 	case '\t':
5249b50d902SRodney W. Grimes 	    cur = ((cur - 1) & tabmask) + tabsize + 1;
5259b50d902SRodney W. Grimes 	    break;
5269b50d902SRodney W. Grimes 
5279b50d902SRodney W. Grimes 	case 010:		/* backspace */
5289b50d902SRodney W. Grimes 	    --cur;
5299b50d902SRodney W. Grimes 	    break;
5309b50d902SRodney W. Grimes 
5319b50d902SRodney W. Grimes 	default:
5329b50d902SRodney W. Grimes 	    ++cur;
5339b50d902SRodney W. Grimes 	    break;
5349b50d902SRodney W. Grimes 	}			/* end of switch */
5359b50d902SRodney W. Grimes     }				/* end of for loop */
5369b50d902SRodney W. Grimes     return (cur);
5379b50d902SRodney W. Grimes }
5389b50d902SRodney W. Grimes 
5399b50d902SRodney W. Grimes int	found_err;
5407916863dSJens Schweikhardt 
5417916863dSJens Schweikhardt void
5428c7e7698SDavid Malone diag4(int level, const char *msg, int a, int b)
5439b50d902SRodney W. Grimes {
5449b50d902SRodney W. Grimes     if (level)
5459b50d902SRodney W. Grimes 	found_err = 1;
5469b50d902SRodney W. Grimes     if (output == stdout) {
5479b50d902SRodney W. Grimes 	fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
5489b50d902SRodney W. Grimes 	fprintf(stdout, msg, a, b);
5499b50d902SRodney W. Grimes 	fprintf(stdout, " */\n");
5509b50d902SRodney W. Grimes     }
5519b50d902SRodney W. Grimes     else {
5529b50d902SRodney W. Grimes 	fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
5539b50d902SRodney W. Grimes 	fprintf(stderr, msg, a, b);
5549b50d902SRodney W. Grimes 	fprintf(stderr, "\n");
5559b50d902SRodney W. Grimes     }
5569b50d902SRodney W. Grimes }
5579b50d902SRodney W. Grimes 
5587916863dSJens Schweikhardt void
5598c7e7698SDavid Malone diag3(int level, const char *msg, int a)
5607916863dSJens Schweikhardt {
5617916863dSJens Schweikhardt     if (level)
5627916863dSJens Schweikhardt 	found_err = 1;
5637916863dSJens Schweikhardt     if (output == stdout) {
5647916863dSJens Schweikhardt 	fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
5657916863dSJens Schweikhardt 	fprintf(stdout, msg, a);
5667916863dSJens Schweikhardt 	fprintf(stdout, " */\n");
5677916863dSJens Schweikhardt     }
5687916863dSJens Schweikhardt     else {
5697916863dSJens Schweikhardt 	fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
5707916863dSJens Schweikhardt 	fprintf(stderr, msg, a);
5717916863dSJens Schweikhardt 	fprintf(stderr, "\n");
5727916863dSJens Schweikhardt     }
5737916863dSJens Schweikhardt }
5747916863dSJens Schweikhardt 
5757916863dSJens Schweikhardt void
5768c7e7698SDavid Malone diag2(int level, const char *msg)
5777916863dSJens Schweikhardt {
5787916863dSJens Schweikhardt     if (level)
5797916863dSJens Schweikhardt 	found_err = 1;
5807916863dSJens Schweikhardt     if (output == stdout) {
5817916863dSJens Schweikhardt 	fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
5827916863dSJens Schweikhardt 	fprintf(stdout, msg);
5837916863dSJens Schweikhardt 	fprintf(stdout, " */\n");
5847916863dSJens Schweikhardt     }
5857916863dSJens Schweikhardt     else {
5867916863dSJens Schweikhardt 	fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
5877916863dSJens Schweikhardt 	fprintf(stderr, msg);
5887916863dSJens Schweikhardt 	fprintf(stderr, "\n");
5897916863dSJens Schweikhardt     }
5907916863dSJens Schweikhardt }
5917916863dSJens Schweikhardt 
5927916863dSJens Schweikhardt void
5937916863dSJens Schweikhardt writefdef(struct fstate *f, int nm)
5949b50d902SRodney W. Grimes {
5959b50d902SRodney W. Grimes     fprintf(output, ".ds f%c %s\n.nr s%c %d\n",
5969b50d902SRodney W. Grimes 	    nm, f->font, nm, f->size);
5979b50d902SRodney W. Grimes }
5989b50d902SRodney W. Grimes 
5999b50d902SRodney W. Grimes char *
6007916863dSJens Schweikhardt chfont(struct fstate *of, struct fstate *nf, char *s)
6019b50d902SRodney W. Grimes {
6029b50d902SRodney W. Grimes     if (of->font[0] != nf->font[0]
6039b50d902SRodney W. Grimes 	    || of->font[1] != nf->font[1]) {
6049b50d902SRodney W. Grimes 	*s++ = '\\';
6059b50d902SRodney W. Grimes 	*s++ = 'f';
6069b50d902SRodney W. Grimes 	if (nf->font[1]) {
6079b50d902SRodney W. Grimes 	    *s++ = '(';
6089b50d902SRodney W. Grimes 	    *s++ = nf->font[0];
6099b50d902SRodney W. Grimes 	    *s++ = nf->font[1];
6109b50d902SRodney W. Grimes 	}
6119b50d902SRodney W. Grimes 	else
6129b50d902SRodney W. Grimes 	    *s++ = nf->font[0];
6139b50d902SRodney W. Grimes     }
6149b50d902SRodney W. Grimes     if (nf->size != of->size) {
6159b50d902SRodney W. Grimes 	*s++ = '\\';
6169b50d902SRodney W. Grimes 	*s++ = 's';
6179b50d902SRodney W. Grimes 	if (nf->size < of->size) {
6189b50d902SRodney W. Grimes 	    *s++ = '-';
6199b50d902SRodney W. Grimes 	    *s++ = '0' + of->size - nf->size;
6209b50d902SRodney W. Grimes 	}
6219b50d902SRodney W. Grimes 	else {
6229b50d902SRodney W. Grimes 	    *s++ = '+';
6239b50d902SRodney W. Grimes 	    *s++ = '0' + nf->size - of->size;
6249b50d902SRodney W. Grimes 	}
6259b50d902SRodney W. Grimes     }
6269b50d902SRodney W. Grimes     return s;
6279b50d902SRodney W. Grimes }
6289b50d902SRodney W. Grimes 
6297916863dSJens Schweikhardt void
6308c7e7698SDavid Malone parsefont(struct fstate *f, const char *s0)
6319b50d902SRodney W. Grimes {
6328c7e7698SDavid Malone     const char *s = s0;
6339b50d902SRodney W. Grimes     int         sizedelta = 0;
6347916863dSJens Schweikhardt 
6359b50d902SRodney W. Grimes     bzero(f, sizeof *f);
6369b50d902SRodney W. Grimes     while (*s) {
6379b50d902SRodney W. Grimes 	if (isdigit(*s))
6389b50d902SRodney W. Grimes 	    f->size = f->size * 10 + *s - '0';
6399b50d902SRodney W. Grimes 	else if (isupper(*s))
6409b50d902SRodney W. Grimes 	    if (f->font[0])
6419b50d902SRodney W. Grimes 		f->font[1] = *s;
6429b50d902SRodney W. Grimes 	    else
6439b50d902SRodney W. Grimes 		f->font[0] = *s;
6449b50d902SRodney W. Grimes 	else if (*s == 'c')
6459b50d902SRodney W. Grimes 	    f->allcaps = 1;
6469b50d902SRodney W. Grimes 	else if (*s == '+')
6479b50d902SRodney W. Grimes 	    sizedelta++;
6489b50d902SRodney W. Grimes 	else if (*s == '-')
6499b50d902SRodney W. Grimes 	    sizedelta--;
6509b50d902SRodney W. Grimes 	else {
651958d7c9fSPhilippe Charnier 	    errx(1, "bad font specification: %s", s0);
6529b50d902SRodney W. Grimes 	}
6539b50d902SRodney W. Grimes 	s++;
6549b50d902SRodney W. Grimes     }
6559b50d902SRodney W. Grimes     if (f->font[0] == 0)
6569b50d902SRodney W. Grimes 	f->font[0] = 'R';
6579b50d902SRodney W. Grimes     if (bodyf.size == 0)
6589b50d902SRodney W. Grimes 	bodyf.size = 11;
6599b50d902SRodney W. Grimes     if (f->size == 0)
6609b50d902SRodney W. Grimes 	f->size = bodyf.size + sizedelta;
6619b50d902SRodney W. Grimes     else if (sizedelta > 0)
6629b50d902SRodney W. Grimes 	f->size += bodyf.size;
6639b50d902SRodney W. Grimes     else
6649b50d902SRodney W. Grimes 	f->size = bodyf.size - f->size;
6659b50d902SRodney W. Grimes }
666