annotate th_args.c @ 143:c878cdcfea9d

Implement terminal width.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 07 Dec 2014 20:49:10 +0200
parents 0a4fd9cfb929
children fdea1c3acc53
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
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
4 * (C) Copyright 2002-2014 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 */
50
1fa969d0f551 Sync changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
8 #ifndef TH_EXTERNAL
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 #include "th_util.h"
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 #include "th_args.h"
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 #include "th_string.h"
49
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 47
diff changeset
12 #endif
0
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
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
15 /* Parse long and short options
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 */
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
17 static BOOL th_args_process_opt(
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
18 char *currArg, int *argIndex,
136
286b2249c5d2 Clean up th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents: 128
diff changeset
19 int argc, char *argv[],
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
20 const th_optarg_t opts[], int numOpts,
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
21 BOOL (*handleOptionCB)(int, char *, char *),
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
22 BOOL doProcess, BOOL isLong)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
24 const th_optarg_t *opt = NULL;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
25 char *optArg = NULL;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
26 int optIndex;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
27
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
28 for (optIndex = 0; optIndex < numOpts; optIndex++)
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
29 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
30 const th_optarg_t *node = &opts[optIndex];
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
31 if (isLong && node->optLong != NULL)
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
32 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
33 if (strcmp(currArg, node->optLong) == 0)
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
34 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
35 opt = node;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
36 optArg = NULL;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
37 break;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
38 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
39
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
40 size_t len = strlen(node->optLong);
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
41 if (strncmp(currArg, node->optLong, len) == 0 &&
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
42 currArg[len] == '=')
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
43 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
44 opt = node;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
45 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
46 break;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
47 }
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
48 }
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
49 else
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
50 if (!isLong && node->optShort != 0)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
51 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
52 if (*currArg == node->optShort)
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
53 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
54 opt = node;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
55 optArg = (currArg[1] != 0) ? &currArg[1] : NULL;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
56 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
57 }
136
286b2249c5d2 Clean up th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents: 128
diff changeset
58 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
59
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
60 if (opt != NULL)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
61 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
62 // Check for the possible option argument
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
63 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
64 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
65 if (*argIndex < argc)
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
66 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
67 (*argIndex)++;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
68 optArg = argv[*argIndex];
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
69 }
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
70 else
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
71 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
72 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
73 isLong ? "--" : "-",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
74 currArg);
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
75 return FALSE;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
76 }
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
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
79 // Option was given succesfully, try to process it
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
80 if (doProcess && !handleOptionCB(opt->id, optArg, currArg))
136
286b2249c5d2 Clean up th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents: 128
diff changeset
81 return FALSE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
82 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
83 else
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
84 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
85 THERR("Unknown %s option '%s%s'\n",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
86 isLong ? "long" : "short",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
87 isLong ? "--" : "-",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
88 currArg);
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
89
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
90 return FALSE;
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
91 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
93 return TRUE;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 /* Process arguments, handling short and long options by
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 * calling the given callback functions.
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 */
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 BOOL th_args_process(int argc, char *argv[],
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
101 const th_optarg_t *opts, const int numOpts,
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
102 BOOL(*handleOptionCB) (int, char *, char *),
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
103 BOOL(*handleOther) (char *), const int flags)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
105 int argIndex, handleFlags = flags & OPTH_ONLY_MASK;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
106 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
107
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
108 for (argIndex = 1; argIndex < argc; argIndex++)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
109 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
110 char *str = argv[argIndex];
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
111 if (*str == '-' && !endOfOptions)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
112 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
113 // Should we process options?
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
114 BOOL doProcess = (handleFlags & OPTH_ONLY_OPTS) || handleFlags == 0;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
115 BOOL isLong;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
116
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
117 str++;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
118 if (*str == '-')
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
119 {
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
120 // 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
121 str++;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
122 if (*str == 0)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
123 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
124 endOfOptions = TRUE;
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
125 continue;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
126 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
127
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
128 // We have a long option
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
129 isLong = TRUE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
130 }
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
131 else
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
132 isLong = FALSE;
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
133
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
134 if (!th_args_process_opt(str, &argIndex, argc, argv,
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
135 opts, numOpts, handleOptionCB, doProcess, isLong))
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
136 optionsOK = FALSE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
137 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
138 else
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
139 if (handleFlags == OPTH_ONLY_OTHER || handleFlags == 0)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
140 {
128
c22caa6e3fcb Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 127
diff changeset
141 // Was not option argument
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
142 if (handleOther == NULL ||
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
143 (handleOther != NULL && !handleOther(str)))
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
144 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
145 THERR("Invalid argument '%s'\n", str);
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
146 optionsOK = FALSE;
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
147 }
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
128
c22caa6e3fcb Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 127
diff changeset
150 // Check if we bail out on invalid argument
137
0f43a94516f4 Improve argument handling module.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
151 if (!optionsOK && (flags & OPTH_BAILOUT))
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
152 return FALSE;
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
153 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
154
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
155 return optionsOK;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
156 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
157
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
158
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159 /* Print help for commandline arguments/options
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
160 */
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
161 static void th_pad(FILE *outFile, int count)
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
162 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
163 while (count--)
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
164 fputc(' ', outFile);
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
165 }
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
166
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
167
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
168 static void th_print_wrap(FILE *fh, const char *str, int spad, int rpad, int width)
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
169 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
170 size_t pos = 0;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
171 BOOL first = TRUE;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
172
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
173 while (str[pos])
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
174 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
175 // Pre-pad line
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
176 int linelen = first ? spad : rpad;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
177 th_pad(fh, first ? 0 : rpad);
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
178 first = FALSE;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
179
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
180 // Skip whitespace at line start
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
181 while (th_isspace(str[pos]) || str[pos] == '\n') pos++;
143
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 142
diff changeset
182
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
183 // Handle each word
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
184 while (str[pos] && str[pos] != '\n')
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
185 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
186 size_t next;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
187 int wlen;
143
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 142
diff changeset
188
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 142
diff changeset
189 // Find word length and next break
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
190 for (wlen = 0, next = pos; str[next] && !th_isspace(str[next]) && str[next] != '\n'; next++, wlen++);
143
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 142
diff changeset
191
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 142
diff changeset
192 // Check if we have too much of text?
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 142
diff changeset
193 if (linelen + wlen >= width)
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 142
diff changeset
194 break;
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
195
143
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 142
diff changeset
196 // Print what we have
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 142
diff changeset
197 for (;pos < next; pos++, linelen++)
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 142
diff changeset
198 fputc(str[pos], fh);
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 142
diff changeset
199
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 142
diff changeset
200 // Check if we are at end of input or hard linefeed
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 142
diff changeset
201 if (str[next] == '\n' || str[next] == 0)
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 142
diff changeset
202 break;
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
203 else
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
204 {
143
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 142
diff changeset
205 fputc(str[pos], fh);
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 142
diff changeset
206 pos++;
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 142
diff changeset
207 linelen++;
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
208 }
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
209 }
143
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 142
diff changeset
210 fprintf(fh, "\n");
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
211 }
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
212 }
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
213
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
214
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
215 void th_args_help(FILE *fh,
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
216 const th_optarg_t *opts, const int numOpts,
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
217 const int flags)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
218 {
136
286b2249c5d2 Clean up th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents: 128
diff changeset
219 int index;
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
220 (void) flags;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
221
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
222 // Print out option list
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
223 for (index = 0; index < numOpts; index++)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
224 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
225 const th_optarg_t *opt = &opts[index];
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
226 char tmpStr[128];
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
227
128
c22caa6e3fcb Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 127
diff changeset
228 // Print short option
136
286b2249c5d2 Clean up th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents: 128
diff changeset
229 if (opt->optShort != 0)
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
230 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
231 snprintf(tmpStr, sizeof(tmpStr),
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
232 "-%c,", opt->optShort);
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
233 }
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
234 else
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
235 tmpStr[0] = 0;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
236
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
237 fprintf(fh, " %-5s", tmpStr);
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
238
128
c22caa6e3fcb Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 127
diff changeset
239 // Print long option
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
240 if (opt->optLong != NULL)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
241 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
242 snprintf(tmpStr, sizeof(tmpStr), "--%s%s",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
243 opt->optLong,
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
244 (opt->flags & OPT_ARGREQ) ? "=ARG" : "");
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
245 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
246 else
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
247 tmpStr[0] = 0;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
248
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
249 fprintf(fh, "%-20s", tmpStr);
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
250
143
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 142
diff changeset
251 th_print_wrap(fh, opt->desc, 26, 26, th_term_width() - 2);
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
252 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
253 }