[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.6

1.6     ! schwarze    1: /*     $Id: man_action.c,v 1.5 2009/08/22 15:15:37 schwarze Exp $ */
1.1       kristaps    2: /*
1.2       schwarze    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.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: #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:
                     30:
                     31: static int       post_TH(struct man *);
                     32: static time_t    man_atotime(const char *);
                     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.6     ! schwarze   59:        { NULL }, /* nf */
        !            60:        { NULL }, /* fi */
        !            61:        { NULL }, /* r*/
1.1       kristaps   62: };
                     63:
                     64:
                     65: int
                     66: man_action_post(struct man *m)
                     67: {
                     68:
                     69:        if (MAN_ACTED & m->last->flags)
                     70:                return(1);
                     71:        m->last->flags |= MAN_ACTED;
                     72:
                     73:        switch (m->last->type) {
                     74:        case (MAN_TEXT):
                     75:                break;
                     76:        case (MAN_ROOT):
                     77:                break;
                     78:        default:
                     79:                if (NULL == man_actions[m->last->tok].post)
                     80:                        break;
                     81:                return((*man_actions[m->last->tok].post)(m));
                     82:        }
                     83:        return(1);
                     84: }
                     85:
                     86:
                     87: static int
                     88: post_TH(struct man *m)
                     89: {
                     90:        struct man_node *n;
                     91:        char            *ep;
                     92:        long             lval;
                     93:
                     94:        if (m->meta.title)
                     95:                free(m->meta.title);
                     96:        if (m->meta.vol)
                     97:                free(m->meta.vol);
                     98:        if (m->meta.source)
                     99:                free(m->meta.source);
                    100:
                    101:        m->meta.title = m->meta.vol = m->meta.source = NULL;
                    102:        m->meta.msec = 0;
                    103:        m->meta.date = 0;
                    104:
                    105:        /* ->TITLE<- MSEC DATE SOURCE VOL */
                    106:
                    107:        n = m->last->child;
                    108:        assert(n);
                    109:
                    110:        if (NULL == (m->meta.title = strdup(n->string)))
1.4       schwarze  111:                return(man_nerr(m, n, WNMEM));
1.1       kristaps  112:
                    113:        /* TITLE ->MSEC<- DATE SOURCE VOL */
                    114:
                    115:        n = n->next;
                    116:        assert(n);
                    117:
                    118:        errno = 0;
                    119:        lval = strtol(n->string, &ep, 10);
                    120:        if (n->string[0] != '\0' && *ep == '\0')
                    121:                m->meta.msec = (int)lval;
1.4       schwarze  122:        else if ( ! man_nwarn(m, n, WMSEC))
1.1       kristaps  123:                return(0);
                    124:
                    125:        /* TITLE MSEC ->DATE<- SOURCE VOL */
                    126:
                    127:        if (NULL == (n = n->next))
                    128:                m->meta.date = time(NULL);
                    129:        else if (0 == (m->meta.date = man_atotime(n->string))) {
1.4       schwarze  130:                if ( ! man_nwarn(m, n, WDATE))
1.1       kristaps  131:                        return(0);
                    132:                m->meta.date = time(NULL);
                    133:        }
                    134:
                    135:        /* TITLE MSEC DATE ->SOURCE<- VOL */
                    136:
                    137:        if (n && (n = n->next))
                    138:                if (NULL == (m->meta.source = strdup(n->string)))
1.4       schwarze  139:                        return(man_nerr(m, n, WNMEM));
1.1       kristaps  140:
                    141:        /* TITLE MSEC DATE SOURCE ->VOL<- */
                    142:
                    143:        if (n && (n = n->next))
                    144:                if (NULL == (m->meta.vol = strdup(n->string)))
1.4       schwarze  145:                        return(man_nerr(m, n, WNMEM));
1.1       kristaps  146:
                    147:        /*
                    148:         * The end document shouldn't have the prologue macros as part
                    149:         * of the syntax tree (they encompass only meta-data).
                    150:         */
                    151:
                    152:        if (m->last->parent->child == m->last) {
                    153:                m->last->parent->child = NULL;
                    154:                n = m->last;
                    155:                m->last = m->last->parent;
                    156:                m->next = MAN_NEXT_CHILD;
                    157:        } else {
                    158:                assert(m->last->prev);
                    159:                m->last->prev->next = NULL;
                    160:                n = m->last;
                    161:                m->last = m->last->prev;
                    162:                m->next = MAN_NEXT_SIBLING;
                    163:        }
                    164:
                    165:        man_node_freelist(n);
                    166:        return(1);
                    167: }
                    168:
                    169:
                    170: static time_t
                    171: man_atotime(const char *p)
                    172: {
                    173:        struct tm        tm;
                    174:        char            *pp;
                    175:
1.4       schwarze  176:        bzero(&tm, sizeof(struct tm));
1.1       kristaps  177:
                    178:        if ((pp = strptime(p, "%b %d %Y", &tm)) && 0 == *pp)
                    179:                return(mktime(&tm));
                    180:        if ((pp = strptime(p, "%d %b %Y", &tm)) && 0 == *pp)
                    181:                return(mktime(&tm));
                    182:        if ((pp = strptime(p, "%b %d, %Y", &tm)) && 0 == *pp)
                    183:                return(mktime(&tm));
                    184:        if ((pp = strptime(p, "%b %Y", &tm)) && 0 == *pp)
                    185:                return(mktime(&tm));
                    186:
                    187:        return(0);
                    188: }