home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d02xx / d0262.lha / Indent / indent_globs.h < prev    next >
C/C++ Source or Header  |  1989-10-31  |  17KB  |  443 lines

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