xref: /openbsd-src/usr.bin/mandoc/tag.c (revision f6b75673f6c960a9743bfd16c1e52dd100265c68)
1 /*	$OpenBSD: tag.c,v 1.21 2018/11/22 11:30:15 schwarze Exp $ */
2 /*
3  * Copyright (c) 2015, 2016, 2018 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 #include <sys/types.h>
18 
19 #include <err.h>
20 #include <limits.h>
21 #include <signal.h>
22 #include <stddef.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 
29 #include "mandoc_aux.h"
30 #include "mandoc_ohash.h"
31 #include "tag.h"
32 
33 struct tag_entry {
34 	size_t	*lines;
35 	size_t	 maxlines;
36 	size_t	 nlines;
37 	int	 prio;
38 	char	 s[];
39 };
40 
41 static	void	 tag_signal(int) __attribute__((__noreturn__));
42 
43 static struct ohash	 tag_data;
44 static struct tag_files	 tag_files;
45 
46 
47 /*
48  * Prepare for using a pager.
49  * Not all pagers are capable of using a tag file,
50  * but for simplicity, create it anyway.
51  */
52 struct tag_files *
53 tag_init(void)
54 {
55 	struct sigaction	 sa;
56 	int			 ofd;
57 
58 	ofd = -1;
59 	tag_files.tfd = -1;
60 	tag_files.tcpgid = -1;
61 
62 	/* Clean up when dying from a signal. */
63 
64 	memset(&sa, 0, sizeof(sa));
65 	sigfillset(&sa.sa_mask);
66 	sa.sa_handler = tag_signal;
67 	sigaction(SIGHUP, &sa, NULL);
68 	sigaction(SIGINT, &sa, NULL);
69 	sigaction(SIGTERM, &sa, NULL);
70 
71 	/*
72 	 * POSIX requires that a process calling tcsetpgrp(3)
73 	 * from the background gets a SIGTTOU signal.
74 	 * In that case, do not stop.
75 	 */
76 
77 	sa.sa_handler = SIG_IGN;
78 	sigaction(SIGTTOU, &sa, NULL);
79 
80 	/* Save the original standard output for use by the pager. */
81 
82 	if ((tag_files.ofd = dup(STDOUT_FILENO)) == -1)
83 		goto fail;
84 
85 	/* Create both temporary output files. */
86 
87 	(void)strlcpy(tag_files.ofn, "/tmp/man.XXXXXXXXXX",
88 	    sizeof(tag_files.ofn));
89 	(void)strlcpy(tag_files.tfn, "/tmp/man.XXXXXXXXXX",
90 	    sizeof(tag_files.tfn));
91 	if ((ofd = mkstemp(tag_files.ofn)) == -1)
92 		goto fail;
93 	if ((tag_files.tfd = mkstemp(tag_files.tfn)) == -1)
94 		goto fail;
95 	if (dup2(ofd, STDOUT_FILENO) == -1)
96 		goto fail;
97 	close(ofd);
98 
99 	/*
100 	 * Set up the ohash table to collect output line numbers
101 	 * where various marked-up terms are documented.
102 	 */
103 
104 	mandoc_ohash_init(&tag_data, 4, offsetof(struct tag_entry, s));
105 	return &tag_files;
106 
107 fail:
108 	tag_unlink();
109 	if (ofd != -1)
110 		close(ofd);
111 	if (tag_files.ofd != -1)
112 		close(tag_files.ofd);
113 	if (tag_files.tfd != -1)
114 		close(tag_files.tfd);
115 	*tag_files.ofn = '\0';
116 	*tag_files.tfn = '\0';
117 	tag_files.ofd = -1;
118 	tag_files.tfd = -1;
119 	return NULL;
120 }
121 
122 /*
123  * Set the line number where a term is defined,
124  * unless it is already defined at a lower priority.
125  */
126 void
127 tag_put(const char *s, int prio, size_t line)
128 {
129 	struct tag_entry	*entry;
130 	const char		*se;
131 	size_t			 len;
132 	unsigned int		 slot;
133 
134 	if (tag_files.tfd <= 0)
135 		return;
136 
137 	if (s[0] == '\\' && (s[1] == '&' || s[1] == 'e'))
138 		s += 2;
139 
140 	/*
141 	 * Skip whitespace and whatever follows it,
142 	 * and if there is any, downgrade the priority.
143 	 */
144 
145 	len = strcspn(s, " \t");
146 	if (len == 0)
147 		return;
148 
149 	se = s + len;
150 	if (*se != '\0')
151 		prio = INT_MAX;
152 
153 	slot = ohash_qlookupi(&tag_data, s, &se);
154 	entry = ohash_find(&tag_data, slot);
155 
156 	if (entry == NULL) {
157 
158 		/* Build a new entry. */
159 
160 		entry = mandoc_malloc(sizeof(*entry) + len + 1);
161 		memcpy(entry->s, s, len);
162 		entry->s[len] = '\0';
163 		entry->lines = NULL;
164 		entry->maxlines = entry->nlines = 0;
165 		ohash_insert(&tag_data, slot, entry);
166 
167 	} else {
168 
169 		/*
170 		 * Lower priority numbers take precedence,
171 		 * but 0 is special.
172 		 * A tag with priority 0 is only used
173 		 * if the tag occurs exactly once.
174 		 */
175 
176 		if (prio == 0) {
177 			if (entry->prio == 0)
178 				entry->prio = -1;
179 			return;
180 		}
181 
182 		/* A better entry is already present, ignore the new one. */
183 
184 		if (entry->prio > 0 && entry->prio < prio)
185 			return;
186 
187 		/* The existing entry is worse, clear it. */
188 
189 		if (entry->prio < 1 || entry->prio > prio)
190 			entry->nlines = 0;
191 	}
192 
193 	/* Remember the new line. */
194 
195 	if (entry->maxlines == entry->nlines) {
196 		entry->maxlines += 4;
197 		entry->lines = mandoc_reallocarray(entry->lines,
198 		    entry->maxlines, sizeof(*entry->lines));
199 	}
200 	entry->lines[entry->nlines++] = line;
201 	entry->prio = prio;
202 }
203 
204 /*
205  * Write out the tags file using the previously collected
206  * information and clear the ohash table while going along.
207  */
208 void
209 tag_write(void)
210 {
211 	FILE			*stream;
212 	struct tag_entry	*entry;
213 	size_t			 i;
214 	unsigned int		 slot;
215 
216 	if (tag_files.tfd <= 0)
217 		return;
218 	if (tag_files.tagname != NULL && ohash_find(&tag_data,
219             ohash_qlookup(&tag_data, tag_files.tagname)) == NULL) {
220 		warnx("%s: no such tag", tag_files.tagname);
221 		tag_files.tagname = NULL;
222 	}
223 	stream = fdopen(tag_files.tfd, "w");
224 	entry = ohash_first(&tag_data, &slot);
225 	while (entry != NULL) {
226 		if (stream != NULL && entry->prio >= 0)
227 			for (i = 0; i < entry->nlines; i++)
228 				fprintf(stream, "%s %s %zu\n",
229 				    entry->s, tag_files.ofn, entry->lines[i]);
230 		free(entry->lines);
231 		free(entry);
232 		entry = ohash_next(&tag_data, &slot);
233 	}
234 	ohash_delete(&tag_data);
235 	if (stream != NULL)
236 		fclose(stream);
237 	else
238 		close(tag_files.tfd);
239 	tag_files.tfd = -1;
240 }
241 
242 void
243 tag_unlink(void)
244 {
245 	pid_t	 tc_pgid;
246 
247 	if (tag_files.tcpgid != -1) {
248 		tc_pgid = tcgetpgrp(tag_files.ofd);
249 		if (tc_pgid == tag_files.pager_pid ||
250 		    tc_pgid == getpgid(0) ||
251 		    getpgid(tc_pgid) == -1)
252 			(void)tcsetpgrp(tag_files.ofd, tag_files.tcpgid);
253 	}
254 	if (*tag_files.ofn != '\0')
255 		unlink(tag_files.ofn);
256 	if (*tag_files.tfn != '\0')
257 		unlink(tag_files.tfn);
258 }
259 
260 static void
261 tag_signal(int signum)
262 {
263 	struct sigaction	 sa;
264 
265 	tag_unlink();
266 	memset(&sa, 0, sizeof(sa));
267 	sigemptyset(&sa.sa_mask);
268 	sa.sa_handler = SIG_DFL;
269 	sigaction(signum, &sa, NULL);
270 	kill(getpid(), signum);
271 	/* NOTREACHED */
272 	_exit(1);
273 }
274