[BACK]Return to tbl.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/tbl.c, Revision 1.3

1.3     ! kristaps    1: /*     $Id: tbl.c,v 1.2 2010/12/28 10:55:24 kristaps Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2009, 2010 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 <assert.h>
1.3     ! kristaps   18: #include <stdio.h>
1.1       kristaps   19: #include <stdlib.h>
                     20: #include <string.h>
                     21: #include <time.h>
                     22:
                     23: #include "mandoc.h"
                     24: #include "roff.h"
                     25: #include "libmandoc.h"
                     26: #include "libroff.h"
                     27:
1.3     ! kristaps   28: static const char       tbl_toks[TBL_TOK__MAX] = {
        !            29:        '(',    ')',    ',',    ';',    '.',
        !            30:        ' ',    '\t',   '\0'
1.1       kristaps   31: };
                     32:
                     33: static void             tbl_init(struct tbl *);
                     34: static void             tbl_clear(struct tbl *);
1.3     ! kristaps   35: static enum tbl_tok     tbl_next_char(char);
1.1       kristaps   36:
                     37: static void
                     38: tbl_clear(struct tbl *tbl)
                     39: {
                     40:
                     41: }
                     42:
                     43: static void
                     44: tbl_init(struct tbl *tbl)
                     45: {
                     46:
                     47:        tbl->part = TBL_PART_OPTS;
                     48: }
                     49:
                     50: enum rofferr
                     51: tbl_read(struct tbl *tbl, int ln, const char *p, int offs)
                     52: {
                     53:        int              len;
                     54:        const char      *cp;
                     55:
                     56:        cp = &p[offs];
                     57:        len = (int)strlen(cp);
                     58:
                     59:        if (len && TBL_PART_OPTS == tbl->part)
                     60:                if (';' != cp[len - 1])
                     61:                        tbl->part = TBL_PART_LAYOUT;
                     62:
1.2       kristaps   63:        return(ROFF_CONT);
1.1       kristaps   64: }
                     65:
                     66: struct tbl *
                     67: tbl_alloc(void)
                     68: {
                     69:        struct tbl      *p;
                     70:
                     71:        p = mandoc_malloc(sizeof(struct tbl));
                     72:        tbl_init(p);
                     73:        return(p);
                     74: }
                     75:
                     76: void
                     77: tbl_free(struct tbl *p)
                     78: {
                     79:
                     80:        tbl_clear(p);
                     81:        free(p);
                     82: }
                     83:
                     84: void
                     85: tbl_reset(struct tbl *tbl)
                     86: {
                     87:
                     88:        tbl_clear(tbl);
                     89:        tbl_init(tbl);
                     90: }
1.3     ! kristaps   91:
        !            92: static enum tbl_tok
        !            93: tbl_next_char(char c)
        !            94: {
        !            95:        int              i;
        !            96:
        !            97:        /*
        !            98:         * These are delimiting tokens.  They separate out words in the
        !            99:         * token stream.
        !           100:         *
        !           101:         * FIXME: make this into a hashtable for faster lookup.
        !           102:         */
        !           103:        for (i = 0; i < TBL_TOK__MAX; i++)
        !           104:                if (c == tbl_toks[i])
        !           105:                        return((enum tbl_tok)i);
        !           106:
        !           107:        return(TBL_TOK__MAX);
        !           108: }
        !           109:
        !           110: enum tbl_tok
        !           111: tbl_next(struct tbl *tbl, const char *p, int *pos)
        !           112: {
        !           113:        int              i;
        !           114:        enum tbl_tok     c;
        !           115:
        !           116:        tbl->buf[0] = '\0';
        !           117:
        !           118:        if (TBL_TOK__MAX != (c = tbl_next_char(p[*pos]))) {
        !           119:                if (TBL_TOK_NIL != c) {
        !           120:                        tbl->buf[0] = p[*pos];
        !           121:                        tbl->buf[1] = '\0';
        !           122:                        (*pos)++;
        !           123:                }
        !           124:                return(c);
        !           125:        }
        !           126:
        !           127:        /*
        !           128:         * Copy words into a nil-terminated buffer.  For now, we use a
        !           129:         * static buffer.  FIXME: eventually this should be made into a
        !           130:         * dynamic one living in struct tbl.
        !           131:         */
        !           132:
        !           133:        for (i = 0; i < BUFSIZ; i++, (*pos)++)
        !           134:                if (TBL_TOK__MAX == tbl_next_char(p[*pos]))
        !           135:                        tbl->buf[i] = p[*pos];
        !           136:                else
        !           137:                        break;
        !           138:
        !           139:        assert(i < BUFSIZ);
        !           140:        tbl->buf[i] = '\0';
        !           141:
        !           142:        return(TBL_TOK__MAX);
        !           143: }
        !           144:
        !           145:

CVSweb