summaryrefslogtreecommitdiffabout
path: root/src/script.c
blob: f77cd287e933781e5897cc5f4b7243a9eea426ec (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
/* This file is part of cfpeek
   Copyright (C) 2011, 2012, 2015 Sergey Poznyakoff

   Cfpeek 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 3, or (at your option)
   any later version.

   Cfpeek 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 cfpeek.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "cfpeek.h"

struct script_tab {
	char *lang;
	char *suf;
	void (*init)(void);
	void (*run)(struct grecs_node *);
	void (*done)(void);
};

static struct script_tab script_tab[] = {
#ifdef GUILE_VERSION_NUMBER
	{ "scheme", "scm\0", guile_init, guile_apply, guile_done },
#endif
	{ NULL }
};

static struct script_tab *cur_script;

static struct script_tab *
script_find_by_lang(const char *lang)
{
	struct script_tab *p;
	for (p = script_tab; p->lang; p++)
		if (strcasecmp(p->lang, lang) == 0)
			return p;
	return NULL;
}

static struct script_tab *
script_find_by_suffix(const char *suf)
{
	struct script_tab *p;
	for (p = script_tab; p->lang; p++) {
		char *s;

		for (s = p->suf; *s; s += strlen (s) + 1)
			if (strcmp (s, suf) == 0)
				return p;
	}
	return NULL;
}

void
script_select(const char *lang)
{
	cur_script = script_find_by_lang(lang);
	if (!cur_script) {
		grecs_error(NULL, 0, "unsupported scripting language: %s",
			    lang);
		exit(EX_USAGE);
	}
}

void
script_init()
{
	if (!(script_file || script_expr))
		return;

	if (!cur_script) {
		if (script_file) {
			char *suf = strrchr(script_file, '.');
			if (suf)
				cur_script = script_find_by_suffix(suf + 1);
		}
		if (!cur_script)
			cur_script = script_tab;
		
		if (!cur_script->lang) {
			grecs_error(NULL, 0,
				    "scripting support not compiled in");
			exit(EX_USAGE);
		}
	}
	
	cur_script->init();
}

int
script_run(struct grecs_node *node)
{
	if (script_file || script_expr) {
		cur_script->run(node);
		return 0;
	}
	return 1;
}
	
void
script_done()
{
	if (cur_script)
		cur_script->done();
}

Return to:

Send suggestions and report system problems to the System administrator.