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

Annotation of mandoc/term_tag.c, Revision 1.4

1.4     ! schwarze    1: /* $Id: term_tag.c,v 1.3 2020/04/08 11:56:04 schwarze Exp $ */
1.1       schwarze    2: /*
                      3:  * Copyright (c) 2015,2016,2018,2019,2020 Ingo Schwarze <schwarze@openbsd.org>
                      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:  * Functions to write a ctags(1) file.
                     18:  * For use by the mandoc(1) ASCII and UTF-8 formatters only.
                     19:  */
                     20: #include "config.h"
                     21:
                     22: #include <sys/types.h>
                     23:
                     24: #include <errno.h>
                     25: #include <signal.h>
                     26: #include <stddef.h>
                     27: #include <stdio.h>
                     28: #include <stdlib.h>
                     29: #include <string.h>
                     30: #include <unistd.h>
                     31:
                     32: #include "mandoc.h"
                     33: #include "roff.h"
1.4     ! schwarze   34: #include "roff_int.h"
1.1       schwarze   35: #include "tag.h"
                     36: #include "term_tag.h"
                     37:
                     38: static void tag_signal(int) __attribute__((__noreturn__));
                     39:
                     40: static struct tag_files tag_files;
                     41:
                     42:
                     43: /*
                     44:  * Prepare for using a pager.
                     45:  * Not all pagers are capable of using a tag file,
                     46:  * but for simplicity, create it anyway.
                     47:  */
                     48: struct tag_files *
1.2       schwarze   49: term_tag_init(void)
1.1       schwarze   50: {
                     51:        struct sigaction         sa;
                     52:        int                      ofd;   /* In /tmp/, dup(2)ed to stdout. */
                     53:        int                      tfd;
                     54:
                     55:        ofd = tfd = -1;
                     56:        tag_files.tfs = NULL;
                     57:        tag_files.tcpgid = -1;
                     58:
                     59:        /* Clean up when dying from a signal. */
                     60:
                     61:        memset(&sa, 0, sizeof(sa));
                     62:        sigfillset(&sa.sa_mask);
                     63:        sa.sa_handler = tag_signal;
                     64:        sigaction(SIGHUP, &sa, NULL);
                     65:        sigaction(SIGINT, &sa, NULL);
                     66:        sigaction(SIGTERM, &sa, NULL);
                     67:
                     68:        /*
                     69:         * POSIX requires that a process calling tcsetpgrp(3)
                     70:         * from the background gets a SIGTTOU signal.
                     71:         * In that case, do not stop.
                     72:         */
                     73:
                     74:        sa.sa_handler = SIG_IGN;
                     75:        sigaction(SIGTTOU, &sa, NULL);
                     76:
                     77:        /* Save the original standard output for use by the pager. */
                     78:
                     79:        if ((tag_files.ofd = dup(STDOUT_FILENO)) == -1) {
                     80:                mandoc_msg(MANDOCERR_DUP, 0, 0, "%s", strerror(errno));
                     81:                goto fail;
                     82:        }
                     83:
                     84:        /* Create both temporary output files. */
                     85:
                     86:        (void)strlcpy(tag_files.ofn, "/tmp/man.XXXXXXXXXX",
                     87:            sizeof(tag_files.ofn));
                     88:        (void)strlcpy(tag_files.tfn, "/tmp/man.XXXXXXXXXX",
                     89:            sizeof(tag_files.tfn));
                     90:        if ((ofd = mkstemp(tag_files.ofn)) == -1) {
                     91:                mandoc_msg(MANDOCERR_MKSTEMP, 0, 0,
                     92:                    "%s: %s", tag_files.ofn, strerror(errno));
                     93:                goto fail;
                     94:        }
                     95:        if ((tfd = mkstemp(tag_files.tfn)) == -1) {
                     96:                mandoc_msg(MANDOCERR_MKSTEMP, 0, 0,
                     97:                    "%s: %s", tag_files.tfn, strerror(errno));
                     98:                goto fail;
                     99:        }
                    100:        if ((tag_files.tfs = fdopen(tfd, "w")) == NULL) {
                    101:                mandoc_msg(MANDOCERR_FDOPEN, 0, 0, "%s", strerror(errno));
                    102:                goto fail;
                    103:        }
                    104:        tfd = -1;
                    105:        if (dup2(ofd, STDOUT_FILENO) == -1) {
                    106:                mandoc_msg(MANDOCERR_DUP, 0, 0, "%s", strerror(errno));
                    107:                goto fail;
                    108:        }
                    109:        close(ofd);
                    110:        return &tag_files;
                    111:
                    112: fail:
                    113:        term_tag_unlink();
                    114:        if (ofd != -1)
                    115:                close(ofd);
                    116:        if (tfd != -1)
                    117:                close(tfd);
                    118:        if (tag_files.ofd != -1) {
                    119:                close(tag_files.ofd);
                    120:                tag_files.ofd = -1;
                    121:        }
                    122:        return NULL;
                    123: }
                    124:
                    125: void
                    126: term_tag_write(struct roff_node *n, size_t line)
                    127: {
                    128:        const char      *cp;
                    129:        int              len;
                    130:
                    131:        if (tag_files.tfs == NULL)
                    132:                return;
1.3       schwarze  133:        cp = n->tag == NULL ? n->child->string : n->tag;
1.1       schwarze  134:        if (cp[0] == '\\' && (cp[1] == '&' || cp[1] == 'e'))
                    135:                cp += 2;
                    136:        len = strcspn(cp, " \t\\");
                    137:        fprintf(tag_files.tfs, "%.*s %s %zu\n",
                    138:            len, cp, tag_files.ofn, line);
                    139: }
                    140:
1.2       schwarze  141: /*
                    142:  * Close both output files and restore the original standard output
                    143:  * to the terminal.  In the unlikely case that the latter fails,
                    144:  * trying to start a pager would be useless, so report the failure
                    145:  * to the main program.
                    146:  */
                    147: int
                    148: term_tag_close(void)
1.1       schwarze  149: {
1.2       schwarze  150:        int irc = 0;
                    151:
                    152:        if (tag_files.tfs != NULL) {
                    153:                fclose(tag_files.tfs);
                    154:                tag_files.tfs = NULL;
                    155:        }
                    156:        if (tag_files.ofd != -1) {
                    157:                fflush(stdout);
                    158:                if ((irc = dup2(tag_files.ofd, STDOUT_FILENO)) == -1)
                    159:                        mandoc_msg(MANDOCERR_DUP, 0, 0, "%s", strerror(errno));
                    160:                close(tag_files.ofd);
                    161:                tag_files.ofd = -1;
1.1       schwarze  162:        }
1.2       schwarze  163:        return irc;
1.1       schwarze  164: }
                    165:
                    166: void
                    167: term_tag_unlink(void)
                    168: {
                    169:        pid_t    tc_pgid;
                    170:
                    171:        if (tag_files.tcpgid != -1) {
1.2       schwarze  172:                tc_pgid = tcgetpgrp(STDOUT_FILENO);
1.1       schwarze  173:                if (tc_pgid == tag_files.pager_pid ||
                    174:                    tc_pgid == getpgid(0) ||
                    175:                    getpgid(tc_pgid) == -1)
1.2       schwarze  176:                        (void)tcsetpgrp(STDOUT_FILENO, tag_files.tcpgid);
1.1       schwarze  177:        }
                    178:        if (*tag_files.ofn != '\0') {
                    179:                unlink(tag_files.ofn);
                    180:                *tag_files.ofn = '\0';
                    181:        }
                    182:        if (*tag_files.tfn != '\0') {
                    183:                unlink(tag_files.tfn);
                    184:                *tag_files.tfn = '\0';
                    185:        }
                    186: }
                    187:
                    188: static void
                    189: tag_signal(int signum)
                    190: {
                    191:        struct sigaction         sa;
                    192:
                    193:        term_tag_unlink();
                    194:        memset(&sa, 0, sizeof(sa));
                    195:        sigemptyset(&sa.sa_mask);
                    196:        sa.sa_handler = SIG_DFL;
                    197:        sigaction(signum, &sa, NULL);
                    198:        kill(getpid(), signum);
                    199:        /* NOTREACHED */
                    200:        _exit(1);
                    201: }

CVSweb