예제 #1
0
파일: sed3.c 프로젝트: ISLEcode/kornshell
int
reexec(regex_t* re, char* s, size_t n, size_t nmatch, regmatch_t* match, int flags)
{
	int code;
	if((code = regnexec(re, s, n, nmatch, match, flags)) && code != REG_NOMATCH)
		reerror(re, code);
	return code;
}
예제 #2
0
파일: regex.c 프로젝트: 8l/xedit
/*
 * Implementation
 */
static re_cod *
LispRecomp(LispBuiltin *builtin, char *pattern, int cflags)
{
    int code;
    re_cod *regex = LispMalloc(sizeof(re_cod));

    if ((code = recomp(regex, pattern, cflags)) != 0) {
	char buffer[256];

	reerror(code, regex, buffer, sizeof(buffer));
	refree(regex);
	LispFree(regex);
	LispDestroy("%s: recomp(\"%s\"): %s", STRFUN(builtin), pattern, buffer);
    }

    return (regex);
}
예제 #3
0
파일: sed3.c 프로젝트: ISLEcode/kornshell
int
substitute(regex_t *re, Text *data)
{
	word n;
	int c;
	regmatch_t matches[100];
	if(reexec(re, (char*)data->s, data->w - data->s, elementsof(matches), matches, 0))
		return 0;
	if(c = regsubexec(re, (char*)data->s, elementsof(matches), matches)) {
		reerror(re, c);
		return 0;
	}
	n = re->re_sub->re_len;
	assure(data, n+1);
	memcpy(data->s, re->re_sub->re_buf, n+1);
	data->w = data->s + n;
	return 1;
}
예제 #4
0
int
main(int argc, char *argv[])
{
    re_cod cod;
    re_mat mat[10];
    int line, ecode, i, len, group, failed;
    long eo, so;
    char buf[8192];
    char str[8192];
    FILE *fp = fopen("tests.txt", "r");

    if (fp == NULL) {
	fprintf(stderr, "failed to open tests.txt\n");
	exit(1);
    }

    ecode = line = group = failed = 0;
    cod.cod = NULL;
    while (fgets(buf, sizeof(buf), fp)) {
	++line;
	if (buf[0] == '#' || buf[0] == '\n')
	    continue;
	else if (buf[0] == '/') {
	    char *ptr = strrchr(buf, '/');

	    if (ptr == buf) {
		fprintf(stderr, "syntax error at line %d\n", line);
		break;
	    }
	    else {
		int flags = 0;

		refree(&cod);
		for (*ptr++ = '\0'; *ptr; ptr++) {
		    if (*ptr == 'i')
			flags |= RE_ICASE;
		    else if (*ptr == 'n')
			flags |= RE_NEWLINE;
		}
		ecode = recomp(&cod, buf + 1, flags);
		failed = ecode;
	    }
	}
	else if (buf[0] == '>') {
	    if (cod.cod == NULL) {
		fprintf(stderr, "no previous pattern at line %d\n", line);
		break;
	    }
	    len = strlen(buf) - 1;
	    buf[len] = '\0';
	    strcpy(str, buf + 1);
	    for (i = 0, --len; i < len - 1; i++) {
		if (str[i] == '\\') {
		    memmove(str + i, str + i + 1, len);
		    --len;
		    switch (str[i]) {
			case 'a':
			    str[i] = '\a';
			    break;
			case 'b':
			    str[i] = '\b';
			    break;
			case 'f':
			    str[i] = '\f';
			    break;
			case 'n':
			    str[i] = '\n';
			    break;
			case 'r':
			    str[i] = '\r';
			    break;
			case 't':
			    str[i] = '\t';
			    break;
			case 'v':
			    str[i] = '\v';
			    break;
			default:
			    break;
		    }
		}
	    }
	    group = 0;
	    ecode = reexec(&cod, str, 10, &mat[0], 0);
	    if (ecode && ecode != RE_NOMATCH) {
		reerror(failed, &cod, buf, sizeof(buf));
		fprintf(stderr, "%s, at line %d\n", buf, line);
		break;
	    }
	}
	else if (buf[0] == ':') {
	    if (failed) {
		len = strlen(buf) - 1;
		buf[len] = '\0';
		if (failed == RE_EESCAPE && strcmp(buf, ":EESCAPE") == 0)
		    continue;
		if (failed == RE_ESUBREG && strcmp(buf, ":ESUBREG") == 0)
		    continue;
		if (failed == RE_EBRACK && strcmp(buf, ":EBRACK") == 0)
		    continue;
		if (failed == RE_EPAREN && strcmp(buf, ":EPAREN") == 0)
		    continue;
		if (failed == RE_EBRACE && strcmp(buf, ":EBRACE") == 0)
		    continue;
		if (failed == RE_EBADBR && strcmp(buf, ":EBADBR") == 0)
		    continue;
		if (failed == RE_ERANGE && strcmp(buf, ":ERANGE") == 0)
		    continue;
		if (failed == RE_ESPACE && strcmp(buf, ":ESPACE") == 0)
		    continue;
		if (failed == RE_BADRPT && strcmp(buf, ":BADRPT") == 0)
		    continue;
		if (failed == RE_EMPTY && strcmp(buf, ":EMPTY") == 0)
		    continue;
		reerror(failed, &cod, buf, sizeof(buf));
		fprintf(stderr, "Error value %d doesn't match: %s, at line %d\n",
			failed, buf, line);
		break;
	    }
	    else if (!ecode) {
		fprintf(stderr, "found match when shoudn't, at line %d\n", line);
		break;
	    }
	}
	else {
	    if (failed) {
		reerror(failed, &cod, buf, sizeof(buf));
		fprintf(stderr, "%s, at line %d\n", buf, line);
		break;
	    }
	    if (sscanf(buf, "%ld,%ld:", &so, &eo) != 2) {
		fprintf(stderr, "expecting match offsets at line %d\n", line);
		break;
	    }
	    else if (ecode) {
		fprintf(stderr, "didn't match, at line %d\n", line);
		break;
	    }
	    else if (group >= 10) {
		fprintf(stderr, "syntax error at line %d (too many groups)\n",
			line);
		break;
	    }
	    else if (so != mat[group].rm_so || eo != mat[group].rm_eo) {
		fprintf(stderr, "match failed at line %d, got %ld,%ld: ",
			line, mat[group].rm_so, mat[group].rm_eo);
		if (mat[group].rm_so < mat[group].rm_eo)
		    fwrite(str + mat[group].rm_so,
		 	   mat[group].rm_eo - mat[group].rm_so, 1, stderr);
		fputc('\n', stderr);
		break;
	    }
	    ++group;
	}
    }

    fclose(fp);

    return (ecode);
}