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

Annotation of mandoc/man_action.c, Revision 1.40

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

CVSweb