tests/gurep.c:1:/* $Header: /home/agc/src/ure-2.4/RCS/gurep.c,v 1.6 1997/01/08 11:21:10 agc Exp agc $ */ tests/gurep.c:3: tests/gurep.c:3:/* tests/gurep.c:4: * Copyright \u00a9 1996-1997 Alistair G. Crooks. All rights reserved. tests/gurep.c:5: * tests/gurep.c:6: * Redistribution and use in source and binary forms, with or without tests/gurep.c:7: * modification, are permitted provided that the following conditions tests/gurep.c:8: * are met: tests/gurep.c:9: * 1. Redistributions of source code must retain the above copyright tests/gurep.c:10: * notice, this list of conditions and the following disclaimer. tests/gurep.c:11: * 2. Redistributions in binary form must reproduce the above copyright tests/gurep.c:12: * notice, this list of conditions and the following disclaimer in the tests/gurep.c:13: * documentation and/or other materials provided with the distribution. tests/gurep.c:14: * 3. All advertising materials mentioning features or use of this software tests/gurep.c:15: * must display the following acknowledgement: tests/gurep.c:16: * This product includes software developed by Alistair G. Crooks. tests/gurep.c:17: * 4. The name of the author may not be used to endorse or promote tests/gurep.c:18: * products derived from this software without specific prior written tests/gurep.c:19: * permission. tests/gurep.c:20: * tests/gurep.c:21: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS tests/gurep.c:22: * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED tests/gurep.c:23: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE tests/gurep.c:24: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY tests/gurep.c:25: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL tests/gurep.c:26: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE tests/gurep.c:27: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS tests/gurep.c:28: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, tests/gurep.c:29: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING tests/gurep.c:30: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS tests/gurep.c:31: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. tests/gurep.c:32: */ tests/gurep.c:33:#include "config.h" tests/gurep.c:35: tests/gurep.c:35:#ifdef HAVE_SYS_TYPES_H tests/gurep.c:36:#include tests/gurep.c:37:#endif tests/gurep.c:39: tests/gurep.c:39:#ifdef HAVE_SYS_STAT_H tests/gurep.c:40:#include tests/gurep.c:41:#endif tests/gurep.c:43: tests/gurep.c:43:#include tests/gurep.c:45: tests/gurep.c:45:#ifdef HAVE_UNISTD_H tests/gurep.c:46:#include tests/gurep.c:47:#endif tests/gurep.c:49: tests/gurep.c:49:#ifdef HAVE_STDLIB_H tests/gurep.c:50:#include tests/gurep.c:51:#endif tests/gurep.c:53: tests/gurep.c:53:#ifdef HAVE_STRING_H tests/gurep.c:54:#include tests/gurep.c:55:#endif tests/gurep.c:57: tests/gurep.c:57:#include "ure.h" tests/gurep.c:58:#include "utf.h" tests/gurep.c:59:#include "urelang.h" tests/gurep.c:61: tests/gurep.c:61:/* print the line number of `s', starting from `buf' */ tests/gurep.c:62:int tests/gurep.c:63:LineNum(char *buf, char *s) tests/gurep.c:64:{ tests/gurep.c:65: Rune r; tests/gurep.c:66: int i; tests/gurep.c:67: int n; tests/gurep.c:69: tests/gurep.c:69: for (n = 1 ; buf <= s ; buf += i) { tests/gurep.c:70: i = chartorune(&r, buf); tests/gurep.c:71: if (r == '\n') { tests/gurep.c:72: n++; tests/gurep.c:73: } tests/gurep.c:74: } tests/gurep.c:75: return n; tests/gurep.c:76:} tests/gurep.c:78: tests/gurep.c:78:/* print the line */ tests/gurep.c:79:void tests/gurep.c:80:PrintLine(char *up, ure_t *sp, char *from, char *to) tests/gurep.c:81:{ tests/gurep.c:82: Rune r; tests/gurep.c:83: char *cp; tests/gurep.c:84: int i; tests/gurep.c:86: tests/gurep.c:86: for (cp = from; cp > up ; cp -= i) { tests/gurep.c:87: i = priorrune(&r, cp); tests/gurep.c:88: if (r == '\n') { tests/gurep.c:89: break; tests/gurep.c:90: } tests/gurep.c:91: } tests/gurep.c:92: for (;;) { tests/gurep.c:93: i = chartorune(&r, cp); tests/gurep.c:94: if (UNICODE_isascii(r)) { tests/gurep.c:95: putchar((char)(r)); tests/gurep.c:96: } else { tests/gurep.c:97: printf("\\u%04x", r); tests/gurep.c:98: } tests/gurep.c:99: if (cp >= to && r == '\n') { tests/gurep.c:100: break; tests/gurep.c:101: } tests/gurep.c:102: cp += i; tests/gurep.c:103: } tests/gurep.c:104:} tests/gurep.c:106: tests/gurep.c:106:/* get the file into memory */ tests/gurep.c:107:static char * tests/gurep.c:108:fgetfile(FILE *fp, int *size) tests/gurep.c:109:{ tests/gurep.c:110: struct stat s; tests/gurep.c:111: char *cp; tests/gurep.c:112: int cc; tests/gurep.c:114: tests/gurep.c:114: (void) fstat(fileno(fp), &s); tests/gurep.c:115: *size = s.st_size; tests/gurep.c:116: cp = (char *) malloc(*size + 1); tests/gurep.c:117: if (cp == (char *) NULL) { tests/gurep.c:118: (void) fprintf(stderr, "Memory problems.\n"); tests/gurep.c:119: exit(1); tests/gurep.c:120: } tests/gurep.c:121: cc = fread(cp, sizeof(char), *size, fp); tests/gurep.c:122: if (cc != *size) { tests/gurep.c:123: free(cp); tests/gurep.c:124: return (char *) NULL; tests/gurep.c:125: } tests/gurep.c:126: cp[cc] = 0; tests/gurep.c:127: return cp; tests/gurep.c:128:} tests/gurep.c:130: tests/gurep.c:130:/* do a utf regexp search for each file */ tests/gurep.c:131:int tests/gurep.c:132:dofile(ure_t *sp, char *f, int eflags, int pname, int plineno, int pline, char *collseq) tests/gurep.c:133:{ tests/gurep.c:134: urematch_t matchv[10]; tests/gurep.c:135: char *buf; tests/gurep.c:136: char *cp; tests/gurep.c:137: Rune r; tests/gurep.c:138: char ebuf[BUFSIZ]; tests/gurep.c:139: char done; tests/gurep.c:140: FILE *fp; tests/gurep.c:141: int ucc; tests/gurep.c:142: int err; tests/gurep.c:143: int i; tests/gurep.c:145: tests/gurep.c:145: if ((fp = fopen(f, "r")) == (FILE *) NULL) { tests/gurep.c:146: return 0; tests/gurep.c:147: } tests/gurep.c:148: if ((buf = fgetfile(fp, &ucc)) == (char *) NULL) { tests/gurep.c:149: return 0; tests/gurep.c:150: } tests/gurep.c:151: cp = buf; tests/gurep.c:152: for (done = 0 ; !done ; ) { tests/gurep.c:153: err = ureexec(sp, cp, 10, matchv, eflags, collseq); tests/gurep.c:154: switch(err) { tests/gurep.c:155: case URE_SUCCESS: tests/gurep.c:156: if (pname) { tests/gurep.c:157: printf("%s:", f); tests/gurep.c:158: } tests/gurep.c:159: if (plineno) { tests/gurep.c:160: printf("%d:", LineNum(buf, &cp[matchv[0].rm_so])); tests/gurep.c:161: } tests/gurep.c:162: if (!pline) { tests/gurep.c:163: (void) fclose(fp); tests/gurep.c:164: return 1; tests/gurep.c:165: } tests/gurep.c:166: PrintLine(cp, sp, &cp[matchv[0].rm_so], &cp[matchv[0].rm_eo]); tests/gurep.c:167: cp = utfrune(&cp[matchv[0].rm_eo], '\n'); tests/gurep.c:168: if (cp == (char *) NULL) { tests/gurep.c:169: done = 1; tests/gurep.c:170: } tests/gurep.c:171: i = chartorune(&r, cp); tests/gurep.c:172: cp += i; tests/gurep.c:173: if (r == 0) { tests/gurep.c:174: done = 1; tests/gurep.c:175: } tests/gurep.c:176: break; tests/gurep.c:177: case URE_NOMATCH: tests/gurep.c:178: done = 1; tests/gurep.c:179: break; tests/gurep.c:180: default: tests/gurep.c:181: ureerror(err, sp, ebuf, sizeof(ebuf)); tests/gurep.c:182: (void) fprintf(stderr, "Bad execution: %s\n", ebuf); tests/gurep.c:183: done = 1; tests/gurep.c:184: } tests/gurep.c:185: } tests/gurep.c:186: (void) fclose(fp); tests/gurep.c:187: free(buf); tests/gurep.c:188: return 1; tests/gurep.c:189:} tests/gurep.c:191: tests/gurep.c:191:extern int optind; tests/gurep.c:192:extern char *optarg; tests/gurep.c:194: tests/gurep.c:194:int tests/gurep.c:195:main(int argc, char **argv) tests/gurep.c:196:{ tests/gurep.c:197: ure_t u; tests/gurep.c:198: char *collseq; tests/gurep.c:199: char errmsg[BUFSIZ]; tests/gurep.c:200: int plineno; tests/gurep.c:201: int pline; tests/gurep.c:202: int eflags; tests/gurep.c:203: int err; tests/gurep.c:204: int i; tests/gurep.c:206: tests/gurep.c:206: /* set defaults */ tests/gurep.c:207: eflags = 0; tests/gurep.c:208: plineno = 0; tests/gurep.c:209: pline = 1; tests/gurep.c:210: collseq = (char *) NULL; tests/gurep.c:211: while ((i = getopt(argc, argv, "a:iln")) != -1) { tests/gurep.c:212: switch(i) { tests/gurep.c:213: case 'a': tests/gurep.c:214: collseq = optarg; tests/gurep.c:215: break; tests/gurep.c:216: case 'i': tests/gurep.c:217: eflags |= URE_ICASE; tests/gurep.c:218: break; tests/gurep.c:219: case 'l': tests/gurep.c:220: pline = 0; tests/gurep.c:221: break; tests/gurep.c:222: case 'n': tests/gurep.c:223: plineno = 1; tests/gurep.c:224: break; tests/gurep.c:225: } tests/gurep.c:226: } tests/gurep.c:227: if ((err = urecomp(&u, argv[optind], 0)) != URE_SUCCESS) { tests/gurep.c:228: (void) ureerror(err, &u, errmsg, sizeof(errmsg)); tests/gurep.c:229: (void) fprintf(stderr, "can't compile ure `%s', %s\n", tests/gurep.c:230: argv[optind], errmsg); tests/gurep.c:231: exit(1); tests/gurep.c:232: } tests/gurep.c:233: for (i = optind + 1 ; i < argc ; i++) { tests/gurep.c:234: dofile(&u, argv[i], eflags, (optind < argc-1), plineno, pline, collseq); tests/gurep.c:235: } tests/gurep.c:236: urefree(&u); tests/gurep.c:237: exit(0); tests/gurep.c:238:}