2 * Functions for handling UObject package search paths.
3 * Copyright (C) 2009 Nick Bowler
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include <uobject/vfs.h>
30 /* Check if a character is a directory separator. */
32 # define IS_DIRSEP(x) ((x) == '/' || (x) == LT_DIRSEP_CHAR)
34 # define IS_DIRSEP(x) ((x) == '/')
41 static struct avl_table *local_tree;
42 static int initialized;
44 static int localcmp(const void *_a, const void *_b, void *_data)
46 const struct local_pkg *a = _a, *b = _b;
47 return strcasecmp(a->name, b->name);
50 static const char *pkgname_base(const char *file)
52 const char *base = NULL;
55 for (size_t i = 0; file[i]; i++) {
56 if (IS_DIRSEP(file[i])) {
58 } else if (slash == 1) {
67 static size_t pkgname_len(const char *base)
71 for (i = 0; base[i]; i++) {
72 if (IS_DIRSEP(base[i]) || base[i] == '.')
79 const char *u_pkg_vfs_add_local(const char *name, const char *file)
81 size_t filelen = strlen(file)+1, namelen;
82 struct local_pkg *spec;
85 name = pkgname_base(file);
88 namelen = pkgname_len(name);
90 /* For simplicity, stuff everything in a single allocation. */
91 spec = malloc(sizeof *spec + filelen + namelen + 1);
96 spec->file = (char *)spec + sizeof *spec;
97 memcpy(spec->file, file, filelen);
99 spec->name = (char *)spec + sizeof *spec + filelen;
100 memcpy(spec->name, name, namelen);
101 spec->name[namelen] = 0;
103 if (avl_find(local_tree, spec)) {
104 fprintf(stderr, "%s: attempted to add duplicate local package.\n", __func__);
107 return name; /* "Success"-ish. */
110 if (avl_probe(local_tree, spec) == NULL) {
118 void u_pkg_vfs_del_local(const char *name)
120 struct local_pkg spec = { .name = (char *)name }, *item;
122 item = avl_find(local_tree, &spec);
126 const char *u_pkg_vfs_lookup(const char *name)
128 struct local_pkg spec = { .name = (char *)name }, *item;
133 item = avl_find(local_tree, &spec);
139 int u_pkg_vfs_init(void)
142 local_tree = avl_create(localcmp, NULL, NULL);
144 fprintf(stderr, "%s: failed to create local module tree.\n", __func__);
153 static void local_destroy(void *item, void *data)
158 void u_pkg_vfs_exit(void)
160 if (--initialized == 0)
161 avl_destroy(local_tree, local_destroy);