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 Lesser
20 * General Public License (LGPL), version 2.1, which should
21 * be included in this distribution (see LICENSE.txt), with
22 * the exception that the permission to replace that license
23 * with the GNU General Public License (GPL) given in section
24 * 3 is restricted to version 2 of the GPL.
26 * This program is distributed in the hope that it will
27 * be useful, but WITHOUT ANY WARRANTY; without even the
28 * implied warranty of MERCHANTABILITY or FITNESS FOR A
29 * PARTICULAR PURPOSE. See the license for more details.
31 * The GNU Lesser General Public License should be included
32 * with this file. If not, you can view it at
33 * http://www.gnu.org/licenses/lgpl.html
34 * or write to the Free Software Foundation, Inc., 59
35 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37 *********************************************************************/
45 * Include project.h just so that the right pcre.h gets
50 /* For snprintf only */
61 static int pcrs_parse_perl_options(const char *optstring, int *flags);
62 static pcrs_substitute *pcrs_compile_replacement(const char *replacement, int trivialflag,
63 int capturecount, int *errptr);
64 static int is_hex_sequence(const char *sequence);
66 /*********************************************************************
68 * Function : pcrs_strerror
70 * Description : Return a string describing a given error code.
73 * 1 : error = the error code
75 * Returns : char * to the descriptive string
77 *********************************************************************/
78 const char *pcrs_strerror(const int error)
86 /* Passed-through PCRE error: */
87 case PCRE_ERROR_NOMEMORY: return "(pcre:) No memory";
89 /* Shouldn't happen unless PCRE or PCRS bug, or user messed with compiled job: */
90 case PCRE_ERROR_NULL: return "(pcre:) NULL code or subject or ovector";
91 case PCRE_ERROR_BADOPTION: return "(pcre:) Unrecognized option bit";
92 case PCRE_ERROR_BADMAGIC: return "(pcre:) Bad magic number in code";
93 case PCRE_ERROR_UNKNOWN_NODE: return "(pcre:) Bad node in pattern";
95 /* Can't happen / not passed: */
96 case PCRE_ERROR_NOSUBSTRING: return "(pcre:) Fire in power supply";
97 case PCRE_ERROR_NOMATCH: return "(pcre:) Water in power supply";
99 #ifdef PCRE_ERROR_MATCHLIMIT
101 * Only reported by PCRE versions newer than our own.
103 case PCRE_ERROR_MATCHLIMIT: return "(pcre:) Match limit reached";
104 #endif /* def PCRE_ERROR_MATCHLIMIT */
107 case PCRS_ERR_NOMEM: return "(pcrs:) No memory";
108 case PCRS_ERR_CMDSYNTAX: return "(pcrs:) Syntax error while parsing command";
109 case PCRS_ERR_STUDY: return "(pcrs:) PCRE error while studying the pattern";
110 case PCRS_ERR_BADJOB: return "(pcrs:) Bad job - NULL job, pattern or substitute";
111 case PCRS_WARN_BADREF: return "(pcrs:) Backreference out of range";
112 case PCRS_WARN_TRUNCATION:
113 return "(pcrs:) At least one variable was too big and has been truncated before compilation";
116 * XXX: With the exception of PCRE_ERROR_MATCHLIMIT we
117 * only catch PCRE errors that can happen with our internal
118 * version. If Privoxy is linked against a newer
119 * PCRE version all bets are off ...
122 snprintf(buf, sizeof(buf),
123 "Error code %d. For details, check the pcre documentation.",
128 /* error >= 0: No error */
129 return "(pcrs:) Everything's just fine. Thanks for asking.";
134 /*********************************************************************
136 * Function : pcrs_parse_perl_options
138 * Description : This function parses a string containing the options to
139 * Perl's s/// operator. It returns an integer that is the
140 * pcre equivalent of the symbolic optstring.
141 * Since pcre doesn't know about Perl's 'g' (global) or pcrs',
142 * 'T' (trivial) options but pcrs needs them, the corresponding
143 * flags are set if 'g'or 'T' is encountered.
144 * Note: The 'T' and 'U' options do not conform to Perl.
147 * 1 : optstring = string with options in perl syntax
148 * 2 : flags = see description
150 * Returns : option integer suitable for pcre
152 *********************************************************************/
153 static int pcrs_parse_perl_options(const char *optstring, int *flags)
159 if (NULL == optstring) return 0;
161 for (i = 0; i < strlen(optstring); i++)
165 case 'e': break; /* ToDo ;-) */
166 case 'g': *flags |= PCRS_GLOBAL; break;
167 case 'i': rc |= PCRE_CASELESS; break;
168 case 'm': rc |= PCRE_MULTILINE; break;
170 case 's': rc |= PCRE_DOTALL; break;
171 case 'x': rc |= PCRE_EXTENDED; 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 text = strncpy(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) free(job->hints);
476 if (job->substitute != NULL)
478 if (job->substitute->text != NULL) free(job->substitute->text);
479 free(job->substitute);
488 /*********************************************************************
490 * Function : pcrs_free_joblist
492 * Description : Iterates through a chained list of pcrs_job's and
493 * frees them using pcrs_free_job.
496 * 1 : joblist = pointer to the first pcrs_job structure to
501 *********************************************************************/
502 void pcrs_free_joblist(pcrs_job *joblist)
504 while (NULL != (joblist = pcrs_free_job(joblist))) {};
511 /*********************************************************************
513 * Function : pcrs_compile_command
515 * Description : Parses a string with a Perl-style s/// command,
516 * calls pcrs_compile, and returns a corresponding
517 * pcrs_job, or NULL if parsing or compiling the job
521 * 1 : command = string with perl-style s/// command
522 * 2 : errptr = pointer to an integer in which error
523 * conditions can be returned.
525 * Returns : a corresponding pcrs_job data structure, or NULL
526 * if an error was encountered. In that case, *errptr
529 *********************************************************************/
530 pcrs_job *pcrs_compile_command(const char *command, int *errptr)
532 int i, k, l, quoted = FALSE;
541 * Tokenize the perl command
543 limit = strlen(command);
546 *errptr = PCRS_ERR_CMDSYNTAX;
551 delimiter = command[1];
554 tokens[l] = (char *) malloc(limit + 1);
556 for (i = 0; i <= (int)limit; i++)
559 if (command[i] == delimiter && !quoted)
566 tokens[0][k++] = '\0';
567 tokens[++l] = tokens[0] + k;
571 else if (command[i] == '\\' && !quoted)
574 if (command[i+1] == delimiter) continue;
580 tokens[0][k++] = command[i];
588 *errptr = PCRS_ERR_CMDSYNTAX;
593 newjob = pcrs_compile(tokens[1], tokens[2], tokens[3], errptr);
600 /*********************************************************************
602 * Function : pcrs_compile
604 * Description : Takes the three arguments to a perl s/// command
605 * and compiles a pcrs_job structure from them.
608 * 1 : pattern = string with perl-style pattern
609 * 2 : substitute = string with perl-style substitute
610 * 3 : options = string with perl-style options
611 * 4 : errptr = pointer to an integer in which error
612 * conditions can be returned.
614 * Returns : a corresponding pcrs_job data structure, or NULL
615 * if an error was encountered. In that case, *errptr
618 *********************************************************************/
619 pcrs_job *pcrs_compile(const char *pattern, const char *substitute, const char *options, int *errptr)
629 * Handle NULL arguments
631 if (pattern == NULL) pattern = "";
632 if (substitute == NULL) substitute = "";
636 * Get and init memory
638 if (NULL == (newjob = (pcrs_job *)malloc(sizeof(pcrs_job))))
640 *errptr = PCRS_ERR_NOMEM;
643 memset(newjob, '\0', sizeof(pcrs_job));
647 * Evaluate the options
649 newjob->options = pcrs_parse_perl_options(options, &flags);
650 newjob->flags = flags;
654 * Compile the pattern
656 newjob->pattern = pcre_compile(pattern, newjob->options, &error, errptr, NULL);
657 if (newjob->pattern == NULL)
659 pcrs_free_job(newjob);
665 * Generate hints. This has little overhead, since the
666 * hints will be NULL for a boring pattern anyway.
668 newjob->hints = pcre_study(newjob->pattern, 0, &error);
671 *errptr = PCRS_ERR_STUDY;
672 pcrs_free_job(newjob);
678 * Determine the number of capturing subpatterns.
679 * This is needed for handling $+ in the substitute.
681 if (0 > (*errptr = pcre_fullinfo(newjob->pattern, newjob->hints, PCRE_INFO_CAPTURECOUNT, &capturecount)))
683 pcrs_free_job(newjob);
689 * Compile the substitute
691 if (NULL == (newjob->substitute = pcrs_compile_replacement(substitute, newjob->flags & PCRS_TRIVIAL, capturecount, errptr)))
693 pcrs_free_job(newjob);
702 /*********************************************************************
704 * Function : pcrs_execute_list
706 * Description : This is a multiple job wrapper for pcrs_execute().
707 * Apply the regular substitutions defined by the jobs in
708 * the joblist to the subject.
709 * The subject itself is left untouched, memory for the result
710 * is malloc()ed and it is the caller's responsibility to free
711 * the result when it's no longer needed.
713 * Note: For convenient string handling, a null byte is
714 * appended to the result. It does not count towards the
715 * result_length, though.
719 * 1 : joblist = the chained list of pcrs_jobs to be executed
720 * 2 : subject = the subject string
721 * 3 : subject_length = the subject's length
722 * 4 : result = char** for returning the result
723 * 5 : result_length = size_t* for returning the result's length
725 * Returns : On success, the number of substitutions that were made.
726 * May be > 1 if job->flags contained PCRS_GLOBAL
727 * On failure, the (negative) pcre error code describing the
728 * failure, which may be translated to text using pcrs_strerror().
730 *********************************************************************/
731 int pcrs_execute_list(pcrs_job *joblist, char *subject, size_t subject_length, char **result, size_t *result_length)
734 char *old, *new = NULL;
735 int hits, total_hits;
738 *result_length = subject_length;
741 for (job = joblist; job != NULL; job = job->next)
743 hits = pcrs_execute(job, old, *result_length, &new, result_length);
745 if (old != subject) free(old);
764 /*********************************************************************
766 * Function : pcrs_execute
768 * Description : Apply the regular substitution defined by the job to the
770 * The subject itself is left untouched, memory for the result
771 * is malloc()ed and it is the caller's responsibility to free
772 * the result when it's no longer needed.
774 * Note: For convenient string handling, a null byte is
775 * appended to the result. It does not count towards the
776 * result_length, though.
779 * 1 : job = the pcrs_job to be executed
780 * 2 : subject = the subject (== original) string
781 * 3 : subject_length = the subject's length
782 * 4 : result = char** for returning the result (NULL on error)
783 * 5 : result_length = size_t* for returning the result's length
785 * Returns : On success, the number of substitutions that were made.
786 * May be > 1 if job->flags contained PCRS_GLOBAL
787 * On failure, the (negative) pcre error code describing the
788 * failure, which may be translated to text using pcrs_strerror().
790 *********************************************************************/
791 int pcrs_execute(pcrs_job *job, const char *subject, size_t subject_length, char **result, size_t *result_length)
793 int offsets[3 * PCRS_MAX_SUBMATCHES],
798 max_matches = PCRS_MAX_MATCH_INIT;
800 pcrs_match *matches, *dummy;
807 * Sanity check & memory allocation
809 if (job == NULL || job->pattern == NULL || job->substitute == NULL || NULL == subject)
811 return(PCRS_ERR_BADJOB);
814 if (NULL == (matches = (pcrs_match *)malloc((size_t)max_matches * sizeof(pcrs_match))))
816 return(PCRS_ERR_NOMEM);
818 memset(matches, '\0', (size_t)max_matches * sizeof(pcrs_match));
822 * Find the pattern and calculate the space
823 * requirements for the result
825 newsize = subject_length;
827 while ((submatches = pcre_exec(job->pattern, job->hints, subject, (int)subject_length, offset, 0, offsets, 3 * PCRS_MAX_SUBMATCHES)) > 0)
829 job->flags |= PCRS_SUCCESS;
830 matches[i].submatches = submatches;
832 for (k = 0; k < submatches; k++)
834 matches[i].submatch_offset[k] = offsets[2 * k];
836 /* Note: Non-found optional submatches have length -1-(-1)==0 */
837 matches[i].submatch_length[k] = (size_t)(offsets[2 * k + 1] - offsets[2 * k]);
839 /* reserve mem for each submatch as often as it is ref'd */
840 newsize += matches[i].submatch_length[k] * (size_t)job->substitute->backref_count[k];
842 /* plus replacement text size minus match text size */
843 newsize += job->substitute->length - matches[i].submatch_length[0];
845 /* chunk before match */
846 matches[i].submatch_offset[PCRS_MAX_SUBMATCHES] = 0;
847 matches[i].submatch_length[PCRS_MAX_SUBMATCHES] = (size_t)offsets[0];
848 newsize += (size_t)offsets[0] * (size_t)job->substitute->backref_count[PCRS_MAX_SUBMATCHES];
850 /* chunk after match */
851 matches[i].submatch_offset[PCRS_MAX_SUBMATCHES + 1] = offsets[1];
852 matches[i].submatch_length[PCRS_MAX_SUBMATCHES + 1] = subject_length - (size_t)offsets[1] - 1;
853 newsize += (subject_length - (size_t)offsets[1]) * (size_t)job->substitute->backref_count[PCRS_MAX_SUBMATCHES + 1];
855 /* Storage for matches exhausted? -> Extend! */
856 if (++i >= max_matches)
858 max_matches = (int)(max_matches * PCRS_MAX_MATCH_GROW);
859 if (NULL == (dummy = (pcrs_match *)realloc(matches, (size_t)max_matches * sizeof(pcrs_match))))
862 return(PCRS_ERR_NOMEM);
867 /* Non-global search or limit reached? */
868 if (!(job->flags & PCRS_GLOBAL)) break;
870 /* Don't loop on empty matches */
871 if (offsets[1] == offset)
872 if ((size_t)offset < subject_length)
876 /* Go find the next one */
880 /* Pass pcre error through if (bad) failure */
881 if (submatches < PCRE_ERROR_NOMATCH)
890 * Get memory for the result (must be freed by caller!)
891 * and append terminating null byte.
893 if ((*result = (char *)malloc(newsize + 1)) == NULL)
896 return PCRS_ERR_NOMEM;
900 (*result)[newsize] = '\0';
908 result_offset = *result;
910 for (i = 0; i < matches_found; i++)
912 /* copy the chunk preceding the match */
913 memcpy(result_offset, subject + offset, (size_t)(matches[i].submatch_offset[0] - offset));
914 result_offset += matches[i].submatch_offset[0] - offset;
916 /* For every segment of the substitute.. */
917 for (k = 0; k <= job->substitute->backrefs; k++)
919 /* ...copy its text.. */
920 memcpy(result_offset, job->substitute->text + job->substitute->block_offset[k], job->substitute->block_length[k]);
921 result_offset += job->substitute->block_length[k];
923 /* ..plus, if it's not the last chunk, i.e.: There *is* a backref.. */
924 if (k != job->substitute->backrefs
925 /* ..in legal range.. */
926 && job->substitute->backref[k] < PCRS_MAX_SUBMATCHES + 2
927 /* ..and referencing a real submatch.. */
928 && job->substitute->backref[k] < matches[i].submatches
929 /* ..that is nonempty.. */
930 && matches[i].submatch_length[job->substitute->backref[k]] > 0)
932 /* ..copy the submatch that is ref'd. */
935 subject + matches[i].submatch_offset[job->substitute->backref[k]],
936 matches[i].submatch_length[job->substitute->backref[k]]
938 result_offset += matches[i].submatch_length[job->substitute->backref[k]];
941 offset = matches[i].submatch_offset[0] + (int)matches[i].submatch_length[0];
945 memcpy(result_offset, subject + offset, subject_length - (size_t)offset);
947 *result_length = newsize;
949 return matches_found;
954 #define is_hex_digit(x) ((x) && strchr("0123456789ABCDEF", toupper(x)))
956 /*********************************************************************
958 * Function : is_hex_sequence
960 * Description : Checks the first four characters of a string
961 * and decides if they are a valid hex sequence
965 * 1 : sequence = The string to check
967 * Returns : Non-zero if it's valid sequence, or
970 *********************************************************************/
971 static int is_hex_sequence(const char *sequence)
973 return (sequence[0] == '\\' &&
974 sequence[1] == 'x' &&
975 is_hex_digit(sequence[2]) &&
976 is_hex_digit(sequence[3]));
981 * Functions below this line are only part of the pcrs version
982 * included in Privoxy. If you use any of them you should not
983 * try to dynamically link against external pcrs versions.
986 /*********************************************************************
988 * Function : pcrs_job_is_dynamic
990 * Description : Checks if a job has the "D" (dynamic) option set.
993 * 1 : job = The job to check
995 * Returns : TRUE if the job is indeed dynamic, otherwise
998 *********************************************************************/
999 int pcrs_job_is_dynamic (char *job)
1001 const char delimiter = job[1];
1002 const size_t length = strlen(job);
1008 * The shortest valid (but useless)
1009 * dynamic pattern is "s@@@D"
1015 * Everything between the last character
1016 * and the last delimiter is an option ...
1018 for (option = job + length; *option != delimiter; option--)
1023 * ... and if said option is 'D' the job is dynamic.
1033 /*********************************************************************
1035 * Function : pcrs_get_delimiter
1037 * Description : Tries to find a character that is safe to
1038 * be used as a pcrs delimiter for a certain string.
1041 * 1 : string = The string to search in
1043 * Returns : A safe delimiter if one was found, otherwise '\0'.
1045 *********************************************************************/
1046 char pcrs_get_delimiter(const char *string)
1049 * Some characters that are unlikely to
1050 * be part of pcrs replacement strings.
1052 static const char delimiters[] = "><#+*~%^-:;!@";
1053 const char *d = delimiters;
1055 /* Take the first delimiter that isn't part of the string */
1056 while (*d && NULL != strchr(string, *d))
1065 /*********************************************************************
1067 * Function : pcrs_execute_single_command
1069 * Description : Apply single pcrs command to the subject.
1070 * The subject itself is left untouched, memory for the result
1071 * is malloc()ed and it is the caller's responsibility to free
1072 * the result when it's no longer needed.
1075 * 1 : subject = the subject (== original) string
1076 * 2 : pcrs_command = the pcrs command as string (s@foo@bar@)
1077 * 3 : hits = int* for returning the number of modifications
1079 * Returns : NULL in case of errors, otherwise the
1080 * result of the pcrs command.
1082 *********************************************************************/
1083 char *pcrs_execute_single_command(const char *subject, const char *pcrs_command, int *hits)
1086 char *result = NULL;
1090 assert(pcrs_command);
1093 size = strlen(subject);
1095 job = pcrs_compile_command(pcrs_command, hits);
1098 *hits = pcrs_execute(job, subject, size, &result, &size);
1110 static const char warning[] = "... [too long, truncated]";
1111 /*********************************************************************
1113 * Function : pcrs_compile_dynamic_command
1115 * Description : Takes a dynamic pcrs command, fills in the
1116 * values of the variables and compiles it.
1119 * 1 : pcrs_command = The dynamic pcrs command to compile
1120 * 2 : v = NULL terminated array of variables and their values.
1121 * 3 : error = pcrs error code
1123 * Returns : NULL in case of hard errors, otherwise the
1124 * compiled pcrs job.
1126 *********************************************************************/
1127 pcrs_job *pcrs_compile_dynamic_command(char *pcrs_command, const struct pcrs_variable v[], int *error)
1129 char buf[PCRS_BUFFER_SIZE];
1130 const char *original_pcrs_command = pcrs_command;
1131 char *pcrs_command_tmp = NULL;
1132 pcrs_job *job = NULL;
1137 while ((NULL != v->name) && (NULL != pcrs_command))
1139 assert(NULL != v->value);
1141 if (NULL == strstr(pcrs_command, v->name))
1144 * Skip the substitution if the variable
1145 * name isn't part of the pattern.
1151 /* Use pcrs to replace the variable with its value. */
1152 d = pcrs_get_delimiter(v->value);
1155 /* No proper delimiter found */
1156 *error = PCRS_ERR_CMDSYNTAX;
1157 freez(pcrs_command_tmp);
1162 * Variable names are supposed to contain alpha
1163 * numerical characters plus '_' only.
1165 assert(NULL == strchr(v->name, d));
1167 ret = snprintf(buf, sizeof(buf), "s%c\\$%s%c%s%cgT", d, v->name, d, v->value, d);
1169 if (ret >= sizeof(buf))
1172 * Value didn't completely fit into buffer,
1173 * overwrite the end of the substitution text
1174 * with a truncation message and close the pattern
1177 const size_t trailer_size = sizeof(warning) + 3; /* 3 for d + "gT" */
1178 char *trailer_start = buf + sizeof(buf) - trailer_size;
1180 ret = snprintf(trailer_start, trailer_size, "%s%cgT", warning, d);
1181 assert(ret == trailer_size - 1);
1182 assert(sizeof(buf) == strlen(buf) + 1);
1186 pcrs_command_tmp = pcrs_execute_single_command(pcrs_command, buf, error);
1187 if (NULL == pcrs_command_tmp)
1192 if (pcrs_command != original_pcrs_command)
1194 freez(pcrs_command);
1196 pcrs_command = pcrs_command_tmp;
1201 job = pcrs_compile_command(pcrs_command, error);
1202 if (pcrs_command != original_pcrs_command)
1204 freez(pcrs_command);
1209 *error = PCRS_WARN_TRUNCATION;