io.c revision 1.27 1 1.27 rillig /* $NetBSD: io.c,v 1.27 2021/03/08 22:28:31 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 #ifndef lint
42 1.19 kamil static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93";
43 1.19 kamil #endif /* not lint */
44 1.19 kamil #endif
45 1.19 kamil
46 1.5 lukem #include <sys/cdefs.h>
47 1.1 cgd #ifndef lint
48 1.19 kamil #if defined(__NetBSD__)
49 1.27 rillig __RCSID("$NetBSD: io.c,v 1.27 2021/03/08 22:28:31 rillig Exp $");
50 1.19 kamil #elif defined(__FreeBSD__)
51 1.19 kamil __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
52 1.19 kamil #endif
53 1.4 mrg #endif
54 1.1 cgd
55 1.5 lukem #include <ctype.h>
56 1.5 lukem #include <err.h>
57 1.1 cgd #include <stdio.h>
58 1.1 cgd #include <stdlib.h>
59 1.1 cgd #include <string.h>
60 1.20 christos #include <stdarg.h>
61 1.22 rillig
62 1.19 kamil #include "indent.h"
63 1.1 cgd
64 1.19 kamil int comment_open;
65 1.19 kamil static int paren_target;
66 1.19 kamil static int pad_output(int current, int target);
67 1.1 cgd
68 1.26 rillig /*
69 1.26 rillig * dump_line is the routine that actually effects the printing of the new
70 1.26 rillig * source. It prints the label section, followed by the code section with
71 1.26 rillig * the appropriate nesting level, followed by any comments.
72 1.26 rillig */
73 1.5 lukem void
74 1.11 wiz dump_line(void)
75 1.26 rillig {
76 1.27 rillig int cur_col, target_col;
77 1.19 kamil static int not_first_line;
78 1.19 kamil
79 1.19 kamil if (ps.procname[0]) {
80 1.19 kamil ps.ind_level = 0;
81 1.19 kamil ps.procname[0] = 0;
82 1.19 kamil }
83 1.19 kamil if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
84 1.19 kamil if (suppress_blanklines > 0)
85 1.19 kamil suppress_blanklines--;
86 1.19 kamil else {
87 1.19 kamil ps.bl_line = true;
88 1.19 kamil n_real_blanklines++;
89 1.19 kamil }
90 1.19 kamil }
91 1.19 kamil else if (!inhibit_formatting) {
92 1.19 kamil suppress_blanklines = 0;
93 1.19 kamil ps.bl_line = false;
94 1.19 kamil if (prefix_blankline_requested && not_first_line) {
95 1.19 kamil if (opt.swallow_optional_blanklines) {
96 1.19 kamil if (n_real_blanklines == 1)
97 1.19 kamil n_real_blanklines = 0;
98 1.19 kamil }
99 1.19 kamil else {
100 1.19 kamil if (n_real_blanklines == 0)
101 1.19 kamil n_real_blanklines = 1;
102 1.19 kamil }
103 1.19 kamil }
104 1.19 kamil while (--n_real_blanklines >= 0)
105 1.19 kamil putc('\n', output);
106 1.19 kamil n_real_blanklines = 0;
107 1.19 kamil if (ps.ind_level == 0)
108 1.19 kamil ps.ind_stmt = 0; /* this is a class A kludge. dont do
109 1.19 kamil * additional statement indentation if we are
110 1.19 kamil * at bracket level 0 */
111 1.19 kamil
112 1.19 kamil if (e_lab != s_lab || e_code != s_code)
113 1.19 kamil ++code_lines; /* keep count of lines with code */
114 1.19 kamil
115 1.19 kamil
116 1.19 kamil if (e_lab != s_lab) { /* print lab, if any */
117 1.19 kamil if (comment_open) {
118 1.19 kamil comment_open = 0;
119 1.19 kamil fprintf(output, ".*/\n");
120 1.19 kamil }
121 1.19 kamil while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
122 1.19 kamil e_lab--;
123 1.19 kamil *e_lab = '\0';
124 1.19 kamil cur_col = pad_output(1, compute_label_target());
125 1.19 kamil if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0
126 1.19 kamil || strncmp(s_lab, "#endif", 6) == 0)) {
127 1.19 kamil char *s = s_lab;
128 1.19 kamil if (e_lab[-1] == '\n') e_lab--;
129 1.25 rillig do {
130 1.25 rillig putc(*s++, output);
131 1.25 rillig } while (s < e_lab && 'a' <= *s && *s <= 'z');
132 1.19 kamil while ((*s == ' ' || *s == '\t') && s < e_lab)
133 1.19 kamil s++;
134 1.19 kamil if (s < e_lab)
135 1.19 kamil fprintf(output, s[0]=='/' && s[1]=='*' ? "\t%.*s" : "\t/* %.*s */",
136 1.19 kamil (int)(e_lab - s), s);
137 1.19 kamil }
138 1.19 kamil else fprintf(output, "%.*s", (int)(e_lab - s_lab), s_lab);
139 1.19 kamil cur_col = count_spaces(cur_col, s_lab);
140 1.19 kamil }
141 1.19 kamil else
142 1.19 kamil cur_col = 1; /* there is no label section */
143 1.19 kamil
144 1.19 kamil ps.pcase = false;
145 1.19 kamil
146 1.19 kamil if (s_code != e_code) { /* print code section, if any */
147 1.19 kamil char *p;
148 1.1 cgd
149 1.19 kamil if (comment_open) {
150 1.19 kamil comment_open = 0;
151 1.19 kamil fprintf(output, ".*/\n");
152 1.19 kamil }
153 1.19 kamil target_col = compute_code_target();
154 1.19 kamil {
155 1.19 kamil int i;
156 1.19 kamil
157 1.19 kamil for (i = 0; i < ps.p_l_follow; i++)
158 1.19 kamil if (ps.paren_indents[i] >= 0)
159 1.19 kamil ps.paren_indents[i] = -(ps.paren_indents[i] + target_col);
160 1.19 kamil }
161 1.19 kamil cur_col = pad_output(cur_col, target_col);
162 1.19 kamil for (p = s_code; p < e_code; p++)
163 1.19 kamil if (*p == (char) 0200)
164 1.19 kamil fprintf(output, "%d", target_col * 7);
165 1.19 kamil else
166 1.19 kamil putc(*p, output);
167 1.19 kamil cur_col = count_spaces(cur_col, s_code);
168 1.19 kamil }
169 1.19 kamil if (s_com != e_com) { /* print comment, if any */
170 1.19 kamil int target = ps.com_col;
171 1.19 kamil char *com_st = s_com;
172 1.19 kamil
173 1.19 kamil target += ps.comment_delta;
174 1.19 kamil while (*com_st == '\t') /* consider original indentation in
175 1.19 kamil * case this is a box comment */
176 1.19 kamil com_st++, target += opt.tabsize;
177 1.19 kamil while (target <= 0)
178 1.19 kamil if (*com_st == ' ')
179 1.19 kamil target++, com_st++;
180 1.19 kamil else if (*com_st == '\t') {
181 1.19 kamil target = opt.tabsize * (1 + (target - 1) / opt.tabsize) + 1;
182 1.19 kamil com_st++;
183 1.5 lukem }
184 1.19 kamil else
185 1.19 kamil target = 1;
186 1.19 kamil if (cur_col > target) { /* if comment can't fit on this line,
187 1.19 kamil * put it on next line */
188 1.19 kamil putc('\n', output);
189 1.19 kamil cur_col = 1;
190 1.19 kamil ++ps.out_lines;
191 1.19 kamil }
192 1.19 kamil while (e_com > com_st && isspace((unsigned char)e_com[-1]))
193 1.19 kamil e_com--;
194 1.19 kamil (void)pad_output(cur_col, target);
195 1.19 kamil fwrite(com_st, e_com - com_st, 1, output);
196 1.19 kamil ps.comment_delta = ps.n_comment_delta;
197 1.19 kamil ++ps.com_lines; /* count lines with comments */
198 1.19 kamil }
199 1.19 kamil if (ps.use_ff)
200 1.19 kamil putc('\014', output);
201 1.19 kamil else
202 1.19 kamil putc('\n', output);
203 1.19 kamil ++ps.out_lines;
204 1.19 kamil if (ps.just_saw_decl == 1 && opt.blanklines_after_declarations) {
205 1.19 kamil prefix_blankline_requested = 1;
206 1.19 kamil ps.just_saw_decl = 0;
207 1.1 cgd }
208 1.19 kamil else
209 1.19 kamil prefix_blankline_requested = postfix_blankline_requested;
210 1.19 kamil postfix_blankline_requested = 0;
211 1.19 kamil }
212 1.24 rillig
213 1.24 rillig /* keep blank lines after '//' comments */
214 1.24 rillig if (e_com - s_com > 1 && s_com[1] == '/')
215 1.24 rillig fprintf(output, "%.*s", (int)(e_token - s_token), s_token);
216 1.24 rillig
217 1.19 kamil ps.decl_on_line = ps.in_decl; /* if we are in the middle of a
218 1.1 cgd * declaration, remember that fact for
219 1.1 cgd * proper comment indentation */
220 1.19 kamil ps.ind_stmt = ps.in_stmt & ~ps.in_decl; /* next line should be
221 1.1 cgd * indented if we have not
222 1.1 cgd * completed this stmt and if
223 1.1 cgd * we are not in the middle of
224 1.1 cgd * a declaration */
225 1.19 kamil ps.use_ff = false;
226 1.19 kamil ps.dumped_decl_indent = 0;
227 1.19 kamil *(e_lab = s_lab) = '\0'; /* reset buffers */
228 1.19 kamil *(e_code = s_code) = '\0';
229 1.19 kamil *(e_com = s_com = combuf + 1) = '\0';
230 1.19 kamil ps.ind_level = ps.i_l_follow;
231 1.19 kamil ps.paren_level = ps.p_l_follow;
232 1.19 kamil if (ps.paren_level > 0)
233 1.5 lukem paren_target = -ps.paren_indents[ps.paren_level - 1];
234 1.19 kamil not_first_line = 1;
235 1.1 cgd }
236 1.1 cgd
237 1.5 lukem int
238 1.11 wiz compute_code_target(void)
239 1.1 cgd {
240 1.19 kamil int target_col = opt.ind_size * ps.ind_level + 1;
241 1.19 kamil
242 1.19 kamil if (ps.paren_level)
243 1.19 kamil if (!opt.lineup_to_parens)
244 1.19 kamil target_col += opt.continuation_indent *
245 1.19 kamil (2 * opt.continuation_indent == opt.ind_size ? 1 : ps.paren_level);
246 1.19 kamil else if (opt.lineup_to_parens_always)
247 1.19 kamil target_col = paren_target;
248 1.19 kamil else {
249 1.19 kamil int w;
250 1.19 kamil int t = paren_target;
251 1.1 cgd
252 1.19 kamil if ((w = count_spaces(t, s_code) - opt.max_col) > 0
253 1.19 kamil && count_spaces(target_col, s_code) <= opt.max_col) {
254 1.19 kamil t -= w + 1;
255 1.19 kamil if (t > target_col)
256 1.19 kamil target_col = t;
257 1.19 kamil }
258 1.19 kamil else
259 1.19 kamil target_col = t;
260 1.19 kamil }
261 1.19 kamil else if (ps.ind_stmt)
262 1.19 kamil target_col += opt.continuation_indent;
263 1.19 kamil return target_col;
264 1.1 cgd }
265 1.1 cgd
266 1.5 lukem int
267 1.11 wiz compute_label_target(void)
268 1.1 cgd {
269 1.19 kamil return
270 1.19 kamil ps.pcase ? (int) (case_ind * opt.ind_size) + 1
271 1.1 cgd : *s_lab == '#' ? 1
272 1.19 kamil : opt.ind_size * (ps.ind_level - label_offset) + 1;
273 1.1 cgd }
274 1.1 cgd
275 1.1 cgd
276 1.1 cgd /*
277 1.1 cgd * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
278 1.5 lukem *
279 1.1 cgd * All rights reserved
280 1.5 lukem *
281 1.5 lukem *
282 1.1 cgd * NAME: fill_buffer
283 1.5 lukem *
284 1.1 cgd * FUNCTION: Reads one block of input into input_buffer
285 1.5 lukem *
286 1.21 rillig * HISTORY: initial coding November 1976 D A Willcox of CAC 1/7/77 A
287 1.1 cgd * Willcox of CAC Added check for switch back to partly full input
288 1.1 cgd * buffer from temporary buffer
289 1.5 lukem *
290 1.1 cgd */
291 1.5 lukem void
292 1.11 wiz fill_buffer(void)
293 1.1 cgd { /* this routine reads stuff from the input */
294 1.19 kamil char *p;
295 1.19 kamil int i;
296 1.19 kamil FILE *f = input;
297 1.19 kamil
298 1.19 kamil if (bp_save != NULL) { /* there is a partly filled input buffer left */
299 1.19 kamil buf_ptr = bp_save; /* do not read anything, just switch buffers */
300 1.19 kamil buf_end = be_save;
301 1.19 kamil bp_save = be_save = NULL;
302 1.19 kamil if (buf_ptr < buf_end)
303 1.19 kamil return; /* only return if there is really something in
304 1.1 cgd * this buffer */
305 1.19 kamil }
306 1.19 kamil for (p = in_buffer;;) {
307 1.19 kamil if (p >= in_buffer_limit) {
308 1.19 kamil int size = (in_buffer_limit - in_buffer) * 2 + 10;
309 1.19 kamil int offset = p - in_buffer;
310 1.19 kamil in_buffer = realloc(in_buffer, size);
311 1.19 kamil if (in_buffer == NULL)
312 1.19 kamil errx(1, "input line too long");
313 1.19 kamil p = in_buffer + offset;
314 1.19 kamil in_buffer_limit = in_buffer + size - 2;
315 1.19 kamil }
316 1.19 kamil if ((i = getc(f)) == EOF) {
317 1.19 kamil *p++ = ' ';
318 1.19 kamil *p++ = '\n';
319 1.19 kamil had_eof = true;
320 1.19 kamil break;
321 1.1 cgd }
322 1.19 kamil if (i != '\0')
323 1.19 kamil *p++ = i;
324 1.19 kamil if (i == '\n')
325 1.19 kamil break;
326 1.19 kamil }
327 1.19 kamil buf_ptr = in_buffer;
328 1.19 kamil buf_end = p;
329 1.19 kamil if (p - in_buffer > 2 && p[-2] == '/' && p[-3] == '*') {
330 1.19 kamil if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
331 1.19 kamil fill_buffer(); /* flush indent error message */
332 1.19 kamil else {
333 1.19 kamil int com = 0;
334 1.19 kamil
335 1.19 kamil p = in_buffer;
336 1.19 kamil while (*p == ' ' || *p == '\t')
337 1.19 kamil p++;
338 1.19 kamil if (*p == '/' && p[1] == '*') {
339 1.19 kamil p += 2;
340 1.19 kamil while (*p == ' ' || *p == '\t')
341 1.19 kamil p++;
342 1.19 kamil if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E'
343 1.19 kamil && p[4] == 'N' && p[5] == 'T') {
344 1.19 kamil p += 6;
345 1.19 kamil while (*p == ' ' || *p == '\t')
346 1.19 kamil p++;
347 1.19 kamil if (*p == '*')
348 1.19 kamil com = 1;
349 1.19 kamil else if (*p == 'O') {
350 1.19 kamil if (*++p == 'N')
351 1.19 kamil p++, com = 1;
352 1.19 kamil else if (*p == 'F' && *++p == 'F')
353 1.19 kamil p++, com = 2;
354 1.19 kamil }
355 1.19 kamil while (*p == ' ' || *p == '\t')
356 1.19 kamil p++;
357 1.19 kamil if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) {
358 1.19 kamil if (s_com != e_com || s_lab != e_lab || s_code != e_code)
359 1.19 kamil dump_line();
360 1.19 kamil if (!(inhibit_formatting = com - 1)) {
361 1.19 kamil n_real_blanklines = 0;
362 1.19 kamil postfix_blankline_requested = 0;
363 1.19 kamil prefix_blankline_requested = 0;
364 1.19 kamil suppress_blanklines = 1;
365 1.1 cgd }
366 1.19 kamil }
367 1.1 cgd }
368 1.19 kamil }
369 1.1 cgd }
370 1.19 kamil }
371 1.19 kamil if (inhibit_formatting) {
372 1.19 kamil p = in_buffer;
373 1.25 rillig do {
374 1.19 kamil putc(*p, output);
375 1.25 rillig } while (*p++ != '\n');
376 1.19 kamil }
377 1.1 cgd }
378 1.19 kamil
379 1.1 cgd /*
380 1.1 cgd * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
381 1.5 lukem *
382 1.1 cgd * All rights reserved
383 1.5 lukem *
384 1.5 lukem *
385 1.1 cgd * NAME: pad_output
386 1.5 lukem *
387 1.1 cgd * FUNCTION: Writes tabs and spaces to move the current column up to the desired
388 1.1 cgd * position.
389 1.5 lukem *
390 1.1 cgd * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf.
391 1.5 lukem *
392 1.1 cgd * PARAMETERS: current integer The current column target
393 1.19 kamil * nteger The desired column
394 1.5 lukem *
395 1.1 cgd * RETURNS: Integer value of the new column. (If current >= target, no action is
396 1.1 cgd * taken, and current is returned.
397 1.5 lukem *
398 1.1 cgd * GLOBALS: None
399 1.5 lukem *
400 1.1 cgd * CALLS: write (sys)
401 1.5 lukem *
402 1.1 cgd * CALLED BY: dump_line
403 1.5 lukem *
404 1.21 rillig * HISTORY: initial coding November 1976 D A Willcox of CAC
405 1.5 lukem *
406 1.1 cgd */
407 1.19 kamil static int
408 1.11 wiz pad_output(int current, int target)
409 1.19 kamil /* writes tabs and blanks (if necessary) to
410 1.19 kamil * get the current output position up to the
411 1.19 kamil * target column */
412 1.19 kamil /* current: the current column value */
413 1.19 kamil /* target: position we want it at */
414 1.1 cgd {
415 1.19 kamil int curr; /* internal column pointer */
416 1.1 cgd
417 1.19 kamil if (current >= target)
418 1.23 rillig return current; /* line is already long enough */
419 1.19 kamil curr = current;
420 1.19 kamil if (opt.use_tabs) {
421 1.19 kamil int tcur;
422 1.19 kamil
423 1.19 kamil while ((tcur = opt.tabsize * (1 + (curr - 1) / opt.tabsize) + 1) <= target) {
424 1.19 kamil putc('\t', output);
425 1.19 kamil curr = tcur;
426 1.1 cgd }
427 1.19 kamil }
428 1.19 kamil while (curr++ < target)
429 1.19 kamil putc(' ', output); /* pad with final blanks */
430 1.19 kamil
431 1.23 rillig return target;
432 1.1 cgd }
433 1.19 kamil
434 1.1 cgd /*
435 1.1 cgd * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
436 1.5 lukem *
437 1.1 cgd * All rights reserved
438 1.5 lukem *
439 1.5 lukem *
440 1.1 cgd * NAME: count_spaces
441 1.5 lukem *
442 1.1 cgd * FUNCTION: Find out where printing of a given string will leave the current
443 1.1 cgd * character position on output.
444 1.5 lukem *
445 1.1 cgd * ALGORITHM: Run thru input string and add appropriate values to current
446 1.1 cgd * position.
447 1.5 lukem *
448 1.1 cgd * RETURNS: Integer value of position after printing "buffer" starting in column
449 1.1 cgd * "current".
450 1.5 lukem *
451 1.21 rillig * HISTORY: initial coding November 1976 D A Willcox of CAC
452 1.5 lukem *
453 1.1 cgd */
454 1.1 cgd int
455 1.19 kamil count_spaces_until(int cur, char *buffer, char *end)
456 1.1 cgd /*
457 1.1 cgd * this routine figures out where the character position will be after
458 1.1 cgd * printing the text in buffer starting at column "current"
459 1.1 cgd */
460 1.1 cgd {
461 1.19 kamil char *buf; /* used to look thru buffer */
462 1.1 cgd
463 1.19 kamil for (buf = buffer; *buf != '\0' && buf != end; ++buf) {
464 1.19 kamil switch (*buf) {
465 1.1 cgd
466 1.19 kamil case '\n':
467 1.19 kamil case 014: /* form feed */
468 1.19 kamil cur = 1;
469 1.19 kamil break;
470 1.19 kamil
471 1.19 kamil case '\t':
472 1.19 kamil cur = opt.tabsize * (1 + (cur - 1) / opt.tabsize) + 1;
473 1.19 kamil break;
474 1.19 kamil
475 1.19 kamil case 010: /* backspace */
476 1.19 kamil --cur;
477 1.19 kamil break;
478 1.19 kamil
479 1.19 kamil default:
480 1.19 kamil ++cur;
481 1.19 kamil break;
482 1.19 kamil } /* end of switch */
483 1.19 kamil } /* end of for loop */
484 1.23 rillig return cur;
485 1.1 cgd }
486 1.1 cgd
487 1.19 kamil int
488 1.19 kamil count_spaces(int cur, char *buffer)
489 1.1 cgd {
490 1.23 rillig return count_spaces_until(cur, buffer, NULL);
491 1.1 cgd }
492 1.1 cgd
493 1.5 lukem void
494 1.20 christos diag(int level, const char *msg, ...)
495 1.1 cgd {
496 1.20 christos va_list ap;
497 1.20 christos const char *s, *e;
498 1.20 christos
499 1.19 kamil if (level)
500 1.19 kamil found_err = 1;
501 1.1 cgd
502 1.19 kamil if (output == stdout) {
503 1.20 christos s = "/**INDENT** ";
504 1.20 christos e = " */";
505 1.20 christos } else {
506 1.20 christos s = e = "";
507 1.19 kamil }
508 1.1 cgd
509 1.20 christos va_start(ap, msg);
510 1.20 christos fprintf(stderr, "%s%s@%d: ", s, level == 0 ? "Warning" : "Error", line_no);
511 1.20 christos vfprintf(stderr, msg, ap);
512 1.20 christos fprintf(stderr, "%s\n", e);
513 1.20 christos va_end(ap);
514 1.1 cgd }
515