+ if (optind >= argc) {
+ fprintf(stderr, "A pattern must be specified.\n");
+ return 1;
+ }
+
+ pat = argv[optind++];
+ re = pcre_compile( pat, PCRE_CASELESS, &pat_error, &pat_error_offset, NULL );
+
+
+ if (re == NULL) {
+ fprintf(stderr, "Syntax error in PCRE pattern at offset: %d: %s\n",
+ pat_error_offset, pat_error );
+ return 1;
+ }
+
+
+ if (optind >= argc || (argc - optind == 1 && strcmp(argv[optind],"-") == 0)) {
+ gzfin = gzdopen( fileno(stdin), "rb" );
+ if (gzfin == NULL) {
+ fprintf(stderr, "Malformed file 'stdin'.\n");
+ return 1;
+ }
+
+ fastq_grep(gzfin, stdout, re);
+
+ gzclose(gzfin);
+ }
+ else {
+ for (; optind < argc; optind++) {
+ fin = fopen(argv[optind], "rb");
+ if (fin == NULL) {
+ fprintf(stderr, "No such file '%s'.\n", argv[optind]);
+ continue;
+ }
+
+ gzfin = gzdopen(fileno(fin), "rb");
+ if (gzfin == NULL) {
+ fprintf(stderr, "Malformed file '%s'.\n", argv[optind]);
+ continue;
+ }
+
+ fastq_grep(gzfin, stdout, re);
+
+ gzclose(gzfin);
+ }
+ }
+
+ pcre_free(re);