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