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