aboutsummaryrefslogtreecommitdiff
path: root/modules/echo/echo.c
blob: 0d4c8080ef325beb62adf79997728a11b78a5a84 (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
/* This file is part of Smap.
   Copyright (C) 2006-2007, 2010, 2014 Sergey Poznyakoff

   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 3, 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, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <smap/stream.h>
#include <smap/diag.h>
#include <smap/module.h>

struct echo_database {
	int argc;
	char **argv;
};

static smap_database_t
echo_init_db(const char *dbid, int argc, char **argv)
{
	struct echo_database *db = malloc(sizeof(*db));
	if (!db) {
		smap_error("not enough memory");
		return NULL;
	}
	db->argc = argc - 1;
	db->argv = argv + 1;
	return (smap_database_t) db;
}

static int
echo_free_db(smap_database_t dbp)
{
	free(dbp);
	return 0;
}

static int
echo_query(smap_database_t dbp,
	   smap_stream_t ostr,
	   const char *map, const char *key,
	   struct smap_conninfo const *conninfo)
{
	struct echo_database *edb = (struct echo_database *) dbp;
	int i;

	for (i = 0; i < edb->argc; i++) {
		if (i)
			smap_stream_write(ostr, " ", 1, NULL);
		smap_stream_write(ostr, edb->argv[i], strlen(edb->argv[i]),
				  NULL);
	}
	smap_stream_write(ostr, "\n", 1, NULL);
	return 0;
}

struct smap_module SMAP_EXPORT(echo, module) = {
	SMAP_MODULE_VERSION,
	SMAP_CAPA_DEFAULT,
	NULL, /* smap_init */
	echo_init_db,
	echo_free_db,
	NULL, /* smap_open */
	NULL, /* smap_close */
	echo_query,
	NULL  /* smap_xform */
};

Return to:

Send suggestions and report system problems to the System administrator.