Add gengetopt support.
authorAndre Noll <maan@systemlinux.org>
Sun, 25 May 2008 23:29:30 +0000 (01:29 +0200)
committerAndre Noll <maan@systemlinux.org>
Sun, 25 May 2008 23:29:30 +0000 (01:29 +0200)
First draft of planned options. None is actually implemented so far..

Makefile
adu.c
adu.ggo [new file with mode: 0644]

index 674ff307ba8636864bd861961d8e3c1999bdf8bf..e039865dbac58ee57d5000b72767abf7f9cdc3d8 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-objects := osl.o fd.o rbtree.o string.o adu.o sha1.o
+objects := osl.o fd.o rbtree.o string.o adu.o sha1.o cmdline.o
 all: adu
 
 DEBUG_CPPFLAGS += -Wno-sign-compare -g -Wunused -Wundef -W
@@ -21,6 +21,13 @@ Makefile.deps: $(wildcard *.c *.h)
 adu: $(objects)
        $(CC) -o $@ $(objects) -lcrypto
 
+cmdline.o: cmdline.c cmdline.h
+       $(CC) -c $(CPPFLAGS) $<
+
+cmdline.c cmdline.h: adu.ggo
+       gengetopt --conf-parser < $<
+
+
 %.o: %.c Makefile
        $(CC) -c $(CPPFLAGS) $(DEBUG_CPPFLAGS) $<
 
diff --git a/adu.c b/adu.c
index 8c84c82639e57f57549652fae3ac0aa4123d2789..1aaea35900d67a89163d51625dee4425fc36354e 100644 (file)
--- a/adu.c
+++ b/adu.c
@@ -2,6 +2,7 @@
 #include <dirent.h> /* readdir() */
 
 #include "gcc-compat.h"
+#include "cmdline.h"
 #include "osl.h"
 #include "fd.h"
 #include "hash.h"
@@ -13,6 +14,9 @@ DEFINE_ERRLIST;
 #define DATABASE_DIR "/tmp/adu"
 #define UID_LIST DATABASE_DIR "/" "uid_list"
 
+/** Command line and config file options. */
+static struct gengetopt_args_info conf;
+
 struct user_info {
        uint32_t uid;
        struct osl_table *table;
@@ -807,6 +811,15 @@ static int com_select(void)
 int main(int argc, char **argv)
 {
        int ret = -E_SYNTAX;
+       struct cmdline_parser_params params = {
+               .override = 0,
+               .initialize = 1,
+               .check_required = 0,
+               .check_ambiguity = 0,
+               .print_errors = 1
+       };
+
+       cmdline_parser_ext(argc, argv, &conf, &params); /* aborts on errors */
        if (argc > 2)
                goto out;
        if (argc == 1)
diff --git a/adu.ggo b/adu.ggo
new file mode 100644 (file)
index 0000000..ab6e025
--- /dev/null
+++ b/adu.ggo
@@ -0,0 +1,163 @@
+# Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
+#
+# Licensed under the GPL v2. For licencing details see COPYING.
+
+package "adu"
+version "0.0.1"
+purpose "advanced disk usage
+
+adu creates a database containing disk usage statistics of a given
+directory. It allows to query that database to quickly retrieve
+usage patterns of subdirectories and/or files owned by a given user id.
+"
+
+#########################
+section "General options"
+#########################
+
+option "config-file" c
+#~~~~~~~~~~~~~~~~~~~~~
+"(default='~/.adurc')"
+string typestr="filename"
+optional
+details="
+       Options may be given at the command line or in the
+       configuration file. As usual, if an option is given both at
+       the command line and in the configuration file, the command
+       line option takes precedence.
+
+"
+
+option "database-dir" d
+#~~~~~~~~~~~~~~~~~~~~~~
+"directory containing the osl tables"
+string typestr="path"
+required
+details="
+       Full path to the directory containing the osl tables. This
+       directory must exist. It must be writable for the user running
+       adu in --create mode and readable in --select mode.
+
+"
+option "loglevel" l
+#~~~~~~~~~~~~~~~~~~
+"Set loglevel (0-6)"
+int typestr="level"
+default="3"
+optional
+details="
+       Log messages are always written to stderr while normal output
+       goes to stdout. Lower values mean more verbose logging.
+"
+
+option "uid" u
+#~~~~~~~~~~~~~
+"user id(s) to take into account"
+string typestr="uid_spec"
+optional
+multiple
+details="
+       An uid specifier may be a single number, or a range of uids.
+       Example:
+
+       --uid 42    # only consider uid 42
+       --uid 42-   # only consider uids greater or equal than 42
+       --uid 23-42 # only consider uids between 23 and 42, inclusively.
+
+       This option may be given multiple times. An uid is taken into
+       account if it satisfies at least one --uid option.
+"
+
+
+option "paths" p
+#~~~~~~~~~~~~~~~
+"files to take into account"
+string typestr="pattern"
+optional
+details="
+       Shell wildcard pattern that must match a file in order to be
+       included in the database in --create mode or in the output
+       for --select mode. Only the part of the filename below the
+       base directory is matched against the pattern. The default
+       is to take all files into account. See fnmatch(3) for details.
+"
+
+###############
+section "Modes"
+###############
+
+defgroup "mode"
+#==============
+groupdesc="
+       adu may started in one of two possible modes, each of which
+       corresponds to a different command line option. Exactly one
+       of these options must be given.
+
+"
+required
+
+groupoption "create" C
+#~~~~~~~~~~~~~~~~~~~~~
+"Create a new database"
+group="mode"
+details="
+       Traverse the given directory and track disk user on a per-user
+       basis. Results are stored in N + 1 osl tables where N is
+       the number of uids that own at least one regular file in
+       that directory.
+"
+
+groupoption "select" S
+#~~~~~~~~~~~~~~~~~~~~~
+"query a database previously created with --create"
+group="mode"
+details="
+       This option prints statistics about matching subdirectories to
+       stdout. The output depends on the other options, see below.
+"
+
+##############################
+section "Options for --create"
+##############################
+
+option "base-dir" b
+#~~~~~~~~~~~~~~~~~~
+"directory to traverse"
+string typestr="path"
+dependon="create"
+required
+details="
+       The base directory to be traversed recursively. A warning
+       message is printed for each subdirectory that could not be
+       read because of insufficient permission. These directories
+       will be ignored when computing statistics.
+"
+
+
+##############################
+section "Options for --select"
+##############################
+
+option "limit" L
+#~~~~~~~~~~~~~~~
+"Limit output"
+int  typestr="num"
+required
+dependon="select"
+details="
+       Only print num lines of output.
+"
+
+option "units" U
+#~~~~~~~~~~~~~~~
+"select numerical output format"
+string typestr="format"
+optional
+details="
+       Print the number of files/directories and the sizes in
+       the given format.  All  sizes  are output in these units:
+       (h)uman-readable, (b)ytes, (k)ilobytes, (m)egabytes,
+       (g)igabytes, (t)erabytes.  Capitalise to use multiples
+       of 1000 (S.I.) instead of 1024. The default is \"h\",
+       i.e. human-readable.
+"