From 3d15c5d7add07ca0e1fafb5496f68b6164eac277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20F=2E=20Aasen?= Date: Wed, 2 Aug 2023 15:56:04 +0200 Subject: [PATCH] man-page: Improve example Changes following: - Free memory in usage(). - Use POPT_TABLEEND macro when creating option table. - Use POPT_ARG_NONE instead of magic number in option table. - Cast 'argv' to const, fixes incompatible pointer warning. - Remove end-of-line whitespace. - Call usage() if to few command-line arguments. --- popt.3 | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/popt.3 b/popt.3 index 1ae0a8a7..14b6d569 100644 --- a/popt.3 +++ b/popt.3 @@ -754,6 +754,7 @@ at least some of the features of the extremely rich popt library. void usage(poptContext optCon, int exitcode, char *error, char *addl) { poptPrintUsage(optCon, stderr, 0); if (error) fprintf(stderr, "%s: %s\\n", error, addl); + poptFreeContext(optCon); exit(exitcode); } @@ -761,7 +762,7 @@ int main(int argc, char *argv[]) { int c; /* used for argument parsing */ int i = 0; /* used for tracking options */ int speed = 0; /* used in argument parsing to set speed */ - int raw = 0; /* raw mode? */ + int raw = 0; /* raw mode? */ int j; char buf[BUFSIZ+1]; const char *portname; @@ -770,35 +771,33 @@ int main(int argc, char *argv[]) { struct poptOption optionsTable[] = { { "bps", 'b', POPT_ARG_INT, &speed, 0, "signaling rate in bits-per-second", "BPS" }, - { "crnl", 'c', 0, 0, 'c', + { "crnl", 'c', POPT_ARG_NONE, 0, 'c', "expand cr characters to cr/lf sequences", NULL }, - { "hwflow", 'h', 0, 0, 'h', + { "hwflow", 'h', POPT_ARG_NONE, 0, 'h', "use hardware (RTS/CTS) flow control", NULL }, - { "noflow", 'n', 0, 0, 'n', + { "noflow", 'n', POPT_ARG_NONE, 0, 'n', "use no flow control", NULL }, - { "raw", 'r', 0, &raw, 0, + { "raw", 'r', POPT_ARG_NONE, &raw, 0, "don't perform any character conversions", NULL }, - { "swflow", 's', 0, 0, 's', + { "swflow", 's', POPT_ARG_NONE, 0, 's', "use software (XON/XOF) flow control", NULL } , POPT_AUTOHELP - { NULL, 0, 0, NULL, 0 } + POPT_TABLEEND }; - optCon = poptGetContext(NULL, argc, argv, optionsTable, 0); + optCon = poptGetContext(NULL, argc, (const char **) argv, optionsTable, 0); poptSetOtherOptionHelp(optCon, "[OPTIONS]* "); - if (argc < 2) { - poptPrintUsage(optCon, stderr, 0); - exit(1); - } + if (argc < 2) + usage(optCon, 1, 0, NULL); /* Now do options processing, get portname */ while ((c = poptGetNextOpt(optCon)) >= 0) { switch (c) { - case 'c': - buf[i++] = 'c'; + case 'c': + buf[i++] = 'c'; break; - case 'h': + case 'h': buf[i++] = 'h'; break; case 's': @@ -815,7 +814,7 @@ int main(int argc, char *argv[]) { if (c < -1) { /* an error occurred during option processing */ - fprintf(stderr, "%s: %s\\n", + fprintf(stderr, "%s: %s\\n", poptBadOption(optCon, POPT_BADOPTION_NOALIAS), poptStrerror(c)); return 1;