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