home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / MPW / indent 1.8 / Orig / indent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-16  |  50.4 KB  |  1,816 lines  |  [TEXT/MPS ]

  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. #include "sys.h"
  21. #include "indent.h"
  22. #include <ctype.h>
  23.  
  24. void
  25. usage ()
  26. {
  27.   fprintf (stderr, "usage: indent file [-o outfile ] [ options ]\n");
  28.   fprintf (stderr, "       indent file1 file2 ... fileN [ options ]\n");
  29.   exit (1);
  30. }
  31.  
  32.  
  33. /* Stuff that needs to be shared with the rest of indent.
  34.    Documented in indent.h.  */
  35. char *labbuf;
  36. char *s_lab;
  37. char *e_lab;
  38. char *l_lab;
  39. char *codebuf;
  40. char *s_code;
  41. char *e_code;
  42. char *l_code;
  43. char *combuf;
  44. char *s_com;
  45. char *e_com;
  46. char *l_com;
  47. struct buf save_com;
  48. char *bp_save;
  49. char *be_save;
  50. int code_lines;
  51. int line_no;
  52. struct fstate keywordf;
  53. struct fstate stringf;
  54. struct fstate boxcomf;
  55. struct fstate blkcomf;
  56. struct fstate scomf;
  57. struct fstate bodyf;
  58. int break_comma;
  59.  
  60. /* Insure that BUFSTRUC has at least REQ more chars left, if not extend it.
  61.       Note:  This may change bufstruc.ptr.  */
  62. #define need_chars(bufstruc, req) \
  63.   if ((bufstruc.end - bufstruc.ptr + (req)) >= bufstruc.size) \
  64. {\
  65.          int cur_chars = bufstruc.end - bufstruc.ptr;\
  66.          bufstruc.size *= 2;\
  67.          bufstruc.ptr = xrealloc (bufstruc.ptr,bufstruc.size);\
  68.          bufstruc.end = bufstruc.ptr + cur_chars;\
  69. }
  70.  
  71. /* True if there is an embedded comment on this code line */
  72. int embedded_comment_on_line;
  73.  
  74. int else_or_endif;
  75.  
  76. /* structure indentation levels */
  77. int *di_stack;
  78.  
  79. /* Currently allocated size of di_stack.  */
  80. int di_stack_alloc;
  81.  
  82. /* when this is positive, we have seen a ? without
  83.    the matching : in a <c>?<s>:<s> construct */
  84. int squest;
  85.  
  86. #define CHECK_CODE_SIZE \
  87.     if (e_code >= l_code) { \
  88.         register nsize = l_code-s_code+400; \
  89.         codebuf = (char *) xrealloc (codebuf, nsize); \
  90.         e_code = codebuf + (e_code-s_code) + 1; \
  91.         l_code = codebuf + nsize - 5; \
  92.         s_code = codebuf + 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 *) xrealloc (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. static void
  105. indent (this_file)
  106.      struct file_buffer *this_file;
  107. {
  108.   register int i;
  109.   enum codes hd_type;
  110.   register char *t_ptr;
  111.   enum codes type_code;
  112.  
  113.   /* current indentation for declarations */
  114.   int dec_ind = 0;
  115.  
  116.   /* used when buffering up comments to remember that
  117.      a newline was passed over */
  118.   int flushed_nl = 0;        
  119.   int force_nl = 0;
  120.  
  121.   /* true when we've just see a case; determines what to do
  122.      with the following colon */
  123.   int scase = 0;
  124.  
  125.   /* true when in the expression part of if(...), while(...), etc. */
  126.   int sp_sw = 0;
  127.  
  128.   /* True if we have just encountered the end of an if (...), etc. (i.e. the
  129.      ')' of the if (...) was the last token).  The variable is set to 2 in
  130.      the middle of the main token reading loop and is decremented at the
  131.      beginning of the loop, so it will reach zero when the second token after
  132.      the ')' is read.  */
  133.   int last_token_ends_sp = 0;
  134.  
  135.   /* true iff last keyword was an else */
  136.   int last_else = 0;
  137.  
  138.  
  139.   in_prog = in_prog_pos = this_file->data;
  140.   in_prog_size = this_file->size;
  141.  
  142.   hd_type = code_eof;
  143.   dec_ind = 0;
  144.   last_token_ends_sp = false;
  145.   last_else = false;
  146.   sp_sw = force_nl = false;
  147.   scase = false;
  148.   squest = false;
  149.  
  150. #if 0
  151.   if (com_ind <= 1)
  152.     com_ind = 2;        /* dont put normal comments before column 2 */
  153. #endif
  154.  
  155.   if (troff)
  156.     {
  157.       if (bodyf.font[0] == 0)
  158.     parsefont (&bodyf, "R");
  159.       if (scomf.font[0] == 0)
  160.     parsefont (&scomf, "I");
  161.       if (blkcomf.font[0] == 0)
  162.     blkcomf = scomf, blkcomf.size += 2;
  163.       if (boxcomf.font[0] == 0)
  164.     boxcomf = blkcomf;
  165.       if (stringf.font[0] == 0)
  166.     parsefont (&stringf, "L");
  167.       if (keywordf.font[0] == 0)
  168.     parsefont (&keywordf, "B");
  169.       writefdef (&bodyf, 'B');
  170.       writefdef (&scomf, 'C');
  171.       writefdef (&blkcomf, 'L');
  172.       writefdef (&boxcomf, 'X');
  173.       writefdef (&stringf, 'S');
  174.       writefdef (&keywordf, 'K');
  175.     }
  176.   if (block_comment_max_col <= 0)
  177.     block_comment_max_col = max_col;
  178.   if (decl_com_ind <= 0)    /* if not specified by user, set this */
  179.     decl_com_ind =
  180.       ljust_decl ? (com_ind <= 10 ? 2 : com_ind - 8) : com_ind;
  181.   if (continuation_indent == 0)
  182.     continuation_indent = ind_size;
  183.   fill_buffer ();        /* get first batch of stuff into input buffer */
  184.  
  185. #if 0
  186.   parse (semicolon);
  187. #endif
  188.  
  189.   {
  190.     register char *p = buf_ptr;
  191.     register col = 1;
  192.  
  193.     while (1)
  194.       {
  195.     if (*p == ' ')
  196.       col++;
  197.     else if (*p == TAB)
  198.       col = tabsize - (col % tabsize) + 1;
  199.     else if (*p == EOL)
  200.       col = 1;
  201.     else
  202.       break;
  203.  
  204.     p++;
  205.       }
  206.  
  207. #if 0
  208.     if (col > ind_size)
  209.       parser_state_tos->ind_level = parser_state_tos->i_l_follow = col;
  210. #endif/
  211.   }
  212.  
  213.   if (troff)
  214.     {
  215.       register char *p = in_name, *beg = in_name;
  216.  
  217.       while (*p)
  218.     if (*p++ == '/')
  219.       beg = p;
  220.       fprintf (output, ".Fn \"%s\"\n", beg);
  221.     }
  222.   /* START OF MAIN LOOP */
  223.  
  224.   while (1)
  225.     {                /* this is the main loop.  it will go until
  226.                    we reach eof */
  227.       int is_procname;
  228.  
  229.       type_code = lexi ();    /* lexi reads one token.  "token" points to
  230.                    the actual characters. lexi returns a code
  231.                    indicating the type of token */
  232.  
  233.       if (last_token_ends_sp > 0)
  234.     last_token_ends_sp--;
  235.       is_procname = parser_state_tos->procname[0];
  236.  
  237.       /* The following code moves everything following an if (), while (),
  238.          else, etc. up to the start of the following stmt to a buffer. This
  239.          allows proper handling of both kinds of brace placement. */
  240.  
  241.       flushed_nl = false;
  242.       while (parser_state_tos->search_brace)
  243.     {
  244.       /* After scanning an if(), while (), etc., it might be necessary to
  245.          keep track of the text between the if() and the start of the
  246.          statement which follows.  Use save_com to do so.  */
  247.  
  248.       switch (type_code)
  249.         {
  250.         case newline:
  251.           ++line_no;
  252.           flushed_nl = true;
  253.         case form_feed:
  254.           break;        /* form feeds and newlines found here will be
  255.                    ignored */
  256.  
  257.         case lbrace:    /* this is a brace that starts the compound
  258.                    stmt */
  259.           if (save_com.end == save_com.ptr)
  260.         {
  261.           /* ignore buffering if a comment wasnt stored up */
  262.           parser_state_tos->search_brace = false;
  263.           goto check_type;
  264.         }
  265.           /* We need to put the '{' back into save_com somewhere.  */
  266.           if (btype_2)
  267.         /* Put it at the beginning, e.g. if (foo) { / * comment here *
  268.            / */
  269.  
  270.         save_com.ptr[0] = '{';
  271.  
  272.           else
  273.         {
  274.           /* Put it at the end, e.g. if (foo) / * comment here * / { */
  275.  
  276.           /* Putting in this newline causes a dump_line to occur
  277.              right after the comment, thus insuring that it will be
  278.              put in the correct column.  */
  279.           *save_com.end++ = EOL;
  280.           *save_com.end++ = '{';
  281.         }
  282.  
  283.           /* Go to common code to get out of this loop.  */
  284.           goto sw_buffer;
  285.  
  286.         case comment:    /* we have a comment, so we must copy it into
  287.                    the buffer */
  288.           if (!flushed_nl || save_com.end != save_com.ptr)
  289.         {
  290.           need_chars (save_com, 10);
  291.           if (save_com.end == save_com.ptr)
  292.             {        /* if this is the first comment, we must set
  293.                    up the buffer */
  294.               save_com.ptr[0] = save_com.ptr[1] = ' ';
  295.               save_com.end = save_com.ptr + 2;
  296.             }
  297.           else
  298.             {
  299.               *save_com.end++ = EOL;    /* add newline between
  300.                            comments */
  301.               *save_com.end++ = ' ';
  302.               --line_no;
  303.             }
  304.           *save_com.end++ = '/';    /* copy in start of comment */
  305.           *save_com.end++ = '*';
  306.  
  307.           for (;;)
  308.             {        /* loop until we get to the end of the
  309.                    comment */
  310.               /* make sure there is room for this character and
  311.                  (while we're at it) the '/' we might add at the end
  312.                  of the loop. */
  313.               need_chars (save_com, 2);
  314.               *save_com.end = *buf_ptr++;
  315.               if (buf_ptr >= buf_end)
  316.             {
  317.               fill_buffer ();
  318.               if (had_eof)
  319.                 {
  320.                   diag (1, "Unclosed comment", 0, 0);
  321.                   exit (1);
  322.                 }
  323.             }
  324.  
  325.               if (*save_com.end++ == '*' && *buf_ptr == '/')
  326.             break;    /* we are at end of comment */
  327.  
  328.             }
  329.           *save_com.end++ = '/';    /* add ending slash */
  330.           if (++buf_ptr >= buf_end)    /* get past / in buffer */
  331.             fill_buffer ();
  332.           break;
  333.         }
  334.         default:        /* it is the start of a normal statment */
  335.           if (flushed_nl)    /* if we flushed a newline, make sure it is
  336.                    put back */
  337.         force_nl = true;
  338.           if ((type_code == sp_paren && *token == 'i'
  339.            && last_else && else_if)
  340.           ||
  341.           (type_code == sp_nparen && *token == 'e'
  342.            && e_code != s_code && e_code[-1] == '}'))
  343.         force_nl = false;
  344.  
  345.           if (save_com.end == save_com.ptr)
  346.         {
  347.           /* ignore buffering if comment wasnt saved up */
  348.           parser_state_tos->search_brace = false;
  349.           goto check_type;
  350.         }
  351.           if (force_nl)
  352.         {        /* if we should insert a nl here, put it into
  353.                    the buffer */
  354.           force_nl = false;
  355.           --line_no;    /* this will be re-increased when the nl is
  356.                    read from the buffer */
  357.           need_chars (save_com, 2);
  358.           *save_com.end++ = EOL;
  359.           *save_com.end++ = ' ';
  360.           if (verbose && !flushed_nl)    /* print error msg if the
  361.                            line was not already
  362.                            broken */
  363.             diag (0, "Line broken", 0, 0);
  364.           flushed_nl = false;
  365.         }
  366.           for (t_ptr = token; t_ptr < token_end; ++t_ptr)
  367.         {
  368.           need_chars (save_com, 1);
  369.           *save_com.end++ = *t_ptr;    /* copy token into temp
  370.                            buffer */
  371.         }
  372.           parser_state_tos->procname = "\0";
  373.  
  374.         sw_buffer:
  375.           parser_state_tos->search_brace = false;    /* stop looking for
  376.                                start of stmt */
  377.           bp_save = buf_ptr;/* save current input buffer */
  378.           be_save = buf_end;
  379.           buf_ptr = save_com.ptr;    /* fix so that subsequent calls to
  380.                        lexi will take tokens out of
  381.                        save_com */
  382.           need_chars (save_com, 1);
  383.           *save_com.end++ = ' ';    /* add trailing blank, just in case */
  384.           buf_end = save_com.end;
  385.           save_com.end = save_com.ptr;    /* make save_com empty */
  386.           break;
  387.         }            /* end of switch */
  388.       /* we must make this check, just in case there was an unexpected
  389.          EOF */
  390.       if (type_code != code_eof)
  391.         type_code = lexi ();/* read another token */
  392.       /* if (parser_state_tos->search_brace)
  393.          parser_state_tos->procname[0] = 0; */
  394.       if ((is_procname = parser_state_tos->procname[0]) && flushed_nl
  395.           && !procnames_start_line && parser_state_tos->in_decl
  396.           && type_code == ident)
  397.         flushed_nl = 0;
  398.     }            /* end of while (search_brace) */
  399.       last_else = 0;
  400.  
  401.     check_type:
  402.       if (type_code == code_eof)
  403.     {            /* we got eof */
  404.       if (s_lab != e_lab || s_code != e_code
  405.           || s_com != e_com)/* must dump end of line */
  406.         dump_line ();
  407.       if (parser_state_tos->tos > 1)    /* check for balanced braces */
  408.         diag (1, "Stuff missing from end of file.", 0, 0);
  409.  
  410.       if (verbose)
  411.         {
  412.           printf ("There were %d output lines and %d comments\n",
  413.               (int) out_lines, (int) out_coms);
  414.           printf ("(Lines with comments)/(Lines with code): %6.3f\n",
  415.               (1.0 * com_lines) / code_lines);
  416.         }
  417.       fflush (output);
  418.       if (found_err)
  419.         exit (found_err);
  420.  
  421.       return;
  422.     }
  423.  
  424.       if ((type_code != comment) &&
  425.       (type_code != cplus_comment) &&
  426.       (type_code != newline) &&
  427.       (type_code != preesc) &&
  428.       (type_code != form_feed))
  429.     {
  430.       if (force_nl &&
  431.           (type_code != semicolon) &&
  432.           (type_code != lbrace || !btype_2))
  433.         {
  434.           /* we should force a broken line here */
  435.           if (verbose && !flushed_nl)
  436.         diag (0, "Line broken", 0, 0);
  437.           flushed_nl = false;
  438.           dump_line ();
  439.           parser_state_tos->want_blank = false;    /* dont insert blank at
  440.                                line start */
  441.           force_nl = false;
  442.         }
  443.       parser_state_tos->in_stmt = true;    /* turn on flag which causes
  444.                            an extra level of
  445.                            indentation. this is
  446.                            turned off by a ; or } */
  447.       if (s_com != e_com)
  448.         {            /* the turkey has embedded a comment in a
  449.                    line. Move it from the com buffer to the
  450.                    code buffer.  */
  451.           /* Do not add a space before the comment if it is the first
  452.              thing on the line.  */
  453.           if (e_code != s_code)
  454.         {
  455.           *e_code++ = ' ';
  456.           embedded_comment_on_line = 2;
  457.         }
  458.           else
  459.         embedded_comment_on_line = 1;
  460.  
  461.           for (t_ptr = s_com; *t_ptr; ++t_ptr)
  462.         {
  463.           CHECK_CODE_SIZE;
  464.           *e_code++ = *t_ptr;
  465.         }
  466.           *e_code++ = ' ';
  467.           *e_code = '\0';    /* null terminate code sect */
  468.           parser_state_tos->want_blank = false;
  469.           e_com = s_com;
  470.         }
  471.     }
  472.       else if (type_code != comment
  473.            && type_code != cplus_comment)
  474.     /* preserve force_nl thru a comment but
  475.        cancel forced newline after newline, form feed, etc */
  476.        force_nl = false;
  477.  
  478.  
  479.  
  480.       /*-----------------------------------------------------*\
  481.       |       do switch on type of token scanned              |
  482.       \*-----------------------------------------------------*/
  483.       CHECK_CODE_SIZE;
  484.       switch (type_code)
  485.     {            /* now, decide what to do with the token */
  486.  
  487.     case form_feed:    /* found a form feed in line */
  488.       parser_state_tos->use_ff = true;    /* a form feed is treated
  489.                            much like a newline */
  490.       dump_line ();
  491.       parser_state_tos->want_blank = false;
  492.       break;
  493.  
  494.     case newline:
  495.       if ((parser_state_tos->last_token != comma
  496.            || !leave_comma || !break_comma
  497.            || parser_state_tos->p_l_follow > 0
  498.            || parser_state_tos->block_init
  499.            || s_com != e_com)
  500.           && (parser_state_tos->last_token != rbrace || !btype_2)
  501.           || (compute_code_target () + (e_code - s_code)
  502.           > max_col - tabsize))
  503.         {
  504.           dump_line ();
  505.           parser_state_tos->want_blank = false;
  506.         }
  507.       /* If we were on the line with a #else or a #endif, we aren't
  508.          anymore.  */
  509.       else_or_endif = false;
  510.       ++line_no;        /* keep track of input line number */
  511.       break;
  512.  
  513.     case lparen:
  514.       /* Braces in initializer lists should be put on new lines. This is
  515.          necessary so that -gnu does not cause things like char
  516.          *this_is_a_string_array[] = { "foo", "this_string_does_not_fit",
  517.          "nor_does_this_rather_long_string" } which is what happens
  518.          because we are trying to line the strings up with the
  519.          parentheses, and those that are too long are moved to the right
  520.          an ugly amount.
  521.     
  522.          However, if the current line is empty, the left brace is
  523.          already on a new line, so don't molest it.  */
  524.       if (token[0] == '{'
  525.           && (s_code != e_code || s_com != e_com || s_lab != e_lab))
  526.         {
  527.           dump_line ();
  528.           /* Do not put a space before the '{'.  */
  529.           parser_state_tos->want_blank = false;
  530.         }
  531.  
  532.       /* Count parens so we know how deep we are.  */
  533.       if (++parser_state_tos->p_l_follow
  534.           >= parser_state_tos->paren_indents_size)
  535.         {
  536.           parser_state_tos->paren_indents_size *= 2;
  537.           parser_state_tos->paren_indents = (short *)
  538.         xrealloc ((char *) parser_state_tos->paren_indents,
  539.               parser_state_tos->paren_indents_size
  540.               * sizeof (short));
  541.         }
  542.       parser_state_tos->paren_depth++;
  543.       if (parser_state_tos->want_blank && *token != '['
  544.           && (parser_state_tos->last_token != ident || proc_calls_space
  545.           || (parser_state_tos->its_a_keyword
  546.               && (!parser_state_tos->sizeof_keyword
  547.               || blank_after_sizeof))))
  548.         *e_code++ = ' ';
  549.  
  550.       if (parser_state_tos->in_decl && !parser_state_tos->block_init)
  551.         {
  552.           if (troff
  553.           && !parser_state_tos->dumped_decl_indent
  554.           && !is_procname
  555.           && parser_state_tos->last_token == decl)
  556.         {
  557.           parser_state_tos->dumped_decl_indent = 1;
  558.           sprintf (e_code, "\n.Du %dp+\200p \"%.*s\"\n",
  559.                (int) (dec_ind * 7),
  560.                token_end - token, token);
  561.           e_code += strlen (e_code);
  562.         }
  563.           else if (*token != '[')
  564.         {
  565.           while ((e_code - s_code) < dec_ind)
  566.             {
  567.               CHECK_CODE_SIZE;
  568.               *e_code++ = ' ';
  569.             }
  570.           *e_code++ = token[0];
  571.         }
  572.           else
  573.         *e_code++ = token[0];
  574.         }
  575.       else
  576.         *e_code++ = token[0];
  577.  
  578.       parser_state_tos->paren_indents[parser_state_tos->p_l_follow - 1]
  579.         = e_code - s_code;
  580.       if (sp_sw && parser_state_tos->p_l_follow == 1
  581.           && extra_expression_indent
  582.           && parser_state_tos->paren_indents[0] < 2 * ind_size)
  583.         parser_state_tos->paren_indents[0] = 2 * ind_size;
  584.       parser_state_tos->want_blank = false;
  585.  
  586.       if (parser_state_tos->in_or_st
  587.           && *token == '('
  588.           && parser_state_tos->tos <= 2)
  589.         {
  590.           /* this is a kluge to make sure that declarations will be
  591.              aligned right if proc decl has an explicit type on it, i.e.
  592.              "int a(x) {..." */
  593.           parse_lparen_in_decl ();
  594.  
  595.           /* Turn off flag for structure decl or initialization.  */
  596.           parser_state_tos->in_or_st = false;
  597.         }
  598.       if (parser_state_tos->sizeof_keyword)
  599.         parser_state_tos->sizeof_mask |= 1 << parser_state_tos->p_l_follow;
  600.  
  601.       /* The '(' that starts a cast can never be preceeded by an
  602.          indentifier or decl.  */
  603.       if (parser_state_tos->last_token == decl
  604.           || (parser_state_tos->last_token == ident
  605.           && parser_state_tos->last_rw != rw_return))
  606.         parser_state_tos->noncast_mask |=
  607.           1 << parser_state_tos->p_l_follow;
  608.       else
  609.         parser_state_tos->noncast_mask &=
  610.           ~(1 << parser_state_tos->p_l_follow);
  611.  
  612.       break;
  613.  
  614.     case rparen:
  615.       parser_state_tos->paren_depth--;
  616.       if (parser_state_tos->cast_mask
  617.           & (1 << parser_state_tos->p_l_follow)
  618.           & ~parser_state_tos->sizeof_mask)
  619.         {
  620.           parser_state_tos->last_u_d = true;
  621.           parser_state_tos->cast_mask &=
  622.         (1 << parser_state_tos->p_l_follow) - 1;
  623.           if (!parser_state_tos->cast_mask && cast_space)
  624.         parser_state_tos->want_blank = true;
  625.           else
  626.         parser_state_tos->want_blank = false;
  627.         }
  628.       else if (parser_state_tos->in_decl
  629.            && ! parser_state_tos->block_init
  630.            && parser_state_tos->paren_depth == 0)
  631.         parser_state_tos->want_blank = true;
  632.  
  633.       parser_state_tos->sizeof_mask
  634.         &= (1 << parser_state_tos->p_l_follow) - 1;
  635.       if (--parser_state_tos->p_l_follow < 0)
  636.         {
  637.           parser_state_tos->p_l_follow = 0;
  638.           diag (0, "Extra %c", (int) *token, 0);
  639.         }
  640.  
  641.       /* if the paren starts the line, then indent it */
  642.       if (e_code == s_code)
  643.         {
  644.           int level = parser_state_tos->p_l_follow;
  645.           parser_state_tos->paren_level = level;
  646.           if (level > 0)
  647.         paren_target = -parser_state_tos->paren_indents[level - 1];
  648.           else
  649.         paren_target = 0;
  650.         }
  651.       *e_code++ = token[0];
  652.  
  653. #if 0
  654.       if (!parser_state_tos->cast_mask || cast_space)
  655.         parser_state_tos->want_blank = true;
  656. #endif
  657.  
  658.       /* check for end of if (...), or some such */
  659.       if (sp_sw && (parser_state_tos->p_l_follow == 0))
  660.         {
  661.  
  662.           /* Indicate that we have just left the parenthesized expression
  663.              of a while, if, or for, unless we are getting out of the
  664.              parenthesized expression of the while of a do-while loop.
  665.              (do-while is different because a semicolon immediately
  666.              following this will not indicate a null loop body).  */
  667.           if (parser_state_tos->p_stack[parser_state_tos->tos]
  668.           != dohead)
  669.         last_token_ends_sp = 2;
  670.           sp_sw = false;
  671.           force_nl = true;    /* must force newline after if */
  672.           parser_state_tos->last_u_d = true;    /* inform lexi that a
  673.                                following operator is
  674.                                unary */
  675.           parser_state_tos->in_stmt = false;    /* dont use stmt
  676.                                continuation
  677.                                indentation */
  678.  
  679.           parse (hd_type);    /* let parser worry about if, or whatever */
  680.         }
  681.       parser_state_tos->search_brace = btype_2;    /* this should insure
  682.                                that constructs such
  683.                                as main(){...} and
  684.                                int[]{...} have their
  685.                                braces put in the
  686.                                right place */
  687.       break;
  688.  
  689.     case unary_op:        /* this could be any unary operation */
  690.       if (parser_state_tos->want_blank)
  691.         *e_code++ = ' ';
  692.  
  693.       if (troff
  694.           && !parser_state_tos->dumped_decl_indent
  695.           && parser_state_tos->in_decl && !is_procname)
  696.         {
  697.           sprintf (e_code, "\n.Du %dp+\200p \"%.*s\"\n",
  698.                (int) (dec_ind * 7),
  699.                token_end - token, token);
  700.           parser_state_tos->dumped_decl_indent = 1;
  701.           e_code += strlen (e_code);
  702.         }
  703.       else
  704.         {
  705.           char *res = token;
  706.           char *res_end = token_end;
  707.  
  708.           /* if this is a unary op in a declaration, we should
  709.          indent this token */
  710.           if (parser_state_tos->paren_depth == 0
  711.           && parser_state_tos->in_decl
  712.           && !parser_state_tos->block_init)
  713.         {
  714.           while ((e_code - s_code) < (dec_ind - (token_end - token)))
  715.             {
  716.               CHECK_CODE_SIZE;
  717.               *e_code++ = ' ';
  718.             }
  719.         }
  720.  
  721.           if (troff && token[0] == '-' && token[1] == '>')
  722.         {
  723.           static char resval[] = "\\(->";
  724.           res = resval;
  725.           res_end = res + sizeof (resval);
  726.         }
  727.  
  728.           for (t_ptr = res; t_ptr < res_end; ++t_ptr)
  729.         {
  730.           CHECK_CODE_SIZE;
  731.           *e_code++ = *t_ptr;
  732.         }
  733.         }
  734.       parser_state_tos->want_blank = false;
  735.       break;
  736.  
  737.     case binary_op:    /* any binary operation */
  738.       if (parser_state_tos->want_blank
  739.           || (e_code > s_code && *e_code != ' '))
  740.         *e_code++ = ' ';
  741.  
  742.       {
  743.         char *res = token;
  744.         char *res_end = token_end;
  745. #define set_res(str) \
  746.           {\
  747.         static char resval[] = str;\
  748.         res = resval;\
  749.         res_end = res + sizeof(resval);\
  750.           }
  751.  
  752.         if (troff)
  753.           switch (token[0])
  754.         {
  755.         case '<':
  756.           if (token[1] == '=')
  757.             set_res ("\\(<=");
  758.           break;
  759.         case '>':
  760.           if (token[1] == '=')
  761.             set_res ("\\(>=");
  762.           break;
  763.         case '!':
  764.           if (token[1] == '=')
  765.             set_res ("\\(!=");
  766.           break;
  767.         case '|':
  768.           if (token[1] == '|')
  769.             {
  770.               set_res ("\\(br\\(br");
  771.             }
  772.           else if (token[1] == 0)
  773.             set_res ("\\(br");
  774.           break;
  775.         }
  776.  
  777.         for (t_ptr = res; t_ptr < res_end; ++t_ptr)
  778.           {
  779.         CHECK_CODE_SIZE;
  780.         *e_code++ = *t_ptr;    /* move the operator */
  781.           }
  782.       }
  783.       parser_state_tos->want_blank = true;
  784.       break;
  785.  
  786.     case postop:        /* got a trailing ++ or -- */
  787.       *e_code++ = token[0];
  788.       *e_code++ = token[1];
  789.       parser_state_tos->want_blank = true;
  790.       break;
  791.  
  792.     case question:        /* got a ? */
  793.       squest++;        /* this will be used when a later colon
  794.                    appears so we can distinguish the
  795.                    <c>?<n>:<n> construct */
  796.       if (parser_state_tos->want_blank)
  797.         *e_code++ = ' ';
  798.       *e_code++ = '?';
  799.       parser_state_tos->want_blank = true;
  800.       break;
  801.  
  802.     case casestmt:        /* got word 'case' or 'default' */
  803.       scase = true;        /* so we can process the later colon
  804.                    properly */
  805.       goto copy_id;
  806.  
  807.     case colon:        /* got a ':' */
  808.       if (squest > 0)
  809.         {            /* it is part of the <c>?<n>: <n> construct */
  810.           --squest;
  811.           if (parser_state_tos->want_blank)
  812.         *e_code++ = ' ';
  813.           *e_code++ = ':';
  814.           parser_state_tos->want_blank = true;
  815.           break;
  816.         }
  817.       if (parser_state_tos->in_decl)
  818.         {
  819.           *e_code++ = ':';
  820.           parser_state_tos->want_blank = false;
  821.           break;
  822.         }
  823.       parser_state_tos->in_stmt = false;    /* seeing a label does not
  824.                            imply we are in a stmt */
  825.       for (t_ptr = s_code; *t_ptr; ++t_ptr)
  826.         *e_lab++ = *t_ptr;    /* turn everything so far into a label */
  827.       e_code = s_code;
  828.       *e_lab++ = ':';
  829.       *e_lab++ = ' ';
  830.       *e_lab = '\0';
  831.       /* parser_state_tos->pcas e will be used by dump_line to decide
  832.          how to indent the label. force_nl will force a case n: to be
  833.          on a line by itself */
  834.       force_nl = parser_state_tos->pcase = scase;
  835.       scase = false;
  836.       parser_state_tos->want_blank = false;
  837.       break;
  838.  
  839.     case semicolon:
  840.       /* we are not in an initialization or structure declaration */
  841.       parser_state_tos->in_or_st = false;
  842.       scase = false;
  843.       squest = 0;
  844.       /* The following code doesn't seem to do much good. Just because
  845.          we've found something like extern int foo();    or int (*foo)();
  846.          doesn't mean we are out of a declaration.  Now if it was serving
  847.          some purpose we'll have to address that.... if
  848.          (parser_state_tos->last_token == rparen)
  849.          parser_state_tos->in_parameter_declaration = 0; */
  850.       parser_state_tos->cast_mask = 0;
  851.       parser_state_tos->sizeof_mask = 0;
  852.       parser_state_tos->block_init = 0;
  853.       parser_state_tos->block_init_level = 0;
  854.       parser_state_tos->just_saw_decl--;
  855.  
  856.       if (parser_state_tos->in_decl
  857.           && s_code == e_code
  858.           && !parser_state_tos->block_init)
  859.         while ((e_code - s_code) < (dec_ind - 1))
  860.           {
  861.         CHECK_CODE_SIZE;
  862.         *e_code++ = ' ';
  863.           }
  864.  
  865.       /* if we were in a first level structure declaration,
  866.          we aren't any more */
  867.       parser_state_tos->in_decl = (parser_state_tos->dec_nest > 0);
  868.  
  869. #if 0    /* Changed to allow "{}" inside parens, as when
  870.        passed to a macro.  -jla */
  871.       if ((!sp_sw || hd_type != forstmt)
  872.           && parser_state_tos->p_l_follow > 0)
  873.         {
  874.  
  875.           /* This should be true iff there were unbalanced parens in the
  876.              stmt.  It is a bit complicated, because the semicolon might
  877.              be in a for stmt */
  878.           diag (1, "Unbalanced parens", 0, 0);
  879.           parser_state_tos->p_l_follow = 0;
  880.           if (sp_sw)
  881.         {        /* this is a check for a if, while, etc. with
  882.                    unbalanced parens */
  883.           sp_sw = false;
  884.           parse (hd_type);    /* dont lose the if, or whatever */
  885.         }
  886.         }
  887. #endif
  888.  
  889.       /* If we have a semicolon following an if, while, or for, and the
  890.          user wants us to, we should insert a space (to show that there
  891.          is a null statement there).  */
  892.       if (last_token_ends_sp && space_sp_semicolon)
  893.         {
  894.           *e_code++ = ' ';
  895.         }
  896.       *e_code++ = ';';
  897.       parser_state_tos->want_blank = true;
  898.       /* we are no longer in the middle of a stmt */
  899.       parser_state_tos->in_stmt = (parser_state_tos->p_l_follow > 0);
  900.  
  901.       if (!sp_sw)
  902.         {            /* if not if for (;;) */
  903.           parse (semicolon);/* let parser know about end of stmt */
  904.           force_nl = true;    /* force newline after a end of stmt */
  905.         }
  906.       break;
  907.  
  908.     case lbrace:        /* got a '{' */
  909.       parser_state_tos->in_stmt = false;    /* dont indent the {} */
  910.       if (!parser_state_tos->block_init)
  911.         force_nl = true;    /* force other stuff on same line as '{' onto
  912.                    new line */
  913.       else if (parser_state_tos->block_init_level <= 0)
  914.         parser_state_tos->block_init_level = 1;
  915.       else
  916.         parser_state_tos->block_init_level++;
  917.  
  918.       if (s_code != e_code && !parser_state_tos->block_init)
  919.         {
  920.           if (!btype_2)
  921.         {
  922.           dump_line ();
  923.           parser_state_tos->want_blank = false;
  924.         }
  925.           else
  926.         {
  927.           if (parser_state_tos->in_parameter_declaration
  928.               && !parser_state_tos->in_or_st)
  929.             {
  930.               parser_state_tos->i_l_follow = 0;
  931.               dump_line ();
  932.               parser_state_tos->want_blank = false;
  933.             }
  934.           else
  935.             parser_state_tos->want_blank = true;
  936.         }
  937.         }
  938.       if (parser_state_tos->in_parameter_declaration)
  939.         prefix_blankline_requested = 0;
  940.  
  941. #if 0    /* Changed to allow "{}" inside parens, as when
  942.        passed to a macro.  -jla */
  943.       if (parser_state_tos->p_l_follow > 0)
  944.         {            /* check for preceeding unbalanced parens */
  945.           diag (1, "Unbalanced parens", 0, 0);
  946.           parser_state_tos->p_l_follow = 0;
  947.           if (sp_sw)
  948.         {        /* check for unclosed if, for, etc. */
  949.           sp_sw = false;
  950.           parse (hd_type);
  951.           parser_state_tos->ind_level = parser_state_tos->i_l_follow;
  952.         }
  953.         }
  954. #endif
  955.       if (s_code == e_code)
  956.         parser_state_tos->ind_stmt = false;    /* dont put extra indentation
  957.                            on line with '{' */
  958.       if (parser_state_tos->in_decl && parser_state_tos->in_or_st)
  959.         {
  960.           /* This is a structure declaration.  */
  961.           if (parser_state_tos->dec_nest >= di_stack_alloc)
  962.         {
  963.           di_stack_alloc *= 2;
  964.           di_stack = (int *)
  965.             xrealloc ((char *) di_stack,
  966.                   di_stack_alloc * sizeof (*di_stack));
  967.         }
  968.           di_stack[parser_state_tos->dec_nest++] = dec_ind;
  969.           /* ?        dec_ind = 0; */
  970.         }
  971.       else
  972.         {
  973.           parser_state_tos->in_decl = false;
  974.           parser_state_tos->decl_on_line = false;    /* we cant be in the
  975.                                middle of a
  976.                                declaration, so dont
  977.                                do special
  978.                                indentation of
  979.                                comments */
  980.  
  981. #if 0                /* Doesn't work currently. */
  982.           if (blanklines_after_declarations_at_proctop
  983.           && parser_state_tos->in_parameter_declaration)
  984.         postfix_blankline_requested = 1;
  985. #endif
  986.           parser_state_tos->in_parameter_declaration = 0;
  987.         }
  988.       dec_ind = 0;
  989.  
  990.       /* We are no longer looking for an initializer or structure. Needed
  991.          so that the '=' in "enum bar {a = 1" does not get interpreted as
  992.          the start of an initializer.  */
  993.       parser_state_tos->in_or_st = false;
  994.  
  995.       parse (lbrace);    /* let parser know about this */
  996.       if (parser_state_tos->want_blank)    /* put a blank before '{' if
  997.                            '{' is not at start of
  998.                            line */
  999.         *e_code++ = ' ';
  1000.       parser_state_tos->want_blank = false;
  1001.       *e_code++ = '{';
  1002.       parser_state_tos->just_saw_decl = 0;
  1003.       break;
  1004.  
  1005.     case rbrace:        /* got a '}' */
  1006.       /* semicolons can be omitted in declarations */
  1007.       if (parser_state_tos->p_stack[parser_state_tos->tos] == decl
  1008.           && !parser_state_tos->block_init)
  1009.         parse (semicolon);
  1010. #if 0    /* Changed to allow "{}" inside parens, as when
  1011.        passed to a macro.  -jla */
  1012.       if (parser_state_tos->p_l_follow)
  1013.         {            /* check for unclosed if, for, else. */
  1014.           diag (1, "Unbalanced parens", 0, 0);
  1015.           parser_state_tos->p_l_follow = 0;
  1016.           sp_sw = false;
  1017.         }
  1018. #endif
  1019.  
  1020.       parser_state_tos->just_saw_decl = 0;
  1021.       parser_state_tos->block_init_level--;
  1022.       if (s_code != e_code && !parser_state_tos->block_init)
  1023.         {            /* '}' must be first on line */
  1024.           if (verbose)
  1025.         diag (0, "Line broken", 0, 0);
  1026.           dump_line ();
  1027.         }
  1028.       *e_code++ = '}';
  1029.       parser_state_tos->want_blank = true;
  1030.       parser_state_tos->in_stmt = parser_state_tos->ind_stmt = false;
  1031.       if (parser_state_tos->dec_nest > 0)
  1032.         {            /* we are in multi-level structure
  1033.                    declaration */
  1034.           dec_ind = di_stack[--parser_state_tos->dec_nest];
  1035.           if (parser_state_tos->dec_nest == 0
  1036.           && !parser_state_tos->in_parameter_declaration)
  1037.         parser_state_tos->just_saw_decl = 2;
  1038.           parser_state_tos->in_decl = true;
  1039.         }
  1040.       prefix_blankline_requested = 0;
  1041.       parse (rbrace);    /* let parser know about this */
  1042.       parser_state_tos->search_brace
  1043.         = (cuddle_else
  1044.          && parser_state_tos->p_stack[parser_state_tos->tos] == ifhead);
  1045.  
  1046.       if ((parser_state_tos->p_stack[parser_state_tos->tos] == stmtl
  1047.            && ((parser_state_tos->last_rw != rw_struct_like
  1048.             && parser_state_tos->last_rw != rw_decl)
  1049.            || ! btype_2))
  1050.           || (parser_state_tos->p_stack[parser_state_tos->tos] == ifhead)
  1051.           || (parser_state_tos->p_stack[parser_state_tos->tos] == dohead
  1052.           && !btype_2))
  1053.         force_nl = true;
  1054.       else if (parser_state_tos->tos <= 1
  1055.            && blanklines_after_procs
  1056.            && parser_state_tos->dec_nest <= 0)
  1057.         postfix_blankline_requested = 1;
  1058.  
  1059. #if 0
  1060.       parser_state_tos->search_brace
  1061.         = (cuddle_else
  1062.            && parser_state_tos->p_stack[parser_state_tos->tos] == ifhead
  1063.            && (parser_state_tos->il[parser_state_tos->tos]
  1064.            >= parser_state_tos->ind_level));
  1065. #endif
  1066.  
  1067.       break;
  1068.  
  1069.     case swstmt:        /* got keyword "switch" */
  1070.       sp_sw = true;
  1071.       hd_type = swstmt;    /* keep this for when we have seen the
  1072.                    expression */
  1073.       parser_state_tos->in_decl = false;
  1074.       goto copy_id;        /* go move the token into buffer */
  1075.  
  1076.     case sp_paren:        /* token is if, while, for */
  1077.       sp_sw = true;        /* the interesting stuff is done after the
  1078.                    expression is scanned */
  1079.       hd_type = (*token == 'i' ? ifstmt :
  1080.              (*token == 'w' ? whilestmt : forstmt));
  1081.  
  1082.       /* remember the type of header for later use by parser */
  1083.       goto copy_id;        /* copy the token into line */
  1084.  
  1085.     case sp_nparen:    /* got else, do */
  1086.       parser_state_tos->in_stmt = false;
  1087.       if (*token == 'e')
  1088.         {
  1089.           if (e_code != s_code && (!cuddle_else || e_code[-1] != '}'))
  1090.         {
  1091.           if (verbose)
  1092.             diag (0, "Line broken", 0, 0);
  1093.           dump_line ();    /* make sure this starts a line */
  1094.           parser_state_tos->want_blank = false;
  1095.         }
  1096.           force_nl = true;    /* also, following stuff must go onto new
  1097.                    line */
  1098.           last_else = 1;
  1099.           parse (elselit);
  1100.         }
  1101.       else
  1102.         {
  1103.           if (e_code != s_code)
  1104.         {        /* make sure this starts a line */
  1105.           if (verbose)
  1106.             diag (0, "Line broken", 0, 0);
  1107.           dump_line ();
  1108.           parser_state_tos->want_blank = false;
  1109.         }
  1110.           force_nl = true;    /* also, following stuff must go onto new
  1111.                    line */
  1112.           last_else = 0;
  1113.           parse (dolit);
  1114.         }
  1115.       goto copy_id;        /* move the token into line */
  1116.  
  1117.     case decl:        /* we have a declaration type (int, register,
  1118.                    etc.) */
  1119.  
  1120.       if (! parser_state_tos->sizeof_mask)
  1121.         parse (decl);
  1122.  
  1123.       if (parser_state_tos->last_token == rparen
  1124.           && parser_state_tos->tos <= 1)
  1125.         {
  1126.           parser_state_tos->in_parameter_declaration = 1;
  1127.           if (s_code != e_code)
  1128.         {
  1129.           dump_line ();
  1130.           parser_state_tos->want_blank = false;
  1131.         }
  1132.         }
  1133.       if (parser_state_tos->in_parameter_declaration
  1134.           && indent_parameters
  1135.           && parser_state_tos->dec_nest == 0
  1136.           && parser_state_tos->p_l_follow == 0)
  1137.         {
  1138.           parser_state_tos->ind_level
  1139.         = parser_state_tos->i_l_follow = indent_parameters;
  1140.           parser_state_tos->ind_stmt = 0;
  1141.         }
  1142.  
  1143.       /* in_or_st set for struct or initialization decl. Don't set it if
  1144.          we're in ansi prototype */
  1145.       if (!parser_state_tos->paren_depth)
  1146.         parser_state_tos->in_or_st = true;
  1147.  
  1148.       parser_state_tos->in_decl = parser_state_tos->decl_on_line = true;
  1149. #if 0
  1150.       if (!parser_state_tos->in_or_st && parser_state_tos->dec_nest <= 0)
  1151. #endif
  1152.         if (parser_state_tos->dec_nest <= 0)
  1153.           parser_state_tos->just_saw_decl = 2;
  1154.       if (prefix_blankline_requested
  1155.           && (parser_state_tos->block_init != 0
  1156.           || parser_state_tos->block_init_level != -1
  1157.           || parser_state_tos->last_token != rbrace
  1158.           || e_code != s_code
  1159.           || e_lab != s_lab
  1160.           || e_com != s_com))
  1161.         prefix_blankline_requested = 0;
  1162.       i = token_end - token + 1;    /* get length of token plus 1 */
  1163.  
  1164.       /* dec_ind = e_code - s_code + (parser_state_tos->decl_indent>i ?
  1165.          parser_state_tos->decl_indent : i); */
  1166.       dec_ind = decl_indent > 0 ? decl_indent : i;
  1167.       goto copy_id;
  1168.  
  1169.     case ident:        /* got an identifier or constant */
  1170.       /* If we are in a declaration, we must indent identifier. But not
  1171.          inside the parentheses of an ANSI function declaration.  */
  1172.       if (parser_state_tos->in_decl
  1173.           && parser_state_tos->p_l_follow == 0
  1174.           && parser_state_tos->last_token != rbrace)
  1175.         {
  1176.           if (parser_state_tos->want_blank)
  1177.         *e_code++ = ' ';
  1178.           parser_state_tos->want_blank = false;
  1179.           if (is_procname == 0 || !procnames_start_line)
  1180.         {
  1181.           if (!parser_state_tos->block_init)
  1182.             if (troff && !parser_state_tos->dumped_decl_indent)
  1183.               {
  1184.             sprintf (e_code, "\n.De %dp+\200p\n",
  1185.                  (int) (dec_ind * 7));
  1186.             parser_state_tos->dumped_decl_indent = 1;
  1187.             e_code += strlen (e_code);
  1188.               }
  1189.             else
  1190.               while ((e_code - s_code) < dec_ind)
  1191.             {
  1192.               CHECK_CODE_SIZE;
  1193.               *e_code++ = ' ';
  1194.             }
  1195.         }
  1196.           else
  1197.         {
  1198.           if (dec_ind && s_code != e_code)
  1199.             dump_line ();
  1200.           dec_ind = 0;
  1201.           parser_state_tos->want_blank = false;
  1202.         }
  1203.         }
  1204.       else if (sp_sw && parser_state_tos->p_l_follow == 0)
  1205.         {
  1206.           sp_sw = false;
  1207.           force_nl = true;
  1208.           parser_state_tos->last_u_d = true;
  1209.           parser_state_tos->in_stmt = false;
  1210.           parse (hd_type);
  1211.         }
  1212.     copy_id:
  1213.       if (parser_state_tos->want_blank)
  1214.         *e_code++ = ' ';
  1215.       if (troff && parser_state_tos->its_a_keyword)
  1216.         {
  1217.           e_code = chfont (&bodyf, &keywordf, e_code);
  1218.           for (t_ptr = token; t_ptr < token_end; ++t_ptr)
  1219.         {
  1220.           CHECK_CODE_SIZE;
  1221.           *e_code++ = keywordf.allcaps && islower (*t_ptr)
  1222.             ? toupper (*t_ptr) : *t_ptr;
  1223.         }
  1224.           e_code = chfont (&keywordf, &bodyf, e_code);
  1225.         }
  1226.       else
  1227.         {
  1228.           /* Troff mode requires that strings be processed specially.  */
  1229.           if (troff && (*token == '"' || *token == '\''))
  1230.         {
  1231.           char qchar;
  1232.  
  1233.           qchar = *token;
  1234.           *e_code++ = '`';
  1235.           if (qchar == '"')
  1236.             *e_code++ = '`';
  1237.           e_code = chfont (&bodyf, &stringf, e_code);
  1238.  
  1239.           t_ptr = token + 1;
  1240.           while (t_ptr < token_end)
  1241.             {
  1242.               *e_code = *t_ptr++;
  1243.               if (*e_code == '\\')
  1244.             {
  1245.               *++e_code = '\\';
  1246.               if (*t_ptr == '\\')
  1247.                 *++e_code = '\\';
  1248.               /* Copy char after backslash.  */
  1249.               *++e_code = *t_ptr++;
  1250.               /* Point after the last char we copied.  */
  1251.               e_code++;
  1252.             }
  1253.             }
  1254.           e_code = chfont (&stringf, &bodyf, e_code - 1);
  1255.           if (qchar == '"')
  1256.             *e_code++ = '\'';
  1257.         }
  1258.           else
  1259.         for (t_ptr = token; t_ptr < token_end; ++t_ptr)
  1260.           {
  1261.             CHECK_CODE_SIZE;
  1262.             *e_code++ = *t_ptr;
  1263.           }
  1264.         }
  1265.       parser_state_tos->want_blank = true;
  1266.  
  1267.       /* If the token is va_dcl, it appears without a semicolon, so we
  1268.          need to pretend that one was there.  */
  1269.       if ((token_end - token) == 6
  1270.           && strncmp (token, "va_dcl", 6) == 0)
  1271.         {
  1272.           parser_state_tos->in_or_st = false;
  1273.           parser_state_tos->just_saw_decl--;
  1274.           parser_state_tos->in_decl = 0;
  1275.           parse (semicolon);
  1276.           force_nl = true;
  1277.         }
  1278.       break;
  1279.  
  1280.     case period:        /* treat a period kind of like a binary
  1281.                    operation */
  1282.       *e_code++ = '.';    /* move the period into line */
  1283.       parser_state_tos->want_blank = false;    /* dont put a blank after a
  1284.                            period */
  1285.       break;
  1286.  
  1287.     case comma:
  1288.       /* only put blank after comma if comma does not start the line */
  1289.       parser_state_tos->want_blank = (s_code != e_code);
  1290.       if (parser_state_tos->paren_depth == 0
  1291.           && parser_state_tos->in_decl
  1292.           && is_procname == 0
  1293.           && !parser_state_tos->block_init)
  1294.         while ((e_code - s_code) < (dec_ind - 1))
  1295.           {
  1296.         CHECK_CODE_SIZE;
  1297.         *e_code++ = ' ';
  1298.           }
  1299.  
  1300.       *e_code++ = ',';
  1301.       if (parser_state_tos->p_l_follow == 0)
  1302.         {
  1303.           if (parser_state_tos->block_init_level <= 0)
  1304.         parser_state_tos->block_init = 0;
  1305.           /* If we are in a declaration, and either the user wants all
  1306.              comma'd declarations broken, or the line is getting too
  1307.              long, break the line.  */
  1308.           if (break_comma &&
  1309.           (!leave_comma
  1310.            || (compute_code_target () + (e_code - s_code)
  1311.                > max_col - tabsize)))
  1312.         force_nl = true;
  1313.         }
  1314.       break;
  1315.  
  1316.     case preesc:        /* got the character '#' */
  1317.       if ((s_com != e_com) ||
  1318.           (s_lab != e_lab) ||
  1319.           (s_code != e_code))
  1320.         dump_line ();
  1321.       {
  1322.         int in_comment = 0;
  1323.         int in_cplus_comment = 0;
  1324.         int com_start = 0;
  1325.         char quote = 0;
  1326.         int com_end = 0;
  1327.  
  1328.         /* ANSI allows spaces between '#' and preprocessor directives.
  1329.            Remove such spaces unless user has specified "-lps", in
  1330.            which case also leave any space preceeding the '#'. */
  1331.         if (leave_preproc_space)
  1332.           {
  1333.         char *p = cur_line;
  1334.         while (p < buf_ptr)
  1335.           *e_lab++ = *p++;
  1336.  
  1337.         while (*buf_ptr == ' ' || *buf_ptr == TAB)
  1338.           {
  1339.             *e_lab++ = *buf_ptr++;
  1340.             if (buf_ptr >= buf_end)
  1341.               fill_buffer ();
  1342.           }
  1343.           }
  1344.         else
  1345.           {
  1346.         *e_lab++ = '#';
  1347.         while (*buf_ptr == ' ' || *buf_ptr == TAB)
  1348.           if (++buf_ptr >= buf_end)
  1349.             fill_buffer ();
  1350.           }
  1351.  
  1352.         while (*buf_ptr != EOL || (in_comment && !had_eof))
  1353.           {
  1354.         CHECK_LAB_SIZE;
  1355.         *e_lab = *buf_ptr++;
  1356.         if (buf_ptr >= buf_end)
  1357.           fill_buffer ();
  1358.         switch (*e_lab++)
  1359.           {
  1360.           case BACKSLASH:
  1361.             if (troff)
  1362.               *e_lab++ = BACKSLASH;
  1363.             if (!in_comment && !in_cplus_comment)
  1364.               {
  1365.             *e_lab++ = *buf_ptr++;
  1366.             if (buf_ptr >= buf_end)
  1367.               fill_buffer ();
  1368.               }
  1369.             break;
  1370.           case '/':
  1371.             if ((*buf_ptr == '*' || *buf_ptr == '/')
  1372.             && !in_comment && !in_cplus_comment && !quote)
  1373.               {
  1374.             if (*buf_ptr == '/')
  1375.               in_cplus_comment = 1;
  1376.             else
  1377.               in_comment = 1;
  1378.             *e_lab++ = *buf_ptr++;
  1379.             com_start = e_lab - s_lab - 2;
  1380.               }
  1381.             break;
  1382.  
  1383.           case '"':
  1384.           case '\'':
  1385.             if (!quote)
  1386.               quote = e_lab[-1];
  1387.             else
  1388.               if (e_lab[-1] == quote)
  1389.             quote = 0;
  1390.             break;
  1391.  
  1392.           case '*':
  1393.             if (*buf_ptr == '/' && in_comment)
  1394.               {
  1395.             in_comment = 0;
  1396.             *e_lab++ = *buf_ptr++;
  1397.             com_end = e_lab - s_lab;
  1398.               }
  1399.             break;
  1400.           }
  1401.           }
  1402.  
  1403.         while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == TAB))
  1404.           e_lab--;
  1405.  
  1406.         if (in_cplus_comment)
  1407.           {
  1408.         in_cplus_comment = 0;
  1409.         *e_lab++ = *buf_ptr++;
  1410.         com_end = e_lab - s_lab;
  1411.           }
  1412.  
  1413.         if (e_lab - s_lab == com_end && bp_save == 0)
  1414.           {            /* comment on preprocessor line */
  1415.         if (save_com.end != save_com.ptr)
  1416.           {
  1417.             need_chars (save_com, 2);
  1418.             *save_com.end++ = EOL;    /* add newline between
  1419.                            comments */
  1420.             *save_com.end++ = ' ';
  1421.             --line_no;
  1422.           }
  1423.         need_chars (save_com, com_end - com_start);
  1424.         strncpy (save_com.end, s_lab + com_start,
  1425.              com_end - com_start);
  1426.         save_com.end += com_end - com_start;
  1427.  
  1428.         e_lab = s_lab + com_start;
  1429.         while (e_lab > s_lab
  1430.                && (e_lab[-1] == ' ' || e_lab[-1] == TAB))
  1431.           e_lab--;
  1432.         bp_save = buf_ptr;    /* save current input buffer */
  1433.         be_save = buf_end;
  1434.         buf_ptr = save_com.ptr;    /* fix so that subsequent calls to
  1435.                        lexi will take tokens out of
  1436.                        save_com */
  1437.         need_chars (save_com, 1);
  1438.         *save_com.end++ = ' ';    /* add trailing blank, just in case */
  1439.         buf_end = save_com.end;
  1440.         save_com.end = save_com.ptr;    /* make save_com empty */
  1441.           }
  1442.         *e_lab = '\0';    /* null terminate line */
  1443.         parser_state_tos->pcase = false;
  1444.       }
  1445.  
  1446.       if (strncmp (s_lab + 1, "if", 2) == 0)
  1447.         {
  1448.           if (blanklines_around_conditional_compilation)
  1449.         {
  1450.           register c;
  1451.           prefix_blankline_requested++;
  1452.           while ((c = *in_prog_pos++) == EOL);
  1453.           in_prog_pos--;
  1454.         }
  1455.           {
  1456.         /* Push a copy of the parser_state onto the stack. All
  1457.            manipulations will use the copy at the top of stack, and
  1458.            then we can return to the previous state by popping the
  1459.            stack.  */
  1460.         struct parser_state *new;
  1461.  
  1462.         new = (struct parser_state *)
  1463.           xmalloc (sizeof (struct parser_state));
  1464.         memcpy (new, parser_state_tos, sizeof (struct parser_state));
  1465.  
  1466.         /* We need to copy the dynamically allocated arrays in the
  1467.            struct parser_state too.  */
  1468.         new->p_stack = (enum codes *)
  1469.           xmalloc (parser_state_tos->p_stack_size
  1470.                * sizeof (enum codes));
  1471.         memcpy (new->p_stack, parser_state_tos->p_stack,
  1472.               parser_state_tos->p_stack_size * sizeof (enum codes));
  1473.  
  1474.         new->il = (int *)
  1475.           xmalloc (parser_state_tos->p_stack_size * sizeof (int));
  1476.         memcpy (new->il, parser_state_tos->il,
  1477.             parser_state_tos->p_stack_size * sizeof (int));
  1478.  
  1479.         new->cstk = (int *)
  1480.           xmalloc (parser_state_tos->p_stack_size
  1481.                * sizeof (int));
  1482.         memcpy (new->cstk, parser_state_tos->cstk,
  1483.             parser_state_tos->p_stack_size * sizeof (int));
  1484.  
  1485.         new->paren_indents = (short *) xmalloc
  1486.           (parser_state_tos->paren_indents_size * sizeof (short));
  1487.         memcpy (new->paren_indents, parser_state_tos->paren_indents,
  1488.              parser_state_tos->paren_indents_size * sizeof (short));
  1489.  
  1490.         new->next = parser_state_tos;
  1491.         parser_state_tos = new;
  1492.           }
  1493.         }
  1494.       else if (strncmp (s_lab + 1, "else", 4) == 0)
  1495.         {
  1496.           /* When we get #else, we want to restore the parser state to
  1497.              what it was before the matching #if, so that things get
  1498.              lined up with the code before the #if.  However, we do not
  1499.              want to pop the stack; we just want to copy the second to
  1500.              top elt of the stack because when we encounter the #endif,
  1501.              it will pop the stack.  */
  1502.           else_or_endif = true;
  1503.           if (parser_state_tos->next)
  1504.         {
  1505.           /* First save the addresses of the arrays for the top of
  1506.              stack.  */
  1507.           enum codes *tos_p_stack = parser_state_tos->p_stack;
  1508.           int *tos_il = parser_state_tos->il;
  1509.           int *tos_cstk = parser_state_tos->cstk;
  1510.           short *tos_paren_indents =
  1511.           parser_state_tos->paren_indents;
  1512.           struct parser_state *second =
  1513.           parser_state_tos->next;
  1514.  
  1515.           memcpy (parser_state_tos, second,
  1516.               sizeof (struct parser_state));
  1517.           parser_state_tos->next = second;
  1518.  
  1519.           /* Now copy the arrays from the second to top of stack to
  1520.              the top of stack.  */
  1521.           /* Since the p_stack, etc. arrays only grow, never shrink,
  1522.              we know that they will be big enough to fit the array
  1523.              from the second to top of stack.  */
  1524.           parser_state_tos->p_stack = tos_p_stack;
  1525.           memcpy (parser_state_tos->p_stack,
  1526.               parser_state_tos->next->p_stack,
  1527.               parser_state_tos->p_stack_size
  1528.               * sizeof (enum codes));
  1529.  
  1530.           parser_state_tos->il = tos_il;
  1531.           memcpy (parser_state_tos->il,
  1532.               parser_state_tos->next->il,
  1533.               parser_state_tos->p_stack_size * sizeof (int));
  1534.  
  1535.           parser_state_tos->cstk = tos_cstk;
  1536.           memcpy (parser_state_tos->cstk,
  1537.               parser_state_tos->next->cstk,
  1538.               parser_state_tos->p_stack_size * sizeof (int));
  1539.  
  1540.           parser_state_tos->paren_indents = tos_paren_indents;
  1541.           memcpy (parser_state_tos->paren_indents,
  1542.               parser_state_tos->next->paren_indents,
  1543.               parser_state_tos->paren_indents_size
  1544.               * sizeof (short));
  1545.         }
  1546.           else
  1547.         diag (1, "Unmatched #else", 0, 0);
  1548.         }
  1549.       else if (strncmp (s_lab + 1, "endif", 5) == 0)
  1550.         {
  1551.           else_or_endif = true;
  1552.           /* We want to remove the second to top elt on the stack, which
  1553.              was put there by #if and was used to restore the stack at
  1554.              the #else (if there was one). We want to leave the top of
  1555.              stack unmolested so that the state which we have been using
  1556.              is unchanged.  */
  1557.           if (parser_state_tos->next)
  1558.         {
  1559.           struct parser_state *second = parser_state_tos->next;
  1560.  
  1561.           parser_state_tos->next = second->next;
  1562.           free (second->p_stack);
  1563.           free (second->il);
  1564.           free (second->cstk);
  1565.           free (second->paren_indents);
  1566.           free (second);
  1567.         }
  1568.           else
  1569.         diag (1, "Unmatched #endif", 0, 0);
  1570.           if (blanklines_around_conditional_compilation)
  1571.         {
  1572.           postfix_blankline_requested++;
  1573.           n_real_blanklines = 0;
  1574.         }
  1575.         }
  1576.  
  1577.       /* Normally, subsequent processing of the newline character
  1578.          causes the line to be printed.  The following clause handles
  1579.          a special case (comma-separated declarations separated
  1580.          by the preprocessor lines) where this doesn't happen. */
  1581.       if (parser_state_tos->last_token == comma
  1582.           && parser_state_tos->p_l_follow <= 0
  1583.           && leave_comma && !parser_state_tos->block_init
  1584.           && break_comma && s_com == e_com)
  1585.         {
  1586.           dump_line ();
  1587.           parser_state_tos->want_blank = false;
  1588.         }
  1589.       break;
  1590.  
  1591.       /* A C or C++ comment. */
  1592.     case comment:
  1593.     case cplus_comment:
  1594.       if (flushed_nl)
  1595.         {
  1596.           flushed_nl = false;
  1597.           dump_line ();
  1598.           parser_state_tos->want_blank = false;
  1599.           force_nl = false;
  1600.         }
  1601.       print_comment ();
  1602.       break;
  1603.     }            /* end of big switch stmt */
  1604.  
  1605.       *e_code = '\0';        /* make sure code section is null terminated */
  1606.       if (type_code != comment
  1607.       && type_code != cplus_comment
  1608.       && type_code != newline
  1609.       && type_code != preesc
  1610.       && type_code != form_feed)
  1611.     parser_state_tos->last_token = type_code;
  1612.  
  1613.     }                /* end of main while (1) loop */
  1614. }
  1615.  
  1616.  
  1617.  
  1618. char *set_profile ();
  1619. void set_defaults ();
  1620. int set_option ();
  1621.  
  1622. /* Points to current input file */
  1623. char *in_name = 0;
  1624.  
  1625. /* Points to the name of the output file */
  1626. char *out_name = 0;
  1627.  
  1628. /* How many input files were specified */
  1629. int input_files;
  1630.  
  1631. /* Names of all input files */
  1632. char **in_file_names;
  1633.  
  1634. /* Initial number of input filenames to allocate. */
  1635. int max_input_files = 128;
  1636.  
  1637.  
  1638. #ifdef DEBUG
  1639. int debug;
  1640. #endif
  1641.  
  1642. main (argc, argv)
  1643.      int argc;
  1644.      char **argv;
  1645. {
  1646.   register int i;
  1647.   struct file_buffer *current_input;
  1648.   char *profile_pathname = 0;
  1649.   int using_stdin = false;
  1650.  
  1651. #ifdef DEBUG
  1652.   if (debug)
  1653.     debug_init ();
  1654. #endif
  1655.  
  1656.   init_parser ();
  1657.   initialize_backups ();
  1658.  
  1659.   output = 0;
  1660.   input_files = 0;
  1661.   in_file_names = (char **) xmalloc (max_input_files * sizeof (char *));
  1662.  
  1663.   set_defaults ();
  1664.   for (i = 1; i < argc; ++i)
  1665.     if (strcmp (argv[i], "-npro") == 0
  1666.     || strcmp (argv[i], "--ignore-profile") == 0
  1667.     || strcmp (argv[i], "+ignore-profile") == 0)
  1668.       break;
  1669.   if (i >= argc)
  1670.     profile_pathname = set_profile ();
  1671.  
  1672.   for (i = 1; i < argc; ++i)
  1673.     {
  1674.       if (argv[i][0] != '-' && argv[i][0] != '+')    /* Filename */
  1675.     {
  1676.       if (expect_output_file == true)    /* Last arg was "-o" */
  1677.         {
  1678.           if (out_name != 0)
  1679.         {
  1680.           fprintf (stderr, "indent: only one output file (2nd was %s)\n", argv[i]);
  1681.           exit (1);
  1682.         }
  1683.  
  1684.           if (input_files > 1)
  1685.         {
  1686.           fprintf (stderr, "indent: only one input file when output file is specified\n");
  1687.           exit (1);
  1688.         }
  1689.  
  1690.           out_name = argv[i];
  1691.           expect_output_file = false;
  1692.           continue;
  1693.         }
  1694.       else
  1695.         {
  1696.           if (using_stdin)
  1697.         {
  1698.           fprintf (stderr, "indent: can't have filenames when specifying standard input\n");
  1699.           exit (1);
  1700.         }
  1701.  
  1702.           input_files++;
  1703.           if (input_files > 1)
  1704.         {
  1705.           if (out_name != 0)
  1706.             {
  1707.               fprintf (stderr, "indent: only one input file when output file is specified\n");
  1708.               exit (1);
  1709.             }
  1710.  
  1711.           if (use_stdout != 0)
  1712.             {
  1713.               fprintf (stderr, "indent: only one input file when stdout is used\n");
  1714.               exit (1);
  1715.             }
  1716.  
  1717.           if (input_files > max_input_files)
  1718.             {
  1719.               max_input_files = 2 * max_input_files;
  1720.               in_file_names
  1721.             = (char **) xrealloc ((char *) in_file_names,
  1722.                           (max_input_files
  1723.                            * sizeof (char *)));
  1724.             }
  1725.         }
  1726.  
  1727.           in_file_names[input_files - 1] = argv[i];
  1728.         }
  1729.     }
  1730.       else
  1731.     {
  1732.       /* '-' as filename means stdin. */
  1733.       if (argv[i][0] == '-' && argv[i][1] == '\0')
  1734.         {
  1735.           if (input_files > 0)
  1736.         {
  1737.           fprintf (stderr, "indent: can't have filenames when specifying standard input\n");
  1738.           exit (1);
  1739.         }
  1740.  
  1741.           using_stdin = true;
  1742.         }
  1743.       else
  1744.         i += set_option (argv[i], (i < argc ? argv[i + 1] : 0), 1);
  1745.     }
  1746.     }
  1747.  
  1748.   if (verbose && profile_pathname)
  1749.     fprintf (stderr, "Read profile %s\n", profile_pathname);
  1750.  
  1751.   if (input_files > 1)
  1752.     {
  1753.       /* When multiple input files are specified, make a backup copy
  1754.      and then output the indented code into the same filename. */
  1755.  
  1756.       for (i = 0; input_files; i++, input_files--)
  1757.     {
  1758.       current_input = read_file (in_file_names[i]);
  1759.       in_name = out_name = in_file_names[i];
  1760.       output = fopen (out_name, "w");
  1761.       if (output == 0)
  1762.         {
  1763.           fprintf (stderr, "indent: can't create %s\n", out_name);
  1764.           exit (1);
  1765.         }
  1766.  
  1767.       make_backup (current_input);
  1768.       reset_parser ();
  1769.       indent (current_input);
  1770.       if (fclose (output) != 0)
  1771.         sys_error (out_name);
  1772.     }
  1773.     }
  1774.   else
  1775.     {
  1776.       /* One input stream -- specified file, or stdin */
  1777.  
  1778.       if (input_files == 0 || using_stdin)
  1779.     {
  1780.       input_files = 1;
  1781.       in_file_names[0] = "Standard input";
  1782.       current_input = read_stdin ();
  1783.     }
  1784.       else
  1785.     /* 1 input file */
  1786.     {
  1787.       current_input = read_file (in_file_names[0]);
  1788.       if (!out_name && !use_stdout)
  1789.         {
  1790.           out_name = in_file_names[0];
  1791.           make_backup (current_input);
  1792.         }
  1793.     }
  1794.       in_name = in_file_names[0];
  1795.  
  1796.       /* Uset stdout if it was specified ("-st"), or neither input
  1797.          nor output file was specified, or we're doing troff. */
  1798.       if (use_stdout || !out_name || troff)
  1799.     output = stdout;
  1800.       else
  1801.     {
  1802.       output = fopen (out_name, "w");
  1803.       if (output == 0)
  1804.         {
  1805.           fprintf (stderr, "indent: can't create %s\n", out_name);
  1806.           exit (1);
  1807.         }
  1808.     }
  1809.  
  1810.       reset_parser ();
  1811.       indent (current_input);
  1812.     }
  1813.  
  1814.   exit (0);
  1815. }
  1816.