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