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