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