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