Skip to content

Commit

Permalink
Change help texts to print to stdout (closes #2542)
Browse files Browse the repository at this point in the history
  • Loading branch information
zuckschwerdt committed Oct 2, 2023
1 parent 08e2391 commit e2f8d62
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
8 changes: 4 additions & 4 deletions include/term_ctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ int term_printf(void *ctx, _Printf_format_string_ const char *format, ...)
* "quoted"
* 'quoted'
*/
int term_help_printf(_Printf_format_string_ char const *format, ...)
int term_help_fprintf(FILE *fp, _Printf_format_string_ char const *format, ...)
#if defined(__GNUC__) || defined(__clang__)
__attribute__((format(printf, 1, 2)))
__attribute__((format(printf, 2, 3)))
#endif
;

Expand All @@ -108,10 +108,10 @@ int term_help_printf(_Printf_format_string_ char const *format, ...)
int term_puts(void *ctx, const char *buf);

/**
* Like 'term_help_printf()', but no var-arg format.
* Like 'term_help_fprintf()', but no var-arg format.
* Simply takes a 0-terminated buffer.
*/
int term_help_puts(void *ctx, const char *buf);
int term_help_fputs(void *ctx, const char *buf, FILE *fp);

/**
* Change the default color map.
Expand Down
26 changes: 14 additions & 12 deletions src/rtl_433.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ static void print_version(void)
_Noreturn
static void usage(int exit_code)
{
term_help_printf(
term_help_fprintf(exit_code ? stderr : stdout,
"Generic RF data receiver and decoder for ISM band devices using RTL-SDR and SoapySDR.\n"
"\nUsage:\n"
"\t\t= General options =\n"
Expand Down Expand Up @@ -196,21 +196,23 @@ static void help_protocols(r_device *devices, unsigned num_devices, int exit_cod
char disabledc;

if (devices) {
term_help_printf("\t\t= Supported device protocols =\n");
FILE *fp = exit_code ? stderr : stdout;
term_help_fprintf(fp,
"\t\t= Supported device protocols =\n");
for (i = 0; i < num_devices; i++) {
disabledc = devices[i].disabled ? '*' : ' ';
if (devices[i].disabled <= 2) // if not hidden
fprintf(stderr, " [%02u]%c %s\n", i + 1, disabledc, devices[i].name);
fprintf(fp, " [%02u]%c %s\n", i + 1, disabledc, devices[i].name);
}
fprintf(stderr, "\n* Disabled by default, use -R n or a conf file to enable\n");
fprintf(fp, "\n* Disabled by default, use -R n or a conf file to enable\n");
}
exit(exit_code);
}

_Noreturn
static void help_device_selection(void)
{
term_help_printf(
term_help_fprintf(stdout,
"\t\t= Input device selection =\n"
#ifdef RTLSDR
"\tRTL-SDR device driver is available.\n"
Expand All @@ -236,7 +238,7 @@ static void help_device_selection(void)
_Noreturn
static void help_gain(void)
{
term_help_printf(
term_help_fprintf(stdout,
"\t\t= Gain option =\n"
" [-g <gain>] (default: auto)\n"
"\tFor RTL-SDR: gain in dB (\"0\" is auto).\n"
Expand All @@ -248,7 +250,7 @@ static void help_gain(void)
_Noreturn
static void help_device_mode(void)
{
term_help_printf(
term_help_fprintf(stdout,
"\t\t= Input device run mode =\n"
" [-D restart | pause | quit | manual] Input device run mode options.\n"
"\tSupported input device run modes:\n"
Expand All @@ -263,7 +265,7 @@ static void help_device_mode(void)
_Noreturn
static void help_output(void)
{
term_help_printf(
term_help_fprintf(stdout,
"\t\t= Output format option =\n"
" [-F log|kv|json|csv|mqtt|influx|syslog|trigger|null] Produce decoded output in given format.\n"
"\tWithout this option the default is LOG and KV output. Use \"-F null\" to remove the default.\n"
Expand All @@ -289,7 +291,7 @@ static void help_output(void)
_Noreturn
static void help_tags(void)
{
term_help_printf(
term_help_fprintf(stdout,
"\t\t= Data tags option =\n"
" [-K FILE | PATH | <tag> | <key>=<tag>] Add an expanded token or fixed tag to every output line.\n"
"\tIf <tag> is \"FILE\" or \"PATH\" an expanded token will be added.\n"
Expand All @@ -310,7 +312,7 @@ static void help_tags(void)
_Noreturn
static void help_meta(void)
{
term_help_printf(
term_help_fprintf(stdout,
"\t\t= Meta information option =\n"
" [-M time[:<options>]|protocol|level|noise[:<secs>]|stats|bits] Add various metadata to every output line.\n"
"\tUse \"time\" to add current date and time meta data (preset for live inputs).\n"
Expand All @@ -336,7 +338,7 @@ static void help_meta(void)
_Noreturn
static void help_read(void)
{
term_help_printf(
term_help_fprintf(stdout,
"\t\t= Read file option =\n"
" [-r <filename>] Read data from input file instead of a receiver\n"
"\tParameters are detected from the full path, file name, and extension.\n\n"
Expand All @@ -358,7 +360,7 @@ static void help_read(void)
_Noreturn
static void help_write(void)
{
term_help_printf(
term_help_fprintf(stdout,
"\t\t= Write file option =\n"
" [-w <filename>] Save data stream to output file (a '-' dumps samples to stdout)\n"
" [-W <filename>] Save data stream to output file, overwrite existing file\n"
Expand Down
20 changes: 10 additions & 10 deletions src/term_ctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -403,14 +403,17 @@ int term_printf(void *ctx, _Printf_format_string_ char const *format, ...)
return len;
}

int term_help_puts(void *ctx, char const *buf)
int term_help_fputs(void *ctx, char const *buf, FILE *fp)
{
char const *p = buf;
int i, len, buf_len, color, state = 0, set_color = -1, next_color = -1;
FILE *fp;
if (!fp) {
fp = stderr;
}

if (!ctx)
return fprintf(stderr, "%s", buf);
if (!ctx) {
return fprintf(fp, "%s", buf);
}

#ifdef _WIN32
console_t *console = (console_t *)ctx;
Expand All @@ -419,9 +422,6 @@ int term_help_puts(void *ctx, char const *buf)
fp = (FILE *)ctx;
#endif

if (!fp)
fp = stderr;

buf_len = (int)strlen(buf);
for (i = len = 0; *p && i < buf_len; i++, p++) {
if (*p == '~') {
Expand Down Expand Up @@ -490,15 +490,15 @@ int term_help_puts(void *ctx, char const *buf)
return len;
}

int term_help_printf(_Printf_format_string_ char const *format, ...)
int term_help_fprintf(FILE *fp, _Printf_format_string_ char const *format, ...)
{
int len;
va_list args;
char buf[4000];

va_start(args, format);

void *term = term_init(stderr);
void *term = term_init(fp);
if (!term_has_color(term)) {
term_free(term);
term = NULL;
Expand All @@ -507,7 +507,7 @@ int term_help_printf(_Printf_format_string_ char const *format, ...)
// Terminate first in case a buggy '_MSC_VER < 1900' is used.
buf[sizeof(buf) - 1] = '\0';
vsnprintf(buf, sizeof(buf) - 1, format, args);
len = term_help_puts(term, buf);
len = term_help_fputs(term, buf, fp);

term_free(term);

Expand Down

0 comments on commit e2f8d62

Please sign in to comment.