Commit 3b200f89 authored by Rusty Russell's avatar Rusty Russell

tools: fix ctype.h and string usage.

Enable CCAN_STR_DEBUG in the default flags, so our tools get checked,
and fix up the resulting errors.
parent 02bdd9ac
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
# SRCFILES += $(wildcard ccan/*/*.c) # SRCFILES += $(wildcard ccan/*/*.c)
#CCAN_CFLAGS=-g -O3 -Wall -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes -Wmissing-declarations #CCAN_CFLAGS=-g -O3 -Wall -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes -Wmissing-declarations
CCAN_CFLAGS=-g -Wall -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes -Wmissing-declarations CCAN_CFLAGS=-g -Wall -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes -Wmissing-declarations -DCCAN_STR_DEBUG=1
CFLAGS = $(CCAN_CFLAGS) -I. $(DEPGEN) -Werror CFLAGS = $(CCAN_CFLAGS) -I. $(DEPGEN) -Werror
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
#define CCAN_CONFIG_H #define CCAN_CONFIG_H
#define _GNU_SOURCE /* Always use GNU extensions. */ #define _GNU_SOURCE /* Always use GNU extensions. */
#define CCAN_COMPILER "cc" #define CCAN_COMPILER "cc"
#define CCAN_CFLAGS "-g -Wall -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes -Wmissing-declarations" #define CCAN_CFLAGS "-g -Wall -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes -Wmissing-declarations -DCCAN_STR_DEBUG=1"
#define HAVE_ALIGNOF 1 #define HAVE_ALIGNOF 1
#define HAVE_ASPRINTF 1 #define HAVE_ASPRINTF 1
......
ALL_TOOLS = tools/configurator/configurator tools/ccan_depends tools/doc_extract tools/namespacize tools/ccanlint/ccanlint ALL_TOOLS = tools/configurator/configurator tools/ccan_depends tools/doc_extract tools/namespacize tools/ccanlint/ccanlint
DEP_OBJS = tools/depends.o tools/compile.o tools/tools.o ccan/str_talloc/str_talloc.o ccan/str/str.o ccan/grab_file/grab_file.o ccan/talloc/talloc.o ccan/noerr/noerr.o ccan/read_write_all/read_write_all.o DEP_OBJS = tools/depends.o tools/compile.o tools/tools.o ccan/str_talloc/str_talloc.o ccan/str/str.o ccan/str/debug.o ccan/grab_file/grab_file.o ccan/talloc/talloc.o ccan/noerr/noerr.o ccan/read_write_all/read_write_all.o
.PHONY: tools .PHONY: tools
tools: $(ALL_TOOLS) tools: $(ALL_TOOLS)
......
...@@ -9,7 +9,7 @@ CORE_OBJS := tools/ccanlint/ccanlint.o \ ...@@ -9,7 +9,7 @@ CORE_OBJS := tools/ccanlint/ccanlint.o \
tools/tools.o \ tools/tools.o \
tools/compile.o \ tools/compile.o \
ccan/str_talloc/str_talloc.o ccan/grab_file/grab_file.o \ ccan/str_talloc/str_talloc.o ccan/grab_file/grab_file.o \
ccan/str/str.o \ ccan/str/str.o ccan/str/debug.o \
ccan/asort/asort.o \ ccan/asort/asort.o \
ccan/btree/btree.o \ ccan/btree/btree.o \
ccan/talloc/talloc.o ccan/noerr/noerr.o \ ccan/talloc/talloc.o ccan/noerr/noerr.o \
......
...@@ -499,7 +499,7 @@ static char *demangle_string(char *string) ...@@ -499,7 +499,7 @@ static char *demangle_string(char *string)
if (string[i] == '\\') { if (string[i] == '\\') {
char repl; char repl;
unsigned len = 0; unsigned len = 0;
char *p = strchr(mapfrom, string[i+1]); const char *p = strchr(mapfrom, string[i+1]);
if (p) { if (p) {
repl = mapto[p - mapfrom]; repl = mapto[p - mapfrom];
len = 1; len = 1;
...@@ -508,7 +508,7 @@ static char *demangle_string(char *string) ...@@ -508,7 +508,7 @@ static char *demangle_string(char *string)
repl = (string[i+2]-'0')*16 repl = (string[i+2]-'0')*16
+ string[i+3]-'0'; + string[i+3]-'0';
len = 3; len = 3;
} else if (isdigit(string[i+1])) { } else if (cisdigit(string[i+1])) {
repl = (string[i+2]-'0')*8*8 repl = (string[i+2]-'0')*8*8
+ (string[i+3]-'0')*8 + (string[i+3]-'0')*8
+ (string[i+4]-'0'); + (string[i+4]-'0');
......
...@@ -253,16 +253,17 @@ struct manifest *get_manifest(const void *ctx, const char *dir) ...@@ -253,16 +253,17 @@ struct manifest *get_manifest(const void *ctx, const char *dir)
/* We expect the ccan dir to be two levels above module dir. */ /* We expect the ccan dir to be two levels above module dir. */
if (!ccan_dir) { if (!ccan_dir) {
char *p; char *p, *dir;
ccan_dir = talloc_strdup(NULL, m->dir); dir = talloc_strdup(NULL, m->dir);
p = strrchr(ccan_dir, '/'); p = strrchr(dir, '/');
if (!p) if (!p)
errx(1, "I expect the ccan root directory in ../.."); errx(1, "I expect the ccan root directory in ../..");
*p = '\0'; *p = '\0';
p = strrchr(ccan_dir, '/'); p = strrchr(dir, '/');
if (!p) if (!p)
errx(1, "I expect the ccan root directory in ../.."); errx(1, "I expect the ccan root directory in ../..");
*p = '\0'; *p = '\0';
ccan_dir = dir;
} }
add_files(m, ""); add_files(m, "");
...@@ -361,7 +362,7 @@ bool get_token(const char **line, const char *token) ...@@ -361,7 +362,7 @@ bool get_token(const char **line, const char *token)
unsigned int toklen; unsigned int toklen;
*line += strspn(*line, " \t"); *line += strspn(*line, " \t");
if (isalnum(token[0]) || token[0] == '_') if (cisalnum(token[0]) || token[0] == '_')
toklen = strspn(*line, IDENT_CHARS); toklen = strspn(*line, IDENT_CHARS);
else { else {
/* FIXME: real tokenizer handles ++ and other multi-chars. */ /* FIXME: real tokenizer handles ++ and other multi-chars. */
......
...@@ -145,13 +145,13 @@ static char *start_main(char *ret, const char *why) ...@@ -145,13 +145,13 @@ static char *start_main(char *ret, const char *why)
static char *add_func(char *others, const char *line) static char *add_func(char *others, const char *line)
{ {
const char *p, *end = strchr(line, '(') - 1; const char *p, *end = strchr(line, '(') - 1;
while (isspace(*end)) { while (cisspace(*end)) {
end--; end--;
if (end == line) if (end == line)
return others; return others;
} }
for (p = end; isalnum(*p) || *p == '_'; p--) { for (p = end; cisalnum(*p) || *p == '_'; p--) {
if (p == line) if (p == line)
return others; return others;
} }
...@@ -188,7 +188,7 @@ static bool looks_internal(char **lines, char **why) ...@@ -188,7 +188,7 @@ static bool looks_internal(char **lines, char **why)
const char *line = lines[i] + strspn(lines[i], " \t"); const char *line = lines[i] + strspn(lines[i], " \t");
unsigned len = strspn(line, IDENT_CHARS); unsigned len = strspn(line, IDENT_CHARS);
if (!line[0] || isspace(line[0]) || strstarts(line, "//")) if (!line[0] || cisspace(line[0]) || strstarts(line, "//"))
continue; continue;
/* The winners. */ /* The winners. */
...@@ -228,7 +228,7 @@ static bool looks_internal(char **lines, char **why) ...@@ -228,7 +228,7 @@ static bool looks_internal(char **lines, char **why)
/* Single identifier then operator == inside function. */ /* Single identifier then operator == inside function. */
if (last_ended && len if (last_ended && len
&& ispunct(line[len+strspn(line+len, " ")])) { && cispunct(line[len+strspn(line+len, " ")])) {
*why = "starts with identifier then punctuation"; *why = "starts with identifier then punctuation";
return true; return true;
} }
...@@ -361,7 +361,7 @@ static char *mangle(struct manifest *m, char **lines) ...@@ -361,7 +361,7 @@ static char *mangle(struct manifest *m, char **lines)
} else { } else {
/* Character at start of line, with ( and no ; /* Character at start of line, with ( and no ;
* == function start. Ignore comments. */ * == function start. Ignore comments. */
if (!isspace(line[0]) if (!cisspace(line[0])
&& strchr(line, '(') && strchr(line, '(')
&& !strchr(line, ';') && !strchr(line, ';')
&& !strstr(line, "//")) { && !strstr(line, "//")) {
......
...@@ -36,10 +36,10 @@ static bool scan_forv(const void *ctx, ...@@ -36,10 +36,10 @@ static bool scan_forv(const void *ctx,
va_copy(ap, *args); va_copy(ap, *args);
if (isspace(fmt[0])) { if (cisspace(fmt[0])) {
/* One format space can swallow many input spaces */ /* One format space can swallow many input spaces */
ret = false; ret = false;
while (isspace(input[0])) { while (cisspace(input[0])) {
if (scan_forv(ctx, ++input, fmt+1, &ap)) { if (scan_forv(ctx, ++input, fmt+1, &ap)) {
ret = true; ret = true;
break; break;
......
...@@ -27,7 +27,7 @@ static void fix_name(char *name) ...@@ -27,7 +27,7 @@ static void fix_name(char *name)
unsigned int i; unsigned int i;
for (i = 0; name[i]; i++) { for (i = 0; name[i]; i++) {
if (isalnum(name[i])) if (cisalnum(name[i]))
name[i] = toupper(name[i]); name[i] = toupper(name[i]);
else else
name[i] = '_'; name[i] = '_';
......
...@@ -112,7 +112,7 @@ static void look_for_macros(char *contents, struct replace **repl) ...@@ -112,7 +112,7 @@ static void look_for_macros(char *contents, struct replace **repl)
for (p = contents; *p; p++) { for (p = contents; *p; p++) {
if (*p == '\n') if (*p == '\n')
state = LINESTART; state = LINESTART;
else if (!isspace(*p)) { else if (!cisspace(*p)) {
if (state == LINESTART && *p == '#') if (state == LINESTART && *p == '#')
state = HASH; state = HASH;
else if (state==HASH && !strncmp(p, "define", 6)) { else if (state==HASH && !strncmp(p, "define", 6)) {
...@@ -178,9 +178,9 @@ static char *get_statement(const void *ctx, char **p) ...@@ -178,9 +178,9 @@ static char *get_statement(const void *ctx, char **p)
return answer; return answer;
} }
/* Compress whitespace into a single ' ' */ /* Compress whitespace into a single ' ' */
if (isspace(c)) { if (cisspace(c)) {
c = ' '; c = ' ';
while (isspace((*p)[1])) while (cisspace((*p)[1]))
(*p)++; (*p)++;
} else if (c == '{' || c == '(' || c == '[') { } else if (c == '{' || c == '(' || c == '[') {
if (c == '(') if (c == '(')
...@@ -317,11 +317,11 @@ static char *find_word(char *f, const char *str) ...@@ -317,11 +317,11 @@ static char *find_word(char *f, const char *str)
while ((p = strstr(p, str)) != NULL) { while ((p = strstr(p, str)) != NULL) {
/* Check it's not in the middle of a word. */ /* Check it's not in the middle of a word. */
if (p > f && (isalnum(p[-1]) || p[-1] == '_')) { if (p > f && (cisalnum(p[-1]) || p[-1] == '_')) {
p++; p++;
continue; continue;
} }
if (isalnum(p[strlen(str)]) || p[strlen(str)] == '_') { if (cisalnum(p[strlen(str)]) || p[strlen(str)] == '_') {
p++; p++;
continue; continue;
} }
...@@ -351,7 +351,7 @@ static const char *rewrite_file(const char *filename, ...@@ -351,7 +351,7 @@ static const char *rewrite_file(const char *filename,
off = p - file; off = p - file;
memcpy(new, file, off); memcpy(new, file, off);
if (isupper(repl->string[0])) if (cisupper(repl->string[0]))
memcpy(new + off, "CCAN_", 5); memcpy(new + off, "CCAN_", 5);
else else
memcpy(new + off, "ccan_", 5); memcpy(new + off, "ccan_", 5);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment