[BACK]Return to tbl_data.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/tbl_data.c, Revision 1.2

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:
1.2     ! schwarze   25: #include "out.h"
        !            26: #include "term.h"
1.1       schwarze   27: #include "tbl_extern.h"
                     28:
                     29: /* FIXME: warn about losing data contents if cell is HORIZ. */
                     30:
                     31: static int             data(struct tbl *, struct tbl_span *,
                     32:                                const char *, int, int,
                     33:                                const char *, int, int);
                     34:
                     35:
                     36: int
                     37: data(struct tbl *tbl, struct tbl_span *dp,
                     38:                const char *f, int ln, int pos,
                     39:                const char *p, int start, int end)
                     40: {
                     41:        struct tbl_data *dat;
                     42:
                     43:        if (NULL == (dat = tbl_data_alloc(dp)))
                     44:                return(0);
                     45:
                     46:        if (NULL == dat->cell)
                     47:                if ( ! tbl_warnx(tbl, ERR_SYNTAX, f, ln, pos))
                     48:                        return(0);
                     49:
                     50:        assert(end >= start);
                     51:        if (NULL == (dat->string = malloc((size_t)(end - start + 1))))
                     52:                return(tbl_err(tbl));
                     53:
                     54:        (void)memcpy(dat->string, &p[start], (size_t)(end - start));
                     55:        dat->string[end - start] = 0;
                     56:
                     57:        /* XXX: do the strcmps, then malloc(). */
                     58:
                     59:        if ( ! strcmp(dat->string, "_"))
                     60:                dat->flags |= TBL_DATA_HORIZ;
                     61:        else if ( ! strcmp(dat->string, "="))
                     62:                dat->flags |= TBL_DATA_DHORIZ;
                     63:        else if ( ! strcmp(dat->string, "\\_"))
                     64:                dat->flags |= TBL_DATA_NHORIZ;
                     65:        else if ( ! strcmp(dat->string, "\\="))
                     66:                dat->flags |= TBL_DATA_NDHORIZ;
                     67:        else
                     68:                return(1);
                     69:
                     70:        free(dat->string);
                     71:        dat->string = NULL;
                     72:        return(1);
                     73: }
                     74:
                     75:
                     76: int
                     77: tbl_data(struct tbl *tbl, const char *f, int ln, const char *p)
                     78: {
                     79:        struct tbl_span *dp;
                     80:        int              i, j;
                     81:
                     82:        if (0 == p[0])
                     83:                return(tbl_errx(tbl, ERR_SYNTAX, f, ln, 0));
                     84:
                     85:        if ('.' == p[0] && ! isdigit((u_char)p[1])) {
                     86:                /*
                     87:                 * XXX: departs from tbl convention in that we disallow
                     88:                 * macros in the data body.
                     89:                 */
                     90:                if (strncasecmp(p, ".T&", 3))
                     91:                        return(tbl_errx(tbl, ERR_SYNTAX, f, ln, 0));
                     92:                return(tbl_data_close(tbl, f, ln));
                     93:        }
                     94:
                     95:        if (NULL == (dp = tbl_span_alloc(tbl)))
                     96:                return(0);
                     97:
                     98:        if ( ! strcmp(p, "_")) {
                     99:                dp->flags |= TBL_SPAN_HORIZ;
                    100:                return(1);
                    101:        } else if ( ! strcmp(p, "=")) {
                    102:                dp->flags |= TBL_SPAN_DHORIZ;
                    103:                return(1);
                    104:        }
                    105:
                    106:        for (j = i = 0; p[i]; i++) {
                    107:                if (p[i] != tbl->tab)
                    108:                        continue;
                    109:                if ( ! data(tbl, dp, f, ln, i, p, j, i))
                    110:                        return(0);
                    111:                j = i + 1;
                    112:        }
                    113:
                    114:        return(data(tbl, dp, f, ln, i, p, j, i));
                    115: }
                    116:
                    117:
                    118: int
                    119: tbl_data_close(struct tbl *tbl, const char *f, int ln)
                    120: {
                    121:        struct tbl_span *span;
                    122:
                    123:        /* LINTED */
                    124:        span = TAILQ_LAST(&tbl->span, tbl_spanh);
                    125:        if (NULL == span)
                    126:                return(tbl_errx(tbl, ERR_SYNTAX, f, ln, 0));
                    127:        if (TAILQ_NEXT(span->row, entries))
                    128:                return(tbl_errx(tbl, ERR_SYNTAX, f, ln, 0));
                    129:
                    130:        tbl->part = TBL_PART_LAYOUT;
                    131:        return(1);
                    132: }