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