121969Sdist /* 221969Sdist * Copyright (c) 1980 Regents of the University of California. 321969Sdist * All rights reserved. The Berkeley software License Agreement 421969Sdist * specifies the terms and conditions for redistribution. 521969Sdist */ 68803Smckusick 721969Sdist #ifndef lint 8*33229Sbostic static char sccsid[] = "@(#)io.c 5.5 (Berkeley) 01/02/88"; 921969Sdist #endif not lint 1021969Sdist 1124454Smckusick /*- 1224454Smckusick * Copyright (C) 1976 1324454Smckusick * by the 1424454Smckusick * Board of Trustees 1524454Smckusick * of the 1624454Smckusick * University of Illinois 1724454Smckusick * All rights reserved 1824454Smckusick * FILE NAME: 1924454Smckusick * io.c 2024454Smckusick * PURPOSE: 2124454Smckusick * Contains routines to handle i/o related stuff for indent. 2224454Smckusick * GLOBALS: 2324454Smckusick * None 2424454Smckusick * FUNCTIONS: 2524454Smckusick * dump_line 2624454Smckusick * fill_buffer 2724454Smckusick * pad_output 2824454Smckusick * count_spaces 2924454Smckusick * eqin 3024454Smckusick * cmp 3124454Smckusick * 3224454Smckusick */ 3324454Smckusick /*- 3424454Smckusick * 3524454Smckusick * Copyright (C) 1976 3624454Smckusick * by the 3724454Smckusick * Board of Trustees 3824454Smckusick * of the 3924454Smckusick * University of Illinois 4024454Smckusick * 4124454Smckusick * All rights reserved 4224454Smckusick * 4324454Smckusick * 4424454Smckusick * NAME: 4524454Smckusick * dump_line 4624454Smckusick * 4724454Smckusick * FUNCTION: 4824454Smckusick * Does the actual printing of the stored up line 4924454Smckusick * 5024454Smckusick * ALGORITHM: 5124454Smckusick * For each of the label, code, and comment sections which are used on 5224454Smckusick * this line: 5324454Smckusick * 5424454Smckusick * 1) Use pad_output to get the section aligned properly. 5524454Smckusick * 2) write the section 5624454Smckusick * 5724454Smckusick * The indentation level used for the code is set by ps.ind_level. After 5824454Smckusick * printing, ps.ind_level is set to ps.i_l_follow. 5924454Smckusick * 6024454Smckusick * An extra level of indentation is added if ps.ind_stmt is 1. After 6124454Smckusick * printing, ps.ind_stmt is set to 1 iff the line just printed has an 6224454Smckusick * unterminated, non-declaration statement. 6324454Smckusick * 6424454Smckusick * HISTORY: 6524454Smckusick * initial coding November 1976 D A Willcox of CAC 6624454Smckusick * 6724454Smckusick */ 68*33229Sbostic #include "indent_globs.h" 698803Smckusick 708803Smckusick 718803Smckusick 7224454Smckusick int ff = 014; /* used to write a form feed */ 7324454Smckusick int comment_open; 7424454Smckusick static paren_target; 758803Smckusick 7624454Smckusick dump_line() 7724454Smckusick { /* dump_line is the routine that actually 7824454Smckusick * effects the printing of the new source. 7924454Smckusick * It prints the label section, followed 8024454Smckusick * by the code section with the 8124454Smckusick * appropriate nesting level, followed by 8224454Smckusick * any comments */ 8324454Smckusick register int cur_col, 8424454Smckusick temp_col, 8524454Smckusick target_col; 868803Smckusick 8724454Smckusick if (ps.procname[0]) { 8824454Smckusick if (troff) 8924454Smckusick fprintf(output, ".Pr \"%s\"\n", ps.procname); 9024454Smckusick ps.ind_level = 0; 9124454Smckusick ps.procname[0] = 0; 9224454Smckusick } 9324454Smckusick if (s_code == e_code && s_lab == e_lab && s_com == e_com) { 9424454Smckusick if (suppress_blanklines>0) suppress_blanklines--; 9524454Smckusick else { 9624454Smckusick ps.bl_line = true; 9724454Smckusick n_real_blanklines++; 9824454Smckusick } 9924454Smckusick } 10024454Smckusick else if (!inhibit_formatting) { 10124454Smckusick suppress_blanklines = 0; 10224454Smckusick ps.bl_line = false; 10324454Smckusick if (prefix_blankline_requested) 10424454Smckusick if (swallow_optional_blanklines) { 10524454Smckusick if (n_real_blanklines == 1) 10624454Smckusick n_real_blanklines = 0; 10724454Smckusick } 10824454Smckusick else { 10924454Smckusick if (n_real_blanklines == 0) 11024454Smckusick n_real_blanklines = 1; 11124454Smckusick } 11224454Smckusick while (--n_real_blanklines >= 0) 11324454Smckusick putc('\n', output); 11424454Smckusick n_real_blanklines = 0; 11524454Smckusick if (ps.ind_level == 0) 11624454Smckusick ps.ind_stmt = 0; /* this is a class A kludge. dont do 11724454Smckusick * additional statement indentation if we 11824454Smckusick * are at bracket level 0 */ 1198803Smckusick 12024454Smckusick if (e_lab != s_lab || e_code != s_code) 12124454Smckusick ++code_lines; /* keep count of lines with code */ 1228803Smckusick 1238803Smckusick 12424454Smckusick if (e_lab != s_lab) { /* print lab, if any */ 12524454Smckusick if (comment_open) { 12624454Smckusick comment_open = 0; 12724454Smckusick fprintf(output, ".*/\n"); 12824454Smckusick } 12924454Smckusick while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t')) 13024454Smckusick e_lab--; 13124454Smckusick cur_col = pad_output(1, compute_label_target()); 13224454Smckusick fprintf(output, "%.*s", e_lab - s_lab, s_lab); 13324454Smckusick cur_col = count_spaces(cur_col, s_lab); 1348803Smckusick } 13524454Smckusick else 13624454Smckusick cur_col = 1; /* there is no label section */ 1378803Smckusick 13824454Smckusick ps.pcase = false; 1398803Smckusick 14024454Smckusick if (s_code != e_code) { /* print code section, if any */ 14124454Smckusick register char *p; 1428803Smckusick 14324454Smckusick if (comment_open) { 14424454Smckusick comment_open = 0; 14524454Smckusick fprintf(output, ".*/\n"); 14624454Smckusick } 14724454Smckusick target_col = compute_code_target(); 14824454Smckusick { 14924454Smckusick register i; 1508803Smckusick 15124454Smckusick for (i = 0; i < ps.p_l_follow; i++) 15224454Smckusick if (ps.paren_indents[i] >= 0) 15324454Smckusick ps.paren_indents[i] = -(ps.paren_indents[i] + target_col); 15424454Smckusick } 15524454Smckusick cur_col = pad_output(cur_col, target_col); 15624454Smckusick for (p = s_code; p < e_code; p++) 157*33229Sbostic if (*p == (char)0200) 15824454Smckusick fprintf(output, "%d", target_col * 7); 15924454Smckusick else 16024454Smckusick putc(*p, output); 16124454Smckusick cur_col = count_spaces(cur_col, s_code); 16224454Smckusick } 16324454Smckusick if (s_com != e_com) 16424454Smckusick if (troff) { 16524454Smckusick register char *p; 1668803Smckusick 16724454Smckusick if (e_com[-1] == '/' && e_com[-2] == '*') 16824454Smckusick e_com -= 2; 16924454Smckusick while (e_com > s_com && e_com[-1] == ' ') 17024454Smckusick e_com--; 17124454Smckusick *e_com = 0; 17224454Smckusick p = s_com; 17324454Smckusick while (*p == ' ') 17424454Smckusick p++; 17524454Smckusick if (p[0] == '/' && p[1] == '*') 17624454Smckusick p += 2; 17724454Smckusick else if (p[0] == '*') 17824454Smckusick p += p[1] == '/' ? 2 : 1; 17924454Smckusick while (*p == ' ') 18024454Smckusick p++; 18124454Smckusick if (*p == 0) 18224454Smckusick goto inhibit_newline; 18324454Smckusick if (!comment_open) { 18424454Smckusick if ('a' <= *p && *p <= 'z') 18524454Smckusick *p = *p + 'A' - 'a'; 18624454Smckusick if (s_code != e_code || s_lab != e_lab) { 18724454Smckusick fprintf(output, "\\c\n./* %dp 1 %dp\n", 18824454Smckusick ps.com_col * 7, target_col * 7); 18924454Smckusick } 19024454Smckusick else 19124454Smckusick fprintf(output, "./* %dp 0 %dp\n", 19224454Smckusick ps.com_col * 7, target_col * 7); 19324454Smckusick } 19424454Smckusick comment_open = 1; 19524454Smckusick while (*p) { 19624454Smckusick if (*p == BACKSLASH) 19724454Smckusick putc(BACKSLASH, output); 19824454Smckusick putc(*p++, output); 19924454Smckusick } 20024454Smckusick } 20124454Smckusick else { /* print comment, if any */ 20224454Smckusick register target = ps.com_col; 20324454Smckusick register char *com_st = s_com; 2048803Smckusick 20524454Smckusick target += ps.comment_delta; 20624454Smckusick while (target <= 0) 20724454Smckusick if (*s_com == ' ') 20824454Smckusick target++, s_com++; 20924454Smckusick else if (*s_com == '\t') 21024454Smckusick target = ((target - 1) & ~7) + 9, s_com++; 21124454Smckusick else 21224454Smckusick target = 1; 21324454Smckusick if (cur_col > target) { /* if comment cant fit on this 21424454Smckusick * line, put it on next line */ 21524454Smckusick putc('\n', output); 21624454Smckusick cur_col = 1; 21724454Smckusick ++ps.out_lines; 21824454Smckusick } 21924454Smckusick cur_col = pad_output(cur_col, target); 22024454Smckusick if (!ps.box_com) { 22124454Smckusick if (star_comment_cont && com_st[1] != '*') 22224454Smckusick if (com_st[1] == ' ' && com_st[0] == ' ') 22324454Smckusick com_st[1] = '*'; 22424454Smckusick else 22524454Smckusick fwrite(" * ", com_st[0] == '\t' ? 2 : com_st[0] == '*' ? 1 : 3, 1, output); 22624454Smckusick } 22724454Smckusick fwrite(com_st, e_com - com_st, 1, output); 22824454Smckusick ps.comment_delta = ps.n_comment_delta; 22924454Smckusick cur_col = count_spaces(cur_col, com_st); 23024454Smckusick ++ps.com_lines; /* count lines with comments */ 23124454Smckusick } 23224454Smckusick if (ps.use_ff) 23324454Smckusick putc('\014', output); 23424454Smckusick else 23524454Smckusick putc('\n', output); 23624454Smckusick inhibit_newline: 23724454Smckusick ++ps.out_lines; 23824454Smckusick if (ps.just_saw_decl == 1 && blanklines_after_declarations) { 23924454Smckusick prefix_blankline_requested = 1; 24024454Smckusick ps.just_saw_decl = 0; 2418803Smckusick } 24224454Smckusick else 24324454Smckusick prefix_blankline_requested = postfix_blankline_requested; 24424454Smckusick postfix_blankline_requested = 0; 2458803Smckusick } 24624454Smckusick ps.decl_on_line = ps.in_decl; /* if we are in the middle of a 24724454Smckusick * declaration, remember that fact 24824454Smckusick * for proper comment indentation */ 24924454Smckusick ps.ind_stmt = ps.in_stmt & ~ps.in_decl; /* next line should be 25024454Smckusick * indented if we have not 25124454Smckusick * completed this stmt and 25224454Smckusick * if we are not in the 25324454Smckusick * middle of a declaration */ 25424454Smckusick ps.use_ff = false; 25524454Smckusick ps.dumped_decl_indent = 0; 25624454Smckusick *(e_lab = s_lab) = '\0'; /* reset buffers */ 2578803Smckusick *(e_code = s_code) = '\0'; 2588803Smckusick *(e_com = s_com) = '\0'; 25924454Smckusick ps.ind_level = ps.i_l_follow; 26024454Smckusick ps.paren_level = ps.p_l_follow; 26124454Smckusick paren_target = -ps.paren_indents[ps.paren_level - 1]; 2628803Smckusick return; 2638803Smckusick }; 2648803Smckusick 26524454Smckusick compute_code_target() { 26624454Smckusick register target_col = ps.ind_size * ps.ind_level + 1; 2678803Smckusick 26824454Smckusick if (ps.paren_level) 26924454Smckusick if (!lineup_to_parens) 27024454Smckusick target_col += continuation_indent * ps.paren_level; 27124454Smckusick else { 27224454Smckusick register w; 27324454Smckusick register t = paren_target; 2748803Smckusick 27524454Smckusick if ((w = count_spaces(t, s_code) - max_col) > 0 27624454Smckusick && count_spaces(target_col, s_code) <= max_col) { 27724454Smckusick t -= w + 1; 27824454Smckusick if (t > target_col) 27924454Smckusick target_col = t; 28024454Smckusick } 28124454Smckusick else 28224454Smckusick target_col = t; 28324454Smckusick } 28424454Smckusick else if (ps.ind_stmt) 28524454Smckusick target_col += continuation_indent; 28624454Smckusick return target_col; 28724454Smckusick } 2888803Smckusick 28924454Smckusick compute_label_target() 29024454Smckusick { 29124454Smckusick return 29224454Smckusick ps.pcase ? (int) (case_ind * ps.ind_size) +1 29324454Smckusick : *s_lab == '#' ? 1 29424454Smckusick : ps.ind_size * (ps.ind_level - label_offset) +1; 29524454Smckusick } 2968803Smckusick 2978803Smckusick 29824454Smckusick /* 29924454Smckusick * Copyright (C) 1976 by the Board of Trustees of the University of 30024454Smckusick * Illinois 30124454Smckusick * 30224454Smckusick * All rights reserved 30324454Smckusick * 30424454Smckusick * 30524454Smckusick * NAME: fill_buffer 30624454Smckusick * 30724454Smckusick * FUNCTION: Reads one block of input into input_buffer 30824454Smckusick * 30924454Smckusick * HISTORY: initial coding November 1976 D A Willcox of CAC 1/7/77 31024454Smckusick * A Willcox of CAC Added check for switch back to partly full input 31124454Smckusick * buffer from temporary buffer 31224454Smckusick * 31324454Smckusick */ 31424454Smckusick int 31524454Smckusick fill_buffer() 31624454Smckusick { /* this routine reads stuff from the input */ 31724454Smckusick int count; 31824454Smckusick register char *p; 31924454Smckusick register int i; 32024454Smckusick register FILE *f = input; 3218803Smckusick 32224454Smckusick if (bp_save != 0) { /* there is a partly filled input buffer 32324454Smckusick * left */ 32424454Smckusick buf_ptr = bp_save; /* dont read anything, just switch buffers */ 3258803Smckusick buf_end = be_save; 3268803Smckusick bp_save = be_save = 0; 3278803Smckusick if (buf_ptr < buf_end) 32824454Smckusick return; /* only return if there is really 32924454Smckusick * something in this buffer */ 3308803Smckusick } 33124454Smckusick p = in_buffer; 33224454Smckusick buf_ptr = p; 33324454Smckusick while ((*p++ = i = getc(f)) != EOF && i != '\n'); 33424454Smckusick if (i == EOF) { 33524454Smckusick p[-1] = ' '; 33624454Smckusick *p++ = '\n'; 3378803Smckusick had_eof = true; 3388803Smckusick } 33924454Smckusick buf_end = p; 34024454Smckusick if (p[-2] == '/' && p[-3] == '*') { 34124454Smckusick if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0) 34224454Smckusick fill_buffer(); /* flush indent error message */ 34324454Smckusick else { 34424454Smckusick int com = 0; 3458803Smckusick 34624454Smckusick p = in_buffer; 34724454Smckusick while (*p == ' ' || *p == '\t') 34824454Smckusick p++; 34924454Smckusick if (*p == '/' && p[1] == '*') { 35024454Smckusick p += 2; 35124454Smckusick while (*p == ' ' || *p == '\t') 35224454Smckusick p++; 35324454Smckusick if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E' 35424454Smckusick && p[4] == 'N' && p[5] == 'T') { 35524454Smckusick p += 6; 35624454Smckusick while (*p == ' ' || *p == '\t') 35724454Smckusick p++; 35824454Smckusick if (*p == '*') 35924454Smckusick com = 1; 36024454Smckusick else if (*p == 'O') 36124454Smckusick if (*++p == 'N') 36224454Smckusick p++, com = 1; 36324454Smckusick else if (*p == 'F' && *++p == 'F') 36424454Smckusick p++, com = 2; 36524454Smckusick while (*p == ' ' || *p == '\t') 36624454Smckusick p++; 36724454Smckusick if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) { 36824454Smckusick if (s_com != e_com || s_lab != e_lab || s_code != e_code) 36924454Smckusick dump_line(); 37024454Smckusick if (!(inhibit_formatting = com - 1)) { 37124454Smckusick n_real_blanklines = 0; 37224454Smckusick postfix_blankline_requested = 0; 37324454Smckusick prefix_blankline_requested = 0; 37424454Smckusick suppress_blanklines = 1; 37524454Smckusick } 37624454Smckusick } 37724454Smckusick } 37824454Smckusick } 37924454Smckusick } 38024454Smckusick } 38124454Smckusick if (inhibit_formatting) { 38224454Smckusick p = in_buffer; 38324454Smckusick do 38424454Smckusick putc(*p, output); 38524454Smckusick while (*p++ != '\n'); 38624454Smckusick } 3878803Smckusick return; 3888803Smckusick }; 3898803Smckusick 39024454Smckusick /* 39124454Smckusick * Copyright (C) 1976 by the Board of Trustees of the University of 39224454Smckusick * Illinois 39324454Smckusick * 39424454Smckusick * All rights reserved 39524454Smckusick * 39624454Smckusick * 39724454Smckusick * NAME: pad_output 39824454Smckusick * 39924454Smckusick * FUNCTION: Writes tabs and spaces to move the current column up to the 40024454Smckusick * desired position. 40124454Smckusick * 40224454Smckusick * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf. 40324454Smckusick * 40424454Smckusick * PARAMETERS: current integer The current column target 40524454Smckusick * nteger The desired column 40624454Smckusick * 40724454Smckusick * RETURNS: Integer value of the new column. (If current >= target, no 40824454Smckusick * action is taken, and current is returned. 40924454Smckusick * 41024454Smckusick * GLOBALS: None 41124454Smckusick * 41224454Smckusick * CALLS: write (sys) 41324454Smckusick * 41424454Smckusick * CALLED BY: dump_line 41524454Smckusick * 41624454Smckusick * HISTORY: initial coding November 1976 D A Willcox of CAC 41724454Smckusick * 41824454Smckusick */ 41924454Smckusick pad_output(current, target) /* writes tabs and blanks (if necessary) 42024454Smckusick * to get the current output position up 42124454Smckusick * to the target column */ 42224454Smckusick int current; /* the current column value */ 42324454Smckusick int target; /* position we want it at */ 4248803Smckusick { 42524454Smckusick register int curr; /* internal column pointer */ 4268803Smckusick register int tcur; 4278803Smckusick 42824454Smckusick if (troff) 42924454Smckusick fprintf(output, "\\h'|%dp'", (target - 1) * 7); 43024454Smckusick else { 43124454Smckusick if (current >= target) 43224454Smckusick return (current); /* line is already long enough */ 43324454Smckusick curr = current; 43424454Smckusick while ((tcur = ((curr - 1) & tabmask) + tabsize + 1) <= target) { 43524454Smckusick putc('\t', output); 4368803Smckusick curr = tcur; 4378803Smckusick } 43824454Smckusick while (curr++ < target) 43924454Smckusick putc(' ', output); /* pad with final blanks */ 4408803Smckusick } 4418803Smckusick return (target); 4428803Smckusick }; 4438803Smckusick 44424454Smckusick /* 44524454Smckusick * Copyright (C) 1976 by the Board of Trustees of the University of 44624454Smckusick * Illinois 44724454Smckusick * 44824454Smckusick * All rights reserved 44924454Smckusick * 45024454Smckusick * 45124454Smckusick * NAME: count_spaces 45224454Smckusick * 45324454Smckusick * FUNCTION: Find out where printing of a given string will leave the current 45424454Smckusick * character position on output. 45524454Smckusick * 45624454Smckusick * ALGORITHM: Run thru input string and add appropriate values to current 45724454Smckusick * position. 45824454Smckusick * 45924454Smckusick * RETURNS: Integer value of position after printing "buffer" starting in 46024454Smckusick * column "current". 46124454Smckusick * 46224454Smckusick * HISTORY: initial coding November 1976 D A Willcox of CAC 46324454Smckusick * 46424454Smckusick */ 46524454Smckusick int 46624454Smckusick count_spaces(current, buffer) 4678803Smckusick 46824454Smckusick /* 46924454Smckusick * this routine figures out where the character position will be after 47024454Smckusick * printing the text in buffer starting at column "current" 47124454Smckusick */ 47224454Smckusick int current; 47324454Smckusick char *buffer; 4748803Smckusick { 47524454Smckusick register char *buf; /* used to look thru buffer */ 47624454Smckusick register int cur; /* current character counter */ 4778803Smckusick 4788803Smckusick cur = current; 4798803Smckusick 4808803Smckusick for (buf = buffer; *buf != '\0'; ++buf) { 4818803Smckusick switch (*buf) { 4828803Smckusick 48324454Smckusick case '\n': 48424454Smckusick case 014: /* form feed */ 4858803Smckusick cur = 1; 4868803Smckusick break; 4878803Smckusick 48824454Smckusick case '\t': 4898803Smckusick cur = ((cur - 1) & tabmask) + tabsize + 1; 4908803Smckusick break; 4918803Smckusick 49224454Smckusick case '': /* this is a backspace */ 4938803Smckusick --cur; 4948803Smckusick break; 4958803Smckusick 49624454Smckusick default: 4978803Smckusick ++cur; 4988803Smckusick break; 49924454Smckusick } /* end of switch */ 50024454Smckusick } /* end of for loop */ 5018803Smckusick return (cur); 5028803Smckusick }; 5038803Smckusick 50430984Sbostic int found_err; 50524454Smckusick diag(level, msg, a, b) 5068803Smckusick { 50730984Sbostic if (level) 50830984Sbostic found_err = 1; 50924454Smckusick if (output == stdout) { 51024454Smckusick fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no); 51124454Smckusick fprintf(stdout, msg, a, b); 51224454Smckusick fprintf(stdout, " */\n"); 5138803Smckusick } 51424454Smckusick else { 51524454Smckusick fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no); 51624454Smckusick fprintf(stderr, msg, a, b); 51724454Smckusick fprintf(stderr, "\n"); 5188803Smckusick } 5198803Smckusick } 520