From 5790d02ceb34a1fba35decc0d79778606482d41a Mon Sep 17 00:00:00 2001 From: Daniele Forsi Date: Mon, 20 Dec 2021 18:57:44 +0100 Subject: [PATCH] Automatically generate a man page Add --help and --version for help2man. --- .gitignore | 1 + Makefile | 5 ++++- rtlsdr_wsprd.c | 33 +++++++++++++++++++++++++++------ rtlsdr_wsprd.h | 2 +- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 47399d9..db9da29 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +*.1 *.o *.a *.iq diff --git a/Makefile b/Makefile index 14b689b..663cc11 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ LIBS = -lusb-1.0 -lrtlsdr -lpthread -lfftw3f -lcurl -lm OBJS = rtlsdr_wsprd.o wsprd/wsprd.o wsprd/wsprsim_utils.o wsprd/wsprd_utils.o wsprd/tab.o wsprd/fano.o wsprd/nhash.o -TARGETS = rtlsdr_wsprd +TARGETS = rtlsdr_wsprd rtlsdr_wsprd.1 .PHONY: all clean @@ -25,3 +25,6 @@ clean: install: install rtlsdr_wsprd /usr/local/bin/rtlsdr_wsprd + +%.1: %.c | % + -help2man --no-info --output=$@ ./$| diff --git a/rtlsdr_wsprd.c b/rtlsdr_wsprd.c index 2d2e2b9..f44557c 100644 --- a/rtlsdr_wsprd.c +++ b/rtlsdr_wsprd.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -746,8 +747,8 @@ int32_t decoderSelfTest() { } -void usage(void) { - fprintf(stderr, +void usage(FILE *stream, int32_t status) { + fprintf(stream, "rtlsdr_wsprd, a simple WSPR daemon for RTL receivers\n\n" "Use:\trtlsdr_wsprd -f frequency -c callsign -l locator [options]\n" "\t-f dial frequency [(,k,M) Hz] or band string\n" @@ -773,14 +774,24 @@ void usage(void) { "\t-w write received signal and exit [filename prefix]\n" "\t-r read signal with .iq or .c2 format, decode and exit [filename]\n" "\t (raw format: 375sps, float 32 bits, 2 channels)\n" + "Other options:\n" + "\t--help show list of options\n" + "\t--version show version of program\n" "Example:\n" "\trtlsdr_wsprd -f 2m -c A1XYZ -l AB12cd -g 29 -o -4200\n"); - exit(1); + exit(status); } int main(int argc, char **argv) { uint32_t opt; + char *short_options = "f:c:l:g:ao:p:u:d:n:i:tw:r:HQS"; + int32_t option_index = 0; + struct option long_options[] = { + {"help", no_argument, 0, 0 }, + {"version", no_argument, 0, 0 }, + {0, 0, 0, 0 } + }; int32_t rtl_result; int32_t rtl_count; @@ -794,10 +805,20 @@ int main(int argc, char **argv) { uint32_t nLoop = 0; if (argc <= 1) - usage(); + usage(stderr, 1); - while ((opt = getopt(argc, argv, "f:c:l:g:ao:p:u:d:n:i:tw:r:HQS")) != -1) { + while ((opt = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1) { switch (opt) { + case 0: + switch (option_index) { + case 0: // --help + usage(stdout, 0); + break; + case 1: // --version + printf("rtlsdr_wsprd 0.5.3\n"); + exit(0); + break; + } case 'f': // Frequency if (!strcasecmp(optarg, "LF")) { rx_options.dialfreq = 136000; @@ -919,7 +940,7 @@ int main(int argc, char **argv) { rx_options.filename = optarg; break; default: - usage(); + usage(stderr, 1); break; } } diff --git a/rtlsdr_wsprd.h b/rtlsdr_wsprd.h index c9146d7..a9c6439 100644 --- a/rtlsdr_wsprd.h +++ b/rtlsdr_wsprd.h @@ -46,4 +46,4 @@ int32_t readC2file(float *iSamples, float *qSamples, char *filename); void decodeRecordedFile(char *filename); float whiteGaussianNoise(float factor); int32_t decoderSelfTest(); -void usage(void); +void usage(FILE *stream, int32_t status);