121975Sdist /*
235500Sbostic  * Copyright (c) 1985 Sun Microsystems, Inc.
335500Sbostic  * Copyright (c) 1980 The Regents of the University of California.
433767Sbostic  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
533767Sbostic  * All rights reserved.
621975Sdist  *
733767Sbostic  * Redistribution and use in source and binary forms are permitted
834885Sbostic  * provided that the above copyright notice and this paragraph are
934885Sbostic  * duplicated in all such forms and that any documentation,
1034885Sbostic  * advertising materials, and other materials related to such
1134885Sbostic  * distribution and use acknowledge that the software was developed
1235500Sbostic  * by the University of California, Berkeley, the University of Illinois,
1335500Sbostic  * Urbana, and Sun Microsystems, Inc.  The name of either University
1435500Sbostic  * or Sun Microsystems may not be used to endorse or promote products
1535500Sbostic  * derived from this software without specific prior written permission.
1634885Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1734885Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1834885Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1933767Sbostic  *
20*40275Sbostic  *	@(#)indent_globs.h	5.9 (Berkeley) 03/05/90
2121975Sdist  */
228802Smckusick 
2324454Smckusick #include <stdio.h>
2424454Smckusick 
2524454Smckusick #define BACKSLASH '\\'
2635500Sbostic #define bufsize 200		/* size of internal buffers */
2735500Sbostic #define sc_size 5000		/* size of save_com buffer */
2835500Sbostic #define label_offset 2		/* number of levels a label is placed to left
2935500Sbostic 				 * of code */
308802Smckusick 
3135500Sbostic #define tabsize 8		/* the size of a tab */
3235500Sbostic #define tabmask 0177770		/* mask used when figuring length of lines
3335500Sbostic 				 * with tabs */
348802Smckusick 
358802Smckusick 
368802Smckusick #define false 0
378802Smckusick #define true  1
388802Smckusick 
398802Smckusick 
4035500Sbostic FILE       *input;		/* the fid for the input file */
4135500Sbostic FILE       *output;		/* the output file */
428802Smckusick 
43*40275Sbostic #define CHECK_SIZE_CODE \
44*40275Sbostic 	if (e_code >= l_code) { \
45*40275Sbostic 	    register nsize = l_code-s_code+400; \
46*40275Sbostic 	    codebuf = (char *) realloc(codebuf, nsize); \
47*40275Sbostic 	    e_code = codebuf + (e_code-s_code) + 1; \
48*40275Sbostic 	    l_code = codebuf + nsize - 5; \
49*40275Sbostic 	    s_code = codebuf + 1; \
5035500Sbostic 	}
51*40275Sbostic #define CHECK_SIZE_COM \
52*40275Sbostic 	if (e_com >= l_com) { \
53*40275Sbostic 	    register nsize = l_com-s_com+400; \
54*40275Sbostic 	    combuf = (char *) realloc(combuf, nsize); \
55*40275Sbostic 	    e_com = combuf + (e_com-s_com) + 1; \
56*40275Sbostic 	    l_com = combuf + nsize - 5; \
57*40275Sbostic 	    s_com = combuf + 1; \
58*40275Sbostic 	}
59*40275Sbostic #define CHECK_SIZE_LAB \
60*40275Sbostic 	if (e_lab >= l_lab) { \
61*40275Sbostic 	    register nsize = l_lab-s_lab+400; \
62*40275Sbostic 	    labbuf = (char *) realloc(labbuf, nsize); \
63*40275Sbostic 	    e_lab = labbuf + (e_lab-s_lab) + 1; \
64*40275Sbostic 	    l_lab = labbuf + nsize - 5; \
65*40275Sbostic 	    s_lab = labbuf + 1; \
66*40275Sbostic 	}
67*40275Sbostic #define CHECK_SIZE_TOKEN \
68*40275Sbostic 	if (e_token >= l_token) { \
69*40275Sbostic 	    register nsize = l_token-s_token+400; \
70*40275Sbostic 	    tokenbuf = (char *) realloc(tokenbuf, nsize); \
71*40275Sbostic 	    e_token = tokenbuf + (e_token-s_token) + 1; \
72*40275Sbostic 	    l_token = tokenbuf + nsize - 5; \
73*40275Sbostic 	    s_token = tokenbuf + 1; \
74*40275Sbostic 	}
758802Smckusick 
7635500Sbostic char       *labbuf;		/* buffer for label */
7735500Sbostic char       *s_lab;		/* start ... */
7835500Sbostic char       *e_lab;		/* .. and end of stored label */
7935500Sbostic char       *l_lab;		/* limit of label buffer */
808802Smckusick 
8135500Sbostic char       *codebuf;		/* buffer for code section */
8235500Sbostic char       *s_code;		/* start ... */
8335500Sbostic char       *e_code;		/* .. and end of stored code */
8435500Sbostic char       *l_code;		/* limit of code section */
858802Smckusick 
8635500Sbostic char       *combuf;		/* buffer for comments */
8735500Sbostic char       *s_com;		/* start ... */
8835500Sbostic char       *e_com;		/* ... and end of stored comments */
8935500Sbostic char       *l_com;		/* limit of comment buffer */
908802Smckusick 
9138011Sbostic #define token s_token
9238011Sbostic char       *tokenbuf;		/* the last token scanned */
9338011Sbostic char	   *s_token;
9438011Sbostic char       *e_token;
9538011Sbostic char	   *l_token;
9638011Sbostic 
9738011Sbostic char       *in_buffer;		/* input buffer */
9838011Sbostic char	   *in_buffer_limit;	/* the end of the input buffer */
9935500Sbostic char       *buf_ptr;		/* ptr to next character to be taken from
10035500Sbostic 				 * in_buffer */
10135500Sbostic char       *buf_end;		/* ptr to first after last char in in_buffer */
1028802Smckusick 
10335500Sbostic char        save_com[sc_size];	/* input text is saved here when looking for
10435500Sbostic 				 * the brace after an if, while, etc */
10535500Sbostic char       *sc_end;		/* pointer into save_com buffer */
1068802Smckusick 
10735500Sbostic char       *bp_save;		/* saved value of buf_ptr when taking input
10835500Sbostic 				 * from save_com */
10935500Sbostic char       *be_save;		/* similarly saved value of buf_end */
1108802Smckusick 
1118802Smckusick 
11235500Sbostic int         pointer_as_binop;
11335500Sbostic int         blanklines_after_declarations;
11435500Sbostic int         blanklines_before_blockcomments;
11535500Sbostic int         blanklines_after_procs;
11635500Sbostic int         blanklines_around_conditional_compilation;
11735500Sbostic int         swallow_optional_blanklines;
11835500Sbostic int         n_real_blanklines;
11935500Sbostic int         prefix_blankline_requested;
12035500Sbostic int         postfix_blankline_requested;
12135500Sbostic int         break_comma;	/* when true and not in parens, break after a
12235500Sbostic 				 * comma */
12335500Sbostic int         btype_2;		/* when true, brace should be on same line as
12435500Sbostic 				 * if, while, etc */
12535500Sbostic float       case_ind;		/* indentation level to be used for a "case
12635500Sbostic 				 * n:" */
12735500Sbostic int         code_lines;		/* count of lines with code */
12835500Sbostic int         had_eof;		/* set to true when input is exhausted */
12935500Sbostic int         line_no;		/* the current line number. */
13035500Sbostic int         max_col;		/* the maximum allowable line length */
13135500Sbostic int         verbose;		/* when true, non-essential error messages are
13235500Sbostic 				 * printed */
13335500Sbostic int         cuddle_else;	/* true if else should cuddle up to '}' */
13435500Sbostic int         star_comment_cont;	/* true iff comment continuation lines should
13535500Sbostic 				 * have stars at the beginning of each line. */
13635500Sbostic int         comment_delimiter_on_blankline;
13735500Sbostic int         troff;		/* true iff were generating troff input */
13835500Sbostic int         procnames_start_line;	/* if true, the names of procedures
13935500Sbostic 					 * being defined get placed in column
14035500Sbostic 					 * 1 (ie. a newline is placed between
14135500Sbostic 					 * the type of the procedure and its
14235500Sbostic 					 * name) */
14335500Sbostic int         proc_calls_space;	/* If true, procedure calls look like:
14435500Sbostic 				 * foo(bar) rather than foo (bar) */
14535500Sbostic int         format_col1_comments;	/* If comments which start in column 1
14635500Sbostic 					 * are to be magically reformatted
14735500Sbostic 					 * (just like comments that begin in
14835500Sbostic 					 * later columns) */
14935500Sbostic int         inhibit_formatting;	/* true if INDENT OFF is in effect */
15035500Sbostic int         suppress_blanklines;/* set iff following blanklines should be
15135500Sbostic 				 * suppressed */
15235500Sbostic int         continuation_indent;/* set to the indentation between the edge of
15335500Sbostic 				 * code and continuation lines */
15435500Sbostic int         lineup_to_parens;	/* if true, continued code within parens will
15535500Sbostic 				 * be lined up to the open paren */
15635500Sbostic int         Bill_Shannon;	/* true iff a blank should always be inserted
15735500Sbostic 				 * after sizeof */
15835500Sbostic int         blanklines_after_declarations_at_proctop;	/* This is vaguely
15935500Sbostic 							 * similar to
16035500Sbostic 							 * blanklines_after_decla
16135500Sbostic 							 * rations except that
16235500Sbostic 							 * it only applies to
16335500Sbostic 							 * the first set of
16435500Sbostic 							 * declarations in a
16535500Sbostic 							 * procedure (just after
16635500Sbostic 							 * the first '{') and it
16735500Sbostic 							 * causes a blank line
16835500Sbostic 							 * to be generated even
16935500Sbostic 							 * if there are no
17035500Sbostic 							 * declarations */
17135500Sbostic int         block_comment_max_col;
17235500Sbostic int         extra_expression_indent;	/* True if continuation lines from the
17335500Sbostic 					 * expression part of "if(e)",
17435500Sbostic 					 * "while(e)", "for(e;e;e)" should be
17535500Sbostic 					 * indented an extra tab stop so that
17635500Sbostic 					 * they don't conflict with the code
17735500Sbostic 					 * that follows */
17824454Smckusick 
17935500Sbostic /* -troff font state information */
18024454Smckusick 
18135500Sbostic struct fstate {
18235500Sbostic     char        font[4];
18335500Sbostic     char        size;
18435500Sbostic     int         allcaps:1;
18535500Sbostic };
18635500Sbostic char       *chfont();
18735500Sbostic 
18835500Sbostic struct fstate
18935500Sbostic             keywordf,		/* keyword font */
19035500Sbostic             stringf,		/* string font */
19135500Sbostic             boxcomf,		/* Box comment font */
19235500Sbostic             blkcomf,		/* Block comment font */
19335500Sbostic             scomf,		/* Same line comment font */
19435500Sbostic             bodyf;		/* major body font */
19535500Sbostic 
19635500Sbostic 
19735500Sbostic #define STACKSIZE 150
19835500Sbostic 
19924454Smckusick struct parser_state {
20024454Smckusick     int         last_token;
20135500Sbostic     struct fstate cfont;	/* Current font */
20235500Sbostic     int         p_stack[STACKSIZE];	/* this is the parsers stack */
20335500Sbostic     int         il[STACKSIZE];	/* this stack stores indentation levels */
20435500Sbostic     float       cstk[STACKSIZE];/* used to store case stmt indentation levels */
20524454Smckusick     int         box_com;	/* set to true when we are in a "boxed"
20635500Sbostic 				 * comment. In that case, the first non-blank
20735500Sbostic 				 * char should be lined up with the / in /* */
20824454Smckusick     int         comment_delta,
20924454Smckusick                 n_comment_delta;
21024454Smckusick     int         cast_mask;	/* indicates which close parens close off
21124454Smckusick 				 * casts */
21235500Sbostic     int         sizeof_mask;	/* indicates which close parens close off
21335500Sbostic 				 * sizeof''s */
21424454Smckusick     int         block_init;	/* true iff inside a block initialization */
21535500Sbostic     int         block_init_level;	/* The level of brace nesting in an
21635500Sbostic 					 * initialization */
21735500Sbostic     int         last_nl;	/* this is true if the last thing scanned was
21835500Sbostic 				 * a newline */
21924454Smckusick     int         in_or_st;	/* Will be true iff there has been a
22035500Sbostic 				 * declarator (e.g. int or char) and no left
22135500Sbostic 				 * paren since the last semicolon. When true,
22235500Sbostic 				 * a '{' is starting a structure definition or
22335500Sbostic 				 * an initialization list */
22435500Sbostic     int         bl_line;	/* set to 1 by dump_line if the line is blank */
22535500Sbostic     int         col_1;		/* set to true if the last token started in
22635500Sbostic 				 * column 1 */
22724454Smckusick     int         com_col;	/* this is the column in which the current
22824454Smckusick 				 * coment should start */
22935500Sbostic     int         com_ind;	/* the column in which comments to the right
23035500Sbostic 				 * of code should start */
23135500Sbostic     int         com_lines;	/* the number of lines with comments, set by
23235500Sbostic 				 * dump_line */
23335500Sbostic     int         dec_nest;	/* current nesting level for structure or init */
23424454Smckusick     int         decl_com_ind;	/* the column in which comments after
23524454Smckusick 				 * declarations should be put */
23635500Sbostic     int         decl_on_line;	/* set to true if this line of code has part
23735500Sbostic 				 * of a declaration on it */
23835500Sbostic     int         i_l_follow;	/* the level to which ind_level should be set
23935500Sbostic 				 * after the current line is printed */
24035500Sbostic     int         in_decl;	/* set to true when we are in a declaration
24135500Sbostic 				 * stmt.  The processing of braces is then
24235500Sbostic 				 * slightly different */
24324454Smckusick     int         in_stmt;	/* set to 1 while in a stmt */
24424454Smckusick     int         ind_level;	/* the current indentation level */
24524454Smckusick     int         ind_size;	/* the size of one indentation level */
24635500Sbostic     int         ind_stmt;	/* set to 1 if next line should have an extra
24735500Sbostic 				 * indentation level because we are in the
24835500Sbostic 				 * middle of a stmt */
24935500Sbostic     int         last_u_d;	/* set to true after scanning a token which
25035500Sbostic 				 * forces a following operator to be unary */
25124454Smckusick     int         leave_comma;	/* if true, never break declarations after
25224454Smckusick 				 * commas */
25324454Smckusick     int         ljust_decl;	/* true if declarations should be left
25424454Smckusick 				 * justified */
25535500Sbostic     int         out_coms;	/* the number of comments processed, set by
25635500Sbostic 				 * pr_comment */
25724454Smckusick     int         out_lines;	/* the number of lines written, set by
25824454Smckusick 				 * dump_line */
25935500Sbostic     int         p_l_follow;	/* used to remember how to indent following
26035500Sbostic 				 * statement */
26124454Smckusick     int         paren_level;	/* parenthesization level. used to indent
26224454Smckusick 				 * within stmts */
26324454Smckusick     short       paren_indents[20];	/* column positions of each paren */
26424454Smckusick     int         pcase;		/* set to 1 if the current line label is a
26535500Sbostic 				 * case.  It is printed differently from a
26635500Sbostic 				 * regular label */
26735500Sbostic     int         search_brace;	/* set to true by parse when it is necessary
26835500Sbostic 				 * to buffer up all info up to the start of a
26935500Sbostic 				 * stmt after an if, while, etc */
27035500Sbostic     int         unindent_displace;	/* comments not to the right of code
27135500Sbostic 					 * will be placed this many
27235500Sbostic 					 * indentation levels to the left of
27335500Sbostic 					 * code */
27435500Sbostic     int         use_ff;		/* set to one if the current line should be
27535500Sbostic 				 * terminated with a form feed */
27635500Sbostic     int         want_blank;	/* set to true when the following token should
27735500Sbostic 				 * be prefixed by a blank. (Said prefixing is
27835500Sbostic 				 * ignored in some cases.) */
27935500Sbostic     int         else_if;	/* True iff else if pairs should be handled
28035500Sbostic 				 * specially */
28135500Sbostic     int         decl_indent;	/* column to indent declared identifiers to */
28224454Smckusick     int         its_a_keyword;
28335500Sbostic     int         sizeof_keyword;
28424454Smckusick     int         dumped_decl_indent;
28535500Sbostic     float       case_indent;	/* The distance to indent case labels from the
28635500Sbostic 				 * switch statement */
28724454Smckusick     int         in_parameter_declaration;
28824454Smckusick     int         indent_parameters;
28924454Smckusick     int         tos;		/* pointer to top of stack */
29024454Smckusick     char        procname[100];	/* The name of the current procedure */
29135500Sbostic     int         just_saw_decl;
29224454Smckusick }           ps;
29324454Smckusick 
29435500Sbostic int         ifdef_level;
29538011Sbostic int	    rparen_count;
29624454Smckusick struct parser_state state_stack[5];
29724454Smckusick struct parser_state match_state[5];
298