123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- #ifndef _UTIL_H
- #define _UTIL_H
- #include <stdarg.h>
- #include <stdbool.h>
- #include <getopt.h>
- #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
- static inline void __attribute__((noreturn)) die(const char *str, ...)
- {
- va_list ap;
- va_start(ap, str);
- fprintf(stderr, "FATAL ERROR: ");
- vfprintf(stderr, str, ap);
- va_end(ap);
- exit(1);
- }
- static inline void *xmalloc(size_t len)
- {
- void *new = malloc(len);
- if (!new)
- die("malloc() failed\n");
- return new;
- }
- static inline void *xrealloc(void *p, size_t len)
- {
- void *new = realloc(p, len);
- if (!new)
- die("realloc() failed (len=%d)\n", len);
- return new;
- }
- extern char *xstrdup(const char *s);
- extern char *join_path(const char *path, const char *name);
- bool util_is_printable_string(const void *data, int len);
- char get_escape_char(const char *s, int *i);
- char *utilfdt_read(const char *filename);
- char *utilfdt_read_len(const char *filename, off_t *len);
- int utilfdt_read_err(const char *filename, char **buffp);
- int utilfdt_read_err_len(const char *filename, char **buffp, off_t *len);
- int utilfdt_write(const char *filename, const void *blob);
- int utilfdt_write_err(const char *filename, const void *blob);
- int utilfdt_decode_type(const char *fmt, int *type, int *size);
- #define USAGE_TYPE_MSG \
- "<type>\ts=string, i=int, u=unsigned, x=hex\n" \
- "\tOptional modifier prefix:\n" \
- "\t\thh or b=byte, h=2 byte, l=4 byte (default)";
- void utilfdt_print_data(const char *data, int len);
- void util_version(void) __attribute__((noreturn));
- void util_usage(const char *errmsg, const char *synopsis,
- const char *short_opts, struct option const long_opts[],
- const char * const opts_help[]) __attribute__((noreturn));
- #define usage(errmsg) \
- util_usage(errmsg, usage_synopsis, usage_short_opts, \
- usage_long_opts, usage_opts_help)
- #define util_getopt_long() getopt_long(argc, argv, usage_short_opts, \
- usage_long_opts, NULL)
- #define a_argument required_argument
- #define USAGE_COMMON_SHORT_OPTS "hV"
- #define USAGE_COMMON_LONG_OPTS \
- {"help", no_argument, NULL, 'h'}, \
- {"version", no_argument, NULL, 'V'}, \
- {NULL, no_argument, NULL, 0x0}
- #define USAGE_COMMON_OPTS_HELP \
- "Print this help and exit", \
- "Print version and exit", \
- NULL
- #define case_USAGE_COMMON_FLAGS \
- case 'h': usage(NULL); \
- case 'V': util_version(); \
- case '?': usage("unknown option");
- #endif
|