summaryrefslogtreecommitdiff
path: root/src/bin
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/aplwc_repl.h2
-rw-r--r--src/bin/main.c56
-rw-r--r--src/bin/util.c8
3 files changed, 22 insertions, 44 deletions
diff --git a/src/bin/aplwc_repl.h b/src/bin/aplwc_repl.h
index 4e594bc..6fb4415 100644
--- a/src/bin/aplwc_repl.h
+++ b/src/bin/aplwc_repl.h
@@ -31,6 +31,6 @@
#include <aplwc_macros.h>
/* util.c */
-long getenv_long(const char *, long);
+unsigned long getenv_ulong(const char *, unsigned long);
#endif /* APLWC_REPL_H */
diff --git a/src/bin/main.c b/src/bin/main.c
index 36dd424..d0328f0 100644
--- a/src/bin/main.c
+++ b/src/bin/main.c
@@ -23,32 +23,12 @@
#include <errno.h>
#include "aplwc_repl.h"
-static uint64_t alloc_count;
-static long memdbg;
-
-static int
-hook_alloc(void)
-{
- alloc_count++;
- if((memdbg >= 0) && (alloc_count > ((uint64_t)memdbg)))
- return 0;
- else
- return 1;
-}
-
-static void *
-my_malloc(size_t size)
-{
- if(!hook_alloc())
- return NULL;
- return malloc(size);
-}
-
static void *
my_alloc(size_t size, size_t *actual)
{
size_t target = size;
void *p;
+ int fail2;
if(actual){
if(NEVER(target == 0))
@@ -65,10 +45,12 @@ my_alloc(size_t size, size_t *actual)
}
}
- p = my_malloc(target);
+ fail2 = aplwc_simulate_failure();
+
+ p = FAILCASE(malloc(target), NULL);
if((p == NULL) && (size < target)){
target = size;
- p = my_malloc(target);
+ p = FAILCASE_X(malloc(target), NULL, fail2);
}
if(actual)
@@ -95,6 +77,7 @@ main(int argc, char *argv[])
size_t input_size, src_size, i;
int opt, xflag, input_file;
long tmp;
+ unsigned long print_failsims;
FILE *f;
aplwc_clear_line(&line);
@@ -106,12 +89,12 @@ main(int argc, char *argv[])
line_number = 0;
aplwc = NULL;
- memdbg = getenv_long("APLWC_ALLOC_FAIL_AFTER", -2);
+ aplwc_simulate_failure_at = getenv_ulong("APLWC_SIMULATE_FAILURE_AT", 0);
+ print_failsims = getenv_ulong("APLWC_SIMULATE_FAILURE_PRINT_COUNT", 0);
memset(&fns, 0, sizeof(fns));
fns.alloc = my_alloc;
fns.dealloc = my_dealloc;
- fns.hook_alloc = hook_alloc;
while((opt = getopt(argc, argv, "x")) != -1){
switch(opt){
@@ -147,24 +130,24 @@ usage:
}
if(input_file){
- if(fseek(f, 0, SEEK_END)){
+ if(FAILCASE(fseek(f, 0, SEEK_END), -1)){
file_size:
fprintf(stderr, "Failed to determine file size\n");
exit_code = 1;
goto end;
}
- tmp = ftell(f);
+ tmp = FAILCASE(ftell(f), -1);
if(tmp < 0)
goto file_size;
input_size = tmp;
rewind(f);
- input = malloc(input_size);
+ input = FAILCASE(malloc(input_size), NULL);
if(input == NULL){
fprintf(stderr, "Failed to allocate space for file contents\n");
exit_code = 1;
goto end;
}
- if(fread(input, 1, input_size, f) < input_size){
+ if(FAILCASE(fread(input, 1, input_size, f), 0) < input_size){
fprintf(stderr, "Failed to read file contents\n");
exit_code = 1;
goto end;
@@ -215,12 +198,7 @@ file_size:
aplwc_read(aplwc, input, &line);
if(!aplwc_valid_line(&line)){
line_error:
- ret = aplwc_last_error(aplwc);
fprintf(stderr, "error on line %d: %s: %s\n", line_number, aplwc_errstr(aplwc), input);
- if(input_file){
- exit_code = 1;
- goto end;
- }
continue;
}
aplwc_eval(&line, &value);
@@ -233,25 +211,25 @@ line_error:
end:
if(input)
free(input);
- if(aplwc_valid_line(&line))
+ if(NEVER(aplwc_valid_line(&line)))
aplwc_free_line(&line);
- if(aplwc_valid_value(&value))
+ if(NEVER(aplwc_valid_value(&value)))
aplwc_free_value(&value);
if(aplwc)
aplwc_destroy(aplwc);
- if(memdbg == -1){
+ if(print_failsims){
f = fdopen(3, "w");
if(f == NULL){
fprintf(stderr, "Failed to open fd 3\n");
exit_code = 1;
}else{
- fprintf(f, "%ld\n", alloc_count);
+ fprintf(f, "%ld\n", aplwc_simulate_failure_count);
fclose(f);
}
}
- if((exit_code != 0) && (memdbg >= 0) && (ret == APLWC_OUT_OF_MEMORY))
+ if((exit_code != 0) && aplwc_simulate_failure_at && (aplwc_simulate_failure_count >= aplwc_simulate_failure_at))
exit_code = 0;
return exit_code;
diff --git a/src/bin/util.c b/src/bin/util.c
index 40892b0..b887447 100644
--- a/src/bin/util.c
+++ b/src/bin/util.c
@@ -19,16 +19,16 @@
#include "aplwc_repl.h"
-long
-getenv_long(const char *name, long def)
+unsigned long
+getenv_ulong(const char *name, unsigned long def)
{
- long ret, parsed;
+ unsigned long ret, parsed;
char *env, *p;
ret = def;
env = getenv(name);
if((env != NULL) && (strlen(env) > 0)){
- parsed = strtol(env, &p, 10);
+ parsed = strtoul(env, &p, 10);
if((*p) == 0)
ret = parsed;
}