예제 #1
0
파일: keymap.c 프로젝트: Lingvorn/udev
int main(int argc, char **argv)
{
	static const struct option options[] = {
		{ "help", no_argument, NULL, 'h' },
		{ "interactive", no_argument, NULL, 'i' },
		{}
	};
	int fd = -1;
	int opt_interactive = 0;
	int i;

	while (1) {
		int option;

		option = getopt_long(argc, argv, "hi", options, NULL);
		if (option == -1)
			break;

		switch (option) {
		case 'h':
		        help(0);

		case 'i':
			opt_interactive = 1;
			break;
		default:
			return 1;
		}
	}

	if (argc < optind+1)
		help (1);

	if ((fd = evdev_open(argv[optind])) < 0)
		return 3;

	/* one argument (device): dump or interactive */
	if (argc == optind+1) {
		if (opt_interactive)
			interactive(fd);
		else
			dump_table(fd);
		return 0;
	}

	/* two arguments (device, mapfile): set map file */
	if (argc == optind+2) {
		merge_table(fd, default_keymap_path(argv[optind+1]));
		return 0;
	}

	/* more arguments (device, scancode/keyname pairs): set keys directly */
	if ((argc - optind - 1) % 2 == 0) {
		for (i = optind+1; i < argc; i += 2)
			set_key(fd, argv[i], argv[i+1]);	
		return 0;
	}

	/* invalid number of arguments */
	help(1);
	return 1; /* not reached */
}
예제 #2
0
파일: keymap.c 프로젝트: RoadRunnr/systemd
int main(int argc, char **argv)
{
        static const struct option options[] = {
                { "help", no_argument, NULL, 'h' },
                { "interactive", no_argument, NULL, 'i' },
                {}
        };
        int fd = -1;
        int opt_interactive = 0;
        int i;

        while (1) {
                int option;

                option = getopt_long(argc, argv, "hi", options, NULL);
                if (option == -1)
                        break;

                switch (option) {
                case 'h':
                        help(0);

                case 'i':
                        opt_interactive = 1;
                        break;
                default:
                        return 1;
                }
        }

        if (argc < optind+1)
                help (1);

        if ((fd = evdev_open(argv[optind])) < 0)
                return 3;

        /* one argument (device): dump or interactive */
        if (argc == optind+1) {
                if (opt_interactive)
                        interactive(fd);
                else
                        dump_table(fd);
                return 0;
        }

        /* two arguments (device, mapfile): set map file */
        if (argc == optind+2) {
                const char *filearg = argv[optind+1];
                if (strchr(filearg, '/')) {
                        /* Keymap file argument is a path */
                        FILE *f = fopen(filearg, "re");
                        if (f)
                                merge_table(fd, f);
                        else
                                perror(filearg);
                } else {
                        /* Keymap file argument is a filename */
                        /* Open override file if present, otherwise default file */
                        char keymap_path[PATH_MAX];
                        FILE *f;

                        snprintf(keymap_path, sizeof(keymap_path), "/etc/udev/keymaps/%s", filearg);
                        f = fopen(keymap_path, "re");
                        if (f) {
                                merge_table(fd, f);
                        } else {
                                snprintf(keymap_path, sizeof(keymap_path), UDEVLIBEXECDIR "/keymaps/%s", filearg);
                                f = fopen(keymap_path, "re");
                                if (f)
                                        merge_table(fd, f);
                                else
                                        perror(keymap_path);
                        }
                }
                return 0;
        }

        /* more arguments (device, scancode/keyname pairs): set keys directly */
        if ((argc - optind - 1) % 2 == 0) {
                for (i = optind+1; i < argc; i += 2)
                        set_key(fd, argv[i], argv[i+1]);
                return 0;
        }

        /* invalid number of arguments */
        help(1);
        return 1; /* not reached */
}