annotate th_args.c @ 616:594f197f7005

Slight improvements to the almost non-existing Doxygen documentation.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 17 Jan 2020 03:18:40 +0200
parents 1e9fa00cc63f
children 6d99150a8f89
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * Simple commandline argument processing
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Programmed and designed by Matti 'ccr' Hamalainen
553
3a852e9f70a6 Bump copyright years.
Matti Hamalainen <ccr@tnsp.org>
parents: 481
diff changeset
4 * (C) Copyright 2002-2020 Tecnic Software productions (TNSP)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 *
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 * Please read file 'COPYING' for information on license and distribution.
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 */
436
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
8 /// @file
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
9 /// @brief Simple commandline argument processing functions
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 #include "th_util.h"
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 #include "th_args.h"
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 #include "th_string.h"
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14
436
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
15 /**
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
16 * Parse and optionally handle the given long or short option argument.
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
17 * @param currArg current argument string
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
18 * @param[in,out] argIndex pointer to index of current argument in argv[]
436
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
19 * @param argc number of arguments
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
20 * @param argv argument string array
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
21 * @param[in] opts options list array
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
22 * @param[in] nopts number of elements in options list array
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
23 * @param[in] handle_option function pointer to callback that handles option arguments
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
24 * @param[in] doProcess if TRUE, actually handle the argument, aka call the handle_option() function. if FALSE, only validity of options are checked.
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
25 * @param[in] isLong TRUE if the option is a --long-format one
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
26 * @returns returns @c TRUE if option processing was successful
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 */
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
28 static BOOL th_args_process_opt(
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
29 char *currArg, int *argIndex,
136
286b2249c5d2 Clean up th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents: 128
diff changeset
30 int argc, char *argv[],
380
ac10155d2b4a Rename th_optarg_t to t_optarg. API break.
Matti Hamalainen <ccr@tnsp.org>
parents: 379
diff changeset
31 const th_optarg opts[], int nopts,
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
32 BOOL (*handle_option)(int id, char *, char *),
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
33 BOOL doProcess, BOOL isLong)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 {
380
ac10155d2b4a Rename th_optarg_t to t_optarg. API break.
Matti Hamalainen <ccr@tnsp.org>
parents: 379
diff changeset
35 const th_optarg *opt = NULL;
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
36 char *optArg = NULL;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
37 int optIndex;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
38
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
39 for (optIndex = 0; optIndex < nopts; optIndex++)
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
40 {
380
ac10155d2b4a Rename th_optarg_t to t_optarg. API break.
Matti Hamalainen <ccr@tnsp.org>
parents: 379
diff changeset
41 const th_optarg *node = &opts[optIndex];
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
42 if (isLong && node->o_long != NULL)
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
43 {
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
44 if (strcmp(currArg, node->o_long) == 0)
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
45 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
46 opt = node;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
47 optArg = NULL;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
48 break;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
49 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
50
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
51 size_t len = strlen(node->o_long);
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
52 if (strncmp(currArg, node->o_long, len) == 0 &&
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
53 currArg[len] == '=')
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
54 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
55 opt = node;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
56 optArg = (&currArg[len+1] != 0) ? &currArg[len+1] : NULL;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
57 break;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
58 }
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
59 }
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
60 else
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
61 if (!isLong && node->o_short != 0)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
62 {
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
63 if (*currArg == node->o_short)
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
64 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
65 opt = node;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
66 optArg = (currArg[1] != 0) ? &currArg[1] : NULL;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
67 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
68 }
136
286b2249c5d2 Clean up th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents: 128
diff changeset
69 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
70
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
71 if (opt != NULL)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
72 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
73 // Check for the possible option argument
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
74 if ((opt->flags & OPT_ARGMASK) == OPT_ARGREQ && optArg == NULL)
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
75 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
76 if (*argIndex < argc)
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
77 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
78 (*argIndex)++;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
79 optArg = argv[*argIndex];
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
80 }
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 436
diff changeset
81
173
503f0cb98775 Fix handling of option arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 161
diff changeset
82 if (optArg == NULL)
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
83 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
84 THERR("Option '%s%s' requires an argument.\n",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
85 isLong ? "--" : "-",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
86 currArg);
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
87 return FALSE;
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
88 }
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
89 }
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 436
diff changeset
90
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
91 // Option was given succesfully, try to process it
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
92 if (doProcess && !handle_option(opt->id, optArg, currArg))
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
93 return FALSE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
94 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
95 else
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
96 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
97 THERR("Unknown %s option '%s%s'\n",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
98 isLong ? "long" : "short",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
99 isLong ? "--" : "-",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
100 currArg);
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
101
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
102 return FALSE;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
103 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
105 return TRUE;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108
436
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
109 /**
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
110 * Process given array of commandline arguments, handling short
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
111 * and long options by calling the respective callback functions.
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
112 *
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
113 * @param argc number of arguments
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
114 * @param argv argument list
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
115 * @param[in] opts supported option list array
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
116 * @param[in] nopts number of elements in the option list array
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
117 * @param[in] handle_option callback function
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
118 * @param[in] handle_other callback function
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
119 * @param[in] flags processing flags
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
120 * @return returns @c TRUE if arguments were processed successfully
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 */
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
122 BOOL th_args_process(int argc, char *argv[],
380
ac10155d2b4a Rename th_optarg_t to t_optarg. API break.
Matti Hamalainen <ccr@tnsp.org>
parents: 379
diff changeset
123 const th_optarg *opts, const int nopts,
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
124 BOOL(*handle_option)(int id, char *, char *),
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
125 BOOL(*handle_other)(char *), const int flags)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
127 int argIndex, handleFlags = flags & OPTH_ONLY_MASK;
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
128 BOOL optionsOK = TRUE, endOfOptions = FALSE;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
130 for (argIndex = 1; argIndex < argc; argIndex++)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
131 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
132 char *str = argv[argIndex];
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
133 if (*str == '-' && !endOfOptions)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
134 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
135 // Should we process options?
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
136 BOOL doProcess = (handleFlags & OPTH_ONLY_OPTS) || handleFlags == 0;
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
137 BOOL isLong;
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
138
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
139 str++;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
140 if (*str == '-')
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
141 {
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
142 // Check for "--", which ends the options-list
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
143 str++;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
144 if (*str == 0)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
145 {
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
146 endOfOptions = TRUE;
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
147 continue;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
148 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
149
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
150 // We have a long option
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
151 isLong = TRUE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
152 }
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
153 else
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
154 isLong = FALSE;
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
155
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
156 if (!th_args_process_opt(str, &argIndex, argc, argv,
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
157 opts, nopts, handle_option, doProcess, isLong))
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
158 optionsOK = FALSE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
159 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
160 else
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
161 if (handleFlags == OPTH_ONLY_OTHER || handleFlags == 0)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
162 {
128
c22caa6e3fcb Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 127
diff changeset
163 // Was not option argument
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
164 if (handle_other == NULL ||
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
165 (handle_other != NULL && !handle_other(str)))
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
166 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
167 THERR("Invalid argument '%s'\n", str);
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
168 optionsOK = FALSE;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
169 }
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
170 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
171
128
c22caa6e3fcb Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 127
diff changeset
172 // Check if we bail out on invalid argument
137
0f43a94516f4 Improve argument handling module.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
173 if (!optionsOK && (flags & OPTH_BAILOUT))
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
174 return FALSE;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
175 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
177 return optionsOK;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
178 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
179
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
180
584
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
181 void th_print_pad(FILE *fh, int count, const char och)
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
182 {
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
183 while (count--)
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
184 fputc(och, fh);
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
185 }
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
186
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
187
585
1a4359ecc5d5 Doxygen th_print_wrap().
Matti Hamalainen <ccr@tnsp.org>
parents: 584
diff changeset
188 /**
1a4359ecc5d5 Doxygen th_print_wrap().
Matti Hamalainen <ccr@tnsp.org>
parents: 584
diff changeset
189 * Print given string indented in such a way that it is automatically
1a4359ecc5d5 Doxygen th_print_wrap().
Matti Hamalainen <ccr@tnsp.org>
parents: 584
diff changeset
190 * line-wrapped as necessary, taking hard linefeeds into account as well.
1a4359ecc5d5 Doxygen th_print_wrap().
Matti Hamalainen <ccr@tnsp.org>
parents: 584
diff changeset
191 * @param fh stdio file handle to output to
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
192 * @param[in] spad starting pad/indent of the first line
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
193 * @param[in] rpad how much to pad the other lines
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
194 * @param[in] width total line width to wrap at
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
195 * @param[in] str string to output
585
1a4359ecc5d5 Doxygen th_print_wrap().
Matti Hamalainen <ccr@tnsp.org>
parents: 584
diff changeset
196 */
588
ef71ef9b5862 Change th_print_wrap() API and move the string argument last.
Matti Hamalainen <ccr@tnsp.org>
parents: 587
diff changeset
197 void th_print_wrap(FILE *fh, const int spad, int const rpad,
ef71ef9b5862 Change th_print_wrap() API and move the string argument last.
Matti Hamalainen <ccr@tnsp.org>
parents: 587
diff changeset
198 const int width, const char *str)
584
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
199 {
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
200 size_t pos = 0;
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
201 BOOL first = TRUE;
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
202
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
203 while (str[pos])
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
204 {
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
205 // Pre-pad line
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
206 int linelen = first ? spad : rpad;
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
207 th_print_pad(fh, first ? 0 : rpad, ' ');
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
208 first = FALSE;
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
209
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
210 // Skip whitespace at line start
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
211 while (th_isspace(str[pos]) || str[pos] == '\n')
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
212 pos++;
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
213
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
214 // Handle each word
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
215 while (str[pos] && str[pos] != '\n')
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
216 {
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
217 size_t next;
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
218 int wlen;
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
219
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
220 // Find word length and next break
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
221 for (wlen = 0, next = pos;
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
222 str[next] && !th_isspace(str[next]) &&
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
223 str[next] != '\n';
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
224 next++, wlen++);
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
225
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
226 // Check if we have too much of text?
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
227 if (linelen + wlen >= width)
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
228 break;
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
229
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
230 // Print what we have
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
231 for (;pos < next; pos++, linelen++)
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
232 fputc(str[pos], fh);
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
233
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
234 // Check if we are at end of input or hard linefeed
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
235 if (str[next] == '\n' || str[next] == 0)
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
236 break;
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
237 else
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
238 {
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
239 fputc(str[pos], fh);
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
240 pos++;
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
241 linelen++;
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
242 }
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
243 }
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
244 fprintf(fh, "\n");
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
245 }
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
246 }
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
247
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
248
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
249 static inline const char *th_args_get_optarg(const th_optarg *opt)
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
250 {
587
98812eb78d8e Add new compile-time optional th_optarg struct field o_arg for specifying the option argument description/name. TH_USE_OPT_ARG must be defined at compile time.
Matti Hamalainen <ccr@tnsp.org>
parents: 586
diff changeset
251 #ifdef TH_USE_OPT_ARG
98812eb78d8e Add new compile-time optional th_optarg struct field o_arg for specifying the option argument description/name. TH_USE_OPT_ARG must be defined at compile time.
Matti Hamalainen <ccr@tnsp.org>
parents: 586
diff changeset
252 return opt->o_arg != NULL ? opt->o_arg : "ARG";
98812eb78d8e Add new compile-time optional th_optarg struct field o_arg for specifying the option argument description/name. TH_USE_OPT_ARG must be defined at compile time.
Matti Hamalainen <ccr@tnsp.org>
parents: 586
diff changeset
253 #else
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
254 (void) opt;
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
255 return "ARG";
587
98812eb78d8e Add new compile-time optional th_optarg struct field o_arg for specifying the option argument description/name. TH_USE_OPT_ARG must be defined at compile time.
Matti Hamalainen <ccr@tnsp.org>
parents: 586
diff changeset
256 #endif
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
257 }
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
258
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
259
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
260 static void th_args_help_print_item(FILE *fh, const th_optarg *opt,
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
261 int *optWidth, const int maxOptWidth, const int termWidth,
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
262 const BOOL doPrint)
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
263 {
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
264 const char *arg = th_args_get_optarg(opt);
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
265 char fmtBuf[32];
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
266 int padWidth;
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
267 BOOL hasLongOpt = opt->o_long != NULL;
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
268
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
269 if (opt->o_short != 0)
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
270 {
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
271 if (!hasLongOpt && (opt->flags & OPT_ARGREQ))
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
272 {
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
273 snprintf(fmtBuf, sizeof(fmtBuf), " -%c <%s>",
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
274 opt->o_short, arg);
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
275 }
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
276 else
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
277 {
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
278 snprintf(fmtBuf, sizeof(fmtBuf), " -%c,",
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
279 opt->o_short);
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
280 }
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
281
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
282 *optWidth = strlen(fmtBuf);
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
283 if (doPrint)
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
284 padWidth = hasLongOpt ? 2 : maxOptWidth - *optWidth;
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
285 else
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
286 padWidth = 2;
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
287 }
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
288 else
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
289 {
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
290 fmtBuf[0] = 0;
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
291 *optWidth = 0;
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
292 padWidth = 4 + 2;
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
293 }
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
294
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
295 if (doPrint)
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
296 {
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
297 fputs(fmtBuf, fh);
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
298 th_print_pad(fh, padWidth, ' ');
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
299 }
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
300 *optWidth += padWidth;
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
301
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
302 if (hasLongOpt)
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
303 {
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
304 if (opt->flags & OPT_ARGREQ)
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
305 {
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
306 snprintf(fmtBuf, sizeof(fmtBuf), "--%s=<%s>",
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
307 opt->o_long, arg);
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
308 }
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
309 else
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
310 {
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
311 snprintf(fmtBuf, sizeof(fmtBuf), "--%s",
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
312 opt->o_long);
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
313 }
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
314
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
315 *optWidth += strlen(fmtBuf);
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
316 }
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
317 else
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
318 fmtBuf[0] = 0;
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
319
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
320 if (doPrint)
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
321 {
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
322 padWidth = hasLongOpt ? maxOptWidth - *optWidth : 0;
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
323 *optWidth += padWidth;
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
324
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
325 fputs(fmtBuf, fh);
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
326 th_print_pad(fh, padWidth, ' ');
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
327 th_print_wrap(fh, *optWidth, *optWidth, termWidth, opt->desc);
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
328 }
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
329 }
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
330
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
331
436
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
332 /**
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
333 * Print help for commandline arguments/options
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
334 * @param fh stdio file handle to output to
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
335 * @param[in] opts options list array
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
336 * @param[in] nopts number of elements in options list array
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
337 * @param[in] flags flags (currently unused)
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
338 * @param[in] width width of the terminal or desired width of the print out
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
339 */
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
340 void th_args_help(FILE *fh, const th_optarg *opts,
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
341 const int nopts, const int flags, const int width)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
342 {
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
343 int index, maxOptWidth;
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
344 (void) flags;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
345
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
346 // Determine width of the options and arguments
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
347 maxOptWidth = 0;
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
348 for (index = 0; index < nopts; index++)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
349 {
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
350 int optWidth = 0;
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
351 th_args_help_print_item(NULL, &opts[index], &optWidth, 0, width, FALSE);
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
352 if (optWidth > maxOptWidth)
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
353 maxOptWidth = optWidth;
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
354 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
355
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
356 maxOptWidth += 2;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
357
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
358 // Print out the formatted option list
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
359 for (index = 0; index < nopts; index++)
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
360 {
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
361 int optWidth;
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
362 th_args_help_print_item(fh, &opts[index], &optWidth, maxOptWidth, width, TRUE);
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
363 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
364 }