1 /* 2 * Copyright (c) 1985 Sun Microsystems, Inc. 3 * Copyright (c) 1980, 1993 4 * The Regents of the University of California. All rights reserved. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)indent_globs.h 8.1 (Berkeley) 6/6/93 36 */ 37 38 #define BACKSLASH '\\' 39 #define bufsize 200 /* size of internal buffers */ 40 #define sc_size 5000 /* size of save_com buffer */ 41 #define label_offset 2 /* number of levels a label is placed to left 42 * of code */ 43 44 #define tabsize 8 /* the size of a tab */ 45 #define tabmask 0177770 /* mask used when figuring length of lines 46 * with tabs */ 47 48 49 #define false 0 50 #define true 1 51 52 53 FILE *input; /* the fid for the input file */ 54 FILE *output; /* the output file */ 55 56 #define CHECK_SIZE_CODE \ 57 if (e_code >= l_code) { \ 58 register int nsize = l_code-s_code+400; \ 59 codebuf = (char *) realloc(codebuf, nsize); \ 60 e_code = codebuf + (e_code-s_code) + 1; \ 61 l_code = codebuf + nsize - 5; \ 62 s_code = codebuf + 1; \ 63 } 64 #define CHECK_SIZE_COM \ 65 if (e_com >= l_com) { \ 66 register int nsize = l_com-s_com+400; \ 67 combuf = (char *) realloc(combuf, nsize); \ 68 e_com = combuf + (e_com-s_com) + 1; \ 69 l_com = combuf + nsize - 5; \ 70 s_com = combuf + 1; \ 71 } 72 #define CHECK_SIZE_LAB \ 73 if (e_lab >= l_lab) { \ 74 register int nsize = l_lab-s_lab+400; \ 75 labbuf = (char *) realloc(labbuf, nsize); \ 76 e_lab = labbuf + (e_lab-s_lab) + 1; \ 77 l_lab = labbuf + nsize - 5; \ 78 s_lab = labbuf + 1; \ 79 } 80 #define CHECK_SIZE_TOKEN \ 81 if (e_token >= l_token) { \ 82 register int nsize = l_token-s_token+400; \ 83 tokenbuf = (char *) realloc(tokenbuf, nsize); \ 84 e_token = tokenbuf + (e_token-s_token) + 1; \ 85 l_token = tokenbuf + nsize - 5; \ 86 s_token = tokenbuf + 1; \ 87 } 88 89 char *labbuf; /* buffer for label */ 90 char *s_lab; /* start ... */ 91 char *e_lab; /* .. and end of stored label */ 92 char *l_lab; /* limit of label buffer */ 93 94 char *codebuf; /* buffer for code section */ 95 char *s_code; /* start ... */ 96 char *e_code; /* .. and end of stored code */ 97 char *l_code; /* limit of code section */ 98 99 char *combuf; /* buffer for comments */ 100 char *s_com; /* start ... */ 101 char *e_com; /* ... and end of stored comments */ 102 char *l_com; /* limit of comment buffer */ 103 104 #define token s_token 105 char *tokenbuf; /* the last token scanned */ 106 char *s_token; 107 char *e_token; 108 char *l_token; 109 110 char *in_buffer; /* input buffer */ 111 char *in_buffer_limit; /* the end of the input buffer */ 112 char *buf_ptr; /* ptr to next character to be taken from 113 * in_buffer */ 114 char *buf_end; /* ptr to first after last char in in_buffer */ 115 116 char save_com[sc_size]; /* input text is saved here when looking for 117 * the brace after an if, while, etc */ 118 char *sc_end; /* pointer into save_com buffer */ 119 120 char *bp_save; /* saved value of buf_ptr when taking input 121 * from save_com */ 122 char *be_save; /* similarly saved value of buf_end */ 123 124 125 int pointer_as_binop; 126 int blanklines_after_declarations; 127 int blanklines_before_blockcomments; 128 int blanklines_after_procs; 129 int blanklines_around_conditional_compilation; 130 int swallow_optional_blanklines; 131 int n_real_blanklines; 132 int prefix_blankline_requested; 133 int postfix_blankline_requested; 134 int break_comma; /* when true and not in parens, break after a 135 * comma */ 136 int btype_2; /* when true, brace should be on same line as 137 * if, while, etc */ 138 float case_ind; /* indentation level to be used for a "case 139 * n:" */ 140 int code_lines; /* count of lines with code */ 141 int had_eof; /* set to true when input is exhausted */ 142 int line_no; /* the current line number. */ 143 int max_col; /* the maximum allowable line length */ 144 int verbose; /* when true, non-essential error messages are 145 * printed */ 146 int cuddle_else; /* true if else should cuddle up to '}' */ 147 int star_comment_cont; /* true iff comment continuation lines should 148 * have stars at the beginning of each line. */ 149 int comment_delimiter_on_blankline; 150 int troff; /* true iff were generating troff input */ 151 int procnames_start_line; /* if true, the names of procedures 152 * being defined get placed in column 153 * 1 (ie. a newline is placed between 154 * the type of the procedure and its 155 * name) */ 156 int proc_calls_space; /* If true, procedure calls look like: 157 * foo(bar) rather than foo (bar) */ 158 int format_col1_comments; /* If comments which start in column 1 159 * are to be magically reformatted 160 * (just like comments that begin in 161 * later columns) */ 162 int inhibit_formatting; /* true if INDENT OFF is in effect */ 163 int suppress_blanklines;/* set iff following blanklines should be 164 * suppressed */ 165 int continuation_indent;/* set to the indentation between the edge of 166 * code and continuation lines */ 167 int lineup_to_parens; /* if true, continued code within parens will 168 * be lined up to the open paren */ 169 int Bill_Shannon; /* true iff a blank should always be inserted 170 * after sizeof */ 171 int blanklines_after_declarations_at_proctop; /* This is vaguely 172 * similar to 173 * blanklines_after_decla 174 * rations except that 175 * it only applies to 176 * the first set of 177 * declarations in a 178 * procedure (just after 179 * the first '{') and it 180 * causes a blank line 181 * to be generated even 182 * if there are no 183 * declarations */ 184 int block_comment_max_col; 185 int extra_expression_indent; /* True if continuation lines from the 186 * expression part of "if(e)", 187 * "while(e)", "for(e;e;e)" should be 188 * indented an extra tab stop so that 189 * they don't conflict with the code 190 * that follows */ 191 192 /* -troff font state information */ 193 194 struct fstate { 195 char font[4]; 196 char size; 197 int allcaps:1; 198 }; 199 char *chfont(); 200 201 struct fstate 202 keywordf, /* keyword font */ 203 stringf, /* string font */ 204 boxcomf, /* Box comment font */ 205 blkcomf, /* Block comment font */ 206 scomf, /* Same line comment font */ 207 bodyf; /* major body font */ 208 209 210 #define STACKSIZE 150 211 212 struct parser_state { 213 int last_token; 214 struct fstate cfont; /* Current font */ 215 int p_stack[STACKSIZE]; /* this is the parsers stack */ 216 int il[STACKSIZE]; /* this stack stores indentation levels */ 217 float cstk[STACKSIZE];/* used to store case stmt indentation levels */ 218 int box_com; /* set to true when we are in a "boxed" 219 * comment. In that case, the first non-blank 220 * char should be lined up with the / in /* */ 221 int comment_delta, 222 n_comment_delta; 223 int cast_mask; /* indicates which close parens close off 224 * casts */ 225 int sizeof_mask; /* indicates which close parens close off 226 * sizeof''s */ 227 int block_init; /* true iff inside a block initialization */ 228 int block_init_level; /* The level of brace nesting in an 229 * initialization */ 230 int last_nl; /* this is true if the last thing scanned was 231 * a newline */ 232 int in_or_st; /* Will be true iff there has been a 233 * declarator (e.g. int or char) and no left 234 * paren since the last semicolon. When true, 235 * a '{' is starting a structure definition or 236 * an initialization list */ 237 int bl_line; /* set to 1 by dump_line if the line is blank */ 238 int col_1; /* set to true if the last token started in 239 * column 1 */ 240 int com_col; /* this is the column in which the current 241 * coment should start */ 242 int com_ind; /* the column in which comments to the right 243 * of code should start */ 244 int com_lines; /* the number of lines with comments, set by 245 * dump_line */ 246 int dec_nest; /* current nesting level for structure or init */ 247 int decl_com_ind; /* the column in which comments after 248 * declarations should be put */ 249 int decl_on_line; /* set to true if this line of code has part 250 * of a declaration on it */ 251 int i_l_follow; /* the level to which ind_level should be set 252 * after the current line is printed */ 253 int in_decl; /* set to true when we are in a declaration 254 * stmt. The processing of braces is then 255 * slightly different */ 256 int in_stmt; /* set to 1 while in a stmt */ 257 int ind_level; /* the current indentation level */ 258 int ind_size; /* the size of one indentation level */ 259 int ind_stmt; /* set to 1 if next line should have an extra 260 * indentation level because we are in the 261 * middle of a stmt */ 262 int last_u_d; /* set to true after scanning a token which 263 * forces a following operator to be unary */ 264 int leave_comma; /* if true, never break declarations after 265 * commas */ 266 int ljust_decl; /* true if declarations should be left 267 * justified */ 268 int out_coms; /* the number of comments processed, set by 269 * pr_comment */ 270 int out_lines; /* the number of lines written, set by 271 * dump_line */ 272 int p_l_follow; /* used to remember how to indent following 273 * statement */ 274 int paren_level; /* parenthesization level. used to indent 275 * within stmts */ 276 short paren_indents[20]; /* column positions of each paren */ 277 int pcase; /* set to 1 if the current line label is a 278 * case. It is printed differently from a 279 * regular label */ 280 int search_brace; /* set to true by parse when it is necessary 281 * to buffer up all info up to the start of a 282 * stmt after an if, while, etc */ 283 int unindent_displace; /* comments not to the right of code 284 * will be placed this many 285 * indentation levels to the left of 286 * code */ 287 int use_ff; /* set to one if the current line should be 288 * terminated with a form feed */ 289 int want_blank; /* set to true when the following token should 290 * be prefixed by a blank. (Said prefixing is 291 * ignored in some cases.) */ 292 int else_if; /* True iff else if pairs should be handled 293 * specially */ 294 int decl_indent; /* column to indent declared identifiers to */ 295 int its_a_keyword; 296 int sizeof_keyword; 297 int dumped_decl_indent; 298 float case_indent; /* The distance to indent case labels from the 299 * switch statement */ 300 int in_parameter_declaration; 301 int indent_parameters; 302 int tos; /* pointer to top of stack */ 303 char procname[100]; /* The name of the current procedure */ 304 int just_saw_decl; 305 } ps; 306 307 int ifdef_level; 308 int rparen_count; 309 struct parser_state state_stack[5]; 310 struct parser_state match_state[5]; 311