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