示例#1
0
文件: io.c 项目: edgar-pek/PerspicuOS
int
compute_code_target(void)
{
    int target_col = ps.ind_size * ps.ind_level + 1;

    if (ps.paren_level)
	if (!lineup_to_parens)
	    target_col += continuation_indent
		* (2 * continuation_indent == ps.ind_size ? 1 : ps.paren_level);
	else {
	    int w;
	    int t = paren_target;

	    if ((w = count_spaces(t, s_code) - max_col) > 0
		    && count_spaces(target_col, s_code) <= max_col) {
		t -= w + 1;
		if (t > target_col)
		    target_col = t;
	    }
	    else
		target_col = t;
	}
    else if (ps.ind_stmt)
	target_col += continuation_indent;
    return target_col;
}
示例#2
0
int code_target()
{
   register        target_col = ps.ind_size * ps.ind_level + 1;

   if (ps.paren_level)
      if (!lineup_to_parens)
	 target_col += continuation_indent * ps.paren_level;
      else
      {
	 register        w;
	 register        t = paren_target;

	 if ((w = count_spaces(t, s_code) - max_col) > 0
	     && count_spaces(target_col, s_code) <= max_col)
	 {
	    t -= w + 1;
	    if (t > target_col)
	       target_col = t;
	 } else
	    target_col = t;
      }
   else if (ps.ind_stmt)
      target_col += continuation_indent;
   return target_col;
}
示例#3
0
/* Figure out where we should put the code in codebuf.
   Return the column number in spaces.  */
int
compute_code_target()
{
#ifdef MSDOS
    register int target_col = parser_state_tos->ind_level + 1;
#else
    register    target_col = parser_state_tos->ind_level + 1;
#endif /* MSDOS */

    if (parser_state_tos->paren_level)
	if (!lineup_to_parens)
	    target_col += continuation_indent * parser_state_tos->paren_level;
	else {
#ifdef MSDOS
	    register int w;
	    register int t = paren_target;
#else
	    register    w;
	    register    t = paren_target;
#endif /* MSDOS */

	    if ((w = count_spaces(t, s_code) - max_col) > 0
		    && count_spaces(target_col, s_code) <= max_col) {
		t -= w + 1;
		if (t > target_col)
		    target_col = t;
	    }
	    else
		target_col = t;
	}
    else if (parser_state_tos->ind_stmt)
	target_col += continuation_indent;
    return target_col;
}
示例#4
0
int			extract_opcode(char **line)
{
	char	**parts;
	int		i;
	int		nb_spaces;
	char	*op_name;
	char	*tmp;

	if (is_comment_or_empty(*line))
		return (0);
	tmp = *line;
	nb_spaces = count_spaces(*line);
	parts = ft_split_whitespaces(*line);
	op_name = ft_strdup(parts[0]);
	i = 0;
	while (parts[i])
		free(parts[i++]);
	free(parts);
	if (op_name != NULL)
	{
		return (get_code_value(op_name, line, tmp, nb_spaces));
	}
	else
		throw_error(1);
	return (0);
}
示例#5
0
文件: ex16.c 项目: DataParisian/knkc
int main(void){
	char inputStr[N + 1] = "";
	
	printf("Enter a string with less than %d characters: ", N);
	gets(inputStr);
	
	printf("There a %d space characters\n", count_spaces(inputStr));
	
	return 0;
}
int main(int argc, char *argv[])
{
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <string>\n", argv[0]);
    return 1;
  }

  printf("Spaces: %d\n", count_spaces(argv[1]));

  return 0;
}
示例#7
0
t_info	how_many_spaces(t_flist *f_list, t_info max)
{
	max.nlink = count_spaces(f_list->st.st_nlink, max.nlink);
	if (S_ISCHR(f_list->st.st_mode) || S_ISBLK(f_list->st.st_mode))
	{
		max.dev = count_spaces(major(f_list->st.st_rdev), max.dev);
		max.size = count_spaces(minor(f_list->st.st_rdev), max.size);
	}
	else
		max.size = count_spaces(f_list->st.st_size, max.size);
	if (getgrgid(f_list->st.st_gid))
	{
		if (ft_strlen(getgrgid(f_list->st.st_gid)->gr_name) > max.gid)
			max.gid = ft_strlen(getgrgid(f_list->st.st_gid)->gr_name);
	}
	else
		max.gid = count_spaces(f_list->st.st_gid, max.gid);
	if (getpwuid(f_list->st.st_uid))
	{
		if (ft_strlen(getpwuid(f_list->st.st_uid)->pw_name) > max.uid)
			max.uid = ft_strlen(getpwuid(f_list->st.st_uid)->pw_name);
	}
	else
		max.uid = count_spaces(f_list->st.st_uid, max.uid);
	max.total += f_list->st.st_blocks;
	return (max);
}
示例#8
0
void		parse_args(char *buf, t_data *data)
{
  int		n;
  int		k;

  if ((buf = my_epur_str(buf)) == NULL)
    exit(1);
  data->n_max = count_spaces(buf) + 1;
  init_all_cmd(data, buf, data->n_max);
  n = 0;
  k = 0;
  word_tab(data, buf, n, k);
  free(buf);
}
示例#9
0
int main(int argc, char *argv[])
{
   int count = 0;
   const char *sentence = "this is a test";
   
   count = count_spaces(sentence);

   printf("The sentence was: %s\n", sentence);
   printf("There were %d spaces in the sentence.", count);
   
   getchar();
   getchar();
   return 0;
}
示例#10
0
char *camelize(const char *s) {
  char *res = malloc((strlen(s) - count_spaces(s)  + 1) * sizeof(char));
  char *start = res;

  while (*s) {
    if (*s == ' ')
      *res++ = *++s - ('a' - 'A');
    else
      *res++ = *s;

    s++;
  }

  *res = '\0';

  return start;
}
示例#11
0
文件: ft_strtrim.c 项目: Sleli42/fdf
char			*ft_strtrim(char const *s)
{
	size_t	len;
	size_t	start;

	if (s)
	{
		len = count_spaces_return(s, ft_strlen(s));
		start = count_spaces(s);
		if (!len && !start)
			return (ft_strdup(s));
		else if (start == ft_strlen(s))
			return (ft_strsub(s, 0, 0));
		else
			return (ft_strsub(s, start, len - start + 1));
	}
	return (NULL);
}
示例#12
0
char		*ft_strtrim(char const *s)
{
	int				spaces;
	char			*new_string;
	unsigned int	i;
	unsigned int	j;

	if (!s)
		return (NULL);
	spaces = count_spaces(s);
	i = 0;
	j = 0;
	new_string = (char*)malloc(sizeof(new_string) * (ft_strlen(s) - spaces));
	if (new_string == NULL)
		return (NULL);
	if (spaces == 0)
		new_string = ft_strcpy(new_string, s);
	while (s[i] == ' ' || s[i] == '\t' || s[i] == '\n')
		i++;
	while (j < (ft_strlen(s) - spaces))
		new_string[j++] = s[i++];
	new_string[j] = '\0';
	return (new_string);
}
示例#13
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;
        }
    }
}
示例#14
0
void 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, target_col;
   static          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_opt_bl)
	 {
	    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, label_target());
	 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 */
	 register char  *p;

	 if (comment_open)
	 {
	    comment_open = 0;
	    fprintf(output, ".*/\n");
	 }
	 target_col = 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 == (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;
	    register 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)
	       {
		  register 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 */
	    register        target = ps.com_col;
	    register 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, (int)(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 && bl_aft_decl)
      {
	 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;
   return;
}
示例#15
0
/* Parse a line like: "active raid1 sdb1[0] sdc1[1](F)" */
static guestfs_int_mdstat_list *
parse_md_stat_line (char *line)
{
  guestfs_int_mdstat_list *ret;
  guestfs_int_mdstat *t;
  size_t spaces, n, i, len;
  char *next;
  char *p, *q;

  ret = malloc (sizeof *ret);
  if (!ret) {
    reply_with_perror ("malloc");
    return NULL;
  }

  /* We don't know exactly how many entries we will need yet, but we
   * can estimate it, and this will always be an over-estimate.
   */
  spaces = count_spaces (line);
  ret->guestfs_int_mdstat_list_val =
    calloc (spaces+1, sizeof (struct guestfs_int_mdstat));
  if (ret->guestfs_int_mdstat_list_val == NULL) {
    reply_with_perror ("calloc");
    free (ret);
    return NULL;
  }

  for (n = 0; *line; line = next) {
    len = strcspn (line, " ");
    if (line[len] == '\0')
      next = &line[len];
    else {
      line[len] = '\0';
      next = &line[len+1];
    }

    if (verbose)
      printf ("mdstat: %s\n", line);

    /* Looking for entries that contain "[..]", skip ones which don't. */
    p = strchr (line, '[');
    if (p == NULL)
      continue;
    q = strchr (line, ']');
    if (q == NULL)
      continue;
    if (p > q)
      continue;

    ret->guestfs_int_mdstat_list_len = n+1;
    t = &ret->guestfs_int_mdstat_list_val[n];

    /* Device name is everything before the '[' character, but we
     * need to prefix with /dev/.
     */
    if (p == line) {
      reply_with_error ("device entry is too short: %s", line);
      goto error;
    }

    *p = '\0';
    if (asprintf (&t->mdstat_device, "/dev/%s", line) == -1) {
      reply_with_perror ("asprintf");
      goto error;
    }

    /* Device index is the number after '['. */
    line = p+1;
    *q = '\0';
    if (sscanf (line, "%" SCNi32, &t->mdstat_index) != 1) {
      reply_with_error ("not a device number: %s", line);
      goto error;
    }

    /* Looking for flags "(F)(S)...". */
    line = q+1;
    len = strlen (line);
    t->mdstat_flags = malloc (len+1);
    if (!t->mdstat_flags) {
      reply_with_error ("malloc");
      goto error;
    }

    for (i = 0; *line; line++) {
      if (c_isalpha (*line))
        t->mdstat_flags[i++] = *line;
    }
    t->mdstat_flags[i] = '\0';

    n++;
  }

  return ret;

 error:
  for (i = 0; i < spaces+1; ++i) {
    free (ret->guestfs_int_mdstat_list_val[i].mdstat_device);
    free (ret->guestfs_int_mdstat_list_val[i].mdstat_flags);
  }
  free (ret->guestfs_int_mdstat_list_val);
  free (ret);
  return NULL;
}
示例#16
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;
}
示例#17
0
int main(int argc, char** argv) {

    std::string s = "There are 4 spaces here.";
    int i = count_spaces( s, std::locale());
    return 0;
}
示例#18
0
文件: io.c 项目: dank101/4.2BSD
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;

    bl_line = true;	       /* if we don't find otherwise, assume a
			          blank line */

    if (ind_level == 0)
	ind_stmt = 0;	       /* this is a class A kludge. don't 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 (pcase)	       /* if the label is really a case, we must
			          indent */
	    cur_col = pad_output (1, case_ind * ind_size + 1);
	else {
	    if (*s_lab == '#') /* check for #define, etc */
		cur_col = 1;
	    else
		cur_col = pad_output (1, ind_size * (ind_level - label_offset) + 1);
	}

	write (output, s_lab, e_lab - s_lab);
	cur_col = count_spaces (cur_col, s_lab);
    /* count_spaces gives number of characters, considering tabs */
	bl_line = false;       /* line not blank after all */
    }
    else
	cur_col = 1;	       /* there is no label section */

    pcase = false;

    if (s_code != e_code) {    /* print code section, if any */
	target_col = ind_size * (ind_level + paren_level + ind_stmt) + 1;

	cur_col = pad_output (cur_col, target_col);
    /* pad_output writes enough tabs and spaces to get the current char
       position up to target_col */
	write (output, s_code, e_code - s_code);
	cur_col = count_spaces (cur_col, s_code);
	bl_line = false;       /* line not blank */
    }

    if ((cur_col - 1) > max_col && output!=1)/* check for line too long */
	printf ("%d: Code has %d chars, max is %d\n", line_no, (cur_col - 1), max_col);

    if (s_com != e_com) {      /* print comment, if any */
	if (cur_col > com_col && count_spaces (cur_col, s_com) >= max_col) {
	/* if comment can't fit on this line, put it on next line */
	    write (output, "\n", 1);
	    cur_col = 1;
	    ++out_lines;
	}
	cur_col = pad_output (cur_col, com_col);
	write (output, s_com, e_com - s_com);

	cur_col = count_spaces (cur_col, s_com);
	if ((cur_col - 1) > max_col && output!=1)/* check for too long comment */
	    printf ("%d: Comment goes to column %d.  Max is %d\n",
		line_no, (cur_col - 1), max_col);

	bl_line = false;
	++com_lines;	       /* count lines with comments */
    }

    if (use_ff)
	write (output, &ff, 1);/* end the output with a ff */
    else
	write (output, "\n", 1); /* or a newline */
    use_ff = false;
    *(e_lab = s_lab) = '\0';   /* reset buffers */
    *(e_code = s_code) = '\0';
    *(e_com = s_com) = '\0';

    ind_level = i_l_follow;
    paren_level = p_l_follow;
    ++out_lines;
    decl_on_line = in_decl;    /* if we are in the middle of a
			          declaration, remember that fact for
			          proper comment indentation */
    ind_stmt = in_stmt & ~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 */

    return;
};
示例#19
0
void
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,
                target_col;
    static      not_first_line;

    if (parser_state_tos->procname[0]) {
	if (troff) {
	    if (comment_open) {
		comment_open = 0;
		fprintf(output, ".*/\n");
	    }
	    fprintf(output, ".Pr \"%.*s\"\n", parser_state_tos->procname_end - parser_state_tos->procname,
		    parser_state_tos->procname);
	}
	parser_state_tos->ind_level = 0;
	parser_state_tos->procname = "\0";
    }
    if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
      /* If we have a formfeed on a blank line, we should just output it,
	 rather than treat it as a normal blank line.  */
      if (parser_state_tos->use_ff)
	{
	  putc('\014',output);
	  parser_state_tos->use_ff = false;
	}
      else
	{
	  if (suppress_blanklines > 0)
	    suppress_blanklines--;
	  else {
	    parser_state_tos->bl_line = true;
	    n_real_blanklines++;
	  }
	}
    }
    else if (!inhibit_formatting) {
	suppress_blanklines = 0;
	parser_state_tos->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 (parser_state_tos->ind_level == 0)
	    parser_state_tos->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)) {
	        /* Treat #else and #endif as a special case because any
		   text after #else or #endif should be converted to
		   a comment.  */
		register 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 */",
			    e_lab - s, s);
	    }
	    else 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 */

	parser_state_tos->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();
	    /* If a line ends in an lparen character, the following
	       line should not line up with the parenthesis, but
	       should be indented by the usual amount.  */
	    if (parser_state_tos->last_token == lparen)
	      {
		parser_state_tos->paren_indents[parser_state_tos->p_l_follow-1]
		  += ind_size - 1;
	      }
	    {
		register    i;

		for (i = 0; i < parser_state_tos->p_l_follow; i++)
		    if (parser_state_tos->paren_indents[i] >= 0)
			parser_state_tos->paren_indents[i] = -(parser_state_tos->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;
		register 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 && parser_state_tos->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) {
			register 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",
			    parser_state_tos->com_col * 7,
			    (s_code != e_code || s_lab != e_lab) - parser_state_tos->box_com,
			    target_col * 7);
		}
		comment_open = 1 + parser_state_tos->box_com;
		while (*p) {
		    if (*p == BACKSLASH)
			putc(BACKSLASH, output);
		    putc(*p++, output);
		}
	    }
	    else {		/* print comment, if any */
#ifdef MSDOS
		register int target = parser_state_tos->com_col;
#else
		register    target = parser_state_tos->com_col;
#endif
		register char *com_st = s_com;

		target += parser_state_tos->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;
		    ++parser_state_tos->out_lines;
		}
		while (e_com > com_st && isspace(e_com[-1]))
		    e_com--;
		cur_col = pad_output(cur_col, target);
		if (!parser_state_tos->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);
		parser_state_tos->comment_delta = parser_state_tos->n_comment_delta;
		cur_col = count_spaces(cur_col, com_st);
		++parser_state_tos->com_lines;	/* count lines with comments */
	    }
	  }
	if (parser_state_tos->use_ff)
	  {
	    putc('\014', output);
	    parser_state_tos->use_ff = false;
	  }
	else
	    putc('\n', output);
inhibit_newline:
	++parser_state_tos->out_lines;
	if (parser_state_tos->just_saw_decl == 1 && blanklines_after_declarations) {
	    prefix_blankline_requested = 1;
	    parser_state_tos->just_saw_decl = 0;
	}
	else
	    prefix_blankline_requested = postfix_blankline_requested;
	postfix_blankline_requested = 0;
    }
    parser_state_tos->decl_on_line = parser_state_tos->in_decl;	/* if we are in the middle of a
					 * declaration, remember that fact for
					 * proper comment indentation */
    parser_state_tos->ind_stmt = parser_state_tos->in_stmt & ~parser_state_tos->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 */
    parser_state_tos->dumped_decl_indent = 0;
    *(e_lab = s_lab) = '\0';	/* reset buffers */
    *(e_code = s_code) = '\0';
    *(e_com = s_com) = '\0';
    parser_state_tos->ind_level = parser_state_tos->i_l_follow;
    parser_state_tos->paren_level = parser_state_tos->p_l_follow;
    paren_target = -parser_state_tos->paren_indents[parser_state_tos->paren_level - 1];
    not_first_line = 1;
    return;
}
示例#20
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;
	}
    }
}
示例#21
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;
};