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 * 7*42688Sbostic * %sccs.include.redist.c% 833767Sbostic * 9*42688Sbostic * @(#)indent_globs.h 5.10 (Berkeley) 06/01/90 1021975Sdist */ 118802Smckusick 1224454Smckusick #include <stdio.h> 1324454Smckusick 1424454Smckusick #define BACKSLASH '\\' 1535500Sbostic #define bufsize 200 /* size of internal buffers */ 1635500Sbostic #define sc_size 5000 /* size of save_com buffer */ 1735500Sbostic #define label_offset 2 /* number of levels a label is placed to left 1835500Sbostic * of code */ 198802Smckusick 2035500Sbostic #define tabsize 8 /* the size of a tab */ 2135500Sbostic #define tabmask 0177770 /* mask used when figuring length of lines 2235500Sbostic * with tabs */ 238802Smckusick 248802Smckusick 258802Smckusick #define false 0 268802Smckusick #define true 1 278802Smckusick 288802Smckusick 2935500Sbostic FILE *input; /* the fid for the input file */ 3035500Sbostic FILE *output; /* the output file */ 318802Smckusick 3240275Sbostic #define CHECK_SIZE_CODE \ 3340275Sbostic if (e_code >= l_code) { \ 3440275Sbostic register nsize = l_code-s_code+400; \ 3540275Sbostic codebuf = (char *) realloc(codebuf, nsize); \ 3640275Sbostic e_code = codebuf + (e_code-s_code) + 1; \ 3740275Sbostic l_code = codebuf + nsize - 5; \ 3840275Sbostic s_code = codebuf + 1; \ 3935500Sbostic } 4040275Sbostic #define CHECK_SIZE_COM \ 4140275Sbostic if (e_com >= l_com) { \ 4240275Sbostic register nsize = l_com-s_com+400; \ 4340275Sbostic combuf = (char *) realloc(combuf, nsize); \ 4440275Sbostic e_com = combuf + (e_com-s_com) + 1; \ 4540275Sbostic l_com = combuf + nsize - 5; \ 4640275Sbostic s_com = combuf + 1; \ 4740275Sbostic } 4840275Sbostic #define CHECK_SIZE_LAB \ 4940275Sbostic if (e_lab >= l_lab) { \ 5040275Sbostic register nsize = l_lab-s_lab+400; \ 5140275Sbostic labbuf = (char *) realloc(labbuf, nsize); \ 5240275Sbostic e_lab = labbuf + (e_lab-s_lab) + 1; \ 5340275Sbostic l_lab = labbuf + nsize - 5; \ 5440275Sbostic s_lab = labbuf + 1; \ 5540275Sbostic } 5640275Sbostic #define CHECK_SIZE_TOKEN \ 5740275Sbostic if (e_token >= l_token) { \ 5840275Sbostic register nsize = l_token-s_token+400; \ 5940275Sbostic tokenbuf = (char *) realloc(tokenbuf, nsize); \ 6040275Sbostic e_token = tokenbuf + (e_token-s_token) + 1; \ 6140275Sbostic l_token = tokenbuf + nsize - 5; \ 6240275Sbostic s_token = tokenbuf + 1; \ 6340275Sbostic } 648802Smckusick 6535500Sbostic char *labbuf; /* buffer for label */ 6635500Sbostic char *s_lab; /* start ... */ 6735500Sbostic char *e_lab; /* .. and end of stored label */ 6835500Sbostic char *l_lab; /* limit of label buffer */ 698802Smckusick 7035500Sbostic char *codebuf; /* buffer for code section */ 7135500Sbostic char *s_code; /* start ... */ 7235500Sbostic char *e_code; /* .. and end of stored code */ 7335500Sbostic char *l_code; /* limit of code section */ 748802Smckusick 7535500Sbostic char *combuf; /* buffer for comments */ 7635500Sbostic char *s_com; /* start ... */ 7735500Sbostic char *e_com; /* ... and end of stored comments */ 7835500Sbostic char *l_com; /* limit of comment buffer */ 798802Smckusick 8038011Sbostic #define token s_token 8138011Sbostic char *tokenbuf; /* the last token scanned */ 8238011Sbostic char *s_token; 8338011Sbostic char *e_token; 8438011Sbostic char *l_token; 8538011Sbostic 8638011Sbostic char *in_buffer; /* input buffer */ 8738011Sbostic char *in_buffer_limit; /* the end of the input buffer */ 8835500Sbostic char *buf_ptr; /* ptr to next character to be taken from 8935500Sbostic * in_buffer */ 9035500Sbostic char *buf_end; /* ptr to first after last char in in_buffer */ 918802Smckusick 9235500Sbostic char save_com[sc_size]; /* input text is saved here when looking for 9335500Sbostic * the brace after an if, while, etc */ 9435500Sbostic char *sc_end; /* pointer into save_com buffer */ 958802Smckusick 9635500Sbostic char *bp_save; /* saved value of buf_ptr when taking input 9735500Sbostic * from save_com */ 9835500Sbostic char *be_save; /* similarly saved value of buf_end */ 998802Smckusick 1008802Smckusick 10135500Sbostic int pointer_as_binop; 10235500Sbostic int blanklines_after_declarations; 10335500Sbostic int blanklines_before_blockcomments; 10435500Sbostic int blanklines_after_procs; 10535500Sbostic int blanklines_around_conditional_compilation; 10635500Sbostic int swallow_optional_blanklines; 10735500Sbostic int n_real_blanklines; 10835500Sbostic int prefix_blankline_requested; 10935500Sbostic int postfix_blankline_requested; 11035500Sbostic int break_comma; /* when true and not in parens, break after a 11135500Sbostic * comma */ 11235500Sbostic int btype_2; /* when true, brace should be on same line as 11335500Sbostic * if, while, etc */ 11435500Sbostic float case_ind; /* indentation level to be used for a "case 11535500Sbostic * n:" */ 11635500Sbostic int code_lines; /* count of lines with code */ 11735500Sbostic int had_eof; /* set to true when input is exhausted */ 11835500Sbostic int line_no; /* the current line number. */ 11935500Sbostic int max_col; /* the maximum allowable line length */ 12035500Sbostic int verbose; /* when true, non-essential error messages are 12135500Sbostic * printed */ 12235500Sbostic int cuddle_else; /* true if else should cuddle up to '}' */ 12335500Sbostic int star_comment_cont; /* true iff comment continuation lines should 12435500Sbostic * have stars at the beginning of each line. */ 12535500Sbostic int comment_delimiter_on_blankline; 12635500Sbostic int troff; /* true iff were generating troff input */ 12735500Sbostic int procnames_start_line; /* if true, the names of procedures 12835500Sbostic * being defined get placed in column 12935500Sbostic * 1 (ie. a newline is placed between 13035500Sbostic * the type of the procedure and its 13135500Sbostic * name) */ 13235500Sbostic int proc_calls_space; /* If true, procedure calls look like: 13335500Sbostic * foo(bar) rather than foo (bar) */ 13435500Sbostic int format_col1_comments; /* If comments which start in column 1 13535500Sbostic * are to be magically reformatted 13635500Sbostic * (just like comments that begin in 13735500Sbostic * later columns) */ 13835500Sbostic int inhibit_formatting; /* true if INDENT OFF is in effect */ 13935500Sbostic int suppress_blanklines;/* set iff following blanklines should be 14035500Sbostic * suppressed */ 14135500Sbostic int continuation_indent;/* set to the indentation between the edge of 14235500Sbostic * code and continuation lines */ 14335500Sbostic int lineup_to_parens; /* if true, continued code within parens will 14435500Sbostic * be lined up to the open paren */ 14535500Sbostic int Bill_Shannon; /* true iff a blank should always be inserted 14635500Sbostic * after sizeof */ 14735500Sbostic int blanklines_after_declarations_at_proctop; /* This is vaguely 14835500Sbostic * similar to 14935500Sbostic * blanklines_after_decla 15035500Sbostic * rations except that 15135500Sbostic * it only applies to 15235500Sbostic * the first set of 15335500Sbostic * declarations in a 15435500Sbostic * procedure (just after 15535500Sbostic * the first '{') and it 15635500Sbostic * causes a blank line 15735500Sbostic * to be generated even 15835500Sbostic * if there are no 15935500Sbostic * declarations */ 16035500Sbostic int block_comment_max_col; 16135500Sbostic int extra_expression_indent; /* True if continuation lines from the 16235500Sbostic * expression part of "if(e)", 16335500Sbostic * "while(e)", "for(e;e;e)" should be 16435500Sbostic * indented an extra tab stop so that 16535500Sbostic * they don't conflict with the code 16635500Sbostic * that follows */ 16724454Smckusick 16835500Sbostic /* -troff font state information */ 16924454Smckusick 17035500Sbostic struct fstate { 17135500Sbostic char font[4]; 17235500Sbostic char size; 17335500Sbostic int allcaps:1; 17435500Sbostic }; 17535500Sbostic char *chfont(); 17635500Sbostic 17735500Sbostic struct fstate 17835500Sbostic keywordf, /* keyword font */ 17935500Sbostic stringf, /* string font */ 18035500Sbostic boxcomf, /* Box comment font */ 18135500Sbostic blkcomf, /* Block comment font */ 18235500Sbostic scomf, /* Same line comment font */ 18335500Sbostic bodyf; /* major body font */ 18435500Sbostic 18535500Sbostic 18635500Sbostic #define STACKSIZE 150 18735500Sbostic 18824454Smckusick struct parser_state { 18924454Smckusick int last_token; 19035500Sbostic struct fstate cfont; /* Current font */ 19135500Sbostic int p_stack[STACKSIZE]; /* this is the parsers stack */ 19235500Sbostic int il[STACKSIZE]; /* this stack stores indentation levels */ 19335500Sbostic float cstk[STACKSIZE];/* used to store case stmt indentation levels */ 19424454Smckusick int box_com; /* set to true when we are in a "boxed" 19535500Sbostic * comment. In that case, the first non-blank 19635500Sbostic * char should be lined up with the / in /* */ 19724454Smckusick int comment_delta, 19824454Smckusick n_comment_delta; 19924454Smckusick int cast_mask; /* indicates which close parens close off 20024454Smckusick * casts */ 20135500Sbostic int sizeof_mask; /* indicates which close parens close off 20235500Sbostic * sizeof''s */ 20324454Smckusick int block_init; /* true iff inside a block initialization */ 20435500Sbostic int block_init_level; /* The level of brace nesting in an 20535500Sbostic * initialization */ 20635500Sbostic int last_nl; /* this is true if the last thing scanned was 20735500Sbostic * a newline */ 20824454Smckusick int in_or_st; /* Will be true iff there has been a 20935500Sbostic * declarator (e.g. int or char) and no left 21035500Sbostic * paren since the last semicolon. When true, 21135500Sbostic * a '{' is starting a structure definition or 21235500Sbostic * an initialization list */ 21335500Sbostic int bl_line; /* set to 1 by dump_line if the line is blank */ 21435500Sbostic int col_1; /* set to true if the last token started in 21535500Sbostic * column 1 */ 21624454Smckusick int com_col; /* this is the column in which the current 21724454Smckusick * coment should start */ 21835500Sbostic int com_ind; /* the column in which comments to the right 21935500Sbostic * of code should start */ 22035500Sbostic int com_lines; /* the number of lines with comments, set by 22135500Sbostic * dump_line */ 22235500Sbostic int dec_nest; /* current nesting level for structure or init */ 22324454Smckusick int decl_com_ind; /* the column in which comments after 22424454Smckusick * declarations should be put */ 22535500Sbostic int decl_on_line; /* set to true if this line of code has part 22635500Sbostic * of a declaration on it */ 22735500Sbostic int i_l_follow; /* the level to which ind_level should be set 22835500Sbostic * after the current line is printed */ 22935500Sbostic int in_decl; /* set to true when we are in a declaration 23035500Sbostic * stmt. The processing of braces is then 23135500Sbostic * slightly different */ 23224454Smckusick int in_stmt; /* set to 1 while in a stmt */ 23324454Smckusick int ind_level; /* the current indentation level */ 23424454Smckusick int ind_size; /* the size of one indentation level */ 23535500Sbostic int ind_stmt; /* set to 1 if next line should have an extra 23635500Sbostic * indentation level because we are in the 23735500Sbostic * middle of a stmt */ 23835500Sbostic int last_u_d; /* set to true after scanning a token which 23935500Sbostic * forces a following operator to be unary */ 24024454Smckusick int leave_comma; /* if true, never break declarations after 24124454Smckusick * commas */ 24224454Smckusick int ljust_decl; /* true if declarations should be left 24324454Smckusick * justified */ 24435500Sbostic int out_coms; /* the number of comments processed, set by 24535500Sbostic * pr_comment */ 24624454Smckusick int out_lines; /* the number of lines written, set by 24724454Smckusick * dump_line */ 24835500Sbostic int p_l_follow; /* used to remember how to indent following 24935500Sbostic * statement */ 25024454Smckusick int paren_level; /* parenthesization level. used to indent 25124454Smckusick * within stmts */ 25224454Smckusick short paren_indents[20]; /* column positions of each paren */ 25324454Smckusick int pcase; /* set to 1 if the current line label is a 25435500Sbostic * case. It is printed differently from a 25535500Sbostic * regular label */ 25635500Sbostic int search_brace; /* set to true by parse when it is necessary 25735500Sbostic * to buffer up all info up to the start of a 25835500Sbostic * stmt after an if, while, etc */ 25935500Sbostic int unindent_displace; /* comments not to the right of code 26035500Sbostic * will be placed this many 26135500Sbostic * indentation levels to the left of 26235500Sbostic * code */ 26335500Sbostic int use_ff; /* set to one if the current line should be 26435500Sbostic * terminated with a form feed */ 26535500Sbostic int want_blank; /* set to true when the following token should 26635500Sbostic * be prefixed by a blank. (Said prefixing is 26735500Sbostic * ignored in some cases.) */ 26835500Sbostic int else_if; /* True iff else if pairs should be handled 26935500Sbostic * specially */ 27035500Sbostic int decl_indent; /* column to indent declared identifiers to */ 27124454Smckusick int its_a_keyword; 27235500Sbostic int sizeof_keyword; 27324454Smckusick int dumped_decl_indent; 27435500Sbostic float case_indent; /* The distance to indent case labels from the 27535500Sbostic * switch statement */ 27624454Smckusick int in_parameter_declaration; 27724454Smckusick int indent_parameters; 27824454Smckusick int tos; /* pointer to top of stack */ 27924454Smckusick char procname[100]; /* The name of the current procedure */ 28035500Sbostic int just_saw_decl; 28124454Smckusick } ps; 28224454Smckusick 28335500Sbostic int ifdef_level; 28438011Sbostic int rparen_count; 28524454Smckusick struct parser_state state_stack[5]; 28624454Smckusick struct parser_state match_state[5]; 287