1 const char pcrs_rcs[] = "$Id: pcrs.c,v 1.46 2014/11/14 10:40:10 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)
87 /* Passed-through PCRE error: */
88 case PCRE_ERROR_NOMEMORY: return "(pcre:) No memory";
90 /* Shouldn't happen unless PCRE or PCRS bug, or user messed with compiled job: */
91 case PCRE_ERROR_NULL: return "(pcre:) NULL code or subject or ovector";
92 case PCRE_ERROR_BADOPTION: return "(pcre:) Unrecognized option bit";
93 case PCRE_ERROR_BADMAGIC: return "(pcre:) Bad magic number in code";
94 case PCRE_ERROR_UNKNOWN_NODE: return "(pcre:) Bad node in pattern";
96 /* Can't happen / not passed: */
97 case PCRE_ERROR_NOSUBSTRING: return "(pcre:) Fire in power supply";
98 case PCRE_ERROR_NOMATCH: return "(pcre:) Water in power supply";
100 #ifdef PCRE_ERROR_MATCHLIMIT
102 * Only reported by PCRE versions newer than our own.
104 case PCRE_ERROR_MATCHLIMIT: return "(pcre:) Match limit reached";
105 #endif /* def PCRE_ERROR_MATCHLIMIT */
108 case PCRS_ERR_NOMEM: return "(pcrs:) No memory";
109 case PCRS_ERR_CMDSYNTAX: return "(pcrs:) Syntax error while parsing command";
110 case PCRS_ERR_STUDY: return "(pcrs:) PCRE error while studying the pattern";
111 case PCRS_ERR_BADJOB: return "(pcrs:) Bad job - NULL job, pattern or substitute";
112 case PCRS_WARN_BADREF: return "(pcrs:) Backreference out of range";
113 case PCRS_WARN_TRUNCATION:
114 return "(pcrs:) At least one variable was too big and has been truncated before compilation";
117 * XXX: With the exception of PCRE_ERROR_MATCHLIMIT we
118 * only catch PCRE errors that can happen with our internal
119 * version. If Privoxy is linked against a newer
120 * PCRE version all bets are off ...
122 default: return "Unknown error. Privoxy out of sync with PCRE?";
125 /* error >= 0: No error */
126 return "(pcrs:) Everything's just fine. Thanks for asking.";
131 /*********************************************************************
133 * Function : pcrs_parse_perl_options
135 * Description : This function parses a string containing the options to
136 * Perl's s/// operator. It returns an integer that is the
137 * pcre equivalent of the symbolic optstring.
138 * Since pcre doesn't know about Perl's 'g' (global) or pcrs',
139 * 'T' (trivial) options but pcrs needs them, the corresponding
140 * flags are set if 'g'or 'T' is encountered.
141 * Note: The 'T' and 'U' options do not conform to Perl.
144 * 1 : optstring = string with options in perl syntax
145 * 2 : flags = see description
147 * Returns : option integer suitable for pcre
149 *********************************************************************/
150 static int pcrs_parse_perl_options(const char *optstring, int *flags)
156 if (NULL == optstring) return 0;
158 for (i = 0; i < strlen(optstring); i++)
162 case 'e': break; /* ToDo ;-) */
163 case 'g': *flags |= PCRS_GLOBAL; break;
164 case 'i': rc |= PCRE_CASELESS; break;
165 case 'm': rc |= PCRE_MULTILINE; break;
167 case 's': rc |= PCRE_DOTALL; break;
168 case 'x': rc |= PCRE_EXTENDED; break;
169 case 'U': rc |= PCRE_UNGREEDY; break;
170 case 'T': *flags |= PCRS_TRIVIAL; break;
179 /*********************************************************************
181 * Function : pcrs_compile_replacement
183 * Description : This function takes a Perl-style replacement (2nd argument
184 * to the s/// operator and returns a compiled pcrs_substitute,
185 * or NULL if memory allocation for the substitute structure
189 * 1 : replacement = replacement part of s/// operator
191 * 2 : trivialflag = Flag that causes backreferences to be
193 * 3 : capturecount = Number of capturing subpatterns in
194 * the pattern. Needed for $+ handling.
195 * 4 : errptr = pointer to an integer in which error
196 * conditions can be returned.
198 * Returns : pcrs_substitute data structure, or NULL if an
199 * error is encountered. In that case, *errptr has
202 *********************************************************************/
203 static pcrs_substitute *pcrs_compile_replacement(const char *replacement, int trivialflag, int capturecount, int *errptr)
210 i = k = l = quoted = 0;
215 if (NULL == replacement)
223 if (NULL == (r = (pcrs_substitute *)malloc(sizeof(pcrs_substitute))))
225 *errptr = PCRS_ERR_NOMEM;
228 memset(r, '\0', sizeof(pcrs_substitute));
230 length = strlen(replacement);
232 if (NULL == (text = (char *)malloc(length + 1)))
235 *errptr = PCRS_ERR_NOMEM;
238 memset(text, '\0', length + 1);
242 * In trivial mode, just copy the substitute text
246 text = strncpy(text, replacement, length + 1);
251 * Else, parse, cut out and record all backreferences
255 while (i < (int)length)
258 if (replacement[i] == '\\')
262 text[k++] = replacement[i++];
267 if (replacement[i+1] && strchr("tnrfae0", replacement[i+1]))
269 switch (replacement[++i])
295 else if (is_hex_sequence(&replacement[i]))
298 * Replace a hex sequence with a single
299 * character with the sequence's ascii value.
300 * e.g.: '\x7e' => '~'
302 const int ascii_value = xtoi(&replacement[i+2]);
304 assert(ascii_value >= 0);
305 assert(ascii_value < 256);
306 text[k++] = (char)ascii_value;
319 if (replacement[i] == '$' && !quoted && i < (int)(length - 1))
321 char *symbol, symbols[] = "'`+&";
322 if (l >= PCRS_MAX_SUBMATCHES)
326 *errptr = PCRS_WARN_BADREF;
329 r->block_length[l] = (size_t)(k - r->block_offset[l]);
331 /* Numerical backreferences */
332 if (isdigit((int)replacement[i + 1]))
334 while (i < (int)length && isdigit((int)replacement[++i]))
336 r->backref[l] = r->backref[l] * 10 + replacement[i] - 48;
338 if (r->backref[l] > capturecount)
342 *errptr = PCRS_WARN_BADREF;
347 /* Symbolic backreferences: */
348 else if (NULL != (symbol = strchr(symbols, replacement[i + 1])))
351 if (symbol - symbols == 2) /* $+ */
353 r->backref[l] = capturecount;
355 else if (symbol - symbols == 3) /* $& */
361 r->backref[l] = (int)(PCRS_MAX_SUBMATCHES + 1 - (symbol - symbols));
366 /* Invalid backref -> plain '$' */
372 /* Valid and in range? -> record */
373 if (0 <= r->backref[l] && r->backref[l] < PCRS_MAX_SUBMATCHES + 2)
375 r->backref_count[r->backref[l]] += 1;
376 r->block_offset[++l] = k;
382 *errptr = PCRS_WARN_BADREF;
389 /* Plain chars are copied */
390 text[k++] = replacement[i++];
393 } /* -END- if (!trivialflag) */
400 r->length = (size_t)k;
401 r->block_length[l] = (size_t)(k - r->block_offset[l]);
408 /*********************************************************************
410 * Function : pcrs_free_job
412 * Description : Frees the memory used by a pcrs_job struct and its
413 * dependent structures.
416 * 1 : job = pointer to the pcrs_job structure to be freed
418 * Returns : a pointer to the next job, if there was any, or
421 *********************************************************************/
422 pcrs_job *pcrs_free_job(pcrs_job *job)
433 if (job->pattern != NULL) free(job->pattern);
434 if (job->hints != NULL) free(job->hints);
435 if (job->substitute != NULL)
437 if (job->substitute->text != NULL) free(job->substitute->text);
438 free(job->substitute);
447 /*********************************************************************
449 * Function : pcrs_free_joblist
451 * Description : Iterates through a chained list of pcrs_job's and
452 * frees them using pcrs_free_job.
455 * 1 : joblist = pointer to the first pcrs_job structure to
460 *********************************************************************/
461 void pcrs_free_joblist(pcrs_job *joblist)
463 while (NULL != (joblist = pcrs_free_job(joblist))) {};
470 /*********************************************************************
472 * Function : pcrs_compile_command
474 * Description : Parses a string with a Perl-style s/// command,
475 * calls pcrs_compile, and returns a corresponding
476 * pcrs_job, or NULL if parsing or compiling the job
480 * 1 : command = string with perl-style s/// command
481 * 2 : errptr = pointer to an integer in which error
482 * conditions can be returned.
484 * Returns : a corresponding pcrs_job data structure, or NULL
485 * if an error was encountered. In that case, *errptr
488 *********************************************************************/
489 pcrs_job *pcrs_compile_command(const char *command, int *errptr)
491 int i, k, l, quoted = FALSE;
500 * Tokenize the perl command
502 limit = strlen(command);
505 *errptr = PCRS_ERR_CMDSYNTAX;
510 delimiter = command[1];
513 tokens[l] = (char *) malloc(limit + 1);
515 for (i = 0; i <= (int)limit; i++)
518 if (command[i] == delimiter && !quoted)
525 tokens[0][k++] = '\0';
526 tokens[++l] = tokens[0] + k;
530 else if (command[i] == '\\' && !quoted)
533 if (command[i+1] == delimiter) continue;
539 tokens[0][k++] = command[i];
547 *errptr = PCRS_ERR_CMDSYNTAX;
552 newjob = pcrs_compile(tokens[1], tokens[2], tokens[3], errptr);
559 /*********************************************************************
561 * Function : pcrs_compile
563 * Description : Takes the three arguments to a perl s/// command
564 * and compiles a pcrs_job structure from them.
567 * 1 : pattern = string with perl-style pattern
568 * 2 : substitute = string with perl-style substitute
569 * 3 : options = string with perl-style options
570 * 4 : errptr = pointer to an integer in which error
571 * conditions can be returned.
573 * Returns : a corresponding pcrs_job data structure, or NULL
574 * if an error was encountered. In that case, *errptr
577 *********************************************************************/
578 pcrs_job *pcrs_compile(const char *pattern, const char *substitute, const char *options, int *errptr)
588 * Handle NULL arguments
590 if (pattern == NULL) pattern = "";
591 if (substitute == NULL) substitute = "";
595 * Get and init memory
597 if (NULL == (newjob = (pcrs_job *)malloc(sizeof(pcrs_job))))
599 *errptr = PCRS_ERR_NOMEM;
602 memset(newjob, '\0', sizeof(pcrs_job));
606 * Evaluate the options
608 newjob->options = pcrs_parse_perl_options(options, &flags);
609 newjob->flags = flags;
613 * Compile the pattern
615 newjob->pattern = pcre_compile(pattern, newjob->options, &error, errptr, NULL);
616 if (newjob->pattern == NULL)
618 pcrs_free_job(newjob);
624 * Generate hints. This has little overhead, since the
625 * hints will be NULL for a boring pattern anyway.
627 newjob->hints = pcre_study(newjob->pattern, 0, &error);
630 *errptr = PCRS_ERR_STUDY;
631 pcrs_free_job(newjob);
637 * Determine the number of capturing subpatterns.
638 * This is needed for handling $+ in the substitute.
640 if (0 > (*errptr = pcre_fullinfo(newjob->pattern, newjob->hints, PCRE_INFO_CAPTURECOUNT, &capturecount)))
642 pcrs_free_job(newjob);
648 * Compile the substitute
650 if (NULL == (newjob->substitute = pcrs_compile_replacement(substitute, newjob->flags & PCRS_TRIVIAL, capturecount, errptr)))
652 pcrs_free_job(newjob);
661 /*********************************************************************
663 * Function : pcrs_execute_list
665 * Description : This is a multiple job wrapper for pcrs_execute().
666 * Apply the regular substitutions defined by the jobs in
667 * the joblist to the subject.
668 * The subject itself is left untouched, memory for the result
669 * is malloc()ed and it is the caller's responsibility to free
670 * the result when it's no longer needed.
672 * Note: For convenient string handling, a null byte is
673 * appended to the result. It does not count towards the
674 * result_length, though.
678 * 1 : joblist = the chained list of pcrs_jobs to be executed
679 * 2 : subject = the subject string
680 * 3 : subject_length = the subject's length
681 * 4 : result = char** for returning the result
682 * 5 : result_length = size_t* for returning the result's length
684 * Returns : On success, the number of substitutions that were made.
685 * May be > 1 if job->flags contained PCRS_GLOBAL
686 * On failure, the (negative) pcre error code describing the
687 * failure, which may be translated to text using pcrs_strerror().
689 *********************************************************************/
690 int pcrs_execute_list(pcrs_job *joblist, char *subject, size_t subject_length, char **result, size_t *result_length)
693 char *old, *new = NULL;
694 int hits, total_hits;
697 *result_length = subject_length;
700 for (job = joblist; job != NULL; job = job->next)
702 hits = pcrs_execute(job, old, *result_length, &new, result_length);
704 if (old != subject) free(old);
723 /*********************************************************************
725 * Function : pcrs_execute
727 * Description : Apply the regular substitution defined by the job to the
729 * The subject itself is left untouched, memory for the result
730 * is malloc()ed and it is the caller's responsibility to free
731 * the result when it's no longer needed.
733 * Note: For convenient string handling, a null byte is
734 * appended to the result. It does not count towards the
735 * result_length, though.
738 * 1 : job = the pcrs_job to be executed
739 * 2 : subject = the subject (== original) string
740 * 3 : subject_length = the subject's length
741 * 4 : result = char** for returning the result (NULL on error)
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(pcrs_job *job, const char *subject, size_t subject_length, char **result, size_t *result_length)
752 int offsets[3 * PCRS_MAX_SUBMATCHES],
757 max_matches = PCRS_MAX_MATCH_INIT;
759 pcrs_match *matches, *dummy;
766 * Sanity check & memory allocation
768 if (job == NULL || job->pattern == NULL || job->substitute == NULL || NULL == subject)
770 return(PCRS_ERR_BADJOB);
773 if (NULL == (matches = (pcrs_match *)malloc((size_t)max_matches * sizeof(pcrs_match))))
775 return(PCRS_ERR_NOMEM);
777 memset(matches, '\0', (size_t)max_matches * sizeof(pcrs_match));
781 * Find the pattern and calculate the space
782 * requirements for the result
784 newsize = subject_length;
786 while ((submatches = pcre_exec(job->pattern, job->hints, subject, (int)subject_length, offset, 0, offsets, 3 * PCRS_MAX_SUBMATCHES)) > 0)
788 job->flags |= PCRS_SUCCESS;
789 matches[i].submatches = submatches;
791 for (k = 0; k < submatches; k++)
793 matches[i].submatch_offset[k] = offsets[2 * k];
795 /* Note: Non-found optional submatches have length -1-(-1)==0 */
796 matches[i].submatch_length[k] = (size_t)(offsets[2 * k + 1] - offsets[2 * k]);
798 /* reserve mem for each submatch as often as it is ref'd */
799 newsize += matches[i].submatch_length[k] * (size_t)job->substitute->backref_count[k];
801 /* plus replacement text size minus match text size */
802 newsize += job->substitute->length - matches[i].submatch_length[0];
804 /* chunk before match */
805 matches[i].submatch_offset[PCRS_MAX_SUBMATCHES] = 0;
806 matches[i].submatch_length[PCRS_MAX_SUBMATCHES] = (size_t)offsets[0];
807 newsize += (size_t)offsets[0] * (size_t)job->substitute->backref_count[PCRS_MAX_SUBMATCHES];
809 /* chunk after match */
810 matches[i].submatch_offset[PCRS_MAX_SUBMATCHES + 1] = offsets[1];
811 matches[i].submatch_length[PCRS_MAX_SUBMATCHES + 1] = subject_length - (size_t)offsets[1] - 1;
812 newsize += (subject_length - (size_t)offsets[1]) * (size_t)job->substitute->backref_count[PCRS_MAX_SUBMATCHES + 1];
814 /* Storage for matches exhausted? -> Extend! */
815 if (++i >= max_matches)
817 max_matches = (int)(max_matches * PCRS_MAX_MATCH_GROW);
818 if (NULL == (dummy = (pcrs_match *)realloc(matches, (size_t)max_matches * sizeof(pcrs_match))))
821 return(PCRS_ERR_NOMEM);
826 /* Non-global search or limit reached? */
827 if (!(job->flags & PCRS_GLOBAL)) break;
829 /* Don't loop on empty matches */
830 if (offsets[1] == offset)
831 if ((size_t)offset < subject_length)
835 /* Go find the next one */
839 /* Pass pcre error through if (bad) failure */
840 if (submatches < PCRE_ERROR_NOMATCH)
849 * Get memory for the result (must be freed by caller!)
850 * and append terminating null byte.
852 if ((*result = (char *)malloc(newsize + 1)) == NULL)
855 return PCRS_ERR_NOMEM;
859 (*result)[newsize] = '\0';
867 result_offset = *result;
869 for (i = 0; i < matches_found; i++)
871 /* copy the chunk preceding the match */
872 memcpy(result_offset, subject + offset, (size_t)(matches[i].submatch_offset[0] - offset));
873 result_offset += matches[i].submatch_offset[0] - offset;
875 /* For every segment of the substitute.. */
876 for (k = 0; k <= job->substitute->backrefs; k++)
878 /* ...copy its text.. */
879 memcpy(result_offset, job->substitute->text + job->substitute->block_offset[k], job->substitute->block_length[k]);
880 result_offset += job->substitute->block_length[k];
882 /* ..plus, if it's not the last chunk, i.e.: There *is* a backref.. */
883 if (k != job->substitute->backrefs
884 /* ..in legal range.. */
885 && job->substitute->backref[k] < PCRS_MAX_SUBMATCHES + 2
886 /* ..and referencing a real submatch.. */
887 && job->substitute->backref[k] < matches[i].submatches
888 /* ..that is nonempty.. */
889 && matches[i].submatch_length[job->substitute->backref[k]] > 0)
891 /* ..copy the submatch that is ref'd. */
894 subject + matches[i].submatch_offset[job->substitute->backref[k]],
895 matches[i].submatch_length[job->substitute->backref[k]]
897 result_offset += matches[i].submatch_length[job->substitute->backref[k]];
900 offset = matches[i].submatch_offset[0] + (int)matches[i].submatch_length[0];
904 memcpy(result_offset, subject + offset, subject_length - (size_t)offset);
906 *result_length = newsize;
908 return matches_found;
913 #define is_hex_digit(x) ((x) && strchr("0123456789ABCDEF", toupper(x)))
915 /*********************************************************************
917 * Function : is_hex_sequence
919 * Description : Checks the first four characters of a string
920 * and decides if they are a valid hex sequence
924 * 1 : sequence = The string to check
926 * Returns : Non-zero if it's valid sequence, or
929 *********************************************************************/
930 static int is_hex_sequence(const char *sequence)
932 return (sequence[0] == '\\' &&
933 sequence[1] == 'x' &&
934 is_hex_digit(sequence[2]) &&
935 is_hex_digit(sequence[3]));
940 * Functions below this line are only part of the pcrs version
941 * included in Privoxy. If you use any of them you should not
942 * try to dynamically link against external pcrs versions.
945 /*********************************************************************
947 * Function : pcrs_job_is_dynamic
949 * Description : Checks if a job has the "D" (dynamic) option set.
952 * 1 : job = The job to check
954 * Returns : TRUE if the job is indeed dynamic, otherwise
957 *********************************************************************/
958 int pcrs_job_is_dynamic (char *job)
960 const char delimiter = job[1];
961 const size_t length = strlen(job);
967 * The shortest valid (but useless)
968 * dynamic pattern is "s@@@D"
974 * Everything between the last character
975 * and the last delimiter is an option ...
977 for (option = job + length; *option != delimiter; option--)
982 * ... and if said option is 'D' the job is dynamic.
992 /*********************************************************************
994 * Function : pcrs_get_delimiter
996 * Description : Tries to find a character that is safe to
997 * be used as a pcrs delimiter for a certain string.
1000 * 1 : string = The string to search in
1002 * Returns : A safe delimiter if one was found, otherwise '\0'.
1004 *********************************************************************/
1005 char pcrs_get_delimiter(const char *string)
1008 * Some characters that are unlikely to
1009 * be part of pcrs replacement strings.
1011 static const char delimiters[] = "><#+*~%^-:;!@";
1012 const char *d = delimiters;
1014 /* Take the first delimiter that isn't part of the string */
1015 while (*d && NULL != strchr(string, *d))
1024 /*********************************************************************
1026 * Function : pcrs_execute_single_command
1028 * Description : Apply single pcrs command to the subject.
1029 * The subject itself is left untouched, memory for the result
1030 * is malloc()ed and it is the caller's responsibility to free
1031 * the result when it's no longer needed.
1034 * 1 : subject = the subject (== original) string
1035 * 2 : pcrs_command = the pcrs command as string (s@foo@bar@)
1036 * 3 : hits = int* for returning the number of modifications
1038 * Returns : NULL in case of errors, otherwise the
1039 * result of the pcrs command.
1041 *********************************************************************/
1042 char *pcrs_execute_single_command(const char *subject, const char *pcrs_command, int *hits)
1045 char *result = NULL;
1049 assert(pcrs_command);
1052 size = strlen(subject);
1054 job = pcrs_compile_command(pcrs_command, hits);
1057 *hits = pcrs_execute(job, subject, size, &result, &size);
1069 static const char warning[] = "... [too long, truncated]";
1070 /*********************************************************************
1072 * Function : pcrs_compile_dynamic_command
1074 * Description : Takes a dynamic pcrs command, fills in the
1075 * values of the variables and compiles it.
1078 * 1 : pcrs_command = The dynamic pcrs command to compile
1079 * 2 : v = NULL terminated array of variables and their values.
1080 * 3 : error = pcrs error code
1082 * Returns : NULL in case of hard errors, otherwise the
1083 * compiled pcrs job.
1085 *********************************************************************/
1086 pcrs_job *pcrs_compile_dynamic_command(char *pcrs_command, const struct pcrs_variable v[], int *error)
1088 char buf[PCRS_BUFFER_SIZE];
1089 const char *original_pcrs_command = pcrs_command;
1090 char *pcrs_command_tmp = NULL;
1091 pcrs_job *job = NULL;
1096 while ((NULL != v->name) && (NULL != pcrs_command))
1098 assert(NULL != v->value);
1100 if (NULL == strstr(pcrs_command, v->name))
1103 * Skip the substitution if the variable
1104 * name isn't part of the pattern.
1110 /* Use pcrs to replace the variable with its value. */
1111 d = pcrs_get_delimiter(v->value);
1114 /* No proper delimiter found */
1115 *error = PCRS_ERR_CMDSYNTAX;
1116 freez(pcrs_command_tmp);
1121 * Variable names are supposed to contain alpha
1122 * numerical characters plus '_' only.
1124 assert(NULL == strchr(v->name, d));
1126 ret = snprintf(buf, sizeof(buf), "s%c\\$%s%c%s%cgT", d, v->name, d, v->value, d);
1128 if (ret >= sizeof(buf))
1131 * Value didn't completely fit into buffer,
1132 * overwrite the end of the substitution text
1133 * with a truncation message and close the pattern
1136 const size_t trailer_size = sizeof(warning) + 3; /* 3 for d + "gT" */
1137 char *trailer_start = buf + sizeof(buf) - trailer_size;
1139 ret = snprintf(trailer_start, trailer_size, "%s%cgT", warning, d);
1140 assert(ret == trailer_size - 1);
1141 assert(sizeof(buf) == strlen(buf) + 1);
1145 pcrs_command_tmp = pcrs_execute_single_command(pcrs_command, buf, error);
1146 if (NULL == pcrs_command_tmp)
1151 if (pcrs_command != original_pcrs_command)
1153 freez(pcrs_command);
1155 pcrs_command = pcrs_command_tmp;
1160 job = pcrs_compile_command(pcrs_command, error);
1161 if (pcrs_command != original_pcrs_command)
1163 freez(pcrs_command);
1168 *error = PCRS_WARN_TRUNCATION;