Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate CLI output functions #621

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions cli/include/hse/cli/output.h
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, ...);
1 change: 1 addition & 0 deletions cli/lib/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-FileCopyrightText: Copyright 2021 Micron Technology, Inc.

cli_sources = files(
'output.c',
'param.c',
'program.c',
'rest/api.c',
Expand Down
81 changes: 81 additions & 0 deletions cli/lib/output.c
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);
}
2 changes: 2 additions & 0 deletions cli/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ hse_cli = static_library(
],
dependencies: [
cjson_dep,
# TODO: Remove when the cli can depend on libhse-util
hse_internal_dep,
hse_error_dep,
libcurl_dep,
rbtree_dep,
Expand Down
29 changes: 1 addition & 28 deletions tools/attack/attack.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,10 @@

#include <hse/hse.h>

#include <hse/cli/output.h>
#include <hse/cli/program.h>
#include <hse/util/compiler.h>

void HSE_PRINTF(2, 3)
fatal(hse_err_t err, char *fmt, ...)
{
va_list ap;

va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
if (err)
fprintf(stderr, ": %ld\n", (long)err);
else
fprintf(stderr, "\n");
va_end(ap);
exit(1);
}

void HSE_PRINTF(1, 2)
syntax(const char *fmt, ...)
{
char msg[256];
va_list ap;

va_start(ap, fmt);
vsnprintf(msg, sizeof(msg), fmt, ap);
va_end(ap);

fprintf(stderr, "%s: %s, use -h for help\n", progname, msg);
}

void
usage(void)
{
Expand Down
13 changes: 1 addition & 12 deletions tools/bnt/bnt.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <hse/hse.h>
#include <hse/version.h>

#include <hse/cli/output.h>
#include <hse/cli/program.h>
#include <hse/util/bonsai_tree.h>
#include <hse/util/compiler.h>
Expand Down Expand Up @@ -350,18 +351,6 @@ humanize(u_long *nump, char **sufp)
}
}

__attribute__((format(printf, 1, 2))) void
syntax(const char *fmt, ...)
{
va_list ap;

va_start(ap, fmt);
vsnprintf(emsg, sizeof(emsg), fmt, ap);
va_end(ap);

fprintf(stderr, "%s: %s, use -h for help\n", progname, emsg);
}

uint64_t
strtou64(const void *str, char **endp, u_int base)
{
Expand Down
18 changes: 3 additions & 15 deletions tools/boundcur/boundcur.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <hse/flags.h>
#include <hse/hse.h>

#include <hse/cli/output.h>
#include <hse/cli/param.h>
#include <hse/cli/program.h>
#include <hse/util/arch.h>
Expand Down Expand Up @@ -134,19 +135,6 @@ do_work(void *arg)
rc = hse_kvs_cursor_destroy(c);
}

void HSE_PRINTF(1, 2)
syntax(const char *fmt, ...)
{
char msg[256];
va_list ap;

va_start(ap, fmt);
vsnprintf(msg, sizeof(msg), fmt, ap);
va_end(ap);

fprintf(stderr, "%s: %s, use -h for help\n", progname, msg);
}

void
usage(void)
{
Expand Down Expand Up @@ -237,10 +225,10 @@ main(int argc, char **argv)
switch (rc) {
case 0:
if (optind < argc)
fatal(0, "unknown parameter: %s", argv[optind]);
fatalx("unknown parameter: %s", argv[optind]);
break;
case EINVAL:
fatal(0, "missing group name (e.g. %s) before parameter %s\n", PG_KVDB_OPEN, argv[optind]);
fatalx("missing group name (e.g. %s) before parameter %s\n", PG_KVDB_OPEN, argv[optind]);
break;
default:
fatal(rc, "error processing parameter %s\n", argv[optind]);
Expand Down
18 changes: 3 additions & 15 deletions tools/capput/capput.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

#include <hse/hse.h>

#include <hse/cli/output.h>
#include <hse/cli/param.h>
#include <hse/cli/program.h>
#include <hse/util/arch.h>
Expand Down Expand Up @@ -533,19 +534,6 @@ reader(void *arg)
hse_kvdb_txn_free(targ->kvdb, txn);
}

void
syntax(const char *fmt, ...)
{
char msg[256];
va_list ap;

va_start(ap, fmt);
vsnprintf(msg, sizeof(msg), fmt, ap);
va_end(ap);

fprintf(stderr, "%s: %s, use -h for help\n", progname, msg);
}

void
usage(void)
{
Expand Down Expand Up @@ -681,10 +669,10 @@ main(int argc, char **argv)
switch (rc) {
case 0:
if (optind < argc)
fatal(0, "unknown parameter: %s", argv[optind]);
fatalx("unknown parameter: %s", argv[optind]);
break;
case EINVAL:
fatal(0, "missing group name (e.g. %s) before parameter %s\n", PG_KVDB_OPEN, argv[optind]);
fatalx("missing group name (e.g. %s) before parameter %s\n", PG_KVDB_OPEN, argv[optind]);
break;
default:
fatal(rc, "error processing parameter %s\n", argv[optind]);
Expand Down
18 changes: 3 additions & 15 deletions tools/cn_metrics/cn_metrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <hse/hse.h>

#include <hse/cli/output.h>
#include <hse/cli/program.h>
#include <hse/ikvdb/cn.h>
#include <hse/ikvdb/csched.h>
Expand Down Expand Up @@ -48,19 +49,6 @@ usage(void)
progname);
}

void
syntax(const char *fmt, ...)
{
char msg[256];
va_list ap;

va_start(ap, fmt);
vsnprintf(msg, sizeof(msg), fmt, ap);
va_end(ap);

fprintf(stderr, "%s: %s, use -h for help\n", progname, msg);
}

/* Big enough for s64 min/max, u64 max, etc */
#define BIGNUM_WIDTH_MAX 21 /* max width for signed 64-bit */

Expand Down Expand Up @@ -551,10 +539,10 @@ main(int argc, char **argv)
switch (rc) {
case 0:
if (optind < argc)
fatal(0, "unknown parameter: %s", argv[optind]);
fatalx("unknown parameter: %s", argv[optind]);
break;
case EINVAL:
fatal(0, "missing group name (e.g. %s) before parameter %s\n", PG_KVDB_OPEN, argv[optind]);
fatalx("missing group name (e.g. %s) before parameter %s\n", PG_KVDB_OPEN, argv[optind]);
break;
default:
fatal(rc, "error processing parameter %s\n", argv[optind]);
Expand Down
8 changes: 4 additions & 4 deletions tools/cndump/cndb_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

#include <hse/hse.h>

#include <hse/cli/output.h>
#include <hse/cli/program.h>
#include <hse/ikvdb/diag_kvdb.h>

#include "cndb_reader.h"
#include "cndb_record.h"
#include "commands.h"
#include "fatal.h"
#include "globals.h"

/* command line options for cndb sub-command */
Expand Down Expand Up @@ -103,11 +103,11 @@ cndb_cmd(int argc, char **argv)

err = hse_init(0, NELEM(paramv), paramv);
if (err)
fatal("hse_init", err);
fatal(err, "hse_init");

err = diag_kvdb_open(opts.home, 0, 0, &kvdb);
if (err)
fatal("diag_kvdb_open", err);
fatal(err, "diag_kvdb_open");

cndb_iter_init(kvdb, &reader);
cndb_rec_init(&rec);
Expand All @@ -117,7 +117,7 @@ cndb_cmd(int argc, char **argv)

err = diag_kvdb_close(kvdb);
if (err)
fatal("diag_kvdb_close", err);
fatal(err, "diag_kvdb_close");

hse_fini();
}
8 changes: 4 additions & 4 deletions tools/cndump/cndb_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* SPDX-FileCopyrightText: Copyright 2022 Micron Technology, Inc.
*/

#include <hse/cli/output.h>
#include <hse/ikvdb/diag_kvdb.h>
#include <hse/mpool/mpool.h>

#include "cndb_reader.h"
#include "cndb_record.h"
#include "fatal.h"

static void
read_record(struct cndb_dump_reader *reader, struct cndb_rec *rec)
Expand All @@ -23,7 +23,7 @@ read_record(struct cndb_dump_reader *reader, struct cndb_rec *rec)
}

if (err)
fatal("mpool_mdc_read", err);
fatal(err, "mpool_mdc_read");

if (reclen) {
rec->type = omf_cnhdr_type(rec->buf);
Expand All @@ -42,14 +42,14 @@ cndb_iter_init(struct hse_kvdb *kvdb, struct cndb_dump_reader *r)

err = diag_kvdb_get_cndb(kvdb, &cndb);
if (err)
fatal("diag_kvdb_get_cndb", err);
fatal(err, "diag_kvdb_get_cndb");

r->mdc = cndb_mdc_get(cndb);
r->eof = false;

err = mpool_mdc_rewind(r->mdc);
if (err)
fatal("mpool_mdc_rewind", err);
fatal(err, "mpool_mdc_rewind");
}

bool
Expand Down
Loading