^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #ifndef __SUBCMD_UTIL_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define __SUBCMD_UTIL_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <stdarg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #define NORETURN __attribute__((__noreturn__))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) static inline void report(const char *prefix, const char *err, va_list params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) char msg[1024];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) vsnprintf(msg, sizeof(msg), err, params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) fprintf(stderr, " %s%s\n", prefix, msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static NORETURN inline void die(const char *err, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) va_list params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) va_start(params, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) report(" Fatal: ", err, params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) exit(128);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) va_end(params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define alloc_nr(x) (((x)+16)*3/2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * Realloc the buffer pointed at by variable 'x' so that it can hold
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * at least 'nr' entries; the number of entries currently allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * is 'alloc', using the standard growing factor alloc_nr() macro.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * DO NOT USE any expression with side-effect for 'x' or 'alloc'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define ALLOC_GROW(x, nr, alloc) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if ((nr) > alloc) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (alloc_nr(alloc) < (nr)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) alloc = (nr); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) else \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) alloc = alloc_nr(alloc); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) x = xrealloc((x), alloc * sizeof(*(x))); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) } while(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static inline void *xrealloc(void *ptr, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) void *ret = realloc(ptr, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) die("Out of memory, realloc failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #define astrcatf(out, fmt, ...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) ({ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) char *tmp = *(out); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (asprintf((out), "%s" fmt, tmp ?: "", ## __VA_ARGS__) == -1) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) die("asprintf failed"); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) free(tmp); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static inline void astrcat(char **out, const char *add)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) char *tmp = *out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (asprintf(out, "%s%s", tmp ?: "", add) == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) die("asprintf failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) free(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #endif /* __SUBCMD_UTIL_H */