📄 fix-header.c
字号:
if (isalpha (c) || c == '_') { for (;;) { SSTRING_PUT (s, c); c = INF_GET (); if (c == EOF || !(isalnum (c) || c == '_')) break; } } MAKE_SSTRING_SPACE (s, 1); *s->ptr = 0; return c;}/* Returns 1 if the file is correctly protected against multiple inclusion, setting *ifndef_line to the line number of the initial #ifndef and setting *endif_line to the final #endif. Otherwise return 0. */intcheck_protection (ifndef_line, endif_line) int *ifndef_line, *endif_line;{ int c; int if_nesting = 1; /* Level of nesting of #if's */ char *protect_name = NULL; /* Identifier following initial #ifndef */ int define_seen = 0; /* Skip initial white space (including comments). */ for (;; lineno++) { c = inf_skip_spaces (' '); if (c == EOF) return 0; if (c != '\n') break; } if (c != '#') return 0; c = inf_scan_ident (&buf, inf_skip_spaces (' ')); if (SSTRING_LENGTH (&buf) == 0 || strcmp (buf.base, "ifndef") != 0) return 0; /* So far so good: We've seen an initial #ifndef. */ *ifndef_line = lineno; c = inf_scan_ident (&buf, inf_skip_spaces (c)); if (SSTRING_LENGTH (&buf) == 0 || c == EOF) return 0; protect_name = xstrdup (buf.base); INF_UNGET (c); c = inf_read_upto (&buf, '\n'); if (c == EOF) return 0; lineno++; for (;;) { c = inf_skip_spaces (' '); if (c == EOF) return 0; if (c == '\n') { lineno++; continue; } if (c != '#') goto skip_to_eol; c = inf_scan_ident (&buf, inf_skip_spaces (' ')); if (SSTRING_LENGTH (&buf) == 0) ; else if (!strcmp (buf.base, "ifndef") || !strcmp (buf.base, "ifdef") || !strcmp (buf.base, "if")) { if_nesting++; } else if (!strcmp (buf.base, "endif")) { if_nesting--; if (if_nesting == 0) break; } else if (!strcmp (buf.base, "else")) { if (if_nesting == 1) return 0; } else if (!strcmp (buf.base, "define")) { if (if_nesting != 1) goto skip_to_eol; c = inf_skip_spaces (c); c = inf_scan_ident (&buf, c); if (buf.base[0] > 0 && strcmp (buf.base, protect_name) == 0) define_seen = 1; } skip_to_eol: for (;;) { if (c == '\n' || c == EOF) break; c = INF_GET (); } if (c == EOF) return 0; lineno++; } if (!define_seen) return 0; *endif_line = lineno; /* Skip final white space (including comments). */ for (;;) { c = inf_skip_spaces (' '); if (c == EOF) break; if (c != '\n') return 0; } return 1;}intmain (argc, argv) int argc; char **argv;{ int inf_fd; struct stat sbuf; int c; int i, done; const char *cptr, **pptr; int ifndef_line; int endif_line; long to_read; long int inf_size; if (argv[0] && argv[0][0]) { register char *p; progname = 0; for (p = argv[0]; *p; p++) if (*p == '/') progname = p; progname = progname ? progname+1 : argv[0]; } if (argc < 4) { fprintf (stderr, "%s: Usage: foo.h infile.h outfile.h options\n", progname); exit (-1); } inc_filename = argv[1]; inc_filename_length = strlen (inc_filename);#ifdef FIXPROTO_IGNORE_LIST for (i = 0; files_to_ignore[i] != NULL; i++) { char *ignore_name = files_to_ignore[i]; int ignore_len = strlen (ignore_name); if (strncmp (inc_filename, ignore_name, ignore_len) == 0) { if (ignore_name[ignore_len-1] == '/' || inc_filename[ignore_len] == '\0') { if (verbose) fprintf (stderr, "%s: ignoring %s\n", progname, inc_filename); exit (0); } } }#endif if (strcmp (inc_filename, "sys/stat.h") == 0) special_file_handling = sys_stat_h; else if (strcmp (inc_filename, "errno.h") == 0) special_file_handling = errno_h, missing_errno = 1; else if (strcmp (inc_filename, "stdio.h") == 0) special_file_handling = stdio_h; include_entry = std_include_table; while (include_entry->name != NULL && strcmp (inc_filename, include_entry->name) != 0) include_entry++; required_functions_list = include_entry->required; /* Count and mark the prototypes required for this include file. */ for (cptr = required_functions_list; *cptr!= '\0'; ) { int name_len = strlen (cptr); struct fn_decl *fn = lookup_std_proto (cptr, name_len); required_unseen_count++; if (fn == NULL) fprintf (stderr, "Internal error: No prototype for %s\n", cptr); else SET_REQUIRED (fn); cptr += name_len + 1; } read_scan_file (argv[2], argc - 4, argv + 4); inf_fd = open (argv[2], O_RDONLY, 0666); if (inf_fd < 0) { fprintf (stderr, "%s: Cannot open '%s' for reading -", progname, argv[2]); perror (NULL); exit (-1); } if (fstat (inf_fd, &sbuf) < 0) { fprintf (stderr, "%s: Cannot get size of '%s' -", progname, argv[2]); perror (NULL); exit (-1); } inf_size = sbuf.st_size; inf_buffer = (char*) xmalloc (inf_size + 2); inf_buffer[inf_size] = '\n'; inf_buffer[inf_size + 1] = '\0'; inf_limit = inf_buffer + inf_size; inf_ptr = inf_buffer; to_read = inf_size; while (to_read > 0) { long i = read (inf_fd, inf_buffer + inf_size - to_read, to_read); if (i < 0) { fprintf (stderr, "%s: Failed to read '%s' -", progname, argv[2]); perror (NULL); exit (-1); } if (i == 0) { inf_size -= to_read; break; } to_read -= i; } close (inf_fd); /* If file doesn't end with '\n', add one. */ if (inf_limit > inf_buffer && inf_limit[-1] != '\n') inf_limit++; unlink (argv[3]); outf = fopen (argv[3], "w"); if (outf == NULL) { fprintf (stderr, "%s: Cannot open '%s' for writing -", progname, argv[3]); perror (NULL); exit (-1); } lineno = 1; if (check_protection (&ifndef_line, &endif_line)) { lbrac_line = ifndef_line+1; rbrac_line = endif_line; } else { lbrac_line = 1; rbrac_line = -1; } /* Reset input file. */ inf_ptr = inf_buffer; lineno = 1; for (;;) { if (lineno == lbrac_line) write_lbrac (); if (lineno == rbrac_line) write_rbrac (); for (;;) { struct fn_decl *fn; c = INF_GET (); if (c == EOF) break; if (isalpha (c) || c == '_') { c = inf_scan_ident (&buf, c); INF_UNGET (c); fputs (buf.base, outf); fn = lookup_std_proto (buf.base, strlen (buf.base)); /* We only want to edit the declaration matching the one seen by scan-decls, as there can be multiple declarations, selected by #ifdef __STDC__ or whatever. */ if (fn && fn->partial && fn->partial->line_seen == lineno) { c = inf_skip_spaces (' '); if (c == EOF) break; if (c == '(') { c = inf_skip_spaces (' '); if (c == ')') { fprintf (outf, " _PARAMS((%s))", fn->params); } else { putc ('(', outf); INF_UNGET (c); } } else fprintf (outf, " %c", c); } } else { putc (c, outf); if (c == '\n') break; } } if (c == EOF) break; lineno++; } if (rbrac_line < 0) write_rbrac (); fclose (outf); return 0;}/* Stub error functions. These replace cpperror.c, because we want to suppress error messages. */voidcpp_file_line_for_message (pfile, filename, line, column) cpp_reader *pfile; char *filename; int line, column;{ if (!verbose) return; if (column > 0) fprintf (stderr, "%s:%d:%d: ", filename, line, column); else fprintf (stderr, "%s:%d: ", filename, line);}voidcpp_print_containing_files (pfile) cpp_reader *pfile;{}/* IS_ERROR is 1 for error, 0 for warning */void cpp_message (pfile, is_error, msg, arg1, arg2, arg3) int is_error; cpp_reader *pfile; char *msg; char *arg1, *arg2, *arg3;{ if (is_error) pfile->errors++; if (!verbose) return; if (!is_error) fprintf (stderr, "warning: "); fprintf (stderr, msg, arg1, arg2, arg3); fprintf (stderr, "\n");}voidfatal (str, arg) char *str, *arg;{ fprintf (stderr, "%s: %s: ", progname, inc_filename); fprintf (stderr, str, arg); fprintf (stderr, "\n"); exit (FATAL_EXIT_CODE);}voidcpp_pfatal_with_name (pfile, name) cpp_reader *pfile; char *name;{ cpp_perror_with_name (pfile, name); exit (FATAL_EXIT_CODE);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -