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

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