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