aboutsummaryrefslogtreecommitdiffstats
path: root/src/mds-kbdc
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/mds-kbdc/builtin-functions.c26
-rw-r--r--src/mds-kbdc/callables.c33
-rw-r--r--src/mds-kbdc/compile-layout.c30
-rw-r--r--src/mds-kbdc/include-stack.c6
-rw-r--r--src/mds-kbdc/make-tree.c16
-rw-r--r--src/mds-kbdc/parsed.c5
-rw-r--r--src/mds-kbdc/paths.c2
-rw-r--r--src/mds-kbdc/process-includes.c2
-rw-r--r--src/mds-kbdc/raw-data.c3
-rw-r--r--src/mds-kbdc/tree.c2
10 files changed, 56 insertions, 69 deletions
diff --git a/src/mds-kbdc/builtin-functions.c b/src/mds-kbdc/builtin-functions.c
index fbff16f..7cd13b7 100644
--- a/src/mds-kbdc/builtin-functions.c
+++ b/src/mds-kbdc/builtin-functions.c
@@ -29,24 +29,24 @@
/**
* Define useful data for built-in function with 2 parameters
*/
-#define define_2 \
- const char32_t* restrict a = *args++; \
- const char32_t* restrict b = *args++; \
- size_t an = string_length(a); \
- size_t bn = string_length(b); \
- size_t i, n = an > bn ? an : bn; \
- char32_t* restrict rc = malloc((n + 1) * sizeof(char32_t)); \
- fail_if (rc == NULL); \
+#define define_2 \
+ const char32_t* restrict a = *args++; \
+ const char32_t* restrict b = *args++; \
+ size_t an = string_length(a); \
+ size_t bn = string_length(b); \
+ size_t i, n = an > bn ? an : bn; \
+ char32_t* restrict rc; \
+ fail_if (xmalloc(rc, n + 1, char32_t)); \
rc[n] = -1
/**
* Define useful data for built-in function with 1 parameter
*/
-#define define_1 \
- const char32_t* restrict a = *args++; \
- size_t i, n = string_length(a); \
- char32_t* restrict rc = malloc((n + 1) * sizeof(char32_t)); \
- fail_if (rc == NULL); \
+#define define_1 \
+ const char32_t* restrict a = *args++; \
+ size_t i, n = string_length(a); \
+ char32_t* restrict rc; \
+ fail_if (xmalloc(rc, n + 1, char32_t)); \
rc[n] = -1
/**
diff --git a/src/mds-kbdc/callables.c b/src/mds-kbdc/callables.c
index 4c984c1..f27f94f 100644
--- a/src/mds-kbdc/callables.c
+++ b/src/mds-kbdc/callables.c
@@ -100,27 +100,25 @@ void callables_terminate(void)
int callables_set(const char* restrict name, size_t arg_count, mds_kbdc_tree_t* restrict callable,
mds_kbdc_include_stack_t* restrict callable_include_stack)
{
-#define yrealloc(var, elements, type) \
- (new_##var = realloc(var, (elements) * sizeof(type)), \
- (new_##var == NULL) ? -1 : (var = new_##var, new_##var = NULL, 0))
+#define _yrealloc(var, elements, type) (yrealloc(tmp_##var, var, elements, type))
char* dupname = NULL;
- char*** new_names = NULL;
- size_t** new_callables = NULL;
- size_t* new_bucket_sizes = NULL;
+ char*** tmp_names = NULL;
+ size_t** tmp_callables = NULL;
+ size_t* tmp_bucket_sizes = NULL;
char** old_names = NULL;
size_t* old_callables = NULL;
- mds_kbdc_tree_t** new_callable_list = NULL;
- mds_kbdc_include_stack_t** new_callable_include_stack_list = NULL;
+ mds_kbdc_tree_t** tmp_callable_list = NULL;
+ mds_kbdc_include_stack_t** tmp_callable_include_stack_list = NULL;
int saved_errno;
- fail_if (dupname = strdup(name), dupname == NULL);
+ fail_if (xstrdup(dupname, name));
if (arg_count >= buckets)
{
- fail_if (yrealloc(names, arg_count + 1, char**));
- fail_if (yrealloc(callables, arg_count + 1, size_t*));
- fail_if (yrealloc(bucket_sizes, arg_count + 1, size_t));
+ fail_if (_yrealloc(names, arg_count + 1, char**));
+ fail_if (_yrealloc(callables, arg_count + 1, size_t*));
+ fail_if (_yrealloc(bucket_sizes, arg_count + 1, size_t));
memset(names + buckets, 0, (arg_count + 1 - buckets) * sizeof(char**));
memset(callables + buckets, 0, (arg_count + 1 - buckets) * sizeof(size_t*));
memset(bucket_sizes + buckets, 0, (arg_count + 1 - buckets) * sizeof(size_t));
@@ -134,8 +132,8 @@ int callables_set(const char* restrict name, size_t arg_count, mds_kbdc_tree_t*
callables[arg_count][bucket_sizes[arg_count]] = list_ptr;
bucket_sizes[arg_count]++;
- fail_if (yrealloc(callable_list, list_ptr + 1, mds_kbdc_tree_t*));
- fail_if (yrealloc(callable_include_stack_list, list_ptr + 1, mds_kbdc_include_stack_t*));
+ fail_if (_yrealloc(callable_list, list_ptr + 1, mds_kbdc_tree_t*));
+ fail_if (_yrealloc(callable_include_stack_list, list_ptr + 1, mds_kbdc_include_stack_t*));
callable_list[list_ptr] = callable;
callable_include_stack_list[list_ptr] = callable_include_stack;
@@ -145,17 +143,12 @@ int callables_set(const char* restrict name, size_t arg_count, mds_kbdc_tree_t*
fail:
saved_errno = errno;
free(dupname);
- free(new_names);
- free(new_callables);
- free(new_bucket_sizes);
- free(new_callable_list);
- free(new_callable_include_stack_list);
if (old_names)
names[arg_count] = old_names;
if (old_callables)
callables[arg_count] = old_callables;
return errno = saved_errno, -1;
-#undef yrealloc
+#undef _yrealloc
}
diff --git a/src/mds-kbdc/compile-layout.c b/src/mds-kbdc/compile-layout.c
index 574f818..011d2d0 100644
--- a/src/mds-kbdc/compile-layout.c
+++ b/src/mds-kbdc/compile-layout.c
@@ -633,20 +633,20 @@ static char32_t* parse_escape(mds_kbdc_tree_t* restrict tree, const char* restri
#define R(LOWER, UPPER) (((LOWER) <= c) && (c <= (UPPER)))
#define CR(COND, LOWER, UPPER) ((*escape == (COND)) && R(LOWER, UPPER))
#define VARIABLE (int)(raw - raw_) - (c == '.'), raw_
-#define RETURN_ERROR(...) \
- do \
- { \
- NEW_ERROR(__VA_ARGS__); \
- error->start = lineoff; \
- error->end = lineoff + (size_t)(raw - raw_); \
- tree->processed = PROCESS_LEVEL; \
- *escape = 0; \
- if (rc) \
- goto done; \
- fail_if (rc = malloc(sizeof(char32_t)), rc == NULL); \
- *rc = -1; \
- goto done; \
- } \
+#define RETURN_ERROR(...) \
+ do \
+ { \
+ NEW_ERROR(__VA_ARGS__); \
+ error->start = lineoff; \
+ error->end = lineoff + (size_t)(raw - raw_); \
+ tree->processed = PROCESS_LEVEL; \
+ *escape = 0; \
+ if (rc) \
+ goto done; \
+ fail_if (xmalloc(rc, 1, char32_t)); \
+ *rc = -1; \
+ goto done; \
+ } \
while (0)
const char* restrict raw_ = raw++;
@@ -900,7 +900,7 @@ static char32_t* parse_unquoted_string(mds_kbdc_tree_t* restrict tree, const cha
else CHAR_ERROR(tree, ERROR, "stray ‘%c’", c);
done:
- fail_if (rc = malloc(2 * sizeof(char32_t)), rc == NULL);
+ fail_if (xmalloc(rc, 2, char32_t));
return rc[0] = buf, rc[1] = -1, rc;
fail:
diff --git a/src/mds-kbdc/include-stack.c b/src/mds-kbdc/include-stack.c
index d8c4547..b34b57b 100644
--- a/src/mds-kbdc/include-stack.c
+++ b/src/mds-kbdc/include-stack.c
@@ -220,15 +220,13 @@ mds_kbdc_include_stack_t* mds_kbdc_include_stack_save(void)
*/
int mds_kbdc_include_stack_restore(mds_kbdc_include_stack_t* restrict stack)
{
- const mds_kbdc_tree_include_t** new;
+ const mds_kbdc_tree_include_t** tmp;
latest_save = stack;
if (stack->ptr > includes_size)
{
- new = realloc(includes, stack->ptr * sizeof(const mds_kbdc_tree_include_t*));
- fail_if (new == NULL);
- includes = new;
+ fail_if (yrealloc(tmp, includes, stack->ptr, const mds_kbdc_tree_include_t*));
includes_size = stack->ptr;
}
diff --git a/src/mds-kbdc/make-tree.c b/src/mds-kbdc/make-tree.c
index 960fe22..b2273e4 100644
--- a/src/mds-kbdc/make-tree.c
+++ b/src/mds-kbdc/make-tree.c
@@ -441,8 +441,7 @@ static int get_pathname(const char* restrict filename)
fail_if (errno != ENOENT);
saved_errno = errno;
fail_if (cwd = curpath(), cwd == NULL);
- result->pathname = strdup(filename);
- fail_if (result->pathname == NULL);
+ fail_if (xstrdup(result->pathname, filename));
NEW_ERROR_(result, ERROR, 0, 0, 0, 0, 1, "no such file or directory in ‘%s’", cwd);
free(cwd);
return 0;
@@ -453,8 +452,7 @@ static int get_pathname(const char* restrict filename)
{
saved_errno = errno;
NEW_ERROR_(result, ERROR, 0, 0, 0, 0, 0, NULL);
- error->description = strdup(strerror(saved_errno));
- fail_if (error->description == NULL);
+ fail_if (xstrdup(error->description, strerror(saved_errno)));
return 0;
}
@@ -649,7 +647,7 @@ static int names_1(char** restrict var)
end = name_end;
prev_end_char = *end;
*end = '\0';
- fail_if ((*var = strdup(line)) == NULL);
+ fail_if (xstrdup(*var, line));
}
return 0;
@@ -699,7 +697,7 @@ static int chars(char** restrict var)
else quote = (c == '"');
}
prev_end_char = *arg_end, *arg_end = '\0', end = arg_end;
- fail_if ((*var = strdup(line)) == NULL);
+ fail_if (xstrdup(*var, line));
line = end;
}
@@ -837,13 +835,13 @@ static int keys(mds_kbdc_tree_t** restrict var)
{
NEW_SUBNODE(keys, KEYS);
*var = (mds_kbdc_tree_t*)subnode;
- fail_if ((subnode->keys = strdup(line)) == NULL);
+ fail_if (xstrdup(subnode->keys, line));
}
else
{
NEW_SUBNODE(string, STRING);
*var = (mds_kbdc_tree_t*)subnode;
- fail_if ((subnode->string = strdup(line)) == NULL);
+ fail_if (xstrdup(subnode->string, line));
}
line = end;
@@ -891,7 +889,7 @@ static int pure_keys(char** restrict var)
else if (IS_END(c) && !triangle) { arg_end--; break; }
}
prev_end_char = *arg_end, *arg_end = '\0';
- fail_if ((*var = strdup(line)) == NULL);
+ fail_if (xstrdup(*var, line));
end = arg_end, line = end;
return 0;
diff --git a/src/mds-kbdc/parsed.c b/src/mds-kbdc/parsed.c
index 8ab483b..f5fc311 100644
--- a/src/mds-kbdc/parsed.c
+++ b/src/mds-kbdc/parsed.c
@@ -138,15 +138,14 @@ mds_kbdc_parse_error_t* mds_kbdc_parsed_new_error(mds_kbdc_parsed_t* restrict th
if (this->severest_error_level < severity)
this->severest_error_level = severity;
- fail_if ((error->pathname = strdup(this->pathname)) == NULL);
+ fail_if (xstrdup(error->pathname, this->pathname));
if ((error->error_is_in_file = error_is_in_file))
{
error->line = line;
error->start = start;
error->end = end;
- error->code = strdup(this->source_code->real_lines[line]);
- fail_if (error->code == NULL);
+ fail_if (xstrdup(error->code, this->source_code->real_lines[line]));
}
return error;
diff --git a/src/mds-kbdc/paths.c b/src/mds-kbdc/paths.c
index e294020..031083e 100644
--- a/src/mds-kbdc/paths.c
+++ b/src/mds-kbdc/paths.c
@@ -74,7 +74,7 @@ char* abspath(const char* path)
if (*path == '/')
{
- fail_if (buf = strdup(path), buf == NULL);
+ fail_if (xstrdup(buf, path));
return buf;
}
diff --git a/src/mds-kbdc/process-includes.c b/src/mds-kbdc/process-includes.c
index 2faaf3f..cf07f56 100644
--- a/src/mds-kbdc/process-includes.c
+++ b/src/mds-kbdc/process-includes.c
@@ -169,7 +169,7 @@ static int process_include(mds_kbdc_tree_include_t* restrict tree)
mds_kbdc_parsed_initialise(&subresult);
/* Get dirname of current file. */
- fail_if ((dirname = strdup(result->pathname)) == NULL);
+ fail_if (xstrdup(dirname, result->pathname));
*(strrchr(dirname, '/')) = '\0';
/* Get the current working directory. */
diff --git a/src/mds-kbdc/raw-data.c b/src/mds-kbdc/raw-data.c
index 52bec82..cf48486 100644
--- a/src/mds-kbdc/raw-data.c
+++ b/src/mds-kbdc/raw-data.c
@@ -489,8 +489,7 @@ char* parse_raw_string(const char* restrict string)
* but when parsed it generateds 2 bytes in UTF-8, and their
* is not code point whose UTF-8 encoding is longer than its
* hexadecimal representation. */
- p = rc = malloc(strlen(string) * sizeof(char));
- fail_if (rc == NULL);
+ fail_if (xmalloc(p = rc, strlen(string), char));
while ((c = *string++))
if (r(escape == 8, '0', '7')) buf = (buf << 3) | (c & 15);
diff --git a/src/mds-kbdc/tree.c b/src/mds-kbdc/tree.c
index 6ee98c1..4e6fef3 100644
--- a/src/mds-kbdc/tree.c
+++ b/src/mds-kbdc/tree.c
@@ -254,7 +254,7 @@ void mds_kbdc_tree_free(mds_kbdc_tree_t* restrict this)
*
* @param member:identifer The member in the tree to duplicate
*/
-#define S(member) fail_if (t->member && (n->member = strdup(t->member), n->member == NULL))
+#define S(member) fail_if (t->member && xstrdup(n->member, t->member))
/**