annotate lwlib/lw_cmdline.c @ 94:61c084b2c727

Fixed bug parsing %-prefix binary constants
author lost@l-w.ca
date Sat, 06 Aug 2011 10:29:12 -0600
parents 2077b755b8b4
children 8b0be0fc42cf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
1 /*
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
2 lw_cmdline.c
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
3
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
4 Copyright © 2010 William Astle
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
5
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
6 This file is part of LWTOOLS.
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
7
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
11 version.
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
12
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
16 more details.
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
17
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
20 */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
21
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
22 #include <stdio.h>
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
23 #include <stdlib.h>
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
24 #include <string.h>
8
fdc11ef4115b Switched lwlink to lw_cmdline from argp and also brought in lw_alloc and lw_string to replace util.c
lost@l-w.ca
parents: 6
diff changeset
25 #include <ctype.h>
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
26
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
27 #include "lw_alloc.h"
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
28
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
29 #define ___lw_cmdline_c_seen___
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
30 #include "lw_cmdline.h"
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
31
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
32 #define DOCCOL 30
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
33 #define LLEN 78
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
34
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
35 static struct lw_cmdline_options builtin[3] =
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
36 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
37 { "help", '?', 0, 0, "give this help list" },
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
38 { "usage", 0, 0, 0, "give a short usage message" },
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
39 { "version", 'V', 0, 0, "print program version" }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
40 };
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
41
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
42 static int cmpr(const void *e1, const void *e2)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
43 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
44 struct lw_cmdline_options **o1, **o2;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
45 o1 = (struct lw_cmdline_options **)e1;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
46 o2 = (struct lw_cmdline_options **)e2;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
47
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
48 return strcmp((*o1) -> name, (*o2) -> name);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
49 }
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
50
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
51 static int cmpr2(const void *e1, const void *e2)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
52 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
53 struct lw_cmdline_options **o1, **o2;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
54 o1 = (struct lw_cmdline_options **)e1;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
55 o2 = (struct lw_cmdline_options **)e2;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
56
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
57 if ((*o1) -> key < ((*o2) -> key))
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
58 return -1;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
59 if ((*o1) -> key > ((*o2) -> key))
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
60 return 1;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
61 return 0;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
62 }
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
63
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
64 static void lw_cmdline_usage(struct lw_cmdline_parser *parser, char *name)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
65 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
66 struct lw_cmdline_options **slist, **llist;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
67 int nopt;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
68 int i;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
69 int t;
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
70 int col;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
71
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
72 for (nopt = 0; parser -> options[nopt].name; nopt++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
73 /* do nothing */ ;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
74
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
75 slist = lw_alloc(sizeof(struct lw_cmdline_options *) * (nopt + 3));
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
76 llist = lw_alloc(sizeof(struct lw_cmdline_options *) * (nopt + 3));
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
77
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
78 for (i = 0; i < nopt; i++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
79 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
80 slist[i] = &(parser -> options[i]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
81 llist[i] = &(parser -> options[i]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
82 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
83
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
84 /* now sort the two lists */
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
85 qsort(slist, nopt, sizeof(struct lw_cmdline_options *), cmpr2);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
86 qsort(llist, nopt, sizeof(struct lw_cmdline_options *), cmpr);
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
87
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
88 /* now append the automatic options */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
89 slist[nopt] = &(builtin[0]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
90 slist[nopt + 1] = &(builtin[1]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
91 slist[nopt + 2] = &(builtin[2]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
92
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
93 llist[nopt] = &(builtin[0]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
94 llist[nopt + 1] = &(builtin[1]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
95 llist[nopt + 2] = &(builtin[2]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
96
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
97 /* now show the usage message */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
98 printf("Usage: %s", name);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
99
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
100 col = 7 + strlen(name);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
101
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
102 /* print short options that take no args */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
103 t = 0;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
104 for (i = 0; i < nopt + 3; i++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
105 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
106 if (slist[i]->key > 0x20 && slist[i]->key < 0x7f)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
107 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
108 if (slist[i]->arg == NULL)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
109 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
110 if (!t)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
111 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
112 printf(" [-");
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
113 t = 1;
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
114 col += 3;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
115 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
116 printf("%c", slist[i]->key);
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
117 col++;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
118 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
119 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
120 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
121 if (t)
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
122 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
123 col++;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
124 printf("]");
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
125 }
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
126
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
127 /* print short options that take args */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
128 for (i = 0; i < nopt + 3; i++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
129 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
130 if (slist[i]->key > 0x20 && slist[i]->key < 0x7f && slist[i] -> arg)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
131 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
132 if (slist[i]->flags & lw_cmdline_opt_optional)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
133 {
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
134 t = 7 + strlen(slist[i] -> arg);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
135 if (col + t > LLEN)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
136 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
137 printf("\n ");
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
138 col = 7;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
139 }
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
140 printf(" [-%c[%s]]", slist[i]->key, slist[i]->arg);
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
141 col += t;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
142 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
143 else
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
144 {
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
145 t = 6 + strlen(slist[i] -> arg);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
146 if (col + t > LLEN)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
147 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
148 printf("\n ");
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
149 col = 7;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
150 }
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
151 printf(" [-%c %s]", slist[i]->key, slist[i]->arg);
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
152 col += t;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
153 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
154 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
155 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
156
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
157 /* print long options */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
158 for (i = 0; i < nopt + 3; i++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
159 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
160 if (llist[i]->arg)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
161 {
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
162 t = strlen(llist[i] -> name) + 6 + strlen(llist[i] -> arg);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
163 if (llist[i] -> flags & lw_cmdline_opt_optional)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
164 t += 2;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
165 if (col + t > LLEN)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
166 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
167 printf("\n ");
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
168 col = 7;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
169 }
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
170 printf(" [--%s%s=%s%s]",
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
171 llist[i] -> name,
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
172 (llist[i] -> flags & lw_cmdline_opt_optional) ? "[" : "",
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
173 llist[i] -> arg,
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
174 (llist[i] -> flags & lw_cmdline_opt_optional) ? "]" : "");
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
175 col += t;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
176 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
177 else
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
178 {
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
179 t = strlen(llist[i] -> name) + 5;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
180 if (col + t > LLEN)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
181 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
182 printf("\n ");
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
183 col = 7;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
184 }
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
185 printf(" [--%s]", llist[i] -> name);
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
186 col += t;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
187 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
188 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
189
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
190 /* print "non option" text */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
191 if (parser -> args_doc)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
192 {
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
193 if (col + strlen(parser -> args_doc) + 1 > LLEN)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
194 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
195 printf("\n ");
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
196 }
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
197 printf(" %s", parser -> args_doc);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
198 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
199 printf("\n");
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
200
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
201 /* clean up scratch lists */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
202 lw_free(slist);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
203 lw_free(llist);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
204 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
205
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
206 static void lw_cmdline_help(struct lw_cmdline_parser *parser, char *name)
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
207 {
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
208 struct lw_cmdline_options **llist;
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
209 int nopt;
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
210 int i;
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
211 char *tstr;
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
212 int col = 0;
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
213
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
214 tstr = parser -> doc;
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
215 for (nopt = 0; parser -> options[nopt].name; nopt++)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
216 /* do nothing */ ;
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
217
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
218 llist = lw_alloc(sizeof(struct lw_cmdline_options *) * (nopt + 3));
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
219
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
220 for (i = 0; i < nopt; i++)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
221 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
222 llist[i] = &(parser -> options[i]);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
223 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
224
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
225 /* now sort the list */
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
226 qsort(llist, nopt, sizeof(struct lw_cmdline_options *), cmpr);
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
227
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
228 /* now append the automatic options */
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
229 llist[nopt] = &(builtin[0]);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
230 llist[nopt + 1] = &(builtin[1]);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
231 llist[nopt + 2] = &(builtin[2]);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
232
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
233 /* print brief usage */
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
234 printf("Usage: %s [OPTION...] %s\n", name, parser -> args_doc ? parser -> args_doc : "");
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
235 if (tstr)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
236 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
237 while (*tstr && *tstr != '\v')
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
238 fputc(*tstr++, stdout);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
239 if (*tstr)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
240 tstr++;
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
241 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
242 fputc('\n', stdout);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
243 fputc('\n', stdout);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
244
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
245 /* display options - do it the naïve way for now */
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
246 for (i = 0; i < (nopt + 3); i++)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
247 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
248 if (llist[i] -> key > 0x20 && llist[i] -> key < 0x7F)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
249 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
250 printf(" -%c, ", llist[i] -> key);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
251 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
252 else
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
253 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
254 printf(" ");
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
255 }
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
256 col = 8 + strlen(llist[i] -> name);
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
257
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
258 printf("--%s", llist[i] -> name);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
259 if (llist[i] -> arg)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
260 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
261 if (llist[i] -> flags & lw_cmdline_opt_optional)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
262 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
263 printf("[=%s]", llist[i] -> arg);
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
264 col += 3 + strlen(llist[i] -> arg);
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
265 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
266 else
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
267 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
268 printf("=%s", llist[i] -> arg);
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
269 col += 1 + strlen(llist[i] -> arg);
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
270 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
271 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
272 if (llist[i] -> doc)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
273 {
6
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
274 char *s = llist[i] -> doc;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
275 char *s2;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
276
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
277 while (*s && isspace(*s))
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
278 s++;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
279
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
280 if (col > DOCCOL)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
281 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
282 fputc('\n', stdout);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
283 col = 0;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
284 }
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
285 while (*s)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
286 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
287 while (col < (DOCCOL - 1))
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
288 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
289 fputc(' ', stdout);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
290 col++;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
291 }
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
292 for (s2 = s; *s2 && !isspace(*s2); s2++)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
293 /* do nothing */ ;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
294 if ((col + (s2 - s) + 1) > LLEN && col >= DOCCOL)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
295 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
296 /* next line */
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
297 fputc('\n', stdout);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
298 col = 0;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
299 continue;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
300 }
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
301 col++;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
302 fputc(' ', stdout);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
303 while (s != s2)
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
304 {
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
305 fputc(*s, stdout);
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
306 col++;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
307 s++;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
308 }
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
309 while (*s && isspace(*s))
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
310 s++;
1e5e8ec406fb Various output cleanups for --help and --usage in lw_cmdline
lost@l-w.ca
parents: 5
diff changeset
311 }
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
312 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
313 fputc('\n', stdout);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
314 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
315
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
316 printf("\nMandatory or optional arguments to long options are also mandatory or optional\nfor any corresponding short options.\n");
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
317
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
318 if (*tstr)
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
319 {
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
320 printf("\n%s\n", tstr);
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
321 }
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
322
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
323 /* clean up scratch lists */
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
324 lw_free(llist);
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
325 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
326
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
327 int lw_cmdline_parse(struct lw_cmdline_parser *parser, int argc, char **argv, unsigned flags, int *arg_index, void *input)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
328 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
329 int i, j, r;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
330 int firstarg;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
331 int nextarg;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
332 char *tstr;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
333 int cch;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
334
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
335 /* first, permute the argv array so that all option arguments are at the start */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
336 for (i = 1, firstarg = 1; i < argc; i++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
337 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
338 if (argv[i][0] == '-' && argv[i][1])
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
339 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
340 /* have an option arg */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
341 if (firstarg == i)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
342 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
343 firstarg++;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
344 continue;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
345 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
346 tstr = argv[i];
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
347 for (j = i; j > firstarg; j--)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
348 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
349 argv[j] = argv[j - 1];
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
350 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
351 argv[firstarg] = tstr;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
352 firstarg++;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
353 if (argv[firstarg - 1][1] == '-' && argv[firstarg - 1][2] == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
354 break;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
355 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
356 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
357
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
358 /* now start parsing options */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
359 nextarg = firstarg;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
360 i = 1;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
361 cch = 0;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
362 while (i < firstarg)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
363 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
364 if (cch > 0 && argv[i][cch] == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
365 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
366 i++;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
367 cch = 0;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
368 continue;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
369 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
370
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
371 if (cch > 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
372 goto shortopt;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
373
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
374 /* skip the "--" option */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
375 if (argv[i][1] == '-' && argv[i][2] == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
376 break;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
377
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
378 if (argv[i][1] == '-')
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
379 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
380 goto longopt;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
381 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
382
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
383 cch = 1;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
384 shortopt:
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
385 /* handle a short option here */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
386
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
387 /* automatic options */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
388 if (argv[i][cch] == '?')
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
389 goto do_help;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
390 if (argv[i][cch] == 'V')
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
391 goto do_version;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
392 /* look up key */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
393 for (j = 0; parser -> options[j].name; j++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
394 if (parser -> options[j].key == argv[i][cch])
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
395 break;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
396 cch++;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
397 tstr = argv[i] + cch;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
398 if (!*tstr)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
399 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
400 if (nextarg < argc)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
401 tstr = argv[nextarg];
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
402 else
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
403 tstr = NULL;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
404 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
405 goto common;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
406
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
407 longopt:
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
408 if (strcmp(argv[i], "--help") == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
409 goto do_help;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
410 if (strcmp(argv[i], "--usage") == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
411 goto do_usage;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
412 if (strcmp(argv[i], "--version") == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
413 goto do_version;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
414 /* look up name */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
415
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
416 for (j = 2; argv[i][j] && argv[i][j] != '='; j++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
417 /* do nothing */ ;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
418 tstr = lw_alloc(j - 1);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
419 strncpy(tstr, argv[i] + 2, j - 2);
52
51c840679a0e Fixed off by one bug parsing long options
lost@l-w.ca
parents: 37
diff changeset
420 tstr[j - 2] = 0;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
421 if (argv[i][j] == '=')
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
422 j++;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
423 cch = j;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
424 for (j = 0; parser -> options[j].name; j++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
425 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
426 if (strcmp(parser -> options[j].name, tstr) == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
427 break;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
428 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
429 lw_free(tstr);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
430 tstr = argv[i] + cch;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
431 cch = 0;
37
a5d4693483af Fix hang while parsing long options
lost@l-w.ca
parents: 8
diff changeset
432 i++;
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
433
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
434 common:
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
435 /* j will be the offset into the option table when we get here */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
436 /* cch will be zero and tstr will point to the arg if it's a long option */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
437 /* cch will be > 0 and tstr points to the theoretical option, either within */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
438 /* this string or "nextarg" */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
439 if (parser -> options[j].name == NULL)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
440 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
441 fprintf(stderr, "Unknown option. See %s --usage.\n", argv[0]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
442 exit(1);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
443 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
444 if (parser -> options[j].arg)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
445 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
446 if (tstr && cch && argv[i][cch] == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
447 nextarg++;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
448
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
449 if (!*tstr)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
450 tstr = NULL;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
451
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
452 if (!tstr && (parser -> options[j].flags & lw_cmdline_opt_optional) == 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
453 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
454 fprintf(stderr, "Option %s requires argument.\n", parser -> options[j].name);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
455 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
456 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
457 r = (*(parser -> parser))(parser -> options[j].key, tstr, input);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
458 if (r != 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
459 return r;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
460 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
461 /* handle non-option args */
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
462 if (arg_index)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
463 *arg_index = nextarg;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
464 for (i = nextarg; i < argc; i++)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
465 {
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
466 r = (*(parser -> parser))(lw_cmdline_key_arg, argv[i], input);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
467 if (r != 0)
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
468 return r;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
469 }
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
470 r = (*(parser -> parser))(lw_cmdline_key_end, NULL, input);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
471 return r;
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
472
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
473 do_help:
5
0e01d1343c02 Added basic --help routine to lw_cmdline
lost@l-w.ca
parents: 4
diff changeset
474 lw_cmdline_help(parser, argv[0]);
4
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
475 exit(0);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
476
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
477 do_version:
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
478 printf("%s\n", parser -> program_version);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
479 exit(0);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
480
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
481 do_usage:
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
482 lw_cmdline_usage(parser, argv[0]);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
483 exit(0);
a4812d57ed13 Started implementing command line parsing in lwlib
lost@l-w.ca
parents:
diff changeset
484 }