📄 indent.c
字号:
if (!in_comment && !in_cplus_comment) { *e_lab++ = *buf_ptr++; if (buf_ptr >= buf_end) fill_buffer (); } break; case '/': if ((*buf_ptr == '*' || *buf_ptr == '/') && !in_comment && !in_cplus_comment && !quote) { if (*buf_ptr == '/') in_cplus_comment = 1; else in_comment = 1; *e_lab++ = *buf_ptr++; com_start = e_lab - s_lab - 2; } break; case '"': case '\'': if (!quote) quote = e_lab[-1]; else if (e_lab[-1] == 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] == TAB)) e_lab--; if (in_cplus_comment) { in_cplus_comment = 0; *e_lab++ = *buf_ptr++; com_end = e_lab - s_lab; } if (e_lab - s_lab == com_end && bp_save == 0) { /* comment on preprocessor line */ if (save_com.end != save_com.ptr) { need_chars (save_com, 2); *save_com.end++ = EOL; /* add newline between comments */ *save_com.end++ = ' '; --line_no; } need_chars (save_com, com_end - com_start); strncpy (save_com.end, s_lab + com_start, com_end - com_start); save_com.end += com_end - com_start; e_lab = s_lab + com_start; while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == TAB)) e_lab--; bp_save = buf_ptr; /* save current input buffer */ be_save = buf_end; buf_ptr = save_com.ptr; /* fix so that subsequent calls to lexi will take tokens out of save_com */ need_chars (save_com, 1); *save_com.end++ = ' '; /* add trailing blank, just in case */ buf_end = save_com.end; save_com.end = save_com.ptr; /* make save_com empty */ } *e_lab = '\0'; /* null terminate line */ parser_state_tos->pcase = false; } if (strncmp (s_lab + 1, "if", 2) == 0) { if (blanklines_around_conditional_compilation) { register c; prefix_blankline_requested++; while ((c = *in_prog_pos++) == EOL); in_prog_pos--; } { /* Push a copy of the parser_state onto the stack. All manipulations will use the copy at the top of stack, and then we can return to the previous state by popping the stack. */ struct parser_state *new; new = (struct parser_state *) xmalloc (sizeof (struct parser_state)); memcpy (new, parser_state_tos, sizeof (struct parser_state)); /* We need to copy the dynamically allocated arrays in the struct parser_state too. */ new->p_stack = (enum codes *) xmalloc (parser_state_tos->p_stack_size * sizeof (enum codes)); memcpy (new->p_stack, parser_state_tos->p_stack, parser_state_tos->p_stack_size * sizeof (enum codes)); new->il = (int *) xmalloc (parser_state_tos->p_stack_size * sizeof (int)); memcpy (new->il, parser_state_tos->il, parser_state_tos->p_stack_size * sizeof (int)); new->cstk = (int *) xmalloc (parser_state_tos->p_stack_size * sizeof (int)); memcpy (new->cstk, parser_state_tos->cstk, parser_state_tos->p_stack_size * sizeof (int)); new->paren_indents = (short *) xmalloc (parser_state_tos->paren_indents_size * sizeof (short)); memcpy (new->paren_indents, parser_state_tos->paren_indents, parser_state_tos->paren_indents_size * sizeof (short)); new->next = parser_state_tos; parser_state_tos = new; } } else if (strncmp (s_lab + 1, "else", 4) == 0) { /* When we get #else, we want to restore the parser state to what it was before the matching #if, so that things get lined up with the code before the #if. However, we do not want to pop the stack; we just want to copy the second to top elt of the stack because when we encounter the #endif, it will pop the stack. */ else_or_endif = true; if (parser_state_tos->next) { /* First save the addresses of the arrays for the top of stack. */ enum codes *tos_p_stack = parser_state_tos->p_stack; int *tos_il = parser_state_tos->il; int *tos_cstk = parser_state_tos->cstk; short *tos_paren_indents = parser_state_tos->paren_indents; struct parser_state *second = parser_state_tos->next; memcpy (parser_state_tos, second, sizeof (struct parser_state)); parser_state_tos->next = second; /* Now copy the arrays from the second to top of stack to the top of stack. */ /* Since the p_stack, etc. arrays only grow, never shrink, we know that they will be big enough to fit the array from the second to top of stack. */ parser_state_tos->p_stack = tos_p_stack; memcpy (parser_state_tos->p_stack, parser_state_tos->next->p_stack, parser_state_tos->p_stack_size * sizeof (enum codes)); parser_state_tos->il = tos_il; memcpy (parser_state_tos->il, parser_state_tos->next->il, parser_state_tos->p_stack_size * sizeof (int)); parser_state_tos->cstk = tos_cstk; memcpy (parser_state_tos->cstk, parser_state_tos->next->cstk, parser_state_tos->p_stack_size * sizeof (int)); parser_state_tos->paren_indents = tos_paren_indents; memcpy (parser_state_tos->paren_indents, parser_state_tos->next->paren_indents, parser_state_tos->paren_indents_size * sizeof (short)); } else diag (1, "Unmatched #else", 0, 0); } else if (strncmp (s_lab + 1, "endif", 5) == 0) { else_or_endif = true; /* We want to remove the second to top elt on the stack, which was put there by #if and was used to restore the stack at the #else (if there was one). We want to leave the top of stack unmolested so that the state which we have been using is unchanged. */ if (parser_state_tos->next) { struct parser_state *second = parser_state_tos->next; parser_state_tos->next = second->next; free (second->p_stack); free (second->il); free (second->cstk); free (second->paren_indents); free (second); } else diag (1, "Unmatched #endif", 0, 0); if (blanklines_around_conditional_compilation) { postfix_blankline_requested++; n_real_blanklines = 0; } } /* Normally, subsequent processing of the newline character causes the line to be printed. The following clause handles a special case (comma-separated declarations separated by the preprocessor lines) where this doesn't happen. */ if (parser_state_tos->last_token == comma && parser_state_tos->p_l_follow <= 0 && leave_comma && !parser_state_tos->block_init && break_comma && s_com == e_com) { dump_line (); parser_state_tos->want_blank = false; } break; /* A C or C++ comment. */ case comment: case cplus_comment: if (flushed_nl) { flushed_nl = false; dump_line (); parser_state_tos->want_blank = false; force_nl = false; } print_comment (); break; } /* end of big switch stmt */ *e_code = '\0'; /* make sure code section is null terminated */ if (type_code != comment && type_code != cplus_comment && type_code != newline && type_code != preesc && type_code != form_feed) parser_state_tos->last_token = type_code; } /* end of main while (1) loop */}char *set_profile ();void set_defaults ();int set_option ();/* Points to current input file */char *in_name = 0;/* Points to the name of the output file */char *out_name = 0;/* How many input files were specified */int input_files;/* Names of all input files */char **in_file_names;/* Initial number of input filenames to allocate. */int max_input_files = 128;#ifdef DEBUGint debug;#endifmain (argc, argv) int argc; char **argv;{ register int i; struct file_buffer *current_input; char *profile_pathname = 0; int using_stdin = false;#ifdef DEBUG if (debug) debug_init ();#endif init_parser (); initialize_backups (); output = 0; input_files = 0; in_file_names = (char **) xmalloc (max_input_files * sizeof (char *)); set_defaults (); for (i = 1; i < argc; ++i) if (strcmp (argv[i], "-npro") == 0 || strcmp (argv[i], "--ignore-profile") == 0 || strcmp (argv[i], "+ignore-profile") == 0) break; if (i >= argc) profile_pathname = set_profile (); for (i = 1; i < argc; ++i) { if (argv[i][0] != '-' && argv[i][0] != '+') /* Filename */ { if (expect_output_file == true) /* Last arg was "-o" */ { if (out_name != 0) { fprintf (stderr, "indent: only one output file (2nd was %s)\n", argv[i]); exit (1); } if (input_files > 1) { fprintf (stderr, "indent: only one input file when output file is specified\n"); exit (1); } out_name = argv[i]; expect_output_file = false; continue; } else { if (using_stdin) { fprintf (stderr, "indent: can't have filenames when specifying standard input\n"); exit (1); } input_files++; if (input_files > 1) { if (out_name != 0) { fprintf (stderr, "indent: only one input file when output file is specified\n"); exit (1); } if (use_stdout != 0) { fprintf (stderr, "indent: only one input file when stdout is used\n"); exit (1); } if (input_files > max_input_files) { max_input_files = 2 * max_input_files; in_file_names = (char **) xrealloc ((char *) in_file_names, (max_input_files * sizeof (char *))); } } in_file_names[input_files - 1] = argv[i]; } } else { /* '-' as filename means stdin. */ if (argv[i][0] == '-' && argv[i][1] == '\0') { if (input_files > 0) { fprintf (stderr, "indent: can't have filenames when specifying standard input\n"); exit (1); } using_stdin = true; } else i += set_option (argv[i], (i < argc ? argv[i + 1] : 0), 1); } } if (verbose && profile_pathname) fprintf (stderr, "Read profile %s\n", profile_pathname); if (input_files > 1) { /* When multiple input files are specified, make a backup copy and then output the indented code into the same filename. */ for (i = 0; input_files; i++, input_files--) { current_input = read_file (in_file_names[i]); in_name = out_name = in_file_names[i]; output = fopen (out_name, "w"); if (output == 0) { fprintf (stderr, "indent: can't create %s\n", out_name); exit (1); } make_backup (current_input); reset_parser (); indent (current_input); if (fclose (output) != 0) sys_error (out_name); } } else { /* One input stream -- specified file, or stdin */ if (input_files == 0 || using_stdin) { input_files = 1; in_file_names[0] = "Standard input"; current_input = read_stdin (); } else /* 1 input file */ { current_input = read_file (in_file_names[0]); if (!out_name && !use_stdout) { out_name = in_file_names[0]; make_backup (current_input); } } in_name = in_file_names[0]; /* Uset stdout if it was specified ("-st"), or neither input nor output file was specified, or we're doing troff. */ if (use_stdout || !out_name || troff) output = stdout; else { output = fopen (out_name, "w"); if (output == 0) { fprintf (stderr, "indent: can't create %s\n", out_name); exit (1); } } reset_parser (); indent (current_input); } exit (0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -