⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 indent.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 3 页
字号:
					 * save_com */		*sc_end++ = ' ';/* add trailing blank, just in case */		buf_end = sc_end;		sc_end = 0;		break;	    }			/* end of switch */	    if (type_code != 0)	/* we must make this check, just in case there				 * was an unexpected EOF */		type_code = lexi();	/* read another token */	    /* if (ps.search_brace) ps.procname[0] = 0; */	    if ((is_procname = ps.procname[0]) && flushed_nl		    && !procnames_start_line && ps.in_decl		    && type_code == ident)		flushed_nl = 0;	}			/* end of while (search_brace) */	last_else = 0;check_type:	if (type_code == 0) {	/* we got eof */	    if (s_lab != e_lab || s_code != e_code		    || s_com != e_com)	/* must dump end of line */		dump_line();	    if (ps.tos > 1)	/* check for balanced braces */		diag(1, "Stuff missing from end of file.");	    if (verbose) {		printf("There were %d output lines and %d comments\n",		       ps.out_lines, ps.out_coms);		printf("(Lines with comments)/(Lines with code): %6.3f\n",		       (1.0 * ps.com_lines) / code_lines);	    }	    fflush(output);	    exit(found_err);	}	if (		(type_code != comment) &&		(type_code != newline) &&		(type_code != preesc) &&		(type_code != form_feed)) {	    if (force_nl &&		    (type_code != semicolon) &&		    (type_code != lbrace || !btype_2)) {		/* we should force a broken line here */		if (verbose && !flushed_nl)		    diag(0, "Line broken");		flushed_nl = false;		dump_line();		ps.want_blank = false;	/* dont insert blank at line start */		force_nl = false;	    }	    ps.in_stmt = true;	/* turn on flag which causes an extra level of				 * indentation. this is turned off by a ; or				 * '}' */	    if (s_com != e_com) {	/* the turkey has embedded a comment					 * in a line. fix it */		*e_code++ = ' ';		for (t_ptr = s_com; *t_ptr; ++t_ptr) {		    CHECK_SIZE_CODE;		    *e_code++ = *t_ptr;		}		*e_code++ = ' ';		*e_code = '\0';	/* null terminate code sect */		ps.want_blank = false;		e_com = s_com;	    }	}	else if (type_code != comment)	/* preserve force_nl thru a comment */	    force_nl = false;	/* cancel forced newline after newline, form				 * feed, etc */	/*-----------------------------------------------------*\	|	   do switch on type of token scanned		|	\*-----------------------------------------------------*/	CHECK_SIZE_CODE;	switch (type_code) {	/* now, decide what to do with the token */	case form_feed:	/* found a form feed in line */	    ps.use_ff = true;	/* a form feed is treated much like a newline */	    dump_line();	    ps.want_blank = false;	    break;	case newline:	    if (ps.last_token != comma || ps.p_l_follow > 0		    || !ps.leave_comma || ps.block_init || !break_comma || s_com != e_com) {		dump_line();		ps.want_blank = false;	    }	    ++line_no;		/* keep track of input line number */	    break;	case lparen:		/* got a '(' or '[' */	    ++ps.p_l_follow;	/* count parens to make Healy happy */	    if (ps.want_blank && *token != '[' &&		    (ps.last_token != ident || proc_calls_space	      || (ps.its_a_keyword && (!ps.sizeof_keyword || Bill_Shannon))))		*e_code++ = ' ';	    if (ps.in_decl && !ps.block_init)		if (troff && !ps.dumped_decl_indent && !is_procname && ps.last_token == decl) {		    ps.dumped_decl_indent = 1;		    sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token);		    e_code += strlen(e_code);		}		else {		    while ((e_code - s_code) < dec_ind) {			CHECK_SIZE_CODE;			*e_code++ = ' ';		    }		    *e_code++ = token[0];		}	    else		*e_code++ = token[0];	    ps.paren_indents[ps.p_l_follow - 1] = e_code - s_code;	    if (sp_sw && ps.p_l_follow == 1 && extra_expression_indent		    && ps.paren_indents[0] < 2 * ps.ind_size)		ps.paren_indents[0] = 2 * ps.ind_size;	    ps.want_blank = false;	    if (ps.in_or_st && *token == '(' && ps.tos <= 2) {		/*		 * this is a kluge to make sure that declarations will be		 * aligned right if proc decl has an explicit type on it, i.e.		 * "int a(x) {..."		 */		parse(semicolon);	/* I said this was a kluge... */		ps.in_or_st = false;	/* turn off flag for structure decl or					 * initialization */	    }	    if (ps.sizeof_keyword)		ps.sizeof_mask |= 1 << ps.p_l_follow;	    break;	case rparen:		/* got a ')' or ']' */	    rparen_count--;	    if (ps.cast_mask & (1 << ps.p_l_follow) & ~ps.sizeof_mask) {		ps.last_u_d = true;		ps.cast_mask &= (1 << ps.p_l_follow) - 1;	    }	    ps.sizeof_mask &= (1 << ps.p_l_follow) - 1;	    if (--ps.p_l_follow < 0) {		ps.p_l_follow = 0;		diag(0, "Extra %c", *token);	    }	    if (e_code == s_code)	/* if the paren starts the line */		ps.paren_level = ps.p_l_follow;	/* then indent it */	    *e_code++ = token[0];	    ps.want_blank = true;	    if (sp_sw && (ps.p_l_follow == 0)) {	/* check for end of if							 * (...), or some such */		sp_sw = false;		force_nl = true;/* must force newline after if */		ps.last_u_d = true;	/* inform lexi that a following					 * operator is unary */		ps.in_stmt = false;	/* dont use stmt continuation					 * indentation */		parse(hd_type);	/* let parser worry about if, or whatever */	    }	    ps.search_brace = btype_2;	/* this should insure that constructs					 * such as main(){...} and int[]{...}					 * have their braces put in the right					 * place */	    break;	case unary_op:		/* this could be any unary operation */	    if (ps.want_blank)		*e_code++ = ' ';	    if (troff && !ps.dumped_decl_indent && ps.in_decl && !is_procname) {		sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token);		ps.dumped_decl_indent = 1;		e_code += strlen(e_code);	    }	    else {		char       *res = token;		if (ps.in_decl && !ps.block_init) {	/* if this is a unary op							 * in a declaration, we							 * should indent this							 * token */		    for (i = 0; token[i]; ++i);	/* find length of token */		    while ((e_code - s_code) < (dec_ind - i)) {			CHECK_SIZE_CODE;			*e_code++ = ' ';	/* pad it */		    }		}		if (troff && token[0] == '-' && token[1] == '>')		    res = "\\(->";		for (t_ptr = res; *t_ptr; ++t_ptr) {		    CHECK_SIZE_CODE;		    *e_code++ = *t_ptr;		}	    }	    ps.want_blank = false;	    break;	case binary_op:	/* any binary operation */	    if (ps.want_blank)		*e_code++ = ' ';	    {		char       *res = token;		if (troff)		    switch (token[0]) {		    case '<':			if (token[1] == '=')			    res = "\\(<=";			break;		    case '>':			if (token[1] == '=')			    res = "\\(>=";			break;		    case '!':			if (token[1] == '=')			    res = "\\(!=";			break;		    case '|':			if (token[1] == '|')			    res = "\\(br\\(br";			else if (token[1] == 0)			    res = "\\(br";			break;		    }		for (t_ptr = res; *t_ptr; ++t_ptr) {		    CHECK_SIZE_CODE;		    *e_code++ = *t_ptr;	/* move the operator */		}	    }	    ps.want_blank = true;	    break;	case postop:		/* got a trailing ++ or -- */	    *e_code++ = token[0];	    *e_code++ = token[1];	    ps.want_blank = true;	    break;	case question:		/* got a ? */	    squest++;		/* this will be used when a later colon				 * appears so we can distinguish the				 * <c>?<n>:<n> construct */	    if (ps.want_blank)		*e_code++ = ' ';	    *e_code++ = '?';	    ps.want_blank = true;	    break;	case casestmt:		/* got word 'case' or 'default' */	    scase = true;	/* so we can process the later colon properly */	    goto copy_id;	case colon:		/* got a ':' */	    if (squest > 0) {	/* it is part of the <c>?<n>: <n> construct */		--squest;		if (ps.want_blank)		    *e_code++ = ' ';		*e_code++ = ':';		ps.want_blank = true;		break;	    }	    if (ps.in_decl) {		*e_code++ = ':';		ps.want_blank = false;		break;	    }	    ps.in_stmt = false;	/* seeing a label does not imply we are in a				 * stmt */	    for (t_ptr = s_code; *t_ptr; ++t_ptr)		*e_lab++ = *t_ptr;	/* turn everything so far into a label */	    e_code = s_code;	    *e_lab++ = ':';	    *e_lab++ = ' ';	    *e_lab = '\0';	    force_nl = ps.pcase = scase;	/* ps.pcase will be used by						 * dump_line to decide how to						 * indent the label. force_nl						 * will force a case n: to be						 * on a line by itself */	    scase = false;	    ps.want_blank = false;	    break;	case semicolon:	/* got a ';' */	    ps.in_or_st = false;/* we are not in an initialization or				 * structure declaration */	    scase = false;	/* these will only need resetting in a error */	    squest = 0;	    if (ps.last_token == rparen && rparen_count == 0)		ps.in_parameter_declaration = 0;	    ps.cast_mask = 0;	    ps.sizeof_mask = 0;	    ps.block_init = 0;	    ps.block_init_level = 0;	    ps.just_saw_decl--;	    if (ps.in_decl && s_code == e_code && !ps.block_init)		while ((e_code - s_code) < (dec_ind - 1)) {		    CHECK_SIZE_CODE;		    *e_code++ = ' ';		}	    ps.in_decl = (ps.dec_nest > 0);	/* if we were in a first level						 * structure declaration, we						 * arent any more */	    if ((!sp_sw || hd_type != forstmt) && ps.p_l_follow > 0) {		/*		 * This should be true iff there were unbalanced parens in the		 * stmt.  It is a bit complicated, because the semicolon might		 * be in a for stmt		 */		diag(1, "Unbalanced parens");		ps.p_l_follow = 0;		if (sp_sw) {	/* this is a check for a if, while, etc. with				 * unbalanced parens */		    sp_sw = false;		    parse(hd_type);	/* dont lose the if, or whatever */		}	    }	    *e_code++ = ';';	    ps.want_blank = true;	    ps.in_stmt = (ps.p_l_follow > 0);	/* we are no longer in the						 * middle of a stmt */	    if (!sp_sw) {	/* if not if for (;;) */		parse(semicolon);	/* let parser know about end of stmt */		force_nl = true;/* force newline after a end of stmt */	    }	    break;	case lbrace:		/* got a '{' */	    ps.in_stmt = false;	/* dont indent the {} */	    if (!ps.block_init)		force_nl = true;/* force other stuff on same line as '{' onto				 * new line */	    else if (ps.block_init_level <= 0)		ps.block_init_level = 1;	    else		ps.block_init_level++;	    if (s_code != e_code && !ps.block_init) {		if (!btype_2) {		    dump_line();		    ps.want_blank = false;		}		else if (ps.in_parameter_declaration && !ps.in_or_st) {		    ps.i_l_follow = 0;		    dump_line();		    ps.want_blank = false;		}	    }	    if (ps.in_parameter_declaration)		prefix_blankline_requested = 0;	    if (ps.p_l_follow > 0) {	/* check for preceeding unbalanced					 * parens */		diag(1, "Unbalanced parens");		ps.p_l_follow = 0;		if (sp_sw) {	/* check for unclosed if, for, etc. */		    sp_sw = false;		    parse(hd_type);		    ps.ind_level = ps.i_l_follow;		}	    }	    if (s_code == e_code)		ps.ind_stmt = false;	/* dont put extra indentation on line					 * with '{' */	    if (ps.in_decl && ps.in_or_st) {	/* this is either a structure						 * declaration or an init */		di_stack[ps.dec_nest++] = dec_ind;		/* ?		dec_ind = 0; */	    }	    else {		ps.decl_on_line = false;	/* we cant be in the middle of						 * a declaration, so dont do						 * special indentation of						 * comments */		if (blanklines_after_declarations_at_proctop			&& ps.in_parameter_declaration)		    postfix_blankline_requested = 1;		ps.in_parameter_declaration = 0;	    }	    dec_ind = 0;	    parse(lbrace);	/* let parser know about this */	    if (ps.want_blank)	/* put a blank before '{' if '{' is not at				 * start of line */		*e_code++ = ' ';	    ps.want_blank = false;	    *e_code++ = '{';	    ps.just_saw_decl = 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -