Annotation of mandoc/tag.c, Revision 1.20
1.20 ! schwarze 1: /* $Id: tag.c,v 1.19 2018/02/23 16:47:10 schwarze Exp $ */
1.1 schwarze 2: /*
1.20 ! schwarze 3: * Copyright (c) 2015, 2016, 2018 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.20 ! schwarze 21: #include <limits.h>
1.2 schwarze 22: #include <signal.h>
1.1 schwarze 23: #include <stddef.h>
1.7 schwarze 24: #include <stdint.h>
1.1 schwarze 25: #include <stdio.h>
26: #include <stdlib.h>
27: #include <string.h>
28: #include <unistd.h>
29:
30: #include "mandoc_aux.h"
1.10 schwarze 31: #include "mandoc_ohash.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.1 schwarze 54: tag_init(void)
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.6 schwarze 62:
1.12 schwarze 63: /* Clean up when dying from a signal. */
64:
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);
71:
72: /*
73: * POSIX requires that a process calling tcsetpgrp(3)
74: * from the background gets a SIGTTOU signal.
75: * In that case, do not stop.
76: */
77:
78: sa.sa_handler = SIG_IGN;
79: sigaction(SIGTTOU, &sa, NULL);
80:
1.6 schwarze 81: /* Save the original standard output for use by the pager. */
82:
83: if ((tag_files.ofd = dup(STDOUT_FILENO)) == -1)
84: goto fail;
85:
86: /* Create both temporary output files. */
87:
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)
93: goto fail;
94: if ((tag_files.tfd = mkstemp(tag_files.tfn)) == -1)
95: goto fail;
96: if (dup2(ofd, STDOUT_FILENO) == -1)
97: goto fail;
98: close(ofd);
99:
100: /*
101: * Set up the ohash table to collect output line numbers
102: * where various marked-up terms are documented.
103: */
1.1 schwarze 104:
1.10 schwarze 105: mandoc_ohash_init(&tag_data, 4, offsetof(struct tag_entry, s));
1.8 schwarze 106: return &tag_files;
1.6 schwarze 107:
108: fail:
109: tag_unlink();
110: if (ofd != -1)
111: close(ofd);
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';
118: tag_files.ofd = -1;
119: tag_files.tfd = -1;
1.8 schwarze 120: return NULL;
1.1 schwarze 121: }
122:
123: /*
1.5 schwarze 124: * Set the line number where a term is defined,
1.20 ! schwarze 125: * unless it is already defined at a lower priority.
1.1 schwarze 126: */
127: void
1.5 schwarze 128: tag_put(const char *s, int prio, size_t line)
1.1 schwarze 129: {
130: struct tag_entry *entry;
1.20 ! schwarze 131: const char *se;
1.5 schwarze 132: size_t len;
1.1 schwarze 133: unsigned int slot;
134:
1.15 schwarze 135: if (tag_files.tfd <= 0)
136: return;
1.20 ! schwarze 137:
1.15 schwarze 138: if (s[0] == '\\' && (s[1] == '&' || s[1] == 'e'))
139: s += 2;
1.20 ! schwarze 140:
! 141: /*
! 142: * Skip whitespace and whatever follows it,
! 143: * and if there is any, downgrade the priority.
! 144: */
! 145:
! 146: len = strcspn(s, " \t");
! 147: if (len == 0)
1.1 schwarze 148: return;
1.14 schwarze 149:
1.20 ! schwarze 150: se = s + len;
! 151: if (*se != '\0')
! 152: prio = INT_MAX;
! 153:
! 154: slot = ohash_qlookupi(&tag_data, s, &se);
1.1 schwarze 155: entry = ohash_find(&tag_data, slot);
1.14 schwarze 156:
1.1 schwarze 157: if (entry == NULL) {
1.14 schwarze 158:
159: /* Build a new entry. */
160:
1.20 ! schwarze 161: entry = mandoc_malloc(sizeof(*entry) + len + 1);
1.1 schwarze 162: memcpy(entry->s, s, len);
1.20 ! schwarze 163: entry->s[len] = '\0';
1.14 schwarze 164: entry->lines = NULL;
165: entry->maxlines = entry->nlines = 0;
1.1 schwarze 166: ohash_insert(&tag_data, slot, entry);
1.14 schwarze 167:
168: } else {
169:
1.20 ! schwarze 170: /*
! 171: * Lower priority numbers take precedence,
! 172: * but 0 is special.
! 173: * A tag with priority 0 is only used
! 174: * if the tag occurs exactly once.
! 175: */
1.16 schwarze 176:
177: if (prio == 0) {
178: if (entry->prio == 0)
179: entry->prio = -1;
180: return;
181: }
182:
1.14 schwarze 183: /* A better entry is already present, ignore the new one. */
184:
1.16 schwarze 185: if (entry->prio > 0 && entry->prio < prio)
1.14 schwarze 186: return;
187:
188: /* The existing entry is worse, clear it. */
189:
1.16 schwarze 190: if (entry->prio < 1 || entry->prio > prio)
1.14 schwarze 191: entry->nlines = 0;
192: }
193:
194: /* Remember the new line. */
195:
196: if (entry->maxlines == entry->nlines) {
197: entry->maxlines += 4;
198: entry->lines = mandoc_reallocarray(entry->lines,
199: entry->maxlines, sizeof(*entry->lines));
200: }
201: entry->lines[entry->nlines++] = line;
1.4 schwarze 202: entry->prio = prio;
1.1 schwarze 203: }
204:
205: /*
206: * Write out the tags file using the previously collected
207: * information and clear the ohash table while going along.
208: */
209: void
210: tag_write(void)
211: {
212: FILE *stream;
213: struct tag_entry *entry;
1.14 schwarze 214: size_t i;
1.1 schwarze 215: unsigned int slot;
216:
1.6 schwarze 217: if (tag_files.tfd <= 0)
1.1 schwarze 218: return;
1.6 schwarze 219: stream = fdopen(tag_files.tfd, "w");
1.1 schwarze 220: entry = ohash_first(&tag_data, &slot);
221: while (entry != NULL) {
1.16 schwarze 222: if (stream != NULL && entry->prio >= 0)
1.14 schwarze 223: for (i = 0; i < entry->nlines; i++)
224: fprintf(stream, "%s %s %zu\n",
225: entry->s, tag_files.ofn, entry->lines[i]);
226: free(entry->lines);
1.1 schwarze 227: free(entry);
228: entry = ohash_next(&tag_data, &slot);
229: }
230: ohash_delete(&tag_data);
231: if (stream != NULL)
232: fclose(stream);
1.19 schwarze 233: else
234: close(tag_files.tfd);
235: tag_files.tfd = -1;
1.1 schwarze 236: }
237:
238: void
239: tag_unlink(void)
240: {
1.11 schwarze 241: pid_t tc_pgid;
1.1 schwarze 242:
1.11 schwarze 243: if (tag_files.tcpgid != -1) {
1.17 schwarze 244: tc_pgid = tcgetpgrp(tag_files.ofd);
1.11 schwarze 245: if (tc_pgid == tag_files.pager_pid ||
246: tc_pgid == getpgid(0) ||
247: getpgid(tc_pgid) == -1)
1.17 schwarze 248: (void)tcsetpgrp(tag_files.ofd, tag_files.tcpgid);
1.11 schwarze 249: }
1.6 schwarze 250: if (*tag_files.ofn != '\0')
251: unlink(tag_files.ofn);
252: if (*tag_files.tfn != '\0')
253: unlink(tag_files.tfn);
1.2 schwarze 254: }
255:
256: static void
257: tag_signal(int signum)
258: {
1.11 schwarze 259: struct sigaction sa;
1.2 schwarze 260:
261: tag_unlink();
1.11 schwarze 262: memset(&sa, 0, sizeof(sa));
263: sigemptyset(&sa.sa_mask);
264: sa.sa_handler = SIG_DFL;
265: sigaction(signum, &sa, NULL);
1.2 schwarze 266: kill(getpid(), signum);
267: /* NOTREACHED */
268: _exit(1);
1.1 schwarze 269: }
CVSweb