Skip to content

Commit c522bda

Browse files
committed
Consolidate CLI output functions
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>
1 parent 22b19f2 commit c522bda

File tree

45 files changed

+299
-731
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+299
-731
lines changed

cli/include/hse/cli/output.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* SPDX-License-Identifier: Apache-2.0 OR MIT
2+
*
3+
* SPDX-FileCopyrightText: Copyright 2023 Micron Technology, Inc.
4+
*/
5+
6+
#include <hse/types.h>
7+
8+
#include <hse/util/compiler.h>
9+
10+
HSE_PRINTF(2, 3) void
11+
error(hse_err_t err, const char *fmt, ...);
12+
13+
#define errorx(_fmt, ...) error(0, (_fmt), ##__VA_ARGS__)
14+
15+
HSE_PRINTF(2, 3) HSE_NORETURN void
16+
fatal(hse_err_t err, const char *fmt, ...);
17+
18+
#define fatalx(_fmt, ...) fatal(0, (_fmt), ##__VA_ARGS__)
19+
20+
HSE_PRINTF(1, 2) HSE_NORETURN void
21+
syntax(const char *fmt, ...);

cli/lib/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# SPDX-FileCopyrightText: Copyright 2021 Micron Technology, Inc.
44

55
cli_sources = files(
6+
'output.c',
67
'param.c',
78
'program.c',
89
'rest/api.c',

cli/lib/output.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/* SPDX-License-Identifier: Apache-2.0 OR MIT
2+
*
3+
* SPDX-FileCopyrightText: Copyright 2023 Micron Technology, Inc.
4+
*/
5+
6+
#include <assert.h>
7+
#include <stdarg.h>
8+
#include <stddef.h>
9+
#include <stdio.h>
10+
#include <stdlib.h>
11+
#include <sysexits.h>
12+
13+
#include <hse/hse.h>
14+
15+
#include <hse/cli/output.h>
16+
#include <hse/cli/program.h>
17+
#include <hse/util/compiler.h>
18+
19+
static thread_local char error_buffer_tls[1024];
20+
21+
static void
22+
eprint(const hse_err_t err, const char *fmt, va_list ap)
23+
{
24+
int rc HSE_MAYBE_UNUSED;
25+
26+
/* We take progname from argv[0]. argv[0] may contain % symbols which could
27+
* cause an escape in the vfprintf() below, so just print it plainly for
28+
* safety.
29+
*/
30+
fputs(progname, stderr);
31+
if (err) {
32+
char buf[256];
33+
size_t needed_sz HSE_MAYBE_UNUSED;
34+
35+
needed_sz = hse_strerror(err, buf, sizeof(buf));
36+
assert(needed_sz < sizeof(buf));
37+
38+
rc = snprintf(error_buffer_tls, sizeof(error_buffer_tls), ": %s: %s\n", fmt, buf);
39+
} else {
40+
rc = snprintf(error_buffer_tls, sizeof(error_buffer_tls), ": %s\n", fmt);
41+
}
42+
assert(rc >= 0 && rc < sizeof(error_buffer_tls));
43+
44+
vfprintf(stderr, error_buffer_tls, ap);
45+
}
46+
47+
void
48+
error(const hse_err_t err, const char * const fmt, ...)
49+
{
50+
va_list ap;
51+
52+
va_start(ap, fmt);
53+
eprint(err, fmt, ap);
54+
va_end(ap);
55+
}
56+
57+
void
58+
fatal(const hse_err_t err, const char * const fmt, ...)
59+
{
60+
va_list ap;
61+
62+
va_start(ap, fmt);
63+
eprint(err, fmt, ap);
64+
va_end(ap);
65+
66+
exit(1);
67+
}
68+
69+
void
70+
syntax(const char * const fmt, ...)
71+
{
72+
va_list ap;
73+
char msg[256];
74+
75+
va_start(ap, fmt);
76+
vsnprintf(msg, sizeof(msg), fmt, ap);
77+
va_end(ap);
78+
79+
error(0, "%s, use -h for help", msg);
80+
exit(EX_USAGE);
81+
}

cli/meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ hse_cli = static_library(
2424
],
2525
dependencies: [
2626
cjson_dep,
27+
# TODO: Remove when the cli can depend on libhse-util
28+
hse_internal_dep,
2729
hse_error_dep,
2830
libcurl_dep,
2931
rbtree_dep,

tools/attack/attack.c

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,10 @@
2929

3030
#include <hse/hse.h>
3131

32+
#include <hse/cli/output.h>
3233
#include <hse/cli/program.h>
3334
#include <hse/util/compiler.h>
3435

35-
void HSE_PRINTF(2, 3)
36-
fatal(hse_err_t err, char *fmt, ...)
37-
{
38-
va_list ap;
39-
40-
va_start(ap, fmt);
41-
vfprintf(stderr, fmt, ap);
42-
if (err)
43-
fprintf(stderr, ": %ld\n", (long)err);
44-
else
45-
fprintf(stderr, "\n");
46-
va_end(ap);
47-
exit(1);
48-
}
49-
50-
void HSE_PRINTF(1, 2)
51-
syntax(const char *fmt, ...)
52-
{
53-
char msg[256];
54-
va_list ap;
55-
56-
va_start(ap, fmt);
57-
vsnprintf(msg, sizeof(msg), fmt, ap);
58-
va_end(ap);
59-
60-
fprintf(stderr, "%s: %s, use -h for help\n", progname, msg);
61-
}
62-
6336
void
6437
usage(void)
6538
{

tools/bnt/bnt.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <hse/hse.h>
3737
#include <hse/version.h>
3838

39+
#include <hse/cli/output.h>
3940
#include <hse/cli/program.h>
4041
#include <hse/util/bonsai_tree.h>
4142
#include <hse/util/compiler.h>
@@ -350,18 +351,6 @@ humanize(u_long *nump, char **sufp)
350351
}
351352
}
352353

353-
__attribute__((format(printf, 1, 2))) void
354-
syntax(const char *fmt, ...)
355-
{
356-
va_list ap;
357-
358-
va_start(ap, fmt);
359-
vsnprintf(emsg, sizeof(emsg), fmt, ap);
360-
va_end(ap);
361-
362-
fprintf(stderr, "%s: %s, use -h for help\n", progname, emsg);
363-
}
364-
365354
uint64_t
366355
strtou64(const void *str, char **endp, u_int base)
367356
{

tools/boundcur/boundcur.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <hse/flags.h>
2020
#include <hse/hse.h>
2121

22+
#include <hse/cli/output.h>
2223
#include <hse/cli/param.h>
2324
#include <hse/cli/program.h>
2425
#include <hse/util/arch.h>
@@ -134,19 +135,6 @@ do_work(void *arg)
134135
rc = hse_kvs_cursor_destroy(c);
135136
}
136137

137-
void HSE_PRINTF(1, 2)
138-
syntax(const char *fmt, ...)
139-
{
140-
char msg[256];
141-
va_list ap;
142-
143-
va_start(ap, fmt);
144-
vsnprintf(msg, sizeof(msg), fmt, ap);
145-
va_end(ap);
146-
147-
fprintf(stderr, "%s: %s, use -h for help\n", progname, msg);
148-
}
149-
150138
void
151139
usage(void)
152140
{
@@ -237,10 +225,10 @@ main(int argc, char **argv)
237225
switch (rc) {
238226
case 0:
239227
if (optind < argc)
240-
fatal(0, "unknown parameter: %s", argv[optind]);
228+
fatalx("unknown parameter: %s", argv[optind]);
241229
break;
242230
case EINVAL:
243-
fatal(0, "missing group name (e.g. %s) before parameter %s\n", PG_KVDB_OPEN, argv[optind]);
231+
fatalx("missing group name (e.g. %s) before parameter %s\n", PG_KVDB_OPEN, argv[optind]);
244232
break;
245233
default:
246234
fatal(rc, "error processing parameter %s\n", argv[optind]);

tools/capput/capput.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
#include <hse/hse.h>
4848

49+
#include <hse/cli/output.h>
4950
#include <hse/cli/param.h>
5051
#include <hse/cli/program.h>
5152
#include <hse/util/arch.h>
@@ -533,19 +534,6 @@ reader(void *arg)
533534
hse_kvdb_txn_free(targ->kvdb, txn);
534535
}
535536

536-
void
537-
syntax(const char *fmt, ...)
538-
{
539-
char msg[256];
540-
va_list ap;
541-
542-
va_start(ap, fmt);
543-
vsnprintf(msg, sizeof(msg), fmt, ap);
544-
va_end(ap);
545-
546-
fprintf(stderr, "%s: %s, use -h for help\n", progname, msg);
547-
}
548-
549537
void
550538
usage(void)
551539
{
@@ -681,10 +669,10 @@ main(int argc, char **argv)
681669
switch (rc) {
682670
case 0:
683671
if (optind < argc)
684-
fatal(0, "unknown parameter: %s", argv[optind]);
672+
fatalx("unknown parameter: %s", argv[optind]);
685673
break;
686674
case EINVAL:
687-
fatal(0, "missing group name (e.g. %s) before parameter %s\n", PG_KVDB_OPEN, argv[optind]);
675+
fatalx("missing group name (e.g. %s) before parameter %s\n", PG_KVDB_OPEN, argv[optind]);
688676
break;
689677
default:
690678
fatal(rc, "error processing parameter %s\n", argv[optind]);

tools/cn_metrics/cn_metrics.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <hse/hse.h>
1111

12+
#include <hse/cli/output.h>
1213
#include <hse/cli/program.h>
1314
#include <hse/ikvdb/cn.h>
1415
#include <hse/ikvdb/csched.h>
@@ -48,19 +49,6 @@ usage(void)
4849
progname);
4950
}
5051

51-
void
52-
syntax(const char *fmt, ...)
53-
{
54-
char msg[256];
55-
va_list ap;
56-
57-
va_start(ap, fmt);
58-
vsnprintf(msg, sizeof(msg), fmt, ap);
59-
va_end(ap);
60-
61-
fprintf(stderr, "%s: %s, use -h for help\n", progname, msg);
62-
}
63-
6452
/* Big enough for s64 min/max, u64 max, etc */
6553
#define BIGNUM_WIDTH_MAX 21 /* max width for signed 64-bit */
6654

@@ -551,10 +539,10 @@ main(int argc, char **argv)
551539
switch (rc) {
552540
case 0:
553541
if (optind < argc)
554-
fatal(0, "unknown parameter: %s", argv[optind]);
542+
fatalx("unknown parameter: %s", argv[optind]);
555543
break;
556544
case EINVAL:
557-
fatal(0, "missing group name (e.g. %s) before parameter %s\n", PG_KVDB_OPEN, argv[optind]);
545+
fatalx("missing group name (e.g. %s) before parameter %s\n", PG_KVDB_OPEN, argv[optind]);
558546
break;
559547
default:
560548
fatal(rc, "error processing parameter %s\n", argv[optind]);

tools/cndump/cndb_dump.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111

1212
#include <hse/hse.h>
1313

14+
#include <hse/cli/output.h>
1415
#include <hse/cli/program.h>
1516
#include <hse/ikvdb/diag_kvdb.h>
1617

1718
#include "cndb_reader.h"
1819
#include "cndb_record.h"
1920
#include "commands.h"
20-
#include "fatal.h"
2121
#include "globals.h"
2222

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

104104
err = hse_init(0, NELEM(paramv), paramv);
105105
if (err)
106-
fatal("hse_init", err);
106+
fatal(err, "hse_init");
107107

108108
err = diag_kvdb_open(opts.home, 0, 0, &kvdb);
109109
if (err)
110-
fatal("diag_kvdb_open", err);
110+
fatal(err, "diag_kvdb_open");
111111

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

118118
err = diag_kvdb_close(kvdb);
119119
if (err)
120-
fatal("diag_kvdb_close", err);
120+
fatal(err, "diag_kvdb_close");
121121

122122
hse_fini();
123123
}

0 commit comments

Comments
 (0)