Mercurial > hg > dmlib
annotate src/dmargs_int.c @ 1315:7687412f9aef
Fix jssmod sample conversion flags storing .. urgh.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 20 Aug 2017 01:54:54 +0300 |
parents | e06abfde6c39 |
children |
rev | line source |
---|---|
0 | 1 /* |
2 * Simple commandline argument processing | |
3 * Programmed and designed by Matti 'ccr' Hamalainen | |
863 | 4 * (C) Copyright 2002-2015 Tecnic Software productions (TNSP) |
0 | 5 * |
6 * Please read file 'COPYING' for information on license and distribution. | |
7 */ | |
8 #ifndef TH_EXTERNAL | |
9 #include "th_util.h" | |
10 #include "th_args.h" | |
11 #include "th_string.h" | |
12 #endif | |
13 | |
14 | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
15 /* Parse long and short options |
0 | 16 */ |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
17 static BOOL th_args_process_opt( |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
18 char *currArg, int *argIndex, |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
19 int argc, char *argv[], |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
20 const th_optarg_t opts[], int numOpts, |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
21 BOOL (*handleOptionCB)(int, char *, char *), |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
22 BOOL doProcess, BOOL isLong) |
0 | 23 { |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
24 const th_optarg_t *opt = NULL; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
25 char *optArg = NULL; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
26 int optIndex; |
0 | 27 |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
28 for (optIndex = 0; optIndex < numOpts; optIndex++) |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
29 { |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
30 const th_optarg_t *node = &opts[optIndex]; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
31 if (isLong && node->optLong != NULL) |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
32 { |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
33 if (strcmp(currArg, node->optLong) == 0) |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
34 { |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
35 opt = node; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
36 optArg = NULL; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
37 break; |
0 | 38 } |
39 | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
40 size_t len = strlen(node->optLong); |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
41 if (strncmp(currArg, node->optLong, len) == 0 && |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
42 currArg[len] == '=') |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
43 { |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
44 opt = node; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
45 optArg = (&currArg[len+1] != 0) ? &currArg[len+1] : NULL; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
46 break; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
47 } |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
48 } |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
49 else |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
50 if (!isLong && node->optShort != 0) |
0 | 51 { |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
52 if (*currArg == node->optShort) |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
53 { |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
54 opt = node; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
55 optArg = (currArg[1] != 0) ? &currArg[1] : NULL; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
56 } |
0 | 57 } |
58 } | |
59 | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
60 if (opt != NULL) |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
61 { |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
62 // Check for the possible option argument |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
63 if ((opt->flags & OPT_ARGMASK) == OPT_ARGREQ && optArg == NULL) |
0 | 64 { |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
65 if (*argIndex < argc) |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
66 { |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
67 (*argIndex)++; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
68 optArg = argv[*argIndex]; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
69 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
895
diff
changeset
|
70 |
895
eb4ebbd5c171
Oops, fix argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
863
diff
changeset
|
71 if (optArg == NULL) |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
72 { |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
73 THERR("Option '%s%s' requires an argument.\n", |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
74 isLong ? "--" : "-", |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
75 currArg); |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
76 return FALSE; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
77 } |
0 | 78 } |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
895
diff
changeset
|
79 |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
80 // Option was given succesfully, try to process it |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
81 if (doProcess && !handleOptionCB(opt->id, optArg, currArg)) |
0 | 82 return FALSE; |
83 } | |
84 else | |
85 { | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
86 THERR("Unknown %s option '%s%s'\n", |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
87 isLong ? "long" : "short", |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
88 isLong ? "--" : "-", |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
89 currArg); |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
90 |
0 | 91 return FALSE; |
92 } | |
93 | |
94 return TRUE; | |
95 } | |
96 | |
97 | |
98 /* Process arguments, handling short and long options by | |
99 * calling the given callback functions. | |
100 */ | |
101 BOOL th_args_process(int argc, char *argv[], | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
102 const th_optarg_t *opts, const int numOpts, |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
103 BOOL(*handleOptionCB) (int, char *, char *), |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
104 BOOL(*handleOther) (char *), int flags) |
0 | 105 { |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
106 int argIndex, handleFlags = flags & OPTH_ONLY_MASK; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
107 BOOL optionsOK = TRUE, endOfOptions = FALSE; |
0 | 108 |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
109 for (argIndex = 1; argIndex < argc; argIndex++) |
0 | 110 { |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
111 char *str = argv[argIndex]; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
112 if (*str == '-' && !endOfOptions) |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
113 { |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
114 // Should we process options? |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
115 BOOL doProcess = (handleFlags & OPTH_ONLY_OPTS) || handleFlags == 0; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
116 BOOL isLong; |
0 | 117 |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
118 str++; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
119 if (*str == '-') |
0 | 120 { |
121 // Check for "--", which ends the options-list | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
122 str++; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
123 if (*str == 0) |
0 | 124 { |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
125 endOfOptions = TRUE; |
0 | 126 continue; |
127 } | |
128 | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
129 // We have a long option |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
130 isLong = TRUE; |
0 | 131 } |
132 else | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
133 isLong = FALSE; |
0 | 134 |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
135 if (!th_args_process_opt(str, &argIndex, argc, argv, |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
136 opts, numOpts, handleOptionCB, doProcess, isLong)) |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
137 optionsOK = FALSE; |
0 | 138 } |
139 else | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
140 if (handleFlags == OPTH_ONLY_OTHER || handleFlags == 0) |
0 | 141 { |
142 // Was not option argument | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
143 if (handleOther == NULL || |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
144 (handleOther != NULL && !handleOther(str))) |
0 | 145 { |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
146 THERR("Invalid argument '%s'\n", str); |
0 | 147 optionsOK = FALSE; |
148 } | |
149 } | |
150 | |
151 // Check if we bail out on invalid argument | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
152 if (!optionsOK && (flags & OPTH_BAILOUT)) |
0 | 153 return FALSE; |
154 } | |
155 | |
156 return optionsOK; | |
157 } | |
158 | |
159 | |
160 /* Print help for commandline arguments/options | |
161 */ | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
162 static void th_pad(FILE *outFile, int count) |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
163 { |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
164 while (count--) |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
165 fputc(' ', outFile); |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
166 } |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
167 |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
168 |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
169 static void th_print_wrap(FILE *fh, const char *str, int spad, int rpad, int width) |
0 | 170 { |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
171 size_t pos = 0; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
172 BOOL first = TRUE; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
173 |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
174 while (str[pos]) |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
175 { |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
176 // Pre-pad line |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
177 int linelen = first ? spad : rpad; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
178 th_pad(fh, first ? 0 : rpad); |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
179 first = FALSE; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
180 |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
181 // Skip whitespace at line start |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
182 while (isspace(str[pos]) || str[pos] == '\n') pos++; |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
895
diff
changeset
|
183 |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
184 // Handle each word |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
185 while (str[pos] && str[pos] != '\n') |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
186 { |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
187 size_t next; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
188 int wlen; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
189 for (wlen = 0, next = pos; str[next] && !isspace(str[next]) && str[next] != '\n'; next++, wlen++); |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
190 // fprintf(stdout, "X '%c', %d .. linelen=%d/%d, wlen=%d\n", str[pos], pos, linelen, width, wlen); |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
191 if (linelen + wlen < width) |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
192 { |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
193 for (;pos < next; pos++, linelen++) |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
194 fputc(str[pos], fh); |
0 | 195 |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
196 if (str[next] == '\n' || str[next] == 0) |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
197 { |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
198 fprintf(fh, "\n"); |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
199 break; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
200 } |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
201 else |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
202 { |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
203 fputc(str[pos], fh); |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
204 pos++; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
205 linelen++; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
206 } |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
207 } |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
208 else |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
209 { |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
210 fprintf(fh, "\n"); |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
211 break; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
212 } |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
213 } |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
214 } |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
215 } |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
216 |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
217 |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
218 void th_args_help(FILE *fh, |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
219 const th_optarg_t *opts, const int numOpts, |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
220 const int flags) |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
221 { |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
222 int index; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
223 (void) flags; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
224 |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
225 // Print out option list |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
226 for (index = 0; index < numOpts; index++) |
0 | 227 { |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
228 const th_optarg_t *opt = &opts[index]; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
229 char tmpStr[128]; |
0 | 230 |
231 // Print short option | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
232 if (opt->optShort != 0) |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
233 { |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
234 snprintf(tmpStr, sizeof(tmpStr), |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
235 "-%c,", opt->optShort); |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
236 } |
0 | 237 else |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
238 tmpStr[0] = 0; |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
239 |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
240 fprintf(fh, " %-5s", tmpStr); |
0 | 241 |
242 // Print long option | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
243 if (opt->optLong != NULL) |
0 | 244 { |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
245 snprintf(tmpStr, sizeof(tmpStr), "--%s%s", |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
246 opt->optLong, |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
247 (opt->flags & OPT_ARGREQ) ? "=ARG" : ""); |
0 | 248 } |
249 else | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
250 tmpStr[0] = 0; |
0 | 251 |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
252 fprintf(fh, "%-20s", tmpStr); |
0 | 253 |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
832
diff
changeset
|
254 th_print_wrap(fh, opt->desc, 26, 26, 73); |
0 | 255 } |
256 } |