Annotation of src/usr.bin/mandoc/tbl_data.c, Revision 1.1
1.1 ! schwarze 1: /* $Id: data.c,v 1.11 2009/09/12 16:05:34 kristaps Exp $ */
! 2: /*
! 3: * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>
! 4: *
! 5: * Permission to use, copy, modify, and distribute this software for any
! 6: * purpose with or without fee is hereby granted, provided that the above
! 7: * copyright notice and this permission notice appear in all copies.
! 8: *
! 9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
! 10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
! 11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
! 12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
! 13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
! 14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
! 15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
! 16: */
! 17: #include <sys/queue.h>
! 18: #include <sys/types.h>
! 19:
! 20: #include <assert.h>
! 21: #include <ctype.h>
! 22: #include <stdlib.h>
! 23: #include <string.h>
! 24:
! 25: #include "tbl_extern.h"
! 26:
! 27: /* FIXME: warn about losing data contents if cell is HORIZ. */
! 28:
! 29: static int data(struct tbl *, struct tbl_span *,
! 30: const char *, int, int,
! 31: const char *, int, int);
! 32:
! 33:
! 34: int
! 35: data(struct tbl *tbl, struct tbl_span *dp,
! 36: const char *f, int ln, int pos,
! 37: const char *p, int start, int end)
! 38: {
! 39: struct tbl_data *dat;
! 40:
! 41: if (NULL == (dat = tbl_data_alloc(dp)))
! 42: return(0);
! 43:
! 44: if (NULL == dat->cell)
! 45: if ( ! tbl_warnx(tbl, ERR_SYNTAX, f, ln, pos))
! 46: return(0);
! 47:
! 48: assert(end >= start);
! 49: if (NULL == (dat->string = malloc((size_t)(end - start + 1))))
! 50: return(tbl_err(tbl));
! 51:
! 52: (void)memcpy(dat->string, &p[start], (size_t)(end - start));
! 53: dat->string[end - start] = 0;
! 54:
! 55: /* XXX: do the strcmps, then malloc(). */
! 56:
! 57: if ( ! strcmp(dat->string, "_"))
! 58: dat->flags |= TBL_DATA_HORIZ;
! 59: else if ( ! strcmp(dat->string, "="))
! 60: dat->flags |= TBL_DATA_DHORIZ;
! 61: else if ( ! strcmp(dat->string, "\\_"))
! 62: dat->flags |= TBL_DATA_NHORIZ;
! 63: else if ( ! strcmp(dat->string, "\\="))
! 64: dat->flags |= TBL_DATA_NDHORIZ;
! 65: else
! 66: return(1);
! 67:
! 68: free(dat->string);
! 69: dat->string = NULL;
! 70: return(1);
! 71: }
! 72:
! 73:
! 74: int
! 75: tbl_data(struct tbl *tbl, const char *f, int ln, const char *p)
! 76: {
! 77: struct tbl_span *dp;
! 78: int i, j;
! 79:
! 80: if (0 == p[0])
! 81: return(tbl_errx(tbl, ERR_SYNTAX, f, ln, 0));
! 82:
! 83: if ('.' == p[0] && ! isdigit((u_char)p[1])) {
! 84: /*
! 85: * XXX: departs from tbl convention in that we disallow
! 86: * macros in the data body.
! 87: */
! 88: if (strncasecmp(p, ".T&", 3))
! 89: return(tbl_errx(tbl, ERR_SYNTAX, f, ln, 0));
! 90: return(tbl_data_close(tbl, f, ln));
! 91: }
! 92:
! 93: if (NULL == (dp = tbl_span_alloc(tbl)))
! 94: return(0);
! 95:
! 96: if ( ! strcmp(p, "_")) {
! 97: dp->flags |= TBL_SPAN_HORIZ;
! 98: return(1);
! 99: } else if ( ! strcmp(p, "=")) {
! 100: dp->flags |= TBL_SPAN_DHORIZ;
! 101: return(1);
! 102: }
! 103:
! 104: for (j = i = 0; p[i]; i++) {
! 105: if (p[i] != tbl->tab)
! 106: continue;
! 107: if ( ! data(tbl, dp, f, ln, i, p, j, i))
! 108: return(0);
! 109: j = i + 1;
! 110: }
! 111:
! 112: return(data(tbl, dp, f, ln, i, p, j, i));
! 113: }
! 114:
! 115:
! 116: int
! 117: tbl_data_close(struct tbl *tbl, const char *f, int ln)
! 118: {
! 119: struct tbl_span *span;
! 120:
! 121: /* LINTED */
! 122: span = TAILQ_LAST(&tbl->span, tbl_spanh);
! 123: if (NULL == span)
! 124: return(tbl_errx(tbl, ERR_SYNTAX, f, ln, 0));
! 125: if (TAILQ_NEXT(span->row, entries))
! 126: return(tbl_errx(tbl, ERR_SYNTAX, f, ln, 0));
! 127:
! 128: tbl->part = TBL_PART_LAYOUT;
! 129: return(1);
! 130: }