1 /*********************************************************************
3 * File : $Source: /cvsroot/ijbswa/current/pcrs.c,v $
5 * Purpose : pcrs is a supplement to the pcre library by Philip Hazel
6 * <ph10@cam.ac.uk> and adds Perl-style substitution. That
7 * is, it mimics Perl's 's' operator. See pcrs(3) for details.
9 * WARNING: This file contains additional functions and bug
10 * fixes that aren't part of the latest official pcrs package
11 * (which apparently is no longer maintained).
13 * Copyright : Written and Copyright (C) 2000, 2001 by Andreas S. Oesterhelt
14 * <andreas@oesterhelt.org>
16 * Copyright (C) 2006, 2007 Fabian Keil <fk@fabiankeil.de>
18 * This program is free software; you can redistribute it
19 * and/or modify it under the terms of the GNU General
20 * Public License as published by the Free Software
21 * Foundation; either version 2 of the License, or (at
22 * your option) any later version.
24 * This program is distributed in the hope that it will
25 * be useful, but WITHOUT ANY WARRANTY; without even the
26 * implied warranty of MERCHANTABILITY or FITNESS FOR A
27 * PARTICULAR PURPOSE. See the GNU General Public
28 * License for more details.
30 * The GNU General Public License should be included with
31 * this file. If not, you can view it at
32 * http://www.gnu.org/copyleft/gpl.html
33 * or write to the Free Software Foundation, Inc., 59
34 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 *********************************************************************/
44 * Include project.h just so that the right pcre.h gets
49 /* For snprintf only */
60 static int pcrs_parse_perl_options(const char *optstring, int *flags);
61 static pcrs_substitute *pcrs_compile_replacement(const char *replacement, int trivialflag,
62 int capturecount, int *errptr);
63 static int is_hex_sequence(const char *sequence);
65 /*********************************************************************
67 * Function : pcrs_strerror
69 * Description : Return a string describing a given error code.
72 * 1 : error = the error code
74 * Returns : char * to the descriptive string
76 *********************************************************************/
77 const char *pcrs_strerror(const int error)
85 /* Passed-through PCRE error: */
86 case PCRE_ERROR_NOMEMORY: return "(pcre:) No memory";
88 /* Shouldn't happen unless PCRE or PCRS bug, or user messed with compiled job: */
89 case PCRE_ERROR_NULL: return "(pcre:) NULL code or subject or ovector";
90 case PCRE_ERROR_BADOPTION: return "(pcre:) Unrecognized option bit";
91 case PCRE_ERROR_BADMAGIC: return "(pcre:) Bad magic number in code";
92 case PCRE_ERROR_UNKNOWN_NODE: return "(pcre:) Bad node in pattern";
94 /* Can't happen / not passed: */
95 case PCRE_ERROR_NOSUBSTRING: return "(pcre:) Fire in power supply";
96 case PCRE_ERROR_NOMATCH: return "(pcre:) Water in power supply";
98 #ifdef PCRE_ERROR_MATCHLIMIT
100 * Only reported by PCRE versions newer than our own.
102 case PCRE_ERROR_MATCHLIMIT: return "(pcre:) Match limit reached";
103 #endif /* def PCRE_ERROR_MATCHLIMIT */
106 case PCRS_ERR_NOMEM: return "(pcrs:) No memory";
107 case PCRS_ERR_CMDSYNTAX: return "(pcrs:) Syntax error while parsing command";
108 case PCRS_ERR_STUDY: return "(pcrs:) PCRE error while studying the pattern";
109 case PCRS_ERR_BADJOB: return "(pcrs:) Bad job - NULL job, pattern or substitute";
110 case PCRS_WARN_BADREF: return "(pcrs:) Backreference out of range";
111 case PCRS_WARN_TRUNCATION:
112 return "(pcrs:) At least one variable was too big and has been truncated before compilation";
115 * XXX: With the exception of PCRE_ERROR_MATCHLIMIT we
116 * only catch PCRE errors that can happen with our internal
117 * version. If Privoxy is linked against a newer
118 * PCRE version all bets are off ...
121 snprintf(buf, sizeof(buf),
122 "Error code %d. For details, check the pcre documentation.",
127 /* error >= 0: No error */
128 return "(pcrs:) Everything's just fine. Thanks for asking.";
133 /*********************************************************************
135 * Function : pcrs_parse_perl_options
137 * Description : This function parses a string containing the options to
138 * Perl's s/// operator. It returns an integer that is the
139 * pcre equivalent of the symbolic optstring.
140 * Since pcre doesn't know about Perl's 'g' (global) or pcrs',
141 * 'T' (trivial) options but pcrs needs them, the corresponding
142 * flags are set if 'g'or 'T' is encountered.
143 * Note: The 'T' and 'U' options do not conform to Perl.
146 * 1 : optstring = string with options in perl syntax
147 * 2 : flags = see description
149 * Returns : option integer suitable for pcre
151 *********************************************************************/
152 static int pcrs_parse_perl_options(const char *optstring, int *flags)
158 if (NULL == optstring) return 0;
160 for (i = 0; i < strlen(optstring); i++)
164 case 'e': break; /* ToDo ;-) */
165 case 'g': *flags |= PCRS_GLOBAL; break;
166 case 'i': rc |= PCRE_CASELESS; break;
167 case 'm': rc |= PCRE_MULTILINE; break;
169 case 's': rc |= PCRE_DOTALL; break;
170 case 'x': rc |= PCRE_EXTENDED; break;
171 case 'D': *flags |= PCRS_DYNAMIC; break;
172 case 'U': rc |= PCRE_UNGREEDY; break;
173 case 'T': *flags |= PCRS_TRIVIAL; break;
183 /*********************************************************************
185 * Function : pcrs_compile_fuzzed_replacement
187 * Description : Wrapper around pcrs_compile_replacement() for
191 * 1 : replacement = replacement part of s/// operator
193 * 2 : errptr = pointer to an integer in which error
194 * conditions can be returned.
196 * Returns : pcrs_substitute data structure, or NULL if an
197 * error is encountered. In that case, *errptr has
200 *********************************************************************/
201 extern pcrs_substitute *pcrs_compile_fuzzed_replacement(const char *replacement, int *errptr)
203 int capturecount = PCRS_MAX_SUBMATCHES; /* XXX: fuzzworthy? */
204 int trivial_flag = 0; /* We don't want to fuzz strncpy() */
206 *errptr = 0; /* XXX: Should pcrs_compile_replacement() do this? */
208 return pcrs_compile_replacement(replacement, trivial_flag, capturecount, errptr);
214 /*********************************************************************
216 * Function : pcrs_compile_replacement
218 * Description : This function takes a Perl-style replacement (2nd argument
219 * to the s/// operator and returns a compiled pcrs_substitute,
220 * or NULL if memory allocation for the substitute structure
224 * 1 : replacement = replacement part of s/// operator
226 * 2 : trivialflag = Flag that causes backreferences to be
228 * 3 : capturecount = Number of capturing subpatterns in
229 * the pattern. Needed for $+ handling.
230 * 4 : errptr = pointer to an integer in which error
231 * conditions can be returned.
233 * Returns : pcrs_substitute data structure, or NULL if an
234 * error is encountered. In that case, *errptr has
237 *********************************************************************/
238 static pcrs_substitute *pcrs_compile_replacement(const char *replacement, int trivialflag, int capturecount, int *errptr)
246 static size_t length;
248 i = k = l = quoted = 0;
253 if (NULL == replacement)
261 if (NULL == (r = (pcrs_substitute *)malloc(sizeof(pcrs_substitute))))
263 *errptr = PCRS_ERR_NOMEM;
266 memset(r, '\0', sizeof(pcrs_substitute));
268 length = strlen(replacement);
270 if (NULL == (text = (char *)malloc(length + 1)))
273 *errptr = PCRS_ERR_NOMEM;
276 memset(text, '\0', length + 1);
280 * In trivial mode, just copy the substitute text
284 strlcpy(text, replacement, length + 1);
289 * Else, parse, cut out and record all backreferences
293 while (i < (int)length)
296 if (replacement[i] == '\\')
300 text[k++] = replacement[i++];
305 if (replacement[i+1] && strchr("tnrfae0", replacement[i+1]))
307 switch (replacement[++i])
333 else if (is_hex_sequence(&replacement[i]))
336 * Replace a hex sequence with a single
337 * character with the sequence's ascii value.
338 * e.g.: '\x7e' => '~'
340 const int ascii_value = xtoi(&replacement[i+2]);
342 assert(ascii_value >= 0);
343 assert(ascii_value < 256);
344 text[k++] = (char)ascii_value;
357 if (replacement[i] == '$' && !quoted && i < (int)(length - 1))
359 char *symbol, symbols[] = "'`+&";
360 if (l >= PCRS_MAX_SUBMATCHES)
364 *errptr = PCRS_WARN_BADREF;
367 r->block_length[l] = (size_t)(k - r->block_offset[l]);
369 /* Numerical backreferences */
370 if (isdigit((int)replacement[i + 1]))
372 while (i < (int)length && isdigit((int)replacement[++i]))
374 r->backref[l] = r->backref[l] * 10 + replacement[i] - 48;
376 if (r->backref[l] > capturecount)
380 *errptr = PCRS_WARN_BADREF;
385 /* Symbolic backreferences: */
386 else if (NULL != (symbol = strchr(symbols, replacement[i + 1])))
389 if (symbol - symbols == 2) /* $+ */
391 r->backref[l] = capturecount;
393 else if (symbol - symbols == 3) /* $& */
399 r->backref[l] = (int)(PCRS_MAX_SUBMATCHES + 1 - (symbol - symbols));
404 /* Invalid backref -> plain '$' */
410 assert(r->backref[l] < PCRS_MAX_SUBMATCHES + 2);
411 /* Valid and in range? -> record */
412 if ((0 <= r->backref[l]) &&
413 (r->backref[l] < PCRS_MAX_SUBMATCHES + 2) &&
414 (l < PCRS_MAX_SUBMATCHES - 1))
416 r->backref_count[r->backref[l]] += 1;
417 r->block_offset[++l] = k;
423 *errptr = PCRS_WARN_BADREF;
430 /* Plain chars are copied */
431 text[k++] = replacement[i++];
434 } /* -END- if (!trivialflag) */
441 r->length = (size_t)k;
442 r->block_length[l] = (size_t)(k - r->block_offset[l]);
449 /*********************************************************************
451 * Function : pcrs_free_job
453 * Description : Frees the memory used by a pcrs_job struct and its
454 * dependent structures.
457 * 1 : job = pointer to the pcrs_job structure to be freed
459 * Returns : a pointer to the next job, if there was any, or
462 *********************************************************************/
463 pcrs_job *pcrs_free_job(pcrs_job *job)
474 if (job->pattern != NULL) free(job->pattern);
475 if (job->hints != NULL)
477 #ifdef PCRE_CONFIG_JIT
478 pcre_free_study(job->hints);
483 if (job->substitute != NULL)
485 if (job->substitute->text != NULL) free(job->substitute->text);
486 free(job->substitute);
495 /*********************************************************************
497 * Function : pcrs_free_joblist
499 * Description : Iterates through a chained list of pcrs_job's and
500 * frees them using pcrs_free_job.
503 * 1 : joblist = pointer to the first pcrs_job structure to
508 *********************************************************************/
509 void pcrs_free_joblist(pcrs_job *joblist)
511 while (NULL != (joblist = pcrs_free_job(joblist))) {};
518 /*********************************************************************
520 * Function : pcrs_compile_command
522 * Description : Parses a string with a Perl-style s/// command,
523 * calls pcrs_compile, and returns a corresponding
524 * pcrs_job, or NULL if parsing or compiling the job
528 * 1 : command = string with perl-style s/// command
529 * 2 : errptr = pointer to an integer in which error
530 * conditions can be returned.
532 * Returns : a corresponding pcrs_job data structure, or NULL
533 * if an error was encountered. In that case, *errptr
536 *********************************************************************/
537 pcrs_job *pcrs_compile_command(const char *command, int *errptr)
539 int i, k, l, quoted = FALSE;
548 * Tokenize the perl command
550 limit = strlen(command);
553 *errptr = PCRS_ERR_CMDSYNTAX;
558 delimiter = command[1];
561 tokens[l] = (char *) malloc(limit + 1);
563 for (i = 0; i <= (int)limit; i++)
566 if (command[i] == delimiter && !quoted)
573 tokens[0][k++] = '\0';
574 tokens[++l] = tokens[0] + k;
578 else if (command[i] == '\\' && !quoted)
581 if (command[i+1] == delimiter) continue;
587 tokens[0][k++] = command[i];
595 *errptr = PCRS_ERR_CMDSYNTAX;
600 newjob = pcrs_compile(tokens[1], tokens[2], tokens[3], errptr);
607 /*********************************************************************
609 * Function : pcrs_compile
611 * Description : Takes the three arguments to a perl s/// command
612 * and compiles a pcrs_job structure from them.
615 * 1 : pattern = string with perl-style pattern
616 * 2 : substitute = string with perl-style substitute
617 * 3 : options = string with perl-style options
618 * 4 : errptr = pointer to an integer in which error
619 * conditions can be returned.
621 * Returns : a corresponding pcrs_job data structure, or NULL
622 * if an error was encountered. In that case, *errptr
625 *********************************************************************/
626 pcrs_job *pcrs_compile(const char *pattern, const char *substitute, const char *options, int *errptr)
632 int pcre_study_options = 0;
637 * Handle NULL arguments
639 if (pattern == NULL) pattern = "";
640 if (substitute == NULL) substitute = "";
644 * Get and init memory
646 if (NULL == (newjob = (pcrs_job *)malloc(sizeof(pcrs_job))))
648 *errptr = PCRS_ERR_NOMEM;
651 memset(newjob, '\0', sizeof(pcrs_job));
655 * Evaluate the options
657 newjob->options = pcrs_parse_perl_options(options, &flags);
658 newjob->flags = flags;
662 * Compile the pattern
664 newjob->pattern = pcre_compile(pattern, newjob->options, &error, errptr, NULL);
665 if (newjob->pattern == NULL)
667 pcrs_free_job(newjob);
672 #ifdef PCRE_STUDY_JIT_COMPILE
673 #ifdef DISABLE_PCRE_JIT_COMPILATION
674 #warning PCRE_STUDY_JIT_COMPILE is supported but Privoxy has been configured not to use it
676 if (!(flags & PCRS_DYNAMIC))
678 pcre_study_options = PCRE_STUDY_JIT_COMPILE;
684 * Generate hints. This has little overhead, since the
685 * hints will be NULL for a boring pattern anyway.
687 newjob->hints = pcre_study(newjob->pattern, pcre_study_options, &error);
690 *errptr = PCRS_ERR_STUDY;
691 pcrs_free_job(newjob);
697 * Determine the number of capturing subpatterns.
698 * This is needed for handling $+ in the substitute.
700 if (0 > (*errptr = pcre_fullinfo(newjob->pattern, newjob->hints, PCRE_INFO_CAPTURECOUNT, &capturecount)))
702 pcrs_free_job(newjob);
708 * Compile the substitute
710 if (NULL == (newjob->substitute = pcrs_compile_replacement(substitute, newjob->flags & PCRS_TRIVIAL, capturecount, errptr)))
712 pcrs_free_job(newjob);
721 /*********************************************************************
723 * Function : pcrs_execute_list
725 * Description : This is a multiple job wrapper for pcrs_execute().
726 * Apply the regular substitutions defined by the jobs in
727 * the joblist to the subject.
728 * The subject itself is left untouched, memory for the result
729 * is malloc()ed and it is the caller's responsibility to free
730 * the result when it's no longer needed.
732 * Note: For convenient string handling, a null byte is
733 * appended to the result. It does not count towards the
734 * result_length, though.
738 * 1 : joblist = the chained list of pcrs_jobs to be executed
739 * 2 : subject = the subject string
740 * 3 : subject_length = the subject's length
741 * 4 : result = char** for returning the result
742 * 5 : result_length = size_t* for returning the result's length
744 * Returns : On success, the number of substitutions that were made.
745 * May be > 1 if job->flags contained PCRS_GLOBAL
746 * On failure, the (negative) pcre error code describing the
747 * failure, which may be translated to text using pcrs_strerror().
749 *********************************************************************/
750 int pcrs_execute_list(pcrs_job *joblist, char *subject, size_t subject_length, char **result, size_t *result_length)
753 char *old, *new = NULL;
754 int hits, total_hits;
757 *result_length = subject_length;
760 for (job = joblist; job != NULL; job = job->next)
762 hits = pcrs_execute(job, old, *result_length, &new, result_length);
764 if (old != subject) free(old);
783 /*********************************************************************
785 * Function : pcrs_execute
787 * Description : Apply the regular substitution defined by the job to the
789 * The subject itself is left untouched, memory for the result
790 * is malloc()ed and it is the caller's responsibility to free
791 * the result when it's no longer needed.
793 * Note: For convenient string handling, a null byte is
794 * appended to the result. It does not count towards the
795 * result_length, though.
798 * 1 : job = the pcrs_job to be executed
799 * 2 : subject = the subject (== original) string
800 * 3 : subject_length = the subject's length
801 * 4 : result = char** for returning the result (NULL on error)
802 * 5 : result_length = size_t* for returning the result's length
804 * Returns : On success, the number of substitutions that were made.
805 * May be > 1 if job->flags contained PCRS_GLOBAL
806 * On failure, the (negative) pcre error code describing the
807 * failure, which may be translated to text using pcrs_strerror().
809 *********************************************************************/
810 int pcrs_execute(pcrs_job *job, const char *subject, size_t subject_length, char **result, size_t *result_length)
812 int offsets[3 * PCRS_MAX_SUBMATCHES],
817 max_matches = PCRS_MAX_MATCH_INIT;
819 pcrs_match *matches, *dummy;
826 * Sanity check & memory allocation
828 if (job == NULL || job->pattern == NULL || job->substitute == NULL || NULL == subject)
830 return(PCRS_ERR_BADJOB);
833 if (NULL == (matches = (pcrs_match *)malloc((size_t)max_matches * sizeof(pcrs_match))))
835 return(PCRS_ERR_NOMEM);
837 memset(matches, '\0', (size_t)max_matches * sizeof(pcrs_match));
841 * Find the pattern and calculate the space
842 * requirements for the result
844 newsize = subject_length;
846 while ((submatches = pcre_exec(job->pattern, job->hints, subject, (int)subject_length, offset, 0, offsets, 3 * PCRS_MAX_SUBMATCHES)) > 0)
848 job->flags |= PCRS_SUCCESS;
849 matches[i].submatches = submatches;
851 for (k = 0; k < submatches; k++)
853 matches[i].submatch_offset[k] = offsets[2 * k];
855 /* Note: Non-found optional submatches have length -1-(-1)==0 */
856 matches[i].submatch_length[k] = (size_t)(offsets[2 * k + 1] - offsets[2 * k]);
858 /* reserve mem for each submatch as often as it is ref'd */
859 newsize += matches[i].submatch_length[k] * (size_t)job->substitute->backref_count[k];
861 /* plus replacement text size minus match text size */
862 newsize += job->substitute->length - matches[i].submatch_length[0];
864 /* chunk before match */
865 matches[i].submatch_offset[PCRS_MAX_SUBMATCHES] = 0;
866 matches[i].submatch_length[PCRS_MAX_SUBMATCHES] = (size_t)offsets[0];
867 newsize += (size_t)offsets[0] * (size_t)job->substitute->backref_count[PCRS_MAX_SUBMATCHES];
869 /* chunk after match */
870 matches[i].submatch_offset[PCRS_MAX_SUBMATCHES + 1] = offsets[1];
871 matches[i].submatch_length[PCRS_MAX_SUBMATCHES + 1] = subject_length - (size_t)offsets[1] - 1;
872 newsize += (subject_length - (size_t)offsets[1]) * (size_t)job->substitute->backref_count[PCRS_MAX_SUBMATCHES + 1];
874 /* Storage for matches exhausted? -> Extend! */
875 if (++i >= max_matches)
877 max_matches = (int)(max_matches * PCRS_MAX_MATCH_GROW);
878 if (NULL == (dummy = (pcrs_match *)realloc(matches, (size_t)max_matches * sizeof(pcrs_match))))
881 return(PCRS_ERR_NOMEM);
886 /* Non-global search or limit reached? */
887 if (!(job->flags & PCRS_GLOBAL)) break;
889 /* Don't loop on empty matches */
890 if (offsets[1] == offset)
891 if ((size_t)offset < subject_length)
895 /* Go find the next one */
899 /* Pass pcre error through if (bad) failure */
900 if (submatches < PCRE_ERROR_NOMATCH)
909 * Get memory for the result (must be freed by caller!)
910 * and append terminating null byte.
912 if ((*result = (char *)malloc(newsize + 1)) == NULL)
915 return PCRS_ERR_NOMEM;
919 (*result)[newsize] = '\0';
927 result_offset = *result;
929 for (i = 0; i < matches_found; i++)
931 /* copy the chunk preceding the match */
932 memcpy(result_offset, subject + offset, (size_t)(matches[i].submatch_offset[0] - offset));
933 result_offset += matches[i].submatch_offset[0] - offset;
935 /* For every segment of the substitute.. */
936 for (k = 0; k <= job->substitute->backrefs; k++)
938 /* ...copy its text.. */
939 memcpy(result_offset, job->substitute->text + job->substitute->block_offset[k], job->substitute->block_length[k]);
940 result_offset += job->substitute->block_length[k];
942 /* ..plus, if it's not the last chunk, i.e.: There *is* a backref.. */
943 if (k != job->substitute->backrefs
944 /* ..in legal range.. */
945 && job->substitute->backref[k] < PCRS_MAX_SUBMATCHES + 2
946 /* ..and referencing a real submatch.. */
947 && job->substitute->backref[k] < matches[i].submatches
948 /* ..that is nonempty.. */
949 && matches[i].submatch_length[job->substitute->backref[k]] > 0)
951 /* ..copy the submatch that is ref'd. */
954 subject + matches[i].submatch_offset[job->substitute->backref[k]],
955 matches[i].submatch_length[job->substitute->backref[k]]
957 result_offset += matches[i].submatch_length[job->substitute->backref[k]];
960 offset = matches[i].submatch_offset[0] + (int)matches[i].submatch_length[0];
964 memcpy(result_offset, subject + offset, subject_length - (size_t)offset);
966 *result_length = newsize;
968 return matches_found;
973 #define is_hex_digit(x) ((x) && strchr("0123456789ABCDEF", toupper(x)))
975 /*********************************************************************
977 * Function : is_hex_sequence
979 * Description : Checks the first four characters of a string
980 * and decides if they are a valid hex sequence
984 * 1 : sequence = The string to check
986 * Returns : Non-zero if it's valid sequence, or
989 *********************************************************************/
990 static int is_hex_sequence(const char *sequence)
992 return (sequence[0] == '\\' &&
993 sequence[1] == 'x' &&
994 is_hex_digit(sequence[2]) &&
995 is_hex_digit(sequence[3]));
1000 * Functions below this line are only part of the pcrs version
1001 * included in Privoxy. If you use any of them you should not
1002 * try to dynamically link against external pcrs versions.
1005 /*********************************************************************
1007 * Function : pcrs_job_is_dynamic
1009 * Description : Checks if a job has the "D" (dynamic) option set.
1012 * 1 : job = The job to check
1014 * Returns : TRUE if the job is indeed dynamic, otherwise
1017 *********************************************************************/
1018 int pcrs_job_is_dynamic(char *job)
1020 const char delimiter = job[1];
1021 const size_t length = strlen(job);
1027 * The shortest valid (but useless)
1028 * dynamic pattern is "s@@@D"
1034 * Everything between the last character
1035 * and the last delimiter is an option ...
1037 for (option = job + length; *option != delimiter; option--)
1042 * ... and if said option is 'D' the job is dynamic.
1052 /*********************************************************************
1054 * Function : pcrs_get_delimiter
1056 * Description : Tries to find a character that is safe to
1057 * be used as a pcrs delimiter for a certain string.
1060 * 1 : string = The string to search in
1062 * Returns : A safe delimiter if one was found, otherwise '\0'.
1064 *********************************************************************/
1065 char pcrs_get_delimiter(const char *string)
1068 * Some characters that are unlikely to
1069 * be part of pcrs replacement strings.
1071 static const char delimiters[] = "><#+*~%^-:;!@";
1072 const char *d = delimiters;
1074 /* Take the first delimiter that isn't part of the string */
1075 while (*d && NULL != strchr(string, *d))
1084 /*********************************************************************
1086 * Function : pcrs_execute_single_command
1088 * Description : Apply single pcrs command to the subject.
1089 * The subject itself is left untouched, memory for the result
1090 * is malloc()ed and it is the caller's responsibility to free
1091 * the result when it's no longer needed.
1094 * 1 : subject = the subject (== original) string
1095 * 2 : pcrs_command = the pcrs command as string (s@foo@bar@)
1096 * 3 : hits = int* for returning the number of modifications
1098 * Returns : NULL in case of errors, otherwise the
1099 * result of the pcrs command.
1101 *********************************************************************/
1102 char *pcrs_execute_single_command(const char *subject, const char *pcrs_command, int *hits)
1105 char *result = NULL;
1109 assert(pcrs_command);
1112 size = strlen(subject);
1114 job = pcrs_compile_command(pcrs_command, hits);
1117 *hits = pcrs_execute(job, subject, size, &result, &size);
1129 /*********************************************************************
1131 * Function : pcrs_compile_dynamic_command
1133 * Description : Takes a dynamic pcrs command, fills in the
1134 * values of the variables and compiles it.
1137 * 1 : pcrs_command = The dynamic pcrs command to compile
1138 * 2 : v = NULL terminated array of variables and their values.
1139 * 3 : error = pcrs error code
1141 * Returns : NULL in case of hard errors, otherwise the
1142 * compiled pcrs job.
1144 *********************************************************************/
1145 pcrs_job *pcrs_compile_dynamic_command(char *pcrs_command, const struct pcrs_variable v[], int *error)
1147 char buf[PCRS_BUFFER_SIZE];
1148 const char *original_pcrs_command = pcrs_command;
1149 char *pcrs_command_tmp = NULL;
1150 pcrs_job *job = NULL;
1155 while ((NULL != v->name) && (NULL != pcrs_command))
1157 assert(NULL != v->value);
1159 if (NULL == strstr(pcrs_command, v->name))
1162 * Skip the substitution if the variable
1163 * name isn't part of the pattern.
1169 /* Use pcrs to replace the variable with its value. */
1170 d = pcrs_get_delimiter(v->value);
1173 /* No proper delimiter found */
1174 *error = PCRS_ERR_CMDSYNTAX;
1175 freez(pcrs_command_tmp);
1180 * Variable names are supposed to contain alpha
1181 * numerical characters plus '_' only.
1183 assert(NULL == strchr(v->name, d));
1185 ret = snprintf(buf, sizeof(buf), "s%c\\$%s%c%s%cDgT", d, v->name, d, v->value, d);
1187 if (ret >= sizeof(buf))
1190 * Value didn't completely fit into buffer,
1191 * overwrite the end of the substitution text
1192 * with a truncation message and close the pattern
1195 static const char warning[] = "... [too long, truncated]";
1196 const size_t trailer_size = sizeof(warning) + 4; /* 4 for d + "DgT" */
1197 char *trailer_start = buf + sizeof(buf) - trailer_size;
1199 ret = snprintf(trailer_start, trailer_size, "%s%cDgT", warning, d);
1200 assert(ret == trailer_size - 1);
1201 assert(sizeof(buf) == strlen(buf) + 1);
1205 pcrs_command_tmp = pcrs_execute_single_command(pcrs_command, buf, error);
1206 if (NULL == pcrs_command_tmp)
1211 if (pcrs_command != original_pcrs_command)
1213 freez(pcrs_command);
1215 pcrs_command = pcrs_command_tmp;
1220 job = pcrs_compile_command(pcrs_command, error);
1221 if (pcrs_command != original_pcrs_command)
1223 freez(pcrs_command);
1228 *error = PCRS_WARN_TRUNCATION;