command_trie_match_type_t cli_context_complete_command (cli_context_t *ctx, gchar ***argv, gint *argc, command_action_t **action, GList **suffixes) { gboolean auto_complete = configuration_get_boolean (ctx->config, "AUTO_UNIQUE_COMPLETE"); return command_trie_find (ctx->commands, argv, argc, auto_complete, action, suffixes); }
void cli_context_loop (cli_context_t *ctx, gint argc, gchar **argv) { /* Execute command, if connection status is ok */ if (argc == 0) { gchar *filename = configuration_get_string (ctx->config, "HISTORY_FILE"); ctx->mode = CLI_EXECUTION_MODE_SHELL; /* print welcome message before initialising readline */ if (configuration_get_boolean (ctx->config, "SHELL_START_MESSAGE")) { g_printf (_("Welcome to the XMMS2 CLI shell!\n")); g_printf (_("Type 'help' to list the available commands " "and 'exit' (or CTRL-D) to leave the shell.\n")); } readline_resume (ctx); read_history (filename); using_history (); while (!cli_context_in_status (ctx, CLI_ACTION_STATUS_FINISH)) { cli_context_event_loop_select (ctx); } write_history (filename); } else { ctx->mode = CLI_EXECUTION_MODE_INLINE; cli_context_command_or_flag_dispatch (ctx, argc , argv); while (cli_context_in_status (ctx, CLI_ACTION_STATUS_BUSY) || cli_context_in_status (ctx, CLI_ACTION_STATUS_REFRESH)) { cli_context_event_loop_select (ctx); } } }
cli_infos_t * cli_infos_init (gint argc, gchar **argv) { cli_infos_t *infos; alias_define_t *aliaslist; gchar *filename; gint i; infos = g_new0 (cli_infos_t, 1); /* readline_init needs PROMPT */ filename = configuration_get_filename (); infos->config = configuration_init (filename); g_free (filename); readline_init (infos); if (argc == 0) { infos->mode = CLI_EXECUTION_MODE_SHELL; /* print welcome message before initialising readline */ if (configuration_get_boolean (infos->config, "SHELL_START_MESSAGE")) { g_printf (_("Welcome to the XMMS2 CLI shell!\n")); g_printf (_("Type 'help' to list the available commands " "and 'exit' (or CTRL-D) to leave the shell.\n")); } readline_resume (infos); } else { infos->mode = CLI_EXECUTION_MODE_INLINE; } infos->status = CLI_ACTION_STATUS_READY; infos->commands = command_trie_alloc (); /* Register commands and command names */ for (i = 0; commandlist[i]; ++i) { command_action_t *action = command_action_alloc (); commandlist[i] (action); if (!register_command (infos->commands, &infos->cmdnames, action)) { command_action_free (action); } } /* Register aliases with a default callback */ aliaslist = alias_list (configuration_get_aliases (infos->config)); for (i = 0; aliaslist[i].name; ++i) { command_action_t *action = command_action_alloc (); alias_setup (action, &aliaslist[i]); if (!register_command (infos->commands, &infos->aliasnames, action)) { command_action_free (action); } } alias_list_free (aliaslist); infos->alias_count = 0; infos->aliasnames = cmdnames_reverse (infos->aliasnames); infos->cmdnames = cmdnames_reverse (infos->cmdnames); infos->cache = cli_cache_init (); return infos; }
static gboolean cli_context_autostart (cli_context_t *ctx, gchar *path) { gint ret = 0; /* Start the server if autostart enabled! */ if (configuration_get_boolean (ctx->config, "SERVER_AUTOSTART") && !system ("xmms2-launcher")) { ret = xmmsc_connect (ctx->conn, path); } return !!ret; }
void command_dispatch (cli_infos_t *infos, gint in_argc, gchar **in_argv) { command_action_t *action; command_trie_match_type_t match; gint argc; gchar **argv; gboolean auto_complete; /* The arguments will be updated by command_trie_find. */ argc = in_argc; argv = in_argv; auto_complete = configuration_get_boolean (infos->config, "AUTO_UNIQUE_COMPLETE"); match = command_trie_find (infos->commands, &argv, &argc, auto_complete, &action); if (match == COMMAND_TRIE_MATCH_ACTION) { gboolean help; gboolean need_io; command_context_t *ctx; /* Include one command token as a workaround for the bug that * the option parser does not parse commands starting with a * flag properly (e.g. "-p foo arg1"). Will be skipped by the * command utils. */ ctx = init_context_from_args (action->argdefs, argc + 1, argv - 1); ctx->name = g_strdup (action->name); if (command_flag_boolean_get (ctx, "help", &help) && help) { /* Help flag passed, bypass action and show help */ /* FIXME(g): select aliasnames list if it's an alias */ help_command (infos, infos->cmdnames, in_argv, in_argc, CMD_TYPE_COMMAND); } else if (command_runnable (infos, action)) { /* All fine, run the command */ cli_infos_loop_suspend (infos); need_io = action->callback (infos, ctx); if (!need_io) { cli_infos_loop_resume (infos); } } command_context_free (ctx); } else { /* Call help to print the "no such command" error */ help_command (infos, infos->cmdnames, in_argv, in_argc, CMD_TYPE_COMMAND); } }