source: trunk/minix/lib/other/v8regsub.c@ 12

Last change on this file since 12 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 2.2 KB
Line 
1/* regsub
2 *
3 * Copyright (c) 1986 by University of Toronto.
4 * Written by Henry Spencer. Not derived from licensed software.
5 *
6 * Permission is granted to anyone to use this software for any
7 * purpose on any computer system, and to redistribute it freely,
8 * subject to the following restrictions:
9 *
10 * 1. The author is not responsible for the consequences of use of
11 * this software, no matter how awful, even if they arise
12 * from defects in it.
13 *
14 * 2. The origin of this software must not be misrepresented, either
15 * by explicit claim or by omission.
16 *
17 * 3. Altered versions must be plainly marked as such, and must not
18 * be misrepresented as being the original software.
19 */
20
21#include <string.h>
22#include <stdio.h>
23#define const /* avoid "const poisoning" */
24#include <regexp.h>
25#undef const
26
27/* The first byte of the regexp internal "program" is actually this magic
28 * number; the start node begins in the second byte.
29 */
30#define MAGIC 0234
31
32#define CHARBITS 0377
33#ifndef CHARBITS
34#define UCHARAT(p) ((int)*(unsigned char *)(p))
35#else
36#define UCHARAT(p) ((int)*(p)&CHARBITS)
37#endif
38
39/*
40 - regsub - perform substitutions after a regexp match
41 */
42void regsub(prog, source, dest)
43regexp *prog;
44char *source;
45char *dest;
46{
47 register char *src;
48 register char *dst;
49 register char c;
50 register int no;
51 register int len;
52
53 if (prog == (regexp *)NULL || source == (char *)NULL || dest == (char *)NULL) {
54 regerror("NULL parm to regsub");
55 return;
56 }
57 if (UCHARAT(prog->program) != MAGIC) {
58 regerror("damaged regexp fed to regsub");
59 return;
60 }
61 src = source;
62 dst = dest;
63 while ((c = *src++) != '\0') {
64 if (c == '&')
65 no = 0;
66 else if (c == '\\' && '0' <= *src && *src <= '9')
67 no = *src++ - '0';
68 else
69 no = -1;
70
71 if (no < 0) { /* Ordinary character. */
72 if (c == '\\' && (*src == '\\' || *src == '&')) c = *src++;
73 *dst++ = c;
74 } else
75 if (prog->startp[no] != (char *)NULL && prog->endp[no] != (char *)NULL) {
76 len = (int) (prog->endp[no] - prog->startp[no]);
77 strncpy(dst, prog->startp[no], len);
78 dst += len;
79 if (len != 0 && *(dst - 1) == '\0') { /* strncpy hit NUL. */
80 regerror("damaged match string");
81 return;
82 }
83 }
84 }
85 *dst++ = '\0';
86}
87
88/*
89 * $PchId: regsub.c,v 1.3 1995/11/27 20:18:16 philip Exp $
90 */
Note: See TracBrowser for help on using the repository browser.