]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/indent/pr_comment.c
Import tzdata 2019c.
[FreeBSD/FreeBSD.git] / usr.bin / indent / pr_comment.c
1 /*-
2  * Copyright (c) 1985 Sun Microsystems, Inc.
3  * Copyright (c) 1980, 1993
4  *      The Regents of the University of California.  All rights reserved.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by the University of
18  *      California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35
36 #if 0
37 #ifndef lint
38 static char sccsid[] = "@(#)pr_comment.c        8.1 (Berkeley) 6/6/93";
39 #endif /* not lint */
40 #endif
41
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44
45 #include <err.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include "indent_globs.h"
50 #include "indent_codes.h"
51 #include "indent.h"
52 /*
53  * NAME:
54  *      pr_comment
55  *
56  * FUNCTION:
57  *      This routine takes care of scanning and printing comments.
58  *
59  * ALGORITHM:
60  *      1) Decide where the comment should be aligned, and if lines should
61  *         be broken.
62  *      2) If lines should not be broken and filled, just copy up to end of
63  *         comment.
64  *      3) If lines should be filled, then scan thru input_buffer copying
65  *         characters to com_buf.  Remember where the last blank, tab, or
66  *         newline was.  When line is filled, print up to last blank and
67  *         continue copying.
68  *
69  * HISTORY:
70  *      November 1976   D A Willcox of CAC      Initial coding
71  *      12/6/76         D A Willcox of CAC      Modification to handle
72  *                                              UNIX-style comments
73  *
74  */\f
75
76 /*
77  * this routine processes comments.  It makes an attempt to keep comments from
78  * going over the max line length.  If a line is too long, it moves everything
79  * from the last blank to the next comment line.  Blanks and tabs from the
80  * beginning of the input line are removed
81  */
82
83 void
84 pr_comment(void)
85 {
86     int         now_col;        /* column we are in now */
87     int         adj_max_col;    /* Adjusted max_col for when we decide to
88                                  * spill comments over the right margin */
89     char       *last_bl;        /* points to the last blank in the output
90                                  * buffer */
91     char       *t_ptr;          /* used for moving string */
92     int         break_delim = comment_delimiter_on_blankline;
93     int         l_just_saw_decl = ps.just_saw_decl;
94     adj_max_col = max_col;
95     ps.just_saw_decl = 0;
96     last_bl = NULL;             /* no blanks found so far */
97     ps.box_com = false;         /* at first, assume that we are not in
98                                          * a boxed comment or some other
99                                          * comment that should not be touched */
100     ++ps.out_coms;              /* keep track of number of comments */
101
102     /* Figure where to align and how to treat the comment */
103
104     if (ps.col_1 && !format_col1_comments) {    /* if comment starts in column
105                                                  * 1 it should not be touched */
106         ps.box_com = true;
107         break_delim = false;
108         ps.com_col = 1;
109     }
110     else {
111         if (*buf_ptr == '-' || *buf_ptr == '*' ||
112             (*buf_ptr == '\n' && !format_block_comments)) {
113             ps.box_com = true;  /* A comment with a '-' or '*' immediately
114                                  * after the /+* is assumed to be a boxed
115                                  * comment. A comment with a newline
116                                  * immediately after the /+* is assumed to
117                                  * be a block comment and is treated as a
118                                  * box comment unless format_block_comments
119                                  * is nonzero (the default). */
120             break_delim = false;
121         }
122         if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) {
123             /* klg: check only if this line is blank */
124             /*
125              * If this (*and previous lines are*) blank, dont put comment way
126              * out at left
127              */
128             ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
129             adj_max_col = block_comment_max_col;
130             if (ps.com_col <= 1)
131                 ps.com_col = 1 + !format_col1_comments;
132         }
133         else {
134             int target_col;
135             break_delim = false;
136             if (s_code != e_code)
137                 target_col = count_spaces(compute_code_target(), s_code);
138             else {
139                 target_col = 1;
140                 if (s_lab != e_lab)
141                     target_col = count_spaces(compute_label_target(), s_lab);
142             }
143             ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? ps.decl_com_ind : ps.com_ind;
144             if (ps.com_col <= target_col)
145                 ps.com_col = ((target_col + 7) & ~7) + 1;
146             if (ps.com_col + 24 > adj_max_col)
147                 adj_max_col = ps.com_col + 24;
148         }
149     }
150     if (ps.box_com) {
151         /*
152          * Find out how much indentation there was originally, because that
153          * much will have to be ignored by pad_output() in dump_line(). This
154          * is a box comment, so nothing changes -- not even indentation.
155          *
156          * The comment we're about to read usually comes from in_buffer,
157          * unless it has been copied into save_com.
158          */
159         char *start = buf_ptr >= save_com && buf_ptr < save_com + sc_size ? bp_save : buf_ptr;
160         ps.n_comment_delta = 1 - count_spaces_until(1, in_buffer, start - 2);
161     }
162     else {
163         ps.n_comment_delta = 0;
164         while (*buf_ptr == ' ' || *buf_ptr == '\t')
165             buf_ptr++;
166     }
167     ps.comment_delta = 0;
168     *e_com++ = '/';             /* put '/' followed by '*' into buffer */
169     *e_com++ = '*';
170     if (*buf_ptr != ' ' && !ps.box_com)
171         *e_com++ = ' ';
172
173     /*
174      * Don't put a break delimiter if this is a one-liner that won't wrap.
175      */
176     if (break_delim)
177         for (t_ptr = buf_ptr; *t_ptr != '\0' && *t_ptr != '\n'; t_ptr++) {
178             if (t_ptr >= buf_end)
179                 fill_buffer();
180             if (t_ptr[0] == '*' && t_ptr[1] == '/') {
181                 if (adj_max_col >= count_spaces_until(ps.com_col, buf_ptr, t_ptr + 2))
182                     break_delim = false;
183                 break;
184             }
185         }
186
187     if (break_delim) {
188         char       *t = e_com;
189         e_com = s_com + 2;
190         *e_com = 0;
191         if (blanklines_before_blockcomments && ps.last_token != lbrace)
192             prefix_blankline_requested = 1;
193         dump_line();
194         e_com = s_com = t;
195         if (!ps.box_com && star_comment_cont)
196             *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
197     }
198
199     if (troff)
200         adj_max_col = 80;
201
202     /* Start to copy the comment */
203
204     while (1) {                 /* this loop will go until the comment is
205                                  * copied */
206         CHECK_SIZE_COM;
207         switch (*buf_ptr) {     /* this checks for various spcl cases */
208         case 014:               /* check for a form feed */
209             if (!ps.box_com) {  /* in a text comment, break the line here */
210                 ps.use_ff = true;
211                 /* fix so dump_line uses a form feed */
212                 dump_line();
213                 last_bl = NULL;
214                 if (!ps.box_com && star_comment_cont)
215                     *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
216                 while (*++buf_ptr == ' ' || *buf_ptr == '\t')
217                     ;
218             }
219             else {
220                 if (++buf_ptr >= buf_end)
221                     fill_buffer();
222                 *e_com++ = 014;
223             }
224             break;
225
226         case '\n':
227             if (had_eof) {      /* check for unexpected eof */
228                 printf("Unterminated comment\n");
229                 dump_line();
230                 return;
231             }
232             last_bl = NULL;
233             if (ps.box_com || ps.last_nl) {     /* if this is a boxed comment,
234                                                  * we dont ignore the newline */
235                 if (s_com == e_com)
236                     *e_com++ = ' ';
237                 if (!ps.box_com && e_com - s_com > 3) {
238                     dump_line();
239                     if (star_comment_cont)
240                         *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
241                 }
242                 dump_line();
243                 if (!ps.box_com && star_comment_cont)
244                     *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
245             }
246             else {
247                 ps.last_nl = 1;
248                 if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
249                     last_bl = e_com - 1;
250                 /*
251                  * if there was a space at the end of the last line, remember
252                  * where it was
253                  */
254                 else {          /* otherwise, insert one */
255                     last_bl = e_com;
256                     CHECK_SIZE_COM;
257                     *e_com++ = ' ';
258                 }
259             }
260             ++line_no;          /* keep track of input line number */
261             if (!ps.box_com) {
262                 int         nstar = 1;
263                 do {            /* flush any blanks and/or tabs at start of
264                                  * next line */
265                     if (++buf_ptr >= buf_end)
266                         fill_buffer();
267                     if (*buf_ptr == '*' && --nstar >= 0) {
268                         if (++buf_ptr >= buf_end)
269                             fill_buffer();
270                         if (*buf_ptr == '/')
271                             goto end_of_comment;
272                     }
273                 } while (*buf_ptr == ' ' || *buf_ptr == '\t');
274             }
275             else if (++buf_ptr >= buf_end)
276                 fill_buffer();
277             break;              /* end of case for newline */
278
279         case '*':               /* must check for possibility of being at end
280                                  * of comment */
281             if (++buf_ptr >= buf_end)   /* get to next char after * */
282                 fill_buffer();
283
284             if (*buf_ptr == '/') {      /* it is the end!!! */
285         end_of_comment:
286                 if (++buf_ptr >= buf_end)
287                     fill_buffer();
288                 CHECK_SIZE_COM;
289                 if (break_delim) {
290                     if (e_com > s_com + 3) {
291                         dump_line();
292                     }
293                     else
294                         s_com = e_com;
295                     *e_com++ = ' ';
296                 }
297                 if (e_com[-1] != ' ' && e_com[-1] != '\t' && !ps.box_com)
298                     *e_com++ = ' ';     /* ensure blank before end */
299                 *e_com++ = '*', *e_com++ = '/', *e_com = '\0';
300                 ps.just_saw_decl = l_just_saw_decl;
301                 return;
302             }
303             else                /* handle isolated '*' */
304                 *e_com++ = '*';
305             break;
306         default:                /* we have a random char */
307             now_col = count_spaces_until(ps.com_col, s_com, e_com);
308             do {
309                 *e_com = *buf_ptr++;
310                 if (buf_ptr >= buf_end)
311                     fill_buffer();
312                 if (*e_com == ' ' || *e_com == '\t')
313                     last_bl = e_com;    /* remember we saw a blank */
314                 ++e_com;
315                 now_col++;
316             } while (!memchr("*\n\r\b\t", *buf_ptr, 6) &&
317                 (now_col <= adj_max_col || !last_bl));
318             ps.last_nl = false;
319             if (now_col > adj_max_col && !ps.box_com && e_com[-1] > ' ') {
320                 /*
321                  * the comment is too long, it must be broken up
322                  */
323                 if (last_bl == NULL) {
324                     dump_line();
325                     if (!ps.box_com && star_comment_cont)
326                         *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
327                     break;
328                 }
329                 *e_com = '\0';
330                 e_com = last_bl;
331                 dump_line();
332                 if (!ps.box_com && star_comment_cont)
333                     *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
334                 for (t_ptr = last_bl + 1; *t_ptr == ' ' || *t_ptr == '\t';
335                     t_ptr++)
336                         ;
337                 last_bl = NULL;
338                 while (*t_ptr != '\0') {
339                     if (*t_ptr == ' ' || *t_ptr == '\t')
340                         last_bl = e_com;
341                     *e_com++ = *t_ptr++;
342                 }
343             }
344             break;
345         }
346     }
347 }