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