1 /* $OpenBSD: io.c,v 1.4 2001/01/08 07:14:42 pjanzen Exp $ */ 2 3 /* 4 * Copyright (c) 1985 Sun Microsystems, Inc. 5 * Copyright (c) 1980, 1993 The Regents of the University of California. 6 * Copyright (c) 1976 Board of Trustees of the University of Illinois. 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 #ifndef lint 39 /*static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93";*/ 40 static char rcsid[] = "$OpenBSD: io.c,v 1.4 2001/01/08 07:14:42 pjanzen Exp $"; 41 #endif /* not lint */ 42 43 #include <stdio.h> 44 #include <ctype.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <err.h> 48 #include "indent_globs.h" 49 50 51 int comment_open; 52 static int paren_target; 53 54 void 55 dump_line() 56 { /* dump_line is the routine that actually 57 * effects the printing of the new source. It 58 * prints the label section, followed by the 59 * code section with the appropriate nesting 60 * level, followed by any comments */ 61 int cur_col, target_col; 62 static int not_first_line; 63 64 if (ps.procname[0]) { 65 if (troff) { 66 if (comment_open) { 67 comment_open = 0; 68 fprintf(output, ".*/\n"); 69 } 70 fprintf(output, ".Pr \"%s\"\n", ps.procname); 71 } 72 ps.ind_level = 0; 73 ps.procname[0] = 0; 74 } 75 if (s_code == e_code && s_lab == e_lab && s_com == e_com) { 76 if (suppress_blanklines > 0) 77 suppress_blanklines--; 78 else { 79 ps.bl_line = true; 80 n_real_blanklines++; 81 } 82 } 83 else if (!inhibit_formatting) { 84 suppress_blanklines = 0; 85 ps.bl_line = false; 86 if (prefix_blankline_requested && not_first_line) { 87 if (swallow_optional_blanklines) { 88 if (n_real_blanklines == 1) 89 n_real_blanklines = 0; 90 } else { 91 if (n_real_blanklines == 0) 92 n_real_blanklines = 1; 93 } 94 } 95 while (--n_real_blanklines >= 0) 96 putc('\n', output); 97 n_real_blanklines = 0; 98 if (ps.ind_level == 0) 99 ps.ind_stmt = 0; /* this is a class A kludge. dont do 100 * additional statement indentation if we are 101 * at bracket level 0 */ 102 103 if (e_lab != s_lab || e_code != s_code) 104 ++code_lines; /* keep count of lines with code */ 105 106 107 if (e_lab != s_lab) { /* print lab, if any */ 108 if (comment_open) { 109 comment_open = 0; 110 fprintf(output, ".*/\n"); 111 } 112 while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t')) 113 e_lab--; 114 cur_col = pad_output(1, compute_label_target()); 115 if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0 116 || strncmp(s_lab, "#endif", 6) == 0)) { 117 char *s = s_lab; 118 if (e_lab[-1] == '\n') 119 e_lab--; 120 do 121 putc(*s++, output); 122 while (s < e_lab && 'a' <= *s && *s<='z'); 123 while ((*s == ' ' || *s == '\t') && s < e_lab) 124 s++; 125 if (s < e_lab) 126 fprintf(output, s[0]=='/' && s[1]=='*' ? "\t%.*s" : "\t/* %.*s */", 127 (int)(e_lab - s), s); 128 } 129 else fprintf(output, "%.*s", (int)(e_lab - s_lab), s_lab); 130 cur_col = count_spaces(cur_col, s_lab); 131 } 132 else 133 cur_col = 1; /* there is no label section */ 134 135 ps.pcase = false; 136 137 if (s_code != e_code) { /* print code section, if any */ 138 char *p; 139 140 if (comment_open) { 141 comment_open = 0; 142 fprintf(output, ".*/\n"); 143 } 144 target_col = compute_code_target(); 145 { 146 int i; 147 148 for (i = 0; i < ps.p_l_follow; i++) 149 if (ps.paren_indents[i] >= 0) 150 ps.paren_indents[i] = -(ps.paren_indents[i] + target_col); 151 } 152 cur_col = pad_output(cur_col, target_col); 153 for (p = s_code; p < e_code; p++) 154 if (*p == (char) 0200) 155 fprintf(output, "%d", target_col * 7); 156 else 157 putc(*p, output); 158 cur_col = count_spaces(cur_col, s_code); 159 } 160 if (s_com != e_com) { 161 if (troff) { 162 int all_here = 0; 163 char *p; 164 165 if (e_com[-1] == '/' && e_com[-2] == '*') 166 e_com -= 2, all_here++; 167 while (e_com > s_com && e_com[-1] == ' ') 168 e_com--; 169 *e_com = 0; 170 p = s_com; 171 while (*p == ' ') 172 p++; 173 if (p[0] == '/' && p[1] == '*') 174 p += 2, all_here++; 175 else if (p[0] == '*') 176 p += p[1] == '/' ? 2 : 1; 177 while (*p == ' ') 178 p++; 179 if (*p == 0) 180 goto inhibit_newline; 181 if (comment_open < 2 && ps.box_com) { 182 comment_open = 0; 183 fprintf(output, ".*/\n"); 184 } 185 if (comment_open == 0) { 186 if ('a' <= *p && *p <= 'z') 187 *p = *p + 'A' - 'a'; 188 if (e_com - p < 50 && all_here == 2) { 189 char *follow = p; 190 fprintf(output, "\n.nr C! \\w\1"); 191 while (follow < e_com) { 192 switch (*follow) { 193 case '\n': 194 putc(' ', output); 195 case 1: 196 break; 197 case '\\': 198 putc('\\', output); 199 default: 200 putc(*follow, output); 201 } 202 follow++; 203 } 204 putc(1, output); 205 } 206 fprintf(output, "\n./* %dp %d %dp\n", 207 ps.com_col * 7, 208 (s_code != e_code || s_lab != e_lab) - ps.box_com, 209 target_col * 7); 210 } 211 comment_open = 1 + ps.box_com; 212 while (*p) { 213 if (*p == BACKSLASH) 214 putc(BACKSLASH, output); 215 putc(*p++, output); 216 } 217 } else { /* print comment, if any */ 218 int target = ps.com_col; 219 char *com_st = s_com; 220 221 target += ps.comment_delta; 222 while (*com_st == '\t') 223 com_st++, target += 8; /* ? */ 224 while (target <= 0) 225 if (*com_st == ' ') 226 target++, com_st++; 227 else if (*com_st == '\t') 228 target = ((target - 1) & ~7) + 9, com_st++; 229 else 230 target = 1; 231 if (cur_col > target) { /* if comment cant fit on this line, 232 * put it on next line */ 233 putc('\n', output); 234 cur_col = 1; 235 ++ps.out_lines; 236 } 237 while (e_com > com_st && isspace(e_com[-1])) 238 e_com--; 239 cur_col = pad_output(cur_col, target); 240 if (!ps.box_com) { 241 if (star_comment_cont && (com_st[1] != '*' || e_com <= com_st + 1)) { 242 if (com_st[1] == ' ' && com_st[0] == ' ' && e_com > com_st + 1) 243 com_st[1] = '*'; 244 else 245 fwrite(" * ", com_st[0] == '\t' ? 2 : com_st[0] == '*' ? 1 : 3, 1, output); 246 } 247 } 248 fwrite(com_st, e_com - com_st, 1, output); 249 ps.comment_delta = ps.n_comment_delta; 250 cur_col = count_spaces(cur_col, com_st); 251 ++ps.com_lines; /* count lines with comments */ 252 } 253 } 254 if (ps.use_ff) 255 putc('\014', output); 256 else 257 putc('\n', output); 258 inhibit_newline: 259 ++ps.out_lines; 260 if (ps.just_saw_decl == 1 && blanklines_after_declarations) { 261 prefix_blankline_requested = 1; 262 ps.just_saw_decl = 0; 263 } 264 else 265 prefix_blankline_requested = postfix_blankline_requested; 266 postfix_blankline_requested = 0; 267 } 268 ps.decl_on_line = ps.in_decl; /* if we are in the middle of a 269 * declaration, remember that fact for 270 * proper comment indentation */ 271 ps.ind_stmt = ps.in_stmt & ~ps.in_decl; /* next line should be 272 * indented if we have not 273 * completed this stmt and if 274 * we are not in the middle of 275 * a declaration */ 276 ps.use_ff = false; 277 ps.dumped_decl_indent = 0; 278 *(e_lab = s_lab) = '\0'; /* reset buffers */ 279 *(e_code = s_code) = '\0'; 280 *(e_com = s_com) = '\0'; 281 ps.ind_level = ps.i_l_follow; 282 ps.paren_level = ps.p_l_follow; 283 paren_target = -ps.paren_indents[ps.paren_level - 1]; 284 not_first_line = 1; 285 return; 286 } 287 288 int 289 compute_code_target() 290 { 291 int target_col; 292 293 target_col = ps.ind_size * ps.ind_level + 1; 294 if (ps.paren_level) 295 if (!lineup_to_parens) 296 target_col += continuation_indent * ps.paren_level; 297 else { 298 int w; 299 int t = paren_target; 300 301 if ((w = count_spaces(t, s_code) - max_col) > 0 302 && count_spaces(target_col, s_code) <= max_col) { 303 t -= w + 1; 304 if (t > target_col) 305 target_col = t; 306 } 307 else 308 target_col = t; 309 } 310 else if (ps.ind_stmt) 311 target_col += continuation_indent; 312 return target_col; 313 } 314 315 int 316 compute_label_target() 317 { 318 return 319 ps.pcase ? (int) (case_ind * ps.ind_size) + 1 320 : *s_lab == '#' ? 1 321 : ps.ind_size * (ps.ind_level - label_offset) + 1; 322 } 323 324 325 /* 326 * Copyright (C) 1976 by the Board of Trustees of the University of Illinois 327 * 328 * All rights reserved 329 * 330 * 331 * NAME: fill_buffer 332 * 333 * FUNCTION: Reads one block of input into input_buffer 334 * 335 * HISTORY: initial coding November 1976 D A Willcox of CAC 1/7/77 A 336 * Willcox of CAC Added check for switch back to partly full input 337 * buffer from temporary buffer 338 * 339 */ 340 void 341 fill_buffer() 342 { /* this routine reads stuff from the input */ 343 char *p; 344 int i; 345 FILE *f = input; 346 347 if (bp_save != 0) { /* there is a partly filled input buffer left */ 348 buf_ptr = bp_save; /* dont read anything, just switch buffers */ 349 buf_end = be_save; 350 bp_save = be_save = 0; 351 if (buf_ptr < buf_end) 352 return; /* only return if there is really something in 353 * this buffer */ 354 } 355 for (p = in_buffer;;) { 356 if (p >= in_buffer_limit) { 357 int size = (in_buffer_limit - in_buffer) * 2 + 10; 358 int offset = p - in_buffer; 359 in_buffer = (char *) realloc(in_buffer, size); 360 if (in_buffer == NULL) 361 errx(1, "input line too long"); 362 p = in_buffer + offset; 363 in_buffer_limit = in_buffer + size - 2; 364 } 365 if ((i = getc(f)) == EOF) { 366 *p++ = ' '; 367 *p++ = '\n'; 368 had_eof = true; 369 break; 370 } 371 *p++ = i; 372 if (i == '\n') 373 break; 374 } 375 buf_ptr = in_buffer; 376 buf_end = p; 377 if (p[-2] == '/' && p[-3] == '*') { 378 if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0) 379 fill_buffer(); /* flush indent error message */ 380 else { 381 int com = 0; 382 383 p = in_buffer; 384 while (*p == ' ' || *p == '\t') 385 p++; 386 if (*p == '/' && p[1] == '*') { 387 p += 2; 388 while (*p == ' ' || *p == '\t') 389 p++; 390 if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E' 391 && p[4] == 'N' && p[5] == 'T') { 392 p += 6; 393 while (*p == ' ' || *p == '\t') 394 p++; 395 if (*p == '*') 396 com = 1; 397 else if (*p == 'O') { 398 if (*++p == 'N') 399 p++, com = 1; 400 else if (*p == 'F' && *++p == 'F') 401 p++, com = 2; 402 } 403 while (*p == ' ' || *p == '\t') 404 p++; 405 if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) { 406 if (s_com != e_com || s_lab != e_lab || s_code != e_code) 407 dump_line(); 408 if (!(inhibit_formatting = com - 1)) { 409 n_real_blanklines = 0; 410 postfix_blankline_requested = 0; 411 prefix_blankline_requested = 0; 412 suppress_blanklines = 1; 413 } 414 } 415 } 416 } 417 } 418 } 419 if (inhibit_formatting) { 420 p = in_buffer; 421 do 422 putc(*p, output); 423 while (*p++ != '\n'); 424 } 425 return; 426 } 427 428 /* 429 * Copyright (C) 1976 by the Board of Trustees of the University of Illinois 430 * 431 * All rights reserved 432 * 433 * 434 * NAME: pad_output 435 * 436 * FUNCTION: Writes tabs and spaces to move the current column up to the desired 437 * position. 438 * 439 * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf. 440 * 441 * PARAMETERS: current integer The current column target 442 * nteger The desired column 443 * 444 * RETURNS: Integer value of the new column. (If current >= target, no action is 445 * taken, and current is returned. 446 * 447 * GLOBALS: None 448 * 449 * CALLS: write (sys) 450 * 451 * CALLED BY: dump_line 452 * 453 * HISTORY: initial coding November 1976 D A Willcox of CAC 454 * 455 */ 456 int 457 pad_output(current, target) /* writes tabs and blanks (if necessary) to 458 * get the current output position up to the 459 * target column */ 460 int current; /* the current column value */ 461 int target; /* position we want it at */ 462 { 463 int curr; /* internal column pointer */ 464 int tcur; 465 466 if (troff) 467 fprintf(output, "\\h'|%dp'", (target - 1) * 7); 468 else { 469 if (current >= target) 470 return (current); /* line is already long enough */ 471 curr = current; 472 while ((tcur = ((curr - 1) & tabmask) + tabsize + 1) <= target) { 473 putc('\t', output); 474 curr = tcur; 475 } 476 while (curr++ < target) 477 putc(' ', output); /* pad with final blanks */ 478 } 479 return (target); 480 } 481 482 /* 483 * Copyright (C) 1976 by the Board of Trustees of the University of Illinois 484 * 485 * All rights reserved 486 * 487 * 488 * NAME: count_spaces 489 * 490 * FUNCTION: Find out where printing of a given string will leave the current 491 * character position on output. 492 * 493 * ALGORITHM: Run thru input string and add appropriate values to current 494 * position. 495 * 496 * RETURNS: Integer value of position after printing "buffer" starting in column 497 * "current". 498 * 499 * HISTORY: initial coding November 1976 D A Willcox of CAC 500 * 501 */ 502 int 503 count_spaces(current, buffer) 504 /* 505 * this routine figures out where the character position will be after 506 * printing the text in buffer starting at column "current" 507 */ 508 int current; 509 char *buffer; 510 { 511 char *buf; /* used to look thru buffer */ 512 int cur; /* current character counter */ 513 514 cur = current; 515 516 for (buf = buffer; *buf != '\0'; ++buf) { 517 switch (*buf) { 518 519 case '\n': 520 case 014: /* form feed */ 521 cur = 1; 522 break; 523 524 case '\t': 525 cur = ((cur - 1) & tabmask) + tabsize + 1; 526 break; 527 528 case 010: /* backspace */ 529 --cur; 530 break; 531 532 default: 533 ++cur; 534 break; 535 } /* end of switch */ 536 } /* end of for loop */ 537 return (cur); 538 } 539 540 #if __STDC__ 541 #include <stdarg.h> 542 #else 543 #include <varargs.h> 544 #endif 545 546 int found_err; 547 548 /* VARARGS2 */ 549 void 550 #if __STDC__ 551 diag(int level, char *msg, ...) 552 #else 553 diag(level, msg, va_alist) 554 int level; 555 char *msg; 556 va_dcl 557 #endif 558 { 559 va_list ap; 560 #if __STDC__ 561 va_start(ap, msg); 562 #else 563 va_start(ap); 564 #endif 565 566 if (level) 567 found_err = 1; 568 if (output == stdout) { 569 fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no); 570 vfprintf(stdout, msg, ap); 571 fprintf(stdout, " */\n"); 572 } 573 else { 574 fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no); 575 vfprintf(stderr, msg, ap); 576 fprintf(stderr, "\n"); 577 } 578 va_end(ap); 579 } 580 581 void 582 writefdef(f, nm) 583 struct fstate *f; 584 int nm; 585 { 586 fprintf(output, ".ds f%c %s\n.nr s%c %d\n", 587 nm, f->font, nm, f->size); 588 } 589 590 char * 591 chfont(of, nf, s) 592 struct fstate *of, *nf; 593 char *s; 594 { 595 if (of->font[0] != nf->font[0] 596 || of->font[1] != nf->font[1]) { 597 *s++ = '\\'; 598 *s++ = 'f'; 599 if (nf->font[1]) { 600 *s++ = '('; 601 *s++ = nf->font[0]; 602 *s++ = nf->font[1]; 603 } 604 else 605 *s++ = nf->font[0]; 606 } 607 if (nf->size != of->size) { 608 *s++ = '\\'; 609 *s++ = 's'; 610 if (nf->size < of->size) { 611 *s++ = '-'; 612 *s++ = '0' + of->size - nf->size; 613 } 614 else { 615 *s++ = '+'; 616 *s++ = '0' + nf->size - of->size; 617 } 618 } 619 return s; 620 } 621 622 void 623 parsefont(f, s0) 624 struct fstate *f; 625 char *s0; 626 { 627 char *s = s0; 628 int sizedelta = 0; 629 bzero(f, sizeof *f); 630 while (*s) { 631 if (isdigit(*s)) 632 f->size = f->size * 10 + *s - '0'; 633 else if (isupper(*s)) 634 if (f->font[0]) 635 f->font[1] = *s; 636 else 637 f->font[0] = *s; 638 else if (*s == 'c') 639 f->allcaps = 1; 640 else if (*s == '+') 641 sizedelta++; 642 else if (*s == '-') 643 sizedelta--; 644 else { 645 fprintf(stderr, "indent: bad font specification: %s\n", s0); 646 exit(1); 647 } 648 s++; 649 } 650 if (f->font[0] == 0) 651 f->font[0] = 'R'; 652 if (bodyf.size == 0) 653 bodyf.size = 11; 654 if (f->size == 0) 655 f->size = bodyf.size + sizedelta; 656 else if (sizedelta > 0) 657 f->size += bodyf.size; 658 else 659 f->size = bodyf.size - f->size; 660 } 661