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