io.c revision 1.151 1 1.151 rillig /* $NetBSD: io.c,v 1.151 2023/05/11 18:26:56 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.151 rillig __RCSID("$NetBSD: io.c,v 1.151 2023/05/11 18:26:56 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.122 rillig inp_comment_init_preproc(void)
198 1.122 rillig {
199 1.122 rillig if (inbuf.save_com_e == NULL) { /* if this is the first comment, we
200 1.122 rillig * must set up the buffer */
201 1.143 rillig /*
202 1.143 rillig * XXX: No space is reserved for a potential '{' here, unlike in
203 1.143 rillig * inp_comment_init_comment.
204 1.143 rillig */
205 1.122 rillig inbuf.save_com_s = inbuf.save_com_buf;
206 1.122 rillig inbuf.save_com_e = inbuf.save_com_s;
207 1.122 rillig } else {
208 1.122 rillig inp_comment_add_char('\n'); /* add newline between comments */
209 1.122 rillig inp_comment_add_char(' ');
210 1.122 rillig --line_no;
211 1.122 rillig }
212 1.122 rillig }
213 1.122 rillig
214 1.122 rillig void
215 1.120 rillig inp_comment_add_char(char ch)
216 1.120 rillig {
217 1.120 rillig inp_comment_check_size(1);
218 1.120 rillig *inbuf.save_com_e++ = ch;
219 1.120 rillig }
220 1.120 rillig
221 1.120 rillig void
222 1.120 rillig inp_comment_add_range(const char *s, const char *e)
223 1.120 rillig {
224 1.120 rillig size_t len = (size_t)(e - s);
225 1.120 rillig inp_comment_check_size(len);
226 1.120 rillig memcpy(inbuf.save_com_e, s, len);
227 1.120 rillig inbuf.save_com_e += len;
228 1.120 rillig }
229 1.120 rillig
230 1.121 rillig bool
231 1.122 rillig inp_comment_seen(void)
232 1.122 rillig {
233 1.122 rillig return inbuf.save_com_e != NULL;
234 1.122 rillig }
235 1.122 rillig
236 1.143 rillig /*
237 1.143 rillig * Switch the input to come from save_com, replaying the copied tokens while
238 1.143 rillig * looking for the next '{'.
239 1.143 rillig */
240 1.120 rillig void
241 1.120 rillig inp_from_comment(void)
242 1.120 rillig {
243 1.139 rillig debug_inp("before inp_from_comment");
244 1.120 rillig inbuf.saved_inp_s = inbuf.inp.s;
245 1.120 rillig inbuf.saved_inp_e = inbuf.inp.e;
246 1.120 rillig
247 1.143 rillig inbuf.inp.s = inbuf.save_com_s;
248 1.120 rillig inbuf.inp.e = inbuf.save_com_e;
249 1.128 rillig inbuf.save_com_s = NULL;
250 1.120 rillig inbuf.save_com_e = NULL;
251 1.139 rillig debug_inp("after inp_from_comment");
252 1.120 rillig }
253 1.118 rillig
254 1.136 rillig /*
255 1.136 rillig * After having read from save_com, continue with the rest of the input line
256 1.136 rillig * before reading the next line from the input file.
257 1.136 rillig */
258 1.136 rillig static bool
259 1.136 rillig inp_from_file(void)
260 1.136 rillig {
261 1.136 rillig if (inbuf.saved_inp_s == NULL)
262 1.136 rillig return false;
263 1.136 rillig
264 1.136 rillig inbuf.inp.s = inbuf.saved_inp_s;
265 1.136 rillig inbuf.inp.e = inbuf.saved_inp_e;
266 1.136 rillig inbuf.saved_inp_s = inbuf.saved_inp_e = NULL;
267 1.136 rillig debug_println("switched inp.s back to saved_inp_s");
268 1.136 rillig return inbuf.inp.s < inbuf.inp.e;
269 1.136 rillig }
270 1.136 rillig
271 1.138 rillig static void
272 1.138 rillig inp_add(char ch)
273 1.137 rillig {
274 1.138 rillig if (inbuf.inp.e >= inbuf.inp.l) {
275 1.138 rillig size_t new_size = (size_t)(inbuf.inp.l - inbuf.inp.buf) * 2 + 10;
276 1.138 rillig size_t offset = (size_t)(inbuf.inp.e - inbuf.inp.buf);
277 1.138 rillig inbuf.inp.buf = xrealloc(inbuf.inp.buf, new_size);
278 1.138 rillig inbuf.inp.s = inbuf.inp.buf;
279 1.138 rillig inbuf.inp.e = inbuf.inp.buf + offset;
280 1.138 rillig inbuf.inp.l = inbuf.inp.buf + new_size - 2;
281 1.138 rillig }
282 1.138 rillig *inbuf.inp.e++ = ch;
283 1.137 rillig }
284 1.137 rillig
285 1.137 rillig static void
286 1.137 rillig inp_read_next_line(FILE *f)
287 1.137 rillig {
288 1.138 rillig inbuf.inp.s = inbuf.inp.buf;
289 1.138 rillig inbuf.inp.e = inbuf.inp.buf;
290 1.137 rillig
291 1.138 rillig for (;;) {
292 1.138 rillig int ch = getc(f);
293 1.138 rillig if (ch == EOF) {
294 1.137 rillig if (!inhibit_formatting) {
295 1.138 rillig inp_add(' ');
296 1.138 rillig inp_add('\n');
297 1.137 rillig }
298 1.137 rillig had_eof = true;
299 1.137 rillig break;
300 1.137 rillig }
301 1.137 rillig
302 1.137 rillig if (ch != '\0')
303 1.138 rillig inp_add((char)ch);
304 1.137 rillig if (ch == '\n')
305 1.137 rillig break;
306 1.137 rillig }
307 1.137 rillig }
308 1.137 rillig
309 1.28 rillig static void
310 1.28 rillig output_char(char ch)
311 1.28 rillig {
312 1.28 rillig fputc(ch, output);
313 1.36 rillig debug_vis_range("output_char '", &ch, &ch + 1, "'\n");
314 1.28 rillig }
315 1.28 rillig
316 1.28 rillig static void
317 1.28 rillig output_range(const char *s, const char *e)
318 1.28 rillig {
319 1.28 rillig fwrite(s, 1, (size_t)(e - s), output);
320 1.36 rillig debug_vis_range("output_range \"", s, e, "\"\n");
321 1.28 rillig }
322 1.28 rillig
323 1.34 rillig static int
324 1.34 rillig output_indent(int old_ind, int new_ind)
325 1.34 rillig {
326 1.34 rillig int ind = old_ind;
327 1.34 rillig
328 1.34 rillig if (opt.use_tabs) {
329 1.34 rillig int tabsize = opt.tabsize;
330 1.34 rillig int n = new_ind / tabsize - ind / tabsize;
331 1.34 rillig if (n > 0)
332 1.34 rillig ind -= ind % tabsize;
333 1.34 rillig for (int i = 0; i < n; i++) {
334 1.36 rillig fputc('\t', output);
335 1.34 rillig ind += tabsize;
336 1.34 rillig }
337 1.34 rillig }
338 1.34 rillig
339 1.34 rillig for (; ind < new_ind; ind++)
340 1.67 rillig fputc(' ', output);
341 1.34 rillig
342 1.36 rillig debug_println("output_indent %d", ind);
343 1.34 rillig return ind;
344 1.34 rillig }
345 1.34 rillig
346 1.86 rillig static int
347 1.141 rillig output_line_label(void)
348 1.86 rillig {
349 1.86 rillig int ind;
350 1.86 rillig
351 1.107 rillig while (lab.e > lab.s && ch_isblank(lab.e[-1]))
352 1.86 rillig lab.e--;
353 1.86 rillig *lab.e = '\0';
354 1.86 rillig
355 1.86 rillig ind = output_indent(0, compute_label_indent());
356 1.135 rillig output_range(lab.s, lab.e);
357 1.109 rillig ind = ind_add(ind, lab.s, lab.e);
358 1.86 rillig
359 1.86 rillig ps.is_case_label = false;
360 1.86 rillig return ind;
361 1.86 rillig }
362 1.86 rillig
363 1.86 rillig static int
364 1.141 rillig output_line_code(int ind)
365 1.86 rillig {
366 1.86 rillig
367 1.86 rillig int target_ind = compute_code_indent();
368 1.146 rillig for (int i = 0; i < ps.nparen; i++) {
369 1.145 rillig if (ps.paren[i].indent >= 0) {
370 1.145 rillig int paren_ind = ps.paren[i].indent;
371 1.145 rillig ps.paren[i].indent = (short)(-1 - (paren_ind + target_ind));
372 1.86 rillig debug_println(
373 1.105 rillig "setting paren_indents[%d] from %d to %d for column %d",
374 1.145 rillig i, paren_ind, ps.paren[i].indent, target_ind + 1);
375 1.86 rillig }
376 1.86 rillig }
377 1.86 rillig
378 1.86 rillig ind = output_indent(ind, target_ind);
379 1.86 rillig output_range(code.s, code.e);
380 1.109 rillig return ind_add(ind, code.s, code.e);
381 1.86 rillig }
382 1.86 rillig
383 1.86 rillig static void
384 1.141 rillig output_line_comment(int ind)
385 1.86 rillig {
386 1.86 rillig int target_ind = ps.com_ind;
387 1.92 rillig const char *p = com.s;
388 1.86 rillig
389 1.86 rillig target_ind += ps.comment_delta;
390 1.86 rillig
391 1.86 rillig /* consider original indentation in case this is a box comment */
392 1.92 rillig for (; *p == '\t'; p++)
393 1.92 rillig target_ind += opt.tabsize;
394 1.86 rillig
395 1.92 rillig for (; target_ind < 0; p++) {
396 1.92 rillig if (*p == ' ')
397 1.92 rillig target_ind++;
398 1.92 rillig else if (*p == '\t')
399 1.91 rillig target_ind = next_tab(target_ind);
400 1.92 rillig else {
401 1.86 rillig target_ind = 0;
402 1.92 rillig break;
403 1.92 rillig }
404 1.86 rillig }
405 1.86 rillig
406 1.105 rillig /* if comment can't fit on this line, put it on the next line */
407 1.86 rillig if (ind > target_ind) {
408 1.86 rillig output_char('\n');
409 1.86 rillig ind = 0;
410 1.86 rillig ps.stats.lines++;
411 1.86 rillig }
412 1.86 rillig
413 1.129 rillig while (com.e > p && ch_isspace(com.e[-1]))
414 1.86 rillig com.e--;
415 1.86 rillig
416 1.86 rillig (void)output_indent(ind, target_ind);
417 1.92 rillig output_range(p, com.e);
418 1.86 rillig
419 1.86 rillig ps.comment_delta = ps.n_comment_delta;
420 1.86 rillig ps.stats.comment_lines++;
421 1.86 rillig }
422 1.86 rillig
423 1.26 rillig /*
424 1.105 rillig * Write a line of formatted source to the output file. The line consists of
425 1.105 rillig * the label, the code and the comment.
426 1.26 rillig */
427 1.101 rillig static void
428 1.141 rillig output_complete_line(char line_terminator)
429 1.26 rillig {
430 1.125 rillig ps.is_function_definition = false;
431 1.30 rillig
432 1.150 rillig if (!inhibit_formatting) {
433 1.19 kamil if (ps.ind_level == 0)
434 1.130 rillig ps.in_stmt_cont = false; /* this is a class A kludge */
435 1.19 kamil
436 1.55 rillig if (lab.e != lab.s || code.e != code.s)
437 1.58 rillig ps.stats.code_lines++;
438 1.19 kamil
439 1.86 rillig int ind = 0;
440 1.86 rillig if (lab.e != lab.s)
441 1.141 rillig ind = output_line_label();
442 1.86 rillig if (code.e != code.s)
443 1.141 rillig ind = output_line_code(ind);
444 1.86 rillig if (com.e != com.s)
445 1.141 rillig output_line_comment(ind);
446 1.77 rillig
447 1.101 rillig output_char(line_terminator);
448 1.58 rillig ps.stats.lines++;
449 1.77 rillig
450 1.143 rillig /* TODO: rename to blank_line_after_decl */
451 1.150 rillig if (ps.just_saw_decl == 1 && opt.blanklines_after_decl)
452 1.19 kamil ps.just_saw_decl = 0;
453 1.19 kamil }
454 1.24 rillig
455 1.76 rillig ps.decl_on_line = ps.in_decl; /* for proper comment indentation */
456 1.131 rillig ps.in_stmt_cont = ps.in_stmt_or_decl && !ps.in_decl;
457 1.104 rillig ps.decl_indent_done = false;
458 1.77 rillig
459 1.54 rillig *(lab.e = lab.s) = '\0'; /* reset buffers */
460 1.55 rillig *(code.e = code.s) = '\0';
461 1.52 rillig *(com.e = com.s = com.buf + 1) = '\0';
462 1.77 rillig
463 1.64 rillig ps.ind_level = ps.ind_level_follow;
464 1.146 rillig ps.line_start_nparen = ps.nparen;
465 1.77 rillig
466 1.147 rillig if (ps.nparen > 0) {
467 1.67 rillig /* TODO: explain what negative indentation means */
468 1.147 rillig paren_indent = -1 - ps.paren[ps.nparen - 1].indent;
469 1.36 rillig debug_println("paren_indent is now %d", paren_indent);
470 1.36 rillig }
471 1.1 cgd }
472 1.1 cgd
473 1.101 rillig void
474 1.141 rillig output_line(void)
475 1.101 rillig {
476 1.141 rillig output_complete_line('\n');
477 1.101 rillig }
478 1.101 rillig
479 1.101 rillig void
480 1.141 rillig output_line_ff(void)
481 1.101 rillig {
482 1.141 rillig output_complete_line('\f');
483 1.101 rillig }
484 1.101 rillig
485 1.113 rillig static int
486 1.113 rillig compute_code_indent_lineup(int base_ind)
487 1.113 rillig {
488 1.113 rillig int ti = paren_indent;
489 1.113 rillig int overflow = ind_add(ti, code.s, code.e) - opt.max_line_length;
490 1.113 rillig if (overflow < 0)
491 1.113 rillig return ti;
492 1.113 rillig
493 1.113 rillig if (ind_add(base_ind, code.s, code.e) < opt.max_line_length) {
494 1.113 rillig ti -= overflow + 2;
495 1.113 rillig if (ti > base_ind)
496 1.113 rillig return ti;
497 1.113 rillig return base_ind;
498 1.113 rillig }
499 1.113 rillig
500 1.113 rillig return ti;
501 1.113 rillig }
502 1.113 rillig
503 1.5 lukem int
504 1.39 rillig compute_code_indent(void)
505 1.1 cgd {
506 1.110 rillig int base_ind = ps.ind_level * opt.indent_size;
507 1.19 kamil
508 1.146 rillig if (ps.line_start_nparen == 0) {
509 1.144 rillig if (ps.in_stmt_cont && ps.in_enum != in_enum_brace)
510 1.110 rillig return base_ind + opt.continuation_indent;
511 1.110 rillig return base_ind;
512 1.110 rillig }
513 1.77 rillig
514 1.110 rillig if (opt.lineup_to_parens) {
515 1.112 rillig if (opt.lineup_to_parens_always)
516 1.112 rillig return paren_indent;
517 1.113 rillig return compute_code_indent_lineup(base_ind);
518 1.110 rillig }
519 1.77 rillig
520 1.110 rillig if (2 * opt.continuation_indent == opt.indent_size)
521 1.110 rillig return base_ind + opt.continuation_indent;
522 1.110 rillig else
523 1.146 rillig return base_ind + opt.continuation_indent * ps.line_start_nparen;
524 1.1 cgd }
525 1.1 cgd
526 1.5 lukem int
527 1.38 rillig compute_label_indent(void)
528 1.1 cgd {
529 1.75 rillig if (ps.is_case_label)
530 1.74 rillig return (int)(case_ind * (float)opt.indent_size);
531 1.54 rillig if (lab.s[0] == '#')
532 1.67 rillig return 0;
533 1.108 rillig return opt.indent_size * (ps.ind_level - 2);
534 1.1 cgd }
535 1.1 cgd
536 1.53 rillig static void
537 1.107 rillig skip_blank(const char **pp)
538 1.53 rillig {
539 1.107 rillig while (ch_isblank(**pp))
540 1.53 rillig (*pp)++;
541 1.53 rillig }
542 1.53 rillig
543 1.73 rillig static bool
544 1.73 rillig skip_string(const char **pp, const char *s)
545 1.73 rillig {
546 1.73 rillig size_t len = strlen(s);
547 1.73 rillig if (strncmp(*pp, s, len) == 0) {
548 1.73 rillig *pp += len;
549 1.73 rillig return true;
550 1.73 rillig }
551 1.73 rillig return false;
552 1.73 rillig }
553 1.73 rillig
554 1.53 rillig static void
555 1.53 rillig parse_indent_comment(void)
556 1.53 rillig {
557 1.90 rillig bool on;
558 1.53 rillig
559 1.117 rillig const char *p = inbuf.inp.buf;
560 1.53 rillig
561 1.107 rillig skip_blank(&p);
562 1.73 rillig if (!skip_string(&p, "/*"))
563 1.53 rillig return;
564 1.107 rillig skip_blank(&p);
565 1.73 rillig if (!skip_string(&p, "INDENT"))
566 1.53 rillig return;
567 1.143 rillig
568 1.107 rillig skip_blank(&p);
569 1.73 rillig if (*p == '*' || skip_string(&p, "ON"))
570 1.90 rillig on = true;
571 1.73 rillig else if (skip_string(&p, "OFF"))
572 1.90 rillig on = false;
573 1.73 rillig else
574 1.53 rillig return;
575 1.53 rillig
576 1.107 rillig skip_blank(&p);
577 1.73 rillig if (!skip_string(&p, "*/\n"))
578 1.53 rillig return;
579 1.53 rillig
580 1.55 rillig if (com.s != com.e || lab.s != lab.e || code.s != code.e)
581 1.141 rillig output_line();
582 1.53 rillig
583 1.90 rillig inhibit_formatting = !on;
584 1.53 rillig }
585 1.1 cgd
586 1.5 lukem void
587 1.116 rillig inp_read_line(void)
588 1.67 rillig {
589 1.136 rillig if (inp_from_file())
590 1.136 rillig return;
591 1.77 rillig
592 1.137 rillig inp_read_next_line(input);
593 1.77 rillig
594 1.137 rillig parse_indent_comment();
595 1.77 rillig
596 1.96 rillig if (inhibit_formatting)
597 1.117 rillig output_range(inbuf.inp.s, inbuf.inp.e);
598 1.1 cgd }
599