Merge branch 'vendor/OPENBSD_LIBM'
[dragonfly.git] / usr.bin / indent / indent_globs.h
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1985 Sun Microsystems, Inc.
5  * Copyright (c) 1980, 1993
6  *      The Regents of the University of California.  All rights reserved.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by the University of
20  *      California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *      @(#)indent_globs.h      8.1 (Berkeley) 6/6/93
38  * $FreeBSD: head/usr.bin/indent/indent_globs.h 337651 2018-08-11 19:20:06Z pstef $
39  */
40
41 #define BACKSLASH '\\'
42 #define bufsize 200             /* size of internal buffers */
43 #define sc_size 5000            /* size of save_com buffer */
44 #define label_offset 2          /* number of levels a label is placed to left
45                                  * of code */
46
47
48 #define false 0
49 #define true  1
50
51
52 FILE       *input;              /* the fid for the input file */
53 FILE       *output;             /* the output file */
54
55 #define CHECK_SIZE_CODE(desired_size) \
56         if (e_code + (desired_size) >= l_code) { \
57             int nsize = l_code-s_code + 400 + desired_size; \
58             int code_len = e_code-s_code; \
59             codebuf = (char *) realloc(codebuf, nsize); \
60             if (codebuf == NULL) \
61                 err(1, NULL); \
62             e_code = codebuf + code_len + 1; \
63             l_code = codebuf + nsize - 5; \
64             s_code = codebuf + 1; \
65         }
66 #define CHECK_SIZE_COM(desired_size) \
67         if (e_com + (desired_size) >= l_com) { \
68             int nsize = l_com-s_com + 400 + desired_size; \
69             int com_len = e_com - s_com; \
70             int blank_pos; \
71             if (last_bl != NULL) \
72                 blank_pos = last_bl - combuf; \
73             else \
74                 blank_pos = -1; \
75             combuf = (char *) realloc(combuf, nsize); \
76             if (combuf == NULL) \
77                 err(1, NULL); \
78             e_com = combuf + com_len + 1; \
79             if (blank_pos > 0) \
80                 last_bl = combuf + blank_pos; \
81             l_com = combuf + nsize - 5; \
82             s_com = combuf + 1; \
83         }
84 #define CHECK_SIZE_LAB(desired_size) \
85         if (e_lab + (desired_size) >= l_lab) { \
86             int nsize = l_lab-s_lab + 400 + desired_size; \
87             int label_len = e_lab - s_lab; \
88             labbuf = (char *) realloc(labbuf, nsize); \
89             if (labbuf == NULL) \
90                 err(1, NULL); \
91             e_lab = labbuf + label_len + 1; \
92             l_lab = labbuf + nsize - 5; \
93             s_lab = labbuf + 1; \
94         }
95 #define CHECK_SIZE_TOKEN(desired_size) \
96         if (e_token + (desired_size) >= l_token) { \
97             int nsize = l_token-s_token + 400 + desired_size; \
98             int token_len = e_token - s_token; \
99             tokenbuf = (char *) realloc(tokenbuf, nsize); \
100             if (tokenbuf == NULL) \
101                 err(1, NULL); \
102             e_token = tokenbuf + token_len + 1; \
103             l_token = tokenbuf + nsize - 5; \
104             s_token = tokenbuf + 1; \
105         }
106
107 char       *labbuf;             /* buffer for label */
108 char       *s_lab;              /* start ... */
109 char       *e_lab;              /* .. and end of stored label */
110 char       *l_lab;              /* limit of label buffer */
111
112 char       *codebuf;            /* buffer for code section */
113 char       *s_code;             /* start ... */
114 char       *e_code;             /* .. and end of stored code */
115 char       *l_code;             /* limit of code section */
116
117 char       *combuf;             /* buffer for comments */
118 char       *s_com;              /* start ... */
119 char       *e_com;              /* ... and end of stored comments */
120 char       *l_com;              /* limit of comment buffer */
121
122 #define token s_token
123 char       *tokenbuf;           /* the last token scanned */
124 char       *s_token;
125 char       *e_token;
126 char       *l_token;
127
128 char       *in_buffer;          /* input buffer */
129 char       *in_buffer_limit;    /* the end of the input buffer */
130 char       *buf_ptr;            /* ptr to next character to be taken from
131                                  * in_buffer */
132 char       *buf_end;            /* ptr to first after last char in in_buffer */
133
134 char        sc_buf[sc_size];    /* input text is saved here when looking for
135                                  * the brace after an if, while, etc */
136 char       *save_com;           /* start of the comment stored in sc_buf */
137 char       *sc_end;             /* pointer into save_com buffer */
138
139 char       *bp_save;            /* saved value of buf_ptr when taking input
140                                  * from save_com */
141 char       *be_save;            /* similarly saved value of buf_end */
142
143
144 struct options {
145     int         blanklines_around_conditional_compilation;
146     int         blanklines_after_declarations_at_proctop; /* this is vaguely
147                                  * similar to blanklines_after_decla except
148                                  * that in only applies to the first set of
149                                  * declarations in a procedure (just after
150                                  * the first '{') and it causes a blank line
151                                  * to be generated even if there are no
152                                  * declarations */
153     int         blanklines_after_declarations;
154     int         blanklines_after_procs;
155     int         blanklines_before_blockcomments;
156     int         leave_comma;    /* if true, never break declarations after
157                                  * commas */
158     int         btype_2;        /* when true, brace should be on same line
159                                  * as if, while, etc */
160     int         Bill_Shannon;   /* true iff a blank should always be
161                                  * inserted after sizeof */
162     int         comment_delimiter_on_blankline;
163     int         decl_com_ind;   /* the column in which comments after
164                                  * declarations should be put */
165     int         cuddle_else;    /* true if else should cuddle up to '}' */
166     int         continuation_indent; /* set to the indentation between the
167                                  * edge of code and continuation lines */
168     float       case_indent;    /* The distance to indent case labels from the
169                                  * switch statement */
170     int         com_ind;        /* the column in which comments to the right
171                                  * of code should start */
172     int         decl_indent;    /* column to indent declared identifiers to */
173     int         ljust_decl;     /* true if declarations should be left
174                                  * justified */
175     int         unindent_displace; /* comments not to the right of code
176                                  * will be placed this many
177                                  * indentation levels to the left of
178                                  * code */
179     int         extra_expression_indent; /* true if continuation lines from
180                                  * the expression part of "if(e)",
181                                  * "while(e)", "for(e;e;e)" should be
182                                  * indented an extra tab stop so that they
183                                  * don't conflict with the code that follows */
184     int         else_if;        /* True iff else if pairs should be handled
185                                  * specially */
186     int         function_brace_split; /* split function declaration and
187                                  * brace onto separate lines */
188     int         format_col1_comments; /* If comments which start in column 1
189                                  * are to be magically reformatted (just
190                                  * like comments that begin in later columns) */
191     int         format_block_comments; /* true if comments beginning with
192                                  * `/ * \n' are to be reformatted */
193     int         indent_parameters;
194     int         ind_size;       /* the size of one indentation level */
195     int         block_comment_max_col;
196     int         local_decl_indent; /* like decl_indent but for locals */
197     int         lineup_to_parens_always; /* if true, do not attempt to keep
198                                  * lined-up code within the margin */
199     int         lineup_to_parens; /* if true, continued code within parens
200                                  * will be lined up to the open paren */
201     int         proc_calls_space; /* If true, procedure calls look like:
202                                  * foo (bar) rather than foo(bar) */
203     int         procnames_start_line; /* if true, the names of procedures
204                                  * being defined get placed in column 1 (ie.
205                                  * a newline is placed between the type of
206                                  * the procedure and its name) */
207     int         space_after_cast; /* "b = (int) a" vs "b = (int)a" */
208     int         star_comment_cont; /* true iff comment continuation lines
209                                  * should have stars at the beginning of
210                                  * each line. */
211     int         swallow_optional_blanklines;
212     int         auto_typedefs;  /* set true to recognize identifiers
213                                  * ending in "_t" like typedefs */
214     int         tabsize;        /* the size of a tab */
215     int         max_col;        /* the maximum allowable line length */
216     int         use_tabs;       /* set true to use tabs for spacing, false
217                                  * uses all spaces */
218     int         verbose;        /* when true, non-essential error messages
219                                  * are printed */
220 } opt;
221
222 int         found_err;
223 int         n_real_blanklines;
224 int         prefix_blankline_requested;
225 int         postfix_blankline_requested;
226 int         break_comma;        /* when true and not in parens, break after a
227                                  * comma */
228 float       case_ind;           /* indentation level to be used for a "case
229                                  * n:" */
230 int         code_lines;         /* count of lines with code */
231 int         had_eof;            /* set to true when input is exhausted */
232 int         line_no;            /* the current line number. */
233 int         inhibit_formatting; /* true if INDENT OFF is in effect */
234 int         suppress_blanklines;/* set iff following blanklines should be
235                                  * suppressed */
236
237 #define STACKSIZE 256
238
239 struct parser_state {
240     int         last_token;
241     int         p_stack[STACKSIZE];     /* this is the parsers stack */
242     int         il[STACKSIZE];  /* this stack stores indentation levels */
243     float       cstk[STACKSIZE];/* used to store case stmt indentation levels */
244     int         box_com;        /* set to true when we are in a "boxed"
245                                  * comment. In that case, the first non-blank
246                                  * char should be lined up with the / in / followed by * */
247     int         comment_delta;  /* used to set up indentation for all lines
248                                  * of a boxed comment after the first one */
249     int         n_comment_delta;/* remembers how many columns there were
250                                  * before the start of a box comment so that
251                                  * forthcoming lines of the comment are
252                                  * indented properly */
253     int         cast_mask;      /* indicates which close parens potentially
254                                  * close off casts */
255     int         not_cast_mask;  /* indicates which close parens definitely
256                                  * close off something else than casts */
257     int         block_init;     /* true iff inside a block initialization */
258     int         block_init_level;       /* The level of brace nesting in an
259                                          * initialization */
260     int         last_nl;        /* this is true if the last thing scanned was
261                                  * a newline */
262     int         in_or_st;       /* Will be true iff there has been a
263                                  * declarator (e.g. int or char) and no left
264                                  * paren since the last semicolon. When true,
265                                  * a '{' is starting a structure definition or
266                                  * an initialization list */
267     int         bl_line;        /* set to 1 by dump_line if the line is blank */
268     int         col_1;          /* set to true if the last token started in
269                                  * column 1 */
270     int         com_col;        /* this is the column in which the current
271                                  * comment should start */
272     int         com_lines;      /* the number of lines with comments, set by
273                                  * dump_line */
274     int         dec_nest;       /* current nesting level for structure or init */
275     int         decl_on_line;   /* set to true if this line of code has part
276                                  * of a declaration on it */
277     int         i_l_follow;     /* the level to which ind_level should be set
278                                  * after the current line is printed */
279     int         in_decl;        /* set to true when we are in a declaration
280                                  * stmt.  The processing of braces is then
281                                  * slightly different */
282     int         in_stmt;        /* set to 1 while in a stmt */
283     int         ind_level;      /* the current indentation level */
284     int         ind_stmt;       /* set to 1 if next line should have an extra
285                                  * indentation level because we are in the
286                                  * middle of a stmt */
287     int         last_u_d;       /* set to true after scanning a token which
288                                  * forces a following operator to be unary */
289     int         out_coms;       /* the number of comments processed, set by
290                                  * pr_comment */
291     int         out_lines;      /* the number of lines written, set by
292                                  * dump_line */
293     int         p_l_follow;     /* used to remember how to indent following
294                                  * statement */
295     int         paren_level;    /* parenthesization level. used to indent
296                                  * within statements */
297     short       paren_indents[20];      /* column positions of each paren */
298     int         pcase;          /* set to 1 if the current line label is a
299                                  * case.  It is printed differently from a
300                                  * regular label */
301     int         search_brace;   /* set to true by parse when it is necessary
302                                  * to buffer up all info up to the start of a
303                                  * stmt after an if, while, etc */
304     int         use_ff;         /* set to one if the current line should be
305                                  * terminated with a form feed */
306     int         want_blank;     /* set to true when the following token should
307                                  * be prefixed by a blank. (Said prefixing is
308                                  * ignored in some cases.) */
309     int         keyword;        /* the type of a keyword or 0 */
310     int         dumped_decl_indent;
311     int         in_parameter_declaration;
312     int         tos;            /* pointer to top of stack */
313     char        procname[100];  /* The name of the current procedure */
314     int         just_saw_decl;
315 }           ps;
316
317 int         ifdef_level;
318 struct parser_state state_stack[5];
319 struct parser_state match_state[5];