1 const char pcrs_rcs[] = "$Id: pcrs.c,v 1.52 2017/05/29 10:09:37 fabiankeil Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/pcrs.c,v $
6 * Purpose : pcrs is a supplement to the pcre library by Philip Hazel
7 * <ph10@cam.ac.uk> and adds Perl-style substitution. That
8 * is, it mimics Perl's 's' operator. See pcrs(3) for details.
10 * WARNING: This file contains additional functions and bug
11 * fixes that aren't part of the latest official pcrs package
12 * (which apparently is no longer maintained).
14 * Copyright : Written and Copyright (C) 2000, 2001 by Andreas S. Oesterhelt
15 * <andreas@oesterhelt.org>
17 * Copyright (C) 2006, 2007 Fabian Keil <fk@fabiankeil.de>
19 * This program is free software; you can redistribute it
20 * and/or modify it under the terms of the GNU Lesser
21 * General Public License (LGPL), version 2.1, which should
22 * be included in this distribution (see LICENSE.txt), with
23 * the exception that the permission to replace that license
24 * with the GNU General Public License (GPL) given in section
25 * 3 is restricted to version 2 of the GPL.
27 * This program is distributed in the hope that it will
28 * be useful, but WITHOUT ANY WARRANTY; without even the
29 * implied warranty of MERCHANTABILITY or FITNESS FOR A
30 * PARTICULAR PURPOSE. See the license for more details.
32 * The GNU Lesser General Public License should be included
33 * with this file. If not, you can view it at
34 * http://www.gnu.org/licenses/lgpl.html
35 * or write to the Free Software Foundation, Inc., 59
36 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
38 *********************************************************************/
46 * Include project.h just so that the right pcre.h gets
51 /* For snprintf only */
58 const char pcrs_h_rcs[] = PCRS_H_VERSION;
64 static int pcrs_parse_perl_options(const char *optstring, int *flags);
65 static pcrs_substitute *pcrs_compile_replacement(const char *replacement, int trivialflag,
66 int capturecount, int *errptr);
67 static int is_hex_sequence(const char *sequence);
69 /*********************************************************************
71 * Function : pcrs_strerror
73 * Description : Return a string describing a given error code.
76 * 1 : error = the error code
78 * Returns : char * to the descriptive string
80 *********************************************************************/
81 const char *pcrs_strerror(const int error)
89 /* Passed-through PCRE error: */
90 case PCRE_ERROR_NOMEMORY: return "(pcre:) No memory";
92 /* Shouldn't happen unless PCRE or PCRS bug, or user messed with compiled job: */
93 case PCRE_ERROR_NULL: return "(pcre:) NULL code or subject or ovector";
94 case PCRE_ERROR_BADOPTION: return "(pcre:) Unrecognized option bit";
95 case PCRE_ERROR_BADMAGIC: return "(pcre:) Bad magic number in code";
96 case PCRE_ERROR_UNKNOWN_NODE: return "(pcre:) Bad node in pattern";
98 /* Can't happen / not passed: */
99 case PCRE_ERROR_NOSUBSTRING: return "(pcre:) Fire in power supply";
100 case PCRE_ERROR_NOMATCH: return "(pcre:) Water in power supply";
102 #ifdef PCRE_ERROR_MATCHLIMIT
104 * Only reported by PCRE versions newer than our own.
106 case PCRE_ERROR_MATCHLIMIT: return "(pcre:) Match limit reached";
107 #endif /* def PCRE_ERROR_MATCHLIMIT */
110 case PCRS_ERR_NOMEM: return "(pcrs:) No memory";
111 case PCRS_ERR_CMDSYNTAX: return "(pcrs:) Syntax error while parsing command";
112 case PCRS_ERR_STUDY: return "(pcrs:) PCRE error while studying the pattern";
113 case PCRS_ERR_BADJOB: return "(pcrs:) Bad job - NULL job, pattern or substitute";
114 case PCRS_WARN_BADREF: return "(pcrs:) Backreference out of range";
115 case PCRS_WARN_TRUNCATION:
116 return "(pcrs:) At least one variable was too big and has been truncated before compilation";
119 * XXX: With the exception of PCRE_ERROR_MATCHLIMIT we
120 * only catch PCRE errors that can happen with our internal
121 * version. If Privoxy is linked against a newer
122 * PCRE version all bets are off ...
125 snprintf(buf, sizeof(buf),
126 "Error code %d. For details, check the pcre documentation.",
131 /* error >= 0: No error */
132 return "(pcrs:) Everything's just fine. Thanks for asking.";
137 /*********************************************************************
139 * Function : pcrs_parse_perl_options
141 * Description : This function parses a string containing the options to
142 * Perl's s/// operator. It returns an integer that is the
143 * pcre equivalent of the symbolic optstring.
144 * Since pcre doesn't know about Perl's 'g' (global) or pcrs',
145 * 'T' (trivial) options but pcrs needs them, the corresponding
146 * flags are set if 'g'or 'T' is encountered.
147 * Note: The 'T' and 'U' options do not conform to Perl.
150 * 1 : optstring = string with options in perl syntax
151 * 2 : flags = see description
153 * Returns : option integer suitable for pcre
155 *********************************************************************/
156 static int pcrs_parse_perl_options(const char *optstring, int *flags)
162 if (NULL == optstring) return 0;
164 for (i = 0; i < strlen(optstring); i++)
168 case 'e': break; /* ToDo ;-) */
169 case 'g': *flags |= PCRS_GLOBAL; break;
170 case 'i': rc |= PCRE_CASELESS; break;
171 case 'm': rc |= PCRE_MULTILINE; break;
173 case 's': rc |= PCRE_DOTALL; break;
174 case 'x': rc |= PCRE_EXTENDED; break;
175 case 'U': rc |= PCRE_UNGREEDY; break;
176 case 'T': *flags |= PCRS_TRIVIAL; break;
186 /*********************************************************************
188 * Function : pcrs_compile_fuzzed_replacement
190 * Description : Wrapper around pcrs_compile_replacement() for
194 * 1 : replacement = replacement part of s/// operator
196 * 2 : errptr = pointer to an integer in which error
197 * conditions can be returned.
199 * Returns : pcrs_substitute data structure, or NULL if an
200 * error is encountered. In that case, *errptr has
203 *********************************************************************/
204 extern pcrs_substitute *pcrs_compile_fuzzed_replacement(const char *replacement, int *errptr)
206 int capturecount = PCRS_MAX_SUBMATCHES; /* XXX: fuzzworthy? */
207 int trivial_flag = 0; /* We don't want to fuzz strncpy() */
209 *errptr = 0; /* XXX: Should pcrs_compile_replacement() do this? */
211 return pcrs_compile_replacement(replacement, trivial_flag, capturecount, errptr);
217 /*********************************************************************
219 * Function : pcrs_compile_replacement
221 * Description : This function takes a Perl-style replacement (2nd argument
222 * to the s/// operator and returns a compiled pcrs_substitute,
223 * or NULL if memory allocation for the substitute structure
227 * 1 : replacement = replacement part of s/// operator
229 * 2 : trivialflag = Flag that causes backreferences to be
231 * 3 : capturecount = Number of capturing subpatterns in
232 * the pattern. Needed for $+ handling.
233 * 4 : errptr = pointer to an integer in which error
234 * conditions can be returned.
236 * Returns : pcrs_substitute data structure, or NULL if an
237 * error is encountered. In that case, *errptr has
240 *********************************************************************/
241 static pcrs_substitute *pcrs_compile_replacement(const char *replacement, int trivialflag, int capturecount, int *errptr)
249 static size_t length;
251 i = k = l = quoted = 0;
256 if (NULL == replacement)
264 if (NULL == (r = (pcrs_substitute *)malloc(sizeof(pcrs_substitute))))
266 *errptr = PCRS_ERR_NOMEM;
269 memset(r, '\0', sizeof(pcrs_substitute));
271 length = strlen(replacement);
273 if (NULL == (text = (char *)malloc(length + 1)))
276 *errptr = PCRS_ERR_NOMEM;
279 memset(text, '\0', length + 1);
283 * In trivial mode, just copy the substitute text
287 text = strncpy(text, replacement, length + 1);
292 * Else, parse, cut out and record all backreferences
296 while (i < (int)length)
299 if (replacement[i] == '\\')
303 text[k++] = replacement[i++];
308 if (replacement[i+1] && strchr("tnrfae0", replacement[i+1]))
310 switch (replacement[++i])
336 else if (is_hex_sequence(&replacement[i]))
339 * Replace a hex sequence with a single
340 * character with the sequence's ascii value.
341 * e.g.: '\x7e' => '~'
343 const int ascii_value = xtoi(&replacement[i+2]);
345 assert(ascii_value >= 0);
346 assert(ascii_value < 256);
347 text[k++] = (char)ascii_value;
360 if (replacement[i] == '$' && !quoted && i < (int)(length - 1))
362 char *symbol, symbols[] = "'`+&";
363 if (l >= PCRS_MAX_SUBMATCHES)
367 *errptr = PCRS_WARN_BADREF;
370 r->block_length[l] = (size_t)(k - r->block_offset[l]);
372 /* Numerical backreferences */
373 if (isdigit((int)replacement[i + 1]))
375 while (i < (int)length && isdigit((int)replacement[++i]))
377 r->backref[l] = r->backref[l] * 10 + replacement[i] - 48;
379 if (r->backref[l] > capturecount)
383 *errptr = PCRS_WARN_BADREF;
388 /* Symbolic backreferences: */
389 else if (NULL != (symbol = strchr(symbols, replacement[i + 1])))
392 if (symbol - symbols == 2) /* $+ */
394 r->backref[l] = capturecount;
396 else if (symbol - symbols == 3) /* $& */
402 r->backref[l] = (int)(PCRS_MAX_SUBMATCHES + 1 - (symbol - symbols));
407 /* Invalid backref -> plain '$' */
413 assert(r->backref[l] < PCRS_MAX_SUBMATCHES + 2);
414 /* Valid and in range? -> record */
415 if ((0 <= r->backref[l]) &&
416 (r->backref[l] < PCRS_MAX_SUBMATCHES + 2) &&
417 (l < PCRS_MAX_SUBMATCHES - 1))
419 r->backref_count[r->backref[l]] += 1;
420 r->block_offset[++l] = k;
426 *errptr = PCRS_WARN_BADREF;
433 /* Plain chars are copied */
434 text[k++] = replacement[i++];
437 } /* -END- if (!trivialflag) */
444 r->length = (size_t)k;
445 r->block_length[l] = (size_t)(k - r->block_offset[l]);
452 /*********************************************************************
454 * Function : pcrs_free_job
456 * Description : Frees the memory used by a pcrs_job struct and its
457 * dependent structures.
460 * 1 : job = pointer to the pcrs_job structure to be freed
462 * Returns : a pointer to the next job, if there was any, or
465 *********************************************************************/
466 pcrs_job *pcrs_free_job(pcrs_job *job)
477 if (job->pattern != NULL) free(job->pattern);
478 if (job->hints != NULL) free(job->hints);
479 if (job->substitute != NULL)
481 if (job->substitute->text != NULL) free(job->substitute->text);
482 free(job->substitute);
491 /*********************************************************************
493 * Function : pcrs_free_joblist
495 * Description : Iterates through a chained list of pcrs_job's and
496 * frees them using pcrs_free_job.
499 * 1 : joblist = pointer to the first pcrs_job structure to
504 *********************************************************************/
505 void pcrs_free_joblist(pcrs_job *joblist)
507 while (NULL != (joblist = pcrs_free_job(joblist))) {};
514 /*********************************************************************
516 * Function : pcrs_compile_command
518 * Description : Parses a string with a Perl-style s/// command,
519 * calls pcrs_compile, and returns a corresponding
520 * pcrs_job, or NULL if parsing or compiling the job
524 * 1 : command = string with perl-style s/// command
525 * 2 : errptr = pointer to an integer in which error
526 * conditions can be returned.
528 * Returns : a corresponding pcrs_job data structure, or NULL
529 * if an error was encountered. In that case, *errptr
532 *********************************************************************/
533 pcrs_job *pcrs_compile_command(const char *command, int *errptr)
535 int i, k, l, quoted = FALSE;
544 * Tokenize the perl command
546 limit = strlen(command);
549 *errptr = PCRS_ERR_CMDSYNTAX;
554 delimiter = command[1];
557 tokens[l] = (char *) malloc(limit + 1);
559 for (i = 0; i <= (int)limit; i++)
562 if (command[i] == delimiter && !quoted)
569 tokens[0][k++] = '\0';
570 tokens[++l] = tokens[0] + k;
574 else if (command[i] == '\\' && !quoted)
577 if (command[i+1] == delimiter) continue;
583 tokens[0][k++] = command[i];
591 *errptr = PCRS_ERR_CMDSYNTAX;
596 newjob = pcrs_compile(tokens[1], tokens[2], tokens[3], errptr);
603 /*********************************************************************
605 * Function : pcrs_compile
607 * Description : Takes the three arguments to a perl s/// command
608 * and compiles a pcrs_job structure from them.
611 * 1 : pattern = string with perl-style pattern
612 * 2 : substitute = string with perl-style substitute
613 * 3 : options = string with perl-style options
614 * 4 : errptr = pointer to an integer in which error
615 * conditions can be returned.
617 * Returns : a corresponding pcrs_job data structure, or NULL
618 * if an error was encountered. In that case, *errptr
621 *********************************************************************/
622 pcrs_job *pcrs_compile(const char *pattern, const char *substitute, const char *options, int *errptr)
632 * Handle NULL arguments
634 if (pattern == NULL) pattern = "";
635 if (substitute == NULL) substitute = "";
639 * Get and init memory
641 if (NULL == (newjob = (pcrs_job *)malloc(sizeof(pcrs_job))))
643 *errptr = PCRS_ERR_NOMEM;
646 memset(newjob, '\0', sizeof(pcrs_job));
650 * Evaluate the options
652 newjob->options = pcrs_parse_perl_options(options, &flags);
653 newjob->flags = flags;
657 * Compile the pattern
659 newjob->pattern = pcre_compile(pattern, newjob->options, &error, errptr, NULL);
660 if (newjob->pattern == NULL)
662 pcrs_free_job(newjob);
668 * Generate hints. This has little overhead, since the
669 * hints will be NULL for a boring pattern anyway.
671 newjob->hints = pcre_study(newjob->pattern, 0, &error);
674 *errptr = PCRS_ERR_STUDY;
675 pcrs_free_job(newjob);
681 * Determine the number of capturing subpatterns.
682 * This is needed for handling $+ in the substitute.
684 if (0 > (*errptr = pcre_fullinfo(newjob->pattern, newjob->hints, PCRE_INFO_CAPTURECOUNT, &capturecount)))
686 pcrs_free_job(newjob);
692 * Compile the substitute
694 if (NULL == (newjob->substitute = pcrs_compile_replacement(substitute, newjob->flags & PCRS_TRIVIAL, capturecount, errptr)))
696 pcrs_free_job(newjob);
705 /*********************************************************************
707 * Function : pcrs_execute_list
709 * Description : This is a multiple job wrapper for pcrs_execute().
710 * Apply the regular substitutions defined by the jobs in
711 * the joblist to the subject.
712 * The subject itself is left untouched, memory for the result
713 * is malloc()ed and it is the caller's responsibility to free
714 * the result when it's no longer needed.
716 * Note: For convenient string handling, a null byte is
717 * appended to the result. It does not count towards the
718 * result_length, though.
722 * 1 : joblist = the chained list of pcrs_jobs to be executed
723 * 2 : subject = the subject string
724 * 3 : subject_length = the subject's length
725 * 4 : result = char** for returning the result
726 * 5 : result_length = size_t* for returning the result's length
728 * Returns : On success, the number of substitutions that were made.
729 * May be > 1 if job->flags contained PCRS_GLOBAL
730 * On failure, the (negative) pcre error code describing the
731 * failure, which may be translated to text using pcrs_strerror().
733 *********************************************************************/
734 int pcrs_execute_list(pcrs_job *joblist, char *subject, size_t subject_length, char **result, size_t *result_length)
737 char *old, *new = NULL;
738 int hits, total_hits;
741 *result_length = subject_length;
744 for (job = joblist; job != NULL; job = job->next)
746 hits = pcrs_execute(job, old, *result_length, &new, result_length);
748 if (old != subject) free(old);
767 /*********************************************************************
769 * Function : pcrs_execute
771 * Description : Apply the regular substitution defined by the job to the
773 * The subject itself is left untouched, memory for the result
774 * is malloc()ed and it is the caller's responsibility to free
775 * the result when it's no longer needed.
777 * Note: For convenient string handling, a null byte is
778 * appended to the result. It does not count towards the
779 * result_length, though.
782 * 1 : job = the pcrs_job to be executed
783 * 2 : subject = the subject (== original) string
784 * 3 : subject_length = the subject's length
785 * 4 : result = char** for returning the result (NULL on error)
786 * 5 : result_length = size_t* for returning the result's length
788 * Returns : On success, the number of substitutions that were made.
789 * May be > 1 if job->flags contained PCRS_GLOBAL
790 * On failure, the (negative) pcre error code describing the
791 * failure, which may be translated to text using pcrs_strerror().
793 *********************************************************************/
794 int pcrs_execute(pcrs_job *job, const char *subject, size_t subject_length, char **result, size_t *result_length)
796 int offsets[3 * PCRS_MAX_SUBMATCHES],
801 max_matches = PCRS_MAX_MATCH_INIT;
803 pcrs_match *matches, *dummy;
810 * Sanity check & memory allocation
812 if (job == NULL || job->pattern == NULL || job->substitute == NULL || NULL == subject)
814 return(PCRS_ERR_BADJOB);
817 if (NULL == (matches = (pcrs_match *)malloc((size_t)max_matches * sizeof(pcrs_match))))
819 return(PCRS_ERR_NOMEM);
821 memset(matches, '\0', (size_t)max_matches * sizeof(pcrs_match));
825 * Find the pattern and calculate the space
826 * requirements for the result
828 newsize = subject_length;
830 while ((submatches = pcre_exec(job->pattern, job->hints, subject, (int)subject_length, offset, 0, offsets, 3 * PCRS_MAX_SUBMATCHES)) > 0)
832 job->flags |= PCRS_SUCCESS;
833 matches[i].submatches = submatches;
835 for (k = 0; k < submatches; k++)
837 matches[i].submatch_offset[k] = offsets[2 * k];
839 /* Note: Non-found optional submatches have length -1-(-1)==0 */
840 matches[i].submatch_length[k] = (size_t)(offsets[2 * k + 1] - offsets[2 * k]);
842 /* reserve mem for each submatch as often as it is ref'd */
843 newsize += matches[i].submatch_length[k] * (size_t)job->substitute->backref_count[k];
845 /* plus replacement text size minus match text size */
846 newsize += job->substitute->length - matches[i].submatch_length[0];
848 /* chunk before match */
849 matches[i].submatch_offset[PCRS_MAX_SUBMATCHES] = 0;
850 matches[i].submatch_length[PCRS_MAX_SUBMATCHES] = (size_t)offsets[0];
851 newsize += (size_t)offsets[0] * (size_t)job->substitute->backref_count[PCRS_MAX_SUBMATCHES];
853 /* chunk after match */
854 matches[i].submatch_offset[PCRS_MAX_SUBMATCHES + 1] = offsets[1];
855 matches[i].submatch_length[PCRS_MAX_SUBMATCHES + 1] = subject_length - (size_t)offsets[1] - 1;
856 newsize += (subject_length - (size_t)offsets[1]) * (size_t)job->substitute->backref_count[PCRS_MAX_SUBMATCHES + 1];
858 /* Storage for matches exhausted? -> Extend! */
859 if (++i >= max_matches)
861 max_matches = (int)(max_matches * PCRS_MAX_MATCH_GROW);
862 if (NULL == (dummy = (pcrs_match *)realloc(matches, (size_t)max_matches * sizeof(pcrs_match))))
865 return(PCRS_ERR_NOMEM);
870 /* Non-global search or limit reached? */
871 if (!(job->flags & PCRS_GLOBAL)) break;
873 /* Don't loop on empty matches */
874 if (offsets[1] == offset)
875 if ((size_t)offset < subject_length)
879 /* Go find the next one */
883 /* Pass pcre error through if (bad) failure */
884 if (submatches < PCRE_ERROR_NOMATCH)
893 * Get memory for the result (must be freed by caller!)
894 * and append terminating null byte.
896 if ((*result = (char *)malloc(newsize + 1)) == NULL)
899 return PCRS_ERR_NOMEM;
903 (*result)[newsize] = '\0';
911 result_offset = *result;
913 for (i = 0; i < matches_found; i++)
915 /* copy the chunk preceding the match */
916 memcpy(result_offset, subject + offset, (size_t)(matches[i].submatch_offset[0] - offset));
917 result_offset += matches[i].submatch_offset[0] - offset;
919 /* For every segment of the substitute.. */
920 for (k = 0; k <= job->substitute->backrefs; k++)
922 /* ...copy its text.. */
923 memcpy(result_offset, job->substitute->text + job->substitute->block_offset[k], job->substitute->block_length[k]);
924 result_offset += job->substitute->block_length[k];
926 /* ..plus, if it's not the last chunk, i.e.: There *is* a backref.. */
927 if (k != job->substitute->backrefs
928 /* ..in legal range.. */
929 && job->substitute->backref[k] < PCRS_MAX_SUBMATCHES + 2
930 /* ..and referencing a real submatch.. */
931 && job->substitute->backref[k] < matches[i].submatches
932 /* ..that is nonempty.. */
933 && matches[i].submatch_length[job->substitute->backref[k]] > 0)
935 /* ..copy the submatch that is ref'd. */
938 subject + matches[i].submatch_offset[job->substitute->backref[k]],
939 matches[i].submatch_length[job->substitute->backref[k]]
941 result_offset += matches[i].submatch_length[job->substitute->backref[k]];
944 offset = matches[i].submatch_offset[0] + (int)matches[i].submatch_length[0];
948 memcpy(result_offset, subject + offset, subject_length - (size_t)offset);
950 *result_length = newsize;
952 return matches_found;
957 #define is_hex_digit(x) ((x) && strchr("0123456789ABCDEF", toupper(x)))
959 /*********************************************************************
961 * Function : is_hex_sequence
963 * Description : Checks the first four characters of a string
964 * and decides if they are a valid hex sequence
968 * 1 : sequence = The string to check
970 * Returns : Non-zero if it's valid sequence, or
973 *********************************************************************/
974 static int is_hex_sequence(const char *sequence)
976 return (sequence[0] == '\\' &&
977 sequence[1] == 'x' &&
978 is_hex_digit(sequence[2]) &&
979 is_hex_digit(sequence[3]));
984 * Functions below this line are only part of the pcrs version
985 * included in Privoxy. If you use any of them you should not
986 * try to dynamically link against external pcrs versions.
989 /*********************************************************************
991 * Function : pcrs_job_is_dynamic
993 * Description : Checks if a job has the "D" (dynamic) option set.
996 * 1 : job = The job to check
998 * Returns : TRUE if the job is indeed dynamic, otherwise
1001 *********************************************************************/
1002 int pcrs_job_is_dynamic (char *job)
1004 const char delimiter = job[1];
1005 const size_t length = strlen(job);
1011 * The shortest valid (but useless)
1012 * dynamic pattern is "s@@@D"
1018 * Everything between the last character
1019 * and the last delimiter is an option ...
1021 for (option = job + length; *option != delimiter; option--)
1026 * ... and if said option is 'D' the job is dynamic.
1036 /*********************************************************************
1038 * Function : pcrs_get_delimiter
1040 * Description : Tries to find a character that is safe to
1041 * be used as a pcrs delimiter for a certain string.
1044 * 1 : string = The string to search in
1046 * Returns : A safe delimiter if one was found, otherwise '\0'.
1048 *********************************************************************/
1049 char pcrs_get_delimiter(const char *string)
1052 * Some characters that are unlikely to
1053 * be part of pcrs replacement strings.
1055 static const char delimiters[] = "><#+*~%^-:;!@";
1056 const char *d = delimiters;
1058 /* Take the first delimiter that isn't part of the string */
1059 while (*d && NULL != strchr(string, *d))
1068 /*********************************************************************
1070 * Function : pcrs_execute_single_command
1072 * Description : Apply single pcrs command to the subject.
1073 * The subject itself is left untouched, memory for the result
1074 * is malloc()ed and it is the caller's responsibility to free
1075 * the result when it's no longer needed.
1078 * 1 : subject = the subject (== original) string
1079 * 2 : pcrs_command = the pcrs command as string (s@foo@bar@)
1080 * 3 : hits = int* for returning the number of modifications
1082 * Returns : NULL in case of errors, otherwise the
1083 * result of the pcrs command.
1085 *********************************************************************/
1086 char *pcrs_execute_single_command(const char *subject, const char *pcrs_command, int *hits)
1089 char *result = NULL;
1093 assert(pcrs_command);
1096 size = strlen(subject);
1098 job = pcrs_compile_command(pcrs_command, hits);
1101 *hits = pcrs_execute(job, subject, size, &result, &size);
1113 static const char warning[] = "... [too long, truncated]";
1114 /*********************************************************************
1116 * Function : pcrs_compile_dynamic_command
1118 * Description : Takes a dynamic pcrs command, fills in the
1119 * values of the variables and compiles it.
1122 * 1 : pcrs_command = The dynamic pcrs command to compile
1123 * 2 : v = NULL terminated array of variables and their values.
1124 * 3 : error = pcrs error code
1126 * Returns : NULL in case of hard errors, otherwise the
1127 * compiled pcrs job.
1129 *********************************************************************/
1130 pcrs_job *pcrs_compile_dynamic_command(char *pcrs_command, const struct pcrs_variable v[], int *error)
1132 char buf[PCRS_BUFFER_SIZE];
1133 const char *original_pcrs_command = pcrs_command;
1134 char *pcrs_command_tmp = NULL;
1135 pcrs_job *job = NULL;
1140 while ((NULL != v->name) && (NULL != pcrs_command))
1142 assert(NULL != v->value);
1144 if (NULL == strstr(pcrs_command, v->name))
1147 * Skip the substitution if the variable
1148 * name isn't part of the pattern.
1154 /* Use pcrs to replace the variable with its value. */
1155 d = pcrs_get_delimiter(v->value);
1158 /* No proper delimiter found */
1159 *error = PCRS_ERR_CMDSYNTAX;
1160 freez(pcrs_command_tmp);
1165 * Variable names are supposed to contain alpha
1166 * numerical characters plus '_' only.
1168 assert(NULL == strchr(v->name, d));
1170 ret = snprintf(buf, sizeof(buf), "s%c\\$%s%c%s%cgT", d, v->name, d, v->value, d);
1172 if (ret >= sizeof(buf))
1175 * Value didn't completely fit into buffer,
1176 * overwrite the end of the substitution text
1177 * with a truncation message and close the pattern
1180 const size_t trailer_size = sizeof(warning) + 3; /* 3 for d + "gT" */
1181 char *trailer_start = buf + sizeof(buf) - trailer_size;
1183 ret = snprintf(trailer_start, trailer_size, "%s%cgT", warning, d);
1184 assert(ret == trailer_size - 1);
1185 assert(sizeof(buf) == strlen(buf) + 1);
1189 pcrs_command_tmp = pcrs_execute_single_command(pcrs_command, buf, error);
1190 if (NULL == pcrs_command_tmp)
1195 if (pcrs_command != original_pcrs_command)
1197 freez(pcrs_command);
1199 pcrs_command = pcrs_command_tmp;
1204 job = pcrs_compile_command(pcrs_command, error);
1205 if (pcrs_command != original_pcrs_command)
1207 freez(pcrs_command);
1212 *error = PCRS_WARN_TRUNCATION;