]>
git.cameronkatri.com Git - mandoc.git/blob - tag.c
1 /* $Id: tag.c,v 1.22 2019/07/10 19:39:01 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.
63 tag_files
.tcpgid
= -1;
65 /* Clean up when dying from a signal. */
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
);
75 * POSIX requires that a process calling tcsetpgrp(3)
76 * from the background gets a SIGTTOU signal.
77 * In that case, do not stop.
80 sa
.sa_handler
= SIG_IGN
;
81 sigaction(SIGTTOU
, &sa
, NULL
);
83 /* Save the original standard output for use by the pager. */
85 if ((tag_files
.ofd
= dup(STDOUT_FILENO
)) == -1) {
86 mandoc_msg(MANDOCERR_DUP
, 0, 0, "%s", strerror(errno
));
90 /* Create both temporary output files. */
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
));
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
));
106 if (dup2(ofd
, STDOUT_FILENO
) == -1) {
107 mandoc_msg(MANDOCERR_DUP
, 0, 0, "%s", strerror(errno
));
113 * Set up the ohash table to collect output line numbers
114 * where various marked-up terms are documented.
117 mandoc_ohash_init(&tag_data
, 4, offsetof(struct tag_entry
, s
));
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';
136 * Set the line number where a term is defined,
137 * unless it is already defined at a lower priority.
140 tag_put(const char *s
, int prio
, size_t line
)
142 struct tag_entry
*entry
;
147 if (tag_files
.tfd
<= 0)
150 if (s
[0] == '\\' && (s
[1] == '&' || s
[1] == 'e'))
154 * Skip whitespace and whatever follows it,
155 * and if there is any, downgrade the priority.
158 len
= strcspn(s
, " \t");
166 slot
= ohash_qlookupi(&tag_data
, s
, &se
);
167 entry
= ohash_find(&tag_data
, slot
);
171 /* Build a new entry. */
173 entry
= mandoc_malloc(sizeof(*entry
) + len
+ 1);
174 memcpy(entry
->s
, s
, len
);
175 entry
->s
[len
] = '\0';
177 entry
->maxlines
= entry
->nlines
= 0;
178 ohash_insert(&tag_data
, slot
, entry
);
183 * Lower priority numbers take precedence,
185 * A tag with priority 0 is only used
186 * if the tag occurs exactly once.
190 if (entry
->prio
== 0)
195 /* A better entry is already present, ignore the new one. */
197 if (entry
->prio
> 0 && entry
->prio
< prio
)
200 /* The existing entry is worse, clear it. */
202 if (entry
->prio
< 1 || entry
->prio
> prio
)
206 /* Remember the new line. */
208 if (entry
->maxlines
== entry
->nlines
) {
209 entry
->maxlines
+= 4;
210 entry
->lines
= mandoc_reallocarray(entry
->lines
,
211 entry
->maxlines
, sizeof(*entry
->lines
));
213 entry
->lines
[entry
->nlines
++] = line
;
218 * Write out the tags file using the previously collected
219 * information and clear the ohash table while going along.
225 struct tag_entry
*entry
;
229 if (tag_files
.tfd
<= 0)
231 if (tag_files
.tagname
!= NULL
&& ohash_find(&tag_data
,
232 ohash_qlookup(&tag_data
, tag_files
.tagname
)) == NULL
) {
233 mandoc_msg(MANDOCERR_TAG
, 0, 0, "%s", tag_files
.tagname
);
234 tag_files
.tagname
= NULL
;
236 if ((stream
= fdopen(tag_files
.tfd
, "w")) == NULL
)
237 mandoc_msg(MANDOCERR_FDOPEN
, 0, 0, "%s", strerror(errno
));
238 entry
= ohash_first(&tag_data
, &slot
);
239 while (entry
!= NULL
) {
240 if (stream
!= NULL
&& entry
->prio
>= 0)
241 for (i
= 0; i
< entry
->nlines
; i
++)
242 fprintf(stream
, "%s %s %zu\n",
243 entry
->s
, tag_files
.ofn
, entry
->lines
[i
]);
246 entry
= ohash_next(&tag_data
, &slot
);
248 ohash_delete(&tag_data
);
252 close(tag_files
.tfd
);
261 if (tag_files
.tcpgid
!= -1) {
262 tc_pgid
= tcgetpgrp(tag_files
.ofd
);
263 if (tc_pgid
== tag_files
.pager_pid
||
264 tc_pgid
== getpgid(0) ||
265 getpgid(tc_pgid
) == -1)
266 (void)tcsetpgrp(tag_files
.ofd
, tag_files
.tcpgid
);
268 if (*tag_files
.ofn
!= '\0')
269 unlink(tag_files
.ofn
);
270 if (*tag_files
.tfn
!= '\0')
271 unlink(tag_files
.tfn
);
275 tag_signal(int signum
)
280 memset(&sa
, 0, sizeof(sa
));
281 sigemptyset(&sa
.sa_mask
);
282 sa
.sa_handler
= SIG_DFL
;
283 sigaction(signum
, &sa
, NULL
);
284 kill(getpid(), signum
);