xref: /illumos-gate/usr/src/lib/libeti/form/common/ty_regexp.c (revision 533affcbc7fc4d0c8132976ea454aaa715fe2307)
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) 1988 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 /*LINTLIBRARY*/
32 
33 #include <sys/types.h>
34 #include <stdlib.h>
35 #include "utility.h"
36 
37 /*
38  *	TYPE_REGEXP standard type
39  *
40  *	usage:
41  *		set_field_type(f, TYPE_REGEXP, expression);
42  *
43  *		char * expression;	regular expression regcmp(3C)
44  */
45 extern char *libform_regcmp(char *, char *);
46 extern char *libform_regex(char *, char *, char *);
47 static char *make_rexp(va_list *);
48 static char *copy_rexp(char *);
49 static void free_rexp(char *);
50 static int fcheck_rexp(FIELD *, char *);
51 
52 static FIELDTYPE typeREGEXP =
53 {
54 				ARGS,			/* status	*/
55 				1,			/* ref		*/
56 				(FIELDTYPE *) 0,	/* left		*/
57 				(FIELDTYPE *) 0,	/* right	*/
58 				make_rexp,		/* makearg	*/
59 				copy_rexp,		/* copyarg	*/
60 				free_rexp,		/* freearg	*/
61 				fcheck_rexp,		/* fcheck	*/
62 				(PTF_int) 0,		/* ccheck	*/
63 				(PTF_int) 0,		/* next		*/
64 				(PTF_int) 0,		/* prev		*/
65 };
66 
67 FIELDTYPE * TYPE_REGEXP = &typeREGEXP;
68 
69 static char *
70 make_rexp(va_list *ap)
71 {
72 	return (libform_regcmp(va_arg(*ap, char *), NULL));
73 					/* (...)$n will dump core */
74 }
75 
76 static char *
77 copy_rexp(char *arg)
78 {
79 	char *rexp;
80 
81 	if (arrayAlloc(rexp, (strlen(arg) + 1), char))
82 		(void) strcpy(rexp, arg);
83 	return (rexp);
84 }
85 
86 static void
87 free_rexp(char *arg)
88 {
89 	Free(arg);
90 }
91 
92 static int
93 fcheck_rexp(FIELD *f, char *arg)
94 {
95 	return (libform_regex(arg, field_buffer(f, 0), NULL) ? TRUE : FALSE);
96 }
97