pcrs.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #ifndef PCRS_H_INCLUDED
  2. #define PCRS_H_INCLUDED
  3. /*********************************************************************
  4. *
  5. * File : $Source: /cvsroot/ijbswa/current/pcrs.h,v $
  6. *
  7. * Purpose : Header file for pcrs.c
  8. *
  9. * Copyright : Written and Copyright (C) 2000, 2001 by Andreas S. Oesterhelt
  10. * <andreas@oesterhelt.org>
  11. *
  12. * Copyright (C) 2006, 2007 Fabian Keil <fk@fabiankeil.de>
  13. *
  14. * This program is free software; you can redistribute it
  15. * and/or modify it under the terms of the GNU General
  16. * Public License as published by the Free Software
  17. * Foundation; either version 2 of the License, or (at
  18. * your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will
  21. * be useful, but WITHOUT ANY WARRANTY; without even the
  22. * implied warranty of MERCHANTABILITY or FITNESS FOR A
  23. * PARTICULAR PURPOSE. See the GNU General Public
  24. * License for more details.
  25. *
  26. * The GNU General Public License should be included with
  27. * this file. If not, you can view it at
  28. * http://www.gnu.org/copyleft/gpl.html
  29. * or write to the Free Software Foundation, Inc., 59
  30. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  31. *
  32. *********************************************************************/
  33. #ifndef _PCRE_H
  34. #include <pcre.h>
  35. #endif
  36. /*
  37. * Constants:
  38. */
  39. #define FALSE 0
  40. #define TRUE 1
  41. /* Capacity */
  42. #define PCRS_MAX_SUBMATCHES 33 /* Maximum number of capturing subpatterns allowed. MUST be <= 99! FIXME: Should be dynamic */
  43. #define PCRS_MAX_MATCH_INIT 40 /* Initial amount of matches that can be stored in global searches */
  44. #define PCRS_MAX_MATCH_GROW 1.6 /* Factor by which storage for matches is extended if exhausted */
  45. /*
  46. * PCRS error codes
  47. *
  48. * They are supposed to be handled together with PCRE error
  49. * codes and have to start with an offset to prevent overlaps.
  50. *
  51. * PCRE 6.7 uses error codes from -1 to -21, PCRS error codes
  52. * below -100 should be safe for a while.
  53. */
  54. #define PCRS_ERR_NOMEM -100 /* Failed to acquire memory. */
  55. #define PCRS_ERR_CMDSYNTAX -101 /* Syntax of s///-command */
  56. #define PCRS_ERR_STUDY -102 /* pcre error while studying the pattern */
  57. #define PCRS_ERR_BADJOB -103 /* NULL job pointer, pattern or substitute */
  58. #define PCRS_WARN_BADREF -104 /* Backreference out of range */
  59. #define PCRS_WARN_TRUNCATION -105 /* At least one pcrs variable was too big,
  60. * only the first part was used. */
  61. /* Flags */
  62. #define PCRS_GLOBAL 1 /* Job should be applied globally, as with perl's g option */
  63. #define PCRS_TRIVIAL 2 /* Backreferences in the substitute are ignored */
  64. #define PCRS_SUCCESS 4 /* Job did previously match */
  65. #define PCRS_DYNAMIC 8 /* Job is dynamic (used to disable JIT compilation) */
  66. /*
  67. * Data types:
  68. */
  69. /* A compiled substitute */
  70. typedef struct {
  71. char *text; /* The plaintext part of the substitute, with all backreferences stripped */
  72. size_t length; /* The substitute may not be a valid C string so we can't rely on strlen(). */
  73. int backrefs; /* The number of backreferences */
  74. int block_offset[PCRS_MAX_SUBMATCHES]; /* Array with the offsets of all plaintext blocks in text */
  75. size_t block_length[PCRS_MAX_SUBMATCHES]; /* Array with the lengths of all plaintext blocks in text */
  76. int backref[PCRS_MAX_SUBMATCHES]; /* Array with the backref number for all plaintext block borders */
  77. int backref_count[PCRS_MAX_SUBMATCHES + 2]; /* Array with the number of references to each backref index */
  78. } pcrs_substitute;
  79. /*
  80. * A match, including all captured subpatterns (submatches)
  81. * Note: The zeroth is the whole match, the PCRS_MAX_SUBMATCHES + 0th
  82. * is the range before the match, the PCRS_MAX_SUBMATCHES + 1th is the
  83. * range after the match.
  84. */
  85. typedef struct {
  86. int submatches; /* Number of captured subpatterns */
  87. int submatch_offset[PCRS_MAX_SUBMATCHES + 2]; /* Offset for each submatch in the subject */
  88. size_t submatch_length[PCRS_MAX_SUBMATCHES + 2]; /* Length of each submatch in the subject */
  89. } pcrs_match;
  90. /* A PCRS job */
  91. typedef struct PCRS_JOB {
  92. pcre *pattern; /* The compiled pcre pattern */
  93. pcre_extra *hints; /* The pcre hints for the pattern */
  94. int options; /* The pcre options (numeric) */
  95. int flags; /* The pcrs and user flags (see "Flags" above) */
  96. pcrs_substitute *substitute; /* The compiled pcrs substitute */
  97. struct PCRS_JOB *next; /* Pointer for chaining jobs to joblists */
  98. } pcrs_job;
  99. /*
  100. * Prototypes:
  101. */
  102. /* Main usage */
  103. extern pcrs_job *pcrs_compile_command(const char *command, int *errptr);
  104. extern pcrs_job *pcrs_compile(const char *pattern, const char *substitute, const char *options, int *errptr);
  105. extern int pcrs_execute(pcrs_job *job, const char *subject, size_t subject_length, char **result, size_t *result_length);
  106. extern int pcrs_execute_list(pcrs_job *joblist, char *subject, size_t subject_length, char **result, size_t *result_length);
  107. /* Freeing jobs */
  108. extern pcrs_job *pcrs_free_job(pcrs_job *job);
  109. extern void pcrs_free_joblist(pcrs_job *joblist);
  110. /* Info on errors: */
  111. extern const char *pcrs_strerror(const int error);
  112. extern int pcrs_job_is_dynamic(char *job);
  113. extern char pcrs_get_delimiter(const char *string);
  114. extern char *pcrs_execute_single_command(const char *subject, const char *pcrs_command, int *hits);
  115. /*
  116. * Variable/value pair for dynamic pcrs commands.
  117. */
  118. struct pcrs_variable
  119. {
  120. const char *name;
  121. char *value;
  122. int static_value;
  123. };
  124. extern pcrs_job *pcrs_compile_dynamic_command(char *pcrs_command, const struct pcrs_variable v[], int *error);
  125. /* Only relevant for maximum pcrs variable size */
  126. #ifndef PCRS_BUFFER_SIZE
  127. #define PCRS_BUFFER_SIZE 4000
  128. #endif /* ndef PCRS_BUFFER_SIZE */
  129. #ifdef FUZZ
  130. extern pcrs_substitute *pcrs_compile_fuzzed_replacement(const char *replacement, int *errptr);
  131. #endif
  132. #endif /* ndef PCRS_H_INCLUDED */
  133. /*
  134. Local Variables:
  135. tab-width: 3
  136. end:
  137. */