1 const char pcrs_rcs[] = "$Id: pcrs.c,v 1.48 2015/12/27 12:45:46 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;
185 /*********************************************************************
187 * Function : pcrs_compile_replacement
189 * Description : This function takes a Perl-style replacement (2nd argument
190 * to the s/// operator and returns a compiled pcrs_substitute,
191 * or NULL if memory allocation for the substitute structure
195 * 1 : replacement = replacement part of s/// operator
197 * 2 : trivialflag = Flag that causes backreferences to be
199 * 3 : capturecount = Number of capturing subpatterns in
200 * the pattern. Needed for $+ handling.
201 * 4 : errptr = pointer to an integer in which error
202 * conditions can be returned.
204 * Returns : pcrs_substitute data structure, or NULL if an
205 * error is encountered. In that case, *errptr has
208 *********************************************************************/
209 static pcrs_substitute *pcrs_compile_replacement(const char *replacement, int trivialflag, int capturecount, int *errptr)
216 i = k = l = quoted = 0;
221 if (NULL == replacement)
229 if (NULL == (r = (pcrs_substitute *)malloc(sizeof(pcrs_substitute))))
231 *errptr = PCRS_ERR_NOMEM;
234 memset(r, '\0', sizeof(pcrs_substitute));
236 length = strlen(replacement);
238 if (NULL == (text = (char *)malloc(length + 1)))
241 *errptr = PCRS_ERR_NOMEM;
244 memset(text, '\0', length + 1);
248 * In trivial mode, just copy the substitute text
252 text = strncpy(text, replacement, length + 1);
257 * Else, parse, cut out and record all backreferences
261 while (i < (int)length)
264 if (replacement[i] == '\\')
268 text[k++] = replacement[i++];
273 if (replacement[i+1] && strchr("tnrfae0", replacement[i+1]))
275 switch (replacement[++i])
301 else if (is_hex_sequence(&replacement[i]))
304 * Replace a hex sequence with a single
305 * character with the sequence's ascii value.
306 * e.g.: '\x7e' => '~'
308 const int ascii_value = xtoi(&replacement[i+2]);
310 assert(ascii_value >= 0);
311 assert(ascii_value < 256);
312 text[k++] = (char)ascii_value;
325 if (replacement[i] == '$' && !quoted && i < (int)(length - 1))
327 char *symbol, symbols[] = "'`+&";
328 if (l >= PCRS_MAX_SUBMATCHES)
332 *errptr = PCRS_WARN_BADREF;
335 r->block_length[l] = (size_t)(k - r->block_offset[l]);
337 /* Numerical backreferences */
338 if (isdigit((int)replacement[i + 1]))
340 while (i < (int)length && isdigit((int)replacement[++i]))
342 r->backref[l] = r->backref[l] * 10 + replacement[i] - 48;
344 if (r->backref[l] > capturecount)
348 *errptr = PCRS_WARN_BADREF;
353 /* Symbolic backreferences: */
354 else if (NULL != (symbol = strchr(symbols, replacement[i + 1])))
357 if (symbol - symbols == 2) /* $+ */
359 r->backref[l] = capturecount;
361 else if (symbol - symbols == 3) /* $& */
367 r->backref[l] = (int)(PCRS_MAX_SUBMATCHES + 1 - (symbol - symbols));
372 /* Invalid backref -> plain '$' */
378 /* Valid and in range? -> record */
379 if ((0 <= r->backref[l]) &&
380 (r->backref[l] < PCRS_MAX_SUBMATCHES + 2) &&
381 (l < PCRS_MAX_SUBMATCHES - 1))
383 r->backref_count[r->backref[l]] += 1;
384 r->block_offset[++l] = k;
390 *errptr = PCRS_WARN_BADREF;
397 /* Plain chars are copied */
398 text[k++] = replacement[i++];
401 } /* -END- if (!trivialflag) */
408 r->length = (size_t)k;
409 r->block_length[l] = (size_t)(k - r->block_offset[l]);
416 /*********************************************************************
418 * Function : pcrs_free_job
420 * Description : Frees the memory used by a pcrs_job struct and its
421 * dependent structures.
424 * 1 : job = pointer to the pcrs_job structure to be freed
426 * Returns : a pointer to the next job, if there was any, or
429 *********************************************************************/
430 pcrs_job *pcrs_free_job(pcrs_job *job)
441 if (job->pattern != NULL) free(job->pattern);
442 if (job->hints != NULL) free(job->hints);
443 if (job->substitute != NULL)
445 if (job->substitute->text != NULL) free(job->substitute->text);
446 free(job->substitute);
455 /*********************************************************************
457 * Function : pcrs_free_joblist
459 * Description : Iterates through a chained list of pcrs_job's and
460 * frees them using pcrs_free_job.
463 * 1 : joblist = pointer to the first pcrs_job structure to
468 *********************************************************************/
469 void pcrs_free_joblist(pcrs_job *joblist)
471 while (NULL != (joblist = pcrs_free_job(joblist))) {};
478 /*********************************************************************
480 * Function : pcrs_compile_command
482 * Description : Parses a string with a Perl-style s/// command,
483 * calls pcrs_compile, and returns a corresponding
484 * pcrs_job, or NULL if parsing or compiling the job
488 * 1 : command = string with perl-style s/// command
489 * 2 : errptr = pointer to an integer in which error
490 * conditions can be returned.
492 * Returns : a corresponding pcrs_job data structure, or NULL
493 * if an error was encountered. In that case, *errptr
496 *********************************************************************/
497 pcrs_job *pcrs_compile_command(const char *command, int *errptr)
499 int i, k, l, quoted = FALSE;
508 * Tokenize the perl command
510 limit = strlen(command);
513 *errptr = PCRS_ERR_CMDSYNTAX;
518 delimiter = command[1];
521 tokens[l] = (char *) malloc(limit + 1);
523 for (i = 0; i <= (int)limit; i++)
526 if (command[i] == delimiter && !quoted)
533 tokens[0][k++] = '\0';
534 tokens[++l] = tokens[0] + k;
538 else if (command[i] == '\\' && !quoted)
541 if (command[i+1] == delimiter) continue;
547 tokens[0][k++] = command[i];
555 *errptr = PCRS_ERR_CMDSYNTAX;
560 newjob = pcrs_compile(tokens[1], tokens[2], tokens[3], errptr);
567 /*********************************************************************
569 * Function : pcrs_compile
571 * Description : Takes the three arguments to a perl s/// command
572 * and compiles a pcrs_job structure from them.
575 * 1 : pattern = string with perl-style pattern
576 * 2 : substitute = string with perl-style substitute
577 * 3 : options = string with perl-style options
578 * 4 : errptr = pointer to an integer in which error
579 * conditions can be returned.
581 * Returns : a corresponding pcrs_job data structure, or NULL
582 * if an error was encountered. In that case, *errptr
585 *********************************************************************/
586 pcrs_job *pcrs_compile(const char *pattern, const char *substitute, const char *options, int *errptr)
596 * Handle NULL arguments
598 if (pattern == NULL) pattern = "";
599 if (substitute == NULL) substitute = "";
603 * Get and init memory
605 if (NULL == (newjob = (pcrs_job *)malloc(sizeof(pcrs_job))))
607 *errptr = PCRS_ERR_NOMEM;
610 memset(newjob, '\0', sizeof(pcrs_job));
614 * Evaluate the options
616 newjob->options = pcrs_parse_perl_options(options, &flags);
617 newjob->flags = flags;
621 * Compile the pattern
623 newjob->pattern = pcre_compile(pattern, newjob->options, &error, errptr, NULL);
624 if (newjob->pattern == NULL)
626 pcrs_free_job(newjob);
632 * Generate hints. This has little overhead, since the
633 * hints will be NULL for a boring pattern anyway.
635 newjob->hints = pcre_study(newjob->pattern, 0, &error);
638 *errptr = PCRS_ERR_STUDY;
639 pcrs_free_job(newjob);
645 * Determine the number of capturing subpatterns.
646 * This is needed for handling $+ in the substitute.
648 if (0 > (*errptr = pcre_fullinfo(newjob->pattern, newjob->hints, PCRE_INFO_CAPTURECOUNT, &capturecount)))
650 pcrs_free_job(newjob);
656 * Compile the substitute
658 if (NULL == (newjob->substitute = pcrs_compile_replacement(substitute, newjob->flags & PCRS_TRIVIAL, capturecount, errptr)))
660 pcrs_free_job(newjob);
669 /*********************************************************************
671 * Function : pcrs_execute_list
673 * Description : This is a multiple job wrapper for pcrs_execute().
674 * Apply the regular substitutions defined by the jobs in
675 * the joblist to the subject.
676 * The subject itself is left untouched, memory for the result
677 * is malloc()ed and it is the caller's responsibility to free
678 * the result when it's no longer needed.
680 * Note: For convenient string handling, a null byte is
681 * appended to the result. It does not count towards the
682 * result_length, though.
686 * 1 : joblist = the chained list of pcrs_jobs to be executed
687 * 2 : subject = the subject string
688 * 3 : subject_length = the subject's length
689 * 4 : result = char** for returning the result
690 * 5 : result_length = size_t* for returning the result's length
692 * Returns : On success, the number of substitutions that were made.
693 * May be > 1 if job->flags contained PCRS_GLOBAL
694 * On failure, the (negative) pcre error code describing the
695 * failure, which may be translated to text using pcrs_strerror().
697 *********************************************************************/
698 int pcrs_execute_list(pcrs_job *joblist, char *subject, size_t subject_length, char **result, size_t *result_length)
701 char *old, *new = NULL;
702 int hits, total_hits;
705 *result_length = subject_length;
708 for (job = joblist; job != NULL; job = job->next)
710 hits = pcrs_execute(job, old, *result_length, &new, result_length);
712 if (old != subject) free(old);
731 /*********************************************************************
733 * Function : pcrs_execute
735 * Description : Apply the regular substitution defined by the job to the
737 * The subject itself is left untouched, memory for the result
738 * is malloc()ed and it is the caller's responsibility to free
739 * the result when it's no longer needed.
741 * Note: For convenient string handling, a null byte is
742 * appended to the result. It does not count towards the
743 * result_length, though.
746 * 1 : job = the pcrs_job to be executed
747 * 2 : subject = the subject (== original) string
748 * 3 : subject_length = the subject's length
749 * 4 : result = char** for returning the result (NULL on error)
750 * 5 : result_length = size_t* for returning the result's length
752 * Returns : On success, the number of substitutions that were made.
753 * May be > 1 if job->flags contained PCRS_GLOBAL
754 * On failure, the (negative) pcre error code describing the
755 * failure, which may be translated to text using pcrs_strerror().
757 *********************************************************************/
758 int pcrs_execute(pcrs_job *job, const char *subject, size_t subject_length, char **result, size_t *result_length)
760 int offsets[3 * PCRS_MAX_SUBMATCHES],
765 max_matches = PCRS_MAX_MATCH_INIT;
767 pcrs_match *matches, *dummy;
774 * Sanity check & memory allocation
776 if (job == NULL || job->pattern == NULL || job->substitute == NULL || NULL == subject)
778 return(PCRS_ERR_BADJOB);
781 if (NULL == (matches = (pcrs_match *)malloc((size_t)max_matches * sizeof(pcrs_match))))
783 return(PCRS_ERR_NOMEM);
785 memset(matches, '\0', (size_t)max_matches * sizeof(pcrs_match));
789 * Find the pattern and calculate the space
790 * requirements for the result
792 newsize = subject_length;
794 while ((submatches = pcre_exec(job->pattern, job->hints, subject, (int)subject_length, offset, 0, offsets, 3 * PCRS_MAX_SUBMATCHES)) > 0)
796 job->flags |= PCRS_SUCCESS;
797 matches[i].submatches = submatches;
799 for (k = 0; k < submatches; k++)
801 matches[i].submatch_offset[k] = offsets[2 * k];
803 /* Note: Non-found optional submatches have length -1-(-1)==0 */
804 matches[i].submatch_length[k] = (size_t)(offsets[2 * k + 1] - offsets[2 * k]);
806 /* reserve mem for each submatch as often as it is ref'd */
807 newsize += matches[i].submatch_length[k] * (size_t)job->substitute->backref_count[k];
809 /* plus replacement text size minus match text size */
810 newsize += job->substitute->length - matches[i].submatch_length[0];
812 /* chunk before match */
813 matches[i].submatch_offset[PCRS_MAX_SUBMATCHES] = 0;
814 matches[i].submatch_length[PCRS_MAX_SUBMATCHES] = (size_t)offsets[0];
815 newsize += (size_t)offsets[0] * (size_t)job->substitute->backref_count[PCRS_MAX_SUBMATCHES];
817 /* chunk after match */
818 matches[i].submatch_offset[PCRS_MAX_SUBMATCHES + 1] = offsets[1];
819 matches[i].submatch_length[PCRS_MAX_SUBMATCHES + 1] = subject_length - (size_t)offsets[1] - 1;
820 newsize += (subject_length - (size_t)offsets[1]) * (size_t)job->substitute->backref_count[PCRS_MAX_SUBMATCHES + 1];
822 /* Storage for matches exhausted? -> Extend! */
823 if (++i >= max_matches)
825 max_matches = (int)(max_matches * PCRS_MAX_MATCH_GROW);
826 if (NULL == (dummy = (pcrs_match *)realloc(matches, (size_t)max_matches * sizeof(pcrs_match))))
829 return(PCRS_ERR_NOMEM);
834 /* Non-global search or limit reached? */
835 if (!(job->flags & PCRS_GLOBAL)) break;
837 /* Don't loop on empty matches */
838 if (offsets[1] == offset)
839 if ((size_t)offset < subject_length)
843 /* Go find the next one */
847 /* Pass pcre error through if (bad) failure */
848 if (submatches < PCRE_ERROR_NOMATCH)
857 * Get memory for the result (must be freed by caller!)
858 * and append terminating null byte.
860 if ((*result = (char *)malloc(newsize + 1)) == NULL)
863 return PCRS_ERR_NOMEM;
867 (*result)[newsize] = '\0';
875 result_offset = *result;
877 for (i = 0; i < matches_found; i++)
879 /* copy the chunk preceding the match */
880 memcpy(result_offset, subject + offset, (size_t)(matches[i].submatch_offset[0] - offset));
881 result_offset += matches[i].submatch_offset[0] - offset;
883 /* For every segment of the substitute.. */
884 for (k = 0; k <= job->substitute->backrefs; k++)
886 /* ...copy its text.. */
887 memcpy(result_offset, job->substitute->text + job->substitute->block_offset[k], job->substitute->block_length[k]);
888 result_offset += job->substitute->block_length[k];
890 /* ..plus, if it's not the last chunk, i.e.: There *is* a backref.. */
891 if (k != job->substitute->backrefs
892 /* ..in legal range.. */
893 && job->substitute->backref[k] < PCRS_MAX_SUBMATCHES + 2
894 /* ..and referencing a real submatch.. */
895 && job->substitute->backref[k] < matches[i].submatches
896 /* ..that is nonempty.. */
897 && matches[i].submatch_length[job->substitute->backref[k]] > 0)
899 /* ..copy the submatch that is ref'd. */
902 subject + matches[i].submatch_offset[job->substitute->backref[k]],
903 matches[i].submatch_length[job->substitute->backref[k]]
905 result_offset += matches[i].submatch_length[job->substitute->backref[k]];
908 offset = matches[i].submatch_offset[0] + (int)matches[i].submatch_length[0];
912 memcpy(result_offset, subject + offset, subject_length - (size_t)offset);
914 *result_length = newsize;
916 return matches_found;
921 #define is_hex_digit(x) ((x) && strchr("0123456789ABCDEF", toupper(x)))
923 /*********************************************************************
925 * Function : is_hex_sequence
927 * Description : Checks the first four characters of a string
928 * and decides if they are a valid hex sequence
932 * 1 : sequence = The string to check
934 * Returns : Non-zero if it's valid sequence, or
937 *********************************************************************/
938 static int is_hex_sequence(const char *sequence)
940 return (sequence[0] == '\\' &&
941 sequence[1] == 'x' &&
942 is_hex_digit(sequence[2]) &&
943 is_hex_digit(sequence[3]));
948 * Functions below this line are only part of the pcrs version
949 * included in Privoxy. If you use any of them you should not
950 * try to dynamically link against external pcrs versions.
953 /*********************************************************************
955 * Function : pcrs_job_is_dynamic
957 * Description : Checks if a job has the "D" (dynamic) option set.
960 * 1 : job = The job to check
962 * Returns : TRUE if the job is indeed dynamic, otherwise
965 *********************************************************************/
966 int pcrs_job_is_dynamic (char *job)
968 const char delimiter = job[1];
969 const size_t length = strlen(job);
975 * The shortest valid (but useless)
976 * dynamic pattern is "s@@@D"
982 * Everything between the last character
983 * and the last delimiter is an option ...
985 for (option = job + length; *option != delimiter; option--)
990 * ... and if said option is 'D' the job is dynamic.
1000 /*********************************************************************
1002 * Function : pcrs_get_delimiter
1004 * Description : Tries to find a character that is safe to
1005 * be used as a pcrs delimiter for a certain string.
1008 * 1 : string = The string to search in
1010 * Returns : A safe delimiter if one was found, otherwise '\0'.
1012 *********************************************************************/
1013 char pcrs_get_delimiter(const char *string)
1016 * Some characters that are unlikely to
1017 * be part of pcrs replacement strings.
1019 static const char delimiters[] = "><#+*~%^-:;!@";
1020 const char *d = delimiters;
1022 /* Take the first delimiter that isn't part of the string */
1023 while (*d && NULL != strchr(string, *d))
1032 /*********************************************************************
1034 * Function : pcrs_execute_single_command
1036 * Description : Apply single pcrs command to the subject.
1037 * The subject itself is left untouched, memory for the result
1038 * is malloc()ed and it is the caller's responsibility to free
1039 * the result when it's no longer needed.
1042 * 1 : subject = the subject (== original) string
1043 * 2 : pcrs_command = the pcrs command as string (s@foo@bar@)
1044 * 3 : hits = int* for returning the number of modifications
1046 * Returns : NULL in case of errors, otherwise the
1047 * result of the pcrs command.
1049 *********************************************************************/
1050 char *pcrs_execute_single_command(const char *subject, const char *pcrs_command, int *hits)
1053 char *result = NULL;
1057 assert(pcrs_command);
1060 size = strlen(subject);
1062 job = pcrs_compile_command(pcrs_command, hits);
1065 *hits = pcrs_execute(job, subject, size, &result, &size);
1077 static const char warning[] = "... [too long, truncated]";
1078 /*********************************************************************
1080 * Function : pcrs_compile_dynamic_command
1082 * Description : Takes a dynamic pcrs command, fills in the
1083 * values of the variables and compiles it.
1086 * 1 : pcrs_command = The dynamic pcrs command to compile
1087 * 2 : v = NULL terminated array of variables and their values.
1088 * 3 : error = pcrs error code
1090 * Returns : NULL in case of hard errors, otherwise the
1091 * compiled pcrs job.
1093 *********************************************************************/
1094 pcrs_job *pcrs_compile_dynamic_command(char *pcrs_command, const struct pcrs_variable v[], int *error)
1096 char buf[PCRS_BUFFER_SIZE];
1097 const char *original_pcrs_command = pcrs_command;
1098 char *pcrs_command_tmp = NULL;
1099 pcrs_job *job = NULL;
1104 while ((NULL != v->name) && (NULL != pcrs_command))
1106 assert(NULL != v->value);
1108 if (NULL == strstr(pcrs_command, v->name))
1111 * Skip the substitution if the variable
1112 * name isn't part of the pattern.
1118 /* Use pcrs to replace the variable with its value. */
1119 d = pcrs_get_delimiter(v->value);
1122 /* No proper delimiter found */
1123 *error = PCRS_ERR_CMDSYNTAX;
1124 freez(pcrs_command_tmp);
1129 * Variable names are supposed to contain alpha
1130 * numerical characters plus '_' only.
1132 assert(NULL == strchr(v->name, d));
1134 ret = snprintf(buf, sizeof(buf), "s%c\\$%s%c%s%cgT", d, v->name, d, v->value, d);
1136 if (ret >= sizeof(buf))
1139 * Value didn't completely fit into buffer,
1140 * overwrite the end of the substitution text
1141 * with a truncation message and close the pattern
1144 const size_t trailer_size = sizeof(warning) + 3; /* 3 for d + "gT" */
1145 char *trailer_start = buf + sizeof(buf) - trailer_size;
1147 ret = snprintf(trailer_start, trailer_size, "%s%cgT", warning, d);
1148 assert(ret == trailer_size - 1);
1149 assert(sizeof(buf) == strlen(buf) + 1);
1153 pcrs_command_tmp = pcrs_execute_single_command(pcrs_command, buf, error);
1154 if (NULL == pcrs_command_tmp)
1159 if (pcrs_command != original_pcrs_command)
1161 freez(pcrs_command);
1163 pcrs_command = pcrs_command_tmp;
1168 job = pcrs_compile_command(pcrs_command, error);
1169 if (pcrs_command != original_pcrs_command)
1171 freez(pcrs_command);
1176 *error = PCRS_WARN_TRUNCATION;