home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / indent-1.9.1-base.tgz / indent-1.9.1-base.tar / fsf / indent / indent.h < prev    next >
C/C++ Source or Header  |  1994-01-29  |  15KB  |  404 lines

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