io.c revision 1.150 1 1.150 rillig /* $NetBSD: io.c,v 1.150 2023/05/11 18:13:55 rillig Exp $ */
2 1.3 tls
3 1.19 kamil /*-
4 1.19 kamil * SPDX-License-Identifier: BSD-4-Clause
5 1.19 kamil *
6 1.19 kamil * Copyright (c) 1985 Sun Microsystems, Inc.
7 1.4 mrg * Copyright (c) 1980, 1993
8 1.4 mrg * The Regents of the University of California. All rights reserved.
9 1.1 cgd * 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.19 kamil #if 0
41 1.19 kamil static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93";
42 1.19 kamil #endif
43 1.19 kamil
44 1.5 lukem #include <sys/cdefs.h>
45 1.19 kamil #if defined(__NetBSD__)
46 1.150 rillig __RCSID("$NetBSD: io.c,v 1.150 2023/05/11 18:13:55 rillig Exp $");
47 1.19 kamil #elif defined(__FreeBSD__)
48 1.19 kamil __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
49 1.19 kamil #endif
50 1.1 cgd
51 1.121 rillig #include <assert.h>
52 1.1 cgd #include <stdio.h>
53 1.120 rillig #include <stdlib.h>
54 1.1 cgd #include <string.h>
55 1.22 rillig
56 1.19 kamil #include "indent.h"
57 1.1 cgd
58 1.143 rillig /*
59 1.143 rillig * There are 3 modes for reading the input.
60 1.143 rillig *
61 1.143 rillig * default: In this mode, the input comes from the input file. The buffer
62 1.143 rillig * 'inp' contains the current line, terminated with '\n'. The current read
63 1.143 rillig * position is inp.s, and there is always inp.buf <= inp.s < inp.e. All other
64 1.143 rillig * pointers are null.
65 1.143 rillig *
66 1.143 rillig * copy-in: After reading 'if (expr)' or similar tokens, the input still comes
67 1.143 rillig * from 'inp', but instead of processing it, it is copied to 'save_com'. The
68 1.143 rillig * goal of this mode is to move the comments after the '{', that is to
69 1.143 rillig * transform 'if (expr) comment {' to 'if (expr) { comment'. When the next
70 1.143 rillig * token cannot be part of this transformation, switch to copy-out.
71 1.143 rillig *
72 1.143 rillig * copy-out: In this mode, the input comes from 'save_com', which contains the
73 1.143 rillig * tokens to be placed after the '{'. The input still comes from the range
74 1.143 rillig * [inp.s, inp.e), but these two members have been overwritten with pointers
75 1.143 rillig * into save_com_buf, so inp.buf and inp.s are unrelated, which is unusual.
76 1.143 rillig * In this mode, inp.e[-1] is usually not terminated with '\n'. After reading
77 1.143 rillig * all tokens from save_com, switch to default mode again.
78 1.143 rillig */
79 1.128 rillig static struct {
80 1.124 rillig struct buffer inp; /* one line of input, ready to be split into
81 1.143 rillig * tokens; occasionally 's' and 'e' switch
82 1.124 rillig * to save_com_buf */
83 1.124 rillig char save_com_buf[5000]; /* input text is saved here when looking for
84 1.124 rillig * the brace after an if, while, etc */
85 1.143 rillig char *save_com_s; /* start of the comment in save_com_buf, or
86 1.143 rillig * null */
87 1.143 rillig char *save_com_e; /* end of the comment in save_com_buf, or
88 1.143 rillig * null */
89 1.124 rillig
90 1.124 rillig char *saved_inp_s; /* saved value of inp.s when taking input from
91 1.143 rillig * save_com, or null */
92 1.143 rillig char *saved_inp_e; /* saved value of inp.e, or null */
93 1.124 rillig } inbuf;
94 1.120 rillig
95 1.67 rillig static int paren_indent;
96 1.1 cgd
97 1.118 rillig
98 1.122 rillig void
99 1.122 rillig inp_init(void)
100 1.122 rillig {
101 1.122 rillig inbuf.inp.buf = xmalloc(10);
102 1.122 rillig inbuf.inp.l = inbuf.inp.buf + 8;
103 1.122 rillig inbuf.inp.s = inbuf.inp.buf;
104 1.122 rillig inbuf.inp.e = inbuf.inp.buf;
105 1.122 rillig }
106 1.122 rillig
107 1.119 rillig const char *
108 1.119 rillig inp_p(void)
109 1.119 rillig {
110 1.140 rillig assert(inbuf.inp.s < inbuf.inp.e);
111 1.119 rillig return inbuf.inp.s;
112 1.119 rillig }
113 1.119 rillig
114 1.119 rillig const char *
115 1.123 rillig inp_line_start(void)
116 1.123 rillig {
117 1.133 rillig return inbuf.saved_inp_s != NULL ? inbuf.save_com_buf : inbuf.inp.buf;
118 1.123 rillig }
119 1.123 rillig
120 1.123 rillig const char *
121 1.119 rillig inp_line_end(void)
122 1.119 rillig {
123 1.119 rillig return inbuf.inp.e;
124 1.119 rillig }
125 1.119 rillig
126 1.118 rillig char
127 1.118 rillig inp_peek(void)
128 1.118 rillig {
129 1.140 rillig assert(inbuf.inp.s < inbuf.inp.e);
130 1.118 rillig return *inbuf.inp.s;
131 1.118 rillig }
132 1.118 rillig
133 1.118 rillig char
134 1.118 rillig inp_lookahead(size_t i)
135 1.118 rillig {
136 1.140 rillig assert(i < (size_t)(inbuf.inp.e - inbuf.inp.s));
137 1.118 rillig return inbuf.inp.s[i];
138 1.118 rillig }
139 1.118 rillig
140 1.118 rillig void
141 1.118 rillig inp_skip(void)
142 1.118 rillig {
143 1.140 rillig assert(inbuf.inp.s < inbuf.inp.e);
144 1.118 rillig inbuf.inp.s++;
145 1.118 rillig if (inbuf.inp.s >= inbuf.inp.e)
146 1.118 rillig inp_read_line();
147 1.118 rillig }
148 1.118 rillig
149 1.118 rillig char
150 1.118 rillig inp_next(void)
151 1.118 rillig {
152 1.118 rillig char ch = inp_peek();
153 1.118 rillig inp_skip();
154 1.118 rillig return ch;
155 1.118 rillig }
156 1.118 rillig
157 1.120 rillig #ifdef debug
158 1.139 rillig static void
159 1.139 rillig debug_inp_buf(const char *name, const char *s, const char *e)
160 1.139 rillig {
161 1.139 rillig if (s != NULL && e != NULL) {
162 1.139 rillig debug_printf(" %-12s ", name);
163 1.139 rillig debug_vis_range("\"", s, e, "\"\n");
164 1.139 rillig }
165 1.139 rillig }
166 1.139 rillig
167 1.120 rillig void
168 1.120 rillig debug_inp(const char *prefix)
169 1.120 rillig {
170 1.143 rillig assert(inp_line_start() <= inbuf.inp.s);
171 1.143 rillig assert(inbuf.inp.s <= inbuf.inp.e);
172 1.143 rillig
173 1.139 rillig debug_println("%s %s:", __func__, prefix);
174 1.139 rillig if (inbuf.saved_inp_s == NULL)
175 1.139 rillig debug_inp_buf("inp.buf", inbuf.inp.buf, inbuf.inp.s);
176 1.139 rillig debug_inp_buf("inp", inbuf.inp.s, inbuf.inp.e); /* never null */
177 1.139 rillig debug_inp_buf("save_com.buf", inbuf.save_com_buf, inbuf.save_com_s);
178 1.139 rillig debug_inp_buf("save_com", inbuf.save_com_s, inbuf.save_com_e);
179 1.139 rillig debug_inp_buf("saved_inp", inbuf.saved_inp_s, inbuf.saved_inp_e);
180 1.120 rillig }
181 1.120 rillig #endif
182 1.120 rillig
183 1.120 rillig static void
184 1.120 rillig inp_comment_check_size(size_t n)
185 1.120 rillig {
186 1.120 rillig if ((size_t)(inbuf.save_com_e - inbuf.save_com_buf) + n <=
187 1.120 rillig array_length(inbuf.save_com_buf))
188 1.120 rillig return;
189 1.120 rillig
190 1.120 rillig diag(1, "Internal buffer overflow - "
191 1.142 rillig "Move big comment from right after if, while, or whatever");
192 1.120 rillig fflush(output);
193 1.120 rillig exit(1);
194 1.120 rillig }
195 1.120 rillig
196 1.120 rillig void
197 1.121 rillig inp_comment_init_newline(void)
198 1.121 rillig {
199 1.121 rillig if (inbuf.save_com_e != NULL)
200 1.121 rillig return;
201 1.121 rillig
202 1.121 rillig inbuf.save_com_s = inbuf.save_com_buf;
203 1.132 rillig inbuf.save_com_s[0] = ' '; /* see inp_comment_insert_lbrace */
204 1.132 rillig inbuf.save_com_s[1] = ' '; /* see inp_comment_insert_lbrace */
205 1.121 rillig inbuf.save_com_e = &inbuf.save_com_s[2];
206 1.121 rillig debug_inp(__func__);
207 1.121 rillig }
208 1.121 rillig
209 1.121 rillig void
210 1.121 rillig inp_comment_init_comment(void)
211 1.121 rillig {
212 1.121 rillig if (inbuf.save_com_e != NULL)
213 1.121 rillig return;
214 1.121 rillig
215 1.121 rillig /*
216 1.142 rillig * Copy everything from the start of the line, because process_comment()
217 1.142 rillig * will use that to calculate the original indentation of a boxed comment.
218 1.121 rillig */
219 1.121 rillig /*
220 1.121 rillig * TODO: Don't store anything in the memory range [input.inp.buf,
221 1.121 rillig * input.inp.s), as that data can easily get lost.
222 1.121 rillig */
223 1.121 rillig /*
224 1.126 rillig * FIXME: The '4' below is completely wrong. For example, in the snippet
225 1.142 rillig * 'if(expr)/''*comment', the 'r)' of the code is not copied. If there is
226 1.142 rillig * an additional line break before the ')', memcpy tries to copy
227 1.121 rillig * (size_t)-1 bytes.
228 1.126 rillig *
229 1.126 rillig * The original author of this magic number doesn't remember its purpose
230 1.126 rillig * anymore, so there is no point in keeping it. The existing tests must
231 1.126 rillig * still pass though.
232 1.121 rillig */
233 1.121 rillig assert((size_t)(inbuf.inp.s - inbuf.inp.buf) >= 4);
234 1.121 rillig size_t line_len = (size_t)(inbuf.inp.s - inbuf.inp.buf) - 4;
235 1.121 rillig assert(line_len < array_length(inbuf.save_com_buf));
236 1.139 rillig
237 1.121 rillig memcpy(inbuf.save_com_buf, inbuf.inp.buf, line_len);
238 1.121 rillig inbuf.save_com_s = inbuf.save_com_buf + line_len;
239 1.139 rillig
240 1.132 rillig inbuf.save_com_s[0] = ' '; /* see inp_comment_insert_lbrace */
241 1.132 rillig inbuf.save_com_s[1] = ' '; /* see inp_comment_insert_lbrace */
242 1.121 rillig inbuf.save_com_e = &inbuf.save_com_s[2];
243 1.139 rillig
244 1.121 rillig debug_vis_range("search_stmt_comment: before save_com is \"",
245 1.126 rillig inbuf.save_com_buf, inbuf.save_com_s, "\"\n");
246 1.121 rillig debug_vis_range("search_stmt_comment: save_com is \"",
247 1.126 rillig inbuf.save_com_s, inbuf.save_com_e, "\"\n");
248 1.121 rillig }
249 1.121 rillig
250 1.121 rillig void
251 1.122 rillig inp_comment_init_preproc(void)
252 1.122 rillig {
253 1.122 rillig if (inbuf.save_com_e == NULL) { /* if this is the first comment, we
254 1.122 rillig * must set up the buffer */
255 1.143 rillig /*
256 1.143 rillig * XXX: No space is reserved for a potential '{' here, unlike in
257 1.143 rillig * inp_comment_init_comment.
258 1.143 rillig */
259 1.122 rillig inbuf.save_com_s = inbuf.save_com_buf;
260 1.122 rillig inbuf.save_com_e = inbuf.save_com_s;
261 1.122 rillig } else {
262 1.122 rillig inp_comment_add_char('\n'); /* add newline between comments */
263 1.122 rillig inp_comment_add_char(' ');
264 1.122 rillig --line_no;
265 1.122 rillig }
266 1.122 rillig }
267 1.122 rillig
268 1.122 rillig void
269 1.120 rillig inp_comment_add_char(char ch)
270 1.120 rillig {
271 1.120 rillig inp_comment_check_size(1);
272 1.120 rillig *inbuf.save_com_e++ = ch;
273 1.120 rillig }
274 1.120 rillig
275 1.120 rillig void
276 1.120 rillig inp_comment_add_range(const char *s, const char *e)
277 1.120 rillig {
278 1.120 rillig size_t len = (size_t)(e - s);
279 1.120 rillig inp_comment_check_size(len);
280 1.120 rillig memcpy(inbuf.save_com_e, s, len);
281 1.120 rillig inbuf.save_com_e += len;
282 1.120 rillig }
283 1.120 rillig
284 1.121 rillig bool
285 1.121 rillig inp_comment_complete_block(void)
286 1.121 rillig {
287 1.121 rillig return inbuf.save_com_e[-2] == '*' && inbuf.save_com_e[-1] == '/';
288 1.121 rillig }
289 1.121 rillig
290 1.122 rillig bool
291 1.122 rillig inp_comment_seen(void)
292 1.122 rillig {
293 1.122 rillig return inbuf.save_com_e != NULL;
294 1.122 rillig }
295 1.122 rillig
296 1.122 rillig void
297 1.143 rillig inp_comment_rtrim_blank(void)
298 1.122 rillig {
299 1.143 rillig while (inbuf.save_com_e > inbuf.save_com_s &&
300 1.143 rillig ch_isblank(inbuf.save_com_e[-1]))
301 1.122 rillig inbuf.save_com_e--;
302 1.122 rillig }
303 1.122 rillig
304 1.122 rillig void
305 1.122 rillig inp_comment_rtrim_newline(void)
306 1.122 rillig {
307 1.143 rillig while (inbuf.save_com_e > inbuf.save_com_s &&
308 1.143 rillig inbuf.save_com_e[-1] == '\n')
309 1.122 rillig inbuf.save_com_e--;
310 1.122 rillig }
311 1.122 rillig
312 1.143 rillig /*
313 1.143 rillig * Switch the input to come from save_com, replaying the copied tokens while
314 1.143 rillig * looking for the next '{'.
315 1.143 rillig */
316 1.120 rillig void
317 1.120 rillig inp_from_comment(void)
318 1.120 rillig {
319 1.139 rillig debug_inp("before inp_from_comment");
320 1.120 rillig inbuf.saved_inp_s = inbuf.inp.s;
321 1.120 rillig inbuf.saved_inp_e = inbuf.inp.e;
322 1.120 rillig
323 1.143 rillig inbuf.inp.s = inbuf.save_com_s;
324 1.120 rillig inbuf.inp.e = inbuf.save_com_e;
325 1.128 rillig inbuf.save_com_s = NULL;
326 1.120 rillig inbuf.save_com_e = NULL;
327 1.139 rillig debug_inp("after inp_from_comment");
328 1.120 rillig }
329 1.118 rillig
330 1.136 rillig /*
331 1.136 rillig * After having read from save_com, continue with the rest of the input line
332 1.136 rillig * before reading the next line from the input file.
333 1.136 rillig */
334 1.136 rillig static bool
335 1.136 rillig inp_from_file(void)
336 1.136 rillig {
337 1.136 rillig if (inbuf.saved_inp_s == NULL)
338 1.136 rillig return false;
339 1.136 rillig
340 1.136 rillig inbuf.inp.s = inbuf.saved_inp_s;
341 1.136 rillig inbuf.inp.e = inbuf.saved_inp_e;
342 1.136 rillig inbuf.saved_inp_s = inbuf.saved_inp_e = NULL;
343 1.136 rillig debug_println("switched inp.s back to saved_inp_s");
344 1.136 rillig return inbuf.inp.s < inbuf.inp.e;
345 1.136 rillig }
346 1.136 rillig
347 1.122 rillig void
348 1.122 rillig inp_comment_insert_lbrace(void)
349 1.122 rillig {
350 1.122 rillig assert(inbuf.save_com_s[0] == ' '); /* see inp_comment_init_newline */
351 1.122 rillig inbuf.save_com_s[0] = '{';
352 1.122 rillig }
353 1.121 rillig
354 1.138 rillig static void
355 1.138 rillig inp_add(char ch)
356 1.137 rillig {
357 1.138 rillig if (inbuf.inp.e >= inbuf.inp.l) {
358 1.138 rillig size_t new_size = (size_t)(inbuf.inp.l - inbuf.inp.buf) * 2 + 10;
359 1.138 rillig size_t offset = (size_t)(inbuf.inp.e - inbuf.inp.buf);
360 1.138 rillig inbuf.inp.buf = xrealloc(inbuf.inp.buf, new_size);
361 1.138 rillig inbuf.inp.s = inbuf.inp.buf;
362 1.138 rillig inbuf.inp.e = inbuf.inp.buf + offset;
363 1.138 rillig inbuf.inp.l = inbuf.inp.buf + new_size - 2;
364 1.138 rillig }
365 1.138 rillig *inbuf.inp.e++ = ch;
366 1.137 rillig }
367 1.137 rillig
368 1.137 rillig static void
369 1.137 rillig inp_read_next_line(FILE *f)
370 1.137 rillig {
371 1.138 rillig inbuf.inp.s = inbuf.inp.buf;
372 1.138 rillig inbuf.inp.e = inbuf.inp.buf;
373 1.137 rillig
374 1.138 rillig for (;;) {
375 1.138 rillig int ch = getc(f);
376 1.138 rillig if (ch == EOF) {
377 1.137 rillig if (!inhibit_formatting) {
378 1.138 rillig inp_add(' ');
379 1.138 rillig inp_add('\n');
380 1.137 rillig }
381 1.137 rillig had_eof = true;
382 1.137 rillig break;
383 1.137 rillig }
384 1.137 rillig
385 1.137 rillig if (ch != '\0')
386 1.138 rillig inp_add((char)ch);
387 1.137 rillig if (ch == '\n')
388 1.137 rillig break;
389 1.137 rillig }
390 1.137 rillig }
391 1.137 rillig
392 1.28 rillig static void
393 1.28 rillig output_char(char ch)
394 1.28 rillig {
395 1.28 rillig fputc(ch, output);
396 1.36 rillig debug_vis_range("output_char '", &ch, &ch + 1, "'\n");
397 1.28 rillig }
398 1.28 rillig
399 1.28 rillig static void
400 1.28 rillig output_range(const char *s, const char *e)
401 1.28 rillig {
402 1.28 rillig fwrite(s, 1, (size_t)(e - s), output);
403 1.36 rillig debug_vis_range("output_range \"", s, e, "\"\n");
404 1.28 rillig }
405 1.28 rillig
406 1.34 rillig static int
407 1.34 rillig output_indent(int old_ind, int new_ind)
408 1.34 rillig {
409 1.34 rillig int ind = old_ind;
410 1.34 rillig
411 1.34 rillig if (opt.use_tabs) {
412 1.34 rillig int tabsize = opt.tabsize;
413 1.34 rillig int n = new_ind / tabsize - ind / tabsize;
414 1.34 rillig if (n > 0)
415 1.34 rillig ind -= ind % tabsize;
416 1.34 rillig for (int i = 0; i < n; i++) {
417 1.36 rillig fputc('\t', output);
418 1.34 rillig ind += tabsize;
419 1.34 rillig }
420 1.34 rillig }
421 1.34 rillig
422 1.34 rillig for (; ind < new_ind; ind++)
423 1.67 rillig fputc(' ', output);
424 1.34 rillig
425 1.36 rillig debug_println("output_indent %d", ind);
426 1.34 rillig return ind;
427 1.34 rillig }
428 1.34 rillig
429 1.86 rillig static int
430 1.141 rillig output_line_label(void)
431 1.86 rillig {
432 1.86 rillig int ind;
433 1.86 rillig
434 1.107 rillig while (lab.e > lab.s && ch_isblank(lab.e[-1]))
435 1.86 rillig lab.e--;
436 1.86 rillig *lab.e = '\0';
437 1.86 rillig
438 1.86 rillig ind = output_indent(0, compute_label_indent());
439 1.135 rillig output_range(lab.s, lab.e);
440 1.109 rillig ind = ind_add(ind, lab.s, lab.e);
441 1.86 rillig
442 1.86 rillig ps.is_case_label = false;
443 1.86 rillig return ind;
444 1.86 rillig }
445 1.86 rillig
446 1.86 rillig static int
447 1.141 rillig output_line_code(int ind)
448 1.86 rillig {
449 1.86 rillig
450 1.86 rillig int target_ind = compute_code_indent();
451 1.146 rillig for (int i = 0; i < ps.nparen; i++) {
452 1.145 rillig if (ps.paren[i].indent >= 0) {
453 1.145 rillig int paren_ind = ps.paren[i].indent;
454 1.145 rillig ps.paren[i].indent = (short)(-1 - (paren_ind + target_ind));
455 1.86 rillig debug_println(
456 1.105 rillig "setting paren_indents[%d] from %d to %d for column %d",
457 1.145 rillig i, paren_ind, ps.paren[i].indent, target_ind + 1);
458 1.86 rillig }
459 1.86 rillig }
460 1.86 rillig
461 1.86 rillig ind = output_indent(ind, target_ind);
462 1.86 rillig output_range(code.s, code.e);
463 1.109 rillig return ind_add(ind, code.s, code.e);
464 1.86 rillig }
465 1.86 rillig
466 1.86 rillig static void
467 1.141 rillig output_line_comment(int ind)
468 1.86 rillig {
469 1.86 rillig int target_ind = ps.com_ind;
470 1.92 rillig const char *p = com.s;
471 1.86 rillig
472 1.86 rillig target_ind += ps.comment_delta;
473 1.86 rillig
474 1.86 rillig /* consider original indentation in case this is a box comment */
475 1.92 rillig for (; *p == '\t'; p++)
476 1.92 rillig target_ind += opt.tabsize;
477 1.86 rillig
478 1.92 rillig for (; target_ind < 0; p++) {
479 1.92 rillig if (*p == ' ')
480 1.92 rillig target_ind++;
481 1.92 rillig else if (*p == '\t')
482 1.91 rillig target_ind = next_tab(target_ind);
483 1.92 rillig else {
484 1.86 rillig target_ind = 0;
485 1.92 rillig break;
486 1.92 rillig }
487 1.86 rillig }
488 1.86 rillig
489 1.105 rillig /* if comment can't fit on this line, put it on the next line */
490 1.86 rillig if (ind > target_ind) {
491 1.86 rillig output_char('\n');
492 1.86 rillig ind = 0;
493 1.86 rillig ps.stats.lines++;
494 1.86 rillig }
495 1.86 rillig
496 1.129 rillig while (com.e > p && ch_isspace(com.e[-1]))
497 1.86 rillig com.e--;
498 1.86 rillig
499 1.86 rillig (void)output_indent(ind, target_ind);
500 1.92 rillig output_range(p, com.e);
501 1.86 rillig
502 1.86 rillig ps.comment_delta = ps.n_comment_delta;
503 1.86 rillig ps.stats.comment_lines++;
504 1.86 rillig }
505 1.86 rillig
506 1.26 rillig /*
507 1.105 rillig * Write a line of formatted source to the output file. The line consists of
508 1.105 rillig * the label, the code and the comment.
509 1.26 rillig */
510 1.101 rillig static void
511 1.141 rillig output_complete_line(char line_terminator)
512 1.26 rillig {
513 1.125 rillig ps.is_function_definition = false;
514 1.30 rillig
515 1.150 rillig if (!inhibit_formatting) {
516 1.19 kamil if (ps.ind_level == 0)
517 1.130 rillig ps.in_stmt_cont = false; /* this is a class A kludge */
518 1.19 kamil
519 1.55 rillig if (lab.e != lab.s || code.e != code.s)
520 1.58 rillig ps.stats.code_lines++;
521 1.19 kamil
522 1.86 rillig int ind = 0;
523 1.86 rillig if (lab.e != lab.s)
524 1.141 rillig ind = output_line_label();
525 1.86 rillig if (code.e != code.s)
526 1.141 rillig ind = output_line_code(ind);
527 1.86 rillig if (com.e != com.s)
528 1.141 rillig output_line_comment(ind);
529 1.77 rillig
530 1.101 rillig output_char(line_terminator);
531 1.58 rillig ps.stats.lines++;
532 1.77 rillig
533 1.143 rillig /* TODO: rename to blank_line_after_decl */
534 1.150 rillig if (ps.just_saw_decl == 1 && opt.blanklines_after_decl)
535 1.19 kamil ps.just_saw_decl = 0;
536 1.19 kamil }
537 1.24 rillig
538 1.76 rillig ps.decl_on_line = ps.in_decl; /* for proper comment indentation */
539 1.131 rillig ps.in_stmt_cont = ps.in_stmt_or_decl && !ps.in_decl;
540 1.104 rillig ps.decl_indent_done = false;
541 1.77 rillig
542 1.54 rillig *(lab.e = lab.s) = '\0'; /* reset buffers */
543 1.55 rillig *(code.e = code.s) = '\0';
544 1.52 rillig *(com.e = com.s = com.buf + 1) = '\0';
545 1.77 rillig
546 1.64 rillig ps.ind_level = ps.ind_level_follow;
547 1.146 rillig ps.line_start_nparen = ps.nparen;
548 1.77 rillig
549 1.147 rillig if (ps.nparen > 0) {
550 1.67 rillig /* TODO: explain what negative indentation means */
551 1.147 rillig paren_indent = -1 - ps.paren[ps.nparen - 1].indent;
552 1.36 rillig debug_println("paren_indent is now %d", paren_indent);
553 1.36 rillig }
554 1.1 cgd }
555 1.1 cgd
556 1.101 rillig void
557 1.141 rillig output_line(void)
558 1.101 rillig {
559 1.141 rillig output_complete_line('\n');
560 1.101 rillig }
561 1.101 rillig
562 1.101 rillig void
563 1.141 rillig output_line_ff(void)
564 1.101 rillig {
565 1.141 rillig output_complete_line('\f');
566 1.101 rillig }
567 1.101 rillig
568 1.113 rillig static int
569 1.113 rillig compute_code_indent_lineup(int base_ind)
570 1.113 rillig {
571 1.113 rillig int ti = paren_indent;
572 1.113 rillig int overflow = ind_add(ti, code.s, code.e) - opt.max_line_length;
573 1.113 rillig if (overflow < 0)
574 1.113 rillig return ti;
575 1.113 rillig
576 1.113 rillig if (ind_add(base_ind, code.s, code.e) < opt.max_line_length) {
577 1.113 rillig ti -= overflow + 2;
578 1.113 rillig if (ti > base_ind)
579 1.113 rillig return ti;
580 1.113 rillig return base_ind;
581 1.113 rillig }
582 1.113 rillig
583 1.113 rillig return ti;
584 1.113 rillig }
585 1.113 rillig
586 1.5 lukem int
587 1.39 rillig compute_code_indent(void)
588 1.1 cgd {
589 1.110 rillig int base_ind = ps.ind_level * opt.indent_size;
590 1.19 kamil
591 1.146 rillig if (ps.line_start_nparen == 0) {
592 1.144 rillig if (ps.in_stmt_cont && ps.in_enum != in_enum_brace)
593 1.110 rillig return base_ind + opt.continuation_indent;
594 1.110 rillig return base_ind;
595 1.110 rillig }
596 1.77 rillig
597 1.110 rillig if (opt.lineup_to_parens) {
598 1.112 rillig if (opt.lineup_to_parens_always)
599 1.112 rillig return paren_indent;
600 1.113 rillig return compute_code_indent_lineup(base_ind);
601 1.110 rillig }
602 1.77 rillig
603 1.110 rillig if (2 * opt.continuation_indent == opt.indent_size)
604 1.110 rillig return base_ind + opt.continuation_indent;
605 1.110 rillig else
606 1.146 rillig return base_ind + opt.continuation_indent * ps.line_start_nparen;
607 1.1 cgd }
608 1.1 cgd
609 1.5 lukem int
610 1.38 rillig compute_label_indent(void)
611 1.1 cgd {
612 1.75 rillig if (ps.is_case_label)
613 1.74 rillig return (int)(case_ind * (float)opt.indent_size);
614 1.54 rillig if (lab.s[0] == '#')
615 1.67 rillig return 0;
616 1.108 rillig return opt.indent_size * (ps.ind_level - 2);
617 1.1 cgd }
618 1.1 cgd
619 1.53 rillig static void
620 1.107 rillig skip_blank(const char **pp)
621 1.53 rillig {
622 1.107 rillig while (ch_isblank(**pp))
623 1.53 rillig (*pp)++;
624 1.53 rillig }
625 1.53 rillig
626 1.73 rillig static bool
627 1.73 rillig skip_string(const char **pp, const char *s)
628 1.73 rillig {
629 1.73 rillig size_t len = strlen(s);
630 1.73 rillig if (strncmp(*pp, s, len) == 0) {
631 1.73 rillig *pp += len;
632 1.73 rillig return true;
633 1.73 rillig }
634 1.73 rillig return false;
635 1.73 rillig }
636 1.73 rillig
637 1.53 rillig static void
638 1.53 rillig parse_indent_comment(void)
639 1.53 rillig {
640 1.90 rillig bool on;
641 1.53 rillig
642 1.117 rillig const char *p = inbuf.inp.buf;
643 1.53 rillig
644 1.107 rillig skip_blank(&p);
645 1.73 rillig if (!skip_string(&p, "/*"))
646 1.53 rillig return;
647 1.107 rillig skip_blank(&p);
648 1.73 rillig if (!skip_string(&p, "INDENT"))
649 1.53 rillig return;
650 1.143 rillig
651 1.107 rillig skip_blank(&p);
652 1.73 rillig if (*p == '*' || skip_string(&p, "ON"))
653 1.90 rillig on = true;
654 1.73 rillig else if (skip_string(&p, "OFF"))
655 1.90 rillig on = false;
656 1.73 rillig else
657 1.53 rillig return;
658 1.53 rillig
659 1.107 rillig skip_blank(&p);
660 1.73 rillig if (!skip_string(&p, "*/\n"))
661 1.53 rillig return;
662 1.53 rillig
663 1.55 rillig if (com.s != com.e || lab.s != lab.e || code.s != code.e)
664 1.141 rillig output_line();
665 1.53 rillig
666 1.90 rillig inhibit_formatting = !on;
667 1.53 rillig }
668 1.1 cgd
669 1.5 lukem void
670 1.116 rillig inp_read_line(void)
671 1.67 rillig {
672 1.136 rillig if (inp_from_file())
673 1.136 rillig return;
674 1.77 rillig
675 1.137 rillig inp_read_next_line(input);
676 1.77 rillig
677 1.137 rillig parse_indent_comment();
678 1.77 rillig
679 1.96 rillig if (inhibit_formatting)
680 1.117 rillig output_range(inbuf.inp.s, inbuf.inp.e);
681 1.1 cgd }
682