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

1.23    ! schwarze    1: /*     $OpenBSD: tag.c,v 1.22 2019/07/10 19:38:56 schwarze Exp $ */
1.1       schwarze    2: /*
1.22      schwarze    3:  * Copyright (c) 2015, 2016, 2018, 2019 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.
                     16:  */
                     17: #include <sys/types.h>
                     18:
1.22      schwarze   19: #include <errno.h>
1.20      schwarze   20: #include <limits.h>
1.2       schwarze   21: #include <signal.h>
1.1       schwarze   22: #include <stddef.h>
1.7       schwarze   23: #include <stdint.h>
1.1       schwarze   24: #include <stdio.h>
                     25: #include <stdlib.h>
                     26: #include <string.h>
                     27: #include <unistd.h>
                     28:
                     29: #include "mandoc_aux.h"
1.10      schwarze   30: #include "mandoc_ohash.h"
1.22      schwarze   31: #include "mandoc.h"
1.1       schwarze   32: #include "tag.h"
                     33:
                     34: struct tag_entry {
1.14      schwarze   35:        size_t  *lines;
                     36:        size_t   maxlines;
                     37:        size_t   nlines;
1.4       schwarze   38:        int      prio;
1.1       schwarze   39:        char     s[];
                     40: };
                     41:
1.18      schwarze   42: static void     tag_signal(int) __attribute__((__noreturn__));
1.1       schwarze   43:
                     44: static struct ohash     tag_data;
1.6       schwarze   45: static struct tag_files         tag_files;
1.1       schwarze   46:
                     47:
                     48: /*
1.6       schwarze   49:  * Prepare for using a pager.
                     50:  * Not all pagers are capable of using a tag file,
                     51:  * but for simplicity, create it anyway.
1.1       schwarze   52:  */
1.6       schwarze   53: struct tag_files *
1.1       schwarze   54: tag_init(void)
                     55: {
1.11      schwarze   56:        struct sigaction         sa;
1.6       schwarze   57:        int                      ofd;
1.1       schwarze   58:
1.6       schwarze   59:        ofd = -1;
                     60:        tag_files.tfd = -1;
1.11      schwarze   61:        tag_files.tcpgid = -1;
1.6       schwarze   62:
1.12      schwarze   63:        /* Clean up when dying from a signal. */
                     64:
                     65:        memset(&sa, 0, sizeof(sa));
                     66:        sigfillset(&sa.sa_mask);
                     67:        sa.sa_handler = tag_signal;
                     68:        sigaction(SIGHUP, &sa, NULL);
                     69:        sigaction(SIGINT, &sa, NULL);
                     70:        sigaction(SIGTERM, &sa, NULL);
                     71:
                     72:        /*
                     73:         * POSIX requires that a process calling tcsetpgrp(3)
                     74:         * from the background gets a SIGTTOU signal.
                     75:         * In that case, do not stop.
                     76:         */
                     77:
                     78:        sa.sa_handler = SIG_IGN;
                     79:        sigaction(SIGTTOU, &sa, NULL);
                     80:
1.6       schwarze   81:        /* Save the original standard output for use by the pager. */
                     82:
1.22      schwarze   83:        if ((tag_files.ofd = dup(STDOUT_FILENO)) == -1) {
                     84:                mandoc_msg(MANDOCERR_DUP, 0, 0, "%s", strerror(errno));
1.6       schwarze   85:                goto fail;
1.22      schwarze   86:        }
1.6       schwarze   87:
                     88:        /* Create both temporary output files. */
                     89:
                     90:        (void)strlcpy(tag_files.ofn, "/tmp/man.XXXXXXXXXX",
                     91:            sizeof(tag_files.ofn));
                     92:        (void)strlcpy(tag_files.tfn, "/tmp/man.XXXXXXXXXX",
                     93:            sizeof(tag_files.tfn));
1.22      schwarze   94:        if ((ofd = mkstemp(tag_files.ofn)) == -1) {
                     95:                mandoc_msg(MANDOCERR_MKSTEMP, 0, 0,
                     96:                    "%s: %s", tag_files.ofn, strerror(errno));
1.6       schwarze   97:                goto fail;
1.22      schwarze   98:        }
                     99:        if ((tag_files.tfd = mkstemp(tag_files.tfn)) == -1) {
                    100:                mandoc_msg(MANDOCERR_MKSTEMP, 0, 0,
                    101:                    "%s: %s", tag_files.tfn, strerror(errno));
1.6       schwarze  102:                goto fail;
1.22      schwarze  103:        }
                    104:        if (dup2(ofd, STDOUT_FILENO) == -1) {
                    105:                mandoc_msg(MANDOCERR_DUP, 0, 0, "%s", strerror(errno));
1.6       schwarze  106:                goto fail;
1.22      schwarze  107:        }
1.6       schwarze  108:        close(ofd);
                    109:
                    110:        /*
                    111:         * Set up the ohash table to collect output line numbers
                    112:         * where various marked-up terms are documented.
                    113:         */
1.1       schwarze  114:
1.10      schwarze  115:        mandoc_ohash_init(&tag_data, 4, offsetof(struct tag_entry, s));
1.8       schwarze  116:        return &tag_files;
1.6       schwarze  117:
                    118: fail:
                    119:        tag_unlink();
                    120:        if (ofd != -1)
                    121:                close(ofd);
                    122:        if (tag_files.ofd != -1)
                    123:                close(tag_files.ofd);
                    124:        if (tag_files.tfd != -1)
                    125:                close(tag_files.tfd);
                    126:        *tag_files.ofn = '\0';
                    127:        *tag_files.tfn = '\0';
                    128:        tag_files.ofd = -1;
                    129:        tag_files.tfd = -1;
1.8       schwarze  130:        return NULL;
1.1       schwarze  131: }
                    132:
                    133: /*
1.5       schwarze  134:  * Set the line number where a term is defined,
1.20      schwarze  135:  * unless it is already defined at a lower priority.
1.1       schwarze  136:  */
                    137: void
1.5       schwarze  138: tag_put(const char *s, int prio, size_t line)
1.1       schwarze  139: {
                    140:        struct tag_entry        *entry;
1.20      schwarze  141:        const char              *se;
1.5       schwarze  142:        size_t                   len;
1.1       schwarze  143:        unsigned int             slot;
                    144:
1.15      schwarze  145:        if (tag_files.tfd <= 0)
                    146:                return;
1.20      schwarze  147:
1.15      schwarze  148:        if (s[0] == '\\' && (s[1] == '&' || s[1] == 'e'))
                    149:                s += 2;
1.20      schwarze  150:
                    151:        /*
                    152:         * Skip whitespace and whatever follows it,
                    153:         * and if there is any, downgrade the priority.
                    154:         */
                    155:
                    156:        len = strcspn(s, " \t");
                    157:        if (len == 0)
1.1       schwarze  158:                return;
1.14      schwarze  159:
1.20      schwarze  160:        se = s + len;
                    161:        if (*se != '\0')
                    162:                prio = INT_MAX;
                    163:
                    164:        slot = ohash_qlookupi(&tag_data, s, &se);
1.1       schwarze  165:        entry = ohash_find(&tag_data, slot);
1.14      schwarze  166:
1.1       schwarze  167:        if (entry == NULL) {
1.14      schwarze  168:
                    169:                /* Build a new entry. */
                    170:
1.20      schwarze  171:                entry = mandoc_malloc(sizeof(*entry) + len + 1);
1.1       schwarze  172:                memcpy(entry->s, s, len);
1.20      schwarze  173:                entry->s[len] = '\0';
1.14      schwarze  174:                entry->lines = NULL;
                    175:                entry->maxlines = entry->nlines = 0;
1.1       schwarze  176:                ohash_insert(&tag_data, slot, entry);
1.14      schwarze  177:
                    178:        } else {
                    179:
1.20      schwarze  180:                /*
                    181:                 * Lower priority numbers take precedence,
                    182:                 * but 0 is special.
                    183:                 * A tag with priority 0 is only used
                    184:                 * if the tag occurs exactly once.
                    185:                 */
1.16      schwarze  186:
                    187:                if (prio == 0) {
                    188:                        if (entry->prio == 0)
                    189:                                entry->prio = -1;
                    190:                        return;
                    191:                }
                    192:
1.14      schwarze  193:                /* A better entry is already present, ignore the new one. */
                    194:
1.16      schwarze  195:                if (entry->prio > 0 && entry->prio < prio)
1.14      schwarze  196:                        return;
                    197:
                    198:                /* The existing entry is worse, clear it. */
                    199:
1.16      schwarze  200:                if (entry->prio < 1 || entry->prio > prio)
1.14      schwarze  201:                        entry->nlines = 0;
                    202:        }
                    203:
                    204:        /* Remember the new line. */
                    205:
                    206:        if (entry->maxlines == entry->nlines) {
                    207:                entry->maxlines += 4;
                    208:                entry->lines = mandoc_reallocarray(entry->lines,
                    209:                    entry->maxlines, sizeof(*entry->lines));
                    210:        }
                    211:        entry->lines[entry->nlines++] = line;
1.4       schwarze  212:        entry->prio = prio;
1.1       schwarze  213: }
                    214:
                    215: /*
                    216:  * Write out the tags file using the previously collected
                    217:  * information and clear the ohash table while going along.
                    218:  */
                    219: void
                    220: tag_write(void)
                    221: {
                    222:        FILE                    *stream;
                    223:        struct tag_entry        *entry;
1.14      schwarze  224:        size_t                   i;
1.1       schwarze  225:        unsigned int             slot;
1.23    ! schwarze  226:        int                      empty;
1.1       schwarze  227:
1.6       schwarze  228:        if (tag_files.tfd <= 0)
1.1       schwarze  229:                return;
1.21      schwarze  230:        if (tag_files.tagname != NULL && ohash_find(&tag_data,
                    231:             ohash_qlookup(&tag_data, tag_files.tagname)) == NULL) {
1.22      schwarze  232:                mandoc_msg(MANDOCERR_TAG, 0, 0, "%s", tag_files.tagname);
1.21      schwarze  233:                tag_files.tagname = NULL;
                    234:        }
1.22      schwarze  235:        if ((stream = fdopen(tag_files.tfd, "w")) == NULL)
                    236:                mandoc_msg(MANDOCERR_FDOPEN, 0, 0, "%s", strerror(errno));
1.23    ! schwarze  237:        empty = 1;
1.1       schwarze  238:        entry = ohash_first(&tag_data, &slot);
                    239:        while (entry != NULL) {
1.23    ! schwarze  240:                if (stream != NULL && entry->prio >= 0) {
        !           241:                        for (i = 0; i < entry->nlines; i++) {
1.14      schwarze  242:                                fprintf(stream, "%s %s %zu\n",
                    243:                                    entry->s, tag_files.ofn, entry->lines[i]);
1.23    ! schwarze  244:                                empty = 0;
        !           245:                        }
        !           246:                }
1.14      schwarze  247:                free(entry->lines);
1.1       schwarze  248:                free(entry);
                    249:                entry = ohash_next(&tag_data, &slot);
                    250:        }
                    251:        ohash_delete(&tag_data);
                    252:        if (stream != NULL)
                    253:                fclose(stream);
1.19      tb        254:        else
                    255:                close(tag_files.tfd);
                    256:        tag_files.tfd = -1;
1.23    ! schwarze  257:        if (empty) {
        !           258:                unlink(tag_files.tfn);
        !           259:                *tag_files.tfn = '\0';
        !           260:        }
1.1       schwarze  261: }
                    262:
                    263: void
                    264: tag_unlink(void)
                    265: {
1.11      schwarze  266:        pid_t    tc_pgid;
1.1       schwarze  267:
1.11      schwarze  268:        if (tag_files.tcpgid != -1) {
1.17      schwarze  269:                tc_pgid = tcgetpgrp(tag_files.ofd);
1.11      schwarze  270:                if (tc_pgid == tag_files.pager_pid ||
                    271:                    tc_pgid == getpgid(0) ||
                    272:                    getpgid(tc_pgid) == -1)
1.17      schwarze  273:                        (void)tcsetpgrp(tag_files.ofd, tag_files.tcpgid);
1.11      schwarze  274:        }
1.6       schwarze  275:        if (*tag_files.ofn != '\0')
                    276:                unlink(tag_files.ofn);
                    277:        if (*tag_files.tfn != '\0')
                    278:                unlink(tag_files.tfn);
1.2       schwarze  279: }
                    280:
                    281: static void
                    282: tag_signal(int signum)
                    283: {
1.11      schwarze  284:        struct sigaction         sa;
1.2       schwarze  285:
                    286:        tag_unlink();
1.11      schwarze  287:        memset(&sa, 0, sizeof(sa));
                    288:        sigemptyset(&sa.sa_mask);
                    289:        sa.sa_handler = SIG_DFL;
                    290:        sigaction(signum, &sa, NULL);
1.2       schwarze  291:        kill(getpid(), signum);
                    292:        /* NOTREACHED */
                    293:        _exit(1);
1.1       schwarze  294: }