xref: /dragonfly/usr.sbin/kbdmap/kbdmap.c (revision 5868d2b9)
1 /*-
2  * Copyright (c) 2002 Jonathan Belson <jon@witchspace.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/usr.sbin/kbdmap/kbdmap.c,v 1.2 2002/10/27 17:44:33 wollman Exp $
27  */
28 
29 #include <sys/types.h>
30 #include <sys/queue.h>
31 
32 #include <assert.h>
33 #include <ctype.h>
34 #include <dirent.h>
35 #include <limits.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stringlist.h>
40 #include <unistd.h>
41 
42 #include "kbdmap.h"
43 
44 
45 static const char *lang_default = DEFAULT_LANG;
46 static const char *font;
47 static const char *lang;
48 static const char *program;
49 static const char *keymapdir = DEFAULT_KEYMAP_DIR;
50 static const char *fontdir = DEFAULT_FONT_DIR;
51 static const char *sysconfig = DEFAULT_SYSCONFIG;
52 static const char *font_default = DEFAULT_FONT;
53 static const char *font_current;
54 static const char *dir;
55 static const char *menu = "";
56 
57 static int x11;
58 static int show;
59 static int verbose;
60 static int print;
61 
62 
63 struct keymap {
64 	char	*desc;
65 	char	*keym;
66 	int	mark;
67 	SLIST_ENTRY(keymap) entries;
68 };
69 static SLIST_HEAD(slisthead, keymap) head = SLIST_HEAD_INITIALIZER(head);
70 
71 
72 /*
73  * Get keymap entry for 'key', or NULL of not found
74  */
75 static struct keymap *
76 get_keymap(const char *key)
77 {
78 	struct keymap *km;
79 
80 	SLIST_FOREACH(km, &head, entries)
81 		if (!strcmp(km->keym, key))
82 			return km;
83 
84 	return NULL;
85 }
86 
87 /*
88  * Count the number of keymaps we found
89  */
90 static int
91 get_num_keymaps(void)
92 {
93 	struct keymap *km;
94 	int count = 0;
95 
96 	SLIST_FOREACH(km, &head, entries)
97 		count++;
98 
99 	return count;
100 }
101 
102 /*
103  * Remove any keymap with given keym
104  */
105 static void
106 remove_keymap(const char *keym)
107 {
108 	struct keymap *km;
109 
110 	SLIST_FOREACH(km, &head, entries) {
111 		if (!strcmp(keym, km->keym)) {
112 			SLIST_REMOVE(&head, km, keymap, entries);
113 			free(km);
114 			break;
115 		}
116 	}
117 }
118 
119 /*
120  * Add to hash with 'key'
121  */
122 static void
123 add_keymap(const char *desc, int mark, const char *keym)
124 {
125 	struct keymap *km, *km_new;
126 
127 	/* Is there already an entry with this key? */
128 	SLIST_FOREACH(km, &head, entries) {
129 		if (!strcmp(km->keym, keym)) {
130 			/* Reuse this entry */
131 			free(km->desc);
132 			km->desc = strdup(desc);
133 			km->mark = mark;
134 			return;
135 		}
136 	}
137 
138 	km_new = (struct keymap *) malloc (sizeof(struct keymap));
139 	km_new->desc = strdup(desc);
140 	km_new->keym = strdup(keym);
141 	km_new->mark = mark;
142 
143 	/* Add to keymap list */
144 	SLIST_INSERT_HEAD(&head, km_new, entries);
145 }
146 
147 /*
148  * Figure out the default language to use.
149  */
150 static const char *
151 get_locale(void)
152 {
153 	const char *locale;
154 
155 	if ((locale = getenv("LC_ALL")) == NULL &&
156 	    (locale = getenv("LC_CTYPE")) == NULL &&
157 	    (locale = getenv("LANG")) == NULL)
158 		locale = lang_default;
159 
160 	/* Check for alias */
161 	if (!strcmp(locale, "C"))
162 		locale = DEFAULT_LANG;
163 
164 	return locale;
165 }
166 
167 /*
168  * Extract filename part
169  */
170 static const char *
171 extract_name(const char *name)
172 {
173 	char *p;
174 
175 	p = strrchr(name, '/');
176 	if (p != NULL && p[1] != '\0')
177 		return p + 1;
178 
179 	return name;
180 }
181 
182 /*
183  * Return file extension or NULL
184  */
185 static char *
186 get_extension(const char *name)
187 {
188 	char *p;
189 
190 	p = strrchr(name, '.');
191 
192 	if (p != NULL && p[1] != '\0')
193 		return p;
194 
195 	return NULL;
196 }
197 
198 /*
199  * Read font from /etc/rc.conf else return default.
200  * Freeing the memory is the caller's responsibility.
201  */
202 static char *
203 get_font(void)
204 {
205 	char line[256], buf[20];
206 	char *fnt = NULL;
207 
208 	FILE *fp = fopen(sysconfig, "r");
209 	if (fp) {
210 		while (fgets(line, sizeof(line), fp)) {
211 			int a, b, matches;
212 
213 			if (line[0] == '#')
214 				continue;
215 
216 			matches = sscanf(line,
217 			    " font%dx%d = \"%20[-.0-9a-zA-Z_]",
218 			    &a, &b, buf);
219 			if (matches==3) {
220 				if (strcmp(buf, "NO")) {
221 					if (fnt)
222 						free(fnt);
223 					fnt = (char *) malloc(strlen(buf) + 1);
224 					strcpy(fnt, buf);
225 				}
226 			}
227 		}
228 	} else
229 		fprintf(stderr, "Could not open %s for reading\n", sysconfig);
230 
231 	return fnt;
232 }
233 
234 /*
235  * Set a font using 'vidcontrol'
236  */
237 static void
238 vidcontrol(const char *fnt)
239 {
240 	char *tmp, *p, *q;
241 	char ch;
242 	int i;
243 
244 	/* syscons test failed */
245 	if (x11)
246 		return;
247 
248 	tmp = strdup(fnt);
249 
250 	/* Extract font size */
251 	p = strrchr(tmp, '-');
252 	if (p && p[1] != '\0') {
253 		p++;
254 		/* Remove any '.fnt' extension */
255 		if ((q = strstr(p, ".fnt")))
256 			*q = '\0';
257 
258 		/*
259 		 * Check font size is valid, with no trailing characters
260 		 *  ('&ch' should not be matched)
261 		 */
262 		if (sscanf(p, "%dx%d%c", &i, &i, &ch) != 2)
263 			fprintf(stderr, "Which font size? %s\n", fnt);
264 		else {
265 			char *cmd;
266 			asprintf(&cmd, "vidcontrol -f %s %s", p, fnt);
267 			if (verbose)
268 				fprintf(stderr, "%s\n", cmd);
269 			system(cmd);
270 			free(cmd);
271 		}
272 	} else
273 		fprintf(stderr, "Which font size? %s\n", fnt);
274 
275 	free(tmp);
276 }
277 
278 /*
279  * Execute 'kbdcontrol' with the appropriate arguments
280  */
281 static void
282 do_kbdcontrol(struct keymap *km)
283 {
284 	char *kbd_cmd;
285 	asprintf(&kbd_cmd, "kbdcontrol -l %s/%s", dir, km->keym);
286 
287 	if (!x11)
288 		system(kbd_cmd);
289 
290 	printf("keymap=%s\n", km->keym);
291 	free(kbd_cmd);
292 }
293 
294 /*
295  * Call 'vidcontrol' with the appropriate arguments
296  */
297 static void
298 do_vidfont(struct keymap *km)
299 {
300 	char *vid_cmd, *tmp, *p, *q;
301 
302 	asprintf(&vid_cmd, "%s/%s", dir, km->keym);
303 	vidcontrol(vid_cmd);
304 	free(vid_cmd);
305 
306 	tmp = strdup(km->keym);
307 	p = strrchr(tmp, '-');
308 	if (p && p[1]!='\0') {
309 		p++;
310 		q = get_extension(p);
311 		if (q) {
312 			*q = '\0';
313 			printf("font%s=%s\n", p, km->keym);
314 		}
315 	}
316 	free(tmp);
317 }
318 
319 /*
320  * Display dialog from 'keymaps[]'
321  */
322 static void
323 show_dialog(struct keymap **km_sorted, int num_keymaps)
324 {
325 	FILE *fp;
326 	char *cmd, *dialog;
327 	char tmp_name[] = "/tmp/_kbd_lang.XXXX";
328 	const char *ext;
329 	int fd, i, size;
330 
331 	fd = mkstemp(tmp_name);
332 	if (fd == -1) {
333 		fprintf(stderr, "Could not open temporary file \"%s\"\n",
334 		    tmp_name);
335 		exit(1);
336 	}
337 	asprintf(&dialog, "/usr/bin/dialog --clear --title \"Keyboard Menu\" "
338 			  "--menu \"%s\" -1 -1 10", menu);
339 
340 	ext = extract_name(dir);
341 
342 	/* start right font, assume that current font is equal
343 	 * to default font in /etc/rc.conf
344 	 *
345 	 * $font is the font which require the language $lang; e.g.
346 	 * russian *need* a koi8 font
347 	 * $font_current is the current font from /etc/rc.conf
348 	 */
349 	if (font && strcmp(font, font_current))
350 		vidcontrol(font);
351 
352 	/* Build up the command */
353 	size = 0;
354 	for (i=0; i<num_keymaps; i++) {
355 		/*
356 		 * Each 'font' is passed as ' "font" ""', so allow the
357 		 * extra space
358 		 */
359 		size += strlen(km_sorted[i]->desc) + 6;
360 	}
361 
362 	/* Allow the space for '2> tmpfilename' redirection */
363 	size += strlen(tmp_name) + 3;
364 
365 	cmd = (char *) malloc(strlen(dialog) + size + 1);
366 	strcpy(cmd, dialog);
367 
368 	for (i=0; i<num_keymaps; i++) {
369 		strcat(cmd, " \"");
370 		strcat(cmd, km_sorted[i]->desc);
371 		strcat(cmd, "\"");
372 		strcat(cmd, " \"\"");
373 	}
374 
375 	strcat(cmd, " 2>");
376 	strcat(cmd, tmp_name);
377 
378 	/* Show the dialog.. */
379 	system(cmd);
380 
381 	fp = fopen(tmp_name, "r");
382 	if (fp) {
383 		char choice[64];
384 		if (fgets(choice, 64, fp) != NULL) {
385 			/* Find key for desc */
386 			for (i=0; i<num_keymaps; i++) {
387 				if (!strcmp(choice, km_sorted[i]->desc)) {
388 					if (!strcmp(program, "kbdmap"))
389 						do_kbdcontrol(km_sorted[i]);
390 					else
391 						do_vidfont(km_sorted[i]);
392 					break;
393 				}
394 			}
395 		} else {
396 			if (font != NULL && strcmp(font, font_current))
397 				/* Cancelled, restore old font */
398 				vidcontrol(font_current);
399 		}
400 		fclose(fp);
401 	} else
402 		fprintf(stderr, "Failed to open temporary file");
403 
404 	/* Tidy up */
405 	remove(tmp_name);
406 	free(cmd);
407 	free(dialog);
408 	close(fd);
409 }
410 
411 /*
412  * Search for 'token' in comma delimited array 'buffer'.
413  * Return true for found, false for not found.
414  */
415 static int
416 find_token(const char *buffer, const char *token)
417 {
418 	char *buffer_tmp, *buffer_copy, *inputstring;
419 	char **ap;
420 	int found;
421 
422 	buffer_copy = strdup(buffer);
423 	buffer_tmp = buffer_copy;
424 	inputstring = buffer_copy;
425 	ap = &buffer_tmp;
426 
427 	found = 0;
428 
429 	while ((*ap = strsep(&inputstring, ",")) != NULL) {
430 		if (strcmp(buffer_tmp, token) == 0) {
431 			found = 1;
432 			break;
433 		}
434 	}
435 
436 	free(buffer_copy);
437 
438 	return found;
439 }
440 
441 /*
442  * Compare function for qsort
443  */
444 static int
445 compare_keymap(const void *a, const void *b)
446 {
447 
448 	/* We've been passed pointers to pointers, so: */
449 	const struct keymap *km1 = *((const struct keymap * const *) a);
450 	const struct keymap *km2 = *((const struct keymap * const *) b);
451 
452 	return strcmp(km1->desc, km2->desc);
453 }
454 
455 /*
456  * Compare function for qsort
457  */
458 static int
459 compare_lang(const void *a, const void *b)
460 {
461 	const char *l1 = *((const char * const *) a);
462 	const char *l2 = *((const char * const *) b);
463 
464 	return strcmp(l1, l2);
465 }
466 
467 /*
468  * Change '8x8' to '8x08' so qsort will put it before eg. '8x14'
469  */
470 static void
471 kludge_desc(struct keymap **km_sorted, int num_keymaps)
472 {
473 	int i;
474 
475 	for (i=0; i<num_keymaps; i++) {
476 		char *p;
477 		char *km = km_sorted[i]->desc;
478 		if ((p = strstr(km, "8x8")) != NULL) {
479 			int len;
480 			int j;
481 			int offset;
482 
483 			offset = p - km;
484 
485 			/* Make enough space for the extra '0' */
486 			len = strlen(km);
487 			km = realloc(km, len + 2);
488 
489 			for (j=len; j!=offset+1; j--)
490 				km[j + 1] = km[j];
491 
492 			km[offset+2] = '0';
493 
494 			km_sorted[i]->desc = km;
495 		}
496 	}
497 }
498 
499 /*
500  * Reverse 'kludge_desc()' - change '8x08' back to '8x8'
501  */
502 static void
503 unkludge_desc(struct keymap **km_sorted, int num_keymaps)
504 {
505 	int i;
506 
507 	for (i=0; i<num_keymaps; i++) {
508 		char *p;
509 		char *km = km_sorted[i]->desc;
510 		if ((p = strstr(km, "8x08")) != NULL) {
511 			p += 2;
512 			while (*p++)
513 				p[-1] = p[0];
514 
515 			km = realloc(km, p - km - 1);
516 			km_sorted[i]->desc = km;
517 		}
518 	}
519 }
520 
521 /*
522  * Return 0 if file exists and is readable, else -1
523  */
524 static int
525 check_file(const char *keym)
526 {
527 	int status = 0;
528 
529 	if (access(keym, R_OK) == -1) {
530 		char *fn;
531 		asprintf(&fn, "%s/%s", dir, keym);
532 		if (access(fn, R_OK) == -1) {
533 			if (verbose)
534 				fprintf(stderr, "%s not found!\n", fn);
535 			status = -1;
536 		}
537 		free(fn);
538 	} else {
539 		if (verbose)
540 			fprintf(stderr, "No read permission for %s!\n", keym);
541 		status = -1;
542 	}
543 
544 	return status;
545 }
546 
547 /*
548  * Read options from the relevent configuration file, then
549  *  present to user.
550  */
551 static void
552 menu_read(void)
553 {
554 	const char *lg;
555 	char *p;
556 	int mark, num_keymaps, items, i;
557 	char buffer[256], filename[PATH_MAX];
558 	char keym[64], lng[64], desc[64];
559 	char dialect[64], lang_abk[64];
560 	struct keymap *km;
561 	struct keymap **km_sorted;
562 	struct dirent *dp;
563 	StringList *lang_list;
564 	FILE *fp;
565 	DIR *dirp;
566 
567 	lang_list = sl_init();
568 
569 	sprintf(filename, "%s/INDEX.%s", dir, extract_name(dir));
570 
571 	/* en_US.ISO8859-1 -> en_..\.ISO8859-1 */
572 	strlcpy(dialect, lang, sizeof(dialect));
573 	if (strlen(dialect) >= 6 && dialect[2] == '-') {
574 		dialect[3] = '.';
575 		dialect[4] = '.';
576 	}
577 
578 
579 	/* en_US.ISO8859-1 -> en */
580 	strlcpy(lang_abk, lang, sizeof(lang_abk));
581 	if (strlen(lang_abk) >= 3 && lang_abk[2] == '-')
582 		lang_abk[2] = '.';
583 
584 	fprintf(stderr, "lang_default = %s\n", lang_default);
585 	fprintf(stderr, "dialect = %s\n", dialect);
586 	fprintf(stderr, "lang_abk = %s\n", lang_abk);
587 
588 	fp = fopen(filename, "r");
589 	if (fp) {
590 		int matches;
591 		while (fgets(buffer, sizeof(buffer), fp)) {
592 			p = buffer;
593 			if (p[0] == '#')
594 				continue;
595 
596 			while (isspace(*p))
597 				p++;
598 
599 			if (*p == '\0')
600 				continue;
601 
602 			/* Parse input, removing newline */
603 			matches = sscanf(p, "%64[^:]:%64[^:]:%64[^:\n]",
604 			    keym, lng, desc);
605 			if (matches == 3) {
606 				if (strcmp(keym, "FONT")
607 				    && strcmp(keym, "MENU")) {
608 					/* Check file exists & is readable */
609 					if (check_file(keym) == -1)
610 						continue;
611 				}
612 			}
613 
614 			if (show) {
615 				/*
616 				 * Take note of supported languages, which
617 				 * might be in a comma-delimited list
618 				 */
619 				char *tmp = strdup(lng);
620 				char *delim = tmp;
621 
622 				for (delim = tmp; ; ) {
623 					char ch = *delim++;
624 					if (ch == ',' || ch == '\0') {
625 						delim[-1] = '\0';
626 						if (!sl_find(lang_list, tmp))
627 							sl_add(lang_list, tmp);
628 						if (ch == '\0')
629 							break;
630 						tmp = delim;
631 					}
632 				}
633 			}
634 			/* Set empty language to default language */
635 			if (lng[0] == '\0')
636 				lg = lang_default;
637 			else
638 				lg = lng;
639 
640 
641 			/* 4) Your choice if it exists
642 			 * 3) Long match eg. en_GB.ISO8859-1 is equal to
643 			 *      en_..\.ISO8859-1
644 			 * 2) short match 'de'
645 			 * 1) default langlist 'en'
646 			 * 0) any language
647 			 *
648 			 * Language may be a comma separated list
649 			 * A higher match overwrites a lower
650 			 * A later entry overwrites a previous if it exists
651 			 *     twice in the database
652 			 */
653 
654 			/* Check for favoured language */
655 			km = get_keymap(keym);
656 			mark = (km) ? km->mark : 0;
657 
658 			if (find_token(lg, lang))
659 				add_keymap(desc, 4, keym);
660 			else if (mark <= 3 && find_token(lg, dialect))
661 				add_keymap(desc, 3, keym);
662 			else if (mark <= 2 && find_token(lg, lang_abk))
663 				add_keymap(desc, 2, keym);
664 			else if (mark <= 1 && find_token(lg, lang_default))
665 				add_keymap(desc, 1, keym);
666 			else if (mark <= 0)
667 				add_keymap(desc, 0, keym);
668 		}
669 		fclose(fp);
670 
671 	} else
672 		printf("Could not open file\n");
673 
674 	if (show) {
675 		qsort(lang_list->sl_str, lang_list->sl_cur, sizeof(char*),
676 		    compare_lang);
677 		printf("Currently supported languages: ");
678 		for (i=0; i< (int) lang_list->sl_cur; i++)
679 			printf("%s ", lang_list->sl_str[i]);
680 		puts("");
681 		exit(0);
682 	}
683 
684 	km = get_keymap("MENU");
685 	if (km)
686 		/* Take note of menu title */
687 		menu = strdup(km->desc);
688 	km = get_keymap("FONT");
689 	if (km)
690 		/* Take note of language font */
691 		font = strdup(km->desc);
692 
693 	/* Remove unwanted items from list */
694 	remove_keymap("MENU");
695 	remove_keymap("FONT");
696 
697 	/* Look for keymaps not in database */
698 	dirp = opendir(dir);
699 	if (dirp) {
700 		while ((dp = readdir(dirp)) != NULL) {
701 			const char *ext = get_extension(dp->d_name);
702 			if (ext) {
703 				if ((!strcmp(ext, ".fnt") ||
704 				    !strcmp(ext, ".kbd")) &&
705 				    !get_keymap(dp->d_name)) {
706 					char *q;
707 
708 					/* Remove any .fnt or .kbd extension */
709 					q = strdup(dp->d_name);
710 					*(get_extension(q)) = '\0';
711 					add_keymap(q, 0, dp->d_name);
712 					free(q);
713 
714 					if (verbose)
715 						fprintf(stderr,
716 						    "'%s' not in database\n",
717 						    dp->d_name);
718 				}
719 			}
720 		}
721 		closedir(dirp);
722 	} else
723 		fprintf(stderr, "Could not open directory '%s'\n", dir);
724 
725 	/* Sort items in keymap */
726 	num_keymaps = get_num_keymaps();
727 
728 	km_sorted = (struct keymap **)
729 	    malloc(num_keymaps*sizeof(struct keymap *));
730 
731 	/* Make array of pointers to items in hash */
732 	items = 0;
733 	SLIST_FOREACH(km, &head, entries)
734 		km_sorted[items++] = km;
735 
736 	/* Change '8x8' to '8x08' so sort works as we might expect... */
737 	kludge_desc(km_sorted, num_keymaps);
738 
739 	qsort(km_sorted, num_keymaps, sizeof(struct keymap *), compare_keymap);
740 
741 	/* ...change back again */
742 	unkludge_desc(km_sorted, num_keymaps);
743 
744 	if (print) {
745 		for (i=0; i<num_keymaps; i++)
746 			printf("%s\n", km_sorted[i]->desc);
747 		exit(0);
748 	}
749 
750 	show_dialog(km_sorted, num_keymaps);
751 
752 	free(km_sorted);
753 }
754 
755 /*
756  * Display usage information and exit
757  */
758 static void
759 usage(void)
760 {
761 
762 	fprintf(stderr, "usage: %s\t[-K] [-V] [-d|-default] [-h|-help] "
763 	    "[-l|-lang language]\n\t\t[-p|-print] [-r|-restore] [-s|-show] "
764 	    "[-v|-verbose]\n", program);
765 	exit(1);
766 }
767 
768 static void
769 parse_args(int argc, char **argv)
770 {
771 	int i;
772 
773 	for (i=1; i<argc; i++) {
774 		if (argv[i][0] != '-')
775 			usage();
776 		else if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "-h"))
777 			usage();
778 		else if (!strcmp(argv[i], "-verbose") || !strcmp(argv[i], "-v"))
779 			verbose = 1;
780 		else if (!strcmp(argv[i], "-lang") || !strcmp(argv[i], "-l"))
781 			if (i + 1 == argc)
782 				usage();
783 			else
784 				lang = argv[++i];
785 		else if (!strcmp(argv[i], "-default") || !strcmp(argv[i], "-d"))
786 			lang = lang_default;
787 		else if (!strcmp(argv[i], "-show") || !strcmp(argv[i], "-s"))
788 			show = 1;
789 		else if (!strcmp(argv[i], "-print") || !strcmp(argv[i], "-p"))
790 			print = 1;
791 		else if (!strcmp(argv[i], "-restore") ||
792 		    !strcmp(argv[i], "-r")) {
793 			vidcontrol(font_current);
794 			exit(0);
795 		} else if (!strcmp(argv[i], "-K"))
796 			dir = keymapdir;
797 		else if (!strcmp(argv[i], "-V"))
798 			dir = fontdir;
799 		else
800 			usage();
801 	}
802 }
803 
804 /*
805  * A front-end for the 'vidfont' and 'kbdmap' programs.
806  */
807 int
808 main(int argc, char **argv)
809 {
810 
811 	x11 = system("kbdcontrol -d >/dev/null");
812 
813 	if (x11) {
814 		fprintf(stderr, "You are not on a virtual console - "
815 				"expect certain strange side-effects\n");
816 		sleep(2);
817 	}
818 
819 	SLIST_INIT(&head);
820 
821 	lang = get_locale();
822 
823 	program = extract_name(argv[0]);
824 
825 	font_current = get_font();
826 	if (font_current == NULL)
827 		font_current = font_default;
828 
829 	if (strcmp(program, "kbdmap"))
830 		dir = fontdir;
831 	else
832 		dir = keymapdir;
833 
834 	/* Parse command line arguments */
835 	parse_args(argc, argv);
836 
837 	/* Read and display options */
838 	menu_read();
839 
840 	return 0;
841 }
842