xref: /illumos-gate/usr/src/cmd/lp/lib/filters/regex.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include "sys/types.h"
36*7c478bd9Sstevel@tonic-gate #include "regexpr.h"
37*7c478bd9Sstevel@tonic-gate #include "regex.h"
38*7c478bd9Sstevel@tonic-gate #include "string.h"
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate /**
41*7c478bd9Sstevel@tonic-gate  ** match() - TEST MATCH OF TEMPLATE/PATTERN WITH PARAMETER
42*7c478bd9Sstevel@tonic-gate  **/
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate int
45*7c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
match(char * re,char * value)46*7c478bd9Sstevel@tonic-gate match (
47*7c478bd9Sstevel@tonic-gate 	char *			re,
48*7c478bd9Sstevel@tonic-gate 	char *			value
49*7c478bd9Sstevel@tonic-gate )
50*7c478bd9Sstevel@tonic-gate #else
51*7c478bd9Sstevel@tonic-gate match (re, value)
52*7c478bd9Sstevel@tonic-gate 	register char *		re;
53*7c478bd9Sstevel@tonic-gate 	register char *		value;
54*7c478bd9Sstevel@tonic-gate #endif
55*7c478bd9Sstevel@tonic-gate {
56*7c478bd9Sstevel@tonic-gate 	int			ret;
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	/*
59*7c478bd9Sstevel@tonic-gate 	 * We want exact matches, just as if the regular expression
60*7c478bd9Sstevel@tonic-gate 	 * was ^...$, to explicitly match the beginning and end of line.
61*7c478bd9Sstevel@tonic-gate 	 * Using "advance" instead of "step" takes care of the ^ and
62*7c478bd9Sstevel@tonic-gate 	 * checking where the match left off takes care of the $.
63*7c478bd9Sstevel@tonic-gate 	 * We don't do something silly like add the ^ and $ ourselves,
64*7c478bd9Sstevel@tonic-gate 	 * because the user may have done that already.
65*7c478bd9Sstevel@tonic-gate 	 */
66*7c478bd9Sstevel@tonic-gate 	ret = advance(value, re);
67*7c478bd9Sstevel@tonic-gate 	if (ret && *loc2)
68*7c478bd9Sstevel@tonic-gate 		ret = 0;
69*7c478bd9Sstevel@tonic-gate 	return (ret);
70*7c478bd9Sstevel@tonic-gate }
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate /**
73*7c478bd9Sstevel@tonic-gate  ** replace() - REPLACE TEMPLATE WITH EXPANDED REGULAR EXPRESSION MATCH
74*7c478bd9Sstevel@tonic-gate  **/
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate size_t
77*7c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
replace(char ** pp,char * result,char * value,int nbra)78*7c478bd9Sstevel@tonic-gate replace (
79*7c478bd9Sstevel@tonic-gate 	char **			pp,
80*7c478bd9Sstevel@tonic-gate 	char *			result,
81*7c478bd9Sstevel@tonic-gate 	char *			value,
82*7c478bd9Sstevel@tonic-gate 	int			nbra
83*7c478bd9Sstevel@tonic-gate )
84*7c478bd9Sstevel@tonic-gate #else
85*7c478bd9Sstevel@tonic-gate replace (pp, result, value)
86*7c478bd9Sstevel@tonic-gate 	char **			pp;
87*7c478bd9Sstevel@tonic-gate 	char *			result;
88*7c478bd9Sstevel@tonic-gate 	char *			value;
89*7c478bd9Sstevel@tonic-gate 	int			nbra;
90*7c478bd9Sstevel@tonic-gate #endif
91*7c478bd9Sstevel@tonic-gate {
92*7c478bd9Sstevel@tonic-gate 	register char *		p;
93*7c478bd9Sstevel@tonic-gate 	register char *		q;
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 	register size_t		ncount	= 0;
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate /*
99*7c478bd9Sstevel@tonic-gate  * Count and perhaps copy a single character:
100*7c478bd9Sstevel@tonic-gate  */
101*7c478bd9Sstevel@tonic-gate #define	CCPY(SRC)	if ((ncount++, pp)) \
102*7c478bd9Sstevel@tonic-gate 				*p++ = SRC
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate /*
105*7c478bd9Sstevel@tonic-gate  * Count and perhaps copy a string:
106*7c478bd9Sstevel@tonic-gate  */
107*7c478bd9Sstevel@tonic-gate #define	SCPY(SRC)	if (pp) { \
108*7c478bd9Sstevel@tonic-gate 				register char *	r; \
109*7c478bd9Sstevel@tonic-gate 				for (r = (SRC); *r; ncount++) \
110*7c478bd9Sstevel@tonic-gate 					*p++ = *r++; \
111*7c478bd9Sstevel@tonic-gate 			} else \
112*7c478bd9Sstevel@tonic-gate 				ncount += strlen(SRC)
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 	if (pp)
116*7c478bd9Sstevel@tonic-gate 		p = *pp;
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	for (q = result; *q; q++)  switch (*q) {
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	case '*':
121*7c478bd9Sstevel@tonic-gate 	case '&':
122*7c478bd9Sstevel@tonic-gate 		SCPY (value);
123*7c478bd9Sstevel@tonic-gate 		break;
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	case '\\':
126*7c478bd9Sstevel@tonic-gate 		switch (*++q) {
127*7c478bd9Sstevel@tonic-gate 		case '1':
128*7c478bd9Sstevel@tonic-gate 		case '2':
129*7c478bd9Sstevel@tonic-gate 		case '3':
130*7c478bd9Sstevel@tonic-gate 		case '4':
131*7c478bd9Sstevel@tonic-gate 		case '5':
132*7c478bd9Sstevel@tonic-gate 		case '6':
133*7c478bd9Sstevel@tonic-gate 		case '7':
134*7c478bd9Sstevel@tonic-gate 		case '8':
135*7c478bd9Sstevel@tonic-gate 		case '9':
136*7c478bd9Sstevel@tonic-gate 		{
137*7c478bd9Sstevel@tonic-gate 			register int		n = *q-'1';
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 			if (n < nbra) {
140*7c478bd9Sstevel@tonic-gate 				register char		c = *(braelist[n]);
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 				*(braelist[n]) = 0;
143*7c478bd9Sstevel@tonic-gate 				SCPY (braslist[n]);
144*7c478bd9Sstevel@tonic-gate 				*(braelist[n]) = c;
145*7c478bd9Sstevel@tonic-gate 			}
146*7c478bd9Sstevel@tonic-gate 			break;
147*7c478bd9Sstevel@tonic-gate 		}
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 		default:
150*7c478bd9Sstevel@tonic-gate 			CCPY (*q);
151*7c478bd9Sstevel@tonic-gate 			break;
152*7c478bd9Sstevel@tonic-gate 		}
153*7c478bd9Sstevel@tonic-gate 		break;
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	default:
156*7c478bd9Sstevel@tonic-gate 		CCPY (*q);
157*7c478bd9Sstevel@tonic-gate 		break;
158*7c478bd9Sstevel@tonic-gate 	}
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	if (pp)
161*7c478bd9Sstevel@tonic-gate 		*pp = p;
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	return (ncount);
164*7c478bd9Sstevel@tonic-gate }
165