* mu: update mu script

This commit is contained in:
djcb
2012-10-22 23:16:01 +03:00
parent 34f47ae5ca
commit 5c301f71ec
8 changed files with 212 additions and 121 deletions

View File

@ -27,7 +27,7 @@ INCLUDES=-I${top_srcdir}/lib $(GLIB_CFLAGS)
# really need all the params they get
AM_CFLAGS=-Wall -Wextra -Wno-unused-parameter -Wdeclaration-after-statement \
-pedantic -Wno-variadic-macros \
-DMU_STATSDIR="\"$(pkgdatadir)/scripts/stats\""
-DMU_SCRIPTS_DIR="\"$(pkgdatadir)/scripts/\""
AM_CXXFLAGS=-Wall -Wextra -Wno-unused-parameter
bin_PROGRAMS= \

View File

@ -35,33 +35,56 @@
#include "mu-script.h"
#define MU_GUILE_EXT ".scm"
#define MU_GUILE_DESCR_PREFIX ";; DESCRIPTION: "
#define MU_GUILE_DESCR_PREFIX ";; INFO: "
static void
print_stats (GSList *scripts)
static gboolean
print_scripts (GSList *scripts, gboolean verbose, const char *rxstr,
GError **err)
{
GSList *cur;
gboolean first;
if (!scripts) {
g_print ("No statistics available\n");
return;
g_print ("No scripts available\n");
return TRUE; /* not an error */
}
g_print ("Available statistics "
"(use with --stat=<stastistic>):\n");
for (cur = scripts; cur; cur = g_slist_next (cur)) {
if (rxstr)
g_print ("Available scripts matching '%s':\n", rxstr);
else
g_print ("Available scripts:\n");
for (cur = scripts, first = TRUE; cur; cur = g_slist_next (cur)) {
MuScriptInfo *msi;
const char* descr;
const char* descr, *oneline, *name;
msi = (MuScriptInfo*)cur->data;
descr = mu_script_info_description (msi);
msi = (MuScriptInfo*)cur->data;
name = mu_script_info_name (msi);
oneline = mu_script_info_one_line (msi);
descr = mu_script_info_description (msi);
g_print ("\t%s%s%s\n",
mu_script_info_name (msi),
descr ? ": " : "",
descr ? descr : "");
/* if rxstr is provide, only consider matching scriptinfos */
if (rxstr && !mu_script_info_matches_regex (msi, rxstr, err)) {
if (err && *err)
return FALSE;
continue;
}
/* whitespace between */
if (verbose && !first)
g_print ("\n");
first = FALSE;
g_print ("%s%s%s", name,
oneline ? ": " : "",
oneline ? oneline :"");
if (verbose && descr)
g_print ("%s", descr);
}
return TRUE;
}
@ -71,9 +94,11 @@ get_script_info_list (const char *muhome, GError **err)
GSList *scripts, *userscripts, *last;
char *userpath;
scripts = mu_script_get_script_info_list (MU_STATSDIR, MU_GUILE_EXT,
MU_GUILE_DESCR_PREFIX,
err);
scripts = mu_script_get_script_info_list
(MU_SCRIPTS_DIR, MU_GUILE_EXT,
MU_GUILE_DESCR_PREFIX,
err);
if (err && *err)
return NULL;
@ -111,7 +136,7 @@ check_params (MuConfig *opts, GError **err)
{
if (!mu_util_supports (MU_FEATURE_GUILE | MU_FEATURE_GNUPLOT)) {
mu_util_g_set_error (err, MU_ERROR_IN_PARAMETERS,
"the 'stats' command is not supported");
"the 'script' command is not supported");
return FALSE;
}
@ -120,18 +145,15 @@ check_params (MuConfig *opts, GError **err)
MuError
mu_cmd_stats (MuConfig *opts, GError **err)
mu_cmd_script (MuConfig *opts, GError **err)
{
MuScriptInfo *msi;
GSList *scripts;
gchar *query;
g_return_val_if_fail (opts, MU_ERROR_INTERNAL);
g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_STATS,
g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_SCRIPT,
MU_ERROR_INTERNAL);
query = NULL;
if (!check_params (opts, err))
return MU_ERROR;
@ -139,29 +161,24 @@ mu_cmd_stats (MuConfig *opts, GError **err)
if (err && *err)
goto leave;
if (!opts->stat) {
print_stats (scripts);
if (!opts->script) {
print_scripts (scripts, opts->verbose,
opts->params[1], err);
goto leave;
}
msi = mu_script_find_script_with_name (scripts, opts->stat);
msi = mu_script_find_script_with_name (scripts, opts->script);
if (!msi) {
mu_util_g_set_error (err, MU_ERROR_IN_PARAMETERS,
"script not found");
goto leave;
}
query = (opts->params[1]) ?
mu_str_quoted_from_strv ((const gchar**)&opts->params[1]) :
NULL;
/* do it! */
mu_script_guile_run (msi, opts->muhome, query ? query : "",
opts->textonly, err);
mu_script_guile_run (msi, opts->muhome,
(const gchar**)&opts->params[1], err);
leave:
/* this won't be reached, unless there is some error */
mu_script_info_list_destroy (scripts);
g_free (query);
return (err && *err) ? MU_ERROR : MU_OK;
}

View File

@ -609,11 +609,11 @@ mu_cmd_execute (MuConfig *opts, GError **err)
/* already handled in mu-config.c */
case MU_CONFIG_CMD_HELP: return MU_OK;
case MU_CONFIG_CMD_CFIND: merr = mu_cmd_cfind (opts, err); break;
case MU_CONFIG_CMD_MKDIR: merr = mu_cmd_mkdir (opts, err); break;
case MU_CONFIG_CMD_STATS: merr = mu_cmd_stats (opts, err); break;
case MU_CONFIG_CMD_VIEW: merr = mu_cmd_view (opts, err); break;
case MU_CONFIG_CMD_VERIFY: merr = mu_cmd_verify (opts, err); break;
case MU_CONFIG_CMD_CFIND: merr = mu_cmd_cfind (opts, err); break;
case MU_CONFIG_CMD_MKDIR: merr = mu_cmd_mkdir (opts, err); break;
case MU_CONFIG_CMD_SCRIPT: merr = mu_cmd_script (opts, err); break;
case MU_CONFIG_CMD_VIEW: merr = mu_cmd_view (opts, err); break;
case MU_CONFIG_CMD_VERIFY: merr = mu_cmd_verify (opts, err); break;
case MU_CONFIG_CMD_EXTRACT: merr = mu_cmd_extract (opts, err); break;
case MU_CONFIG_CMD_FIND:

View File

@ -105,7 +105,7 @@ MuError mu_cmd_mv (MuConfig *opts, GError **err);
/**
* execute the 'stats' command
* execute the 'script' command
*
* @param opts configuration options
* @param err receives error information, or NULL
@ -113,7 +113,7 @@ MuError mu_cmd_mv (MuConfig *opts, GError **err);
* @return MU_OK (0) if the command succeeds,
* some error code otherwise
*/
MuError mu_cmd_stats (MuConfig *opts, GError **err);
MuError mu_cmd_script (MuConfig *opts, GError **err);
/**
* execute the cfind command

View File

@ -95,7 +95,7 @@ config_options_group_mu (void)
"print debug output to standard error (false)", NULL},
{"quiet", 'q', 0, G_OPTION_ARG_NONE, &MU_CONFIG.quiet,
"don't give any progress information (false)", NULL},
{"version", 'v', 0, G_OPTION_ARG_NONE, &MU_CONFIG.version,
{"version", 0, 0, G_OPTION_ARG_NONE, &MU_CONFIG.version,
"display version and copyright information (false)", NULL},
{"muhome", 0, 0, G_OPTION_ARG_FILENAME, &MU_CONFIG.muhome,
"specify an alternative mu directory", "<dir>"},
@ -292,18 +292,16 @@ config_options_group_cfind (void)
static GOptionGroup *
config_options_group_stats (void)
config_options_group_script (void)
{
GOptionGroup *og;
GOptionEntry entries[] = {
{"stat", 0, 0, G_OPTION_ARG_STRING, &MU_CONFIG.stat,
"statistic to show (see `mu help stats')", "<statistic>"},
{"textonly", 0, 0, G_OPTION_ARG_NONE, &MU_CONFIG.textonly,
"use text-only output", NULL},
{"script", 0, 0, G_OPTION_ARG_STRING, &MU_CONFIG.script,
"script to run (see `mu help script')", "<script>"},
{NULL, 0, 0, 0, NULL, NULL, NULL}
};
og = g_option_group_new("stats", "Options for the 'stats' command",
og = g_option_group_new("script", "Options for the 'script' command",
"", NULL, NULL);
g_option_group_add_entries(og, entries);
@ -459,8 +457,8 @@ cmd_from_string (const char *str)
{ "index", MU_CONFIG_CMD_INDEX },
{ "mkdir", MU_CONFIG_CMD_MKDIR },
{ "remove", MU_CONFIG_CMD_REMOVE },
{ "script", MU_CONFIG_CMD_SCRIPT },
{ "server", MU_CONFIG_CMD_SERVER },
{ "stats", MU_CONFIG_CMD_STATS },
{ "verify", MU_CONFIG_CMD_VERIFY },
{ "view", MU_CONFIG_CMD_VIEW }
};
@ -514,8 +512,8 @@ get_option_group (MuConfigCmd cmd)
return config_options_group_mkdir();
case MU_CONFIG_CMD_SERVER:
return config_options_group_server();
case MU_CONFIG_CMD_STATS:
return config_options_group_stats();
case MU_CONFIG_CMD_SCRIPT:
return config_options_group_script();
case MU_CONFIG_CMD_VERIFY:
return config_options_group_verify();
case MU_CONFIG_CMD_VIEW:
@ -628,25 +626,31 @@ parse_params (int *argcp, char ***argvp)
{
GError *err;
GOptionContext *context;
GOptionGroup *group;
gboolean rv;
context = g_option_context_new("- mu general options");
g_option_context_set_main_group(context, config_options_group_mu());
g_option_context_set_help_enabled (context, TRUE);
err = NULL;
rv = TRUE;
if (MU_CONFIG.cmd == MU_CONFIG_CMD_NONE) {
show_usage ();
return TRUE;
}
/* help is special */
if (MU_CONFIG.cmd == MU_CONFIG_CMD_HELP) {
switch (MU_CONFIG.cmd) {
case MU_CONFIG_CMD_NONE: show_usage(); break;
case MU_CONFIG_CMD_HELP:
/* 'help' is special; sucks in the options of the
* command after it */
rv = g_option_context_parse (context, argcp, argvp, &err) &&
cmd_help ();
} else {
GOptionGroup *group;
break;
case MU_CONFIG_CMD_SCRIPT:
/* script feeds the rest of the options to the script, so
* we accept all of them */
g_option_context_set_ignore_unknown_options (context, TRUE);
/* fall through */
default:
g_option_context_set_main_group(context,
config_options_group_mu());
group = get_option_group (MU_CONFIG.cmd);
if (group)
g_option_context_add_group(context, group);
@ -654,14 +658,14 @@ parse_params (int *argcp, char ***argvp)
}
g_option_context_free (context);
if (!rv) {
g_printerr ("mu: error in options: %s\n",
err ? err->message : "?");
g_clear_error (&err);
return FALSE;
}
return TRUE;
if (rv)
return TRUE;
/* something when wrong */
g_printerr ("mu: option error: %s\n", err ? err->message : "?");
g_clear_error (&err);
return FALSE;
}

View File

@ -72,8 +72,8 @@ enum _MuConfigCmd {
MU_CONFIG_CMD_INDEX,
MU_CONFIG_CMD_MKDIR,
MU_CONFIG_CMD_REMOVE,
MU_CONFIG_CMD_SCRIPT,
MU_CONFIG_CMD_SERVER,
MU_CONFIG_CMD_STATS,
MU_CONFIG_CMD_VERIFY,
MU_CONFIG_CMD_VIEW,
@ -176,9 +176,8 @@ struct _MuConfig {
gboolean overwrite; /* should we overwrite same-named files */
gboolean play; /* after saving, try to 'play'
* (open) the attmnt using xdgopen */
/* options for mu-stats */
gboolean textonly; /* no non-textual graphs */
gchar *stat; /* statistic to show */
/* options for mu-script */
gchar *script; /* script to run */
};
typedef struct _MuConfig MuConfig;