]>
git.cameronkatri.com Git - mandoc.git/blob - tag.c
1 /* $Id: tag.c,v 1.12 2016/07/08 20:42:15 schwarze Exp $ */
3 * Copyright (c) 2015 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>
29 #include "mandoc_aux.h"
30 #include "mandoc_ohash.h"
39 static void tag_signal(int);
41 static struct ohash tag_data
;
42 static struct tag_files tag_files
;
46 * Prepare for using a pager.
47 * Not all pagers are capable of using a tag file,
48 * but for simplicity, create it anyway.
58 tag_files
.tcpgid
= -1;
60 /* Clean up when dying from a signal. */
62 memset(&sa
, 0, sizeof(sa
));
63 sigfillset(&sa
.sa_mask
);
64 sa
.sa_handler
= tag_signal
;
65 sigaction(SIGHUP
, &sa
, NULL
);
66 sigaction(SIGINT
, &sa
, NULL
);
67 sigaction(SIGTERM
, &sa
, NULL
);
70 * POSIX requires that a process calling tcsetpgrp(3)
71 * from the background gets a SIGTTOU signal.
72 * In that case, do not stop.
75 sa
.sa_handler
= SIG_IGN
;
76 sigaction(SIGTTOU
, &sa
, NULL
);
78 /* Save the original standard output for use by the pager. */
80 if ((tag_files
.ofd
= dup(STDOUT_FILENO
)) == -1)
83 /* Create both temporary output files. */
85 (void)strlcpy(tag_files
.ofn
, "/tmp/man.XXXXXXXXXX",
86 sizeof(tag_files
.ofn
));
87 (void)strlcpy(tag_files
.tfn
, "/tmp/man.XXXXXXXXXX",
88 sizeof(tag_files
.tfn
));
89 if ((ofd
= mkstemp(tag_files
.ofn
)) == -1)
91 if ((tag_files
.tfd
= mkstemp(tag_files
.tfn
)) == -1)
93 if (dup2(ofd
, STDOUT_FILENO
) == -1)
98 * Set up the ohash table to collect output line numbers
99 * where various marked-up terms are documented.
102 mandoc_ohash_init(&tag_data
, 4, offsetof(struct tag_entry
, s
));
109 if (tag_files
.ofd
!= -1)
110 close(tag_files
.ofd
);
111 if (tag_files
.tfd
!= -1)
112 close(tag_files
.tfd
);
113 *tag_files
.ofn
= '\0';
114 *tag_files
.tfn
= '\0';
121 * Set the line number where a term is defined,
122 * unless it is already defined at a higher priority.
125 tag_put(const char *s
, int prio
, size_t line
)
127 struct tag_entry
*entry
;
131 if (tag_files
.tfd
<= 0 || strchr(s
, ' ') != NULL
)
133 slot
= ohash_qlookup(&tag_data
, s
);
134 entry
= ohash_find(&tag_data
, slot
);
137 entry
= mandoc_malloc(sizeof(*entry
) + len
);
138 memcpy(entry
->s
, s
, len
);
139 ohash_insert(&tag_data
, slot
, entry
);
140 } else if (entry
->prio
<= prio
)
147 * Write out the tags file using the previously collected
148 * information and clear the ohash table while going along.
154 struct tag_entry
*entry
;
157 if (tag_files
.tfd
<= 0)
159 stream
= fdopen(tag_files
.tfd
, "w");
160 entry
= ohash_first(&tag_data
, &slot
);
161 while (entry
!= NULL
) {
163 fprintf(stream
, "%s %s %zu\n",
164 entry
->s
, tag_files
.ofn
, entry
->line
);
166 entry
= ohash_next(&tag_data
, &slot
);
168 ohash_delete(&tag_data
);
178 if (tag_files
.tcpgid
!= -1) {
179 tc_pgid
= tcgetpgrp(STDIN_FILENO
);
180 if (tc_pgid
== tag_files
.pager_pid
||
181 tc_pgid
== getpgid(0) ||
182 getpgid(tc_pgid
) == -1)
183 (void)tcsetpgrp(STDIN_FILENO
, tag_files
.tcpgid
);
185 if (*tag_files
.ofn
!= '\0')
186 unlink(tag_files
.ofn
);
187 if (*tag_files
.tfn
!= '\0')
188 unlink(tag_files
.tfn
);
192 tag_signal(int signum
)
197 memset(&sa
, 0, sizeof(sa
));
198 sigemptyset(&sa
.sa_mask
);
199 sa
.sa_handler
= SIG_DFL
;
200 sigaction(signum
, &sa
, NULL
);
201 kill(getpid(), signum
);