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