Main Page | Class List | Directories | File List | Class Members | File Members

dl.c

Go to the documentation of this file.
00001 /*
00002  * dl.c - Example of adding a new builtin function to gawk.
00003  *
00004  * Christos Zoulas, Thu Jun 29 17:40:41 EDT 1995
00005  * Arnold Robbins, update for 3.1, Wed Sep 13 09:38:56 2000
00006  */
00007 
00008 /*
00009  * Copyright (C) 1995 - 2001 the Free Software Foundation, Inc.
00010  * 
00011  * This file is part of GAWK, the GNU implementation of the
00012  * AWK Programming Language.
00013  * 
00014  * GAWK is free software; you can redistribute it and/or modify
00015  * it under the terms of the GNU General Public License as published by
00016  * the Free Software Foundation; either version 2 of the License, or
00017  * (at your option) any later version.
00018  * 
00019  * GAWK is distributed in the hope that it will be useful,
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022  * GNU General Public License for more details.
00023  * 
00024  * You should have received a copy of the GNU General Public License
00025  * along with this program; if not, write to the Free Software
00026  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
00027  */
00028 
00029 #include "awk.h"
00030 #include <dlfcn.h>
00031 
00032 static void *sdl = NULL;
00033 
00034 static NODE *
00035 zaxxon(tree)
00036 NODE *tree;
00037 {
00038         NODE *obj;
00039         int i;
00040         int comma = 0;
00041 
00042         /*
00043          * Print the arguments
00044          */
00045         printf("External linkage %s(", tree->param);
00046 
00047         for (i = 0; i < tree->param_cnt; i++) {
00048 
00049                 obj = get_argument(tree, i);
00050 
00051                 if (obj == NULL)
00052                         break;
00053 
00054                 force_string(obj);
00055 
00056                 printf(comma ? ", %s" : "%s", obj->stptr);
00057                 free_temp(obj);
00058                 comma = 1;
00059         }
00060 
00061         printf(");\n");
00062 
00063         /*
00064          * Do something useful
00065          */
00066         obj = get_argument(tree, 0);
00067 
00068         if (obj != NULL) {
00069                 force_string(obj);
00070                 if (strcmp(obj->stptr, "unload") == 0 && sdl) {
00071                         /*
00072                          * XXX: How to clean up the function? 
00073                          * I would like the ability to remove a function...
00074                          */
00075                         dlclose(sdl);
00076                         sdl = NULL;
00077                 }
00078                 free_temp(obj);
00079         }
00080 
00081         /* Set the return value */
00082         set_value(tmp_number((AWKNUM) 3.14));
00083 
00084         /* Just to make the interpreter happy */
00085         return tmp_number((AWKNUM) 0);
00086 }
00087 
00088 NODE *
00089 dlload(tree, dl)
00090 NODE *tree;
00091 void *dl;
00092 {
00093         sdl = dl;
00094         make_builtin("zaxxon", zaxxon, 4);
00095         return tmp_number((AWKNUM) 0);
00096 }

© sourcejam.com 2005-2008