1 /*- 2 * SPDX-License-Identifier: BSD-4-Clause 3 * 4 * Copyright (c) 1985 Sun Microsystems, Inc. 5 * Copyright (c) 1980, 1993 6 * The Regents of the University of California. All rights reserved. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38 #if 0 39 #endif 40 41 #include <sys/cdefs.h> 42 #include <ctype.h> 43 #include <err.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include "indent_globs.h" 48 #include "indent.h" 49 50 /* Globals */ 51 int found_err; 52 int n_real_blanklines; 53 int prefix_blankline_requested, postfix_blankline_requested; 54 int code_lines; 55 int had_eof; 56 int inhibit_formatting; 57 int suppress_blanklines; 58 59 int comment_open; 60 static int paren_target; 61 static int pad_output(int current, int target); 62 63 void 64 dump_line(void) 65 { /* dump_line is the routine that actually 66 * effects the printing of the new source. It 67 * prints the label section, followed by the 68 * code section with the appropriate nesting 69 * level, followed by any comments */ 70 int cur_col, 71 target_col = 1; 72 static int not_first_line; 73 74 if (ps.procname[0]) { 75 ps.ind_level = 0; 76 ps.procname[0] = 0; 77 } 78 if (s_code == e_code && s_lab == e_lab && s_com == e_com) { 79 if (suppress_blanklines > 0) 80 suppress_blanklines--; 81 else { 82 ps.bl_line = true; 83 n_real_blanklines++; 84 } 85 } 86 else if (!inhibit_formatting) { 87 suppress_blanklines = 0; 88 ps.bl_line = false; 89 if (prefix_blankline_requested && not_first_line) { 90 if (opt.swallow_optional_blanklines) { 91 if (n_real_blanklines == 1) 92 n_real_blanklines = 0; 93 } 94 else { 95 if (n_real_blanklines == 0) 96 n_real_blanklines = 1; 97 } 98 } 99 while (--n_real_blanklines >= 0) 100 putc('\n', output); 101 n_real_blanklines = 0; 102 if (ps.ind_level == 0) 103 ps.ind_stmt = 0; /* this is a class A kludge. dont do 104 * additional statement indentation if we are 105 * at bracket level 0 */ 106 107 if (e_lab != s_lab || e_code != s_code) 108 ++code_lines; /* keep count of lines with code */ 109 110 111 if (e_lab != s_lab) { /* print lab, if any */ 112 if (comment_open) { 113 comment_open = 0; 114 fprintf(output, ".*/\n"); 115 } 116 while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t')) 117 e_lab--; 118 *e_lab = '\0'; 119 cur_col = pad_output(1, compute_label_target()); 120 if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0 121 || strncmp(s_lab, "#endif", 6) == 0)) { 122 char *s = s_lab; 123 if (e_lab[-1] == '\n') e_lab--; 124 do putc(*s++, output); 125 while (s < e_lab && 'a' <= *s && *s<='z'); 126 while ((*s == ' ' || *s == '\t') && s < e_lab) 127 s++; 128 if (s < e_lab) 129 fprintf(output, s[0]=='/' && s[1]=='*' ? "\t%.*s" : "\t/* %.*s */", 130 (int)(e_lab - s), s); 131 } 132 else fprintf(output, "%.*s", (int)(e_lab - s_lab), s_lab); 133 cur_col = count_spaces(cur_col, s_lab); 134 } 135 else 136 cur_col = 1; /* there is no label section */ 137 138 ps.pcase = false; 139 140 if (s_code != e_code) { /* print code section, if any */ 141 char *p; 142 143 if (comment_open) { 144 comment_open = 0; 145 fprintf(output, ".*/\n"); 146 } 147 target_col = compute_code_target(); 148 { 149 int i; 150 151 for (i = 0; i < ps.p_l_follow; i++) 152 if (ps.paren_indents[i] >= 0) 153 ps.paren_indents[i] = -(ps.paren_indents[i] + target_col); 154 } 155 cur_col = pad_output(cur_col, target_col); 156 for (p = s_code; p < e_code; p++) 157 if (*p == (char) 0200) 158 fprintf(output, "%d", target_col * 7); 159 else 160 putc(*p, output); 161 cur_col = count_spaces(cur_col, s_code); 162 } 163 if (s_com != e_com) { /* print comment, if any */ 164 int target = ps.com_col; 165 char *com_st = s_com; 166 167 target += ps.comment_delta; 168 while (*com_st == '\t') /* consider original indentation in 169 * case this is a box comment */ 170 com_st++, target += opt.tabsize; 171 while (target <= 0) 172 if (*com_st == ' ') 173 target++, com_st++; 174 else if (*com_st == '\t') { 175 target = opt.tabsize * (1 + (target - 1) / opt.tabsize) + 1; 176 com_st++; 177 } 178 else 179 target = 1; 180 if (cur_col > target) { /* if comment can't fit on this line, 181 * put it on next line */ 182 putc('\n', output); 183 cur_col = 1; 184 ++ps.out_lines; 185 } 186 while (e_com > com_st && isspace((unsigned char)e_com[-1])) 187 e_com--; 188 (void)pad_output(cur_col, target); 189 fwrite(com_st, e_com - com_st, 1, output); 190 ps.comment_delta = ps.n_comment_delta; 191 ++ps.com_lines; /* count lines with comments */ 192 } 193 if (ps.use_ff) 194 putc('\014', output); 195 else 196 putc('\n', output); 197 ++ps.out_lines; 198 if (ps.just_saw_decl == 1 && opt.blanklines_after_declarations) { 199 prefix_blankline_requested = 1; 200 ps.just_saw_decl = 0; 201 } 202 else 203 prefix_blankline_requested = postfix_blankline_requested; 204 postfix_blankline_requested = 0; 205 } 206 ps.decl_on_line = ps.in_decl; /* if we are in the middle of a 207 * declaration, remember that fact for 208 * proper comment indentation */ 209 ps.ind_stmt = ps.in_stmt & ~ps.in_decl; /* next line should be 210 * indented if we have not 211 * completed this stmt and if 212 * we are not in the middle of 213 * a declaration */ 214 ps.use_ff = false; 215 ps.dumped_decl_indent = 0; 216 *(e_lab = s_lab) = '\0'; /* reset buffers */ 217 *(e_code = s_code) = '\0'; 218 *(e_com = s_com = combuf + 1) = '\0'; 219 ps.ind_level = ps.i_l_follow; 220 ps.paren_level = ps.p_l_follow; 221 if (ps.paren_level > 0) 222 paren_target = -ps.paren_indents[ps.paren_level - 1]; 223 not_first_line = 1; 224 } 225 226 int 227 compute_code_target(void) 228 { 229 int target_col = opt.ind_size * ps.ind_level + 1; 230 231 if (ps.paren_level) 232 if (!opt.lineup_to_parens) 233 target_col += opt.continuation_indent * 234 (2 * opt.continuation_indent == opt.ind_size ? 1 : ps.paren_level); 235 else if (opt.lineup_to_parens_always) 236 target_col = paren_target; 237 else { 238 int w; 239 int t = paren_target; 240 241 if ((w = count_spaces(t, s_code) - opt.max_col) > 0 242 && count_spaces(target_col, s_code) <= opt.max_col) { 243 t -= w + 1; 244 if (t > target_col) 245 target_col = t; 246 } 247 else 248 target_col = t; 249 } 250 else if (ps.ind_stmt) 251 target_col += opt.continuation_indent; 252 return target_col; 253 } 254 255 int 256 compute_label_target(void) 257 { 258 return 259 ps.pcase ? (int) (case_ind * opt.ind_size) + 1 260 : *s_lab == '#' ? 1 261 : opt.ind_size * (ps.ind_level - label_offset) + 1; 262 } 263 264 265 /* 266 * Copyright (C) 1976 by the Board of Trustees of the University of Illinois 267 * 268 * All rights reserved 269 * 270 * 271 * NAME: fill_buffer 272 * 273 * FUNCTION: Reads one block of input into input_buffer 274 * 275 * HISTORY: initial coding November 1976 D A Willcox of CAC 1/7/77 A 276 * Willcox of CAC Added check for switch back to partly full input 277 * buffer from temporary buffer 278 * 279 */ 280 void 281 fill_buffer(void) 282 { /* this routine reads stuff from the input */ 283 char *p; 284 int i; 285 FILE *f = input; 286 287 if (bp_save != NULL) { /* there is a partly filled input buffer left */ 288 buf_ptr = bp_save; /* do not read anything, just switch buffers */ 289 buf_end = be_save; 290 bp_save = be_save = NULL; 291 if (buf_ptr < buf_end) 292 return; /* only return if there is really something in 293 * this buffer */ 294 } 295 for (p = in_buffer;;) { 296 if (p >= in_buffer_limit) { 297 int size = (in_buffer_limit - in_buffer) * 2 + 10; 298 int offset = p - in_buffer; 299 in_buffer = realloc(in_buffer, size); 300 if (in_buffer == NULL) 301 errx(1, "input line too long"); 302 p = in_buffer + offset; 303 in_buffer_limit = in_buffer + size - 2; 304 } 305 if ((i = getc(f)) == EOF) { 306 *p++ = ' '; 307 *p++ = '\n'; 308 had_eof = true; 309 break; 310 } 311 if (i != '\0') 312 *p++ = i; 313 if (i == '\n') 314 break; 315 } 316 buf_ptr = in_buffer; 317 buf_end = p; 318 if (p - in_buffer > 2 && p[-2] == '/' && p[-3] == '*') { 319 if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0) 320 fill_buffer(); /* flush indent error message */ 321 else { 322 int com = 0; 323 324 p = in_buffer; 325 while (*p == ' ' || *p == '\t') 326 p++; 327 if (*p == '/' && p[1] == '*') { 328 p += 2; 329 while (*p == ' ' || *p == '\t') 330 p++; 331 if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E' 332 && p[4] == 'N' && p[5] == 'T') { 333 p += 6; 334 while (*p == ' ' || *p == '\t') 335 p++; 336 if (*p == '*') 337 com = 1; 338 else if (*p == 'O') { 339 if (*++p == 'N') 340 p++, com = 1; 341 else if (*p == 'F' && *++p == 'F') 342 p++, com = 2; 343 } 344 while (*p == ' ' || *p == '\t') 345 p++; 346 if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) { 347 if (s_com != e_com || s_lab != e_lab || s_code != e_code) 348 dump_line(); 349 if (!(inhibit_formatting = com - 1)) { 350 n_real_blanklines = 0; 351 postfix_blankline_requested = 0; 352 prefix_blankline_requested = 0; 353 suppress_blanklines = 1; 354 } 355 } 356 } 357 } 358 } 359 } 360 if (inhibit_formatting) { 361 p = in_buffer; 362 do 363 putc(*p, output); 364 while (*p++ != '\n'); 365 } 366 } 367 368 /* 369 * Copyright (C) 1976 by the Board of Trustees of the University of Illinois 370 * 371 * All rights reserved 372 * 373 * 374 * NAME: pad_output 375 * 376 * FUNCTION: Writes tabs and spaces to move the current column up to the desired 377 * position. 378 * 379 * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf. 380 * 381 * PARAMETERS: current integer The current column target 382 * nteger The desired column 383 * 384 * RETURNS: Integer value of the new column. (If current >= target, no action is 385 * taken, and current is returned. 386 * 387 * GLOBALS: None 388 * 389 * CALLS: write (sys) 390 * 391 * CALLED BY: dump_line 392 * 393 * HISTORY: initial coding November 1976 D A Willcox of CAC 394 * 395 */ 396 static int 397 pad_output(int current, int target) 398 /* writes tabs and blanks (if necessary) to 399 * get the current output position up to the 400 * target column */ 401 /* current: the current column value */ 402 /* target: position we want it at */ 403 { 404 int curr; /* internal column pointer */ 405 406 if (current >= target) 407 return (current); /* line is already long enough */ 408 curr = current; 409 if (opt.use_tabs) { 410 int tcur; 411 412 while ((tcur = opt.tabsize * (1 + (curr - 1) / opt.tabsize) + 1) <= target) { 413 putc('\t', output); 414 curr = tcur; 415 } 416 } 417 while (curr++ < target) 418 putc(' ', output); /* pad with final blanks */ 419 420 return (target); 421 } 422 423 /* 424 * Copyright (C) 1976 by the Board of Trustees of the University of Illinois 425 * 426 * All rights reserved 427 * 428 * 429 * NAME: count_spaces 430 * 431 * FUNCTION: Find out where printing of a given string will leave the current 432 * character position on output. 433 * 434 * ALGORITHM: Run thru input string and add appropriate values to current 435 * position. 436 * 437 * RETURNS: Integer value of position after printing "buffer" starting in column 438 * "current". 439 * 440 * HISTORY: initial coding November 1976 D A Willcox of CAC 441 * 442 */ 443 int 444 count_spaces_until(int cur, char *buffer, char *end) 445 /* 446 * this routine figures out where the character position will be after 447 * printing the text in buffer starting at column "current" 448 */ 449 { 450 char *buf; /* used to look thru buffer */ 451 452 for (buf = buffer; *buf != '\0' && buf != end; ++buf) { 453 switch (*buf) { 454 455 case '\n': 456 case 014: /* form feed */ 457 cur = 1; 458 break; 459 460 case '\t': 461 cur = opt.tabsize * (1 + (cur - 1) / opt.tabsize) + 1; 462 break; 463 464 case 010: /* backspace */ 465 --cur; 466 break; 467 468 default: 469 ++cur; 470 break; 471 } /* end of switch */ 472 } /* end of for loop */ 473 return (cur); 474 } 475 476 int 477 count_spaces(int cur, char *buffer) 478 { 479 return (count_spaces_until(cur, buffer, NULL)); 480 } 481 482 void 483 diag4(int level, const char *msg, int a, int b) 484 { 485 if (level) 486 found_err = 1; 487 if (output == stdout) { 488 fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no); 489 fprintf(stdout, msg, a, b); 490 fprintf(stdout, " */\n"); 491 } 492 else { 493 fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no); 494 fprintf(stderr, msg, a, b); 495 fprintf(stderr, "\n"); 496 } 497 } 498 499 void 500 diag3(int level, const char *msg, int a) 501 { 502 if (level) 503 found_err = 1; 504 if (output == stdout) { 505 fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no); 506 fprintf(stdout, msg, a); 507 fprintf(stdout, " */\n"); 508 } 509 else { 510 fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no); 511 fprintf(stderr, msg, a); 512 fprintf(stderr, "\n"); 513 } 514 } 515 516 void 517 diag2(int level, const char *msg) 518 { 519 if (level) 520 found_err = 1; 521 if (output == stdout) { 522 fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no); 523 fprintf(stdout, "%s", msg); 524 fprintf(stdout, " */\n"); 525 } 526 else { 527 fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no); 528 fprintf(stderr, "%s", msg); 529 fprintf(stderr, "\n"); 530 } 531 } 532 533