summaryrefslogtreecommitdiff
path: root/extension/dl.c
blob: afc16af50a615e8d229fda257a7627bb97176a5c (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
/*
 * dl.c - Example of adding a new builtin function to gawk.
 *
 * Christos Zoulas, Thu Jun 29 17:40:41 EDT 1995
 * Arnold Robbins, update for 3.1, Wed Sep 13 09:38:56 2000
 */

/*
 * Copyright (C) 1995 - 2001, 2011 the Free Software Foundation, Inc.
 * 
 * This file is part of GAWK, the GNU implementation of the
 * AWK Programming Language.
 * 
 * GAWK 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 of the License, or
 * (at your option) any later version.
 * 
 * GAWK 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 "awk.h"
#include <dlfcn.h>

int plugin_is_GPL_compatible;

static void *sdl = NULL;

static NODE *
zaxxon(int nargs)
{
	NODE *obj;
	int i;
	int comma = 0;

	/*
	 * Print the arguments
	 */
	printf("External linkage zaxxon(");

	for (i = 0; i < nargs; i++) {

		obj = get_scalar_argument(i, TRUE);

		if (obj == NULL)
			break;

		force_string(obj);

		printf(comma ? ", %s" : "%s", obj->stptr);
		comma = 1;
	}

	printf(");\n");

	/*
	 * Do something useful
	 */
	obj = get_scalar_argument(0, FALSE);

	if (obj != NULL) {
		force_string(obj);
		if (strcmp(obj->stptr, "unload") == 0 && sdl) {
			/*
			 * XXX: How to clean up the function? 
			 * I would like the ability to remove a function...
			 */
			dlclose(sdl);
			sdl = NULL;
		}
	}

	/* Set the return value */
	return make_number((AWKNUM) 3.14);
}

NODE *
dlload(tree, dl)
NODE *tree;
void *dl;
{
	sdl = dl;
	make_builtin("zaxxon", zaxxon, 4);
	return make_number((AWKNUM) 0);
}