summaryrefslogtreecommitdiff
path: root/backend/dvi/mdvi-lib/special.c
blob: 64a1647becc52d6ce746ffc1e64f77709c1db897 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
 * Copyright (C) 2000, Matias Atria
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <config.h>
#include <ctype.h>
#include <string.h>

#include "mdvi.h"
#include "private.h"

#if defined(WITH_REGEX_SPECIALS) && defined(HAVE_REGEX_H)
#include <regex.h>
#endif

typedef struct _DviSpecial {
	struct _DviSpecial *next;
	struct _DviSpecial *prev;
	char	*label;
	char	*prefix;
	size_t	plen;
#ifdef WITH_REGEX_SPECIALS
	regex_t	reg;
	int	has_reg;
#endif
	DviSpecialHandler handler;
} DviSpecial;

static ListHead specials = {NULL, NULL, 0};

#define SPECIAL(x)	\
	void x __PROTO((DviContext *, const char *, const char *))

static SPECIAL(sp_layer);
extern SPECIAL(epsf_special);
extern SPECIAL(do_color_special);

static struct {
	char	*label;
	char	*prefix;
	char	*regex;
	DviSpecialHandler handler;
} builtins[] = {
	{"Layers", "layer", NULL, sp_layer},
	{"EPSF", "psfile", NULL, epsf_special}
};
#define NSPECIALS	(sizeof(builtins) / sizeof(builtins[0]))
static int registered_builtins = 0;

static void register_builtin_specials(void)
{
	int	i;

	ASSERT(registered_builtins == 0);
	registered_builtins = 1;
	for(i = 0; i < NSPECIALS; i++)
		mdvi_register_special(
			builtins[i].label,
			builtins[i].prefix,
			builtins[i].regex,
			builtins[i].handler,
			1 /* replace if exists */);
}

static DviSpecial *find_special_prefix(const char *prefix)
{
	DviSpecial *sp;

	/* should have a hash table here, but I'm so lazy */
	for(sp = (DviSpecial *)specials.head; sp; sp = sp->next) {
		if(STRCEQ(sp->prefix, prefix))
			break;
	}
	return sp;
}

int	mdvi_register_special(const char *label, const char *prefix,
	const char *regex, DviSpecialHandler handler, int replace)
{
	DviSpecial *sp;
	int	newsp = 0;

	if(!registered_builtins)
		register_builtin_specials();

	sp = find_special_prefix(prefix);
	if(sp == NULL) {
		sp = xalloc(DviSpecial);
		sp->prefix = mdvi_strdup(prefix);
		newsp = 1;
	} else if(!replace)
		return -1;
	else {
		mdvi_free(sp->label);
		sp->label = NULL;
	}

#ifdef WITH_REGEX_SPECIALS
	if(!newsp && sp->has_reg) {
		regfree(&sp->reg);
		sp->has_reg = 0;
	}
	if(regex && regcomp(&sp->reg, regex, REG_NOSUB) != 0) {
		if(newsp) {
			mdvi_free(sp->prefix);
			mdvi_free(sp);
		}
		return -1;
	}
	sp->has_reg = (regex != NULL);
#endif
	sp->handler = handler;
	sp->label = mdvi_strdup(label);
	sp->plen = strlen(prefix);
	if(newsp)
		listh_prepend(&specials, LIST(sp));
	DEBUG((DBG_SPECIAL,
		"New \\special handler `%s' with prefix `%s'\n",
		label, prefix));
	return 0;
}

int	mdvi_unregister_special(const char *prefix)
{
	DviSpecial *sp;

	sp = find_special_prefix(prefix);
	if(sp == NULL)
		return -1;
	mdvi_free(sp->prefix);
#ifdef WITH_REGEX_SPECIALS
	if(sp->has_reg)
		regfree(&sp->reg);
#endif
	listh_remove(&specials, LIST(sp));
	mdvi_free(sp);
	return 0;
}

int	mdvi_do_special(DviContext *dvi, char *string)
{
	char	*prefix;
	char 	*ptr;
	DviSpecial *sp;

	if(!registered_builtins) {
	}

	if(!string || !*string)
		return 0;

	/* skip leading spaces */
	while(*string && isspace(*string))
		string++;

	DEBUG((DBG_SPECIAL, "Looking for a handler for `%s'\n", string));

	/* now try to find a match */
	ptr = string;
	for(sp = (DviSpecial *)specials.head; sp; sp = sp->next) {
#ifdef WITH_REGEX_SPECIALS
		if(sp->has_reg && !regexec(&sp->reg, ptr, 0, 0, 0))
			break;
#endif
		/* check the prefix */
		if(STRNCEQ(sp->prefix, ptr, sp->plen)) {
			ptr += sp->plen;
			break;
		}
	}

	if(sp == NULL) {
		DEBUG((DBG_SPECIAL, "None found\n"));
		return -1;
	}

	/* extract the prefix */
	if(ptr == string) {
		prefix = NULL;
		DEBUG((DBG_SPECIAL,
			"REGEX match with `%s' (arg `%s')\n",
			sp->label, ptr));
	} else {
		if(*ptr) *ptr++ = 0;
		prefix = string;
		DEBUG((DBG_SPECIAL,
			"PREFIX match with `%s' (prefix `%s', arg `%s')\n",
			sp->label, prefix, ptr));
	}

	/* invoke the handler */
	sp->handler(dvi, prefix, ptr);

	return 0;
}

void	mdvi_flush_specials(void)
{
	DviSpecial *sp, *list;


	for(list = (DviSpecial *)specials.head; (sp = list); ) {
		list = sp->next;
		if(sp->prefix) mdvi_free(sp->prefix);
		if(sp->label) mdvi_free(sp->label);
#ifdef WITH_REGEX_SPECIALS
		if(sp->has_reg)
			regfree(&sp->reg);
#endif
		mdvi_free(sp);
	}
	specials.head = NULL;
	specials.tail = NULL;
	specials.count = 0;
}

/* some builtin specials */

void	sp_layer(DviContext *dvi, const char *prefix, const char *arg)
{
	if(STREQ("push", arg))
		dvi->curr_layer++;
	else if(STREQ("pop", arg)) {
		if(dvi->curr_layer)
			dvi->curr_layer--;
		else
			mdvi_warning(_("%s: tried to pop top level layer\n"),
				     dvi->filename);
	} else if(STREQ("reset", arg))
		dvi->curr_layer = 0;
	DEBUG((DBG_SPECIAL, "Layer level: %d\n", dvi->curr_layer));
}