Browse Source

tools: use tal/path instead of writing own path handlers.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Rusty Russell 13 years ago
parent
commit
09d5cd70d7

+ 1 - 0
tools/Makefile

@@ -11,6 +11,7 @@ DEP_OBJS = ccan/err/err.o \
 	ccan/take/take.o \
 	ccan/take/take.o \
 	ccan/tal/tal.o \
 	ccan/tal/tal.o \
 	ccan/tal/link/link.o \
 	ccan/tal/link/link.o \
+	ccan/tal/path/path.o \
 	ccan/tal/str/str.o \
 	ccan/tal/str/str.o \
 	ccan/time/time.o \
 	ccan/time/time.o \
 	tools/read_config_header.o \
 	tools/read_config_header.o \

+ 3 - 2
tools/ccan_dir.c

@@ -1,4 +1,5 @@
 #include <ccan/err/err.h>
 #include <ccan/err/err.h>
+#include <ccan/tal/path/path.h>
 #include "tools.h"
 #include "tools.h"
 #include <assert.h>
 #include <assert.h>
 #include <string.h>
 #include <string.h>
@@ -26,8 +27,8 @@ const char *find_ccan_dir(const char *base)
 
 
 	if (!ccan_dir) {
 	if (!ccan_dir) {
 		if (base[0] != '/') {
 		if (base[0] != '/') {
-			const char *tmpctx = tal_getcwd(NULL);
-			find_ccan_dir(tal_fmt(tmpctx, "%s/%s", tmpctx, base));
+			const char *tmpctx = path_cwd(NULL);
+			find_ccan_dir(path_join(tmpctx, tmpctx, base));
 			tal_free(tmpctx);
 			tal_free(tmpctx);
 		} else {
 		} else {
 			unsigned int prefix = ccan_dir_prefix(base);
 			unsigned int prefix = ccan_dir_prefix(base);

+ 1 - 0
tools/ccanlint/Makefile

@@ -24,6 +24,7 @@ CORE_OBJS := \
 	ccan/take/take.o \
 	ccan/take/take.o \
 	ccan/tal/tal.o \
 	ccan/tal/tal.o \
 	ccan/tal/link/link.o \
 	ccan/tal/link/link.o \
+	ccan/tal/path/path.o \
 	ccan/tal/str/str.o \
 	ccan/tal/str/str.o \
 	ccan/time/time.o \
 	ccan/time/time.o \
 	tools/ccanlint/async.o \
 	tools/ccanlint/async.o \

+ 4 - 2
tools/ccanlint/ccanlint.c

@@ -32,6 +32,7 @@
 #include <ccan/foreach/foreach.h>
 #include <ccan/foreach/foreach.h>
 #include <ccan/cast/cast.h>
 #include <ccan/cast/cast.h>
 #include <ccan/tlist/tlist.h>
 #include <ccan/tlist/tlist.h>
+#include <ccan/tal/path/path.h>
 #include <ccan/strmap/strmap.h>
 #include <ccan/strmap/strmap.h>
 
 
 struct ccanlint_map {
 struct ccanlint_map {
@@ -590,7 +591,7 @@ int main(int argc, char *argv[])
 	unsigned int i;
 	unsigned int i;
 	struct manifest *m;
 	struct manifest *m;
 	const char *prefix = "";
 	const char *prefix = "";
-	char *dir = tal_getcwd(NULL), *base_dir = dir, *testlink;
+	char *dir = path_cwd(NULL), *base_dir = dir, *testlink;
 	struct dgraph_node all;
 	struct dgraph_node all;
 	
 	
 	/* Empty graph node to which we attach everything else. */
 	/* Empty graph node to which we attach everything else. */
@@ -675,7 +676,8 @@ int main(int argc, char *argv[])
 		}
 		}
 
 
 		if (dir != base_dir)
 		if (dir != base_dir)
-			prefix = tal_strcat(NULL, take(tal_basename(NULL,dir)),
+			prefix = tal_strcat(NULL,
+					    take(path_basename(NULL,dir)),
 					    ": ");
 					    ": ");
 
 
 		m = get_manifest(autofree(), dir);
 		m = get_manifest(autofree(), dir);

+ 4 - 3
tools/ccanlint/tests/examples_compile.c

@@ -4,6 +4,7 @@
 #include <ccan/tal/str/str.h>
 #include <ccan/tal/str/str.h>
 #include <ccan/take/take.h>
 #include <ccan/take/take.h>
 #include <ccan/cast/cast.h>
 #include <ccan/cast/cast.h>
+#include <ccan/tal/path/path.h>
 #include <ccan/str/str.h>
 #include <ccan/str/str.h>
 #include <sys/types.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/stat.h>
@@ -461,10 +462,10 @@ static struct ccan_file *mangle_example(struct manifest *m,
 	struct ccan_file *f;
 	struct ccan_file *f;
 
 
 	name = temp_file(example, ".c",
 	name = temp_file(example, ".c",
-			 tal_fmt(m, "%s/mangled-%s", m->dir, example->name));
+			 take(tal_fmt(NULL, "mangled-%s", example->name)));
 	f = new_ccan_file(example,
 	f = new_ccan_file(example,
-			  tal_dirname(example, name),
-			  tal_basename(example, name));
+			  path_dirname(example, name),
+			  path_basename(example, name));
 	tal_steal(f, name);
 	tal_steal(f, name);
 
 
 	fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
 	fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);

+ 6 - 6
tools/ccanlint/tests/examples_exist.c

@@ -1,6 +1,8 @@
 #include <tools/ccanlint/ccanlint.h>
 #include <tools/ccanlint/ccanlint.h>
 #include <tools/tools.h>
 #include <tools/tools.h>
 #include <ccan/str/str.h>
 #include <ccan/str/str.h>
+#include <ccan/tal/path/path.h>
+#include <ccan/take/take.h>
 #include <ccan/cast/cast.h>
 #include <ccan/cast/cast.h>
 #include <sys/types.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/stat.h>
@@ -23,16 +25,14 @@ static char *add_example(struct manifest *m, struct ccan_file *source,
 	int fd;
 	int fd;
 	struct ccan_file *f;
 	struct ccan_file *f;
 
 
-	name = tal_fmt(m, "%s/example-%s-%s.c",
-		       tal_dirname(m, source->fullname),
-		       source->name,
-		       example->function);
+	name = tal_fmt(m, "example-%s-%s",
+		       source->name, example->function);
 	/* example->function == 'struct foo' */
 	/* example->function == 'struct foo' */
 	while (strchr(name, ' '))
 	while (strchr(name, ' '))
 		*strchr(name, ' ') = '_';
 		*strchr(name, ' ') = '_';
 
 
-	name = temp_file(m, ".c", name);
-	f = new_ccan_file(m, tal_dirname(m, name), tal_basename(m, name));
+	name = temp_file(m, ".c", take(name));
+	f = new_ccan_file(m, path_dirname(m, name), path_basename(m, name));
 	tal_steal(f, name);
 	tal_steal(f, name);
 	list_add_tail(&m->examples, &f->list);
 	list_add_tail(&m->examples, &f->list);
 
 

+ 3 - 2
tools/ccanlint/tests/tests_coverage.c

@@ -1,6 +1,7 @@
 #include <tools/ccanlint/ccanlint.h>
 #include <tools/ccanlint/ccanlint.h>
 #include <tools/tools.h>
 #include <tools/tools.h>
 #include <ccan/str/str.h>
 #include <ccan/str/str.h>
+#include <ccan/tal/path/path.h>
 #include <ccan/foreach/foreach.h>
 #include <ccan/foreach/foreach.h>
 #include <sys/types.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/stat.h>
@@ -142,8 +143,8 @@ static void do_run_coverage_tests(struct manifest *m,
 	bool ran_some = false;
 	bool ran_some = false;
 
 
 	/* This tells gcov where we put those .gcno files. */
 	/* This tells gcov where we put those .gcno files. */
-	outdir = tal_dirname(score,
-				m->info_file->compiled[COMPILE_NORMAL]);
+	outdir = path_dirname(score,
+			      m->info_file->compiled[COMPILE_NORMAL]);
 	covcmd = tal_fmt(m, "gcov %s -o %s",
 	covcmd = tal_fmt(m, "gcov %s -o %s",
 			 full_gcov ? "" : "-n",
 			 full_gcov ? "" : "-n",
 			 outdir);
 			 outdir);

+ 9 - 10
tools/manifest.c

@@ -3,6 +3,7 @@
 #include "tools.h"
 #include "tools.h"
 #include <ccan/str/str.h>
 #include <ccan/str/str.h>
 #include <ccan/tal/link/link.h>
 #include <ccan/tal/link/link.h>
+#include <ccan/tal/path/path.h>
 #include <ccan/hash/hash.h>
 #include <ccan/hash/hash.h>
 #include <ccan/htable/htable_type.h>
 #include <ccan/htable/htable_type.h>
 #include <ccan/noerr/noerr.h>
 #include <ccan/noerr/noerr.h>
@@ -193,23 +194,22 @@ static void sort_files(struct list_head *list)
 struct manifest *get_manifest(const void *ctx, const char *dir)
 struct manifest *get_manifest(const void *ctx, const char *dir)
 {
 {
 	struct manifest *m;
 	struct manifest *m;
-	char *olddir, *canon_dir;
+	char *canon_dir;
 	unsigned int len;
 	unsigned int len;
 	struct list_head *list;
 	struct list_head *list;
+	struct path_pushd *old;
 
 
 	if (!manifests) {
 	if (!manifests) {
 		manifests = tal(NULL, struct htable_manifest);
 		manifests = tal(NULL, struct htable_manifest);
 		htable_manifest_init(manifests);
 		htable_manifest_init(manifests);
 	}
 	}
 
 
-	olddir = tal_getcwd(NULL);
-	if (!olddir)
-		err(1, "Getting current directory");
-
-	if (chdir(dir) != 0)
+	/* FIXME: Use path_canon, don't chdir! */
+	old = path_pushd(ctx, dir);
+	if (!old)
 		err(1, "Failed to chdir to %s", dir);
 		err(1, "Failed to chdir to %s", dir);
 
 
-	canon_dir = tal_getcwd(olddir);
+	canon_dir = path_cwd(old);
 	if (!canon_dir)
 	if (!canon_dir)
 		err(1, "Getting current directory");
 		err(1, "Getting current directory");
 
 
@@ -257,9 +257,8 @@ struct manifest *get_manifest(const void *ctx, const char *dir)
 	htable_manifest_add(manifests, tal_link(manifests, m));
 	htable_manifest_add(manifests, tal_link(manifests, m));
 
 
 done:
 done:
-	if (chdir(olddir) != 0)
-		err(1, "Returning to original directory '%s'", olddir);
-	tal_free(olddir);
+	if (!path_popd(old))
+		err(1, "Returning to original directory");
 
 
 	return m;
 	return m;
 }
 }

+ 7 - 5
tools/namespacize.c

@@ -12,6 +12,7 @@
 #include "ccan/str/str.h"
 #include "ccan/str/str.h"
 #include "ccan/take/take.h"
 #include "ccan/take/take.h"
 #include "ccan/rbuf/rbuf.h"
 #include "ccan/rbuf/rbuf.h"
+#include "ccan/tal/path/path.h"
 #include "ccan/err/err.h"
 #include "ccan/err/err.h"
 #include "tools.h"
 #include "tools.h"
 
 
@@ -255,7 +256,8 @@ static void analyze_headers(const char *dir, struct replace **repl)
 	char *hdr, *contents;
 	char *hdr, *contents;
 
 
 	/* Get hold of header, assume that's it. */
 	/* Get hold of header, assume that's it. */
-	hdr = tal_fmt(dir, "%s/%s.h", dir, tal_basename(dir, dir));
+	hdr = tal_fmt(dir, "%s/%s.h", dir, path_basename(dir, dir));
+
 	contents = tal_grab_file(dir, hdr, NULL);
 	contents = tal_grab_file(dir, hdr, NULL);
 	if (!contents)
 	if (!contents)
 		err(1, "Reading %s", hdr);
 		err(1, "Reading %s", hdr);
@@ -445,7 +447,7 @@ static struct replace *read_replacement_file(const char *depdir)
 
 
 static void adjust_dir(const char *dir)
 static void adjust_dir(const char *dir)
 {
 {
-	char *parent = tal_dirname(autofree(), dir);
+	char *parent = path_dirname(autofree(), dir);
 	char **deps;
 	char **deps;
 
 
 	verbose("Adjusting %s\n", dir);
 	verbose("Adjusting %s\n", dir);
@@ -473,8 +475,8 @@ static void adjust_dir(const char *dir)
 
 
 static void adjust_dependents(const char *dir)
 static void adjust_dependents(const char *dir)
 {
 {
-	char *parent = tal_dirname(NULL, dir);
-	char *base = tal_basename(parent, dir);
+	char *parent = path_dirname(NULL, dir);
+	char *base = path_basename(parent, dir);
 	char **file;
 	char **file;
 
 
 	verbose("Looking for dependents in %s\n", parent);
 	verbose("Looking for dependents in %s\n", parent);
@@ -483,7 +485,7 @@ static void adjust_dependents(const char *dir)
 		char *info, **deps;
 		char *info, **deps;
 		bool isdep = false;
 		bool isdep = false;
 
 
-		if (tal_basename(*file, *file)[0] == '.')
+		if (path_basename(*file, *file)[0] == '.')
 			continue;
 			continue;
 
 
 		info = tal_fmt(*file, "%s/_info", *file);
 		info = tal_fmt(*file, "%s/_info", *file);

+ 9 - 48
tools/tools.c

@@ -5,6 +5,7 @@
 #include <ccan/read_write_all/read_write_all.h>
 #include <ccan/read_write_all/read_write_all.h>
 #include <ccan/noerr/noerr.h>
 #include <ccan/noerr/noerr.h>
 #include <ccan/time/time.h>
 #include <ccan/time/time.h>
+#include <ccan/tal/path/path.h>
 #include <sys/stat.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/types.h>
 #include <sys/time.h>
 #include <sys/time.h>
@@ -25,42 +26,6 @@ bool tools_verbose = false;
 /* Ten minutes. */
 /* Ten minutes. */
 const unsigned int default_timeout_ms = 10 * 60 * 1000;
 const unsigned int default_timeout_ms = 10 * 60 * 1000;
 
 
-char *tal_basename(const void *ctx, const char *dir)
-{
-	const char *p = strrchr(dir, '/');
-
-	if (!p)
-		return tal_strdup(ctx, dir);
-	return tal_strdup(ctx, p+1);
-}
-
-char *tal_dirname(const void *ctx, const char *dir)
-{
-	const char *p = strrchr(dir, '/');
-
-	if (!p)
-		return tal_strdup(ctx, ".");
-	return tal_strndup(ctx, dir, p - dir);
-}
-
-char *tal_getcwd(const void *ctx)
-{
-	unsigned int len;
-	char *cwd;
-
-	/* *This* is why people hate C. */
-	len = 32;
-	cwd = tal_arr(ctx, char, len);
-	while (!getcwd(cwd, len)) {
-		if (errno != ERANGE) {
-			tal_free(cwd);
-			return NULL;
-		}
-		tal_resize(&cwd, len *= 2);
-	}
-	return cwd;
-}
-
 static void killme(int sig)
 static void killme(int sig)
 {
 {
 	kill(-getpid(), SIGKILL);
 	kill(-getpid(), SIGKILL);
@@ -229,28 +194,24 @@ void keep_temp_dir(void)
 
 
 char *temp_file(const void *ctx, const char *extension, const char *srcname)
 char *temp_file(const void *ctx, const char *extension, const char *srcname)
 {
 {
-	unsigned baselen;
-	char *f, *suffix = tal_strdup(ctx, "");
+	char *f, *base, *suffix;
 	struct stat st;
 	struct stat st;
 	unsigned int count = 0;
 	unsigned int count = 0;
 
 
-	srcname = tal_basename(ctx, srcname);
-	if (strrchr(srcname, '.'))
-		baselen = strrchr(srcname, '.') - srcname;
-	else
-		baselen = strlen(srcname);
+	base = path_join(ctx, temp_dir(), take(path_basename(ctx, srcname)));
+	/* Trim extension. */
+	base[path_ext_off(base)] = '\0';
+	suffix = tal_strdup(ctx, extension);
 
 
 	do {
 	do {
-		f = tal_fmt(ctx, "%s/%.*s%s%s",
-			    temp_dir(), baselen, srcname, suffix, extension);
-		tal_free(suffix);
-		suffix = tal_fmt(ctx, "-%u", ++count);
+		f = tal_strcat(ctx, base, suffix);
+		suffix = tal_fmt(base, "-%u%s", ++count, extension);
 	} while (lstat(f, &st) == 0);
 	} while (lstat(f, &st) == 0);
 
 
 	if (tools_verbose)
 	if (tools_verbose)
 		printf("Creating file %s\n", f);
 		printf("Creating file %s\n", f);
 
 
-	tal_free(suffix);
+	tal_free(base);
 	return f;
 	return f;
 }
 }
 
 

+ 0 - 3
tools/tools.h

@@ -46,9 +46,6 @@ char **get_libs(const void *ctx, const char *dir, const char *style,
 /* From tools.c */
 /* From tools.c */
 /* If set, print all commands run, all output they give and exit status. */
 /* If set, print all commands run, all output they give and exit status. */
 extern bool tools_verbose;
 extern bool tools_verbose;
-char *tal_basename(const void *ctx, const char *dir);
-char *tal_dirname(const void *ctx, const char *dir);
-char *tal_getcwd(const void *ctx);
 bool PRINTF_FMT(4,5) run_command(const void *ctx,
 bool PRINTF_FMT(4,5) run_command(const void *ctx,
 				 unsigned int *time_ms,
 				 unsigned int *time_ms,
 				 char **output,
 				 char **output,