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