Ejemplo n.º 1
0
pr_comment ()
{
  int now_col;			/* column we are in now */
  int adj_max_col;		/* Adjusted max_col for when we decide to
				   spill comments over the right margin */
  char *last_bl;		/* points to the last blank in the output
				   buffer */
  char *t_ptr;			/* used for moving string */
  int unix_comment;		/* tri-state variable used to decide if it is
				   a unix-style comment. 0 means only blanks
				   since /*, 1 means regular style comment, 2
				   means unix style comment */
  int break_delim = comment_delimiter_on_blankline;
  int l_just_saw_decl = parser_state_tos->just_saw_decl;
  /* int         parser_state_tos->last_nl = 0; /* true iff the last
     significant thing weve seen is a newline */
  int one_liner = 1;		/* true iff this comment is a one-liner */
  adj_max_col = max_col;
  parser_state_tos->just_saw_decl = 0;
  last_bl = 0;			/* no blanks found so far */
  parser_state_tos->box_com = false;	/* at first, assume that we are not
					   in a boxed comment or some other
					   comment that should not be touched */
  ++out_coms;			/* keep track of number of comments */
  unix_comment = 1;		/* set flag to let us figure out if there is
				   a unix-style comment ** DISABLED: use 0 to
				   reenable this hack! */

  /* Figure where to align and how to treat the comment */

  if (parser_state_tos->col_1 && !format_col1_comments)
    {				/* if comment starts in column 1 it should
				   not be touched */
      parser_state_tos->box_com = true;
      parser_state_tos->com_col = 1;
    }
  else
    {
      if (*buf_ptr == '-' || *buf_ptr == '*' || !format_comments)
	{
	  parser_state_tos->box_com = true;	/* a comment with a '-' or
						   '*' immediately after the
						   /* is assumed to be a
						   boxed comment */
	  break_delim = 0;
	}


      /* Used to also check that parser_state_tos->bl_line != 0 */
      if ((s_lab == e_lab) && (s_code == e_code))
	{
	  /* klg: check only if this line is blank */
	  /* If this (*and previous lines are*) blank, dont put comment way
	     out at left */
	  parser_state_tos->com_col
	    = (parser_state_tos->ind_level - unindent_displace) + 1;
	  adj_max_col = block_comment_max_col;
	  if (parser_state_tos->com_col <= 1)
	    parser_state_tos->com_col = 1 + !format_col1_comments;

	  /* If we have a comment in an old-style (pre-ANSI) parameter
	     declaration, indent it like we would the parameter declaration.
	     For example:
	     int destroy (what) / * N things to destroy.  * / int what;
	   */
	  if (parser_state_tos->in_parameter_declaration
	      && indent_parameters != 0 && parser_state_tos->dec_nest == 0)
	    {
	      parser_state_tos->com_col = indent_parameters + 1;
	      parser_state_tos->ind_stmt = 0;
	    }
	}
      else
	{
	  register target_col;
	  break_delim = 0;
	  if (s_code != e_code)
	    target_col = count_spaces (compute_code_target (), s_code);
	  else
	    {
	      target_col = 1;
	      if (s_lab != e_lab)
		target_col = count_spaces (compute_label_target (), s_lab);
	    }
	  parser_state_tos->com_col = parser_state_tos->decl_on_line
	    || parser_state_tos->ind_level == 0 ? decl_com_ind : com_ind;
	  /* If we are already past the position for the comment, put it at
	     the next tab stop.  */
	  if (parser_state_tos->com_col < target_col)
	    parser_state_tos->com_col = ((target_col + (tabsize - 1))
					 & ~(tabsize - 1)) + 1;
	  if (else_or_endif)
	    {
	      parser_state_tos->com_col = else_endif_col;
	      else_or_endif = false;
	      /* We want the comment to appear one space after the #else or
	         #endif.  */
	      if (parser_state_tos->com_col < target_col)
		parser_state_tos->com_col = target_col + 1;
	    }
	  if (parser_state_tos->com_col + 24 > adj_max_col)
	    adj_max_col = parser_state_tos->com_col + 24;
	}
    }
  if (parser_state_tos->box_com)
    {
      parser_state_tos->n_comment_delta = 1 - parser_state_tos->com_col;
#if 0
      buf_ptr[-2] = 0;
      parser_state_tos->n_comment_delta = 1 - count_spaces (1, cur_line);
      buf_ptr[-2] = '/';
#endif
    }
  else
    {
      parser_state_tos->n_comment_delta = 0;
      while (*buf_ptr == ' ' || *buf_ptr == '\t')
	buf_ptr++;
    }
  parser_state_tos->comment_delta = 0;
  *e_com++ = '/';		/* put '/*' into buffer */
  *e_com++ = '*';
  if (*buf_ptr != ' ' && !parser_state_tos->box_com)
    *e_com++ = ' ';

  *e_com = '\0';
  if (troff)
    {
      now_col = 1;
      adj_max_col = 80;
    }
  else
    /* figure what column we would be in if we printed the comment now */
    now_col = count_spaces (parser_state_tos->com_col, s_com);

  /* Start to copy the comment */

  while (1)
    {				/* this loop will go until the comment is
				   copied */
      if (*buf_ptr > 040 && *buf_ptr != '*')
	parser_state_tos->last_nl = 0;
      check_com_size;
      switch (*buf_ptr)
	{			/* this checks for various spcl cases */
	case 014:		/* check for a form feed */
	  if (!parser_state_tos->box_com)
	    {			/* in a text comment, break the line here */
	      parser_state_tos->use_ff = true;
	      /* fix so dump_line uses a form feed */
	      dump_line ();
	      last_bl = 0;
	      *e_com++ = ' ';
	      *e_com++ = '*';
	      *e_com++ = ' ';
	      while (*++buf_ptr == ' ' || *buf_ptr == '\t');
	    }
	  else
	    {
	      if (++buf_ptr >= buf_end)
		fill_buffer ();
	      *e_com++ = 014;
	    }
	  break;

	case '\n':
	  if (had_eof)
	    {			/* check for unexpected eof */
	      printf ("Unterminated comment\n");
	      *e_com = '\0';
	      dump_line ();
	      return;
	    }
	  one_liner = 0;
	  if (parser_state_tos->box_com || parser_state_tos->last_nl)
	    {			/* if this is a boxed comment, we dont ignore
				   the newline */
	      if (s_com == e_com)
		{
		  *e_com++ = ' ';
		  *e_com++ = ' ';
		}
	      *e_com = '\0';
	      if (!parser_state_tos->box_com && e_com - s_com > 3)
		{
		  if (break_delim == 1 && s_com[0] == '/'
		      && s_com[1] == '*' && s_com[2] == ' ')
		    {
		      char *t = e_com;
		      break_delim = 2;
		      e_com = s_com + 2;
		      *e_com = 0;
		      if (blanklines_before_blockcomments)
			prefix_blankline_requested = 1;
		      dump_line ();
		      e_com = t;
		      s_com[0] = s_com[1] = s_com[2] = ' ';
		    }
		  dump_line ();
		  check_com_size;
		  *e_com++ = ' ';
		  *e_com++ = ' ';
		}
	      dump_line ();
	      now_col = parser_state_tos->com_col;
	    }
	  else
	    {
	      parser_state_tos->last_nl = 1;
	      if (unix_comment != 1)
		{		/* we not are in unix_style comment */
		  if (unix_comment == 0 && s_code == e_code)
		    {
		      /* if it is a UNIX-style comment, ignore the
		         requirement that previous line be blank for
		         unindention */
		      parser_state_tos->com_col
			= ((parser_state_tos->ind_level - unindent_displace)
			   + ind_size);
		      if (parser_state_tos->com_col <= 1)
			parser_state_tos->com_col = 2;
		    }
		  unix_comment = 2;	/* permanently remember that we are
					   in this type of comment */
		  dump_line ();
		  ++line_no;
		  now_col = parser_state_tos->com_col;
		  *e_com++ = ' ';
		  /* fix so that the star at the start of the line will line
		     up */
		  do		/* flush leading white space */
		    if (++buf_ptr >= buf_end)
		      fill_buffer ();
		  while (*buf_ptr == ' ' || *buf_ptr == '\t');
		  break;
		}
	      if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
		last_bl = e_com - 1;
	      /* if there was a space at the end of the last line, remember
	         where it was */
	      else
		{		/* otherwise, insert one */
		  last_bl = e_com;
		  check_com_size;
		  *e_com++ = ' ';
		  ++now_col;
		}
	    }
	  ++line_no;		/* keep track of input line number */
	  if (!parser_state_tos->box_com)
	    {
	      int nstar = 1;
	      do
		{		/* flush any blanks and/or tabs at start of
				   next line */
		  if (++buf_ptr >= buf_end)
		    fill_buffer ();
		  if (*buf_ptr == '*' && --nstar >= 0)
		    {
		      if (++buf_ptr >= buf_end)
			fill_buffer ();
		      if (*buf_ptr == '/')
			goto end_of_comment;
		    }
		}
	      while (*buf_ptr == ' ' || *buf_ptr == '\t');
	    }
	  else if (++buf_ptr >= buf_end)
	    fill_buffer ();
	  break;		/* end of case for newline */

	case '*':		/* must check for possibility of being at end
				   of comment */
	  if (++buf_ptr >= buf_end)	/* get to next char after * */
	    fill_buffer ();

	  if (unix_comment == 0)	/* set flag to show we are not in unix-style
					   comment */
	    unix_comment = 1;

	  if (*buf_ptr == '/')
	    {			/* it is the end!!! */
	    end_of_comment:
	      if (++buf_ptr >= buf_end)
		fill_buffer ();

	      if (*(e_com - 1) != ' ' && !parser_state_tos->box_com)
		{		/* insure blank before end */
		  *e_com++ = ' ';
		  ++now_col;
		}
	      if (break_delim == 1 && !one_liner && s_com[0] == '/'
		  && s_com[1] == '*' && s_com[2] == ' ')
		{
		  char *t = e_com;
		  break_delim = 2;
		  e_com = s_com + 2;
		  *e_com = 0;
		  if (blanklines_before_blockcomments)
		    prefix_blankline_requested = 1;
		  dump_line ();
		  e_com = t;
		  s_com[0] = s_com[1] = s_com[2] = ' ';
		}
	      if (break_delim == 2 && e_com > s_com + 3
		  /* now_col > adj_max_col - 2 && !parser_state_tos->box_com */
		  )
		{
		  *e_com = '\0';
		  dump_line ();
		  now_col = parser_state_tos->com_col;
		}
	      check_com_size;
	      *e_com++ = '*';
	      *e_com++ = '/';
	      *e_com = '\0';
	      parser_state_tos->just_saw_decl = l_just_saw_decl;
	      return;
	    }
	  else
	    {			/* handle isolated '*' */
	      *e_com++ = '*';
	      ++now_col;
	    }
	  break;
	default:		/* we have a random char */
	  if (unix_comment == 0 && *buf_ptr != ' ' && *buf_ptr != '\t')
	    unix_comment = 1;	/* we are not in unix-style comment */

	  *e_com = *buf_ptr++;
	  if (buf_ptr >= buf_end)
	    fill_buffer ();

	  if (*e_com == '\t')	/* keep track of column */
	    now_col = now_col + tabsize - (now_col - 1) % tabsize;
	  else if (*e_com == '\b')	/* this is a backspace */
	    --now_col;
	  else
	    ++now_col;

	  if (*e_com == ' ' || *e_com == '\t')
	    last_bl = e_com;
	  /* remember we saw a blank */

	  ++e_com;
	  if (now_col > adj_max_col
	      && !parser_state_tos->box_com
	      && unix_comment == 1 && e_com[-1] > ' ')
	    {
	      /* the comment is too long, it must be broken up */
	      if (break_delim == 1 && s_com[0] == '/'
		  && s_com[1] == '*' && s_com[2] == ' ')
		{
		  char *t = e_com;
		  break_delim = 2;
		  e_com = s_com + 2;
		  *e_com = 0;
		  if (blanklines_before_blockcomments)
		    prefix_blankline_requested = 1;
		  dump_line ();
		  e_com = t;
		  s_com[0] = s_com[1] = s_com[2] = ' ';
		}
	      if (last_bl == 0)
		{		/* we have seen no blanks */
		  last_bl = e_com;	/* fake it */
		  *e_com++ = ' ';
		}
	      *e_com = '\0';	/* print what we have */
	      *last_bl = '\0';
	      while (last_bl > s_com && last_bl[-1] < 040)
		*--last_bl = 0;
	      e_com = last_bl;
	      dump_line ();

	      *e_com++ = ' ';	/* add blanks for continuation */
	      *e_com++ = ' ';
	      *e_com++ = ' ';

	      t_ptr = last_bl + 1;
	      last_bl = 0;
	      if (t_ptr >= e_com)
		{
		  while (*t_ptr == ' ' || *t_ptr == '\t')
		    t_ptr++;
		  while (*t_ptr != '\0')
		    {		/* move unprinted part of comment down in
				   buffer */
		      if (*t_ptr == ' ' || *t_ptr == '\t')
			last_bl = e_com;
		      *e_com++ = *t_ptr++;
		    }
		}
	      *e_com = '\0';
	      /* recompute current position */
	      now_col = count_spaces (parser_state_tos->com_col, s_com);
	    }
	  break;
	}
    }
}
Ejemplo n.º 2
0
void
pr_comment(void)
{
    int         now_col;	/* column we are in now */
    int         adj_max_col;	/* Adjusted max_col for when we decide to
				 * spill comments over the right margin */
    char       *last_bl;	/* points to the last blank in the output
				 * buffer */
    char       *t_ptr;		/* used for moving string */
    int         unix_comment;	/* tri-state variable used to decide if it is
				 * a unix-style comment. 0 means only blanks
				 * since /+*, 1 means regular style comment, 2
				 * means unix style comment */
    int         break_delim = comment_delimiter_on_blankline;
    int         l_just_saw_decl = ps.just_saw_decl;
    /*
     * int         ps.last_nl = 0;	 true iff the last significant thing
     * weve seen is a newline
     */
    int         one_liner = 1;	/* true iff this comment is a one-liner */
    adj_max_col = max_col;
    ps.just_saw_decl = 0;
    last_bl = 0;		/* no blanks found so far */
    ps.box_com = false;		/* at first, assume that we are not in
					 * a boxed comment or some other
					 * comment that should not be touched */
    ++ps.out_coms;		/* keep track of number of comments */
    unix_comment = 1;		/* set flag to let us figure out if there is a
				 * unix-style comment ** DISABLED: use 0 to
				 * reenable this hack! */

    /* Figure where to align and how to treat the comment */

    if (ps.col_1 && !format_col1_comments) {
        /* if comment starts in column
        					 * 1 it should not be touched */
        ps.box_com = true;
        ps.com_col = 1;
    }
    else {
        if (*buf_ptr == '-' || *buf_ptr == '*' ||
                (*buf_ptr == '\n' && !format_block_comments)) {
            ps.box_com = true;	/* A comment with a '-' or '*' immediately
				 * after the /+* is assumed to be a boxed
				 * comment. A comment with a newline
				 * immediately after the /+* is assumed to
				 * be a block comment and is treated as a
				 * box comment unless format_block_comments
				 * is nonzero (the default). */
            break_delim = 0;
        }
        if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) {
            /* klg: check only if this line is blank */
            /*
             * If this (*and previous lines are*) blank, dont put comment way
             * out at left
             */
            ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
            adj_max_col = block_comment_max_col;
            if (ps.com_col <= 1)
                ps.com_col = 1 + !format_col1_comments;
        }
        else {
            int target_col;
            break_delim = 0;
            if (s_code != e_code)
                target_col = count_spaces(compute_code_target(), s_code);
            else {
                target_col = 1;
                if (s_lab != e_lab)
                    target_col = count_spaces(compute_label_target(), s_lab);
            }
            ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? ps.decl_com_ind : ps.com_ind;
            if (ps.com_col < target_col)
                ps.com_col = ((target_col + 7) & ~7) + 1;
            if (ps.com_col + 24 > adj_max_col)
                adj_max_col = ps.com_col + 24;
        }
    }
    if (ps.box_com) {
        buf_ptr[-2] = 0;
        ps.n_comment_delta = 1 - count_spaces(1, in_buffer);
        buf_ptr[-2] = '/';
    }
    else {
        ps.n_comment_delta = 0;
        while (*buf_ptr == ' ' || *buf_ptr == '\t')
            buf_ptr++;
    }
    ps.comment_delta = 0;
    *e_com++ = '/';		/* put '/' followed by '*' into buffer */
    *e_com++ = '*';
    if (*buf_ptr != ' ' && !ps.box_com)
        *e_com++ = ' ';

    *e_com = '\0';
    if (troff) {
        now_col = 1;
        adj_max_col = 80;
    }
    else
        now_col = count_spaces(ps.com_col, s_com);	/* figure what column we
							 * would be in if we
							 * printed the comment
							 * now */

    /* Start to copy the comment */

    while (1) {
        /* this loop will go until the comment is
        			 * copied */
        if (*buf_ptr > 040 && *buf_ptr != '*')
            ps.last_nl = 0;
        CHECK_SIZE_COM;
        switch (*buf_ptr) {	/* this checks for various spcl cases */
        case 014:		/* check for a form feed */
            if (!ps.box_com) {	/* in a text comment, break the line here */
                ps.use_ff = true;
                /* fix so dump_line uses a form feed */
                dump_line();
                last_bl = 0;
                *e_com++ = ' ';
                *e_com++ = '*';
                *e_com++ = ' ';
                while (*++buf_ptr == ' ' || *buf_ptr == '\t');
            }
            else {
                if (++buf_ptr >= buf_end)
                    fill_buffer();
                *e_com++ = 014;
            }
            break;

        case '\n':
            if (had_eof) {	/* check for unexpected eof */
                printf("Unterminated comment\n");
                *e_com = '\0';
                dump_line();
                return;
            }
            one_liner = 0;
            if (ps.box_com || ps.last_nl) {
                /* if this is a boxed comment,
                				 * we dont ignore the newline */
                if (s_com == e_com) {
                    *e_com++ = ' ';
                    *e_com++ = ' ';
                }
                *e_com = '\0';
                if (!ps.box_com && e_com - s_com > 3) {
                    if (break_delim == 1 && s_com[0] == '/'
                            && s_com[1] == '*' && s_com[2] == ' ') {
                        char       *t = e_com;
                        break_delim = 2;
                        e_com = s_com + 2;
                        *e_com = 0;
                        if (blanklines_before_blockcomments)
                            prefix_blankline_requested = 1;
                        dump_line();
                        e_com = t;
                        s_com[0] = s_com[1] = s_com[2] = ' ';
                    }
                    dump_line();
                    CHECK_SIZE_COM;
                    *e_com++ = ' ';
                    *e_com++ = ' ';
                }
                dump_line();
                now_col = ps.com_col;
            }
            else {
                ps.last_nl = 1;
                if (unix_comment != 1) {
                    /* we not are in unix_style
                    				 * comment */
                    if (unix_comment == 0 && s_code == e_code) {
                        /*
                         * if it is a UNIX-style comment, ignore the
                         * requirement that previous line be blank for
                         * unindention
                         */
                        ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
                        if (ps.com_col <= 1)
                            ps.com_col = 2;
                    }
                    unix_comment = 2;	/* permanently remember that we are in
					 * this type of comment */
                    dump_line();
                    ++line_no;
                    now_col = ps.com_col;
                    *e_com++ = ' ';
                    /*
                     * fix so that the star at the start of the line will line
                     * up
                     */
                    do		/* flush leading white space */
                        if (++buf_ptr >= buf_end)
                            fill_buffer();
                    while (*buf_ptr == ' ' || *buf_ptr == '\t');
                    break;
                }
                if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
                    last_bl = e_com - 1;
                /*
                 * if there was a space at the end of the last line, remember
                 * where it was
                 */
                else {		/* otherwise, insert one */
                    last_bl = e_com;
                    CHECK_SIZE_COM;
                    *e_com++ = ' ';
                    ++now_col;
                }
            }
            ++line_no;		/* keep track of input line number */
            if (!ps.box_com) {
                int         nstar = 1;
                do {
                    /* flush any blanks and/or tabs at start of
                    		 * next line */
                    if (++buf_ptr >= buf_end)
                        fill_buffer();
                    if (*buf_ptr == '*' && --nstar >= 0) {
                        if (++buf_ptr >= buf_end)
                            fill_buffer();
                        if (*buf_ptr == '/')
                            goto end_of_comment;
                    }
                } while (*buf_ptr == ' ' || *buf_ptr == '\t');
            }
            else if (++buf_ptr >= buf_end)
                fill_buffer();
            break;		/* end of case for newline */

        case '*':		/* must check for possibility of being at end
				 * of comment */
            if (++buf_ptr >= buf_end)	/* get to next char after * */
                fill_buffer();

            if (unix_comment == 0)	/* set flag to show we are not in
					 * unix-style comment */
                unix_comment = 1;

            if (*buf_ptr == '/') {	/* it is the end!!! */
end_of_comment:
                if (++buf_ptr >= buf_end)
                    fill_buffer();

                if (*(e_com - 1) != ' ' && !ps.box_com) {
                    /* insure blank before
                    						 * end */
                    *e_com++ = ' ';
                    ++now_col;
                }
                if (break_delim == 1 && !one_liner && s_com[0] == '/'
                        && s_com[1] == '*' && s_com[2] == ' ') {
                    char       *t = e_com;
                    break_delim = 2;
                    e_com = s_com + 2;
                    *e_com = 0;
                    if (blanklines_before_blockcomments)
                        prefix_blankline_requested = 1;
                    dump_line();
                    e_com = t;
                    s_com[0] = s_com[1] = s_com[2] = ' ';
                }
                if (break_delim == 2 && e_com > s_com + 3
                        /* now_col > adj_max_col - 2 && !ps.box_com */ ) {
                    *e_com = '\0';
                    dump_line();
                    now_col = ps.com_col;
                }
                CHECK_SIZE_COM;
                *e_com++ = '*';
                *e_com++ = '/';
                *e_com = '\0';
                ps.just_saw_decl = l_just_saw_decl;
                return;
            }
            else {		/* handle isolated '*' */
                *e_com++ = '*';
                ++now_col;
            }
            break;
        default:		/* we have a random char */
            if (unix_comment == 0 && *buf_ptr != ' ' && *buf_ptr != '\t')
                unix_comment = 1;	/* we are not in unix-style comment */

            *e_com = *buf_ptr++;
            if (buf_ptr >= buf_end)
                fill_buffer();

            if (*e_com == '\t')	/* keep track of column */
                now_col = ((now_col - 1) & tabmask) + tabsize + 1;
            else if (*e_com == '\b')	/* this is a backspace */
                --now_col;
            else
                ++now_col;

            if (*e_com == ' ' || *e_com == '\t')
                last_bl = e_com;
            /* remember we saw a blank */

            ++e_com;
            if (now_col > adj_max_col && !ps.box_com && unix_comment == 1 && e_com[-1] > ' ') {
                /*
                 * the comment is too long, it must be broken up
                 */
                if (break_delim == 1 && s_com[0] == '/'
                        && s_com[1] == '*' && s_com[2] == ' ') {
                    char       *t = e_com;
                    break_delim = 2;
                    e_com = s_com + 2;
                    *e_com = 0;
                    if (blanklines_before_blockcomments)
                        prefix_blankline_requested = 1;
                    dump_line();
                    e_com = t;
                    s_com[0] = s_com[1] = s_com[2] = ' ';
                }
                if (last_bl == 0) {	/* we have seen no blanks */
                    last_bl = e_com;	/* fake it */
                    *e_com++ = ' ';
                }
                *e_com = '\0';	/* print what we have */
                *last_bl = '\0';
                while (last_bl > s_com && last_bl[-1] < 040)
                    *--last_bl = 0;
                e_com = last_bl;
                dump_line();

                *e_com++ = ' ';	/* add blanks for continuation */
                *e_com++ = ' ';
                *e_com++ = ' ';

                t_ptr = last_bl + 1;
                last_bl = 0;
                if (t_ptr >= e_com) {
                    while (*t_ptr == ' ' || *t_ptr == '\t')
                        t_ptr++;
                    while (*t_ptr != '\0') {
                        /* move unprinted part of
                        			 * comment down in buffer */
                        if (*t_ptr == ' ' || *t_ptr == '\t')
                            last_bl = e_com;
                        *e_com++ = *t_ptr++;
                    }
                }
                *e_com = '\0';
                now_col = count_spaces(ps.com_col, s_com);	/* recompute current
								 * position */
            }
            break;
        }
    }
}
Ejemplo n.º 3
0
int
main(int argc, char **argv)
{
    cap_rights_t rights;

    int         dec_ind;	/* current indentation for declarations */
    int         di_stack[20];	/* a stack of structure indentation levels */
    int         force_nl;	/* when true, code must be broken */
    int         hd_type = 0;	/* used to store type of stmt for if (...),
				 * for (...), etc */
    int		i;		/* local loop counter */
    int         scase;		/* set to true when we see a case, so we will
				 * know what to do with the following colon */
    int         sp_sw;		/* when true, we are in the expression of
				 * if(...), while(...), etc. */
    int         squest;		/* when this is positive, we have seen a ?
				 * without the matching : in a <c>?<s>:<s>
				 * construct */
    const char *t_ptr;		/* used for copying tokens */
    int		tabs_to_var;	/* true if using tabs to indent to var name */
    int         type_code;	/* the type of token, returned by lexi */

    int         last_else = 0;	/* true iff last keyword was an else */
    const char *profile_name = NULL;
    const char *envval = NULL;
    struct parser_state transient_state; /* a copy for lookup */

    /*-----------------------------------------------*\
    |		      INITIALIZATION		      |
    \*-----------------------------------------------*/

    found_err = 0;

    ps.p_stack[0] = stmt;	/* this is the parser's stack */
    ps.last_nl = true;		/* this is true if the last thing scanned was
				 * a newline */
    ps.last_token = semicolon;
    combuf = (char *) malloc(bufsize);
    if (combuf == NULL)
	err(1, NULL);
    labbuf = (char *) malloc(bufsize);
    if (labbuf == NULL)
	err(1, NULL);
    codebuf = (char *) malloc(bufsize);
    if (codebuf == NULL)
	err(1, NULL);
    tokenbuf = (char *) malloc(bufsize);
    if (tokenbuf == NULL)
	err(1, NULL);
    alloc_typenames();
    l_com = combuf + bufsize - 5;
    l_lab = labbuf + bufsize - 5;
    l_code = codebuf + bufsize - 5;
    l_token = tokenbuf + bufsize - 5;
    combuf[0] = codebuf[0] = labbuf[0] = ' ';	/* set up code, label, and
						 * comment buffers */
    combuf[1] = codebuf[1] = labbuf[1] = '\0';
    ps.else_if = 1;		/* Default else-if special processing to on */
    s_lab = e_lab = labbuf + 1;
    s_code = e_code = codebuf + 1;
    s_com = e_com = combuf + 1;
    s_token = e_token = tokenbuf + 1;

    in_buffer = (char *) malloc(10);
    if (in_buffer == NULL)
	err(1, NULL);
    in_buffer_limit = in_buffer + 8;
    buf_ptr = buf_end = in_buffer;
    line_no = 1;
    had_eof = ps.in_decl = ps.decl_on_line = break_comma = false;
    sp_sw = force_nl = false;
    ps.in_or_st = false;
    ps.bl_line = true;
    dec_ind = 0;
    di_stack[ps.dec_nest = 0] = 0;
    ps.want_blank = ps.in_stmt = ps.ind_stmt = false;

    scase = ps.pcase = false;
    squest = 0;
    sc_end = NULL;
    bp_save = NULL;
    be_save = NULL;

    output = NULL;
    tabs_to_var = 0;

    envval = getenv("SIMPLE_BACKUP_SUFFIX");
    if (envval)
        simple_backup_suffix = envval;

    /*--------------------------------------------------*\
    |   		COMMAND LINE SCAN		 |
    \*--------------------------------------------------*/

#ifdef undef
    max_col = 78;		/* -l78 */
    lineup_to_parens = 1;	/* -lp */
    lineup_to_parens_always = 0;	/* -nlpl */
    ps.ljust_decl = 0;		/* -ndj */
    ps.com_ind = 33;		/* -c33 */
    star_comment_cont = 1;	/* -sc */
    ps.ind_size = 8;		/* -i8 */
    verbose = 0;
    ps.decl_indent = 16;	/* -di16 */
    ps.local_decl_indent = -1;	/* if this is not set to some nonnegative value
				 * by an arg, we will set this equal to
				 * ps.decl_ind */
    ps.indent_parameters = 1;	/* -ip */
    ps.decl_com_ind = 0;	/* if this is not set to some positive value
				 * by an arg, we will set this equal to
				 * ps.com_ind */
    btype_2 = 1;		/* -br */
    cuddle_else = 1;		/* -ce */
    ps.unindent_displace = 0;	/* -d0 */
    ps.case_indent = 0;		/* -cli0 */
    format_block_comments = 1;	/* -fcb */
    format_col1_comments = 1;	/* -fc1 */
    procnames_start_line = 1;	/* -psl */
    proc_calls_space = 0;	/* -npcs */
    comment_delimiter_on_blankline = 1;	/* -cdb */
    ps.leave_comma = 1;		/* -nbc */
#endif

    for (i = 1; i < argc; ++i)
	if (strcmp(argv[i], "-npro") == 0)
	    break;
	else if (argv[i][0] == '-' && argv[i][1] == 'P' && argv[i][2] != '\0')
	    profile_name = argv[i];	/* non-empty -P (set profile) */
    set_defaults();
    if (i >= argc)
	set_profile(profile_name);

    for (i = 1; i < argc; ++i) {

	/*
	 * look thru args (if any) for changes to defaults
	 */
	if (argv[i][0] != '-') {/* no flag on parameter */
	    if (input == NULL) {	/* we must have the input file */
		in_name = argv[i];	/* remember name of input file */
		input = fopen(in_name, "r");
		if (input == NULL)	/* check for open error */
			err(1, "%s", in_name);
		continue;
	    }
	    else if (output == NULL) {	/* we have the output file */
		out_name = argv[i];	/* remember name of output file */
		if (strcmp(in_name, out_name) == 0) {	/* attempt to overwrite
							 * the file */
		    errx(1, "input and output files must be different");
		}
		output = fopen(out_name, "w");
		if (output == NULL)	/* check for create error */
			err(1, "%s", out_name);
		continue;
	    }
	    errx(1, "unknown parameter: %s", argv[i]);
	}
	else
	    set_option(argv[i]);
    }				/* end of for */
    if (input == NULL)
	input = stdin;
    if (output == NULL) {
	if (input == stdin)
	    output = stdout;
	else {
	    out_name = in_name;
	    bakcopy();
	}
    }

    /* Restrict input/output descriptors and enter Capsicum sandbox. */
    cap_rights_init(&rights, CAP_FSTAT, CAP_WRITE);
    if (cap_rights_limit(fileno(output), &rights) < 0 && errno != ENOSYS)
	err(EXIT_FAILURE, "unable to limit rights for %s", out_name);
    cap_rights_init(&rights, CAP_FSTAT, CAP_READ);
    if (cap_rights_limit(fileno(input), &rights) < 0 && errno != ENOSYS)
	err(EXIT_FAILURE, "unable to limit rights for %s", in_name);
    if (cap_enter() < 0 && errno != ENOSYS)
	err(EXIT_FAILURE, "unable to enter capability mode");

    if (ps.com_ind <= 1)
	ps.com_ind = 2;		/* dont put normal comments before column 2 */
    if (block_comment_max_col <= 0)
	block_comment_max_col = max_col;
    if (ps.local_decl_indent < 0)	/* if not specified by user, set this */
	ps.local_decl_indent = ps.decl_indent;
    if (ps.decl_com_ind <= 0)	/* if not specified by user, set this */
	ps.decl_com_ind = ps.ljust_decl ? (ps.com_ind <= 10 ? 2 : ps.com_ind - 8) : ps.com_ind;
    if (continuation_indent == 0)
	continuation_indent = ps.ind_size;
    fill_buffer();		/* get first batch of stuff into input buffer */

    parse(semicolon);
    {
	char *p = buf_ptr;
	int col = 1;

	while (1) {
	    if (*p == ' ')
		col++;
	    else if (*p == '\t')
		col = tabsize * (1 + (col - 1) / tabsize) + 1;
	    else
		break;
	    p++;
	}
	if (col > ps.ind_size)
	    ps.ind_level = ps.i_l_follow = col / ps.ind_size;
    }

    /*
     * START OF MAIN LOOP
     */

    while (1) {			/* this is the main loop.  it will go until we
				 * reach eof */
	int comment_buffered = false;

	type_code = lexi(&ps);	/* lexi reads one token.  The actual
				 * characters read are stored in "token". lexi
				 * returns a code indicating the type of token */

	/*
	 * The following code moves newlines and comments following an if (),
	 * while (), else, etc. up to the start of the following stmt to
	 * a buffer. This allows proper handling of both kinds of brace
	 * placement (-br, -bl) and cuddling "else" (-ce).
	 */

	while (ps.search_brace) {
	    switch (type_code) {
	    case newline:
		if (sc_end == NULL) {
		    save_com = sc_buf;
		    save_com[0] = save_com[1] = ' ';
		    sc_end = &save_com[2];
		}
		*sc_end++ = '\n';
		/*
		 * We may have inherited a force_nl == true from the previous
		 * token (like a semicolon). But once we know that a newline
		 * has been scanned in this loop, force_nl should be false.
		 *
		 * However, the force_nl == true must be preserved if newline
		 * is never scanned in this loop, so this assignment cannot be
		 * done earlier.
		 */
		force_nl = false;
	    case form_feed:
		break;
	    case comment:
		if (sc_end == NULL) {
		    /*
		     * Copy everything from the start of the line, because
		     * pr_comment() will use that to calculate original
		     * indentation of a boxed comment.
		     */
		    memcpy(sc_buf, in_buffer, buf_ptr - in_buffer - 4);
		    save_com = sc_buf + (buf_ptr - in_buffer - 4);
		    save_com[0] = save_com[1] = ' ';
		    sc_end = &save_com[2];
		}
		comment_buffered = true;
		*sc_end++ = '/';	/* copy in start of comment */
		*sc_end++ = '*';
		for (;;) {	/* loop until we get to the end of the comment */
		    *sc_end = *buf_ptr++;
		    if (buf_ptr >= buf_end)
			fill_buffer();
		    if (*sc_end++ == '*' && *buf_ptr == '/')
			break;	/* we are at end of comment */
		    if (sc_end >= &save_com[sc_size]) {	/* check for temp buffer
							 * overflow */
			diag2(1, "Internal buffer overflow - Move big comment from right after if, while, or whatever");
			fflush(output);
			exit(1);
		    }
		}
		*sc_end++ = '/';	/* add ending slash */
		if (++buf_ptr >= buf_end)	/* get past / in buffer */
		    fill_buffer();
		break;
	    case lbrace:
		/*
		 * Put KNF-style lbraces before the buffered up tokens and
		 * jump out of this loop in order to avoid copying the token
		 * again under the default case of the switch below.
		 */
		if (sc_end != NULL && btype_2) {
		    save_com[0] = '{';
		    /*
		     * Originally the lbrace may have been alone on its own
		     * line, but it will be moved into "the else's line", so
		     * if there was a newline resulting from the "{" before,
		     * it must be scanned now and ignored.
		     */
		    while (isspace((unsigned char)*buf_ptr)) {
			if (++buf_ptr >= buf_end)
			    fill_buffer();
			if (*buf_ptr == '\n')
			    break;
		    }
		    goto sw_buffer;
		}
		/* FALLTHROUGH */
	    default:		/* it is the start of a normal statement */
		{
		    int remove_newlines;

		    remove_newlines =
			/* "} else" */
			(type_code == sp_nparen && *token == 'e' &&
			    e_code != s_code && e_code[-1] == '}')
			/* "else if" */
			|| (type_code == sp_paren && *token == 'i' &&
			    last_else && ps.else_if);
		    if (remove_newlines)
			force_nl = false;
		    if (sc_end == NULL) {	/* ignore buffering if
						 * comment wasn't saved up */
			ps.search_brace = false;
			goto check_type;
		    }
		    while (sc_end > save_com && isblank((unsigned char)sc_end[-1])) {
			sc_end--;
		    }
		    if (swallow_optional_blanklines ||
			(!comment_buffered && remove_newlines)) {
			force_nl = !remove_newlines;
			while (sc_end > save_com && sc_end[-1] == '\n') {
			    sc_end--;
			}
		    }
		    if (force_nl) {	/* if we should insert a nl here, put
					 * it into the buffer */
			force_nl = false;
			--line_no;	/* this will be re-increased when the
					 * newline is read from the buffer */
			*sc_end++ = '\n';
			*sc_end++ = ' ';
			if (verbose)	/* print error msg if the line was
					 * not already broken */
			    diag2(0, "Line broken");
		    }
		    for (t_ptr = token; *t_ptr; ++t_ptr)
			*sc_end++ = *t_ptr;

	    sw_buffer:
		    ps.search_brace = false;	/* stop looking for start of
						 * stmt */
		    bp_save = buf_ptr;	/* save current input buffer */
		    be_save = buf_end;
		    buf_ptr = save_com;	/* fix so that subsequent calls to
					 * lexi will take tokens out of
					 * save_com */
		    *sc_end++ = ' ';/* add trailing blank, just in case */
		    buf_end = sc_end;
		    sc_end = NULL;
		    break;
		}
	    }			/* end of switch */
	    /*
	     * We must make this check, just in case there was an unexpected
	     * EOF.
	     */
	    if (type_code != 0) {
		/*
		 * The only intended purpose of calling lexi() below is to
		 * categorize the next token in order to decide whether to
		 * continue buffering forthcoming tokens. Once the buffering
		 * is over, lexi() will be called again elsewhere on all of
		 * the tokens - this time for normal processing.
		 *
		 * Calling it for this purpose is a bug, because lexi() also
		 * changes the parser state and discards leading whitespace,
		 * which is needed mostly for comment-related considerations.
		 *
		 * Work around the former problem by giving lexi() a copy of
		 * the current parser state and discard it if the call turned
		 * out to be just a look ahead.
		 *
		 * Work around the latter problem by copying all whitespace
		 * characters into the buffer so that the later lexi() call
		 * will read them.
		 */
		if (sc_end != NULL) {
		    while (*buf_ptr == ' ' || *buf_ptr == '\t') {
			*sc_end++ = *buf_ptr++;
			if (sc_end >= &save_com[sc_size]) {
			    errx(1, "input too long");
			}
		    }
		    if (buf_ptr >= buf_end) {
			fill_buffer();
		    }
		}
		transient_state = ps;
		type_code = lexi(&transient_state);	/* read another token */
		if (type_code != newline && type_code != form_feed &&
		    type_code != comment && !transient_state.search_brace) {
		    ps = transient_state;
		}
	    }
	}			/* end of while (search_brace) */
	last_else = 0;
check_type:
	if (type_code == 0) {	/* we got eof */
	    if (s_lab != e_lab || s_code != e_code
		    || s_com != e_com)	/* must dump end of line */
		dump_line();
	    if (ps.tos > 1)	/* check for balanced braces */
		diag2(1, "Stuff missing from end of file");

	    if (verbose) {
		printf("There were %d output lines and %d comments\n",
		       ps.out_lines, ps.out_coms);
		printf("(Lines with comments)/(Lines with code): %6.3f\n",
		       (1.0 * ps.com_lines) / code_lines);
	    }
	    fflush(output);
	    exit(found_err);
	}
	if (
		(type_code != comment) &&
		(type_code != newline) &&
		(type_code != preesc) &&
		(type_code != form_feed)) {
	    if (force_nl &&
		    (type_code != semicolon) &&
		    (type_code != lbrace || !btype_2)) {
		/* we should force a broken line here */
		if (verbose)
		    diag2(0, "Line broken");
		dump_line();
		ps.want_blank = false;	/* dont insert blank at line start */
		force_nl = false;
	    }
	    ps.in_stmt = true;	/* turn on flag which causes an extra level of
				 * indentation. this is turned off by a ; or
				 * '}' */
	    if (s_com != e_com) {	/* the turkey has embedded a comment
					 * in a line. fix it */
		int len = e_com - s_com;

		CHECK_SIZE_CODE(len + 3);
		*e_code++ = ' ';
		memcpy(e_code, s_com, len);
		e_code += len;
		*e_code++ = ' ';
		*e_code = '\0';	/* null terminate code sect */
		ps.want_blank = false;
		e_com = s_com;
	    }
	}
	else if (type_code != comment)	/* preserve force_nl thru a comment */
	    force_nl = false;	/* cancel forced newline after newline, form
				 * feed, etc */



	/*-----------------------------------------------------*\
	|	   do switch on type of token scanned		|
	\*-----------------------------------------------------*/
	CHECK_SIZE_CODE(3);	/* maximum number of increments of e_code
				 * before the next CHECK_SIZE_CODE or
				 * dump_line() is 2. After that there's the
				 * final increment for the null character. */
	switch (type_code) {	/* now, decide what to do with the token */

	case form_feed:	/* found a form feed in line */
	    ps.use_ff = true;	/* a form feed is treated much like a newline */
	    dump_line();
	    ps.want_blank = false;
	    break;

	case newline:
	    if (ps.last_token != comma || ps.p_l_follow > 0
		    || !ps.leave_comma || ps.block_init || !break_comma || s_com != e_com) {
		dump_line();
		ps.want_blank = false;
	    }
	    ++line_no;		/* keep track of input line number */
	    break;

	case lparen:		/* got a '(' or '[' */
	    /* count parens to make Healy happy */
	    if (++ps.p_l_follow == nitems(ps.paren_indents)) {
		diag3(0, "Reached internal limit of %d unclosed parens",
		    nitems(ps.paren_indents));
		ps.p_l_follow--;
	    }
	    if (*token == '[')
		/* not a function pointer declaration or a function call */;
	    else if (ps.in_decl && !ps.block_init && !ps.dumped_decl_indent &&
		ps.procname[0] == '\0' && ps.paren_level == 0) {
		/* function pointer declarations */
		indent_declaration(dec_ind, tabs_to_var);
		ps.dumped_decl_indent = true;
	    }
	    else if (ps.want_blank &&
		    ((ps.last_token != ident && ps.last_token != funcname) ||
		    proc_calls_space ||
		    /* offsetof (1) is never allowed a space; sizeof (2) gets
		     * one iff -bs; all other keywords (>2) always get a space
		     * before lparen */
			ps.keyword + Bill_Shannon > 2))
		*e_code++ = ' ';
	    ps.want_blank = false;
	    *e_code++ = token[0];
	    ps.paren_indents[ps.p_l_follow - 1] = count_spaces_until(1, s_code, e_code) - 1;
	    if (sp_sw && ps.p_l_follow == 1 && extra_expression_indent
		    && ps.paren_indents[0] < 2 * ps.ind_size)
		ps.paren_indents[0] = 2 * ps.ind_size;
	    if (ps.in_or_st && *token == '(' && ps.tos <= 2) {
		/*
		 * this is a kluge to make sure that declarations will be
		 * aligned right if proc decl has an explicit type on it, i.e.
		 * "int a(x) {..."
		 */
		parse(semicolon);	/* I said this was a kluge... */
		ps.in_or_st = false;	/* turn off flag for structure decl or
					 * initialization */
	    }
	    /* parenthesized type following sizeof or offsetof is not a cast */
	    if (ps.keyword == 1 || ps.keyword == 2)
		ps.not_cast_mask |= 1 << ps.p_l_follow;
	    break;

	case rparen:		/* got a ')' or ']' */
	    if (ps.cast_mask & (1 << ps.p_l_follow) & ~ps.not_cast_mask) {
		ps.last_u_d = true;
		ps.cast_mask &= (1 << ps.p_l_follow) - 1;
		ps.want_blank = space_after_cast;
	    } else
		ps.want_blank = true;
	    ps.not_cast_mask &= (1 << ps.p_l_follow) - 1;
	    if (--ps.p_l_follow < 0) {
		ps.p_l_follow = 0;
		diag3(0, "Extra %c", *token);
	    }
	    if (e_code == s_code)	/* if the paren starts the line */
		ps.paren_level = ps.p_l_follow;	/* then indent it */

	    *e_code++ = token[0];

	    if (sp_sw && (ps.p_l_follow == 0)) {	/* check for end of if
							 * (...), or some such */
		sp_sw = false;
		force_nl = true;/* must force newline after if */
		ps.last_u_d = true;	/* inform lexi that a following
					 * operator is unary */
		ps.in_stmt = false;	/* dont use stmt continuation
					 * indentation */

		parse(hd_type);	/* let parser worry about if, or whatever */
	    }
	    ps.search_brace = btype_2;	/* this should insure that constructs
					 * such as main(){...} and int[]{...}
					 * have their braces put in the right
					 * place */
	    break;

	case unary_op:		/* this could be any unary operation */
	    if (!ps.dumped_decl_indent && ps.in_decl && !ps.block_init &&
		ps.procname[0] == '\0' && ps.paren_level == 0) {
		/* pointer declarations */

		/*
		 * if this is a unary op in a declaration, we should indent
		 * this token
		 */
		for (i = 0; token[i]; ++i)
		    /* find length of token */;
		indent_declaration(dec_ind - i, tabs_to_var);
		ps.dumped_decl_indent = true;
	    }
	    else if (ps.want_blank)
		*e_code++ = ' ';

	    {
		int len = e_token - s_token;

		CHECK_SIZE_CODE(len);
		memcpy(e_code, token, len);
		e_code += len;
	    }
	    ps.want_blank = false;
	    break;

	case binary_op:	/* any binary operation */
	    {
		int len = e_token - s_token;

		CHECK_SIZE_CODE(len + 1);
		if (ps.want_blank)
		    *e_code++ = ' ';
		memcpy(e_code, token, len);
		e_code += len;
	    }
	    ps.want_blank = true;
	    break;

	case postop:		/* got a trailing ++ or -- */
	    *e_code++ = token[0];
	    *e_code++ = token[1];
	    ps.want_blank = true;
	    break;

	case question:		/* got a ? */
	    squest++;		/* this will be used when a later colon
				 * appears so we can distinguish the
				 * <c>?<n>:<n> construct */
	    if (ps.want_blank)
		*e_code++ = ' ';
	    *e_code++ = '?';
	    ps.want_blank = true;
	    break;

	case casestmt:		/* got word 'case' or 'default' */
	    scase = true;	/* so we can process the later colon properly */
	    goto copy_id;

	case colon:		/* got a ':' */
	    if (squest > 0) {	/* it is part of the <c>?<n>: <n> construct */
		--squest;
		if (ps.want_blank)
		    *e_code++ = ' ';
		*e_code++ = ':';
		ps.want_blank = true;
		break;
	    }
	    if (ps.in_or_st) {
		*e_code++ = ':';
		ps.want_blank = false;
		break;
	    }
	    ps.in_stmt = false;	/* seeing a label does not imply we are in a
				 * stmt */
	    /*
	     * turn everything so far into a label
	     */
	    {
		int len = e_code - s_code;

		CHECK_SIZE_LAB(len + 3);
		memcpy(e_lab, s_code, len);
		e_lab += len;
		*e_lab++ = ':';
		*e_lab = '\0';
		e_code = s_code;
	    }
	    force_nl = ps.pcase = scase;	/* ps.pcase will be used by
						 * dump_line to decide how to
						 * indent the label. force_nl
						 * will force a case n: to be
						 * on a line by itself */
	    scase = false;
	    ps.want_blank = false;
	    break;

	case semicolon:	/* got a ';' */
	    if (ps.dec_nest == 0)
		ps.in_or_st = false;/* we are not in an initialization or
				     * structure declaration */
	    scase = false;	/* these will only need resetting in an error */
	    squest = 0;
	    if (ps.last_token == rparen)
		ps.in_parameter_declaration = 0;
	    ps.cast_mask = 0;
	    ps.not_cast_mask = 0;
	    ps.block_init = 0;
	    ps.block_init_level = 0;
	    ps.just_saw_decl--;

	    if (ps.in_decl && s_code == e_code && !ps.block_init &&
		!ps.dumped_decl_indent && ps.paren_level == 0) {
		/* indent stray semicolons in declarations */
		indent_declaration(dec_ind - 1, tabs_to_var);
		ps.dumped_decl_indent = true;
	    }

	    ps.in_decl = (ps.dec_nest > 0);	/* if we were in a first level
						 * structure declaration, we
						 * arent any more */

	    if ((!sp_sw || hd_type != forstmt) && ps.p_l_follow > 0) {

		/*
		 * This should be true iff there were unbalanced parens in the
		 * stmt.  It is a bit complicated, because the semicolon might
		 * be in a for stmt
		 */
		diag2(1, "Unbalanced parens");
		ps.p_l_follow = 0;
		if (sp_sw) {	/* this is a check for an if, while, etc. with
				 * unbalanced parens */
		    sp_sw = false;
		    parse(hd_type);	/* dont lose the if, or whatever */
		}
	    }
	    *e_code++ = ';';
	    ps.want_blank = true;
	    ps.in_stmt = (ps.p_l_follow > 0);	/* we are no longer in the
						 * middle of a stmt */

	    if (!sp_sw) {	/* if not if for (;;) */
		parse(semicolon);	/* let parser know about end of stmt */
		force_nl = true;/* force newline after an end of stmt */
	    }
	    break;

	case lbrace:		/* got a '{' */
	    ps.in_stmt = false;	/* dont indent the {} */
	    if (!ps.block_init)
		force_nl = true;/* force other stuff on same line as '{' onto
				 * new line */
	    else if (ps.block_init_level <= 0)
		ps.block_init_level = 1;
	    else
		ps.block_init_level++;

	    if (s_code != e_code && !ps.block_init) {
		if (!btype_2) {
		    dump_line();
		    ps.want_blank = false;
		}
		else if (ps.in_parameter_declaration && !ps.in_or_st) {
		    ps.i_l_follow = 0;
		    if (function_brace_split) {	/* dump the line prior to the
						 * brace ... */
			dump_line();
			ps.want_blank = false;
		    } else	/* add a space between the decl and brace */
			ps.want_blank = true;
		}
	    }
	    if (ps.in_parameter_declaration)
		prefix_blankline_requested = 0;

	    if (ps.p_l_follow > 0) {	/* check for preceding unbalanced
					 * parens */
		diag2(1, "Unbalanced parens");
		ps.p_l_follow = 0;
		if (sp_sw) {	/* check for unclosed if, for, etc. */
		    sp_sw = false;
		    parse(hd_type);
		    ps.ind_level = ps.i_l_follow;
		}
	    }
	    if (s_code == e_code)
		ps.ind_stmt = false;	/* dont put extra indentation on line
					 * with '{' */
	    if (ps.in_decl && ps.in_or_st) {	/* this is either a structure
						 * declaration or an init */
		di_stack[ps.dec_nest] = dec_ind;
		if (++ps.dec_nest == nitems(di_stack)) {
		    diag3(0, "Reached internal limit of %d struct levels",
			nitems(di_stack));
		    ps.dec_nest--;
		}
		/* ?		dec_ind = 0; */
	    }
	    else {
		ps.decl_on_line = false;	/* we can't be in the middle of
						 * a declaration, so don't do
						 * special indentation of
						 * comments */
		if (blanklines_after_declarations_at_proctop
			&& ps.in_parameter_declaration)
		    postfix_blankline_requested = 1;
		ps.in_parameter_declaration = 0;
		ps.in_decl = false;
	    }
	    dec_ind = 0;
	    parse(lbrace);	/* let parser know about this */
	    if (ps.want_blank)	/* put a blank before '{' if '{' is not at
				 * start of line */
		*e_code++ = ' ';
	    ps.want_blank = false;
	    *e_code++ = '{';
	    ps.just_saw_decl = 0;
	    break;

	case rbrace:		/* got a '}' */
	    if (ps.p_stack[ps.tos] == decl && !ps.block_init)	/* semicolons can be
								 * omitted in
								 * declarations */
		parse(semicolon);
	    if (ps.p_l_follow) {/* check for unclosed if, for, else. */
		diag2(1, "Unbalanced parens");
		ps.p_l_follow = 0;
		sp_sw = false;
	    }
	    ps.just_saw_decl = 0;
	    ps.block_init_level--;
	    if (s_code != e_code && !ps.block_init) {	/* '}' must be first on
							 * line */
		if (verbose)
		    diag2(0, "Line broken");
		dump_line();
	    }
	    *e_code++ = '}';
	    ps.want_blank = true;
	    ps.in_stmt = ps.ind_stmt = false;
	    if (ps.dec_nest > 0) {	/* we are in multi-level structure
					 * declaration */
		dec_ind = di_stack[--ps.dec_nest];
		if (ps.dec_nest == 0 && !ps.in_parameter_declaration)
		    ps.just_saw_decl = 2;
		ps.in_decl = true;
	    }
	    prefix_blankline_requested = 0;
	    parse(rbrace);	/* let parser know about this */
	    ps.search_brace = cuddle_else && ps.p_stack[ps.tos] == ifhead
		&& ps.il[ps.tos] >= ps.ind_level;
	    if (ps.tos <= 1 && blanklines_after_procs && ps.dec_nest <= 0)
		postfix_blankline_requested = 1;
	    break;

	case swstmt:		/* got keyword "switch" */
	    sp_sw = true;
	    hd_type = swstmt;	/* keep this for when we have seen the
				 * expression */
	    goto copy_id;	/* go move the token into buffer */

	case sp_paren:		/* token is if, while, for */
	    sp_sw = true;	/* the interesting stuff is done after the
				 * expression is scanned */
	    hd_type = (*token == 'i' ? ifstmt :
		       (*token == 'w' ? whilestmt : forstmt));

	    /*
	     * remember the type of header for later use by parser
	     */
	    goto copy_id;	/* copy the token into line */

	case sp_nparen:	/* got else, do */
	    ps.in_stmt = false;
	    if (*token == 'e') {
		if (e_code != s_code && (!cuddle_else || e_code[-1] != '}')) {
		    if (verbose)
			diag2(0, "Line broken");
		    dump_line();/* make sure this starts a line */
		    ps.want_blank = false;
		}
		force_nl = true;/* also, following stuff must go onto new line */
		last_else = 1;
		parse(elselit);
	    }
	    else {
		if (e_code != s_code) {	/* make sure this starts a line */
		    if (verbose)
			diag2(0, "Line broken");
		    dump_line();
		    ps.want_blank = false;
		}
		force_nl = true;/* also, following stuff must go onto new line */
		last_else = 0;
		parse(dolit);
	    }
	    goto copy_id;	/* move the token into line */

	case type_def:
	case storage:
	    prefix_blankline_requested = 0;
	    goto copy_id;

	case structure:
	    if (ps.p_l_follow > 0)
		goto copy_id;
	case decl:		/* we have a declaration type (int, etc.) */
	    parse(decl);	/* let parser worry about indentation */
	    if (ps.last_token == rparen && ps.tos <= 1) {
		if (s_code != e_code) {
		    dump_line();
		    ps.want_blank = 0;
		}
	    }
	    if (ps.in_parameter_declaration && ps.indent_parameters && ps.dec_nest == 0) {
		ps.ind_level = ps.i_l_follow = 1;
		ps.ind_stmt = 0;
	    }
	    ps.in_or_st = true;	/* this might be a structure or initialization
				 * declaration */
	    ps.in_decl = ps.decl_on_line = ps.last_token != type_def;
	    if ( /* !ps.in_or_st && */ ps.dec_nest <= 0)
		ps.just_saw_decl = 2;
	    prefix_blankline_requested = 0;
	    for (i = 0; token[i++];);	/* get length of token */

	    if (ps.ind_level == 0 || ps.dec_nest > 0) {
		/* global variable or struct member in local variable */
		dec_ind = ps.decl_indent > 0 ? ps.decl_indent : i;
		tabs_to_var = (use_tabs ? ps.decl_indent > 0 : 0);
	    } else {
		/* local variable */
		dec_ind = ps.local_decl_indent > 0 ? ps.local_decl_indent : i;
		tabs_to_var = (use_tabs ? ps.local_decl_indent > 0 : 0);
	    }
	    goto copy_id;

	case funcname:
	case ident:		/* got an identifier or constant */
	    if (ps.in_decl) {
		if (type_code == funcname) {
		    ps.in_decl = false;
		    if (procnames_start_line && s_code != e_code) {
			*e_code = '\0';
			dump_line();
		    }
		    else if (ps.want_blank) {
			*e_code++ = ' ';
		    }
		    ps.want_blank = false;
		}
		else if (!ps.block_init && !ps.dumped_decl_indent &&
		    ps.paren_level == 0) { /* if we are in a declaration, we
					    * must indent identifier */
		    indent_declaration(dec_ind, tabs_to_var);
		    ps.dumped_decl_indent = true;
		    ps.want_blank = false;
		}
	    }
	    else if (sp_sw && ps.p_l_follow == 0) {
		sp_sw = false;
		force_nl = true;
		ps.last_u_d = true;
		ps.in_stmt = false;
		parse(hd_type);
	    }
    copy_id:
	    {
		int len = e_token - s_token;

		CHECK_SIZE_CODE(len + 1);
		if (ps.want_blank)
		    *e_code++ = ' ';
		memcpy(e_code, s_token, len);
		e_code += len;
	    }
	    if (type_code != funcname)
		ps.want_blank = true;
	    break;

	case strpfx:
	    {
		int len = e_token - s_token;

		CHECK_SIZE_CODE(len + 1);
		if (ps.want_blank)
		    *e_code++ = ' ';
		memcpy(e_code, token, len);
		e_code += len;
	    }
	    ps.want_blank = false;
	    break;

	case period:		/* treat a period kind of like a binary
				 * operation */
	    *e_code++ = '.';	/* move the period into line */
	    ps.want_blank = false;	/* dont put a blank after a period */
	    break;

	case comma:
	    ps.want_blank = (s_code != e_code);	/* only put blank after comma
						 * if comma does not start the
						 * line */
	    if (ps.in_decl && ps.procname[0] == '\0' && !ps.block_init &&
		!ps.dumped_decl_indent && ps.paren_level == 0) {
		/* indent leading commas and not the actual identifiers */
		indent_declaration(dec_ind - 1, tabs_to_var);
		ps.dumped_decl_indent = true;
	    }
	    *e_code++ = ',';
	    if (ps.p_l_follow == 0) {
		if (ps.block_init_level <= 0)
		    ps.block_init = 0;
		if (break_comma && (!ps.leave_comma ||
		    count_spaces_until(compute_code_target(), s_code, e_code) >
		    max_col - tabsize))
		    force_nl = true;
	    }
	    break;

	case preesc:		/* got the character '#' */
	    if ((s_com != e_com) ||
		    (s_lab != e_lab) ||
		    (s_code != e_code))
		dump_line();
	    CHECK_SIZE_LAB(1);
	    *e_lab++ = '#';	/* move whole line to 'label' buffer */
	    {
		int         in_comment = 0;
		int         com_start = 0;
		char        quote = 0;
		int         com_end = 0;

		while (*buf_ptr == ' ' || *buf_ptr == '\t') {
		    buf_ptr++;
		    if (buf_ptr >= buf_end)
			fill_buffer();
		}
		while (*buf_ptr != '\n' || (in_comment && !had_eof)) {
		    CHECK_SIZE_LAB(2);
		    *e_lab = *buf_ptr++;
		    if (buf_ptr >= buf_end)
			fill_buffer();
		    switch (*e_lab++) {
		    case BACKSLASH:
			if (!in_comment) {
			    *e_lab++ = *buf_ptr++;
			    if (buf_ptr >= buf_end)
				fill_buffer();
			}
			break;
		    case '/':
			if (*buf_ptr == '*' && !in_comment && !quote) {
			    in_comment = 1;
			    *e_lab++ = *buf_ptr++;
			    com_start = e_lab - s_lab - 2;
			}
			break;
		    case '"':
			if (quote == '"')
			    quote = 0;
			break;
		    case '\'':
			if (quote == '\'')
			    quote = 0;
			break;
		    case '*':
			if (*buf_ptr == '/' && in_comment) {
			    in_comment = 0;
			    *e_lab++ = *buf_ptr++;
			    com_end = e_lab - s_lab;
			}
			break;
		    }
		}

		while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
		    e_lab--;
		if (e_lab - s_lab == com_end && bp_save == NULL) {
		    /* comment on preprocessor line */
		    if (sc_end == NULL) {	/* if this is the first comment,
						 * we must set up the buffer */
			save_com = sc_buf;
			sc_end = &save_com[0];
		    }
		    else {
			*sc_end++ = '\n';	/* add newline between
						 * comments */
			*sc_end++ = ' ';
			--line_no;
		    }
		    if (sc_end - save_com + com_end - com_start > sc_size)
			errx(1, "input too long");
		    memmove(sc_end, s_lab + com_start, com_end - com_start);
		    sc_end += com_end - com_start;
		    e_lab = s_lab + com_start;
		    while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
			e_lab--;
		    bp_save = buf_ptr;	/* save current input buffer */
		    be_save = buf_end;
		    buf_ptr = save_com;	/* fix so that subsequent calls to
					 * lexi will take tokens out of
					 * save_com */
		    *sc_end++ = ' ';	/* add trailing blank, just in case */
		    buf_end = sc_end;
		    sc_end = NULL;
		}
		CHECK_SIZE_LAB(1);
		*e_lab = '\0';	/* null terminate line */
		ps.pcase = false;
	    }

	    if (strncmp(s_lab, "#if", 3) == 0) { /* also ifdef, ifndef */
		if ((size_t)ifdef_level < nitems(state_stack)) {
		    match_state[ifdef_level].tos = -1;
		    state_stack[ifdef_level++] = ps;
		}
		else
		    diag2(1, "#if stack overflow");
	    }
	    else if (strncmp(s_lab, "#el", 3) == 0) { /* else, elif */
		if (ifdef_level <= 0)
		    diag2(1, s_lab[3] == 'i' ? "Unmatched #elif" : "Unmatched #else");
		else {
		    match_state[ifdef_level - 1] = ps;
		    ps = state_stack[ifdef_level - 1];
		}
	    }
	    else if (strncmp(s_lab, "#endif", 6) == 0) {
		if (ifdef_level <= 0)
		    diag2(1, "Unmatched #endif");
		else
		    ifdef_level--;
	    } else {
		struct directives {
		    int size;
		    const char *string;
		}
		recognized[] = {
		    {7, "include"},
		    {6, "define"},
		    {5, "undef"},
		    {4, "line"},
		    {5, "error"},
		    {6, "pragma"}
		};
		int d = nitems(recognized);
		while (--d >= 0)
		    if (strncmp(s_lab + 1, recognized[d].string, recognized[d].size) == 0)
			break;
		if (d < 0) {
		    diag2(1, "Unrecognized cpp directive");
		    break;
		}
	    }
	    if (blanklines_around_conditional_compilation) {
		postfix_blankline_requested++;
		n_real_blanklines = 0;
	    }
	    else {
		postfix_blankline_requested = 0;
		prefix_blankline_requested = 0;
	    }
	    break;		/* subsequent processing of the newline
				 * character will cause the line to be printed */

	case comment:		/* we have gotten a / followed by * this is a biggie */
	    pr_comment();
	    break;
	}			/* end of big switch stmt */

	*e_code = '\0';		/* make sure code section is null terminated */
	if (type_code != comment && type_code != newline && type_code != preesc)
	    ps.last_token = type_code;
    }				/* end of main while (1) loop */
}
Ejemplo n.º 4
0
int
main(int argc, char **argv)
{

    int         dec_ind;	/* current indentation for declarations */
    int         di_stack[20];	/* a stack of structure indentation levels */
    int         flushed_nl;	/* used when buffering up comments to remember
				 * that a newline was passed over */
    int         force_nl;	/* when true, code must be broken */
    int         hd_type = 0;	/* used to store type of stmt for if (...),
				 * for (...), etc */
    int		i;		/* local loop counter */
    int         scase;		/* set to true when we see a case, so we will
				 * know what to do with the following colon */
    int         sp_sw;		/* when true, we are in the expression of
				 * if(...), while(...), etc. */
    int         squest;		/* when this is positive, we have seen a ?
				 * without the matching : in a <c>?<s>:<s>
				 * construct */
    const char *t_ptr;		/* used for copying tokens */
    int		tabs_to_var;	/* true if using tabs to indent to var name */
    int         type_code;	/* the type of token, returned by lexi */

    int         last_else = 0;	/* true iff last keyword was an else */


    /*-----------------------------------------------*\
    |		      INITIALIZATION		      |
    \*-----------------------------------------------*/

    found_err = 0;

    ps.p_stack[0] = stmt;	/* this is the parser's stack */
    ps.last_nl = true;		/* this is true if the last thing scanned was
				 * a newline */
    ps.last_token = semicolon;
    combuf = (char *) malloc(bufsize);
    if (combuf == NULL)
        err(1, NULL);
    labbuf = (char *) malloc(bufsize);
    if (labbuf == NULL)
        err(1, NULL);
    codebuf = (char *) malloc(bufsize);
    if (codebuf == NULL)
        err(1, NULL);
    tokenbuf = (char *) malloc(bufsize);
    if (tokenbuf == NULL)
        err(1, NULL);
    l_com = combuf + bufsize - 5;
    l_lab = labbuf + bufsize - 5;
    l_code = codebuf + bufsize - 5;
    l_token = tokenbuf + bufsize - 5;
    combuf[0] = codebuf[0] = labbuf[0] = ' ';	/* set up code, label, and
						 * comment buffers */
    combuf[1] = codebuf[1] = labbuf[1] = '\0';
    ps.else_if = 1;		/* Default else-if special processing to on */
    s_lab = e_lab = labbuf + 1;
    s_code = e_code = codebuf + 1;
    s_com = e_com = combuf + 1;
    s_token = e_token = tokenbuf + 1;

    in_buffer = (char *) malloc(10);
    if (in_buffer == NULL)
        err(1, NULL);
    in_buffer_limit = in_buffer + 8;
    buf_ptr = buf_end = in_buffer;
    line_no = 1;
    had_eof = ps.in_decl = ps.decl_on_line = break_comma = false;
    sp_sw = force_nl = false;
    ps.in_or_st = false;
    ps.bl_line = true;
    dec_ind = 0;
    di_stack[ps.dec_nest = 0] = 0;
    ps.want_blank = ps.in_stmt = ps.ind_stmt = false;

    scase = ps.pcase = false;
    squest = 0;
    sc_end = 0;
    bp_save = 0;
    be_save = 0;

    output = 0;
    tabs_to_var = 0;

    /*--------------------------------------------------*\
    |   		COMMAND LINE SCAN		 |
    \*--------------------------------------------------*/

#ifdef undef
    max_col = 78;		/* -l78 */
    lineup_to_parens = 1;	/* -lp */
    ps.ljust_decl = 0;		/* -ndj */
    ps.com_ind = 33;		/* -c33 */
    star_comment_cont = 1;	/* -sc */
    ps.ind_size = 8;		/* -i8 */
    verbose = 0;
    ps.decl_indent = 16;	/* -di16 */
    ps.local_decl_indent = -1;	/* if this is not set to some nonnegative value
				 * by an arg, we will set this equal to
				 * ps.decl_ind */
    ps.indent_parameters = 1;	/* -ip */
    ps.decl_com_ind = 0;	/* if this is not set to some positive value
				 * by an arg, we will set this equal to
				 * ps.com_ind */
    btype_2 = 1;		/* -br */
    cuddle_else = 1;		/* -ce */
    ps.unindent_displace = 0;	/* -d0 */
    ps.case_indent = 0;		/* -cli0 */
    format_block_comments = 1;	/* -fcb */
    format_col1_comments = 1;	/* -fc1 */
    procnames_start_line = 1;	/* -psl */
    proc_calls_space = 0;	/* -npcs */
    comment_delimiter_on_blankline = 1;	/* -cdb */
    ps.leave_comma = 1;		/* -nbc */
#endif

    for (i = 1; i < argc; ++i)
        if (strcmp(argv[i], "-npro") == 0)
            break;
    set_defaults();
    if (i >= argc)
        set_profile();

    for (i = 1; i < argc; ++i) {

        /*
         * look thru args (if any) for changes to defaults
         */
        if (argv[i][0] != '-') {/* no flag on parameter */
            if (input == NULL) {	/* we must have the input file */
                in_name = argv[i];	/* remember name of input file */
                input = fopen(in_name, "r");
                if (input == NULL)	/* check for open error */
                    err(1, "%s", in_name);
                continue;
            }
            else if (output == NULL) {	/* we have the output file */
                out_name = argv[i];	/* remember name of output file */
                if (strcmp(in_name, out_name) == 0) {
                    /* attempt to overwrite
                    					 * the file */
                    errx(1, "input and output files must be different");
                }
                output = fopen(out_name, "w");
                if (output == NULL)	/* check for create error */
                    err(1, "%s", out_name);
                continue;
            }
            errx(1, "unknown parameter: %s", argv[i]);
        }
        else
            set_option(argv[i]);
    }				/* end of for */
    if (input == NULL)
        input = stdin;
    if (output == NULL) {
        if (troff || input == stdin)
            output = stdout;
        else {
            out_name = in_name;
            bakcopy();
        }
    }
    if (ps.com_ind <= 1)
        ps.com_ind = 2;		/* dont put normal comments before column 2 */
    if (troff) {
        if (bodyf.font[0] == 0)
            parsefont(&bodyf, "R");
        if (scomf.font[0] == 0)
            parsefont(&scomf, "I");
        if (blkcomf.font[0] == 0)
            blkcomf = scomf, blkcomf.size += 2;
        if (boxcomf.font[0] == 0)
            boxcomf = blkcomf;
        if (stringf.font[0] == 0)
            parsefont(&stringf, "L");
        if (keywordf.font[0] == 0)
            parsefont(&keywordf, "B");
        writefdef(&bodyf, 'B');
        writefdef(&scomf, 'C');
        writefdef(&blkcomf, 'L');
        writefdef(&boxcomf, 'X');
        writefdef(&stringf, 'S');
        writefdef(&keywordf, 'K');
    }
    if (block_comment_max_col <= 0)
        block_comment_max_col = max_col;
    if (ps.local_decl_indent < 0)	/* if not specified by user, set this */
        ps.local_decl_indent = ps.decl_indent;
    if (ps.decl_com_ind <= 0)	/* if not specified by user, set this */
        ps.decl_com_ind = ps.ljust_decl ? (ps.com_ind <= 10 ? 2 : ps.com_ind - 8) : ps.com_ind;
    if (continuation_indent == 0)
        continuation_indent = ps.ind_size;
    fill_buffer();		/* get first batch of stuff into input buffer */

    parse(semicolon);
    {
        char *p = buf_ptr;
        int col = 1;

        while (1) {
            if (*p == ' ')
                col++;
            else if (*p == '\t')
                col = ((col - 1) & ~7) + 9;
            else
                break;
            p++;
        }
        if (col > ps.ind_size)
            ps.ind_level = ps.i_l_follow = col / ps.ind_size;
    }
    if (troff) {
        const char *p = in_name,
                    *beg = in_name;

        while (*p)
            if (*p++ == '/')
                beg = p;
        fprintf(output, ".Fn \"%s\"\n", beg);
    }
    /*
     * START OF MAIN LOOP
     */

    while (1) {
        /* this is the main loop.  it will go until we
        			 * reach eof */
        int         is_procname;

        type_code = lexi();	/* lexi reads one token.  The actual
				 * characters read are stored in "token". lexi
				 * returns a code indicating the type of token */
        is_procname = ps.procname[0];

        /*
         * The following code moves everything following an if (), while (),
         * else, etc. up to the start of the following stmt to a buffer. This
         * allows proper handling of both kinds of brace placement.
         */

        flushed_nl = false;
        while (ps.search_brace) {
            /* if we scanned an if(), while(),
            				 * etc., we might need to copy stuff
            				 * into a buffer we must loop, copying
            				 * stuff into save_com, until we find
            				 * the start of the stmt which follows
            				 * the if, or whatever */
            switch (type_code) {
            case newline:
                ++line_no;
                flushed_nl = true;
            case form_feed:
                break;		/* form feeds and newlines found here will be
				 * ignored */

            case lbrace:	/* this is a brace that starts the compound
				 * stmt */
                if (sc_end == 0) {
                    /* ignore buffering if a comment wasnt
                    			 * stored up */
                    ps.search_brace = false;
                    goto check_type;
                }
                if (btype_2) {
                    save_com[0] = '{';	/* we either want to put the brace
					 * right after the if */
                    goto sw_buffer;	/* go to common code to get out of
					 * this loop */
                }
            case comment:	/* we have a comment, so we must copy it into
				 * the buffer */
                if (!flushed_nl || sc_end != 0) {
                    if (sc_end == 0) {
                        /* if this is the first comment, we
                        		 * must set up the buffer */
                        save_com[0] = save_com[1] = ' ';
                        sc_end = &(save_com[2]);
                    }
                    else {
                        *sc_end++ = '\n';	/* add newline between
						 * comments */
                        *sc_end++ = ' ';
                        --line_no;
                    }
                    *sc_end++ = '/';	/* copy in start of comment */
                    *sc_end++ = '*';

                    for (;;) {	/* loop until we get to the end of the comment */
                        *sc_end = *buf_ptr++;
                        if (buf_ptr >= buf_end)
                            fill_buffer();

                        if (*sc_end++ == '*' && *buf_ptr == '/')
                            break;	/* we are at end of comment */

                        if (sc_end >= &(save_com[sc_size])) {
                            /* check for temp buffer
                            					 * overflow */
                            diag2(1, "Internal buffer overflow - Move big comment from right after if, while, or whatever");
                            fflush(output);
                            exit(1);
                        }
                    }
                    *sc_end++ = '/';	/* add ending slash */
                    if (++buf_ptr >= buf_end)	/* get past / in buffer */
                        fill_buffer();
                    break;
                }
            default:		/* it is the start of a normal statement */
                if (flushed_nl)	/* if we flushed a newline, make sure it is
				 * put back */
                    force_nl = true;
                if ((type_code == sp_paren && *token == 'i'
                        && last_else && ps.else_if)
                        || (type_code == sp_nparen && *token == 'e'
                            && e_code != s_code && e_code[-1] == '}'))
                    force_nl = false;

                if (sc_end == 0) {
                    /* ignore buffering if comment wasnt
                    			 * saved up */
                    ps.search_brace = false;
                    goto check_type;
                }
                if (force_nl) {
                    /* if we should insert a nl here, put it into
                    		 * the buffer */
                    force_nl = false;
                    --line_no;	/* this will be re-increased when the nl is
				 * read from the buffer */
                    *sc_end++ = '\n';
                    *sc_end++ = ' ';
                    if (verbose && !flushed_nl)	/* print error msg if the line
						 * was not already broken */
                        diag2(0, "Line broken");
                    flushed_nl = false;
                }
                for (t_ptr = token; *t_ptr; ++t_ptr)
                    *sc_end++ = *t_ptr;	/* copy token into temp buffer */
                ps.procname[0] = 0;

sw_buffer:
                ps.search_brace = false;	/* stop looking for start of
						 * stmt */
                bp_save = buf_ptr;	/* save current input buffer */
                be_save = buf_end;
                buf_ptr = save_com;	/* fix so that subsequent calls to
					 * lexi will take tokens out of
					 * save_com */
                *sc_end++ = ' ';/* add trailing blank, just in case */
                buf_end = sc_end;
                sc_end = 0;
                break;
            }			/* end of switch */
            if (type_code != 0)	/* we must make this check, just in case there
				 * was an unexpected EOF */
                type_code = lexi();	/* read another token */
            /* if (ps.search_brace) ps.procname[0] = 0; */
            if ((is_procname = ps.procname[0]) && flushed_nl
                    && !procnames_start_line && ps.in_decl
                    && type_code == ident)
                flushed_nl = 0;
        }			/* end of while (search_brace) */
        last_else = 0;
check_type:
        if (type_code == 0) {	/* we got eof */
            if (s_lab != e_lab || s_code != e_code
                    || s_com != e_com)	/* must dump end of line */
                dump_line();
            if (ps.tos > 1)	/* check for balanced braces */
                diag2(1, "Stuff missing from end of file");

            if (verbose) {
                printf("There were %d output lines and %d comments\n",
                       ps.out_lines, ps.out_coms);
                printf("(Lines with comments)/(Lines with code): %6.3f\n",
                       (1.0 * ps.com_lines) / code_lines);
            }
            fflush(output);
            exit(found_err);
        }
        if (
            (type_code != comment) &&
            (type_code != newline) &&
            (type_code != preesc) &&
            (type_code != form_feed)) {
            if (force_nl &&
                    (type_code != semicolon) &&
                    (type_code != lbrace || !btype_2)) {
                /* we should force a broken line here */
                if (verbose && !flushed_nl)
                    diag2(0, "Line broken");
                flushed_nl = false;
                dump_line();
                ps.want_blank = false;	/* dont insert blank at line start */
                force_nl = false;
            }
            ps.in_stmt = true;	/* turn on flag which causes an extra level of
				 * indentation. this is turned off by a ; or
				 * '}' */
            if (s_com != e_com) {
                /* the turkey has embedded a comment
                			 * in a line. fix it */
                *e_code++ = ' ';
                for (t_ptr = s_com; *t_ptr; ++t_ptr) {
                    CHECK_SIZE_CODE;
                    *e_code++ = *t_ptr;
                }
                *e_code++ = ' ';
                *e_code = '\0';	/* null terminate code sect */
                ps.want_blank = false;
                e_com = s_com;
            }
        }
        else if (type_code != comment)	/* preserve force_nl thru a comment */
            force_nl = false;	/* cancel forced newline after newline, form
				 * feed, etc */



        /*-----------------------------------------------------*\
        |	   do switch on type of token scanned		|
        \*-----------------------------------------------------*/
        CHECK_SIZE_CODE;
        switch (type_code) {	/* now, decide what to do with the token */

        case form_feed:	/* found a form feed in line */
            ps.use_ff = true;	/* a form feed is treated much like a newline */
            dump_line();
            ps.want_blank = false;
            break;

        case newline:
            if (ps.last_token != comma || ps.p_l_follow > 0
                    || !ps.leave_comma || ps.block_init || !break_comma || s_com != e_com) {
                dump_line();
                ps.want_blank = false;
            }
            ++line_no;		/* keep track of input line number */
            break;

        case lparen:		/* got a '(' or '[' */
            ++ps.p_l_follow;	/* count parens to make Healy happy */
            if (ps.want_blank && *token != '[' &&
                    (ps.last_token != ident || proc_calls_space
                     || (ps.its_a_keyword && (!ps.sizeof_keyword || Bill_Shannon))))
                *e_code++ = ' ';
            if (ps.in_decl && !ps.block_init)
                if (troff && !ps.dumped_decl_indent && !is_procname && ps.last_token == decl) {
                    ps.dumped_decl_indent = 1;
                    sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token);
                    e_code += strlen(e_code);
                }
                else {
                    while ((e_code - s_code) < dec_ind) {
                        CHECK_SIZE_CODE;
                        *e_code++ = ' ';
                    }
                    *e_code++ = token[0];
                }
            else
                *e_code++ = token[0];
            ps.paren_indents[ps.p_l_follow - 1] = e_code - s_code;
            if (sp_sw && ps.p_l_follow == 1 && extra_expression_indent
                    && ps.paren_indents[0] < 2 * ps.ind_size)
                ps.paren_indents[0] = 2 * ps.ind_size;
            ps.want_blank = false;
            if (ps.in_or_st && *token == '(' && ps.tos <= 2) {
                /*
                 * this is a kluge to make sure that declarations will be
                 * aligned right if proc decl has an explicit type on it, i.e.
                 * "int a(x) {..."
                 */
                parse(semicolon);	/* I said this was a kluge... */
                ps.in_or_st = false;	/* turn off flag for structure decl or
					 * initialization */
            }
            if (ps.sizeof_keyword)
                ps.sizeof_mask |= 1 << ps.p_l_follow;
            break;

        case rparen:		/* got a ')' or ']' */
            rparen_count--;
            if (ps.cast_mask & (1 << ps.p_l_follow) & ~ps.sizeof_mask) {
                ps.last_u_d = true;
                ps.cast_mask &= (1 << ps.p_l_follow) - 1;
                ps.want_blank = false;
            } else
                ps.want_blank = true;
            ps.sizeof_mask &= (1 << ps.p_l_follow) - 1;
            if (--ps.p_l_follow < 0) {
                ps.p_l_follow = 0;
                diag3(0, "Extra %c", *token);
            }
            if (e_code == s_code)	/* if the paren starts the line */
                ps.paren_level = ps.p_l_follow;	/* then indent it */

            *e_code++ = token[0];

            if (sp_sw && (ps.p_l_follow == 0)) {
                /* check for end of if
                					 * (...), or some such */
                sp_sw = false;
                force_nl = true;/* must force newline after if */
                ps.last_u_d = true;	/* inform lexi that a following
					 * operator is unary */
                ps.in_stmt = false;	/* dont use stmt continuation
					 * indentation */

                parse(hd_type);	/* let parser worry about if, or whatever */
            }
            ps.search_brace = btype_2;	/* this should insure that constructs
					 * such as main(){...} and int[]{...}
					 * have their braces put in the right
					 * place */
            break;

        case unary_op:		/* this could be any unary operation */
            if (ps.want_blank)
                *e_code++ = ' ';

            if (troff && !ps.dumped_decl_indent && ps.in_decl && !is_procname) {
                sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token);
                ps.dumped_decl_indent = 1;
                e_code += strlen(e_code);
            }
            else {
                const char *res = token;

                if (ps.in_decl && !ps.block_init) {
                    /* if this is a unary op
                    					 * in a declaration, we
                    					 * should indent this
                    					 * token */
                    for (i = 0; token[i]; ++i);	/* find length of token */
                    while ((e_code - s_code) < (dec_ind - i)) {
                        CHECK_SIZE_CODE;
                        *e_code++ = ' ';	/* pad it */
                    }
                }
                if (troff && token[0] == '-' && token[1] == '>')
                    res = "\\(->";
                for (t_ptr = res; *t_ptr; ++t_ptr) {
                    CHECK_SIZE_CODE;
                    *e_code++ = *t_ptr;
                }
            }
            ps.want_blank = false;
            break;

        case binary_op:	/* any binary operation */
            if (ps.want_blank)
                *e_code++ = ' ';
            {
                const char *res = token;

                if (troff)
                    switch (token[0]) {
                    case '<':
                        if (token[1] == '=')
                            res = "\\(<=";
                        break;
                    case '>':
                        if (token[1] == '=')
                            res = "\\(>=";
                        break;
                    case '!':
                        if (token[1] == '=')
                            res = "\\(!=";
                        break;
                    case '|':
                        if (token[1] == '|')
                            res = "\\(br\\(br";
                        else if (token[1] == 0)
                            res = "\\(br";
                        break;
                    }
                for (t_ptr = res; *t_ptr; ++t_ptr) {
                    CHECK_SIZE_CODE;
                    *e_code++ = *t_ptr;	/* move the operator */
                }
            }
            ps.want_blank = true;
            break;

        case postop:		/* got a trailing ++ or -- */
            *e_code++ = token[0];
            *e_code++ = token[1];
            ps.want_blank = true;
            break;

        case question:		/* got a ? */
            squest++;		/* this will be used when a later colon
				 * appears so we can distinguish the
				 * <c>?<n>:<n> construct */
            if (ps.want_blank)
                *e_code++ = ' ';
            *e_code++ = '?';
            ps.want_blank = true;
            break;

        case casestmt:		/* got word 'case' or 'default' */
            scase = true;	/* so we can process the later colon properly */
            goto copy_id;

        case colon:		/* got a ':' */
            if (squest > 0) {	/* it is part of the <c>?<n>: <n> construct */
                --squest;
                if (ps.want_blank)
                    *e_code++ = ' ';
                *e_code++ = ':';
                ps.want_blank = true;
                break;
            }
            if (ps.in_or_st) {
                *e_code++ = ':';
                ps.want_blank = false;
                break;
            }
            ps.in_stmt = false;	/* seeing a label does not imply we are in a
				 * stmt */
            for (t_ptr = s_code; *t_ptr; ++t_ptr)
                *e_lab++ = *t_ptr;	/* turn everything so far into a label */
            e_code = s_code;
            *e_lab++ = ':';
            *e_lab++ = ' ';
            *e_lab = '\0';

            force_nl = ps.pcase = scase;	/* ps.pcase will be used by
						 * dump_line to decide how to
						 * indent the label. force_nl
						 * will force a case n: to be
						 * on a line by itself */
            scase = false;
            ps.want_blank = false;
            break;

        case semicolon:	/* got a ';' */
            ps.in_or_st = false;/* we are not in an initialization or
				 * structure declaration */
            scase = false;	/* these will only need resetting in an error */
            squest = 0;
            if (ps.last_token == rparen && rparen_count == 0)
                ps.in_parameter_declaration = 0;
            ps.cast_mask = 0;
            ps.sizeof_mask = 0;
            ps.block_init = 0;
            ps.block_init_level = 0;
            ps.just_saw_decl--;

            if (ps.in_decl && s_code == e_code && !ps.block_init)
                while ((e_code - s_code) < (dec_ind - 1)) {
                    CHECK_SIZE_CODE;
                    *e_code++ = ' ';
                }

            ps.in_decl = (ps.dec_nest > 0);	/* if we were in a first level
						 * structure declaration, we
						 * arent any more */

            if ((!sp_sw || hd_type != forstmt) && ps.p_l_follow > 0) {

                /*
                 * This should be true iff there were unbalanced parens in the
                 * stmt.  It is a bit complicated, because the semicolon might
                 * be in a for stmt
                 */
                diag2(1, "Unbalanced parens");
                ps.p_l_follow = 0;
                if (sp_sw) {
                    /* this is a check for an if, while, etc. with
                    		 * unbalanced parens */
                    sp_sw = false;
                    parse(hd_type);	/* dont lose the if, or whatever */
                }
            }
            *e_code++ = ';';
            ps.want_blank = true;
            ps.in_stmt = (ps.p_l_follow > 0);	/* we are no longer in the
						 * middle of a stmt */

            if (!sp_sw) {	/* if not if for (;;) */
                parse(semicolon);	/* let parser know about end of stmt */
                force_nl = true;/* force newline after an end of stmt */
            }
            break;

        case lbrace:		/* got a '{' */
            ps.in_stmt = false;	/* dont indent the {} */
            if (!ps.block_init)
                force_nl = true;/* force other stuff on same line as '{' onto
				 * new line */
            else if (ps.block_init_level <= 0)
                ps.block_init_level = 1;
            else
                ps.block_init_level++;

            if (s_code != e_code && !ps.block_init) {
                if (!btype_2) {
                    dump_line();
                    ps.want_blank = false;
                }
                else if (ps.in_parameter_declaration && !ps.in_or_st) {
                    ps.i_l_follow = 0;
                    if (function_brace_split) {
                        /* dump the line prior to the
                        			 * brace ... */
                        dump_line();
                        ps.want_blank = false;
                    } else	/* add a space between the decl and brace */
                        ps.want_blank = true;
                }
            }
            if (ps.in_parameter_declaration)
                prefix_blankline_requested = 0;

            if (ps.p_l_follow > 0) {
                /* check for preceding unbalanced
                			 * parens */
                diag2(1, "Unbalanced parens");
                ps.p_l_follow = 0;
                if (sp_sw) {	/* check for unclosed if, for, etc. */
                    sp_sw = false;
                    parse(hd_type);
                    ps.ind_level = ps.i_l_follow;
                }
            }
            if (s_code == e_code)
                ps.ind_stmt = false;	/* dont put extra indentation on line
					 * with '{' */
            if (ps.in_decl && ps.in_or_st) {
                /* this is either a structure
                				 * declaration or an init */
                di_stack[ps.dec_nest++] = dec_ind;
                /* ?		dec_ind = 0; */
            }
            else {
                ps.decl_on_line = false;	/* we cant be in the middle of
						 * a declaration, so dont do
						 * special indentation of
						 * comments */
                if (blanklines_after_declarations_at_proctop
                        && ps.in_parameter_declaration)
                    postfix_blankline_requested = 1;
                ps.in_parameter_declaration = 0;
            }
            dec_ind = 0;
            parse(lbrace);	/* let parser know about this */
            if (ps.want_blank)	/* put a blank before '{' if '{' is not at
				 * start of line */
                *e_code++ = ' ';
            ps.want_blank = false;
            *e_code++ = '{';
            ps.just_saw_decl = 0;
            break;

        case rbrace:		/* got a '}' */
            if (ps.p_stack[ps.tos] == decl && !ps.block_init)	/* semicolons can be
								 * omitted in
								 * declarations */
                parse(semicolon);
            if (ps.p_l_follow) {/* check for unclosed if, for, else. */
                diag2(1, "Unbalanced parens");
                ps.p_l_follow = 0;
                sp_sw = false;
            }
            ps.just_saw_decl = 0;
            ps.block_init_level--;
            if (s_code != e_code && !ps.block_init) {
                /* '}' must be first on
                					 * line */
                if (verbose)
                    diag2(0, "Line broken");
                dump_line();
            }
            *e_code++ = '}';
            ps.want_blank = true;
            ps.in_stmt = ps.ind_stmt = false;
            if (ps.dec_nest > 0) {
                /* we are in multi-level structure
                			 * declaration */
                dec_ind = di_stack[--ps.dec_nest];
                if (ps.dec_nest == 0 && !ps.in_parameter_declaration)
                    ps.just_saw_decl = 2;
                ps.in_decl = true;
            }
            prefix_blankline_requested = 0;
            parse(rbrace);	/* let parser know about this */
            ps.search_brace = cuddle_else && ps.p_stack[ps.tos] == ifhead
                              && ps.il[ps.tos] >= ps.ind_level;
            if (ps.tos <= 1 && blanklines_after_procs && ps.dec_nest <= 0)
                postfix_blankline_requested = 1;
            break;

        case swstmt:		/* got keyword "switch" */
            sp_sw = true;
            hd_type = swstmt;	/* keep this for when we have seen the
				 * expression */
            goto copy_id;	/* go move the token into buffer */

        case sp_paren:		/* token is if, while, for */
            sp_sw = true;	/* the interesting stuff is done after the
				 * expression is scanned */
            hd_type = (*token == 'i' ? ifstmt :
                       (*token == 'w' ? whilestmt : forstmt));

            /*
             * remember the type of header for later use by parser
             */
            goto copy_id;	/* copy the token into line */

        case sp_nparen:	/* got else, do */
            ps.in_stmt = false;
            if (*token == 'e') {
                if (e_code != s_code && (!cuddle_else || e_code[-1] != '}')) {
                    if (verbose)
                        diag2(0, "Line broken");
                    dump_line();/* make sure this starts a line */
                    ps.want_blank = false;
                }
                force_nl = true;/* also, following stuff must go onto new line */
                last_else = 1;
                parse(elselit);
            }
            else {
                if (e_code != s_code) {	/* make sure this starts a line */
                    if (verbose)
                        diag2(0, "Line broken");
                    dump_line();
                    ps.want_blank = false;
                }
                force_nl = true;/* also, following stuff must go onto new line */
                last_else = 0;
                parse(dolit);
            }
            goto copy_id;	/* move the token into line */

        case decl:		/* we have a declaration type (int, register,
				 * etc.) */
            parse(decl);	/* let parser worry about indentation */
            if (ps.last_token == rparen && ps.tos <= 1) {
                ps.in_parameter_declaration = 1;
                if (s_code != e_code) {
                    dump_line();
                    ps.want_blank = 0;
                }
            }
            if (ps.in_parameter_declaration && ps.indent_parameters && ps.dec_nest == 0) {
                ps.ind_level = ps.i_l_follow = 1;
                ps.ind_stmt = 0;
            }
            ps.in_or_st = true;	/* this might be a structure or initialization
				 * declaration */
            ps.in_decl = ps.decl_on_line = true;
            if ( /* !ps.in_or_st && */ ps.dec_nest <= 0)
                ps.just_saw_decl = 2;
            prefix_blankline_requested = 0;
            for (i = 0; token[i++];);	/* get length of token */

            if (ps.ind_level == 0 || ps.dec_nest > 0) {
                /* global variable or struct member in local variable */
                dec_ind = ps.decl_indent > 0 ? ps.decl_indent : i;
                tabs_to_var = (use_tabs ? ps.decl_indent > 0 : 0);
            } else {
                /* local variable */
                dec_ind = ps.local_decl_indent > 0 ? ps.local_decl_indent : i;
                tabs_to_var = (use_tabs ? ps.local_decl_indent > 0 : 0);
            }
            goto copy_id;

        case ident:		/* got an identifier or constant */
            if (ps.in_decl) {
                /* if we are in a declaration, we must indent
                		 * identifier */
                if (is_procname == 0 || !procnames_start_line) {
                    if (!ps.block_init) {
                        if (troff && !ps.dumped_decl_indent) {
                            if (ps.want_blank)
                                *e_code++ = ' ';
                            ps.want_blank = false;
                            sprintf(e_code, "\n.De %dp+\200p\n", dec_ind * 7);
                            ps.dumped_decl_indent = 1;
                            e_code += strlen(e_code);
                        } else {
                            int cur_dec_ind;
                            int pos, startpos;

                            /*
                             * in order to get the tab math right for
                             * indentations that are not multiples of 8 we
                             * need to modify both startpos and dec_ind
                             * (cur_dec_ind) here by eight minus the
                             * remainder of the current starting column
                             * divided by eight. This seems to be a
                             * properly working fix
                             */
                            startpos = e_code - s_code;
                            cur_dec_ind = dec_ind;
                            pos = startpos;
                            if ((ps.ind_level * ps.ind_size) % 8 != 0) {
                                pos += (ps.ind_level * ps.ind_size) % 8;
                                cur_dec_ind += (ps.ind_level * ps.ind_size) % 8;
                            }

                            if (tabs_to_var) {
                                while ((pos & ~7) + 8 <= cur_dec_ind) {
                                    CHECK_SIZE_CODE;
                                    *e_code++ = '\t';
                                    pos = (pos & ~7) + 8;
                                }
                            }
                            while (pos < cur_dec_ind) {
                                CHECK_SIZE_CODE;
                                *e_code++ = ' ';
                                pos++;
                            }
                            if (ps.want_blank && e_code - s_code == startpos)
                                *e_code++ = ' ';
                            ps.want_blank = false;
                        }
                    }
                } else {
                    if (ps.want_blank)
                        *e_code++ = ' ';
                    ps.want_blank = false;
                    if (dec_ind && s_code != e_code)
                        dump_line();
                    dec_ind = 0;
                }
            }
            else if (sp_sw && ps.p_l_follow == 0) {
                sp_sw = false;
                force_nl = true;
                ps.last_u_d = true;
                ps.in_stmt = false;
                parse(hd_type);
            }
copy_id:
            if (ps.want_blank)
                *e_code++ = ' ';
            if (troff && ps.its_a_keyword) {
                e_code = chfont(&bodyf, &keywordf, e_code);
                for (t_ptr = token; *t_ptr; ++t_ptr) {
                    CHECK_SIZE_CODE;
                    *e_code++ = keywordf.allcaps && islower(*t_ptr)
                                ? toupper(*t_ptr) : *t_ptr;
                }
                e_code = chfont(&keywordf, &bodyf, e_code);
            }
            else
                for (t_ptr = token; *t_ptr; ++t_ptr) {
                    CHECK_SIZE_CODE;
                    *e_code++ = *t_ptr;
                }
            ps.want_blank = true;
            break;

        case period:		/* treat a period kind of like a binary
				 * operation */
            *e_code++ = '.';	/* move the period into line */
            ps.want_blank = false;	/* dont put a blank after a period */
            break;

        case comma:
            ps.want_blank = (s_code != e_code);	/* only put blank after comma
						 * if comma does not start the
						 * line */
            if (ps.in_decl && is_procname == 0 && !ps.block_init)
                while ((e_code - s_code) < (dec_ind - 1)) {
                    CHECK_SIZE_CODE;
                    *e_code++ = ' ';
                }

            *e_code++ = ',';
            if (ps.p_l_follow == 0) {
                if (ps.block_init_level <= 0)
                    ps.block_init = 0;
                if (break_comma && (!ps.leave_comma || compute_code_target() + (e_code - s_code) > max_col - 8))
                    force_nl = true;
            }
            break;

        case preesc:		/* got the character '#' */
            if ((s_com != e_com) ||
                    (s_lab != e_lab) ||
                    (s_code != e_code))
                dump_line();
            *e_lab++ = '#';	/* move whole line to 'label' buffer */
            {
                int         in_comment = 0;
                int         com_start = 0;
                char        quote = 0;
                int         com_end = 0;

                while (*buf_ptr == ' ' || *buf_ptr == '\t') {
                    buf_ptr++;
                    if (buf_ptr >= buf_end)
                        fill_buffer();
                }
                while (*buf_ptr != '\n' || (in_comment && !had_eof)) {
                    CHECK_SIZE_LAB;
                    *e_lab = *buf_ptr++;
                    if (buf_ptr >= buf_end)
                        fill_buffer();
                    switch (*e_lab++) {
                    case BACKSLASH:
                        if (troff)
                            *e_lab++ = BACKSLASH;
                        if (!in_comment) {
                            *e_lab++ = *buf_ptr++;
                            if (buf_ptr >= buf_end)
                                fill_buffer();
                        }
                        break;
                    case '/':
                        if (*buf_ptr == '*' && !in_comment && !quote) {
                            in_comment = 1;
                            *e_lab++ = *buf_ptr++;
                            com_start = e_lab - s_lab - 2;
                        }
                        break;
                    case '"':
                        if (quote == '"')
                            quote = 0;
                        break;
                    case '\'':
                        if (quote == '\'')
                            quote = 0;
                        break;
                    case '*':
                        if (*buf_ptr == '/' && in_comment) {
                            in_comment = 0;
                            *e_lab++ = *buf_ptr++;
                            com_end = e_lab - s_lab;
                        }
                        break;
                    }
                }

                while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
                    e_lab--;
                if (e_lab - s_lab == com_end && bp_save == 0) {
                    /* comment on
                    						 * preprocessor line */
                    if (sc_end == 0)	/* if this is the first comment, we
					 * must set up the buffer */
                        sc_end = &(save_com[0]);
                    else {
                        *sc_end++ = '\n';	/* add newline between
						 * comments */
                        *sc_end++ = ' ';
                        --line_no;
                    }
                    bcopy(s_lab + com_start, sc_end, com_end - com_start);
                    sc_end += com_end - com_start;
                    if (sc_end >= &save_com[sc_size])
                        abort();
                    e_lab = s_lab + com_start;
                    while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
                        e_lab--;
                    bp_save = buf_ptr;	/* save current input buffer */
                    be_save = buf_end;
                    buf_ptr = save_com;	/* fix so that subsequent calls to
					 * lexi will take tokens out of
					 * save_com */
                    *sc_end++ = ' ';	/* add trailing blank, just in case */
                    buf_end = sc_end;
                    sc_end = 0;
                }
                *e_lab = '\0';	/* null terminate line */
                ps.pcase = false;
            }

            if (strncmp(s_lab, "#if", 3) == 0) {
                if (blanklines_around_conditional_compilation) {
                    int c;
                    prefix_blankline_requested++;
                    while ((c = getc(input)) == '\n');
                    ungetc(c, input);
                }
                if ((size_t)ifdef_level < sizeof(state_stack)/sizeof(state_stack[0])) {
                    match_state[ifdef_level].tos = -1;
                    state_stack[ifdef_level++] = ps;
                }
                else
                    diag2(1, "#if stack overflow");
            }
            else if (strncmp(s_lab, "#else", 5) == 0)
                if (ifdef_level <= 0)
                    diag2(1, "Unmatched #else");
                else {
                    match_state[ifdef_level - 1] = ps;
                    ps = state_stack[ifdef_level - 1];
                }
            else if (strncmp(s_lab, "#endif", 6) == 0) {
                if (ifdef_level <= 0)
                    diag2(1, "Unmatched #endif");
                else {
                    ifdef_level--;

#ifdef undef
                    /*
                     * This match needs to be more intelligent before the
                     * message is useful
                     */
                    if (match_state[ifdef_level].tos >= 0
                            && bcmp(&ps, &match_state[ifdef_level], sizeof ps))
                        diag2(0, "Syntactically inconsistent #ifdef alternatives");
#endif
                }
                if (blanklines_around_conditional_compilation) {
                    postfix_blankline_requested++;
                    n_real_blanklines = 0;
                }
            }
            break;		/* subsequent processing of the newline
				 * character will cause the line to be printed */

        case comment:		/* we have gotten a / followed by * this is a biggie */
            if (flushed_nl) {	/* we should force a broken line here */
                flushed_nl = false;
                dump_line();
                ps.want_blank = false;	/* dont insert blank at line start */
                force_nl = false;
            }
            pr_comment();
            break;
        }			/* end of big switch stmt */

        *e_code = '\0';		/* make sure code section is null terminated */
        if (type_code != comment && type_code != newline && type_code != preesc)
            ps.last_token = type_code;
    }				/* end of main while (1) loop */
}
Ejemplo n.º 5
0
void
dump_line(void)
{				/* dump_line is the routine that actually
				 * effects the printing of the new source. It
				 * prints the label section, followed by the
				 * code section with the appropriate nesting
				 * level, followed by any comments */
    int cur_col,
                target_col = 1;
    static int  not_first_line;

    if (ps.procname[0]) {
	if (troff) {
	    if (comment_open) {
		comment_open = 0;
		fprintf(output, ".*/\n");
	    }
	    fprintf(output, ".Pr \"%s\"\n", ps.procname);
	}
	ps.ind_level = 0;
	ps.procname[0] = 0;
    }
    if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
	if (suppress_blanklines > 0)
	    suppress_blanklines--;
	else {
	    ps.bl_line = true;
	    n_real_blanklines++;
	}
    }
    else if (!inhibit_formatting) {
	suppress_blanklines = 0;
	ps.bl_line = false;
	if (prefix_blankline_requested && not_first_line) {
	    if (swallow_optional_blanklines) {
		if (n_real_blanklines == 1)
		    n_real_blanklines = 0;
	    }
	    else {
		if (n_real_blanklines == 0)
		    n_real_blanklines = 1;
	    }
	}
	while (--n_real_blanklines >= 0)
	    putc('\n', output);
	n_real_blanklines = 0;
	if (ps.ind_level == 0)
	    ps.ind_stmt = 0;	/* this is a class A kludge. dont do
				 * additional statement indentation if we are
				 * at bracket level 0 */

	if (e_lab != s_lab || e_code != s_code)
	    ++code_lines;	/* keep count of lines with code */


	if (e_lab != s_lab) {	/* print lab, if any */
	    if (comment_open) {
		comment_open = 0;
		fprintf(output, ".*/\n");
	    }
	    while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
		e_lab--;
	    cur_col = pad_output(1, compute_label_target());
	    if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0
				    || strncmp(s_lab, "#endif", 6) == 0)) {
		char *s = s_lab;
		if (e_lab[-1] == '\n') e_lab--;
		do putc(*s++, output);
		while (s < e_lab && 'a' <= *s && *s<='z');
		while ((*s == ' ' || *s == '\t') && s < e_lab)
		    s++;
		if (s < e_lab)
		    fprintf(output, s[0]=='/' && s[1]=='*' ? "\t%.*s" : "\t/* %.*s */",
			    (int)(e_lab - s), s);
	    }
	    else fprintf(output, "%.*s", (int)(e_lab - s_lab), s_lab);
	    cur_col = count_spaces(cur_col, s_lab);
	}
	else
	    cur_col = 1;	/* there is no label section */

	ps.pcase = false;

	if (s_code != e_code) {	/* print code section, if any */
	    char *p;

	    if (comment_open) {
		comment_open = 0;
		fprintf(output, ".*/\n");
	    }
	    target_col = compute_code_target();
	    {
		int i;

		for (i = 0; i < ps.p_l_follow; i++)
		    if (ps.paren_indents[i] >= 0)
			ps.paren_indents[i] = -(ps.paren_indents[i] + target_col);
	    }
	    cur_col = pad_output(cur_col, target_col);
	    for (p = s_code; p < e_code; p++)
		if (*p == (char) 0200)
		    fprintf(output, "%d", target_col * 7);
		else
		    putc(*p, output);
	    cur_col = count_spaces(cur_col, s_code);
	}
	if (s_com != e_com) {
	    if (troff) {
		int         all_here = 0;
		char *p;

		if (e_com[-1] == '/' && e_com[-2] == '*')
		    e_com -= 2, all_here++;
		while (e_com > s_com && e_com[-1] == ' ')
		    e_com--;
		*e_com = 0;
		p = s_com;
		while (*p == ' ')
		    p++;
		if (p[0] == '/' && p[1] == '*')
		    p += 2, all_here++;
		else if (p[0] == '*')
		    p += p[1] == '/' ? 2 : 1;
		while (*p == ' ')
		    p++;
		if (*p == 0)
		    goto inhibit_newline;
		if (comment_open < 2 && ps.box_com) {
		    comment_open = 0;
		    fprintf(output, ".*/\n");
		}
		if (comment_open == 0) {
		    if ('a' <= *p && *p <= 'z')
			*p = *p + 'A' - 'a';
		    if (e_com - p < 50 && all_here == 2) {
			char *follow = p;
			fprintf(output, "\n.nr C! \\w\1");
			while (follow < e_com) {
			    switch (*follow) {
			    case '\n':
				putc(' ', output);
			    case 1:
				break;
			    case '\\':
				putc('\\', output);
			    default:
				putc(*follow, output);
			    }
			    follow++;
			}
			putc(1, output);
		    }
		    fprintf(output, "\n./* %dp %d %dp\n",
			    ps.com_col * 7,
			    (s_code != e_code || s_lab != e_lab) - ps.box_com,
			    target_col * 7);
		}
		comment_open = 1 + ps.box_com;
		while (*p) {
		    if (*p == BACKSLASH)
			putc(BACKSLASH, output);
		    putc(*p++, output);
		}
	    }
	    else {		/* print comment, if any */
		int target = ps.com_col;
		char *com_st = s_com;

		target += ps.comment_delta;
		while (*com_st == '\t')
		    com_st++, target += 8;	/* ? */
		while (target <= 0)
		    if (*com_st == ' ')
			target++, com_st++;
		    else if (*com_st == '\t')
			target = ((target - 1) & ~7) + 9, com_st++;
		    else
			target = 1;
		if (cur_col > target) {	/* if comment cant fit on this line,
					 * put it on next line */
		    putc('\n', output);
		    cur_col = 1;
		    ++ps.out_lines;
		}
		while (e_com > com_st && isspace(e_com[-1]))
		    e_com--;
		cur_col = pad_output(cur_col, target);
		if (!ps.box_com) {
		    if (star_comment_cont && (com_st[1] != '*' || e_com <= com_st + 1)) {
			if (com_st[1] == ' ' && com_st[0] == ' ' && e_com > com_st + 1)
			    com_st[1] = '*';
			else
			    fwrite(" * ", com_st[0] == '\t' ? 2 : com_st[0] == '*' ? 1 : 3, 1, output);
		    }
		}
		fwrite(com_st, e_com - com_st, 1, output);
		ps.comment_delta = ps.n_comment_delta;
		cur_col = count_spaces(cur_col, com_st);
		++ps.com_lines;	/* count lines with comments */
	    }
	}
	if (ps.use_ff)
	    putc('\014', output);
	else
	    putc('\n', output);
inhibit_newline:
	++ps.out_lines;
	if (ps.just_saw_decl == 1 && blanklines_after_declarations) {
	    prefix_blankline_requested = 1;
	    ps.just_saw_decl = 0;
	}
	else
	    prefix_blankline_requested = postfix_blankline_requested;
	postfix_blankline_requested = 0;
    }
    ps.decl_on_line = ps.in_decl;	/* if we are in the middle of a
					 * declaration, remember that fact for
					 * proper comment indentation */
    ps.ind_stmt = ps.in_stmt & ~ps.in_decl;	/* next line should be
						 * indented if we have not
						 * completed this stmt and if
						 * we are not in the middle of
						 * a declaration */
    ps.use_ff = false;
    ps.dumped_decl_indent = 0;
    *(e_lab = s_lab) = '\0';	/* reset buffers */
    *(e_code = s_code) = '\0';
    *(e_com = s_com) = '\0';
    ps.ind_level = ps.i_l_follow;
    ps.paren_level = ps.p_l_follow;
    paren_target = -ps.paren_indents[ps.paren_level - 1];
    not_first_line = 1;
}
Ejemplo n.º 6
0
void
print_comment ()
{
  register int column, format;
  enum codes comment_type;

  int start_column, found_column;
  int first_comment_line, right_margin;
  int boxed_comment, stars, blankline_delims, paragraph_break, merge_blank_comment_lines;

  int two_contiguous_comments = 0;
  int save_length = 0;
  char *save_ptr = 0;
  char *text_on_line = 0;
  char *line_break_ptr = 0;
  char *start_delim;

  char *line_preamble;
  int line_preamble_length, visible_preamble;
  int suppress_cdb = 0;

  /* GDB_HOOK_print_comment() */

  /* Increment the parser stack, as we will store some things
     there for dump_line to use. */
  inc_pstack ();

  /* Have to do it this way because this piece of shit program doesn't
     always place the last token code on the stack. */
  if (*(token + 1) == '/')
    comment_type = cplus_comment;
  else
    comment_type = comment;

  /* First, decide what kind of comment this is: C++, C, or boxed C.
     Even if this appears to be a normal C comment, we may change our
     minds if we find a star in the right column of the second line,
     in which case that's a boxed comment too. */
  if (comment_type == cplus_comment)
    {
      start_delim = "//";
      line_preamble = "// ";
      line_preamble_length = 3;
      visible_preamble = 1;
      boxed_comment = 0;
      stars = 0;
      blankline_delims = 0;
    }
  else if (*buf_ptr == '*' || *buf_ptr == '-' || *buf_ptr == '=' || *buf_ptr == '_'
	   || (parser_state_tos->col_1 && !format_col1_comments))
    /* Boxed comment.  This block of code will return. */
    {
      int comment_lines_count = 1;

      found_column = start_column = current_column () - 2;
      parser_state_tos->box_com = 1;
      parser_state_tos->com_col = found_column;

      if (blanklines_before_blockcomments)
	prefix_blankline_requested = 1;

      *e_com++ = '/';
      *e_com++ = '*';
      while (1)
	{
	  do
	    {
	      if (*buf_ptr == EOL)	/* Count line numbers within comment blocks */
		++line_no;
	      *e_com++ = *buf_ptr++;
	      CHECK_COM_SIZE;
	    }
	  while (*buf_ptr != '*' && buf_ptr < buf_end);

	  /* We have reached the end of the comment, and it's all on
	     this line. */
	  if (*buf_ptr == '*' && *(buf_ptr + 1) == '/')
	    {
	      if (buf_ptr == buf_end)
		fill_buffer ();

	      buf_ptr += 2;
	      if (buf_ptr == buf_end)
		fill_buffer ();

	      *e_com++ = '*';
	      *e_com++ = '/';
	      *e_com = '\0';
	      parser_state_tos->tos--;

	      /* If this is the only line of a boxed comment, it may
	         be after some other text (e.g., #if foo <comment>),
	         in which case we want to specify the correct column.
	         In the other cases, the leading spaces account for
	         the columns and we start it in column 1. */

	      if (comment_lines_count > 1)
		parser_state_tos->com_col = 1;
	      else
		parser_state_tos->com_col = found_column;
	      return;
	    }

	  /* End of the line, or end of file. */
	  if (buf_ptr == buf_end)
	    {
	      if (*(buf_ptr - 1) == EOL)
		{
		  *(--e_com) = '\0';
		  dump_line (true);
		  comment_lines_count++;
		  parser_state_tos->com_col = 1;
		}

	      fill_buffer ();
	      if (had_eof)
		{
		  *e_com++ = '\0';
		  parser_state_tos->tos--;
		  parser_state_tos->com_col = start_column;
		  return;
		}
	    }
	}
    }
  else
    {
      start_delim = "/*";
      line_preamble = 0;
      line_preamble_length = 0;
      visible_preamble = 0;
      boxed_comment = 0;
      stars = star_comment_cont;
      blankline_delims = comment_delimiter_on_blankline;
    }

  paragraph_break = 0;
  merge_blank_comment_lines = 0;
  first_comment_line = com_lines;
  right_margin = comment_max_col;

  /* Now, compute the correct indentation for this comment
     and whether or not it should be formatted. */
  found_column = current_column () - 2;

  if ((s_lab == e_lab) && (s_code == e_code))
    /* First handle comments which begin the line. */
    {
      if (parser_state_tos->col_1 && !format_col1_comments)
	{
	  format = format_col1_comments;
	  start_column = 1;
	}
      else if (s_com != e_com)
	/* The fool has a line consisting of two contiguous comments.
	   In this case, we don't try too hard, 'cause nothing will
	   look good. */
	{
	  format = 0;
	  start_column = found_column;
	  two_contiguous_comments = 1;
	}
      else
	{
	  format = format_comments;

	  if (parser_state_tos->ind_level <= 0
	      && (!parser_state_tos->in_stmt || (parser_state_tos->in_decl && parser_state_tos->paren_level == 0)))
	    start_column = found_column;
	  else
	    /* This comment is within a procedure or other code. */
	    {
	      start_column = compute_code_target () - unindent_displace;
	      if (start_column < 0)
		start_column = 1;
	    }
	}
    }
  else
    /* This comment follows code of some sort. */
    {
      int target;

      suppress_cdb = 1;

      /* First, compute where the comment SHOULD go. */
      if (parser_state_tos->decl_on_line)
	target = decl_com_ind;
      else if (else_or_endif)
	target = else_endif_col;
      else
	target = com_ind;

      /* Now determine if the code on the line is short enough
         to allow the comment to begin where it should. */
      if (s_code != e_code)
	start_column = count_columns (compute_code_target (), s_code, NULL_CHAR);
      else
	/* s_lab != e_lab : there is a label here. */
	start_column = count_columns (compute_label_target (), s_lab, NULL_CHAR);

      if (start_column < target)
	start_column = target;
      else
	{
	  /* If the too-long code is a pre-processor command,
	     start the comment 1 space afterwards, otherwise
	     start at the next tab mark. */
	  if (else_or_endif)
	    {
	      start_column++;
	      else_or_endif = false;
	    }
	  else
	    start_column += tabsize - ((start_column - 1) % tabsize);
	}

      format = format_comments;
    }

  if (!line_preamble)
    {
      line_preamble_length = 3;
      if (stars)
	{
	  line_preamble = " * ";
	  visible_preamble = 1;
	}
      else
	{
	  line_preamble = "   ";
	  visible_preamble = 0;
	}
    }

  /* These are the parser stack variables used to communicate
     formatting information to dump_line (). */
  parser_state_tos->com_col = (two_contiguous_comments ? 1 : start_column);
  parser_state_tos->box_com = boxed_comment;

  /* Output the beginning comment delimiter.  They are both two
     characters long. */
  *e_com++ = *start_delim;
  *e_com++ = *(start_delim + 1);
  column = start_column + 2;

  /* If the user specified -cdb, put the delimiter on one line. */
  if (blankline_delims && !suppress_cdb)
    {
      char *p = buf_ptr;

      *e_com = '\0';
      dump_line (true);

      /* Check if the delimiter was already on a line by itself,
         and skip whitespace if formating. */
      while (*p == ' ' || *p == TAB)
	p++;
      if (*p == EOL)
	buf_ptr = p + 1;
      else if (format)
	buf_ptr = p;
      if (buf_ptr >= buf_end)
	fill_buffer ();

      column = start_column;
      goto begin_line;
    }
  else if (format)
    {
      *e_com++ = ' ';
      column = start_column + 3;
      while (*buf_ptr == ' ' || *buf_ptr == TAB)
	if (++buf_ptr >= buf_end)
	  fill_buffer ();
    }

  /* Iterate through the lines of the comment */
  while (!had_eof)
    {
      /* Iterate through the characters on one line */
      while (!had_eof)
	{
	  CHECK_COM_SIZE;

	  switch (*buf_ptr)
	    {
	    case ' ':
	    case TAB:
	      /* If formatting, and previous break marker is
	         nonexistant, or before text on line, reset
	         it to here. */
	      if (format && line_break_ptr < text_on_line)
		line_break_ptr = e_com;

	      if (format)
		{
		  /* Don't write two spaces after another, unless the first space it preceeded by a dot. */
		  if (e_com == s_com || e_com[-1] != ' ' || e_com - 1 == s_com || e_com[-2] == '.')
		    {
		      *e_com++ = ' ';
		      column++;
		    }
		}
	      else if (*buf_ptr == ' ')
		{
		  *e_com++ = ' ';
		  column++;
		}
	      else
		{
		  /* Convert the tab to the appropriate number of spaces,
		     based on the column we found the comment in, not
		     the one we're printing in. */
		  int tab_width = (tabsize - ((column + found_column - start_column - 1) % tabsize));
		  column += tab_width;
		  while (tab_width--)
		    *e_com++ = ' ';
		}
	      break;

	    case EOL:
	      /* We may be at the end of a C++ comment */
	      if (comment_type == cplus_comment)
		{
		cplus_exit:
		  parser_state_tos->tos--;
		  parser_state_tos->com_col = (two_contiguous_comments ? 1 : start_column);
		  parser_state_tos->box_com = boxed_comment;
		  *e_com = 0;
		  return;
		}

	      if (format)
		{
		  /* Newline and null are the two characters which
		     end an input line, so check here if we need to
		     get the next line. */
		  if (*buf_ptr == EOL)
		    ++line_no;
		  buf_ptr++;
		  if (buf_ptr >= buf_end)
		    fill_buffer ();

		  /* If there are any spaces between the text and this
		     newline character, remove them. */
		  if (e_com > line_break_ptr && text_on_line < line_break_ptr)
		    e_com = line_break_ptr;

		  /* If this is "\n\n", or "\n<whitespace>\n",
		     it's a paragraph break. */
		  while (*buf_ptr == TAB || *buf_ptr == ' ')
		    if (++buf_ptr >= buf_end)
		      fill_buffer ();
		  if (*buf_ptr == EOL || !text_on_line)
		    {
		      paragraph_break = 1;
		      goto end_line;
		    }

		  /* Also need to eat the preamble. */
		  if (!boxed_comment && current_column () == found_column + 1 && buf_ptr[0] == '*' && buf_ptr[1] != '/')
		    {
		      if (++buf_ptr >= buf_end)
			fill_buffer ();
		      if (*buf_ptr == ' ' && ++buf_ptr >= buf_end)
			fill_buffer ();
		    }

		  /* This is a single newline.  Transform it (and any
		     following whitespace) into a single blank. */
		  if (e_com[-1] != ' ')
		    {
		      line_break_ptr = e_com;
		      *e_com++ = ' ';
		      column++;
		    }
		  continue;
		}

	      /* We are printing this line "as is", so output it
	         and continue on to the next line. */
	      goto end_line;

	    case '*':
	      /* Check if we've reached the end of the comment. */
	      if (comment_type == comment)
		{
		  if (*(buf_ptr + 1) == '/')
		    {
		      /* If it's not a boxed comment, put some whitespace
		         before the ending delimiter.  Otherwise, simply
		         insert the delimiter. */
		      if (!boxed_comment)
			{
			  if (text_on_line)
			    {
			      if (blankline_delims && !suppress_cdb)
				{
				  *e_com = '\0';
				  dump_line (true);
				  *e_com++ = ' ';
				}
			      else
				/* Insert space before closing delim */
			      if (*(e_com - 1) != ' ' && *(e_com - 1) != TAB)
				*e_com++ = ' ';
			    }
			  /* If no text on line, then line is completely empty
			     or starts with preamble, or is beginning of
			     comment and starts with beginning delimiter. */
			  else if (s_com == e_com || *s_com != '/')
			    {
			      e_com = s_com;
			      *e_com++ = ' ';
			    }
			  else
			    /* This is case of first comment line.  Test
			       with:
			       if (first_comment_line != com_lines)
			       abort (); */
			  if (*(e_com - 1) != ' ' && *(e_com - 1) != TAB)
			    *e_com++ = ' ';
			}

		      /* Now insert the ending delimiter */
		      *e_com++ = '*';
		      *e_com++ = '/';
		      *e_com = '\0';

		      /* Skip any whitespace following the comment.  If
		         there is only whitespace after it, print the line.

		         NOTE:  We're not printing the line: TRY IT! */
		      buf_ptr += 2;
		      while (*buf_ptr == ' ' || *buf_ptr == TAB)
			buf_ptr++;
		      if (buf_ptr >= buf_end)
			fill_buffer ();

		      parser_state_tos->tos--;
		      parser_state_tos->com_col = (two_contiguous_comments ? 1 : start_column);
		      parser_state_tos->box_com = boxed_comment;
		      return;
		    }

		  /* If this star is on the second line of the
		     comment in the same column as the star of the
		     beginning delimiter, then consider it
		     a boxed comment. */
		  if (first_comment_line == com_lines - 1 && e_com == s_com + line_preamble_length
		      && current_column () == found_column + 1)
		    {
		      /* Account for change in line_preamble_length: */
		      column -= line_preamble_length - 1;
		      line_preamble = " ";
		      line_preamble_length = 1;
		      boxed_comment = 1;
		      format = 0;
		      blankline_delims = 0;
		      *s_com = ' ';
		      *(s_com + 1) = '*';
		      text_on_line = e_com = s_com + 2;
		      column++;
		      break;
		    }
		}
	      /* If it was not the end of the comment, drop through
	         and insert the star on the line. */

	    default:
	      /* Some textual character. */
	      text_on_line = e_com;
	      *e_com++ = *buf_ptr;
	      column++;
	      break;
	    }


	  /* If we are formatting, check that we haven't exceeded the
	     line length.  If we haven't set line_break_ptr, keep going. */
	  if (format && column > right_margin && line_break_ptr)
	    {
	      if (line_break_ptr < e_com - 1)
		/* Here if we are really "breaking" the line:  the line
		   break is before some text we've seen. */
		{
		  *line_break_ptr = '\0';
		  save_ptr = line_break_ptr + 1;
		  save_length = e_com - save_ptr;
		  e_com = line_break_ptr;

		  /* If we had to go past `right_margin' to print stuff out,
		     extend `right_margin' out to this point. */
		  if ((column - save_length) > right_margin)
		    right_margin = column - save_length;
		}
	      else
		/* The line break is after the last text;  we're really
		   truncating the line. */
		{
		  if (comment_type == cplus_comment)
		    {
		      while (*buf_ptr == TAB || *buf_ptr == ' ')
			buf_ptr++;
		      buf_ptr--;
		      if (*buf_ptr == EOL)
			goto cplus_exit;
		    }
		  else
		    {
		      while (*buf_ptr == TAB || *buf_ptr == ' ' || *buf_ptr == EOL)
			buf_ptr++;

		      buf_ptr--;
		    }

		  *e_com = '\0';
		}
	      goto end_line;
	    }

	  if (*buf_ptr == EOL)
	    ++line_no;
	  buf_ptr++;
	  if (buf_ptr == buf_end)
	    fill_buffer ();
	}


    end_line:
      /* Compress pure whitespace lines into newlines. */
      if (!text_on_line && !visible_preamble && !(first_comment_line == com_lines))
	e_com = s_com;
      *e_com = '\0';
      dump_line (true);
      /* We're in the middle of a C-comment, don't add blank lines! */
      prefix_blankline_requested = 0;

      /* If formatting (paragraph_break is only used for formatted
         comments) and user wants blank lines merged, kill all white
         space after the "\n\n" indicating a paragraph break. */
      if (paragraph_break)
	{
	  if (merge_blank_comment_lines)
	    while (*buf_ptr == EOL || *buf_ptr == ' ' || *buf_ptr == TAB)
	      {
		if (*buf_ptr == EOL)
		  ++line_no;
		if (++buf_ptr >= buf_end)
		  fill_buffer ();
	      }
	  paragraph_break = 0;
	}
      else
	{
	  /* If it was a paragraph break (`if' clause), we scanned ahead
	     one character.  So, here in the `else' clause, advance buf_ptr. */
	  if (*buf_ptr == EOL)
	    ++line_no;
	  buf_ptr++;
	  if (buf_ptr >= buf_end)
	    fill_buffer ();
	}

    begin_line:
      if (had_eof)
	break;

      /* Indent the line properly.  If it's a boxed comment, align with
         the '*' in the beginning slash-star and start inserting there.
         Otherwise, insert blanks for alignment, or a star if the
         user specified -sc. */
      if (line_preamble)
	{
	  (void) memcpy (e_com, line_preamble, line_preamble_length);
	  e_com += line_preamble_length;
	  column = start_column + line_preamble_length;
	}
      else
	column = start_column;
      line_break_ptr = 0;

      /* If we have broken the line before the end for formatting,
         copy the text after the break onto the beginning of this
         new comment line. */
      if (save_ptr)
	{
	  while ((*save_ptr == ' ' || *save_ptr == TAB) && save_length)
	    {
	      save_ptr++;
	      save_length--;
	    }
	  (void) memcpy (e_com, save_ptr, save_length);
	  text_on_line = e_com;
	  e_com += save_length;
	  /* We only break if formatting, in which cases there
	     are no tabs, only spaces. */
	  column += save_length;
	  save_ptr = 0;
	  save_length = 0;
	}
      else
	{
	  while (*buf_ptr == ' ' || *buf_ptr == TAB)
	    if (++buf_ptr >= buf_end)
	      fill_buffer ();
	  text_on_line = 0;
	}
    }

  parser_state_tos->tos--;
  parser_state_tos->com_col = (two_contiguous_comments ? 1 : start_column);
  parser_state_tos->box_com = boxed_comment;
}
Ejemplo n.º 7
0
void
dump_line(void)
{				/* dump_line is the routine that actually
				 * effects the printing of the new source. It
				 * prints the label section, followed by the
				 * code section with the appropriate nesting
				 * level, followed by any comments */
    int cur_col,
                target_col = 1;
    static int  not_first_line;

    if (ps.procname[0]) {
	ps.ind_level = 0;
	ps.procname[0] = 0;
    }
    if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
	if (suppress_blanklines > 0)
	    suppress_blanklines--;
	else {
	    ps.bl_line = true;
	    n_real_blanklines++;
	}
    }
    else if (!inhibit_formatting) {
	suppress_blanklines = 0;
	ps.bl_line = false;
	if (prefix_blankline_requested && not_first_line) {
	    if (swallow_optional_blanklines) {
		if (n_real_blanklines == 1)
		    n_real_blanklines = 0;
	    }
	    else {
		if (n_real_blanklines == 0)
		    n_real_blanklines = 1;
	    }
	}
	while (--n_real_blanklines >= 0)
	    putc('\n', output);
	n_real_blanklines = 0;
	if (ps.ind_level == 0)
	    ps.ind_stmt = 0;	/* this is a class A kludge. dont do
				 * additional statement indentation if we are
				 * at bracket level 0 */

	if (e_lab != s_lab || e_code != s_code)
	    ++code_lines;	/* keep count of lines with code */


	if (e_lab != s_lab) {	/* print lab, if any */
	    if (comment_open) {
		comment_open = 0;
		fprintf(output, ".*/\n");
	    }
	    while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
		e_lab--;
	    *e_lab = '\0';
	    cur_col = pad_output(1, compute_label_target());
	    if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0
				    || strncmp(s_lab, "#endif", 6) == 0)) {
		char *s = s_lab;
		if (e_lab[-1] == '\n') e_lab--;
		do putc(*s++, output);
		while (s < e_lab && 'a' <= *s && *s<='z');
		while ((*s == ' ' || *s == '\t') && s < e_lab)
		    s++;
		if (s < e_lab)
		    fprintf(output, s[0]=='/' && s[1]=='*' ? "\t%.*s" : "\t/* %.*s */",
			    (int)(e_lab - s), s);
	    }
	    else fprintf(output, "%.*s", (int)(e_lab - s_lab), s_lab);
	    cur_col = count_spaces(cur_col, s_lab);
	}
	else
	    cur_col = 1;	/* there is no label section */

	ps.pcase = false;

	if (s_code != e_code) {	/* print code section, if any */
	    char *p;

	    if (comment_open) {
		comment_open = 0;
		fprintf(output, ".*/\n");
	    }
	    target_col = compute_code_target();
	    {
		int i;

		for (i = 0; i < ps.p_l_follow; i++)
		    if (ps.paren_indents[i] >= 0)
			ps.paren_indents[i] = -(ps.paren_indents[i] + target_col);
	    }
	    cur_col = pad_output(cur_col, target_col);
	    for (p = s_code; p < e_code; p++)
		if (*p == (char) 0200)
		    fprintf(output, "%d", target_col * 7);
		else
		    putc(*p, output);
	    cur_col = count_spaces(cur_col, s_code);
	}
	if (s_com != e_com) {		/* print comment, if any */
	    int target = ps.com_col;
	    char *com_st = s_com;

	    target += ps.comment_delta;
	    while (*com_st == '\t')	/* consider original indentation in
				     * case this is a box comment */
		com_st++, target += tabsize;
	    while (target <= 0)
		if (*com_st == ' ')
		    target++, com_st++;
		else if (*com_st == '\t')
		    target = tabsize * (1 + (target - 1) / tabsize) + 1, com_st++;
		else
		    target = 1;
	    if (cur_col > target) {	/* if comment can't fit on this line,
				     * put it on next line */
		putc('\n', output);
		cur_col = 1;
		++ps.out_lines;
	    }
	    while (e_com > com_st && isspace((unsigned char)e_com[-1]))
		e_com--;
	    (void)pad_output(cur_col, target);
	    fwrite(com_st, e_com - com_st, 1, output);
	    ps.comment_delta = ps.n_comment_delta;
	    ++ps.com_lines;	/* count lines with comments */
	}
	if (ps.use_ff)
	    putc('\014', output);
	else
	    putc('\n', output);
	++ps.out_lines;
	if (ps.just_saw_decl == 1 && blanklines_after_declarations) {
	    prefix_blankline_requested = 1;
	    ps.just_saw_decl = 0;
	}
	else
	    prefix_blankline_requested = postfix_blankline_requested;
	postfix_blankline_requested = 0;
    }
    ps.decl_on_line = ps.in_decl;	/* if we are in the middle of a
					 * declaration, remember that fact for
					 * proper comment indentation */
    ps.ind_stmt = ps.in_stmt & ~ps.in_decl;	/* next line should be
						 * indented if we have not
						 * completed this stmt and if
						 * we are not in the middle of
						 * a declaration */
    ps.use_ff = false;
    ps.dumped_decl_indent = 0;
    *(e_lab = s_lab) = '\0';	/* reset buffers */
    *(e_code = s_code) = '\0';
    *(e_com = s_com = combuf + 1) = '\0';
    ps.ind_level = ps.i_l_follow;
    ps.paren_level = ps.p_l_follow;
    if (ps.paren_level > 0)
	paren_target = -ps.paren_indents[ps.paren_level - 1];
    not_first_line = 1;
}
Ejemplo n.º 8
0
dump_line()
{				/* dump_line is the routine that actually
				 * effects the printing of the new source.
				 * It prints the label section, followed
				 * by the code section with the
				 * appropriate nesting level, followed by
				 * any comments */
    register int cur_col,
                temp_col,
                target_col;

    if (ps.procname[0]) {
	if (troff)
	    fprintf(output, ".Pr \"%s\"\n", ps.procname);
	ps.ind_level = 0;
	ps.procname[0] = 0;
    }
    if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
	if (suppress_blanklines>0) suppress_blanklines--;
	else {
	ps.bl_line = true;
	n_real_blanklines++;
	}
    }
    else if (!inhibit_formatting) {
	suppress_blanklines = 0;
	ps.bl_line = false;
	if (prefix_blankline_requested)
	    if (swallow_optional_blanklines) {
		if (n_real_blanklines == 1)
		    n_real_blanklines = 0;
	    }
	    else {
		if (n_real_blanklines == 0)
		    n_real_blanklines = 1;
	    }
	while (--n_real_blanklines >= 0)
	    putc('\n', output);
	n_real_blanklines = 0;
	if (ps.ind_level == 0)
	    ps.ind_stmt = 0;	/* this is a class A kludge. dont do
				 * additional statement indentation if we
				 * are at bracket level 0 */

	if (e_lab != s_lab || e_code != s_code)
	    ++code_lines;	/* keep count of lines with code */


	if (e_lab != s_lab) {	/* print lab, if any */
	    if (comment_open) {
		comment_open = 0;
		fprintf(output, ".*/\n");
	    }
	    while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
		e_lab--;
	    cur_col = pad_output(1, compute_label_target());
	    fprintf(output, "%.*s", e_lab - s_lab, s_lab);
	    cur_col = count_spaces(cur_col, s_lab);
	}
	else
	    cur_col = 1;	/* there is no label section */

	ps.pcase = false;

	if (s_code != e_code) {	/* print code section, if any */
	    register char *p;

	    if (comment_open) {
		comment_open = 0;
		fprintf(output, ".*/\n");
	    }
	    target_col = compute_code_target();
	    {
		register    i;

		for (i = 0; i < ps.p_l_follow; i++)
		    if (ps.paren_indents[i] >= 0)
			ps.paren_indents[i] = -(ps.paren_indents[i] + target_col);
	    }
	    cur_col = pad_output(cur_col, target_col);
	    for (p = s_code; p < e_code; p++)
		if (*p == 0200)
		    fprintf(output, "%d", target_col * 7);
		else
		    putc(*p, output);
	    cur_col = count_spaces(cur_col, s_code);
	}
	if (s_com != e_com)
	    if (troff) {
		register char *p;

		if (e_com[-1] == '/' && e_com[-2] == '*')
		    e_com -= 2;
		while (e_com > s_com && e_com[-1] == ' ')
		    e_com--;
		*e_com = 0;
		p = s_com;
		while (*p == ' ')
		    p++;
		if (p[0] == '/' && p[1] == '*')
		    p += 2;
		else if (p[0] == '*')
		    p += p[1] == '/' ? 2 : 1;
		while (*p == ' ')
		    p++;
		if (*p == 0)
		    goto inhibit_newline;
		if (!comment_open) {
		    if ('a' <= *p && *p <= 'z')
			*p = *p + 'A' - 'a';
		    if (s_code != e_code || s_lab != e_lab) {
			fprintf(output, "\\c\n./* %dp 1 %dp\n",
				ps.com_col * 7, target_col * 7);
		    }
		    else
			fprintf(output, "./* %dp 0 %dp\n",
				ps.com_col * 7, target_col * 7);
		}
		comment_open = 1;
		while (*p) {
		    if (*p == BACKSLASH)
			putc(BACKSLASH, output);
		    putc(*p++, output);
		}
	    }
	    else {		/* print comment, if any */
		register    target = ps.com_col;
		register char *com_st = s_com;

		target += ps.comment_delta;
		while (target <= 0)
		    if (*s_com == ' ')
			target++, s_com++;
		    else if (*s_com == '\t')
			target = ((target - 1) & ~7) + 9, s_com++;
		    else
			target = 1;
		if (cur_col > target) {	/* if comment cant fit on this
					 * line, put it on next line */
		    putc('\n', output);
		    cur_col = 1;
		    ++ps.out_lines;
		}
		cur_col = pad_output(cur_col, target);
		if (!ps.box_com) {
		    if (star_comment_cont && com_st[1] != '*')
			if (com_st[1] == ' ' && com_st[0] == ' ')
			    com_st[1] = '*';
			else
			    fwrite(" * ", com_st[0] == '\t' ? 2 : com_st[0] == '*' ? 1 : 3, 1, output);
		}
		fwrite(com_st, e_com - com_st, 1, output);
		ps.comment_delta = ps.n_comment_delta;
		cur_col = count_spaces(cur_col, com_st);
		++ps.com_lines;	/* count lines with comments */
	    }
	if (ps.use_ff)
	    putc('\014', output);
	else
	    putc('\n', output);
inhibit_newline:
	++ps.out_lines;
	if (ps.just_saw_decl == 1 && blanklines_after_declarations) {
	    prefix_blankline_requested = 1;
	    ps.just_saw_decl = 0;
	}
	else
	    prefix_blankline_requested = postfix_blankline_requested;
	postfix_blankline_requested = 0;
    }
    ps.decl_on_line = ps.in_decl;	/* if we are in the middle of a
					 * declaration, remember that fact
					 * for proper comment indentation */
    ps.ind_stmt = ps.in_stmt & ~ps.in_decl;	/* next line should be
						 * indented if we have not
						 * completed this stmt and
						 * if we are not in the
						 * middle of a declaration */
    ps.use_ff = false;
    ps.dumped_decl_indent = 0;
    *(e_lab = s_lab) = '\0';	/* reset buffers */
    *(e_code = s_code) = '\0';
    *(e_com = s_com) = '\0';
    ps.ind_level = ps.i_l_follow;
    ps.paren_level = ps.p_l_follow;
    paren_target = -ps.paren_indents[ps.paren_level - 1];
    return;
};