pr_comment.c revision 1.10.16.1 1 1.10.16.1 christos /* $NetBSD: pr_comment.c,v 1.10.16.1 2019/06/10 22:10:20 christos Exp $ */
2 1.4 tls
3 1.10.16.1 christos /*-
4 1.10.16.1 christos * SPDX-License-Identifier: BSD-4-Clause
5 1.9 agc *
6 1.1 cgd * Copyright (c) 1985 Sun Microsystems, Inc.
7 1.10.16.1 christos * Copyright (c) 1980, 1993
8 1.10.16.1 christos * 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.5 mrg #if 0
41 1.10.16.1 christos #ifndef lint
42 1.5 mrg static char sccsid[] = "@(#)pr_comment.c 8.1 (Berkeley) 6/6/93";
43 1.10.16.1 christos #endif /* not lint */
44 1.5 mrg #endif
45 1.1 cgd
46 1.10.16.1 christos #include <sys/cdefs.h>
47 1.10.16.1 christos #ifndef lint
48 1.10.16.1 christos #if defined(__NetBSD__)
49 1.10.16.1 christos __RCSID("$NetBSD: pr_comment.c,v 1.10.16.1 2019/06/10 22:10:20 christos Exp $");
50 1.10.16.1 christos #elif defined(__FreeBSD__)
51 1.10.16.1 christos __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
52 1.10.16.1 christos #endif
53 1.10.16.1 christos #endif
54 1.10.16.1 christos
55 1.10.16.1 christos #include <err.h>
56 1.1 cgd #include <stdio.h>
57 1.1 cgd #include <stdlib.h>
58 1.10.16.1 christos #include <string.h>
59 1.1 cgd #include "indent_globs.h"
60 1.10.16.1 christos #include "indent_codes.h"
61 1.10.16.1 christos #include "indent.h"
62 1.1 cgd /*
63 1.1 cgd * NAME:
64 1.1 cgd * pr_comment
65 1.1 cgd *
66 1.1 cgd * FUNCTION:
67 1.1 cgd * This routine takes care of scanning and printing comments.
68 1.1 cgd *
69 1.1 cgd * ALGORITHM:
70 1.1 cgd * 1) Decide where the comment should be aligned, and if lines should
71 1.1 cgd * be broken.
72 1.1 cgd * 2) If lines should not be broken and filled, just copy up to end of
73 1.1 cgd * comment.
74 1.1 cgd * 3) If lines should be filled, then scan thru input_buffer copying
75 1.1 cgd * characters to com_buf. Remember where the last blank, tab, or
76 1.1 cgd * newline was. When line is filled, print up to last blank and
77 1.1 cgd * continue copying.
78 1.1 cgd *
79 1.1 cgd * HISTORY:
80 1.1 cgd * November 1976 D A Willcox of CAC Initial coding
81 1.1 cgd * 12/6/76 D A Willcox of CAC Modification to handle
82 1.1 cgd * UNIX-style comments
83 1.1 cgd *
84 1.1 cgd */
85 1.1 cgd
87 1.1 cgd /*
88 1.1 cgd * this routine processes comments. It makes an attempt to keep comments from
89 1.1 cgd * going over the max line length. If a line is too long, it moves everything
90 1.1 cgd * from the last blank to the next comment line. Blanks and tabs from the
91 1.1 cgd * beginning of the input line are removed
92 1.1 cgd */
93 1.6 lukem
94 1.7 wiz void
95 1.1 cgd pr_comment(void)
96 1.10.16.1 christos {
97 1.10.16.1 christos int now_col; /* column we are in now */
98 1.1 cgd int adj_max_col; /* Adjusted max_col for when we decide to
99 1.10.16.1 christos * spill comments over the right margin */
100 1.1 cgd char *last_bl; /* points to the last blank in the output
101 1.10.16.1 christos * buffer */
102 1.10.16.1 christos char *t_ptr; /* used for moving string */
103 1.10.16.1 christos int break_delim = opt.comment_delimiter_on_blankline;
104 1.10.16.1 christos int l_just_saw_decl = ps.just_saw_decl;
105 1.10.16.1 christos
106 1.10.16.1 christos adj_max_col = opt.max_col;
107 1.10.16.1 christos ps.just_saw_decl = 0;
108 1.10.16.1 christos last_bl = NULL; /* no blanks found so far */
109 1.10.16.1 christos ps.box_com = false; /* at first, assume that we are not in
110 1.10.16.1 christos * a boxed comment or some other
111 1.10.16.1 christos * comment that should not be touched */
112 1.10.16.1 christos ++ps.out_coms; /* keep track of number of comments */
113 1.10.16.1 christos
114 1.10.16.1 christos /* Figure where to align and how to treat the comment */
115 1.10.16.1 christos
116 1.10.16.1 christos if (ps.col_1 && !opt.format_col1_comments) { /* if comment starts in column
117 1.10.16.1 christos * 1 it should not be touched */
118 1.10.16.1 christos ps.box_com = true;
119 1.10.16.1 christos break_delim = false;
120 1.10.16.1 christos ps.com_col = 1;
121 1.10.16.1 christos }
122 1.10.16.1 christos else {
123 1.10.16.1 christos if (*buf_ptr == '-' || *buf_ptr == '*' ||
124 1.10.16.1 christos (*buf_ptr == '\n' && !opt.format_block_comments)) {
125 1.10.16.1 christos ps.box_com = true; /* A comment with a '-' or '*' immediately
126 1.10.16.1 christos * after the /+* is assumed to be a boxed
127 1.10.16.1 christos * comment. A comment with a newline
128 1.10.16.1 christos * immediately after the /+* is assumed to
129 1.10.16.1 christos * be a block comment and is treated as a
130 1.10.16.1 christos * box comment unless format_block_comments
131 1.10.16.1 christos * is nonzero (the default). */
132 1.10.16.1 christos break_delim = false;
133 1.10.16.1 christos }
134 1.10.16.1 christos if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) {
135 1.10.16.1 christos /* klg: check only if this line is blank */
136 1.10.16.1 christos /*
137 1.10.16.1 christos * If this (*and previous lines are*) blank, dont put comment way
138 1.10.16.1 christos * out at left
139 1.10.16.1 christos */
140 1.10.16.1 christos ps.com_col = (ps.ind_level - opt.unindent_displace) * opt.ind_size + 1;
141 1.10.16.1 christos adj_max_col = opt.block_comment_max_col;
142 1.10.16.1 christos if (ps.com_col <= 1)
143 1.10.16.1 christos ps.com_col = 1 + !opt.format_col1_comments;
144 1.10.16.1 christos }
145 1.10.16.1 christos else {
146 1.10.16.1 christos int target_col;
147 1.10.16.1 christos break_delim = false;
148 1.10.16.1 christos if (s_code != e_code)
149 1.10.16.1 christos target_col = count_spaces(compute_code_target(), s_code);
150 1.10.16.1 christos else {
151 1.10.16.1 christos target_col = 1;
152 1.10.16.1 christos if (s_lab != e_lab)
153 1.10.16.1 christos target_col = count_spaces(compute_label_target(), s_lab);
154 1.10.16.1 christos }
155 1.10.16.1 christos ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? opt.decl_com_ind : opt.com_ind;
156 1.10.16.1 christos if (ps.com_col <= target_col)
157 1.10.16.1 christos ps.com_col = opt.tabsize * (1 + (target_col - 1) / opt.tabsize) + 1;
158 1.10.16.1 christos if (ps.com_col + 24 > adj_max_col)
159 1.6 lukem adj_max_col = ps.com_col + 24;
160 1.10.16.1 christos }
161 1.10.16.1 christos }
162 1.10.16.1 christos if (ps.box_com) {
163 1.10.16.1 christos /*
164 1.10.16.1 christos * Find out how much indentation there was originally, because that
165 1.10.16.1 christos * much will have to be ignored by pad_output() in dump_line(). This
166 1.10.16.1 christos * is a box comment, so nothing changes -- not even indentation.
167 1.10.16.1 christos *
168 1.10.16.1 christos * The comment we're about to read usually comes from in_buffer,
169 1.10.16.1 christos * unless it has been copied into save_com.
170 1.10.16.1 christos */
171 1.10.16.1 christos char *start;
172 1.10.16.1 christos
173 1.10.16.1 christos start = buf_ptr >= save_com && buf_ptr < save_com + sc_size ?
174 1.10.16.1 christos sc_buf : in_buffer;
175 1.10.16.1 christos ps.n_comment_delta = 1 - count_spaces_until(1, start, buf_ptr - 2);
176 1.10.16.1 christos }
177 1.10.16.1 christos else {
178 1.10.16.1 christos ps.n_comment_delta = 0;
179 1.10.16.1 christos while (*buf_ptr == ' ' || *buf_ptr == '\t')
180 1.10.16.1 christos buf_ptr++;
181 1.10.16.1 christos }
182 1.10.16.1 christos ps.comment_delta = 0;
183 1.10.16.1 christos *e_com++ = '/'; /* put '/' followed by '*' into buffer */
184 1.10.16.1 christos *e_com++ = '*';
185 1.10.16.1 christos if (*buf_ptr != ' ' && !ps.box_com)
186 1.10.16.1 christos *e_com++ = ' ';
187 1.10.16.1 christos
188 1.10.16.1 christos /*
189 1.10.16.1 christos * Don't put a break delimiter if this is a one-liner that won't wrap.
190 1.10.16.1 christos */
191 1.10.16.1 christos if (break_delim)
192 1.10.16.1 christos for (t_ptr = buf_ptr; *t_ptr != '\0' && *t_ptr != '\n'; t_ptr++) {
193 1.10.16.1 christos if (t_ptr >= buf_end)
194 1.10.16.1 christos fill_buffer();
195 1.10.16.1 christos if (t_ptr[0] == '*' && t_ptr[1] == '/') {
196 1.10.16.1 christos if (adj_max_col >= count_spaces_until(ps.com_col, buf_ptr, t_ptr + 2))
197 1.10.16.1 christos break_delim = false;
198 1.10.16.1 christos break;
199 1.6 lukem }
200 1.1 cgd }
201 1.10.16.1 christos
202 1.10.16.1 christos if (break_delim) {
203 1.10.16.1 christos char *t = e_com;
204 1.10.16.1 christos e_com = s_com + 2;
205 1.10.16.1 christos *e_com = 0;
206 1.10.16.1 christos if (opt.blanklines_before_blockcomments && ps.last_token != lbrace)
207 1.10.16.1 christos prefix_blankline_requested = 1;
208 1.10.16.1 christos dump_line();
209 1.10.16.1 christos e_com = s_com = t;
210 1.10.16.1 christos if (!ps.box_com && opt.star_comment_cont)
211 1.10.16.1 christos *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
212 1.10.16.1 christos }
213 1.10.16.1 christos
214 1.1 cgd /* Start to copy the comment */
215 1.10.16.1 christos
216 1.6 lukem while (1) { /* this loop will go until the comment is
217 1.10.16.1 christos * copied */
218 1.10.16.1 christos switch (*buf_ptr) { /* this checks for various spcl cases */
219 1.10.16.1 christos case 014: /* check for a form feed */
220 1.10.16.1 christos CHECK_SIZE_COM(3);
221 1.10.16.1 christos if (!ps.box_com) { /* in a text comment, break the line here */
222 1.10.16.1 christos ps.use_ff = true;
223 1.10.16.1 christos /* fix so dump_line uses a form feed */
224 1.10.16.1 christos dump_line();
225 1.10.16.1 christos last_bl = NULL;
226 1.10.16.1 christos if (!ps.box_com && opt.star_comment_cont)
227 1.10.16.1 christos *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
228 1.10.16.1 christos while (*++buf_ptr == ' ' || *buf_ptr == '\t')
229 1.10.16.1 christos ;
230 1.10.16.1 christos }
231 1.10.16.1 christos else {
232 1.10.16.1 christos if (++buf_ptr >= buf_end)
233 1.10.16.1 christos fill_buffer();
234 1.10.16.1 christos *e_com++ = 014;
235 1.10.16.1 christos }
236 1.10.16.1 christos break;
237 1.10.16.1 christos
238 1.10.16.1 christos case '\n':
239 1.10.16.1 christos if (had_eof) { /* check for unexpected eof */
240 1.10.16.1 christos printf("Unterminated comment\n");
241 1.10.16.1 christos dump_line();
242 1.10.16.1 christos return;
243 1.10.16.1 christos }
244 1.10.16.1 christos last_bl = NULL;
245 1.10.16.1 christos CHECK_SIZE_COM(4);
246 1.10.16.1 christos if (ps.box_com || ps.last_nl) { /* if this is a boxed comment,
247 1.10.16.1 christos * we dont ignore the newline */
248 1.10.16.1 christos if (s_com == e_com)
249 1.10.16.1 christos *e_com++ = ' ';
250 1.10.16.1 christos if (!ps.box_com && e_com - s_com > 3) {
251 1.10.16.1 christos dump_line();
252 1.10.16.1 christos if (opt.star_comment_cont)
253 1.10.16.1 christos *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
254 1.10.16.1 christos }
255 1.10.16.1 christos dump_line();
256 1.10.16.1 christos if (!ps.box_com && opt.star_comment_cont)
257 1.10.16.1 christos *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
258 1.10.16.1 christos }
259 1.10.16.1 christos else {
260 1.10.16.1 christos ps.last_nl = 1;
261 1.10.16.1 christos if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
262 1.10.16.1 christos last_bl = e_com - 1;
263 1.10.16.1 christos /*
264 1.10.16.1 christos * if there was a space at the end of the last line, remember
265 1.10.16.1 christos * where it was
266 1.10.16.1 christos */
267 1.10.16.1 christos else { /* otherwise, insert one */
268 1.10.16.1 christos last_bl = e_com;
269 1.10.16.1 christos *e_com++ = ' ';
270 1.10.16.1 christos }
271 1.10.16.1 christos }
272 1.10.16.1 christos ++line_no; /* keep track of input line number */
273 1.10.16.1 christos if (!ps.box_com) {
274 1.10.16.1 christos int nstar = 1;
275 1.10.16.1 christos do { /* flush any blanks and/or tabs at start of
276 1.10.16.1 christos * next line */
277 1.10.16.1 christos if (++buf_ptr >= buf_end)
278 1.10.16.1 christos fill_buffer();
279 1.10.16.1 christos if (*buf_ptr == '*' && --nstar >= 0) {
280 1.10.16.1 christos if (++buf_ptr >= buf_end)
281 1.10.16.1 christos fill_buffer();
282 1.10.16.1 christos if (*buf_ptr == '/')
283 1.10.16.1 christos goto end_of_comment;
284 1.10.16.1 christos }
285 1.10.16.1 christos } while (*buf_ptr == ' ' || *buf_ptr == '\t');
286 1.10.16.1 christos }
287 1.10.16.1 christos else if (++buf_ptr >= buf_end)
288 1.10.16.1 christos fill_buffer();
289 1.1 cgd break; /* end of case for newline */
290 1.10.16.1 christos
291 1.6 lukem case '*': /* must check for possibility of being at end
292 1.10.16.1 christos * of comment */
293 1.10.16.1 christos if (++buf_ptr >= buf_end) /* get to next char after * */
294 1.10.16.1 christos fill_buffer();
295 1.10.16.1 christos CHECK_SIZE_COM(4);
296 1.10.16.1 christos if (*buf_ptr == '/') { /* it is the end!!! */
297 1.10.16.1 christos end_of_comment:
298 1.10.16.1 christos if (++buf_ptr >= buf_end)
299 1.10.16.1 christos fill_buffer();
300 1.10.16.1 christos if (break_delim) {
301 1.10.16.1 christos if (e_com > s_com + 3) {
302 1.10.16.1 christos dump_line();
303 1.10.16.1 christos }
304 1.10.16.1 christos else
305 1.10.16.1 christos s_com = e_com;
306 1.10.16.1 christos *e_com++ = ' ';
307 1.10.16.1 christos }
308 1.10.16.1 christos if (e_com[-1] != ' ' && e_com[-1] != '\t' && !ps.box_com)
309 1.10.16.1 christos *e_com++ = ' '; /* ensure blank before end */
310 1.10.16.1 christos *e_com++ = '*', *e_com++ = '/', *e_com = '\0';
311 1.10.16.1 christos ps.just_saw_decl = l_just_saw_decl;
312 1.10.16.1 christos return;
313 1.10.16.1 christos }
314 1.10.16.1 christos else /* handle isolated '*' */
315 1.10.16.1 christos *e_com++ = '*';
316 1.10.16.1 christos break;
317 1.10.16.1 christos default: /* we have a random char */
318 1.10.16.1 christos now_col = count_spaces_until(ps.com_col, s_com, e_com);
319 1.10.16.1 christos do {
320 1.10.16.1 christos CHECK_SIZE_COM(1);
321 1.10.16.1 christos *e_com = *buf_ptr++;
322 1.10.16.1 christos if (buf_ptr >= buf_end)
323 1.10.16.1 christos fill_buffer();
324 1.10.16.1 christos if (*e_com == ' ' || *e_com == '\t')
325 1.10.16.1 christos last_bl = e_com; /* remember we saw a blank */
326 1.10.16.1 christos ++e_com;
327 1.10.16.1 christos now_col++;
328 1.10.16.1 christos } while (!memchr("*\n\r\b\t", *buf_ptr, 6) &&
329 1.10.16.1 christos (now_col <= adj_max_col || !last_bl));
330 1.10.16.1 christos ps.last_nl = false;
331 1.10.16.1 christos if (now_col > adj_max_col && !ps.box_com && e_com[-1] > ' ') {
332 1.10.16.1 christos /*
333 1.10.16.1 christos * the comment is too long, it must be broken up
334 1.10.16.1 christos */
335 1.10.16.1 christos if (last_bl == NULL) {
336 1.10.16.1 christos dump_line();
337 1.10.16.1 christos if (!ps.box_com && opt.star_comment_cont)
338 1.10.16.1 christos *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
339 1.10.16.1 christos break;
340 1.10.16.1 christos }
341 1.10.16.1 christos *e_com = '\0';
342 1.10.16.1 christos e_com = last_bl;
343 1.10.16.1 christos dump_line();
344 1.10.16.1 christos if (!ps.box_com && opt.star_comment_cont)
345 1.10.16.1 christos *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
346 1.10.16.1 christos for (t_ptr = last_bl + 1; *t_ptr == ' ' || *t_ptr == '\t';
347 1.10.16.1 christos t_ptr++)
348 1.10.16.1 christos ;
349 1.10.16.1 christos last_bl = NULL;
350 1.10.16.1 christos /*
351 1.10.16.1 christos * t_ptr will be somewhere between e_com (dump_line() reset)
352 1.10.16.1 christos * and l_com. So it's safe to copy byte by byte from t_ptr
353 1.10.16.1 christos * to e_com without any CHECK_SIZE_COM().
354 1.10.16.1 christos */
355 1.10.16.1 christos while (*t_ptr != '\0') {
356 1.10.16.1 christos if (*t_ptr == ' ' || *t_ptr == '\t')
357 1.10.16.1 christos last_bl = e_com;
358 1.1 cgd *e_com++ = *t_ptr++;
359 1.10.16.1 christos }
360 1.10.16.1 christos }
361 1.1 cgd break;
362 1.10.16.1 christos }
363 1.1 cgd }
364 }
365