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

Annotation of src/usr.bin/mandoc/man_action.c, Revision 1.16

1.16    ! schwarze    1: /*     $Id: man_action.c,v 1.15 2010/04/25 16:32:19 schwarze Exp $ */
1.1       kristaps    2: /*
1.16    ! schwarze    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@bsd.lv>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
1.2       schwarze    6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    8:  *
1.2       schwarze    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.
1.1       kristaps   16:  */
                     17:
                     18: #include <assert.h>
                     19: #include <stdlib.h>
                     20: #include <string.h>
                     21:
                     22: #include "libman.h"
1.10      schwarze   23: #include "libmandoc.h"
1.1       kristaps   24:
                     25: struct actions {
                     26:        int     (*post)(struct man *);
                     27: };
                     28:
                     29: static int       post_TH(struct man *);
1.14      schwarze   30: static int       post_de(struct man *);
1.7       schwarze   31: static int       post_fi(struct man *);
                     32: static int       post_nf(struct man *);
1.1       kristaps   33:
                     34: const  struct actions man_actions[MAN_MAX] = {
1.3       schwarze   35:        { NULL }, /* br */
1.1       kristaps   36:        { post_TH }, /* TH */
                     37:        { NULL }, /* SH */
                     38:        { NULL }, /* SS */
                     39:        { NULL }, /* TP */
                     40:        { NULL }, /* LP */
                     41:        { NULL }, /* PP */
                     42:        { NULL }, /* P */
                     43:        { NULL }, /* IP */
                     44:        { NULL }, /* HP */
                     45:        { NULL }, /* SM */
                     46:        { NULL }, /* SB */
                     47:        { NULL }, /* BI */
                     48:        { NULL }, /* IB */
                     49:        { NULL }, /* BR */
                     50:        { NULL }, /* RB */
                     51:        { NULL }, /* R */
                     52:        { NULL }, /* B */
                     53:        { NULL }, /* I */
                     54:        { NULL }, /* IR */
                     55:        { NULL }, /* RI */
                     56:        { NULL }, /* na */
                     57:        { NULL }, /* i */
1.5       schwarze   58:        { NULL }, /* sp */
1.7       schwarze   59:        { post_nf }, /* nf */
                     60:        { post_fi }, /* fi */
                     61:        { NULL }, /* r */
                     62:        { NULL }, /* RE */
                     63:        { NULL }, /* RS */
                     64:        { NULL }, /* DT */
1.8       schwarze   65:        { NULL }, /* UC */
1.9       schwarze   66:        { NULL }, /* PD */
1.12      schwarze   67:        { NULL }, /* Sp */
                     68:        { post_nf }, /* Vb */
                     69:        { post_fi }, /* Ve */
1.14      schwarze   70:        { post_de }, /* de */
                     71:        { post_de }, /* dei */
                     72:        { post_de }, /* am */
                     73:        { post_de }, /* ami */
                     74:        { post_de }, /* ig */
                     75:        { NULL }, /* . */
1.15      schwarze   76:        { NULL }, /* if */
                     77:        { NULL }, /* ie */
                     78:        { NULL }, /* el */
1.1       kristaps   79: };
                     80:
                     81:
                     82: int
                     83: man_action_post(struct man *m)
                     84: {
                     85:
                     86:        if (MAN_ACTED & m->last->flags)
                     87:                return(1);
                     88:        m->last->flags |= MAN_ACTED;
                     89:
                     90:        switch (m->last->type) {
                     91:        case (MAN_TEXT):
1.7       schwarze   92:                /* FALLTHROUGH */
1.1       kristaps   93:        case (MAN_ROOT):
1.7       schwarze   94:                return(1);
                     95:        default:
1.1       kristaps   96:                break;
                     97:        }
1.7       schwarze   98:
                     99:        if (NULL == man_actions[m->last->tok].post)
                    100:                return(1);
                    101:        return((*man_actions[m->last->tok].post)(m));
                    102: }
                    103:
                    104:
                    105: static int
                    106: post_fi(struct man *m)
                    107: {
                    108:
                    109:        if ( ! (MAN_LITERAL & m->flags))
                    110:                if ( ! man_nwarn(m, m->last, WNLITERAL))
                    111:                        return(0);
                    112:        m->flags &= ~MAN_LITERAL;
                    113:        return(1);
                    114: }
                    115:
                    116:
                    117: static int
1.14      schwarze  118: post_de(struct man *m)
                    119: {
                    120:
                    121:        /*
                    122:         * XXX: for the time being, we indiscriminately remove roff
                    123:         * instructions from the parse stream.
                    124:         */
                    125:        if (MAN_BLOCK == m->last->type)
                    126:                man_node_delete(m, m->last);
                    127:        return(1);
                    128: }
                    129:
                    130:
                    131: static int
1.7       schwarze  132: post_nf(struct man *m)
                    133: {
                    134:
                    135:        if (MAN_LITERAL & m->flags)
                    136:                if ( ! man_nwarn(m, m->last, WOLITERAL))
                    137:                        return(0);
                    138:        m->flags |= MAN_LITERAL;
1.1       kristaps  139:        return(1);
                    140: }
                    141:
                    142:
                    143: static int
                    144: post_TH(struct man *m)
                    145: {
                    146:        struct man_node *n;
                    147:
                    148:        if (m->meta.title)
                    149:                free(m->meta.title);
                    150:        if (m->meta.vol)
                    151:                free(m->meta.vol);
                    152:        if (m->meta.source)
                    153:                free(m->meta.source);
1.16    ! schwarze  154:        if (m->meta.msec)
        !           155:                free(m->meta.msec);
1.1       kristaps  156:
1.16    ! schwarze  157:        m->meta.title = m->meta.vol =
        !           158:                m->meta.msec = m->meta.source = NULL;
1.1       kristaps  159:        m->meta.date = 0;
                    160:
                    161:        /* ->TITLE<- MSEC DATE SOURCE VOL */
                    162:
                    163:        n = m->last->child;
                    164:        assert(n);
1.10      schwarze  165:        m->meta.title = mandoc_strdup(n->string);
1.1       kristaps  166:
                    167:        /* TITLE ->MSEC<- DATE SOURCE VOL */
                    168:
                    169:        n = n->next;
                    170:        assert(n);
1.16    ! schwarze  171:        m->meta.msec = mandoc_strdup(n->string);
1.1       kristaps  172:
                    173:        /* TITLE MSEC ->DATE<- SOURCE VOL */
                    174:
1.11      schwarze  175:        n = n->next;
                    176:        if (n) {
                    177:                m->meta.date = mandoc_a2time
                    178:                        (MTIME_ISO_8601, n->string);
                    179:                if (0 == m->meta.date) {
                    180:                        if ( ! man_nwarn(m, n, WDATE))
                    181:                                return(0);
                    182:                        m->meta.date = time(NULL);
                    183:                }
                    184:        } else
1.1       kristaps  185:                m->meta.date = time(NULL);
                    186:
                    187:        /* TITLE MSEC DATE ->SOURCE<- VOL */
                    188:
                    189:        if (n && (n = n->next))
1.10      schwarze  190:                m->meta.source = mandoc_strdup(n->string);
1.1       kristaps  191:
                    192:        /* TITLE MSEC DATE SOURCE ->VOL<- */
                    193:
                    194:        if (n && (n = n->next))
1.10      schwarze  195:                m->meta.vol = mandoc_strdup(n->string);
1.1       kristaps  196:
1.14      schwarze  197:        /*
                    198:         * Remove the `TH' node after we've processed it for our
                    199:         * meta-data.
                    200:         */
                    201:        man_node_delete(m, m->last);
1.1       kristaps  202:        return(1);
                    203: }