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