summaryrefslogtreecommitdiff
path: root/db/doc.c
blob: e06983d2ae10ac22b6ccd53d8854a88d167c7084 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * db/doc.c - Documentation database
 *
 * Written 2016 by Werner Almesberger
 * Copyright 2016 by Werner Almesberger
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#include "misc/util.h"
#include "misc/diag.h"
#include "file/file.h"
#include "db/doc.h"


static struct doc {
	const char *ref;	/* component reference */
	const char *tag;	/* document type (shown in pop-up) */
	const char *s;		/* string to pass to viewer */
	struct doc *next;
} *docs = NULL, *curr_doc = NULL;


/* ----- Lookup ------------------------------------------------------------ */


bool doc_find(const char *ref,
    void (*fn)(void *user, const char *tag, const char *s), void *user)
{
	const struct doc *d;
	bool found = 0;
	for (d = docs; d; d = d->next)
		if (!strcmp(d->ref, ref)) {
			fn(user, d->tag, d->s);
			found = 1;
		}
	return found;
}


/* ----- Parsing ----------------------------------------------------------- */


static const char *untail(const char *s)
{
	const char *start = s;
	const char *last = s;
	char *new;

	while (*s) {
		if (!isspace(*s))
			last = s;
		s++;
	}
	new = alloc_size(last - start + 2);
	memcpy(new, start, last - start + 1);
	new[last - start + 1] = 0;
	return new;
}


static bool parse_line(const struct file *file,
    void *user, const char *line)
{
	bool *start = user;
	const char *s = line;
	struct doc *new;

	while (1) {
		switch (*s) {
		case 0:			/* empty line(s) separate(s) records */
			*start = 1;
			return 1;
		case '\r':		/* ignore leading whitespace */
		case ' ':
		case '\t':
			s++;
			continue;
		case '#':		/* ignore comment-only line */
			return 1;
		default:
			break;
			
		}
		break;
	}

	if (*start) {
		new = alloc_type(struct doc);
		new->ref = new->tag = new->s = NULL;
		new->next = NULL;
		if (curr_doc)
			curr_doc->next = new;
		else
			docs = new;
		curr_doc = new;
		*start = 0;
	}
	if (curr_doc->s)
		warning("ignoring extra line for \"%s\"", curr_doc->ref);
	else if (curr_doc->tag)
		curr_doc->s = untail(line);
	else if (curr_doc->ref)
		curr_doc->tag = untail(line);
	else
		curr_doc->ref = untail(line);
	return 1;
}


bool doc_load(const char *name, const struct file *related)
{
	struct file file;
	bool start = 1;

	if (!file_open(&file, name, related))
		return 0;
	file_read(&file, parse_line, &start);
	file_close(&file);
	return 1;
}


/* ----- Cleanup ----------------------------------------------------------- */


void doc_free(void)
{
	struct doc *next;

	while (docs) {
		next = docs->next;
		free((void *) docs->ref);
		free((void *) docs->tag);
		free((void *) docs->s);
		free(docs);
		docs = next;
	}
	curr_doc = NULL;
}