PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
indent.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define nitems(x)   (sizeof((x)) / sizeof((x)[0]))
 

Functions

void add_typename (const char *)
 
void alloc_typenames (void)
 
int compute_code_target (void)
 
int compute_label_target (void)
 
int count_spaces (int, char *)
 
int count_spaces_until (int, char *, char *)
 
int lexi (struct parser_state *)
 
void diag2 (int, const char *)
 
void diag3 (int, const char *, int)
 
void diag4 (int, const char *, int, int)
 
void dump_line (void)
 
int lookahead (void)
 
void lookahead_reset (void)
 
void fill_buffer (void)
 
void parse (int)
 
void pr_comment (void)
 
void set_defaults (void)
 
void set_option (char *)
 
void set_profile (const char *)
 

Macro Definition Documentation

◆ nitems

#define nitems (   x)    (sizeof((x)) / sizeof((x)[0]))

Definition at line 31 of file indent.h.

Function Documentation

◆ add_typename()

void add_typename ( const char *  key)

Definition at line 687 of file lexi.c.

688 {
689  int comparison;
690  const char *copy;
691 
692  if (typename_top + 1 >= typename_count) {
693  typenames = realloc((void *)typenames,
694  sizeof(typenames[0]) * (typename_count *= 2));
695  if (typenames == NULL)
696  err(1, NULL);
697  }
698  if (typename_top == -1)
699  typenames[++typename_top] = copy = strdup(key);
700  else if ((comparison = strcmp(key, typenames[typename_top])) >= 0) {
701  /* take advantage of sorted input */
702  if (comparison == 0) /* remove duplicates */
703  return;
704  typenames[++typename_top] = copy = strdup(key);
705  }
706  else {
707  int p;
708 
709  for (p = 0; (comparison = strcmp(key, typenames[p])) > 0; p++)
710  /* find place for the new key */;
711  if (comparison == 0) /* remove duplicates */
712  return;
713  memmove(&typenames[p + 1], &typenames[p],
714  sizeof(typenames[0]) * (++typename_top - p));
715  typenames[p] = copy = strdup(key);
716  }
717 
718  if (copy == NULL)
719  err(1, NULL);
720 }
void err(int eval, const char *fmt,...)
Definition: err.c:43
#define realloc(a, b)
Definition: header.h:60
int typename_count
Definition: lexi.c:116
int typename_top
Definition: lexi.c:117
const char ** typenames
Definition: lexi.c:115

References err(), sort-test::key, realloc, typename_count, typename_top, and typenames.

Referenced by add_typedefs_from_file(), and set_option().

◆ alloc_typenames()

void alloc_typenames ( void  )

Definition at line 677 of file lexi.c.

678 {
679 
680  typenames = (const char **)malloc(sizeof(typenames[0]) *
681  (typename_count = 16));
682  if (typenames == NULL)
683  err(1, NULL);
684 }
#define malloc(a)
Definition: header.h:50

References err(), malloc, typename_count, and typenames.

Referenced by main().

◆ 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:72

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:68

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:69

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:72
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
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().

◆ lexi()

int lexi ( struct parser_state state)

Definition at line 216 of file lexi.c.

217 {
218  int unary_delim; /* this is set to 1 if the current token
219  * forces a following operator to be unary */
220  int code; /* internal code to be returned */
221  char qchar; /* the delimiter character for a string */
222 
223  e_token = s_token; /* point to start of place to save token */
224  unary_delim = false;
225  state->col_1 = state->last_nl; /* tell world that this token started
226  * in column 1 iff the last thing
227  * scanned was a newline */
228  state->last_nl = false;
229 
230  while (*buf_ptr == ' ' || *buf_ptr == '\t') { /* get rid of blanks */
231  state->col_1 = false; /* leading blanks imply token is not in column
232  * 1 */
233  if (++buf_ptr >= buf_end)
234  fill_buffer();
235  }
236 
237  /* Scan an alphanumeric token */
238  if (chartype[*buf_ptr & 127] == alphanum ||
239  (buf_ptr[0] == '.' && isdigit((unsigned char)buf_ptr[1]))) {
240  /*
241  * we have a character or number
242  */
243  struct templ *p;
244 
245  if (isdigit((unsigned char)*buf_ptr) ||
246  (buf_ptr[0] == '.' && isdigit((unsigned char)buf_ptr[1]))) {
247  int seendot = 0,
248  seenexp = 0,
249  seensfx = 0;
250 
251  /*
252  * base 2, base 8, base 16:
253  */
254  if (buf_ptr[0] == '0' && buf_ptr[1] != '.') {
255  int len;
256 
257  if (buf_ptr[1] == 'b' || buf_ptr[1] == 'B')
258  len = strspn(buf_ptr + 2, "01") + 2;
259  else if (buf_ptr[1] == 'x' || buf_ptr[1] == 'X')
260  len = strspn(buf_ptr + 2, "0123456789ABCDEFabcdef") + 2;
261  else
262  len = strspn(buf_ptr + 1, "012345678") + 1;
263  if (len > 0) {
265  memcpy(e_token, buf_ptr, len);
266  e_token += len;
267  buf_ptr += len;
268  }
269  else
270  diag2(1, "Unterminated literal");
271  }
272  else /* base 10: */
273  while (1) {
274  if (*buf_ptr == '.') {
275  if (seendot)
276  break;
277  else
278  seendot++;
279  }
280  CHECK_SIZE_TOKEN(3);
281  *e_token++ = *buf_ptr++;
282  if (!isdigit((unsigned char)*buf_ptr) && *buf_ptr != '.') {
283  if ((*buf_ptr != 'E' && *buf_ptr != 'e') || seenexp)
284  break;
285  else {
286  seenexp++;
287  seendot++;
288  *e_token++ = *buf_ptr++;
289  if (*buf_ptr == '+' || *buf_ptr == '-')
290  *e_token++ = *buf_ptr++;
291  }
292  }
293  }
294 
295  while (1) {
296  CHECK_SIZE_TOKEN(2);
297  if (!(seensfx & 1) && (*buf_ptr == 'U' || *buf_ptr == 'u')) {
298  *e_token++ = *buf_ptr++;
299  seensfx |= 1;
300  continue;
301  }
302  if (!(seensfx & 2) && (strchr("fFlL", *buf_ptr) != NULL)) {
303  if (buf_ptr[1] == buf_ptr[0])
304  *e_token++ = *buf_ptr++;
305  *e_token++ = *buf_ptr++;
306  seensfx |= 2;
307  continue;
308  }
309  break;
310  }
311  }
312  else
313  while (chartype[*buf_ptr & 127] == alphanum || *buf_ptr == BACKSLASH) {
314  /* fill_buffer() terminates buffer with newline */
315  if (*buf_ptr == BACKSLASH) {
316  if (*(buf_ptr + 1) == '\n') {
317  buf_ptr += 2;
318  if (buf_ptr >= buf_end)
319  fill_buffer();
320  } else
321  break;
322  }
323  CHECK_SIZE_TOKEN(1);
324  /* copy it over */
325  *e_token++ = *buf_ptr++;
326  if (buf_ptr >= buf_end)
327  fill_buffer();
328  }
329  *e_token = '\0';
330 
331  if (s_token[0] == 'L' && s_token[1] == '\0' &&
332  (*buf_ptr == '"' || *buf_ptr == '\''))
333  return (strpfx);
334 
335  while (*buf_ptr == ' ' || *buf_ptr == '\t') { /* get rid of blanks */
336  if (++buf_ptr >= buf_end)
337  fill_buffer();
338  }
339  state->keyword = 0;
340  if (state->last_token == structure && !state->p_l_follow) {
341  /* if last token was 'struct' and we're not
342  * in parentheses, then this token
343  * should be treated as a declaration */
344  state->last_u_d = true;
345  return (decl);
346  }
347  /*
348  * Operator after identifier is binary unless last token was 'struct'
349  */
350  state->last_u_d = (state->last_token == structure);
351 
352  p = bsearch(s_token,
353  specials,
354  sizeof(specials) / sizeof(specials[0]),
355  sizeof(specials[0]),
356  strcmp_type);
357  if (p == NULL) { /* not a special keyword... */
358  char *u;
359 
360  /* ... so maybe a type_t or a typedef */
361  if ((auto_typedefs && ((u = strrchr(s_token, '_')) != NULL) &&
362  strcmp(u, "_t") == 0) || (typename_top >= 0 &&
363  bsearch(s_token, typenames, typename_top + 1,
364  sizeof(typenames[0]), strcmp_type))) {
365  state->keyword = 4; /* a type name */
366  state->last_u_d = true;
367  goto found_typename;
368  }
369  } else { /* we have a keyword */
370  state->keyword = p->rwcode;
371  state->last_u_d = true;
372  switch (p->rwcode) {
373  case 7: /* it is a switch */
374  return (swstmt);
375  case 8: /* a case or default */
376  return (casestmt);
377 
378  case 3: /* a "struct" */
379  /* FALLTHROUGH */
380  case 4: /* one of the declaration keywords */
381  found_typename:
382  if (state->p_l_follow) {
383  /* inside parens: cast, param list, offsetof or sizeof */
384  state->cast_mask |= (1 << state->p_l_follow) & ~state->not_cast_mask;
385  }
386  if (state->last_token == period || state->last_token == unary_op) {
387  state->keyword = 0;
388  break;
389  }
390  if (p != NULL && p->rwcode == 3)
391  return (structure);
392  if (state->p_l_follow)
393  break;
394  return (decl);
395 
396  case 5: /* if, while, for */
397  return (sp_paren);
398 
399  case 6: /* do, else */
400  return (sp_nparen);
401 
402  case 10: /* storage class specifier */
403  return (storage);
404 
405  case 11: /* typedef */
406  return (type_def);
407 
408  default: /* all others are treated like any other
409  * identifier */
410  return (ident);
411  } /* end of switch */
412  } /* end of if (found_it) */
413  if (*buf_ptr == '(' && state->tos <= 1 && state->ind_level == 0 &&
414  state->in_parameter_declaration == 0 && state->block_init == 0) {
416  strncpy(state->procname, token, sizeof state->procname - 1);
417  if (state->in_decl)
418  state->in_parameter_declaration = 1;
419  return (funcname);
420  }
421  }
422  /*
423  * The following hack attempts to guess whether or not the current
424  * token is in fact a declaration keyword -- one that has been
425  * typedefd
426  */
427  else if (!state->p_l_follow && !state->block_init &&
428  !state->in_stmt &&
429  ((*buf_ptr == '*' && buf_ptr[1] != '=') ||
430  isalpha((unsigned char)*buf_ptr)) &&
431  (state->last_token == semicolon || state->last_token == lbrace ||
432  state->last_token == rbrace)) {
433  state->keyword = 4; /* a type name */
434  state->last_u_d = true;
435  return decl;
436  }
437  if (state->last_token == decl) /* if this is a declared variable,
438  * then following sign is unary */
439  state->last_u_d = true; /* will make "int a -1" work */
440  return (ident); /* the ident is not in the list */
441  } /* end of processing for alphanum character */
442 
443  /* Scan a non-alphanumeric token */
444 
445  CHECK_SIZE_TOKEN(3); /* things like "<<=" */
446  *e_token++ = *buf_ptr; /* if it is only a one-character token, it is
447  * moved here */
448  *e_token = '\0';
449  if (++buf_ptr >= buf_end)
450  fill_buffer();
451 
452  switch (*token) {
453  case '\n':
454  unary_delim = state->last_u_d;
455  state->last_nl = true; /* remember that we just had a newline */
456  code = (had_eof ? 0 : newline);
457 
458  /*
459  * if data has been exhausted, the newline is a dummy, and we should
460  * return code to stop
461  */
462  break;
463 
464  case '\'': /* start of quoted character */
465  case '"': /* start of string */
466  qchar = *token;
467  do { /* copy the string */
468  while (1) { /* move one character or [/<char>]<char> */
469  if (*buf_ptr == '\n') {
470  diag2(1, "Unterminated literal");
471  goto stop_lit;
472  }
473  CHECK_SIZE_TOKEN(2);
474  *e_token = *buf_ptr++;
475  if (buf_ptr >= buf_end)
476  fill_buffer();
477  if (*e_token == BACKSLASH) { /* if escape, copy extra char */
478  if (*buf_ptr == '\n') /* check for escaped newline */
479  ++line_no;
480  *++e_token = *buf_ptr++;
481  ++e_token; /* we must increment this again because we
482  * copied two chars */
483  if (buf_ptr >= buf_end)
484  fill_buffer();
485  }
486  else
487  break; /* we copied one character */
488  } /* end of while (1) */
489  } while (*e_token++ != qchar);
490 stop_lit:
491  code = ident;
492  break;
493 
494  case ('('):
495  case ('['):
496  unary_delim = true;
497  code = lparen;
498  break;
499 
500  case (')'):
501  case (']'):
502  code = rparen;
503  break;
504 
505  case '#':
506  unary_delim = state->last_u_d;
507  code = preesc;
508  break;
509 
510  case '?':
511  unary_delim = true;
512  code = question;
513  break;
514 
515  case (':'):
516  code = colon;
517  unary_delim = true;
518  break;
519 
520  case (';'):
521  unary_delim = true;
522  code = semicolon;
523  break;
524 
525  case ('{'):
526  unary_delim = true;
527 
528  /*
529  * if (state->in_or_st) state->block_init = 1;
530  */
531  /* ? code = state->block_init ? lparen : lbrace; */
532  code = lbrace;
533  break;
534 
535  case ('}'):
536  unary_delim = true;
537  /* ? code = state->block_init ? rparen : rbrace; */
538  code = rbrace;
539  break;
540 
541  case 014: /* a form feed */
542  unary_delim = state->last_u_d;
543  state->last_nl = true; /* remember this so we can set 'state->col_1'
544  * right */
545  code = form_feed;
546  break;
547 
548  case (','):
549  unary_delim = true;
550  code = comma;
551  break;
552 
553  case '.':
554  unary_delim = false;
555  code = period;
556  break;
557 
558  case '-':
559  case '+': /* check for -, +, --, ++ */
560  code = (state->last_u_d ? unary_op : binary_op);
561  unary_delim = true;
562 
563  if (*buf_ptr == token[0]) {
564  /* check for doubled character */
565  *e_token++ = *buf_ptr++;
566  /* buffer overflow will be checked at end of loop */
567  if (state->last_token == ident || state->last_token == rparen) {
568  code = (state->last_u_d ? unary_op : postop);
569  /* check for following ++ or -- */
570  unary_delim = false;
571  }
572  }
573  else if (*buf_ptr == '=')
574  /* check for operator += */
575  *e_token++ = *buf_ptr++;
576  else if (*buf_ptr == '>') {
577  /* check for operator -> */
578  *e_token++ = *buf_ptr++;
579  unary_delim = false;
580  code = unary_op;
581  state->want_blank = false;
582  }
583  break; /* buffer overflow will be checked at end of
584  * switch */
585 
586  case '=':
587  if (state->in_or_st)
588  state->block_init = 1;
589 #ifdef undef
590  if (chartype[*buf_ptr & 127] == opchar) { /* we have two char assignment */
591  e_token[-1] = *buf_ptr++;
592  if ((e_token[-1] == '<' || e_token[-1] == '>') && e_token[-1] == *buf_ptr)
593  *e_token++ = *buf_ptr++;
594  *e_token++ = '='; /* Flip =+ to += */
595  *e_token = 0;
596  }
597 #else
598  if (*buf_ptr == '=') {/* == */
599  *e_token++ = '='; /* Flip =+ to += */
600  buf_ptr++;
601  *e_token = 0;
602  }
603 #endif
604  code = binary_op;
605  unary_delim = true;
606  break;
607  /* can drop thru!!! */
608 
609  case '>':
610  case '<':
611  case '!': /* ops like <, <<, <=, !=, etc */
612  if (*buf_ptr == '>' || *buf_ptr == '<' || *buf_ptr == '=') {
613  *e_token++ = *buf_ptr;
614  if (++buf_ptr >= buf_end)
615  fill_buffer();
616  }
617  if (*buf_ptr == '=')
618  *e_token++ = *buf_ptr++;
619  code = (state->last_u_d ? unary_op : binary_op);
620  unary_delim = true;
621  break;
622 
623  case '*':
624  unary_delim = true;
625  if (!state->last_u_d) {
626  if (*buf_ptr == '=')
627  *e_token++ = *buf_ptr++;
628  code = binary_op;
629  break;
630  }
631  while (*buf_ptr == '*' || isspace((unsigned char)*buf_ptr)) {
632  if (*buf_ptr == '*') {
633  CHECK_SIZE_TOKEN(1);
634  *e_token++ = *buf_ptr;
635  }
636  if (++buf_ptr >= buf_end)
637  fill_buffer();
638  }
639  code = unary_op;
640  break;
641 
642  default:
643  if (token[0] == '/' && *buf_ptr == '*') {
644  /* it is start of comment */
645  *e_token++ = '*';
646 
647  if (++buf_ptr >= buf_end)
648  fill_buffer();
649 
650  code = comment;
651  unary_delim = state->last_u_d;
652  break;
653  }
654  while (*(e_token - 1) == *buf_ptr || *buf_ptr == '=') {
655  /*
656  * handle ||, &&, etc, and also things as in int *****i
657  */
658  CHECK_SIZE_TOKEN(1);
659  *e_token++ = *buf_ptr;
660  if (++buf_ptr >= buf_end)
661  fill_buffer();
662  }
663  code = (state->last_u_d ? unary_op : binary_op);
664  unary_delim = true;
665 
666 
667  } /* end of switch */
668  if (buf_ptr >= buf_end) /* check for input buffer empty */
669  fill_buffer();
670  state->last_u_d = unary_delim;
671  CHECK_SIZE_TOKEN(1);
672  *e_token = '\0'; /* null terminate the token */
673  return (code);
674 }
void diag2(int, const char *)
Definition: io.c:590
void fill_buffer(void)
Definition: io.c:346
#define comma
Definition: indent_codes.h:48
#define form_feed
Definition: indent_codes.h:52
#define lparen
Definition: indent_codes.h:36
#define swstmt
Definition: indent_codes.h:50
#define sp_nparen
Definition: indent_codes.h:55
#define rbrace
Definition: indent_codes.h:46
#define colon
Definition: indent_codes.h:43
#define postop
Definition: indent_codes.h:40
#define period
Definition: indent_codes.h:66
#define comment
Definition: indent_codes.h:49
#define question
Definition: indent_codes.h:41
#define lbrace
Definition: indent_codes.h:45
#define semicolon
Definition: indent_codes.h:44
#define ident
Definition: indent_codes.h:47
#define type_def
Definition: indent_codes.h:70
#define preesc
Definition: indent_codes.h:51
#define structure
Definition: indent_codes.h:71
#define funcname
Definition: indent_codes.h:69
#define binary_op
Definition: indent_codes.h:39
#define casestmt
Definition: indent_codes.h:42
#define storage
Definition: indent_codes.h:68
#define newline
Definition: indent_codes.h:35
#define sp_paren
Definition: indent_codes.h:54
#define decl
Definition: indent_codes.h:53
#define unary_op
Definition: indent_codes.h:38
#define strpfx
Definition: indent_codes.h:67
#define rparen
Definition: indent_codes.h:37
int auto_typedefs
#define token
Definition: indent_globs.h:126
#define CHECK_SIZE_TOKEN(desired_size)
Definition: indent_globs.h:99
char * e_token
char * s_token
#define BACKSLASH
Definition: indent_globs.h:35
char chartype[128]
Definition: lexi.c:119
static int strcmp_type(const void *e1, const void *e2)
Definition: lexi.c:142
static int is_func_definition(char *tp)
Definition: lexi.c:160
#define alphanum
Definition: lexi.c:55
struct templ specials[]
Definition: lexi.c:69
const void size_t len
Definition: regguts.h:323
Definition: lexi.c:60
int rwcode
Definition: lexi.c:62

References alphanum, auto_typedefs, BACKSLASH, binary_op, buf_end, buf_ptr, casestmt, chartype, CHECK_SIZE_TOKEN, colon, comma, comment, decl, diag2(), e_token, fill_buffer(), form_feed, funcname, had_eof, ident, is_func_definition(), lbrace, len, line_no, lparen, newline, period, postop, preesc, question, rbrace, rparen, templ::rwcode, s_token, semicolon, sp_nparen, sp_paren, specials, storage, strcmp_type(), strpfx, structure, swstmt, token, type_def, typename_top, typenames, and unary_op.

Referenced by main().

◆ 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 }
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().

◆ parse()

void parse ( int  tk)

Definition at line 49 of file parse.c.

50 {
51  int i;
52 
53 #ifdef debug
54  printf("%2d - %s\n", tk, token);
55 #endif
56 
57  while (ps.p_stack[ps.tos] == ifhead && tk != elselit) {
58  /* true if we have an if without an else */
59  ps.p_stack[ps.tos] = stmt; /* apply the if(..) stmt ::= stmt
60  * reduction */
61  reduce(); /* see if this allows any reduction */
62  }
63 
64 
65  switch (tk) { /* go on and figure out what to do with the
66  * input */
67 
68  case decl: /* scanned a declaration word */
70  /* indicate that following brace should be on same line */
71  if (ps.p_stack[ps.tos] != decl) { /* only put one declaration
72  * onto stack */
73  break_comma = true; /* while in declaration, newline should be
74  * forced after comma */
75  ps.p_stack[++ps.tos] = decl;
76  ps.il[ps.tos] = ps.i_l_follow;
77 
78  if (ps.ljust_decl) {/* only do if we want left justified
79  * declarations */
80  ps.ind_level = 0;
81  for (i = ps.tos - 1; i > 0; --i)
82  if (ps.p_stack[i] == decl)
83  ++ps.ind_level; /* indentation is number of
84  * declaration levels deep we are */
86  }
87  }
88  break;
89 
90  case ifstmt: /* scanned if (...) */
91  if (ps.p_stack[ps.tos] == elsehead && ps.else_if) /* "else if ..." */
92  /*
93  * Note that the stack pointer here is decremented, effectively
94  * reducing "else if" to "if". This saves a lot of stack space
95  * in case of a long "if-else-if ... else-if" sequence.
96  */
97  ps.i_l_follow = ps.il[ps.tos--];
98  /* the rest is the same as for dolit and forstmt */
99  /* FALLTHROUGH */
100  case dolit: /* 'do' */
101  case forstmt: /* for (...) */
102  ps.p_stack[++ps.tos] = tk;
104  ++ps.i_l_follow; /* subsequent statements should be indented 1 */
106  break;
107 
108  case lbrace: /* scanned { */
109  break_comma = false; /* don't break comma in an initial list */
110  if (ps.p_stack[ps.tos] == stmt || ps.p_stack[ps.tos] == decl
111  || ps.p_stack[ps.tos] == stmtl)
112  ++ps.i_l_follow; /* it is a random, isolated stmt group or a
113  * declaration */
114  else {
115  if (s_code == e_code) {
116  /*
117  * only do this if there is nothing on the line
118  */
119  --ps.ind_level;
120  /*
121  * it is a group as part of a while, for, etc.
122  */
123  if (ps.p_stack[ps.tos] == swstmt && ps.case_indent >= 1)
124  --ps.ind_level;
125  /*
126  * for a switch, brace should be two levels out from the code
127  */
128  }
129  }
130 
131  ps.p_stack[++ps.tos] = lbrace;
132  ps.il[ps.tos] = ps.ind_level;
133  ps.p_stack[++ps.tos] = stmt;
134  /* allow null stmt between braces */
135  ps.il[ps.tos] = ps.i_l_follow;
136  break;
137 
138  case whilestmt: /* scanned while (...) */
139  if (ps.p_stack[ps.tos] == dohead) {
140  /* it is matched with do stmt */
142  ps.p_stack[++ps.tos] = whilestmt;
144  }
145  else { /* it is a while loop */
146  ps.p_stack[++ps.tos] = whilestmt;
147  ps.il[ps.tos] = ps.i_l_follow;
148  ++ps.i_l_follow;
150  }
151 
152  break;
153 
154  case elselit: /* scanned an else */
155 
156  if (ps.p_stack[ps.tos] != ifhead)
157  diag2(1, "Unmatched 'else'");
158  else {
159  ps.ind_level = ps.il[ps.tos]; /* indentation for else should
160  * be same as for if */
161  ps.i_l_follow = ps.ind_level + 1; /* everything following should
162  * be in 1 level */
163  ps.p_stack[ps.tos] = elsehead;
164  /* remember if with else */
166  }
167  break;
168 
169  case rbrace: /* scanned a } */
170  /* stack should have <lbrace> <stmt> or <lbrace> <stmtl> */
171  if (ps.tos > 0 && ps.p_stack[ps.tos - 1] == lbrace) {
172  ps.ind_level = ps.i_l_follow = ps.il[--ps.tos];
173  ps.p_stack[ps.tos] = stmt;
174  }
175  else
176  diag2(1, "Statement nesting error");
177  break;
178 
179  case swstmt: /* had switch (...) */
180  ps.p_stack[++ps.tos] = swstmt;
181  ps.cstk[ps.tos] = case_ind;
182  /* save current case indent level */
183  ps.il[ps.tos] = ps.i_l_follow;
184  case_ind = ps.i_l_follow + ps.case_indent; /* cases should be one
185  * level down from
186  * switch */
187  ps.i_l_follow += ps.case_indent + 1; /* statements should be two
188  * levels in */
190  break;
191 
192  case semicolon: /* this indicates a simple stmt */
193  break_comma = false; /* turn off flag to break after commas in a
194  * declaration */
195  ps.p_stack[++ps.tos] = stmt;
196  ps.il[ps.tos] = ps.ind_level;
197  break;
198 
199  default: /* this is an error */
200  diag2(1, "Unknown code to parser");
201  return;
202 
203 
204  } /* end of switch */
205 
206  if (ps.tos >= nitems(ps.p_stack) - 1)
207  errx(1, "Parser stack overflow");
208 
209  reduce(); /* see if any reduction can be done */
210 
211 #ifdef debug
212  for (i = 1; i <= ps.tos; ++i)
213  printf("(%d %d)", ps.p_stack[i], ps.il[i]);
214  printf("\n");
215 #endif
216 
217  return;
218 }
#define nitems(x)
Definition: indent.h:31
#define stmt
Definition: indent_codes.h:59
#define whilestmt
Definition: indent_codes.h:57
#define ifhead
Definition: indent_codes.h:64
#define ifstmt
Definition: indent_codes.h:56
#define forstmt
Definition: indent_codes.h:58
#define dolit
Definition: indent_codes.h:62
#define dohead
Definition: indent_codes.h:63
#define elsehead
Definition: indent_codes.h:65
#define elselit
Definition: indent_codes.h:61
#define stmtl
Definition: indent_codes.h:60
int btype_2
int break_comma
static void reduce(void)
Definition: parse.c:260
#define printf(...)
Definition: port.h:244
float cstk[32]
Definition: indent_globs.h:237
float case_indent
Definition: indent_globs.h:322
int p_stack[256]
Definition: indent_globs.h:235

References break_comma, btype_2, case_ind, parser_state::case_indent, parser_state::cstk, decl, diag2(), dohead, dolit, e_code, parser_state::else_if, elsehead, elselit, errx(), forstmt, i, parser_state::i_l_follow, ifhead, ifstmt, parser_state::il, parser_state::ind_level, lbrace, parser_state::ljust_decl, nitems, parser_state::p_stack, printf, ps, rbrace, reduce(), s_code, parser_state::search_brace, semicolon, stmt, stmtl, swstmt, token, parser_state::tos, and whilestmt.

◆ 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 
164  sc_buf : in_buffer;
166  }
167  else {
168  ps.n_comment_delta = 0;
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;
200  if (!ps.box_com && star_comment_cont)
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 */
210  CHECK_SIZE_COM(3);
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;
216  if (!ps.box_com && star_comment_cont)
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;
235  CHECK_SIZE_COM(4);
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();
242  if (star_comment_cont)
243  *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
244  }
245  dump_line();
246  if (!ps.box_com && star_comment_cont)
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();
285  CHECK_SIZE_COM(4);
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 */
308  now_col = count_spaces_until(ps.com_col, s_com, e_com);
309  do {
310  CHECK_SIZE_COM(1);
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();
327  if (!ps.box_com && star_comment_cont)
328  *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
329  break;
330  }
331  *e_com = '\0';
332  e_com = last_bl;
333  dump_line();
334  if (!ps.box_com && star_comment_cont)
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 dump_line(void)
Definition: io.c:61
int count_spaces(int, char *)
Definition: io.c:550
int star_comment_cont
int else_endif_com_ind
int block_comment_max_col
#define sc_size
Definition: indent_globs.h:37
int format_col1_comments
char sc_buf[sc_size]
#define CHECK_SIZE_COM(desired_size)
Definition: indent_globs.h:70
char * save_com
int blanklines_before_blockcomments
int format_block_comments
int comment_delimiter_on_blankline
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:76
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().

◆ set_defaults()

void set_defaults ( void  )

Definition at line 246 of file args.c.

247 {
248  struct pro *p;
249 
250  /*
251  * Because ps.case_indent is a float, we can't initialize it from the
252  * table:
253  */
254  ps.case_indent = 0.0; /* -cli0.0 */
255  for (p = pro; p->p_name; p++)
256  if (p->p_type != PRO_SPECIAL)
257  *p->p_obj = p->p_default;
258 }
#define PRO_SPECIAL
Definition: args.c:57
Definition: args.c:86
int p_type
Definition: args.c:88
const char * p_name
Definition: args.c:87
int * p_obj
Definition: args.c:91
int p_default
Definition: args.c:89

References parser_state::case_indent, pro::p_default, pro::p_name, pro::p_obj, pro::p_type, PRO_SPECIAL, and ps.

Referenced by main().

◆ set_option()

void set_option ( char *  arg)

Definition at line 261 of file args.c.

262 {
263  struct pro *p;
264  const char *param_start;
265 
266  arg++; /* ignore leading "-" */
267  for (p = pro; p->p_name; p++)
268  if (*p->p_name == *arg && (param_start = eqin(p->p_name, arg)) != NULL)
269  goto found;
270  errx(1, "%s: unknown parameter \"%s\"", option_source, arg - 1);
271 found:
272  switch (p->p_type) {
273 
274  case PRO_SPECIAL:
275  switch (p->p_special) {
276 
277  case IGN:
278  break;
279 
280  case CLI:
281  if (*param_start == 0)
282  goto need_param;
283  ps.case_indent = atof(param_start);
284  break;
285 
286  case STDIN:
287  if (input == NULL)
288  input = stdin;
289  if (output == NULL)
290  output = stdout;
291  break;
292 
293  case KEY:
294  if (*param_start == 0)
295  goto need_param;
296  add_typename(param_start);
297  break;
298 
299  case KEY_FILE:
300  if (*param_start == 0)
301  goto need_param;
302  add_typedefs_from_file(param_start);
303  break;
304 
305  case VERSION:
306  printf("pg_bsd_indent %s (based on FreeBSD indent)\n", INDENT_VERSION);
307  exit(0);
308 
309  default:
310  errx(1, "set_option: internal error: p_special %d", p->p_special);
311  }
312  break;
313 
314  case PRO_BOOL:
315  if (p->p_special == OFF)
316  *p->p_obj = false;
317  else
318  *p->p_obj = true;
319  break;
320 
321  case PRO_INT:
322  if (!isdigit((unsigned char)*param_start)) {
323  need_param:
324  errx(1, "%s: ``%s'' requires a parameter", option_source, p->p_name);
325  }
326  *p->p_obj = atoi(param_start);
327  break;
328 
329  default:
330  errx(1, "set_option: internal error: p_type %d", p->p_type);
331  }
332 }
static const char * eqin(const char *s1, const char *s2)
Definition: args.c:233
#define VERSION
Definition: args.c:74
#define KEY_FILE
Definition: args.c:73
#define OFF
Definition: args.c:63
void add_typedefs_from_file(const char *str)
Definition: args.c:335
#define CLI
Definition: args.c:67
#define KEY
Definition: args.c:69
#define PRO_INT
Definition: args.c:59
#define STDIN
Definition: args.c:68
const char * option_source
Definition: args.c:76
#define IGN
Definition: args.c:66
#define PRO_BOOL
Definition: args.c:58
#define INDENT_VERSION
Definition: args.c:54
void add_typename(const char *)
Definition: lexi.c:687
exit(1)
void * arg
int p_special
Definition: args.c:90

References add_typedefs_from_file(), add_typename(), arg, parser_state::case_indent, CLI, eqin(), errx(), exit(), IGN, INDENT_VERSION, input, KEY, KEY_FILE, OFF, option_source, output, pro::p_name, pro::p_obj, pro::p_special, pro::p_type, printf, PRO_BOOL, PRO_INT, PRO_SPECIAL, ps, STDIN, generate_unaccent_rules::stdout, and VERSION.

Referenced by scan_profile().

◆ set_profile()

void set_profile ( const char *  profile_name)

Definition at line 176 of file args.c.

177 {
178  FILE *f;
179  char fname[MAXPGPATH];
180  static char prof[] = ".indent.pro";
181 
182  if (profile_name == NULL)
183  snprintf(fname, sizeof(fname), "%s/%s", getenv("HOME"), prof);
184  else
185  snprintf(fname, sizeof(fname), "%s", profile_name + 2);
186  if ((f = fopen(option_source = fname, "r")) != NULL) {
187  scan_profile(f);
188  (void) fclose(f);
189  }
190  if ((f = fopen(option_source = prof, "r")) != NULL) {
191  scan_profile(f);
192  (void) fclose(f);
193  }
194  option_source = "Command line";
195 }
static void scan_profile(FILE *f)
Definition: args.c:198
#define MAXPGPATH
#define snprintf
Definition: port.h:238

References MAXPGPATH, option_source, scan_profile(), and snprintf.

Referenced by main().