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

Annotation of mandoc/man_action.c, Revision 1.22

1.22    ! kristaps    1: /*     $Id: man_action.c,v 1.21 2009/10/27 08:26:12 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"
1.22    ! kristaps   25: #include "libmandoc.h"
1.1       kristaps   26:
                     27: struct actions {
                     28:        int     (*post)(struct man *);
                     29: };
                     30:
1.2       kristaps   31: static int       post_TH(struct man *);
1.18      kristaps   32: static int       post_fi(struct man *);
                     33: static int       post_nf(struct man *);
1.2       kristaps   34:
1.1       kristaps   35: const  struct actions man_actions[MAN_MAX] = {
1.12      kristaps   36:        { NULL }, /* br */
1.2       kristaps   37:        { post_TH }, /* TH */
1.1       kristaps   38:        { NULL }, /* SH */
                     39:        { NULL }, /* SS */
                     40:        { NULL }, /* TP */
                     41:        { NULL }, /* LP */
                     42:        { NULL }, /* PP */
                     43:        { NULL }, /* P */
                     44:        { NULL }, /* IP */
                     45:        { NULL }, /* HP */
                     46:        { NULL }, /* SM */
                     47:        { NULL }, /* SB */
                     48:        { NULL }, /* BI */
                     49:        { NULL }, /* IB */
                     50:        { NULL }, /* BR */
                     51:        { NULL }, /* RB */
                     52:        { NULL }, /* R */
                     53:        { NULL }, /* B */
                     54:        { NULL }, /* I */
                     55:        { NULL }, /* IR */
1.5       kristaps   56:        { NULL }, /* RI */
1.8       kristaps   57:        { NULL }, /* na */
1.9       kristaps   58:        { NULL }, /* i */
1.14      kristaps   59:        { NULL }, /* sp */
1.18      kristaps   60:        { post_nf }, /* nf */
                     61:        { post_fi }, /* fi */
1.16      kristaps   62:        { NULL }, /* r */
                     63:        { NULL }, /* RE */
                     64:        { NULL }, /* RS */
1.17      kristaps   65:        { NULL }, /* DT */
1.19      kristaps   66:        { NULL }, /* UC */
1.20      kristaps   67:        { NULL }, /* PD */
1.1       kristaps   68: };
                     69:
1.18      kristaps   70: static time_t    man_atotime(const char *);
                     71: #ifdef __linux__
                     72: extern char     *strptime(const char *, const char *, struct tm *);
                     73: #endif
                     74:
1.1       kristaps   75:
                     76: int
                     77: man_action_post(struct man *m)
                     78: {
                     79:
                     80:        if (MAN_ACTED & m->last->flags)
                     81:                return(1);
                     82:        m->last->flags |= MAN_ACTED;
                     83:
                     84:        switch (m->last->type) {
                     85:        case (MAN_TEXT):
1.18      kristaps   86:                /* FALLTHROUGH */
1.1       kristaps   87:        case (MAN_ROOT):
1.18      kristaps   88:                return(1);
                     89:        default:
1.1       kristaps   90:                break;
                     91:        }
1.18      kristaps   92:
                     93:        if (NULL == man_actions[m->last->tok].post)
                     94:                return(1);
                     95:        return((*man_actions[m->last->tok].post)(m));
                     96: }
                     97:
                     98:
                     99: static int
                    100: post_fi(struct man *m)
                    101: {
                    102:
                    103:        if ( ! (MAN_LITERAL & m->flags))
                    104:                if ( ! man_nwarn(m, m->last, WNLITERAL))
                    105:                        return(0);
                    106:        m->flags &= ~MAN_LITERAL;
                    107:        return(1);
                    108: }
                    109:
                    110:
                    111: static int
                    112: post_nf(struct man *m)
                    113: {
                    114:
                    115:        if (MAN_LITERAL & m->flags)
                    116:                if ( ! man_nwarn(m, m->last, WOLITERAL))
                    117:                        return(0);
                    118:        m->flags |= MAN_LITERAL;
1.1       kristaps  119:        return(1);
                    120: }
                    121:
1.2       kristaps  122:
1.1       kristaps  123: static int
1.2       kristaps  124: post_TH(struct man *m)
1.1       kristaps  125: {
1.2       kristaps  126:        struct man_node *n;
                    127:        char            *ep;
                    128:        long             lval;
1.1       kristaps  129:
                    130:        if (m->meta.title)
                    131:                free(m->meta.title);
                    132:        if (m->meta.vol)
                    133:                free(m->meta.vol);
1.2       kristaps  134:        if (m->meta.source)
                    135:                free(m->meta.source);
1.1       kristaps  136:
1.2       kristaps  137:        m->meta.title = m->meta.vol = m->meta.source = NULL;
1.1       kristaps  138:        m->meta.msec = 0;
1.2       kristaps  139:        m->meta.date = 0;
                    140:
                    141:        /* ->TITLE<- MSEC DATE SOURCE VOL */
                    142:
                    143:        n = m->last->child;
                    144:        assert(n);
1.22    ! kristaps  145:        m->meta.title = mandoc_strdup(n->string);
1.2       kristaps  146:
                    147:        /* TITLE ->MSEC<- DATE SOURCE VOL */
1.1       kristaps  148:
1.2       kristaps  149:        n = n->next;
                    150:        assert(n);
1.1       kristaps  151:
1.2       kristaps  152:        errno = 0;
                    153:        lval = strtol(n->string, &ep, 10);
                    154:        if (n->string[0] != '\0' && *ep == '\0')
                    155:                m->meta.msec = (int)lval;
1.13      kristaps  156:        else if ( ! man_nwarn(m, n, WMSEC))
1.2       kristaps  157:                return(0);
1.1       kristaps  158:
1.2       kristaps  159:        /* TITLE MSEC ->DATE<- SOURCE VOL */
1.1       kristaps  160:
1.5       kristaps  161:        if (NULL == (n = n->next))
1.2       kristaps  162:                m->meta.date = time(NULL);
1.5       kristaps  163:        else if (0 == (m->meta.date = man_atotime(n->string))) {
1.13      kristaps  164:                if ( ! man_nwarn(m, n, WDATE))
1.2       kristaps  165:                        return(0);
                    166:                m->meta.date = time(NULL);
                    167:        }
1.1       kristaps  168:
1.2       kristaps  169:        /* TITLE MSEC DATE ->SOURCE<- VOL */
1.1       kristaps  170:
1.5       kristaps  171:        if (n && (n = n->next))
1.22    ! kristaps  172:                m->meta.source = mandoc_strdup(n->string);
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.22    ! kristaps  177:                m->meta.vol = mandoc_strdup(n->string);
1.1       kristaps  178:
                    179:        /*
                    180:         * The end document shouldn't have the prologue macros as part
                    181:         * of the syntax tree (they encompass only meta-data).
                    182:         */
                    183:
1.4       kristaps  184:        if (m->last->parent->child == m->last) {
                    185:                m->last->parent->child = NULL;
                    186:                n = m->last;
                    187:                m->last = m->last->parent;
                    188:                m->next = MAN_NEXT_CHILD;
                    189:        } else {
                    190:                assert(m->last->prev);
                    191:                m->last->prev->next = NULL;
                    192:                n = m->last;
                    193:                m->last = m->last->prev;
                    194:                m->next = MAN_NEXT_SIBLING;
                    195:        }
1.2       kristaps  196:
                    197:        man_node_freelist(n);
                    198:        return(1);
                    199: }
1.1       kristaps  200:
                    201:
1.2       kristaps  202: static time_t
                    203: man_atotime(const char *p)
                    204: {
                    205:        struct tm        tm;
                    206:        char            *pp;
                    207:
1.21      kristaps  208:        memset(&tm, 0, sizeof(struct tm));
1.2       kristaps  209:
                    210:        if ((pp = strptime(p, "%b %d %Y", &tm)) && 0 == *pp)
                    211:                return(mktime(&tm));
                    212:        if ((pp = strptime(p, "%d %b %Y", &tm)) && 0 == *pp)
                    213:                return(mktime(&tm));
                    214:        if ((pp = strptime(p, "%b %d, %Y", &tm)) && 0 == *pp)
                    215:                return(mktime(&tm));
                    216:        if ((pp = strptime(p, "%b %Y", &tm)) && 0 == *pp)
                    217:                return(mktime(&tm));
1.1       kristaps  218:
1.2       kristaps  219:        return(0);
1.1       kristaps  220: }

CVSweb