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*38011Sbostic  *	@(#)indent_globs.h	5.8 (Berkeley) 05/15/89
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 
4335500Sbostic #define check_size(name) \
4435500Sbostic 	if (e_/**/name >= l_/**/name) { \
4535500Sbostic 	    register nsize = l_/**/name-s_/**/name+400; \
4635500Sbostic 	    name/**/buf = (char *) realloc(name/**/buf, nsize); \
4735500Sbostic 	    e_/**/name = name/**/buf + (e_/**/name-s_/**/name) + 1; \
4835500Sbostic 	    l_/**/name = name/**/buf + nsize - 5; \
4935500Sbostic 	    s_/**/name = name/**/buf + 1; \
5035500Sbostic 	}
518802Smckusick 
5235500Sbostic char       *labbuf;		/* buffer for label */
5335500Sbostic char       *s_lab;		/* start ... */
5435500Sbostic char       *e_lab;		/* .. and end of stored label */
5535500Sbostic char       *l_lab;		/* limit of label buffer */
568802Smckusick 
5735500Sbostic char       *codebuf;		/* buffer for code section */
5835500Sbostic char       *s_code;		/* start ... */
5935500Sbostic char       *e_code;		/* .. and end of stored code */
6035500Sbostic char       *l_code;		/* limit of code section */
618802Smckusick 
6235500Sbostic char       *combuf;		/* buffer for comments */
6335500Sbostic char       *s_com;		/* start ... */
6435500Sbostic char       *e_com;		/* ... and end of stored comments */
6535500Sbostic char       *l_com;		/* limit of comment buffer */
668802Smckusick 
67*38011Sbostic #define token s_token
68*38011Sbostic char       *tokenbuf;		/* the last token scanned */
69*38011Sbostic char	   *s_token;
70*38011Sbostic char       *e_token;
71*38011Sbostic char	   *l_token;
72*38011Sbostic 
73*38011Sbostic char       *in_buffer;		/* input buffer */
74*38011Sbostic char	   *in_buffer_limit;	/* the end of the input buffer */
7535500Sbostic char       *buf_ptr;		/* ptr to next character to be taken from
7635500Sbostic 				 * in_buffer */
7735500Sbostic char       *buf_end;		/* ptr to first after last char in in_buffer */
788802Smckusick 
7935500Sbostic char        save_com[sc_size];	/* input text is saved here when looking for
8035500Sbostic 				 * the brace after an if, while, etc */
8135500Sbostic char       *sc_end;		/* pointer into save_com buffer */
828802Smckusick 
8335500Sbostic char       *bp_save;		/* saved value of buf_ptr when taking input
8435500Sbostic 				 * from save_com */
8535500Sbostic char       *be_save;		/* similarly saved value of buf_end */
868802Smckusick 
878802Smckusick 
8835500Sbostic int         pointer_as_binop;
8935500Sbostic int         blanklines_after_declarations;
9035500Sbostic int         blanklines_before_blockcomments;
9135500Sbostic int         blanklines_after_procs;
9235500Sbostic int         blanklines_around_conditional_compilation;
9335500Sbostic int         swallow_optional_blanklines;
9435500Sbostic int         n_real_blanklines;
9535500Sbostic int         prefix_blankline_requested;
9635500Sbostic int         postfix_blankline_requested;
9735500Sbostic int         break_comma;	/* when true and not in parens, break after a
9835500Sbostic 				 * comma */
9935500Sbostic int         btype_2;		/* when true, brace should be on same line as
10035500Sbostic 				 * if, while, etc */
10135500Sbostic float       case_ind;		/* indentation level to be used for a "case
10235500Sbostic 				 * n:" */
10335500Sbostic int         code_lines;		/* count of lines with code */
10435500Sbostic int         had_eof;		/* set to true when input is exhausted */
10535500Sbostic int         line_no;		/* the current line number. */
10635500Sbostic int         max_col;		/* the maximum allowable line length */
10735500Sbostic int         verbose;		/* when true, non-essential error messages are
10835500Sbostic 				 * printed */
10935500Sbostic int         cuddle_else;	/* true if else should cuddle up to '}' */
11035500Sbostic int         star_comment_cont;	/* true iff comment continuation lines should
11135500Sbostic 				 * have stars at the beginning of each line. */
11235500Sbostic int         comment_delimiter_on_blankline;
11335500Sbostic int         troff;		/* true iff were generating troff input */
11435500Sbostic int         procnames_start_line;	/* if true, the names of procedures
11535500Sbostic 					 * being defined get placed in column
11635500Sbostic 					 * 1 (ie. a newline is placed between
11735500Sbostic 					 * the type of the procedure and its
11835500Sbostic 					 * name) */
11935500Sbostic int         proc_calls_space;	/* If true, procedure calls look like:
12035500Sbostic 				 * foo(bar) rather than foo (bar) */
12135500Sbostic int         format_col1_comments;	/* If comments which start in column 1
12235500Sbostic 					 * are to be magically reformatted
12335500Sbostic 					 * (just like comments that begin in
12435500Sbostic 					 * later columns) */
12535500Sbostic int         inhibit_formatting;	/* true if INDENT OFF is in effect */
12635500Sbostic int         suppress_blanklines;/* set iff following blanklines should be
12735500Sbostic 				 * suppressed */
12835500Sbostic int         continuation_indent;/* set to the indentation between the edge of
12935500Sbostic 				 * code and continuation lines */
13035500Sbostic int         lineup_to_parens;	/* if true, continued code within parens will
13135500Sbostic 				 * be lined up to the open paren */
13235500Sbostic int         Bill_Shannon;	/* true iff a blank should always be inserted
13335500Sbostic 				 * after sizeof */
13435500Sbostic int         blanklines_after_declarations_at_proctop;	/* This is vaguely
13535500Sbostic 							 * similar to
13635500Sbostic 							 * blanklines_after_decla
13735500Sbostic 							 * rations except that
13835500Sbostic 							 * it only applies to
13935500Sbostic 							 * the first set of
14035500Sbostic 							 * declarations in a
14135500Sbostic 							 * procedure (just after
14235500Sbostic 							 * the first '{') and it
14335500Sbostic 							 * causes a blank line
14435500Sbostic 							 * to be generated even
14535500Sbostic 							 * if there are no
14635500Sbostic 							 * declarations */
14735500Sbostic int         block_comment_max_col;
14835500Sbostic int         extra_expression_indent;	/* True if continuation lines from the
14935500Sbostic 					 * expression part of "if(e)",
15035500Sbostic 					 * "while(e)", "for(e;e;e)" should be
15135500Sbostic 					 * indented an extra tab stop so that
15235500Sbostic 					 * they don't conflict with the code
15335500Sbostic 					 * that follows */
15424454Smckusick 
15535500Sbostic /* -troff font state information */
15624454Smckusick 
15735500Sbostic struct fstate {
15835500Sbostic     char        font[4];
15935500Sbostic     char        size;
16035500Sbostic     int         allcaps:1;
16135500Sbostic };
16235500Sbostic char       *chfont();
16335500Sbostic 
16435500Sbostic struct fstate
16535500Sbostic             keywordf,		/* keyword font */
16635500Sbostic             stringf,		/* string font */
16735500Sbostic             boxcomf,		/* Box comment font */
16835500Sbostic             blkcomf,		/* Block comment font */
16935500Sbostic             scomf,		/* Same line comment font */
17035500Sbostic             bodyf;		/* major body font */
17135500Sbostic 
17235500Sbostic 
17335500Sbostic #define STACKSIZE 150
17435500Sbostic 
17524454Smckusick struct parser_state {
17624454Smckusick     int         last_token;
17735500Sbostic     struct fstate cfont;	/* Current font */
17835500Sbostic     int         p_stack[STACKSIZE];	/* this is the parsers stack */
17935500Sbostic     int         il[STACKSIZE];	/* this stack stores indentation levels */
18035500Sbostic     float       cstk[STACKSIZE];/* used to store case stmt indentation levels */
18124454Smckusick     int         box_com;	/* set to true when we are in a "boxed"
18235500Sbostic 				 * comment. In that case, the first non-blank
18335500Sbostic 				 * char should be lined up with the / in /* */
18424454Smckusick     int         comment_delta,
18524454Smckusick                 n_comment_delta;
18624454Smckusick     int         cast_mask;	/* indicates which close parens close off
18724454Smckusick 				 * casts */
18835500Sbostic     int         sizeof_mask;	/* indicates which close parens close off
18935500Sbostic 				 * sizeof''s */
19024454Smckusick     int         block_init;	/* true iff inside a block initialization */
19135500Sbostic     int         block_init_level;	/* The level of brace nesting in an
19235500Sbostic 					 * initialization */
19335500Sbostic     int         last_nl;	/* this is true if the last thing scanned was
19435500Sbostic 				 * a newline */
19524454Smckusick     int         in_or_st;	/* Will be true iff there has been a
19635500Sbostic 				 * declarator (e.g. int or char) and no left
19735500Sbostic 				 * paren since the last semicolon. When true,
19835500Sbostic 				 * a '{' is starting a structure definition or
19935500Sbostic 				 * an initialization list */
20035500Sbostic     int         bl_line;	/* set to 1 by dump_line if the line is blank */
20135500Sbostic     int         col_1;		/* set to true if the last token started in
20235500Sbostic 				 * column 1 */
20324454Smckusick     int         com_col;	/* this is the column in which the current
20424454Smckusick 				 * coment should start */
20535500Sbostic     int         com_ind;	/* the column in which comments to the right
20635500Sbostic 				 * of code should start */
20735500Sbostic     int         com_lines;	/* the number of lines with comments, set by
20835500Sbostic 				 * dump_line */
20935500Sbostic     int         dec_nest;	/* current nesting level for structure or init */
21024454Smckusick     int         decl_com_ind;	/* the column in which comments after
21124454Smckusick 				 * declarations should be put */
21235500Sbostic     int         decl_on_line;	/* set to true if this line of code has part
21335500Sbostic 				 * of a declaration on it */
21435500Sbostic     int         i_l_follow;	/* the level to which ind_level should be set
21535500Sbostic 				 * after the current line is printed */
21635500Sbostic     int         in_decl;	/* set to true when we are in a declaration
21735500Sbostic 				 * stmt.  The processing of braces is then
21835500Sbostic 				 * slightly different */
21924454Smckusick     int         in_stmt;	/* set to 1 while in a stmt */
22024454Smckusick     int         ind_level;	/* the current indentation level */
22124454Smckusick     int         ind_size;	/* the size of one indentation level */
22235500Sbostic     int         ind_stmt;	/* set to 1 if next line should have an extra
22335500Sbostic 				 * indentation level because we are in the
22435500Sbostic 				 * middle of a stmt */
22535500Sbostic     int         last_u_d;	/* set to true after scanning a token which
22635500Sbostic 				 * forces a following operator to be unary */
22724454Smckusick     int         leave_comma;	/* if true, never break declarations after
22824454Smckusick 				 * commas */
22924454Smckusick     int         ljust_decl;	/* true if declarations should be left
23024454Smckusick 				 * justified */
23135500Sbostic     int         out_coms;	/* the number of comments processed, set by
23235500Sbostic 				 * pr_comment */
23324454Smckusick     int         out_lines;	/* the number of lines written, set by
23424454Smckusick 				 * dump_line */
23535500Sbostic     int         p_l_follow;	/* used to remember how to indent following
23635500Sbostic 				 * statement */
23724454Smckusick     int         paren_level;	/* parenthesization level. used to indent
23824454Smckusick 				 * within stmts */
23924454Smckusick     short       paren_indents[20];	/* column positions of each paren */
24024454Smckusick     int         pcase;		/* set to 1 if the current line label is a
24135500Sbostic 				 * case.  It is printed differently from a
24235500Sbostic 				 * regular label */
24335500Sbostic     int         search_brace;	/* set to true by parse when it is necessary
24435500Sbostic 				 * to buffer up all info up to the start of a
24535500Sbostic 				 * stmt after an if, while, etc */
24635500Sbostic     int         unindent_displace;	/* comments not to the right of code
24735500Sbostic 					 * will be placed this many
24835500Sbostic 					 * indentation levels to the left of
24935500Sbostic 					 * code */
25035500Sbostic     int         use_ff;		/* set to one if the current line should be
25135500Sbostic 				 * terminated with a form feed */
25235500Sbostic     int         want_blank;	/* set to true when the following token should
25335500Sbostic 				 * be prefixed by a blank. (Said prefixing is
25435500Sbostic 				 * ignored in some cases.) */
25535500Sbostic     int         else_if;	/* True iff else if pairs should be handled
25635500Sbostic 				 * specially */
25735500Sbostic     int         decl_indent;	/* column to indent declared identifiers to */
25824454Smckusick     int         its_a_keyword;
25935500Sbostic     int         sizeof_keyword;
26024454Smckusick     int         dumped_decl_indent;
26135500Sbostic     float       case_indent;	/* The distance to indent case labels from the
26235500Sbostic 				 * switch statement */
26324454Smckusick     int         in_parameter_declaration;
26424454Smckusick     int         indent_parameters;
26524454Smckusick     int         tos;		/* pointer to top of stack */
26624454Smckusick     char        procname[100];	/* The name of the current procedure */
26735500Sbostic     int         just_saw_decl;
26824454Smckusick }           ps;
26924454Smckusick 
27035500Sbostic int         ifdef_level;
271*38011Sbostic int	    rparen_count;
27224454Smckusick struct parser_state state_stack[5];
27324454Smckusick struct parser_state match_state[5];
274