home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / util / editor / gnuid16.sit / indent-1.6 / indent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-23  |  49.4 KB  |  1,760 lines

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