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