⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pcregrep.c

📁 php-4.4.7学习linux时下载的源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
*/(void)pcre_config(PCRE_CONFIG_NEWLINE, &i);switch(i)  {  default:                 newline = (char *)"lf"; break;  case '\r':               newline = (char *)"cr"; break;  case ('\r' << 8) | '\n': newline = (char *)"crlf"; break;  case -1:                 newline = (char *)"any"; break;  }/* Process the options */for (i = 1; i < argc; i++)  {  option_item *op = NULL;  char *option_data = (char *)"";    /* default to keep compiler happy */  BOOL longop;  BOOL longopwasequals = FALSE;  if (argv[i][0] != '-') break;  /* If we hit an argument that is just "-", it may be a reference to STDIN,  but only if we have previously had -e or -f to define the patterns. */  if (argv[i][1] == 0)    {    if (pattern_filename != NULL || pattern_count > 0) break;      else exit(usage(2));    }  /* Handle a long name option, or -- to terminate the options */  if (argv[i][1] == '-')    {    char *arg = argv[i] + 2;    char *argequals = strchr(arg, '=');    if (*arg == 0)    /* -- terminates options */      {      i++;      break;                /* out of the options-handling loop */      }    longop = TRUE;    /* Some long options have data that follows after =, for example file=name.    Some options have variations in the long name spelling: specifically, we    allow "regexp" because GNU grep allows it, though I personally go along    with Jeffrey Friedl and Larry Wall in preferring "regex" without the "p".    These options are entered in the table as "regex(p)". No option is in both    these categories, fortunately. */    for (op = optionlist; op->one_char != 0; op++)      {      char *opbra = strchr(op->long_name, '(');      char *equals = strchr(op->long_name, '=');      if (opbra == NULL)     /* Not a (p) case */        {        if (equals == NULL)  /* Not thing=data case */          {          if (strcmp(arg, op->long_name) == 0) break;          }        else                 /* Special case xxx=data */          {          int oplen = equals - op->long_name;          int arglen = (argequals == NULL)? strlen(arg) : argequals - arg;          if (oplen == arglen && strncmp(arg, op->long_name, oplen) == 0)            {            option_data = arg + arglen;            if (*option_data == '=')              {              option_data++;              longopwasequals = TRUE;              }            break;            }          }        }      else                   /* Special case xxxx(p) */        {        char buff1[24];        char buff2[24];        int baselen = opbra - op->long_name;        sprintf(buff1, "%.*s", baselen, op->long_name);        sprintf(buff2, "%s%.*s", buff1, strlen(op->long_name) - baselen - 2,          opbra + 1);        if (strcmp(arg, buff1) == 0 || strcmp(arg, buff2) == 0)          break;        }      }    if (op->one_char == 0)      {      fprintf(stderr, "pcregrep: Unknown option %s\n", argv[i]);      exit(usage(2));      }    }  /* Jeffrey Friedl's debugging harness uses these additional options which  are not in the right form for putting in the option table because they use  only one hyphen, yet are more than one character long. By putting them  separately here, they will not get displayed as part of the help() output,  but I don't think Jeffrey will care about that. */#ifdef JFRIEDL_DEBUG  else if (strcmp(argv[i], "-pre") == 0) {          jfriedl_prefix = argv[++i];          continue;  } else if (strcmp(argv[i], "-post") == 0) {          jfriedl_postfix = argv[++i];          continue;  } else if (strcmp(argv[i], "-XT") == 0) {          sscanf(argv[++i], "%d", &jfriedl_XT);          continue;  } else if (strcmp(argv[i], "-XR") == 0) {          sscanf(argv[++i], "%d", &jfriedl_XR);          continue;  }#endif  /* One-char options; many that have no data may be in a single argument; we  continue till we hit the last one or one that needs data. */  else    {    char *s = argv[i] + 1;    longop = FALSE;    while (*s != 0)      {      for (op = optionlist; op->one_char != 0; op++)        { if (*s == op->one_char) break; }      if (op->one_char == 0)        {        fprintf(stderr, "pcregrep: Unknown option letter '%c' in \"%s\"\n",          *s, argv[i]);        exit(usage(2));        }      if (op->type != OP_NODATA || s[1] == 0)        {        option_data = s+1;        break;        }      pcre_options = handle_option(*s++, pcre_options);      }    }  /* At this point we should have op pointing to a matched option. If the type  is NO_DATA, it means that there is no data, and the option might set  something in the PCRE options. */  if (op->type == OP_NODATA)    {    pcre_options = handle_option(op->one_char, pcre_options);    continue;    }  /* If the option type is OP_OP_STRING or OP_OP_NUMBER, it's an option that  either has a value or defaults to something. It cannot have data in a  separate item. At the moment, the only such options are "colo(u)r" and  Jeffrey Friedl's special -S debugging option. */  if (*option_data == 0 &&      (op->type == OP_OP_STRING || op->type == OP_OP_NUMBER))    {    switch (op->one_char)      {      case N_COLOUR:      colour_option = (char *)"auto";      break;#ifdef JFRIEDL_DEBUG      case 'S':      S_arg = 0;      break;#endif      }    continue;    }  /* Otherwise, find the data string for the option. */  if (*option_data == 0)    {    if (i >= argc - 1 || longopwasequals)      {      fprintf(stderr, "pcregrep: Data missing after %s\n", argv[i]);      exit(usage(2));      }    option_data = argv[++i];    }  /* If the option type is OP_PATLIST, it's the -e option, which can be called  multiple times to create a list of patterns. */  if (op->type == OP_PATLIST)    {    if (cmd_pattern_count >= MAX_PATTERN_COUNT)      {      fprintf(stderr, "pcregrep: Too many command-line patterns (max %d)\n",        MAX_PATTERN_COUNT);      return 2;      }    patterns[cmd_pattern_count++] = option_data;    }  /* Otherwise, deal with single string or numeric data values. */  else if (op->type != OP_NUMBER && op->type != OP_OP_NUMBER)    {    *((char **)op->dataptr) = option_data;    }  else    {    char *endptr;    int n = strtoul(option_data, &endptr, 10);    if (*endptr != 0)      {      if (longop)        {        char *equals = strchr(op->long_name, '=');        int nlen = (equals == NULL)? (int)strlen(op->long_name) :          equals - op->long_name;        fprintf(stderr, "pcregrep: Malformed number \"%s\" after --%.*s\n",          option_data, nlen, op->long_name);        }      else        fprintf(stderr, "pcregrep: Malformed number \"%s\" after -%c\n",          option_data, op->one_char);      exit(usage(2));      }    *((int *)op->dataptr) = n;    }  }/* Options have been decoded. If -C was used, its value is used as a defaultfor -A and -B. */if (both_context > 0)  {  if (after_context == 0) after_context = both_context;  if (before_context == 0) before_context = both_context;  }/* If a locale has not been provided as an option, see if the LC_CTYPE orLC_ALL environment variable is set, and if so, use it. */if (locale == NULL)  {  locale = getenv("LC_ALL");  locale_from = "LCC_ALL";  }if (locale == NULL)  {  locale = getenv("LC_CTYPE");  locale_from = "LC_CTYPE";  }/* If a locale has been provided, set it, and generate the tables the PCREneeds. Otherwise, pcretables==NULL, which causes the use of default tables. */if (locale != NULL)  {  if (setlocale(LC_CTYPE, locale) == NULL)    {    fprintf(stderr, "pcregrep: Failed to set locale %s (obtained from %s)\n",      locale, locale_from);    return 2;    }  pcretables = pcre_maketables();  }/* Sort out colouring */if (colour_option != NULL && strcmp(colour_option, "never") != 0)  {  if (strcmp(colour_option, "always") == 0) do_colour = TRUE;  else if (strcmp(colour_option, "auto") == 0) do_colour = is_stdout_tty();  else    {    fprintf(stderr, "pcregrep: Unknown colour setting \"%s\"\n",      colour_option);    return 2;    }  if (do_colour)    {    char *cs = getenv("PCREGREP_COLOUR");    if (cs == NULL) cs = getenv("PCREGREP_COLOR");    if (cs != NULL) colour_string = cs;    }  }/* Interpret the newline type; the default settings are Unix-like. */if (strcmp(newline, "cr") == 0 || strcmp(newline, "CR") == 0)  {  pcre_options |= PCRE_NEWLINE_CR;  endlinetype = EL_CR;  }else if (strcmp(newline, "lf") == 0 || strcmp(newline, "LF") == 0)  {  pcre_options |= PCRE_NEWLINE_LF;  endlinetype = EL_LF;  }else if (strcmp(newline, "crlf") == 0 || strcmp(newline, "CRLF") == 0)  {  pcre_options |= PCRE_NEWLINE_CRLF;  endlinetype = EL_CRLF;  }else if (strcmp(newline, "any") == 0 || strcmp(newline, "ANY") == 0)  {  pcre_options |= PCRE_NEWLINE_ANY;  endlinetype = EL_ANY;  }else  {  fprintf(stderr, "pcregrep: Invalid newline specifier \"%s\"\n", newline);  return 2;  }/* Interpret the text values for -d and -D */if (dee_option != NULL)  {  if (strcmp(dee_option, "read") == 0) dee_action = dee_READ;  else if (strcmp(dee_option, "recurse") == 0) dee_action = dee_RECURSE;  else if (strcmp(dee_option, "skip") == 0) dee_action = dee_SKIP;  else    {    fprintf(stderr, "pcregrep: Invalid value \"%s\" for -d\n", dee_option);    return 2;    }  }if (DEE_option != NULL)  {  if (strcmp(DEE_option, "read") == 0) DEE_action = DEE_READ;  else if (strcmp(DEE_option, "skip") == 0) DEE_action = DEE_SKIP;  else    {    fprintf(stderr, "pcregrep: Invalid value \"%s\" for -D\n", DEE_option);    return 2;    }  }/* Check the values for Jeffrey Friedl's debugging options. */#ifdef JFRIEDL_DEBUGif (S_arg > 9)  {  fprintf(stderr, "pcregrep: bad value for -S option\n");  return 2;  }if (jfriedl_XT != 0 || jfriedl_XR != 0)  {  if (jfriedl_XT == 0) jfriedl_XT = 1;  if (jfriedl_XR == 0) jfriedl_XR = 1;  }#endif/* Get memory to store the pattern and hints lists. */pattern_list = (pcre **)malloc(MAX_PATTERN_COUNT * sizeof(pcre *));hints_list = (pcre_extra **)malloc(MAX_PATTERN_COUNT * sizeof(pcre_extra *));if (pattern_list == NULL || hints_list == NULL)  {  fprintf(stderr, "pcregrep: malloc failed\n");  return 2;  }/* If no patterns were provided by -e, and there is no file provided by -f,the first argument is the one and only pattern, and it must exist. */if (cmd_pattern_count == 0 && pattern_filename == NULL)  {  if (i >= argc) return usage(2);  patterns[cmd_pattern_count++] = argv[i++];  }/* Compile the patterns that were provided on the command line, either bymultiple uses of -e or as a single unkeyed pattern. */for (j = 0; j < cmd_pattern_count; j++)  {  if (!compile_pattern(patterns[j], pcre_options, NULL,       (j == 0 && cmd_pattern_count == 1)? 0 : j + 1))    return 2;  }/* Compile the regular expressions that are provided in a file. */if (pattern_filename != NULL)  {  int linenumber = 0;  FILE *f;  char *filename;  char buffer[MBUFTHIRD];  if (strcmp(pattern_filename, "-") == 0)    {    f = stdin;    filename = stdin_name;    }  else    {    f = fopen(pattern_filename, "r");    if (f == NULL)      {      fprintf(stderr, "pcregrep: Failed to open %s: %s\n", pattern_filename,        strerror(errno));      return 2;      }    filename = pattern_filename;    }  while (fgets(buffer, MBUFTHIRD, f) != NULL)    {    char *s = buffer + (int)strlen(buffer);    while (s > buffer && isspace((unsigned char)(s[-1]))) s--;    *s = 0;    linenumber++;    if (buffer[0] == 0) continue;   /* Skip blank lines */    if (!compile_pattern(buffer, pcre_options, filename, linenumber))      return 2;    }  if (f != stdin) fclose(f);  }/* Study the regular expressions, as we will be running them many times */for (j = 0; j < pattern_count; j++)  {  hints_list[j] = pcre_study(pattern_list[j], 0, &error);  if (error != NULL)    {    char s[16];    if (pattern_count == 1) s[0] = 0; else sprintf(s, " number %d", j);    fprintf(stderr, "pcregrep: Error while studying regex%s: %s\n", s, error);    return 2;    }  }/* If there are include or exclude patterns, compile them. */if (exclude_pattern != NULL)  {  exclude_compiled = pcre_compile(exclude_pattern, 0, &error, &errptr,    pcretables);  if (exclude_compiled == NULL)    {    fprintf(stderr, "pcregrep: Error in 'exclude' regex at offset %d: %s\n",      errptr, error);    return 2;    }  }if (include_pattern != NULL)  {  include_compiled = pcre_compile(include_pattern, 0, &error, &errptr,    pcretables);  if (include_compiled == NULL)    {    fprintf(stderr, "pcregrep: Error in 'include' regex at offset %d: %s\n",      errptr, error);    return 2;    }  }/* If there are no further arguments, do the business on stdin and exit. */if (i >= argc)  return pcregrep(stdin, (filenames > FN_DEFAULT)? stdin_name : NULL);/* Otherwise, work through the remaining arguments as files or directories.Pass in the fact that there is only one argument at top level - this suppressesthe file name if the argument is not a directory and filenames are nototherwise forced. */only_one_at_top = i == argc - 1;   /* Catch initial value of i */for (; i < argc; i++)  {  int frc = grep_or_recurse(argv[i], dee_action == dee_RECURSE,    only_one_at_top);  if (frc > 1) rc = frc;    else if (frc == 0 && rc == 1) rc = 0;  }return rc;}/* End of pcregrep */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -