indent.c revision 1.87 1 1.87 rillig /* $NetBSD: indent.c,v 1.87 2021/09/26 19:57:23 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.25 kamil static char sccsid[] = "@(#)indent.c 5.17 (Berkeley) 6/7/93";
42 1.25 kamil #endif
43 1.1 cgd
44 1.25 kamil #include <sys/cdefs.h>
45 1.25 kamil #if defined(__NetBSD__)
46 1.87 rillig __RCSID("$NetBSD: indent.c,v 1.87 2021/09/26 19:57:23 rillig Exp $");
47 1.25 kamil #elif defined(__FreeBSD__)
48 1.25 kamil __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
49 1.25 kamil #endif
50 1.1 cgd
51 1.1 cgd #include <sys/param.h>
52 1.25 kamil #if HAVE_CAPSICUM
53 1.25 kamil #include <sys/capsicum.h>
54 1.25 kamil #include <capsicum_helpers.h>
55 1.25 kamil #endif
56 1.82 rillig #include <ctype.h>
57 1.6 lukem #include <err.h>
58 1.6 lukem #include <errno.h>
59 1.1 cgd #include <fcntl.h>
60 1.1 cgd #include <stdio.h>
61 1.1 cgd #include <stdlib.h>
62 1.1 cgd #include <string.h>
63 1.82 rillig #include <unistd.h>
64 1.29 rillig
65 1.25 kamil #include "indent.h"
66 1.25 kamil
67 1.71 rillig struct options opt = {
68 1.86 rillig .btype_2 = true,
69 1.86 rillig .comment_delimiter_on_blankline = true,
70 1.86 rillig .cuddle_else = true,
71 1.86 rillig .comment_column = 33,
72 1.86 rillig .decl_indent = 16,
73 1.86 rillig .else_if = true,
74 1.86 rillig .function_brace_split = true,
75 1.86 rillig .format_col1_comments = true,
76 1.86 rillig .format_block_comments = true,
77 1.86 rillig .indent_parameters = true,
78 1.86 rillig .indent_size = 8,
79 1.86 rillig .local_decl_indent = -1,
80 1.86 rillig .lineup_to_parens = true,
81 1.86 rillig .procnames_start_line = true,
82 1.86 rillig .star_comment_cont = true,
83 1.86 rillig .tabsize = 8,
84 1.86 rillig .max_line_length = 78,
85 1.86 rillig .use_tabs = true,
86 1.71 rillig };
87 1.71 rillig
88 1.27 joerg struct parser_state ps;
89 1.27 joerg
90 1.63 rillig struct buffer lab;
91 1.65 rillig struct buffer code;
92 1.63 rillig struct buffer com;
93 1.67 rillig struct buffer token;
94 1.27 joerg
95 1.86 rillig char *in_buffer;
96 1.86 rillig char *in_buffer_limit;
97 1.86 rillig char *buf_ptr;
98 1.86 rillig char *buf_end;
99 1.86 rillig
100 1.86 rillig char sc_buf[sc_size];
101 1.86 rillig char *save_com;
102 1.86 rillig char *sc_end;
103 1.86 rillig
104 1.86 rillig char *bp_save;
105 1.86 rillig char *be_save;
106 1.86 rillig
107 1.86 rillig bool found_err;
108 1.86 rillig int n_real_blanklines;
109 1.86 rillig bool prefix_blankline_requested;
110 1.86 rillig bool postfix_blankline_requested;
111 1.86 rillig bool break_comma;
112 1.86 rillig float case_ind;
113 1.86 rillig bool had_eof;
114 1.86 rillig int line_no;
115 1.86 rillig bool inhibit_formatting;
116 1.86 rillig int suppress_blanklines;
117 1.27 joerg
118 1.86 rillig 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.86 rillig FILE *input;
123 1.86 rillig FILE *output;
124 1.27 joerg
125 1.25 kamil static void bakcopy(void);
126 1.75 rillig static void indent_declaration(int, bool);
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.86 rillig 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.81 rillig if (code.e + desired_size >= code.l)
140 1.81 rillig buf_expand(&code, desired_size);
141 1.34 rillig }
142 1.34 rillig
143 1.34 rillig static void
144 1.34 rillig check_size_label(size_t desired_size)
145 1.34 rillig {
146 1.81 rillig if (lab.e + desired_size >= lab.l)
147 1.81 rillig buf_expand(&lab, desired_size);
148 1.34 rillig }
149 1.34 rillig
150 1.40 rillig #if HAVE_CAPSICUM
151 1.40 rillig static void
152 1.40 rillig init_capsicum(void)
153 1.1 cgd {
154 1.25 kamil cap_rights_t rights;
155 1.40 rillig
156 1.40 rillig /* Restrict input/output descriptors and enter Capsicum sandbox. */
157 1.40 rillig cap_rights_init(&rights, CAP_FSTAT, CAP_WRITE);
158 1.40 rillig if (caph_rights_limit(fileno(output), &rights) < 0)
159 1.40 rillig err(EXIT_FAILURE, "unable to limit rights for %s", out_name);
160 1.40 rillig cap_rights_init(&rights, CAP_FSTAT, CAP_READ);
161 1.40 rillig if (caph_rights_limit(fileno(input), &rights) < 0)
162 1.40 rillig err(EXIT_FAILURE, "unable to limit rights for %s", in_name);
163 1.40 rillig if (caph_enter() < 0)
164 1.40 rillig err(EXIT_FAILURE, "unable to enter capability mode");
165 1.40 rillig }
166 1.41 rillig #endif
167 1.41 rillig
168 1.41 rillig static void
169 1.75 rillig search_brace(token_type *inout_ttype, bool *inout_force_nl,
170 1.86 rillig bool *inout_comment_buffered, bool *inout_last_else)
171 1.41 rillig {
172 1.41 rillig while (ps.search_brace) {
173 1.64 rillig switch (*inout_ttype) {
174 1.41 rillig case newline:
175 1.41 rillig if (sc_end == NULL) {
176 1.41 rillig save_com = sc_buf;
177 1.41 rillig save_com[0] = save_com[1] = ' ';
178 1.41 rillig sc_end = &save_com[2];
179 1.41 rillig }
180 1.41 rillig *sc_end++ = '\n';
181 1.41 rillig /*
182 1.41 rillig * We may have inherited a force_nl == true from the previous
183 1.86 rillig * token (like a semicolon). But once we know that a newline has
184 1.86 rillig * been scanned in this loop, force_nl should be false.
185 1.41 rillig *
186 1.86 rillig * However, the force_nl == true must be preserved if newline is
187 1.86 rillig * never scanned in this loop, so this assignment cannot be done
188 1.86 rillig * earlier.
189 1.41 rillig */
190 1.41 rillig *inout_force_nl = false;
191 1.59 rillig break;
192 1.41 rillig case form_feed:
193 1.41 rillig break;
194 1.41 rillig case comment:
195 1.41 rillig if (sc_end == NULL) {
196 1.41 rillig /*
197 1.41 rillig * Copy everything from the start of the line, because
198 1.56 rillig * process_comment() will use that to calculate original
199 1.41 rillig * indentation of a boxed comment.
200 1.41 rillig */
201 1.59 rillig memcpy(sc_buf, in_buffer, (size_t)(buf_ptr - in_buffer) - 4);
202 1.41 rillig save_com = sc_buf + (buf_ptr - in_buffer - 4);
203 1.41 rillig save_com[0] = save_com[1] = ' ';
204 1.41 rillig sc_end = &save_com[2];
205 1.41 rillig }
206 1.41 rillig *inout_comment_buffered = true;
207 1.41 rillig *sc_end++ = '/'; /* copy in start of comment */
208 1.41 rillig *sc_end++ = '*';
209 1.41 rillig for (;;) { /* loop until the end of the comment */
210 1.41 rillig *sc_end = *buf_ptr++;
211 1.41 rillig if (buf_ptr >= buf_end)
212 1.41 rillig fill_buffer();
213 1.41 rillig if (*sc_end++ == '*' && *buf_ptr == '/')
214 1.41 rillig break; /* we are at end of comment */
215 1.41 rillig if (sc_end >= &save_com[sc_size]) { /* check for temp buffer
216 1.41 rillig * overflow */
217 1.41 rillig diag(1, "Internal buffer overflow - Move big comment from right after if, while, or whatever");
218 1.41 rillig fflush(output);
219 1.41 rillig exit(1);
220 1.41 rillig }
221 1.41 rillig }
222 1.41 rillig *sc_end++ = '/'; /* add ending slash */
223 1.41 rillig if (++buf_ptr >= buf_end) /* get past / in buffer */
224 1.41 rillig fill_buffer();
225 1.41 rillig break;
226 1.41 rillig case lbrace:
227 1.41 rillig /*
228 1.86 rillig * Put KNF-style lbraces before the buffered up tokens and jump
229 1.86 rillig * out of this loop in order to avoid copying the token again
230 1.86 rillig * under the default case of the switch below.
231 1.41 rillig */
232 1.41 rillig if (sc_end != NULL && opt.btype_2) {
233 1.41 rillig save_com[0] = '{';
234 1.41 rillig /*
235 1.86 rillig * Originally the lbrace may have been alone on its own line,
236 1.86 rillig * but it will be moved into "the else's line", so if there
237 1.86 rillig * was a newline resulting from the "{" before, it must be
238 1.86 rillig * scanned now and ignored.
239 1.41 rillig */
240 1.41 rillig while (isspace((unsigned char)*buf_ptr)) {
241 1.41 rillig if (++buf_ptr >= buf_end)
242 1.41 rillig fill_buffer();
243 1.41 rillig if (*buf_ptr == '\n')
244 1.41 rillig break;
245 1.41 rillig }
246 1.41 rillig goto sw_buffer;
247 1.41 rillig }
248 1.41 rillig /* FALLTHROUGH */
249 1.41 rillig default: /* it is the start of a normal statement */
250 1.41 rillig {
251 1.75 rillig bool remove_newlines;
252 1.41 rillig
253 1.41 rillig remove_newlines =
254 1.41 rillig /* "} else" */
255 1.67 rillig (*inout_ttype == keyword_do_else && *token.s == 'e' &&
256 1.65 rillig code.e != code.s && code.e[-1] == '}')
257 1.41 rillig /* "else if" */
258 1.64 rillig || (*inout_ttype == keyword_for_if_while &&
259 1.67 rillig *token.s == 'i' && *inout_last_else && opt.else_if);
260 1.41 rillig if (remove_newlines)
261 1.41 rillig *inout_force_nl = false;
262 1.41 rillig if (sc_end == NULL) { /* ignore buffering if
263 1.41 rillig * comment wasn't saved up */
264 1.41 rillig ps.search_brace = false;
265 1.41 rillig return;
266 1.41 rillig }
267 1.41 rillig while (sc_end > save_com && isblank((unsigned char)sc_end[-1])) {
268 1.41 rillig sc_end--;
269 1.41 rillig }
270 1.41 rillig if (opt.swallow_optional_blanklines ||
271 1.41 rillig (!*inout_comment_buffered && remove_newlines)) {
272 1.41 rillig *inout_force_nl = !remove_newlines;
273 1.41 rillig while (sc_end > save_com && sc_end[-1] == '\n') {
274 1.41 rillig sc_end--;
275 1.41 rillig }
276 1.41 rillig }
277 1.41 rillig if (*inout_force_nl) { /* if we should insert a nl here, put
278 1.41 rillig * it into the buffer */
279 1.41 rillig *inout_force_nl = false;
280 1.41 rillig --line_no; /* this will be re-increased when the
281 1.41 rillig * newline is read from the buffer */
282 1.41 rillig *sc_end++ = '\n';
283 1.41 rillig *sc_end++ = ' ';
284 1.41 rillig if (opt.verbose) /* print error msg if the line was
285 1.41 rillig * not already broken */
286 1.41 rillig diag(0, "Line broken");
287 1.41 rillig }
288 1.73 rillig for (const char *t_ptr = token.s; *t_ptr != '\0'; ++t_ptr)
289 1.41 rillig *sc_end++ = *t_ptr;
290 1.41 rillig
291 1.41 rillig sw_buffer:
292 1.41 rillig ps.search_brace = false; /* stop looking for start of stmt */
293 1.41 rillig bp_save = buf_ptr; /* save current input buffer */
294 1.41 rillig be_save = buf_end;
295 1.41 rillig buf_ptr = save_com; /* fix so that subsequent calls to
296 1.41 rillig * lexi will take tokens out of save_com */
297 1.41 rillig *sc_end++ = ' '; /* add trailing blank, just in case */
298 1.41 rillig buf_end = sc_end;
299 1.41 rillig sc_end = NULL;
300 1.58 rillig debug_println("switched buf_ptr to save_com");
301 1.41 rillig break;
302 1.41 rillig }
303 1.41 rillig } /* end of switch */
304 1.41 rillig /*
305 1.86 rillig * We must make this check, just in case there was an unexpected EOF.
306 1.41 rillig */
307 1.64 rillig if (*inout_ttype != end_of_file) {
308 1.41 rillig /*
309 1.41 rillig * The only intended purpose of calling lexi() below is to
310 1.41 rillig * categorize the next token in order to decide whether to
311 1.86 rillig * continue buffering forthcoming tokens. Once the buffering is
312 1.86 rillig * over, lexi() will be called again elsewhere on all of the
313 1.86 rillig * tokens - this time for normal processing.
314 1.41 rillig *
315 1.41 rillig * Calling it for this purpose is a bug, because lexi() also
316 1.86 rillig * changes the parser state and discards leading whitespace, which
317 1.86 rillig * is needed mostly for comment-related considerations.
318 1.41 rillig *
319 1.86 rillig * Work around the former problem by giving lexi() a copy of the
320 1.86 rillig * current parser state and discard it if the call turned out to
321 1.86 rillig * be just a look ahead.
322 1.41 rillig *
323 1.41 rillig * Work around the latter problem by copying all whitespace
324 1.86 rillig * characters into the buffer so that the later lexi() call will
325 1.86 rillig * read them.
326 1.41 rillig */
327 1.41 rillig if (sc_end != NULL) {
328 1.41 rillig while (*buf_ptr == ' ' || *buf_ptr == '\t') {
329 1.41 rillig *sc_end++ = *buf_ptr++;
330 1.41 rillig if (sc_end >= &save_com[sc_size]) {
331 1.41 rillig errx(1, "input too long");
332 1.41 rillig }
333 1.41 rillig }
334 1.41 rillig if (buf_ptr >= buf_end) {
335 1.41 rillig fill_buffer();
336 1.41 rillig }
337 1.41 rillig }
338 1.41 rillig
339 1.41 rillig struct parser_state transient_state;
340 1.41 rillig transient_state = ps;
341 1.64 rillig *inout_ttype = lexi(&transient_state); /* read another token */
342 1.64 rillig if (*inout_ttype != newline && *inout_ttype != form_feed &&
343 1.64 rillig *inout_ttype != comment && !transient_state.search_brace) {
344 1.41 rillig ps = transient_state;
345 1.41 rillig }
346 1.41 rillig }
347 1.41 rillig }
348 1.40 rillig
349 1.73 rillig *inout_last_else = false;
350 1.41 rillig }
351 1.1 cgd
352 1.53 rillig static void
353 1.72 rillig buf_init(struct buffer *buf)
354 1.72 rillig {
355 1.72 rillig buf->buf = xmalloc(bufsize);
356 1.86 rillig buf->buf[0] = ' '; /* allow accessing buf->e[-1] */
357 1.72 rillig buf->buf[1] = '\0';
358 1.72 rillig buf->s = buf->buf + 1;
359 1.72 rillig buf->e = buf->s;
360 1.72 rillig buf->l = buf->buf + bufsize - 5; /* safety margin, though unreliable */
361 1.72 rillig }
362 1.72 rillig
363 1.81 rillig void
364 1.81 rillig buf_expand(struct buffer *buf, size_t desired_size)
365 1.81 rillig {
366 1.81 rillig size_t nsize = buf->l - buf->s + 400 + desired_size;
367 1.81 rillig size_t code_len = buf->e - buf->s;
368 1.81 rillig buf->buf = xrealloc(buf->buf, nsize);
369 1.81 rillig buf->e = buf->buf + code_len + 1;
370 1.81 rillig buf->l = buf->buf + nsize - 5;
371 1.81 rillig buf->s = buf->buf + 1;
372 1.81 rillig }
373 1.81 rillig
374 1.72 rillig static void
375 1.53 rillig main_init_globals(void)
376 1.40 rillig {
377 1.83 rillig found_err = false;
378 1.1 cgd
379 1.25 kamil ps.p_stack[0] = stmt; /* this is the parser's stack */
380 1.25 kamil ps.last_nl = true; /* this is true if the last thing scanned was
381 1.1 cgd * a newline */
382 1.25 kamil ps.last_token = semicolon;
383 1.72 rillig buf_init(&com);
384 1.72 rillig buf_init(&lab);
385 1.72 rillig buf_init(&code);
386 1.72 rillig buf_init(&token);
387 1.25 kamil alloc_typenames();
388 1.73 rillig opt.else_if = true; /* XXX: redundant? */
389 1.25 kamil
390 1.68 rillig in_buffer = xmalloc(10);
391 1.25 kamil in_buffer_limit = in_buffer + 8;
392 1.25 kamil buf_ptr = buf_end = in_buffer;
393 1.25 kamil line_no = 1;
394 1.74 rillig had_eof = ps.in_decl = ps.decl_on_line = (break_comma = false);
395 1.25 kamil ps.in_or_st = false;
396 1.25 kamil ps.want_blank = ps.in_stmt = ps.ind_stmt = false;
397 1.25 kamil
398 1.53 rillig ps.pcase = false;
399 1.25 kamil sc_end = NULL;
400 1.25 kamil bp_save = NULL;
401 1.25 kamil be_save = NULL;
402 1.25 kamil
403 1.25 kamil output = NULL;
404 1.25 kamil
405 1.53 rillig const char *suffix = getenv("SIMPLE_BACKUP_SUFFIX");
406 1.53 rillig if (suffix != NULL)
407 1.53 rillig simple_backup_suffix = suffix;
408 1.53 rillig }
409 1.53 rillig
410 1.53 rillig static void
411 1.53 rillig main_parse_command_line(int argc, char **argv)
412 1.53 rillig {
413 1.53 rillig int i;
414 1.53 rillig const char *profile_name = NULL;
415 1.1 cgd
416 1.25 kamil for (i = 1; i < argc; ++i)
417 1.25 kamil if (strcmp(argv[i], "-npro") == 0)
418 1.25 kamil break;
419 1.25 kamil else if (argv[i][0] == '-' && argv[i][1] == 'P' && argv[i][2] != '\0')
420 1.25 kamil profile_name = argv[i]; /* non-empty -P (set profile) */
421 1.25 kamil if (i >= argc)
422 1.25 kamil set_profile(profile_name);
423 1.1 cgd
424 1.25 kamil for (i = 1; i < argc; ++i) {
425 1.80 rillig if (argv[i][0] == '-') {
426 1.80 rillig set_option(argv[i]);
427 1.80 rillig
428 1.80 rillig } else if (input == NULL) {
429 1.80 rillig in_name = argv[i];
430 1.80 rillig input = fopen(in_name, "r");
431 1.80 rillig if (input == NULL)
432 1.80 rillig err(1, "%s", in_name);
433 1.80 rillig
434 1.80 rillig } else if (output == NULL) {
435 1.80 rillig out_name = argv[i];
436 1.80 rillig if (strcmp(in_name, out_name) == 0)
437 1.80 rillig errx(1, "input and output files must be different");
438 1.80 rillig output = fopen(out_name, "w");
439 1.80 rillig if (output == NULL)
440 1.80 rillig err(1, "%s", out_name);
441 1.1 cgd
442 1.80 rillig } else
443 1.25 kamil errx(1, "unknown parameter: %s", argv[i]);
444 1.80 rillig }
445 1.80 rillig
446 1.25 kamil if (input == NULL)
447 1.25 kamil input = stdin;
448 1.25 kamil if (output == NULL) {
449 1.25 kamil if (input == stdin)
450 1.25 kamil output = stdout;
451 1.25 kamil else {
452 1.25 kamil out_name = in_name;
453 1.25 kamil bakcopy();
454 1.1 cgd }
455 1.25 kamil }
456 1.25 kamil
457 1.57 rillig if (opt.comment_column <= 1)
458 1.57 rillig opt.comment_column = 2; /* don't put normal comments before column 2 */
459 1.51 rillig if (opt.block_comment_max_line_length <= 0)
460 1.51 rillig opt.block_comment_max_line_length = opt.max_line_length;
461 1.25 kamil if (opt.local_decl_indent < 0) /* if not specified by user, set this */
462 1.25 kamil opt.local_decl_indent = opt.decl_indent;
463 1.57 rillig if (opt.decl_comment_column <= 0) /* if not specified by user, set this */
464 1.57 rillig opt.decl_comment_column = opt.ljust_decl
465 1.57 rillig ? (opt.comment_column <= 10 ? 2 : opt.comment_column - 8)
466 1.57 rillig : opt.comment_column;
467 1.25 kamil if (opt.continuation_indent == 0)
468 1.57 rillig opt.continuation_indent = opt.indent_size;
469 1.53 rillig }
470 1.53 rillig
471 1.53 rillig static void
472 1.53 rillig main_prepare_parsing(void)
473 1.53 rillig {
474 1.25 kamil fill_buffer(); /* get first batch of stuff into input buffer */
475 1.25 kamil
476 1.25 kamil parse(semicolon);
477 1.53 rillig
478 1.53 rillig char *p = buf_ptr;
479 1.53 rillig int col = 1;
480 1.53 rillig
481 1.59 rillig for (;;) {
482 1.53 rillig if (*p == ' ')
483 1.53 rillig col++;
484 1.53 rillig else if (*p == '\t')
485 1.53 rillig col = opt.tabsize * (1 + (col - 1) / opt.tabsize) + 1;
486 1.53 rillig else
487 1.53 rillig break;
488 1.53 rillig p++;
489 1.25 kamil }
490 1.57 rillig if (col > opt.indent_size)
491 1.79 rillig ps.ind_level = ps.ind_level_follow = col / opt.indent_size;
492 1.53 rillig }
493 1.25 kamil
494 1.60 rillig static void __attribute__((__noreturn__))
495 1.54 rillig process_end_of_file(void)
496 1.54 rillig {
497 1.65 rillig if (lab.s != lab.e || code.s != code.e || com.s != com.e)
498 1.54 rillig dump_line();
499 1.54 rillig
500 1.54 rillig if (ps.tos > 1) /* check for balanced braces */
501 1.54 rillig diag(1, "Stuff missing from end of file");
502 1.54 rillig
503 1.54 rillig if (opt.verbose) {
504 1.54 rillig printf("There were %d output lines and %d comments\n",
505 1.86 rillig ps.stats.lines, ps.stats.comments);
506 1.54 rillig printf("(Lines with comments)/(Lines with code): %6.3f\n",
507 1.86 rillig (1.0 * ps.stats.comment_lines) / ps.stats.code_lines);
508 1.54 rillig }
509 1.54 rillig
510 1.54 rillig fflush(output);
511 1.83 rillig exit(found_err ? EXIT_FAILURE : EXIT_SUCCESS);
512 1.54 rillig }
513 1.54 rillig
514 1.54 rillig static void
515 1.75 rillig process_comment_in_code(token_type ttype, bool *inout_force_nl)
516 1.54 rillig {
517 1.54 rillig if (*inout_force_nl &&
518 1.64 rillig ttype != semicolon &&
519 1.64 rillig (ttype != lbrace || !opt.btype_2)) {
520 1.54 rillig
521 1.54 rillig /* we should force a broken line here */
522 1.54 rillig if (opt.verbose)
523 1.54 rillig diag(0, "Line broken");
524 1.54 rillig dump_line();
525 1.54 rillig ps.want_blank = false; /* dont insert blank at line start */
526 1.54 rillig *inout_force_nl = false;
527 1.54 rillig }
528 1.54 rillig
529 1.54 rillig ps.in_stmt = true; /* turn on flag which causes an extra level of
530 1.84 rillig * indentation. this is turned off by a ';' or
531 1.54 rillig * '}' */
532 1.86 rillig if (com.s != com.e) { /* the turkey has embedded a comment in a
533 1.86 rillig * line. fix it */
534 1.62 rillig size_t len = com.e - com.s;
535 1.54 rillig
536 1.54 rillig check_size_code(len + 3);
537 1.65 rillig *code.e++ = ' ';
538 1.65 rillig memcpy(code.e, com.s, len);
539 1.65 rillig code.e += len;
540 1.65 rillig *code.e++ = ' ';
541 1.65 rillig *code.e = '\0';
542 1.54 rillig ps.want_blank = false;
543 1.62 rillig com.e = com.s;
544 1.54 rillig }
545 1.54 rillig }
546 1.54 rillig
547 1.54 rillig static void
548 1.54 rillig process_form_feed(void)
549 1.54 rillig {
550 1.54 rillig ps.use_ff = true; /* a form feed is treated much like a newline */
551 1.54 rillig dump_line();
552 1.54 rillig ps.want_blank = false;
553 1.54 rillig }
554 1.54 rillig
555 1.54 rillig static void
556 1.54 rillig process_newline(void)
557 1.54 rillig {
558 1.87 rillig if (ps.last_token != comma || ps.p_l_follow > 0 || opt.break_after_comma
559 1.87 rillig || ps.block_init || !break_comma || com.s != com.e) {
560 1.54 rillig dump_line();
561 1.54 rillig ps.want_blank = false;
562 1.54 rillig }
563 1.54 rillig ++line_no; /* keep track of input line number */
564 1.54 rillig }
565 1.54 rillig
566 1.54 rillig static void
567 1.75 rillig process_lparen_or_lbracket(int dec_ind, bool tabs_to_var, bool sp_sw)
568 1.54 rillig {
569 1.54 rillig /* count parens to make Healy happy */
570 1.54 rillig if (++ps.p_l_follow == nitems(ps.paren_indents)) {
571 1.54 rillig diag(0, "Reached internal limit of %zu unclosed parens",
572 1.54 rillig nitems(ps.paren_indents));
573 1.54 rillig ps.p_l_follow--;
574 1.54 rillig }
575 1.67 rillig if (*token.s == '[')
576 1.54 rillig /* not a function pointer declaration or a function call */;
577 1.54 rillig else if (ps.in_decl && !ps.block_init && !ps.dumped_decl_indent &&
578 1.54 rillig ps.procname[0] == '\0' && ps.paren_level == 0) {
579 1.54 rillig /* function pointer declarations */
580 1.54 rillig indent_declaration(dec_ind, tabs_to_var);
581 1.54 rillig ps.dumped_decl_indent = true;
582 1.54 rillig } else if (ps.want_blank &&
583 1.54 rillig ((ps.last_token != ident && ps.last_token != funcname) ||
584 1.54 rillig opt.proc_calls_space ||
585 1.76 rillig (ps.keyword == rw_sizeof ? opt.blank_after_sizeof :
586 1.54 rillig ps.keyword != rw_0 && ps.keyword != rw_offsetof)))
587 1.65 rillig *code.e++ = ' ';
588 1.54 rillig ps.want_blank = false;
589 1.67 rillig *code.e++ = token.s[0];
590 1.58 rillig
591 1.54 rillig ps.paren_indents[ps.p_l_follow - 1] =
592 1.65 rillig indentation_after_range(0, code.s, code.e);
593 1.58 rillig debug_println("paren_indent[%d] is now %d",
594 1.58 rillig ps.p_l_follow - 1, ps.paren_indents[ps.p_l_follow - 1]);
595 1.58 rillig
596 1.54 rillig if (sp_sw && ps.p_l_follow == 1 && opt.extra_expression_indent
597 1.58 rillig && ps.paren_indents[0] < 2 * opt.indent_size) {
598 1.57 rillig ps.paren_indents[0] = 2 * opt.indent_size;
599 1.58 rillig debug_println("paren_indent[0] is now %d", ps.paren_indents[0]);
600 1.58 rillig }
601 1.67 rillig if (ps.in_or_st && *token.s == '(' && ps.tos <= 2) {
602 1.54 rillig /*
603 1.86 rillig * this is a kluge to make sure that declarations will be aligned
604 1.86 rillig * right if proc decl has an explicit type on it, i.e. "int a(x) {..."
605 1.54 rillig */
606 1.54 rillig parse(semicolon); /* I said this was a kluge... */
607 1.54 rillig ps.in_or_st = false; /* turn off flag for structure decl or
608 1.54 rillig * initialization */
609 1.54 rillig }
610 1.54 rillig /* parenthesized type following sizeof or offsetof is not a cast */
611 1.54 rillig if (ps.keyword == rw_offsetof || ps.keyword == rw_sizeof)
612 1.54 rillig ps.not_cast_mask |= 1 << ps.p_l_follow;
613 1.54 rillig }
614 1.54 rillig
615 1.54 rillig static void
616 1.75 rillig process_rparen_or_rbracket(bool *inout_sp_sw, bool *inout_force_nl,
617 1.86 rillig token_type hd_type)
618 1.54 rillig {
619 1.73 rillig if ((ps.cast_mask & (1 << ps.p_l_follow) & ~ps.not_cast_mask) != 0) {
620 1.54 rillig ps.last_u_d = true;
621 1.54 rillig ps.cast_mask &= (1 << ps.p_l_follow) - 1;
622 1.54 rillig ps.want_blank = opt.space_after_cast;
623 1.54 rillig } else
624 1.54 rillig ps.want_blank = true;
625 1.54 rillig ps.not_cast_mask &= (1 << ps.p_l_follow) - 1;
626 1.54 rillig
627 1.54 rillig if (--ps.p_l_follow < 0) {
628 1.54 rillig ps.p_l_follow = 0;
629 1.67 rillig diag(0, "Extra %c", *token.s);
630 1.54 rillig }
631 1.54 rillig
632 1.65 rillig if (code.e == code.s) /* if the paren starts the line */
633 1.54 rillig ps.paren_level = ps.p_l_follow; /* then indent it */
634 1.54 rillig
635 1.67 rillig *code.e++ = token.s[0];
636 1.54 rillig
637 1.86 rillig if (*inout_sp_sw && (ps.p_l_follow == 0)) { /* check for end of if (...),
638 1.86 rillig * or some such */
639 1.54 rillig *inout_sp_sw = false;
640 1.54 rillig *inout_force_nl = true; /* must force newline after if */
641 1.86 rillig ps.last_u_d = true; /* inform lexi that a following operator is
642 1.86 rillig * unary */
643 1.54 rillig ps.in_stmt = false; /* dont use stmt continuation indentation */
644 1.54 rillig
645 1.54 rillig parse(hd_type); /* let parser worry about if, or whatever */
646 1.54 rillig }
647 1.86 rillig ps.search_brace = opt.btype_2; /* this should ensure that constructs
648 1.86 rillig * such as main(){...} and int[]{...}
649 1.86 rillig * have their braces put in the right
650 1.86 rillig * place */
651 1.54 rillig }
652 1.54 rillig
653 1.54 rillig static void
654 1.75 rillig process_unary_op(int dec_ind, bool tabs_to_var)
655 1.54 rillig {
656 1.54 rillig if (!ps.dumped_decl_indent && ps.in_decl && !ps.block_init &&
657 1.54 rillig ps.procname[0] == '\0' && ps.paren_level == 0) {
658 1.54 rillig /* pointer declarations */
659 1.82 rillig indent_declaration(dec_ind - (int)strlen(token.s), tabs_to_var);
660 1.54 rillig ps.dumped_decl_indent = true;
661 1.54 rillig } else if (ps.want_blank)
662 1.65 rillig *code.e++ = ' ';
663 1.54 rillig
664 1.54 rillig {
665 1.67 rillig size_t len = token.e - token.s;
666 1.54 rillig
667 1.54 rillig check_size_code(len);
668 1.67 rillig memcpy(code.e, token.s, len);
669 1.65 rillig code.e += len;
670 1.54 rillig }
671 1.54 rillig ps.want_blank = false;
672 1.54 rillig }
673 1.54 rillig
674 1.54 rillig static void
675 1.54 rillig process_binary_op(void)
676 1.54 rillig {
677 1.67 rillig size_t len = token.e - token.s;
678 1.54 rillig
679 1.54 rillig check_size_code(len + 1);
680 1.54 rillig if (ps.want_blank)
681 1.65 rillig *code.e++ = ' ';
682 1.67 rillig memcpy(code.e, token.s, len);
683 1.65 rillig code.e += len;
684 1.54 rillig
685 1.54 rillig ps.want_blank = true;
686 1.54 rillig }
687 1.54 rillig
688 1.54 rillig static void
689 1.54 rillig process_postfix_op(void)
690 1.54 rillig {
691 1.67 rillig *code.e++ = token.s[0];
692 1.67 rillig *code.e++ = token.s[1];
693 1.54 rillig ps.want_blank = true;
694 1.54 rillig }
695 1.54 rillig
696 1.54 rillig static void
697 1.54 rillig process_question(int *inout_squest)
698 1.54 rillig {
699 1.54 rillig (*inout_squest)++; /* this will be used when a later colon
700 1.54 rillig * appears so we can distinguish the
701 1.54 rillig * <c>?<n>:<n> construct */
702 1.54 rillig if (ps.want_blank)
703 1.65 rillig *code.e++ = ' ';
704 1.65 rillig *code.e++ = '?';
705 1.54 rillig ps.want_blank = true;
706 1.54 rillig }
707 1.54 rillig
708 1.54 rillig static void
709 1.75 rillig process_colon(int *inout_squest, bool *inout_force_nl, bool *inout_scase)
710 1.54 rillig {
711 1.54 rillig if (*inout_squest > 0) { /* it is part of the <c>?<n>: <n> construct */
712 1.54 rillig --*inout_squest;
713 1.54 rillig if (ps.want_blank)
714 1.65 rillig *code.e++ = ' ';
715 1.65 rillig *code.e++ = ':';
716 1.54 rillig ps.want_blank = true;
717 1.54 rillig return;
718 1.54 rillig }
719 1.54 rillig if (ps.in_or_st) {
720 1.65 rillig *code.e++ = ':';
721 1.54 rillig ps.want_blank = false;
722 1.54 rillig return;
723 1.54 rillig }
724 1.54 rillig ps.in_stmt = false; /* seeing a label does not imply we are in a
725 1.54 rillig * stmt */
726 1.54 rillig /*
727 1.54 rillig * turn everything so far into a label
728 1.54 rillig */
729 1.54 rillig {
730 1.65 rillig size_t len = code.e - code.s;
731 1.54 rillig
732 1.54 rillig check_size_label(len + 3);
733 1.65 rillig memcpy(lab.e, code.s, len);
734 1.63 rillig lab.e += len;
735 1.63 rillig *lab.e++ = ':';
736 1.63 rillig *lab.e = '\0';
737 1.65 rillig code.e = code.s;
738 1.54 rillig }
739 1.54 rillig *inout_force_nl = ps.pcase = *inout_scase; /* ps.pcase will be used by
740 1.54 rillig * dump_line to decide how to
741 1.54 rillig * indent the label. force_nl
742 1.54 rillig * will force a case n: to be
743 1.54 rillig * on a line by itself */
744 1.54 rillig *inout_scase = false;
745 1.54 rillig ps.want_blank = false;
746 1.54 rillig }
747 1.54 rillig
748 1.54 rillig static void
749 1.75 rillig process_semicolon(bool *inout_scase, int *inout_squest, int dec_ind,
750 1.86 rillig bool tabs_to_var, bool *inout_sp_sw,
751 1.86 rillig token_type hd_type,
752 1.86 rillig bool *inout_force_nl)
753 1.54 rillig {
754 1.79 rillig if (ps.decl_nest == 0)
755 1.54 rillig ps.in_or_st = false; /* we are not in an initialization or
756 1.54 rillig * structure declaration */
757 1.86 rillig *inout_scase = false; /* these will only need resetting in an error */
758 1.54 rillig *inout_squest = 0;
759 1.54 rillig if (ps.last_token == rparen)
760 1.73 rillig ps.in_parameter_declaration = false;
761 1.54 rillig ps.cast_mask = 0;
762 1.54 rillig ps.not_cast_mask = 0;
763 1.73 rillig ps.block_init = false;
764 1.54 rillig ps.block_init_level = 0;
765 1.54 rillig ps.just_saw_decl--;
766 1.54 rillig
767 1.65 rillig if (ps.in_decl && code.s == code.e && !ps.block_init &&
768 1.54 rillig !ps.dumped_decl_indent && ps.paren_level == 0) {
769 1.54 rillig /* indent stray semicolons in declarations */
770 1.54 rillig indent_declaration(dec_ind - 1, tabs_to_var);
771 1.54 rillig ps.dumped_decl_indent = true;
772 1.54 rillig }
773 1.54 rillig
774 1.79 rillig ps.in_decl = (ps.decl_nest > 0); /* if we were in a first level
775 1.86 rillig * structure declaration, we arent any
776 1.86 rillig * more */
777 1.54 rillig
778 1.54 rillig if ((!*inout_sp_sw || hd_type != for_exprs) && ps.p_l_follow > 0) {
779 1.54 rillig
780 1.54 rillig /*
781 1.86 rillig * This should be true iff there were unbalanced parens in the stmt.
782 1.86 rillig * It is a bit complicated, because the semicolon might be in a for
783 1.86 rillig * stmt
784 1.54 rillig */
785 1.54 rillig diag(1, "Unbalanced parens");
786 1.54 rillig ps.p_l_follow = 0;
787 1.54 rillig if (*inout_sp_sw) { /* this is a check for an if, while, etc. with
788 1.54 rillig * unbalanced parens */
789 1.54 rillig *inout_sp_sw = false;
790 1.54 rillig parse(hd_type); /* dont lose the if, or whatever */
791 1.54 rillig }
792 1.54 rillig }
793 1.65 rillig *code.e++ = ';';
794 1.54 rillig ps.want_blank = true;
795 1.86 rillig ps.in_stmt = (ps.p_l_follow > 0); /* we are no longer in the middle of a
796 1.86 rillig * stmt */
797 1.54 rillig
798 1.54 rillig if (!*inout_sp_sw) { /* if not if for (;;) */
799 1.54 rillig parse(semicolon); /* let parser know about end of stmt */
800 1.86 rillig *inout_force_nl = true; /* force newline after an end of stmt */
801 1.54 rillig }
802 1.54 rillig }
803 1.54 rillig
804 1.54 rillig static void
805 1.75 rillig process_lbrace(bool *inout_force_nl, bool *inout_sp_sw, token_type hd_type,
806 1.86 rillig int *di_stack, int di_stack_cap, int *inout_dec_ind)
807 1.54 rillig {
808 1.86 rillig ps.in_stmt = false; /* dont indent the {} */
809 1.54 rillig if (!ps.block_init)
810 1.54 rillig *inout_force_nl = true; /* force other stuff on same line as '{' onto
811 1.54 rillig * new line */
812 1.54 rillig else if (ps.block_init_level <= 0)
813 1.54 rillig ps.block_init_level = 1;
814 1.54 rillig else
815 1.54 rillig ps.block_init_level++;
816 1.54 rillig
817 1.65 rillig if (code.s != code.e && !ps.block_init) {
818 1.54 rillig if (!opt.btype_2) {
819 1.54 rillig dump_line();
820 1.54 rillig ps.want_blank = false;
821 1.54 rillig } else if (ps.in_parameter_declaration && !ps.in_or_st) {
822 1.79 rillig ps.ind_level_follow = 0;
823 1.86 rillig if (opt.function_brace_split) { /* dump the line prior to the
824 1.86 rillig * brace ... */
825 1.54 rillig dump_line();
826 1.54 rillig ps.want_blank = false;
827 1.54 rillig } else /* add a space between the decl and brace */
828 1.54 rillig ps.want_blank = true;
829 1.54 rillig }
830 1.54 rillig }
831 1.54 rillig if (ps.in_parameter_declaration)
832 1.73 rillig prefix_blankline_requested = false;
833 1.54 rillig
834 1.86 rillig if (ps.p_l_follow > 0) { /* check for preceding unbalanced parens */
835 1.54 rillig diag(1, "Unbalanced parens");
836 1.54 rillig ps.p_l_follow = 0;
837 1.54 rillig if (*inout_sp_sw) { /* check for unclosed if, for, etc. */
838 1.54 rillig *inout_sp_sw = false;
839 1.54 rillig parse(hd_type);
840 1.79 rillig ps.ind_level = ps.ind_level_follow;
841 1.54 rillig }
842 1.54 rillig }
843 1.65 rillig if (code.s == code.e)
844 1.54 rillig ps.ind_stmt = false; /* dont put extra indentation on line
845 1.54 rillig * with '{' */
846 1.54 rillig if (ps.in_decl && ps.in_or_st) { /* this is either a structure
847 1.86 rillig * declaration or an init */
848 1.79 rillig di_stack[ps.decl_nest] = *inout_dec_ind;
849 1.79 rillig if (++ps.decl_nest == di_stack_cap) {
850 1.54 rillig diag(0, "Reached internal limit of %d struct levels",
851 1.86 rillig di_stack_cap);
852 1.79 rillig ps.decl_nest--;
853 1.54 rillig }
854 1.54 rillig /* ? dec_ind = 0; */
855 1.54 rillig } else {
856 1.86 rillig ps.decl_on_line = false; /* we can't be in the middle of a
857 1.86 rillig * declaration, so don't do special
858 1.86 rillig * indentation of comments */
859 1.54 rillig if (opt.blanklines_after_declarations_at_proctop
860 1.54 rillig && ps.in_parameter_declaration)
861 1.73 rillig postfix_blankline_requested = true;
862 1.73 rillig ps.in_parameter_declaration = false;
863 1.54 rillig ps.in_decl = false;
864 1.54 rillig }
865 1.54 rillig *inout_dec_ind = 0;
866 1.86 rillig parse(lbrace); /* let parser know about this */
867 1.86 rillig if (ps.want_blank) /* put a blank before '{' if '{' is not at
868 1.54 rillig * start of line */
869 1.65 rillig *code.e++ = ' ';
870 1.54 rillig ps.want_blank = false;
871 1.65 rillig *code.e++ = '{';
872 1.54 rillig ps.just_saw_decl = 0;
873 1.54 rillig }
874 1.54 rillig
875 1.54 rillig static void
876 1.75 rillig process_rbrace(bool *inout_sp_sw, int *inout_dec_ind, const int *di_stack)
877 1.54 rillig {
878 1.54 rillig if (ps.p_stack[ps.tos] == decl && !ps.block_init) /* semicolons can be
879 1.86 rillig * omitted in
880 1.86 rillig * declarations */
881 1.54 rillig parse(semicolon);
882 1.73 rillig if (ps.p_l_follow != 0) { /* check for unclosed if, for, else. */
883 1.54 rillig diag(1, "Unbalanced parens");
884 1.54 rillig ps.p_l_follow = 0;
885 1.54 rillig *inout_sp_sw = false;
886 1.54 rillig }
887 1.54 rillig ps.just_saw_decl = 0;
888 1.54 rillig ps.block_init_level--;
889 1.65 rillig if (code.s != code.e && !ps.block_init) { /* '}' must be first on line */
890 1.54 rillig if (opt.verbose)
891 1.54 rillig diag(0, "Line broken");
892 1.54 rillig dump_line();
893 1.54 rillig }
894 1.65 rillig *code.e++ = '}';
895 1.54 rillig ps.want_blank = true;
896 1.54 rillig ps.in_stmt = ps.ind_stmt = false;
897 1.79 rillig if (ps.decl_nest > 0) { /* we are in multi-level structure declaration */
898 1.79 rillig *inout_dec_ind = di_stack[--ps.decl_nest];
899 1.79 rillig if (ps.decl_nest == 0 && !ps.in_parameter_declaration)
900 1.54 rillig ps.just_saw_decl = 2;
901 1.54 rillig ps.in_decl = true;
902 1.54 rillig }
903 1.73 rillig prefix_blankline_requested = false;
904 1.54 rillig parse(rbrace); /* let parser know about this */
905 1.54 rillig ps.search_brace = opt.cuddle_else
906 1.86 rillig && ps.p_stack[ps.tos] == if_expr_stmt
907 1.86 rillig && ps.il[ps.tos] >= ps.ind_level;
908 1.79 rillig if (ps.tos <= 1 && opt.blanklines_after_procs && ps.decl_nest <= 0)
909 1.73 rillig postfix_blankline_requested = true;
910 1.54 rillig }
911 1.54 rillig
912 1.54 rillig static void
913 1.75 rillig process_keyword_do_else(bool *inout_force_nl, bool *inout_last_else)
914 1.54 rillig {
915 1.54 rillig ps.in_stmt = false;
916 1.67 rillig if (*token.s == 'e') {
917 1.65 rillig if (code.e != code.s && (!opt.cuddle_else || code.e[-1] != '}')) {
918 1.54 rillig if (opt.verbose)
919 1.54 rillig diag(0, "Line broken");
920 1.54 rillig dump_line(); /* make sure this starts a line */
921 1.54 rillig ps.want_blank = false;
922 1.54 rillig }
923 1.54 rillig *inout_force_nl = true;/* also, following stuff must go onto new line */
924 1.73 rillig *inout_last_else = true;
925 1.54 rillig parse(keyword_else);
926 1.54 rillig } else {
927 1.65 rillig if (code.e != code.s) { /* make sure this starts a line */
928 1.54 rillig if (opt.verbose)
929 1.54 rillig diag(0, "Line broken");
930 1.54 rillig dump_line();
931 1.54 rillig ps.want_blank = false;
932 1.54 rillig }
933 1.54 rillig *inout_force_nl = true;/* also, following stuff must go onto new line */
934 1.73 rillig *inout_last_else = false;
935 1.54 rillig parse(keyword_do);
936 1.54 rillig }
937 1.54 rillig }
938 1.54 rillig
939 1.54 rillig static void
940 1.75 rillig process_decl(int *out_dec_ind, bool *out_tabs_to_var)
941 1.54 rillig {
942 1.54 rillig parse(decl); /* let parser worry about indentation */
943 1.54 rillig if (ps.last_token == rparen && ps.tos <= 1) {
944 1.65 rillig if (code.s != code.e) {
945 1.54 rillig dump_line();
946 1.73 rillig ps.want_blank = false;
947 1.54 rillig }
948 1.54 rillig }
949 1.79 rillig if (ps.in_parameter_declaration && opt.indent_parameters &&
950 1.79 rillig ps.decl_nest == 0) {
951 1.79 rillig ps.ind_level = ps.ind_level_follow = 1;
952 1.73 rillig ps.ind_stmt = false;
953 1.54 rillig }
954 1.54 rillig ps.in_or_st = true; /* this might be a structure or initialization
955 1.54 rillig * declaration */
956 1.54 rillig ps.in_decl = ps.decl_on_line = ps.last_token != type_def;
957 1.79 rillig if ( /* !ps.in_or_st && */ ps.decl_nest <= 0)
958 1.54 rillig ps.just_saw_decl = 2;
959 1.73 rillig prefix_blankline_requested = false;
960 1.54 rillig
961 1.82 rillig int len = (int)strlen(token.s) + 1;
962 1.82 rillig int ind = ps.ind_level == 0 || ps.decl_nest > 0
963 1.82 rillig ? opt.decl_indent /* global variable or local member */
964 1.82 rillig : opt.local_decl_indent; /* local variable */
965 1.82 rillig *out_dec_ind = ind > 0 ? ind : len;
966 1.82 rillig *out_tabs_to_var = opt.use_tabs ? ind > 0 : false;
967 1.54 rillig }
968 1.54 rillig
969 1.54 rillig static void
970 1.75 rillig process_ident(token_type ttype, int dec_ind, bool tabs_to_var,
971 1.86 rillig bool *inout_sp_sw, bool *inout_force_nl, token_type hd_type)
972 1.54 rillig {
973 1.54 rillig if (ps.in_decl) {
974 1.64 rillig if (ttype == funcname) {
975 1.54 rillig ps.in_decl = false;
976 1.65 rillig if (opt.procnames_start_line && code.s != code.e) {
977 1.65 rillig *code.e = '\0';
978 1.54 rillig dump_line();
979 1.54 rillig } else if (ps.want_blank) {
980 1.65 rillig *code.e++ = ' ';
981 1.54 rillig }
982 1.54 rillig ps.want_blank = false;
983 1.54 rillig } else if (!ps.block_init && !ps.dumped_decl_indent &&
984 1.86 rillig ps.paren_level == 0) { /* if we are in a declaration, we must
985 1.86 rillig * indent identifier */
986 1.54 rillig indent_declaration(dec_ind, tabs_to_var);
987 1.54 rillig ps.dumped_decl_indent = true;
988 1.54 rillig ps.want_blank = false;
989 1.54 rillig }
990 1.54 rillig } else if (*inout_sp_sw && ps.p_l_follow == 0) {
991 1.54 rillig *inout_sp_sw = false;
992 1.54 rillig *inout_force_nl = true;
993 1.54 rillig ps.last_u_d = true;
994 1.54 rillig ps.in_stmt = false;
995 1.54 rillig parse(hd_type);
996 1.54 rillig }
997 1.54 rillig }
998 1.54 rillig
999 1.54 rillig static void
1000 1.54 rillig copy_id(void)
1001 1.54 rillig {
1002 1.67 rillig size_t len = token.e - token.s;
1003 1.54 rillig
1004 1.54 rillig check_size_code(len + 1);
1005 1.54 rillig if (ps.want_blank)
1006 1.65 rillig *code.e++ = ' ';
1007 1.67 rillig memcpy(code.e, token.s, len);
1008 1.65 rillig code.e += len;
1009 1.54 rillig }
1010 1.54 rillig
1011 1.54 rillig static void
1012 1.54 rillig process_string_prefix(void)
1013 1.54 rillig {
1014 1.67 rillig size_t len = token.e - token.s;
1015 1.54 rillig
1016 1.54 rillig check_size_code(len + 1);
1017 1.54 rillig if (ps.want_blank)
1018 1.65 rillig *code.e++ = ' ';
1019 1.67 rillig memcpy(code.e, token.s, len);
1020 1.65 rillig code.e += len;
1021 1.54 rillig
1022 1.54 rillig ps.want_blank = false;
1023 1.54 rillig }
1024 1.54 rillig
1025 1.54 rillig static void
1026 1.54 rillig process_period(void)
1027 1.54 rillig {
1028 1.85 rillig if (code.e[-1] == ',')
1029 1.85 rillig *code.e++ = ' ';
1030 1.65 rillig *code.e++ = '.'; /* move the period into line */
1031 1.54 rillig ps.want_blank = false; /* dont put a blank after a period */
1032 1.54 rillig }
1033 1.54 rillig
1034 1.54 rillig static void
1035 1.75 rillig process_comma(int dec_ind, bool tabs_to_var, bool *inout_force_nl)
1036 1.54 rillig {
1037 1.86 rillig ps.want_blank = (code.s != code.e); /* only put blank after comma if comma
1038 1.86 rillig * does not start the line */
1039 1.54 rillig if (ps.in_decl && ps.procname[0] == '\0' && !ps.block_init &&
1040 1.54 rillig !ps.dumped_decl_indent && ps.paren_level == 0) {
1041 1.54 rillig /* indent leading commas and not the actual identifiers */
1042 1.54 rillig indent_declaration(dec_ind - 1, tabs_to_var);
1043 1.54 rillig ps.dumped_decl_indent = true;
1044 1.54 rillig }
1045 1.65 rillig *code.e++ = ',';
1046 1.54 rillig if (ps.p_l_follow == 0) {
1047 1.54 rillig if (ps.block_init_level <= 0)
1048 1.73 rillig ps.block_init = false;
1049 1.87 rillig if (break_comma && (opt.break_after_comma ||
1050 1.54 rillig indentation_after_range(
1051 1.65 rillig compute_code_indent(), code.s, code.e)
1052 1.54 rillig >= opt.max_line_length - opt.tabsize))
1053 1.54 rillig *inout_force_nl = true;
1054 1.54 rillig }
1055 1.54 rillig }
1056 1.54 rillig
1057 1.54 rillig static void
1058 1.54 rillig process_preprocessing(void)
1059 1.54 rillig {
1060 1.65 rillig if (com.s != com.e || lab.s != lab.e || code.s != code.e)
1061 1.54 rillig dump_line();
1062 1.54 rillig check_size_label(1);
1063 1.86 rillig *lab.e++ = '#'; /* move whole line to 'label' buffer */
1064 1.54 rillig
1065 1.54 rillig {
1066 1.75 rillig bool in_comment = false;
1067 1.86 rillig int com_start = 0;
1068 1.86 rillig char quote = '\0';
1069 1.86 rillig int com_end = 0;
1070 1.54 rillig
1071 1.54 rillig while (*buf_ptr == ' ' || *buf_ptr == '\t') {
1072 1.54 rillig buf_ptr++;
1073 1.54 rillig if (buf_ptr >= buf_end)
1074 1.54 rillig fill_buffer();
1075 1.54 rillig }
1076 1.54 rillig while (*buf_ptr != '\n' || (in_comment && !had_eof)) {
1077 1.54 rillig check_size_label(2);
1078 1.63 rillig *lab.e = *buf_ptr++;
1079 1.54 rillig if (buf_ptr >= buf_end)
1080 1.54 rillig fill_buffer();
1081 1.63 rillig switch (*lab.e++) {
1082 1.54 rillig case '\\':
1083 1.54 rillig if (!in_comment) {
1084 1.63 rillig *lab.e++ = *buf_ptr++;
1085 1.54 rillig if (buf_ptr >= buf_end)
1086 1.54 rillig fill_buffer();
1087 1.54 rillig }
1088 1.54 rillig break;
1089 1.54 rillig case '/':
1090 1.55 rillig if (*buf_ptr == '*' && !in_comment && quote == '\0') {
1091 1.73 rillig in_comment = true;
1092 1.63 rillig *lab.e++ = *buf_ptr++;
1093 1.63 rillig com_start = (int)(lab.e - lab.s) - 2;
1094 1.54 rillig }
1095 1.54 rillig break;
1096 1.54 rillig case '"':
1097 1.54 rillig if (quote == '"')
1098 1.55 rillig quote = '\0';
1099 1.55 rillig else if (quote == '\0')
1100 1.55 rillig quote = '"';
1101 1.54 rillig break;
1102 1.54 rillig case '\'':
1103 1.54 rillig if (quote == '\'')
1104 1.55 rillig quote = '\0';
1105 1.55 rillig else if (quote == '\0')
1106 1.55 rillig quote = '\'';
1107 1.54 rillig break;
1108 1.54 rillig case '*':
1109 1.54 rillig if (*buf_ptr == '/' && in_comment) {
1110 1.73 rillig in_comment = false;
1111 1.63 rillig *lab.e++ = *buf_ptr++;
1112 1.63 rillig com_end = (int)(lab.e - lab.s);
1113 1.54 rillig }
1114 1.54 rillig break;
1115 1.54 rillig }
1116 1.54 rillig }
1117 1.54 rillig
1118 1.63 rillig while (lab.e > lab.s && (lab.e[-1] == ' ' || lab.e[-1] == '\t'))
1119 1.63 rillig lab.e--;
1120 1.63 rillig if (lab.e - lab.s == com_end && bp_save == NULL) {
1121 1.54 rillig /* comment on preprocessor line */
1122 1.86 rillig if (sc_end == NULL) { /* if this is the first comment, we
1123 1.86 rillig * must set up the buffer */
1124 1.54 rillig save_com = sc_buf;
1125 1.54 rillig sc_end = &save_com[0];
1126 1.54 rillig } else {
1127 1.86 rillig *sc_end++ = '\n'; /* add newline between comments */
1128 1.54 rillig *sc_end++ = ' ';
1129 1.54 rillig --line_no;
1130 1.54 rillig }
1131 1.54 rillig if (sc_end - save_com + com_end - com_start > sc_size)
1132 1.54 rillig errx(1, "input too long");
1133 1.63 rillig memmove(sc_end, lab.s + com_start, (size_t)(com_end - com_start));
1134 1.54 rillig sc_end += com_end - com_start;
1135 1.63 rillig lab.e = lab.s + com_start;
1136 1.63 rillig while (lab.e > lab.s && (lab.e[-1] == ' ' || lab.e[-1] == '\t'))
1137 1.63 rillig lab.e--;
1138 1.54 rillig bp_save = buf_ptr; /* save current input buffer */
1139 1.54 rillig be_save = buf_end;
1140 1.58 rillig buf_ptr = save_com; /* fix so that subsequent calls to lexi will
1141 1.58 rillig * take tokens out of save_com */
1142 1.54 rillig *sc_end++ = ' '; /* add trailing blank, just in case */
1143 1.54 rillig buf_end = sc_end;
1144 1.54 rillig sc_end = NULL;
1145 1.58 rillig debug_println("switched buf_ptr to save_com");
1146 1.54 rillig }
1147 1.54 rillig check_size_label(1);
1148 1.86 rillig *lab.e = '\0'; /* null terminate line */
1149 1.54 rillig ps.pcase = false;
1150 1.54 rillig }
1151 1.54 rillig
1152 1.86 rillig if (strncmp(lab.s, "#if", 3) == 0) { /* also ifdef, ifndef */
1153 1.54 rillig if ((size_t)ifdef_level < nitems(state_stack)) {
1154 1.54 rillig match_state[ifdef_level].tos = -1;
1155 1.54 rillig state_stack[ifdef_level++] = ps;
1156 1.54 rillig } else
1157 1.54 rillig diag(1, "#if stack overflow");
1158 1.86 rillig } else if (strncmp(lab.s, "#el", 3) == 0) { /* else, elif */
1159 1.54 rillig if (ifdef_level <= 0)
1160 1.63 rillig diag(1, lab.s[3] == 'i' ? "Unmatched #elif" : "Unmatched #else");
1161 1.54 rillig else {
1162 1.54 rillig match_state[ifdef_level - 1] = ps;
1163 1.54 rillig ps = state_stack[ifdef_level - 1];
1164 1.54 rillig }
1165 1.63 rillig } else if (strncmp(lab.s, "#endif", 6) == 0) {
1166 1.54 rillig if (ifdef_level <= 0)
1167 1.54 rillig diag(1, "Unmatched #endif");
1168 1.54 rillig else
1169 1.54 rillig ifdef_level--;
1170 1.54 rillig } else {
1171 1.63 rillig if (strncmp(lab.s + 1, "pragma", 6) != 0 &&
1172 1.63 rillig strncmp(lab.s + 1, "error", 5) != 0 &&
1173 1.63 rillig strncmp(lab.s + 1, "line", 4) != 0 &&
1174 1.63 rillig strncmp(lab.s + 1, "undef", 5) != 0 &&
1175 1.63 rillig strncmp(lab.s + 1, "define", 6) != 0 &&
1176 1.63 rillig strncmp(lab.s + 1, "include", 7) != 0) {
1177 1.54 rillig diag(1, "Unrecognized cpp directive");
1178 1.54 rillig return;
1179 1.54 rillig }
1180 1.54 rillig }
1181 1.54 rillig if (opt.blanklines_around_conditional_compilation) {
1182 1.73 rillig postfix_blankline_requested = true;
1183 1.54 rillig n_real_blanklines = 0;
1184 1.54 rillig } else {
1185 1.73 rillig postfix_blankline_requested = false;
1186 1.73 rillig prefix_blankline_requested = false;
1187 1.54 rillig }
1188 1.54 rillig
1189 1.54 rillig /*
1190 1.54 rillig * subsequent processing of the newline character will cause the line to
1191 1.54 rillig * be printed
1192 1.54 rillig */
1193 1.54 rillig }
1194 1.54 rillig
1195 1.60 rillig static void __attribute__((__noreturn__))
1196 1.53 rillig main_loop(void)
1197 1.53 rillig {
1198 1.64 rillig token_type ttype;
1199 1.75 rillig bool force_nl; /* when true, code must be broken */
1200 1.75 rillig bool last_else = false; /* true iff last keyword was an else */
1201 1.86 rillig int dec_ind; /* current indentation for declarations */
1202 1.86 rillig int di_stack[20]; /* a stack of structure indentation levels */
1203 1.75 rillig bool tabs_to_var; /* true if using tabs to indent to var name */
1204 1.75 rillig bool sp_sw; /* when true, we are in the expression of
1205 1.53 rillig * if(...), while(...), etc. */
1206 1.86 rillig token_type hd_type = end_of_file; /* used to store type of stmt for if
1207 1.86 rillig * (...), for (...), etc */
1208 1.73 rillig int squest; /* when this is positive, we have seen a '?'
1209 1.73 rillig * without the matching ':' in a <c>?<s>:<s>
1210 1.53 rillig * construct */
1211 1.84 rillig bool scase; /* set to true when we see a 'case', so we
1212 1.53 rillig * know what to do with the following colon */
1213 1.53 rillig
1214 1.53 rillig sp_sw = force_nl = false;
1215 1.53 rillig dec_ind = 0;
1216 1.79 rillig di_stack[ps.decl_nest = 0] = 0;
1217 1.53 rillig scase = false;
1218 1.53 rillig squest = 0;
1219 1.73 rillig tabs_to_var = false;
1220 1.1 cgd
1221 1.59 rillig for (;;) { /* this is the main loop. it will go until we
1222 1.1 cgd * reach eof */
1223 1.75 rillig bool comment_buffered = false;
1224 1.1 cgd
1225 1.64 rillig ttype = lexi(&ps); /* Read the next token. The actual characters
1226 1.64 rillig * read are stored in "token". */
1227 1.1 cgd
1228 1.25 kamil /*
1229 1.25 kamil * The following code moves newlines and comments following an if (),
1230 1.86 rillig * while (), else, etc. up to the start of the following stmt to a
1231 1.86 rillig * buffer. This allows proper handling of both kinds of brace
1232 1.25 kamil * placement (-br, -bl) and cuddling "else" (-ce).
1233 1.25 kamil */
1234 1.64 rillig search_brace(&ttype, &force_nl, &comment_buffered, &last_else);
1235 1.25 kamil
1236 1.64 rillig if (ttype == end_of_file) {
1237 1.54 rillig process_end_of_file();
1238 1.60 rillig /* NOTREACHED */
1239 1.54 rillig }
1240 1.25 kamil
1241 1.25 kamil if (
1242 1.64 rillig ttype != comment &&
1243 1.64 rillig ttype != newline &&
1244 1.64 rillig ttype != preprocessing &&
1245 1.64 rillig ttype != form_feed) {
1246 1.64 rillig process_comment_in_code(ttype, &force_nl);
1247 1.25 kamil
1248 1.86 rillig } else if (ttype != comment) /* preserve force_nl through a comment */
1249 1.25 kamil force_nl = false; /* cancel forced newline after newline, form
1250 1.25 kamil * feed, etc */
1251 1.1 cgd
1252 1.1 cgd
1253 1.1 cgd
1254 1.25 kamil /*-----------------------------------------------------*\
1255 1.25 kamil | do switch on type of token scanned |
1256 1.25 kamil \*-----------------------------------------------------*/
1257 1.65 rillig check_size_code(3); /* maximum number of increments of code.e
1258 1.34 rillig * before the next check_size_code or
1259 1.25 kamil * dump_line() is 2. After that there's the
1260 1.25 kamil * final increment for the null character. */
1261 1.64 rillig switch (ttype) {
1262 1.25 kamil
1263 1.64 rillig case form_feed:
1264 1.54 rillig process_form_feed();
1265 1.25 kamil break;
1266 1.25 kamil
1267 1.25 kamil case newline:
1268 1.54 rillig process_newline();
1269 1.25 kamil break;
1270 1.25 kamil
1271 1.25 kamil case lparen: /* got a '(' or '[' */
1272 1.54 rillig process_lparen_or_lbracket(dec_ind, tabs_to_var, sp_sw);
1273 1.25 kamil break;
1274 1.25 kamil
1275 1.25 kamil case rparen: /* got a ')' or ']' */
1276 1.54 rillig process_rparen_or_rbracket(&sp_sw, &force_nl, hd_type);
1277 1.25 kamil break;
1278 1.25 kamil
1279 1.25 kamil case unary_op: /* this could be any unary operation */
1280 1.54 rillig process_unary_op(dec_ind, tabs_to_var);
1281 1.25 kamil break;
1282 1.25 kamil
1283 1.36 rillig case binary_op: /* any binary operation */
1284 1.54 rillig process_binary_op();
1285 1.25 kamil break;
1286 1.25 kamil
1287 1.39 rillig case postfix_op: /* got a trailing ++ or -- */
1288 1.54 rillig process_postfix_op();
1289 1.25 kamil break;
1290 1.25 kamil
1291 1.25 kamil case question: /* got a ? */
1292 1.54 rillig process_question(&squest);
1293 1.25 kamil break;
1294 1.25 kamil
1295 1.38 rillig case case_label: /* got word 'case' or 'default' */
1296 1.25 kamil scase = true; /* so we can process the later colon properly */
1297 1.25 kamil goto copy_id;
1298 1.25 kamil
1299 1.25 kamil case colon: /* got a ':' */
1300 1.54 rillig process_colon(&squest, &force_nl, &scase);
1301 1.25 kamil break;
1302 1.25 kamil
1303 1.54 rillig case semicolon: /* got a ';' */
1304 1.54 rillig process_semicolon(&scase, &squest, dec_ind, tabs_to_var, &sp_sw,
1305 1.54 rillig hd_type, &force_nl);
1306 1.25 kamil break;
1307 1.25 kamil
1308 1.25 kamil case lbrace: /* got a '{' */
1309 1.54 rillig process_lbrace(&force_nl, &sp_sw, hd_type, di_stack,
1310 1.59 rillig (int)nitems(di_stack), &dec_ind);
1311 1.25 kamil break;
1312 1.25 kamil
1313 1.25 kamil case rbrace: /* got a '}' */
1314 1.54 rillig process_rbrace(&sp_sw, &dec_ind, di_stack);
1315 1.25 kamil break;
1316 1.25 kamil
1317 1.38 rillig case switch_expr: /* got keyword "switch" */
1318 1.25 kamil sp_sw = true;
1319 1.86 rillig hd_type = switch_expr; /* keep this for when we have seen the
1320 1.86 rillig * expression */
1321 1.25 kamil goto copy_id; /* go move the token into buffer */
1322 1.25 kamil
1323 1.37 rillig case keyword_for_if_while:
1324 1.25 kamil sp_sw = true; /* the interesting stuff is done after the
1325 1.25 kamil * expression is scanned */
1326 1.67 rillig hd_type = (*token.s == 'i' ? if_expr :
1327 1.86 rillig (*token.s == 'w' ? while_expr : for_exprs));
1328 1.25 kamil
1329 1.54 rillig /* remember the type of header for later use by parser */
1330 1.25 kamil goto copy_id; /* copy the token into line */
1331 1.25 kamil
1332 1.37 rillig case keyword_do_else:
1333 1.54 rillig process_keyword_do_else(&force_nl, &last_else);
1334 1.25 kamil goto copy_id; /* move the token into line */
1335 1.25 kamil
1336 1.25 kamil case type_def:
1337 1.39 rillig case storage_class:
1338 1.73 rillig prefix_blankline_requested = false;
1339 1.25 kamil goto copy_id;
1340 1.25 kamil
1341 1.39 rillig case keyword_struct_union_enum:
1342 1.25 kamil if (ps.p_l_follow > 0)
1343 1.25 kamil goto copy_id;
1344 1.25 kamil /* FALLTHROUGH */
1345 1.25 kamil case decl: /* we have a declaration type (int, etc.) */
1346 1.54 rillig process_decl(&dec_ind, &tabs_to_var);
1347 1.25 kamil goto copy_id;
1348 1.25 kamil
1349 1.25 kamil case funcname:
1350 1.25 kamil case ident: /* got an identifier or constant */
1351 1.64 rillig process_ident(ttype, dec_ind, tabs_to_var, &sp_sw, &force_nl,
1352 1.86 rillig hd_type);
1353 1.25 kamil copy_id:
1354 1.54 rillig copy_id();
1355 1.64 rillig if (ttype != funcname)
1356 1.25 kamil ps.want_blank = true;
1357 1.25 kamil break;
1358 1.25 kamil
1359 1.39 rillig case string_prefix:
1360 1.54 rillig process_string_prefix();
1361 1.25 kamil break;
1362 1.1 cgd
1363 1.54 rillig case period:
1364 1.54 rillig process_period();
1365 1.25 kamil break;
1366 1.25 kamil
1367 1.25 kamil case comma:
1368 1.54 rillig process_comma(dec_ind, tabs_to_var, &force_nl);
1369 1.25 kamil break;
1370 1.25 kamil
1371 1.39 rillig case preprocessing: /* '#' */
1372 1.54 rillig process_preprocessing();
1373 1.54 rillig break;
1374 1.56 rillig case comment: /* the initial '/' '*' or '//' of a comment */
1375 1.56 rillig process_comment();
1376 1.25 kamil break;
1377 1.30 rillig
1378 1.30 rillig default:
1379 1.30 rillig break;
1380 1.64 rillig }
1381 1.25 kamil
1382 1.65 rillig *code.e = '\0';
1383 1.64 rillig if (ttype != comment &&
1384 1.64 rillig ttype != newline &&
1385 1.64 rillig ttype != preprocessing)
1386 1.64 rillig ps.last_token = ttype;
1387 1.53 rillig }
1388 1.53 rillig }
1389 1.53 rillig
1390 1.53 rillig int
1391 1.53 rillig main(int argc, char **argv)
1392 1.53 rillig {
1393 1.53 rillig main_init_globals();
1394 1.53 rillig main_parse_command_line(argc, argv);
1395 1.53 rillig #if HAVE_CAPSICUM
1396 1.53 rillig init_capsicum();
1397 1.53 rillig #endif
1398 1.53 rillig main_prepare_parsing();
1399 1.53 rillig main_loop();
1400 1.25 kamil }
1401 1.6 lukem
1402 1.1 cgd /*
1403 1.1 cgd * copy input file to backup file if in_name is /blah/blah/blah/file, then
1404 1.1 cgd * backup file will be ".Bfile" then make the backup file the input and
1405 1.1 cgd * original input file the output
1406 1.1 cgd */
1407 1.25 kamil static void
1408 1.13 wiz bakcopy(void)
1409 1.1 cgd {
1410 1.59 rillig ssize_t n;
1411 1.59 rillig int bakchn;
1412 1.59 rillig char buff[8 * 1024];
1413 1.25 kamil const char *p;
1414 1.25 kamil
1415 1.25 kamil /* construct file name .Bfile */
1416 1.73 rillig for (p = in_name; *p != '\0'; p++); /* skip to end of string */
1417 1.25 kamil while (p > in_name && *p != '/') /* find last '/' */
1418 1.25 kamil p--;
1419 1.25 kamil if (*p == '/')
1420 1.25 kamil p++;
1421 1.25 kamil sprintf(bakfile, "%s%s", p, simple_backup_suffix);
1422 1.25 kamil
1423 1.25 kamil /* copy in_name to backup file */
1424 1.25 kamil bakchn = creat(bakfile, 0600);
1425 1.25 kamil if (bakchn < 0)
1426 1.25 kamil err(1, "%s", bakfile);
1427 1.25 kamil while ((n = read(fileno(input), buff, sizeof(buff))) > 0)
1428 1.59 rillig if (write(bakchn, buff, (size_t)n) != n)
1429 1.25 kamil err(1, "%s", bakfile);
1430 1.25 kamil if (n < 0)
1431 1.25 kamil err(1, "%s", in_name);
1432 1.25 kamil close(bakchn);
1433 1.25 kamil fclose(input);
1434 1.25 kamil
1435 1.25 kamil /* re-open backup file as the input file */
1436 1.25 kamil input = fopen(bakfile, "r");
1437 1.25 kamil if (input == NULL)
1438 1.25 kamil err(1, "%s", bakfile);
1439 1.25 kamil /* now the original input file will be the output */
1440 1.25 kamil output = fopen(in_name, "w");
1441 1.25 kamil if (output == NULL) {
1442 1.25 kamil unlink(bakfile);
1443 1.25 kamil err(1, "%s", in_name);
1444 1.25 kamil }
1445 1.25 kamil }
1446 1.25 kamil
1447 1.25 kamil static void
1448 1.75 rillig indent_declaration(int cur_dec_ind, bool tabs_to_var)
1449 1.25 kamil {
1450 1.65 rillig int pos = (int)(code.e - code.s);
1451 1.65 rillig char *startpos = code.e;
1452 1.25 kamil
1453 1.25 kamil /*
1454 1.86 rillig * get the tab math right for indentations that are not multiples of
1455 1.86 rillig * tabsize
1456 1.25 kamil */
1457 1.57 rillig if ((ps.ind_level * opt.indent_size) % opt.tabsize != 0) {
1458 1.57 rillig pos += (ps.ind_level * opt.indent_size) % opt.tabsize;
1459 1.57 rillig cur_dec_ind += (ps.ind_level * opt.indent_size) % opt.tabsize;
1460 1.25 kamil }
1461 1.25 kamil if (tabs_to_var) {
1462 1.25 kamil int tpos;
1463 1.25 kamil
1464 1.61 rillig check_size_code((size_t)(cur_dec_ind / opt.tabsize));
1465 1.25 kamil while ((tpos = opt.tabsize * (1 + pos / opt.tabsize)) <= cur_dec_ind) {
1466 1.65 rillig *code.e++ = '\t';
1467 1.25 kamil pos = tpos;
1468 1.6 lukem }
1469 1.25 kamil }
1470 1.61 rillig check_size_code((size_t)(cur_dec_ind - pos + 1));
1471 1.25 kamil while (pos < cur_dec_ind) {
1472 1.65 rillig *code.e++ = ' ';
1473 1.25 kamil pos++;
1474 1.25 kamil }
1475 1.65 rillig if (code.e == startpos && ps.want_blank) {
1476 1.65 rillig *code.e++ = ' ';
1477 1.25 kamil ps.want_blank = false;
1478 1.25 kamil }
1479 1.1 cgd }
1480 1.48 rillig
1481 1.48 rillig #ifdef debug
1482 1.48 rillig void
1483 1.48 rillig debug_printf(const char *fmt, ...)
1484 1.48 rillig {
1485 1.48 rillig FILE *f = output == stdout ? stderr : stdout;
1486 1.48 rillig va_list ap;
1487 1.48 rillig
1488 1.48 rillig va_start(ap, fmt);
1489 1.48 rillig vfprintf(f, fmt, ap);
1490 1.48 rillig va_end(ap);
1491 1.48 rillig }
1492 1.48 rillig
1493 1.48 rillig void
1494 1.48 rillig debug_println(const char *fmt, ...)
1495 1.48 rillig {
1496 1.48 rillig FILE *f = output == stdout ? stderr : stdout;
1497 1.48 rillig va_list ap;
1498 1.48 rillig
1499 1.48 rillig va_start(ap, fmt);
1500 1.48 rillig vfprintf(f, fmt, ap);
1501 1.48 rillig va_end(ap);
1502 1.48 rillig fprintf(f, "\n");
1503 1.48 rillig }
1504 1.48 rillig
1505 1.48 rillig void
1506 1.48 rillig debug_vis_range(const char *prefix, const char *s, const char *e,
1507 1.86 rillig const char *suffix)
1508 1.48 rillig {
1509 1.48 rillig debug_printf("%s", prefix);
1510 1.48 rillig for (const char *p = s; p < e; p++) {
1511 1.48 rillig if (isprint((unsigned char)*p) && *p != '\\' && *p != '"')
1512 1.48 rillig debug_printf("%c", *p);
1513 1.48 rillig else if (*p == '\n')
1514 1.48 rillig debug_printf("\\n");
1515 1.48 rillig else if (*p == '\t')
1516 1.48 rillig debug_printf("\\t");
1517 1.48 rillig else
1518 1.48 rillig debug_printf("\\x%02x", *p);
1519 1.48 rillig }
1520 1.48 rillig debug_printf("%s", suffix);
1521 1.48 rillig }
1522 1.48 rillig #endif
1523 1.68 rillig
1524 1.68 rillig static void *
1525 1.68 rillig nonnull(void *p)
1526 1.68 rillig {
1527 1.68 rillig if (p == NULL)
1528 1.68 rillig err(EXIT_FAILURE, NULL);
1529 1.68 rillig return p;
1530 1.68 rillig }
1531 1.68 rillig
1532 1.68 rillig void *
1533 1.68 rillig xmalloc(size_t size)
1534 1.68 rillig {
1535 1.68 rillig return nonnull(malloc(size));
1536 1.68 rillig }
1537 1.68 rillig
1538 1.68 rillig void *
1539 1.68 rillig xrealloc(void *p, size_t new_size)
1540 1.68 rillig {
1541 1.68 rillig return nonnull(realloc(p, new_size));
1542 1.68 rillig }
1543 1.68 rillig
1544 1.68 rillig char *
1545 1.68 rillig xstrdup(const char *s)
1546 1.68 rillig {
1547 1.68 rillig return nonnull(strdup(s));
1548 1.68 rillig }
1549