Annotation of src/usr.bin/mandoc/tag.c, Revision 1.26
1.26 ! schwarze 1: /* $OpenBSD: tag.c,v 1.25 2019/07/27 13:40:42 schwarze Exp $ */
1.1 schwarze 2: /*
1.26 ! schwarze 3: * Copyright (c) 2015,2016,2018,2019,2020 Ingo Schwarze <schwarze@openbsd.org>
1.1 schwarze 4: *
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.
8: *
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.
16: */
17: #include <sys/types.h>
18:
1.22 schwarze 19: #include <errno.h>
1.20 schwarze 20: #include <limits.h>
1.2 schwarze 21: #include <signal.h>
1.1 schwarze 22: #include <stddef.h>
1.7 schwarze 23: #include <stdint.h>
1.1 schwarze 24: #include <stdio.h>
25: #include <stdlib.h>
26: #include <string.h>
27: #include <unistd.h>
28:
29: #include "mandoc_aux.h"
1.10 schwarze 30: #include "mandoc_ohash.h"
1.22 schwarze 31: #include "mandoc.h"
1.1 schwarze 32: #include "tag.h"
33:
34: struct tag_entry {
1.14 schwarze 35: size_t *lines;
36: size_t maxlines;
37: size_t nlines;
1.4 schwarze 38: int prio;
1.1 schwarze 39: char s[];
40: };
41:
1.18 schwarze 42: static void tag_signal(int) __attribute__((__noreturn__));
1.1 schwarze 43:
44: static struct ohash tag_data;
1.6 schwarze 45: static struct tag_files tag_files;
1.1 schwarze 46:
47:
48: /*
1.6 schwarze 49: * Prepare for using a pager.
50: * Not all pagers are capable of using a tag file,
51: * but for simplicity, create it anyway.
1.1 schwarze 52: */
1.6 schwarze 53: struct tag_files *
1.25 schwarze 54: tag_init(char *tagname)
1.1 schwarze 55: {
1.11 schwarze 56: struct sigaction sa;
1.6 schwarze 57: int ofd;
1.1 schwarze 58:
1.6 schwarze 59: ofd = -1;
60: tag_files.tfd = -1;
1.11 schwarze 61: tag_files.tcpgid = -1;
1.25 schwarze 62: tag_files.tagname = tagname;
1.6 schwarze 63:
1.12 schwarze 64: /* Clean up when dying from a signal. */
65:
66: memset(&sa, 0, sizeof(sa));
67: sigfillset(&sa.sa_mask);
68: sa.sa_handler = tag_signal;
69: sigaction(SIGHUP, &sa, NULL);
70: sigaction(SIGINT, &sa, NULL);
71: sigaction(SIGTERM, &sa, NULL);
72:
73: /*
74: * POSIX requires that a process calling tcsetpgrp(3)
75: * from the background gets a SIGTTOU signal.
76: * In that case, do not stop.
77: */
78:
79: sa.sa_handler = SIG_IGN;
80: sigaction(SIGTTOU, &sa, NULL);
81:
1.6 schwarze 82: /* Save the original standard output for use by the pager. */
83:
1.22 schwarze 84: if ((tag_files.ofd = dup(STDOUT_FILENO)) == -1) {
85: mandoc_msg(MANDOCERR_DUP, 0, 0, "%s", strerror(errno));
1.6 schwarze 86: goto fail;
1.22 schwarze 87: }
1.6 schwarze 88:
89: /* Create both temporary output files. */
90:
91: (void)strlcpy(tag_files.ofn, "/tmp/man.XXXXXXXXXX",
92: sizeof(tag_files.ofn));
93: (void)strlcpy(tag_files.tfn, "/tmp/man.XXXXXXXXXX",
94: sizeof(tag_files.tfn));
1.22 schwarze 95: if ((ofd = mkstemp(tag_files.ofn)) == -1) {
96: mandoc_msg(MANDOCERR_MKSTEMP, 0, 0,
97: "%s: %s", tag_files.ofn, strerror(errno));
1.6 schwarze 98: goto fail;
1.22 schwarze 99: }
100: if ((tag_files.tfd = mkstemp(tag_files.tfn)) == -1) {
101: mandoc_msg(MANDOCERR_MKSTEMP, 0, 0,
102: "%s: %s", tag_files.tfn, strerror(errno));
1.6 schwarze 103: goto fail;
1.22 schwarze 104: }
105: if (dup2(ofd, STDOUT_FILENO) == -1) {
106: mandoc_msg(MANDOCERR_DUP, 0, 0, "%s", strerror(errno));
1.6 schwarze 107: goto fail;
1.22 schwarze 108: }
1.6 schwarze 109: close(ofd);
110:
111: /*
112: * Set up the ohash table to collect output line numbers
113: * where various marked-up terms are documented.
114: */
1.1 schwarze 115:
1.10 schwarze 116: mandoc_ohash_init(&tag_data, 4, offsetof(struct tag_entry, s));
1.8 schwarze 117: return &tag_files;
1.6 schwarze 118:
119: fail:
120: tag_unlink();
121: if (ofd != -1)
122: close(ofd);
123: if (tag_files.ofd != -1)
124: close(tag_files.ofd);
125: if (tag_files.tfd != -1)
126: close(tag_files.tfd);
127: *tag_files.ofn = '\0';
128: *tag_files.tfn = '\0';
129: tag_files.ofd = -1;
130: tag_files.tfd = -1;
1.25 schwarze 131: tag_files.tagname = NULL;
1.8 schwarze 132: return NULL;
1.1 schwarze 133: }
134:
135: /*
1.5 schwarze 136: * Set the line number where a term is defined,
1.20 schwarze 137: * unless it is already defined at a lower priority.
1.1 schwarze 138: */
139: void
1.5 schwarze 140: tag_put(const char *s, int prio, size_t line)
1.1 schwarze 141: {
142: struct tag_entry *entry;
1.20 schwarze 143: const char *se;
1.5 schwarze 144: size_t len;
1.1 schwarze 145: unsigned int slot;
146:
1.15 schwarze 147: if (tag_files.tfd <= 0)
148: return;
1.20 schwarze 149:
1.15 schwarze 150: if (s[0] == '\\' && (s[1] == '&' || s[1] == 'e'))
151: s += 2;
1.20 schwarze 152:
153: /*
1.24 schwarze 154: * Skip whitespace and escapes and whatever follows,
1.20 schwarze 155: * and if there is any, downgrade the priority.
156: */
157:
1.24 schwarze 158: len = strcspn(s, " \t\\");
1.20 schwarze 159: if (len == 0)
1.1 schwarze 160: return;
1.14 schwarze 161:
1.20 schwarze 162: se = s + len;
163: if (*se != '\0')
164: prio = INT_MAX;
165:
166: slot = ohash_qlookupi(&tag_data, s, &se);
1.1 schwarze 167: entry = ohash_find(&tag_data, slot);
1.14 schwarze 168:
1.1 schwarze 169: if (entry == NULL) {
1.14 schwarze 170:
171: /* Build a new entry. */
172:
1.20 schwarze 173: entry = mandoc_malloc(sizeof(*entry) + len + 1);
1.1 schwarze 174: memcpy(entry->s, s, len);
1.20 schwarze 175: entry->s[len] = '\0';
1.14 schwarze 176: entry->lines = NULL;
177: entry->maxlines = entry->nlines = 0;
1.1 schwarze 178: ohash_insert(&tag_data, slot, entry);
1.14 schwarze 179:
180: } else {
181:
1.20 schwarze 182: /*
183: * Lower priority numbers take precedence,
184: * but 0 is special.
185: * A tag with priority 0 is only used
186: * if the tag occurs exactly once.
187: */
1.16 schwarze 188:
189: if (prio == 0) {
190: if (entry->prio == 0)
191: entry->prio = -1;
192: return;
193: }
194:
1.14 schwarze 195: /* A better entry is already present, ignore the new one. */
196:
1.26 ! schwarze 197: if (entry->prio != -1 && entry->prio < prio)
1.14 schwarze 198: return;
199:
200: /* The existing entry is worse, clear it. */
201:
1.26 ! schwarze 202: if (entry->prio == -1 || entry->prio == 0 ||
! 203: entry->prio > prio)
1.14 schwarze 204: entry->nlines = 0;
205: }
206:
207: /* Remember the new line. */
208:
209: if (entry->maxlines == entry->nlines) {
210: entry->maxlines += 4;
211: entry->lines = mandoc_reallocarray(entry->lines,
212: entry->maxlines, sizeof(*entry->lines));
213: }
214: entry->lines[entry->nlines++] = line;
1.4 schwarze 215: entry->prio = prio;
1.1 schwarze 216: }
217:
218: /*
219: * Write out the tags file using the previously collected
220: * information and clear the ohash table while going along.
221: */
222: void
223: tag_write(void)
224: {
225: FILE *stream;
226: struct tag_entry *entry;
1.14 schwarze 227: size_t i;
1.1 schwarze 228: unsigned int slot;
1.23 schwarze 229: int empty;
1.1 schwarze 230:
1.6 schwarze 231: if (tag_files.tfd <= 0)
1.1 schwarze 232: return;
1.21 schwarze 233: if (tag_files.tagname != NULL && ohash_find(&tag_data,
234: ohash_qlookup(&tag_data, tag_files.tagname)) == NULL) {
1.22 schwarze 235: mandoc_msg(MANDOCERR_TAG, 0, 0, "%s", tag_files.tagname);
1.21 schwarze 236: tag_files.tagname = NULL;
237: }
1.22 schwarze 238: if ((stream = fdopen(tag_files.tfd, "w")) == NULL)
239: mandoc_msg(MANDOCERR_FDOPEN, 0, 0, "%s", strerror(errno));
1.23 schwarze 240: empty = 1;
1.1 schwarze 241: entry = ohash_first(&tag_data, &slot);
242: while (entry != NULL) {
1.26 ! schwarze 243: if (stream != NULL && entry->prio != -1) {
1.23 schwarze 244: for (i = 0; i < entry->nlines; i++) {
1.14 schwarze 245: fprintf(stream, "%s %s %zu\n",
246: entry->s, tag_files.ofn, entry->lines[i]);
1.23 schwarze 247: empty = 0;
248: }
249: }
1.14 schwarze 250: free(entry->lines);
1.1 schwarze 251: free(entry);
252: entry = ohash_next(&tag_data, &slot);
253: }
254: ohash_delete(&tag_data);
255: if (stream != NULL)
256: fclose(stream);
1.19 tb 257: else
258: close(tag_files.tfd);
259: tag_files.tfd = -1;
1.23 schwarze 260: if (empty) {
261: unlink(tag_files.tfn);
262: *tag_files.tfn = '\0';
263: }
1.1 schwarze 264: }
265:
266: void
267: tag_unlink(void)
268: {
1.11 schwarze 269: pid_t tc_pgid;
1.1 schwarze 270:
1.11 schwarze 271: if (tag_files.tcpgid != -1) {
1.17 schwarze 272: tc_pgid = tcgetpgrp(tag_files.ofd);
1.11 schwarze 273: if (tc_pgid == tag_files.pager_pid ||
274: tc_pgid == getpgid(0) ||
275: getpgid(tc_pgid) == -1)
1.17 schwarze 276: (void)tcsetpgrp(tag_files.ofd, tag_files.tcpgid);
1.11 schwarze 277: }
1.6 schwarze 278: if (*tag_files.ofn != '\0')
279: unlink(tag_files.ofn);
280: if (*tag_files.tfn != '\0')
281: unlink(tag_files.tfn);
1.2 schwarze 282: }
283:
284: static void
285: tag_signal(int signum)
286: {
1.11 schwarze 287: struct sigaction sa;
1.2 schwarze 288:
289: tag_unlink();
1.11 schwarze 290: memset(&sa, 0, sizeof(sa));
291: sigemptyset(&sa.sa_mask);
292: sa.sa_handler = SIG_DFL;
293: sigaction(signum, &sa, NULL);
1.2 schwarze 294: kill(getpid(), signum);
295: /* NOTREACHED */
296: _exit(1);
1.1 schwarze 297: }