]>
git.cameronkatri.com Git - mandoc.git/blob - tag.c
1 /* $Id: tag.c,v 1.25 2019/07/27 13:40:57 schwarze Exp $ */
3 * Copyright (c) 2015, 2016, 2018, 2019 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.
19 #include <sys/types.h>
31 #include "mandoc_aux.h"
32 #include "mandoc_ohash.h"
44 static void tag_signal(int) __attribute__((__noreturn__
));
46 static struct ohash tag_data
;
47 static struct tag_files tag_files
;
51 * Prepare for using a pager.
52 * Not all pagers are capable of using a tag file,
53 * but for simplicity, create it anyway.
56 tag_init(char *tagname
)
63 tag_files
.tcpgid
= -1;
64 tag_files
.tagname
= tagname
;
66 /* Clean up when dying from a signal. */
68 memset(&sa
, 0, sizeof(sa
));
69 sigfillset(&sa
.sa_mask
);
70 sa
.sa_handler
= tag_signal
;
71 sigaction(SIGHUP
, &sa
, NULL
);
72 sigaction(SIGINT
, &sa
, NULL
);
73 sigaction(SIGTERM
, &sa
, NULL
);
76 * POSIX requires that a process calling tcsetpgrp(3)
77 * from the background gets a SIGTTOU signal.
78 * In that case, do not stop.
81 sa
.sa_handler
= SIG_IGN
;
82 sigaction(SIGTTOU
, &sa
, NULL
);
84 /* Save the original standard output for use by the pager. */
86 if ((tag_files
.ofd
= dup(STDOUT_FILENO
)) == -1) {
87 mandoc_msg(MANDOCERR_DUP
, 0, 0, "%s", strerror(errno
));
91 /* Create both temporary output files. */
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 if ((ofd
= mkstemp(tag_files
.ofn
)) == -1) {
98 mandoc_msg(MANDOCERR_MKSTEMP
, 0, 0,
99 "%s: %s", tag_files
.ofn
, strerror(errno
));
102 if ((tag_files
.tfd
= mkstemp(tag_files
.tfn
)) == -1) {
103 mandoc_msg(MANDOCERR_MKSTEMP
, 0, 0,
104 "%s: %s", tag_files
.tfn
, strerror(errno
));
107 if (dup2(ofd
, STDOUT_FILENO
) == -1) {
108 mandoc_msg(MANDOCERR_DUP
, 0, 0, "%s", strerror(errno
));
114 * Set up the ohash table to collect output line numbers
115 * where various marked-up terms are documented.
118 mandoc_ohash_init(&tag_data
, 4, offsetof(struct tag_entry
, s
));
125 if (tag_files
.ofd
!= -1)
126 close(tag_files
.ofd
);
127 if (tag_files
.tfd
!= -1)
128 close(tag_files
.tfd
);
129 *tag_files
.ofn
= '\0';
130 *tag_files
.tfn
= '\0';
133 tag_files
.tagname
= NULL
;
138 * Set the line number where a term is defined,
139 * unless it is already defined at a lower priority.
142 tag_put(const char *s
, int prio
, size_t line
)
144 struct tag_entry
*entry
;
149 if (tag_files
.tfd
<= 0)
152 if (s
[0] == '\\' && (s
[1] == '&' || s
[1] == 'e'))
156 * Skip whitespace and escapes and whatever follows,
157 * and if there is any, downgrade the priority.
160 len
= strcspn(s
, " \t\\");
168 slot
= ohash_qlookupi(&tag_data
, s
, &se
);
169 entry
= ohash_find(&tag_data
, slot
);
173 /* Build a new entry. */
175 entry
= mandoc_malloc(sizeof(*entry
) + len
+ 1);
176 memcpy(entry
->s
, s
, len
);
177 entry
->s
[len
] = '\0';
179 entry
->maxlines
= entry
->nlines
= 0;
180 ohash_insert(&tag_data
, slot
, entry
);
185 * Lower priority numbers take precedence,
187 * A tag with priority 0 is only used
188 * if the tag occurs exactly once.
192 if (entry
->prio
== 0)
197 /* A better entry is already present, ignore the new one. */
199 if (entry
->prio
> 0 && entry
->prio
< prio
)
202 /* The existing entry is worse, clear it. */
204 if (entry
->prio
< 1 || entry
->prio
> prio
)
208 /* Remember the new line. */
210 if (entry
->maxlines
== entry
->nlines
) {
211 entry
->maxlines
+= 4;
212 entry
->lines
= mandoc_reallocarray(entry
->lines
,
213 entry
->maxlines
, sizeof(*entry
->lines
));
215 entry
->lines
[entry
->nlines
++] = line
;
220 * Write out the tags file using the previously collected
221 * information and clear the ohash table while going along.
227 struct tag_entry
*entry
;
232 if (tag_files
.tfd
<= 0)
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
;
239 if ((stream
= fdopen(tag_files
.tfd
, "w")) == NULL
)
240 mandoc_msg(MANDOCERR_FDOPEN
, 0, 0, "%s", strerror(errno
));
242 entry
= ohash_first(&tag_data
, &slot
);
243 while (entry
!= NULL
) {
244 if (stream
!= NULL
&& entry
->prio
>= 0) {
245 for (i
= 0; i
< entry
->nlines
; i
++) {
246 fprintf(stream
, "%s %s %zu\n",
247 entry
->s
, tag_files
.ofn
, entry
->lines
[i
]);
253 entry
= ohash_next(&tag_data
, &slot
);
255 ohash_delete(&tag_data
);
259 close(tag_files
.tfd
);
262 unlink(tag_files
.tfn
);
263 *tag_files
.tfn
= '\0';
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
);
279 if (*tag_files
.ofn
!= '\0')
280 unlink(tag_files
.ofn
);
281 if (*tag_files
.tfn
!= '\0')
282 unlink(tag_files
.tfn
);
286 tag_signal(int signum
)
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
);