1 /* $OpenBSD: indent.c,v 1.10 2001/06/25 04:58:31 pjanzen Exp $ */ 2 3 /* 4 * Copyright (c) 1980, 1993 5 * The Regents of the University of California. 6 * Copyright (c) 1976 Board of Trustees of the University of Illinois. 7 * Copyright (c) 1985 Sun Microsystems, Inc. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #ifndef lint 40 char copyright[] = 41 "@(#) Copyright (c) 1985 Sun Microsystems, Inc.\n\ 42 @(#) Copyright (c) 1980, 1993\n\ 43 The Regents of the University of California.\n\ 44 @(#) Copyright (c) 1976 Board of Trustees of the University of Illinois.\n\ 45 All rights reserved.\n"; 46 #endif /* not lint */ 47 48 #ifndef lint 49 /*static char sccsid[] = "@(#)indent.c 5.17 (Berkeley) 6/7/93";*/ 50 static char rcsid[] = "$OpenBSD: indent.c,v 1.10 2001/06/25 04:58:31 pjanzen Exp $"; 51 #endif /* not lint */ 52 53 #include <sys/param.h> 54 #include <fcntl.h> 55 #include <unistd.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include "indent_globs.h" 60 #include "indent_codes.h" 61 #include <ctype.h> 62 #include <errno.h> 63 #include <err.h> 64 65 char *in_name = "Standard Input"; /* will always point to name of input 66 * file */ 67 char *out_name = "Standard Output"; /* will always point to name 68 * of output file */ 69 char bakfile[MAXPATHLEN] = ""; 70 71 void bakcopy(); 72 73 int 74 main(argc, argv) 75 int argc; 76 char **argv; 77 { 78 79 extern int found_err; /* flag set in diag() on error */ 80 int dec_ind; /* current indentation for declarations */ 81 int di_stack[20]; /* a stack of structure indentation levels */ 82 int flushed_nl; /* used when buffering up comments to remember 83 * that a newline was passed over */ 84 int force_nl; /* when true, code must be broken */ 85 int hd_type; /* used to store type of stmt for if (...), 86 * for (...), etc */ 87 register int i; /* local loop counter */ 88 int scase; /* set to true when we see a case, so we will 89 * know what to do with the following colon */ 90 int sp_sw; /* when true, we are in the expressin of 91 * if(...), while(...), etc. */ 92 int squest; /* when this is positive, we have seen a ? 93 * without the matching : in a <c>?<s>:<s> 94 * construct */ 95 register char *t_ptr; /* used for copying tokens */ 96 int type_code; /* the type of token, returned by lexi */ 97 98 int last_else = 0; /* true iff last keyword was an else */ 99 100 101 /*-----------------------------------------------*\ 102 | INITIALIZATION | 103 \*-----------------------------------------------*/ 104 105 106 hd_type = 0; 107 ps.p_stack[0] = stmt; /* this is the parser's stack */ 108 ps.last_nl = true; /* this is true if the last thing scanned was 109 * a newline */ 110 ps.last_token = semicolon; 111 combuf = (char *) malloc(bufsize); 112 labbuf = (char *) malloc(bufsize); 113 codebuf = (char *) malloc(bufsize); 114 tokenbuf = (char *) malloc(bufsize); 115 if (combuf == NULL || labbuf == NULL || codebuf == NULL || 116 tokenbuf == NULL) 117 err(1, NULL); 118 l_com = combuf + bufsize - 5; 119 l_lab = labbuf + bufsize - 5; 120 l_code = codebuf + bufsize - 5; 121 l_token = tokenbuf + bufsize - 5; 122 combuf[0] = codebuf[0] = labbuf[0] = ' '; /* set up code, label, and 123 * comment buffers */ 124 combuf[1] = codebuf[1] = labbuf[1] = '\0'; 125 ps.else_if = 1; /* Default else-if special processing to on */ 126 s_lab = e_lab = labbuf + 1; 127 s_code = e_code = codebuf + 1; 128 s_com = e_com = combuf + 1; 129 s_token = e_token = tokenbuf + 1; 130 131 in_buffer = (char *) malloc(10); 132 if (in_buffer == NULL) 133 err(1, NULL); 134 in_buffer_limit = in_buffer + 8; 135 buf_ptr = buf_end = in_buffer; 136 line_no = 1; 137 had_eof = ps.in_decl = ps.decl_on_line = break_comma = false; 138 sp_sw = force_nl = false; 139 ps.in_or_st = false; 140 ps.bl_line = true; 141 dec_ind = 0; 142 di_stack[ps.dec_nest = 0] = 0; 143 ps.want_blank = ps.in_stmt = ps.ind_stmt = false; 144 145 146 scase = ps.pcase = false; 147 squest = 0; 148 sc_end = 0; 149 bp_save = 0; 150 be_save = 0; 151 152 output = 0; 153 154 155 156 /*--------------------------------------------------*\ 157 | COMMAND LINE SCAN | 158 \*--------------------------------------------------*/ 159 160 #ifdef undef 161 max_col = 78; /* -l78 */ 162 lineup_to_parens = 1; /* -lp */ 163 ps.ljust_decl = 0; /* -ndj */ 164 ps.com_ind = 33; /* -c33 */ 165 star_comment_cont = 1; /* -sc */ 166 ps.ind_size = 8; /* -i8 */ 167 verbose = 0; 168 ps.decl_indent = 16; /* -di16 */ 169 ps.indent_parameters = 1; /* -ip */ 170 ps.decl_com_ind = 0; /* if this is not set to some positive value 171 * by an arg, we will set this equal to 172 * ps.com_ind */ 173 btype_2 = 1; /* -br */ 174 cuddle_else = 1; /* -ce */ 175 ps.unindent_displace = 0; /* -d0 */ 176 ps.case_indent = 0; /* -cli0 */ 177 format_col1_comments = 1; /* -fc1 */ 178 procnames_start_line = 1; /* -psl */ 179 proc_calls_space = 0; /* -npcs */ 180 comment_delimiter_on_blankline = 1; /* -cdb */ 181 ps.leave_comma = 1; /* -nbc */ 182 #endif 183 184 for (i = 1; i < argc; ++i) 185 if (strcmp(argv[i], "-npro") == 0) 186 break; 187 set_defaults(); 188 if (i >= argc) 189 set_profile(); 190 191 for (i = 1; i < argc; ++i) { 192 193 /* 194 * look thru args (if any) for changes to defaults 195 */ 196 if (argv[i][0] != '-') {/* no flag on parameter */ 197 if (input == 0) { /* we must have the input file */ 198 in_name = argv[i]; /* remember name of input file */ 199 input = fopen(in_name, "r"); 200 if (input == NULL) /* check for open error */ 201 err(1, "%s", in_name); 202 continue; 203 } 204 else if (output == 0) { /* we have the output file */ 205 out_name = argv[i]; /* remember name of output file */ 206 if (strcmp(in_name, out_name) == 0) /* attempt to overwrite 207 * the file */ 208 errx(1, "input and output files must be different"); 209 output = fopen(out_name, "w"); 210 if (output == NULL) /* check for create error */ 211 err(1, "%s", out_name); 212 continue; 213 } 214 errx(1, "unknown parameter: %s", argv[i]); 215 } 216 else 217 set_option(argv[i]); 218 } /* end of for */ 219 if (input == NULL) { 220 fprintf(stderr, "usage: indent file [ outfile ] [ options ]\n"); 221 exit(1); 222 } 223 if (output == NULL) { 224 if (troff) 225 output = stdout; 226 else { 227 out_name = in_name; 228 bakcopy(); 229 } 230 } 231 if (ps.com_ind <= 1) 232 ps.com_ind = 2; /* dont put normal comments before column 2 */ 233 if (troff) { 234 if (bodyf.font[0] == 0) 235 parsefont(&bodyf, "R"); 236 if (scomf.font[0] == 0) 237 parsefont(&scomf, "I"); 238 if (blkcomf.font[0] == 0) 239 blkcomf = scomf, blkcomf.size += 2; 240 if (boxcomf.font[0] == 0) 241 boxcomf = blkcomf; 242 if (stringf.font[0] == 0) 243 parsefont(&stringf, "L"); 244 if (keywordf.font[0] == 0) 245 parsefont(&keywordf, "B"); 246 writefdef(&bodyf, 'B'); 247 writefdef(&scomf, 'C'); 248 writefdef(&blkcomf, 'L'); 249 writefdef(&boxcomf, 'X'); 250 writefdef(&stringf, 'S'); 251 writefdef(&keywordf, 'K'); 252 } 253 if (block_comment_max_col <= 0) 254 block_comment_max_col = max_col; 255 if (ps.decl_com_ind <= 0) /* if not specified by user, set this */ 256 ps.decl_com_ind = ps.ljust_decl ? (ps.com_ind <= 10 ? 2 : ps.com_ind - 8) : ps.com_ind; 257 if (continuation_indent == 0) 258 continuation_indent = ps.ind_size; 259 fill_buffer(); /* get first batch of stuff into input buffer */ 260 261 parse(semicolon); 262 { 263 char *p = buf_ptr; 264 int col = 1; 265 266 while (1) { 267 if (*p == ' ') 268 col++; 269 else if (*p == '\t') 270 col = ((col - 1) & ~7) + 9; 271 else 272 break; 273 p++; 274 } 275 if (col > ps.ind_size) 276 ps.ind_level = ps.i_l_follow = col / ps.ind_size; 277 } 278 if (troff) { 279 register char *p = in_name, 280 *beg = in_name; 281 282 while (*p) 283 if (*p++ == '/') 284 beg = p; 285 fprintf(output, ".Fn \"%s\"\n", beg); 286 } 287 /* 288 * START OF MAIN LOOP 289 */ 290 291 while (1) { /* this is the main loop. it will go until we 292 * reach eof */ 293 int is_procname; 294 295 type_code = lexi(); /* lexi reads one token. The actual 296 * characters read are stored in "token". lexi 297 * returns a code indicating the type of token */ 298 is_procname = ps.procname[0]; 299 300 /* 301 * The following code moves everything following an if (), while (), 302 * else, etc. up to the start of the following stmt to a buffer. This 303 * allows proper handling of both kinds of brace placement. 304 */ 305 306 flushed_nl = false; 307 while (ps.search_brace) { /* if we scanned an if(), while(), 308 * etc., we might need to copy stuff 309 * into a buffer we must loop, copying 310 * stuff into save_com, until we find 311 * the start of the stmt which follows 312 * the if, or whatever */ 313 switch (type_code) { 314 case newline: 315 ++line_no; 316 flushed_nl = true; 317 case form_feed: 318 break; /* form feeds and newlines found here will be 319 * ignored */ 320 321 case lbrace: /* this is a brace that starts the compound 322 * stmt */ 323 if (sc_end == 0) { /* ignore buffering if a comment wasnt 324 * stored up */ 325 ps.search_brace = false; 326 goto check_type; 327 } 328 if (btype_2) { 329 save_com[0] = '{'; /* we either want to put the brace 330 * right after the if */ 331 goto sw_buffer; /* go to common code to get out of 332 * this loop */ 333 } 334 case comment: /* we have a comment, so we must copy it into 335 * the buffer */ 336 if (!flushed_nl || sc_end != 0) { 337 if (sc_end == 0) { /* if this is the first comment, we 338 * must set up the buffer */ 339 save_com[0] = save_com[1] = ' '; 340 sc_end = &(save_com[2]); 341 } 342 else { 343 *sc_end++ = '\n'; /* add newline between 344 * comments */ 345 *sc_end++ = ' '; 346 --line_no; 347 } 348 *sc_end++ = '/'; /* copy in start of comment */ 349 *sc_end++ = '*'; 350 351 for (;;) { /* loop until we get to the end of the comment */ 352 *sc_end = *buf_ptr++; 353 if (buf_ptr >= buf_end) 354 fill_buffer(); 355 356 if (*sc_end++ == '*' && *buf_ptr == '/') 357 break; /* we are at end of comment */ 358 359 if (sc_end >= &(save_com[sc_size])) { /* check for temp buffer 360 * overflow */ 361 diag(1, "Internal buffer overflow - Move big comment from right after if, while, or whatever."); 362 fflush(output); 363 exit(1); 364 } 365 } 366 *sc_end++ = '/'; /* add ending slash */ 367 if (++buf_ptr >= buf_end) /* get past / in buffer */ 368 fill_buffer(); 369 break; 370 } 371 default: /* it is the start of a normal statment */ 372 if (flushed_nl) /* if we flushed a newline, make sure it is 373 * put back */ 374 force_nl = true; 375 if ((type_code == sp_paren && *token == 'i' 376 && last_else && ps.else_if) || 377 (type_code == sp_nparen && *token == 'e' 378 && e_code != s_code && e_code[-1] == '}')) 379 force_nl = false; 380 381 if (sc_end == 0) { /* ignore buffering if comment wasnt 382 * saved up */ 383 ps.search_brace = false; 384 goto check_type; 385 } 386 if (force_nl) { /* if we should insert a nl here, put it into 387 * the buffer */ 388 force_nl = false; 389 --line_no; /* this will be re-increased when the nl is 390 * read from the buffer */ 391 *sc_end++ = '\n'; 392 *sc_end++ = ' '; 393 if (verbose && !flushed_nl) /* print error msg if the line 394 * was not already broken */ 395 diag(0, "Line broken"); 396 flushed_nl = false; 397 } 398 for (t_ptr = token; *t_ptr; ++t_ptr) 399 *sc_end++ = *t_ptr; /* copy token into temp buffer */ 400 ps.procname[0] = 0; 401 402 sw_buffer: 403 ps.search_brace = false; /* stop looking for start of 404 * stmt */ 405 bp_save = buf_ptr; /* save current input buffer */ 406 be_save = buf_end; 407 buf_ptr = save_com; /* fix so that subsequent calls to 408 * lexi will take tokens out of 409 * save_com */ 410 *sc_end++ = ' ';/* add trailing blank, just in case */ 411 buf_end = sc_end; 412 sc_end = 0; 413 break; 414 } /* end of switch */ 415 if (type_code != 0) /* we must make this check, just in case there 416 * was an unexpected EOF */ 417 type_code = lexi(); /* read another token */ 418 /* if (ps.search_brace) ps.procname[0] = 0; */ 419 if ((is_procname = ps.procname[0]) && flushed_nl 420 && !procnames_start_line && ps.in_decl 421 && type_code == ident) 422 flushed_nl = 0; 423 } /* end of while (search_brace) */ 424 last_else = 0; 425 check_type: 426 if (type_code == 0) { /* we got eof */ 427 if (s_lab != e_lab || s_code != e_code 428 || s_com != e_com) /* must dump end of line */ 429 dump_line(); 430 if (ps.tos > 1) /* check for balanced braces */ 431 diag(1, "Stuff missing from end of file."); 432 433 if (verbose) { 434 printf("There were %d output lines and %d comments\n", 435 ps.out_lines, ps.out_coms); 436 printf("(Lines with comments)/(Lines with code): %6.3f\n", 437 (1.0 * ps.com_lines) / code_lines); 438 } 439 fflush(output); 440 exit(found_err); 441 } 442 if ( 443 (type_code != comment) && 444 (type_code != newline) && 445 (type_code != preesc) && 446 (type_code != form_feed)) { 447 if (force_nl && 448 (type_code != semicolon) && 449 (type_code != lbrace || !btype_2)) { 450 /* we should force a broken line here */ 451 if (verbose && !flushed_nl) 452 diag(0, "Line broken"); 453 flushed_nl = false; 454 dump_line(); 455 ps.want_blank = false; /* dont insert blank at line start */ 456 force_nl = false; 457 } 458 ps.in_stmt = true; /* turn on flag which causes an extra level of 459 * indentation. this is turned off by a ; or 460 * '}' */ 461 if (s_com != e_com) { /* the turkey has embedded a comment 462 * in a line. fix it */ 463 *e_code++ = ' '; 464 for (t_ptr = s_com; *t_ptr; ++t_ptr) { 465 CHECK_SIZE_CODE; 466 *e_code++ = *t_ptr; 467 } 468 *e_code++ = ' '; 469 *e_code = '\0'; /* null terminate code sect */ 470 ps.want_blank = false; 471 e_com = s_com; 472 } 473 } 474 else if (type_code != comment) /* preserve force_nl thru a comment */ 475 force_nl = false; /* cancel forced newline after newline, form 476 * feed, etc */ 477 478 479 480 /*-----------------------------------------------------*\ 481 | do switch on type of token scanned | 482 \*-----------------------------------------------------*/ 483 CHECK_SIZE_CODE; 484 switch (type_code) { /* now, decide what to do with the token */ 485 486 case form_feed: /* found a form feed in line */ 487 ps.use_ff = true; /* a form feed is treated much like a newline */ 488 dump_line(); 489 ps.want_blank = false; 490 break; 491 492 case newline: 493 if (ps.last_token != comma || ps.p_l_follow > 0 494 || !ps.leave_comma || ps.block_init || !break_comma || s_com != e_com) { 495 dump_line(); 496 ps.want_blank = false; 497 } 498 ++line_no; /* keep track of input line number */ 499 break; 500 501 case lparen: /* got a '(' or '[' */ 502 ++ps.p_l_follow; /* count parens to make Healy happy */ 503 if (ps.want_blank && *token != '[' && 504 (ps.last_token != ident || proc_calls_space 505 || (ps.its_a_keyword && (!ps.sizeof_keyword || Bill_Shannon)))) 506 *e_code++ = ' '; 507 if (ps.in_decl && !ps.block_init) 508 if (troff && !ps.dumped_decl_indent && !is_procname && ps.last_token == decl) { 509 ps.dumped_decl_indent = 1; 510 sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token); 511 e_code += strlen(e_code); 512 } 513 else { 514 while ((e_code - s_code) < dec_ind) { 515 CHECK_SIZE_CODE; 516 *e_code++ = ' '; 517 } 518 *e_code++ = token[0]; 519 } 520 else 521 *e_code++ = token[0]; 522 ps.paren_indents[ps.p_l_follow - 1] = e_code - s_code; 523 if (sp_sw && ps.p_l_follow == 1 && extra_expression_indent 524 && ps.paren_indents[0] < 2 * ps.ind_size) 525 ps.paren_indents[0] = 2 * ps.ind_size; 526 ps.want_blank = false; 527 if (ps.in_or_st && *token == '(' && ps.tos <= 2) { 528 /* 529 * this is a kluge to make sure that declarations will be 530 * aligned right if proc decl has an explicit type on it, i.e. 531 * "int a(x) {..." 532 */ 533 parse(semicolon); /* I said this was a kluge... */ 534 ps.in_or_st = false; /* turn off flag for structure decl or 535 * initialization */ 536 } 537 if (ps.sizeof_keyword) 538 ps.sizeof_mask |= 1 << ps.p_l_follow; 539 break; 540 541 case rparen: /* got a ')' or ']' */ 542 rparen_count--; 543 if (ps.cast_mask & (1 << ps.p_l_follow) & ~ps.sizeof_mask) { 544 ps.last_u_d = true; 545 ps.cast_mask &= (1 << ps.p_l_follow) - 1; 546 } 547 ps.sizeof_mask &= (1 << ps.p_l_follow) - 1; 548 if (--ps.p_l_follow < 0) { 549 ps.p_l_follow = 0; 550 diag(0, "Extra %c", *token); 551 } 552 if (e_code == s_code) /* if the paren starts the line */ 553 ps.paren_level = ps.p_l_follow; /* then indent it */ 554 555 *e_code++ = token[0]; 556 ps.want_blank = true; 557 558 if (sp_sw && (ps.p_l_follow == 0)) { /* check for end of if 559 * (...), or some such */ 560 sp_sw = false; 561 force_nl = true;/* must force newline after if */ 562 ps.last_u_d = true; /* inform lexi that a following 563 * operator is unary */ 564 ps.in_stmt = false; /* dont use stmt continuation 565 * indentation */ 566 567 parse(hd_type); /* let parser worry about if, or whatever */ 568 } 569 ps.search_brace = btype_2; /* this should insure that constructs 570 * such as main(){...} and int[]{...} 571 * have their braces put in the right 572 * place */ 573 break; 574 575 case unary_op: /* this could be any unary operation */ 576 if (ps.want_blank) 577 *e_code++ = ' '; 578 579 if (troff && !ps.dumped_decl_indent && ps.in_decl && !is_procname) { 580 sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token); 581 ps.dumped_decl_indent = 1; 582 e_code += strlen(e_code); 583 } 584 else { 585 char *res = token; 586 587 if (ps.in_decl && !ps.block_init) { /* if this is a unary op 588 * in a declaration, we 589 * should indent this 590 * token */ 591 for (i = 0; token[i]; ++i); /* find length of token */ 592 while ((e_code - s_code) < (dec_ind - i)) { 593 CHECK_SIZE_CODE; 594 *e_code++ = ' '; /* pad it */ 595 } 596 } 597 if (troff && token[0] == '-' && token[1] == '>') 598 res = "\\(->"; 599 for (t_ptr = res; *t_ptr; ++t_ptr) { 600 CHECK_SIZE_CODE; 601 *e_code++ = *t_ptr; 602 } 603 } 604 ps.want_blank = false; 605 break; 606 607 case binary_op: /* any binary operation */ 608 if (ps.want_blank) 609 *e_code++ = ' '; 610 { 611 char *res = token; 612 613 if (troff) 614 switch (token[0]) { 615 case '<': 616 if (token[1] == '=') 617 res = "\\(<="; 618 break; 619 case '>': 620 if (token[1] == '=') 621 res = "\\(>="; 622 break; 623 case '!': 624 if (token[1] == '=') 625 res = "\\(!="; 626 break; 627 case '|': 628 if (token[1] == '|') 629 res = "\\(br\\(br"; 630 else if (token[1] == 0) 631 res = "\\(br"; 632 break; 633 } 634 for (t_ptr = res; *t_ptr; ++t_ptr) { 635 CHECK_SIZE_CODE; 636 *e_code++ = *t_ptr; /* move the operator */ 637 } 638 } 639 ps.want_blank = true; 640 break; 641 642 case postop: /* got a trailing ++ or -- */ 643 *e_code++ = token[0]; 644 *e_code++ = token[1]; 645 ps.want_blank = true; 646 break; 647 648 case question: /* got a ? */ 649 squest++; /* this will be used when a later colon 650 * appears so we can distinguish the 651 * <c>?<n>:<n> construct */ 652 if (ps.want_blank) 653 *e_code++ = ' '; 654 *e_code++ = '?'; 655 ps.want_blank = true; 656 break; 657 658 case casestmt: /* got word 'case' or 'default' */ 659 scase = true; /* so we can process the later colon properly */ 660 goto copy_id; 661 662 case colon: /* got a ':' */ 663 if (squest > 0) { /* it is part of the <c>?<n>: <n> construct */ 664 --squest; 665 if (ps.want_blank) 666 *e_code++ = ' '; 667 *e_code++ = ':'; 668 ps.want_blank = true; 669 break; 670 } 671 if (ps.in_decl) { 672 *e_code++ = ':'; 673 ps.want_blank = false; 674 break; 675 } 676 ps.in_stmt = false; /* seeing a label does not imply we are in a 677 * stmt */ 678 for (t_ptr = s_code; *t_ptr; ++t_ptr) 679 *e_lab++ = *t_ptr; /* turn everything so far into a label */ 680 e_code = s_code; 681 *e_lab++ = ':'; 682 *e_lab++ = ' '; 683 *e_lab = '\0'; 684 685 force_nl = ps.pcase = scase; /* ps.pcase will be used by 686 * dump_line to decide how to 687 * indent the label. force_nl 688 * will force a case n: to be 689 * on a line by itself */ 690 scase = false; 691 ps.want_blank = false; 692 break; 693 694 case semicolon: /* got a ';' */ 695 ps.in_or_st = false;/* we are not in an initialization or 696 * structure declaration */ 697 scase = false; /* these will only need resetting in a error */ 698 squest = 0; 699 if (ps.last_token == rparen && rparen_count == 0) 700 ps.in_parameter_declaration = 0; 701 ps.cast_mask = 0; 702 ps.sizeof_mask = 0; 703 ps.block_init = 0; 704 ps.block_init_level = 0; 705 ps.just_saw_decl--; 706 707 if (ps.in_decl && s_code == e_code && !ps.block_init) 708 while ((e_code - s_code) < (dec_ind - 1)) { 709 CHECK_SIZE_CODE; 710 *e_code++ = ' '; 711 } 712 713 ps.in_decl = (ps.dec_nest > 0); /* if we were in a first level 714 * structure declaration, we 715 * arent any more */ 716 717 if ((!sp_sw || hd_type != forstmt) && ps.p_l_follow > 0) { 718 719 /* 720 * This should be true iff there were unbalanced parens in the 721 * stmt. It is a bit complicated, because the semicolon might 722 * be in a for stmt 723 */ 724 diag(1, "Unbalanced parens"); 725 ps.p_l_follow = 0; 726 if (sp_sw) { /* this is a check for a if, while, etc. with 727 * unbalanced parens */ 728 sp_sw = false; 729 parse(hd_type); /* dont lose the if, or whatever */ 730 } 731 } 732 *e_code++ = ';'; 733 ps.want_blank = true; 734 ps.in_stmt = (ps.p_l_follow > 0); /* we are no longer in the 735 * middle of a stmt */ 736 737 if (!sp_sw) { /* if not if for (;;) */ 738 parse(semicolon); /* let parser know about end of stmt */ 739 force_nl = true;/* force newline after a end of stmt */ 740 } 741 break; 742 743 case lbrace: /* got a '{' */ 744 ps.in_stmt = false; /* dont indent the {} */ 745 if (!ps.block_init) 746 force_nl = true;/* force other stuff on same line as '{' onto 747 * new line */ 748 else if (ps.block_init_level <= 0) 749 ps.block_init_level = 1; 750 else 751 ps.block_init_level++; 752 753 if (s_code != e_code && !ps.block_init) { 754 if (!btype_2) { 755 dump_line(); 756 ps.want_blank = false; 757 } 758 else if (ps.in_parameter_declaration && !ps.in_or_st) { 759 ps.i_l_follow = 0; 760 dump_line(); 761 ps.want_blank = false; 762 } 763 } 764 if (ps.in_parameter_declaration) 765 prefix_blankline_requested = 0; 766 767 if (ps.p_l_follow > 0) { /* check for preceeding unbalanced 768 * parens */ 769 diag(1, "Unbalanced parens"); 770 ps.p_l_follow = 0; 771 if (sp_sw) { /* check for unclosed if, for, etc. */ 772 sp_sw = false; 773 parse(hd_type); 774 ps.ind_level = ps.i_l_follow; 775 } 776 } 777 if (s_code == e_code) 778 ps.ind_stmt = false; /* dont put extra indentation on line 779 * with '{' */ 780 if (ps.in_decl && ps.in_or_st) { /* this is either a structure 781 * declaration or an init */ 782 di_stack[ps.dec_nest++] = dec_ind; 783 /* ? dec_ind = 0; */ 784 } 785 else { 786 ps.decl_on_line = false; 787 /* we can't be in the middle of a declaration, so don't do 788 * special indentation of comments */ 789 if (blanklines_after_declarations_at_proctop 790 && ps.in_parameter_declaration) 791 postfix_blankline_requested = 1; 792 ps.in_parameter_declaration = 0; 793 } 794 dec_ind = 0; 795 parse(lbrace); /* let parser know about this */ 796 if (ps.want_blank) /* put a blank before '{' if '{' is not at 797 * start of line */ 798 *e_code++ = ' '; 799 ps.want_blank = false; 800 *e_code++ = '{'; 801 ps.just_saw_decl = 0; 802 break; 803 804 case rbrace: /* got a '}' */ 805 if (ps.p_stack[ps.tos] == decl && !ps.block_init) /* semicolons can be 806 * omitted in 807 * declarations */ 808 parse(semicolon); 809 if (ps.p_l_follow) {/* check for unclosed if, for, else. */ 810 diag(1, "Unbalanced parens"); 811 ps.p_l_follow = 0; 812 sp_sw = false; 813 } 814 ps.just_saw_decl = 0; 815 ps.block_init_level--; 816 if (s_code != e_code && !ps.block_init) { /* '}' must be first on 817 * line */ 818 if (verbose) 819 diag(0, "Line broken"); 820 dump_line(); 821 } 822 *e_code++ = '}'; 823 ps.want_blank = true; 824 ps.in_stmt = ps.ind_stmt = false; 825 if (ps.dec_nest > 0) { /* we are in multi-level structure 826 * declaration */ 827 dec_ind = di_stack[--ps.dec_nest]; 828 if (ps.dec_nest == 0 && !ps.in_parameter_declaration) 829 ps.just_saw_decl = 2; 830 ps.in_decl = true; 831 } 832 prefix_blankline_requested = 0; 833 parse(rbrace); /* let parser know about this */ 834 ps.search_brace = cuddle_else && ps.p_stack[ps.tos] == ifhead 835 && ps.il[ps.tos] >= ps.ind_level; 836 if (ps.tos <= 1 && blanklines_after_procs && ps.dec_nest <= 0) 837 postfix_blankline_requested = 1; 838 break; 839 840 case swstmt: /* got keyword "switch" */ 841 sp_sw = true; 842 hd_type = swstmt; /* keep this for when we have seen the 843 * expression */ 844 goto copy_id; /* go move the token into buffer */ 845 846 case sp_paren: /* token is if, while, for */ 847 sp_sw = true; /* the interesting stuff is done after the 848 * expression is scanned */ 849 hd_type = (*token == 'i' ? ifstmt : 850 (*token == 'w' ? whilestmt : forstmt)); 851 852 /* 853 * remember the type of header for later use by parser 854 */ 855 goto copy_id; /* copy the token into line */ 856 857 case sp_nparen: /* got else, do */ 858 ps.in_stmt = false; 859 if (*token == 'e') { 860 if (e_code != s_code && (!cuddle_else || e_code[-1] != '}')) { 861 if (verbose) 862 diag(0, "Line broken"); 863 dump_line();/* make sure this starts a line */ 864 ps.want_blank = false; 865 } 866 force_nl = true;/* also, following stuff must go onto new line */ 867 last_else = 1; 868 parse(elselit); 869 } 870 else { 871 if (e_code != s_code) { /* make sure this starts a line */ 872 if (verbose) 873 diag(0, "Line broken"); 874 dump_line(); 875 ps.want_blank = false; 876 } 877 force_nl = true;/* also, following stuff must go onto new line */ 878 last_else = 0; 879 parse(dolit); 880 } 881 goto copy_id; /* move the token into line */ 882 883 case decl: /* we have a declaration type (int, register, 884 * etc.) */ 885 parse(decl); /* let parser worry about indentation */ 886 if (ps.last_token == rparen && ps.tos <= 1) { 887 ps.in_parameter_declaration = 1; 888 if (s_code != e_code) { 889 dump_line(); 890 ps.want_blank = 0; 891 } 892 } 893 if (ps.in_parameter_declaration && ps.indent_parameters && ps.dec_nest == 0) { 894 ps.ind_level = ps.i_l_follow = 1; 895 ps.ind_stmt = 0; 896 } 897 ps.in_or_st = true; /* this might be a structure or initialization 898 * declaration */ 899 ps.in_decl = ps.decl_on_line = true; 900 if ( /* !ps.in_or_st && */ ps.dec_nest <= 0) 901 ps.just_saw_decl = 2; 902 prefix_blankline_requested = 0; 903 for (i = 0; token[i++];); /* get length of token */ 904 905 /* 906 * dec_ind = e_code - s_code + (ps.decl_indent>i ? ps.decl_indent 907 * : i); 908 */ 909 dec_ind = ps.decl_indent > 0 ? ps.decl_indent : i; 910 goto copy_id; 911 912 case ident: /* got an identifier or constant */ 913 if (ps.in_decl) { /* if we are in a declaration, we must indent 914 * identifier */ 915 if (ps.want_blank) 916 *e_code++ = ' '; 917 ps.want_blank = false; 918 if (is_procname == 0 || !procnames_start_line) { 919 if (!ps.block_init) { 920 if (troff && !ps.dumped_decl_indent) { 921 sprintf(e_code, "\n.De %dp+\200p\n", dec_ind * 7); 922 ps.dumped_decl_indent = 1; 923 e_code += strlen(e_code); 924 } 925 else 926 while ((e_code - s_code) < dec_ind) { 927 CHECK_SIZE_CODE; 928 *e_code++ = ' '; 929 } 930 } 931 } 932 else { 933 if (dec_ind && s_code != e_code) 934 dump_line(); 935 dec_ind = 0; 936 ps.want_blank = false; 937 } 938 } 939 else if (sp_sw && ps.p_l_follow == 0) { 940 sp_sw = false; 941 force_nl = true; 942 ps.last_u_d = true; 943 ps.in_stmt = false; 944 parse(hd_type); 945 } 946 copy_id: 947 if (ps.want_blank) 948 *e_code++ = ' '; 949 if (troff && ps.its_a_keyword) { 950 e_code = chfont(&bodyf, &keywordf, e_code); 951 for (t_ptr = token; *t_ptr; ++t_ptr) { 952 CHECK_SIZE_CODE; 953 *e_code++ = keywordf.allcaps && islower(*t_ptr) 954 ? toupper(*t_ptr) : *t_ptr; 955 } 956 e_code = chfont(&keywordf, &bodyf, e_code); 957 } 958 else 959 for (t_ptr = token; *t_ptr; ++t_ptr) { 960 CHECK_SIZE_CODE; 961 *e_code++ = *t_ptr; 962 } 963 ps.want_blank = true; 964 break; 965 966 case period: /* treat a period kind of like a binary 967 * operation */ 968 *e_code++ = '.'; /* move the period into line */ 969 ps.want_blank = false; /* dont put a blank after a period */ 970 break; 971 972 case comma: 973 ps.want_blank = (s_code != e_code); /* only put blank after comma 974 * if comma does not start the 975 * line */ 976 if (ps.in_decl && is_procname == 0 && !ps.block_init) 977 while ((e_code - s_code) < (dec_ind - 1)) { 978 CHECK_SIZE_CODE; 979 *e_code++ = ' '; 980 } 981 982 *e_code++ = ','; 983 if (ps.p_l_follow == 0) { 984 if (ps.block_init_level <= 0) 985 ps.block_init = 0; 986 if (break_comma && (!ps.leave_comma || compute_code_target() + (e_code - s_code) > max_col - 8)) 987 force_nl = true; 988 } 989 break; 990 991 case preesc: /* got the character '#' */ 992 if ((s_com != e_com) || 993 (s_lab != e_lab) || 994 (s_code != e_code)) 995 dump_line(); 996 *e_lab++ = '#'; /* move whole line to 'label' buffer */ 997 { 998 int in_comment = 0; 999 int com_start = 0; 1000 char quote = 0; 1001 int com_end = 0; 1002 1003 while (*buf_ptr == ' ' || *buf_ptr == '\t') { 1004 buf_ptr++; 1005 if (buf_ptr >= buf_end) 1006 fill_buffer(); 1007 } 1008 while (*buf_ptr != '\n' || (in_comment && !had_eof)) { 1009 CHECK_SIZE_LAB; 1010 *e_lab = *buf_ptr++; 1011 if (buf_ptr >= buf_end) 1012 fill_buffer(); 1013 switch (*e_lab++) { 1014 case BACKSLASH: 1015 if (troff) 1016 *e_lab++ = BACKSLASH; 1017 if (!in_comment) { 1018 *e_lab++ = *buf_ptr++; 1019 if (buf_ptr >= buf_end) 1020 fill_buffer(); 1021 } 1022 break; 1023 case '/': 1024 if (*buf_ptr == '*' && !in_comment && !quote) { 1025 in_comment = 1; 1026 *e_lab++ = *buf_ptr++; 1027 com_start = e_lab - s_lab - 2; 1028 } 1029 break; 1030 case '"': 1031 if (quote == '"') 1032 quote = 0; 1033 break; 1034 case '\'': 1035 if (quote == '\'') 1036 quote = 0; 1037 break; 1038 case '*': 1039 if (*buf_ptr == '/' && in_comment) { 1040 in_comment = 0; 1041 *e_lab++ = *buf_ptr++; 1042 com_end = e_lab - s_lab; 1043 } 1044 break; 1045 } 1046 } 1047 1048 while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t')) 1049 e_lab--; 1050 if (e_lab - s_lab == com_end && bp_save == 0) { /* comment on 1051 * preprocessor line */ 1052 if (sc_end == 0) /* if this is the first comment, we 1053 * must set up the buffer */ 1054 sc_end = &(save_com[0]); 1055 else { 1056 *sc_end++ = '\n'; /* add newline between 1057 * comments */ 1058 *sc_end++ = ' '; 1059 --line_no; 1060 } 1061 bcopy(s_lab + com_start, sc_end, com_end - com_start); 1062 sc_end += com_end - com_start; 1063 if (sc_end >= &save_com[sc_size]) 1064 abort(); 1065 e_lab = s_lab + com_start; 1066 while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t')) 1067 e_lab--; 1068 bp_save = buf_ptr; /* save current input buffer */ 1069 be_save = buf_end; 1070 buf_ptr = save_com; /* fix so that subsequent calls to 1071 * lexi will take tokens out of 1072 * save_com */ 1073 *sc_end++ = ' '; /* add trailing blank, just in case */ 1074 buf_end = sc_end; 1075 sc_end = 0; 1076 } 1077 *e_lab = '\0'; /* null terminate line */ 1078 ps.pcase = false; 1079 } 1080 1081 if (strncmp(s_lab, "#if", 3) == 0) { 1082 if (blanklines_around_conditional_compilation) { 1083 int c; 1084 prefix_blankline_requested++; 1085 while ((c = getc(input)) == '\n'); 1086 ungetc(c, input); 1087 } 1088 if (ifdef_level < sizeof state_stack / sizeof state_stack[0]) { 1089 match_state[ifdef_level].tos = -1; 1090 state_stack[ifdef_level++] = ps; 1091 } 1092 else 1093 diag(1, "#if stack overflow"); 1094 } 1095 else if (strncmp(s_lab, "#else", 5) == 0) 1096 if (ifdef_level <= 0) 1097 diag(1, "Unmatched #else"); 1098 else { 1099 match_state[ifdef_level - 1] = ps; 1100 ps = state_stack[ifdef_level - 1]; 1101 } 1102 else if (strncmp(s_lab, "#endif", 6) == 0) { 1103 if (ifdef_level <= 0) 1104 diag(1, "Unmatched #endif"); 1105 else { 1106 ifdef_level--; 1107 1108 #ifdef undef 1109 /* 1110 * This match needs to be more intelligent before the 1111 * message is useful 1112 */ 1113 if (match_state[ifdef_level].tos >= 0 1114 && bcmp(&ps, &match_state[ifdef_level], sizeof ps)) 1115 diag(0, "Syntactically inconsistant #ifdef alternatives."); 1116 #endif 1117 } 1118 if (blanklines_around_conditional_compilation) { 1119 postfix_blankline_requested++; 1120 n_real_blanklines = 0; 1121 } 1122 } 1123 break; /* subsequent processing of the newline 1124 * character will cause the line to be printed */ 1125 1126 case comment: /* we have gotten a comment this is a biggie */ 1127 if (flushed_nl) { /* we should force a broken line here */ 1128 flushed_nl = false; 1129 dump_line(); 1130 ps.want_blank = false; /* dont insert blank at line start */ 1131 force_nl = false; 1132 } 1133 pr_comment(); 1134 break; 1135 } /* end of big switch stmt */ 1136 1137 *e_code = '\0'; /* make sure code section is null terminated */ 1138 if (type_code != comment && type_code != newline && type_code != preesc) 1139 ps.last_token = type_code; 1140 } /* end of main while (1) loop */ 1141 } 1142 1143 /* 1144 * copy input file to backup file if in_name is /blah/blah/blah/file, then 1145 * backup file will be ".Bfile" then make the backup file the input and 1146 * original input file the output 1147 */ 1148 void 1149 bakcopy() 1150 { 1151 int n, 1152 bakchn; 1153 char buff[8 * 1024]; 1154 char *p; 1155 1156 /* construct file name .Bfile */ 1157 for (p = in_name; *p; p++); /* skip to end of string */ 1158 while (p > in_name && *p != '/') /* find last '/' */ 1159 p--; 1160 if (*p == '/') 1161 p++; 1162 if (snprintf(bakfile, MAXPATHLEN, "%s.BAK", p) >= MAXPATHLEN) 1163 errx(1, "%s.BAK: %s", p, strerror(ENAMETOOLONG)); 1164 1165 /* copy in_name to backup file */ 1166 bakchn = creat(bakfile, 0600); 1167 if (bakchn < 0) 1168 err(1, "%s", bakfile); 1169 while ((n = read(fileno(input), buff, sizeof buff)) > 0) 1170 if (write(bakchn, buff, n) != n) 1171 err(1, "%s", bakfile); 1172 if (n < 0) 1173 err(1, "%s", in_name); 1174 close(bakchn); 1175 fclose(input); 1176 1177 /* re-open backup file as the input file */ 1178 input = fopen(bakfile, "r"); 1179 if (input == NULL) 1180 err(1, "%s", bakfile); 1181 /* now the original input file will be the output */ 1182 output = fopen(in_name, "w"); 1183 if (output == NULL) { 1184 unlink(bakfile); 1185 err(1, "%s", in_name); 1186 } 1187 } 1188