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

1.24    ! schwarze    1: /*     $Id: man_action.c,v 1.23 2010/06/06 18:08:41 schwarze Exp $ */
1.1       kristaps    2: /*
1.24    ! schwarze    3:  * Copyright (c) 2008, 2009, 2010 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.24    ! schwarze   73:        { NULL }, /* in */
1.1       kristaps   74: };
                     75:
                     76:
                     77: int
                     78: man_action_post(struct man *m)
                     79: {
                     80:
                     81:        if (MAN_ACTED & m->last->flags)
                     82:                return(1);
                     83:        m->last->flags |= MAN_ACTED;
                     84:
                     85:        switch (m->last->type) {
                     86:        case (MAN_TEXT):
1.7       schwarze   87:                /* FALLTHROUGH */
1.1       kristaps   88:        case (MAN_ROOT):
1.7       schwarze   89:                return(1);
                     90:        default:
1.1       kristaps   91:                break;
                     92:        }
1.7       schwarze   93:
                     94:        if (NULL == man_actions[m->last->tok].post)
                     95:                return(1);
                     96:        return((*man_actions[m->last->tok].post)(m));
                     97: }
                     98:
                     99:
                    100: static int
                    101: post_fi(struct man *m)
                    102: {
                    103:
                    104:        if ( ! (MAN_LITERAL & m->flags))
1.20      schwarze  105:                if ( ! man_nmsg(m, m->last, MANDOCERR_NOSCOPE))
1.7       schwarze  106:                        return(0);
                    107:        m->flags &= ~MAN_LITERAL;
1.14      schwarze  108:        return(1);
                    109: }
                    110:
                    111:
                    112: static int
1.7       schwarze  113: post_nf(struct man *m)
                    114: {
                    115:
                    116:        if (MAN_LITERAL & m->flags)
1.20      schwarze  117:                if ( ! man_nmsg(m, m->last, MANDOCERR_SCOPEREP))
1.7       schwarze  118:                        return(0);
                    119:        m->flags |= MAN_LITERAL;
1.1       kristaps  120:        return(1);
                    121: }
                    122:
                    123:
                    124: static int
                    125: post_TH(struct man *m)
                    126: {
                    127:        struct man_node *n;
                    128:
                    129:        if (m->meta.title)
                    130:                free(m->meta.title);
                    131:        if (m->meta.vol)
                    132:                free(m->meta.vol);
                    133:        if (m->meta.source)
                    134:                free(m->meta.source);
1.16      schwarze  135:        if (m->meta.msec)
                    136:                free(m->meta.msec);
1.23      schwarze  137:        if (m->meta.rawdate)
                    138:                free(m->meta.rawdate);
1.1       kristaps  139:
1.23      schwarze  140:        m->meta.title = m->meta.vol = m->meta.rawdate =
1.16      schwarze  141:                m->meta.msec = m->meta.source = NULL;
1.1       kristaps  142:        m->meta.date = 0;
                    143:
                    144:        /* ->TITLE<- MSEC DATE SOURCE VOL */
                    145:
                    146:        n = m->last->child;
                    147:        assert(n);
1.10      schwarze  148:        m->meta.title = mandoc_strdup(n->string);
1.1       kristaps  149:
                    150:        /* TITLE ->MSEC<- DATE SOURCE VOL */
                    151:
                    152:        n = n->next;
                    153:        assert(n);
1.16      schwarze  154:        m->meta.msec = mandoc_strdup(n->string);
1.1       kristaps  155:
                    156:        /* TITLE MSEC ->DATE<- SOURCE VOL */
                    157:
1.23      schwarze  158:        /*
                    159:         * Try to parse the date.  If this works, stash the epoch (this
                    160:         * is optimal because we can reformat it in the canonical form).
                    161:         * If it doesn't parse, isn't specified at all, or is an empty
                    162:         * string, then use the current date.
                    163:         */
                    164:
1.11      schwarze  165:        n = n->next;
1.23      schwarze  166:        if (n && n->string && *n->string) {
1.11      schwarze  167:                m->meta.date = mandoc_a2time
                    168:                        (MTIME_ISO_8601, n->string);
                    169:                if (0 == m->meta.date) {
1.20      schwarze  170:                        if ( ! man_nmsg(m, n, MANDOCERR_BADDATE))
1.11      schwarze  171:                                return(0);
1.23      schwarze  172:                        m->meta.rawdate = mandoc_strdup(n->string);
1.11      schwarze  173:                }
                    174:        } else
1.1       kristaps  175:                m->meta.date = time(NULL);
                    176:
                    177:        /* TITLE MSEC DATE ->SOURCE<- VOL */
                    178:
                    179:        if (n && (n = n->next))
1.10      schwarze  180:                m->meta.source = mandoc_strdup(n->string);
1.1       kristaps  181:
                    182:        /* TITLE MSEC DATE SOURCE ->VOL<- */
                    183:
                    184:        if (n && (n = n->next))
1.10      schwarze  185:                m->meta.vol = mandoc_strdup(n->string);
1.1       kristaps  186:
1.14      schwarze  187:        /*
                    188:         * Remove the `TH' node after we've processed it for our
                    189:         * meta-data.
                    190:         */
                    191:        man_node_delete(m, m->last);
1.19      schwarze  192:        return(1);
                    193: }
                    194:
                    195:
                    196: static int
                    197: post_AT(struct man *m)
                    198: {
                    199:        static const char * const unix_versions[] = {
                    200:            "7th Edition",
                    201:            "System III",
                    202:            "System V",
                    203:            "System V Release 2",
                    204:        };
                    205:
                    206:        const char      *p, *s;
                    207:        struct man_node *n, *nn;
                    208:
                    209:        n = m->last->child;
                    210:
                    211:        if (NULL == n || MAN_TEXT != n->type)
                    212:                p = unix_versions[0];
                    213:        else {
                    214:                s = n->string;
                    215:                if (0 == strcmp(s, "3"))
                    216:                        p = unix_versions[0];
                    217:                else if (0 == strcmp(s, "4"))
                    218:                        p = unix_versions[1];
                    219:                else if (0 == strcmp(s, "5")) {
                    220:                        nn = n->next;
                    221:                        if (nn && MAN_TEXT == nn->type && nn->string[0])
                    222:                                p = unix_versions[3];
                    223:                        else
                    224:                                p = unix_versions[2];
                    225:                } else
                    226:                        p = unix_versions[0];
                    227:        }
1.21      schwarze  228:
                    229:        if (m->meta.source)
                    230:                free(m->meta.source);
1.19      schwarze  231:
                    232:        m->meta.source = mandoc_strdup(p);
                    233:
                    234:        return(1);
                    235: }
                    236:
                    237:
                    238: static int
                    239: post_UC(struct man *m)
                    240: {
                    241:        static const char * const bsd_versions[] = {
                    242:            "3rd Berkeley Distribution",
                    243:            "4th Berkeley Distribution",
                    244:            "4.2 Berkeley Distribution",
                    245:            "4.3 Berkeley Distribution",
                    246:            "4.4 Berkeley Distribution",
                    247:        };
                    248:
                    249:        const char      *p, *s;
                    250:        struct man_node *n;
                    251:
                    252:        n = m->last->child;
                    253:
                    254:        if (NULL == n || MAN_TEXT != n->type)
                    255:                p = bsd_versions[0];
                    256:        else {
                    257:                s = n->string;
                    258:                if (0 == strcmp(s, "3"))
                    259:                        p = bsd_versions[0];
                    260:                else if (0 == strcmp(s, "4"))
                    261:                        p = bsd_versions[1];
                    262:                else if (0 == strcmp(s, "5"))
                    263:                        p = bsd_versions[2];
                    264:                else if (0 == strcmp(s, "6"))
                    265:                        p = bsd_versions[3];
                    266:                else if (0 == strcmp(s, "7"))
                    267:                        p = bsd_versions[4];
                    268:                else
                    269:                        p = bsd_versions[0];
                    270:        }
1.22      schwarze  271:
                    272:        if (m->meta.source)
                    273:                free(m->meta.source);
1.19      schwarze  274:
                    275:        m->meta.source = mandoc_strdup(p);
                    276:
1.1       kristaps  277:        return(1);
                    278: }