Annotation of mandoc/tag.c, Revision 1.1
1.1 ! schwarze 1: /* $Id$ */
! 2: /*
! 3: * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
! 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:
! 19: #include <stddef.h>
! 20: #include <stdio.h>
! 21: #include <stdlib.h>
! 22: #include <string.h>
! 23: #include <unistd.h>
! 24:
! 25: #if HAVE_OHASH
! 26: #include <ohash.h>
! 27: #else
! 28: #include "compat_ohash.h"
! 29: #endif
! 30:
! 31: #include "mandoc_aux.h"
! 32: #include "tag.h"
! 33:
! 34: struct tag_entry {
! 35: size_t line;
! 36: char s[];
! 37: };
! 38:
! 39: static void *tag_alloc(size_t, void *);
! 40: static void tag_free(void *, void *);
! 41: static void *tag_calloc(size_t, size_t, void *);
! 42:
! 43: static struct ohash tag_data;
! 44: static char *tag_fn = NULL;
! 45: static int tag_fd = -1;
! 46:
! 47:
! 48: /*
! 49: * Set up the ohash table to collect output line numbers
! 50: * where various marked-up terms are documented and create
! 51: * the temporary tags file, saving the name for the pager.
! 52: */
! 53: void
! 54: tag_init(void)
! 55: {
! 56: struct ohash_info tag_info;
! 57:
! 58: tag_fn = mandoc_strdup("/tmp/man.XXXXXXXXXX");
! 59: if ((tag_fd = mkstemp(tag_fn)) == -1) {
! 60: free(tag_fn);
! 61: tag_fn = NULL;
! 62: return;
! 63: }
! 64:
! 65: tag_info.alloc = tag_alloc;
! 66: tag_info.calloc = tag_calloc;
! 67: tag_info.free = tag_free;
! 68: tag_info.key_offset = offsetof(struct tag_entry, s);
! 69: tag_info.data = NULL;
! 70: ohash_init(&tag_data, 4, &tag_info);
! 71: }
! 72:
! 73: char *
! 74: tag_filename(void)
! 75: {
! 76:
! 77: return(tag_fn);
! 78: }
! 79:
! 80: /*
! 81: * Return the line number where a term is defined,
! 82: * or 0 if the term is unknown.
! 83: */
! 84: size_t
! 85: tag_get(const char *s, size_t len)
! 86: {
! 87: struct tag_entry *entry;
! 88: const char *end;
! 89: unsigned int slot;
! 90:
! 91: if (tag_fd == -1)
! 92: return(0);
! 93: if (len == 0)
! 94: len = strlen(s);
! 95: end = s + len;
! 96: slot = ohash_qlookupi(&tag_data, s, &end);
! 97: entry = ohash_find(&tag_data, slot);
! 98: return(entry == NULL ? 0 : entry->line);
! 99: }
! 100:
! 101: /*
! 102: * Set the line number where a term is defined.
! 103: */
! 104: void
! 105: tag_put(const char *s, size_t len, size_t line)
! 106: {
! 107: struct tag_entry *entry;
! 108: const char *end;
! 109: unsigned int slot;
! 110:
! 111: if (tag_fd == -1)
! 112: return;
! 113: if (len == 0)
! 114: len = strlen(s);
! 115: end = s + len;
! 116: slot = ohash_qlookupi(&tag_data, s, &end);
! 117: entry = ohash_find(&tag_data, slot);
! 118: if (entry == NULL) {
! 119: entry = mandoc_malloc(sizeof(*entry) + len + 1);
! 120: memcpy(entry->s, s, len);
! 121: entry->s[len] = '\0';
! 122: ohash_insert(&tag_data, slot, entry);
! 123: }
! 124: entry->line = line;
! 125: }
! 126:
! 127: /*
! 128: * Write out the tags file using the previously collected
! 129: * information and clear the ohash table while going along.
! 130: */
! 131: void
! 132: tag_write(void)
! 133: {
! 134: FILE *stream;
! 135: struct tag_entry *entry;
! 136: unsigned int slot;
! 137:
! 138: if (tag_fd == -1)
! 139: return;
! 140: stream = fdopen(tag_fd, "w");
! 141: entry = ohash_first(&tag_data, &slot);
! 142: while (entry != NULL) {
! 143: if (stream != NULL)
! 144: fprintf(stream, "%s - %zu\n", entry->s, entry->line);
! 145: free(entry);
! 146: entry = ohash_next(&tag_data, &slot);
! 147: }
! 148: ohash_delete(&tag_data);
! 149: if (stream != NULL)
! 150: fclose(stream);
! 151: }
! 152:
! 153: void
! 154: tag_unlink(void)
! 155: {
! 156:
! 157: if (tag_fn != NULL)
! 158: unlink(tag_fn);
! 159: }
! 160:
! 161: /*
! 162: * Memory management callback functions for ohash.
! 163: */
! 164: static void *
! 165: tag_alloc(size_t sz, void *arg)
! 166: {
! 167:
! 168: return(mandoc_malloc(sz));
! 169: }
! 170:
! 171: static void *
! 172: tag_calloc(size_t nmemb, size_t sz, void *arg)
! 173: {
! 174:
! 175: return(mandoc_calloc(nmemb, sz));
! 176: }
! 177:
! 178: static void
! 179: tag_free(void *p, void *arg)
! 180: {
! 181:
! 182: free(p);
! 183: }
CVSweb