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

Annotation of mandoc/man_action.c, Revision 1.18

1.18    ! kristaps    1: /*     $Id: man_action.c,v 1.17 2009/08/20 11:51:07 kristaps Exp $ */
1.1       kristaps    2: /*
1.11      kristaps    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
1.10      kristaps    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.10      kristaps    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: #include <sys/utsname.h>
                     18:
                     19: #include <assert.h>
                     20: #include <errno.h>
                     21: #include <stdlib.h>
                     22: #include <string.h>
                     23:
                     24: #include "libman.h"
                     25:
                     26: struct actions {
                     27:        int     (*post)(struct man *);
                     28: };
                     29:
1.2       kristaps   30: static int       post_TH(struct man *);
1.18    ! kristaps   31: static int       post_fi(struct man *);
        !            32: static int       post_nf(struct man *);
1.2       kristaps   33:
1.1       kristaps   34: const  struct actions man_actions[MAN_MAX] = {
1.12      kristaps   35:        { NULL }, /* br */
1.2       kristaps   36:        { post_TH }, /* TH */
1.1       kristaps   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 */
1.5       kristaps   55:        { NULL }, /* RI */
1.8       kristaps   56:        { NULL }, /* na */
1.9       kristaps   57:        { NULL }, /* i */
1.14      kristaps   58:        { NULL }, /* sp */
1.18    ! kristaps   59:        { post_nf }, /* nf */
        !            60:        { post_fi }, /* fi */
1.16      kristaps   61:        { NULL }, /* r */
                     62:        { NULL }, /* RE */
                     63:        { NULL }, /* RS */
1.17      kristaps   64:        { NULL }, /* DT */
1.1       kristaps   65: };
                     66:
1.18    ! kristaps   67: static time_t    man_atotime(const char *);
        !            68: #ifdef __linux__
        !            69: extern char     *strptime(const char *, const char *, struct tm *);
        !            70: #endif
        !            71:
1.1       kristaps   72:
                     73: int
                     74: man_action_post(struct man *m)
                     75: {
                     76:
                     77:        if (MAN_ACTED & m->last->flags)
                     78:                return(1);
                     79:        m->last->flags |= MAN_ACTED;
                     80:
                     81:        switch (m->last->type) {
                     82:        case (MAN_TEXT):
1.18    ! kristaps   83:                /* FALLTHROUGH */
1.1       kristaps   84:        case (MAN_ROOT):
1.18    ! kristaps   85:                return(1);
        !            86:        default:
1.1       kristaps   87:                break;
                     88:        }
1.18    ! kristaps   89:
        !            90:        if (NULL == man_actions[m->last->tok].post)
        !            91:                return(1);
        !            92:        return((*man_actions[m->last->tok].post)(m));
        !            93: }
        !            94:
        !            95:
        !            96: static int
        !            97: post_fi(struct man *m)
        !            98: {
        !            99:
        !           100:        if ( ! (MAN_LITERAL & m->flags))
        !           101:                if ( ! man_nwarn(m, m->last, WNLITERAL))
        !           102:                        return(0);
        !           103:        m->flags &= ~MAN_LITERAL;
        !           104:        return(1);
        !           105: }
        !           106:
        !           107:
        !           108: static int
        !           109: post_nf(struct man *m)
        !           110: {
        !           111:
        !           112:        if (MAN_LITERAL & m->flags)
        !           113:                if ( ! man_nwarn(m, m->last, WOLITERAL))
        !           114:                        return(0);
        !           115:        m->flags |= MAN_LITERAL;
1.1       kristaps  116:        return(1);
                    117: }
                    118:
1.2       kristaps  119:
1.1       kristaps  120: static int
1.2       kristaps  121: post_TH(struct man *m)
1.1       kristaps  122: {
1.2       kristaps  123:        struct man_node *n;
                    124:        char            *ep;
                    125:        long             lval;
1.1       kristaps  126:
                    127:        if (m->meta.title)
                    128:                free(m->meta.title);
                    129:        if (m->meta.vol)
                    130:                free(m->meta.vol);
1.2       kristaps  131:        if (m->meta.source)
                    132:                free(m->meta.source);
1.1       kristaps  133:
1.2       kristaps  134:        m->meta.title = m->meta.vol = m->meta.source = NULL;
1.1       kristaps  135:        m->meta.msec = 0;
1.2       kristaps  136:        m->meta.date = 0;
                    137:
                    138:        /* ->TITLE<- MSEC DATE SOURCE VOL */
                    139:
                    140:        n = m->last->child;
                    141:        assert(n);
1.1       kristaps  142:
1.2       kristaps  143:        if (NULL == (m->meta.title = strdup(n->string)))
1.13      kristaps  144:                return(man_nerr(m, n, WNMEM));
1.2       kristaps  145:
                    146:        /* TITLE ->MSEC<- DATE SOURCE VOL */
1.1       kristaps  147:
1.2       kristaps  148:        n = n->next;
                    149:        assert(n);
1.1       kristaps  150:
1.2       kristaps  151:        errno = 0;
                    152:        lval = strtol(n->string, &ep, 10);
                    153:        if (n->string[0] != '\0' && *ep == '\0')
                    154:                m->meta.msec = (int)lval;
1.13      kristaps  155:        else if ( ! man_nwarn(m, n, WMSEC))
1.2       kristaps  156:                return(0);
1.1       kristaps  157:
1.2       kristaps  158:        /* TITLE MSEC ->DATE<- SOURCE VOL */
1.1       kristaps  159:
1.5       kristaps  160:        if (NULL == (n = n->next))
1.2       kristaps  161:                m->meta.date = time(NULL);
1.5       kristaps  162:        else if (0 == (m->meta.date = man_atotime(n->string))) {
1.13      kristaps  163:                if ( ! man_nwarn(m, n, WDATE))
1.2       kristaps  164:                        return(0);
                    165:                m->meta.date = time(NULL);
                    166:        }
1.1       kristaps  167:
1.2       kristaps  168:        /* TITLE MSEC DATE ->SOURCE<- VOL */
1.1       kristaps  169:
1.5       kristaps  170:        if (n && (n = n->next))
1.2       kristaps  171:                if (NULL == (m->meta.source = strdup(n->string)))
1.13      kristaps  172:                        return(man_nerr(m, n, WNMEM));
1.1       kristaps  173:
1.2       kristaps  174:        /* TITLE MSEC DATE SOURCE ->VOL<- */
1.1       kristaps  175:
1.5       kristaps  176:        if (n && (n = n->next))
1.2       kristaps  177:                if (NULL == (m->meta.vol = strdup(n->string)))
1.13      kristaps  178:                        return(man_nerr(m, n, WNMEM));
1.1       kristaps  179:
                    180:        /*
                    181:         * The end document shouldn't have the prologue macros as part
                    182:         * of the syntax tree (they encompass only meta-data).
                    183:         */
                    184:
1.4       kristaps  185:        if (m->last->parent->child == m->last) {
                    186:                m->last->parent->child = NULL;
                    187:                n = m->last;
                    188:                m->last = m->last->parent;
                    189:                m->next = MAN_NEXT_CHILD;
                    190:        } else {
                    191:                assert(m->last->prev);
                    192:                m->last->prev->next = NULL;
                    193:                n = m->last;
                    194:                m->last = m->last->prev;
                    195:                m->next = MAN_NEXT_SIBLING;
                    196:        }
1.2       kristaps  197:
                    198:        man_node_freelist(n);
                    199:        return(1);
                    200: }
1.1       kristaps  201:
                    202:
1.2       kristaps  203: static time_t
                    204: man_atotime(const char *p)
                    205: {
                    206:        struct tm        tm;
                    207:        char            *pp;
                    208:
1.13      kristaps  209:        bzero(&tm, sizeof(struct tm));
1.2       kristaps  210:
                    211:        if ((pp = strptime(p, "%b %d %Y", &tm)) && 0 == *pp)
                    212:                return(mktime(&tm));
                    213:        if ((pp = strptime(p, "%d %b %Y", &tm)) && 0 == *pp)
                    214:                return(mktime(&tm));
                    215:        if ((pp = strptime(p, "%b %d, %Y", &tm)) && 0 == *pp)
                    216:                return(mktime(&tm));
                    217:        if ((pp = strptime(p, "%b %Y", &tm)) && 0 == *pp)
                    218:                return(mktime(&tm));
1.1       kristaps  219:
1.2       kristaps  220:        return(0);
1.1       kristaps  221: }

CVSweb