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

1.23    ! schwarze    1: /*     $Id: man_action.c,v 1.22 2010/05/24 13:42:58 schwarze Exp $ */
1.1       kristaps    2: /*
1.16      schwarze    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@bsd.lv>
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:
                     18: #include <assert.h>
                     19: #include <stdlib.h>
                     20: #include <string.h>
                     21:
1.20      schwarze   22: #include "mandoc.h"
1.1       kristaps   23: #include "libman.h"
1.10      schwarze   24: #include "libmandoc.h"
1.1       kristaps   25:
                     26: struct actions {
                     27:        int     (*post)(struct man *);
                     28: };
                     29:
                     30: static int       post_TH(struct man *);
1.7       schwarze   31: static int       post_fi(struct man *);
                     32: static int       post_nf(struct man *);
1.19      schwarze   33: static int       post_AT(struct man *);
                     34: static int       post_UC(struct man *);
1.1       kristaps   35:
                     36: const  struct actions man_actions[MAN_MAX] = {
1.3       schwarze   37:        { NULL }, /* br */
1.1       kristaps   38:        { post_TH }, /* TH */
                     39:        { NULL }, /* SH */
                     40:        { NULL }, /* SS */
                     41:        { NULL }, /* TP */
                     42:        { NULL }, /* LP */
                     43:        { NULL }, /* PP */
                     44:        { NULL }, /* P */
                     45:        { NULL }, /* IP */
                     46:        { NULL }, /* HP */
                     47:        { NULL }, /* SM */
                     48:        { NULL }, /* SB */
                     49:        { NULL }, /* BI */
                     50:        { NULL }, /* IB */
                     51:        { NULL }, /* BR */
                     52:        { NULL }, /* RB */
                     53:        { NULL }, /* R */
                     54:        { NULL }, /* B */
                     55:        { NULL }, /* I */
                     56:        { NULL }, /* IR */
                     57:        { NULL }, /* RI */
                     58:        { NULL }, /* na */
                     59:        { NULL }, /* i */
1.5       schwarze   60:        { NULL }, /* sp */
1.7       schwarze   61:        { post_nf }, /* nf */
                     62:        { post_fi }, /* fi */
                     63:        { NULL }, /* r */
                     64:        { NULL }, /* RE */
                     65:        { NULL }, /* RS */
                     66:        { NULL }, /* DT */
1.19      schwarze   67:        { post_UC }, /* UC */
1.9       schwarze   68:        { NULL }, /* PD */
1.12      schwarze   69:        { NULL }, /* Sp */
                     70:        { post_nf }, /* Vb */
                     71:        { post_fi }, /* Ve */
1.19      schwarze   72:        { post_AT }, /* AT */
1.1       kristaps   73: };
                     74:
                     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.7       schwarze   86:                /* FALLTHROUGH */
1.1       kristaps   87:        case (MAN_ROOT):
1.7       schwarze   88:                return(1);
                     89:        default:
1.1       kristaps   90:                break;
                     91:        }
1.7       schwarze   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))
1.20      schwarze  104:                if ( ! man_nmsg(m, m->last, MANDOCERR_NOSCOPE))
1.7       schwarze  105:                        return(0);
                    106:        m->flags &= ~MAN_LITERAL;
1.14      schwarze  107:        return(1);
                    108: }
                    109:
                    110:
                    111: static int
1.7       schwarze  112: post_nf(struct man *m)
                    113: {
                    114:
                    115:        if (MAN_LITERAL & m->flags)
1.20      schwarze  116:                if ( ! man_nmsg(m, m->last, MANDOCERR_SCOPEREP))
1.7       schwarze  117:                        return(0);
                    118:        m->flags |= MAN_LITERAL;
1.1       kristaps  119:        return(1);
                    120: }
                    121:
                    122:
                    123: static int
                    124: post_TH(struct man *m)
                    125: {
                    126:        struct man_node *n;
                    127:
                    128:        if (m->meta.title)
                    129:                free(m->meta.title);
                    130:        if (m->meta.vol)
                    131:                free(m->meta.vol);
                    132:        if (m->meta.source)
                    133:                free(m->meta.source);
1.16      schwarze  134:        if (m->meta.msec)
                    135:                free(m->meta.msec);
1.23    ! schwarze  136:        if (m->meta.rawdate)
        !           137:                free(m->meta.rawdate);
1.1       kristaps  138:
1.23    ! schwarze  139:        m->meta.title = m->meta.vol = m->meta.rawdate =
1.16      schwarze  140:                m->meta.msec = m->meta.source = NULL;
1.1       kristaps  141:        m->meta.date = 0;
                    142:
                    143:        /* ->TITLE<- MSEC DATE SOURCE VOL */
                    144:
                    145:        n = m->last->child;
                    146:        assert(n);
1.10      schwarze  147:        m->meta.title = mandoc_strdup(n->string);
1.1       kristaps  148:
                    149:        /* TITLE ->MSEC<- DATE SOURCE VOL */
                    150:
                    151:        n = n->next;
                    152:        assert(n);
1.16      schwarze  153:        m->meta.msec = mandoc_strdup(n->string);
1.1       kristaps  154:
                    155:        /* TITLE MSEC ->DATE<- SOURCE VOL */
                    156:
1.23    ! schwarze  157:        /*
        !           158:         * Try to parse the date.  If this works, stash the epoch (this
        !           159:         * is optimal because we can reformat it in the canonical form).
        !           160:         * If it doesn't parse, isn't specified at all, or is an empty
        !           161:         * string, then use the current date.
        !           162:         */
        !           163:
1.11      schwarze  164:        n = n->next;
1.23    ! schwarze  165:        if (n && n->string && *n->string) {
1.11      schwarze  166:                m->meta.date = mandoc_a2time
                    167:                        (MTIME_ISO_8601, n->string);
                    168:                if (0 == m->meta.date) {
1.20      schwarze  169:                        if ( ! man_nmsg(m, n, MANDOCERR_BADDATE))
1.11      schwarze  170:                                return(0);
1.23    ! schwarze  171:                        m->meta.rawdate = mandoc_strdup(n->string);
1.11      schwarze  172:                }
                    173:        } else
1.1       kristaps  174:                m->meta.date = time(NULL);
                    175:
                    176:        /* TITLE MSEC DATE ->SOURCE<- VOL */
                    177:
                    178:        if (n && (n = n->next))
1.10      schwarze  179:                m->meta.source = mandoc_strdup(n->string);
1.1       kristaps  180:
                    181:        /* TITLE MSEC DATE SOURCE ->VOL<- */
                    182:
                    183:        if (n && (n = n->next))
1.10      schwarze  184:                m->meta.vol = mandoc_strdup(n->string);
1.1       kristaps  185:
1.14      schwarze  186:        /*
                    187:         * Remove the `TH' node after we've processed it for our
                    188:         * meta-data.
                    189:         */
                    190:        man_node_delete(m, m->last);
1.19      schwarze  191:        return(1);
                    192: }
                    193:
                    194:
                    195: static int
                    196: post_AT(struct man *m)
                    197: {
                    198:        static const char * const unix_versions[] = {
                    199:            "7th Edition",
                    200:            "System III",
                    201:            "System V",
                    202:            "System V Release 2",
                    203:        };
                    204:
                    205:        const char      *p, *s;
                    206:        struct man_node *n, *nn;
                    207:
                    208:        n = m->last->child;
                    209:
                    210:        if (NULL == n || MAN_TEXT != n->type)
                    211:                p = unix_versions[0];
                    212:        else {
                    213:                s = n->string;
                    214:                if (0 == strcmp(s, "3"))
                    215:                        p = unix_versions[0];
                    216:                else if (0 == strcmp(s, "4"))
                    217:                        p = unix_versions[1];
                    218:                else if (0 == strcmp(s, "5")) {
                    219:                        nn = n->next;
                    220:                        if (nn && MAN_TEXT == nn->type && nn->string[0])
                    221:                                p = unix_versions[3];
                    222:                        else
                    223:                                p = unix_versions[2];
                    224:                } else
                    225:                        p = unix_versions[0];
                    226:        }
1.21      schwarze  227:
                    228:        if (m->meta.source)
                    229:                free(m->meta.source);
1.19      schwarze  230:
                    231:        m->meta.source = mandoc_strdup(p);
                    232:
                    233:        return(1);
                    234: }
                    235:
                    236:
                    237: static int
                    238: post_UC(struct man *m)
                    239: {
                    240:        static const char * const bsd_versions[] = {
                    241:            "3rd Berkeley Distribution",
                    242:            "4th Berkeley Distribution",
                    243:            "4.2 Berkeley Distribution",
                    244:            "4.3 Berkeley Distribution",
                    245:            "4.4 Berkeley Distribution",
                    246:        };
                    247:
                    248:        const char      *p, *s;
                    249:        struct man_node *n;
                    250:
                    251:        n = m->last->child;
                    252:
                    253:        if (NULL == n || MAN_TEXT != n->type)
                    254:                p = bsd_versions[0];
                    255:        else {
                    256:                s = n->string;
                    257:                if (0 == strcmp(s, "3"))
                    258:                        p = bsd_versions[0];
                    259:                else if (0 == strcmp(s, "4"))
                    260:                        p = bsd_versions[1];
                    261:                else if (0 == strcmp(s, "5"))
                    262:                        p = bsd_versions[2];
                    263:                else if (0 == strcmp(s, "6"))
                    264:                        p = bsd_versions[3];
                    265:                else if (0 == strcmp(s, "7"))
                    266:                        p = bsd_versions[4];
                    267:                else
                    268:                        p = bsd_versions[0];
                    269:        }
1.22      schwarze  270:
                    271:        if (m->meta.source)
                    272:                free(m->meta.source);
1.19      schwarze  273:
                    274:        m->meta.source = mandoc_strdup(p);
                    275:
1.1       kristaps  276:        return(1);
                    277: }