indent_globs.h revision 131184
11590Srgrimes/*
21590Srgrimes * Copyright (c) 1985 Sun Microsystems, Inc.
31590Srgrimes * Copyright (c) 1980, 1993
41590Srgrimes *	The Regents of the University of California.  All rights reserved.
51590Srgrimes * All rights reserved.
61590Srgrimes *
71590Srgrimes * Redistribution and use in source and binary forms, with or without
81590Srgrimes * modification, are permitted provided that the following conditions
91590Srgrimes * are met:
101590Srgrimes * 1. Redistributions of source code must retain the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer.
121590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
131590Srgrimes *    notice, this list of conditions and the following disclaimer in the
141590Srgrimes *    documentation and/or other materials provided with the distribution.
151590Srgrimes * 3. All advertising materials mentioning features or use of this software
161590Srgrimes *    must display the following acknowledgement:
171590Srgrimes *	This product includes software developed by the University of
181590Srgrimes *	California, Berkeley and its contributors.
191590Srgrimes * 4. Neither the name of the University nor the names of its contributors
201590Srgrimes *    may be used to endorse or promote products derived from this software
211590Srgrimes *    without specific prior written permission.
221590Srgrimes *
231590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
241590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
251590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
261590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
271590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
281590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
291590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
301590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
311590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
321590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
331590Srgrimes * SUCH DAMAGE.
341590Srgrimes *
351590Srgrimes *	@(#)indent_globs.h	8.1 (Berkeley) 6/6/93
3669795Sobrien * $FreeBSD: head/usr.bin/indent/indent_globs.h 131184 2004-06-27 10:58:37Z schweikh $
371590Srgrimes */
381590Srgrimes
391590Srgrimes#define BACKSLASH '\\'
401590Srgrimes#define bufsize 200		/* size of internal buffers */
411590Srgrimes#define sc_size 5000		/* size of save_com buffer */
421590Srgrimes#define label_offset 2		/* number of levels a label is placed to left
431590Srgrimes				 * of code */
441590Srgrimes
451590Srgrimes#define tabsize 8		/* the size of a tab */
461590Srgrimes#define tabmask 0177770		/* mask used when figuring length of lines
471590Srgrimes				 * with tabs */
481590Srgrimes
491590Srgrimes
501590Srgrimes#define false 0
511590Srgrimes#define true  1
521590Srgrimes
531590Srgrimes
541590SrgrimesFILE       *input;		/* the fid for the input file */
551590SrgrimesFILE       *output;		/* the output file */
561590Srgrimes
571590Srgrimes#define CHECK_SIZE_CODE \
581590Srgrimes	if (e_code >= l_code) { \
5998771Sjmallett	    int nsize = l_code-s_code+400; \
601590Srgrimes	    codebuf = (char *) realloc(codebuf, nsize); \
61116390Scharnier	    if (codebuf == NULL) \
62116390Scharnier		err(1, NULL); \
631590Srgrimes	    e_code = codebuf + (e_code-s_code) + 1; \
641590Srgrimes	    l_code = codebuf + nsize - 5; \
651590Srgrimes	    s_code = codebuf + 1; \
661590Srgrimes	}
671590Srgrimes#define CHECK_SIZE_COM \
681590Srgrimes	if (e_com >= l_com) { \
6998771Sjmallett	    int nsize = l_com-s_com+400; \
701590Srgrimes	    combuf = (char *) realloc(combuf, nsize); \
71116390Scharnier	    if (combuf == NULL) \
72116390Scharnier		err(1, NULL); \
731590Srgrimes	    e_com = combuf + (e_com-s_com) + 1; \
741590Srgrimes	    l_com = combuf + nsize - 5; \
751590Srgrimes	    s_com = combuf + 1; \
761590Srgrimes	}
771590Srgrimes#define CHECK_SIZE_LAB \
781590Srgrimes	if (e_lab >= l_lab) { \
7998771Sjmallett	    int nsize = l_lab-s_lab+400; \
801590Srgrimes	    labbuf = (char *) realloc(labbuf, nsize); \
81116390Scharnier	    if (labbuf == NULL) \
82116390Scharnier		err(1, NULL); \
831590Srgrimes	    e_lab = labbuf + (e_lab-s_lab) + 1; \
841590Srgrimes	    l_lab = labbuf + nsize - 5; \
851590Srgrimes	    s_lab = labbuf + 1; \
861590Srgrimes	}
871590Srgrimes#define CHECK_SIZE_TOKEN \
881590Srgrimes	if (e_token >= l_token) { \
8998771Sjmallett	    int nsize = l_token-s_token+400; \
901590Srgrimes	    tokenbuf = (char *) realloc(tokenbuf, nsize); \
91116390Scharnier	    if (tokenbuf == NULL) \
92116390Scharnier		err(1, NULL); \
931590Srgrimes	    e_token = tokenbuf + (e_token-s_token) + 1; \
941590Srgrimes	    l_token = tokenbuf + nsize - 5; \
951590Srgrimes	    s_token = tokenbuf + 1; \
961590Srgrimes	}
971590Srgrimes
981590Srgrimeschar       *labbuf;		/* buffer for label */
991590Srgrimeschar       *s_lab;		/* start ... */
1001590Srgrimeschar       *e_lab;		/* .. and end of stored label */
1011590Srgrimeschar       *l_lab;		/* limit of label buffer */
1021590Srgrimes
1031590Srgrimeschar       *codebuf;		/* buffer for code section */
1041590Srgrimeschar       *s_code;		/* start ... */
1051590Srgrimeschar       *e_code;		/* .. and end of stored code */
1061590Srgrimeschar       *l_code;		/* limit of code section */
1071590Srgrimes
1081590Srgrimeschar       *combuf;		/* buffer for comments */
1091590Srgrimeschar       *s_com;		/* start ... */
1101590Srgrimeschar       *e_com;		/* ... and end of stored comments */
1111590Srgrimeschar       *l_com;		/* limit of comment buffer */
1121590Srgrimes
1131590Srgrimes#define token s_token
1141590Srgrimeschar       *tokenbuf;		/* the last token scanned */
1151590Srgrimeschar	   *s_token;
1161590Srgrimeschar       *e_token;
1171590Srgrimeschar	   *l_token;
1181590Srgrimes
1191590Srgrimeschar       *in_buffer;		/* input buffer */
1201590Srgrimeschar	   *in_buffer_limit;	/* the end of the input buffer */
1211590Srgrimeschar       *buf_ptr;		/* ptr to next character to be taken from
1221590Srgrimes				 * in_buffer */
1231590Srgrimeschar       *buf_end;		/* ptr to first after last char in in_buffer */
1241590Srgrimes
1251590Srgrimeschar        save_com[sc_size];	/* input text is saved here when looking for
1261590Srgrimes				 * the brace after an if, while, etc */
1271590Srgrimeschar       *sc_end;		/* pointer into save_com buffer */
1281590Srgrimes
1291590Srgrimeschar       *bp_save;		/* saved value of buf_ptr when taking input
1301590Srgrimes				 * from save_com */
1311590Srgrimeschar       *be_save;		/* similarly saved value of buf_end */
1321590Srgrimes
1331590Srgrimes
1341590Srgrimesint         pointer_as_binop;
1351590Srgrimesint         blanklines_after_declarations;
1361590Srgrimesint         blanklines_before_blockcomments;
1371590Srgrimesint         blanklines_after_procs;
1381590Srgrimesint         blanklines_around_conditional_compilation;
1391590Srgrimesint         swallow_optional_blanklines;
1401590Srgrimesint         n_real_blanklines;
1411590Srgrimesint         prefix_blankline_requested;
1421590Srgrimesint         postfix_blankline_requested;
1431590Srgrimesint         break_comma;	/* when true and not in parens, break after a
1441590Srgrimes				 * comma */
1451590Srgrimesint         btype_2;		/* when true, brace should be on same line as
1461590Srgrimes				 * if, while, etc */
1471590Srgrimesfloat       case_ind;		/* indentation level to be used for a "case
1481590Srgrimes				 * n:" */
1491590Srgrimesint         code_lines;		/* count of lines with code */
1501590Srgrimesint         had_eof;		/* set to true when input is exhausted */
1511590Srgrimesint         line_no;		/* the current line number. */
1521590Srgrimesint         max_col;		/* the maximum allowable line length */
1531590Srgrimesint         verbose;		/* when true, non-essential error messages are
1541590Srgrimes				 * printed */
1551590Srgrimesint         cuddle_else;	/* true if else should cuddle up to '}' */
1561590Srgrimesint         star_comment_cont;	/* true iff comment continuation lines should
1571590Srgrimes				 * have stars at the beginning of each line. */
1581590Srgrimesint         comment_delimiter_on_blankline;
1591590Srgrimesint         troff;		/* true iff were generating troff input */
1601590Srgrimesint         procnames_start_line;	/* if true, the names of procedures
1611590Srgrimes					 * being defined get placed in column
1621590Srgrimes					 * 1 (ie. a newline is placed between
1631590Srgrimes					 * the type of the procedure and its
1641590Srgrimes					 * name) */
1651590Srgrimesint         proc_calls_space;	/* If true, procedure calls look like:
1661590Srgrimes				 * foo(bar) rather than foo (bar) */
16769795Sobrienint         format_block_comments;	/* true if comments beginning with
16885632Sschweikh					 * `/ * \n' are to be reformatted */
1691590Srgrimesint         format_col1_comments;	/* If comments which start in column 1
1701590Srgrimes					 * are to be magically reformatted
1711590Srgrimes					 * (just like comments that begin in
1721590Srgrimes					 * later columns) */
1731590Srgrimesint         inhibit_formatting;	/* true if INDENT OFF is in effect */
1741590Srgrimesint         suppress_blanklines;/* set iff following blanklines should be
1751590Srgrimes				 * suppressed */
1761590Srgrimesint         continuation_indent;/* set to the indentation between the edge of
1771590Srgrimes				 * code and continuation lines */
1781590Srgrimesint         lineup_to_parens;	/* if true, continued code within parens will
1791590Srgrimes				 * be lined up to the open paren */
1801590Srgrimesint         Bill_Shannon;	/* true iff a blank should always be inserted
1811590Srgrimes				 * after sizeof */
1821590Srgrimesint         blanklines_after_declarations_at_proctop;	/* This is vaguely
1831590Srgrimes							 * similar to
1841590Srgrimes							 * blanklines_after_decla
1851590Srgrimes							 * rations except that
1861590Srgrimes							 * it only applies to
1871590Srgrimes							 * the first set of
1881590Srgrimes							 * declarations in a
1891590Srgrimes							 * procedure (just after
1901590Srgrimes							 * the first '{') and it
1911590Srgrimes							 * causes a blank line
1921590Srgrimes							 * to be generated even
1931590Srgrimes							 * if there are no
1941590Srgrimes							 * declarations */
1951590Srgrimesint         block_comment_max_col;
196131184Sschweikhint         extra_expression_indent;	/* true if continuation lines from the
1971590Srgrimes					 * expression part of "if(e)",
1981590Srgrimes					 * "while(e)", "for(e;e;e)" should be
1991590Srgrimes					 * indented an extra tab stop so that
2001590Srgrimes					 * they don't conflict with the code
2011590Srgrimes					 * that follows */
202131184Sschweikhint	    function_brace_split;	/* split function declaration and
203131184Sschweikh					 * brace onto separate lines */
204131184Sschweikhint	    use_tabs;			/* set true to use tabs for spacing,
205131184Sschweikh					 * false uses all spaces */
2061590Srgrimes
2071590Srgrimes/* -troff font state information */
2081590Srgrimes
2091590Srgrimesstruct fstate {
2101590Srgrimes    char        font[4];
2111590Srgrimes    char        size;
2121590Srgrimes    int         allcaps:1;
2131590Srgrimes};
21493440Sdwmalonechar       *chfont(struct fstate *, struct fstate *, char *);
2151590Srgrimes
2161590Srgrimesstruct fstate
2171590Srgrimes            keywordf,		/* keyword font */
2181590Srgrimes            stringf,		/* string font */
2191590Srgrimes            boxcomf,		/* Box comment font */
2201590Srgrimes            blkcomf,		/* Block comment font */
2211590Srgrimes            scomf,		/* Same line comment font */
2221590Srgrimes            bodyf;		/* major body font */
2231590Srgrimes
2241590Srgrimes
2251590Srgrimes#define STACKSIZE 150
2261590Srgrimes
2271590Srgrimesstruct parser_state {
2281590Srgrimes    int         last_token;
2291590Srgrimes    struct fstate cfont;	/* Current font */
2301590Srgrimes    int         p_stack[STACKSIZE];	/* this is the parsers stack */
2311590Srgrimes    int         il[STACKSIZE];	/* this stack stores indentation levels */
2321590Srgrimes    float       cstk[STACKSIZE];/* used to store case stmt indentation levels */
2331590Srgrimes    int         box_com;	/* set to true when we are in a "boxed"
2341590Srgrimes				 * comment. In that case, the first non-blank
23585632Sschweikh				 * char should be lined up with the / in / followed by * */
2361590Srgrimes    int         comment_delta,
2371590Srgrimes                n_comment_delta;
2381590Srgrimes    int         cast_mask;	/* indicates which close parens close off
2391590Srgrimes				 * casts */
2401590Srgrimes    int         sizeof_mask;	/* indicates which close parens close off
2411590Srgrimes				 * sizeof''s */
2421590Srgrimes    int         block_init;	/* true iff inside a block initialization */
2431590Srgrimes    int         block_init_level;	/* The level of brace nesting in an
2441590Srgrimes					 * initialization */
2451590Srgrimes    int         last_nl;	/* this is true if the last thing scanned was
2461590Srgrimes				 * a newline */
2471590Srgrimes    int         in_or_st;	/* Will be true iff there has been a
2481590Srgrimes				 * declarator (e.g. int or char) and no left
2491590Srgrimes				 * paren since the last semicolon. When true,
2501590Srgrimes				 * a '{' is starting a structure definition or
2511590Srgrimes				 * an initialization list */
2521590Srgrimes    int         bl_line;	/* set to 1 by dump_line if the line is blank */
2531590Srgrimes    int         col_1;		/* set to true if the last token started in
2541590Srgrimes				 * column 1 */
2551590Srgrimes    int         com_col;	/* this is the column in which the current
256105244Scharnier				 * comment should start */
2571590Srgrimes    int         com_ind;	/* the column in which comments to the right
2581590Srgrimes				 * of code should start */
2591590Srgrimes    int         com_lines;	/* the number of lines with comments, set by
2601590Srgrimes				 * dump_line */
2611590Srgrimes    int         dec_nest;	/* current nesting level for structure or init */
2621590Srgrimes    int         decl_com_ind;	/* the column in which comments after
2631590Srgrimes				 * declarations should be put */
2641590Srgrimes    int         decl_on_line;	/* set to true if this line of code has part
2651590Srgrimes				 * of a declaration on it */
2661590Srgrimes    int         i_l_follow;	/* the level to which ind_level should be set
2671590Srgrimes				 * after the current line is printed */
2681590Srgrimes    int         in_decl;	/* set to true when we are in a declaration
2691590Srgrimes				 * stmt.  The processing of braces is then
2701590Srgrimes				 * slightly different */
2711590Srgrimes    int         in_stmt;	/* set to 1 while in a stmt */
2721590Srgrimes    int         ind_level;	/* the current indentation level */
2731590Srgrimes    int         ind_size;	/* the size of one indentation level */
2741590Srgrimes    int         ind_stmt;	/* set to 1 if next line should have an extra
2751590Srgrimes				 * indentation level because we are in the
2761590Srgrimes				 * middle of a stmt */
2771590Srgrimes    int         last_u_d;	/* set to true after scanning a token which
2781590Srgrimes				 * forces a following operator to be unary */
2791590Srgrimes    int         leave_comma;	/* if true, never break declarations after
2801590Srgrimes				 * commas */
2811590Srgrimes    int         ljust_decl;	/* true if declarations should be left
2821590Srgrimes				 * justified */
2831590Srgrimes    int         out_coms;	/* the number of comments processed, set by
2841590Srgrimes				 * pr_comment */
2851590Srgrimes    int         out_lines;	/* the number of lines written, set by
2861590Srgrimes				 * dump_line */
2871590Srgrimes    int         p_l_follow;	/* used to remember how to indent following
2881590Srgrimes				 * statement */
2891590Srgrimes    int         paren_level;	/* parenthesization level. used to indent
290105244Scharnier				 * within statements */
2911590Srgrimes    short       paren_indents[20];	/* column positions of each paren */
2921590Srgrimes    int         pcase;		/* set to 1 if the current line label is a
2931590Srgrimes				 * case.  It is printed differently from a
2941590Srgrimes				 * regular label */
2951590Srgrimes    int         search_brace;	/* set to true by parse when it is necessary
2961590Srgrimes				 * to buffer up all info up to the start of a
2971590Srgrimes				 * stmt after an if, while, etc */
2981590Srgrimes    int         unindent_displace;	/* comments not to the right of code
2991590Srgrimes					 * will be placed this many
3001590Srgrimes					 * indentation levels to the left of
3011590Srgrimes					 * code */
3021590Srgrimes    int         use_ff;		/* set to one if the current line should be
3031590Srgrimes				 * terminated with a form feed */
3041590Srgrimes    int         want_blank;	/* set to true when the following token should
3051590Srgrimes				 * be prefixed by a blank. (Said prefixing is
3061590Srgrimes				 * ignored in some cases.) */
3071590Srgrimes    int         else_if;	/* True iff else if pairs should be handled
3081590Srgrimes				 * specially */
3091590Srgrimes    int         decl_indent;	/* column to indent declared identifiers to */
310125633Sbde    int         local_decl_indent;	/* like decl_indent but for locals */
3111590Srgrimes    int         its_a_keyword;
3121590Srgrimes    int         sizeof_keyword;
3131590Srgrimes    int         dumped_decl_indent;
3141590Srgrimes    float       case_indent;	/* The distance to indent case labels from the
3151590Srgrimes				 * switch statement */
3161590Srgrimes    int         in_parameter_declaration;
3171590Srgrimes    int         indent_parameters;
3181590Srgrimes    int         tos;		/* pointer to top of stack */
3191590Srgrimes    char        procname[100];	/* The name of the current procedure */
3201590Srgrimes    int         just_saw_decl;
3211590Srgrimes}           ps;
3221590Srgrimes
3231590Srgrimesint         ifdef_level;
3241590Srgrimesint	    rparen_count;
3251590Srgrimesstruct parser_state state_stack[5];
3261590Srgrimesstruct parser_state match_state[5];
327