+static struct internal_function *
+create_internal_function (const char *name,
+ internal_function_fn handler, void *cookie)
+{
+ struct internal_function *ifn = XNEW (struct internal_function);
+ ifn->name = xstrdup (name);
+ ifn->handler = handler;
+ ifn->cookie = cookie;
+ return ifn;
+}
+
+char *
+value_internal_function_name (struct value *val)
+{
+ struct internal_function *ifn;
+ int result;
+
+ gdb_assert (VALUE_LVAL (val) == lval_internalvar);
+ result = get_internalvar_function (VALUE_INTERNALVAR (val), &ifn);
+ gdb_assert (result);
+
+ return ifn->name;
+}
+
+struct value *
+call_internal_function (struct gdbarch *gdbarch,
+ const struct language_defn *language,
+ struct value *func, int argc, struct value **argv)
+{
+ struct internal_function *ifn;
+ int result;
+
+ gdb_assert (VALUE_LVAL (func) == lval_internalvar);
+ result = get_internalvar_function (VALUE_INTERNALVAR (func), &ifn);
+ gdb_assert (result);
+
+ return (*ifn->handler) (gdbarch, language, ifn->cookie, argc, argv);
+}
+
+/* The 'function' command. This does nothing -- it is just a
+ placeholder to let "help function NAME" work. This is also used as
+ the implementation of the sub-command that is created when
+ registering an internal function. */
+static void
+function_command (char *command, int from_tty)
+{
+ /* Do nothing. */
+}
+
+/* Clean up if an internal function's command is destroyed. */
+static void
+function_destroyer (struct cmd_list_element *self, void *ignore)
+{
+ xfree (self->name);
+ xfree (self->doc);
+}
+
+/* Add a new internal function. NAME is the name of the function; DOC
+ is a documentation string describing the function. HANDLER is
+ called when the function is invoked. COOKIE is an arbitrary
+ pointer which is passed to HANDLER and is intended for "user
+ data". */
+void
+add_internal_function (const char *name, const char *doc,
+ internal_function_fn handler, void *cookie)
+{
+ struct cmd_list_element *cmd;
+ struct internal_function *ifn;
+ struct internalvar *var = lookup_internalvar (name);
+
+ ifn = create_internal_function (name, handler, cookie);
+ set_internalvar_function (var, ifn);
+
+ cmd = add_cmd (xstrdup (name), no_class, function_command, (char *) doc,
+ &functionlist);
+ cmd->destroyer = function_destroyer;
+}
+