1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
/*
* Command-line arg handling
*/
#include "ctwm.h"
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "clargs.h"
#include "ctopts.h"
#include "deftwmrc.h"
#include "screen.h"
#include "version.h"
static void usage(void) __attribute__((noreturn));
static void print_version(void);
static void DisplayInfo(void);
static void dump_default_config(void);
/*
* Command-line args. Initialize with useful default values.
*/
ctwm_cl_args CLarg = {
.MultiScreen = true,
.Monochrome = false,
.cfgchk = false,
.InitFile = NULL,
.display_name = NULL,
.PrintErrorMessages = false,
#ifdef DEBUG
.ShowWelcomeWindow = false,
#else
.ShowWelcomeWindow = true,
#endif
#ifdef CAPTIVE
.is_captive = false,
.capwin = (Window) 0,
.captivename = NULL,
#endif
#ifdef USEM4
.KeepTmpFile = false,
.keepM4_filename = NULL,
.GoThroughM4 = true,
#endif
#ifdef EWMH
.ewmh_replace = false,
#endif
.client_id = NULL,
.restore_filename = NULL,
};
/*
* Parse them out and setup CLargs.
*/
void
clargs_parse(int argc, char *argv[])
{
int ch, optidx;
/*
* Setup long options for arg parsing
*/
static struct option long_options[] = {
/* Simple flags */
{ "single", no_argument, NULL, 0 },
{ "mono", no_argument, NULL, 0 },
{ "verbose", no_argument, NULL, 'v' },
{ "quiet", no_argument, NULL, 'q' },
{ "nowelcome", no_argument, NULL, 'W' },
/* Config/file related */
{ "file", required_argument, NULL, 'f' },
{ "cfgchk", no_argument, NULL, 0 },
/* Show something and exit right away */
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 0 },
{ "info", no_argument, NULL, 0 },
{ "dumpcfg", no_argument, NULL, 0 },
/* Misc control bits */
{ "display", required_argument, NULL, 'd' },
{ "xrm", required_argument, NULL, 0 },
#ifdef CAPTIVE
{ "window", optional_argument, NULL, 'w' },
{ "name", required_argument, NULL, 0 },
#endif
#ifdef EWMH
{ "replace", no_argument, NULL, 0 },
#endif
/* M4 control params */
#ifdef USEM4
{ "keep-defs", no_argument, NULL, 'k' },
{ "keep", required_argument, NULL, 'K' },
{ "nom4", no_argument, NULL, 'n' },
#endif
/* Random session-related bits */
{ "clientId", required_argument, NULL, 0 },
{ "restore", required_argument, NULL, 0 },
{ NULL, 0, NULL, 0 },
};
/*
* Short aliases for some
*
* I assume '::' for optional args is portable; getopt_long(3)
* doesn't describe it, but it's a GNU extension for getopt(3).
*/
const char *short_options = "vqWf:hd:"
#ifdef CAPTIVE
"w::"
#endif
#ifdef USEM4
"kK:n"
#endif
;
/*
* Backward-compat cheat: accept a few old-style long args if they
* came first. Of course, this assumed argv[x] is editable, which on
* most systems it is, and C99 requires it.
*/
if(argc > 1) {
#define CHK(x) else if(strcmp(argv[1], (x)) == 0)
if(0) {
/* nada */
}
CHK("-version") {
print_version();
exit(0);
}
CHK("-info") {
DisplayInfo();
exit(0);
}
CHK("-cfgchk") {
CLarg.cfgchk = true;
*argv[1] = '\0';
}
CHK("-display") {
if(argc <= 2 || strlen(argv[2]) < 1) {
usage();
}
CLarg.display_name = strdup(argv[2]);
*argv[1] = '\0';
*argv[2] = '\0';
}
#undef CHK
}
/*
* Parse out the args
*/
optidx = 0;
while((ch = getopt_long(argc, argv, short_options, long_options,
&optidx)) != -1) {
switch(ch) {
/* First handle the simple cases that have short args */
case 'v':
CLarg.PrintErrorMessages = true;
break;
case 'q':
CLarg.PrintErrorMessages = false;
break;
case 'W':
CLarg.ShowWelcomeWindow = false;
break;
case 'f':
CLarg.InitFile = optarg;
break;
case 'h':
usage();
case 'd':
CLarg.display_name = optarg;
break;
#ifdef CAPTIVE
case 'w':
CLarg.is_captive = true;
CLarg.MultiScreen = false;
if(optarg != NULL) {
sscanf(optarg, "%x", (unsigned int *)&CLarg.capwin);
/* Failure will just leave capwin as initialized */
}
break;
#endif
#ifdef USEM4
/* Args that only mean anything if we're built with m4 */
case 'k':
CLarg.KeepTmpFile = true;
break;
case 'K':
CLarg.keepM4_filename = optarg;
break;
case 'n':
CLarg.GoThroughM4 = false;
break;
#endif
/*
* Now the stuff that doesn't have short variants.
*/
case 0:
#define IFIS(x) if(strcmp(long_options[optidx].name, (x)) == 0)
/* Simple flag-setting */
IFIS("single") {
CLarg.MultiScreen = false;
break;
}
IFIS("mono") {
CLarg.Monochrome = true;
break;
}
IFIS("cfgchk") {
CLarg.cfgchk = true;
break;
}
#ifdef EWMH
IFIS("replace") {
CLarg.ewmh_replace = true;
break;
}
#endif
/* Simple value-setting */
#ifdef CAPTIVE
IFIS("name") {
CLarg.captivename = optarg;
break;
}
#endif
IFIS("clientId") {
CLarg.client_id = optarg;
break;
}
IFIS("restore") {
CLarg.restore_filename = optarg;
break;
}
/* Some immediate actions */
IFIS("version") {
print_version();
exit(0);
}
IFIS("info") {
DisplayInfo();
exit(0);
}
IFIS("dumpcfg") {
dump_default_config();
exit(0);
}
/* Misc */
IFIS("xrm") {
/*
* Quietly ignored by us; Xlib processes it
* internally in XtToolkitInitialize();
*/
break;
}
#undef IFIS
/*
* Some choices may just be internally setting a flag.
* We have none right now, but leave this in case we grow
* more later.
*/
if(long_options[optidx].flag != NULL) {
break;
}
/* Don't think it should be possible to get here... */
fprintf(stderr, "Internal error in getopt: '%s' unhandled.\n",
long_options[optidx].name);
usage();
/* Something totally unexpected */
case '?':
/* getopt_long() already printed an error */
usage();
default:
/* Uhhh... */
fprintf(stderr, "Internal error: getopt confused us.\n");
usage();
}
}
/* Should do it */
return;
}
/*
* Sanity check CLarg's
*/
void
clargs_check(void)
{
#ifdef USEM4
/* If we're not doing m4, don't specify m4 options */
if(!CLarg.GoThroughM4) {
if(CLarg.KeepTmpFile) {
fprintf(stderr, "--keep-defs is incompatible with --nom4.\n");
usage();
}
if(CLarg.keepM4_filename) {
fprintf(stderr, "--keep is incompatible with --nom4.\n");
usage();
}
}
#endif
#ifdef CAPTIVE
/* If we're not captive, captivename is meaningless too */
if(CLarg.captivename && !CLarg.is_captive) {
fprintf(stderr, "--name is meaningless without --window.\n");
usage();
}
/*
* Being captive and --cfgchk'ing is kinda meaningless. There's no
* reason to create a window just to destroy things, and it never
* adds anything. And it's one more way we're forcing changes on the
* X side before we parse the actual config, so let's just disallow
* it.
*/
if(CLarg.is_captive && CLarg.cfgchk) {
fprintf(stderr, "--window is incompatible with --cfgchk.\n");
usage();
}
#endif
/* Guess that's it */
return;
}
/*
* Small utils only currently used in this file. Over time they may need
* to be exported, if we start using them from more places.
*/
static void
usage(void)
{
/* How far to indent continuation lines */
int llen = 10;
fprintf(stderr, "usage: %s [(--display | -d) dpy] "
#ifdef EWMH
"[--replace] "
#endif
"[--single]\n", ProgramName);
fprintf(stderr, "%*s[(--file | -f) initfile] [--cfgchk] [--dumpcfg]\n",
llen, "");
#ifdef USEM4
fprintf(stderr, "%*s[--nom4 | -n] [--keep-defs | -k] "
"[(--keep | -K) m4file]\n", llen, "");
#endif
fprintf(stderr, "%*s[--verbose | -v] [--quiet | -q] [--mono] "
"[--xrm resource]\n", llen, "");
fprintf(stderr, "%*s[--version] [--info] [--nowelcome | -W]\n",
llen, "");
#ifdef CAPTIVE
fprintf(stderr, "%*s[(--window | -w) [win-id]] [--name name]\n", llen, "");
#endif
/* Semi-intentionally not documenting --clientId/--restore */
fprintf(stderr, "%*s[--help]\n", llen, "");
exit(1);
}
static void
print_version(void)
{
printf("ctwm %s\n", VersionNumberFull);
if(VCSType && VCSRevision) {
printf(" (%s:%s)\n", VCSType, VCSRevision);
}
}
static void
DisplayInfo(void)
{
char *ctopts;
printf("Twm version: %s\n", TwmVersion);
ctopts = ctopts_string(" ");
printf("Compile time options : %s\n", ctopts);
free(ctopts);
}
static void
dump_default_config(void)
{
int i;
for(i = 0 ; defTwmrc[i] != NULL ; i++) {
printf("%s\n", defTwmrc[i]);
}
}
|