Skip to content

Commit

Permalink
man-page: Improve example
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
havardAasen authored and pmatilai committed Mar 4, 2024
1 parent 6e9eccb commit 3d15c5d
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions popt.3
Original file line number Diff line number Diff line change
Expand Up @@ -754,14 +754,15 @@ 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);
}

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;
Expand All @@ -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]* <port>");

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':
Expand All @@ -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;
Expand Down

0 comments on commit 3d15c5d

Please sign in to comment.