[BACK]Return to tag.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/tag.c, Revision 1.21

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

CVSweb