Print this page
9718 update mandoc to 1.14.4
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/mandoc/tag.c
+++ new/usr/src/cmd/mandoc/tag.c
1 -/* $Id: tag.c,v 1.18 2017/02/17 14:31:52 schwarze Exp $ */
1 +/* $Id: tag.c,v 1.19 2018/02/23 16:47:10 schwarze Exp $ */
2 2 /*
3 3 * Copyright (c) 2015, 2016 Ingo Schwarze <schwarze@openbsd.org>
4 4 *
5 5 * Permission to use, copy, modify, and distribute this software for any
6 6 * purpose with or without fee is hereby granted, provided that the above
7 7 * copyright notice and this permission notice appear in all copies.
8 8 *
9 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 16 */
17 17 #include "config.h"
18 18
19 19 #include <sys/types.h>
20 20
21 21 #include <signal.h>
22 22 #include <stddef.h>
23 23 #include <stdint.h>
24 24 #include <stdio.h>
25 25 #include <stdlib.h>
26 26 #include <string.h>
27 27 #include <unistd.h>
28 28
29 29 #include "mandoc_aux.h"
30 30 #include "mandoc_ohash.h"
31 31 #include "tag.h"
32 32
33 33 struct tag_entry {
34 34 size_t *lines;
35 35 size_t maxlines;
36 36 size_t nlines;
37 37 int prio;
38 38 char s[];
39 39 };
40 40
41 41 static void tag_signal(int) __attribute__((__noreturn__));
42 42
43 43 static struct ohash tag_data;
44 44 static struct tag_files tag_files;
45 45
46 46
47 47 /*
48 48 * Prepare for using a pager.
49 49 * Not all pagers are capable of using a tag file,
50 50 * but for simplicity, create it anyway.
51 51 */
52 52 struct tag_files *
53 53 tag_init(void)
54 54 {
55 55 struct sigaction sa;
56 56 int ofd;
57 57
58 58 ofd = -1;
59 59 tag_files.tfd = -1;
60 60 tag_files.tcpgid = -1;
61 61
62 62 /* Clean up when dying from a signal. */
63 63
64 64 memset(&sa, 0, sizeof(sa));
65 65 sigfillset(&sa.sa_mask);
66 66 sa.sa_handler = tag_signal;
67 67 sigaction(SIGHUP, &sa, NULL);
68 68 sigaction(SIGINT, &sa, NULL);
69 69 sigaction(SIGTERM, &sa, NULL);
70 70
71 71 /*
72 72 * POSIX requires that a process calling tcsetpgrp(3)
73 73 * from the background gets a SIGTTOU signal.
74 74 * In that case, do not stop.
75 75 */
76 76
77 77 sa.sa_handler = SIG_IGN;
78 78 sigaction(SIGTTOU, &sa, NULL);
79 79
80 80 /* Save the original standard output for use by the pager. */
81 81
82 82 if ((tag_files.ofd = dup(STDOUT_FILENO)) == -1)
83 83 goto fail;
84 84
85 85 /* Create both temporary output files. */
86 86
87 87 (void)strlcpy(tag_files.ofn, "/tmp/man.XXXXXXXXXX",
88 88 sizeof(tag_files.ofn));
89 89 (void)strlcpy(tag_files.tfn, "/tmp/man.XXXXXXXXXX",
90 90 sizeof(tag_files.tfn));
91 91 if ((ofd = mkstemp(tag_files.ofn)) == -1)
92 92 goto fail;
93 93 if ((tag_files.tfd = mkstemp(tag_files.tfn)) == -1)
94 94 goto fail;
95 95 if (dup2(ofd, STDOUT_FILENO) == -1)
96 96 goto fail;
97 97 close(ofd);
98 98
99 99 /*
100 100 * Set up the ohash table to collect output line numbers
101 101 * where various marked-up terms are documented.
102 102 */
103 103
104 104 mandoc_ohash_init(&tag_data, 4, offsetof(struct tag_entry, s));
105 105 return &tag_files;
106 106
107 107 fail:
108 108 tag_unlink();
109 109 if (ofd != -1)
110 110 close(ofd);
111 111 if (tag_files.ofd != -1)
112 112 close(tag_files.ofd);
113 113 if (tag_files.tfd != -1)
114 114 close(tag_files.tfd);
115 115 *tag_files.ofn = '\0';
116 116 *tag_files.tfn = '\0';
117 117 tag_files.ofd = -1;
118 118 tag_files.tfd = -1;
119 119 return NULL;
120 120 }
121 121
122 122 /*
123 123 * Set the line number where a term is defined,
124 124 * unless it is already defined at a higher priority.
125 125 */
126 126 void
127 127 tag_put(const char *s, int prio, size_t line)
128 128 {
129 129 struct tag_entry *entry;
130 130 size_t len;
131 131 unsigned int slot;
132 132
133 133 /* Sanity checks. */
134 134
135 135 if (tag_files.tfd <= 0)
136 136 return;
137 137 if (s[0] == '\\' && (s[1] == '&' || s[1] == 'e'))
138 138 s += 2;
139 139 if (*s == '\0' || strchr(s, ' ') != NULL)
140 140 return;
141 141
142 142 slot = ohash_qlookup(&tag_data, s);
143 143 entry = ohash_find(&tag_data, slot);
144 144
145 145 if (entry == NULL) {
146 146
147 147 /* Build a new entry. */
148 148
149 149 len = strlen(s) + 1;
150 150 entry = mandoc_malloc(sizeof(*entry) + len);
151 151 memcpy(entry->s, s, len);
152 152 entry->lines = NULL;
153 153 entry->maxlines = entry->nlines = 0;
154 154 ohash_insert(&tag_data, slot, entry);
155 155
156 156 } else {
157 157
158 158 /* Handle priority 0 entries. */
159 159
160 160 if (prio == 0) {
161 161 if (entry->prio == 0)
162 162 entry->prio = -1;
163 163 return;
164 164 }
165 165
166 166 /* A better entry is already present, ignore the new one. */
167 167
168 168 if (entry->prio > 0 && entry->prio < prio)
169 169 return;
170 170
171 171 /* The existing entry is worse, clear it. */
172 172
173 173 if (entry->prio < 1 || entry->prio > prio)
174 174 entry->nlines = 0;
175 175 }
176 176
177 177 /* Remember the new line. */
178 178
179 179 if (entry->maxlines == entry->nlines) {
180 180 entry->maxlines += 4;
181 181 entry->lines = mandoc_reallocarray(entry->lines,
182 182 entry->maxlines, sizeof(*entry->lines));
183 183 }
184 184 entry->lines[entry->nlines++] = line;
185 185 entry->prio = prio;
186 186 }
187 187
188 188 /*
189 189 * Write out the tags file using the previously collected
190 190 * information and clear the ohash table while going along.
191 191 */
192 192 void
193 193 tag_write(void)
194 194 {
195 195 FILE *stream;
196 196 struct tag_entry *entry;
197 197 size_t i;
198 198 unsigned int slot;
199 199
200 200 if (tag_files.tfd <= 0)
201 201 return;
202 202 stream = fdopen(tag_files.tfd, "w");
203 203 entry = ohash_first(&tag_data, &slot);
204 204 while (entry != NULL) {
205 205 if (stream != NULL && entry->prio >= 0)
↓ open down ↓ |
194 lines elided |
↑ open up ↑ |
206 206 for (i = 0; i < entry->nlines; i++)
207 207 fprintf(stream, "%s %s %zu\n",
208 208 entry->s, tag_files.ofn, entry->lines[i]);
209 209 free(entry->lines);
210 210 free(entry);
211 211 entry = ohash_next(&tag_data, &slot);
212 212 }
213 213 ohash_delete(&tag_data);
214 214 if (stream != NULL)
215 215 fclose(stream);
216 + else
217 + close(tag_files.tfd);
218 + tag_files.tfd = -1;
216 219 }
217 220
218 221 void
219 222 tag_unlink(void)
220 223 {
221 224 pid_t tc_pgid;
222 225
223 226 if (tag_files.tcpgid != -1) {
224 227 tc_pgid = tcgetpgrp(tag_files.ofd);
225 228 if (tc_pgid == tag_files.pager_pid ||
226 229 tc_pgid == getpgid(0) ||
227 230 getpgid(tc_pgid) == -1)
228 231 (void)tcsetpgrp(tag_files.ofd, tag_files.tcpgid);
229 232 }
230 233 if (*tag_files.ofn != '\0')
231 234 unlink(tag_files.ofn);
232 235 if (*tag_files.tfn != '\0')
233 236 unlink(tag_files.tfn);
234 237 }
235 238
236 239 static void
237 240 tag_signal(int signum)
238 241 {
239 242 struct sigaction sa;
240 243
241 244 tag_unlink();
242 245 memset(&sa, 0, sizeof(sa));
243 246 sigemptyset(&sa.sa_mask);
244 247 sa.sa_handler = SIG_DFL;
245 248 sigaction(signum, &sa, NULL);
246 249 kill(getpid(), signum);
247 250 /* NOTREACHED */
248 251 _exit(1);
249 252 }
↓ open down ↓ |
24 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX