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$
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
134152395Sdwmaloneint         found_err;
1351590Srgrimesint         pointer_as_binop;
1361590Srgrimesint         blanklines_after_declarations;
1371590Srgrimesint         blanklines_before_blockcomments;
1381590Srgrimesint         blanklines_after_procs;
1391590Srgrimesint         blanklines_around_conditional_compilation;
1401590Srgrimesint         swallow_optional_blanklines;
1411590Srgrimesint         n_real_blanklines;
1421590Srgrimesint         prefix_blankline_requested;
1431590Srgrimesint         postfix_blankline_requested;
1441590Srgrimesint         break_comma;	/* when true and not in parens, break after a
1451590Srgrimes				 * comma */
1461590Srgrimesint         btype_2;		/* when true, brace should be on same line as
1471590Srgrimes				 * if, while, etc */
1481590Srgrimesfloat       case_ind;		/* indentation level to be used for a "case
1491590Srgrimes				 * n:" */
1501590Srgrimesint         code_lines;		/* count of lines with code */
1511590Srgrimesint         had_eof;		/* set to true when input is exhausted */
1521590Srgrimesint         line_no;		/* the current line number. */
1531590Srgrimesint         max_col;		/* the maximum allowable line length */
1541590Srgrimesint         verbose;		/* when true, non-essential error messages are
1551590Srgrimes				 * printed */
1561590Srgrimesint         cuddle_else;	/* true if else should cuddle up to '}' */
1571590Srgrimesint         star_comment_cont;	/* true iff comment continuation lines should
1581590Srgrimes				 * have stars at the beginning of each line. */
1591590Srgrimesint         comment_delimiter_on_blankline;
1601590Srgrimesint         troff;		/* true iff were generating troff input */
1611590Srgrimesint         procnames_start_line;	/* if true, the names of procedures
1621590Srgrimes					 * being defined get placed in column
1631590Srgrimes					 * 1 (ie. a newline is placed between
1641590Srgrimes					 * the type of the procedure and its
1651590Srgrimes					 * name) */
1661590Srgrimesint         proc_calls_space;	/* If true, procedure calls look like:
1671590Srgrimes				 * foo(bar) rather than foo (bar) */
16869795Sobrienint         format_block_comments;	/* true if comments beginning with
16985632Sschweikh					 * `/ * \n' are to be reformatted */
1701590Srgrimesint         format_col1_comments;	/* If comments which start in column 1
1711590Srgrimes					 * are to be magically reformatted
1721590Srgrimes					 * (just like comments that begin in
1731590Srgrimes					 * later columns) */
1741590Srgrimesint         inhibit_formatting;	/* true if INDENT OFF is in effect */
1751590Srgrimesint         suppress_blanklines;/* set iff following blanklines should be
1761590Srgrimes				 * suppressed */
1771590Srgrimesint         continuation_indent;/* set to the indentation between the edge of
1781590Srgrimes				 * code and continuation lines */
1791590Srgrimesint         lineup_to_parens;	/* if true, continued code within parens will
1801590Srgrimes				 * be lined up to the open paren */
1811590Srgrimesint         Bill_Shannon;	/* true iff a blank should always be inserted
1821590Srgrimes				 * after sizeof */
1831590Srgrimesint         blanklines_after_declarations_at_proctop;	/* This is vaguely
1841590Srgrimes							 * similar to
1851590Srgrimes							 * blanklines_after_decla
1861590Srgrimes							 * rations except that
1871590Srgrimes							 * it only applies to
1881590Srgrimes							 * the first set of
1891590Srgrimes							 * declarations in a
1901590Srgrimes							 * procedure (just after
1911590Srgrimes							 * the first '{') and it
1921590Srgrimes							 * causes a blank line
1931590Srgrimes							 * to be generated even
1941590Srgrimes							 * if there are no
1951590Srgrimes							 * declarations */
1961590Srgrimesint         block_comment_max_col;
197131184Sschweikhint         extra_expression_indent;	/* true if continuation lines from the
1981590Srgrimes					 * expression part of "if(e)",
1991590Srgrimes					 * "while(e)", "for(e;e;e)" should be
2001590Srgrimes					 * indented an extra tab stop so that
2011590Srgrimes					 * they don't conflict with the code
2021590Srgrimes					 * that follows */
203131184Sschweikhint	    function_brace_split;	/* split function declaration and
204131184Sschweikh					 * brace onto separate lines */
205131184Sschweikhint	    use_tabs;			/* set true to use tabs for spacing,
206131184Sschweikh					 * false uses all spaces */
207205989Savgint	    auto_typedefs;		/* set true to recognize identifiers
208205989Savg					 * ending in "_t" like typedefs */
2091590Srgrimes
2101590Srgrimes/* -troff font state information */
2111590Srgrimes
2121590Srgrimesstruct fstate {
2131590Srgrimes    char        font[4];
2141590Srgrimes    char        size;
2151590Srgrimes    int         allcaps:1;
216244578Sandrew} __aligned(sizeof(int));
21793440Sdwmalonechar       *chfont(struct fstate *, struct fstate *, char *);
2181590Srgrimes
2191590Srgrimesstruct fstate
2201590Srgrimes            keywordf,		/* keyword font */
2211590Srgrimes            stringf,		/* string font */
2221590Srgrimes            boxcomf,		/* Box comment font */
2231590Srgrimes            blkcomf,		/* Block comment font */
2241590Srgrimes            scomf,		/* Same line comment font */
2251590Srgrimes            bodyf;		/* major body font */
2261590Srgrimes
2271590Srgrimes
2281590Srgrimes#define STACKSIZE 150
2291590Srgrimes
2301590Srgrimesstruct parser_state {
2311590Srgrimes    int         last_token;
2321590Srgrimes    struct fstate cfont;	/* Current font */
2331590Srgrimes    int         p_stack[STACKSIZE];	/* this is the parsers stack */
2341590Srgrimes    int         il[STACKSIZE];	/* this stack stores indentation levels */
2351590Srgrimes    float       cstk[STACKSIZE];/* used to store case stmt indentation levels */
2361590Srgrimes    int         box_com;	/* set to true when we are in a "boxed"
2371590Srgrimes				 * comment. In that case, the first non-blank
23885632Sschweikh				 * char should be lined up with the / in / followed by * */
2391590Srgrimes    int         comment_delta,
2401590Srgrimes                n_comment_delta;
2411590Srgrimes    int         cast_mask;	/* indicates which close parens close off
2421590Srgrimes				 * casts */
2431590Srgrimes    int         sizeof_mask;	/* indicates which close parens close off
2441590Srgrimes				 * sizeof''s */
2451590Srgrimes    int         block_init;	/* true iff inside a block initialization */
2461590Srgrimes    int         block_init_level;	/* The level of brace nesting in an
2471590Srgrimes					 * initialization */
2481590Srgrimes    int         last_nl;	/* this is true if the last thing scanned was
2491590Srgrimes				 * a newline */
2501590Srgrimes    int         in_or_st;	/* Will be true iff there has been a
2511590Srgrimes				 * declarator (e.g. int or char) and no left
2521590Srgrimes				 * paren since the last semicolon. When true,
2531590Srgrimes				 * a '{' is starting a structure definition or
2541590Srgrimes				 * an initialization list */
2551590Srgrimes    int         bl_line;	/* set to 1 by dump_line if the line is blank */
2561590Srgrimes    int         col_1;		/* set to true if the last token started in
2571590Srgrimes				 * column 1 */
2581590Srgrimes    int         com_col;	/* this is the column in which the current
259105244Scharnier				 * comment should start */
2601590Srgrimes    int         com_ind;	/* the column in which comments to the right
2611590Srgrimes				 * of code should start */
2621590Srgrimes    int         com_lines;	/* the number of lines with comments, set by
2631590Srgrimes				 * dump_line */
2641590Srgrimes    int         dec_nest;	/* current nesting level for structure or init */
2651590Srgrimes    int         decl_com_ind;	/* the column in which comments after
2661590Srgrimes				 * declarations should be put */
2671590Srgrimes    int         decl_on_line;	/* set to true if this line of code has part
2681590Srgrimes				 * of a declaration on it */
2691590Srgrimes    int         i_l_follow;	/* the level to which ind_level should be set
2701590Srgrimes				 * after the current line is printed */
2711590Srgrimes    int         in_decl;	/* set to true when we are in a declaration
2721590Srgrimes				 * stmt.  The processing of braces is then
2731590Srgrimes				 * slightly different */
2741590Srgrimes    int         in_stmt;	/* set to 1 while in a stmt */
2751590Srgrimes    int         ind_level;	/* the current indentation level */
2761590Srgrimes    int         ind_size;	/* the size of one indentation level */
2771590Srgrimes    int         ind_stmt;	/* set to 1 if next line should have an extra
2781590Srgrimes				 * indentation level because we are in the
2791590Srgrimes				 * middle of a stmt */
2801590Srgrimes    int         last_u_d;	/* set to true after scanning a token which
2811590Srgrimes				 * forces a following operator to be unary */
2821590Srgrimes    int         leave_comma;	/* if true, never break declarations after
2831590Srgrimes				 * commas */
2841590Srgrimes    int         ljust_decl;	/* true if declarations should be left
2851590Srgrimes				 * justified */
2861590Srgrimes    int         out_coms;	/* the number of comments processed, set by
2871590Srgrimes				 * pr_comment */
2881590Srgrimes    int         out_lines;	/* the number of lines written, set by
2891590Srgrimes				 * dump_line */
2901590Srgrimes    int         p_l_follow;	/* used to remember how to indent following
2911590Srgrimes				 * statement */
2921590Srgrimes    int         paren_level;	/* parenthesization level. used to indent
293105244Scharnier				 * within statements */
2941590Srgrimes    short       paren_indents[20];	/* column positions of each paren */
2951590Srgrimes    int         pcase;		/* set to 1 if the current line label is a
2961590Srgrimes				 * case.  It is printed differently from a
2971590Srgrimes				 * regular label */
2981590Srgrimes    int         search_brace;	/* set to true by parse when it is necessary
2991590Srgrimes				 * to buffer up all info up to the start of a
3001590Srgrimes				 * stmt after an if, while, etc */
3011590Srgrimes    int         unindent_displace;	/* comments not to the right of code
3021590Srgrimes					 * will be placed this many
3031590Srgrimes					 * indentation levels to the left of
3041590Srgrimes					 * code */
3051590Srgrimes    int         use_ff;		/* set to one if the current line should be
3061590Srgrimes				 * terminated with a form feed */
3071590Srgrimes    int         want_blank;	/* set to true when the following token should
3081590Srgrimes				 * be prefixed by a blank. (Said prefixing is
3091590Srgrimes				 * ignored in some cases.) */
3101590Srgrimes    int         else_if;	/* True iff else if pairs should be handled
3111590Srgrimes				 * specially */
3121590Srgrimes    int         decl_indent;	/* column to indent declared identifiers to */
313125633Sbde    int         local_decl_indent;	/* like decl_indent but for locals */
3141590Srgrimes    int         its_a_keyword;
3151590Srgrimes    int         sizeof_keyword;
3161590Srgrimes    int         dumped_decl_indent;
3171590Srgrimes    float       case_indent;	/* The distance to indent case labels from the
3181590Srgrimes				 * switch statement */
3191590Srgrimes    int         in_parameter_declaration;
3201590Srgrimes    int         indent_parameters;
3211590Srgrimes    int         tos;		/* pointer to top of stack */
3221590Srgrimes    char        procname[100];	/* The name of the current procedure */
3231590Srgrimes    int         just_saw_decl;
3241590Srgrimes}           ps;
3251590Srgrimes
3261590Srgrimesint         ifdef_level;
3271590Srgrimesint	    rparen_count;
3281590Srgrimesstruct parser_state state_stack[5];
3291590Srgrimesstruct parser_state match_state[5];
330