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