📄 grep.c
字号:
program_name = bslash + 1; else if (program_name == argv[0] && argv[0][0] && argv[0][1] == ':') /* "c:progname" */ program_name = argv[0] + 2; /* Collapse the letter-case, so `strcmp' could be used hence. */ for ( ; *p; p++) if (*p >= 'A' && *p <= 'Z') *p += 'a' - 'A'; /* Remove the .exe extension, if any. */ if ((p = strrchr (program_name, '.')) && strcmp (p, ".exe") == 0) *p = '\0'; }#endif keys = NULL; keycc = 0; with_filenames = 0; eolbyte = '\n'; filename_mask = ~0; max_count = TYPE_MAXIMUM (off_t); /* The value -1 means to use DEFAULT_CONTEXT. */ out_after = out_before = -1; /* Default before/after context: chaged by -C/-NUM options */ default_context = 0; /* Changed by -o option */ only_matching = 0; /* Internationalization. */#if defined(HAVE_SETLOCALE) setlocale (LC_ALL, "");#endif#if defined(ENABLE_NLS) bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE);#endif atexit (close_stdout); prepend_default_options (getenv ("GREP_OPTIONS"), &argc, &argv); while ((opt = get_nondigit_option (argc, argv, &default_context)) != -1) switch (opt) { case 'A': context_length_arg (optarg, &out_after); break; case 'B': context_length_arg (optarg, &out_before); break; case 'C': /* Set output match context, but let any explicit leading or trailing amount specified with -A or -B stand. */ context_length_arg (optarg, &default_context); break; case 'D': if (strcmp (optarg, "read") == 0) devices = READ_DEVICES; else if (strcmp (optarg, "skip") == 0) devices = SKIP_DEVICES; else error (2, 0, _("unknown devices method")); break; case 'E': setmatcher ("egrep"); break; case 'F': setmatcher ("fgrep"); break; case 'P': setmatcher ("perl"); break; case 'G': setmatcher ("grep"); break; case 'H': with_filenames = 1; break; case 'I': binary_files = WITHOUT_MATCH_BINARY_FILES; break; case 'U':#if defined(HAVE_DOS_FILE_CONTENTS) dos_use_file_type = DOS_BINARY;#endif break; case 'u':#if defined(HAVE_DOS_FILE_CONTENTS) dos_report_unix_offset = 1;#endif break; case 'V': show_version = 1; break; case 'X': setmatcher (optarg); break; case 'a': binary_files = TEXT_BINARY_FILES; break; case 'b': out_byte = 1; break; case 'c': count_matches = 1; break; case 'd': if (strcmp (optarg, "read") == 0) directories = READ_DIRECTORIES; else if (strcmp (optarg, "skip") == 0) directories = SKIP_DIRECTORIES; else if (strcmp (optarg, "recurse") == 0) directories = RECURSE_DIRECTORIES; else error (2, 0, _("unknown directories method")); break; case 'e': cc = strlen (optarg); keys = xrealloc (keys, keycc + cc + 1); strcpy (&keys[keycc], optarg); keycc += cc; keys[keycc++] = '\n'; break; case 'f': fp = strcmp (optarg, "-") != 0 ? fopen (optarg, "r") : stdin; if (!fp) error (2, errno, "%s", optarg); for (keyalloc = 1; keyalloc <= keycc + 1; keyalloc *= 2) ; keys = xrealloc (keys, keyalloc); oldcc = keycc; while (!feof (fp) && (cc = fread (keys + keycc, 1, keyalloc - 1 - keycc, fp)) > 0) { keycc += cc; if (keycc == keyalloc - 1) keys = xrealloc (keys, keyalloc *= 2); } if (fp != stdin) fclose(fp); /* Append final newline if file ended in non-newline. */ if (oldcc != keycc && keys[keycc - 1] != '\n') keys[keycc++] = '\n'; break; case 'h': no_filenames = 1; break; case 'i': case 'y': /* For old-timers . . . */ match_icase = 1; break; case 'L': /* Like -l, except list files that don't contain matches. Inspired by the same option in Hume's gre. */ list_files = -1; break; case 'l': list_files = 1; break; case 'm': { uintmax_t value; switch (xstrtoumax (optarg, 0, 10, &value, "")) { case LONGINT_OK: max_count = value; if (0 <= max_count && max_count == value) break; /* Fall through. */ case LONGINT_OVERFLOW: max_count = TYPE_MAXIMUM (off_t); break; default: error (2, 0, _("invalid max count")); } } break; case 'n': out_line = 1; break; case 'o': only_matching = 1; break; case 'q': exit_on_match = 1; break; case 'R': case 'r': directories = RECURSE_DIRECTORIES; break; case 's': suppress_errors = 1; break; case 'v': out_invert = 1; break; case 'w': match_words = 1; break; case 'x': match_lines = 1; break; case 'Z': filename_mask = 0; break; case 'z': eolbyte = '\0'; break; case BINARY_FILES_OPTION: if (strcmp (optarg, "binary") == 0) binary_files = BINARY_BINARY_FILES; else if (strcmp (optarg, "text") == 0) binary_files = TEXT_BINARY_FILES; else if (strcmp (optarg, "without-match") == 0) binary_files = WITHOUT_MATCH_BINARY_FILES; else error (2, 0, _("unknown binary-files type")); break; case COLOR_OPTION: if(optarg) { if(!strcasecmp(optarg, "always") || !strcasecmp(optarg, "yes") || !strcasecmp(optarg, "force")) color_option = 1; else if(!strcasecmp(optarg, "never") || !strcasecmp(optarg, "no") || !strcasecmp(optarg, "none")) color_option = 0; else if(!strcasecmp(optarg, "auto") || !strcasecmp(optarg, "tty") || !strcasecmp(optarg, "if-tty")) color_option = 2; else show_help = 1; } else color_option = 2; if(color_option == 2) { if(isatty(STDOUT_FILENO) && getenv("TERM") && strcmp(getenv("TERM"), "dumb")) color_option = 1; else color_option = 0; } break; case EXCLUDE_OPTION: if (!excluded_patterns) excluded_patterns = new_exclude (); add_exclude (excluded_patterns, optarg); break; case EXCLUDE_FROM_OPTION: if (!excluded_patterns) excluded_patterns = new_exclude (); if (add_exclude_file (add_exclude, excluded_patterns, optarg, '\n') != 0) { error (2, errno, "%s", optarg); } break; case INCLUDE_OPTION: if (!included_patterns) included_patterns = new_exclude (); add_exclude (included_patterns, optarg); break; case LINE_BUFFERED_OPTION: line_buffered = 1; break; case LABEL_OPTION: label = optarg; break; case 0: /* long options */ break; default: usage (2); break; } /* POSIX.2 says that -q overrides -l, which in turn overrides the other output options. */ if (exit_on_match) list_files = 0; if (exit_on_match | list_files) { count_matches = 0; done_on_match = 1; } out_quiet = count_matches | done_on_match; if (out_after < 0) out_after = default_context; if (out_before < 0) out_before = default_context; if (color_option) { char *userval = getenv ("GREP_COLOR"); if (userval != NULL && *userval != '\0') grep_color = userval; } if (! matcher) matcher = "grep"; if (show_version) { printf (_("%s (GNU grep) %s\n"), matcher, VERSION); printf ("\n"); printf (_("\Copyright 1988, 1992-1999, 2000, 2001 Free Software Foundation, Inc.\n")); printf (_("\This is free software; see the source for copying conditions. There is NO\n\warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n")); printf ("\n"); exit (0); } if (show_help) usage (0); if (keys) { if (keycc == 0) { /* No keys were specified (e.g. -f /dev/null). Match nothing. */ out_invert ^= 1; match_lines = match_words = 0; } else /* Strip trailing newline. */ --keycc; } else if (optind < argc) { keys = argv[optind++]; keycc = strlen (keys); } else usage (2); if (!install_matcher (matcher) && !install_matcher ("default")) abort (); (*compile)(keys, keycc); if ((argc - optind > 1 && !no_filenames) || with_filenames) out_file = 1;#ifdef SET_BINARY /* Output is set to binary mode because we shouldn't convert NL to CR-LF pairs, especially when grepping binary files. */ if (!isatty (1)) SET_BINARY (1);#endif if (max_count == 0) exit (1); if (optind < argc) { status = 1; do { char *file = argv[optind]; if ((included_patterns || excluded_patterns) && !isdir (file)) { if (included_patterns && ! excluded_filename (included_patterns, file, 0)) continue; if (excluded_patterns && excluded_filename (excluded_patterns, file, 0)) continue; } status &= grepfile (strcmp (file, "-") == 0 ? (char *) NULL : file, &stats_base); } while ( ++optind < argc); } else status = grepfile ((char *) NULL, &stats_base); /* We register via atexit() to test stdout. */ exit (errseen ? 2 : status);}/* vim:set shiftwidth=2: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -