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

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