1 /* Copyright (c) 1992, Free Software Foundation, Inc. All rights reserved. 2 3 Copyright (c) 1985 Sun Microsystems, Inc. Copyright (c) 1980 The Regents 4 of the University of California. Copyright (c) 1976 Board of Trustees of 5 the University of Illinois. All rights reserved. 6 7 Redistribution and use in source and binary forms are permitted 8 provided that 9 the above copyright notice and this paragraph are duplicated in all such 10 forms and that any documentation, advertising materials, and other 11 materials related to such distribution and use acknowledge that the 12 software was developed by the University of California, Berkeley, the 13 University of Illinois, Urbana, and Sun Microsystems, Inc. The name of 14 either University or Sun Microsystems may not be used to endorse or 15 promote products derived from this software without specific prior written 16 permission. THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 17 IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES 18 OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 19 20 @(#)indent_globs.h 5.7 (Berkeley) 9/15/88 */ 21 22 #ifdef DEBUG 23 extern int debug; 24 #endif 25 26 #include <stdio.h> 27 #if 0 28 /* Some systems don't have string.h. If there are any places where indent 29 depends on the return type of strcpy and friends, declare the return type 30 (e.g. "char *strcpy();"). */ 31 #include <string.h> 32 #endif /* 0 */ 33 34 35 struct file_buffer 36 { 37 char *name; 38 int size; 39 char *data; 40 }; 41 42 extern struct file_buffer *read_file (), *read_stdin (); 43 extern void make_backup (); 44 45 /* Standard memory allocation routines. */ 46 char *malloc (); 47 char *realloc (); 48 /* Do the same thing, but abort with an error if out of memory (see globs.c). */ 49 char *xmalloc (); 50 char *xrealloc (); 51 52 char *memcpy (); 53 54 #define BACKSLASH '\\' 55 #define label_offset 2 /* number of levels a label is placed to left 56 of code */ 57 /* Initial size of internal buffers (they are extended as necessary). */ 58 #define bufsize 1000 59 60 enum codes 61 { 62 code_eof = 0, /* end of file */ 63 newline, 64 lparen, /* '(' or '['. Also '{' in an 65 initialization. */ 66 rparen, /* ')' or ']'. Also '}' in an 67 initialization. */ 68 unary_op, binary_op, postop, 69 question, casestmt, colon, semicolon, lbrace, rbrace, 70 ident, /* string or char literal, identifier, number */ 71 comma, comment, swstmt, 72 preesc, /* '#'. */ 73 form_feed, decl, 74 sp_paren, /* if, for, or while token */ 75 sp_nparen, ifstmt, whilestmt, 76 forstmt, stmt, stmtl, elselit, dolit, dohead, dostmt, ifhead, 77 elsehead, period 78 }; 79 80 enum rwcodes 81 { 82 rw_none, 83 rw_break, 84 rw_switch, 85 rw_case, 86 rw_struct_like, /* struct, enum, union */ 87 rw_decl, 88 rw_sp_paren, /* if, while, for */ 89 rw_sp_nparen, /* do, else */ 90 rw_sizeof, 91 rw_return 92 }; 93 94 #define false 0 95 #define true 1 96 97 /* Name of input file. */ 98 extern char *in_name; 99 100 extern char *in_prog; /* pointer to the null-terminated input 101 program */ 102 103 /* Point to the position in the input program which we are currently looking 104 at. */ 105 extern char *in_prog_pos; 106 107 /* Point to the start of the current line. */ 108 extern char *cur_line; 109 110 /* Size of the input program, not including the ' \n\0' we add at the end */ 111 extern unsigned int in_prog_size; 112 113 extern FILE *output; /* the output file */ 114 115 #define check_code_size \ 116 if (e_code >= l_code) { \ 117 register nsize = l_code-s_code+400; \ 118 codebuf = (char *) realloc(codebuf, nsize); \ 119 e_code = codebuf + (e_code-s_code) + 1; \ 120 l_code = codebuf + nsize - 5; \ 121 s_code = codebuf + 1; \ 122 } 123 124 #define check_com_size \ 125 if (e_com >= l_com) { \ 126 register nsize = l_com-s_com+400; \ 127 combuf = (char *) realloc(combuf, nsize); \ 128 e_com = combuf + (e_com-s_com) + 1; \ 129 l_com = combuf + nsize - 5; \ 130 s_com = combuf + 1; \ 131 } 132 133 #define check_lab_size \ 134 if (e_lab >= l_lab) { \ 135 register nsize = l_lab-s_lab+400; \ 136 labbuf = (char *) realloc(labbuf, nsize); \ 137 e_lab = labbuf + (e_lab-s_lab) + 1; \ 138 l_lab = labbuf + nsize - 5; \ 139 s_lab = labbuf + 1; \ 140 } 141 142 extern char *labbuf; /* buffer for label */ 143 extern char *s_lab; /* start ... */ 144 extern char *e_lab; /* .. and end of stored label */ 145 extern char *l_lab; /* limit of label buffer */ 146 147 extern char *codebuf; /* buffer for code section */ 148 extern char *s_code; /* start ... */ 149 extern char *e_code; /* .. and end of stored code */ 150 extern char *l_code; /* limit of code section */ 151 152 extern char *combuf; /* buffer for comments */ 153 extern char *s_com; /* start ... */ 154 extern char *e_com; /* ... and end of stored comments */ 155 extern char *l_com; /* limit of comment buffer */ 156 157 extern char *buf_ptr; /* ptr to next character to be taken from 158 in_buffer */ 159 extern char *buf_end; /* ptr to first after last char in in_buffer */ 160 161 /* pointer to the token that lexi() has just found */ 162 extern char *token; 163 /* points to the first char after the end of token */ 164 extern char *token_end; 165 /* Functions from lexi.c */ 166 enum codes lexi (); 167 168 /* Used to keep track of buffers. */ 169 struct buf 170 { 171 char *ptr; /* points to the start of the buffer */ 172 char *end; /* points to the character beyond the last 173 one (e.g. is equal to ptr if the buffer is 174 empty). */ 175 int size; /* how many chars are currently allocated. */ 176 }; 177 178 /* Insure that BUFSTRUC has at least REQ more chars left, if not extend it. 179 Note: This may change bufstruc.ptr. */ 180 #define need_chars(bufstruc, req) \ 181 if ((bufstruc.end - bufstruc.ptr + (req)) >= bufstruc.size) \ 182 {\ 183 int cur_chars = bufstruc.end - bufstruc.ptr;\ 184 bufstruc.size *= 2;\ 185 bufstruc.ptr = xrealloc(bufstruc.ptr,bufstruc.size);\ 186 bufstruc.end = bufstruc.ptr + cur_chars;\ 187 } 188 189 /* Buffer in which to save a comment which occurs between an if(), while(), 190 etc., and the statement following it. Note: the fact that we point into 191 this buffer, and that we might realloc() it (via the need_chars macro) is 192 a bad thing (since when the buffer is realloc'd its address might change, 193 making any pointers into it point to garbage), but since the filling of 194 the buffer (hence the need_chars) and the using of the buffer (where 195 buf_ptr points into it) occur at different times, we can get away with it 196 (it would not be trivial to fix). */ 197 extern struct buf save_com; 198 199 extern char *bp_save; /* saved value of buf_ptr when taking input 200 from save_com */ 201 extern char *be_save; /* similarly saved value of buf_end */ 202 203 204 extern int pointer_as_binop; 205 extern int blanklines_after_declarations; 206 extern int blanklines_before_blockcomments; 207 extern int blanklines_after_procs; 208 extern int blanklines_around_conditional_compilation; 209 extern int swallow_optional_blanklines; 210 extern int n_real_blanklines; 211 extern int prefix_blankline_requested; 212 extern int postfix_blankline_requested; 213 extern int break_comma; /* when true and not in parens, break after a 214 comma */ 215 216 extern int found_err; /* flag set in diag() on error */ 217 218 219 extern int else_or_endif; 220 extern int di_stack_alloc; 221 extern int *di_stack; 222 223 /* number of spaces to indent braces from the suround if, while, etc. in -bl 224 (bype_2 == 0) code */ 225 extern int brace_indent; 226 227 extern int btype_2; /* when true, brace should be on same line as 228 if, while, etc */ 229 /* If true, a space is inserted between if, while, or for, and a semicolon 230 for example while (*p++ == ' ') ; */ 231 extern int space_sp_semicolon; 232 233 /* True if a #else or #endif has been encountered. */ 234 extern int else_or_endif; 235 236 extern int case_ind; /* indentation level to be used for a "case 237 n:" in spaces */ 238 239 extern int code_lines; /* count of lines with code */ 240 /* the number of comments processed, set by pr_comment. */ 241 extern int out_coms; 242 extern int out_lines; /* the number of lines written, set by 243 dump_line */ 244 extern int com_lines; /* the number of lines with comments, set by 245 dump_line */ 246 247 248 extern int had_eof; /* set to true when input is exhausted */ 249 extern int line_no; /* the current line number. */ 250 251 extern int max_col; /* the maximum allowable line length */ 252 extern int verbose; /* when true, non-essential error messages 253 are printed */ 254 extern int cuddle_else; /* true if else should cuddle up to '}' */ 255 extern int star_comment_cont; /* true iff comment continuation lines should 256 have stars at the beginning of each line. */ 257 extern int comment_delimiter_on_blankline; 258 extern int troff; /* true iff were generating troff input */ 259 extern int procnames_start_line;/* if true, the names of procedures being 260 defined get placed in column 1 (ie. a 261 newline is placed between the type of the 262 procedure and its name) */ 263 extern int expect_output_file; /* Means "-o" was specified. */ 264 extern int proc_calls_space; /* If true, procedure calls look like: foo 265 (bar) rather than foo(bar) */ 266 extern int cast_space; /* If true, casts look like: r * 267 (char *) bar rather than (char *)bar */ 268 269 /* If comments which start in column 1 are to be magically reformatted */ 270 extern int format_col1_comments; 271 /* If any comments are to be reformatted */ 272 extern int format_comments; 273 274 extern int suppress_blanklines; /* set iff following blanklines should be 275 suppressed */ 276 extern int continuation_indent; /* set to the indentation between the edge of 277 code and continuation lines in spaces */ 278 extern int lineup_to_parens; /* if true, continued code within parens will 279 be lined up to the open paren */ 280 281 /* The position that we will line the current line up with when it comes time 282 to print it (if we are lining up to parentheses). */ 283 extern int paren_target; 284 285 /* true iff a blank should always be inserted after sizeof */ 286 extern int Bill_Shannon; 287 288 extern int blanklines_after_declarations_at_proctop; /* This is vaguely 289 similar to 290 blanklines_after_decla 291 rations except that 292 it only applies to 293 the first set of 294 declarations in a 295 procedure (just after 296 the first '{') and it 297 causes a blank line 298 to be generated even 299 if there are no 300 declarations */ 301 extern int block_comment_max_col; 302 extern int extra_expression_indent; /* True if continuation lines from 303 the expression part of "if(e)", 304 "while(e)", "for(e;e;e)" should be 305 indented an extra tab stop so that 306 they don't conflict with the code 307 that follows */ 308 309 /* The following are all controlled by command line switches (as are some of 310 the things above). */ 311 extern int leave_comma; /* if true, never break declarations after 312 commas */ 313 extern int decl_com_ind; /* the column in which comments after 314 declarations should be put */ 315 extern int case_indent; /* The distance to indent case labels from 316 the switch statement */ 317 extern int com_ind; /* the column in which comments to the right 318 of code should start */ 319 extern int decl_indent; /* column to indent declared identifiers to */ 320 extern int ljust_decl; /* true if declarations should be left 321 justified */ 322 extern int unindent_displace; /* comments not to the right of code will be 323 placed this many indentation levels to the 324 left of code */ 325 extern int else_if; /* True iff else if pairs should be handled 326 specially */ 327 /* Number of spaces to indent parameters. */ 328 extern int indent_parameters; 329 /* The size of one indentation level in spaces. */ 330 extern int ind_size; 331 /* The number of columns a tab character generates. */ 332 extern int tabsize; 333 /* Nonzero if we should use standard input/output when files are not 334 explicitly specified. */ 335 extern int use_stdinout; 336 337 /* -troff font state information */ 338 339 struct fstate 340 { 341 char font[4]; 342 char size; 343 int allcaps:1; 344 }; 345 char *chfont (); 346 347 extern struct fstate 348 keywordf, /* keyword font */ 349 stringf, /* string font */ 350 boxcomf, /* Box comment font */ 351 blkcomf, /* Block comment font */ 352 scomf, /* Same line comment font */ 353 bodyf; /* major body font */ 354 355 /* This structure contains information relating to the state of parsing the 356 code. The difference is that the state is saved on #if and restored on 357 #else. */ 358 struct parser_state 359 { 360 struct parser_state *next; 361 enum codes last_token; 362 struct fstate cfont; /* Current font */ 363 364 /* This is the parsers stack, and the current allocated size. */ 365 enum codes *p_stack; 366 int p_stack_size; 367 368 /* This stack stores indentation levels */ 369 /* Currently allocated size is stored in p_stack_size. */ 370 int *il; 371 372 /* If the last token was an ident and is a reserved word, 373 remember the type. */ 374 enum rwcodes last_rw; 375 376 /* Used to store case stmt indentation levels. */ 377 /* Currently allocated size is stored in p_stack_size. */ 378 int *cstk; 379 380 /* Pointer to the top of stack of the p_stack, il and cstk arrays. */ 381 int tos; 382 383 int box_com; /* set to true when we are in a "boxed" 384 comment. In that case, the first non-blank 385 char should be lined up with the / in /* */ 386 /* Shift comments by this many columns. */ 387 int comment_delta; 388 /* Value of comment_delta for the following line. */ 389 int n_comment_delta; 390 391 int cast_mask; /* indicates which close parens close off 392 casts */ 393 /* A bit for each paren level, set if the open paren was in a context which 394 indicates that this pair of parentheses is not a cast. */ 395 int noncast_mask; 396 397 int sizeof_mask; /* indicates which close parens close off 398 sizeof''s */ 399 int block_init; /* true iff inside a block initialization */ 400 int block_init_level; /* The level of brace nesting in an 401 initialization */ 402 int last_nl; /* this is true if the last thing scanned was 403 a newline */ 404 int in_or_st; /* Will be true iff there has been a 405 declarator (e.g. int or char) and no left 406 paren since the last semicolon. When true, 407 a '{' is starting a structure definition 408 or an initialization list */ 409 int bl_line; /* set to 1 by dump_line if the line is 410 blank */ 411 int col_1; /* set to true if the last token started in 412 column 1 */ 413 int com_col; /* this is the column in which the current 414 coment should start */ 415 int dec_nest; /* current nesting level for structure or 416 init */ 417 int decl_on_line; /* set to true if this line of code has part 418 of a declaration on it */ 419 int i_l_follow; /* the level in spaces to which ind_level 420 should be set after the current line is 421 printed */ 422 int in_decl; /* set to true when we are in a declaration 423 stmt. The processing of braces is then 424 slightly different */ 425 int in_stmt; /* set to 1 while in a stmt */ 426 int ind_level; /* the current indentation level in spaces */ 427 int ind_stmt; /* set to 1 if next line should have an extra 428 indentation level because we are in the 429 middle of a stmt */ 430 int last_u_d; /* set to true after scanning a token which 431 forces a following operator to be unary */ 432 int p_l_follow; /* used to remember how to indent following 433 statement */ 434 int paren_level; /* parenthesization level. used to indent 435 within stmts */ 436 int paren_depth; /* Depth of paren nesting anywhere. */ 437 /* Column positions of paren at each level. If positive, it contains just 438 the number of characters of code on the line up to and including the 439 right parenthesis character. If negative, it contains the opposite of 440 the actual level of indentation in characters (that is, the indentation 441 of the line has been added to the number of characters and the sign has 442 been reversed to indicate that this has been done). */ 443 short *paren_indents; /* column positions of each paren */ 444 int paren_indents_size; /* Currently allocated size. */ 445 446 int pcase; /* set to 1 if the current line label is a 447 case. It is printed differently from a 448 regular label */ 449 int search_brace; /* set to true by parse when it is necessary 450 to buffer up all info up to the start of a 451 stmt after an if, while, etc */ 452 int use_ff; /* set to one if the current line should be 453 terminated with a form feed */ 454 int want_blank; /* set to true when the following token 455 should be prefixed by a blank. (Said 456 prefixing is ignored in some cases.) */ 457 int its_a_keyword; 458 int sizeof_keyword; 459 int dumped_decl_indent; 460 int in_parameter_declaration; 461 char *procname; /* The name of the current procedure */ 462 char *procname_end; /* One char past the last one in procname */ 463 int just_saw_decl; 464 }; 465 466 /* All manipulations of the parser state occur at the top of stack (tos). A 467 stack is kept for conditional compilation (unrelated to the p_stack, il, & 468 cstk stacks)--it is implemented as a linked list via the next field. */ 469 extern struct parser_state *parser_state_tos; 470 471 /* The column in which comments to the right of #else and #endif should 472 start. */ 473 extern int else_endif_col; 474