]>
git.cameronkatri.com Git - mandoc.git/blob - tag.c
1 /* $Id: tag.c,v 1.5 2015/07/25 14:28:59 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.
17 #include <sys/types.h>
29 #include "compat_ohash.h"
32 #include "mandoc_aux.h"
41 static void tag_signal(int);
42 static void *tag_alloc(size_t, void *);
43 static void tag_free(void *, void *);
44 static void *tag_calloc(size_t, size_t, void *);
46 static struct ohash tag_data
;
47 static char *tag_fn
= NULL
;
48 static int tag_fd
= -1;
52 * Set up the ohash table to collect output line numbers
53 * where various marked-up terms are documented and create
54 * the temporary tags file, saving the name for the pager.
59 struct ohash_info tag_info
;
61 tag_fn
= mandoc_strdup("/tmp/man.XXXXXXXXXX");
62 signal(SIGHUP
, tag_signal
);
63 signal(SIGINT
, tag_signal
);
64 signal(SIGTERM
, tag_signal
);
65 if ((tag_fd
= mkstemp(tag_fn
)) == -1) {
71 tag_info
.alloc
= tag_alloc
;
72 tag_info
.calloc
= tag_calloc
;
73 tag_info
.free
= tag_free
;
74 tag_info
.key_offset
= offsetof(struct tag_entry
, s
);
76 ohash_init(&tag_data
, 4, &tag_info
);
81 * Set the line number where a term is defined,
82 * unless it is already defined at a higher priority.
85 tag_put(const char *s
, int prio
, size_t line
)
87 struct tag_entry
*entry
;
93 slot
= ohash_qlookup(&tag_data
, s
);
94 entry
= ohash_find(&tag_data
, slot
);
97 entry
= mandoc_malloc(sizeof(*entry
) + len
);
98 memcpy(entry
->s
, s
, len
);
99 ohash_insert(&tag_data
, slot
, entry
);
100 } else if (entry
->prio
<= prio
)
107 * Write out the tags file using the previously collected
108 * information and clear the ohash table while going along.
114 struct tag_entry
*entry
;
119 stream
= fdopen(tag_fd
, "w");
120 entry
= ohash_first(&tag_data
, &slot
);
121 while (entry
!= NULL
) {
123 fprintf(stream
, "%s - %zu\n", entry
->s
, entry
->line
);
125 entry
= ohash_next(&tag_data
, &slot
);
127 ohash_delete(&tag_data
);
141 tag_signal(int signum
)
145 signal(signum
, SIG_DFL
);
146 kill(getpid(), signum
);
152 * Memory management callback functions for ohash.
155 tag_alloc(size_t sz
, void *arg
)
158 return(mandoc_malloc(sz
));
162 tag_calloc(size_t nmemb
, size_t sz
, void *arg
)
165 return(mandoc_calloc(nmemb
, sz
));
169 tag_free(void *p
, void *arg
)