- Introduced pcrs_strerror()
[privoxy.git] / pcrs.h
diff --git a/pcrs.h b/pcrs.h
index bdb1e47..527c8c9 100644 (file)
--- a/pcrs.h
+++ b/pcrs.h
@@ -1,31 +1,37 @@
-#ifndef _PCRS_H
-#define _PCRS_H
+#ifndef PCRS_H_INCLUDED
+#define PCRS_H_INCLUDED
 
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/pcrs.h,v $
  *
- * Purpose     :  This is the pre-pre-alpha realease of libpcrs. It is only
- *                published at this (ugly) stage of development, because it is
- *                needed for a new feature in JunkBuster.
+ * Purpose     :  Header file for pcrs.c
  *
- *                Apart from the code being quite a mess, no inconsistencies,
- *                memory leaks or functional bugs **should** be present.
+ * Copyright   :  see pcrs.c
  *
- *                While you ROTFL at the code, you could just as well mail me
- *                (oes@paradis.rhein.de) with advice for improvement.
+ * Revisions   :
+ *    $Log: pcrs.h,v $
+ *    Revision 1.8  2001/08/15 15:32:50  oes
+ *    Replaced the hard limit for the maximum number of matches
+ *    by dynamic reallocation
  *
- *                pcrs is a supplement to the brilliant pcre library by Philip
- *                Hazel (ph10@cam.ac.uk) and adds Perl-style substitution. That
- *                is, it mimics Perl's 's' operator.
+ *    Revision 1.7  2001/08/05 13:13:11  jongfoster
+ *    Making parameters "const" where possible.
  *
- *                Currently, there's no documentation besides comments and the
- *                source itself ;-)
+ *    Revision 1.6  2001/07/29 18:52:06  jongfoster
+ *    Renaming _PCRS_H, and adding "extern C {}"
  *
- * Copyright   :  Written and copyright 2001 by Sourceforge IJBSWA team.
+ *    Revision 1.5  2001/07/18 17:27:00  oes
+ *    Changed interface; Cosmetics
+ *
+ *    Revision 1.4  2001/06/29 13:33:19  oes
+ *    - Cleaned up, commented and adapted to reflect the
+ *      changes in pcrs.c
+ *    - Introduced the PCRS_* flags
+ *
+ *    Revision 1.3  2001/06/09 10:58:57  jongfoster
+ *    Removing a single unused #define which referenced BUFSIZ
  *
- * Revisions   :
- *    $Log: pcrs.h,v $
  *    Revision 1.2  2001/05/25 11:03:55  oes
  *    Added sanity check for NULL jobs to pcrs_exec_substitution
  *
  *
  *********************************************************************/
 
-#define PCRS_H_VERSION "$Id: pcrs.h,v 1.2 2001/05/25 11:03:55 oes Exp $"
+#define PCRS_H_VERSION "$Id: pcrs.h,v 1.8 2001/08/15 15:32:50 oes Exp $"
 \f
 
-
 #include <pcre.h>
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Constants:
+ */
+
 #define FALSE 0
 #define TRUE 1
-#define PCRS_MAX_MATCHES 300
-#define PCRS_MAX_SUBMATCHES 33
 
+/* Capacity */
+#define PCRS_MAX_SUBMATCHES  33     /* Maximum number of capturing subpatterns allowed. MUST be <= 99! FIXME: Should be dynamic */
+#define PCRS_MAX_MATCH_INIT  40     /* Initial amount of matches that can be stored in global searches */
+#define PCRS_MAX_MATCH_GROW  1.6    /* Factor by which storage for matches is extended if exhausted */
+
+/* Error codes */
 #define PCRS_ERR_NOMEM     -10      /* Failed to acquire memory. */
 #define PCRS_ERR_CMDSYNTAX -11      /* Syntax of s///-command */
 #define PCRS_ERR_STUDY     -12      /* pcre error while studying the pattern */
 #define PCRS_ERR_BADJOB    -13      /* NULL job pointer, pattern or substitute */
+#define PCRS_WARN_BADREF   -14      /* Backreference out of range */
+
+/* Flags */
+#define PCRS_GLOBAL          1      /* Job should be applied globally, as with perl's g option */
+#define PCRS_TRIVIAL         2      /* Backreferences in the substitute are ignored */
+#define PCRS_SUCCESS         4      /* Job did previously match */
+
 
-typedef struct S_PCRS_SUBSTITUTE {
-  char *text;
-  int backrefs;
-  int block_offset[PCRS_MAX_SUBMATCHES];
-  int block_length[PCRS_MAX_SUBMATCHES];
-  int backref[PCRS_MAX_SUBMATCHES];
-  int backref_count[PCRS_MAX_SUBMATCHES];
+/*
+ * Data types:
+ */
+
+/* A compiled substitute */
+
+typedef struct {
+  char *text;                                 /* The plaintext part of the substitute, with all backreferences stripped */
+  int backrefs;                               /* The number of backreferences */
+  int block_offset[PCRS_MAX_SUBMATCHES];      /* Array with the offsets of all plaintext blocks in text */
+  int block_length[PCRS_MAX_SUBMATCHES];      /* Array with the lengths of all plaintext blocks in text */
+  int backref[PCRS_MAX_SUBMATCHES];           /* Array with the backref number for all plaintext block borders */
+  int backref_count[PCRS_MAX_SUBMATCHES + 2]; /* Array with the number of references to each backref index */
 } pcrs_substitute;
 
-typedef struct S_PCRS_MATCH {
-  /* char *buffer; */
-  int submatches;
-  int submatch_offset[PCRS_MAX_SUBMATCHES];
-  int submatch_length[PCRS_MAX_SUBMATCHES];
+/*
+ * A match, including all captured subpatterns (submatches)
+ * Note: The zeroth is the whole match, the PCRS_MAX_SUBMATCHES + 0th
+ * is the range before the match, the PCRS_MAX_SUBMATCHES + 1th is the
+ * range after the match.
+ */
+
+typedef struct {
+  int submatches;                               /* Number of captured subpatterns */
+  int submatch_offset[PCRS_MAX_SUBMATCHES + 2]; /* Offset for each submatch in the subject */
+  int submatch_length[PCRS_MAX_SUBMATCHES + 2]; /* Length of each submatch in the subject */
 } pcrs_match;
 
-typedef struct S_PCRS_JOB {
-  pcre *pattern;
-  pcre_extra *hints;
-  int options;
-  int globalflag;
-  int successflag;
-  pcrs_substitute *substitute;
-  struct S_PCRS_JOB *next;
+/* A pcrs job */
+
+typedef struct PCRS_JOB {
+  pcre *pattern;                            /* The compiled pcre pattern */
+  pcre_extra *hints;                        /* The pcre hints for the pattern */
+  int options;                              /* The pcre options (numeric) */
+  int flags;                                /* The pcrs and user flags (see "Flags" above) */
+  pcrs_substitute *substitute;              /* The compiled pcrs substitute */
+  struct PCRS_JOB *next;                    /* Pointer for chaining jobs to joblists */
 } pcrs_job;
 
-extern int              pcrs_compile_perl_options(char *optstring, int *globalflag);
-extern pcrs_substitute *pcrs_compile_replacement(char *replacement, int *errptr);
+
+/*
+ * Prototypes:
+ */
+
+/* Main usage */
+extern pcrs_job        *pcrs_compile_command(const char *command, int *errptr);
+extern pcrs_job        *pcrs_compile(const char *pattern, const char *substitute, const char *options, int *errptr);
+extern int              pcrs_execute(pcrs_job *job, char *subject, size_t subject_length, char **result, size_t *result_length);
+extern int              pcrs_execute_list(pcrs_job *joblist, char *subject, size_t subject_length, char **result, size_t *result_length);
+
+/* Freeing jobs */
 extern pcrs_job        *pcrs_free_job(pcrs_job *job);
-extern pcrs_job        *pcrs_make_job(char *command, int *errptr);
-extern pcrs_job        *create_pcrs_job(pcre *pattern, pcre_extra *hints, int options, int globalflag, pcrs_substitute *substitute, int *errptr);
-extern int              pcrs_exec_substitution(pcrs_job *job, char *subject, int subject_length, char **result, int *result_length);
+extern void             pcrs_free_joblist(pcrs_job *joblist);
+
+/* Info on errors: */
+extern const char *pcrs_strerror(const int error);
+
+/* Expert usage */
+extern int              pcrs_parse_perl_options(const char *optstring, int *flags);
+extern pcrs_substitute *pcrs_compile_replacement(const char *replacement, int trivialflag, int capturecount, int *errptr);
+
 
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
 
-#endif /* ndef _PCRS_H */
+#endif /* ndef PCRS_H_INCLUDED */
 
 /*
   Local Variables: