From 05d0076d97ed69a531df1aa5cde3a1e6ed17f922 Mon Sep 17 00:00:00 2001 From: Waldemar Brodkorb Date: Tue, 14 Sep 2010 19:04:46 +0200 Subject: replace mksh scripts with faster C programs depmaker and pkgmaker is replaced by C programs. scan-pkgs.sh will be replaced by another mechanism. scan-pkgs.sh is needed to recognize package flavour changes, so that a package is rebuild. Generation of meta-data is a lot faster now. Fix or add new PKG variables to fulfill the needs of the new programs. Documentation will follow as soon as it is stable. --- tools/Makefile | 2 +- tools/adk/Makefile | 11 + tools/adk/depmaker.c | 233 +++++++++++++++ tools/adk/pkgmaker.c | 797 +++++++++++++++++++++++++++++++++++++++++++++++++++ tools/adk/sortfile.c | 153 ++++++++++ tools/adk/sortfile.h | 1 + tools/adk/strmap.c | 510 ++++++++++++++++++++++++++++++++ tools/adk/strmap.h | 350 ++++++++++++++++++++++ 8 files changed, 2056 insertions(+), 1 deletion(-) create mode 100644 tools/adk/Makefile create mode 100644 tools/adk/depmaker.c create mode 100644 tools/adk/pkgmaker.c create mode 100644 tools/adk/sortfile.c create mode 100644 tools/adk/sortfile.h create mode 100644 tools/adk/strmap.c create mode 100644 tools/adk/strmap.h (limited to 'tools') diff --git a/tools/Makefile b/tools/Makefile index 7c5931319..b828f6765 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -3,7 +3,7 @@ include $(TOPDIR)/rules.mk -TARGETS:=mkcrypt cpio +TARGETS:=adk mkcrypt cpio TARGETS_INSTALL:=$(patsubst %,%-install,$(TARGETS)) TARGETS_CLEAN:=$(patsubst %,%-clean,$(TARGETS)) diff --git a/tools/adk/Makefile b/tools/adk/Makefile new file mode 100644 index 000000000..28d8787ce --- /dev/null +++ b/tools/adk/Makefile @@ -0,0 +1,11 @@ +# This file is part of the OpenADK project. OpenADK is copyrighted +# material, please see the LICENCE file in the top-level directory. + +include $(TOPDIR)/rules.mk + +${TOPDIR}/bin/tools/depmaker: + $(HOSTCC) -o $(TOPDIR)/bin/tools/depmaker depmaker.c + +install: ${TOPDIR}/bin/tools/depmaker + +include $(TOPDIR)/mk/tools.mk diff --git a/tools/adk/depmaker.c b/tools/adk/depmaker.c new file mode 100644 index 000000000..c0e434590 --- /dev/null +++ b/tools/adk/depmaker.c @@ -0,0 +1,233 @@ +/* + * depmaker - create package/Depends.mk for OpenADK buildsystem + * + * Copyright (C) 2010 Waldemar Brodkorb + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +#define MAXLINE 512 +#define MAXPATH 128 + +static int prefix = 0; + +static int check_symbol(char *symbol) { + + FILE *config; + char buf[MAXLINE]; + char *sym; + int ret; + + if ((sym = malloc(strlen(symbol) + 2)) != NULL) + memset(sym, 0, strlen(symbol) + 2); + else { + perror("Can not allocate memory."); + exit(EXIT_FAILURE); + } + + strncat(sym, symbol, strlen(symbol)); + strncat(sym, "=", 1); + if ((config = fopen(".config", "r")) == NULL) { + perror("Can not open file \".config\"."); + exit(EXIT_FAILURE); + } + + ret = 1; + while (fgets(buf, MAXLINE, config) != NULL) { + if (strncmp(buf, sym, strlen(sym)) == 0) + ret = 0; + } + + free(sym); + if (fclose(config) != 0) + perror("Closing file stream failed"); + + return(ret); +} + +/*@null@*/ +static char *parse_line(char *package, char *pkgvar, char *string, int checksym) { + + char *key, *value, *dep, *key_sym, *pkgdeps; + char temp[MAXLINE]; + + string[strlen(string)-1] = '\0'; + if ((key = strtok(string, ":=")) == NULL) { + perror("Can not get key from string."); + exit(EXIT_FAILURE); + } + + if (checksym == 1) { + /* extract symbol */ + if ((key_sym = malloc(MAXLINE)) != NULL) + memset(key_sym, 0, MAXLINE); + else { + perror("Can not allocate memory."); + exit(EXIT_FAILURE); + } + if (snprintf(key_sym, MAXLINE, "ADK_PACKAGE_%s_", pkgvar) < 0) + perror("Can not create string variable."); + + strncat(key_sym, key+6, strlen(key)-6); + if (check_symbol(key_sym) != 0) { + free(key_sym); + return(NULL); + } + free(key_sym); + } + + if ((pkgdeps = malloc(MAXLINE)) != NULL) + memset(pkgdeps, 0, MAXLINE); + else { + perror("Can not allocate memory."); + exit(EXIT_FAILURE); + } + + value = strtok(NULL, "=\t"); + dep = strtok(value, " "); + while (dep != NULL) { + if (prefix == 0) { + prefix = 1; + if (snprintf(temp, MAXLINE, "%s-compile: %s-compile", package, dep) < 0) + perror("Can not create string variable."); + } else { + if (snprintf(temp, MAXLINE, " %s-compile", dep) < 0) + perror("Can not create string variable."); + } + strncat(pkgdeps, temp, strlen(temp)); + dep = strtok(NULL, " "); + } + return(pkgdeps); +} + +int main() { + + DIR *pkgdir; + struct dirent *pkgdirp; + FILE *pkg; + char buf[MAXLINE]; + char path[MAXPATH]; + char *string, *pkgvar, *pkgdeps, *tmp; + int i; + + /* read Makefile's for all packages */ + pkgdir = opendir("package"); + while ((pkgdirp = readdir(pkgdir)) != NULL) { + /* skip dotfiles */ + if (strncmp(pkgdirp->d_name, ".", 1) > 0) { + if (snprintf(path, MAXLINE, "package/%s/Makefile", pkgdirp->d_name) < 0) + perror("Can not create string variable."); + pkg = fopen(path, "r"); + if (pkg == NULL) + continue; + + /* transform to uppercase variable name */ + pkgvar = strndup(pkgdirp->d_name, strlen(pkgdirp->d_name)); + for (i=0; i<(int)strlen(pkgvar); i++) { + if (pkgvar[i] == '+') + pkgvar[i] = 'X'; + if (pkgvar[i] == '-') + pkgvar[i] = '_'; + pkgvar[i] = toupper(pkgvar[i]); + } + + /* exclude manual maintained packages from package/Makefile */ + if (!(strncmp(pkgdirp->d_name, "eglibc", 6) == 0) && + !(strncmp(pkgdirp->d_name, "libc", 4) == 0) && + !(strncmp(pkgdirp->d_name, "libpthread", 10) == 0) && + !(strncmp(pkgdirp->d_name, "uclibc++", 8) == 0) && + !(strncmp(pkgdirp->d_name, "uclibc", 6) == 0) && + !(strncmp(pkgdirp->d_name, "glibc", 5) == 0)) { + /* print result to stdout */ + printf("package-$(ADK_COMPILE_%s) += %s\n", pkgvar, pkgdirp->d_name); + } + + if ((pkgdeps = malloc(MAXLINE)) != NULL) + memset(pkgdeps, 0, MAXLINE); + else { + perror("Can not allocate memory."); + exit(EXIT_FAILURE); + } + prefix = 0; + + /* generate build dependencies */ + while (fgets(buf, MAXLINE, pkg) != NULL) { + if ((tmp = malloc(MAXLINE)) != NULL) + memset(tmp, 0 , MAXLINE); + else { + perror("Can not allocate memory."); + exit(EXIT_FAILURE); + } + + /* just read variables prefixed with PKG */ + if (strncmp(buf, "PKG", 3) == 0) { + + string = strstr(buf, "PKG_BUILDDEP:="); + if (string != NULL) { + tmp = parse_line(pkgdirp->d_name, pkgvar, string, 0); + if (tmp != NULL) { + strncat(pkgdeps, tmp, strlen(tmp)); + } + } + + string = strstr(buf, "PKG_BUILDDEP+="); + if (string != NULL) { + tmp = parse_line(pkgdirp->d_name, pkgvar, string, 0); + if (tmp != NULL) + strncat(pkgdeps, tmp, strlen(tmp)); + } + + string = strstr(buf, "PKGFB_"); + if (string != NULL) { + tmp = parse_line(pkgdirp->d_name, pkgvar, string, 1); + if (tmp != NULL) + strncat(pkgdeps, tmp, strlen(tmp)); + } + + string = strstr(buf, "PKGCB_"); + if (string != NULL) { + tmp = parse_line(pkgdirp->d_name, pkgvar, string, 1); + if (tmp != NULL) + strncat(pkgdeps, tmp, strlen(tmp)); + } + + string = strstr(buf, "PKGSB_"); + if (string != NULL) { + tmp = parse_line(pkgdirp->d_name, pkgvar, string, 1); + if (tmp != NULL) + strncat(pkgdeps, tmp, strlen(tmp)); + } + } + free(tmp); + } + if (strlen(pkgdeps) != 0) + printf("%s\n", pkgdeps); + free(pkgdeps); + free(pkgvar); + if (fclose(pkg) != 0) + perror("Closing file stream failed"); + } + } + if (closedir(pkgdir) != 0) + perror("Closing directory stream failed"); + + return(0); +} diff --git a/tools/adk/pkgmaker.c b/tools/adk/pkgmaker.c new file mode 100644 index 000000000..667707d0b --- /dev/null +++ b/tools/adk/pkgmaker.c @@ -0,0 +1,797 @@ +/* + * pkgmaker - create package meta-data for OpenADK buildsystem + * + * Copyright (C) 2010 Waldemar Brodkorb + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "sortfile.h" +#include "strmap.h" + +#define MAXLINE 4096 +#define MAXVALUE 168 +#define MAXVAR 64 +#define MAXPATH 320 +#define HASHSZ 32 + +static int nobinpkgs; + +static void fatal_error(const char *message) { + + fprintf(stderr, "Fatal error. %s\n", message); + exit(1); +} + +static int parse_var_hash(char *buf, const char *varname, StrMap *strmap) { + + char *key, *value, *string; + + string = strstr(buf, varname); + if (string != NULL) { + string[strlen(string)-1] = '\0'; + key = strtok(string, ":="); + value = strtok(NULL, "=\t"); + if (value != NULL) + strmap_put(strmap, key, value); + return(0); + } + return(1); +} + +static int parse_var(char *buf, const char *varname, char *pvalue, char **result) { + + char *pkg_var; + char *key, *value, *string; + char pkg_str[MAXVAR]; + + if ((pkg_var = malloc(MAXLINE)) != NULL) + memset(pkg_var, 0, MAXLINE); + else { + perror("Can not allocate memory"); + exit(EXIT_FAILURE); + } + + if (snprintf(pkg_str, MAXVAR, "%s:=", varname) < 0) + perror("can not create path variable."); + string = strstr(buf, pkg_str); + if (string != NULL) { + string[strlen(string)-1] = '\0'; + key = strtok(string, ":="); + value = strtok(NULL, "=\t"); + if (value != NULL) { + strncat(pkg_var, value, strlen(value)); + *result = strdup(pkg_var); + } else { + nobinpkgs = 1; + *result = NULL; + } + free(pkg_var); + return(0); + } else { + if (snprintf(pkg_str, MAXVAR, "%s+=", varname) < 0) + perror("can not create path variable."); + string = strstr(buf, pkg_str); + if (string != NULL) { + string[strlen(string)-1] = '\0'; + key = strtok(string, "+="); + value = strtok(NULL, "=\t"); + if (pvalue != NULL) + strncat(pkg_var, pvalue, strlen(pvalue)); + strncat(pkg_var, " ", 1); + if (value != NULL) + strncat(pkg_var, value, strlen(value)); + *result = strdup(pkg_var); + free(pkg_var); + return(0); + } + } + free(pkg_var); + return(1); +} + +/* +static void iter_debug(const char *key, const char *value, const void *obj) { + fprintf(stderr, "HASHMAP key: %s value: %s\n", key, value); +} +*/ + +static int hash_str(char *string) { + + int i; + int hash; + + hash = 0; + for (i=0; i<(int)strlen(string); i++) { + hash += string[i]; + } + return(hash); +} + +static void iter(const char *key, const char *value, const void *obj) { + + FILE *config, *section; + int hash; + char *valuestr, *pkg, *subpkg; + char buf[MAXPATH]; + char infile[MAXPATH]; + char outfile[MAXPATH]; + + valuestr = strdup(value); + config = fopen("package/Config.in.auto", "a"); + if (config == NULL) + fatal_error("Can not open file package/Config.in.auto"); + + hash = hash_str(valuestr); + snprintf(infile, MAXPATH, "package/pkglist.d/sectionlst.%d", hash); + snprintf(outfile, MAXPATH, "package/pkglist.d/sectionlst.%d.sorted", hash); + + if (access(infile, F_OK) == 0) { + valuestr[strlen(valuestr)-1] = '\0'; + fprintf(config, "menu \"%s\"\n", valuestr); + sortfile(infile, outfile); + /* avoid duplicate section entries */ + unlink(infile); + section = fopen(outfile, "r"); + while (fgets(buf, MAXPATH, section) != NULL) { + buf[strlen(buf)-1] = '\0'; + if (buf[strlen(buf)-1] == '@') { + buf[strlen(buf)-1] = '\0'; + fprintf(config, "source \"package/%s/Config.in.manual\"\n", buf); + } else { + subpkg = strtok(buf, "|"); + pkg = strtok(NULL, "|"); + fprintf(config, "source \"package/pkgconfigs.d/%s/Config.in.%s\"\n", pkg, subpkg); + } + } + fprintf(config, "endmenu\n\n"); + fclose(section); + } + fclose(config); +} + +static char *print_target_depline(char *value, int neg, char *sp, FILE *cfg) { + + char *val; + char *np; + char *sptr; + + sptr = NULL; + np = ""; + val = strdup(value); + /* strtok_r is required here */ + val = strtok_r(val, " ", &sptr); + while (val != NULL) { + if (neg == 1) np = "!"; + fprintf(cfg, "%s%s%s", sp, np, val); + val = strtok_r(NULL, " ", &sptr); + if (neg == 1) + sp = " && "; + else + sp = " || "; + } + return(val); +} + +static char *tolowerstr(char *string) { + + int i; + char *str; + + /* transform to lowercase variable name */ + str = strdup(string); + for (i=0; i<(int)strlen(str); i++) { + if (str[i] == '_') + str[i] = '-'; + str[i] = tolower(str[i]); + } + return(str); +} + +static char *toupperstr(char *string) { + + int i; + char *str; + + /* transform to uppercase variable name */ + str = strdup(string); + for (i=0; i<(int)strlen(str); i++) { + if (str[i] == '+') + str[i] = 'X'; + if (str[i] == '-') + str[i] = '_'; + /* remove negation here, useful for package host depends */ + if (str[i] == '!') + str[i] = '_'; + str[i] = toupper(str[i]); + } + return(str); +} + + +int main() { + + DIR *pkgdir, *pkglistdir; + struct dirent *pkgdirp; + FILE *pkg, *cfg, *target, *menuglobal, *section; + char hvalue[MAXVALUE]; + char buf[MAXPATH]; + char tbuf[MAXPATH]; + char path[MAXPATH]; + char spath[MAXPATH]; + char dir[MAXPATH]; + char variable[2*MAXVAR]; + char *key, *value, *token, *cftoken, *sp, *hkey, *val, *pkg_fd; + char *pkg_name, *pkg_depends, *pkg_section, *pkg_descr, *pkg_url; + char *pkg_cxx, *pkg_subpkgs, *pkg_cfline, *pkg_dflt, *pkg_multi; + char *pkg_host_depends, *pkg_target_depends, *pkg_flavours, *pkg_choices, *pseudo_name; + char *packages, *pkg_name_u, *pkgs; + char *saveptr, *p_ptr, *s_ptr; + int result, neg; + StrMap *pkgmap, *targetmap, *sectionmap; + + pkg_name = NULL; + pkg_descr = NULL; + pkg_section = NULL; + pkg_url = NULL; + pkg_depends = NULL; + pkg_flavours = NULL; + pkg_choices = NULL; + pkg_subpkgs = NULL; + pkg_target_depends = NULL; + pkg_host_depends = NULL; + pkg_cxx = NULL; + pkg_dflt = NULL; + pkg_cfline = NULL; + pkg_multi = NULL; + + p_ptr = NULL; + s_ptr = NULL; + + unlink("package/Config.in.auto"); + /* open global sectionfile */ + menuglobal = fopen("package/Config.in.auto.global", "w"); + if (menuglobal == NULL) + fatal_error("global section file not writable."); + + /* read section list and create a hash table */ + section = fopen("package/section.lst", "r"); + if (section == NULL) + fatal_error("section listfile is missing"); + + sectionmap = strmap_new(HASHSZ); + while (fgets(tbuf, MAXPATH, section) != NULL) { + key = strtok(tbuf, "\t"); + value = strtok(NULL, "\t"); + strmap_put(sectionmap, key, value); + } + fclose(section); + + /* read target list and create a hash table */ + target = fopen("target/target.lst", "r"); + if (target == NULL) + fatal_error("target listfile is missing."); + + targetmap = strmap_new(HASHSZ); + while (fgets(tbuf, MAXPATH, target) != NULL) { + key = strtok(tbuf, "\t"); + value = strtok(NULL, "\t"); + strmap_put(targetmap, key, value); + } + fclose(target); + + if (mkdir("package/pkgconfigs.d", S_IRWXU) > 0) + fatal_error("creation of package/pkgconfigs.d failed."); + if (mkdir("package/pkglist.d", S_IRWXU) > 0) + fatal_error("creation of package/pkglist.d failed."); + + /* read Makefile's for all packages */ + pkgdir = opendir("package"); + while ((pkgdirp = readdir(pkgdir)) != NULL) { + /* skip dotfiles */ + if (strncmp(pkgdirp->d_name, ".", 1) > 0) { + if (snprintf(path, MAXPATH, "package/%s/Makefile", pkgdirp->d_name) < 0) + fatal_error("can not create path variable."); + pkg = fopen(path, "r"); + if (pkg == NULL) + continue; + + /* skip manually maintained packages */ + if (snprintf(path, MAXPATH, "package/%s/Config.in.manual", pkgdirp->d_name) < 0) + fatal_error("can not create path variable."); + if (!access(path, F_OK)) { + while (fgets(buf, MAXPATH, pkg) != NULL) { + if ((parse_var(buf, "PKG_SECTION", NULL, &pkg_section)) == 0) + continue; + } + + memset(hvalue, 0 , MAXVALUE); + result = strmap_get(sectionmap, pkg_section, hvalue, sizeof(hvalue)); + if (result == 1) { + if (snprintf(spath, MAXPATH, "package/pkglist.d/sectionlst.%d", hash_str(hvalue)) < 0) + fatal_error("can not create path variable."); + section = fopen(spath, "a"); + if (section != NULL) { + fprintf(section, "%s@\n", pkgdirp->d_name); + fclose(section); + } + } else + fatal_error("Can not find section description for package."); + + fclose(pkg); + continue; + } + + nobinpkgs = 0; + + /* create output directories */ + if (snprintf(dir, MAXPATH, "package/pkgconfigs.d/%s", pkgdirp->d_name) < 0) + fatal_error("can not create dir variable."); + if (mkdir(dir, S_IRWXU) > 0) + fatal_error("can not create directory."); + + /* allocate memory */ + hkey = malloc(MAXVAR); + memset(hkey, 0, MAXVAR); + memset(variable, 0, 2*MAXVAR); + + pkgmap = strmap_new(HASHSZ); + + /* parse package Makefile */ + while (fgets(buf, MAXPATH, pkg) != NULL) { + /* just read variables prefixed with PKG */ + if (strncmp(buf, "PKG", 3) == 0) { + if ((parse_var(buf, "PKG_NAME", NULL, &pkg_name)) == 0) + continue; + if (pkg_name != NULL) + pkg_name_u = toupperstr(pkg_name); + else + pkg_name_u = toupperstr(pkgdirp->d_name); + + snprintf(variable, MAXVAR, "PKG_CFLINE_%s", pkg_name_u); + if ((parse_var(buf, variable, pkg_cfline, &pkg_cfline)) == 0) + continue; + snprintf(variable, MAXVAR, "PKG_DFLT_%s", pkg_name_u); + if ((parse_var(buf, variable, NULL, &pkg_dflt)) == 0) + continue; + if ((parse_var(buf, "PKG_HOST_DEPENDS", NULL, &pkg_host_depends)) == 0) + continue; + if ((parse_var(buf, "PKG_TARGET_DEPENDS", NULL, &pkg_target_depends)) == 0) + continue; + if ((parse_var(buf, "PKG_DESCR", NULL, &pkg_descr)) == 0) + continue; + if ((parse_var(buf, "PKG_SECTION", NULL, &pkg_section)) == 0) + continue; + if ((parse_var(buf, "PKG_URL", NULL, &pkg_url)) == 0) + continue; + if ((parse_var(buf, "PKG_CXX", NULL, &pkg_cxx)) == 0) + continue; + if ((parse_var(buf, "PKG_MULTI", NULL, &pkg_multi)) == 0) + continue; + if ((parse_var(buf, "PKG_DEPENDS", pkg_depends, &pkg_depends)) == 0) + continue; + if ((parse_var(buf, "PKG_FLAVOURS", pkg_flavours, &pkg_flavours)) == 0) + continue; + if ((parse_var_hash(buf, "PKGFD_", pkgmap)) == 0) + continue; + if ((parse_var_hash(buf, "PKGFS_", pkgmap)) == 0) + continue; + if ((parse_var(buf, "PKG_CHOICES", pkg_choices, &pkg_choices)) == 0) + continue; + if ((parse_var_hash(buf, "PKGCD_", pkgmap)) == 0) + continue; + if ((parse_var_hash(buf, "PKGCS_", pkgmap)) == 0) + continue; + if ((parse_var(buf, "PKG_SUBPKGS", pkg_subpkgs, &pkg_subpkgs)) == 0) + continue; + if ((parse_var_hash(buf, "PKGSD_", pkgmap)) == 0) + continue; + if ((parse_var_hash(buf, "PKGSS_", pkgmap)) == 0) + continue; + if ((parse_var_hash(buf, "PKGSC_", pkgmap)) == 0) + continue; + } + } + + /* end of package Makefile parsing */ + if (fclose(pkg) != 0) + perror("Failed to close file stream for Makefile"); + + /* + if (pkg_name != NULL) + fprintf(stderr, "Package name is %s\n", pkg_name); + if (pkg_section != NULL) + fprintf(stderr, "Package section is %s\n", pkg_section); + if (pkg_descr != NULL) + fprintf(stderr, "Package description is %s\n", pkg_descr); + if (pkg_depends != NULL) + fprintf(stderr, "Package dependencies are %s\n", pkg_depends); + if (pkg_subpkgs != NULL) + fprintf(stderr, "Package subpackages are %s\n", pkg_subpkgs); + if (pkg_flavours != NULL) + fprintf(stderr, "Package flavours are %s\n", pkg_flavours); + if (pkg_choices != NULL) + fprintf(stderr, "Package choices are %s\n", pkg_choices); + if (pkg_url != NULL) + fprintf(stderr, "Package homepage is %s\n", pkg_url); + if (pkg_cfline != NULL) + fprintf(stderr, "Package cfline is %s\n", pkg_cfline); + if (pkg_multi != NULL) + fprintf(stderr, "Package multi is %s\n", pkg_multi); + + strmap_enum(pkgmap, iter_debug, NULL); + */ + + /* generate master source Config.in file */ + if (snprintf(path, MAXPATH, "package/pkgconfigs.d/%s/Config.in", pkgdirp->d_name) < 0) + fatal_error("path variable creation failed."); + fprintf(menuglobal, "source \"%s\"\n", path); + /* recreating file is faster than truncating with w+ */ + unlink(path); + cfg = fopen(path, "w"); + if (cfg == NULL) + continue; + + pkgs = NULL; + if (pkg_subpkgs != NULL) + pkgs = strdup(pkg_subpkgs); + + fprintf(cfg, "config ADK_COMPILE_%s\n", toupperstr(pkgdirp->d_name)); + fprintf(cfg, "\ttristate\n"); + if (nobinpkgs == 0) { + fprintf(cfg, "\tdepends on "); + if (pkgs != NULL) { + if (pkg_multi != NULL) + if (strncmp(pkg_multi, "1", 1) == 0) + fprintf(cfg, "ADK_HAVE_DOT_CONFIG || "); + token = strtok(pkgs, " "); + fprintf(cfg, "ADK_PACKAGE_%s", token); + token = strtok(NULL, " "); + while (token != NULL) { + fprintf(cfg, " || ADK_PACKAGE_%s", token); + token = strtok(NULL, " "); + } + fprintf(cfg, "\n"); + } else { + fprintf(cfg, "ADK_PACKAGE_%s\n", toupperstr(pkgdirp->d_name)); + } + } + //else { + // fprintf(cfg, "\tprompt \"%s\"\n", pkgdirp->d_name); + //} + fprintf(cfg, "\tdefault n\n"); + fclose(cfg); + free(pkgs); + + /* skip packages without binary package output */ + if (nobinpkgs == 1) + continue; + + /* generate binary package specific Config.in files */ + if (pkg_subpkgs != NULL) + packages = tolowerstr(pkg_subpkgs); + else + packages = strdup(pkgdirp->d_name); + + token = strtok_r(packages, " ", &p_ptr); + while (token != NULL) { + strncat(hkey, "PKGSC_", 6); + strncat(hkey, toupperstr(token), strlen(token)); + memset(hvalue, 0 , MAXVALUE); + result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue)); + memset(hkey, 0 , MAXVAR); + if (result == 1) + pkg_section = strdup(hvalue); + + strncat(hkey, "PKGSD_", 6); + strncat(hkey, toupperstr(token), strlen(token)); + memset(hvalue, 0 , MAXVALUE); + result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue)); + memset(hkey, 0 , MAXVAR); + if (result == 1) + pkg_descr = strdup(hvalue); + + pseudo_name = malloc(MAXLINE); + memset(pseudo_name, 0, MAXLINE); + strncat(pseudo_name, token, strlen(token)); + while (strlen(pseudo_name) < 20) + strncat(pseudo_name, ".", 1); + + if (snprintf(path, MAXPATH, "package/pkgconfigs.d/%s/Config.in.%s", pkgdirp->d_name, token) < 0) + fatal_error("failed to create path variable."); + + /* create temporary section files */ + memset(hvalue, 0 , MAXVALUE); + result = strmap_get(sectionmap, pkg_section, hvalue, sizeof(hvalue)); + if (result == 1) { + if (snprintf(spath, MAXPATH, "package/pkglist.d/sectionlst.%d", hash_str(hvalue)) < 0) + fatal_error("failed to create path variable."); + section = fopen(spath, "a"); + if (section != NULL) { + fprintf(section, "%s|%s\n", token, pkgdirp->d_name); + fclose(section); + } + } else + fatal_error("Can not find section description for package"); + + unlink(path); + cfg = fopen(path, "w"); + if (cfg == NULL) + perror("Can not open Config.in file"); + + fprintf(cfg, "config ADK_PACKAGE_%s\n", toupperstr(token)); + fprintf(cfg, "\tprompt \"%s... %s\"\n", pseudo_name, pkg_descr); + fprintf(cfg, "\ttristate\n"); + + free(pseudo_name); + + /* print custom cf line */ + if (pkg_cfline != NULL) { + cftoken = strtok_r(pkg_cfline, "@", &saveptr); + while (cftoken != NULL) { + fprintf(cfg, "\t%s\n", cftoken); + cftoken = strtok_r(NULL, "@", &saveptr); + } + } + + /* add sub package dependencies */ + strncat(hkey, "PKGSS_", 6); + strncat(hkey, toupperstr(token), strlen(token)); + memset(hvalue, 0, MAXVALUE); + result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue)); + if (result == 1) { + val = strtok_r(hvalue, " ", &saveptr); + while (val != NULL) { + fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(val)); + val = strtok_r(NULL, " ", &saveptr); + } + } + memset(hkey, 0, MAXVAR); + + /* create package host dependency information */ + if (pkg_host_depends != NULL) { + token = strtok(pkg_host_depends, " "); + fprintf(cfg, "\tdepends on "); + sp = ""; + while (token != NULL) { + if(strncmp(token, "!", 1) == 0) { + fprintf(cfg, "%s!ADK_HOST%s", sp, toupperstr(token)); + sp = " && "; + } else { + fprintf(cfg, "%sADK_HOST%s", sp, toupperstr(token)); + sp = " || "; + } + token = strtok(NULL, " "); + } + fprintf(cfg, "\n"); + } + + /* create package target dependency information */ + if (pkg_target_depends != NULL) { + token = strtok(pkg_target_depends, " "); + neg = 0; + sp = ""; + fprintf(cfg, "\tdepends on "); + memset(hvalue, 0, MAXVALUE); + while (token != NULL) { + if (strncmp(token, "!", 1) == 0) { + result = strmap_get(targetmap, token+1, hvalue, sizeof(hvalue)); + neg = 1; + } else { + result = strmap_get(targetmap, token, hvalue, sizeof(hvalue)); + } + hvalue[strlen(hvalue)-1] = '\0'; + print_target_depline(hvalue, neg, sp, cfg); + if (neg == 1) + sp = " && "; + else + sp = " || "; + token = strtok(NULL, " "); + } + fprintf(cfg, "\n"); + } + + /* create package dependency information */ + if (pkg_depends != NULL) { + token = strtok(pkg_depends, " "); + while (token != NULL) { + if (strncmp(token, "kmod", 4) == 0) + fprintf(cfg, "\tselect ADK_KPACKAGE_%s\n", toupperstr(token)); + else + fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(token)); + token = strtok(NULL, " "); + } + free(pkg_depends); + pkg_depends = NULL; + } + + fprintf(cfg, "\tselect ADK_COMPILE_%s\n", toupperstr(pkgdirp->d_name)); + + if (pkg_dflt != NULL) + fprintf(cfg, "\tdefault %s\n", pkg_dflt); + else + fprintf(cfg, "\tdefault n\n"); + + fprintf(cfg, "\thelp\n"); + fprintf(cfg, "\t %s\n\n", pkg_descr); + if (pkg_url != NULL) + fprintf(cfg, "\t WWW: %s\n", pkg_url); + + /* handle special C++ packages */ + if (pkg_cxx != NULL) { + fprintf(cfg, "\nchoice\n"); + fprintf(cfg, "prompt \"C++ library to use\"\n"); + fprintf(cfg, "depends on ADK_COMPILE_%s\n\n", toupperstr(pkgdirp->d_name)); + fprintf(cfg, "default ADK_COMPILE_%s_WITH_STDCXX if ADK_TARGET_LIB_GLIBC || ADK_TARGET_LIB_EGLIBC\n", pkg_cxx); + fprintf(cfg, "default ADK_COMPILE_%s_WITH_UCLIBCXX if ADK_TARGET_LIB_UCLIBC\n\n", pkg_cxx); + fprintf(cfg, "config ADK_COMPILE_%s_WITH_STDCXX\n", pkg_cxx); + fprintf(cfg, "\tbool \"GNU C++ library\"\n"); + fprintf(cfg, "\tselect ADK_PACKAGE_LIBSTDCXX\n\n"); + fprintf(cfg, "config ADK_COMPILE_%s_WITH_UCLIBCXX\n", pkg_cxx); + fprintf(cfg, "\tbool \"uClibc++ library\"\n"); + fprintf(cfg, "\tselect ADK_PACKAGE_UCLIBCXX\n\n"); + fprintf(cfg, "endchoice\n"); + free(pkg_cxx); + pkg_cxx = NULL; + } + + /* package flavours */ + if (pkg_flavours != NULL) { + token = strtok(pkg_flavours, " "); + while (token != NULL) { + fprintf(cfg, "\nconfig ADK_PACKAGE_%s_%s\n", toupperstr(pkgdirp->d_name), + toupperstr(token)); + fprintf(cfg, "\tbool "); + strncat(hkey, "PKGFD_", 6); + strncat(hkey, token, strlen(token)); + memset(hvalue, 0 , MAXVALUE); + strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue)); + memset(hkey, 0 , MAXVAR); + pkg_fd = strdup(hvalue); + + fprintf(cfg, "\"%s\"\n", pkg_fd); + fprintf(cfg, "\tdefault n\n"); + fprintf(cfg, "\tdepends on ADK_COMPILE_%s\n", toupperstr(pkgdirp->d_name)); + strncat(hkey, "PKGFS_", 6); + strncat(hkey, token, strlen(token)); + + result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue)); + if (result == 1) { + val = strtok_r(hvalue, " ", &saveptr); + while (val != NULL) { + fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(val)); + val = strtok_r(NULL, " ", &saveptr); + } + } + memset(hkey, 0, MAXVAR); + fprintf(cfg, "\thelp\n"); + fprintf(cfg, "\t %s\n", pkg_fd); + token = strtok(NULL, " "); + } + free(pkg_flavours); + pkg_flavours = NULL; + } + + /* package choices */ + if (pkg_choices != NULL) { + fprintf(cfg, "\nchoice\n"); + fprintf(cfg, "prompt \"Package flavour choice\"\n"); + fprintf(cfg, "depends on ADK_COMPILE_%s\n\n", toupperstr(pkgdirp->d_name)); + token = strtok(pkg_choices, " "); + while (token != NULL) { + fprintf(cfg, "config ADK_PACKAGE_%s_%s\n", toupperstr(pkgdirp->d_name), + toupperstr(token)); + + fprintf(cfg, "\tbool "); + strncat(hkey, "PKGCD_", 6); + strncat(hkey, token, strlen(token)); + memset(hvalue, 0 , MAXVALUE); + strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue)); + memset(hkey, 0 , MAXVAR); + fprintf(cfg, "\"%s\"\n", hvalue); + + strncat(hkey, "PKGCS_", 6); + strncat(hkey, token, strlen(token)); + memset(hvalue, 0, MAXVALUE); + result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue)); + if (result == 1) { + val = strtok_r(hvalue, " ", &saveptr); + while (val != NULL) { + fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(val)); + val = strtok_r(NULL, " ", &saveptr); + } + } + memset(hkey, 0, MAXVAR); + token = strtok(NULL, " "); + } + fprintf(cfg, "\nendchoice\n"); + free(pkg_choices); + pkg_choices = NULL; + } + /* close file descriptor, parse next package */ + fclose(cfg); + token = strtok_r(NULL, " ", &p_ptr); + } + + /* end of package output generation */ + free(packages); + packages = NULL; + + /* reset flags, free memory */ + free(pkg_name); + free(pkg_descr); + free(pkg_section); + free(pkg_url); + free(pkg_depends); + free(pkg_flavours); + free(pkg_choices); + free(pkg_subpkgs); + free(pkg_target_depends); + free(pkg_host_depends); + free(pkg_cxx); + free(pkg_dflt); + free(pkg_cfline); + free(pkg_multi); + pkg_name = NULL; + pkg_descr = NULL; + pkg_section = NULL; + pkg_url = NULL; + pkg_depends = NULL; + pkg_flavours = NULL; + pkg_choices = NULL; + pkg_subpkgs = NULL; + pkg_target_depends = NULL; + pkg_host_depends = NULL; + pkg_cxx = NULL; + pkg_dflt = NULL; + pkg_cfline = NULL; + pkg_multi = NULL; + + strmap_delete(pkgmap); + nobinpkgs = 0; + free(hkey); + } + } + + + /* create Config.in.auto */ + strmap_enum(sectionmap, iter, NULL); + + strmap_delete(targetmap); + strmap_delete(sectionmap); + fclose(menuglobal); + closedir(pkgdir); + + /* remove temporary section files */ + pkglistdir = opendir("package/pkglist.d"); + while ((pkgdirp = readdir(pkglistdir)) != NULL) { + if (strncmp(pkgdirp->d_name, "sectionlst.", 11) == 0) { + if (snprintf(path, MAXPATH, "package/pkglist.d/%s", pkgdirp->d_name) < 0) + fatal_error("creating path variable failed."); + if (unlink(path) < 0) + fatal_error("removing file failed."); + } + } + closedir(pkglistdir); + return(0); +} diff --git a/tools/adk/sortfile.c b/tools/adk/sortfile.c new file mode 100644 index 000000000..1e9fc9623 --- /dev/null +++ b/tools/adk/sortfile.c @@ -0,0 +1,153 @@ +/*- + * Copyright (c) 2010 + * Thorsten Glaser + * + * Provided that these terms and disclaimer and all copyright notices + * are retained or reproduced in an accompanying document, permission + * is granted to deal in this work without restriction, including un- + * limited rights to use, publicly perform, distribute, sell, modify, + * merge, give away, or sublicence. + * + * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to + * the utmost extent permitted by applicable law, neither express nor + * implied; without malicious intent or gross negligence. In no event + * may a licensor, author or contributor be held liable for indirect, + * direct, other damage, loss, or other issues arising in any way out + * of dealing in the work, even if advised of the possibility of such + * damage or existence of a defect, except proven that it results out + * of said person's immediate fault when using the work as intended. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct ptrsize { + const char *ptr; + size_t size; +}; + +static void *xrecalloc(void *, size_t, size_t); +static int cmpfn(const void *, const void *); + +#define MUL_NO_OVERFLOW (1UL << (sizeof (size_t) * 8 / 2)) + +#ifndef SIZE_MAX +#ifdef SIZE_T_MAX +#define SIZE_MAX SIZE_T_MAX +#else +#define SIZE_MAX ((size_t)-1) +#endif +#endif + +#if !defined(MAP_FAILED) +/* XXX imake style */ +# if defined(__linux) +#define MAP_FAILED ((void *)-1) +# elif defined(__bsdi__) || defined(__osf__) || defined(__ultrix) +#define MAP_FAILED ((caddr_t)-1) +# endif +#endif + +static void * +xrecalloc(void *ptr, size_t nmemb, size_t size) +{ + void *rv; + + if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && + nmemb > 0 && SIZE_MAX / nmemb < size) + errx(1, "attempted integer overflow: %zu * %zu", nmemb, size); + size *= nmemb; + if ((rv = realloc(ptr, size)) == NULL) + err(1, "cannot allocate %zu bytes", size); + return (rv); +} + +int +sortfile(char *infile, char *outfile) +{ + int fd, fdout; + size_t fsz, asz, anents; + char *cp, *thefile, *endfile; + struct ptrsize *thearray; + + if ((fd = open(infile, O_RDONLY)) < 0) + err(1, "open: %s", infile); + else { + struct stat sb; + + /* reasonable maximum size: 3/4 of SIZE_MAX */ + fsz = (SIZE_MAX / 2) + (SIZE_MAX / 4); + + if (fstat(fd, &sb)) + err(1, "stat: %s", infile); + if (sb.st_size > fsz) + errx(1, "file %s too big, %llu > %zu", infile, + (unsigned long long)sb.st_size, fsz); + fsz = (size_t)sb.st_size; + } + + if ((thefile = mmap(NULL, fsz, PROT_READ, MAP_FILE | MAP_PRIVATE, + fd, (off_t)0)) == MAP_FAILED) + err(1, "mmap %zu bytes from %s", fsz, infile); + /* last valid byte in the file, must be newline anyway */ + endfile = thefile + fsz - 1; + + thearray = xrecalloc(NULL, (asz = 8), sizeof(thearray[0])); + thearray[(anents = 0)].ptr = cp = thefile; + + while ((cp = memchr(cp, '\n', endfile - cp)) != NULL) { + /* byte after the \n */ + if (++cp > endfile) + /* end of file */ + break; + thearray[anents].size = cp - thearray[anents].ptr; + if (++anents == asz) + /* resize array */ + thearray = xrecalloc(thearray, (asz <<= 1), + sizeof(thearray[0])); + thearray[anents].ptr = cp; + } + thearray[anents].size = endfile - thearray[anents].ptr + 1; + + qsort(thearray, ++anents, sizeof(thearray[0]), cmpfn); + + if ((fdout = open(outfile, O_WRONLY | O_CREAT, S_IRWXU)) < 0) + err(1, "open: %s", outfile); + else { + for (asz = 0; asz < anents; ++asz) + if ((size_t)write(fdout, thearray[asz].ptr, + thearray[asz].size) != thearray[asz].size) + err(1, "write %zu bytes", thearray[asz].size); + } + + if (munmap(thefile, fsz)) + warn("munmap"); + + free(thearray); + close(fd); + + return (0); +} + +static int +cmpfn(const void *p1, const void *p2) +{ + int rv; + const struct ptrsize *a1 = (const struct ptrsize *)p1; + const struct ptrsize *a2 = (const struct ptrsize *)p2; + + if ((rv = memcmp(a1->ptr, a2->ptr, (a1->size > a2->size ? + a2->size : a1->size) - /* '\n' */ 1)) != 0) + /* unequal in the common part */ + return (rv); + + /* shorter string is smaller */ + return (a1->size > a2->size ? 1 : a1->size == a2->size ? 0 : -1); +} diff --git a/tools/adk/sortfile.h b/tools/adk/sortfile.h new file mode 100644 index 000000000..c54294e69 --- /dev/null +++ b/tools/adk/sortfile.h @@ -0,0 +1 @@ +int sortfile(char *infile, char *outfile); diff --git a/tools/adk/strmap.c b/tools/adk/strmap.c new file mode 100644 index 000000000..f2c660e1f --- /dev/null +++ b/tools/adk/strmap.c @@ -0,0 +1,510 @@ +/* + * strmap version 1.0.0 + * + * ANSI C hash table for strings. + * + * strmap.c + * + * Copyright (c) 2009 Per Ola Kristensson. + * + * Per Ola Kristensson + * Inference Group, Department of Physics + * University of Cambridge + * Cavendish Laboratory + * JJ Thomson Avenue + * CB3 0HE Cambridge + * United Kingdom + * + * strmap is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * strmap 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 strmap. If not, see . + */ +#include "strmap.h" + +typedef struct Pair Pair; + +typedef struct Bucket Bucket; + +struct Pair { + char *key; + char *value; +}; + +struct Bucket { + unsigned int count; + Pair *pairs; +}; + +struct StrMap { + unsigned int count; + Bucket *buckets; +}; + +static Pair * get_pair(Bucket *bucket, const char *key); +static unsigned long hash(const char *str); + +StrMap * strmap_new(unsigned int capacity) +{ + StrMap *map; + + map = malloc(sizeof(StrMap)); + if (map == NULL) { + return NULL; + } + map->count = capacity; + map->buckets = malloc(map->count * sizeof(Bucket)); + if (map->buckets == NULL) { + free(map); + return NULL; + } + memset(map->buckets, 0, map->count * sizeof(Bucket)); + return map; +} + +void strmap_delete(StrMap *map) +{ + unsigned int i, j, n, m; + Bucket *bucket; + Pair *pair; + + if (map == NULL) { + return; + } + n = map->count; + bucket = map->buckets; + i = 0; + while (i < n) { + m = bucket->count; + pair = bucket->pairs; + j = 0; + while(j < m) { + free(pair->key); + free(pair->value); + pair++; + j++; + } + free(bucket->pairs); + bucket++; + i++; + } + free(map->buckets); + free(map); +} + +int strmap_get(const StrMap *map, const char *key, char *out_buf, unsigned int n_out_buf) +{ + unsigned int index; + Bucket *bucket; + Pair *pair; + + if (map == NULL) { + return 0; + } + if (key == NULL) { + return 0; + } + index = hash(key) % map->count; + bucket = &(map->buckets[index]); + pair = get_pair(bucket, key); + if (pair == NULL) { + return 0; + } + if (out_buf == NULL && n_out_buf == 0) { + return strlen(pair->value) + 1; + } + if (out_buf == NULL) { + return 0; + } + if (strlen(pair->value) >= n_out_buf) { + return 0; + } + strcpy(out_buf, pair->value); + return 1; +} + +int strmap_exists(const StrMap *map, const char *key) +{ + unsigned int index; + Bucket *bucket; + Pair *pair; + + if (map == NULL) { + return 0; + } + if (key == NULL) { + return 0; + } + index = hash(key) % map->count; + bucket = &(map->buckets[index]); + pair = get_pair(bucket, key); + if (pair == NULL) { + return 0; + } + return 1; +} + +int strmap_put(StrMap *map, const char *key, const char *value) +{ + unsigned int key_len, value_len, index; + Bucket *bucket; + Pair *tmp_pairs, *pair; + char *tmp_value; + char *new_key, *new_value; + + if (map == NULL) { + return 0; + } + if (key == NULL || value == NULL) { + return 0; + } + key_len = strlen(key); + value_len = strlen(value); + /* Get a pointer to the bucket the key string hashes to */ + index = hash(key) % map->count; + bucket = &(map->buckets[index]); + /* Check if we can handle insertion by simply replacing + * an existing value in a key-value pair in the bucket. + */ + if ((pair = get_pair(bucket, key)) != NULL) { + /* The bucket contains a pair that matches the provided key, + * change the value for that pair to the new value. + */ + if (strlen(pair->value) < value_len) { + /* If the new value is larger than the old value, re-allocate + * space for the new larger value. + */ + tmp_value = realloc(pair->value, (value_len + 1) * sizeof(char)); + if (tmp_value == NULL) { + return 0; + } + pair->value = tmp_value; + } + /* Copy the new value into the pair that matches the key */ + strcpy(pair->value, value); + return 1; + } + /* Allocate space for a new key and value */ + new_key = malloc((key_len + 1) * sizeof(char)); + if (new_key == NULL) { + return 0; + } + new_value = malloc((value_len + 1) * sizeof(char)); + if (new_value == NULL) { + free(new_key); + return 0; + } + /* Create a key-value pair */ + if (bucket->count == 0) { + /* The bucket is empty, lazily allocate space for a single + * key-value pair. + */ + bucket->pairs = malloc(sizeof(Pair)); + if (bucket->pairs == NULL) { + free(new_key); + free(new_value); + return 0; + } + bucket->count = 1; + } + else { + /* The bucket wasn't empty but no pair existed that matches the provided + * key, so create a new key-value pair. + */ + tmp_pairs = realloc(bucket->pairs, (bucket->count + 1) * sizeof(Pair)); + if (tmp_pairs == NULL) { + free(new_key); + free(new_value); + return 0; + } + bucket->pairs = tmp_pairs; + bucket->count++; + } + /* Get the last pair in the chain for the bucket */ + pair = &(bucket->pairs[bucket->count - 1]); + pair->key = new_key; + pair->value = new_value; + /* Copy the key and its value into the key-value pair */ + strcpy(pair->key, key); + strcpy(pair->value, value); + return 1; +} + +int strmap_get_count(const StrMap *map) +{ + unsigned int i, j, n, m; + unsigned int count; + Bucket *bucket; + Pair *pair; + + if (map == NULL) { + return 0; + } + bucket = map->buckets; + n = map->count; + i = 0; + count = 0; + while (i < n) { + pair = bucket->pairs; + m = bucket->count; + j = 0; + while (j < m) { + count++; + pair++; + j++; + } + bucket++; + i++; + } + return count; +} + +int strmap_enum(const StrMap *map, strmap_enum_func enum_func, const void *obj) +{ + unsigned int i, j, n, m; + Bucket *bucket; + Pair *pair; + + if (map == NULL) { + return 0; + } + if (enum_func == NULL) { + return 0; + } + bucket = map->buckets; + n = map->count; + i = 0; + while (i < n) { + pair = bucket->pairs; + m = bucket->count; + j = 0; + while (j < m) { + enum_func(pair->key, pair->value, obj); + pair++; + j++; + } + bucket++; + i++; + } + return 1; +} + +/* + * Returns a pair from the bucket that matches the provided key, + * or null if no such pair exist. + */ +static Pair * get_pair(Bucket *bucket, const char *key) +{ + unsigned int i, n; + Pair *pair; + + n = bucket->count; + if (n == 0) { + return NULL; + } + pair = bucket->pairs; + i = 0; + while (i < n) { + if (pair->key != NULL && pair->value != NULL) { + if (strcmp(pair->key, key) == 0) { + return pair; + } + } + pair++; + i++; + } + return NULL; +} + +/* + * Returns a hash code for the provided string. + */ +static unsigned long hash(const char *str) +{ + unsigned long hash = 5381; + int c; + c = 0; + + while (c == *str++) { + hash = ((hash << 5) + hash) + c; + } + return hash; +} + +/* + + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. + +*/ diff --git a/tools/adk/strmap.h b/tools/adk/strmap.h new file mode 100644 index 000000000..99687b236 --- /dev/null +++ b/tools/adk/strmap.h @@ -0,0 +1,350 @@ +/* + * strmap version 1.0.0 + * + * ANSI C hash table for strings. + * + * strmap.h + * + * Copyright (c) 2009 Per Ola Kristensson. + * + * Per Ola Kristensson + * Inference Group, Department of Physics + * University of Cambridge + * Cavendish Laboratory + * JJ Thomson Avenue + * CB3 0HE Cambridge + * United Kingdom + * + * strmap is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * strmap 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 strmap. If not, see . + */ +#ifndef _STRMAP_H_ +#define _STRMAP_H_ + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include +#include + +typedef struct StrMap StrMap; + +/* + * This callback function is called once per key-value when enumerating + * all keys associated to values. + * + * Parameters: + * + * key: A pointer to a null-terminated C string. The string must not + * be modified by the client. + * + * value: A pointer to a null-terminated C string. The string must + * not be modified by the client. + * + * obj: A pointer to a client-specific object. This parameter may be + * null. + * + * Return value: None. + */ +typedef void(*strmap_enum_func)(const char *key, const char *value, const void *obj); + +/* + * Creates a string map. + * + * Parameters: + * + * capacity: The number of top-level slots this string map + * should allocate. This parameter must be > 0. + * + * Return value: A pointer to a string map object, + * or null if a new string map could not be allocated. + */ +StrMap * strmap_new(unsigned int capacity); + +/* + * Releases all memory held by a string map object. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * If the supplied string map has been previously released, the + * behaviour of this function is undefined. + * + * Return value: None. + */ +void strmap_delete(StrMap *map); + +/* + * Returns the value associated with the supplied key. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * out_buf: A pointer to an output buffer which will contain the value, + * if it exists and fits into the buffer. + * + * n_out_buf: The size of the output buffer in bytes. + * + * Return value: If out_buf is set to null and n_out_buf is set to 0 the return + * value will be the number of bytes required to store the value (if it exists) + * and its null-terminator. For all other parameter configurations the return value + * is 1 if an associated value was found and completely copied into the output buffer, + * 0 otherwise. + */ +int strmap_get(const StrMap *map, const char *key, char *out_buf, unsigned int n_out_buf); + +/* + * Queries the existence of a key. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * Return value: 1 if the key exists, 0 otherwise. + */ +int strmap_exists(const StrMap *map, const char *key); + +/* + * Associates a value with the supplied key. If the key is already + * associated with a value, the previous value is replaced. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * + * key: A pointer to a null-terminated C string. This parameter + * cannot be null. The string must have a string length > 0. The + * string will be copied. + * + * value: A pointer to a null-terminated C string. This parameter + * cannot be null. The string must have a string length > 0. The + * string will be copied. + * + * Return value: 1 if the association succeeded, 0 otherwise. + */ +int strmap_put(StrMap *map, const char *key, const char *value); + +/* + * Returns the number of associations between keys and values. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * + * Return value: The number of associations between keys and values. + */ +int strmap_get_count(const StrMap *map); + +/* + * Enumerates all associations between keys and values. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * + * enum_func: A pointer to a callback function that will be + * called by this procedure once for every key associated + * with a value. This parameter cannot be null. + * + * obj: A pointer to a client-specific object. This parameter will be + * passed back to the client's callback function. This parameter can + * be null. + * + * Return value: 1 if enumeration completed, 0 otherwise. + */ +int strmap_enum(const StrMap *map, strmap_enum_func enum_func, const void *obj); + +#ifdef __cplusplus +} +#endif + +#endif + +/* + + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. + +*/ -- cgit v1.2.3