From 0f5ab59e62a9c7a6ceca88458d20e7309ec2a01e Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Fri, 1 Jan 2010 20:44:19 +0200 Subject: [PATCH] * : refactor command parsing / checking a bit --- TODO | 15 ++----- src/Makefile.am | 2 + src/mu-cmd.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++ src/mu-cmd.h | 59 +++++++++++++++++++++++++ src/mu-config.c | 17 ++++---- src/mu-config.h | 22 ++++++---- src/mu-query.c | 3 -- src/mu.c | 108 ++++++++++++++++----------------------------- 8 files changed, 237 insertions(+), 103 deletions(-) create mode 100644 src/mu-cmd.c create mode 100644 src/mu-cmd.h diff --git a/TODO b/TODO index b6dd3555..fb611c13 100644 --- a/TODO +++ b/TODO @@ -1,15 +1,15 @@ #+STARTUP:showall * to-do list for mu-ng first release -** release 0.6 [87%] +** release 0.6 [88%] - [X] implement re-index - [X] mu-index config options - [X] check which options actually work, remove rest - [X] fix AND/OR escaping issue - [ ] man page / help - - [ ] add mkmdir - - [ ] re-add symlink support for search + - [X] add mkmdir + - [X] re-add symlink support for search - [X] support MAILDIR - [X] config system (centralize options) - [X] logging system @@ -22,7 +22,6 @@ - [X] cleanup ascending/descending stuff - [ ] testing - ** release 0.7 [%] - [ ] detect mail threads @@ -37,11 +36,3 @@ - [ ] mu-setup - [ ] config system (config file) - [ ] UTF-8 output to console - - - - - - - - diff --git a/src/Makefile.am b/src/Makefile.am index ecb6ba8d..7cb7b231 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -4,6 +4,8 @@ bin_PROGRAMS= \ mu mu_SOURCES= \ + mu-cmd.h \ + mu-cmd.c \ mu-config.h \ mu-config.c \ mu-index.c \ diff --git a/src/mu-cmd.c b/src/mu-cmd.c new file mode 100644 index 00000000..c741fd10 --- /dev/null +++ b/src/mu-cmd.c @@ -0,0 +1,114 @@ +/* +** Copyright (C) 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 +** Free Software Foundation; either version 3, or (at your option) any +** later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software Foundation, +** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +** +*/ + +#include +#include "mu-cmd.h" + +MuCmd +mu_cmd_from_string (const char* cmd) +{ + if (!cmd) + return MU_CMD_UNKNOWN; + + if (strcmp (cmd, "index") == 0) + return MU_CMD_INDEX; + + /* support some synonyms... */ + if ((strcmp (cmd, "query") == 0) || + (strcmp (cmd, "find") == 0) || + (strcmp (cmd, "search") == 0)) + return MU_CMD_QUERY; + + if ((strcmp (cmd, "mkmdir") == 0) || + (strcmp (cmd, "mkdir") == 0)) + return MU_CMD_MKDIR; + + if (strcmp (cmd, "link") == 0) + return MU_CMD_LINK; + + if ((strcmp (cmd, "help") == 0) || + (strcmp (cmd, "info") == 0)) + return MU_CMD_HELP; + + return MU_CMD_UNKNOWN; +} + + + +static gboolean +_check_query_params (MuConfigOptions *opts) +{ + if (opts->linksdir) + if (opts->fields || opts->sortfield || opts->xquery) { + g_warning ("Invalid option for '--linksdir'"); + return FALSE; + } + + if (opts->xquery) + if (opts->fields || opts->sortfield) { + g_warning ("Invalid option for '--xquery'"); + return FALSE; + } + + if (opts->ascending && opts->descending) { + g_warning ("Cannot specify both '--ascending'" + " and '--descending'"); + return FALSE; + } + + if (!opts->params[0] || !opts->params[1]) { + g_warning ("Missing search expression"); + return FALSE; + } + + return TRUE; +} + + +static gboolean +_check_index_params (MuConfigOptions *opts) +{ + if (opts->linksdir) + if (opts->linksdir || opts->fields || + opts->sortfield || opts->xquery || + opts->ascending || opts->descending|| + opts->xquery) { + g_warning ("Invalid option(s) for command"); + return FALSE; + } + + return TRUE; +} + + + +gboolean +mu_cmd_check_parameters (MuCmd cmd, MuConfigOptions *opts) +{ + g_return_val_if_fail (cmd > 0 && cmd < MU_CMD_UNKNOWN, + FALSE); + switch (cmd) { + case MU_CMD_INDEX: + return _check_index_params (opts); + case MU_CMD_QUERY: + return _check_query_params (opts); + default: + return TRUE; + } +} diff --git a/src/mu-cmd.h b/src/mu-cmd.h new file mode 100644 index 00000000..37191257 --- /dev/null +++ b/src/mu-cmd.h @@ -0,0 +1,59 @@ +/* +** Copyright (C) 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 +** Free Software Foundation; either version 3, or (at your option) any +** later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software Foundation, +** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +** +*/ + + +#ifndef __MU_CMD_H__ +#define __MU_CMD_H__ + +#include +#include "mu-config.h" + +enum _MuCmd { + MU_CMD_INDEX, + MU_CMD_QUERY, + MU_CMD_MKDIR, + MU_CMD_LINK, + MU_CMD_HELP, + + MU_CMD_UNKNOWN +}; +typedef enum _MuCmd MuCmd; + +/** + * determine the Mu command from a string + * (as given on the cmdline) + * + * @param cmd string for a command + * + * @return the MuCmd or MU_CMD_UNKNOWN + */ +MuCmd mu_cmd_from_string (const char* cmd); + + +/** + * check the command line parameters + * + * @param cmd the command + * @param opts options + * + * @return TRUE if there are no errors, FALSE otherwise + */ +gboolean mu_cmd_check_parameters (MuCmd cmd, MuConfigOptions *opts); + +#endif /*__MU_CMD_H__*/ diff --git a/src/mu-config.c b/src/mu-config.c index bb74f47b..3d48c3d9 100644 --- a/src/mu-config.c +++ b/src/mu-config.c @@ -122,18 +122,17 @@ mu_config_init (MuConfigOptions *opts) opts->log_stderr = FALSE; /* indexing */ - opts->maildir = mu_util_guess_maildir (); - + opts->maildir = mu_util_guess_maildir(); opts->cleanup = FALSE; opts->reindex = FALSE; /* querying */ - opts->xquery = FALSE; - opts->fields = "d f s"; - opts->sortfield = "d"; - opts->ascending = FALSE; - opts->descending = TRUE; - opts->linksdir = NULL; + opts->xquery = FALSE; + opts->fields = "d f s"; + opts->sortfield = "d"; + opts->ascending = FALSE; + opts->descending = TRUE; + opts->linksdir = NULL; } @@ -145,6 +144,8 @@ mu_config_uninit (MuConfigOptions *opts) g_free (opts->muhome); g_free (opts->maildir); g_free (opts->linksdir); + g_strfreev (opts->params); } + diff --git a/src/mu-config.h b/src/mu-config.h index 9a9332bd..9f78efe8 100644 --- a/src/mu-config.h +++ b/src/mu-config.h @@ -29,23 +29,22 @@ struct _MuConfigOptions { /* general options */ - gboolean quiet; /* don't give any output */ - gboolean debug; /* spew out debug info */ - char *muhome;/* the House of Mu */ + gboolean quiet; /* don't give any output */ + gboolean debug; /* spew out debug info */ + char *muhome; /* the House of Mu */ gboolean version; /* request mu version */ - gboolean log_stderr; /*log to stderr (instead of logfile)*/ + gboolean log_stderr; /* log to stderr (not logfile) */ gboolean log_append; /* append to log (don't overwrite)*/ gchar** params; /* parameters (for querying) */ - /* options for indexing */ char *maildir; /* where the mails are */ gboolean cleanup; /* cleanup deleted mails form db */ gboolean reindex; /* re-index existing mails */ /* options for querying */ - gboolean xquery; /* give the Xapian query instead of - search results */ + gboolean xquery; /* give the Xapian query instead of + search results */ char *fields; /* fields to show in output */ char *sortfield; /* field to sort by (string) */ @@ -102,8 +101,13 @@ GOptionGroup* mu_config_options_group_index (MuConfigOptions *opts); */ GOptionGroup* mu_config_options_group_query (MuConfigOptions *opts); - - +/** + * + * + * @param opts + * + * @return + */ char* mu_config_expanded_mu_home (MuConfigOptions *opts); #endif /*__MU_CONFIG_H__*/ diff --git a/src/mu-query.c b/src/mu-query.c index 274d5ad2..8ef7d946 100644 --- a/src/mu-query.c +++ b/src/mu-query.c @@ -223,9 +223,6 @@ _do_output_links (MuQueryXapian *xapian, MuConfigOptions* opts, gchar **params) } - - - MuResult mu_query_run (MuConfigOptions *opts, gchar **params) { diff --git a/src/mu.c b/src/mu.c index 354adc55..53ce266a 100644 --- a/src/mu.c +++ b/src/mu.c @@ -30,51 +30,11 @@ #include "mu-util.h" #include "mu-config.h" +#include "mu-cmd.h" #include "mu-log.h" #include "mu-msg-gmime.h" -enum _MuCmd { - MU_CMD_INDEX, - MU_CMD_QUERY, - MU_CMD_MKDIR, - MU_CMD_LINK, - MU_CMD_HELP, - MU_CMD_UNKNOWN -}; -typedef enum _MuCmd MuCmd; - - -MuCmd -parse_cmd (const char* cmd) -{ - if (!cmd) - return MU_CMD_UNKNOWN; - - if (strcmp (cmd, "index") == 0) - return MU_CMD_INDEX; - - /* support some synonyms... */ - if ((strcmp (cmd, "query") == 0) || - (strcmp (cmd, "find") == 0) || - (strcmp (cmd, "search") == 0)) - return MU_CMD_QUERY; - - if ((strcmp (cmd, "mkmdir") == 0) || - (strcmp (cmd, "mkdir") == 0)) - return MU_CMD_MKDIR; - - if (strcmp (cmd, "link") == 0) - return MU_CMD_LINK; - - if ((strcmp (cmd, "help") == 0) || - (strcmp (cmd, "info") == 0)) - return MU_CMD_HELP; - - return MU_CMD_UNKNOWN; -} - - static MuResult msg_cb (MuIndexStats* stats, void *user_data) @@ -183,6 +143,28 @@ show_help (MuConfigOptions *opts) return show_usage (FALSE); } +static int +run_index (MuConfigOptions *opts) +{ + MuIndex *midx; + MuIndexStats stats; + int rv; + + midx = mu_index_new (opts->muhome); + rv = mu_index_run (midx, + opts->maildir, + opts->reindex, + &stats, + opts->quiet ? NULL : msg_cb, + NULL, + NULL); + g_print ("\n"); + mu_index_destroy (midx); + + return rv; +} + + static gboolean init_log (MuConfigOptions *opts) @@ -219,7 +201,7 @@ main (int argc, char *argv[]) g_type_init (); - context = g_option_context_new ("- search your e-mail"); + context = g_option_context_new ("- maildir utilities"); g_option_context_set_main_group (context, mu_config_options_group_mu(&config)); @@ -231,10 +213,12 @@ main (int argc, char *argv[]) mu_config_init (&config); ok = g_option_context_parse (context, &argc, &argv, &error); g_option_context_free (context); - + + if (!init_log (&config)) + return 1; + if (!ok) { - g_printerr ("error in options: %s\n", - error->message); + g_printerr ("error in options: %s\n", error->message); g_error_free (error); return 1; } @@ -245,10 +229,13 @@ main (int argc, char *argv[]) if (!config.params[0]) /* no command? */ return show_usage (FALSE); - cmd = parse_cmd (config.params[0]); + cmd = mu_cmd_from_string (config.params[0]); if (cmd == MU_CMD_UNKNOWN) return show_usage (FALSE); + if (!mu_cmd_check_parameters (cmd, &config)) + return 1; + if (cmd == MU_CMD_HELP) return show_help (&config); @@ -258,34 +245,13 @@ main (int argc, char *argv[]) if (cmd == MU_CMD_LINK) return make_symlink (&config); - if (!init_log (&config)) - return 1; - mu_msg_gmime_init (); rv = MU_OK; - if (cmd == MU_CMD_INDEX) { - MuIndex *midx; - MuIndexStats stats; - - midx = mu_index_new (config.muhome); - rv = mu_index_run (midx, - config.maildir, - config.reindex, - &stats, - config.quiet ? NULL : msg_cb, - NULL, - NULL); - g_print ("\n"); - mu_index_destroy (midx); - - } else if (cmd == MU_CMD_QUERY) { - if (!config.params[1]) { - g_printerr ("error: missing something to search for\n"); - rv = 1; - } else - rv = mu_query_run (&config, &config.params[1]); - } + if (cmd == MU_CMD_INDEX) + rv = run_index (&config); + else if (cmd == MU_CMD_QUERY) + rv = mu_query_run (&config, &config.params[1]); mu_msg_gmime_uninit(); mu_log_uninit();