1/*********************************************************************** 2* * 3* This software is part of the ast package * 4* Copyright (c) 1985-2011 AT&T Intellectual Property * 5* and is licensed under the * 6* Common Public License, Version 1.0 * 7* by AT&T Intellectual Property * 8* * 9* A copy of the License is available at * 10* http://www.opensource.org/licenses/cpl1.0.txt * 11* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) * 12* * 13* Information and Software Systems Research * 14* AT&T Research * 15* Florham Park NJ * 16* * 17* Glenn Fowler <gsf@research.att.com> * 18* David Korn <dgk@research.att.com> * 19* Phong Vo <kpv@research.att.com> * 20* * 21***********************************************************************/ 22#pragma prototyped 23/* 24 * <regexp.h> library support 25 */ 26 27#define _REGEXP_DECLARE 28 29#include <ast.h> 30#include <regexp.h> 31#include <regex.h> 32#include <align.h> 33 34typedef struct 35{ 36 regex_t re; 37 char* buf; 38 char* cur; 39 unsigned int size; 40} Env_t; 41 42static void* 43block(void* handle, void* data, size_t size) 44{ 45 register Env_t* env = (Env_t*)handle; 46 47 if (data || (size = roundof(size, ALIGN_BOUND2)) > (env->buf + env->size - env->cur)) 48 return 0; 49 data = (void*)env->cur; 50 env->cur += size; 51 return data; 52} 53 54int 55_re_comp(regexp_t* re, const char* pattern, char* handle, unsigned int size) 56{ 57 register Env_t* env = (Env_t*)handle; 58 register int n; 59 60 if (size <= sizeof(Env_t)) 61 return 50; 62 env->buf = env->cur = (char*)env + sizeof(Env_t); 63 env->size = size - sizeof(Env_t); 64 regalloc(env, block, REG_NOFREE); 65 n = regcomp(&env->re, pattern, REG_LENIENT|REG_NULL); 66 switch (n) 67 { 68 case 0: 69 break; 70 case REG_ERANGE: 71 n = 11; 72 break; 73 case REG_BADBR: 74 n = 16; 75 break; 76 case REG_ESUBREG: 77 n = 25; 78 break; 79 case REG_EPAREN: 80 n = 42; 81 break; 82 case REG_EBRACK: 83 n = 49; 84 break; 85 default: 86 n = 50; 87 break; 88 } 89 re->re_nbra = env->re.re_nsub; 90 return n; 91} 92 93int 94_re_exec(regexp_t* re, const char* subject, const char* handle, int anchor) 95{ 96 register Env_t* env = (Env_t*)handle; 97 register int n; 98 regmatch_t match[elementsof(re->re_braslist)+1]; 99 100 if (regexec(&env->re, subject, elementsof(match), match, 0) || anchor && match[0].rm_so) 101 return 0; 102 re->re_loc1 = (char*)subject + match[0].rm_so; 103 re->re_loc2 = (char*)subject + match[0].rm_eo; 104 for (n = 1; n <= env->re.re_nsub; n++) 105 { 106 re->re_braslist[n-1] = (char*)subject + match[n].rm_so; 107 re->re_braelist[n-1] = (char*)subject + match[n].rm_eo; 108 } 109 return 1; 110} 111 112char* 113_re_putc(int c) 114{ 115 static Sfio_t* sp; 116 117 if (!sp && !(sp = sfstropen())) 118 return 0; 119 if (!c) 120 return sfstruse(sp); 121 sfputc(sp, c); 122 return 0; 123} 124