#include "exit-status.h"
#include "macro.h"
+#include "parse-util.h"
#include "set.h"
+#include "string-util.h"
const ExitStatusMapping exit_status_mappings[256] = {
/* Exit status ranges:
}
}
+int exit_status_from_string(const char *s) {
+ uint8_t val;
+ int r;
+
+ for (size_t i = 0; i < ELEMENTSOF(exit_status_mappings); i++)
+ if (streq_ptr(s, exit_status_mappings[i].name))
+ return i;
+
+ r = safe_atou8(s, &val);
+ if (r < 0)
+ return r;
+
+ return val;
+}
+
bool is_clean_exit(int code, int status, ExitClean clean, ExitStatusSet *success_status) {
if (code == CLD_EXITED)
return status == 0 ||
const char* exit_status_to_string(int code, ExitStatusClass class) _const_;
const char* exit_status_class(int code) _const_;
+int exit_status_from_string(const char *s) _pure_;
typedef struct ExitStatusMapping {
const char *name;
log_info("%d: %s%s%s%s",
i, s ?: "-",
class ? " (" : "", class ?: "", class ? ")" : "");
+
+ if (s)
+ assert_se(exit_status_from_string(s) == i);
}
}
+static void test_exit_status_from_string(void) {
+ log_info("/* %s */", __func__);
+
+ assert_se(exit_status_from_string("11") == 11);
+ assert_se(exit_status_from_string("-1") == -ERANGE);
+ assert_se(exit_status_from_string("256") == -ERANGE);
+ assert_se(exit_status_from_string("foo") == -EINVAL);
+ assert_se(exit_status_from_string("SUCCESS") == 0);
+ assert_se(exit_status_from_string("FAILURE") == 1);
+}
+
int main(int argc, char *argv[]) {
test_setup_logging(LOG_DEBUG);
test_exit_status_to_string();
+ test_exit_status_from_string();
return 0;
}