usr.sbin/makefs: Add -o c|C option to specify comp|check type
[dragonfly.git] / contrib / mdocml / term_tag.c
blobc26b942531fc082a2ac62669544e103c4e55c0af
1 /* $Id: term_tag.c,v 1.6 2021/03/30 17:16:55 schwarze Exp $ */
2 /*
3 * Copyright (c) 2015,2016,2018,2019,2020 Ingo Schwarze <schwarze@openbsd.org>
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.
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.
17 * Functions to write a ctags(1) file.
18 * For use by the mandoc(1) ASCII and UTF-8 formatters only.
20 #include "config.h"
22 #include <sys/types.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <stddef.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
33 #include "mandoc.h"
34 #include "roff.h"
35 #include "roff_int.h"
36 #include "tag.h"
37 #include "term_tag.h"
39 static void tag_signal(int) __attribute__((__noreturn__));
41 static struct tag_files tag_files;
45 * Prepare for using a pager.
46 * Not all pagers are capable of using a tag file,
47 * but for simplicity, create it anyway.
49 struct tag_files *
50 term_tag_init(const char *outfilename, const char *suffix,
51 const char *tagfilename)
53 struct sigaction sa;
54 int ofd; /* In /tmp/, dup(2)ed to stdout. */
55 int tfd;
57 ofd = tfd = -1;
58 tag_files.tfs = NULL;
59 tag_files.tcpgid = -1;
61 /* Clean up when dying from a signal. */
63 memset(&sa, 0, sizeof(sa));
64 sigfillset(&sa.sa_mask);
65 sa.sa_handler = tag_signal;
66 sigaction(SIGHUP, &sa, NULL);
67 sigaction(SIGINT, &sa, NULL);
68 sigaction(SIGTERM, &sa, NULL);
71 * POSIX requires that a process calling tcsetpgrp(3)
72 * from the background gets a SIGTTOU signal.
73 * In that case, do not stop.
76 sa.sa_handler = SIG_IGN;
77 sigaction(SIGTTOU, &sa, NULL);
79 /* Save the original standard output for use by the pager. */
81 if ((tag_files.ofd = dup(STDOUT_FILENO)) == -1) {
82 mandoc_msg(MANDOCERR_DUP, 0, 0, "%s", strerror(errno));
83 goto fail;
86 /* Create both temporary output files. */
88 if (outfilename == NULL) {
89 (void)snprintf(tag_files.ofn, sizeof(tag_files.ofn),
90 "/tmp/man.XXXXXXXXXX%s", suffix);
91 if ((ofd = mkstemps(tag_files.ofn, strlen(suffix))) == -1) {
92 mandoc_msg(MANDOCERR_MKSTEMP, 0, 0,
93 "%s: %s", tag_files.ofn, strerror(errno));
94 goto fail;
96 } else {
97 (void)strlcpy(tag_files.ofn, outfilename,
98 sizeof(tag_files.ofn));
99 unlink(outfilename);
100 ofd = open(outfilename, O_WRONLY | O_CREAT | O_EXCL, 0644);
101 if (ofd == -1) {
102 mandoc_msg(MANDOCERR_OPEN, 0, 0,
103 "%s: %s", outfilename, strerror(errno));
104 goto fail;
107 if (tagfilename == NULL) {
108 (void)strlcpy(tag_files.tfn, "/tmp/man.XXXXXXXXXX",
109 sizeof(tag_files.tfn));
110 if ((tfd = mkstemp(tag_files.tfn)) == -1) {
111 mandoc_msg(MANDOCERR_MKSTEMP, 0, 0,
112 "%s: %s", tag_files.tfn, strerror(errno));
113 goto fail;
115 } else {
116 (void)strlcpy(tag_files.tfn, tagfilename,
117 sizeof(tag_files.tfn));
118 unlink(tagfilename);
119 tfd = open(tagfilename, O_WRONLY | O_CREAT | O_EXCL, 0644);
120 if (tfd == -1) {
121 mandoc_msg(MANDOCERR_OPEN, 0, 0,
122 "%s: %s", tagfilename, strerror(errno));
123 goto fail;
126 if ((tag_files.tfs = fdopen(tfd, "w")) == NULL) {
127 mandoc_msg(MANDOCERR_FDOPEN, 0, 0, "%s", strerror(errno));
128 goto fail;
130 tfd = -1;
131 if (dup2(ofd, STDOUT_FILENO) == -1) {
132 mandoc_msg(MANDOCERR_DUP, 0, 0, "%s", strerror(errno));
133 goto fail;
135 close(ofd);
136 return &tag_files;
138 fail:
139 term_tag_unlink();
140 if (ofd != -1)
141 close(ofd);
142 if (tfd != -1)
143 close(tfd);
144 if (tag_files.ofd != -1) {
145 close(tag_files.ofd);
146 tag_files.ofd = -1;
148 return NULL;
151 void
152 term_tag_write(struct roff_node *n, size_t line)
154 const char *cp;
155 int len;
157 if (tag_files.tfs == NULL)
158 return;
159 cp = n->tag == NULL ? n->child->string : n->tag;
160 if (cp[0] == '\\' && (cp[1] == '&' || cp[1] == 'e'))
161 cp += 2;
162 len = strcspn(cp, " \t\\");
163 fprintf(tag_files.tfs, "%.*s %s %zu\n",
164 len, cp, tag_files.ofn, line);
168 * Close both output files and restore the original standard output
169 * to the terminal. In the unlikely case that the latter fails,
170 * trying to start a pager would be useless, so report the failure
171 * to the main program.
174 term_tag_close(void)
176 int irc = 0;
178 if (tag_files.tfs != NULL) {
179 fclose(tag_files.tfs);
180 tag_files.tfs = NULL;
182 if (tag_files.ofd != -1) {
183 fflush(stdout);
184 if ((irc = dup2(tag_files.ofd, STDOUT_FILENO)) == -1)
185 mandoc_msg(MANDOCERR_DUP, 0, 0, "%s", strerror(errno));
186 close(tag_files.ofd);
187 tag_files.ofd = -1;
189 return irc;
192 void
193 term_tag_unlink(void)
195 pid_t tc_pgid;
197 if (tag_files.tcpgid != -1) {
198 tc_pgid = tcgetpgrp(STDOUT_FILENO);
199 if (tc_pgid == tag_files.pager_pid ||
200 tc_pgid == getpgid(0) ||
201 getpgid(tc_pgid) == -1)
202 (void)tcsetpgrp(STDOUT_FILENO, tag_files.tcpgid);
204 if (strncmp(tag_files.ofn, "/tmp/man.", 9) == 0) {
205 unlink(tag_files.ofn);
206 *tag_files.ofn = '\0';
208 if (strncmp(tag_files.tfn, "/tmp/man.", 9) == 0) {
209 unlink(tag_files.tfn);
210 *tag_files.tfn = '\0';
214 static void
215 tag_signal(int signum)
217 struct sigaction sa;
219 term_tag_unlink();
220 memset(&sa, 0, sizeof(sa));
221 sigemptyset(&sa.sa_mask);
222 sa.sa_handler = SIG_DFL;
223 sigaction(signum, &sa, NULL);
224 kill(getpid(), signum);
225 /* NOTREACHED */
226 _exit(1);