PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pr_comment.c File Reference
#include "c.h"
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "indent_globs.h"
#include "indent_codes.h"
#include "indent.h"
Include dependency graph for pr_comment.c:

Go to the source code of this file.

Functions

void pr_comment (void)
 

Function Documentation

◆ pr_comment()

void pr_comment ( void  )

Definition at line 79 of file pr_comment.c.

80{
81 int now_col; /* column we are in now */
82 int adj_max_col; /* Adjusted max_col for when we decide to
83 * spill comments over the right margin */
84 char *last_bl; /* points to the last blank in the output
85 * buffer */
86 char *t_ptr; /* used for moving string */
87 int break_delim = comment_delimiter_on_blankline;
88 int l_just_saw_decl = ps.just_saw_decl;
89 adj_max_col = max_col;
90 ps.just_saw_decl = 0;
91 last_bl = NULL; /* no blanks found so far */
92 ps.box_com = false; /* at first, assume that we are not in
93 * a boxed comment or some other
94 * comment that should not be touched */
95 ++ps.out_coms; /* keep track of number of comments */
96
97 /* Figure where to align and how to treat the comment */
98
99 if (ps.col_1 && !format_col1_comments) { /* if comment starts in column
100 * 1 it should not be touched */
101 ps.box_com = true;
102 break_delim = false;
103 ps.com_col = 1;
104 }
105 else {
106 if (*buf_ptr == '-' || *buf_ptr == '*' ||
107 (*buf_ptr == '\n' && !format_block_comments)) {
108 ps.box_com = true; /* A comment with a '-' or '*' immediately
109 * after the /+* is assumed to be a boxed
110 * comment. A comment with a newline
111 * immediately after the /+* is assumed to
112 * be a block comment and is treated as a
113 * box comment unless format_block_comments
114 * is nonzero (the default). */
115 break_delim = false;
116 }
117 if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) {
118 /* klg: check only if this line is blank */
119 /*
120 * If this (*and previous lines are*) blank, dont put comment way
121 * out at left
122 */
124 adj_max_col = block_comment_max_col;
125 if (ps.com_col <= 1)
127 }
128 else {
129 int target_col;
130 break_delim = false;
131 if (s_code != e_code)
132 target_col = count_spaces(compute_code_target(), s_code);
133 else {
134 target_col = 1;
135 if (s_lab != e_lab)
136 target_col = count_spaces(compute_label_target(), s_lab);
137 }
138 if (s_lab != e_lab && s_lab[1] == 'e' &&
139 (strncmp(s_lab, "#endif", 6) == 0 ||
140 strncmp(s_lab, "#else", 5) == 0))
141 ps.com_col = else_endif_com_ind <= target_col
142 ? target_col + 1 : else_endif_com_ind;
143 else
146 if (ps.com_col <= target_col)
147 ps.com_col = tabsize * (1 + (target_col - 1) / tabsize) + 1;
148 if (ps.com_col + 24 > adj_max_col)
149 adj_max_col = ps.com_col + 24;
150 }
151 }
152 if (ps.box_com) {
153 /*
154 * Find out how much indentation there was originally, because that
155 * much will have to be ignored by pad_output() in dump_line(). This
156 * is a box comment, so nothing changes -- not even indentation.
157 *
158 * The comment we're about to read usually comes from in_buffer,
159 * unless it has been copied into save_com.
160 */
161 char *start;
162
166 }
167 else {
169 while (*buf_ptr == ' ' || *buf_ptr == '\t')
170 buf_ptr++;
171 }
172 ps.comment_delta = 0;
173 *e_com++ = '/'; /* put '/' followed by '*' into buffer */
174 *e_com++ = '*';
175 if (*buf_ptr != ' ' && !ps.box_com)
176 *e_com++ = ' ';
177
178 /*
179 * Don't put a break delimiter if this is a one-liner that won't wrap.
180 */
181 if (break_delim)
182 for (t_ptr = buf_ptr; *t_ptr != '\0' && *t_ptr != '\n'; t_ptr++) {
183 if (t_ptr >= buf_end)
184 fill_buffer();
185 if (t_ptr[0] == '*' && t_ptr[1] == '/') {
186 if (adj_max_col >= count_spaces_until(ps.com_col, buf_ptr, t_ptr + 2))
187 break_delim = false;
188 break;
189 }
190 }
191
192 if (break_delim) {
193 char *t = e_com;
194 e_com = s_com + 2;
195 *e_com = 0;
198 dump_line();
199 e_com = s_com = t;
201 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
202 }
203
204 /* Start to copy the comment */
205
206 while (1) { /* this loop will go until the comment is
207 * copied */
208 switch (*buf_ptr) { /* this checks for various spcl cases */
209 case 014: /* check for a form feed */
211 if (!ps.box_com) { /* in a text comment, break the line here */
212 ps.use_ff = true;
213 /* fix so dump_line uses a form feed */
214 dump_line();
215 last_bl = NULL;
217 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
218 while (*++buf_ptr == ' ' || *buf_ptr == '\t')
219 ;
220 }
221 else {
222 if (++buf_ptr >= buf_end)
223 fill_buffer();
224 *e_com++ = 014;
225 }
226 break;
227
228 case '\n':
229 if (had_eof) { /* check for unexpected eof */
230 printf("Unterminated comment\n");
231 dump_line();
232 return;
233 }
234 last_bl = NULL;
236 if (ps.box_com || ps.last_nl) { /* if this is a boxed comment,
237 * we dont ignore the newline */
238 if (s_com == e_com)
239 *e_com++ = ' ';
240 if (!ps.box_com && e_com - s_com > 3) {
241 dump_line();
243 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
244 }
245 dump_line();
247 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
248 }
249 else {
250 ps.last_nl = 1;
251 if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
252 last_bl = e_com - 1;
253 /*
254 * if there was a space at the end of the last line, remember
255 * where it was
256 */
257 else { /* otherwise, insert one */
258 last_bl = e_com;
259 *e_com++ = ' ';
260 }
261 }
262 ++line_no; /* keep track of input line number */
263 if (!ps.box_com) {
264 int nstar = 1;
265 do { /* flush any blanks and/or tabs at start of
266 * next line */
267 if (++buf_ptr >= buf_end)
268 fill_buffer();
269 if (*buf_ptr == '*' && --nstar >= 0) {
270 if (++buf_ptr >= buf_end)
271 fill_buffer();
272 if (*buf_ptr == '/')
273 goto end_of_comment;
274 }
275 } while (*buf_ptr == ' ' || *buf_ptr == '\t');
276 }
277 else if (++buf_ptr >= buf_end)
278 fill_buffer();
279 break; /* end of case for newline */
280
281 case '*': /* must check for possibility of being at end
282 * of comment */
283 if (++buf_ptr >= buf_end) /* get to next char after * */
284 fill_buffer();
286 if (*buf_ptr == '/') { /* it is the end!!! */
287 end_of_comment:
288 if (++buf_ptr >= buf_end)
289 fill_buffer();
290 if (break_delim) {
291 if (e_com > s_com + 3) {
292 dump_line();
293 }
294 else
295 s_com = e_com;
296 *e_com++ = ' ';
297 }
298 if (e_com[-1] != ' ' && e_com[-1] != '\t' && !ps.box_com)
299 *e_com++ = ' '; /* ensure blank before end */
300 *e_com++ = '*', *e_com++ = '/', *e_com = '\0';
301 ps.just_saw_decl = l_just_saw_decl;
302 return;
303 }
304 else /* handle isolated '*' */
305 *e_com++ = '*';
306 break;
307 default: /* we have a random char */
309 do {
311 *e_com = *buf_ptr++;
312 if (buf_ptr >= buf_end)
313 fill_buffer();
314 if (*e_com == ' ' || *e_com == '\t')
315 last_bl = e_com; /* remember we saw a blank */
316 ++e_com;
317 now_col++;
318 } while (!memchr("*\n\r\b\t", *buf_ptr, 6) &&
319 (now_col <= adj_max_col || !last_bl));
320 ps.last_nl = false;
321 if (now_col > adj_max_col && !ps.box_com && e_com[-1] > ' ') {
322 /*
323 * the comment is too long, it must be broken up
324 */
325 if (last_bl == NULL) {
326 dump_line();
328 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
329 break;
330 }
331 *e_com = '\0';
332 e_com = last_bl;
333 dump_line();
335 *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
336 for (t_ptr = last_bl + 1; *t_ptr == ' ' || *t_ptr == '\t';
337 t_ptr++)
338 ;
339 last_bl = NULL;
340 /*
341 * t_ptr will be somewhere between e_com (dump_line() reset)
342 * and l_com. So it's safe to copy byte by byte from t_ptr
343 * to e_com without any CHECK_SIZE_COM().
344 */
345 while (*t_ptr != '\0') {
346 if (*t_ptr == ' ' || *t_ptr == '\t')
347 last_bl = e_com;
348 *e_com++ = *t_ptr++;
349 }
350 }
351 break;
352 }
353 }
354}
return str start
int compute_code_target(void)
Definition: io.c:223
int compute_label_target(void)
Definition: io.c:252
int count_spaces_until(int, char *, char *)
Definition: io.c:517
void fill_buffer(void)
Definition: io.c:346
void dump_line(void)
Definition: io.c:61
int count_spaces(int, char *)
Definition: io.c:550
#define lbrace
Definition: indent_codes.h:45
int star_comment_cont
char * e_com
int tabsize
int else_endif_com_ind
int block_comment_max_col
#define sc_size
Definition: indent_globs.h:37
int format_col1_comments
char * in_buffer
int had_eof
char * s_com
char sc_buf[sc_size]
#define CHECK_SIZE_COM(desired_size)
Definition: indent_globs.h:70
char * buf_ptr
int prefix_blankline_requested
char * save_com
int blanklines_before_blockcomments
int line_no
char * e_code
char * e_lab
char * buf_end
char * s_lab
int max_col
int format_block_comments
struct parser_state ps
int comment_delimiter_on_blankline
char * s_code
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:76
#define printf(...)
Definition: port.h:244
int unindent_displace
Definition: indent_globs.h:307

References blanklines_before_blockcomments, block_comment_max_col, parser_state::box_com, buf_end, buf_ptr, CHECK_SIZE_COM, parser_state::col_1, parser_state::com_col, parser_state::com_ind, comment_delimiter_on_blankline, parser_state::comment_delta, compute_code_target(), compute_label_target(), count_spaces(), count_spaces_until(), parser_state::decl_com_ind, parser_state::decl_on_line, dump_line(), e_code, e_com, e_lab, else_endif_com_ind, fill_buffer(), format_block_comments, format_col1_comments, had_eof, if(), in_buffer, parser_state::ind_level, parser_state::ind_size, parser_state::just_saw_decl, parser_state::last_nl, parser_state::last_token, lbrace, line_no, max_col, parser_state::n_comment_delta, parser_state::out_coms, prefix_blankline_requested, printf, ps, s_code, s_com, s_lab, save_com, sc_buf, sc_size, star_comment_cont, start, tabsize, parser_state::unindent_displace, and parser_state::use_ff.

Referenced by main().