annotate src/dmargs.c @ 2426:bb7264ddaefd

Fix the check for --long option argument.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 21 Jan 2020 07:54:54 +0200
parents 85b6d7ce8ca5
children b7f622d39efc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
1 /*
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
2 * Simple commandline argument processing
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
3 * Programmed and designed by Matti 'ccr' Hamalainen
2361
c801995cbb13 Bump copyright years.
Matti Hamalainen <ccr@tnsp.org>
parents: 1743
diff changeset
4 * (C) Copyright 2002-2020 Tecnic Software productions (TNSP)
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
5 *
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
6 * Please read file 'COPYING' for information on license and distribution.
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
7 */
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
8 /// @file
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
9 /// @brief Simple commandline argument processing functions
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 #include "dmargs.h"
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
11
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
12
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
13 /**
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
14 * Parse and optionally handle the given long or short option argument.
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
15 * @param currArg current argument string
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
16 * @param argIndex pointer to index of current argument in argv[]
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
17 * @param argc number of arguments
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
18 * @param argv argument string array
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
19 * @param opts options list array
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
20 * @param nopts number of elements in options list array
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
21 * @param handle_option function pointer to callback that handles option arguments
2425
85b6d7ce8ca5 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 2423
diff changeset
22 * @param process if TRUE, actually handle the argument, aka call the handle_option() function. if FALSE, only validity of options are checked.
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
23 * @param isLong TRUE if the option is a --long-format one
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
24 */
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
25 static BOOL dmArgsProcessOpt(
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
26 char *currArg, int *argIndex,
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
27 int argc, char *argv[],
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
28 const DMOptArg opts[], int nopts,
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
29 BOOL (*handle_option)(int id, char *, char *),
2425
85b6d7ce8ca5 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 2423
diff changeset
30 BOOL process, BOOL isLong)
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
31 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
32 const DMOptArg *opt = NULL;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
33 char *optArg = NULL;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
34
2425
85b6d7ce8ca5 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 2423
diff changeset
35 for (int optIndex = 0; optIndex < nopts; optIndex++)
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
36 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
37 const DMOptArg *node = &opts[optIndex];
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
38 if (isLong && node->o_long != NULL)
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
39 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
40 if (strcmp(currArg, node->o_long) == 0)
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
41 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
42 opt = node;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
43 optArg = NULL;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
44 break;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
45 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
46
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
47 size_t len = strlen(node->o_long);
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
48 if (strncmp(currArg, node->o_long, len) == 0 &&
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
49 currArg[len] == '=')
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
50 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
51 opt = node;
2426
bb7264ddaefd Fix the check for --long option argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 2425
diff changeset
52 optArg = (currArg[len+1] != 0) ? &currArg[len+1] : NULL;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
53 break;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
54 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
55 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
56 else
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
57 if (!isLong && node->o_short != 0)
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
58 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
59 if (*currArg == node->o_short)
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
60 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
61 opt = node;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
62 optArg = (currArg[1] != 0) ? &currArg[1] : NULL;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
63 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
64 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
65 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
66
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
67 if (opt != NULL)
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
68 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
69 // Check for the possible option argument
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
70 if ((opt->flags & OPT_ARGMASK) == OPT_ARGREQ && optArg == NULL)
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
71 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
72 if (*argIndex < argc)
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
73 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
74 (*argIndex)++;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
75 optArg = argv[*argIndex];
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
76 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
77
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
78 if (optArg == NULL)
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
79 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
80 dmErrorMsg("Option '%s%s' requires an argument.\n",
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
81 isLong ? "--" : "-",
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
82 currArg);
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
83 return FALSE;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
84 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
85 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
86
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
87 // Option was given succesfully, try to process it
2425
85b6d7ce8ca5 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 2423
diff changeset
88 if (process && !handle_option(opt->id, optArg, currArg))
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
89 return FALSE;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
90 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
91 else
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
92 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
93 dmErrorMsg("Unknown %s option '%s%s'\n",
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
94 isLong ? "long" : "short",
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
95 isLong ? "--" : "-",
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
96 currArg);
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
97
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
98 return FALSE;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
99 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
100
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
101 return TRUE;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
102 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
103
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
104
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
105 /**
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
106 * Process given array of commandline arguments, handling short
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
107 * and long options by calling the respective callback functions.
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
108 *
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
109 * @param argc number of arguments
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
110 * @param argv argument list
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
111 * @param opts supported option list array
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
112 * @param nopts number of elements in the option list array
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
113 * @param handle_option callback function
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
114 * @param handle_other callback function
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
115 * @param flags processing flags
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
116 * @return return TRUE if all is well
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
117 */
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
118 BOOL dmArgsProcess(int argc, char *argv[],
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
119 const DMOptArg *opts, const int nopts,
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
120 BOOL(*handle_option)(int id, char *, char *),
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
121 BOOL(*handle_other)(char *), const int flags)
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
122 {
2425
85b6d7ce8ca5 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 2423
diff changeset
123 int handleFlags = flags & OPTH_ONLY_MASK;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
124 BOOL optionsOK = TRUE, endOfOptions = FALSE;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
125
2425
85b6d7ce8ca5 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 2423
diff changeset
126 for (int argIndex = 1; argIndex < argc; argIndex++)
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
127 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
128 char *str = argv[argIndex];
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
129 if (*str == '-' && !endOfOptions)
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
130 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
131 // Should we process options?
2425
85b6d7ce8ca5 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 2423
diff changeset
132 BOOL process = (handleFlags & OPTH_ONLY_OPTS) || handleFlags == 0;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
133 BOOL isLong;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
135 str++;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
136 if (*str == '-')
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
137 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
138 // Check for "--", which ends the options-list
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
139 str++;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
140 if (*str == 0)
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
141 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
142 endOfOptions = TRUE;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
143 continue;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
144 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
145
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
146 // We have a long option
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
147 isLong = TRUE;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
148 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
149 else
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
150 isLong = FALSE;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
151
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
152 if (!dmArgsProcessOpt(str, &argIndex, argc, argv,
2425
85b6d7ce8ca5 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 2423
diff changeset
153 opts, nopts, handle_option, process, isLong))
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
154 optionsOK = FALSE;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
155 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
156 else
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
157 if (handleFlags == OPTH_ONLY_OTHER || handleFlags == 0)
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
158 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
159 // Was not option argument
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
160 if (handle_other == NULL ||
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
161 (handle_other != NULL && !handle_other(str)))
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
162 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
163 dmErrorMsg("Invalid argument '%s'\n", str);
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
164 optionsOK = FALSE;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
165 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
166 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
167
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
168 // Check if we bail out on invalid argument
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
169 if (!optionsOK && (flags & OPTH_BAILOUT))
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
170 return FALSE;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
171 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
172
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
173 return optionsOK;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
174 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
175
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
176
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
177 void dmPrintPad(FILE *fh, int count, const char och)
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
178 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
179 while (count--)
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
180 fputc(och, fh);
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
181 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
182
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
183
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
184 /**
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
185 * Print given string indented in such a way that it is automatically
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
186 * line-wrapped as necessary, taking hard linefeeds into account as well.
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
187 * @param fh stdio file handle to output to
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
188 * @param spad starting pad/indent of the first line
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
189 * @param rpad how much to pad the other lines
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
190 * @param width total line width to wrap at
2401
263093248f26 Change dmPrintWrap() API and move the string argument last.
Matti Hamalainen <ccr@tnsp.org>
parents: 2400
diff changeset
191 * @param str string to output
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
192 */
2401
263093248f26 Change dmPrintWrap() API and move the string argument last.
Matti Hamalainen <ccr@tnsp.org>
parents: 2400
diff changeset
193 void dmPrintWrap(FILE *fh, const int spad, int const rpad,
263093248f26 Change dmPrintWrap() API and move the string argument last.
Matti Hamalainen <ccr@tnsp.org>
parents: 2400
diff changeset
194 const int width, const char *str)
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
195 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
196 size_t pos = 0;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
197 BOOL first = TRUE;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
198
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
199 while (str[pos])
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
200 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
201 // Pre-pad line
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
202 int linelen = first ? spad : rpad;
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
203 dmPrintPad(fh, first ? 0 : rpad, ' ');
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
204 first = FALSE;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
205
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
206 // Skip whitespace at line start
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
207 while (isspace(str[pos]) || str[pos] == '\n')
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
208 pos++;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
209
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
210 // Handle each word
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
211 while (str[pos] && str[pos] != '\n')
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
212 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
213 size_t next;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
214 int wlen;
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
215
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
216 // Find word length and next break
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
217 for (wlen = 0, next = pos;
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
218 str[next] && !isspace(str[next]) &&
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
219 str[next] != '\n';
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
220 next++, wlen++);
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
221
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
222 // Check if we have too much of text?
2423
8d057a2459e3 Adjust options help width check, it was off-by-one.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
223 if (linelen + wlen > width)
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
224 break;
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
225
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
226 // Print what we have
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
227 for (;pos < next; pos++, linelen++)
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
228 fputc(str[pos], fh);
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
229
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
230 // Check if we are at end of input or hard linefeed
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
231 if (str[next] == '\n' || str[next] == 0)
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
232 break;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
233 else
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
234 {
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
235 fputc(str[pos], fh);
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
236 pos++;
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
237 linelen++;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
238 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
239 }
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
240 fprintf(fh, "\n");
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
241 }
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
242 }
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
243
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
244
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
245 static inline const char *dmArgsGetOptArg(const DMOptArg *opt)
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
246 {
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
247 #ifdef DM_USE_OPT_ARG
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
248 return opt->o_arg != NULL ? opt->o_arg : "ARG";
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
249 #else
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
250 (void) opt;
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
251 return "ARG";
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
252 #endif
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
253 }
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
254
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
255
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
256 static void dmArgsPrintHelpPrintItem(FILE *fh, const DMOptArg *opt,
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
257 int *optWidth, const int maxOptWidth, const int termWidth,
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
258 const BOOL doPrint)
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
259 {
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
260 const char *arg = dmArgsGetOptArg(opt);
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
261 char fmtBuf[32];
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
262 int padWidth;
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
263 BOOL hasLongOpt = opt->o_long != NULL;
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
264
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
265 if (opt->o_short != 0)
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
266 {
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
267 if (!hasLongOpt && (opt->flags & OPT_ARGREQ))
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
268 {
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
269 snprintf(fmtBuf, sizeof(fmtBuf), " -%c <%s>",
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
270 opt->o_short, arg);
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
271 }
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
272 else
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
273 {
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
274 snprintf(fmtBuf, sizeof(fmtBuf), " -%c,",
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
275 opt->o_short);
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
276 }
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
277
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
278 *optWidth = strlen(fmtBuf);
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
279 if (doPrint)
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
280 padWidth = hasLongOpt ? 2 : maxOptWidth - *optWidth;
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
281 else
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
282 padWidth = 2;
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
283 }
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
284 else
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
285 {
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
286 fmtBuf[0] = 0;
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
287 *optWidth = 0;
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
288 padWidth = 4 + 2;
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
289 }
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
290
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
291 if (doPrint)
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
292 {
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
293 fputs(fmtBuf, fh);
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
294 dmPrintPad(fh, padWidth, ' ');
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
295 }
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
296 *optWidth += padWidth;
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
297
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
298 if (hasLongOpt)
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
299 {
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
300 if (opt->flags & OPT_ARGREQ)
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
301 {
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
302 snprintf(fmtBuf, sizeof(fmtBuf), "--%s=<%s>",
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
303 opt->o_long, arg);
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
304 }
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
305 else
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
306 {
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
307 snprintf(fmtBuf, sizeof(fmtBuf), "--%s",
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
308 opt->o_long);
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
309 }
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
310
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
311 *optWidth += strlen(fmtBuf);
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
312 }
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
313 else
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
314 fmtBuf[0] = 0;
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
315
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
316 if (doPrint)
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
317 {
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
318 padWidth = hasLongOpt ? maxOptWidth - *optWidth : 0;
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
319 *optWidth += padWidth;
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
320
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
321 fputs(fmtBuf, fh);
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
322 dmPrintPad(fh, padWidth, ' ');
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
323 dmPrintWrap(fh, *optWidth, *optWidth, termWidth, opt->desc);
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
324 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
325 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
326
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
327
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
328 /**
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
329 * Print help for commandline arguments/options
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
330 * @param fh stdio file handle to output to
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
331 * @param opts options list array
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
332 * @param nopts number of elements in options list array
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
333 * @param flags flags (currently unused)
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
334 * @param width width of the terminal or desired width of the print out
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
335 */
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
336 void dmArgsPrintHelp(FILE *fh, const DMOptArg *opts,
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
337 const int nopts, const int flags, const int width)
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
338 {
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
339 int index, maxOptWidth;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
340 (void) flags;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
341
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
342 // Determine width of the options and arguments
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
343 maxOptWidth = 0;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
344 for (index = 0; index < nopts; index++)
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
345 {
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
346 int optWidth = 0;
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
347 dmArgsPrintHelpPrintItem(NULL, &opts[index], &optWidth, 0, width, FALSE);
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
348 if (optWidth > maxOptWidth)
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
349 maxOptWidth = optWidth;
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
350 }
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
351
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
352 maxOptWidth += 2;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
353
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
354 // Print out the formatted option list
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
355 for (index = 0; index < nopts; index++)
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
356 {
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
357 int optWidth;
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
358 dmArgsPrintHelpPrintItem(fh, &opts[index], &optWidth, maxOptWidth, width, TRUE);
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
359 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
360 }