1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
|
/********************************************
re_cmpl.c
copyright 1991, Michael D. Brennan
This is a source file for mawk, an implementation of
the AWK programming language.
Mawk is distributed without warranty under the terms of
the GNU General Public License, version 2, 1991.
********************************************/
/* $Log: re_cmpl.c,v $
* Revision 1.6 1994/12/13 00:14:58 mike
* \\ -> \ on second replacement scan
*
* Revision 1.5 1994/10/08 19:15:51 mike
* remove SM_DOS
*
* Revision 1.4 1993/07/21 01:17:53 mike
* handle "&" as replacement correctly
*
* Revision 1.3 1993/07/17 13:23:10 mike
* indent and general code cleanup
*
* Revision 1.2 1993/07/15 23:38:23 mike
* SIZE_T and indent
*
* Revision 1.1.1.1 1993/07/03 18:58:19 mike
* move source to cvs
*
* Revision 5.2 1993/01/01 21:30:48 mike
* split new_STRING() into new_STRING and new_STRING0
*
* Revision 5.1 1991/12/05 07:56:25 brennan
* 1.1 pre-release
*
*/
/* re_cmpl.c */
#include "mawk.h"
#include "memory.h"
#include "scan.h"
#include "regexp.h"
#include "repl.h"
static CELL *PROTO(REPL_compile, (STRING *)) ;
typedef struct re_node
{
STRING *sval ;
PTR re ;
struct re_node *link ;
} RE_NODE ;
/* a list of compiled regular expressions */
static RE_NODE *re_list ;
static char efmt[] = "regular expression compile failed (%s)\n%s" ;
/* compile a STRING to a regular expression machine.
Search a list of pre-compiled strings first
*/
PTR
re_compile(sval)
STRING *sval ;
{
register RE_NODE *p ;
RE_NODE *q ;
char *s ;
/* search list */
s = sval->str ;
p = re_list ;
q = (RE_NODE *) 0 ;
while (p)
{
if (strcmp(s, p->sval->str) == 0) /* found */
{
if (!q) /* already at front */
goto _return ;
else /* delete from list for move to front */
{
q->link = p->link ; goto found ;
}
}
else
{
q = p ; p = p->link ;
}
}
/* not found */
p = ZMALLOC(RE_NODE) ;
p->sval = sval ;
sval->ref_cnt++ ;
if (!(p->re = REcompile(s)))
{
if (mawk_state == EXECUTION)
rt_error(efmt, REerrlist[REerrno], s) ;
else /* compiling */
{
compile_error(efmt, REerrlist[REerrno], s) ;
return (PTR) 0 ;
}
}
found :
/* insert p at the front of the list */
p->link = re_list ; re_list = p ;
_return :
#ifdef DEBUG
if (dump_RE) REmprint(p->re, stderr) ;
#endif
return p->re ;
}
/* this is only used by da() */
char *
re_uncompile(m)
PTR m ;
{
register RE_NODE *p ;
for (p = re_list; p; p = p->link)
if (p->re == m) return p->sval->str ;
#ifdef DEBUG
bozo("non compiled machine") ;
#endif
}
/*=================================================*/
/* replacement operations */
/* create a replacement CELL from a STRING * */
static CELL *
REPL_compile(sval)
STRING *sval ;
{
int i = 0 ;
register char *p = sval->str ;
register char *q ;
char *xbuff ;
CELL *cp ;
q = xbuff = (char *) zmalloc(sval->len + 1) ;
while (1)
{
switch (*p)
{
case 0:
*q = 0 ;
goto done ;
case '\\':
if (p[1] == '&'|| p[1] == '\\')
{
*q++ = p[1] ;
p += 2 ;
continue ;
}
else break ;
case '&':
/* if empty we don't need to make a node */
if (q != xbuff)
{
*q = 0 ;
split_buff[i++] = new_STRING(xbuff) ;
}
/* and a null node for the '&' */
split_buff[i++] = (STRING *) 0 ;
/* reset */
p++ ; q = xbuff ;
continue ;
default:
break ;
}
*q++ = *p++ ;
}
done :
/* if we have one empty string it will get made now */
if (q > xbuff || i == 0) split_buff[i++] = new_STRING(xbuff) ;
/* This will never happen */
if (i > MAX_SPLIT) overflow("replacement pieces", MAX_SPLIT) ;
cp = ZMALLOC(CELL) ;
if (i == 1 && split_buff[0])
{
cp->type = C_REPL ;
cp->ptr = (PTR) split_buff[0] ;
}
else
{
STRING **sp = (STRING **)
(cp->ptr = zmalloc(sizeof(STRING *) * i)) ;
int j = 0 ;
while (j < i) *sp++ = split_buff[j++] ;
cp->type = C_REPLV ;
cp->vcnt = i ;
}
zfree(xbuff, sval->len + 1) ;
return cp ;
}
/* free memory used by a replacement CELL */
void
repl_destroy(cp)
register CELL *cp ;
{
register STRING **p ;
unsigned cnt ;
if (cp->type == C_REPL) free_STRING(string(cp)) ;
else /* an C_REPLV */
{
p = (STRING **) cp->ptr ;
for (cnt = cp->vcnt; cnt; cnt--)
{
if (*p) free_STRING(*p) ;
p++ ;
}
zfree(cp->ptr, cp->vcnt * sizeof(STRING *)) ;
}
}
/* copy a C_REPLV cell to another CELL */
CELL *
replv_cpy(target, source)
CELL *target, *source ;
{
STRING **t, **s ;
unsigned cnt ;
target->type = C_REPLV ;
cnt = target->vcnt = source->vcnt ;
target->ptr = (PTR) zmalloc(cnt * sizeof(STRING *)) ;
t = (STRING **) target->ptr ;
s = (STRING **) source->ptr ;
while (cnt)
{
cnt-- ;
if ( *s ) (*s)->ref_cnt++ ;
*t++ = *s++ ;
}
return target ;
}
/* here's our old friend linked linear list with move to the front
for compilation of replacement CELLs */
typedef struct repl_node
{
struct repl_node *link ;
STRING *sval ; /* the input */
CELL *cp ; /* the output */
} REPL_NODE ;
static REPL_NODE *repl_list ;
/* search the list (with move to the front) for a compiled
separator.
return a ptr to a CELL (C_REPL or C_REPLV)
*/
CELL *
repl_compile(sval)
STRING *sval ;
{
register REPL_NODE *p ;
REPL_NODE *q ;
char *s ;
/* search the list */
s = sval->str ;
p = repl_list ;
q = (REPL_NODE *) 0 ;
while (p)
{
if (strcmp(s, p->sval->str) == 0) /* found */
{
if (!q) /* already at front */
return p->cp ;
else /* delete from list for move to front */
{
q->link = p->link ;
goto found ;
}
}
else
{
q = p ; p = p->link ;
}
}
/* not found */
p = ZMALLOC(REPL_NODE) ;
p->sval = sval ;
sval->ref_cnt++ ;
p->cp = REPL_compile(sval) ;
found :
/* insert p at the front of the list */
p->link = repl_list ; repl_list = p ;
return p->cp ;
}
/* return the string for a CELL or type REPL or REPLV,
this is only used by da() */
char *
repl_uncompile(cp)
CELL *cp ;
{
register REPL_NODE *p = repl_list ;
if (cp->type == C_REPL)
{
while (p)
{
if (p->cp->type == C_REPL && p->cp->ptr == cp->ptr)
return p->sval->str ;
else p = p->link ;
}
}
else
{
while (p)
{
if (p->cp->type == C_REPLV &&
memcmp(cp->ptr, p->cp->ptr, cp->vcnt * sizeof(STRING *))
== 0)
return p->sval->str ;
else p = p->link ;
}
}
#if DEBUG
bozo("unable to uncompile an repl") ;
#endif
}
/*
convert a C_REPLV to C_REPL
replacing the &s with sval
*/
CELL *
replv_to_repl(cp, sval)
CELL *cp ; STRING *sval ;
{
register STRING **p ;
STRING **sblock = (STRING **) cp->ptr ;
unsigned cnt, vcnt = cp->vcnt ;
unsigned len ;
char *target ;
#ifdef DEBUG
if (cp->type != C_REPLV) bozo("not replv") ;
#endif
p = sblock ; cnt = vcnt ; len = 0 ;
while (cnt--)
{
if (*p) len += (*p++)->len ;
else
{
*p++ = sval ;
sval->ref_cnt++ ;
len += sval->len ;
}
}
cp->type = C_REPL ;
cp->ptr = (PTR) new_STRING0(len) ;
p = sblock ; cnt = vcnt ; target = string(cp)->str ;
while (cnt--)
{
memcpy(target, (*p)->str, (*p)->len) ;
target += (*p)->len ;
free_STRING(*p) ;
p++ ;
}
zfree(sblock, vcnt * sizeof(STRING *)) ;
return cp ;
}
|