]> git.ozlabs.org Git - ccan/blobdiff - tools/ccanlint/tests/reduce_features.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / tools / ccanlint / tests / reduce_features.c
index 39a2bde067a96dbc0072514f5633eebff39974aa..34a897061bb4fbe16c69800bf699f930d753502b 100644 (file)
@@ -2,10 +2,7 @@
 #include <tools/tools.h>
 #include <ccan/htable/htable_type.h>
 #include <ccan/foreach/foreach.h>
 #include <tools/tools.h>
 #include <ccan/htable/htable_type.h>
 #include <ccan/foreach/foreach.h>
-#include <ccan/talloc/talloc.h>
-#include <ccan/grab_file/grab_file.h>
 #include <ccan/str/str.h>
 #include <ccan/str/str.h>
-#include <ccan/str_talloc/str_talloc.h>
 #include <ccan/hash/hash.h>
 #include <ccan/read_write_all/read_write_all.h>
 #include <errno.h>
 #include <ccan/hash/hash.h>
 #include <ccan/read_write_all/read_write_all.h>
 #include <errno.h>
@@ -21,7 +18,7 @@ bool features_were_reduced;
 static const char *can_run(struct manifest *m)
 {
        if (!config_header)
 static const char *can_run(struct manifest *m)
 {
        if (!config_header)
-               return talloc_strdup(m, "Could not read config.h");
+               return tal_strdup(m, "Could not read config.h");
        return NULL;
 }
 
        return NULL;
 }
 
@@ -40,7 +37,20 @@ static bool option_cmp(const char *name1, const char *name2)
        return streq(name1, name2);
 }
 
        return streq(name1, name2);
 }
 
-HTABLE_DEFINE_TYPE(char, option_name, option_hash, option_cmp, option);
+HTABLE_DEFINE_TYPE(char, option_name, option_hash, option_cmp, htable_option);
+
+static struct htable_option *htable_option_new(void)
+{
+       struct htable_option *opts = malloc(sizeof(*opts));
+       htable_option_init(opts);
+       return opts;
+}
+
+static void htable_option_free(struct htable_option *opts)
+{
+       htable_option_clear(opts);
+       free(opts);
+}
 
 static unsigned int add_options(struct htable_option *opts,
                                struct pp_conditions *cond)
 
 static unsigned int add_options(struct htable_option *opts,
                                struct pp_conditions *cond)
@@ -73,7 +83,7 @@ static struct htable_option *get_used_options(struct manifest *m)
                        info = get_ccan_line_info(f);
                        struct pp_conditions *prev = NULL;
 
                        info = get_ccan_line_info(f);
                        struct pp_conditions *prev = NULL;
 
-                       for (i = 0; i < f->num_lines; i++) {
+                       for (i = 0; f->lines[i]; i++) {
                                if (info[i].cond && info[i].cond != prev) {
                                        num += add_options(opts, info[i].cond);
                                        prev = info[i].cond;
                                if (info[i].cond && info[i].cond != prev) {
                                        num += add_options(opts, info[i].cond);
                                        prev = info[i].cond;
@@ -91,11 +101,12 @@ static struct htable_option *get_used_options(struct manifest *m)
 
 static struct htable_option *get_config_options(struct manifest *m)
 {
 
 static struct htable_option *get_config_options(struct manifest *m)
 {
-       const char **lines = (const char **)strsplit(m, config_header, "\n");
+       const char **lines = (const char **)tal_strsplit(m, config_header, "\n",
+                                                        STR_EMPTY_OK);
        unsigned int i;
        struct htable_option *opts = htable_option_new();
 
        unsigned int i;
        struct htable_option *opts = htable_option_new();
 
-       for (i = 0; i < talloc_array_length(lines) - 1; i++) {
+       for (i = 0; i < tal_count(lines) - 1; i++) {
                char *sym;
 
                if (!get_token(&lines[i], "#"))
                char *sym;
 
                if (!get_token(&lines[i], "#"))
@@ -108,6 +119,9 @@ static struct htable_option *get_config_options(struct manifest *m)
                /* Don't override endian... */
                if (strends(sym, "_ENDIAN"))
                        continue;
                /* Don't override endian... */
                if (strends(sym, "_ENDIAN"))
                        continue;
+               /* Don't override HAVE_STRUCT_TIMESPEC. */
+               if (streq(sym, "HAVE_STRUCT_TIMESPEC"))
+                       continue;
                if (!get_token(&lines[i], "1"))
                        continue;
                htable_option_add(opts, sym);
                if (!get_token(&lines[i], "1"))
                        continue;
                htable_option_add(opts, sym);
@@ -116,8 +130,8 @@ static struct htable_option *get_config_options(struct manifest *m)
 }
 
 static void do_reduce_features(struct manifest *m,
 }
 
 static void do_reduce_features(struct manifest *m,
-                              bool keep,
-                              unsigned int *timeleft, struct score *score)
+                              unsigned int *timeleft UNNEEDED,
+                              struct score *score)
 {
        struct htable_option *options_used, *options_avail, *options;
        struct htable_option_iter i;
 {
        struct htable_option *options_used, *options_avail, *options;
        struct htable_option_iter i;
@@ -152,22 +166,22 @@ static void do_reduce_features(struct manifest *m,
                return;
 
        /* Now make our own config.h variant, with our own options. */
                return;
 
        /* Now make our own config.h variant, with our own options. */
-       hdr = talloc_strdup(m, "/* Modified by reduce_features */\n");
-       hdr = talloc_append_string(hdr, config_header);
+       hdr = tal_strcat(m, "/* Modified by reduce_features */\n",
+                        config_header);
        for (sym = htable_option_first(options, &i);
             sym;
             sym = htable_option_next(options, &i)) {
        for (sym = htable_option_first(options, &i);
             sym;
             sym = htable_option_next(options, &i)) {
-               hdr = talloc_asprintf_append
-                       (hdr, "#undef %s\n#define %s 0\n", sym, sym);
+               tal_append_fmt(&hdr, "#undef %s\n#define %s 0\n", sym, sym);
        }
        }
-       if (mkdir("reduced-features", 0700) != 0)
+       if (mkdir("reduced-features", 0700) != 0 && errno != EEXIST)
                err(1, "Creating reduced-features directory");
 
                err(1, "Creating reduced-features directory");
 
-       fd = open("reduced-features/config.h", O_EXCL|O_CREAT|O_RDWR, 0600);
+       fd = open("reduced-features/config.h", O_TRUNC|O_CREAT|O_RDWR, 0600);
        if (fd < 0)
                err(1, "Creating reduced-features/config.h");
        if (!write_all(fd, hdr, strlen(hdr)))
                err(1, "Writing reduced-features/config.h");
        if (fd < 0)
                err(1, "Creating reduced-features/config.h");
        if (!write_all(fd, hdr, strlen(hdr)))
                err(1, "Writing reduced-features/config.h");
+       htable_option_free(options);
        close(fd);
        features_were_reduced = true;
 }
        close(fd);
        features_were_reduced = true;
 }