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

1.32    ! schwarze    1: /* $OpenBSD: tag.c,v 1.31 2020/04/02 22:10:27 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>
1.32    ! schwarze   25: #include <stdint.h>
1.1       schwarze   26: #include <stdlib.h>
                     27: #include <string.h>
                     28:
                     29: #include "mandoc_aux.h"
1.10      schwarze   30: #include "mandoc_ohash.h"
1.28      schwarze   31: #include "roff.h"
1.1       schwarze   32: #include "tag.h"
                     33:
                     34: struct tag_entry {
1.28      schwarze   35:        struct roff_node **nodes;
                     36:        size_t   maxnodes;
                     37:        size_t   nnodes;
1.4       schwarze   38:        int      prio;
1.1       schwarze   39:        char     s[];
                     40: };
                     41:
                     42: static struct ohash     tag_data;
                     43:
                     44:
                     45: /*
1.28      schwarze   46:  * Set up the ohash table to collect nodes
                     47:  * where various marked-up terms are documented.
1.1       schwarze   48:  */
1.28      schwarze   49: void
                     50: tag_alloc(void)
1.1       schwarze   51: {
1.28      schwarze   52:        mandoc_ohash_init(&tag_data, 4, offsetof(struct tag_entry, s));
                     53: }
1.1       schwarze   54:
1.28      schwarze   55: void
                     56: tag_free(void)
                     57: {
                     58:        struct tag_entry        *entry;
                     59:        unsigned int             slot;
1.12      schwarze   60:
1.29      schwarze   61:        if (tag_data.info.free == NULL)
                     62:                return;
1.28      schwarze   63:        entry = ohash_first(&tag_data, &slot);
                     64:        while (entry != NULL) {
                     65:                free(entry->nodes);
                     66:                free(entry);
                     67:                entry = ohash_next(&tag_data, &slot);
1.22      schwarze   68:        }
1.28      schwarze   69:        ohash_delete(&tag_data);
1.29      schwarze   70:        tag_data.info.free = NULL;
1.1       schwarze   71: }
                     72:
                     73: /*
1.28      schwarze   74:  * Set a node where a term is defined,
1.20      schwarze   75:  * unless it is already defined at a lower priority.
1.1       schwarze   76:  */
                     77: void
1.28      schwarze   78: tag_put(const char *s, int prio, struct roff_node *n)
1.1       schwarze   79: {
                     80:        struct tag_entry        *entry;
1.20      schwarze   81:        const char              *se;
1.5       schwarze   82:        size_t                   len;
1.1       schwarze   83:        unsigned int             slot;
                     84:
1.27      schwarze   85:        assert(prio <= TAG_FALLBACK);
1.20      schwarze   86:
1.28      schwarze   87:        if (s == NULL) {
                     88:                if (n->child == NULL || n->child->type != ROFFT_TEXT)
                     89:                        return;
                     90:                s = n->child->string;
1.30      schwarze   91:                switch (s[0]) {
                     92:                case '-':
                     93:                        s++;
                     94:                        break;
                     95:                case '\\':
                     96:                        switch (s[1]) {
                     97:                        case '&':
                     98:                        case '-':
                     99:                        case 'e':
                    100:                                s += 2;
                    101:                                break;
                    102:                        default:
                    103:                                break;
                    104:                        }
                    105:                        break;
                    106:                default:
                    107:                        break;
                    108:                }
1.28      schwarze  109:        }
1.20      schwarze  110:
                    111:        /*
1.24      schwarze  112:         * Skip whitespace and escapes and whatever follows,
1.20      schwarze  113:         * and if there is any, downgrade the priority.
                    114:         */
                    115:
1.24      schwarze  116:        len = strcspn(s, " \t\\");
1.20      schwarze  117:        if (len == 0)
1.1       schwarze  118:                return;
1.14      schwarze  119:
1.20      schwarze  120:        se = s + len;
1.27      schwarze  121:        if (*se != '\0' && prio < TAG_WEAK)
                    122:                prio = TAG_WEAK;
1.20      schwarze  123:
                    124:        slot = ohash_qlookupi(&tag_data, s, &se);
1.1       schwarze  125:        entry = ohash_find(&tag_data, slot);
1.14      schwarze  126:
1.28      schwarze  127:        /* Build a new entry. */
                    128:
1.1       schwarze  129:        if (entry == NULL) {
1.20      schwarze  130:                entry = mandoc_malloc(sizeof(*entry) + len + 1);
1.1       schwarze  131:                memcpy(entry->s, s, len);
1.20      schwarze  132:                entry->s[len] = '\0';
1.28      schwarze  133:                entry->nodes = NULL;
                    134:                entry->maxnodes = entry->nnodes = 0;
1.1       schwarze  135:                ohash_insert(&tag_data, slot, entry);
1.28      schwarze  136:        }
1.14      schwarze  137:
1.28      schwarze  138:        /*
                    139:         * Lower priority numbers take precedence.
                    140:         * If a better entry is already present, ignore the new one.
                    141:         */
                    142:
                    143:        else if (entry->prio < prio)
                    144:                        return;
                    145:
                    146:        /*
                    147:         * If the existing entry is worse, clear it.
                    148:         * In addition, a tag with priority TAG_FALLBACK
                    149:         * is only used if the tag occurs exactly once.
                    150:         */
1.14      schwarze  151:
1.28      schwarze  152:        else if (entry->prio > prio || prio == TAG_FALLBACK) {
                    153:                while (entry->nnodes > 0)
                    154:                        entry->nodes[--entry->nnodes]->flags &= ~NODE_ID;
1.16      schwarze  155:
1.27      schwarze  156:                if (prio == TAG_FALLBACK) {
1.28      schwarze  157:                        entry->prio = TAG_DELETE;
1.16      schwarze  158:                        return;
                    159:                }
1.14      schwarze  160:        }
                    161:
1.28      schwarze  162:        /* Remember the new node. */
1.14      schwarze  163:
1.28      schwarze  164:        if (entry->maxnodes == entry->nnodes) {
                    165:                entry->maxnodes += 4;
                    166:                entry->nodes = mandoc_reallocarray(entry->nodes,
                    167:                    entry->maxnodes, sizeof(*entry->nodes));
1.14      schwarze  168:        }
1.28      schwarze  169:        entry->nodes[entry->nnodes++] = n;
1.4       schwarze  170:        entry->prio = prio;
1.28      schwarze  171:        n->flags |= NODE_ID;
                    172:        if (n->child == NULL || n->child->string != s || *se != '\0') {
                    173:                assert(n->string == NULL);
                    174:                n->string = mandoc_strndup(s, len);
                    175:        }
1.1       schwarze  176: }
                    177:
1.31      schwarze  178: int
                    179: tag_exists(const char *tag)
1.1       schwarze  180: {
1.31      schwarze  181:        return ohash_find(&tag_data, ohash_qlookup(&tag_data, tag)) != NULL;
1.1       schwarze  182: }