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().

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 − 中吉
master
Avril 3 years ago
parent 254d9fbadc
commit aff04b6d4b
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -27,20 +27,23 @@
// Returns from the program // 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) // Invalid arguments to program (print usage to stderr)
#define PROG_RET_ARGS 1 #define PROG_RET_ARGS -2
// Mapping of argv[1] failed // 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 // `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 // `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. // 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 // 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) #define PROG_RET_MAP_HAYSTACK_FAILED AS((AS(INT_MAX, u64) >> 1lu) ^ AS(INT_MAX, u64), int)
// Unmapping of argv[1] failed // Unmapping of argv[1] failed
#define PROG_RET_UNMAP_NEEDLE_FAILED 3 #define PROG_RET_UNMAP_NEEDLE_FAILED -4
// Internal error // Internal error
#define PROG_RET_INTERNAL -1 #define PROG_RET_INTERNAL -1

@ -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: 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: Within the threadpool: output information regarding each match/nonmatch.
//TODO: Join the threadpool and consolidate results. //TODO: Join the threadpool and consolidate results.
//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
//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.
if(!map_handle_err(map_free(needle))) return PROG_RET_UNMAP_NEEDLE_FAILED; if(!map_handle_err(map_free(needle))) return PROG_RET_UNMAP_NEEDLE_FAILED;
TRACE("main end"); TRACE("main end");

Loading…
Cancel
Save