📄 indent.c
字号:
{ /* check for unclosed if, for, else. */ diag(1, "Unbalanced parens"); ps.p_l_follow = 0; sp_sw = false; } ps.just_saw_decl = 0; ps.block_init_level--; if (s_code != e_code && !ps.block_init) { /* '}' must be first on line */ if (verbose) diag(0, "Line broken"); dump_line(); } *e_code++ = '}'; ps.want_blank = true; ps.in_stmt = ps.ind_stmt = false; if (ps.dec_nest > 0) { /* we are in multi-level structure declaration */ dec_ind = di_stack[--ps.dec_nest]; if (ps.dec_nest == 0 && !ps.in_par_decl) ps.just_saw_decl = 2; ps.in_decl = true; } prefix_blankline_requested = 0; parse(rbrace); /* let parser know about this */ ps.search_brace = cuddle_else && ps.p_stack[ps.tos] == ifhead && ps.il[ps.tos] >= ps.ind_level; if (ps.tos <= 1 && bl_a_procs && ps.dec_nest <= 0) postfix_blankline_requested = 1; break; case swstmt: /* got keyword "switch" */ sp_sw = true; hd_type = swstmt; /* keep this for when we have seen the expression */ goto copy_id; /* go move the token into buffer */ case sp_paren: /* token is if, while, for */ sp_sw = true; /* the interesting stuff is done after the expression is scanned */ hd_type = (*token == 'i' ? ifstmt : (*token == 'w' ? whilestmt : forstmt)); /* remember the type of header for later use by parser */ goto copy_id; /* copy the token into line */ case sp_nparen: /* got else, do */ ps.in_stmt = false; if (*token == 'e') { if (e_code != s_code && (!cuddle_else || e_code[-1] != '}')) { if (verbose) diag(0, "Line broken"); dump_line(); /* make sure this starts a line */ ps.want_blank = false; } force_nl = true; /* also, following stuff must go onto new line */ last_else = 1; parse(elselit); } else { if (e_code != s_code) { /* make sure this starts a line */ if (verbose) diag(0, "Line broken"); dump_line(); ps.want_blank = false; } force_nl = true; /* also, following stuff must go onto new line */ last_else = 0; parse(dolit); } goto copy_id; /* move the token into line */ case decl: /* we have a declaration type (int, register, etc.) */ parse(decl); /* let parser worry about indentation */ if (ps.last_token == rparen && ps.tos <= 1) { ps.in_par_decl = 1; if (s_code != e_code) { dump_line(); ps.want_blank = 0; } } if (ps.in_par_decl && ps.indent_parameters && ps.dec_nest == 0) { ps.ind_level = ps.i_l_follow = 1; ps.ind_stmt = 0; } ps.in_or_st = true; /* this might be a structure or initialization declaration */ ps.in_decl = ps.decl_on_line = true; if ( /* !ps.in_or_st && */ ps.dec_nest <= 0) ps.just_saw_decl = 2; prefix_blankline_requested = 0; for (i = 0; token[i++];); /* get length of token */ /* dec_ind = e_code - s_code + (ps.decl_indent>i ? ps.decl_indent : i); */ dec_ind = ps.decl_indent > 0 ? ps.decl_indent : i; goto copy_id; case ident: /* got an identifier or constant */ if (ps.in_decl) { /* if we are in a declaration, we must indent identifier */ if (ps.want_blank) *e_code++ = ' '; ps.want_blank = false; if (is_procname == 0 || !proc_str_line) { if (!ps.block_init) if (troff && !ps.dumped_decl_indent) { sprintf(e_code, "\n.De %dp+\200p\n", dec_ind * 7); ps.dumped_decl_indent = 1; e_code += strlen(e_code); } else while ((e_code - s_code) < dec_ind) { if (e_code >= l_code) { register nsize = l_code - s_code + 400; codebuf = (char *) realloc(codebuf, nsize); e_code = codebuf + (e_code - s_code) + 1; l_code = codebuf + nsize - 5; s_code = codebuf + 1; } *e_code++ = ' '; } } else { if (dec_ind && s_code != e_code) dump_line(); dec_ind = 0; ps.want_blank = false; } } else if (sp_sw && ps.p_l_follow == 0) { sp_sw = false; force_nl = true; ps.last_u_d = true; ps.in_stmt = false; parse(hd_type); } copy_id: if (ps.want_blank) *e_code++ = ' '; if (troff && ps.its_a_keyword) { e_code = chfont(&bodyf, &keywordf, e_code); for (t_ptr = token; *t_ptr; ++t_ptr) { if (e_code >= l_code) { register nsize = l_code - s_code + 400; codebuf = (char *) realloc(codebuf, nsize); e_code = codebuf + (e_code - s_code) + 1; l_code = codebuf + nsize - 5; s_code = codebuf + 1; } *e_code++ = keywordf.allcaps && islower(*t_ptr) ? toupper(*t_ptr) : *t_ptr; } e_code = chfont(&keywordf, &bodyf, e_code); } else for (t_ptr = token; *t_ptr; ++t_ptr) { if (e_code >= l_code) { register nsize = l_code - s_code + 400; codebuf = (char *) realloc(codebuf, nsize); e_code = codebuf + (e_code - s_code) + 1; l_code = codebuf + nsize - 5; s_code = codebuf + 1; } *e_code++ = *t_ptr; } ps.want_blank = true; break; case period: /* treat a period kind of like a binary operation */ *e_code++ = '.'; /* move the period into line */ ps.want_blank = false; /* dont put a blank after a period */ break; case comma: ps.want_blank = (s_code != e_code); /* only put blank after comma if comma does not start the line */ if (ps.in_decl && is_procname == 0 && !ps.block_init) while ((e_code - s_code) < (dec_ind - 1)) { if (e_code >= l_code) { register nsize = l_code - s_code + 400; codebuf = (char *) realloc(codebuf, nsize); e_code = codebuf + (e_code - s_code) + 1; l_code = codebuf + nsize - 5; s_code = codebuf + 1; } *e_code++ = ' '; } *e_code++ = ','; if (ps.p_l_follow == 0) { if (ps.block_init_level <= 0) ps.block_init = 0; if (break_comma && !ps.leave_comma) force_nl = true; } break; case preesc: /* got the character '#' */ if ((s_com != e_com) || (s_lab != e_lab) || (s_code != e_code)) dump_line(); *e_lab++ = '#'; /* move whole line to 'label' buffer */ { int in_comment = 0; int com_start = 0; char quote = 0; int com_end = 0; while (*buf_ptr != '\n' || in_comment) { if (e_lab >= l_lab) { register nsize = l_lab - s_lab + 400; labbuf = (char *) realloc(labbuf, nsize); e_lab = labbuf + (e_lab - s_lab) + 1; l_lab = labbuf + nsize - 5; s_lab = labbuf + 1; } *e_lab = *buf_ptr++; if (buf_ptr >= buf_end) fill_buffer(); switch (*e_lab++) { case BACKSLASH: if (troff) *e_lab++ = BACKSLASH; if (!in_comment) { *e_lab++ = *buf_ptr++; if (buf_ptr >= buf_end) fill_buffer(); } break; case '/': if (*buf_ptr == '*' && !in_comment && !quote) { in_comment = 1; *e_lab++ = *buf_ptr++; com_start = e_lab - s_lab - 2; } break; case '"': if (quote == '"') quote = 0; break; case '\'': if (quote == '\'') quote = 0; break; case '*': if (*buf_ptr == '/' && in_comment) { in_comment = 0; *e_lab++ = *buf_ptr++; com_end = e_lab - s_lab; } break; } } while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t')) e_lab--; if (e_lab - s_lab == com_end && bp_save == 0) { /* comment on preprocessor line */ if (sc_end == 0) /* if this is the first comment, we must set up the buffer */ sc_end = &(save_com[0]); else { *sc_end++ = '\n'; /* add newline between comments */ *sc_end++ = ' '; --line_no; } memcpy(sc_end, s_lab + com_start, com_end - com_start); sc_end += com_end - com_start; if (sc_end >= &save_com[sc_size]) abort(); e_lab = s_lab + com_start; while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t')) e_lab--; bp_save = buf_ptr; /* save current input buffer */ be_save = buf_end; buf_ptr = save_com; /* fix so that subsequent calls to lexi will take tokens out of save_com */ *sc_end++ = ' '; /* add trailing blank, just in case */ buf_end = sc_end; sc_end = 0; } *e_lab = '\0'; /* null terminate line */ ps.pcase = false; } if (strncmp(s_lab, "#if", 3) == 0) { if (bl_around) { register c; prefix_blankline_requested++; while ((c = getc(input)) == '\n'); ungetc(c, input); } if (ifdef_level < sizeof state_stack / sizeof state_stack[0]) { match_state[ifdef_level].tos = -1; state_stack[ifdef_level++] = ps; } else diag(1, "#if stack overflow"); } else if (strncmp(s_lab, "#else", 5) == 0) if (ifdef_level <= 0) diag(1, "Unmatched #else"); else { match_state[ifdef_level - 1] = ps; ps = state_stack[ifdef_level - 1]; } else if (strncmp(s_lab, "#endif", 6) == 0) { if (ifdef_level <= 0) diag(1, "Unmatched #endif"); else { ifdef_level--;#ifdef undef /* This match needs to be more intelligent before the message is useful */ if (match_state[ifdef_level].tos >= 0 && bcmp(&ps, &match_state[ifdef_level], sizeof ps)) diag(0, "Syntactically inconsistant #ifdef alternatives.");#endif } if (bl_around) { postfix_blankline_requested++; n_real_blanklines = 0; } } break; /* subsequent processing of the newline character will cause the line to be printed */ case comment: /* we have gotten a / * this is a biggie */ if (flushed_nl) { /* we should force a broken line here */ flushed_nl = false; dump_line(); ps.want_blank = false; /* dont insert blank at line start */ force_nl = false; } pr_comment(); break; } /* end of big switch stmt */ *e_code = '\0'; /* make sure code section is null terminated */ if (type_code != comment && type_code != newline && type_code != preesc) ps.last_token = type_code; } /* end of main while (1) loop */}/* * copy input file to backup file if in_name is /blah/blah/blah/file, then * backup file will be ".Bfile" then make the backup file the input and * original input file the output */void bakcopy(){ int n, bakchn; char buff[8 * 1024]; register char *p; /* construct file name .Bfile */ for (p = in_name; *p; p++); /* skip to end of string */ while (p > in_name && *p != '/') /* find last '/' */ p--; if (*p == '/') p++; sprintf(bakfile, "%s.BAK", p); if (strlen(p) >= NAME_SIZE) *bakfile ^= 040; /* toggle char */ /* copy in_name to backup file */ bakchn = creat(bakfile, 0600); if (bakchn < 0) { fprintf(stderr, "indent: can't create backup file \"%s\"\n", bakfile); exit(1); } while (n = read(fileno(input), buff, sizeof buff)) if (write(bakchn, buff, n) != n) { fprintf(stderr, "indent: error writing backup file \"%s\"\n", bakfile); exit(1); } if (n < 0) { fprintf(stderr, "indent: error reading input file \"%s\"\n", in_name); exit(1); } close(bakchn); fclose(input); /* re-open backup file as the input file */ input = fopen(bakfile, "r"); if (input == 0) { fprintf(stderr, "indent: can't re-open backup file\n"); exit(1); } /* now the original input file will be the output */ output = fopen(in_name, "w"); if (output == 0) { fprintf(stderr, "indent: can't create %s\n", in_name); unlink(bakfile); exit(1); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -