Annotation of embedaddon/libpdel/tmpl/tmpl.c, revision 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 "tmpl_internal.h"
! 42:
! 43: /* Internal functions */
! 44: static struct tmpl *tmpl_create_internal(FILE *input, void *addr,
! 45: size_t len, int *num_errors, const char *mtype);
! 46:
! 47: /*
! 48: * Create a new template object.
! 49: */
! 50: struct tmpl *
! 51: tmpl_create(FILE *input, int *num_errors, const char *mtype)
! 52: {
! 53: return (tmpl_create_internal(input, NULL, 0, num_errors, mtype));
! 54: }
! 55:
! 56: /*
! 57: * Create a new template object using mmap().
! 58: */
! 59: struct tmpl *
! 60: tmpl_create_mmap(const char *path, int *num_errors, const char *mtype)
! 61: {
! 62: struct tmpl *tmpl;
! 63: struct stat sb;
! 64: void *addr;
! 65: int esave;
! 66: FILE *fp;
! 67: int fd;
! 68:
! 69: /* Open and memory map file */
! 70: if ((fd = open(path, O_RDONLY)) == -1)
! 71: return (NULL);
! 72: if (fstat(fd, &sb) == -1) {
! 73: close(fd);
! 74: return (NULL);
! 75: }
! 76: if ((addr = mmap(NULL, sb.st_size,
! 77: PROT_READ, MAP_SHARED, fd, 0)) == NULL) {
! 78: close(fd);
! 79: return (NULL);
! 80: }
! 81: close(fd);
! 82:
! 83: /* Parse file */
! 84: if ((fp = fopen(path, "r")) == NULL)
! 85: return (NULL);
! 86: if ((tmpl = tmpl_create_internal(fp, addr,
! 87: sb.st_size, num_errors, mtype)) == NULL) {
! 88: esave = errno;
! 89: fclose(fp);
! 90: errno = esave;
! 91: return (NULL);
! 92: }
! 93:
! 94: /* Done */
! 95: return (tmpl);
! 96: }
! 97:
! 98: /*
! 99: * Create a new template object.
! 100: */
! 101: static struct tmpl *
! 102: tmpl_create_internal(FILE *input, void *addr, size_t len,
! 103: int *num_errors, const char *mtype)
! 104: {
! 105: struct tmpl *tmpl;
! 106: int dummy;
! 107: int r;
! 108:
! 109: /* Sanity check */
! 110: if (input == NULL) {
! 111: errno = EINVAL;
! 112: return (NULL);
! 113: }
! 114: if (num_errors == NULL)
! 115: num_errors = &dummy;
! 116:
! 117: /* Initialize template object */
! 118: if ((tmpl = MALLOC(mtype, sizeof(*tmpl))) == NULL)
! 119: return (NULL);
! 120: memset(tmpl, 0, sizeof(*tmpl));
! 121: tmpl->mmap_addr = addr;
! 122: tmpl->mmap_len = len;
! 123: if (mtype != NULL) {
! 124: strlcpy(tmpl->mtype_buf, mtype, sizeof(tmpl->mtype_buf));
! 125: tmpl->mtype = tmpl->mtype_buf;
! 126: }
! 127:
! 128: /* Parse template; cleanup if thread is canceled */
! 129: pthread_cleanup_push((void (*)(void *))tmpl_destroy, &tmpl);
! 130: r = _tmpl_parse(tmpl, input, num_errors);
! 131: pthread_cleanup_pop(0);
! 132:
! 133: /* Check for error */
! 134: if (r == -1) {
! 135: tmpl_destroy(&tmpl);
! 136: return (NULL);
! 137: }
! 138:
! 139: /* Done */
! 140: return (tmpl);
! 141: }
! 142:
! 143: /*
! 144: * Destroy a template object.
! 145: */
! 146: void
! 147: tmpl_destroy(struct tmpl **tmplp)
! 148: {
! 149: struct tmpl *const tmpl = *tmplp;
! 150:
! 151: if (tmpl == NULL)
! 152: return;
! 153: *tmplp = NULL;
! 154: _tmpl_free_elems(tmpl->mtype,
! 155: tmpl->eblock, tmpl->elems, tmpl->num_elems);
! 156: if (tmpl->mmap_addr != NULL)
! 157: munmap(tmpl->mmap_addr, tmpl->mmap_len);
! 158: FREE(tmpl->mtype, tmpl);
! 159: }
! 160:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>