]>
git.cameronkatri.com Git - mandoc.git/blob - term_tag.c
1 /* $Id: term_tag.c,v 1.1 2020/03/13 15:32:29 schwarze Exp $ */
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.
22 #include <sys/types.h>
37 static void tag_signal(int) __attribute__((__noreturn__
));
39 static struct tag_files tag_files
;
43 * Prepare for using a pager.
44 * Not all pagers are capable of using a tag file,
45 * but for simplicity, create it anyway.
48 term_tag_init(char *tagname
)
51 int ofd
; /* In /tmp/, dup(2)ed to stdout. */
56 tag_files
.tcpgid
= -1;
57 tag_files
.tagname
= tagname
;
59 /* Clean up when dying from a signal. */
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
);
69 * POSIX requires that a process calling tcsetpgrp(3)
70 * from the background gets a SIGTTOU signal.
71 * In that case, do not stop.
74 sa
.sa_handler
= SIG_IGN
;
75 sigaction(SIGTTOU
, &sa
, NULL
);
77 /* Save the original standard output for use by the pager. */
79 if ((tag_files
.ofd
= dup(STDOUT_FILENO
)) == -1) {
80 mandoc_msg(MANDOCERR_DUP
, 0, 0, "%s", strerror(errno
));
84 /* Create both temporary output files. */
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
));
95 if ((tfd
= mkstemp(tag_files
.tfn
)) == -1) {
96 mandoc_msg(MANDOCERR_MKSTEMP
, 0, 0,
97 "%s: %s", tag_files
.tfn
, strerror(errno
));
100 if ((tag_files
.tfs
= fdopen(tfd
, "w")) == NULL
) {
101 mandoc_msg(MANDOCERR_FDOPEN
, 0, 0, "%s", strerror(errno
));
105 if (dup2(ofd
, STDOUT_FILENO
) == -1) {
106 mandoc_msg(MANDOCERR_DUP
, 0, 0, "%s", strerror(errno
));
118 if (tag_files
.ofd
!= -1) {
119 close(tag_files
.ofd
);
122 tag_files
.tagname
= NULL
;
127 term_tag_write(struct roff_node
*n
, size_t line
)
132 if (tag_files
.tfs
== NULL
)
134 if (n
->string
== NULL
)
137 if (cp
[0] == '\\' && (cp
[1] == '&' || cp
[1] == 'e'))
139 len
= strcspn(cp
, " \t\\");
140 fprintf(tag_files
.tfs
, "%.*s %s %zu\n",
141 len
, cp
, tag_files
.ofn
, line
);
145 term_tag_finish(void)
147 if (tag_files
.tfs
== NULL
)
149 fclose(tag_files
.tfs
);
150 tag_files
.tfs
= NULL
;
151 switch (tag_check(tag_files
.tagname
)) {
153 unlink(tag_files
.tfn
);
154 *tag_files
.tfn
= '\0';
157 if (tag_files
.tagname
== NULL
)
159 mandoc_msg(MANDOCERR_TAG
, 0, 0, "%s", tag_files
.tagname
);
160 tag_files
.tagname
= NULL
;
168 term_tag_unlink(void)
172 if (tag_files
.tcpgid
!= -1) {
173 tc_pgid
= tcgetpgrp(tag_files
.ofd
);
174 if (tc_pgid
== tag_files
.pager_pid
||
175 tc_pgid
== getpgid(0) ||
176 getpgid(tc_pgid
) == -1)
177 (void)tcsetpgrp(tag_files
.ofd
, tag_files
.tcpgid
);
179 if (*tag_files
.ofn
!= '\0') {
180 unlink(tag_files
.ofn
);
181 *tag_files
.ofn
= '\0';
183 if (*tag_files
.tfn
!= '\0') {
184 unlink(tag_files
.tfn
);
185 *tag_files
.tfn
= '\0';
187 if (tag_files
.tfs
!= NULL
) {
188 fclose(tag_files
.tfs
);
189 tag_files
.tfs
= NULL
;
194 tag_signal(int signum
)
199 memset(&sa
, 0, sizeof(sa
));
200 sigemptyset(&sa
.sa_mask
);
201 sa
.sa_handler
= SIG_DFL
;
202 sigaction(signum
, &sa
, NULL
);
203 kill(getpid(), signum
);