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

Annotation of mandoc/man_action.c, Revision 1.17

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

CVSweb