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