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*24454Smckusick static char sccsid[] = "@(#)io.c 5.2 (Berkeley) 08/28/85"; 921969Sdist #endif not lint 1021969Sdist 11*24454Smckusick /*- 12*24454Smckusick * Copyright (C) 1976 13*24454Smckusick * by the 14*24454Smckusick * Board of Trustees 15*24454Smckusick * of the 16*24454Smckusick * University of Illinois 17*24454Smckusick * All rights reserved 18*24454Smckusick * FILE NAME: 19*24454Smckusick * io.c 20*24454Smckusick * PURPOSE: 21*24454Smckusick * Contains routines to handle i/o related stuff for indent. 22*24454Smckusick * GLOBALS: 23*24454Smckusick * None 24*24454Smckusick * FUNCTIONS: 25*24454Smckusick * dump_line 26*24454Smckusick * fill_buffer 27*24454Smckusick * pad_output 28*24454Smckusick * count_spaces 29*24454Smckusick * eqin 30*24454Smckusick * cmp 31*24454Smckusick * 32*24454Smckusick */ 33*24454Smckusick /*- 34*24454Smckusick * 35*24454Smckusick * Copyright (C) 1976 36*24454Smckusick * by the 37*24454Smckusick * Board of Trustees 38*24454Smckusick * of the 39*24454Smckusick * University of Illinois 40*24454Smckusick * 41*24454Smckusick * All rights reserved 42*24454Smckusick * 43*24454Smckusick * 44*24454Smckusick * NAME: 45*24454Smckusick * dump_line 46*24454Smckusick * 47*24454Smckusick * FUNCTION: 48*24454Smckusick * Does the actual printing of the stored up line 49*24454Smckusick * 50*24454Smckusick * ALGORITHM: 51*24454Smckusick * For each of the label, code, and comment sections which are used on 52*24454Smckusick * this line: 53*24454Smckusick * 54*24454Smckusick * 1) Use pad_output to get the section aligned properly. 55*24454Smckusick * 2) write the section 56*24454Smckusick * 57*24454Smckusick * The indentation level used for the code is set by ps.ind_level. After 58*24454Smckusick * printing, ps.ind_level is set to ps.i_l_follow. 59*24454Smckusick * 60*24454Smckusick * An extra level of indentation is added if ps.ind_stmt is 1. After 61*24454Smckusick * printing, ps.ind_stmt is set to 1 iff the line just printed has an 62*24454Smckusick * unterminated, non-declaration statement. 63*24454Smckusick * 64*24454Smckusick * HISTORY: 65*24454Smckusick * initial coding November 1976 D A Willcox of CAC 66*24454Smckusick * 67*24454Smckusick */ 688803Smckusick #include "indent_globs.h"; 698803Smckusick 708803Smckusick 718803Smckusick 72*24454Smckusick int ff = 014; /* used to write a form feed */ 73*24454Smckusick int comment_open; 74*24454Smckusick static paren_target; 758803Smckusick 76*24454Smckusick dump_line() 77*24454Smckusick { /* dump_line is the routine that actually 78*24454Smckusick * effects the printing of the new source. 79*24454Smckusick * It prints the label section, followed 80*24454Smckusick * by the code section with the 81*24454Smckusick * appropriate nesting level, followed by 82*24454Smckusick * any comments */ 83*24454Smckusick register int cur_col, 84*24454Smckusick temp_col, 85*24454Smckusick target_col; 868803Smckusick 87*24454Smckusick if (ps.procname[0]) { 88*24454Smckusick if (troff) 89*24454Smckusick fprintf(output, ".Pr \"%s\"\n", ps.procname); 90*24454Smckusick ps.ind_level = 0; 91*24454Smckusick ps.procname[0] = 0; 92*24454Smckusick } 93*24454Smckusick if (s_code == e_code && s_lab == e_lab && s_com == e_com) { 94*24454Smckusick if (suppress_blanklines>0) suppress_blanklines--; 95*24454Smckusick else { 96*24454Smckusick ps.bl_line = true; 97*24454Smckusick n_real_blanklines++; 98*24454Smckusick } 99*24454Smckusick } 100*24454Smckusick else if (!inhibit_formatting) { 101*24454Smckusick suppress_blanklines = 0; 102*24454Smckusick ps.bl_line = false; 103*24454Smckusick if (prefix_blankline_requested) 104*24454Smckusick if (swallow_optional_blanklines) { 105*24454Smckusick if (n_real_blanklines == 1) 106*24454Smckusick n_real_blanklines = 0; 107*24454Smckusick } 108*24454Smckusick else { 109*24454Smckusick if (n_real_blanklines == 0) 110*24454Smckusick n_real_blanklines = 1; 111*24454Smckusick } 112*24454Smckusick while (--n_real_blanklines >= 0) 113*24454Smckusick putc('\n', output); 114*24454Smckusick n_real_blanklines = 0; 115*24454Smckusick if (ps.ind_level == 0) 116*24454Smckusick ps.ind_stmt = 0; /* this is a class A kludge. dont do 117*24454Smckusick * additional statement indentation if we 118*24454Smckusick * are at bracket level 0 */ 1198803Smckusick 120*24454Smckusick if (e_lab != s_lab || e_code != s_code) 121*24454Smckusick ++code_lines; /* keep count of lines with code */ 1228803Smckusick 1238803Smckusick 124*24454Smckusick if (e_lab != s_lab) { /* print lab, if any */ 125*24454Smckusick if (comment_open) { 126*24454Smckusick comment_open = 0; 127*24454Smckusick fprintf(output, ".*/\n"); 128*24454Smckusick } 129*24454Smckusick while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t')) 130*24454Smckusick e_lab--; 131*24454Smckusick cur_col = pad_output(1, compute_label_target()); 132*24454Smckusick fprintf(output, "%.*s", e_lab - s_lab, s_lab); 133*24454Smckusick cur_col = count_spaces(cur_col, s_lab); 1348803Smckusick } 135*24454Smckusick else 136*24454Smckusick cur_col = 1; /* there is no label section */ 1378803Smckusick 138*24454Smckusick ps.pcase = false; 1398803Smckusick 140*24454Smckusick if (s_code != e_code) { /* print code section, if any */ 141*24454Smckusick register char *p; 1428803Smckusick 143*24454Smckusick if (comment_open) { 144*24454Smckusick comment_open = 0; 145*24454Smckusick fprintf(output, ".*/\n"); 146*24454Smckusick } 147*24454Smckusick target_col = compute_code_target(); 148*24454Smckusick { 149*24454Smckusick register i; 1508803Smckusick 151*24454Smckusick for (i = 0; i < ps.p_l_follow; i++) 152*24454Smckusick if (ps.paren_indents[i] >= 0) 153*24454Smckusick ps.paren_indents[i] = -(ps.paren_indents[i] + target_col); 154*24454Smckusick } 155*24454Smckusick cur_col = pad_output(cur_col, target_col); 156*24454Smckusick for (p = s_code; p < e_code; p++) 157*24454Smckusick if (*p == 0200) 158*24454Smckusick fprintf(output, "%d", target_col * 7); 159*24454Smckusick else 160*24454Smckusick putc(*p, output); 161*24454Smckusick cur_col = count_spaces(cur_col, s_code); 162*24454Smckusick } 163*24454Smckusick if (s_com != e_com) 164*24454Smckusick if (troff) { 165*24454Smckusick register char *p; 1668803Smckusick 167*24454Smckusick if (e_com[-1] == '/' && e_com[-2] == '*') 168*24454Smckusick e_com -= 2; 169*24454Smckusick while (e_com > s_com && e_com[-1] == ' ') 170*24454Smckusick e_com--; 171*24454Smckusick *e_com = 0; 172*24454Smckusick p = s_com; 173*24454Smckusick while (*p == ' ') 174*24454Smckusick p++; 175*24454Smckusick if (p[0] == '/' && p[1] == '*') 176*24454Smckusick p += 2; 177*24454Smckusick else if (p[0] == '*') 178*24454Smckusick p += p[1] == '/' ? 2 : 1; 179*24454Smckusick while (*p == ' ') 180*24454Smckusick p++; 181*24454Smckusick if (*p == 0) 182*24454Smckusick goto inhibit_newline; 183*24454Smckusick if (!comment_open) { 184*24454Smckusick if ('a' <= *p && *p <= 'z') 185*24454Smckusick *p = *p + 'A' - 'a'; 186*24454Smckusick if (s_code != e_code || s_lab != e_lab) { 187*24454Smckusick fprintf(output, "\\c\n./* %dp 1 %dp\n", 188*24454Smckusick ps.com_col * 7, target_col * 7); 189*24454Smckusick } 190*24454Smckusick else 191*24454Smckusick fprintf(output, "./* %dp 0 %dp\n", 192*24454Smckusick ps.com_col * 7, target_col * 7); 193*24454Smckusick } 194*24454Smckusick comment_open = 1; 195*24454Smckusick while (*p) { 196*24454Smckusick if (*p == BACKSLASH) 197*24454Smckusick putc(BACKSLASH, output); 198*24454Smckusick putc(*p++, output); 199*24454Smckusick } 200*24454Smckusick } 201*24454Smckusick else { /* print comment, if any */ 202*24454Smckusick register target = ps.com_col; 203*24454Smckusick register char *com_st = s_com; 2048803Smckusick 205*24454Smckusick target += ps.comment_delta; 206*24454Smckusick while (target <= 0) 207*24454Smckusick if (*s_com == ' ') 208*24454Smckusick target++, s_com++; 209*24454Smckusick else if (*s_com == '\t') 210*24454Smckusick target = ((target - 1) & ~7) + 9, s_com++; 211*24454Smckusick else 212*24454Smckusick target = 1; 213*24454Smckusick if (cur_col > target) { /* if comment cant fit on this 214*24454Smckusick * line, put it on next line */ 215*24454Smckusick putc('\n', output); 216*24454Smckusick cur_col = 1; 217*24454Smckusick ++ps.out_lines; 218*24454Smckusick } 219*24454Smckusick cur_col = pad_output(cur_col, target); 220*24454Smckusick if (!ps.box_com) { 221*24454Smckusick if (star_comment_cont && com_st[1] != '*') 222*24454Smckusick if (com_st[1] == ' ' && com_st[0] == ' ') 223*24454Smckusick com_st[1] = '*'; 224*24454Smckusick else 225*24454Smckusick fwrite(" * ", com_st[0] == '\t' ? 2 : com_st[0] == '*' ? 1 : 3, 1, output); 226*24454Smckusick } 227*24454Smckusick fwrite(com_st, e_com - com_st, 1, output); 228*24454Smckusick ps.comment_delta = ps.n_comment_delta; 229*24454Smckusick cur_col = count_spaces(cur_col, com_st); 230*24454Smckusick ++ps.com_lines; /* count lines with comments */ 231*24454Smckusick } 232*24454Smckusick if (ps.use_ff) 233*24454Smckusick putc('\014', output); 234*24454Smckusick else 235*24454Smckusick putc('\n', output); 236*24454Smckusick inhibit_newline: 237*24454Smckusick ++ps.out_lines; 238*24454Smckusick if (ps.just_saw_decl == 1 && blanklines_after_declarations) { 239*24454Smckusick prefix_blankline_requested = 1; 240*24454Smckusick ps.just_saw_decl = 0; 2418803Smckusick } 242*24454Smckusick else 243*24454Smckusick prefix_blankline_requested = postfix_blankline_requested; 244*24454Smckusick postfix_blankline_requested = 0; 2458803Smckusick } 246*24454Smckusick ps.decl_on_line = ps.in_decl; /* if we are in the middle of a 247*24454Smckusick * declaration, remember that fact 248*24454Smckusick * for proper comment indentation */ 249*24454Smckusick ps.ind_stmt = ps.in_stmt & ~ps.in_decl; /* next line should be 250*24454Smckusick * indented if we have not 251*24454Smckusick * completed this stmt and 252*24454Smckusick * if we are not in the 253*24454Smckusick * middle of a declaration */ 254*24454Smckusick ps.use_ff = false; 255*24454Smckusick ps.dumped_decl_indent = 0; 256*24454Smckusick *(e_lab = s_lab) = '\0'; /* reset buffers */ 2578803Smckusick *(e_code = s_code) = '\0'; 2588803Smckusick *(e_com = s_com) = '\0'; 259*24454Smckusick ps.ind_level = ps.i_l_follow; 260*24454Smckusick ps.paren_level = ps.p_l_follow; 261*24454Smckusick paren_target = -ps.paren_indents[ps.paren_level - 1]; 2628803Smckusick return; 2638803Smckusick }; 2648803Smckusick 265*24454Smckusick compute_code_target() { 266*24454Smckusick register target_col = ps.ind_size * ps.ind_level + 1; 2678803Smckusick 268*24454Smckusick if (ps.paren_level) 269*24454Smckusick if (!lineup_to_parens) 270*24454Smckusick target_col += continuation_indent * ps.paren_level; 271*24454Smckusick else { 272*24454Smckusick register w; 273*24454Smckusick register t = paren_target; 2748803Smckusick 275*24454Smckusick if ((w = count_spaces(t, s_code) - max_col) > 0 276*24454Smckusick && count_spaces(target_col, s_code) <= max_col) { 277*24454Smckusick t -= w + 1; 278*24454Smckusick if (t > target_col) 279*24454Smckusick target_col = t; 280*24454Smckusick } 281*24454Smckusick else 282*24454Smckusick target_col = t; 283*24454Smckusick } 284*24454Smckusick else if (ps.ind_stmt) 285*24454Smckusick target_col += continuation_indent; 286*24454Smckusick return target_col; 287*24454Smckusick } 2888803Smckusick 289*24454Smckusick compute_label_target() 290*24454Smckusick { 291*24454Smckusick return 292*24454Smckusick ps.pcase ? (int) (case_ind * ps.ind_size) +1 293*24454Smckusick : *s_lab == '#' ? 1 294*24454Smckusick : ps.ind_size * (ps.ind_level - label_offset) +1; 295*24454Smckusick } 2968803Smckusick 2978803Smckusick 298*24454Smckusick /* 299*24454Smckusick * Copyright (C) 1976 by the Board of Trustees of the University of 300*24454Smckusick * Illinois 301*24454Smckusick * 302*24454Smckusick * All rights reserved 303*24454Smckusick * 304*24454Smckusick * 305*24454Smckusick * NAME: fill_buffer 306*24454Smckusick * 307*24454Smckusick * FUNCTION: Reads one block of input into input_buffer 308*24454Smckusick * 309*24454Smckusick * HISTORY: initial coding November 1976 D A Willcox of CAC 1/7/77 310*24454Smckusick * A Willcox of CAC Added check for switch back to partly full input 311*24454Smckusick * buffer from temporary buffer 312*24454Smckusick * 313*24454Smckusick */ 314*24454Smckusick int 315*24454Smckusick fill_buffer() 316*24454Smckusick { /* this routine reads stuff from the input */ 317*24454Smckusick int count; 318*24454Smckusick register char *p; 319*24454Smckusick register int i; 320*24454Smckusick register FILE *f = input; 3218803Smckusick 322*24454Smckusick if (bp_save != 0) { /* there is a partly filled input buffer 323*24454Smckusick * left */ 324*24454Smckusick 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) 328*24454Smckusick return; /* only return if there is really 329*24454Smckusick * something in this buffer */ 3308803Smckusick } 331*24454Smckusick p = in_buffer; 332*24454Smckusick buf_ptr = p; 333*24454Smckusick while ((*p++ = i = getc(f)) != EOF && i != '\n'); 334*24454Smckusick if (i == EOF) { 335*24454Smckusick p[-1] = ' '; 336*24454Smckusick *p++ = '\n'; 3378803Smckusick had_eof = true; 3388803Smckusick } 339*24454Smckusick buf_end = p; 340*24454Smckusick if (p[-2] == '/' && p[-3] == '*') { 341*24454Smckusick if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0) 342*24454Smckusick fill_buffer(); /* flush indent error message */ 343*24454Smckusick else { 344*24454Smckusick int com = 0; 3458803Smckusick 346*24454Smckusick p = in_buffer; 347*24454Smckusick while (*p == ' ' || *p == '\t') 348*24454Smckusick p++; 349*24454Smckusick if (*p == '/' && p[1] == '*') { 350*24454Smckusick p += 2; 351*24454Smckusick while (*p == ' ' || *p == '\t') 352*24454Smckusick p++; 353*24454Smckusick if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E' 354*24454Smckusick && p[4] == 'N' && p[5] == 'T') { 355*24454Smckusick p += 6; 356*24454Smckusick while (*p == ' ' || *p == '\t') 357*24454Smckusick p++; 358*24454Smckusick if (*p == '*') 359*24454Smckusick com = 1; 360*24454Smckusick else if (*p == 'O') 361*24454Smckusick if (*++p == 'N') 362*24454Smckusick p++, com = 1; 363*24454Smckusick else if (*p == 'F' && *++p == 'F') 364*24454Smckusick p++, com = 2; 365*24454Smckusick while (*p == ' ' || *p == '\t') 366*24454Smckusick p++; 367*24454Smckusick if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) { 368*24454Smckusick if (s_com != e_com || s_lab != e_lab || s_code != e_code) 369*24454Smckusick dump_line(); 370*24454Smckusick if (!(inhibit_formatting = com - 1)) { 371*24454Smckusick n_real_blanklines = 0; 372*24454Smckusick postfix_blankline_requested = 0; 373*24454Smckusick prefix_blankline_requested = 0; 374*24454Smckusick suppress_blanklines = 1; 375*24454Smckusick } 376*24454Smckusick } 377*24454Smckusick } 378*24454Smckusick } 379*24454Smckusick } 380*24454Smckusick } 381*24454Smckusick if (inhibit_formatting) { 382*24454Smckusick p = in_buffer; 383*24454Smckusick do 384*24454Smckusick putc(*p, output); 385*24454Smckusick while (*p++ != '\n'); 386*24454Smckusick } 3878803Smckusick return; 3888803Smckusick }; 3898803Smckusick 390*24454Smckusick /* 391*24454Smckusick * Copyright (C) 1976 by the Board of Trustees of the University of 392*24454Smckusick * Illinois 393*24454Smckusick * 394*24454Smckusick * All rights reserved 395*24454Smckusick * 396*24454Smckusick * 397*24454Smckusick * NAME: pad_output 398*24454Smckusick * 399*24454Smckusick * FUNCTION: Writes tabs and spaces to move the current column up to the 400*24454Smckusick * desired position. 401*24454Smckusick * 402*24454Smckusick * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf. 403*24454Smckusick * 404*24454Smckusick * PARAMETERS: current integer The current column target 405*24454Smckusick * nteger The desired column 406*24454Smckusick * 407*24454Smckusick * RETURNS: Integer value of the new column. (If current >= target, no 408*24454Smckusick * action is taken, and current is returned. 409*24454Smckusick * 410*24454Smckusick * GLOBALS: None 411*24454Smckusick * 412*24454Smckusick * CALLS: write (sys) 413*24454Smckusick * 414*24454Smckusick * CALLED BY: dump_line 415*24454Smckusick * 416*24454Smckusick * HISTORY: initial coding November 1976 D A Willcox of CAC 417*24454Smckusick * 418*24454Smckusick */ 419*24454Smckusick pad_output(current, target) /* writes tabs and blanks (if necessary) 420*24454Smckusick * to get the current output position up 421*24454Smckusick * to the target column */ 422*24454Smckusick int current; /* the current column value */ 423*24454Smckusick int target; /* position we want it at */ 4248803Smckusick { 425*24454Smckusick register int curr; /* internal column pointer */ 4268803Smckusick register int tcur; 4278803Smckusick 428*24454Smckusick if (troff) 429*24454Smckusick fprintf(output, "\\h'|%dp'", (target - 1) * 7); 430*24454Smckusick else { 431*24454Smckusick if (current >= target) 432*24454Smckusick return (current); /* line is already long enough */ 433*24454Smckusick curr = current; 434*24454Smckusick while ((tcur = ((curr - 1) & tabmask) + tabsize + 1) <= target) { 435*24454Smckusick putc('\t', output); 4368803Smckusick curr = tcur; 4378803Smckusick } 438*24454Smckusick while (curr++ < target) 439*24454Smckusick putc(' ', output); /* pad with final blanks */ 4408803Smckusick } 4418803Smckusick return (target); 4428803Smckusick }; 4438803Smckusick 444*24454Smckusick /* 445*24454Smckusick * Copyright (C) 1976 by the Board of Trustees of the University of 446*24454Smckusick * Illinois 447*24454Smckusick * 448*24454Smckusick * All rights reserved 449*24454Smckusick * 450*24454Smckusick * 451*24454Smckusick * NAME: count_spaces 452*24454Smckusick * 453*24454Smckusick * FUNCTION: Find out where printing of a given string will leave the current 454*24454Smckusick * character position on output. 455*24454Smckusick * 456*24454Smckusick * ALGORITHM: Run thru input string and add appropriate values to current 457*24454Smckusick * position. 458*24454Smckusick * 459*24454Smckusick * RETURNS: Integer value of position after printing "buffer" starting in 460*24454Smckusick * column "current". 461*24454Smckusick * 462*24454Smckusick * HISTORY: initial coding November 1976 D A Willcox of CAC 463*24454Smckusick * 464*24454Smckusick */ 465*24454Smckusick int 466*24454Smckusick count_spaces(current, buffer) 4678803Smckusick 468*24454Smckusick /* 469*24454Smckusick * this routine figures out where the character position will be after 470*24454Smckusick * printing the text in buffer starting at column "current" 471*24454Smckusick */ 472*24454Smckusick int current; 473*24454Smckusick char *buffer; 4748803Smckusick { 475*24454Smckusick register char *buf; /* used to look thru buffer */ 476*24454Smckusick register int cur; /* current character counter */ 4778803Smckusick 4788803Smckusick cur = current; 4798803Smckusick 4808803Smckusick for (buf = buffer; *buf != '\0'; ++buf) { 4818803Smckusick switch (*buf) { 4828803Smckusick 483*24454Smckusick case '\n': 484*24454Smckusick case 014: /* form feed */ 4858803Smckusick cur = 1; 4868803Smckusick break; 4878803Smckusick 488*24454Smckusick case '\t': 4898803Smckusick cur = ((cur - 1) & tabmask) + tabsize + 1; 4908803Smckusick break; 4918803Smckusick 492*24454Smckusick case '': /* this is a backspace */ 4938803Smckusick --cur; 4948803Smckusick break; 4958803Smckusick 496*24454Smckusick default: 4978803Smckusick ++cur; 4988803Smckusick break; 499*24454Smckusick } /* end of switch */ 500*24454Smckusick } /* end of for loop */ 5018803Smckusick return (cur); 5028803Smckusick }; 5038803Smckusick 504*24454Smckusick diag(level, msg, a, b) 5058803Smckusick { 506*24454Smckusick if (output == stdout) { 507*24454Smckusick fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no); 508*24454Smckusick fprintf(stdout, msg, a, b); 509*24454Smckusick fprintf(stdout, " */\n"); 5108803Smckusick } 511*24454Smckusick else { 512*24454Smckusick fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no); 513*24454Smckusick fprintf(stderr, msg, a, b); 514*24454Smckusick fprintf(stderr, "\n"); 5158803Smckusick } 5168803Smckusick } 517