From a7a08dde7f24ca900691d41d9cc7fdef6cd172d0 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sun, 29 Aug 2010 16:39:27 +0300 Subject: [PATCH] * add some extra error checking for the right command --- src/mu-cmd-extract.c | 3 ++- src/mu-cmd-find.c | 1 + src/mu-cmd-index.c | 3 +++ src/mu-cmd-mkdir.c | 6 +++--- src/mu-cmd.c | 15 ++++++++++++++- src/mu-cmd.h | 14 +++++++++++--- 6 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/mu-cmd-extract.c b/src/mu-cmd-extract.c index 0b645d3b..9722f059 100644 --- a/src/mu-cmd-extract.c +++ b/src/mu-cmd-extract.c @@ -81,7 +81,8 @@ mu_cmd_extract (MuConfigOptions *opts) gboolean rv; g_return_val_if_fail (opts, FALSE); - + g_return_val_if_fail (mu_cmd_equals (opts, "extract"), FALSE); + /* note: params[0] will be 'view' */ if (!opts->params[0] || !opts->params[1]) { g_warning ("missing mail file to extract something from"); diff --git a/src/mu-cmd-find.c b/src/mu-cmd-find.c index a179a8a1..c56758a7 100644 --- a/src/mu-cmd-find.c +++ b/src/mu-cmd-find.c @@ -325,6 +325,7 @@ mu_cmd_find (MuConfigOptions *opts) const gchar **params; g_return_val_if_fail (opts, FALSE); + g_return_val_if_fail (mu_cmd_equals (opts, "find"), FALSE); if (!query_params_valid (opts)) return FALSE; diff --git a/src/mu-cmd-index.c b/src/mu-cmd-index.c index a70eb53e..38a2bbf1 100644 --- a/src/mu-cmd-index.c +++ b/src/mu-cmd-index.c @@ -173,6 +173,8 @@ mu_cmd_cleanup (MuConfigOptions *opts) MuIndexStats stats; g_return_val_if_fail (opts, FALSE); + g_return_val_if_fail (mu_cmd_equals (opts, "cleanup"), FALSE); + if (!check_index_params (opts)) return FALSE; @@ -225,6 +227,7 @@ mu_cmd_index (MuConfigOptions *opts) MuIndexStats stats; g_return_val_if_fail (opts, FALSE); + g_return_val_if_fail (mu_cmd_equals (opts, "find"), FALSE); if (!check_index_params (opts)) return FALSE; diff --git a/src/mu-cmd-mkdir.c b/src/mu-cmd-mkdir.c index 4c6398f6..fa29e5ba 100644 --- a/src/mu-cmd-mkdir.c +++ b/src/mu-cmd-mkdir.c @@ -34,10 +34,10 @@ gboolean mu_cmd_mkdir (MuConfigOptions *opts) { int i; + + g_return_val_if_fail (opts, FALSE); + g_return_val_if_fail (mu_cmd_equals (opts, "mkdir"), FALSE); - if (!opts->params[0]) - return FALSE; /* shouldn't happen */ - if (!opts->params[1]) { g_printerr ( "usage: mu mkdir [-u,--mode=] " diff --git a/src/mu-cmd.c b/src/mu-cmd.c index 41e6729a..3c1697a7 100644 --- a/src/mu-cmd.c +++ b/src/mu-cmd.c @@ -1,5 +1,5 @@ /* -** Copyright (C) 2010 Dirk-Jan C. Binnema +** Copyright (C) 2008-2010 Dirk-Jan C. Binnema ** ** This program is free software; you can redistribute it and/or modify it ** under the terms of the GNU General Public License as published by the @@ -28,6 +28,19 @@ #include "mu-maildir.h" #include "mu-cmd.h" +gboolean +mu_cmd_equals (MuConfigOptions *config, const gchar *cmd) +{ + g_return_val_if_fail (config, FALSE); + g_return_val_if_fail (cmd, FALSE); + + if (!config->params || !config->params[0]) + return FALSE; + + return (strcmp (config->params[0], cmd) == 0); +} + + static MuCmd cmd_from_string (const char* cmd) { diff --git a/src/mu-cmd.h b/src/mu-cmd.h index 01a2b700..4ddc095d 100644 --- a/src/mu-cmd.h +++ b/src/mu-cmd.h @@ -38,6 +38,17 @@ enum _MuCmd { }; typedef enum _MuCmd MuCmd; + +/** + * check whether the MuConfigOptions are for command X + * + * @param config the config options + * @param cmd the command to check (ie., "mkdir" or "find") + * + * @return TRUE if the options are for cmd, FALSE otherwise + */ +gboolean mu_cmd_equals (MuConfigOptions *config, const gchar *cmd); + /** * try to execute whatever is specified on the command line * @@ -47,8 +58,6 @@ typedef enum _MuCmd MuCmd; */ gboolean mu_cmd_execute (MuConfigOptions *config); - - /** * execute the 'mkdir' command * @@ -108,7 +117,6 @@ gboolean mu_cmd_find (MuConfigOptions *opts); */ gboolean mu_cmd_extract (MuConfigOptions *opts); - G_END_DECLS #endif /*__MU_CMD_H__*/