home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d7xx / d702 / indent.lha / Indent / Indent.lha / indent-1.4indent.h < prev    next >
C/C++ Source or Header  |  1992-05-06  |  16KB  |  437 lines

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