summaryrefslogtreecommitdiff
path: root/app/xkbprint/xkbprint.c
blob: 81e85e5c81e1da2529ed2f55463e17a70348ce8d (plain)
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
/* $Xorg: xkbprint.c,v 1.4 2000/08/17 19:54:50 cpqbld Exp $ */
/************************************************************
 Copyright (c) 1995 by Silicon Graphics Computer Systems, Inc.

 Permission to use, copy, modify, and distribute this
 software and its documentation for any purpose and without
 fee is hereby granted, provided that the above copyright
 notice appear in all copies and that both that copyright
 notice and this permission notice appear in supporting
 documentation, and that the name of Silicon Graphics not be 
 used in advertising or publicity pertaining to distribution 
 of the software without specific prior written permission.
 Silicon Graphics makes no representation about the suitability 
 of this software for any purpose. It is provided "as is"
 without any express or implied warranty.
 
 SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS 
 SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 
 AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
 GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL 
 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 
 DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 
 OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
 THE USE OR PERFORMANCE OF THIS SOFTWARE.

 ********************************************************/
/* $XFree86: xc/programs/xkbprint/xkbprint.c,v 3.10 2003/05/27 22:27:07 tsi Exp $ */

#include <stdio.h>
#include <ctype.h>
#include <X11/Xlocale.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/XKBlib.h>
#include <X11/extensions/XKBgeom.h>
#include <X11/extensions/XKM.h>
#include <X11/extensions/XKBfile.h>
#include <X11/keysym.h>

#if defined(sgi)
#include <malloc.h>
#endif

#define	DEBUG_VAR_NOT_LOCAL
#define	DEBUG_VAR debugFlags
#include <stdlib.h>

#include "utils.h"
#include "xkbprint.h"

/***====================================================================***/


#define	WANT_DEFAULT	0
#define	WANT_PS_FILE	1
#define	WANT_X_SERVER	2

#define	INPUT_UNKNOWN	0
#define	INPUT_XKB	1
#define	INPUT_XKM	2

#ifdef notyet
static char *fileTypeExt[] = {
	"XXX",
	"xkm",
	"xkb"
};
#endif

static	unsigned	outputFormat= WANT_DEFAULT;
static	char *		wantLocale= "C";
static	char *		rootDir;
static	char *		inputFile;
static	char *		outputFile;
static	char *		outputFont= NULL;
static	char *		inDpyName,*outDpyName;
static	Display *	inDpy;
static	Display *	outDpy;
#ifdef NOTYET
static	Bool		computeDflts= False;
#endif
static	XKBPrintArgs	args;
	unsigned	warningLevel= 5;
	Bool		synch;

/***====================================================================***/

static void
Usage(int argc, char *argv[])
{
    fprintf(stderr,"Usage: %s [options] input-file [ output-file ]\n",argv[0]);
    fprintf(stderr,"Legal options:\n");
    fprintf(stderr,"-?,-help      Print this message\n");
    fprintf(stderr,"-color        Use colors from geometry\n");
#ifdef DEBUG
    fprintf(stderr,"-d [flags]    Report debugging information\n");
#endif
    fprintf(stderr,"-dflts        Compute defaults for missing components\n");
    fprintf(stderr,"-diffs        Only show explicit key definitions\n");
    fprintf(stderr,"-eps          Generate an EPS file\n");
    fprintf(stderr,"-fit          Fit keyboard image on page (default)\n");
    fprintf(stderr,"-full         Print keyboard image full sized\n");
    fprintf(stderr,"-grid <n>     Print a grid with <n> mm resolution\n");
    fprintf(stderr,"-if <name>    Specifies the name of an internal font to dump\n");
#ifdef DEBUG
    fprintf(stderr,"-I[<dir>]     Specifies a top level directory\n");
    fprintf(stderr,"              for include directives.  You can\n");
    fprintf(stderr,"              specify multiple directories.\n");
#endif
    fprintf(stderr,"-kc           Also print keycodes, if possible\n");
    fprintf(stderr,"-label <what> Specifies the label to be drawn on keys\n");
    fprintf(stderr,"              Legal values for <what> are:\n");
    fprintf(stderr,"                  none,name,code,symbols\n");
    fprintf(stderr,"-lc <locale>  Use <locale> for fonts and symbols\n");
    fprintf(stderr,"-level1       Use level 1 PostScript (default)\n");
    fprintf(stderr,"-level2       Use level 2 PostScript\n");
    fprintf(stderr,"-lg <num>     Use keyboard group <num> to print labels\n");
    fprintf(stderr,"-ll <num>     Use shift level <num> to print labels\n");
    fprintf(stderr,"-mono         Ignore colors from geometry (default)\n");
    fprintf(stderr,"-n <num>      Print <num> copies (default 1)\n");
    fprintf(stderr,"-nkg <num>    Number of groups to print on each key\n");
    fprintf(stderr,"-nokc         Don't print keycodes, even if possible\n");
    fprintf(stderr,"-npk <num>    Number of keyboards to print on each page\n");
    fprintf(stderr,"-ntg <num>    Total number of groups to print\n");
    fprintf(stderr,"-o <file>     Specifies output file name\n");
    fprintf(stderr,"-R[<DIR>]     Specifies the root directory for relative\n");
    fprintf(stderr,"              path names\n");
    fprintf(stderr,"-pict <what>  Specifies use of pictographs instead of\n");
    fprintf(stderr,"              keysym names where available, <what> can\n");
fprintf(stderr,"              be \"all\", \"none\" or \"common\" (default)\n");
    fprintf(stderr,"-synch        Force synchronization\n");
    fprintf(stderr,"-w <lvl>      Set warning level (0=none, 10=all)\n");
}

/***====================================================================***/

static Bool
parseArgs(int argc, char *argv[])
{
register int i;

    args.copies=	1;
    args.grid=		0;
    args.level1=	True;
    args.scaleToFit=	True;
    args.wantColor=	False;
    args.wantSymbols=	COMMON_SYMBOLS;
    args.wantKeycodes=	True;
    args.wantDiffs=	False;
    args.wantEPS=	False;
    args.label=		LABEL_AUTO;
    args.baseLabelGroup=0;
    args.nLabelGroups=	1;
    args.nTotalGroups=	0;
    args.nKBPerPage=	0;
    args.labelLevel=	0;
    for (i=1;i<argc;i++) {
	if ((argv[i][0]!='-')||(uStringEqual(argv[i],"-"))) {
	    if (inputFile==NULL) {
		inputFile= argv[i];
	    }
	    else if (outputFile==NULL) {
		outputFile= argv[i];
	    }
	    else {
		uWarning("Too many file names on command line\n");
		uAction("Compiling %s, writing to %s, ignoring %s\n",
					inputFile,outputFile,argv[i]);
	    }
	}
	else if ((strcmp(argv[i],"-?")==0)||(strcmp(argv[i],"-help")==0)) {
	    Usage(argc,argv);
	    exit(0);
	}
	else if (strcmp(argv[i],"-color")==0) {
	    args.wantColor= True;
	}
#ifdef DEBUG
	else if (strcmp(argv[i],"-d")==0) {
	    if ((i>=(argc-1))||(!isdigit(argv[i+1][0]))) {
		debugFlags= 1;
	    }
	    else {
		sscanf(argv[++i],"%i",&debugFlags);
	    }
	    uInformation("Setting debug flags to %d\n",debugFlags);
	}
#endif
	else if (strcmp(argv[i],"-dflts")==0) {
#ifdef NOTYET
	    computeDflts= True;
#endif
	    uWarning("Compute defaults not implemented yet\n");
	}
	else if (strcmp(argv[i],"-diffs")==0) {
	    args.wantDiffs= True;
	}
	else if (strcmp(argv[i],"-eps")==0) {
	    args.wantEPS= True;
	}
	else if (strcmp(argv[i],"-fit")==0) {
	    args.scaleToFit= True;
	}
	else if (strcmp(argv[i],"-full")==0) {
	    args.scaleToFit= False;
	}
	else if (strcmp(argv[i],"-grid")==0) {
	    int tmp;
	    if (++i>=argc) {
		uWarning("Grid frequency not specified\n");
		uAction("Trailing \"-grid\" option ignored\n");
	    }
	    else if ((sscanf(argv[i],"%i",&tmp)!=1)||(tmp<1)) {
		uWarning("Grid frequency must be an integer > zero\n");
		uAction("Illegal frequency %d ignored\n",tmp);
	    }	 
	    else args.grid= tmp;
	}
#ifdef NOTYET
	else if (strncmp(argv[i],"-I",2)==0) {
	    if (!XkbAddDirectoryToPath(&argv[i][2])) {
		uAction("Exiting\n");
		exit(1);
	    }
	    uInternalError("Includes not implemented yet\n");
	}
#endif
	else if (strcmp(argv[i],"-if")==0) {
	    if (++i>=argc) {
		uWarning("Internal Font name not specified\n");
		uAction("Trailing \"-if\" option ignored\n");
	    }
	    else outputFont= argv[i];
	}
	else if (strcmp(argv[i],"-kc")==0) {
	    args.wantKeycodes= True;
	}
	else if (strcmp(argv[i],"-label")==0) {
	    if (++i>=argc) {
		uWarning("Label type not specified\n");
		uAction("Trailing \"-label\" option ignored\n");
	    }
	    else if (uStrCaseEqual(argv[i],"none")) 
		args.label= LABEL_NONE;
	    else if (uStrCaseEqual(argv[i],"name")) 
		args.label= LABEL_KEYNAME;
	    else if (uStrCaseEqual(argv[i],"code")) 
		args.label= LABEL_KEYCODE;
	    else if (uStrCaseEqual(argv[i],"symbols")) 
		args.label= LABEL_SYMBOLS;
	    else {
		uWarning("Unknown label type \"%s\" specified\n",argv[i]);
		uAction("Ignored\n");
	    }
	}
	else if (strcmp(argv[i],"-lc")==0) {
	    if (++i>=argc) {
		uWarning("Locale not specified\n");
		uAction("Trailing \"-lc\" option ignored\n");
	    }
	    else wantLocale= argv[i];
	}
	else if (strcmp(argv[i],"-lg")==0) {
	    int tmp;
	    if (++i>=argc) {
		uWarning("Label group not specified\n");
		uAction("Trailing \"-lg\" option ignored\n");
	    }
	    else if ((sscanf(argv[i],"%i",&tmp)!=1)||(tmp<1)||(tmp>4)) {
		uWarning("Label group must be an integer in the range 1..4\n");
		uAction("Illegal group %d ignored\n",tmp);
	    }	 
	    else args.baseLabelGroup= tmp-1;
	}
	else if (strcmp(argv[i],"-ll")==0) {
	    int tmp;
	    if (++i>=argc) {
		uWarning("Label level not specified\n");
		uAction("Trailing \"-ll\" option ignored\n");
	    }
	    else if ((sscanf(argv[i],"%i",&tmp)!=1)||(tmp<1)||(tmp>255)) {
		uWarning("Label level must be in the range 1..255\n");
		uAction("Illegal level %d ignored\n",tmp);
	    }	 
	    else args.labelLevel= tmp-1;
	}
	else if (strcmp(argv[i],"-level1")==0)
	    args.level1= True;
	else if (strcmp(argv[i],"-level2")==0)
	    args.level1= False;
	else if (strcmp(argv[i],"-mono")==0) {
	    args.wantColor= False;
	}
	else if (strcmp(argv[i],"-n")==0) {
	    int tmp;
	    if (++i>=argc) {
		uWarning("Number of copies not specified\n");
		uAction("Trailing \"-n\" option ignored\n");
	    }
	    else if ((sscanf(argv[i],"%i",&tmp)!=1)||(tmp<1)) {
		uWarning("Number of copies must be an integer > zero\n");
		uAction("Illegal count %d ignored\n",tmp);
	    }	 
	    else args.copies= tmp;
	}
	else if (strcmp(argv[i],"-nokc")==0) {
	    args.wantKeycodes= False;
	}
	else if (strcmp(argv[i],"-nkg")==0) {
	    int tmp;
	    if (++i>=argc) {
		uWarning("Number of groups per key not specified\n");
		uAction("Trailing \"-nkg\" option ignored\n");
	    }
	    else if ((sscanf(argv[i],"%i",&tmp)!=1)||(tmp<1)||(tmp>2)) {
		uWarning("Groups per key must be in the range 1..2\n");
		uAction("Illegal number of groups %d ignored\n",tmp);
	    }	 
	    else args.nLabelGroups= tmp;
	}
	else if (strcmp(argv[i],"-npk")==0) {
	    int tmp;
	    if (++i>=argc) {
		uWarning("Number of keyboards per page not specified\n");
		uAction("Trailing \"-npk\" option ignored\n");
	    }
	    else if ((sscanf(argv[i],"%i",&tmp)!=1)||(tmp<1)||(tmp>2)) {
		uWarning("Keyboards per page must be in the range 1..2\n");
		uAction("Illegal number of keyboards %d ignored\n",tmp);
	    }	 
	    else args.nKBPerPage= tmp;
	}
	else if (strcmp(argv[i],"-ntg")==0) {
	    int tmp;
	    if (++i>=argc) {
		uWarning("Total number of groups not specified\n");
		uAction("Trailing \"-ntg\" option ignored\n");
	    }
	    else if ((sscanf(argv[i],"%i",&tmp)!=1)||(tmp<1)||(tmp>4)) {
		uWarning("Total number of groups must be in the range 1..4\n");
		uAction("Illegal number of groups %d ignored\n",tmp);
	    }	 
	    else args.nTotalGroups= tmp;
	}
	else if (strcmp(argv[i],"-o")==0) {
	    if (++i>=argc) {
		uWarning("No output file specified\n");
		uAction("Trailing \"-o\" option ignored\n");
	    }
	    else if (outputFile!=NULL) {
		uWarning("Multiple output files specified\n");
		uAction("Compiling %s, ignoring %s\n",outputFile,argv[i]);
	    }
	    else outputFile= argv[i];
	}
	else if (strncmp(argv[i],"-R",2)==0) {
	    if (argv[i][2]=='\0') {
		uWarning("No root directory specified\n");
		uAction("Ignoring -R option\n");
	    }
	    else if (rootDir!=NULL) {
		uWarning("Multiple root directories specified\n");
		uAction("Using %s, ignoring %s\n",rootDir,argv[i]);
	    }
	    else rootDir= &argv[i][2];
	}
	else if (strcmp(argv[i],"-pict")==0) {
	    if (++i>=argc) {
		uWarning("No level of pictographs specified\n");
		uAction("Trailing \"-pict\" option ignored\n");
	    }
	    else if (strcmp(argv[i],"none")==0)
		args.wantSymbols= NO_SYMBOLS;
	    else if (strcmp(argv[i],"common")==0)
		args.wantSymbols= COMMON_SYMBOLS;
	    else if (strcmp(argv[i],"all")==0)
		args.wantSymbols= ALL_SYMBOLS;
	    else if (outputFile!=NULL) {
		uWarning("Unknown pictograph level specified\n");
		uAction("Ignoring illegal value %s\n",argv[i]);
	    }
	}
	else if ((strcmp(argv[i],"-synch")==0)||(strcmp(argv[i],"-s")==0)) {
	    synch= True;
	}
	else if (strcmp(argv[i],"-w")==0) {
	    if ((i>=(argc-1))||(!isdigit(argv[i+1][0]))) {
		warningLevel= 0;
	    }
	    else {
		int itmp;
		if (sscanf(argv[++i],"%i",&itmp))
		    warningLevel = itmp;
	    }
	}
	else {
	    uError("Unknown flag \"%s\" on command line\n",argv[i]);
	    Usage(argc,argv);
	    return False;
	}
    }
    if (rootDir) {
	if (warningLevel>8) {
	    uWarning("Changing root directory to \"%s\"\n",rootDir);
	}
	if ((chdir(rootDir)<0) && (warningLevel>0)) {
	    uWarning("Couldn't change root directory to \"%s\"\n",rootDir);
	    uAction("Root directory (-R) option ignored\n");
	}
    }
    if (outputFont!=NULL) {
	Bool  ok;
	FILE *file= NULL;

	if (outputFile==NULL) {
	    outputFile= uAlloc(strlen(outputFont)+5);
	    sprintf(outputFile,"%s.pfa",outputFont);
	}
	else if (uStringEqual(outputFile,"-"))
	    file= stdout;

	if (file==NULL)
	    file= fopen(outputFile,"w");
	   	 
	if (!file) {
	    uError("Couldn't open \"%s\" to dump internal font \"%s\"\n",
						outputFile,outputFont);
	    uAction("Exiting\n");
	    exit(1);
	}
	ok= DumpInternalFont(file,outputFont);
	if (file!=stdout)
	    fclose(file);
	if (!ok) {
	    uWarning("No internal font to dump\n");
	    if (file!=stdout) {
		uAction("Removing \"%s\"\n",outputFile);
		unlink(outputFile);
	    }
	}
	exit((ok!=0));
    }
    if (inputFile==NULL) {
	uError("No input file specified\n");
	Usage(argc,argv);
	return False;
    }
    else if (uStringEqual(inputFile,"-")) {
	/* Nothing */
    }
    else if (strchr(inputFile,':')==0) {
	int	len= strlen(inputFile);
	if ((len>4)&&(strcmp(&inputFile[len-4],".xkm")==0)) {
	    /* Nothing */
	}
	else {
	    FILE *file;
	    file= fopen(inputFile,"r");
	    if (file) {
		fclose(file);
	    }
	    else {
		fprintf(stderr,"Cannot open \"%s\" for reading\n",inputFile);
		return False;
	    }
	}
    }
    else {
	inDpyName= inputFile;
	inputFile= NULL;
    }

    if (outputFormat==WANT_DEFAULT)
	outputFormat= WANT_PS_FILE;
    if ((outputFile==NULL)&&(inputFile!=NULL)&&uStringEqual(inputFile,"-")) {
	int len;
	len= strlen("stdin.eps")+2;
	outputFile= uTypedCalloc(len,char);
	if (outputFile==NULL) {
	    uInternalError("Cannot allocate space for output file name\n");
	    uAction("Exiting\n");
	    exit(1);
	}
	if (args.wantEPS)	sprintf(outputFile,"stdin.eps");
	else			sprintf(outputFile,"stdin.ps");
    }
    else if ((outputFile==NULL)&&(inputFile!=NULL)) {
	int len;
	char *base,*ext;

	base= strrchr(inputFile,'/');
	if (base==NULL)	base= inputFile;
	else		base++;

	len= strlen(base)+strlen("eps")+2;
	outputFile= uTypedCalloc(len,char);
	if (outputFile==NULL) {
	    uInternalError("Cannot allocate space for output file name\n");
	    uAction("Exiting\n");
	    exit(1);
	}
	ext= strrchr(base,'.');
	if (ext==NULL) {
	    if (args.wantEPS)	sprintf(outputFile,"%s.eps",base);
	    else		sprintf(outputFile,"%s.ps",base);
	}
	else {
	    strcpy(outputFile,base);
	    if (args.wantEPS)	strcpy(&outputFile[ext-base+1],"eps");
	    else		strcpy(&outputFile[ext-base+1],"ps");
	}
    }
    else if (outputFile==NULL) {
	int len;
	char *ch,*name,buf[128];
	if (inDpyName[0]==':')	
	     sprintf(name=buf,"server%s",inDpyName);
	else name= inDpyName;

	len= strlen(name)+strlen("eps")+2;
	outputFile= uTypedCalloc(len,char);
	if (outputFile==NULL) {
	    uInternalError("Cannot allocate space for output file name\n");
	    uAction("Exiting\n");
	    exit(1);
	}
	strcpy(outputFile,name);
	for (ch=outputFile;(*ch)!='\0';ch++) {
	    if 	(*ch==':')	*ch= '-';
	    else if (*ch=='.')	*ch= '_';
	}
	*ch++= '.';
	if (args.wantEPS)	strcpy(ch,"eps");
	else			strcpy(ch,"ps");
    }
    else if (strchr(outputFile,':')!=NULL) {
	outDpyName= outputFile;
	outputFile= NULL;
	outputFormat= WANT_X_SERVER;
	uInternalError("Output to an X server not implemented yet\n");
	return False;
    }
    return True;
}

static Display *
GetDisplay(char *program, char *dpyName)
{
int	mjr,mnr,error;
Display	*dpy;

    mjr= XkbMajorVersion;
    mnr= XkbMinorVersion;
    dpy= XkbOpenDisplay(dpyName,NULL,NULL,&mjr,&mnr,&error);
    if (dpy==NULL) {
	switch (error) {
	    case XkbOD_BadLibraryVersion:
		uInformation("%s was compiled with XKB version %d.%02d\n",
				program,XkbMajorVersion,XkbMinorVersion);
		uError("X library supports incompatible version %d.%02d\n",
				mjr,mnr);
		break;
	    case XkbOD_ConnectionRefused:
		uError("Cannot open display \"%s\"\n",dpyName);
		break;
	    case XkbOD_NonXkbServer:
		uError("XKB extension not present on %s\n",dpyName);
		break;
	    case XkbOD_BadServerVersion:
		uInformation("%s was compiled with XKB version %d.%02d\n",
				program,XkbMajorVersion,XkbMinorVersion);
		uError("Server %s uses incompatible version %d.%02d\n",
				dpyName,mjr,mnr);
		break;
	    default:
		uInternalError("Unknown error %d from XkbOpenDisplay\n",error);
	}
    }
    else if (synch)
	XSynchronize(dpy,True);
    return dpy;
}

/***====================================================================***/

#ifdef notyet
#define MAX_INCLUDE_OPTS	10
static char	*includeOpt[MAX_INCLUDE_OPTS];
static int	numIncludeOpts = 0;
#endif

int
main(int argc, char *argv[])
{
FILE 	*	file;
int		ok;
XkbFileInfo 	result;

    uSetEntryFile(NullString);
    uSetDebugFile(NullString);
    uSetErrorFile(NullString);
    if (!parseArgs(argc,argv))
	exit(1);
#ifdef DEBUG
#ifdef sgi
    if (debugFlags&0x4)
	mallopt(M_DEBUG,1);
#endif
#endif
    file= NULL;
    XkbInitAtoms(NULL);
/*     XkbInitIncludePath(); */
    if (inputFile!=NULL) {
	if (uStringEqual(inputFile,"-")) {
	    static char *in= "stdin";
	    file= stdin;
	    inputFile= in;
	}
	else {
	    file= fopen(inputFile,"r");
	}
    }
    else if (inDpyName!=NULL) {
	inDpy= GetDisplay(argv[0],inDpyName);
	if (!inDpy) {
	    uAction("Exiting\n");
	    exit(1);
	}
    }
    if (outDpyName!=NULL) {
	uInternalError("Output to an X server not implemented yet\n");
	outDpy= GetDisplay(argv[0],outDpyName);
	if (!outDpy) {
	    uAction("Exiting\n");
	    exit(1);
	}
    }
    if ((inDpy==NULL) && (outDpy==NULL)) {
	int	mjr,mnr;
	mjr= XkbMajorVersion;
	mnr= XkbMinorVersion;
	if (!XkbLibraryVersion(&mjr,&mnr)) {
	    uInformation("%s was compiled with XKB version %d.%02d\n",
				argv[0],XkbMajorVersion,XkbMinorVersion);
	    uError("X library supports incompatible version %d.%02d\n",
				mjr,mnr);
	    uAction("Exiting\n");
	    exit(1);
	}
    }
    ok= True;
    if (file) {
	unsigned tmp;
	bzero((char *)&result,sizeof(result));
	if ((result.xkb= XkbAllocKeyboard())==NULL) {
	    uFatalError("Cannot allocate keyboard description\n");
	    /* NOTREACHED */
	}
	tmp= XkmReadFile(file,XkmGeometryMask,XkmKeymapLegal,&result);
	if ((tmp&XkmGeometryMask)!=0) {
	    uError("Couldn't read geometry from XKM file \"%s\"\n",inputFile);
	    uAction("Exiting\n");
	    ok= False;
	}
	if ((tmp&XkmKeyNamesMask)!=0)
	    args.wantKeycodes= False;
	if (args.label==LABEL_AUTO) {
	    if (result.defined&XkmSymbolsMask)
		 args.label= LABEL_SYMBOLS;
	    else if (result.defined&XkmKeyNamesMask)	
		 args.label= LABEL_KEYCODE;
	    else args.label= LABEL_KEYNAME;
	}
	else if ((args.label==LABEL_KEYCODE)&&((tmp&XkmKeyNamesMask)!=0)) {
	    uError("XKM file \"%s\" doesn't have keycodes\n",inputFile);
	    uAction("Cannot label keys as requested. Exiting\n");
	    ok= False;
	}
	else if ((args.label==LABEL_SYMBOLS)&&((tmp&XkmSymbolsMask)!=0)) {
	    uError("XKM file \"%s\" doesn't have symbols\n",inputFile);
	    uAction("Cannot label keys as requested. Exiting\n");
	    ok= False;
	}
    }
    else if (inDpy!=NULL) {
	bzero((char *)&result,sizeof(result));
	result.type= XkmKeymapFile;
	result.xkb= XkbGetMap(inDpy,XkbAllMapComponentsMask,XkbUseCoreKbd);
	if (result.xkb==NULL)
	    uWarning("Cannot load keyboard description\n");
	if (XkbGetNames(inDpy,XkbAllNamesMask,result.xkb)!=Success)
	    uWarning("Cannot load names\n");
	if (XkbGetGeometry(inDpy,result.xkb)!=Success) {
	    uFatalError("Cannot load geometry for %s\n",inDpyName);
	}
#ifdef NOTYET
	if (computeDflts)
	     ok= (ComputeKbdDefaults(result.xkb)==Success);
	else ok= True;
#endif
	if (args.label==LABEL_AUTO) 
	    args.label= LABEL_SYMBOLS;
    }
    else {
	fprintf(stderr,"Cannot open \"%s\" to read geometry\n",inputFile);
	ok= 0;
    }
    if (ok) {
	FILE *out = NULL;
	if (setlocale(LC_ALL,(wantLocale))==NULL) {
	    if (wantLocale!=NULL) {
		uWarning("Couldn't change to locale %s\n",wantLocale);
		uAction("Using \"C\" locale, instead\n");
	    }
	}
	/* need C numerics so decimal point doesn't get screwed up */
	setlocale(LC_NUMERIC,"C");
	if ((inDpy!=outDpy)&&
	    (XkbChangeKbdDisplay(outDpy,&result)!=Success)) {
	    uInternalError("Error converting keyboard display from %s to %s\n",
	    						inDpyName,outDpyName);
	    exit(1);
	}
	if (outputFile!=NULL) {
	    if (uStringEqual(outputFile,"-")) {
		static char *of= "stdout";
		out= stdout;
		outputFile= of;
	    }
	    else {
		out= fopen(outputFile,"w");
		if (out==NULL) {
		    uError("Cannot open \"%s\" to write keyboard description\n",
								outputFile);
		     uAction("Exiting\n");
		     exit(1);
		}
	    }
	}
	switch (outputFormat) {
	    case WANT_PS_FILE:
		ok= GeometryToPostScript(out,&result,&args);
		break;
	    case WANT_X_SERVER:
		uInternalError("Output to X server not implemented yet\n");
		break;
	    default:
		uInternalError("Unknown output format %d\n",outputFormat);
		uAction("No output file created\n");
		ok= False;
		break;
	}
	if (!ok) {
	    uError("Error creating output file\n");
	}
    }
    if (inDpy) 
	XCloseDisplay(inDpy);
    inDpy= NULL;
    if (outDpy)
	XCloseDisplay(outDpy);
    return (ok==0);
}