[BACK]Return to tag.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/tag.c, Revision 1.28

1.28    ! schwarze    1: /* $OpenBSD: tag.c,v 1.27 2020/01/20 10:29:31 schwarze Exp $ */
1.1       schwarze    2: /*
1.26      schwarze    3:  * Copyright (c) 2015,2016,2018,2019,2020 Ingo Schwarze <schwarze@openbsd.org>
1.1       schwarze    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      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.28    ! schwarze   16:  *
        !            17:  * Functions to tag syntax tree nodes.
        !            18:  * For internal use by mandoc(1) validation modules only.
1.1       schwarze   19:  */
                     20: #include <sys/types.h>
                     21:
1.27      schwarze   22: #include <assert.h>
1.20      schwarze   23: #include <limits.h>
1.1       schwarze   24: #include <stddef.h>
                     25: #include <stdlib.h>
                     26: #include <string.h>
                     27:
                     28: #include "mandoc_aux.h"
1.10      schwarze   29: #include "mandoc_ohash.h"
1.28    ! schwarze   30: #include "roff.h"
1.1       schwarze   31: #include "tag.h"
                     32:
                     33: struct tag_entry {
1.28    ! schwarze   34:        struct roff_node **nodes;
        !            35:        size_t   maxnodes;
        !            36:        size_t   nnodes;
1.4       schwarze   37:        int      prio;
1.1       schwarze   38:        char     s[];
                     39: };
                     40:
                     41: static struct ohash     tag_data;
                     42:
                     43:
                     44: /*
1.28    ! schwarze   45:  * Set up the ohash table to collect nodes
        !            46:  * where various marked-up terms are documented.
1.1       schwarze   47:  */
1.28    ! schwarze   48: void
        !            49: tag_alloc(void)
1.1       schwarze   50: {
1.28    ! schwarze   51:        mandoc_ohash_init(&tag_data, 4, offsetof(struct tag_entry, s));
        !            52: }
1.1       schwarze   53:
1.28    ! schwarze   54: void
        !            55: tag_free(void)
        !            56: {
        !            57:        struct tag_entry        *entry;
        !            58:        unsigned int             slot;
1.12      schwarze   59:
1.28    ! schwarze   60:        entry = ohash_first(&tag_data, &slot);
        !            61:        while (entry != NULL) {
        !            62:                free(entry->nodes);
        !            63:                free(entry);
        !            64:                entry = ohash_next(&tag_data, &slot);
1.22      schwarze   65:        }
1.28    ! schwarze   66:        ohash_delete(&tag_data);
1.1       schwarze   67: }
                     68:
                     69: /*
1.28    ! schwarze   70:  * Set a node where a term is defined,
1.20      schwarze   71:  * unless it is already defined at a lower priority.
1.1       schwarze   72:  */
                     73: void
1.28    ! schwarze   74: tag_put(const char *s, int prio, struct roff_node *n)
1.1       schwarze   75: {
                     76:        struct tag_entry        *entry;
1.20      schwarze   77:        const char              *se;
1.5       schwarze   78:        size_t                   len;
1.1       schwarze   79:        unsigned int             slot;
                     80:
1.27      schwarze   81:        assert(prio <= TAG_FALLBACK);
1.20      schwarze   82:
1.28    ! schwarze   83:        if (s == NULL) {
        !            84:                if (n->child == NULL || n->child->type != ROFFT_TEXT)
        !            85:                        return;
        !            86:                s = n->child->string;
        !            87:                if (s[0] == '\\' && (s[1] == '&' || s[1] == 'e'))
        !            88:                        s += 2;
        !            89:        }
1.20      schwarze   90:
                     91:        /*
1.24      schwarze   92:         * Skip whitespace and escapes and whatever follows,
1.20      schwarze   93:         * and if there is any, downgrade the priority.
                     94:         */
                     95:
1.24      schwarze   96:        len = strcspn(s, " \t\\");
1.20      schwarze   97:        if (len == 0)
1.1       schwarze   98:                return;
1.14      schwarze   99:
1.20      schwarze  100:        se = s + len;
1.27      schwarze  101:        if (*se != '\0' && prio < TAG_WEAK)
                    102:                prio = TAG_WEAK;
1.20      schwarze  103:
                    104:        slot = ohash_qlookupi(&tag_data, s, &se);
1.1       schwarze  105:        entry = ohash_find(&tag_data, slot);
1.14      schwarze  106:
1.28    ! schwarze  107:        /* Build a new entry. */
        !           108:
1.1       schwarze  109:        if (entry == NULL) {
1.20      schwarze  110:                entry = mandoc_malloc(sizeof(*entry) + len + 1);
1.1       schwarze  111:                memcpy(entry->s, s, len);
1.20      schwarze  112:                entry->s[len] = '\0';
1.28    ! schwarze  113:                entry->nodes = NULL;
        !           114:                entry->maxnodes = entry->nnodes = 0;
1.1       schwarze  115:                ohash_insert(&tag_data, slot, entry);
1.28    ! schwarze  116:        }
1.14      schwarze  117:
1.28    ! schwarze  118:        /*
        !           119:         * Lower priority numbers take precedence.
        !           120:         * If a better entry is already present, ignore the new one.
        !           121:         */
        !           122:
        !           123:        else if (entry->prio < prio)
        !           124:                        return;
        !           125:
        !           126:        /*
        !           127:         * If the existing entry is worse, clear it.
        !           128:         * In addition, a tag with priority TAG_FALLBACK
        !           129:         * is only used if the tag occurs exactly once.
        !           130:         */
1.14      schwarze  131:
1.28    ! schwarze  132:        else if (entry->prio > prio || prio == TAG_FALLBACK) {
        !           133:                while (entry->nnodes > 0)
        !           134:                        entry->nodes[--entry->nnodes]->flags &= ~NODE_ID;
1.16      schwarze  135:
1.27      schwarze  136:                if (prio == TAG_FALLBACK) {
1.28    ! schwarze  137:                        entry->prio = TAG_DELETE;
1.16      schwarze  138:                        return;
                    139:                }
1.14      schwarze  140:        }
                    141:
1.28    ! schwarze  142:        /* Remember the new node. */
1.14      schwarze  143:
1.28    ! schwarze  144:        if (entry->maxnodes == entry->nnodes) {
        !           145:                entry->maxnodes += 4;
        !           146:                entry->nodes = mandoc_reallocarray(entry->nodes,
        !           147:                    entry->maxnodes, sizeof(*entry->nodes));
1.14      schwarze  148:        }
1.28    ! schwarze  149:        entry->nodes[entry->nnodes++] = n;
1.4       schwarze  150:        entry->prio = prio;
1.28    ! schwarze  151:        n->flags |= NODE_ID;
        !           152:        if (n->child == NULL || n->child->string != s || *se != '\0') {
        !           153:                assert(n->string == NULL);
        !           154:                n->string = mandoc_strndup(s, len);
        !           155:        }
1.1       schwarze  156: }
                    157:
1.28    ! schwarze  158: enum tag_result
        !           159: tag_check(const char *test_tag)
1.1       schwarze  160: {
1.28    ! schwarze  161:        unsigned int slot;
1.1       schwarze  162:
1.28    ! schwarze  163:        if (ohash_first(&tag_data, &slot) == NULL)
        !           164:                return TAG_EMPTY;
        !           165:        else if (test_tag != NULL && ohash_find(&tag_data,
        !           166:            ohash_qlookup(&tag_data, test_tag)) == NULL)
        !           167:                return TAG_MISS;
1.19      tb        168:        else
1.28    ! schwarze  169:                return TAG_OK;
1.1       schwarze  170: }