14
14
#include <string.h>
15
15
16
16
#include "xkbcommon/xkbcommon.h"
17
- #if ENABLE_PRIVATE_APIS
17
+ #ifdef ENABLE_PRIVATE_APIS
18
18
#include "xkbcomp/xkbcomp-priv.h"
19
19
#include "xkbcomp/rules.h"
20
20
#endif
@@ -49,7 +49,7 @@ usage(FILE *file, const char *progname)
49
49
" Enable verbose debugging output\n"
50
50
" --test\n"
51
51
" Test compilation but do not print the keymap.\n"
52
- #if ENABLE_PRIVATE_APIS
52
+ #ifdef ENABLE_PRIVATE_APIS
53
53
" --kccgst\n"
54
54
" Print a keymap which only includes the KcCGST component names instead of the full keymap\n"
55
55
#endif
@@ -59,7 +59,7 @@ usage(FILE *file, const char *progname)
59
59
" --from-xkb <file>\n"
60
60
" Load the corresponding XKB file, ignore RMLVO options. If <file>\n"
61
61
" is \"-\" or missing, then load from stdin."
62
- #if ENABLE_PRIVATE_APIS
62
+ #ifdef ENABLE_PRIVATE_APIS
63
63
" This option must not be used with --kccgst.\n"
64
64
#endif
65
65
" --include\n"
@@ -84,6 +84,14 @@ usage(FILE *file, const char *progname)
84
84
" The XKB layout variant (default: '%s')\n"
85
85
" --options <options>\n"
86
86
" The XKB options (default: '%s')\n"
87
+ " --enable-environment-names\n"
88
+ " Allow to set the default RMLVO values via the following environment variables:\n"
89
+ " - XKB_DEFAULT_RULES\n"
90
+ " - XKB_DEFAULT_MODEL\n"
91
+ " - XKB_DEFAULT_LAYOUT\n"
92
+ " - XKB_DEFAULT_VARIANT\n"
93
+ " - XKB_DEFAULT_OPTIONS\n"
94
+ " Note that this option may affect the default values of the previous options.\n"
87
95
"\n" ,
88
96
progname , DEFAULT_XKB_RULES ,
89
97
DEFAULT_XKB_MODEL , DEFAULT_XKB_LAYOUT ,
@@ -92,7 +100,8 @@ usage(FILE *file, const char *progname)
92
100
}
93
101
94
102
static bool
95
- parse_options (int argc , char * * argv , char * * path , struct xkb_rule_names * names )
103
+ parse_options (int argc , char * * argv , bool * use_env_names ,
104
+ char * * path , struct xkb_rule_names * names )
96
105
{
97
106
enum options {
98
107
OPT_VERBOSE ,
@@ -102,6 +111,7 @@ parse_options(int argc, char **argv, char **path, struct xkb_rule_names *names)
102
111
OPT_FROM_XKB ,
103
112
OPT_INCLUDE ,
104
113
OPT_INCLUDE_DEFAULTS ,
114
+ OPT_ENABLE_ENV_NAMES ,
105
115
OPT_RULES ,
106
116
OPT_MODEL ,
107
117
OPT_LAYOUT ,
@@ -112,7 +122,7 @@ parse_options(int argc, char **argv, char **path, struct xkb_rule_names *names)
112
122
{"help" , no_argument , 0 , 'h' },
113
123
{"verbose" , no_argument , 0 , OPT_VERBOSE },
114
124
{"test" , no_argument , 0 , OPT_TEST },
115
- #if ENABLE_PRIVATE_APIS
125
+ #ifdef ENABLE_PRIVATE_APIS
116
126
{"kccgst" , no_argument , 0 , OPT_KCCGST },
117
127
#endif
118
128
{"rmlvo" , no_argument , 0 , OPT_RMLVO },
@@ -121,6 +131,7 @@ parse_options(int argc, char **argv, char **path, struct xkb_rule_names *names)
121
131
{"from-xkb" , optional_argument , 0 , OPT_FROM_XKB },
122
132
{"include" , required_argument , 0 , OPT_INCLUDE },
123
133
{"include-defaults" , no_argument , 0 , OPT_INCLUDE_DEFAULTS },
134
+ {"enable-environment-names" , no_argument , 0 , OPT_ENABLE_ENV_NAMES },
124
135
{"rules" , required_argument , 0 , OPT_RULES },
125
136
{"model" , required_argument , 0 , OPT_MODEL },
126
137
{"layout" , required_argument , 0 , OPT_LAYOUT },
@@ -130,6 +141,7 @@ parse_options(int argc, char **argv, char **path, struct xkb_rule_names *names)
130
141
};
131
142
132
143
bool has_rmlvo_options = false;
144
+ * use_env_names = false;
133
145
while (1 ) {
134
146
int option_index = 0 ;
135
147
int c = getopt_long (argc , argv , "h" , opts , & option_index );
@@ -154,6 +166,10 @@ parse_options(int argc, char **argv, char **path, struct xkb_rule_names *names)
154
166
case OPT_RMLVO :
155
167
if (output_format != FORMAT_KEYMAP_FROM_RMLVO )
156
168
goto output_format_error ;
169
+ #ifndef ENABLE_PRIVATE_APIS
170
+ if (* use_env_names )
171
+ goto rmlvo_env_error ;
172
+ #endif
157
173
output_format = FORMAT_RMLVO ;
158
174
break ;
159
175
case OPT_FROM_XKB :
@@ -182,6 +198,13 @@ parse_options(int argc, char **argv, char **path, struct xkb_rule_names *names)
182
198
goto too_many_includes ;
183
199
includes [num_includes ++ ] = DEFAULT_INCLUDE_PATH_PLACEHOLDER ;
184
200
break ;
201
+ case OPT_ENABLE_ENV_NAMES :
202
+ #ifndef ENABLE_PRIVATE_APIS
203
+ if (output_format == FORMAT_RMLVO )
204
+ goto rmlvo_env_error ;
205
+ #endif
206
+ * use_env_names = true;
207
+ break ;
185
208
case OPT_RULES :
186
209
if (output_format == FORMAT_KEYMAP_FROM_XKB )
187
210
goto input_format_error ;
@@ -243,6 +266,14 @@ parse_options(int argc, char **argv, char **path, struct xkb_rule_names *names)
243
266
244
267
return true;
245
268
269
+ #ifndef ENABLE_PRIVATE_APIS
270
+ rmlvo_env_error :
271
+ /* See comment in print_rmlvo */
272
+ fprintf (stderr , "ERROR: --rmlvo is not compatible with "
273
+ "--enable-environment-names yet\n" );
274
+ exit (EXIT_INVALID_USAGE );
275
+ #endif
276
+
246
277
output_format_error :
247
278
fprintf (stderr , "ERROR: Cannot mix output formats\n" );
248
279
usage (stderr , argv [0 ]);
@@ -260,9 +291,36 @@ parse_options(int argc, char **argv, char **path, struct xkb_rule_names *names)
260
291
}
261
292
262
293
static int
263
- print_rmlvo (struct xkb_context * ctx , const struct xkb_rule_names * rmlvo )
294
+ print_rmlvo (struct xkb_context * ctx , struct xkb_rule_names * rmlvo )
264
295
{
265
- printf ("rules: \"%s\"\nmodel: \"%s\"\nlayout: \"%s\"\nvariant: \"%s\"\noptions: \"%s\"\n" ,
296
+ /* Fill defaults */
297
+ #ifndef ENABLE_PRIVATE_APIS
298
+ /* FIXME: We should use `xkb_context_sanitize_rule_names`, but this is
299
+ * not a public API yet. Instead we just do not support names from
300
+ * environment variables. */
301
+ if (isempty (rmlvo -> rules ))
302
+ rmlvo -> rules = DEFAULT_XKB_RULES ;
303
+ if (isempty (rmlvo -> model ))
304
+ rmlvo -> model = DEFAULT_XKB_MODEL ;
305
+ /* Layout and variant are tied together, so we either get user-supplied for
306
+ * both or default for both */
307
+ if (isempty (rmlvo -> layout )) {
308
+ if (!isempty (rmlvo -> variant )) {
309
+ fprintf (stderr , "ERROR: a variant requires a layout\n" );
310
+ return EXIT_INVALID_USAGE ;
311
+ }
312
+ rmlvo -> layout = DEFAULT_XKB_LAYOUT ;
313
+ rmlvo -> variant = DEFAULT_XKB_VARIANT ;
314
+ }
315
+ if (isempty (rmlvo -> options ))
316
+ rmlvo -> options = DEFAULT_XKB_OPTIONS ;
317
+ #else
318
+ /* Resolve default RMLVO values */
319
+ xkb_context_sanitize_rule_names (ctx , rmlvo );
320
+ #endif
321
+
322
+ printf ("rules: \"%s\"\nmodel: \"%s\"\nlayout: \"%s\"\nvariant: \"%s\"\n"
323
+ "options: \"%s\"\n" ,
266
324
rmlvo -> rules , rmlvo -> model , rmlvo -> layout ,
267
325
rmlvo -> variant ? rmlvo -> variant : "" ,
268
326
rmlvo -> options ? rmlvo -> options : "" );
@@ -272,7 +330,7 @@ print_rmlvo(struct xkb_context *ctx, const struct xkb_rule_names *rmlvo)
272
330
static int
273
331
print_kccgst (struct xkb_context * ctx , struct xkb_rule_names * rmlvo )
274
332
{
275
- #if ENABLE_PRIVATE_APIS
333
+ #ifdef ENABLE_PRIVATE_APIS
276
334
struct xkb_component_names kccgst ;
277
335
278
336
/* Resolve default RMLVO values */
@@ -377,36 +435,23 @@ main(int argc, char **argv)
377
435
{
378
436
struct xkb_context * ctx ;
379
437
char * keymap_path = NULL ;
380
- struct xkb_rule_names names = {
381
- .rules = DEFAULT_XKB_RULES ,
382
- .model = DEFAULT_XKB_MODEL ,
383
- /* layout and variant are tied together, so we either get user-supplied for
384
- * both or default for both, see below */
385
- .layout = NULL ,
386
- .variant = NULL ,
387
- .options = DEFAULT_XKB_OPTIONS ,
388
- };
438
+ struct xkb_rule_names names = { 0 };
439
+ bool use_env_names = false;
389
440
int rc = 1 ;
390
441
391
442
if (argc < 1 ) {
392
443
usage (stderr , argv [0 ]);
393
444
return EXIT_INVALID_USAGE ;
394
445
}
395
446
396
- if (!parse_options (argc , argv , & keymap_path , & names ))
447
+ if (!parse_options (argc , argv , & use_env_names , & keymap_path , & names ))
397
448
return EXIT_INVALID_USAGE ;
398
449
399
- /* Now fill in the layout */
400
- if (!names .layout || !* names .layout ) {
401
- if (names .variant && * names .variant ) {
402
- fprintf (stderr , "ERROR: a variant requires a layout\n" );
403
- return EXIT_INVALID_USAGE ;
404
- }
405
- names .layout = DEFAULT_XKB_LAYOUT ;
406
- names .variant = DEFAULT_XKB_VARIANT ;
407
- }
450
+ enum xkb_context_flags ctx_flags = XKB_CONTEXT_NO_DEFAULT_INCLUDES ;
451
+ if (!use_env_names )
452
+ ctx_flags |= XKB_CONTEXT_NO_ENVIRONMENT_NAMES ;
408
453
409
- ctx = xkb_context_new (XKB_CONTEXT_NO_DEFAULT_INCLUDES );
454
+ ctx = xkb_context_new (ctx_flags );
410
455
assert (ctx );
411
456
412
457
if (verbose ) {
0 commit comments