void patlist_add_file(struct patlist **dst, const char *fn) { FILE *fd; char *line = NULL; size_t linelen = 0; size_t len; fd = fopen (fn, "r"); if (NULL == fd) return; while ((len = getline (&line, &linelen, fd)) != -1) { if (len < 1) /* Shouldn't really happen */ continue; /* Remove '\n' from pattern */ if ('\n' == line[len - 1]) { if (len == 1) /* only '\n' present */ continue; line[len - 1] = '\0'; } patlist_add (dst, line); } fclose (fd); }
int main (int argc, char *argv[]) { int i; FILE *f = stdin; char format = '\0'; int regex_file_specified = 0; setlocale (LC_TIME, "C"); determine_mode_from_name (argv[0]); while (1) { static struct option long_options[] = { {"help", 0, 0, 1000 + 'H'}, {"version", 0, 0, 1000 + 'V'}, {"verbose", 0, 0, 'v'}, {"list", 0, 0, 'l'}, {"filter", 0, 0, 1000 + 'f'}, {"grep", 0, 0, 'g'}, {"strip", 1, 0, 1000 + 'S'}, {"addprefix", 1, 0, 1000 + 'A'}, {"addoldprefix", 1, 0, 1000 + 'O'}, {"addnewprefix", 1, 0, 1000 + 'N'}, {"hunks", 1, 0, '#'}, {"lines", 1, 0, 1000 + ':'}, {"files", 1, 0, 'F'}, {"as-numbered-lines", 1, 0, 1000 + 'L'}, {"annotate", 0, 0, 1000 + 'a'}, {"format", 1, 0, 1000 + 'F'}, {"output-matching", 1, 0, 1000 + 'o'}, {"remove-timestamps", 0, 0, 1000 + 'r'}, {"with-filename", 0, 0, 'H'}, {"no-filename", 0, 0, 'h'}, {"empty-files-as-absent", 0, 0, 'E'}, {"number-files", 0, 0, 'N'}, {"clean", 0, 0, 1000 + 'c'}, {"strip-match", 1, 0, 'p'}, {"include", 1, 0, 'i'}, {"exclude", 1, 0, 'x'}, {"include-from-file", 1, 0, 'I'}, {"exclude-from-file", 1, 0, 'X'}, {"decompress", 0, 0, 'z'}, {"line-number", 0, 0, 'n'}, {"strip-match", 1, 0, 'p'}, {"status", 0, 0, 's'}, {"extended-regexp", 0, 0, 'E'}, {"empty-files-as-removed", 0, 0, 'E'}, {"file", 1, 0, 'f'}, {0, 0, 0, 0} }; char *end; int c = getopt_long (argc, argv, "vp:i:I:x:X:zns#:F:Ef:HhN", long_options, NULL); if (c == -1) break; switch (c) { case 'g': set_grep (); break; case 1000 + 'f': set_filter (); break; case 'l': set_list (); break; case 'E': if (mode == mode_grep) egrepping = REG_EXTENDED; else if (mode == mode_list) empty_files_as_absent = 1; else syntax (1); break; case 'f': if (mode == mode_grep) { regex_file_specified = 1; read_regex_file (optarg); } else syntax (1); break; case 1000 + 'V': printf("%s - patchutils version %s\n", progname, VERSION); exit(0); case 1000 + 'H': syntax (0); break; case 1000 + 'S': strip_components = strtoul (optarg, &end, 0); if (optarg == end) syntax (1); break; case 1000 + 'A': prefix_to_add = optarg; break; case 1000 + 'O': old_prefix_to_add = optarg; break; case 1000 + 'N': new_prefix_to_add = optarg; break; case 'p': ignore_components = strtoul (optarg, &end, 0); if (optarg == end) syntax (1); break; case 'x': patlist_add (&pat_exclude, optarg); break; case 'X': patlist_add_file (&pat_exclude, optarg); break; case 'i': patlist_add (&pat_include, optarg); break; case 'I': patlist_add_file (&pat_include, optarg); break; case 'z': unzip = 1; break; case 'n': numbering = 1; break; case 'N': number_files = 1; break; case 's': show_status = 1; break; case 'v': verbose++; if (numbering && verbose > 1) number_files = 1; break; case '#': if (hunks) syntax (1); if (*optarg == 'x') { hunks_exclude = 1; optarg = optarg + 1; } parse_range (&hunks, optarg); break; case 'H': if (mode == mode_list || mode == mode_grep) print_patchnames = 1; else syntax (1); break; case 'h': if (mode == mode_list || mode == mode_grep) print_patchnames = 0; else syntax (1); break; case 1000 + ':': if (lines) syntax (1); if (*optarg == 'x') { lines_exclude = 1; optarg = optarg + 1; } parse_range (&lines, optarg); break; case 'F': if (files) syntax (1); if (*optarg == 'x') { files_exclude = 1; optarg = optarg + 1; } parse_range (&files, optarg); break; case 1000 + 'L': if (!strcmp (optarg, "before")) number_lines = Before; else if (!strcmp (optarg, "after")) number_lines = After; else syntax (1); break; case 1000 + 'a': if (mode == mode_list) syntax (1); annotating = 1; break; case 1000 + 'F': if (!strcmp (optarg, "context") && !format) format = 'c'; else if (!strcmp (optarg, "unified") && !format) format = 'u'; else syntax (1); break; case 1000 + 'o': if (!strncmp (optarg, "hunk", 4)) output_matching = output_hunk; else if (!strncmp (optarg, "file", 4)) output_matching = output_file; else syntax (1); break; case 1000 + 'r': removing_timestamp = 1; break; case 1000 + 'c': clean_comments = 1; break; default: syntax(1); } } /* Preserve the old semantics of -p. */ if (mode != mode_filter && ignore_components && !strip_components && !pat_include && !pat_exclude) { fprintf (stderr, "-p given without -i or -x; guessing that you " "meant --strip instead.\n"); strip_components = ignore_components; ignore_components = 0; } if (mode != mode_grep && output_matching != output_none) error (EXIT_FAILURE, 0, "--output-matching only applies to " "grep mode"); if (numbering && !(mode == mode_list || (mode == mode_grep && output_matching == output_none))) error (EXIT_FAILURE, 0, "-n only applies to list mode"); if (mode != mode_filter && output_matching == output_none && number_lines != None) error (EXIT_FAILURE, 0, "--as-numbered-lines is " "inappropriate in this context"); if (mode == mode_filter && verbose && clean_comments) error (EXIT_FAILURE, 0, "can't use --verbose and " "--clean options simultaneously"); if (mode == mode_grep && !regex_file_specified) { int err; if (optind == argc) syntax (1); regex = xrealloc (regex, ++num_regex * sizeof (regex[0])); err = regcomp (®ex[num_regex - 1], argv[optind++], REG_NOSUB | egrepping); if (err) { char errstr[300]; regerror (err, ®ex[num_regex - 1], errstr, sizeof (errstr)); error (EXIT_FAILURE, 0, "%s", errstr); exit (1); } } if (number_lines != None || output_matching != output_none) { if (print_patchnames == 1) error (EXIT_FAILURE, 0, "-H is inappropriate in this context"); } else if (print_patchnames == -1) { if ((mode == mode_list || mode == mode_grep) && optind + 1 < argc) print_patchnames = 1; else print_patchnames = 0; } if (optind == argc) { f = convert_format (stdin, format); filterdiff (f, "(standard input)"); fclose (f); } else { for (i = optind; i < argc; i++) { if (unzip) { f = xopen_unzip (argv[i], "rb"); } else { f = xopen(argv[i], "rbm"); } f = convert_format (f, format); filterdiff (f, argv[i]); fclose (f); } } return 0; }