xref: /illumos-gate/usr/src/head/regex.h (revision 7c478bd9)
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 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Copyright 1989, 1994 by Mortice Kern Systems Inc.
29  * All rights reserved.
30  */
31 
32 #ifndef	_REGEX_H
33 #define	_REGEX_H
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 #include <sys/feature_tests.h>
38 #include <sys/types.h>
39 
40 #ifdef	__cplusplus
41 extern "C" {
42 #endif
43 
44 
45 /*
46  * wchar_t is a built-in type in standard C++ and as such is not
47  * defined here when using standard C++. However, the GNU compiler
48  * fixincludes utility nonetheless creates it's own version of this
49  * header for use by gcc and g++. In that version it adds a redundant
50  * guard for __cplusplus. To avoid the creation of a gcc/g++ specific
51  * header we need to include the following magic comment:
52  *
53  * we must use the C++ compiler's type
54  *
55  * The above comment should not be removed or changed until GNU
56  * gcc/fixinc/inclhack.def is updated to bypass this header.
57  */
58 #if !defined(__cplusplus) || (__cplusplus < 199711L && !defined(__GNUG__))
59 #ifndef _WCHAR_T
60 #define	_WCHAR_T
61 #if defined(_LP64)
62 typedef int	wchar_t;
63 #else
64 typedef long    wchar_t;
65 #endif
66 #endif	/* !_WCHAR_T */
67 #endif	/* !defined(__cplusplus) ... */
68 
69 typedef ssize_t regoff_t;
70 
71 /* regcomp flags */
72 #define	REG_EXTENDED	0x01		/* Use Extended Regular Expressions */
73 #define	REG_NEWLINE	0x08		/* Treat \n as regular character */
74 #define	REG_ICASE	0x04		/* Ignore case in match */
75 #define	REG_NOSUB	0x02		/* Don't set subexpression */
76 #define	REG_EGREP	0x1000		/* running as egrep(1) */
77 
78 /* non-standard flags */
79 #define	REG_DELIM	0x10		/* string[0] is delimiter */
80 #define	REG_DEBUG	0x20		/* Debug recomp and regexec */
81 #define	REG_ANCHOR	0x40		/* Implicit ^ and $ */
82 #define	REG_WORDS	0x80		/* \< and \> match word boundries */
83 
84 /* internal flags */
85 #define	REG_MUST	0x100		/* check for regmust substring */
86 
87 /* regexec flags */
88 #define	REG_NOTBOL	0x200		/* string is not BOL */
89 #define	REG_NOTEOL	0x400		/* string has no EOL */
90 #define	REG_NOOPT	0x800		/* don't do regmust optimization */
91 
92 /* regcomp and regexec return codes */
93 #define	REG_OK		0		/* success (non-standard) */
94 #define	REG_NOMATCH	1		/* regexec failed to match */
95 #define	REG_ECOLLATE	2		/* invalid collation element ref. */
96 #define	REG_EESCAPE	3		/* trailing \ in pattern */
97 #define	REG_ENEWLINE	4		/* \n found before end of pattern */
98 #define	REG_ENSUB	5		/* more than 9 \( \) pairs (OBS) */
99 #define	REG_ESUBREG	6		/* number in \[0-9] invalid */
100 #define	REG_EBRACK	7		/* [ ] imbalance */
101 #define	REG_EPAREN	8		/* ( ) imbalance */
102 #define	REG_EBRACE	9		/* \{ \} imbalance */
103 #define	REG_ERANGE	10		/* invalid endpoint in range */
104 #define	REG_ESPACE	11		/* no memory for compiled pattern */
105 #define	REG_BADRPT	12		/* invalid repetition */
106 #define	REG_ECTYPE	13		/* invalid char-class type */
107 #define	REG_BADPAT	14		/* syntax error */
108 #define	REG_BADBR	15		/* \{ \} contents bad */
109 #define	REG_EFATAL	16		/* internal error, not POSIX.2 */
110 #define	REG_ECHAR	17		/* invalid mulitbyte character */
111 #define	REG_STACK	18		/* backtrack stack overflow */
112 #define	REG_ENOSYS	19		/* function not supported (XPG4) */
113 #define	REG__LAST	20		/* first unused code */
114 #define	REG_EBOL	21		/* ^ anchor and not BOL */
115 #define	REG_EEOL	22		/* $ anchor and not EOL */
116 #define	_REG_BACKREF_MAX 9		/* Max # of subexp. backreference */
117 
118 typedef struct {		/* regcomp() data saved for regexec() */
119 	size_t  re_nsub;	/* # of subexpressions in RE pattern */
120 
121 	/*
122 	 * Internal use only
123 	 */
124 	void	*re_comp;	/* compiled RE; freed by regfree() */
125 	int	re_cflags;	/* saved cflags for regexec() */
126 	size_t	re_erroff;	/* RE pattern error offset */
127 	size_t	re_len;		/* # wchar_t chars in compiled pattern */
128 	struct _regex_ext_t *re_sc;	/* for binary compatibility */
129 } regex_t;
130 
131 /* subexpression positions */
132 typedef struct {
133 #ifdef __STDC__
134 	const char	*rm_sp, *rm_ep;	/* Start pointer, end pointer */
135 #else
136 	char		*rm_sp, *rm_ep;	/* Start pointer, end pointer */
137 #endif
138 	regoff_t	rm_so, rm_eo;	/* Start offset, end offset */
139 	int		rm_ss, rm_es;	/* Used internally */
140 } regmatch_t;
141 
142 
143 /*
144  * Additional API and structs to support regular expression manipulations
145  * on wide characters.
146  */
147 
148 #if defined(__STDC__)
149 
150 extern int regcomp(regex_t *_RESTRICT_KYWD, const char *_RESTRICT_KYWD, int);
151 extern int regexec(const regex_t *_RESTRICT_KYWD, const char *_RESTRICT_KYWD,
152 	size_t, regmatch_t *_RESTRICT_KYWD, int);
153 extern size_t regerror(int, const regex_t *_RESTRICT_KYWD,
154 	char *_RESTRICT_KYWD, size_t);
155 extern void regfree(regex_t *);
156 
157 #else  /* defined(__STDC__) */
158 
159 extern int regcomp();
160 extern int regexec();
161 extern size_t regerror();
162 extern void regfree();
163 
164 #endif  /* defined(__STDC__) */
165 
166 #ifdef	__cplusplus
167 }
168 #endif
169 
170 #endif	/* _REGEX_H */
171