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