home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / utilities / utilsf / indent / Source / c / indntglobs < prev    next >
Encoding:
Text File  |  1995-10-30  |  12.0 KB  |  297 lines

  1. /*
  2.  * Copyright 1989 Object Design, Inc.
  3.  * Copyright (c) 1985 Sun Microsystems, Inc.
  4.  * Copyright (c) 1980 The Regents of the University of California.
  5.  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
  6.  * All rights reserved.
  7.  *
  8.  * Redistribution and use in source and binary forms are permitted
  9.  * provided that the above copyright notice and this paragraph are
  10.  * duplicated in all such forms and that any documentation,
  11.  * advertising materials, and other materials related to such
  12.  * distribution and use acknowledge that the software was developed
  13.  * by the University of California, Berkeley, the University of Illinois,
  14.  * Urbana, and Sun Microsystems, Inc.  The name of either University
  15.  * or Sun Microsystems may not be used to endorse or promote products
  16.  * derived from this software without specific prior written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  19.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  *
  21.  *    @(#)indent_globs.h    5.7 (Berkeley) 9/15/88
  22.  */
  23.  
  24. /* Archimedes port by Steven Flintham
  25.    Wednesday 25th October 1995
  26.    Thursday 26th October 1995
  27.    Friday 27th October 1995
  28.    Saturday 28th October 1995
  29.    Monday 30th October 1995 */
  30.  
  31. /* SF: this is (almost) the original h.indntglobs file. See the accompanying
  32.    documentation for more details. */
  33.  
  34. #include <stdio.h>
  35. #ifndef __riscos /* SF */
  36. #include <memory.h>        /* gst: 3/24/89 */
  37. #endif /* __riscos */
  38.  
  39. #define BACKSLASH '\\'
  40. #define bufsize 200        /* size of internal buffers */
  41. #define inp_bufs 600        /* size of input buffer */
  42. #define sc_size 5000        /* size of save_com buffer */
  43. #define label_offset 2        /* number of levels a label is placed to left
  44.                  * of code */
  45. #define cplus_ppp_indent 2      /* spaces to indent public, private, protected in c++ */
  46.  
  47. #define tabsize 8        /* the size of a tab */
  48. #define tabmask 0177770        /* mask used when figuring length of lines
  49.                  * with tabs */
  50.  
  51.  
  52. #define false 0
  53. #define true  1
  54.  
  55.  
  56. FILE       *input;        /* the fid for the input file */
  57. FILE       *output;        /* the output file */
  58.  
  59. /* SF: changed to use ## */
  60. #define check_size(name) \
  61.     if (e_##name >= l_##name) { \
  62.         register nsize = l_##name-s_##name+400; \
  63.         name##buf = (char *) realloc(name##buf, nsize); \
  64.         e_##name = name##buf + (e_##name-s_##name) + 1; \
  65.         l_##name = name##buf + nsize - 5; \
  66.         s_##name = name##buf + 1; \
  67.     }
  68.  
  69. char       *labbuf;        /* buffer for label */
  70. char       *s_lab;        /* start ... */
  71. char       *e_lab;        /* .. and end of stored label */
  72. char       *l_lab;        /* limit of label buffer */
  73.  
  74. char       *codebuf;        /* buffer for code section */
  75. char       *s_code;        /* start ... */
  76. char       *e_code;        /* .. and end of stored code */
  77. char       *l_code;        /* limit of code section */
  78.  
  79. char       *combuf;        /* buffer for comments */
  80. char       *s_com;        /* start ... */
  81. char       *e_com;        /* ... and end of stored comments */
  82. char       *l_com;        /* limit of comment buffer */
  83.  
  84. char        in_buffer[inp_bufs];/* input buffer */
  85. char       *buf_ptr;        /* ptr to next character to be taken from
  86.                  * in_buffer */
  87. char       *buf_end;        /* ptr to first after last char in in_buffer */
  88.  
  89. char        save_com[sc_size];    /* input text is saved here when looking for
  90.                  * the brace after an if, while, etc */
  91. char       *sc_end;        /* pointer into save_com buffer */
  92.  
  93. char       *bp_save;        /* saved value of buf_ptr when taking input
  94.                  * from save_com */
  95. char       *be_save;        /* similarly saved value of buf_end */
  96.  
  97. char        token[bufsize];    /* the last token scanned */
  98.  
  99. int         cplus;        /* c++ */
  100. int         pointer_as_binop;
  101. int         blanklines_after_declarations;
  102. int         blanklines_before_blockcomments;
  103. int         blanklines_after_procs;
  104. int         blanklines_around_conditional_compilation;
  105. int         swallow_optional_blanklines;
  106. int         n_real_blanklines;
  107. int         prefix_blankline_requested;
  108. int         postfix_blankline_requested;
  109. int         break_comma;    /* when true and not in parens, break after a
  110.                  * comma */
  111. int         btype_2;        /* when true, brace should be on same line as
  112.                  * if, while, etc */
  113. int         btype_3;        /* when true, braces are not only on the next
  114.                  * line but indented with the enclosed code */
  115. float       case_ind;        /* indentation level to be used for a "case
  116.                  * n:" */
  117. int         code_lines;        /* count of lines with code */
  118. int         had_eof;        /* set to true when input is exhausted */
  119. int         line_no;        /* the current line number. */
  120. int         max_col;        /* the maximum allowable line length */
  121. int         verbose;        /* when true, non-essential error messages are
  122.                  * printed */
  123. int         cuddle_else;    /* true if else should cuddle up to '}' */
  124. int         star_comment_cont;    /* true iff comment continuation lines should
  125.                  * have stars at the beginning of each line. */
  126. int         comment_delimiter_on_blankline;
  127. int         troff;        /* true iff were generating troff input */
  128. int         procnames_start_line;    /* if true, the names of procedures
  129.                      * being defined get placed in column
  130.                      * 1 (ie. a newline is placed between
  131.                      * the type of the procedure and its
  132.                      * name) */
  133. int         proc_calls_space;    /* If true, procedure calls look like:
  134.                  * foo(bar) rather than foo (bar) */
  135. int         parens_space;    /* If true, parens gets spaces inside them */
  136. int         format_col1_comments;    /* If comments which start in column 1
  137.                      * are to be magically reformatted
  138.                      * (just like comments that begin in
  139.                      * later columns) */
  140. int         inhibit_formatting;    /* true if INDENT OFF is in effect */
  141. int         suppress_blanklines;/* set iff following blanklines should be
  142.                  * suppressed */
  143. int         continuation_indent;/* set to the indentation between the edge of
  144.                  * code and continuation lines */
  145. int         lineup_to_parens;    /* if true, continued code within parens will
  146.                  * be lined up to the open paren */
  147. int         Bill_Shannon;    /* true iff a blank should always be inserted
  148.                  * after sizeof */
  149. int         blanklines_after_declarations_at_proctop;    /* This is vaguely
  150.                              * similar to
  151.                              * blanklines_after_decla
  152.                              * rations except that
  153.                              * it only applies to
  154.                              * the first set of
  155.                              * declarations in a
  156.                              * procedure (just after
  157.                              * the first '{') and it
  158.                              * causes a blank line
  159.                              * to be generated even
  160.                              * if there are no
  161.                              * declarations */
  162. int         block_comment_max_col;
  163. int         extra_expression_indent;    /* True if continuation lines from the
  164.                      * expression part of "if(e)",
  165.                      * "while(e)", "for(e;e;e)" should be
  166.                      * indented an extra tab stop so that
  167.                      * they don't conflict with the code
  168.                      * that follows */
  169.  
  170. /* -troff font state information */
  171.  
  172. struct fstate {
  173.     char        font[4];
  174.     char        size;
  175.     int         allcaps:1;
  176. };
  177. char       *chfont();
  178.  
  179. struct fstate
  180.             keywordf,        /* keyword font */
  181.             stringf,        /* string font */
  182.             boxcomf,        /* Box comment font */
  183.             blkcomf,        /* Block comment font */
  184.             scomf,        /* Same line comment font */
  185.             bodyf;        /* major body font */
  186.  
  187.  
  188. #define STACKSIZE 150
  189.  
  190. struct parser_state {
  191.     int         last_token;
  192.     struct fstate cfont;    /* Current font */
  193.     int         p_stack[STACKSIZE];    /* this is the parsers stack */
  194.     int         il[STACKSIZE];    /* this stack stores indentation levels */
  195.     float       cstk[STACKSIZE];/* used to store case stmt indentation levels */
  196.     int         box_com;    /* set to true when we are in a "boxed"
  197.                  * comment. In that case, the first non-blank
  198.                  * char should be lined up with the / in /* */
  199.     int         comment_delta,
  200.                 n_comment_delta;
  201.     int         cast_mask;    /* indicates which close parens close off
  202.                  * casts */
  203.     int         sizeof_mask;    /* indicates which close parens close off
  204.                  * sizeof''s */
  205.     int         block_init;    /* true iff inside a block initialization */
  206.     int         block_init_level;    /* The level of brace nesting in an
  207.                      * initialization */
  208.     int         last_nl;    /* this is true if the last thing scanned was
  209.                  * a newline */
  210.     int         in_or_st;    /* Will be true iff there has been a
  211.                  * declarator (e.g. int or char) and no left
  212.                  * paren since the last semicolon. When true,
  213.                  * a '{' is starting a structure definition or
  214.                  * an initialization list */
  215.     int         bl_line;    /* set to 1 by dump_line if the line is blank */
  216.     int         col_1;        /* set to true if the last token started in
  217.                  * column 1 */
  218.     int         com_col;    /* this is the column in which the current
  219.                  * coment should start */
  220.     int         com_ind;    /* the column in which comments to the right
  221.                  * of code should start */
  222.     int         com_lines;    /* the number of lines with comments, set by
  223.                  * dump_line */
  224.     int         dec_nest;    /* current nesting level for structure or init */
  225.     int         decl_com_ind;    /* the column in which comments after
  226.                  * declarations should be put */
  227.     int         decl_on_line;    /* set to true if this line of code has part
  228.                  * of a declaration on it */
  229.     int         i_l_follow;    /* the level to which ind_level should be set
  230.                  * after the current line is printed */
  231.     int         in_decl;    /* set to true when we are in a declaration
  232.                  * stmt.  The processing of braces is then
  233.                  * slightly different */
  234.     int         in_stmt;    /* set to 1 while in a stmt */
  235.     int         ind_level;    /* the current indentation level */
  236.     int         ind_size;    /* the size of one indentation level */
  237.     int         ind_stmt;    /* set to 1 if next line should have an extra
  238.                  * indentation level because we are in the
  239.                  * middle of a stmt */
  240.     int         last_u_d;    /* set to true after scanning a token which
  241.                  * forces a following operator to be unary */
  242.     int         leave_comma;    /* if true, never break declarations after
  243.                  * commas */
  244.     int         ljust_decl;    /* true if declarations should be left
  245.                  * justified */
  246.     int         out_coms;    /* the number of comments processed, set by
  247.                  * pr_comment */
  248.     int         out_lines;    /* the number of lines written, set by
  249.                  * dump_line */
  250.     int         p_l_follow;    /* used to remember how to indent following
  251.                  * statement */
  252.     int         paren_level;    /* parenthesization level. used to indent
  253.                  * within stmts */
  254.     short       paren_indents[20];    /* column positions of each paren */
  255.     int         pcase;        /* set to 1 if the current line label is a
  256.                  * case.  It is printed differently from a
  257.                  * regular label */
  258.     int         search_brace;    /* set to true by parse when it is necessary
  259.                  * to buffer up all info up to the start of a
  260.                  * stmt after an if, while, etc */
  261.     int         unindent_displace;    /* comments not to the right of code
  262.                      * will be placed this many
  263.                      * indentation levels to the left of
  264.                      * code */
  265.     int         use_ff;        /* set to one if the current line should be
  266.                  * terminated with a form feed */
  267.     int         want_blank;    /* set to true when the following token should
  268.                  * be prefixed by a blank. (Said prefixing is
  269.                  * ignored in some cases.) */
  270.     int         else_if;    /* True iff else if pairs should be handled
  271.                  * specially */
  272.     int         decl_indent;    /* column to indent declared identifiers to */
  273.     int         its_a_keyword;
  274.     int         sizeof_keyword;
  275.     int         dumped_decl_indent;
  276.     float       case_indent;    /* The distance to indent case labels from the
  277.                  * switch statement */
  278.     float       case_code_indent;    /* The distance to indent case code
  279.                      * from the case label */
  280.     int         in_parameter_declaration;
  281.     int         indent_parameters;
  282.     int         tos;        /* pointer to top of stack */
  283.     char        procname[100];    /* The name of the current procedure */
  284.     int         just_saw_decl;
  285.     int        cc_comment;    /* saw a C++ comment ("//...") */
  286. }           ps;
  287.  
  288. int         ifdef_level;
  289. struct parser_state state_stack[5];
  290. struct parser_state match_state[5];
  291.  
  292.  typedef enum cplus_flag {
  293.      c_only,
  294.      c_and_cplus,
  295.      cplus_only
  296. } cplus_flag;
  297.