]> git.ozlabs.org Git - ccan/commitdiff
tools: explicit find_ccan_dir()
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 19 Mar 2012 05:15:45 +0000 (15:45 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 19 Mar 2012 05:15:45 +0000 (15:45 +1030)
Move ccan_dir determination out to its own function, rather than implying it
by the first time we get the manifest of a module.

tools/ccan_dir.c [new file with mode: 0644]
tools/ccanlint/Makefile
tools/ccanlint/ccanlint.c
tools/ccanlint/ccanlint.h
tools/manifest.c
tools/manifest.h
tools/tools.h

diff --git a/tools/ccan_dir.c b/tools/ccan_dir.c
new file mode 100644 (file)
index 0000000..0bb9b14
--- /dev/null
@@ -0,0 +1,31 @@
+#include <ccan/talloc/talloc.h>
+#include "tools.h"
+#include <assert.h>
+#include <string.h>
+#include <stdlib.h>
+
+/* Walk up to find /ccan/ => ccan directory. */
+static unsigned int ccan_dir_prefix(const char *fulldir)
+{
+       unsigned int i;
+
+       assert(fulldir[0] == '/');
+       for (i = strlen(fulldir) - 1; i > 0; i--) {
+               if (strncmp(fulldir+i, "/ccan", 5) != 0)
+                       continue;
+               if (fulldir[i+5] != '\0' && fulldir[i+5] != '/')
+                       continue;
+               return i + 1;
+       }
+       return 0;
+}
+
+const char *find_ccan_dir(const char *base)
+{
+       unsigned int prefix = ccan_dir_prefix(base);
+
+       if (!prefix)
+               return NULL;
+
+       return talloc_strndup(NULL, base, prefix);
+}
index 463ef89e4c4ef9b61df8d3754e5de3f81f4dd7f1..cf450f27fd106a94d872e63b66ffd769409f96cb 100644 (file)
@@ -27,6 +27,7 @@ CORE_OBJS := \
        tools/ccanlint/ccanlint.o \
        tools/ccanlint/file_analysis.o \
        tools/ccanlint/licenses.o \
+       tools/ccan_dir.o \
        tools/compile.o \
        tools/depends.o \
        tools/doc_extract-core.o \
index 516c5bb6cbe062e43d23e910989f69c077161155..931ed41a44429c91affa837c789cd7294e8287cb 100644 (file)
@@ -52,6 +52,8 @@ const char *cflags = NULL;
 
 const char *config_header;
 
+const char *ccan_dir;
+
 #if 0
 static void indent_print(const char *string)
 {
@@ -777,16 +779,21 @@ int main(int argc, char *argv[])
                        dir[strlen(dir)-1] = '\0';
 
        got_dir:
+               /* We assume there's a ccan/ in there somewhere... */
+               if (i == 1) {
+                       ccan_dir = find_ccan_dir(dir);
+                       if (!ccan_dir)
+                               errx(1, "Cannot find ccan/ base directory in %s",
+                                    dir);
+                       read_config_header();
+               }
+
                if (dir != base_dir)
                        prefix = talloc_append_string(talloc_basename(NULL,dir),
                                                      ": ");
 
                m = get_manifest(talloc_autofree_context(), dir);
 
-               /* FIXME: This has to come after we've got manifest. */
-               if (i == 1)
-                       read_config_header();
-
                /* Create a symlink from temp dir back to src dir's
                 * test directory. */
                unlink(testlink);
index 6fcca5fa8eb26fbf40153e244a27c4d9e66e0922..8ebf68d276e8f2bc4042bf5694e1a1b534cbd1e1 100644 (file)
@@ -191,4 +191,7 @@ extern const char *compiler, *cflags;
 /* Contents of config.h (or NULL if not found) */
 extern const char *config_header;
 
+/* Where is the ccan dir?   */
+extern const char *ccan_dir;
+
 #endif /* CCAN_LINT_H */
index 9de9030b2a0e71d6e18f95c36536fe98a4c5ad35..024eaa42a0d42c469e08f2ccba78846f58e0f1d5 100644 (file)
@@ -23,8 +23,6 @@
 #include <stdarg.h>
 #include <assert.h>
 
-const char *ccan_dir;
-
 static size_t dir_hash(const char *name)
 {
        return hash(name, strlen(name), 0);
@@ -196,22 +194,6 @@ static void sort_files(struct list_head *list)
        talloc_free(files);
 }
 
-/* Walk up tp find /ccan/ => ccan directory. */
-static unsigned int ccan_dir_prefix(const char *fulldir)
-{
-       unsigned int i;
-
-       assert(fulldir[0] == '/');
-       for (i = strlen(fulldir) - 1; i > 0; i--) {
-               if (strncmp(fulldir+i, "/ccan", 5) != 0)
-                       continue;
-               if (fulldir[i+5] != '\0' && fulldir[i+5] != '/')
-                       continue;
-               return i + 1;
-       }
-       errx(1, "Could not find /ccan/ dir in %s", fulldir);
-}
-
 struct manifest *get_manifest(const void *ctx, const char *dir)
 {
        struct manifest *m;
@@ -265,12 +247,6 @@ struct manifest *get_manifest(const void *ctx, const char *dir)
                errx(1, "I don't expect to be run from the root directory");
        m->basename++;
 
-       if (!ccan_dir) {
-               unsigned int prefix = ccan_dir_prefix(m->dir);
-
-               ccan_dir = talloc_strndup(NULL, m->dir, prefix);
-       }
-
        add_files(m, "");
 
        /* Nicer to run tests in a predictable order. */
index 588efe5f0185f0c5941cfaeb489d8ff930a589d9..ee5ae9dfcd6aaa1651753408147a28173284152a 100644 (file)
@@ -89,7 +89,4 @@ const char *get_ccan_file_contents(struct ccan_file *f);
 /* Use this rather than accessing f->lines directly: loads on demand. */
 char **get_ccan_file_lines(struct ccan_file *f);
 
-/* Where is the ccan dir?  Available after first manifest. */
-extern const char *ccan_dir;
-
 #endif /* CCAN_TOOLS_MANIFEST_H */
index 4ac9818307212ed790d5008061a79f06a2084744..8d1ae04887493206107990ec810c209e475f1cb0 100644 (file)
@@ -75,4 +75,6 @@ extern const unsigned int default_timeout_ms;
 /* Talloc destructor which unlinks file. */
 int unlink_file_destructor(char *filename);
 
+/* Get ccan/ top dir, given a directory within it. */
+const char *find_ccan_dir(const char *base);
 #endif /* CCAN_TOOLS_H */