121969Sdist /* 221969Sdist * Copyright (c) 1980 Regents of the University of California. 333767Sbostic * Copyright (c) 1976 Board of Trustees of the University of Illinois. 433767Sbostic * All rights reserved. 533767Sbostic * 633767Sbostic * Redistribution and use in source and binary forms are permitted 7*34885Sbostic * provided that the above copyright notice and this paragraph are 8*34885Sbostic * duplicated in all such forms and that any documentation, 9*34885Sbostic * advertising materials, and other materials related to such 10*34885Sbostic * distribution and use acknowledge that the software was developed 11*34885Sbostic * by the University of California, Berkeley and the University 12*34885Sbostic * of Illinois, Urbana. The name of either 13*34885Sbostic * University may not be used to endorse or promote products derived 14*34885Sbostic * from this software without specific prior written permission. 15*34885Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 16*34885Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 17*34885Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1821969Sdist */ 198803Smckusick 2021969Sdist #ifndef lint 21*34885Sbostic static char sccsid[] = "@(#)io.c 5.7 (Berkeley) 06/29/88"; 2233767Sbostic #endif /* not lint */ 2321969Sdist 2433767Sbostic /* 2524454Smckusick * FILE NAME: 2624454Smckusick * io.c 2724454Smckusick * PURPOSE: 2824454Smckusick * Contains routines to handle i/o related stuff for indent. 2924454Smckusick * GLOBALS: 3024454Smckusick * None 3124454Smckusick * FUNCTIONS: 3224454Smckusick * dump_line 3324454Smckusick * fill_buffer 3424454Smckusick * pad_output 3524454Smckusick * count_spaces 3624454Smckusick * eqin 3724454Smckusick * cmp 3824454Smckusick * 3924454Smckusick */ 4024454Smckusick /*- 4124454Smckusick * 4224454Smckusick * Copyright (C) 1976 4324454Smckusick * by the 4424454Smckusick * Board of Trustees 4524454Smckusick * of the 4624454Smckusick * University of Illinois 4724454Smckusick * 4824454Smckusick * All rights reserved 4924454Smckusick * 5024454Smckusick * 5124454Smckusick * NAME: 5224454Smckusick * dump_line 5324454Smckusick * 5424454Smckusick * FUNCTION: 5524454Smckusick * Does the actual printing of the stored up line 5624454Smckusick * 5724454Smckusick * ALGORITHM: 5824454Smckusick * For each of the label, code, and comment sections which are used on 5924454Smckusick * this line: 6024454Smckusick * 6124454Smckusick * 1) Use pad_output to get the section aligned properly. 6224454Smckusick * 2) write the section 6324454Smckusick * 6424454Smckusick * The indentation level used for the code is set by ps.ind_level. After 6524454Smckusick * printing, ps.ind_level is set to ps.i_l_follow. 6624454Smckusick * 6724454Smckusick * An extra level of indentation is added if ps.ind_stmt is 1. After 6824454Smckusick * printing, ps.ind_stmt is set to 1 iff the line just printed has an 6924454Smckusick * unterminated, non-declaration statement. 7024454Smckusick * 7124454Smckusick * HISTORY: 7224454Smckusick * initial coding November 1976 D A Willcox of CAC 7324454Smckusick * 7424454Smckusick */ 7533229Sbostic #include "indent_globs.h" 768803Smckusick 778803Smckusick 788803Smckusick 7924454Smckusick int ff = 014; /* used to write a form feed */ 8024454Smckusick int comment_open; 8124454Smckusick static paren_target; 828803Smckusick 8324454Smckusick dump_line() 8424454Smckusick { /* dump_line is the routine that actually 8524454Smckusick * effects the printing of the new source. 8624454Smckusick * It prints the label section, followed 8724454Smckusick * by the code section with the 8824454Smckusick * appropriate nesting level, followed by 8924454Smckusick * any comments */ 9024454Smckusick register int cur_col, 9124454Smckusick temp_col, 9224454Smckusick target_col; 938803Smckusick 9424454Smckusick if (ps.procname[0]) { 9524454Smckusick if (troff) 9624454Smckusick fprintf(output, ".Pr \"%s\"\n", ps.procname); 9724454Smckusick ps.ind_level = 0; 9824454Smckusick ps.procname[0] = 0; 9924454Smckusick } 10024454Smckusick if (s_code == e_code && s_lab == e_lab && s_com == e_com) { 10124454Smckusick if (suppress_blanklines>0) suppress_blanklines--; 10224454Smckusick else { 10324454Smckusick ps.bl_line = true; 10424454Smckusick n_real_blanklines++; 10524454Smckusick } 10624454Smckusick } 10724454Smckusick else if (!inhibit_formatting) { 10824454Smckusick suppress_blanklines = 0; 10924454Smckusick ps.bl_line = false; 11024454Smckusick if (prefix_blankline_requested) 11124454Smckusick if (swallow_optional_blanklines) { 11224454Smckusick if (n_real_blanklines == 1) 11324454Smckusick n_real_blanklines = 0; 11424454Smckusick } 11524454Smckusick else { 11624454Smckusick if (n_real_blanklines == 0) 11724454Smckusick n_real_blanklines = 1; 11824454Smckusick } 11924454Smckusick while (--n_real_blanklines >= 0) 12024454Smckusick putc('\n', output); 12124454Smckusick n_real_blanklines = 0; 12224454Smckusick if (ps.ind_level == 0) 12324454Smckusick ps.ind_stmt = 0; /* this is a class A kludge. dont do 12424454Smckusick * additional statement indentation if we 12524454Smckusick * are at bracket level 0 */ 1268803Smckusick 12724454Smckusick if (e_lab != s_lab || e_code != s_code) 12824454Smckusick ++code_lines; /* keep count of lines with code */ 1298803Smckusick 1308803Smckusick 13124454Smckusick if (e_lab != s_lab) { /* print lab, if any */ 13224454Smckusick if (comment_open) { 13324454Smckusick comment_open = 0; 13424454Smckusick fprintf(output, ".*/\n"); 13524454Smckusick } 13624454Smckusick while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t')) 13724454Smckusick e_lab--; 13824454Smckusick cur_col = pad_output(1, compute_label_target()); 13924454Smckusick fprintf(output, "%.*s", e_lab - s_lab, s_lab); 14024454Smckusick cur_col = count_spaces(cur_col, s_lab); 1418803Smckusick } 14224454Smckusick else 14324454Smckusick cur_col = 1; /* there is no label section */ 1448803Smckusick 14524454Smckusick ps.pcase = false; 1468803Smckusick 14724454Smckusick if (s_code != e_code) { /* print code section, if any */ 14824454Smckusick register char *p; 1498803Smckusick 15024454Smckusick if (comment_open) { 15124454Smckusick comment_open = 0; 15224454Smckusick fprintf(output, ".*/\n"); 15324454Smckusick } 15424454Smckusick target_col = compute_code_target(); 15524454Smckusick { 15624454Smckusick register i; 1578803Smckusick 15824454Smckusick for (i = 0; i < ps.p_l_follow; i++) 15924454Smckusick if (ps.paren_indents[i] >= 0) 16024454Smckusick ps.paren_indents[i] = -(ps.paren_indents[i] + target_col); 16124454Smckusick } 16224454Smckusick cur_col = pad_output(cur_col, target_col); 16324454Smckusick for (p = s_code; p < e_code; p++) 16433229Sbostic if (*p == (char)0200) 16524454Smckusick fprintf(output, "%d", target_col * 7); 16624454Smckusick else 16724454Smckusick putc(*p, output); 16824454Smckusick cur_col = count_spaces(cur_col, s_code); 16924454Smckusick } 17024454Smckusick if (s_com != e_com) 17124454Smckusick if (troff) { 17224454Smckusick register char *p; 1738803Smckusick 17424454Smckusick if (e_com[-1] == '/' && e_com[-2] == '*') 17524454Smckusick e_com -= 2; 17624454Smckusick while (e_com > s_com && e_com[-1] == ' ') 17724454Smckusick e_com--; 17824454Smckusick *e_com = 0; 17924454Smckusick p = s_com; 18024454Smckusick while (*p == ' ') 18124454Smckusick p++; 18224454Smckusick if (p[0] == '/' && p[1] == '*') 18324454Smckusick p += 2; 18424454Smckusick else if (p[0] == '*') 18524454Smckusick p += p[1] == '/' ? 2 : 1; 18624454Smckusick while (*p == ' ') 18724454Smckusick p++; 18824454Smckusick if (*p == 0) 18924454Smckusick goto inhibit_newline; 19024454Smckusick if (!comment_open) { 19124454Smckusick if ('a' <= *p && *p <= 'z') 19224454Smckusick *p = *p + 'A' - 'a'; 19324454Smckusick if (s_code != e_code || s_lab != e_lab) { 19424454Smckusick fprintf(output, "\\c\n./* %dp 1 %dp\n", 19524454Smckusick ps.com_col * 7, target_col * 7); 19624454Smckusick } 19724454Smckusick else 19824454Smckusick fprintf(output, "./* %dp 0 %dp\n", 19924454Smckusick ps.com_col * 7, target_col * 7); 20024454Smckusick } 20124454Smckusick comment_open = 1; 20224454Smckusick while (*p) { 20324454Smckusick if (*p == BACKSLASH) 20424454Smckusick putc(BACKSLASH, output); 20524454Smckusick putc(*p++, output); 20624454Smckusick } 20724454Smckusick } 20824454Smckusick else { /* print comment, if any */ 20924454Smckusick register target = ps.com_col; 21024454Smckusick register char *com_st = s_com; 2118803Smckusick 21224454Smckusick target += ps.comment_delta; 21324454Smckusick while (target <= 0) 21424454Smckusick if (*s_com == ' ') 21524454Smckusick target++, s_com++; 21624454Smckusick else if (*s_com == '\t') 21724454Smckusick target = ((target - 1) & ~7) + 9, s_com++; 21824454Smckusick else 21924454Smckusick target = 1; 22024454Smckusick if (cur_col > target) { /* if comment cant fit on this 22124454Smckusick * line, put it on next line */ 22224454Smckusick putc('\n', output); 22324454Smckusick cur_col = 1; 22424454Smckusick ++ps.out_lines; 22524454Smckusick } 22624454Smckusick cur_col = pad_output(cur_col, target); 22724454Smckusick if (!ps.box_com) { 22824454Smckusick if (star_comment_cont && com_st[1] != '*') 22924454Smckusick if (com_st[1] == ' ' && com_st[0] == ' ') 23024454Smckusick com_st[1] = '*'; 23124454Smckusick else 23224454Smckusick fwrite(" * ", com_st[0] == '\t' ? 2 : com_st[0] == '*' ? 1 : 3, 1, output); 23324454Smckusick } 23424454Smckusick fwrite(com_st, e_com - com_st, 1, output); 23524454Smckusick ps.comment_delta = ps.n_comment_delta; 23624454Smckusick cur_col = count_spaces(cur_col, com_st); 23724454Smckusick ++ps.com_lines; /* count lines with comments */ 23824454Smckusick } 23924454Smckusick if (ps.use_ff) 24024454Smckusick putc('\014', output); 24124454Smckusick else 24224454Smckusick putc('\n', output); 24324454Smckusick inhibit_newline: 24424454Smckusick ++ps.out_lines; 24524454Smckusick if (ps.just_saw_decl == 1 && blanklines_after_declarations) { 24624454Smckusick prefix_blankline_requested = 1; 24724454Smckusick ps.just_saw_decl = 0; 2488803Smckusick } 24924454Smckusick else 25024454Smckusick prefix_blankline_requested = postfix_blankline_requested; 25124454Smckusick postfix_blankline_requested = 0; 2528803Smckusick } 25324454Smckusick ps.decl_on_line = ps.in_decl; /* if we are in the middle of a 25424454Smckusick * declaration, remember that fact 25524454Smckusick * for proper comment indentation */ 25624454Smckusick ps.ind_stmt = ps.in_stmt & ~ps.in_decl; /* next line should be 25724454Smckusick * indented if we have not 25824454Smckusick * completed this stmt and 25924454Smckusick * if we are not in the 26024454Smckusick * middle of a declaration */ 26124454Smckusick ps.use_ff = false; 26224454Smckusick ps.dumped_decl_indent = 0; 26324454Smckusick *(e_lab = s_lab) = '\0'; /* reset buffers */ 2648803Smckusick *(e_code = s_code) = '\0'; 2658803Smckusick *(e_com = s_com) = '\0'; 26624454Smckusick ps.ind_level = ps.i_l_follow; 26724454Smckusick ps.paren_level = ps.p_l_follow; 26824454Smckusick paren_target = -ps.paren_indents[ps.paren_level - 1]; 2698803Smckusick return; 2708803Smckusick }; 2718803Smckusick 27224454Smckusick compute_code_target() { 27324454Smckusick register target_col = ps.ind_size * ps.ind_level + 1; 2748803Smckusick 27524454Smckusick if (ps.paren_level) 27624454Smckusick if (!lineup_to_parens) 27724454Smckusick target_col += continuation_indent * ps.paren_level; 27824454Smckusick else { 27924454Smckusick register w; 28024454Smckusick register t = paren_target; 2818803Smckusick 28224454Smckusick if ((w = count_spaces(t, s_code) - max_col) > 0 28324454Smckusick && count_spaces(target_col, s_code) <= max_col) { 28424454Smckusick t -= w + 1; 28524454Smckusick if (t > target_col) 28624454Smckusick target_col = t; 28724454Smckusick } 28824454Smckusick else 28924454Smckusick target_col = t; 29024454Smckusick } 29124454Smckusick else if (ps.ind_stmt) 29224454Smckusick target_col += continuation_indent; 29324454Smckusick return target_col; 29424454Smckusick } 2958803Smckusick 29624454Smckusick compute_label_target() 29724454Smckusick { 29824454Smckusick return 29924454Smckusick ps.pcase ? (int) (case_ind * ps.ind_size) +1 30024454Smckusick : *s_lab == '#' ? 1 30124454Smckusick : ps.ind_size * (ps.ind_level - label_offset) +1; 30224454Smckusick } 3038803Smckusick 3048803Smckusick 30524454Smckusick /* 30624454Smckusick * Copyright (C) 1976 by the Board of Trustees of the University of 30724454Smckusick * Illinois 30824454Smckusick * 30924454Smckusick * All rights reserved 31024454Smckusick * 31124454Smckusick * 31224454Smckusick * NAME: fill_buffer 31324454Smckusick * 31424454Smckusick * FUNCTION: Reads one block of input into input_buffer 31524454Smckusick * 31624454Smckusick * HISTORY: initial coding November 1976 D A Willcox of CAC 1/7/77 31724454Smckusick * A Willcox of CAC Added check for switch back to partly full input 31824454Smckusick * buffer from temporary buffer 31924454Smckusick * 32024454Smckusick */ 32124454Smckusick int 32224454Smckusick fill_buffer() 32324454Smckusick { /* this routine reads stuff from the input */ 32424454Smckusick int count; 32524454Smckusick register char *p; 32624454Smckusick register int i; 32724454Smckusick register FILE *f = input; 3288803Smckusick 32924454Smckusick if (bp_save != 0) { /* there is a partly filled input buffer 33024454Smckusick * left */ 33124454Smckusick buf_ptr = bp_save; /* dont read anything, just switch buffers */ 3328803Smckusick buf_end = be_save; 3338803Smckusick bp_save = be_save = 0; 3348803Smckusick if (buf_ptr < buf_end) 33524454Smckusick return; /* only return if there is really 33624454Smckusick * something in this buffer */ 3378803Smckusick } 33824454Smckusick p = in_buffer; 33924454Smckusick buf_ptr = p; 34024454Smckusick while ((*p++ = i = getc(f)) != EOF && i != '\n'); 34124454Smckusick if (i == EOF) { 34224454Smckusick p[-1] = ' '; 34324454Smckusick *p++ = '\n'; 3448803Smckusick had_eof = true; 3458803Smckusick } 34624454Smckusick buf_end = p; 34724454Smckusick if (p[-2] == '/' && p[-3] == '*') { 34824454Smckusick if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0) 34924454Smckusick fill_buffer(); /* flush indent error message */ 35024454Smckusick else { 35124454Smckusick int com = 0; 3528803Smckusick 35324454Smckusick p = in_buffer; 35424454Smckusick while (*p == ' ' || *p == '\t') 35524454Smckusick p++; 35624454Smckusick if (*p == '/' && p[1] == '*') { 35724454Smckusick p += 2; 35824454Smckusick while (*p == ' ' || *p == '\t') 35924454Smckusick p++; 36024454Smckusick if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E' 36124454Smckusick && p[4] == 'N' && p[5] == 'T') { 36224454Smckusick p += 6; 36324454Smckusick while (*p == ' ' || *p == '\t') 36424454Smckusick p++; 36524454Smckusick if (*p == '*') 36624454Smckusick com = 1; 36724454Smckusick else if (*p == 'O') 36824454Smckusick if (*++p == 'N') 36924454Smckusick p++, com = 1; 37024454Smckusick else if (*p == 'F' && *++p == 'F') 37124454Smckusick p++, com = 2; 37224454Smckusick while (*p == ' ' || *p == '\t') 37324454Smckusick p++; 37424454Smckusick if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) { 37524454Smckusick if (s_com != e_com || s_lab != e_lab || s_code != e_code) 37624454Smckusick dump_line(); 37724454Smckusick if (!(inhibit_formatting = com - 1)) { 37824454Smckusick n_real_blanklines = 0; 37924454Smckusick postfix_blankline_requested = 0; 38024454Smckusick prefix_blankline_requested = 0; 38124454Smckusick suppress_blanklines = 1; 38224454Smckusick } 38324454Smckusick } 38424454Smckusick } 38524454Smckusick } 38624454Smckusick } 38724454Smckusick } 38824454Smckusick if (inhibit_formatting) { 38924454Smckusick p = in_buffer; 39024454Smckusick do 39124454Smckusick putc(*p, output); 39224454Smckusick while (*p++ != '\n'); 39324454Smckusick } 3948803Smckusick return; 3958803Smckusick }; 3968803Smckusick 39724454Smckusick /* 39824454Smckusick * Copyright (C) 1976 by the Board of Trustees of the University of 39924454Smckusick * Illinois 40024454Smckusick * 40124454Smckusick * All rights reserved 40224454Smckusick * 40324454Smckusick * 40424454Smckusick * NAME: pad_output 40524454Smckusick * 40624454Smckusick * FUNCTION: Writes tabs and spaces to move the current column up to the 40724454Smckusick * desired position. 40824454Smckusick * 40924454Smckusick * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf. 41024454Smckusick * 41124454Smckusick * PARAMETERS: current integer The current column target 41224454Smckusick * nteger The desired column 41324454Smckusick * 41424454Smckusick * RETURNS: Integer value of the new column. (If current >= target, no 41524454Smckusick * action is taken, and current is returned. 41624454Smckusick * 41724454Smckusick * GLOBALS: None 41824454Smckusick * 41924454Smckusick * CALLS: write (sys) 42024454Smckusick * 42124454Smckusick * CALLED BY: dump_line 42224454Smckusick * 42324454Smckusick * HISTORY: initial coding November 1976 D A Willcox of CAC 42424454Smckusick * 42524454Smckusick */ 42624454Smckusick pad_output(current, target) /* writes tabs and blanks (if necessary) 42724454Smckusick * to get the current output position up 42824454Smckusick * to the target column */ 42924454Smckusick int current; /* the current column value */ 43024454Smckusick int target; /* position we want it at */ 4318803Smckusick { 43224454Smckusick register int curr; /* internal column pointer */ 4338803Smckusick register int tcur; 4348803Smckusick 43524454Smckusick if (troff) 43624454Smckusick fprintf(output, "\\h'|%dp'", (target - 1) * 7); 43724454Smckusick else { 43824454Smckusick if (current >= target) 43924454Smckusick return (current); /* line is already long enough */ 44024454Smckusick curr = current; 44124454Smckusick while ((tcur = ((curr - 1) & tabmask) + tabsize + 1) <= target) { 44224454Smckusick putc('\t', output); 4438803Smckusick curr = tcur; 4448803Smckusick } 44524454Smckusick while (curr++ < target) 44624454Smckusick putc(' ', output); /* pad with final blanks */ 4478803Smckusick } 4488803Smckusick return (target); 4498803Smckusick }; 4508803Smckusick 45124454Smckusick /* 45224454Smckusick * Copyright (C) 1976 by the Board of Trustees of the University of 45324454Smckusick * Illinois 45424454Smckusick * 45524454Smckusick * All rights reserved 45624454Smckusick * 45724454Smckusick * 45824454Smckusick * NAME: count_spaces 45924454Smckusick * 46024454Smckusick * FUNCTION: Find out where printing of a given string will leave the current 46124454Smckusick * character position on output. 46224454Smckusick * 46324454Smckusick * ALGORITHM: Run thru input string and add appropriate values to current 46424454Smckusick * position. 46524454Smckusick * 46624454Smckusick * RETURNS: Integer value of position after printing "buffer" starting in 46724454Smckusick * column "current". 46824454Smckusick * 46924454Smckusick * HISTORY: initial coding November 1976 D A Willcox of CAC 47024454Smckusick * 47124454Smckusick */ 47224454Smckusick int 47324454Smckusick count_spaces(current, buffer) 4748803Smckusick 47524454Smckusick /* 47624454Smckusick * this routine figures out where the character position will be after 47724454Smckusick * printing the text in buffer starting at column "current" 47824454Smckusick */ 47924454Smckusick int current; 48024454Smckusick char *buffer; 4818803Smckusick { 48224454Smckusick register char *buf; /* used to look thru buffer */ 48324454Smckusick register int cur; /* current character counter */ 4848803Smckusick 4858803Smckusick cur = current; 4868803Smckusick 4878803Smckusick for (buf = buffer; *buf != '\0'; ++buf) { 4888803Smckusick switch (*buf) { 4898803Smckusick 49024454Smckusick case '\n': 49124454Smckusick case 014: /* form feed */ 4928803Smckusick cur = 1; 4938803Smckusick break; 4948803Smckusick 49524454Smckusick case '\t': 4968803Smckusick cur = ((cur - 1) & tabmask) + tabsize + 1; 4978803Smckusick break; 4988803Smckusick 49924454Smckusick case '': /* this is a backspace */ 5008803Smckusick --cur; 5018803Smckusick break; 5028803Smckusick 50324454Smckusick default: 5048803Smckusick ++cur; 5058803Smckusick break; 50624454Smckusick } /* end of switch */ 50724454Smckusick } /* end of for loop */ 5088803Smckusick return (cur); 5098803Smckusick }; 5108803Smckusick 51130984Sbostic int found_err; 51224454Smckusick diag(level, msg, a, b) 5138803Smckusick { 51430984Sbostic if (level) 51530984Sbostic 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 } 527