1 | /*
|
---|
2 | * dl.c - Example of adding a new builtin function to gawk.
|
---|
3 | *
|
---|
4 | * Christos Zoulas, Thu Jun 29 17:40:41 EDT 1995
|
---|
5 | * Arnold Robbins, update for 3.1, Wed Sep 13 09:38:56 2000
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 1995 - 2001 the Free Software Foundation, Inc.
|
---|
10 | *
|
---|
11 | * This file is part of GAWK, the GNU implementation of the
|
---|
12 | * AWK Programming Language.
|
---|
13 | *
|
---|
14 | * GAWK is free software; you can redistribute it and/or modify
|
---|
15 | * it under the terms of the GNU General Public License as published by
|
---|
16 | * the Free Software Foundation; either version 2 of the License, or
|
---|
17 | * (at your option) any later version.
|
---|
18 | *
|
---|
19 | * GAWK is distributed in the hope that it will be useful,
|
---|
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
22 | * GNU General Public License for more details.
|
---|
23 | *
|
---|
24 | * You should have received a copy of the GNU General Public License
|
---|
25 | * along with this program; if not, write to the Free Software
|
---|
26 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include "awk.h"
|
---|
30 | #include <dlfcn.h>
|
---|
31 |
|
---|
32 | static void *sdl = NULL;
|
---|
33 |
|
---|
34 | static NODE *
|
---|
35 | zaxxon(tree)
|
---|
36 | NODE *tree;
|
---|
37 | {
|
---|
38 | NODE *obj;
|
---|
39 | int i;
|
---|
40 | int comma = 0;
|
---|
41 |
|
---|
42 | /*
|
---|
43 | * Print the arguments
|
---|
44 | */
|
---|
45 | printf("External linkage %s(", tree->param);
|
---|
46 |
|
---|
47 | for (i = 0; i < tree->param_cnt; i++) {
|
---|
48 |
|
---|
49 | obj = get_argument(tree, i);
|
---|
50 |
|
---|
51 | if (obj == NULL)
|
---|
52 | break;
|
---|
53 |
|
---|
54 | force_string(obj);
|
---|
55 |
|
---|
56 | printf(comma ? ", %s" : "%s", obj->stptr);
|
---|
57 | free_temp(obj);
|
---|
58 | comma = 1;
|
---|
59 | }
|
---|
60 |
|
---|
61 | printf(");\n");
|
---|
62 |
|
---|
63 | /*
|
---|
64 | * Do something useful
|
---|
65 | */
|
---|
66 | obj = get_argument(tree, 0);
|
---|
67 |
|
---|
68 | if (obj != NULL) {
|
---|
69 | force_string(obj);
|
---|
70 | if (strcmp(obj->stptr, "unload") == 0 && sdl) {
|
---|
71 | /*
|
---|
72 | * XXX: How to clean up the function?
|
---|
73 | * I would like the ability to remove a function...
|
---|
74 | */
|
---|
75 | dlclose(sdl);
|
---|
76 | sdl = NULL;
|
---|
77 | }
|
---|
78 | free_temp(obj);
|
---|
79 | }
|
---|
80 |
|
---|
81 | /* Set the return value */
|
---|
82 | set_value(tmp_number((AWKNUM) 3.14));
|
---|
83 |
|
---|
84 | /* Just to make the interpreter happy */
|
---|
85 | return tmp_number((AWKNUM) 0);
|
---|
86 | }
|
---|
87 |
|
---|
88 | NODE *
|
---|
89 | dlload(tree, dl)
|
---|
90 | NODE *tree;
|
---|
91 | void *dl;
|
---|
92 | {
|
---|
93 | sdl = dl;
|
---|
94 | make_builtin("zaxxon", zaxxon, 4);
|
---|
95 | return tmp_number((AWKNUM) 0);
|
---|
96 | }
|
---|