示例#1
0
/*fork process and calls re_execute to exec the args*/
void redirection(char **argv)
{
	char *filename;
	int status;	
	int t_argc=0;
	pid_t pid;
	parse_r_argv(&r_argv,argv,&t_argc);
	filename = get_file(argv);
	
	pid = fork();
	switch (pid)
	{
		case -1:printf("\nswitch error\n");
			exit(20);
			break;
		case 0:	
			re_execute(r_argv,filename,argv);
			break;
		default:
			while(pid!=wait(&status));
			pr_exit(status);
			break;
	}

	free(filename);

}
示例#2
0
/**
 *  Determine the portion of the original file name to preserve.
 *
 *  If an error occurs in this function it will be appended to the log and
 *  error mail messages, and the process status will be set appropriately.
 *
 *  @param  ds_id      datastream ID
 *  @param  file_name  name of the raw data file
 *
 *  @retval  1  if successful
 *  @retval  0  if a pattern matching error occurred
 */
int dsproc_set_preserve_dots_from_name(int ds_id, const char *file_name)
{
    DataStream *ds = _DSProc->datastreams[ds_id];

    char        pattern[512];
    regex_t     preg;
    regmatch_t  pmatch[1];
    const char *strp;
    int         status;
    int         ndots;

    sprintf(pattern, "^%s\\.[[:digit:]]{8}\\.[[:digit:]]{6}\\.[[:alpha:]]+\\.",
        ds->name);

    if (!re_compile(&preg, pattern, REG_EXTENDED)) {

        DSPROC_ERROR( NULL,
            "Could not compile regular expression: '%s'",
            pattern);

        return(0);
    }

    status = re_execute(&preg, file_name, 1, pmatch, 0);
    if (status < 0) {

        DSPROC_ERROR( NULL,
            "Could not execute regular expression: '%s'",
            pattern);

        re_free(&preg);
        return(0);
    }

    strp = file_name;

    if (status == 1) {
         strp += pmatch[0].rm_eo;
    }

    ndots = 1;

    while ((strp = strchr(strp, '.'))) {
        ++ndots;
        ++strp;
    }

    re_free(&preg);

    DEBUG_LV1( DSPROC_LIB_NAME,
        "%s: Setting rename preserve dots value to: %d\n",
        ds->name, ndots);

    ds->preserve_dots = ndots;

    return(1);
}
示例#3
0
/**
 *  Compare a string with a list of regular expressions.
 *
 *  For the outputs that are not NULL, this function will return the results
 *  for the first regular expression that matches the specified string. The
 *  returned pointers to the pmatch and substrings arrays will be valid until
 *  the next call to relist_execute() or relist_free().
 *
 *  The pmatch and substrings arrays will have an entry for every parenthesised
 *  subexpression for the pattern that was matched (starting at index 1).
 *  Entries at index 0 correspond to the entire regular expression. For
 *  subexpressions that were not matched, the offsets in the pmatch entry will
 *  be -1 and the substrings entry will be NULL.
 *
 *  See the regexec man page for more detailed descriptions of the execute
 *  flags and output pmatch array.
 *
 *  Error messages from this function are sent to the message handler
 *  (see msngr_init_log() and msngr_init_mail()).
 *
 *  @param  re_list    - pointer to the regular expressions list
 *  @param  string     - string to compare with the regular expression
 *  @param  eflags     - execute flags
 *  @param  mindex     - output: index of the pattern that was matched
 *  @param  nsubs      - output: number of parenthesised subexpressions
 *  @param  pmatch     - output: pointer to array of substring offsets
 *  @param  substrings - output: pointer to array of substrings
 *
 *  @return
 *    -  1 if match
 *    -  0 if no match
 *    - -1 if an error occurred
 */
int relist_execute(
    REList       *re_list,
    const char   *string,
    int           eflags,
    int          *mindex,
    size_t       *nsubs,
    regmatch_t  **pmatch,
    char       ***substrings)
{
    size_t      max_nsubs;
    size_t      max_nmatch;
    regex_t    *preg;
    regmatch_t *offsets;
    int         status;
    int         ri;

    /* Initialize outputs */

    if (mindex)     *mindex     = -1;
    if (nsubs)      *nsubs      =  0;
    if (pmatch)     *pmatch     =  (regmatch_t *)NULL;
    if (substrings) *substrings =  (char **)NULL;

    /* Free results from previous match */

    __relist_free_results(re_list);

    /* Set string and eflags */

    re_list->eflags = eflags;
    re_list->string = strdup(string);
    if (!re_list->string) {
        goto MEMORY_ERROR;
    }

    /* Determine the maximum number of parenthesised subexpressions */

    max_nsubs = 0;

    for (ri = 0; ri < re_list->nregs; ri++) {

        if (re_list->cflags[ri] & REG_NOSUB) continue;

        if (max_nsubs < re_list->regs[ri]->re_nsub) {
            max_nsubs = re_list->regs[ri]->re_nsub;
        }
    }

    /* Create array to store substring offsets */

    max_nmatch = max_nsubs + 1;

    offsets = (regmatch_t *)malloc(max_nmatch * sizeof(regmatch_t));
    if (!offsets) {
        goto MEMORY_ERROR;
    }

    /* Find the first matching regular expression */

    for (ri = 0; ri < re_list->nregs; ri++) {

        preg   = re_list->regs[ri];
        status = re_execute(preg, string, max_nmatch, offsets, eflags);

        if (status >  0) break;
        if (status == 0) continue;
        if (status <  0) {
            free(offsets);
            return(-1);
        }
    }

    if (ri == re_list->nregs) {
        free(offsets);
        return(0);
    }

    /* Set the results in the REList structure */

    re_list->mindex  = ri;
    re_list->offsets = offsets;

    if (re_list->cflags[ri] & REG_NOSUB) {
        re_list->nsubs = 0;
    }
    else {
        re_list->nsubs = re_list->regs[ri]->re_nsub;
    }

    if (substrings) {

        re_list->substrs = re_substrings(
            string, (re_list->nsubs + 1), re_list->offsets);

        if (!re_list->substrs) {
            return(-1);
        }
    }

    /* Set outputs */

    if (mindex)     *mindex     = re_list->mindex;
    if (nsubs)      *nsubs      = re_list->nsubs;
    if (pmatch)     *pmatch     = re_list->offsets;
    if (substrings) *substrings = re_list->substrs;

    return(1);

MEMORY_ERROR:

    ERROR( ARMUTILS_LIB_NAME,
        "Could not compare string to regular expressions list: '%s'\n"
        " -> memory allocation error\n", string);

    return(-1);
}