From aff04b6d4bc2145221393fae74583e3511cb6e8a Mon Sep 17 00:00:00 2001 From: Avril Date: Sat, 10 Jul 2021 00:58:42 +0100 Subject: [PATCH] Reworked `PROG_RET_` to return negatives on internal, memory, or file/mapping failures, and to return the first haystack number that failed to match instead as return code (haystack numbers start at 1, since 0 is the needle). If all matched, return 0 from main(). MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Deprecated `PROG_RET_MAP_HAYSTACK_FAILED` XXX: Currently `PROG_RET_MATCH_HAYSTACK_N_FAILED(n)` returns `n` verbatim (casted to `int`). Maybe we should ensure the number is nonzero in the macro? Or rework haystack numbers to start at 0 and have this macro add 1 to `n`? The former seems easier. Fortune for naka's current commit: Middle blessing − 中吉 --- include/project.h | 15 +++++++++------ src/main.c | 3 +-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/include/project.h b/include/project.h index d90300d..df16259 100644 --- a/include/project.h +++ b/include/project.h @@ -27,20 +27,23 @@ // Returns from the program +//NOTE: -- Reworked `PROG_RET_` to have mapping and internal failures be negative, and the first haystack number to fail returned verbatim (haystack numbers start at 1). So, after consolidating the threadpool's results, we iterate over haystack numbers, find the first non-match, and exit process with its number. Otherwise, we return 0. + // Invalid arguments to program (print usage to stderr) -#define PROG_RET_ARGS 1 +#define PROG_RET_ARGS -2 // Mapping of argv[1] failed -#define PROG_RET_MAP_NEEDLE_FAILED 2 +#define PROG_RET_MAP_NEEDLE_FAILED -3 // `h` is the number in argv[] of the haystack file -#define PROG_RET_MAP_HAYSTACK_N_FAILED(h) ((h) << 2) +#define PROG_RET_MAP_HAYSTACK_N_FAILED(h) -((h) << 2) // `h` is the number in argv[] of the haystack file -#define PROG_RET_UNMAP_HAYSTACK_N_FAILED(h) (((h) << 2) | 1) +#define PROG_RET_UNMAP_HAYSTACK_N_FAILED(h) -(((h) << 2) | 1) // Match of haystack number `h` failed. -#define PROG_RET_MAP_HAYSTACK_N_FAILED(h) ((h) << 2 | 2) +#define PROG_RET_MATCH_HAYSTACK_N_FAILED(h) AS(h, int) // Bitwise OR this with the number(s) of the haystack that match failed. It must not exceed INT_MAX-1 +// XXX: DEPRECATED #define PROG_RET_MAP_HAYSTACK_FAILED AS((AS(INT_MAX, u64) >> 1lu) ^ AS(INT_MAX, u64), int) // Unmapping of argv[1] failed -#define PROG_RET_UNMAP_NEEDLE_FAILED 3 +#define PROG_RET_UNMAP_NEEDLE_FAILED -4 // Internal error #define PROG_RET_INTERNAL -1 diff --git a/src/main.c b/src/main.c index 6025dc7..f4e15bd 100644 --- a/src/main.c +++ b/src/main.c @@ -69,8 +69,7 @@ inv_args: //TODO: Use either `cmp_find()` or `cmp_find_many()` to find the `needle` (mapped above) within those haystacks. //TODO: Within the threadpool: output information regarding each match/nonmatch. //TODO: Join the threadpool and consolidate results. - - //TODO: Should we return an error (`PROG_RET_MAP_HAYSTACK_N_FAILED(n)`) if one (or more) of the haystacks fail? Or consolidate multiple failures into `PROG_RET_MAP_HAYSTACK_FAILED | (failures)`? The latter would be the most complete. + //TODO: Iterate through the haystack numbers match results, return the first non-match haystack number through `PROG_RET_MATCH_HAYSTACK_N_FAILED(n)` (haystack numbers are always nonzero). If all were matched, return 0 if(!map_handle_err(map_free(needle))) return PROG_RET_UNMAP_NEEDLE_FAILED; TRACE("main end");