15dfecf96Smrg/* 25dfecf96Smrg * Copyright (c) 2002 by The XFree86 Project, Inc. 35dfecf96Smrg * 45dfecf96Smrg * Permission is hereby granted, free of charge, to any person obtaining a 55dfecf96Smrg * copy of this software and associated documentation files (the "Software"), 65dfecf96Smrg * to deal in the Software without restriction, including without limitation 75dfecf96Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 85dfecf96Smrg * and/or sell copies of the Software, and to permit persons to whom the 95dfecf96Smrg * Software is furnished to do so, subject to the following conditions: 105dfecf96Smrg * 115dfecf96Smrg * The above copyright notice and this permission notice shall be included in 125dfecf96Smrg * all copies or substantial portions of the Software. 135dfecf96Smrg * 145dfecf96Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 155dfecf96Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 165dfecf96Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 175dfecf96Smrg * THE XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 185dfecf96Smrg * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF 195dfecf96Smrg * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 205dfecf96Smrg * SOFTWARE. 215dfecf96Smrg * 225dfecf96Smrg * Except as contained in this notice, the name of the XFree86 Project shall 235dfecf96Smrg * not be used in advertising or otherwise to promote the sale, use or other 245dfecf96Smrg * dealings in this Software without prior written authorization from the 255dfecf96Smrg * XFree86 Project. 265dfecf96Smrg * 275dfecf96Smrg * Author: Paulo César Pereira de Andrade 285dfecf96Smrg */ 295dfecf96Smrg 305dfecf96Smrg/* $XFree86$ */ 315dfecf96Smrg 325dfecf96Smrg/* 335dfecf96Smrg * Compile with: cc -o tests tests.c -L. -lre 345dfecf96Smrg */ 355dfecf96Smrg 365dfecf96Smrg#include <stdio.h> 375dfecf96Smrg#include <string.h> 385dfecf96Smrg#include "re.h" 395dfecf96Smrg 405dfecf96Smrgint 415dfecf96Smrgmain(int argc, char *argv[]) 425dfecf96Smrg{ 435dfecf96Smrg re_cod cod; 445dfecf96Smrg re_mat mat[10]; 455dfecf96Smrg int line, ecode, i, len, group, failed; 465dfecf96Smrg long eo, so; 475dfecf96Smrg char buf[8192]; 485dfecf96Smrg char str[8192]; 495dfecf96Smrg FILE *fp = fopen("tests.txt", "r"); 505dfecf96Smrg 515dfecf96Smrg if (fp == NULL) { 525dfecf96Smrg fprintf(stderr, "failed to open tests.txt\n"); 535dfecf96Smrg exit(1); 545dfecf96Smrg } 555dfecf96Smrg 565dfecf96Smrg ecode = line = group = failed = 0; 575dfecf96Smrg cod.cod = NULL; 585dfecf96Smrg while (fgets(buf, sizeof(buf), fp)) { 595dfecf96Smrg ++line; 605dfecf96Smrg if (buf[0] == '#' || buf[0] == '\n') 615dfecf96Smrg continue; 625dfecf96Smrg else if (buf[0] == '/') { 635dfecf96Smrg char *ptr = strrchr(buf, '/'); 645dfecf96Smrg 655dfecf96Smrg if (ptr == buf) { 665dfecf96Smrg fprintf(stderr, "syntax error at line %d\n", line); 675dfecf96Smrg break; 685dfecf96Smrg } 695dfecf96Smrg else { 705dfecf96Smrg int flags = 0; 715dfecf96Smrg 725dfecf96Smrg refree(&cod); 735dfecf96Smrg for (*ptr++ = '\0'; *ptr; ptr++) { 745dfecf96Smrg if (*ptr == 'i') 755dfecf96Smrg flags |= RE_ICASE; 765dfecf96Smrg else if (*ptr == 'n') 775dfecf96Smrg flags |= RE_NEWLINE; 785dfecf96Smrg } 795dfecf96Smrg ecode = recomp(&cod, buf + 1, flags); 805dfecf96Smrg failed = ecode; 815dfecf96Smrg } 825dfecf96Smrg } 835dfecf96Smrg else if (buf[0] == '>') { 845dfecf96Smrg if (cod.cod == NULL) { 855dfecf96Smrg fprintf(stderr, "no previous pattern at line %d\n", line); 865dfecf96Smrg break; 875dfecf96Smrg } 885dfecf96Smrg len = strlen(buf) - 1; 895dfecf96Smrg buf[len] = '\0'; 905dfecf96Smrg strcpy(str, buf + 1); 915dfecf96Smrg for (i = 0, --len; i < len - 1; i++) { 925dfecf96Smrg if (str[i] == '\\') { 935dfecf96Smrg memmove(str + i, str + i + 1, len); 945dfecf96Smrg --len; 955dfecf96Smrg switch (str[i]) { 965dfecf96Smrg case 'a': 975dfecf96Smrg str[i] = '\a'; 985dfecf96Smrg break; 995dfecf96Smrg case 'b': 1005dfecf96Smrg str[i] = '\b'; 1015dfecf96Smrg break; 1025dfecf96Smrg case 'f': 1035dfecf96Smrg str[i] = '\f'; 1045dfecf96Smrg break; 1055dfecf96Smrg case 'n': 1065dfecf96Smrg str[i] = '\n'; 1075dfecf96Smrg break; 1085dfecf96Smrg case 'r': 1095dfecf96Smrg str[i] = '\r'; 1105dfecf96Smrg break; 1115dfecf96Smrg case 't': 1125dfecf96Smrg str[i] = '\t'; 1135dfecf96Smrg break; 1145dfecf96Smrg case 'v': 1155dfecf96Smrg str[i] = '\v'; 1165dfecf96Smrg break; 1175dfecf96Smrg default: 1185dfecf96Smrg break; 1195dfecf96Smrg } 1205dfecf96Smrg } 1215dfecf96Smrg } 1225dfecf96Smrg group = 0; 1235dfecf96Smrg ecode = reexec(&cod, str, 10, &mat[0], 0); 1245dfecf96Smrg if (ecode && ecode != RE_NOMATCH) { 1255dfecf96Smrg reerror(failed, &cod, buf, sizeof(buf)); 1265dfecf96Smrg fprintf(stderr, "%s, at line %d\n", buf, line); 1275dfecf96Smrg break; 1285dfecf96Smrg } 1295dfecf96Smrg } 1305dfecf96Smrg else if (buf[0] == ':') { 1315dfecf96Smrg if (failed) { 1325dfecf96Smrg len = strlen(buf) - 1; 1335dfecf96Smrg buf[len] = '\0'; 1345dfecf96Smrg if (failed == RE_EESCAPE && strcmp(buf, ":EESCAPE") == 0) 1355dfecf96Smrg continue; 1365dfecf96Smrg if (failed == RE_ESUBREG && strcmp(buf, ":ESUBREG") == 0) 1375dfecf96Smrg continue; 1385dfecf96Smrg if (failed == RE_EBRACK && strcmp(buf, ":EBRACK") == 0) 1395dfecf96Smrg continue; 1405dfecf96Smrg if (failed == RE_EPAREN && strcmp(buf, ":EPAREN") == 0) 1415dfecf96Smrg continue; 1425dfecf96Smrg if (failed == RE_EBRACE && strcmp(buf, ":EBRACE") == 0) 1435dfecf96Smrg continue; 1445dfecf96Smrg if (failed == RE_EBADBR && strcmp(buf, ":EBADBR") == 0) 1455dfecf96Smrg continue; 1465dfecf96Smrg if (failed == RE_ERANGE && strcmp(buf, ":ERANGE") == 0) 1475dfecf96Smrg continue; 1485dfecf96Smrg if (failed == RE_ESPACE && strcmp(buf, ":ESPACE") == 0) 1495dfecf96Smrg continue; 1505dfecf96Smrg if (failed == RE_BADRPT && strcmp(buf, ":BADRPT") == 0) 1515dfecf96Smrg continue; 1525dfecf96Smrg if (failed == RE_EMPTY && strcmp(buf, ":EMPTY") == 0) 1535dfecf96Smrg continue; 1545dfecf96Smrg reerror(failed, &cod, buf, sizeof(buf)); 1555dfecf96Smrg fprintf(stderr, "Error value %d doesn't match: %s, at line %d\n", 1565dfecf96Smrg failed, buf, line); 1575dfecf96Smrg break; 1585dfecf96Smrg } 1595dfecf96Smrg else if (!ecode) { 1605dfecf96Smrg fprintf(stderr, "found match when shoudn't, at line %d\n", line); 1615dfecf96Smrg break; 1625dfecf96Smrg } 1635dfecf96Smrg } 1645dfecf96Smrg else { 1655dfecf96Smrg if (failed) { 1665dfecf96Smrg reerror(failed, &cod, buf, sizeof(buf)); 167f14f4646Smrg fprintf(stderr, "%s, at line %d\n", buf, line); 1685dfecf96Smrg break; 1695dfecf96Smrg } 1705dfecf96Smrg if (sscanf(buf, "%ld,%ld:", &so, &eo) != 2) { 1715dfecf96Smrg fprintf(stderr, "expecting match offsets at line %d\n", line); 1725dfecf96Smrg break; 1735dfecf96Smrg } 1745dfecf96Smrg else if (ecode) { 1755dfecf96Smrg fprintf(stderr, "didn't match, at line %d\n", line); 1765dfecf96Smrg break; 1775dfecf96Smrg } 1785dfecf96Smrg else if (group >= 10) { 1795dfecf96Smrg fprintf(stderr, "syntax error at line %d (too many groups)\n", 1805dfecf96Smrg line); 1815dfecf96Smrg break; 1825dfecf96Smrg } 1835dfecf96Smrg else if (so != mat[group].rm_so || eo != mat[group].rm_eo) { 1845dfecf96Smrg fprintf(stderr, "match failed at line %d, got %ld,%ld: ", 1855dfecf96Smrg line, mat[group].rm_so, mat[group].rm_eo); 1865dfecf96Smrg if (mat[group].rm_so < mat[group].rm_eo) 1875dfecf96Smrg fwrite(str + mat[group].rm_so, 1885dfecf96Smrg mat[group].rm_eo - mat[group].rm_so, 1, stderr); 1895dfecf96Smrg fputc('\n', stderr); 1905dfecf96Smrg break; 1915dfecf96Smrg } 1925dfecf96Smrg ++group; 1935dfecf96Smrg } 1945dfecf96Smrg } 1955dfecf96Smrg 1965dfecf96Smrg fclose(fp); 1975dfecf96Smrg 1985dfecf96Smrg return (ecode); 1995dfecf96Smrg} 200