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