next up previous contents index
Next: Conditions Up: Substitutions Previous: Substitution patterns   Contents   Index


How to add new substitution patterns

First of all the new substitution pattern must be defined. The choice of the delimiter structure and the member orig must be done carefully to avoid ambiguities. For instance one could choose #[], %[], ![], @() or @[]. Then the new substitution pattern must be put in the array Subst_Pat and the global variable n_subst_pat must be incremented (it contains the number of used substitution patterns) :

    Subst_Pat[n_subst_pat] = My_new_substitution_pattern;
    n_subst_pat++;

Next we have to define the F member of the substitution pattern. It is a fonction

void My_subst_funct(char *inxx, char **outxx, flow_data *flow_interp)

Here inxx is the character string between the two delimiters (after possible other substitutions), the function will produce a character string in outxx[0], and flow_interp is the flow_data structure corresponding to the running thread (cf. 10).

In the function, outxx[0] must be allocated (it will be freed later in the routine that makes substitutions). The reader can look at the functions corresponding to the 5 built-in substitution patterns in interp.c (functions Subst_Pat_act0 to Subst_Pat_act4). Here is an example of a function that would double the input (i.e. for instance a string ABCD would be replaced by ABCDABCD) :

void My_subst_funct(char *inxx, char **outxx, flow_data *flow_interp)
{
    if (inxx[0] == 0)
        outxx[0] = NULL;
    else {
        outx[0] = (char *) calloc(sizeof(char), 1 + strlen(inxx) * 2);
        strcpy(outxx[0], inxx);
        strcat(outxx[0], inxx);
    }
}

The new substitution rule will be applied only to programs that are loaded after the addition of the new substitution pattern. If it must be applied to all programs it is also possible to modify the function default_Subst_Pat in interp.c where the default substitution patterns are defined.


next up previous contents index
Next: Conditions Up: Substitutions Previous: Substitution patterns   Contents   Index
2009-11-12