121969Sdist /* 221969Sdist * Copyright (c) 1980 Regents of the University of California. 3*33767Sbostic * Copyright (c) 1976 Board of Trustees of the University of Illinois. 4*33767Sbostic * All rights reserved. 5*33767Sbostic * 6*33767Sbostic * Redistribution and use in source and binary forms are permitted 7*33767Sbostic * provided that this notice is preserved and that due credit is given 8*33767Sbostic * to the University of California at Berkeley and the University of 9*33767Sbostic * Illinois at Urbana. The name of either University may not be used 10*33767Sbostic * to endorse or promote products derived from this software without 11*33767Sbostic * specific prior written permission. This software is provided 12*33767Sbostic * ``as is'' without express or implied warranty. 1321969Sdist */ 148803Smckusick 1521969Sdist #ifndef lint 16*33767Sbostic static char sccsid[] = "@(#)io.c 5.6 (Berkeley) 03/22/88"; 17*33767Sbostic #endif /* not lint */ 1821969Sdist 19*33767Sbostic /* 2024454Smckusick * FILE NAME: 2124454Smckusick * io.c 2224454Smckusick * PURPOSE: 2324454Smckusick * Contains routines to handle i/o related stuff for indent. 2424454Smckusick * GLOBALS: 2524454Smckusick * None 2624454Smckusick * FUNCTIONS: 2724454Smckusick * dump_line 2824454Smckusick * fill_buffer 2924454Smckusick * pad_output 3024454Smckusick * count_spaces 3124454Smckusick * eqin 3224454Smckusick * cmp 3324454Smckusick * 3424454Smckusick */ 3524454Smckusick /*- 3624454Smckusick * 3724454Smckusick * Copyright (C) 1976 3824454Smckusick * by the 3924454Smckusick * Board of Trustees 4024454Smckusick * of the 4124454Smckusick * University of Illinois 4224454Smckusick * 4324454Smckusick * All rights reserved 4424454Smckusick * 4524454Smckusick * 4624454Smckusick * NAME: 4724454Smckusick * dump_line 4824454Smckusick * 4924454Smckusick * FUNCTION: 5024454Smckusick * Does the actual printing of the stored up line 5124454Smckusick * 5224454Smckusick * ALGORITHM: 5324454Smckusick * For each of the label, code, and comment sections which are used on 5424454Smckusick * this line: 5524454Smckusick * 5624454Smckusick * 1) Use pad_output to get the section aligned properly. 5724454Smckusick * 2) write the section 5824454Smckusick * 5924454Smckusick * The indentation level used for the code is set by ps.ind_level. After 6024454Smckusick * printing, ps.ind_level is set to ps.i_l_follow. 6124454Smckusick * 6224454Smckusick * An extra level of indentation is added if ps.ind_stmt is 1. After 6324454Smckusick * printing, ps.ind_stmt is set to 1 iff the line just printed has an 6424454Smckusick * unterminated, non-declaration statement. 6524454Smckusick * 6624454Smckusick * HISTORY: 6724454Smckusick * initial coding November 1976 D A Willcox of CAC 6824454Smckusick * 6924454Smckusick */ 7033229Sbostic #include "indent_globs.h" 718803Smckusick 728803Smckusick 738803Smckusick 7424454Smckusick int ff = 014; /* used to write a form feed */ 7524454Smckusick int comment_open; 7624454Smckusick static paren_target; 778803Smckusick 7824454Smckusick dump_line() 7924454Smckusick { /* dump_line is the routine that actually 8024454Smckusick * effects the printing of the new source. 8124454Smckusick * It prints the label section, followed 8224454Smckusick * by the code section with the 8324454Smckusick * appropriate nesting level, followed by 8424454Smckusick * any comments */ 8524454Smckusick register int cur_col, 8624454Smckusick temp_col, 8724454Smckusick target_col; 888803Smckusick 8924454Smckusick if (ps.procname[0]) { 9024454Smckusick if (troff) 9124454Smckusick fprintf(output, ".Pr \"%s\"\n", ps.procname); 9224454Smckusick ps.ind_level = 0; 9324454Smckusick ps.procname[0] = 0; 9424454Smckusick } 9524454Smckusick if (s_code == e_code && s_lab == e_lab && s_com == e_com) { 9624454Smckusick if (suppress_blanklines>0) suppress_blanklines--; 9724454Smckusick else { 9824454Smckusick ps.bl_line = true; 9924454Smckusick n_real_blanklines++; 10024454Smckusick } 10124454Smckusick } 10224454Smckusick else if (!inhibit_formatting) { 10324454Smckusick suppress_blanklines = 0; 10424454Smckusick ps.bl_line = false; 10524454Smckusick if (prefix_blankline_requested) 10624454Smckusick if (swallow_optional_blanklines) { 10724454Smckusick if (n_real_blanklines == 1) 10824454Smckusick n_real_blanklines = 0; 10924454Smckusick } 11024454Smckusick else { 11124454Smckusick if (n_real_blanklines == 0) 11224454Smckusick n_real_blanklines = 1; 11324454Smckusick } 11424454Smckusick while (--n_real_blanklines >= 0) 11524454Smckusick putc('\n', output); 11624454Smckusick n_real_blanklines = 0; 11724454Smckusick if (ps.ind_level == 0) 11824454Smckusick ps.ind_stmt = 0; /* this is a class A kludge. dont do 11924454Smckusick * additional statement indentation if we 12024454Smckusick * are at bracket level 0 */ 1218803Smckusick 12224454Smckusick if (e_lab != s_lab || e_code != s_code) 12324454Smckusick ++code_lines; /* keep count of lines with code */ 1248803Smckusick 1258803Smckusick 12624454Smckusick if (e_lab != s_lab) { /* print lab, if any */ 12724454Smckusick if (comment_open) { 12824454Smckusick comment_open = 0; 12924454Smckusick fprintf(output, ".*/\n"); 13024454Smckusick } 13124454Smckusick while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t')) 13224454Smckusick e_lab--; 13324454Smckusick cur_col = pad_output(1, compute_label_target()); 13424454Smckusick fprintf(output, "%.*s", e_lab - s_lab, s_lab); 13524454Smckusick cur_col = count_spaces(cur_col, s_lab); 1368803Smckusick } 13724454Smckusick else 13824454Smckusick cur_col = 1; /* there is no label section */ 1398803Smckusick 14024454Smckusick ps.pcase = false; 1418803Smckusick 14224454Smckusick if (s_code != e_code) { /* print code section, if any */ 14324454Smckusick register char *p; 1448803Smckusick 14524454Smckusick if (comment_open) { 14624454Smckusick comment_open = 0; 14724454Smckusick fprintf(output, ".*/\n"); 14824454Smckusick } 14924454Smckusick target_col = compute_code_target(); 15024454Smckusick { 15124454Smckusick register i; 1528803Smckusick 15324454Smckusick for (i = 0; i < ps.p_l_follow; i++) 15424454Smckusick if (ps.paren_indents[i] >= 0) 15524454Smckusick ps.paren_indents[i] = -(ps.paren_indents[i] + target_col); 15624454Smckusick } 15724454Smckusick cur_col = pad_output(cur_col, target_col); 15824454Smckusick for (p = s_code; p < e_code; p++) 15933229Sbostic if (*p == (char)0200) 16024454Smckusick fprintf(output, "%d", target_col * 7); 16124454Smckusick else 16224454Smckusick putc(*p, output); 16324454Smckusick cur_col = count_spaces(cur_col, s_code); 16424454Smckusick } 16524454Smckusick if (s_com != e_com) 16624454Smckusick if (troff) { 16724454Smckusick register char *p; 1688803Smckusick 16924454Smckusick if (e_com[-1] == '/' && e_com[-2] == '*') 17024454Smckusick e_com -= 2; 17124454Smckusick while (e_com > s_com && e_com[-1] == ' ') 17224454Smckusick e_com--; 17324454Smckusick *e_com = 0; 17424454Smckusick p = s_com; 17524454Smckusick while (*p == ' ') 17624454Smckusick p++; 17724454Smckusick if (p[0] == '/' && p[1] == '*') 17824454Smckusick p += 2; 17924454Smckusick else if (p[0] == '*') 18024454Smckusick p += p[1] == '/' ? 2 : 1; 18124454Smckusick while (*p == ' ') 18224454Smckusick p++; 18324454Smckusick if (*p == 0) 18424454Smckusick goto inhibit_newline; 18524454Smckusick if (!comment_open) { 18624454Smckusick if ('a' <= *p && *p <= 'z') 18724454Smckusick *p = *p + 'A' - 'a'; 18824454Smckusick if (s_code != e_code || s_lab != e_lab) { 18924454Smckusick fprintf(output, "\\c\n./* %dp 1 %dp\n", 19024454Smckusick ps.com_col * 7, target_col * 7); 19124454Smckusick } 19224454Smckusick else 19324454Smckusick fprintf(output, "./* %dp 0 %dp\n", 19424454Smckusick ps.com_col * 7, target_col * 7); 19524454Smckusick } 19624454Smckusick comment_open = 1; 19724454Smckusick while (*p) { 19824454Smckusick if (*p == BACKSLASH) 19924454Smckusick putc(BACKSLASH, output); 20024454Smckusick putc(*p++, output); 20124454Smckusick } 20224454Smckusick } 20324454Smckusick else { /* print comment, if any */ 20424454Smckusick register target = ps.com_col; 20524454Smckusick register char *com_st = s_com; 2068803Smckusick 20724454Smckusick target += ps.comment_delta; 20824454Smckusick while (target <= 0) 20924454Smckusick if (*s_com == ' ') 21024454Smckusick target++, s_com++; 21124454Smckusick else if (*s_com == '\t') 21224454Smckusick target = ((target - 1) & ~7) + 9, s_com++; 21324454Smckusick else 21424454Smckusick target = 1; 21524454Smckusick if (cur_col > target) { /* if comment cant fit on this 21624454Smckusick * line, put it on next line */ 21724454Smckusick putc('\n', output); 21824454Smckusick cur_col = 1; 21924454Smckusick ++ps.out_lines; 22024454Smckusick } 22124454Smckusick cur_col = pad_output(cur_col, target); 22224454Smckusick if (!ps.box_com) { 22324454Smckusick if (star_comment_cont && com_st[1] != '*') 22424454Smckusick if (com_st[1] == ' ' && com_st[0] == ' ') 22524454Smckusick com_st[1] = '*'; 22624454Smckusick else 22724454Smckusick fwrite(" * ", com_st[0] == '\t' ? 2 : com_st[0] == '*' ? 1 : 3, 1, output); 22824454Smckusick } 22924454Smckusick fwrite(com_st, e_com - com_st, 1, output); 23024454Smckusick ps.comment_delta = ps.n_comment_delta; 23124454Smckusick cur_col = count_spaces(cur_col, com_st); 23224454Smckusick ++ps.com_lines; /* count lines with comments */ 23324454Smckusick } 23424454Smckusick if (ps.use_ff) 23524454Smckusick putc('\014', output); 23624454Smckusick else 23724454Smckusick putc('\n', output); 23824454Smckusick inhibit_newline: 23924454Smckusick ++ps.out_lines; 24024454Smckusick if (ps.just_saw_decl == 1 && blanklines_after_declarations) { 24124454Smckusick prefix_blankline_requested = 1; 24224454Smckusick ps.just_saw_decl = 0; 2438803Smckusick } 24424454Smckusick else 24524454Smckusick prefix_blankline_requested = postfix_blankline_requested; 24624454Smckusick postfix_blankline_requested = 0; 2478803Smckusick } 24824454Smckusick ps.decl_on_line = ps.in_decl; /* if we are in the middle of a 24924454Smckusick * declaration, remember that fact 25024454Smckusick * for proper comment indentation */ 25124454Smckusick ps.ind_stmt = ps.in_stmt & ~ps.in_decl; /* next line should be 25224454Smckusick * indented if we have not 25324454Smckusick * completed this stmt and 25424454Smckusick * if we are not in the 25524454Smckusick * middle of a declaration */ 25624454Smckusick ps.use_ff = false; 25724454Smckusick ps.dumped_decl_indent = 0; 25824454Smckusick *(e_lab = s_lab) = '\0'; /* reset buffers */ 2598803Smckusick *(e_code = s_code) = '\0'; 2608803Smckusick *(e_com = s_com) = '\0'; 26124454Smckusick ps.ind_level = ps.i_l_follow; 26224454Smckusick ps.paren_level = ps.p_l_follow; 26324454Smckusick paren_target = -ps.paren_indents[ps.paren_level - 1]; 2648803Smckusick return; 2658803Smckusick }; 2668803Smckusick 26724454Smckusick compute_code_target() { 26824454Smckusick register target_col = ps.ind_size * ps.ind_level + 1; 2698803Smckusick 27024454Smckusick if (ps.paren_level) 27124454Smckusick if (!lineup_to_parens) 27224454Smckusick target_col += continuation_indent * ps.paren_level; 27324454Smckusick else { 27424454Smckusick register w; 27524454Smckusick register t = paren_target; 2768803Smckusick 27724454Smckusick if ((w = count_spaces(t, s_code) - max_col) > 0 27824454Smckusick && count_spaces(target_col, s_code) <= max_col) { 27924454Smckusick t -= w + 1; 28024454Smckusick if (t > target_col) 28124454Smckusick target_col = t; 28224454Smckusick } 28324454Smckusick else 28424454Smckusick target_col = t; 28524454Smckusick } 28624454Smckusick else if (ps.ind_stmt) 28724454Smckusick target_col += continuation_indent; 28824454Smckusick return target_col; 28924454Smckusick } 2908803Smckusick 29124454Smckusick compute_label_target() 29224454Smckusick { 29324454Smckusick return 29424454Smckusick ps.pcase ? (int) (case_ind * ps.ind_size) +1 29524454Smckusick : *s_lab == '#' ? 1 29624454Smckusick : ps.ind_size * (ps.ind_level - label_offset) +1; 29724454Smckusick } 2988803Smckusick 2998803Smckusick 30024454Smckusick /* 30124454Smckusick * Copyright (C) 1976 by the Board of Trustees of the University of 30224454Smckusick * Illinois 30324454Smckusick * 30424454Smckusick * All rights reserved 30524454Smckusick * 30624454Smckusick * 30724454Smckusick * NAME: fill_buffer 30824454Smckusick * 30924454Smckusick * FUNCTION: Reads one block of input into input_buffer 31024454Smckusick * 31124454Smckusick * HISTORY: initial coding November 1976 D A Willcox of CAC 1/7/77 31224454Smckusick * A Willcox of CAC Added check for switch back to partly full input 31324454Smckusick * buffer from temporary buffer 31424454Smckusick * 31524454Smckusick */ 31624454Smckusick int 31724454Smckusick fill_buffer() 31824454Smckusick { /* this routine reads stuff from the input */ 31924454Smckusick int count; 32024454Smckusick register char *p; 32124454Smckusick register int i; 32224454Smckusick register FILE *f = input; 3238803Smckusick 32424454Smckusick if (bp_save != 0) { /* there is a partly filled input buffer 32524454Smckusick * left */ 32624454Smckusick buf_ptr = bp_save; /* dont read anything, just switch buffers */ 3278803Smckusick buf_end = be_save; 3288803Smckusick bp_save = be_save = 0; 3298803Smckusick if (buf_ptr < buf_end) 33024454Smckusick return; /* only return if there is really 33124454Smckusick * something in this buffer */ 3328803Smckusick } 33324454Smckusick p = in_buffer; 33424454Smckusick buf_ptr = p; 33524454Smckusick while ((*p++ = i = getc(f)) != EOF && i != '\n'); 33624454Smckusick if (i == EOF) { 33724454Smckusick p[-1] = ' '; 33824454Smckusick *p++ = '\n'; 3398803Smckusick had_eof = true; 3408803Smckusick } 34124454Smckusick buf_end = p; 34224454Smckusick if (p[-2] == '/' && p[-3] == '*') { 34324454Smckusick if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0) 34424454Smckusick fill_buffer(); /* flush indent error message */ 34524454Smckusick else { 34624454Smckusick int com = 0; 3478803Smckusick 34824454Smckusick p = in_buffer; 34924454Smckusick while (*p == ' ' || *p == '\t') 35024454Smckusick p++; 35124454Smckusick if (*p == '/' && p[1] == '*') { 35224454Smckusick p += 2; 35324454Smckusick while (*p == ' ' || *p == '\t') 35424454Smckusick p++; 35524454Smckusick if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E' 35624454Smckusick && p[4] == 'N' && p[5] == 'T') { 35724454Smckusick p += 6; 35824454Smckusick while (*p == ' ' || *p == '\t') 35924454Smckusick p++; 36024454Smckusick if (*p == '*') 36124454Smckusick com = 1; 36224454Smckusick else if (*p == 'O') 36324454Smckusick if (*++p == 'N') 36424454Smckusick p++, com = 1; 36524454Smckusick else if (*p == 'F' && *++p == 'F') 36624454Smckusick p++, com = 2; 36724454Smckusick while (*p == ' ' || *p == '\t') 36824454Smckusick p++; 36924454Smckusick if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) { 37024454Smckusick if (s_com != e_com || s_lab != e_lab || s_code != e_code) 37124454Smckusick dump_line(); 37224454Smckusick if (!(inhibit_formatting = com - 1)) { 37324454Smckusick n_real_blanklines = 0; 37424454Smckusick postfix_blankline_requested = 0; 37524454Smckusick prefix_blankline_requested = 0; 37624454Smckusick suppress_blanklines = 1; 37724454Smckusick } 37824454Smckusick } 37924454Smckusick } 38024454Smckusick } 38124454Smckusick } 38224454Smckusick } 38324454Smckusick if (inhibit_formatting) { 38424454Smckusick p = in_buffer; 38524454Smckusick do 38624454Smckusick putc(*p, output); 38724454Smckusick while (*p++ != '\n'); 38824454Smckusick } 3898803Smckusick return; 3908803Smckusick }; 3918803Smckusick 39224454Smckusick /* 39324454Smckusick * Copyright (C) 1976 by the Board of Trustees of the University of 39424454Smckusick * Illinois 39524454Smckusick * 39624454Smckusick * All rights reserved 39724454Smckusick * 39824454Smckusick * 39924454Smckusick * NAME: pad_output 40024454Smckusick * 40124454Smckusick * FUNCTION: Writes tabs and spaces to move the current column up to the 40224454Smckusick * desired position. 40324454Smckusick * 40424454Smckusick * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf. 40524454Smckusick * 40624454Smckusick * PARAMETERS: current integer The current column target 40724454Smckusick * nteger The desired column 40824454Smckusick * 40924454Smckusick * RETURNS: Integer value of the new column. (If current >= target, no 41024454Smckusick * action is taken, and current is returned. 41124454Smckusick * 41224454Smckusick * GLOBALS: None 41324454Smckusick * 41424454Smckusick * CALLS: write (sys) 41524454Smckusick * 41624454Smckusick * CALLED BY: dump_line 41724454Smckusick * 41824454Smckusick * HISTORY: initial coding November 1976 D A Willcox of CAC 41924454Smckusick * 42024454Smckusick */ 42124454Smckusick pad_output(current, target) /* writes tabs and blanks (if necessary) 42224454Smckusick * to get the current output position up 42324454Smckusick * to the target column */ 42424454Smckusick int current; /* the current column value */ 42524454Smckusick int target; /* position we want it at */ 4268803Smckusick { 42724454Smckusick register int curr; /* internal column pointer */ 4288803Smckusick register int tcur; 4298803Smckusick 43024454Smckusick if (troff) 43124454Smckusick fprintf(output, "\\h'|%dp'", (target - 1) * 7); 43224454Smckusick else { 43324454Smckusick if (current >= target) 43424454Smckusick return (current); /* line is already long enough */ 43524454Smckusick curr = current; 43624454Smckusick while ((tcur = ((curr - 1) & tabmask) + tabsize + 1) <= target) { 43724454Smckusick putc('\t', output); 4388803Smckusick curr = tcur; 4398803Smckusick } 44024454Smckusick while (curr++ < target) 44124454Smckusick putc(' ', output); /* pad with final blanks */ 4428803Smckusick } 4438803Smckusick return (target); 4448803Smckusick }; 4458803Smckusick 44624454Smckusick /* 44724454Smckusick * Copyright (C) 1976 by the Board of Trustees of the University of 44824454Smckusick * Illinois 44924454Smckusick * 45024454Smckusick * All rights reserved 45124454Smckusick * 45224454Smckusick * 45324454Smckusick * NAME: count_spaces 45424454Smckusick * 45524454Smckusick * FUNCTION: Find out where printing of a given string will leave the current 45624454Smckusick * character position on output. 45724454Smckusick * 45824454Smckusick * ALGORITHM: Run thru input string and add appropriate values to current 45924454Smckusick * position. 46024454Smckusick * 46124454Smckusick * RETURNS: Integer value of position after printing "buffer" starting in 46224454Smckusick * column "current". 46324454Smckusick * 46424454Smckusick * HISTORY: initial coding November 1976 D A Willcox of CAC 46524454Smckusick * 46624454Smckusick */ 46724454Smckusick int 46824454Smckusick count_spaces(current, buffer) 4698803Smckusick 47024454Smckusick /* 47124454Smckusick * this routine figures out where the character position will be after 47224454Smckusick * printing the text in buffer starting at column "current" 47324454Smckusick */ 47424454Smckusick int current; 47524454Smckusick char *buffer; 4768803Smckusick { 47724454Smckusick register char *buf; /* used to look thru buffer */ 47824454Smckusick register int cur; /* current character counter */ 4798803Smckusick 4808803Smckusick cur = current; 4818803Smckusick 4828803Smckusick for (buf = buffer; *buf != '\0'; ++buf) { 4838803Smckusick switch (*buf) { 4848803Smckusick 48524454Smckusick case '\n': 48624454Smckusick case 014: /* form feed */ 4878803Smckusick cur = 1; 4888803Smckusick break; 4898803Smckusick 49024454Smckusick case '\t': 4918803Smckusick cur = ((cur - 1) & tabmask) + tabsize + 1; 4928803Smckusick break; 4938803Smckusick 49424454Smckusick case '': /* this is a backspace */ 4958803Smckusick --cur; 4968803Smckusick break; 4978803Smckusick 49824454Smckusick default: 4998803Smckusick ++cur; 5008803Smckusick break; 50124454Smckusick } /* end of switch */ 50224454Smckusick } /* end of for loop */ 5038803Smckusick return (cur); 5048803Smckusick }; 5058803Smckusick 50630984Sbostic int found_err; 50724454Smckusick diag(level, msg, a, b) 5088803Smckusick { 50930984Sbostic if (level) 51030984Sbostic found_err = 1; 51124454Smckusick if (output == stdout) { 51224454Smckusick fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no); 51324454Smckusick fprintf(stdout, msg, a, b); 51424454Smckusick fprintf(stdout, " */\n"); 5158803Smckusick } 51624454Smckusick else { 51724454Smckusick fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no); 51824454Smckusick fprintf(stderr, msg, a, b); 51924454Smckusick fprintf(stderr, "\n"); 5208803Smckusick } 5218803Smckusick } 522