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