Annotation of embedaddon/libpdel/structs/type/structs_type_regex.c, revision 1.1.1.1

1.1       misho       1: 
                      2: /*
                      3:  * Copyright (c) 2001-2002 Packet Design, LLC.
                      4:  * All rights reserved.
                      5:  * 
                      6:  * Subject to the following obligations and disclaimer of warranty,
                      7:  * use and redistribution of this software, in source or object code
                      8:  * forms, with or without modifications are expressly permitted by
                      9:  * Packet Design; provided, however, that:
                     10:  * 
                     11:  *    (i)  Any and all reproductions of the source or object code
                     12:  *         must include the copyright notice above and the following
                     13:  *         disclaimer of warranties; and
                     14:  *    (ii) No rights are granted, in any manner or form, to use
                     15:  *         Packet Design trademarks, including the mark "PACKET DESIGN"
                     16:  *         on advertising, endorsements, or otherwise except as such
                     17:  *         appears in the above copyright notice or in the software.
                     18:  * 
                     19:  * THIS SOFTWARE IS BEING PROVIDED BY PACKET DESIGN "AS IS", AND
                     20:  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, PACKET DESIGN MAKES NO
                     21:  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING
                     22:  * THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED
                     23:  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
                     24:  * OR NON-INFRINGEMENT.  PACKET DESIGN DOES NOT WARRANT, GUARANTEE,
                     25:  * OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS
                     26:  * OF THE USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY,
                     27:  * RELIABILITY OR OTHERWISE.  IN NO EVENT SHALL PACKET DESIGN BE
                     28:  * LIABLE FOR ANY DAMAGES RESULTING FROM OR ARISING OUT OF ANY USE
                     29:  * OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY DIRECT,
                     30:  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL
                     31:  * DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF
                     32:  * USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY THEORY OF
                     33:  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     34:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
                     35:  * THE USE OF THIS SOFTWARE, EVEN IF PACKET DESIGN IS ADVISED OF
                     36:  * THE POSSIBILITY OF SUCH DAMAGE.
                     37:  *
                     38:  * Author: Archie Cobbs <archie@freebsd.org>
                     39:  */
                     40: 
                     41: #include <sys/types.h>
                     42: 
                     43: #include <stdlib.h>
                     44: #include <stdio.h>
                     45: #include <stdarg.h>
                     46: #include <string.h>
                     47: #include <errno.h>
                     48: #include <regex.h>
                     49: 
                     50: #include "structs/structs.h"
                     51: #include "structs/type/array.h"
                     52: #include "structs/type/string.h"
                     53: #include "structs/type/regex.h"
                     54: #include "util/typed_mem.h"
                     55: 
                     56: /* Pre-defined types */
                     57: const struct structs_type structs_type_regex =
                     58:        STRUCTS_REGEX_TYPE(STRUCTS_REGEX_MTYPE, REG_EXTENDED);
                     59: const struct structs_type structs_type_regex_icase =
                     60:        STRUCTS_REGEX_TYPE(STRUCTS_REGEX_MTYPE, REG_EXTENDED | REG_ICASE);
                     61: 
                     62: int
                     63: structs_regex_equal(const struct structs_type *type,
                     64:        const void *v1, const void *v2)
                     65: {
                     66:        const struct structs_regex *const r1 = v1;
                     67:        const struct structs_regex *const r2 = v2;
                     68: 
                     69:        if (r1->pat == NULL)
                     70:                return (r2->pat == NULL);
                     71:        if (r2->pat == NULL)
                     72:                return (0);
                     73:        return (strcmp(r1->pat, r2->pat) == 0);
                     74: }
                     75: 
                     76: char *
                     77: structs_regex_ascify(const struct structs_type *type,
                     78:        const char *mtype, const void *data)
                     79: {
                     80:        const struct structs_regex *const r = data;
                     81: 
                     82:        return (STRDUP(mtype, (r->pat != NULL) ? r->pat : ""));
                     83: }
                     84: 
                     85: int
                     86: structs_regex_binify(const struct structs_type *type,
                     87:        const char *ascii, void *data, char *ebuf, size_t emax)
                     88: {
                     89:        struct structs_regex *const r = data;
                     90:        const char *mtype = type->args[0].s;
                     91:        const int flags = type->args[1].i;
                     92:        int errno_save;
                     93:        int err;
                     94: 
                     95:        /* Empty string? */
                     96:        if (*ascii == '\0') {
                     97:                memset(r, 0, sizeof(*r));
                     98:                return (0);
                     99:        }
                    100: 
                    101:        /* Compile pattern */
                    102:        if ((err = regcomp(&r->reg, ascii, flags)) != 0) {
                    103:                regerror(err, &r->reg, ebuf, emax);
                    104:                switch (err) {
                    105:                case REG_ESPACE:
                    106:                        errno = ENOMEM;
                    107:                        break;
                    108:                default:
                    109:                        errno = EINVAL;
                    110:                        break;
                    111:                }
                    112:                return (-1);
                    113:        }
                    114: 
                    115:        /* Save a copy of the pattern string */
                    116:        if ((r->pat = STRDUP(mtype, ascii)) == NULL) {
                    117:                errno_save = errno;
                    118:                regfree(&r->reg);
                    119:                errno = errno_save;
                    120:                return (-1);
                    121:        }
                    122: 
                    123:        /* OK */
                    124:        return (0);
                    125: }
                    126: 
                    127: void
                    128: structs_regex_free(const struct structs_type *type, void *data)
                    129: {
                    130:        const char *mtype = type->args[0].s;
                    131:        struct structs_regex *const r = data;
                    132: 
                    133:        if (r->pat != NULL) {
                    134:                FREE(mtype, (char *)r->pat);
                    135:                regfree(&r->reg);
                    136:                memset(r, 0, sizeof(*r));
                    137:        }
                    138: }
                    139: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>