home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / indent / indent.h < prev    next >
C/C++ Source or Header  |  1999-06-02  |  15KB  |  407 lines

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