* <many>: refactor command parsing / checking a bit

This commit is contained in:
Dirk-Jan C. Binnema 2010-01-01 20:44:19 +02:00
parent 144fffc4be
commit 0f5ab59e62
8 changed files with 237 additions and 103 deletions

15
TODO
View File

@ -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

View File

@ -4,6 +4,8 @@ bin_PROGRAMS= \
mu
mu_SOURCES= \
mu-cmd.h \
mu-cmd.c \
mu-config.h \
mu-config.c \
mu-index.c \

114
src/mu-cmd.c Normal file
View File

@ -0,0 +1,114 @@
/*
** Copyright (C) 2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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 <string.h>
#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;
}
}

59
src/mu-cmd.h Normal file
View File

@ -0,0 +1,59 @@
/*
** Copyright (C) 2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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 <glib.h>
#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__*/

View File

@ -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);
}

View File

@ -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__*/

View File

@ -223,9 +223,6 @@ _do_output_links (MuQueryXapian *xapian, MuConfigOptions* opts, gchar **params)
}
MuResult
mu_query_run (MuConfigOptions *opts, gchar **params)
{

108
src/mu.c
View File

@ -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();