1*0a6a1f1dSLionel Sambuc /* $NetBSD: io.c,v 1.15 2014/09/04 04:06:07 mrg Exp $ */
297c7d358SVivek Prakash
397c7d358SVivek Prakash /*
497c7d358SVivek Prakash * Copyright (c) 1980, 1993
597c7d358SVivek Prakash * The Regents of the University of California. All rights reserved.
697c7d358SVivek Prakash *
797c7d358SVivek Prakash * Redistribution and use in source and binary forms, with or without
897c7d358SVivek Prakash * modification, are permitted provided that the following conditions
997c7d358SVivek Prakash * are met:
1097c7d358SVivek Prakash * 1. Redistributions of source code must retain the above copyright
1197c7d358SVivek Prakash * notice, this list of conditions and the following disclaimer.
1297c7d358SVivek Prakash * 2. Redistributions in binary form must reproduce the above copyright
1397c7d358SVivek Prakash * notice, this list of conditions and the following disclaimer in the
1497c7d358SVivek Prakash * documentation and/or other materials provided with the distribution.
1597c7d358SVivek Prakash * 3. Neither the name of the University nor the names of its contributors
1697c7d358SVivek Prakash * may be used to endorse or promote products derived from this software
1797c7d358SVivek Prakash * without specific prior written permission.
1897c7d358SVivek Prakash *
1997c7d358SVivek Prakash * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2097c7d358SVivek Prakash * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2197c7d358SVivek Prakash * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2297c7d358SVivek Prakash * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2397c7d358SVivek Prakash * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2497c7d358SVivek Prakash * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2597c7d358SVivek Prakash * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2697c7d358SVivek Prakash * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2797c7d358SVivek Prakash * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2897c7d358SVivek Prakash * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2997c7d358SVivek Prakash * SUCH DAMAGE.
3097c7d358SVivek Prakash */
3197c7d358SVivek Prakash
3297c7d358SVivek Prakash /*
3397c7d358SVivek Prakash * Copyright (c) 1976 Board of Trustees of the University of Illinois.
3497c7d358SVivek Prakash * Copyright (c) 1985 Sun Microsystems, Inc.
3597c7d358SVivek Prakash * All rights reserved.
3697c7d358SVivek Prakash *
3797c7d358SVivek Prakash * Redistribution and use in source and binary forms, with or without
3897c7d358SVivek Prakash * modification, are permitted provided that the following conditions
3997c7d358SVivek Prakash * are met:
4097c7d358SVivek Prakash * 1. Redistributions of source code must retain the above copyright
4197c7d358SVivek Prakash * notice, this list of conditions and the following disclaimer.
4297c7d358SVivek Prakash * 2. Redistributions in binary form must reproduce the above copyright
4397c7d358SVivek Prakash * notice, this list of conditions and the following disclaimer in the
4497c7d358SVivek Prakash * documentation and/or other materials provided with the distribution.
4597c7d358SVivek Prakash * 3. All advertising materials mentioning features or use of this software
4697c7d358SVivek Prakash * must display the following acknowledgement:
4797c7d358SVivek Prakash * This product includes software developed by the University of
4897c7d358SVivek Prakash * California, Berkeley and its contributors.
4997c7d358SVivek Prakash * 4. Neither the name of the University nor the names of its contributors
5097c7d358SVivek Prakash * may be used to endorse or promote products derived from this software
5197c7d358SVivek Prakash * without specific prior written permission.
5297c7d358SVivek Prakash *
5397c7d358SVivek Prakash * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5497c7d358SVivek Prakash * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5597c7d358SVivek Prakash * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5697c7d358SVivek Prakash * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5797c7d358SVivek Prakash * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5897c7d358SVivek Prakash * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5997c7d358SVivek Prakash * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6097c7d358SVivek Prakash * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6197c7d358SVivek Prakash * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6297c7d358SVivek Prakash * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6397c7d358SVivek Prakash * SUCH DAMAGE.
6497c7d358SVivek Prakash */
6597c7d358SVivek Prakash
6697c7d358SVivek Prakash #include <sys/cdefs.h>
6797c7d358SVivek Prakash #ifndef lint
6897c7d358SVivek Prakash #if 0
6997c7d358SVivek Prakash static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93";
7097c7d358SVivek Prakash #else
71*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: io.c,v 1.15 2014/09/04 04:06:07 mrg Exp $");
7297c7d358SVivek Prakash #endif
7397c7d358SVivek Prakash #endif /* not lint */
7497c7d358SVivek Prakash
7597c7d358SVivek Prakash #include <ctype.h>
7697c7d358SVivek Prakash #include <err.h>
7797c7d358SVivek Prakash #include <stdarg.h>
7897c7d358SVivek Prakash #include <stdio.h>
7997c7d358SVivek Prakash #include <stdlib.h>
8097c7d358SVivek Prakash #include <string.h>
8197c7d358SVivek Prakash #include "indent_globs.h"
8297c7d358SVivek Prakash
8397c7d358SVivek Prakash int comment_open;
8497c7d358SVivek Prakash static int paren_target;
8597c7d358SVivek Prakash
8697c7d358SVivek Prakash void
dump_line(void)8797c7d358SVivek Prakash dump_line(void)
8897c7d358SVivek Prakash { /* dump_line is the routine that actually
8997c7d358SVivek Prakash * effects the printing of the new source. It
9097c7d358SVivek Prakash * prints the label section, followed by the
9197c7d358SVivek Prakash * code section with the appropriate nesting
9297c7d358SVivek Prakash * level, followed by any comments */
9397c7d358SVivek Prakash int cur_col, target_col;
9497c7d358SVivek Prakash static int not_first_line;
9597c7d358SVivek Prakash
9697c7d358SVivek Prakash target_col = 0;
9797c7d358SVivek Prakash if (ps.procname[0]) {
9897c7d358SVivek Prakash if (troff) {
9997c7d358SVivek Prakash if (comment_open) {
10097c7d358SVivek Prakash comment_open = 0;
10197c7d358SVivek Prakash fprintf(output, ".*/\n");
10297c7d358SVivek Prakash }
10397c7d358SVivek Prakash fprintf(output, ".Pr \"%s\"\n", ps.procname);
10497c7d358SVivek Prakash }
10597c7d358SVivek Prakash ps.ind_level = 0;
10697c7d358SVivek Prakash ps.procname[0] = 0;
10797c7d358SVivek Prakash }
10897c7d358SVivek Prakash if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
10997c7d358SVivek Prakash if (suppress_blanklines > 0)
11097c7d358SVivek Prakash suppress_blanklines--;
11197c7d358SVivek Prakash else {
11297c7d358SVivek Prakash ps.bl_line = true;
11397c7d358SVivek Prakash n_real_blanklines++;
11497c7d358SVivek Prakash }
11597c7d358SVivek Prakash } else
11697c7d358SVivek Prakash if (!inhibit_formatting) {
11797c7d358SVivek Prakash suppress_blanklines = 0;
11897c7d358SVivek Prakash ps.bl_line = false;
11997c7d358SVivek Prakash if (prefix_blankline_requested && not_first_line) {
12097c7d358SVivek Prakash if (swallow_optional_blanklines) {
12197c7d358SVivek Prakash if (n_real_blanklines == 1)
12297c7d358SVivek Prakash n_real_blanklines = 0;
12397c7d358SVivek Prakash } else {
12497c7d358SVivek Prakash if (n_real_blanklines == 0)
12597c7d358SVivek Prakash n_real_blanklines = 1;
12697c7d358SVivek Prakash }
12797c7d358SVivek Prakash }
12897c7d358SVivek Prakash while (--n_real_blanklines >= 0)
12997c7d358SVivek Prakash putc('\n', output);
13097c7d358SVivek Prakash n_real_blanklines = 0;
13197c7d358SVivek Prakash if (ps.ind_level == 0)
13297c7d358SVivek Prakash ps.ind_stmt = 0; /* this is a class A
13397c7d358SVivek Prakash * kludge. dont do
13497c7d358SVivek Prakash * additional statement
13597c7d358SVivek Prakash * indentation if we are
13697c7d358SVivek Prakash * at bracket level 0 */
13797c7d358SVivek Prakash
13897c7d358SVivek Prakash if (e_lab != s_lab || e_code != s_code)
13997c7d358SVivek Prakash ++code_lines; /* keep count of lines with
14097c7d358SVivek Prakash * code */
14197c7d358SVivek Prakash
14297c7d358SVivek Prakash
14397c7d358SVivek Prakash if (e_lab != s_lab) { /* print lab, if any */
14497c7d358SVivek Prakash if (comment_open) {
14597c7d358SVivek Prakash comment_open = 0;
14697c7d358SVivek Prakash fprintf(output, ".*/\n");
14797c7d358SVivek Prakash }
14897c7d358SVivek Prakash while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
14997c7d358SVivek Prakash e_lab--;
15097c7d358SVivek Prakash cur_col = pad_output(1, compute_label_target());
15197c7d358SVivek Prakash if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0
15297c7d358SVivek Prakash || strncmp(s_lab, "#endif", 6) == 0)) {
15397c7d358SVivek Prakash char *s = s_lab;
15497c7d358SVivek Prakash if (e_lab[-1] == '\n')
15597c7d358SVivek Prakash e_lab--;
15697c7d358SVivek Prakash do
15797c7d358SVivek Prakash putc(*s++, output);
15897c7d358SVivek Prakash while (s < e_lab && 'a' <= *s && *s <= 'z');
15997c7d358SVivek Prakash while ((*s == ' ' || *s == '\t') && s < e_lab)
16097c7d358SVivek Prakash s++;
16197c7d358SVivek Prakash if (s < e_lab)
16297c7d358SVivek Prakash fprintf(output, s[0] == '/' && s[1] == '*' ? "\t%.*s" : "\t/* %.*s */",
16397c7d358SVivek Prakash (int)(e_lab - s), s);
16497c7d358SVivek Prakash } else
16597c7d358SVivek Prakash fprintf(output, "%.*s", (int)(e_lab - s_lab), s_lab);
16697c7d358SVivek Prakash cur_col = count_spaces(cur_col, s_lab);
16797c7d358SVivek Prakash } else
16897c7d358SVivek Prakash cur_col = 1; /* there is no label section */
16997c7d358SVivek Prakash
17097c7d358SVivek Prakash ps.pcase = false;
17197c7d358SVivek Prakash
17297c7d358SVivek Prakash if (s_code != e_code) { /* print code section, if any */
17397c7d358SVivek Prakash char *p;
17497c7d358SVivek Prakash
17597c7d358SVivek Prakash if (comment_open) {
17697c7d358SVivek Prakash comment_open = 0;
17797c7d358SVivek Prakash fprintf(output, ".*/\n");
17897c7d358SVivek Prakash }
17997c7d358SVivek Prakash target_col = compute_code_target();
18097c7d358SVivek Prakash {
18197c7d358SVivek Prakash int i;
18297c7d358SVivek Prakash
18397c7d358SVivek Prakash for (i = 0; i < ps.p_l_follow; i++)
18497c7d358SVivek Prakash if (ps.paren_indents[i] >= 0)
18597c7d358SVivek Prakash ps.paren_indents[i] = -(ps.paren_indents[i] + target_col);
18697c7d358SVivek Prakash }
18797c7d358SVivek Prakash cur_col = pad_output(cur_col, target_col);
18897c7d358SVivek Prakash for (p = s_code; p < e_code; p++)
18997c7d358SVivek Prakash if (*p == (char) 0200)
19097c7d358SVivek Prakash fprintf(output, "%d", target_col * 7);
19197c7d358SVivek Prakash else
19297c7d358SVivek Prakash putc(*p, output);
19397c7d358SVivek Prakash cur_col = count_spaces(cur_col, s_code);
19497c7d358SVivek Prakash }
19597c7d358SVivek Prakash if (s_com != e_com) {
19697c7d358SVivek Prakash if (troff) {
19797c7d358SVivek Prakash int all_here = 0;
19897c7d358SVivek Prakash char *p;
19997c7d358SVivek Prakash
20097c7d358SVivek Prakash if (e_com[-1] == '/' && e_com[-2] == '*')
20197c7d358SVivek Prakash e_com -= 2, all_here++;
20297c7d358SVivek Prakash while (e_com > s_com && e_com[-1] == ' ')
20397c7d358SVivek Prakash e_com--;
20497c7d358SVivek Prakash *e_com = 0;
20597c7d358SVivek Prakash p = s_com;
20697c7d358SVivek Prakash while (*p == ' ')
20797c7d358SVivek Prakash p++;
20897c7d358SVivek Prakash if (p[0] == '/' && p[1] == '*')
20997c7d358SVivek Prakash p += 2, all_here++;
21097c7d358SVivek Prakash else
21197c7d358SVivek Prakash if (p[0] == '*')
21297c7d358SVivek Prakash p += p[1] == '/' ? 2 : 1;
21397c7d358SVivek Prakash while (*p == ' ')
21497c7d358SVivek Prakash p++;
21597c7d358SVivek Prakash if (*p == 0)
21697c7d358SVivek Prakash goto inhibit_newline;
21797c7d358SVivek Prakash if (comment_open < 2 && ps.box_com) {
21897c7d358SVivek Prakash comment_open = 0;
21997c7d358SVivek Prakash fprintf(output, ".*/\n");
22097c7d358SVivek Prakash }
22197c7d358SVivek Prakash if (comment_open == 0) {
22297c7d358SVivek Prakash if ('a' <= *p && *p <= 'z')
22397c7d358SVivek Prakash *p = *p + 'A' - 'a';
22497c7d358SVivek Prakash if (e_com - p < 50 && all_here == 2) {
22597c7d358SVivek Prakash char *follow = p;
22697c7d358SVivek Prakash fprintf(output, "\n.nr C! \\w\1");
22797c7d358SVivek Prakash while (follow < e_com) {
22897c7d358SVivek Prakash switch (*follow) {
22997c7d358SVivek Prakash case '\n':
23097c7d358SVivek Prakash putc(' ', output);
23197c7d358SVivek Prakash case 1:
23297c7d358SVivek Prakash break;
23397c7d358SVivek Prakash case '\\':
23497c7d358SVivek Prakash putc('\\', output);
23597c7d358SVivek Prakash default:
23697c7d358SVivek Prakash putc(*follow, output);
23797c7d358SVivek Prakash }
23897c7d358SVivek Prakash follow++;
23997c7d358SVivek Prakash }
24097c7d358SVivek Prakash putc(1, output);
24197c7d358SVivek Prakash }
24297c7d358SVivek Prakash fprintf(output, "\n./* %dp %d %dp\n",
24397c7d358SVivek Prakash ps.com_col * 7,
24497c7d358SVivek Prakash (s_code != e_code || s_lab != e_lab) - ps.box_com,
24597c7d358SVivek Prakash target_col * 7);
24697c7d358SVivek Prakash }
24797c7d358SVivek Prakash comment_open = 1 + ps.box_com;
24897c7d358SVivek Prakash while (*p) {
24997c7d358SVivek Prakash if (*p == BACKSLASH)
25097c7d358SVivek Prakash putc(BACKSLASH, output);
25197c7d358SVivek Prakash putc(*p++, output);
25297c7d358SVivek Prakash }
25397c7d358SVivek Prakash } else { /* print comment, if any */
25497c7d358SVivek Prakash int target = ps.com_col;
25597c7d358SVivek Prakash char *com_st = s_com;
25697c7d358SVivek Prakash
25797c7d358SVivek Prakash target += ps.comment_delta;
25897c7d358SVivek Prakash while (*com_st == '\t')
25997c7d358SVivek Prakash com_st++, target += 8; /* ? */
26097c7d358SVivek Prakash while (target <= 0)
26197c7d358SVivek Prakash if (*com_st == ' ')
26297c7d358SVivek Prakash target++, com_st++;
26397c7d358SVivek Prakash else
26497c7d358SVivek Prakash if (*com_st == '\t')
26597c7d358SVivek Prakash target = ((target - 1) & ~7) + 9, com_st++;
26697c7d358SVivek Prakash else
26797c7d358SVivek Prakash target = 1;
26897c7d358SVivek Prakash if (cur_col > target) { /* if comment cant fit
26997c7d358SVivek Prakash * on this line, put it
27097c7d358SVivek Prakash * on next line */
27197c7d358SVivek Prakash putc('\n', output);
27297c7d358SVivek Prakash cur_col = 1;
27397c7d358SVivek Prakash ++ps.out_lines;
27497c7d358SVivek Prakash }
27597c7d358SVivek Prakash while (e_com > com_st
27697c7d358SVivek Prakash && isspace((unsigned char)e_com[-1]))
27797c7d358SVivek Prakash e_com--;
27897c7d358SVivek Prakash cur_col = pad_output(cur_col, target);
27997c7d358SVivek Prakash if (!ps.box_com) {
28097c7d358SVivek Prakash if (star_comment_cont
28197c7d358SVivek Prakash && (com_st[1] != '*'
28297c7d358SVivek Prakash || e_com <= com_st + 1)) {
28397c7d358SVivek Prakash if (com_st[1] == ' '
28497c7d358SVivek Prakash && com_st[0] == ' '
28597c7d358SVivek Prakash && e_com > com_st + 1)
28697c7d358SVivek Prakash com_st[1] = '*';
28797c7d358SVivek Prakash else
28897c7d358SVivek Prakash fwrite(" * ",
28997c7d358SVivek Prakash com_st[0] == '\t'
29097c7d358SVivek Prakash ? 2
29197c7d358SVivek Prakash : com_st[0]=='*'
29297c7d358SVivek Prakash ? 1
29397c7d358SVivek Prakash : 3, 1, output);
29497c7d358SVivek Prakash }
29597c7d358SVivek Prakash }
29697c7d358SVivek Prakash fwrite(com_st,
29797c7d358SVivek Prakash e_com - com_st, 1, output);
29897c7d358SVivek Prakash ps.comment_delta = ps.n_comment_delta;
29997c7d358SVivek Prakash cur_col = count_spaces(cur_col, com_st);
30097c7d358SVivek Prakash ++ps.com_lines; /* count lines with
30197c7d358SVivek Prakash * comments */
30297c7d358SVivek Prakash }
30397c7d358SVivek Prakash }
30497c7d358SVivek Prakash if (ps.use_ff)
30597c7d358SVivek Prakash putc('\014', output);
30697c7d358SVivek Prakash else
30797c7d358SVivek Prakash putc('\n', output);
30897c7d358SVivek Prakash inhibit_newline:
30997c7d358SVivek Prakash ++ps.out_lines;
31097c7d358SVivek Prakash if (ps.just_saw_decl == 1 && blanklines_after_declarations) {
31197c7d358SVivek Prakash prefix_blankline_requested = 1;
31297c7d358SVivek Prakash ps.just_saw_decl = 0;
31397c7d358SVivek Prakash } else
31497c7d358SVivek Prakash prefix_blankline_requested = postfix_blankline_requested;
31597c7d358SVivek Prakash postfix_blankline_requested = 0;
31697c7d358SVivek Prakash }
31797c7d358SVivek Prakash ps.decl_on_line = ps.in_decl; /* if we are in the middle of a
31897c7d358SVivek Prakash * declaration, remember that fact for
31997c7d358SVivek Prakash * proper comment indentation */
32097c7d358SVivek Prakash ps.ind_stmt = ps.in_stmt & ~ps.in_decl; /* next line should be
32197c7d358SVivek Prakash * indented if we have not
32297c7d358SVivek Prakash * completed this stmt and if
32397c7d358SVivek Prakash * we are not in the middle of
32497c7d358SVivek Prakash * a declaration */
32597c7d358SVivek Prakash ps.use_ff = false;
32697c7d358SVivek Prakash ps.dumped_decl_indent = 0;
32797c7d358SVivek Prakash *(e_lab = s_lab) = '\0';/* reset buffers */
32897c7d358SVivek Prakash *(e_code = s_code) = '\0';
32997c7d358SVivek Prakash *(e_com = s_com) = '\0';
33097c7d358SVivek Prakash ps.ind_level = ps.i_l_follow;
33197c7d358SVivek Prakash ps.paren_level = ps.p_l_follow;
33297c7d358SVivek Prakash paren_target = -ps.paren_indents[ps.paren_level - 1];
33397c7d358SVivek Prakash not_first_line = 1;
33497c7d358SVivek Prakash }
33597c7d358SVivek Prakash
33697c7d358SVivek Prakash int
compute_code_target(void)33797c7d358SVivek Prakash compute_code_target(void)
33897c7d358SVivek Prakash {
33997c7d358SVivek Prakash int target_col = ps.ind_size * ps.ind_level + 1;
34097c7d358SVivek Prakash
34197c7d358SVivek Prakash if (ps.paren_level) {
34297c7d358SVivek Prakash if (!lineup_to_parens)
34397c7d358SVivek Prakash target_col += continuation_indent * ps.paren_level;
34497c7d358SVivek Prakash else {
34597c7d358SVivek Prakash int w;
34697c7d358SVivek Prakash int t = paren_target;
34797c7d358SVivek Prakash
34897c7d358SVivek Prakash if ((w = count_spaces(t, s_code) - max_col) > 0
34997c7d358SVivek Prakash && count_spaces(target_col, s_code) <= max_col) {
35097c7d358SVivek Prakash t -= w + 1;
35197c7d358SVivek Prakash if (t > target_col)
35297c7d358SVivek Prakash target_col = t;
35397c7d358SVivek Prakash } else
35497c7d358SVivek Prakash target_col = t;
35597c7d358SVivek Prakash }
35697c7d358SVivek Prakash } else
35797c7d358SVivek Prakash if (ps.ind_stmt)
35897c7d358SVivek Prakash target_col += continuation_indent;
35997c7d358SVivek Prakash return target_col;
36097c7d358SVivek Prakash }
36197c7d358SVivek Prakash
36297c7d358SVivek Prakash int
compute_label_target(void)36397c7d358SVivek Prakash compute_label_target(void)
36497c7d358SVivek Prakash {
36597c7d358SVivek Prakash return
36697c7d358SVivek Prakash ps.pcase ? (int) (case_ind * ps.ind_size) + 1
36797c7d358SVivek Prakash : *s_lab == '#' ? 1
36897c7d358SVivek Prakash : ps.ind_size * (ps.ind_level - label_offset) + 1;
36997c7d358SVivek Prakash }
37097c7d358SVivek Prakash
37197c7d358SVivek Prakash
37297c7d358SVivek Prakash /*
37397c7d358SVivek Prakash * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
37497c7d358SVivek Prakash *
37597c7d358SVivek Prakash * All rights reserved
37697c7d358SVivek Prakash *
37797c7d358SVivek Prakash *
37897c7d358SVivek Prakash * NAME: fill_buffer
37997c7d358SVivek Prakash *
38097c7d358SVivek Prakash * FUNCTION: Reads one block of input into input_buffer
38197c7d358SVivek Prakash *
38297c7d358SVivek Prakash * HISTORY: initial coding November 1976 D A Willcox of CAC 1/7/77 A
38397c7d358SVivek Prakash * Willcox of CAC Added check for switch back to partly full input
38497c7d358SVivek Prakash * buffer from temporary buffer
38597c7d358SVivek Prakash *
38697c7d358SVivek Prakash */
38797c7d358SVivek Prakash void
fill_buffer(void)38897c7d358SVivek Prakash fill_buffer(void)
38997c7d358SVivek Prakash { /* this routine reads stuff from the input */
39097c7d358SVivek Prakash char *p;
39197c7d358SVivek Prakash int i;
39297c7d358SVivek Prakash FILE *f = input;
39397c7d358SVivek Prakash char *n;
39497c7d358SVivek Prakash
39597c7d358SVivek Prakash if (bp_save != 0) { /* there is a partly filled input buffer left */
39697c7d358SVivek Prakash buf_ptr = bp_save; /* dont read anything, just switch
39797c7d358SVivek Prakash * buffers */
39897c7d358SVivek Prakash buf_end = be_save;
39997c7d358SVivek Prakash bp_save = be_save = 0;
40097c7d358SVivek Prakash if (buf_ptr < buf_end)
40197c7d358SVivek Prakash return; /* only return if there is really something in
40297c7d358SVivek Prakash * this buffer */
40397c7d358SVivek Prakash }
40497c7d358SVivek Prakash for (p = in_buffer;;) {
40597c7d358SVivek Prakash if (p >= in_buffer_limit) {
40697c7d358SVivek Prakash int size = (in_buffer_limit - in_buffer) * 2 + 10;
40797c7d358SVivek Prakash int offset = p - in_buffer;
40897c7d358SVivek Prakash n = (char *) realloc(in_buffer, size);
40997c7d358SVivek Prakash if (n == 0)
41097c7d358SVivek Prakash errx(1, "input line too long");
41197c7d358SVivek Prakash in_buffer = n;
41297c7d358SVivek Prakash p = in_buffer + offset;
41397c7d358SVivek Prakash in_buffer_limit = in_buffer + size - 2;
41497c7d358SVivek Prakash }
41597c7d358SVivek Prakash if ((i = getc(f)) == EOF) {
41697c7d358SVivek Prakash *p++ = ' ';
41797c7d358SVivek Prakash *p++ = '\n';
41897c7d358SVivek Prakash had_eof = true;
41997c7d358SVivek Prakash break;
42097c7d358SVivek Prakash }
42197c7d358SVivek Prakash *p++ = i;
42297c7d358SVivek Prakash if (i == '\n')
42397c7d358SVivek Prakash break;
42497c7d358SVivek Prakash }
42597c7d358SVivek Prakash buf_ptr = in_buffer;
42697c7d358SVivek Prakash buf_end = p;
42797c7d358SVivek Prakash if (p[-2] == '/' && p[-3] == '*') {
42897c7d358SVivek Prakash if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
42997c7d358SVivek Prakash fill_buffer(); /* flush indent error message */
43097c7d358SVivek Prakash else {
43197c7d358SVivek Prakash int com = 0;
43297c7d358SVivek Prakash
43397c7d358SVivek Prakash p = in_buffer;
43497c7d358SVivek Prakash while (*p == ' ' || *p == '\t')
43597c7d358SVivek Prakash p++;
43697c7d358SVivek Prakash if (*p == '/' && p[1] == '*') {
43797c7d358SVivek Prakash p += 2;
43897c7d358SVivek Prakash while (*p == ' ' || *p == '\t')
43997c7d358SVivek Prakash p++;
44097c7d358SVivek Prakash if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E'
44197c7d358SVivek Prakash && p[4] == 'N' && p[5] == 'T') {
44297c7d358SVivek Prakash p += 6;
44397c7d358SVivek Prakash while (*p == ' ' || *p == '\t')
44497c7d358SVivek Prakash p++;
44597c7d358SVivek Prakash if (*p == '*')
44697c7d358SVivek Prakash com = 1;
44797c7d358SVivek Prakash else {
44897c7d358SVivek Prakash if (*p == 'O') {
44997c7d358SVivek Prakash if (*++p == 'N')
45097c7d358SVivek Prakash p++, com = 1;
45197c7d358SVivek Prakash else
45297c7d358SVivek Prakash if (*p == 'F' && *++p == 'F')
45397c7d358SVivek Prakash p++, com = 2;
45497c7d358SVivek Prakash }
45597c7d358SVivek Prakash }
45697c7d358SVivek Prakash while (*p == ' ' || *p == '\t')
45797c7d358SVivek Prakash p++;
45897c7d358SVivek Prakash if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) {
45997c7d358SVivek Prakash if (s_com != e_com || s_lab != e_lab || s_code != e_code)
46097c7d358SVivek Prakash dump_line();
46197c7d358SVivek Prakash if (!(inhibit_formatting = com - 1)) {
46297c7d358SVivek Prakash n_real_blanklines = 0;
46397c7d358SVivek Prakash postfix_blankline_requested = 0;
46497c7d358SVivek Prakash prefix_blankline_requested = 0;
46597c7d358SVivek Prakash suppress_blanklines = 1;
46697c7d358SVivek Prakash }
46797c7d358SVivek Prakash }
46897c7d358SVivek Prakash }
46997c7d358SVivek Prakash }
47097c7d358SVivek Prakash }
47197c7d358SVivek Prakash }
47297c7d358SVivek Prakash if (inhibit_formatting) {
47397c7d358SVivek Prakash p = in_buffer;
47497c7d358SVivek Prakash do
47597c7d358SVivek Prakash putc(*p, output);
47697c7d358SVivek Prakash while (*p++ != '\n');
47797c7d358SVivek Prakash }
47897c7d358SVivek Prakash }
47997c7d358SVivek Prakash /*
48097c7d358SVivek Prakash * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
48197c7d358SVivek Prakash *
48297c7d358SVivek Prakash * All rights reserved
48397c7d358SVivek Prakash *
48497c7d358SVivek Prakash *
48597c7d358SVivek Prakash * NAME: pad_output
48697c7d358SVivek Prakash *
48797c7d358SVivek Prakash * FUNCTION: Writes tabs and spaces to move the current column up to the desired
48897c7d358SVivek Prakash * position.
48997c7d358SVivek Prakash *
49097c7d358SVivek Prakash * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf.
49197c7d358SVivek Prakash *
49297c7d358SVivek Prakash * PARAMETERS: current integer The current column target
49397c7d358SVivek Prakash * target integer The desired column
49497c7d358SVivek Prakash *
49597c7d358SVivek Prakash * RETURNS: Integer value of the new column. (If current >= target, no action is
49697c7d358SVivek Prakash * taken, and current is returned.
49797c7d358SVivek Prakash *
49897c7d358SVivek Prakash * GLOBALS: None
49997c7d358SVivek Prakash *
50097c7d358SVivek Prakash * CALLS: write (sys)
50197c7d358SVivek Prakash *
50297c7d358SVivek Prakash * CALLED BY: dump_line
50397c7d358SVivek Prakash *
50497c7d358SVivek Prakash * HISTORY: initial coding November 1976 D A Willcox of CAC
50597c7d358SVivek Prakash *
50697c7d358SVivek Prakash */
50797c7d358SVivek Prakash int
pad_output(int current,int target)50897c7d358SVivek Prakash pad_output(int current, int target)
50997c7d358SVivek Prakash {
51097c7d358SVivek Prakash int curr; /* internal column pointer */
51197c7d358SVivek Prakash int tcur;
51297c7d358SVivek Prakash
51397c7d358SVivek Prakash if (troff)
51497c7d358SVivek Prakash fprintf(output, "\\h'|%dp'", (target - 1) * 7);
51597c7d358SVivek Prakash else {
51697c7d358SVivek Prakash if (current >= target)
51797c7d358SVivek Prakash return (current); /* line is already long enough */
51897c7d358SVivek Prakash curr = current;
519*0a6a1f1dSLionel Sambuc if (use_tabs) {
52097c7d358SVivek Prakash while ((tcur = ((curr - 1) & tabmask) + tabsize + 1) <= target) {
52197c7d358SVivek Prakash putc('\t', output);
52297c7d358SVivek Prakash curr = tcur;
52397c7d358SVivek Prakash }
524*0a6a1f1dSLionel Sambuc }
52597c7d358SVivek Prakash while (curr++ < target)
52697c7d358SVivek Prakash putc(' ', output); /* pad with final blanks */
52797c7d358SVivek Prakash }
52897c7d358SVivek Prakash return (target);
52997c7d358SVivek Prakash }
53097c7d358SVivek Prakash /*
53197c7d358SVivek Prakash * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
53297c7d358SVivek Prakash *
53397c7d358SVivek Prakash * All rights reserved
53497c7d358SVivek Prakash *
53597c7d358SVivek Prakash *
53697c7d358SVivek Prakash * NAME: count_spaces
53797c7d358SVivek Prakash *
53897c7d358SVivek Prakash * FUNCTION: Find out where printing of a given string will leave the current
53997c7d358SVivek Prakash * character position on output.
54097c7d358SVivek Prakash *
54197c7d358SVivek Prakash * ALGORITHM: Run thru input string and add appropriate values to current
54297c7d358SVivek Prakash * position.
54397c7d358SVivek Prakash *
54497c7d358SVivek Prakash * RETURNS: Integer value of position after printing "buffer" starting in column
54597c7d358SVivek Prakash * "current".
54697c7d358SVivek Prakash *
54797c7d358SVivek Prakash * HISTORY: initial coding November 1976 D A Willcox of CAC
54897c7d358SVivek Prakash *
54997c7d358SVivek Prakash */
55097c7d358SVivek Prakash int
count_spaces(int current,char * buffer)55197c7d358SVivek Prakash count_spaces(int current, char *buffer)
55297c7d358SVivek Prakash /*
55397c7d358SVivek Prakash * this routine figures out where the character position will be after
55497c7d358SVivek Prakash * printing the text in buffer starting at column "current"
55597c7d358SVivek Prakash */
55697c7d358SVivek Prakash {
55797c7d358SVivek Prakash char *buf; /* used to look thru buffer */
55897c7d358SVivek Prakash int cur; /* current character counter */
55997c7d358SVivek Prakash
56097c7d358SVivek Prakash cur = current;
56197c7d358SVivek Prakash
56297c7d358SVivek Prakash for (buf = buffer; *buf != '\0'; ++buf) {
56397c7d358SVivek Prakash switch (*buf) {
56497c7d358SVivek Prakash
56597c7d358SVivek Prakash case '\n':
56697c7d358SVivek Prakash case 014: /* form feed */
56797c7d358SVivek Prakash cur = 1;
56897c7d358SVivek Prakash break;
56997c7d358SVivek Prakash
57097c7d358SVivek Prakash case '\t':
57197c7d358SVivek Prakash cur = ((cur - 1) & tabmask) + tabsize + 1;
57297c7d358SVivek Prakash break;
57397c7d358SVivek Prakash
57497c7d358SVivek Prakash case 010: /* backspace */
57597c7d358SVivek Prakash --cur;
57697c7d358SVivek Prakash break;
57797c7d358SVivek Prakash
57897c7d358SVivek Prakash default:
57997c7d358SVivek Prakash ++cur;
58097c7d358SVivek Prakash break;
58197c7d358SVivek Prakash } /* end of switch */
58297c7d358SVivek Prakash } /* end of for loop */
58397c7d358SVivek Prakash return (cur);
58497c7d358SVivek Prakash }
58597c7d358SVivek Prakash
58697c7d358SVivek Prakash
58797c7d358SVivek Prakash int found_err;
58897c7d358SVivek Prakash
58997c7d358SVivek Prakash void
diag(int level,const char * msg,...)59097c7d358SVivek Prakash diag(int level, const char *msg, ...)
59197c7d358SVivek Prakash {
59297c7d358SVivek Prakash va_list ap;
59397c7d358SVivek Prakash
59497c7d358SVivek Prakash va_start(ap, msg);
59597c7d358SVivek Prakash
59697c7d358SVivek Prakash if (level)
59797c7d358SVivek Prakash found_err = 1;
59897c7d358SVivek Prakash if (output == stdout) {
59997c7d358SVivek Prakash fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
60097c7d358SVivek Prakash vfprintf(stdout, msg, ap);
60197c7d358SVivek Prakash fprintf(stdout, " */\n");
60297c7d358SVivek Prakash } else {
60397c7d358SVivek Prakash fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
60497c7d358SVivek Prakash vfprintf(stdout, msg, ap);
60597c7d358SVivek Prakash fprintf(stderr, "\n");
60697c7d358SVivek Prakash }
60797c7d358SVivek Prakash va_end(ap);
60897c7d358SVivek Prakash }
60997c7d358SVivek Prakash
61097c7d358SVivek Prakash void
writefdef(struct fstate * f,int nm)61197c7d358SVivek Prakash writefdef(struct fstate *f, int nm)
61297c7d358SVivek Prakash {
61397c7d358SVivek Prakash fprintf(output, ".ds f%c %s\n.nr s%c %d\n",
61497c7d358SVivek Prakash nm, f->font, nm, f->size);
61597c7d358SVivek Prakash }
61697c7d358SVivek Prakash
61797c7d358SVivek Prakash char *
chfont(struct fstate * of,struct fstate * nf,char * s)61897c7d358SVivek Prakash chfont(struct fstate *of, struct fstate *nf, char *s)
61997c7d358SVivek Prakash {
62097c7d358SVivek Prakash if (of->font[0] != nf->font[0]
62197c7d358SVivek Prakash || of->font[1] != nf->font[1]) {
62297c7d358SVivek Prakash *s++ = '\\';
62397c7d358SVivek Prakash *s++ = 'f';
62497c7d358SVivek Prakash if (nf->font[1]) {
62597c7d358SVivek Prakash *s++ = '(';
62697c7d358SVivek Prakash *s++ = nf->font[0];
62797c7d358SVivek Prakash *s++ = nf->font[1];
62897c7d358SVivek Prakash } else
62997c7d358SVivek Prakash *s++ = nf->font[0];
63097c7d358SVivek Prakash }
63197c7d358SVivek Prakash if (nf->size != of->size) {
63297c7d358SVivek Prakash *s++ = '\\';
63397c7d358SVivek Prakash *s++ = 's';
63497c7d358SVivek Prakash if (nf->size < of->size) {
63597c7d358SVivek Prakash *s++ = '-';
63697c7d358SVivek Prakash *s++ = '0' + of->size - nf->size;
63797c7d358SVivek Prakash } else {
63897c7d358SVivek Prakash *s++ = '+';
63997c7d358SVivek Prakash *s++ = '0' + nf->size - of->size;
64097c7d358SVivek Prakash }
64197c7d358SVivek Prakash }
64297c7d358SVivek Prakash return s;
64397c7d358SVivek Prakash }
64497c7d358SVivek Prakash
64597c7d358SVivek Prakash
64697c7d358SVivek Prakash void
parsefont(struct fstate * f,const char * s0)64797c7d358SVivek Prakash parsefont(struct fstate *f, const char *s0)
64897c7d358SVivek Prakash {
64997c7d358SVivek Prakash const char *s = s0;
65097c7d358SVivek Prakash int sizedelta = 0;
65197c7d358SVivek Prakash memset(f, 0, sizeof *f);
65297c7d358SVivek Prakash while (*s) {
65397c7d358SVivek Prakash if (isdigit((unsigned char)*s))
65497c7d358SVivek Prakash f->size = f->size * 10 + *s - '0';
65597c7d358SVivek Prakash else
65697c7d358SVivek Prakash if (isupper((unsigned char)*s)) {
65797c7d358SVivek Prakash if (f->font[0])
65897c7d358SVivek Prakash f->font[1] = *s;
65997c7d358SVivek Prakash else
66097c7d358SVivek Prakash f->font[0] = *s;
66197c7d358SVivek Prakash } else
66297c7d358SVivek Prakash if (*s == 'c')
66397c7d358SVivek Prakash f->allcaps = 1;
66497c7d358SVivek Prakash else
66597c7d358SVivek Prakash if (*s == '+')
66697c7d358SVivek Prakash sizedelta++;
66797c7d358SVivek Prakash else
66897c7d358SVivek Prakash if (*s == '-')
66997c7d358SVivek Prakash sizedelta--;
67097c7d358SVivek Prakash else {
67197c7d358SVivek Prakash fprintf(stderr, "indent: bad font specification: %s\n", s0);
67297c7d358SVivek Prakash exit(1);
67397c7d358SVivek Prakash }
67497c7d358SVivek Prakash s++;
67597c7d358SVivek Prakash }
67697c7d358SVivek Prakash if (f->font[0] == 0)
67797c7d358SVivek Prakash f->font[0] = 'R';
67897c7d358SVivek Prakash if (bodyf.size == 0)
67997c7d358SVivek Prakash bodyf.size = 11;
68097c7d358SVivek Prakash if (f->size == 0)
68197c7d358SVivek Prakash f->size = bodyf.size + sizedelta;
68297c7d358SVivek Prakash else
68397c7d358SVivek Prakash if (sizedelta > 0)
68497c7d358SVivek Prakash f->size += bodyf.size;
68597c7d358SVivek Prakash else
68697c7d358SVivek Prakash f->size = bodyf.size - f->size;
68797c7d358SVivek Prakash }
688