示例#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;
	}
    }
}
示例#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;
        }
    }
}
示例#3
0
文件: io.c 项目: edgar-pek/PerspicuOS
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;
}
示例#4
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;
}
示例#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]) {
	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;
}
示例#6
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;
};