Home | History | Annotate | Line # | Download | only in indent
indent.h revision 1.32
      1  1.32  rillig /*	$NetBSD: indent.h,v 1.32 2021/10/08 16:20:33 rillig Exp $	*/
      2   1.1   kamil 
      3   1.1   kamil /*-
      4   1.1   kamil  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
      5   1.1   kamil  *
      6   1.1   kamil  * Copyright (c) 2001 Jens Schweikhardt
      7   1.1   kamil  * All rights reserved.
      8   1.1   kamil  *
      9   1.1   kamil  * Redistribution and use in source and binary forms, with or without
     10   1.1   kamil  * modification, are permitted provided that the following conditions
     11   1.1   kamil  * are met:
     12   1.1   kamil  * 1. Redistributions of source code must retain the above copyright
     13   1.1   kamil  *    notice, this list of conditions and the following disclaimer.
     14   1.1   kamil  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1   kamil  *    notice, this list of conditions and the following disclaimer in the
     16   1.1   kamil  *    documentation and/or other materials provided with the distribution.
     17   1.1   kamil  *
     18   1.1   kamil  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
     19   1.1   kamil  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20   1.1   kamil  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21   1.1   kamil  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
     22   1.1   kamil  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23   1.1   kamil  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24   1.1   kamil  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25   1.1   kamil  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26   1.1   kamil  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27   1.1   kamil  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28   1.1   kamil  * SUCH DAMAGE.
     29   1.1   kamil  */
     30  1.31  rillig /*-
     31  1.31  rillig  * SPDX-License-Identifier: BSD-4-Clause
     32  1.31  rillig  *
     33  1.31  rillig  * Copyright (c) 1985 Sun Microsystems, Inc.
     34  1.31  rillig  * Copyright (c) 1980, 1993
     35  1.31  rillig  *	The Regents of the University of California.  All rights reserved.
     36  1.31  rillig  * All rights reserved.
     37  1.31  rillig  *
     38  1.31  rillig  * Redistribution and use in source and binary forms, with or without
     39  1.31  rillig  * modification, are permitted provided that the following conditions
     40  1.31  rillig  * are met:
     41  1.31  rillig  * 1. Redistributions of source code must retain the above copyright
     42  1.31  rillig  *    notice, this list of conditions and the following disclaimer.
     43  1.31  rillig  * 2. Redistributions in binary form must reproduce the above copyright
     44  1.31  rillig  *    notice, this list of conditions and the following disclaimer in the
     45  1.31  rillig  *    documentation and/or other materials provided with the distribution.
     46  1.31  rillig  * 3. All advertising materials mentioning features or use of this software
     47  1.31  rillig  *    must display the following acknowledgement:
     48  1.31  rillig  *	This product includes software developed by the University of
     49  1.31  rillig  *	California, Berkeley and its contributors.
     50  1.31  rillig  * 4. Neither the name of the University nor the names of its contributors
     51  1.31  rillig  *    may be used to endorse or promote products derived from this software
     52  1.31  rillig  *    without specific prior written permission.
     53  1.31  rillig  *
     54  1.31  rillig  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55  1.31  rillig  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56  1.31  rillig  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57  1.31  rillig  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58  1.31  rillig  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59  1.31  rillig  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60  1.31  rillig  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61  1.31  rillig  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62  1.31  rillig  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63  1.31  rillig  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64  1.31  rillig  * SUCH DAMAGE.
     65  1.31  rillig  */
     66   1.1   kamil 
     67   1.1   kamil #if 0
     68   1.1   kamil __FBSDID("$FreeBSD: head/usr.bin/indent/indent.h 336333 2018-07-16 05:46:50Z pstef $");
     69   1.1   kamil #endif
     70   1.1   kamil 
     71  1.20  rillig #include <stdbool.h>
     72  1.20  rillig 
     73  1.31  rillig typedef enum token_type {
     74  1.31  rillig     end_of_file,
     75  1.31  rillig     newline,
     76  1.32  rillig     lparen_or_lbracket,
     77  1.32  rillig     rparen_or_rbracket,
     78  1.31  rillig     unary_op,			/* e.g. '+' or '&' */
     79  1.31  rillig     binary_op,			/* e.g. '<<' or '+' or '&&' or '/=' */
     80  1.31  rillig     postfix_op,			/* trailing '++' or '--' */
     81  1.31  rillig     question,			/* the '?' from a '?:' expression */
     82  1.31  rillig     case_label,
     83  1.31  rillig     colon,
     84  1.31  rillig     semicolon,
     85  1.31  rillig     lbrace,
     86  1.31  rillig     rbrace,
     87  1.31  rillig     ident,			/* identifier, constant or string */
     88  1.31  rillig     comma,
     89  1.31  rillig     comment,
     90  1.31  rillig     switch_expr,		/* 'switch' '(' <expr> ')' */
     91  1.31  rillig     preprocessing,		/* '#' */
     92  1.31  rillig     form_feed,
     93  1.31  rillig     decl,
     94  1.31  rillig     keyword_for_if_while,	/* 'for', 'if' or 'while' */
     95  1.31  rillig     keyword_do_else,		/* 'do' or 'else' */
     96  1.31  rillig     if_expr,			/* 'if' '(' <expr> ')' */
     97  1.31  rillig     while_expr,			/* 'while' '(' <expr> ')' */
     98  1.31  rillig     for_exprs,			/* 'for' '(' ... ')' */
     99  1.31  rillig     stmt,
    100  1.31  rillig     stmt_list,
    101  1.31  rillig     keyword_else,		/* 'else' */
    102  1.31  rillig     keyword_do,			/* 'do' */
    103  1.31  rillig     do_stmt,			/* 'do' <stmt> */
    104  1.31  rillig     if_expr_stmt,		/* 'if' '(' <expr> ')' <stmt> */
    105  1.31  rillig     if_expr_stmt_else,		/* 'if' '(' <expr> ')' <stmt> 'else' */
    106  1.31  rillig     period,
    107  1.31  rillig     string_prefix,		/* 'L' */
    108  1.31  rillig     storage_class,
    109  1.31  rillig     funcname,
    110  1.31  rillig     type_def,
    111  1.31  rillig     keyword_struct_union_enum
    112  1.31  rillig } token_type;
    113  1.31  rillig 
    114  1.31  rillig #define sc_size 5000		/* size of save_com buffer */
    115  1.31  rillig #define label_offset 2		/* number of levels a label is placed to left
    116  1.31  rillig 				 * of code */
    117  1.31  rillig 
    118  1.31  rillig 
    119  1.31  rillig struct buffer {
    120  1.31  rillig     char *buf;			/* buffer */
    121  1.31  rillig     char *s;			/* start */
    122  1.31  rillig     char *e;			/* end */
    123  1.31  rillig     char *l;			/* limit */
    124  1.31  rillig };
    125  1.31  rillig 
    126  1.31  rillig extern FILE       *input;		/* the fid for the input file */
    127  1.31  rillig extern FILE       *output;		/* the output file */
    128  1.31  rillig 
    129  1.31  rillig extern struct buffer lab;		/* label or preprocessor directive */
    130  1.31  rillig extern struct buffer code;		/* code */
    131  1.31  rillig extern struct buffer com;		/* comment */
    132  1.31  rillig extern struct buffer token;		/* the last token scanned */
    133  1.31  rillig 
    134  1.31  rillig extern struct buffer inp;
    135  1.31  rillig 
    136  1.31  rillig extern char        sc_buf[sc_size];	/* input text is saved here when looking for
    137  1.31  rillig 				 * the brace after an if, while, etc */
    138  1.31  rillig extern char       *save_com;	/* start of the comment stored in sc_buf */
    139  1.31  rillig 
    140  1.31  rillig extern char       *saved_inp_s;	/* saved value of inp.s when taking input
    141  1.31  rillig 				 * from save_com */
    142  1.31  rillig extern char       *saved_inp_e;	/* similarly saved value of inp.e */
    143  1.31  rillig 
    144  1.31  rillig 
    145  1.31  rillig extern struct options {
    146  1.31  rillig     bool	blanklines_around_conditional_compilation;
    147  1.31  rillig     bool	blanklines_after_declarations_at_proctop; /* this is vaguely
    148  1.31  rillig 				 * similar to blanklines_after_declarations
    149  1.31  rillig 				 * except that it only applies to the first
    150  1.31  rillig 				 * set of declarations in a procedure (just
    151  1.31  rillig 				 * after the first '{') and it causes a blank
    152  1.31  rillig 				 * line to be generated even if there are no
    153  1.31  rillig 				 * declarations */
    154  1.31  rillig     bool	blanklines_after_declarations;
    155  1.31  rillig     bool	blanklines_after_procs;
    156  1.31  rillig     bool	blanklines_before_blockcomments;
    157  1.31  rillig     bool	break_after_comma; /* whether to break declarations after
    158  1.31  rillig 				 * commas */
    159  1.31  rillig     bool	brace_same_line;/* whether brace should be on same line
    160  1.31  rillig 				 * as if, while, etc */
    161  1.31  rillig     bool	blank_after_sizeof; /* whether a blank should always be
    162  1.31  rillig 				 * inserted after sizeof */
    163  1.31  rillig     bool	comment_delimiter_on_blankline;
    164  1.31  rillig     int         decl_comment_column; /* the column in which comments after
    165  1.31  rillig 				 * declarations should be put */
    166  1.31  rillig     bool	cuddle_else;	/* whether 'else' should cuddle up to '}' */
    167  1.31  rillig     int         continuation_indent; /* the indentation between the
    168  1.31  rillig 				 * edge of code and continuation lines */
    169  1.31  rillig     float       case_indent;	/* The distance (measured in indentation
    170  1.31  rillig 				 * levels) to indent case labels from the
    171  1.31  rillig 				 * switch statement */
    172  1.31  rillig     int         comment_column;	/* the column in which comments to the right
    173  1.31  rillig 				 * of code should start */
    174  1.31  rillig     int         decl_indent;	/* indentation of identifier in declaration */
    175  1.31  rillig     bool	ljust_decl;	/* true if declarations should be left
    176  1.31  rillig 				 * justified */
    177  1.31  rillig     int         unindent_displace; /* comments not to the right of code
    178  1.31  rillig 				 * will be placed this many
    179  1.31  rillig 				 * indentation levels to the left of
    180  1.31  rillig 				 * code */
    181  1.31  rillig     bool	extra_expression_indent; /* whether continuation lines from
    182  1.31  rillig 				 * the expression part of "if(e)",
    183  1.31  rillig 				 * "while(e)", "for(e;e;e)" should be
    184  1.31  rillig 				 * indented an extra tab stop so that they
    185  1.31  rillig 				 * don't conflict with the code that follows */
    186  1.31  rillig     bool	else_if;	/* whether else-if pairs should be handled
    187  1.31  rillig 				 * specially */
    188  1.31  rillig     bool	function_brace_split; /* split function declaration and
    189  1.31  rillig 				 * brace onto separate lines */
    190  1.31  rillig     bool	format_col1_comments; /* If comments which start in column 1
    191  1.31  rillig 				 * are to be magically reformatted (just
    192  1.31  rillig 				 * like comments that begin in later columns) */
    193  1.31  rillig     bool	format_block_comments; /* whether comments beginning with
    194  1.31  rillig 				 * '/ * \n' are to be reformatted */
    195  1.31  rillig     bool	indent_parameters;
    196  1.31  rillig     int         indent_size;	/* the size of one indentation level */
    197  1.31  rillig     int         block_comment_max_line_length;
    198  1.31  rillig     int         local_decl_indent; /* like decl_indent but for locals */
    199  1.31  rillig     bool	lineup_to_parens_always; /* whether to not(?) attempt to keep
    200  1.31  rillig 				 * lined-up code within the margin */
    201  1.31  rillig     bool	lineup_to_parens; /* whether continued code within parens
    202  1.31  rillig 				 * will be lined up to the open paren */
    203  1.31  rillig     bool	proc_calls_space; /* whether procedure calls look like:
    204  1.31  rillig 				 * foo (bar) rather than foo(bar) */
    205  1.31  rillig     bool	procnames_start_line; /* whether the names of procedures
    206  1.31  rillig 				 * being defined get placed in column 1 (i.e.
    207  1.31  rillig 				 * a newline is placed between the type of
    208  1.31  rillig 				 * the procedure and its name) */
    209  1.31  rillig     bool	space_after_cast; /* "b = (int) a" vs "b = (int)a" */
    210  1.31  rillig     bool	star_comment_cont; /* whether comment continuation lines
    211  1.31  rillig 				 * should have stars at the beginning of
    212  1.31  rillig 				 * each line. */
    213  1.31  rillig     bool	swallow_optional_blanklines;
    214  1.31  rillig     bool	auto_typedefs;	/* whether to recognize identifiers
    215  1.31  rillig 				 * ending in "_t" like typedefs */
    216  1.31  rillig     int         tabsize;	/* the size of a tab */
    217  1.31  rillig     int         max_line_length;
    218  1.31  rillig     bool	use_tabs;	/* set true to use tabs for spacing, false
    219  1.31  rillig 				 * uses all spaces */
    220  1.31  rillig     bool	verbose;	/* whether non-essential error messages
    221  1.31  rillig 				 * are printed */
    222  1.31  rillig } opt;
    223  1.31  rillig 
    224  1.31  rillig enum keyword_kind {
    225  1.31  rillig     kw_0,
    226  1.31  rillig     kw_offsetof,
    227  1.31  rillig     kw_sizeof,
    228  1.31  rillig     kw_struct_or_union_or_enum,
    229  1.31  rillig     kw_type,
    230  1.31  rillig     kw_for_or_if_or_while,
    231  1.31  rillig     kw_do_or_else,
    232  1.31  rillig     kw_switch,
    233  1.31  rillig     kw_case_or_default,
    234  1.31  rillig     kw_jump,
    235  1.31  rillig     kw_storage_class,
    236  1.31  rillig     kw_typedef,
    237  1.31  rillig     kw_inline_or_restrict
    238  1.31  rillig };
    239  1.31  rillig 
    240  1.31  rillig 
    241  1.31  rillig extern bool        found_err;
    242  1.31  rillig extern int         next_blank_lines;
    243  1.31  rillig extern bool        prefix_blankline_requested;
    244  1.31  rillig extern bool        postfix_blankline_requested;
    245  1.31  rillig extern bool        break_comma;	/* when true and not in parens, break after a
    246  1.31  rillig 				 * comma */
    247  1.31  rillig extern float       case_ind;	/* indentation level to be used for a "case
    248  1.31  rillig 				 * n:" */
    249  1.31  rillig extern bool        had_eof;		/* whether input is exhausted */
    250  1.31  rillig extern int         line_no;		/* the current line number. */
    251  1.31  rillig extern bool        inhibit_formatting;	/* true if INDENT OFF is in effect */
    252  1.31  rillig 
    253  1.31  rillig #define	STACKSIZE 256
    254  1.31  rillig 
    255  1.31  rillig extern struct parser_state {
    256  1.31  rillig     token_type  last_token;
    257  1.31  rillig     token_type	p_stack[STACKSIZE];	/* this is the parser's stack */
    258  1.31  rillig     int         il[STACKSIZE];	/* this stack stores indentation levels */
    259  1.31  rillig     float       cstk[STACKSIZE];/* used to store case stmt indentation levels */
    260  1.31  rillig     bool	box_com;	/* whether we are in a "boxed" comment. In
    261  1.31  rillig 				 * that case, the first non-blank char should
    262  1.31  rillig 				 * be lined up with the '/' in '/' + '*' */
    263  1.31  rillig     int         comment_delta;	/* used to set up indentation for all lines
    264  1.31  rillig 				 * of a boxed comment after the first one */
    265  1.31  rillig     int         n_comment_delta;/* remembers how many columns there were
    266  1.31  rillig 				 * before the start of a box comment so that
    267  1.31  rillig 				 * forthcoming lines of the comment are
    268  1.31  rillig 				 * indented properly */
    269  1.31  rillig     int         cast_mask;	/* indicates which close parens potentially
    270  1.31  rillig 				 * close off casts */
    271  1.31  rillig     int         not_cast_mask;	/* indicates which close parens definitely
    272  1.31  rillig 				 * close off something else than casts */
    273  1.31  rillig     bool	block_init;	/* whether inside a block initialization */
    274  1.31  rillig     int         block_init_level; /* The level of brace nesting in an
    275  1.31  rillig 				 * initialization */
    276  1.31  rillig     bool	last_nl;	/* whether the last thing scanned was
    277  1.31  rillig 				 * a newline */
    278  1.31  rillig     bool	in_or_st;	/* true iff there has been a
    279  1.31  rillig 				 * declarator (e.g. int or char) and no left
    280  1.31  rillig 				 * paren since the last semicolon. When true,
    281  1.31  rillig 				 * a '{' is starting a structure definition or
    282  1.31  rillig 				 * an initialization list */
    283  1.31  rillig     bool	col_1;		/* whether the last token started in
    284  1.31  rillig 				 * column 1 */
    285  1.31  rillig     int         com_col;	/* this is the column in which the current
    286  1.31  rillig 				 * comment should start */
    287  1.31  rillig     int         decl_nest;	/* current nesting level for structure or init */
    288  1.31  rillig     bool	decl_on_line;	/* whether this line of code has part
    289  1.31  rillig 				 * of a declaration on it */
    290  1.31  rillig     int         ind_level_follow; /* the level to which ind_level should be set
    291  1.31  rillig 				 * after the current line is printed */
    292  1.31  rillig     bool	in_decl;	/* whether we are in a declaration stmt.
    293  1.31  rillig 				 * The processing of braces is then slightly
    294  1.31  rillig 				 * different */
    295  1.31  rillig     bool	in_stmt;
    296  1.31  rillig     int         ind_level;	/* the current indentation level */
    297  1.31  rillig     bool	ind_stmt;	/* whether the next line should have an extra
    298  1.31  rillig 				 * indentation level because we are in the
    299  1.31  rillig 				 * middle of a stmt */
    300  1.31  rillig     bool	last_u_d;	/* whether the following operator should be
    301  1.31  rillig 				 * unary */
    302  1.31  rillig     int         p_l_follow;	/* used to remember how to indent the
    303  1.31  rillig 				 * following statement */
    304  1.31  rillig     int         paren_level;	/* parenthesization level. used to indent
    305  1.31  rillig 				 * within statements */
    306  1.31  rillig     short       paren_indents[20]; /* indentation of the operand/argument of
    307  1.31  rillig 				 * each level of parentheses or brackets,
    308  1.31  rillig 				 * relative to the enclosing statement */
    309  1.31  rillig     bool	is_case_label;	/* 'case' and 'default' labels are indented
    310  1.31  rillig 				 * differently from regular labels */
    311  1.31  rillig     bool	search_brace;	/* whether it is necessary
    312  1.31  rillig 				 * to buffer up all info up to the start of a
    313  1.31  rillig 				 * stmt after an if, while, etc */
    314  1.31  rillig     bool	use_ff;		/* whether the current line should be
    315  1.31  rillig 				 * terminated with a form feed */
    316  1.31  rillig     bool	want_blank;	/* whether the following token should
    317  1.31  rillig 				 * be prefixed by a blank. (Said prefixing is
    318  1.31  rillig 				 * ignored in some cases.) */
    319  1.31  rillig     enum keyword_kind keyword;
    320  1.31  rillig     bool	dumped_decl_indent;
    321  1.31  rillig     bool	in_parameter_declaration;
    322  1.31  rillig     int         tos;		/* pointer to top of stack */
    323  1.31  rillig     char        procname[100];	/* The name of the current procedure */
    324  1.31  rillig     int         just_saw_decl;
    325  1.31  rillig 
    326  1.31  rillig     struct {
    327  1.31  rillig 	int	comments;
    328  1.31  rillig 	int	lines;
    329  1.31  rillig 	int	code_lines;
    330  1.31  rillig 	int	comment_lines;
    331  1.31  rillig     }		stats;
    332  1.31  rillig }           ps;
    333  1.31  rillig 
    334   1.3  rillig 
    335   1.1   kamil #ifndef nitems
    336   1.1   kamil #define nitems(array) (sizeof (array) / sizeof (array[0]))
    337   1.1   kamil #endif
    338   1.1   kamil 
    339  1.16  rillig void		add_typename(const char *);
    340  1.16  rillig int		compute_code_indent(void);
    341  1.16  rillig int		compute_label_indent(void);
    342  1.16  rillig int		indentation_after_range(int, const char *, const char *);
    343  1.16  rillig int		indentation_after(int, const char *);
    344   1.5  rillig #ifdef debug
    345  1.16  rillig void		debug_vis_range(const char *, const char *, const char *,
    346  1.16  rillig 		    const char *);
    347  1.16  rillig void		debug_printf(const char *, ...) __printflike(1, 2);
    348  1.16  rillig void		debug_println(const char *, ...) __printflike(1, 2);
    349  1.16  rillig const char *	token_type_name(token_type);
    350   1.9  rillig #else
    351  1.16  rillig #define		debug_printf(fmt, ...) do { } while (false)
    352  1.16  rillig #define		debug_println(fmt, ...) do { } while (false)
    353  1.16  rillig #define		debug_vis_range(prefix, s, e, suffix) do { } while (false)
    354   1.5  rillig #endif
    355  1.28  rillig void		inbuf_skip(void);
    356  1.28  rillig char		inbuf_next(void);
    357  1.16  rillig token_type	lexi(struct parser_state *);
    358  1.16  rillig void		diag(int, const char *, ...) __printflike(2, 3);
    359  1.16  rillig void		dump_line(void);
    360  1.16  rillig void		fill_buffer(void);
    361  1.16  rillig void		parse(token_type);
    362  1.16  rillig void		process_comment(void);
    363  1.30  rillig void		set_option(const char *, const char *);
    364  1.27  rillig void		load_profiles(const char *);
    365  1.17  rillig 
    366  1.17  rillig void		*xmalloc(size_t);
    367  1.17  rillig void		*xrealloc(void *, size_t);
    368  1.17  rillig char		*xstrdup(const char *);
    369  1.25  rillig 
    370  1.25  rillig void		buf_expand(struct buffer *, size_t);
    371  1.29  rillig 
    372  1.29  rillig static inline bool
    373  1.29  rillig is_hspace(char ch)
    374  1.29  rillig {
    375  1.29  rillig     return ch == ' ' || ch == '\t';
    376  1.29  rillig }
    377