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

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