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