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

1.15    ! schwarze    1: /*     $OpenBSD: tag.c,v 1.14 2016/11/08 15:27:06 schwarze Exp $ */
1.1       schwarze    2: /*
1.14      schwarze    3:  * Copyright (c) 2015, 2016 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.2       schwarze   19: #include <signal.h>
1.1       schwarze   20: #include <stddef.h>
1.7       schwarze   21: #include <stdint.h>
1.1       schwarze   22: #include <stdio.h>
                     23: #include <stdlib.h>
                     24: #include <string.h>
                     25: #include <unistd.h>
                     26:
                     27: #include "mandoc_aux.h"
1.10      schwarze   28: #include "mandoc_ohash.h"
1.1       schwarze   29: #include "tag.h"
                     30:
                     31: struct tag_entry {
1.14      schwarze   32:        size_t  *lines;
                     33:        size_t   maxlines;
                     34:        size_t   nlines;
1.4       schwarze   35:        int      prio;
1.1       schwarze   36:        char     s[];
                     37: };
                     38:
1.13      schwarze   39: static void     tag_signal(int) __attribute__((noreturn));
1.1       schwarze   40:
                     41: static struct ohash     tag_data;
1.6       schwarze   42: static struct tag_files         tag_files;
1.1       schwarze   43:
                     44:
                     45: /*
1.6       schwarze   46:  * Prepare for using a pager.
                     47:  * Not all pagers are capable of using a tag file,
                     48:  * but for simplicity, create it anyway.
1.1       schwarze   49:  */
1.6       schwarze   50: struct tag_files *
1.1       schwarze   51: tag_init(void)
                     52: {
1.11      schwarze   53:        struct sigaction         sa;
1.6       schwarze   54:        int                      ofd;
1.1       schwarze   55:
1.6       schwarze   56:        ofd = -1;
                     57:        tag_files.tfd = -1;
1.11      schwarze   58:        tag_files.tcpgid = -1;
1.6       schwarze   59:
1.12      schwarze   60:        /* Clean up when dying from a signal. */
                     61:
                     62:        memset(&sa, 0, sizeof(sa));
                     63:        sigfillset(&sa.sa_mask);
                     64:        sa.sa_handler = tag_signal;
                     65:        sigaction(SIGHUP, &sa, NULL);
                     66:        sigaction(SIGINT, &sa, NULL);
                     67:        sigaction(SIGTERM, &sa, NULL);
                     68:
                     69:        /*
                     70:         * POSIX requires that a process calling tcsetpgrp(3)
                     71:         * from the background gets a SIGTTOU signal.
                     72:         * In that case, do not stop.
                     73:         */
                     74:
                     75:        sa.sa_handler = SIG_IGN;
                     76:        sigaction(SIGTTOU, &sa, NULL);
                     77:
1.6       schwarze   78:        /* Save the original standard output for use by the pager. */
                     79:
                     80:        if ((tag_files.ofd = dup(STDOUT_FILENO)) == -1)
                     81:                goto fail;
                     82:
                     83:        /* Create both temporary output files. */
                     84:
                     85:        (void)strlcpy(tag_files.ofn, "/tmp/man.XXXXXXXXXX",
                     86:            sizeof(tag_files.ofn));
                     87:        (void)strlcpy(tag_files.tfn, "/tmp/man.XXXXXXXXXX",
                     88:            sizeof(tag_files.tfn));
                     89:        if ((ofd = mkstemp(tag_files.ofn)) == -1)
                     90:                goto fail;
                     91:        if ((tag_files.tfd = mkstemp(tag_files.tfn)) == -1)
                     92:                goto fail;
                     93:        if (dup2(ofd, STDOUT_FILENO) == -1)
                     94:                goto fail;
                     95:        close(ofd);
                     96:
                     97:        /*
                     98:         * Set up the ohash table to collect output line numbers
                     99:         * where various marked-up terms are documented.
                    100:         */
1.1       schwarze  101:
1.10      schwarze  102:        mandoc_ohash_init(&tag_data, 4, offsetof(struct tag_entry, s));
1.8       schwarze  103:        return &tag_files;
1.6       schwarze  104:
                    105: fail:
                    106:        tag_unlink();
                    107:        if (ofd != -1)
                    108:                close(ofd);
                    109:        if (tag_files.ofd != -1)
                    110:                close(tag_files.ofd);
                    111:        if (tag_files.tfd != -1)
                    112:                close(tag_files.tfd);
                    113:        *tag_files.ofn = '\0';
                    114:        *tag_files.tfn = '\0';
                    115:        tag_files.ofd = -1;
                    116:        tag_files.tfd = -1;
1.8       schwarze  117:        return NULL;
1.1       schwarze  118: }
                    119:
                    120: /*
1.5       schwarze  121:  * Set the line number where a term is defined,
                    122:  * unless it is already defined at a higher priority.
1.1       schwarze  123:  */
                    124: void
1.5       schwarze  125: tag_put(const char *s, int prio, size_t line)
1.1       schwarze  126: {
                    127:        struct tag_entry        *entry;
1.5       schwarze  128:        size_t                   len;
1.1       schwarze  129:        unsigned int             slot;
                    130:
1.15    ! schwarze  131:        /* Sanity checks. */
        !           132:
        !           133:        if (tag_files.tfd <= 0)
        !           134:                return;
        !           135:        if (s[0] == '\\' && (s[1] == '&' || s[1] == 'e'))
        !           136:                s += 2;
        !           137:        if (*s == '\0' || strchr(s, ' ') != NULL)
1.1       schwarze  138:                return;
1.14      schwarze  139:
1.5       schwarze  140:        slot = ohash_qlookup(&tag_data, s);
1.1       schwarze  141:        entry = ohash_find(&tag_data, slot);
1.14      schwarze  142:
1.1       schwarze  143:        if (entry == NULL) {
1.14      schwarze  144:
                    145:                /* Build a new entry. */
                    146:
1.5       schwarze  147:                len = strlen(s) + 1;
                    148:                entry = mandoc_malloc(sizeof(*entry) + len);
1.1       schwarze  149:                memcpy(entry->s, s, len);
1.14      schwarze  150:                entry->lines = NULL;
                    151:                entry->maxlines = entry->nlines = 0;
1.1       schwarze  152:                ohash_insert(&tag_data, slot, entry);
1.14      schwarze  153:
                    154:        } else {
                    155:
                    156:                /* A better entry is already present, ignore the new one. */
                    157:
                    158:                if (entry->prio < prio)
                    159:                        return;
                    160:
                    161:                /* The existing entry is worse, clear it. */
                    162:
                    163:                if (entry->prio > prio)
                    164:                        entry->nlines = 0;
                    165:        }
                    166:
                    167:        /* Remember the new line. */
                    168:
                    169:        if (entry->maxlines == entry->nlines) {
                    170:                entry->maxlines += 4;
                    171:                entry->lines = mandoc_reallocarray(entry->lines,
                    172:                    entry->maxlines, sizeof(*entry->lines));
                    173:        }
                    174:        entry->lines[entry->nlines++] = line;
1.4       schwarze  175:        entry->prio = prio;
1.1       schwarze  176: }
                    177:
                    178: /*
                    179:  * Write out the tags file using the previously collected
                    180:  * information and clear the ohash table while going along.
                    181:  */
                    182: void
                    183: tag_write(void)
                    184: {
                    185:        FILE                    *stream;
                    186:        struct tag_entry        *entry;
1.14      schwarze  187:        size_t                   i;
1.1       schwarze  188:        unsigned int             slot;
                    189:
1.6       schwarze  190:        if (tag_files.tfd <= 0)
1.1       schwarze  191:                return;
1.6       schwarze  192:        stream = fdopen(tag_files.tfd, "w");
1.1       schwarze  193:        entry = ohash_first(&tag_data, &slot);
                    194:        while (entry != NULL) {
                    195:                if (stream != NULL)
1.14      schwarze  196:                        for (i = 0; i < entry->nlines; i++)
                    197:                                fprintf(stream, "%s %s %zu\n",
                    198:                                    entry->s, tag_files.ofn, entry->lines[i]);
                    199:                free(entry->lines);
1.1       schwarze  200:                free(entry);
                    201:                entry = ohash_next(&tag_data, &slot);
                    202:        }
                    203:        ohash_delete(&tag_data);
                    204:        if (stream != NULL)
                    205:                fclose(stream);
                    206: }
                    207:
                    208: void
                    209: tag_unlink(void)
                    210: {
1.11      schwarze  211:        pid_t    tc_pgid;
1.1       schwarze  212:
1.11      schwarze  213:        if (tag_files.tcpgid != -1) {
                    214:                tc_pgid = tcgetpgrp(STDIN_FILENO);
                    215:                if (tc_pgid == tag_files.pager_pid ||
                    216:                    tc_pgid == getpgid(0) ||
                    217:                    getpgid(tc_pgid) == -1)
                    218:                        (void)tcsetpgrp(STDIN_FILENO, tag_files.tcpgid);
                    219:        }
1.6       schwarze  220:        if (*tag_files.ofn != '\0')
                    221:                unlink(tag_files.ofn);
                    222:        if (*tag_files.tfn != '\0')
                    223:                unlink(tag_files.tfn);
1.2       schwarze  224: }
                    225:
                    226: static void
                    227: tag_signal(int signum)
                    228: {
1.11      schwarze  229:        struct sigaction         sa;
1.2       schwarze  230:
                    231:        tag_unlink();
1.11      schwarze  232:        memset(&sa, 0, sizeof(sa));
                    233:        sigemptyset(&sa.sa_mask);
                    234:        sa.sa_handler = SIG_DFL;
                    235:        sigaction(signum, &sa, NULL);
1.2       schwarze  236:        kill(getpid(), signum);
                    237:        /* NOTREACHED */
                    238:        _exit(1);
1.1       schwarze  239: }