-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It seemed like every tool defined its own `syntax()` and/or `fatal()`. By making a single a definition for each in libhse-cli, we can cut down all the redefinitions. This also has the added benefit of reducing the number of tools needing tools/common.c. Less compilation. Yay! Signed-off-by: Tristan Partin <tpartin@micron.com>
- Loading branch information
1 parent
22b19f2
commit c522bda
Showing
45 changed files
with
299 additions
and
731 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* SPDX-License-Identifier: Apache-2.0 OR MIT | ||
* | ||
* SPDX-FileCopyrightText: Copyright 2023 Micron Technology, Inc. | ||
*/ | ||
|
||
#include <hse/types.h> | ||
|
||
#include <hse/util/compiler.h> | ||
|
||
HSE_PRINTF(2, 3) void | ||
error(hse_err_t err, const char *fmt, ...); | ||
|
||
#define errorx(_fmt, ...) error(0, (_fmt), ##__VA_ARGS__) | ||
|
||
HSE_PRINTF(2, 3) HSE_NORETURN void | ||
fatal(hse_err_t err, const char *fmt, ...); | ||
|
||
#define fatalx(_fmt, ...) fatal(0, (_fmt), ##__VA_ARGS__) | ||
|
||
HSE_PRINTF(1, 2) HSE_NORETURN void | ||
syntax(const char *fmt, ...); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* SPDX-License-Identifier: Apache-2.0 OR MIT | ||
* | ||
* SPDX-FileCopyrightText: Copyright 2023 Micron Technology, Inc. | ||
*/ | ||
|
||
#include <assert.h> | ||
#include <stdarg.h> | ||
#include <stddef.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <sysexits.h> | ||
|
||
#include <hse/hse.h> | ||
|
||
#include <hse/cli/output.h> | ||
#include <hse/cli/program.h> | ||
#include <hse/util/compiler.h> | ||
|
||
static thread_local char error_buffer_tls[1024]; | ||
|
||
static void | ||
eprint(const hse_err_t err, const char *fmt, va_list ap) | ||
{ | ||
int rc HSE_MAYBE_UNUSED; | ||
|
||
/* We take progname from argv[0]. argv[0] may contain % symbols which could | ||
* cause an escape in the vfprintf() below, so just print it plainly for | ||
* safety. | ||
*/ | ||
fputs(progname, stderr); | ||
if (err) { | ||
char buf[256]; | ||
size_t needed_sz HSE_MAYBE_UNUSED; | ||
|
||
needed_sz = hse_strerror(err, buf, sizeof(buf)); | ||
assert(needed_sz < sizeof(buf)); | ||
|
||
rc = snprintf(error_buffer_tls, sizeof(error_buffer_tls), ": %s: %s\n", fmt, buf); | ||
} else { | ||
rc = snprintf(error_buffer_tls, sizeof(error_buffer_tls), ": %s\n", fmt); | ||
} | ||
assert(rc >= 0 && rc < sizeof(error_buffer_tls)); | ||
|
||
vfprintf(stderr, error_buffer_tls, ap); | ||
} | ||
|
||
void | ||
error(const hse_err_t err, const char * const fmt, ...) | ||
{ | ||
va_list ap; | ||
|
||
va_start(ap, fmt); | ||
eprint(err, fmt, ap); | ||
va_end(ap); | ||
} | ||
|
||
void | ||
fatal(const hse_err_t err, const char * const fmt, ...) | ||
{ | ||
va_list ap; | ||
|
||
va_start(ap, fmt); | ||
eprint(err, fmt, ap); | ||
va_end(ap); | ||
|
||
exit(1); | ||
} | ||
|
||
void | ||
syntax(const char * const fmt, ...) | ||
{ | ||
va_list ap; | ||
char msg[256]; | ||
|
||
va_start(ap, fmt); | ||
vsnprintf(msg, sizeof(msg), fmt, ap); | ||
va_end(ap); | ||
|
||
error(0, "%s, use -h for help", msg); | ||
exit(EX_USAGE); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.