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