null+****@clear*****
null+****@clear*****
2010年 6月 11日 (金) 15:01:00 JST
Nobuyoshi Nakada 2010-06-11 06:01:00 +0000 (Fri, 11 Jun 2010)
New Revision: 61c4781d571962f3582a76cdfa027e23b45887f9
Log:
load default values of command line options from the config file. #66
Added files:
test/unit/gqtp/test-config-file.rb
Modified files:
configure.ac
lib/str.c
src/groonga.c
test/unit/gqtp/Makefile.am
Modified: configure.ac (+7 -0)
===================================================================
--- configure.ac 2010-06-11 05:34:25 +0000 (94e8ade)
+++ configure.ac 2010-06-11 06:01:00 +0000 (0f12fd2)
@@ -577,6 +577,13 @@ AC_ARG_WITH(rsync-path,
[RSYNC_PATH=""])
AC_SUBST(RSYNC_PATH)
+GRN_CONFIG_PATH="`
+ test \"$prefix\" = NONE && prefix=
+ eval echo ${sysconfdir}/groonga/groonga.conf
+`"
+AC_DEFINE_UNQUOTED(GRN_CONFIG_PATH, ["$GRN_CONFIG_PATH"],
+ [Default command line option configuration file.])
+
AC_OUTPUT([
groonga.spec
groonga.pc
Modified: lib/str.c (+1 -1)
===================================================================
--- lib/str.c 2010-06-11 05:34:25 +0000 (8cac0db)
+++ lib/str.c 2010-06-11 06:01:00 +0000 (c91cfba)
@@ -1814,7 +1814,7 @@ grn_str_getopt(int argc, char * const argv[], const grn_str_getopt_opt *opts,
}
return i;
exit:
- fprintf(stderr, "cannot recognize option '%s'.\n", argv[i]);
+ fprintf(stderr, "%s: cannot recognize option '%s'.\n", argv[0], argv[i]);
return -1;
}
Modified: src/groonga.c (+84 -23)
===================================================================
--- src/groonga.c 2010-06-11 05:34:25 +0000 (e85d5a4)
+++ src/groonga.c 2010-06-11 06:01:00 +0000 (c6d191c)
@@ -23,6 +23,7 @@
#include "lib/db.h"
#include <string.h>
#include <stdio.h>
+#include <ctype.h>
#include <fcntl.h>
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
@@ -65,23 +66,23 @@ usage(FILE *output)
fprintf(output,
"Usage: groonga [options...] [dest]\n"
"options:\n"
- " -n: create new database\n"
- " -c: run in client mode\n"
- " -s: run in server mode\n"
- " -d: run in daemon mode\n"
- " -e: encoding for new database [none|euc|utf8|sjis|latin1|koi8r]\n"
- " -l <log level>: log level\n"
- " -a <ip/hostname>: server address to listen (default: %s)\n"
- " -p <port number>: server port number (default: %d)\n"
- " -i <ip/hostname>: server ID address (default: %s)\n"
- " -t <max threads>: max number of free threads (default: %d)\n"
- " -h, --help: show usage\n"
- " --admin-html-path <path>: specify admin html path\n"
- " --protocol <protocol>: server protocol to listen (default: gqtp)\n"
- " --version: show groonga version\n"
- " --log-path <path>: specify log path\n"
- " --query-log-path <path>: specify query log path\n"
- " --pid-file <path>: specify pid file path (daemon mode only)\n"
+ " -n: create new database\n"
+ " -c: run in client mode\n"
+ " -s: run in server mode\n"
+ " -d: run in daemon mode\n"
+ " -e, --default-encoding: encoding for new database [none|euc|utf8|sjis|latin1|koi8r]\n"
+ " -l, --log-level <log level>: log level\n"
+ " -a, --address <ip/hostname>: server address to listen (default: %s)\n"
+ " -p, --port <port number>: server port number (default: %d)\n"
+ " -i, --server-id <ip/hostname>: server ID address (default: %s)\n"
+ " -t, --max-threads <max threads>: max number of free threads (default: %d)\n"
+ " -h, --help: show usage\n"
+ " --admin-html-path <path>: specify admin html path\n"
+ " --protocol <protocol>: server protocol to listen (default: gqtp)\n"
+ " --version: show groonga version\n"
+ " --log-path <path>: specify log path\n"
+ " --query-log-path <path>: specify query log path\n"
+ " --pid-file <path>: specify pid file path (daemon mode only)\n"
"\n"
"dest: <db pathname> [<command>] or <dest hostname>\n"
" <db pathname> [<command>]: when standalone/server mode\n"
@@ -1653,6 +1654,59 @@ get_core_number(void)
#endif /* WIN32 */
}
+static inline char *
+skipspace(char *str)
+{
+ while (*str == ' ' || *str == '\t') { ++str; }
+ return str;
+}
+
+static int
+load_config_file(const char *path,
+ const grn_str_getopt_opt *opts, int *flags)
+{
+ int name_len, value_len;
+ char buf[1024+2], *str, *name, *value, *args[4];
+ FILE *file;
+
+ if (!(file = fopen(path, "r"))) return 0;
+
+ args[0] = (char *)path;
+ args[3] = NULL;
+ while ((str = fgets(buf + 2, sizeof(buf) - 2, file))) {
+ str = skipspace(str);
+ switch (*str) {
+ case '#': case ';': case '\0':
+ continue;
+ }
+ name = str;
+ while (*str && !isspace(*str) && *str != '=') { str++; }
+ if ((name_len = (int)(str - name)) == 0) {
+ continue;
+ }
+ value_len = 0;
+ if (*str && (*str == '=' || *(str = skipspace(str)) == '=')) {
+ str++;
+ value = str = skipspace(str);
+ while (*str && *str != '#' && *str != ';') {
+ if (!isspace(*str)) {
+ value_len = (int)(str - value) + 1;
+ }
+ str++;
+ }
+ value[value_len] = '\0';
+ }
+ name[name_len] = '\0';
+ memset(name -= 2, '-', 2);
+ args[1] = name;
+ args[2] = value;
+ grn_str_getopt((value_len > 0) + 2, args, opts, flags);
+ }
+ fclose(file);
+
+ return 1;
+}
+
int
main(int argc, char **argv)
{
@@ -1662,16 +1716,16 @@ main(int argc, char **argv)
*listen_addressstr = NULL, *hostnamestr = NULL, *protocol = NULL;
int r, i, mode = mode_alone;
static grn_str_getopt_opt opts[] = {
- {'p', NULL, NULL, 0, getopt_op_none},
- {'e', NULL, NULL, 0, getopt_op_none},
- {'t', NULL, NULL, 0, getopt_op_none},
+ {'p', "port", NULL, 0, getopt_op_none},
+ {'e', "default-encoding", NULL, 0, getopt_op_none},
+ {'t', "max-threads", NULL, 0, getopt_op_none},
{'h', "help", NULL, mode_usage, getopt_op_update},
- {'a', NULL, NULL, 0, getopt_op_none},
+ {'a', "address", NULL, 0, getopt_op_none},
{'c', NULL, NULL, mode_client, getopt_op_update},
{'d', NULL, NULL, mode_daemon, getopt_op_update},
{'s', NULL, NULL, mode_server, getopt_op_update},
- {'l', NULL, NULL, 0, getopt_op_none},
- {'i', NULL, NULL, 0, getopt_op_none},
+ {'l', "log-level", NULL, 0, getopt_op_none},
+ {'i', "server", NULL, 0, getopt_op_none},
{'q', NULL, NULL, MODE_USE_QL, getopt_op_on},
{'n', NULL, NULL, MODE_NEW_DB, getopt_op_on},
{'\0', "admin-html-path", NULL, 0, getopt_op_none},
@@ -1697,6 +1751,13 @@ main(int argc, char **argv)
default_max_nfthreads = DEFAULT_MAX_NFTHREADS;
}
strcpy(listen_address, "0.0.0.0");
+ {
+ const char *config_path = getenv("GRN_CONFIG_PATH");
+ if (!config_path) {
+ config_path = GRN_CONFIG_PATH;
+ }
+ load_config_file(config_path, opts, &mode);
+ }
i = grn_str_getopt(argc, argv, opts, &mode);
if (i < 0) { mode = mode_error; }
if (portstr) { port = atoi(portstr); }
Modified: test/unit/gqtp/Makefile.am (+1 -0)
===================================================================
--- test/unit/gqtp/Makefile.am 2010-06-11 05:34:25 +0000 (d3285bd)
+++ test/unit/gqtp/Makefile.am 2010-06-11 06:01:00 +0000 (b211652)
@@ -2,5 +2,6 @@ EXTRA_DIST = \
test-cache.rb \
test-restore.rb \
test-load.rb \
+ test-config-file.rb \
test-option-help.rb \
test-option-pid-file.rb
Added: test/unit/gqtp/test-config-file.rb (+38 -0) 100644
===================================================================
--- /dev/null
+++ test/unit/gqtp/test-config-file.rb 2010-06-11 06:01:00 +0000 (edecc54)
@@ -0,0 +1,38 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2010 Nobuyoshi Nakada <nakad****@clear*****>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License version 2.1 as published by the Free Software Foundation.
+#
+# This library 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
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+class OptionPidFileTest < Test::Unit::TestCase
+ include GroongaTestUtils
+
+ def setup
+ setup_database_path
+ @config_env = ENV['GRN_CONFIG_PATH']
+ @config_path = File.join(@tmp_dir, "groonga.conf")
+ ENV['GRN_CONFIG_PATH'] = @config_path
+ end
+
+ def teardown
+ ENV['GRN_CONFIG_PATH'] = @config_env
+ FileUtils.rm_f(@config_path)
+ teardown_database_path
+ end
+
+ def test_config_empty
+ open(@config_path, "w") {|f|}
+ assert_equal("", run_groonga("-n", @database_path, "quit"))
+ end
+end