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