PostgreSQL Source Code  git master
io.c File Reference
#include "c.h"
#include <ctype.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "indent_globs.h"
#include "indent.h"
Include dependency graph for io.c:

Go to the source code of this file.

Functions

static int pad_output (int current, int target)
 
void dump_line (void)
 
int compute_code_target (void)
 
int compute_label_target (void)
 
int lookahead (void)
 
void lookahead_reset (void)
 
void fill_buffer (void)
 
int count_spaces_until (int cur, char *buffer, char *end)
 
int count_spaces (int cur, char *buffer)
 
void diag4 (int level, const char *msg, int a, int b)
 
void diag3 (int level, const char *msg, int a)
 
void diag2 (int level, const char *msg)
 

Variables

int comment_open
 
static int paren_target
 
static char * lookahead_buf
 
static char * lookahead_buf_end
 
static char * lookahead_start
 
static char * lookahead_ptr
 
static char * lookahead_end
 
static char * lookahead_bp_save
 

Function Documentation

◆ compute_code_target()

int compute_code_target ( void  )

Definition at line 223 of file io.c.

224 {
225  int target_col = ps.ind_size * ps.ind_level + 1;
226 
227  if (ps.paren_level)
228  if (!lineup_to_parens)
229  target_col += continuation_indent
230  * (2 * continuation_indent == ps.ind_size ? 1 : ps.paren_level);
231  else if (lineup_to_parens_always)
232  target_col = paren_target;
233  else {
234  int w;
235  int t = paren_target;
236 
237  if ((w = count_spaces(t, s_code) - max_col) > 0
238  && count_spaces(target_col, s_code) <= max_col) {
239  t -= w + 1;
240  if (t > target_col)
241  target_col = t;
242  }
243  else
244  target_col = t;
245  }
246  else if (ps.ind_stmt)
247  target_col += continuation_indent;
248  return target_col;
249 }
int lineup_to_parens
int max_col
struct parser_state ps
int continuation_indent
char * s_code
int lineup_to_parens_always
int count_spaces(int cur, char *buffer)
Definition: io.c:550
static int paren_target
Definition: io.c:49

References continuation_indent, count_spaces(), parser_state::ind_level, parser_state::ind_size, parser_state::ind_stmt, lineup_to_parens, lineup_to_parens_always, max_col, parser_state::paren_level, paren_target, ps, and s_code.

Referenced by dump_line(), main(), and pr_comment().

◆ compute_label_target()

int compute_label_target ( void  )

Definition at line 252 of file io.c.

253 {
254  return
255  ps.pcase ? (int) (case_ind * ps.ind_size) + 1
256  : *s_lab == '#' ? 1
257  : ps.ind_size * (ps.ind_level - label_offset) + 1;
258 }
#define label_offset
Definition: indent_globs.h:38
char * s_lab
float case_ind

References case_ind, parser_state::ind_level, parser_state::ind_size, label_offset, parser_state::pcase, ps, and s_lab.

Referenced by dump_line(), and pr_comment().

◆ count_spaces()

int count_spaces ( int  cur,
char *  buffer 
)

Definition at line 550 of file io.c.

551 {
552  return (count_spaces_until(cur, buffer, NULL));
553 }
struct cursor * cur
Definition: ecpg.c:28
int count_spaces_until(int cur, char *buffer, char *end)
Definition: io.c:517

References count_spaces_until(), and cur.

Referenced by compute_code_target(), dump_line(), and pr_comment().

◆ count_spaces_until()

int count_spaces_until ( int  cur,
char *  buffer,
char *  end 
)

Definition at line 517 of file io.c.

522 {
523  char *buf; /* used to look thru buffer */
524 
525  for (buf = buffer; *buf != '\0' && buf != end; ++buf) {
526  switch (*buf) {
527 
528  case '\n':
529  case 014: /* form feed */
530  cur = 1;
531  break;
532 
533  case '\t':
534  cur = tabsize * (1 + (cur - 1) / tabsize) + 1;
535  break;
536 
537  case 010: /* backspace */
538  --cur;
539  break;
540 
541  default:
542  ++cur;
543  break;
544  } /* end of switch */
545  } /* end of for loop */
546  return (cur);
547 }
int tabsize
static char * buf
Definition: pg_test_fsync.c:73

References buf, cur, and tabsize.

Referenced by count_spaces(), main(), and pr_comment().

◆ diag2()

void diag2 ( int  level,
const char *  msg 
)

Definition at line 590 of file io.c.

591 {
592  if (level)
593  found_err = 1;
594  if (output == stdout) {
595  fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
596  fprintf(stdout, "%s", msg);
597  fprintf(stdout, " */\n");
598  }
599  else {
600  fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
601  fprintf(stderr, "%s", msg);
602  fprintf(stderr, "\n");
603  }
604 }
int line_no
int found_err
FILE * output
#define fprintf
Definition: port.h:242

References found_err, fprintf, line_no, output, and generate_unaccent_rules::stdout.

Referenced by lexi(), main(), and parse().

◆ diag3()

void diag3 ( int  level,
const char *  msg,
int  a 
)

Definition at line 573 of file io.c.

574 {
575  if (level)
576  found_err = 1;
577  if (output == stdout) {
578  fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
579  fprintf(stdout, msg, a);
580  fprintf(stdout, " */\n");
581  }
582  else {
583  fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
584  fprintf(stderr, msg, a);
585  fprintf(stderr, "\n");
586  }
587 }
int a
Definition: isn.c:69

References a, found_err, fprintf, line_no, output, and generate_unaccent_rules::stdout.

Referenced by main().

◆ diag4()

void diag4 ( int  level,
const char *  msg,
int  a,
int  b 
)

Definition at line 556 of file io.c.

557 {
558  if (level)
559  found_err = 1;
560  if (output == stdout) {
561  fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
562  fprintf(stdout, msg, a, b);
563  fprintf(stdout, " */\n");
564  }
565  else {
566  fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
567  fprintf(stderr, msg, a, b);
568  fprintf(stderr, "\n");
569  }
570 }
int b
Definition: isn.c:70

References a, b, found_err, fprintf, line_no, output, and generate_unaccent_rules::stdout.

◆ dump_line()

void dump_line ( void  )

Definition at line 61 of file io.c.

62 { /* dump_line is the routine that actually
63  * effects the printing of the new source. It
64  * prints the label section, followed by the
65  * code section with the appropriate nesting
66  * level, followed by any comments */
67  int cur_col,
68  target_col = 1;
69  static int not_first_line;
70 
71  if (ps.procname[0]) {
72  ps.ind_level = 0;
73  ps.procname[0] = 0;
74  }
75  if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
76  if (suppress_blanklines > 0)
78  else {
79  ps.bl_line = true;
81  }
82  }
83  else if (!inhibit_formatting) {
85  ps.bl_line = false;
86  if (prefix_blankline_requested && not_first_line) {
88  if (n_real_blanklines == 1)
90  }
91  else {
92  if (n_real_blanklines == 0)
94  }
95  }
96  while (--n_real_blanklines >= 0)
97  putc('\n', output);
99  if (ps.ind_level == 0)
100  ps.ind_stmt = 0; /* this is a class A kludge. dont do
101  * additional statement indentation if we are
102  * at bracket level 0 */
103 
104  if (e_lab != s_lab || e_code != s_code)
105  ++code_lines; /* keep count of lines with code */
106 
107 
108  if (e_lab != s_lab) { /* print lab, if any */
109  if (comment_open) {
110  comment_open = 0;
111  fprintf(output, ".*/\n");
112  }
113  while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
114  e_lab--;
115  *e_lab = '\0';
116  cur_col = pad_output(1, compute_label_target());
117  if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0
118  || strncmp(s_lab, "#endif", 6) == 0)) {
119  char *s = s_lab;
120  if (e_lab[-1] == '\n') e_lab--;
121  do putc(*s++, output);
122  while (s < e_lab && 'a' <= *s && *s<='z');
123  while ((*s == ' ' || *s == '\t') && s < e_lab)
124  s++;
125  if (s < e_lab)
126  fprintf(output, s[0]=='/' && s[1]=='*' ? "\t%.*s" : "\t/* %.*s */",
127  (int)(e_lab - s), s);
128  }
129  else fprintf(output, "%.*s", (int)(e_lab - s_lab), s_lab);
130  cur_col = count_spaces(cur_col, s_lab);
131  }
132  else
133  cur_col = 1; /* there is no label section */
134 
135  ps.pcase = false;
136 
137  if (s_code != e_code) { /* print code section, if any */
138  char *p;
139 
140  if (comment_open) {
141  comment_open = 0;
142  fprintf(output, ".*/\n");
143  }
144  target_col = compute_code_target();
145  {
146  int i;
147 
148  for (i = 0; i < ps.p_l_follow; i++)
149  if (ps.paren_indents[i] >= 0)
150  ps.paren_indents[i] = -(ps.paren_indents[i] + target_col);
151  }
152  cur_col = pad_output(cur_col, target_col);
153  for (p = s_code; p < e_code; p++)
154  if (*p == (char) 0200)
155  fprintf(output, "%d", target_col * 7);
156  else
157  putc(*p, output);
158  cur_col = count_spaces(cur_col, s_code);
159  }
160  if (s_com != e_com) { /* print comment, if any */
161  int target = ps.com_col;
162  char *com_st = s_com;
163 
164  target += ps.comment_delta;
165  while (*com_st == '\t') /* consider original indentation in
166  * case this is a box comment */
167  com_st++, target += tabsize;
168  while (target <= 0)
169  if (*com_st == ' ')
170  target++, com_st++;
171  else if (*com_st == '\t')
172  target = tabsize * (1 + (target - 1) / tabsize) + 1, com_st++;
173  else
174  target = 1;
175  if (cur_col > target) { /* if comment can't fit on this line,
176  * put it on next line */
177  putc('\n', output);
178  cur_col = 1;
179  ++ps.out_lines;
180  }
181  while (e_com > com_st && isspace((unsigned char)e_com[-1]))
182  e_com--;
183  (void)pad_output(cur_col, target);
184  fwrite(com_st, e_com - com_st, 1, output);
186  ++ps.com_lines; /* count lines with comments */
187  }
188  if (ps.use_ff)
189  putc('\014', output);
190  else
191  putc('\n', output);
192  ++ps.out_lines;
195  ps.just_saw_decl = 0;
196  }
197  else
200  }
201  ps.decl_on_line = ps.in_decl; /* if we are in the middle of a
202  * declaration, remember that fact for
203  * proper comment indentation */
204  /* next line should be indented if we have not completed this stmt, and
205  * either we are not in a declaration or we are in an initialization
206  * assignment; but not if we're within braces in an initialization,
207  * because that scenario is handled by other rules. */
208  ps.ind_stmt = ps.in_stmt &&
209  (!ps.in_decl || (ps.block_init && ps.block_init_level <= 0));
210  ps.use_ff = false;
212  *(e_lab = s_lab) = '\0'; /* reset buffers */
213  *(e_code = s_code) = '\0';
214  *(e_com = s_com = combuf + 1) = '\0';
217  if (ps.paren_level > 0)
219  not_first_line = 1;
220 }
char * e_com
int blanklines_after_declarations
char * s_com
int suppress_blanklines
int postfix_blankline_requested
int prefix_blankline_requested
int inhibit_formatting
char * e_code
char * e_lab
char * combuf
int n_real_blanklines
int swallow_optional_blanklines
int code_lines
int compute_code_target(void)
Definition: io.c:223
int compute_label_target(void)
Definition: io.c:252
int comment_open
Definition: io.c:48
static int pad_output(int current, int target)
Definition: io.c:468
int i
Definition: isn.c:73
char procname[100]
Definition: indent_globs.h:327
int block_init_level
Definition: indent_globs.h:252
int dumped_decl_indent
Definition: indent_globs.h:321
short paren_indents[20]
Definition: indent_globs.h:300

References parser_state::bl_line, blanklines_after_declarations, parser_state::block_init, parser_state::block_init_level, code_lines, parser_state::com_col, parser_state::com_lines, combuf, parser_state::comment_delta, comment_open, compute_code_target(), compute_label_target(), count_spaces(), parser_state::decl_on_line, parser_state::dumped_decl_indent, e_code, e_com, e_lab, fprintf, i, parser_state::i_l_follow, parser_state::in_decl, parser_state::in_stmt, parser_state::ind_level, parser_state::ind_stmt, inhibit_formatting, parser_state::just_saw_decl, parser_state::n_comment_delta, n_real_blanklines, parser_state::out_lines, output, parser_state::p_l_follow, pad_output(), parser_state::paren_indents, parser_state::paren_level, paren_target, parser_state::pcase, postfix_blankline_requested, prefix_blankline_requested, parser_state::procname, ps, s_code, s_com, s_lab, suppress_blanklines, swallow_optional_blanklines, tabsize, and parser_state::use_ff.

Referenced by fill_buffer(), main(), and pr_comment().

◆ fill_buffer()

void fill_buffer ( void  )

Definition at line 346 of file io.c.

347 { /* this routine reads stuff from the input */
348  char *p;
349  int i;
350  FILE *f = input;
351 
352  if (bp_save != NULL) { /* there is a partly filled input buffer left */
353  buf_ptr = bp_save; /* do not read anything, just switch buffers */
354  buf_end = be_save;
355  bp_save = be_save = NULL;
356  lookahead_bp_save = NULL;
357  if (buf_ptr < buf_end)
358  return; /* only return if there is really something in
359  * this buffer */
360  }
361  for (p = in_buffer;;) {
362  if (p >= in_buffer_limit) {
363  int size = (in_buffer_limit - in_buffer) * 2 + 10;
364  int offset = p - in_buffer;
366  if (in_buffer == NULL)
367  errx(1, "input line too long");
368  p = in_buffer + offset;
370  }
372  i = (unsigned char) *lookahead_start++;
373  } else {
375  if ((i = getc(f)) == EOF) {
376  *p++ = ' ';
377  *p++ = '\n';
378  had_eof = true;
379  break;
380  }
381  }
382  if (i != '\0')
383  *p++ = i;
384  if (i == '\n')
385  break;
386  }
387  buf_ptr = in_buffer;
388  buf_end = p;
389  if (p - in_buffer > 2 && p[-2] == '/' && p[-3] == '*') {
390  if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
391  fill_buffer(); /* flush indent error message */
392  else {
393  int com = 0;
394 
395  p = in_buffer;
396  while (*p == ' ' || *p == '\t')
397  p++;
398  if (*p == '/' && p[1] == '*') {
399  p += 2;
400  while (*p == ' ' || *p == '\t')
401  p++;
402  if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E'
403  && p[4] == 'N' && p[5] == 'T') {
404  p += 6;
405  while (*p == ' ' || *p == '\t')
406  p++;
407  if (*p == '*')
408  com = 1;
409  else if (*p == 'O') {
410  if (*++p == 'N')
411  p++, com = 1;
412  else if (*p == 'F' && *++p == 'F')
413  p++, com = 2;
414  }
415  while (*p == ' ' || *p == '\t')
416  p++;
417  if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) {
418  if (s_com != e_com || s_lab != e_lab || s_code != e_code)
419  dump_line();
420  if (!(inhibit_formatting = com - 1)) {
421  n_real_blanklines = 0;
425  }
426  }
427  }
428  }
429  }
430  }
431  if (inhibit_formatting) {
432  p = in_buffer;
433  do
434  putc(*p, output);
435  while (*p++ != '\n');
436  }
437 }
void errx(int eval, const char *fmt,...)
Definition: err.c:58
#define realloc(a, b)
Definition: header.h:60
char * in_buffer
int had_eof
char * buf_ptr
char * in_buffer_limit
char * buf_end
FILE * input
char * be_save
char * bp_save
static char * lookahead_bp_save
Definition: io.c:56
static char * lookahead_start
Definition: io.c:53
static char * lookahead_buf
Definition: io.c:51
static char * lookahead_ptr
Definition: io.c:54
void fill_buffer(void)
Definition: io.c:346
void dump_line(void)
Definition: io.c:61
static char * lookahead_end
Definition: io.c:55
static pg_noinline void Size size
Definition: slab.c:607

References be_save, bp_save, buf_end, buf_ptr, dump_line(), e_code, e_com, e_lab, errx(), fill_buffer(), had_eof, i, in_buffer, in_buffer_limit, inhibit_formatting, input, lookahead_bp_save, lookahead_buf, lookahead_end, lookahead_ptr, lookahead_start, n_real_blanklines, output, postfix_blankline_requested, prefix_blankline_requested, realloc, s_code, s_com, s_lab, size, and suppress_blanklines.

Referenced by fill_buffer(), lexi(), main(), and pr_comment().

◆ lookahead()

int lookahead ( void  )

Definition at line 275 of file io.c.

276 {
277  /* First read whatever's in bp_save area */
279  return (unsigned char) *lookahead_bp_save++;
280  /* Else, we have to examine and probably fill the main lookahead buffer */
281  while (lookahead_ptr >= lookahead_end) {
282  int i = getc(input);
283 
284  if (i == EOF)
285  return i;
286  if (i == '\0')
287  continue; /* fill_buffer drops nulls, and so do we */
288 
290  /* Need to allocate or enlarge lookahead_buf */
291  char *new_buf;
292  size_t req;
293 
294  if (lookahead_buf == NULL) {
295  req = 64;
296  new_buf = malloc(req);
297  } else {
298  req = (lookahead_buf_end - lookahead_buf) * 2;
299  new_buf = realloc(lookahead_buf, req);
300  }
301  if (new_buf == NULL)
302  errx(1, "too much lookahead required");
304  lookahead_ptr = new_buf + (lookahead_ptr - lookahead_buf);
305  lookahead_end = new_buf + (lookahead_end - lookahead_buf);
306  lookahead_buf = new_buf;
307  lookahead_buf_end = new_buf + req;
308  }
309 
310  *lookahead_end++ = i;
311  }
312  return (unsigned char) *lookahead_ptr++;
313 }
#define malloc(a)
Definition: header.h:50
static char * lookahead_buf_end
Definition: io.c:52

References be_save, errx(), i, input, lookahead_bp_save, lookahead_buf, lookahead_buf_end, lookahead_end, lookahead_ptr, lookahead_start, malloc, and realloc.

Referenced by is_func_definition().

◆ lookahead_reset()

void lookahead_reset ( void  )

Definition at line 320 of file io.c.

321 {
322  /* Reset the main lookahead buffer */
324  /* If bp_save isn't NULL, we need to scan that first */
326 }

References bp_save, lookahead_bp_save, lookahead_ptr, and lookahead_start.

Referenced by is_func_definition().

◆ pad_output()

static int pad_output ( int  current,
int  target 
)
static

Definition at line 468 of file io.c.

474 {
475  int curr; /* internal column pointer */
476 
477  if (current >= target)
478  return (current); /* line is already long enough */
479  curr = current;
480  if (use_tabs) {
481  int tcur;
482 
483  while ((tcur = tabsize * (1 + (curr - 1) / tabsize) + 1) <= target) {
484  putc((!postgres_tab_rules ||
485  tcur != curr + 1 ||
486  target >= tcur + tabsize) ? '\t' : ' ', output);
487  curr = tcur;
488  }
489  }
490  while (curr++ < target)
491  putc(' ', output); /* pad with final blanks */
492 
493  return (target);
494 }
int use_tabs
int postgres_tab_rules

References output, postgres_tab_rules, tabsize, and use_tabs.

Referenced by dump_line().

Variable Documentation

◆ comment_open

int comment_open

Definition at line 48 of file io.c.

Referenced by dump_line().

◆ lookahead_bp_save

char* lookahead_bp_save
static

Definition at line 56 of file io.c.

Referenced by fill_buffer(), lookahead(), and lookahead_reset().

◆ lookahead_buf

char* lookahead_buf
static

Definition at line 51 of file io.c.

Referenced by fill_buffer(), and lookahead().

◆ lookahead_buf_end

char* lookahead_buf_end
static

Definition at line 52 of file io.c.

Referenced by lookahead().

◆ lookahead_end

char* lookahead_end
static

Definition at line 55 of file io.c.

Referenced by fill_buffer(), and lookahead().

◆ lookahead_ptr

char* lookahead_ptr
static

Definition at line 54 of file io.c.

Referenced by fill_buffer(), lookahead(), and lookahead_reset().

◆ lookahead_start

char* lookahead_start
static

Definition at line 53 of file io.c.

Referenced by fill_buffer(), lookahead(), and lookahead_reset().

◆ paren_target

int paren_target
static

Definition at line 49 of file io.c.

Referenced by compute_code_target(), and dump_line().