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