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