Go to the documentation of this file.00001
00035 #ifndef _REGISTER_FUNCTIONS_
00036 #define _REGISTER_FUNCTIONS_
00037
00038
00039 #include "register_functions.h"
00040
00041
00048 static int
00049 fpw_function_sort
00050 (
00051 const void *va,
00053 const void *vb
00055 )
00056 {
00057 fpw_function_t *a = (fpw_function_t *) va;
00058 fpw_function_t *b = (fpw_function_t *) vb;
00059 return strcmp (a->name, b->name);
00060 }
00061
00062
00068 fpw_function_t *
00069 fpw_find_function
00070 (
00071 const char *name
00073 )
00074 {
00075 fpw_function_node_t *gf;
00076 int i, n, lower, upper;
00077
00078 if (name == NULL)
00079 return 0;
00080 if (all_functions == 0)
00081 {
00082 n = 0;
00083 all_functions = malloc (n_functions * sizeof (fpw_function_t));
00084 for (gf = fpw_function_nodes; gf; gf = gf->next)
00085 for (i = 0; i < gf->n; i++)
00086 all_functions[n++] = gf->functions[i];
00087 qsort (all_functions, n_functions, sizeof (fpw_function_t), fpw_function_sort);
00088 }
00089 lower = -1;
00090 upper = n_functions;
00091
00092 while (lower < upper - 1)
00093 {
00094 i = (lower + upper) / 2;
00095 n = strcmp (all_functions[i].name, name);
00096
00097 if (n == 0)
00098 return all_functions + i;
00099 if (n > 0)
00100 upper = i;
00101 else
00102 lower = i;
00103 }
00104 for (i = 0; i < n_functions; i++)
00105 if (strcasecmp (all_functions[i].name, name) == 0)
00106 return all_functions + i;
00107 g_log ("", G_LOG_LEVEL_WARNING,
00108 _("unknown function `%s'\n"), name);
00109 return 0;
00110 }
00111
00112
00123 int
00124 fpw_functionv
00125 (
00126 const char *name,
00128 int argc,
00130 char **argv
00132 )
00133 {
00134 int x = 0;
00135 int y = 0;
00136 int i;
00137 fpw_function_t *a;
00138
00139 if (verbose && name)
00140 {
00141 fprintf (stderr, "Function: \033[34m%s(", name);
00142 for (i = 0; i < argc; i++)
00143 fprintf (stderr, "%s%s", i ? "," : "", argv[i]);
00144 fprintf (stderr, ")\033[0m\n");
00145 }
00146 a = fpw_find_function (name);
00147 if (!a)
00148 return 1;
00149 return a->trigger_cb (argc, argv, x, y);
00150 }
00151
00152
00163 int
00164 fpw_function
00165 (
00166 const char *name
00168 )
00169 {
00170 return fpw_functionv (name, 0, 0);
00171 }
00172
00173
00180 void
00181 fpw_register_functions
00182 (
00183 fpw_function_t * a,
00184 int n
00185 )
00186 {
00187 fpw_function_node_t *gf;
00188
00189 if (verbose)
00190 {
00191 fprintf(stderr, "%d functions registered.\n", n);
00192 }
00193 gf = (fpw_function_node_t *) malloc (sizeof (fpw_function_node_t));
00194 gf->next = fpw_function_nodes;
00195 fpw_function_nodes = gf;
00196 gf->functions = a;
00197 gf->n = n;
00198 n_functions += n;
00199 if (all_functions)
00200 {
00201 free (all_functions);
00202 all_functions = 0;
00203 }
00204 }
00205
00206
00207 #endif
00208
00209
00210