]>
git.cameronkatri.com Git - mandoc.git/blob - tag.c
1 /* $Id: tag.c,v 1.20 2018/10/23 20:42:37 schwarze Exp $ */
3 * Copyright (c) 2015, 2016, 2018 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>
30 #include "mandoc_aux.h"
31 #include "mandoc_ohash.h"
42 static void tag_signal(int) __attribute__((__noreturn__
));
44 static struct ohash tag_data
;
45 static struct tag_files tag_files
;
49 * Prepare for using a pager.
50 * Not all pagers are capable of using a tag file,
51 * but for simplicity, create it anyway.
61 tag_files
.tcpgid
= -1;
63 /* Clean up when dying from a signal. */
65 memset(&sa
, 0, sizeof(sa
));
66 sigfillset(&sa
.sa_mask
);
67 sa
.sa_handler
= tag_signal
;
68 sigaction(SIGHUP
, &sa
, NULL
);
69 sigaction(SIGINT
, &sa
, NULL
);
70 sigaction(SIGTERM
, &sa
, NULL
);
73 * POSIX requires that a process calling tcsetpgrp(3)
74 * from the background gets a SIGTTOU signal.
75 * In that case, do not stop.
78 sa
.sa_handler
= SIG_IGN
;
79 sigaction(SIGTTOU
, &sa
, NULL
);
81 /* Save the original standard output for use by the pager. */
83 if ((tag_files
.ofd
= dup(STDOUT_FILENO
)) == -1)
86 /* Create both temporary output files. */
88 (void)strlcpy(tag_files
.ofn
, "/tmp/man.XXXXXXXXXX",
89 sizeof(tag_files
.ofn
));
90 (void)strlcpy(tag_files
.tfn
, "/tmp/man.XXXXXXXXXX",
91 sizeof(tag_files
.tfn
));
92 if ((ofd
= mkstemp(tag_files
.ofn
)) == -1)
94 if ((tag_files
.tfd
= mkstemp(tag_files
.tfn
)) == -1)
96 if (dup2(ofd
, STDOUT_FILENO
) == -1)
101 * Set up the ohash table to collect output line numbers
102 * where various marked-up terms are documented.
105 mandoc_ohash_init(&tag_data
, 4, offsetof(struct tag_entry
, s
));
112 if (tag_files
.ofd
!= -1)
113 close(tag_files
.ofd
);
114 if (tag_files
.tfd
!= -1)
115 close(tag_files
.tfd
);
116 *tag_files
.ofn
= '\0';
117 *tag_files
.tfn
= '\0';
124 * Set the line number where a term is defined,
125 * unless it is already defined at a lower priority.
128 tag_put(const char *s
, int prio
, size_t line
)
130 struct tag_entry
*entry
;
135 if (tag_files
.tfd
<= 0)
138 if (s
[0] == '\\' && (s
[1] == '&' || s
[1] == 'e'))
142 * Skip whitespace and whatever follows it,
143 * and if there is any, downgrade the priority.
146 len
= strcspn(s
, " \t");
154 slot
= ohash_qlookupi(&tag_data
, s
, &se
);
155 entry
= ohash_find(&tag_data
, slot
);
159 /* Build a new entry. */
161 entry
= mandoc_malloc(sizeof(*entry
) + len
+ 1);
162 memcpy(entry
->s
, s
, len
);
163 entry
->s
[len
] = '\0';
165 entry
->maxlines
= entry
->nlines
= 0;
166 ohash_insert(&tag_data
, slot
, entry
);
171 * Lower priority numbers take precedence,
173 * A tag with priority 0 is only used
174 * if the tag occurs exactly once.
178 if (entry
->prio
== 0)
183 /* A better entry is already present, ignore the new one. */
185 if (entry
->prio
> 0 && entry
->prio
< prio
)
188 /* The existing entry is worse, clear it. */
190 if (entry
->prio
< 1 || entry
->prio
> prio
)
194 /* Remember the new line. */
196 if (entry
->maxlines
== entry
->nlines
) {
197 entry
->maxlines
+= 4;
198 entry
->lines
= mandoc_reallocarray(entry
->lines
,
199 entry
->maxlines
, sizeof(*entry
->lines
));
201 entry
->lines
[entry
->nlines
++] = line
;
206 * Write out the tags file using the previously collected
207 * information and clear the ohash table while going along.
213 struct tag_entry
*entry
;
217 if (tag_files
.tfd
<= 0)
219 stream
= fdopen(tag_files
.tfd
, "w");
220 entry
= ohash_first(&tag_data
, &slot
);
221 while (entry
!= NULL
) {
222 if (stream
!= NULL
&& entry
->prio
>= 0)
223 for (i
= 0; i
< entry
->nlines
; i
++)
224 fprintf(stream
, "%s %s %zu\n",
225 entry
->s
, tag_files
.ofn
, entry
->lines
[i
]);
228 entry
= ohash_next(&tag_data
, &slot
);
230 ohash_delete(&tag_data
);
234 close(tag_files
.tfd
);
243 if (tag_files
.tcpgid
!= -1) {
244 tc_pgid
= tcgetpgrp(tag_files
.ofd
);
245 if (tc_pgid
== tag_files
.pager_pid
||
246 tc_pgid
== getpgid(0) ||
247 getpgid(tc_pgid
) == -1)
248 (void)tcsetpgrp(tag_files
.ofd
, tag_files
.tcpgid
);
250 if (*tag_files
.ofn
!= '\0')
251 unlink(tag_files
.ofn
);
252 if (*tag_files
.tfn
!= '\0')
253 unlink(tag_files
.tfn
);
257 tag_signal(int signum
)
262 memset(&sa
, 0, sizeof(sa
));
263 sigemptyset(&sa
.sa_mask
);
264 sa
.sa_handler
= SIG_DFL
;
265 sigaction(signum
, &sa
, NULL
);
266 kill(getpid(), signum
);