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